Skip to content

Commit 4c62ce8

Browse files
committed
Merge pull request #1451 from karalabe/handle-potential-TD-forge-attack
eth/downloader: drop peer if advertised TD but won't delvier
2 parents b041aed + 492d545 commit 4c62ce8

File tree

2 files changed

+44
-99
lines changed

2 files changed

+44
-99
lines changed

eth/downloader/downloader.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,8 @@ func (d *Downloader) fetchHashes(p *peer, from uint64) error {
804804
}
805805
// Start pulling hashes, until all are exhausted
806806
getHashes(from)
807+
gotHashes := false
808+
807809
for {
808810
select {
809811
case <-d.cancelCh:
@@ -825,8 +827,14 @@ func (d *Downloader) fetchHashes(p *peer, from uint64) error {
825827
case d.processCh <- false:
826828
case <-d.cancelCh:
827829
}
830+
// Error out if no hashes were retrieved at all
831+
if !gotHashes {
832+
return errStallingPeer
833+
}
828834
return nil
829835
}
836+
gotHashes = true
837+
830838
// Otherwise insert all the new hashes, aborting in case of junk
831839
glog.V(logger.Detail).Infof("%v: inserting %d hashes from #%d", p, len(hashPack.hashes), from)
832840

eth/downloader/downloader_test.go

Lines changed: 36 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ func TestSynchronisation60(t *testing.T) {
272272
// Tests that simple synchronization against a canonical chain works correctly.
273273
// In this test common ancestor lookup should be short circuited and not require
274274
// binary searching.
275-
func TestCanonicalSynchronisation(t *testing.T) {
275+
func TestCanonicalSynchronisation61(t *testing.T) {
276276
// Create a small enough block chain to download
277277
targetBlocks := blockCacheLimit - 15
278278
hashes, blocks := makeChain(targetBlocks, 0, genesis)
@@ -291,69 +291,16 @@ func TestCanonicalSynchronisation(t *testing.T) {
291291

292292
// Tests that if a large batch of blocks are being downloaded, it is throttled
293293
// until the cached blocks are retrieved.
294-
func TestThrottling60(t *testing.T) {
295-
// Create a long block chain to download and the tester
296-
targetBlocks := 8 * blockCacheLimit
297-
hashes, blocks := makeChain(targetBlocks, 0, genesis)
298-
299-
tester := newTester()
300-
tester.newPeer("peer", eth60, hashes, blocks)
301-
302-
// Wrap the importer to allow stepping
303-
done := make(chan int)
304-
tester.downloader.insertChain = func(blocks types.Blocks) (int, error) {
305-
n, err := tester.insertChain(blocks)
306-
done <- n
307-
return n, err
308-
}
309-
// Start a synchronisation concurrently
310-
errc := make(chan error)
311-
go func() {
312-
errc <- tester.sync("peer")
313-
}()
314-
// Iteratively take some blocks, always checking the retrieval count
315-
for len(tester.ownBlocks) < targetBlocks+1 {
316-
// Wait a bit for sync to throttle itself
317-
var cached int
318-
for start := time.Now(); time.Since(start) < 3*time.Second; {
319-
time.Sleep(25 * time.Millisecond)
320-
321-
cached = len(tester.downloader.queue.blockPool)
322-
if cached == blockCacheLimit || len(tester.ownBlocks)+cached == targetBlocks+1 {
323-
break
324-
}
325-
}
326-
// Make sure we filled up the cache, then exhaust it
327-
time.Sleep(25 * time.Millisecond) // give it a chance to screw up
328-
if cached != blockCacheLimit && len(tester.ownBlocks)+cached < targetBlocks+1 {
329-
t.Fatalf("block count mismatch: have %v, want %v", cached, blockCacheLimit)
330-
}
331-
<-done // finish previous blocking import
332-
for cached > maxBlockProcess {
333-
cached -= <-done
334-
}
335-
time.Sleep(25 * time.Millisecond) // yield to the insertion
336-
}
337-
<-done // finish the last blocking import
294+
func TestThrottling60(t *testing.T) { testThrottling(t, eth60) }
295+
func TestThrottling61(t *testing.T) { testThrottling(t, eth61) }
338296

339-
// Check that we haven't pulled more blocks than available
340-
if len(tester.ownBlocks) > targetBlocks+1 {
341-
t.Fatalf("target block count mismatch: have %v, want %v", len(tester.ownBlocks), targetBlocks+1)
342-
}
343-
if err := <-errc; err != nil {
344-
t.Fatalf("block synchronization failed: %v", err)
345-
}
346-
}
347-
348-
// Tests that if a large batch of blocks are being downloaded, it is throttled
349-
// until the cached blocks are retrieved.
350-
func TestThrottling(t *testing.T) {
297+
func testThrottling(t *testing.T, protocol int) {
351298
// Create a long block chain to download and the tester
352299
targetBlocks := 8 * blockCacheLimit
353300
hashes, blocks := makeChain(targetBlocks, 0, genesis)
354301

355302
tester := newTester()
356-
tester.newPeer("peer", eth61, hashes, blocks)
303+
tester.newPeer("peer", protocol, hashes, blocks)
357304

358305
// Wrap the importer to allow stepping
359306
done := make(chan int)
@@ -404,7 +351,7 @@ func TestThrottling(t *testing.T) {
404351
// Tests that simple synchronization against a forked chain works correctly. In
405352
// this test common ancestor lookup should *not* be short circuited, and a full
406353
// binary search should be executed.
407-
func TestForkedSynchronisation(t *testing.T) {
354+
func TestForkedSynchronisation61(t *testing.T) {
408355
// Create a long enough forked chain
409356
common, fork := MaxHashFetch, 2*MaxHashFetch
410357
hashesA, hashesB, blocksA, blocksB := makeChainFork(common+fork, fork, genesis)
@@ -443,33 +390,10 @@ func TestInactiveDownloader(t *testing.T) {
443390
}
444391

445392
// Tests that a canceled download wipes all previously accumulated state.
446-
func TestCancel60(t *testing.T) {
447-
// Create a small enough block chain to download and the tester
448-
targetBlocks := blockCacheLimit - 15
449-
hashes, blocks := makeChain(targetBlocks, 0, genesis)
450-
451-
tester := newTester()
452-
tester.newPeer("peer", eth60, hashes, blocks)
393+
func TestCancel60(t *testing.T) { testCancel(t, eth60) }
394+
func TestCancel61(t *testing.T) { testCancel(t, eth61) }
453395

454-
// Make sure canceling works with a pristine downloader
455-
tester.downloader.cancel()
456-
hashCount, blockCount := tester.downloader.queue.Size()
457-
if hashCount > 0 || blockCount > 0 {
458-
t.Errorf("block or hash count mismatch: %d hashes, %d blocks, want 0", hashCount, blockCount)
459-
}
460-
// Synchronise with the peer, but cancel afterwards
461-
if err := tester.sync("peer"); err != nil {
462-
t.Fatalf("failed to synchronise blocks: %v", err)
463-
}
464-
tester.downloader.cancel()
465-
hashCount, blockCount = tester.downloader.queue.Size()
466-
if hashCount > 0 || blockCount > 0 {
467-
t.Errorf("block or hash count mismatch: %d hashes, %d blocks, want 0", hashCount, blockCount)
468-
}
469-
}
470-
471-
// Tests that a canceled download wipes all previously accumulated state.
472-
func TestCancel(t *testing.T) {
396+
func testCancel(t *testing.T, protocol int) {
473397
// Create a small enough block chain to download and the tester
474398
targetBlocks := blockCacheLimit - 15
475399
if targetBlocks >= MaxHashFetch {
@@ -478,7 +402,7 @@ func TestCancel(t *testing.T) {
478402
hashes, blocks := makeChain(targetBlocks, 0, genesis)
479403

480404
tester := newTester()
481-
tester.newPeer("peer", eth61, hashes, blocks)
405+
tester.newPeer("peer", protocol, hashes, blocks)
482406

483407
// Make sure canceling works with a pristine downloader
484408
tester.downloader.cancel()
@@ -498,7 +422,10 @@ func TestCancel(t *testing.T) {
498422
}
499423

500424
// Tests that synchronisation from multiple peers works as intended (multi thread sanity test).
501-
func TestMultiSynchronisation(t *testing.T) {
425+
func TestMultiSynchronisation60(t *testing.T) { testMultiSynchronisation(t, eth60) }
426+
func TestMultiSynchronisation61(t *testing.T) { testMultiSynchronisation(t, eth61) }
427+
428+
func testMultiSynchronisation(t *testing.T, protocol int) {
502429
// Create various peers with various parts of the chain
503430
targetPeers := 16
504431
targetBlocks := targetPeers*blockCacheLimit - 15
@@ -507,7 +434,7 @@ func TestMultiSynchronisation(t *testing.T) {
507434
tester := newTester()
508435
for i := 0; i < targetPeers; i++ {
509436
id := fmt.Sprintf("peer #%d", i)
510-
tester.newPeer(id, eth60, hashes[i*blockCacheLimit:], blocks)
437+
tester.newPeer(id, protocol, hashes[i*blockCacheLimit:], blocks)
511438
}
512439
// Synchronise with the middle peer and make sure half of the blocks were retrieved
513440
id := fmt.Sprintf("peer #%d", targetPeers/2)
@@ -528,7 +455,7 @@ func TestMultiSynchronisation(t *testing.T) {
528455

529456
// Tests that synchronising with a peer who's very slow at network IO does not
530457
// stall the other peers in the system.
531-
func TestSlowSynchronisation(t *testing.T) {
458+
func TestSlowSynchronisation60(t *testing.T) {
532459
tester := newTester()
533460

534461
// Create a batch of blocks, with a slow and a full speed peer
@@ -557,7 +484,7 @@ func TestSlowSynchronisation(t *testing.T) {
557484

558485
// Tests that if a peer returns an invalid chain with a block pointing to a non-
559486
// existing parent, it is correctly detected and handled.
560-
func TestNonExistingParentAttack(t *testing.T) {
487+
func TestNonExistingParentAttack60(t *testing.T) {
561488
tester := newTester()
562489

563490
// Forge a single-link chain with a forged header
@@ -587,7 +514,7 @@ func TestNonExistingParentAttack(t *testing.T) {
587514

588515
// Tests that if a malicious peers keeps sending us repeating hashes, we don't
589516
// loop indefinitely.
590-
func TestRepeatingHashAttack(t *testing.T) { // TODO: Is this thing valid??
517+
func TestRepeatingHashAttack60(t *testing.T) { // TODO: Is this thing valid??
591518
tester := newTester()
592519

593520
// Create a valid chain, but drop the last link
@@ -617,7 +544,7 @@ func TestRepeatingHashAttack(t *testing.T) { // TODO: Is this thing valid??
617544

618545
// Tests that if a malicious peers returns a non-existent block hash, it should
619546
// eventually time out and the sync reattempted.
620-
func TestNonExistingBlockAttack(t *testing.T) {
547+
func TestNonExistingBlockAttack60(t *testing.T) {
621548
tester := newTester()
622549

623550
// Create a valid chain, but forge the last link
@@ -639,7 +566,7 @@ func TestNonExistingBlockAttack(t *testing.T) {
639566

640567
// Tests that if a malicious peer is returning hashes in a weird order, that the
641568
// sync throttler doesn't choke on them waiting for the valid blocks.
642-
func TestInvalidHashOrderAttack(t *testing.T) {
569+
func TestInvalidHashOrderAttack60(t *testing.T) {
643570
tester := newTester()
644571

645572
// Create a valid long chain, but reverse some hashes within
@@ -667,7 +594,7 @@ func TestInvalidHashOrderAttack(t *testing.T) {
667594

668595
// Tests that if a malicious peer makes up a random hash chain and tries to push
669596
// indefinitely, it actually gets caught with it.
670-
func TestMadeupHashChainAttack(t *testing.T) {
597+
func TestMadeupHashChainAttack60(t *testing.T) {
671598
tester := newTester()
672599
blockSoftTTL = 100 * time.Millisecond
673600
crossCheckCycle = 25 * time.Millisecond
@@ -697,7 +624,7 @@ func TestMadeupHashChainAttack(t *testing.T) {
697624
// indefinitely, one hash at a time, it actually gets caught with it. The reason
698625
// this is separate from the classical made up chain attack is that sending hashes
699626
// one by one prevents reliable block/parent verification.
700-
func TestMadeupHashChainDrippingAttack(t *testing.T) {
627+
func TestMadeupHashChainDrippingAttack60(t *testing.T) {
701628
// Create a random chain of hashes to drip
702629
randomHashes := make([]common.Hash, 16*blockCacheLimit)
703630
for i := range randomHashes {
@@ -716,7 +643,7 @@ func TestMadeupHashChainDrippingAttack(t *testing.T) {
716643

717644
// Tests that if a malicious peer makes up a random block chain, and tried to
718645
// push indefinitely, it actually gets caught with it.
719-
func TestMadeupBlockChainAttack(t *testing.T) {
646+
func TestMadeupBlockChainAttack60(t *testing.T) {
720647
defaultBlockTTL := blockSoftTTL
721648
defaultCrossCheckCycle := crossCheckCycle
722649

@@ -748,7 +675,7 @@ func TestMadeupBlockChainAttack(t *testing.T) {
748675
// Tests that if one/multiple malicious peers try to feed a banned blockchain to
749676
// the downloader, it will not keep refetching the same chain indefinitely, but
750677
// gradually block pieces of it, until its head is also blocked.
751-
func TestBannedChainStarvationAttack(t *testing.T) {
678+
func TestBannedChainStarvationAttack60(t *testing.T) {
752679
n := 8 * blockCacheLimit
753680
fork := n/2 - 23
754681
hashes, forkHashes, blocks, forkBlocks := makeChainFork(n, fork, genesis)
@@ -792,7 +719,7 @@ func TestBannedChainStarvationAttack(t *testing.T) {
792719
// Tests that if a peer sends excessively many/large invalid chains that are
793720
// gradually banned, it will have an upper limit on the consumed memory and also
794721
// the origin bad hashes will not be evacuated.
795-
func TestBannedChainMemoryExhaustionAttack(t *testing.T) {
722+
func TestBannedChainMemoryExhaustionAttack60(t *testing.T) {
796723
// Construct a banned chain with more chunks than the ban limit
797724
n := 8 * blockCacheLimit
798725
fork := n/2 - 23
@@ -848,7 +775,7 @@ func TestBannedChainMemoryExhaustionAttack(t *testing.T) {
848775
// internal state problems
849776
//
850777
// No, don't delete this test, it actually did happen!
851-
func TestOverlappingDeliveryAttack(t *testing.T) {
778+
func TestOverlappingDeliveryAttack60(t *testing.T) {
852779
// Create an arbitrary batch of blocks ( < cache-size not to block)
853780
targetBlocks := blockCacheLimit - 23
854781
hashes, blocks := makeChain(targetBlocks, 0, genesis)
@@ -875,6 +802,16 @@ func TestOverlappingDeliveryAttack(t *testing.T) {
875802
}
876803
}
877804

805+
// Tests that a peer advertising an high TD doesn't get to stall the downloader
806+
// afterwards by not sending any useful hashes.
807+
func TestHighTDStarvationAttack61(t *testing.T) {
808+
tester := newTester()
809+
tester.newPeer("attack", eth61, []common.Hash{genesis.Hash()}, nil)
810+
if err := tester.sync("attack"); err != errStallingPeer {
811+
t.Fatalf("synchronisation error mismatch: have %v, want %v", err, errStallingPeer)
812+
}
813+
}
814+
878815
// Tests that misbehaving peers are disconnected, whilst behaving ones are not.
879816
func TestHashAttackerDropping(t *testing.T) {
880817
// Define the disconnection requirement for individual hash fetch errors

0 commit comments

Comments
 (0)