Skip to content

Commit

Permalink
Merge bitcoin/bitcoin#29524: p2p: Don't consider blocks mutated if th…
Browse files Browse the repository at this point in the history
…ey don't connect to known prev block

a1fbde0 p2p: Don't consider blocks mutated if they don't connect to known prev block (Greg Sanders)

Pull request description:

  Followup to bitcoin/bitcoin#29412 to revert some of the behavior change that was likely unintentional.

  Based on comments from bitcoin/bitcoin#29412 (comment)

ACKs for top commit:
  0xB10C:
    utACK a1fbde0
  dergoegge:
    Code review ACK a1fbde0
  Sjors:
    ACK a1fbde0
  sr-gi:
    tACK bitcoin/bitcoin@a1fbde0

Tree-SHA512: be6204c8cc57b271d55c1d02a5c77d03a37738d91cb5ac164f483b0bab3991c24679c5ff02fbaa52bf37ee625874b63f4c9f7b39ad6fd5f3a25386567a0942e4
  • Loading branch information
fanquake committed Mar 4, 2024
2 parents fce53f1 + a1fbde0 commit e60804f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4721,7 +4721,8 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,

const CBlockIndex* prev_block{WITH_LOCK(m_chainman.GetMutex(), return m_chainman.m_blockman.LookupBlockIndex(pblock->hashPrevBlock))};

if (IsBlockMutated(/*block=*/*pblock,
// Check for possible mutation if it connects to something we know so we can check for DEPLOYMENT_SEGWIT being active
if (prev_block && IsBlockMutated(/*block=*/*pblock,
/*check_witness_root=*/DeploymentActiveAfter(prev_block, m_chainman, Consensus::DEPLOYMENT_SEGWIT))) {
LogDebug(BCLog::NET, "Received mutated block from peer=%d\n", peer->m_id);
Misbehaving(*peer, 100, "mutated block");
Expand Down
22 changes: 21 additions & 1 deletion test/functional/p2p_mutated_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ class MutatedBlocksTest(BitcoinTestFramework):
def set_test_params(self):
self.setup_clean_chain = True
self.num_nodes = 1
self.extra_args = [
[
"-testactivationheight=segwit@1", # causes unconnected headers/blocks to not have segwit considered deployed
],
]

def run_test(self):
self.wallet = MiniWallet(self.nodes[0])
Expand Down Expand Up @@ -74,7 +79,7 @@ def self_transfer_requested():

# Attempt to clear the honest relayer's download request by sending the
# mutated block (as the attacker).
with self.nodes[0].assert_debug_log(expected_msgs=["bad-txnmrklroot, hashMerkleRoot mismatch"]):
with self.nodes[0].assert_debug_log(expected_msgs=["Block mutated: bad-txnmrklroot, hashMerkleRoot mismatch"]):
attacker.send_message(msg_block(mutated_block))
# Attacker should get disconnected for sending a mutated block
attacker.wait_for_disconnect(timeout=5)
Expand All @@ -92,5 +97,20 @@ def self_transfer_requested():
honest_relayer.send_and_ping(block_txn)
assert_equal(self.nodes[0].getbestblockhash(), block.hash)

# Check that unexpected-witness mutation check doesn't trigger on a header that doesn't connect to anything
assert_equal(len(self.nodes[0].getpeerinfo()), 1)
attacker = self.nodes[0].add_p2p_connection(P2PInterface())
block_missing_prev = copy.deepcopy(block)
block_missing_prev.hashPrevBlock = 123
block_missing_prev.solve()

# Attacker gets a DoS score of 10, not immediately disconnected, so we do it 10 times to get to 100
for _ in range(10):
assert_equal(len(self.nodes[0].getpeerinfo()), 2)
with self.nodes[0].assert_debug_log(expected_msgs=["AcceptBlock FAILED (prev-blk-not-found)"]):
attacker.send_message(msg_block(block_missing_prev))
attacker.wait_for_disconnect(timeout=5)


if __name__ == '__main__':
MutatedBlocksTest().main()

0 comments on commit e60804f

Please sign in to comment.