Skip to content

Commit 060a07c

Browse files
committed
Merge pull request bnb-chain#974 from karalabe/downloader-fix-unknown-parent-attack
eth, eth/downloader: handle a potential unknown parent attack
2 parents 90b94e6 + fe87fec commit 060a07c

4 files changed

Lines changed: 92 additions & 38 deletions

File tree

‎eth/downloader/downloader.go‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,9 @@ func (d *Downloader) Synchronise(id string, hash common.Hash) error {
150150
return d.syncWithPeer(p, hash)
151151
}
152152

153-
// TakeBlocks takes blocks from the queue and yields them to the blockTaker handler
154-
// it's possible it yields no blocks
153+
// TakeBlocks takes blocks from the queue and yields them to the caller.
155154
func (d *Downloader) TakeBlocks() types.Blocks {
156-
// Check that there are blocks available and its parents are known
157-
head := d.queue.GetHeadBlock()
158-
if head == nil || !d.hasBlock(head.ParentHash()) {
159-
return nil
160-
}
161-
// Retrieve a full batch of blocks
162-
return d.queue.TakeBlocks(head)
155+
return d.queue.TakeBlocks()
163156
}
164157

165158
func (d *Downloader) Has(hash common.Hash) bool {

‎eth/downloader/downloader_test.go‎

Lines changed: 78 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import (
1010
"github.com/ethereum/go-ethereum/core/types"
1111
)
1212

13-
var knownHash = common.Hash{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
13+
var (
14+
knownHash = common.Hash{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
15+
unknownHash = common.Hash{9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9}
16+
)
1417

1518
func createHashes(start, amount int) (hashes []common.Hash) {
1619
hashes = make([]common.Hash, amount+1)
@@ -27,7 +30,7 @@ func createBlock(i int, prevHash, hash common.Hash) *types.Block {
2730
header := &types.Header{Number: big.NewInt(int64(i))}
2831
block := types.NewBlockWithHeader(header)
2932
block.HeaderHash = hash
30-
block.ParentHeaderHash = knownHash
33+
block.ParentHeaderHash = prevHash
3134
return block
3235
}
3336

@@ -42,17 +45,28 @@ func createBlocksFromHashes(hashes []common.Hash) map[common.Hash]*types.Block {
4245
}
4346

4447
type downloadTester struct {
45-
downloader *Downloader
46-
hashes []common.Hash
47-
blocks map[common.Hash]*types.Block
48+
downloader *Downloader
49+
50+
hashes []common.Hash // Chain of hashes simulating
51+
blocks map[common.Hash]*types.Block // Blocks associated with the hashes
52+
chain []common.Hash // Block-chain being constructed
53+
4854
t *testing.T
4955
pcount int
5056
done chan bool
5157
activePeerId string
5258
}
5359

5460
func newTester(t *testing.T, hashes []common.Hash, blocks map[common.Hash]*types.Block) *downloadTester {
55-
tester := &downloadTester{t: t, hashes: hashes, blocks: blocks, done: make(chan bool)}
61+
tester := &downloadTester{
62+
t: t,
63+
64+
hashes: hashes,
65+
blocks: blocks,
66+
chain: []common.Hash{knownHash},
67+
68+
done: make(chan bool),
69+
}
5670
downloader := New(tester.hasBlock, tester.getBlock)
5771
tester.downloader = downloader
5872

@@ -64,9 +78,17 @@ func (dl *downloadTester) sync(peerId string, hash common.Hash) error {
6478
return dl.downloader.Synchronise(peerId, hash)
6579
}
6680

81+
func (dl *downloadTester) insertBlocks(blocks types.Blocks) {
82+
for _, block := range blocks {
83+
dl.chain = append(dl.chain, block.Hash())
84+
}
85+
}
86+
6787
func (dl *downloadTester) hasBlock(hash common.Hash) bool {
68-
if knownHash == hash {
69-
return true
88+
for _, h := range dl.chain {
89+
if h == hash {
90+
return true
91+
}
7092
}
7193
return false
7294
}
@@ -175,10 +197,9 @@ func TestTaking(t *testing.T) {
175197
if err != nil {
176198
t.Error("download error", err)
177199
}
178-
179-
bs1 := tester.downloader.TakeBlocks()
180-
if len(bs1) != 1000 {
181-
t.Error("expected to take 1000, got", len(bs1))
200+
bs := tester.downloader.TakeBlocks()
201+
if len(bs) != targetBlocks {
202+
t.Error("retrieved block mismatch: have %v, want %v", len(bs), targetBlocks)
182203
}
183204
}
184205

@@ -248,17 +269,17 @@ func TestThrottling(t *testing.T) {
248269
done := make(chan struct{})
249270
took := []*types.Block{}
250271
go func() {
251-
for {
272+
for running := true; running; {
252273
select {
253274
case <-done:
254-
took = append(took, tester.downloader.TakeBlocks()...)
255-
done <- struct{}{}
256-
return
275+
running = false
257276
default:
258-
took = append(took, tester.downloader.TakeBlocks()...)
259277
time.Sleep(time.Millisecond)
260278
}
279+
// Take a batch of blocks and accumulate
280+
took = append(took, tester.downloader.TakeBlocks()...)
261281
}
282+
done <- struct{}{}
262283
}()
263284

264285
// Synchronise the two threads and verify
@@ -273,3 +294,43 @@ func TestThrottling(t *testing.T) {
273294
t.Fatalf("downloaded block mismatch: have %v, want %v", len(took), targetBlocks)
274295
}
275296
}
297+
298+
// Tests that if a peer returns an invalid chain with a block pointing to a non-
299+
// existing parent, it is correctly detected and handled.
300+
func TestNonExistingParentAttack(t *testing.T) {
301+
// Forge a single-link chain with a forged header
302+
hashes := createHashes(0, 1)
303+
blocks := createBlocksFromHashes(hashes)
304+
305+
forged := blocks[hashes[0]]
306+
forged.ParentHeaderHash = unknownHash
307+
308+
// Try and sync with the malicious node and check that it fails
309+
tester := newTester(t, hashes, blocks)
310+
tester.newPeer("attack", big.NewInt(10000), hashes[0])
311+
if err := tester.sync("attack", hashes[0]); err != nil {
312+
t.Fatalf("failed to synchronise blocks: %v", err)
313+
}
314+
bs := tester.downloader.TakeBlocks()
315+
if len(bs) != 1 {
316+
t.Fatalf("retrieved block mismatch: have %v, want %v", len(bs), 1)
317+
}
318+
if tester.hasBlock(bs[0].ParentHash()) {
319+
t.Fatalf("tester knows about the unknown hash")
320+
}
321+
tester.downloader.Cancel()
322+
323+
// Reconstruct a valid chain, and try to synchronize with it
324+
forged.ParentHeaderHash = knownHash
325+
tester.newPeer("valid", big.NewInt(20000), hashes[0])
326+
if err := tester.sync("valid", hashes[0]); err != nil {
327+
t.Fatalf("failed to synchronise blocks: %v", err)
328+
}
329+
bs = tester.downloader.TakeBlocks()
330+
if len(bs) != 1 {
331+
t.Fatalf("retrieved block mismatch: have %v, want %v", len(bs), 1)
332+
}
333+
if !tester.hasBlock(bs[0].ParentHash()) {
334+
t.Fatalf("tester doesn't know about the origin hash")
335+
}
336+
}

‎eth/downloader/queue.go‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,17 +172,11 @@ func (q *queue) GetBlock(hash common.Hash) *types.Block {
172172
}
173173

174174
// TakeBlocks retrieves and permanently removes a batch of blocks from the cache.
175-
// The head parameter is required to prevent a race condition where concurrent
176-
// takes may fail parent verifications.
177-
func (q *queue) TakeBlocks(head *types.Block) types.Blocks {
175+
func (q *queue) TakeBlocks() types.Blocks {
178176
q.lock.Lock()
179177
defer q.lock.Unlock()
180178

181-
// Short circuit if the head block's different
182-
if len(q.blockCache) == 0 || q.blockCache[0] != head {
183-
return nil
184-
}
185-
// Otherwise accumulate all available blocks
179+
// Accumulate all available blocks
186180
var blocks types.Blocks
187181
for _, block := range q.blockCache {
188182
if block == nil {

‎eth/sync.go‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package eth
22

33
import (
44
"math"
5+
"sync/atomic"
56
"time"
67

78
"github.com/ethereum/go-ethereum/eth/downloader"
@@ -14,6 +15,7 @@ import (
1415
func (pm *ProtocolManager) update() {
1516
forceSync := time.Tick(forceSyncCycle)
1617
blockProc := time.Tick(blockProcCycle)
18+
blockProcPend := int32(0)
1719

1820
for {
1921
select {
@@ -36,7 +38,12 @@ func (pm *ProtocolManager) update() {
3638
}
3739
case <-blockProc:
3840
// Try to pull some blocks from the downloaded
39-
go pm.processBlocks()
41+
if atomic.CompareAndSwapInt32(&blockProcPend, 0, 1) {
42+
go func() {
43+
pm.processBlocks()
44+
atomic.StoreInt32(&blockProcPend, 0)
45+
}()
46+
}
4047

4148
case <-pm.quitSync:
4249
return
@@ -52,7 +59,7 @@ func (pm *ProtocolManager) processBlocks() error {
5259
pm.wg.Add(1)
5360
defer pm.wg.Done()
5461

55-
// Take a batch of blocks (will return nil if a previous batch has not reached the chain yet)
62+
// Short circuit if no blocks are available for insertion
5663
blocks := pm.downloader.TakeBlocks()
5764
if len(blocks) == 0 {
5865
return nil
@@ -63,9 +70,8 @@ func (pm *ProtocolManager) processBlocks() error {
6370
max := int(math.Min(float64(len(blocks)), float64(blockProcAmount)))
6471
_, err := pm.chainman.InsertChain(blocks[:max])
6572
if err != nil {
66-
// cancel download process
73+
glog.V(logger.Warn).Infof("Block insertion failed: %v", err)
6774
pm.downloader.Cancel()
68-
6975
return err
7076
}
7177
blocks = blocks[max:]

0 commit comments

Comments
 (0)