mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-07-03 12:11:52 +02:00
Require callers of AcceptBlockHeader() to perform anti-dos checks
In order to prevent memory DoS, we must ensure that we don't accept a new header into memory until we've performed anti-DoS checks, such as verifying that the header is part of a sufficiently high work chain. This commit adds a new argument to AcceptBlockHeader() so that we can ensure that all call-sites which might cause a new header to be accepted into memory have to grapple with the question of whether the header is safe to accept, or needs further validation. This patch also fixes two places where low-difficulty-headers could have been processed without such validation (processing an unrequested block from the network, and processing a compact block). Credit to Niklas Gögge for noticing this issue, and thanks to Sjors Provoost for test code.
This commit is contained in:
@ -1297,7 +1297,7 @@ class FullBlockTest(BitcoinTestFramework):
|
||||
blocks2 = []
|
||||
for i in range(89, LARGE_REORG_SIZE + 89):
|
||||
blocks2.append(self.next_block("alt" + str(i)))
|
||||
self.send_blocks(blocks2, False, force_send=True)
|
||||
self.send_blocks(blocks2, False, force_send=False)
|
||||
|
||||
# extend alt chain to trigger re-org
|
||||
block = self.next_block("alt" + str(chain1_tip + 1))
|
||||
|
@ -615,6 +615,27 @@ class CompactBlocksTest(BitcoinTestFramework):
|
||||
bad_peer.send_message(msg)
|
||||
bad_peer.wait_for_disconnect()
|
||||
|
||||
def test_low_work_compactblocks(self, test_node):
|
||||
# A compactblock with insufficient work won't get its header included
|
||||
node = self.nodes[0]
|
||||
hashPrevBlock = int(node.getblockhash(node.getblockcount() - 150), 16)
|
||||
block = self.build_block_on_tip(node)
|
||||
block.hashPrevBlock = hashPrevBlock
|
||||
block.solve()
|
||||
|
||||
comp_block = HeaderAndShortIDs()
|
||||
comp_block.initialize_from_block(block)
|
||||
with self.nodes[0].assert_debug_log(['[net] Ignoring low-work compact block from peer 0']):
|
||||
test_node.send_and_ping(msg_cmpctblock(comp_block.to_p2p()))
|
||||
|
||||
tips = node.getchaintips()
|
||||
found = False
|
||||
for x in tips:
|
||||
if x["hash"] == block.hash:
|
||||
found = True
|
||||
break
|
||||
assert not found
|
||||
|
||||
def test_compactblocks_not_at_tip(self, test_node):
|
||||
node = self.nodes[0]
|
||||
# Test that requesting old compactblocks doesn't work.
|
||||
@ -833,6 +854,9 @@ class CompactBlocksTest(BitcoinTestFramework):
|
||||
self.log.info("Testing compactblock requests/announcements not at chain tip...")
|
||||
self.test_compactblocks_not_at_tip(self.segwit_node)
|
||||
|
||||
self.log.info("Testing handling of low-work compact blocks...")
|
||||
self.test_low_work_compactblocks(self.segwit_node)
|
||||
|
||||
self.log.info("Testing handling of incorrect blocktxn responses...")
|
||||
self.test_incorrect_blocktxn_response(self.segwit_node)
|
||||
|
||||
|
@ -72,6 +72,13 @@ class AcceptBlockTest(BitcoinTestFramework):
|
||||
def setup_network(self):
|
||||
self.setup_nodes()
|
||||
|
||||
def check_hash_in_chaintips(self, node, blockhash):
|
||||
tips = node.getchaintips()
|
||||
for x in tips:
|
||||
if x["hash"] == blockhash:
|
||||
return True
|
||||
return False
|
||||
|
||||
def run_test(self):
|
||||
test_node = self.nodes[0].add_p2p_connection(P2PInterface())
|
||||
min_work_node = self.nodes[1].add_p2p_connection(P2PInterface())
|
||||
@ -89,10 +96,15 @@ class AcceptBlockTest(BitcoinTestFramework):
|
||||
blocks_h2[i].solve()
|
||||
block_time += 1
|
||||
test_node.send_and_ping(msg_block(blocks_h2[0]))
|
||||
min_work_node.send_and_ping(msg_block(blocks_h2[1]))
|
||||
|
||||
with self.nodes[1].assert_debug_log(expected_msgs=[f"AcceptBlockHeader: not adding new block header {blocks_h2[1].hash}, missing anti-dos proof-of-work validation"]):
|
||||
min_work_node.send_and_ping(msg_block(blocks_h2[1]))
|
||||
|
||||
assert_equal(self.nodes[0].getblockcount(), 2)
|
||||
assert_equal(self.nodes[1].getblockcount(), 1)
|
||||
|
||||
# Ensure that the header of the second block was also not accepted by node1
|
||||
assert_equal(self.check_hash_in_chaintips(self.nodes[1], blocks_h2[1].hash), False)
|
||||
self.log.info("First height 2 block accepted by node0; correctly rejected by node1")
|
||||
|
||||
# 3. Send another block that builds on genesis.
|
||||
|
@ -452,8 +452,9 @@ class BlockchainTest(BitcoinTestFramework):
|
||||
# (Previously this was broken based on setting
|
||||
# `rpc/blockchain.cpp:latestblock` incorrectly.)
|
||||
#
|
||||
b20hash = node.getblockhash(20)
|
||||
b20 = node.getblock(b20hash)
|
||||
fork_height = current_height - 100 # choose something vaguely near our tip
|
||||
fork_hash = node.getblockhash(fork_height)
|
||||
fork_block = node.getblock(fork_hash)
|
||||
|
||||
def solve_and_send_block(prevhash, height, time):
|
||||
b = create_block(prevhash, create_coinbase(height), time)
|
||||
@ -461,10 +462,10 @@ class BlockchainTest(BitcoinTestFramework):
|
||||
peer.send_and_ping(msg_block(b))
|
||||
return b
|
||||
|
||||
b21f = solve_and_send_block(int(b20hash, 16), 21, b20['time'] + 1)
|
||||
b22f = solve_and_send_block(b21f.sha256, 22, b21f.nTime + 1)
|
||||
b1 = solve_and_send_block(int(fork_hash, 16), fork_height+1, fork_block['time'] + 1)
|
||||
b2 = solve_and_send_block(b1.sha256, fork_height+2, b1.nTime + 1)
|
||||
|
||||
node.invalidateblock(b22f.hash)
|
||||
node.invalidateblock(b2.hash)
|
||||
|
||||
def assert_waitforheight(height, timeout=2):
|
||||
assert_equal(
|
||||
|
Reference in New Issue
Block a user