Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

p2p: Don't process mutated blocks #29412

Merged
merged 9 commits into from Feb 28, 2024
Merged

Conversation

dergoegge
Copy link
Member

This PR proposes to check for mutated blocks early as a defense-in-depth mitigation against attacks leveraging mutated blocks.

We introduce IsBlockMutated which catches all known forms of block malleation and use it to do an early mutation check whenever we receive a block message.

We have observed attacks that abused mutated blocks in the past, which could have been prevented by simply not processing mutated blocks (e.g. #27608 for which a regression test is included in this PR).

@DrahtBot
Copy link
Contributor

DrahtBot commented Feb 8, 2024

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Code Coverage

For detailed information about the code coverage, see the test coverage report.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK maflcko, fjahr, sr-gi, achow101
Concept ACK TheCharlatan, epiccurious, instagibbs, glozow, naumenkogs

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

No conflicts as of last run.

@DrahtBot
Copy link
Contributor

DrahtBot commented Feb 8, 2024

🚧 At least one of the CI tasks failed. Make sure to run all tests locally, according to the
documentation.

Possibly this is due to a silent merge conflict (the changes in this pull request being
incompatible with the current code in the target branch). If so, make sure to rebase on the latest
commit of the target branch.

Leave a comment here, if you need help tracking down a confusing failure.

Debug: https://github.com/bitcoin/bitcoin/runs/21374346056

@DrahtBot DrahtBot removed the CI failed label Feb 8, 2024
@epiccurious
Copy link

How do you define a mutated block? What are the known forms of mutated blocks?

@dergoegge
Copy link
Member Author

How do you define a mutated block? What are the known forms of mutated blocks?

Looking at IsBlockMutated in this PR should provide the answers for these questions, but to recap:

@TheCharlatan
Copy link
Contributor

Concept ACK

@epiccurious
Copy link

Concept ACK.

@instagibbs
Copy link
Member

concept ACK

might be good to recap why it was only added in BLOCK processing but not other ProcessBlocks: In every other case we already don't punish the sender of compact blocks for failing these higher level checks, while full blocks allow for punishment.

@dergoegge
Copy link
Member Author

might be good to recap why it was only added in BLOCK processing but not other ProcessBlocks: In every other case we already don't punish the sender of compact blocks for failing these higher level checks, while full blocks allow for punishment.

We call ProcessBlock when we receive a block message (handled in this PR) or as the result of a compact block reconstruction. Compact blocks are relayed before full validation occurs, therefore we don't punish peers for sending us invalid blocks through compact block relay. Block mutation might also occur randomly during compact bock relay due to short-id collisions, which is another reason not to punish.

Copy link
Member

@sr-gi sr-gi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK

src/validation.cpp Outdated Show resolved Hide resolved
src/validation.cpp Outdated Show resolved Hide resolved
src/net_processing.cpp Show resolved Hide resolved
test/functional/p2p_mutated_blocks.py Outdated Show resolved Hide resolved
Copy link
Member

@glozow glozow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

concept ACK, I agree with the approach of catching and dropping these earlier rather than later

@dergoegge
Copy link
Member Author

I would like to see this in v27, can we add it to the milestone?

@maflcko maflcko added this to the 27.0 milestone Feb 20, 2024
Copy link
Contributor

@fjahr fjahr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK

"CBlock::GetHash() is a foot-gun without a prior mutation check", I hadn't felt this strongly about but I get it. I think it makes sense to rename it. I have drafted it here: fjahr@8e11e9c If it sounds valuable I will open a PR.

src/validation.cpp Outdated Show resolved Hide resolved
src/validation.cpp Show resolved Hide resolved
@naumenkogs
Copy link
Member

Concept ACK. Looking forward to addressing already pending comments, then i review.

src/test/validation_tests.cpp Outdated Show resolved Hide resolved
@@ -3758,6 +3814,40 @@ bool HasValidProofOfWork(const std::vector<CBlockHeader>& headers, const Consens
[&](const auto& header) { return CheckProofOfWork(header.GetHash(), header.nBits, consensusParams);});
}

bool IsBlockMutated(const CBlock& block, bool check_witness_root)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have you thought about having this function return an optional error string so unit tests can check expected failure reason?

Copy link
Member

@maflcko maflcko Feb 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(reply to #29412 (comment))

note: unit tests may use the ASSERT_DEBUG_LOG, as an alternative.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have you thought about having this function return an optional error string so unit tests can check expected failure reason?

I'm considering returning the mutation type but I will not be asserting logs...

src/validation.cpp Outdated Show resolved Hide resolved
src/validation.cpp Outdated Show resolved Hide resolved
src/validation.cpp Outdated Show resolved Hide resolved
test/functional/p2p_mutated_blocks.py Outdated Show resolved Hide resolved
get_block_txn = honest_relayer.last_message['getblocktxn']
return get_block_txn.block_txn_request.blockhash == block.sha256 and \
get_block_txn.block_txn_request.indexes == [1]
honest_relayer.wait_until(self_transfer_requested, timeout=5)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in e2d1eb2
does this need a with p2p_lock?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it? None of the other wait_for_* helpers require it.

@0xB10C
Copy link
Contributor

0xB10C commented Feb 23, 2024

I'm running this PR on my mainnet monitoring infrastructure as I was looking at the currently broadcast mutated blocks (bad-witness-nonce-size) in detail anyway. Can report back in a few days (currently, I receive only 1 or 2 per week).

@0xB10C
Copy link
Contributor

0xB10C commented Feb 23, 2024

Received two bad-witness-nonce-size blocks from a mining pools custom client shortly after my last comment. In both cases I my node had reconstructed a cmpctblock two seconds before the mutated block arrived. In this case, the block is "mutated" because the coinbase witness is empty. That's probably a bug on the mining pool side. I have the IPs of the pool clients as noban-peers, so I didn't actually see them getting disconnected.

2024-02-23T10:33:55Z [msghand] [blockencodings.cpp:217] [FillBlock] [cmpctblock] Successfully reconstructed block 000000000000000000017f8957106aae020ce65f69f3ee189559d97fc0f974bb with 1 txn prefilled, 2936 txn from mempool (incl at least 5 from extra pool) and 45 txn requested
2024-02-23T10:33:55Z [msghand] [validationinterface.cpp:273] [NewPoWValidBlock] [validation] NewPoWValidBlock: block hash=000000000000000000017f8957106aae020ce65f69f3ee189559d97fc0f974bb
2024-02-23T10:33:55Z [msghand] [validationinterface.cpp:267] [BlockChecked] [validation] BlockChecked: block hash=000000000000000000017f8957106aae020ce65f69f3ee189559d97fc0f974bb state=Valid
2024-02-23T10:33:55Z [msghand] [validationinterface.cpp:243] [MempoolTransactionsRemovedForBlock] [validation] Enqueuing MempoolTransactionsRemovedForBlock: block height=831673 txs removed=2931
2024-02-23T10:33:55Z [msghand] [validation.cpp:2738] [UpdateTipLog] UpdateTip: new best=000000000000000000017f8957106aae020ce65f69f3ee189559d97fc0f974bb height=831673 version=0x2bc70000 log2_work=94.750438 tx=968653267 date='2024-02-23T10:33:34Z' progress=1.000000 cache=87.0MiB(713889txo)

...

2024-02-23T10:33:57Z [msghand] [net_processing.cpp:3396] [ProcessMessage] [net] received: block (1519889 bytes) peer=153
2024-02-23T10:33:57Z [msghand] [net_processing.cpp:4728] [ProcessMessage] [net] received block 000000000000000000017f8957106aae020ce65f69f3ee189559d97fc0f974bb peer=153
2024-02-23T10:33:57Z [msghand] [validation.cpp:3862] [IsBlockMutated] [validation] IsBlockMutated: bad-witness-nonce-size, CheckWitnessCommitment : invalid witness reserved value size
2024-02-23T10:33:57Z [msghand] [net_processing.cpp:4734] [ProcessMessage] [net] Received mutated block from peer=153
2024-02-23T10:33:57Z [msghand] [net_processing.cpp:1791] [Misbehaving] [net] Misbehaving: peer=153 (0 -> 100) DISCOURAGE THRESHOLD EXCEEDED: mutated block
2024-02-23T10:33:57Z [msghand] [net_processing.cpp:5037] [MaybeDiscourageAndDisconnect] Warning: not punishing noban peer 153!
2024-02-23T11:17:32Z [msghand] [blockencodings.cpp:217] [FillBlock] [cmpctblock] Successfully reconstructed block 0000000000000000000345e15958b691f5079b84a6d5a3f959a8d84ee7989659 with 1 txn prefilled, 1024 txn from mempool (incl at least 0 from extra pool) and 5 txn requested
2024-02-23T11:17:32Z [msghand] [validationinterface.cpp:273] [NewPoWValidBlock] [validation] NewPoWValidBlock: block hash=0000000000000000000345e15958b691f5079b84a6d5a3f959a8d84ee7989659
2024-02-23T11:17:32Z [msghand] [validationinterface.cpp:267] [BlockChecked] [validation] BlockChecked: block hash=0000000000000000000345e15958b691f5079b84a6d5a3f959a8d84ee7989659 state=Valid
2024-02-23T11:17:32Z [msghand] [net.cpp:3784] [PushMessage] [net] sending sendcmpct (9 bytes) peer=117
2024-02-23T11:17:32Z [msghand] [validationinterface.cpp:243] [MempoolTransactionsRemovedForBlock] [validation] Enqueuing MempoolTransactionsRemovedForBlock: block height=831677 txs removed=1024
2024-02-23T11:17:32Z [msghand] [validation.cpp:2738] [UpdateTipLog] UpdateTip: new best=0000000000000000000345e15958b691f5079b84a6d5a3f959a8d84ee7989659 height=831677 version=0x2fffe000 log2_work=94.750499 tx=968663963 date='2024-02-23T11:17:27Z' progress=1.000000 cache=78.7MiB(689008txo)

...

2024-02-23T11:17:34Z [msghand] [net_processing.cpp:3396] [ProcessMessage] [net] received: block (1509042 bytes) peer=20
2024-02-23T11:17:34Z [msghand] [net_processing.cpp:4728] [ProcessMessage] [net] received block 0000000000000000000345e15958b691f5079b84a6d5a3f959a8d84ee7989659 peer=20
2024-02-23T11:17:34Z [msghand] [validation.cpp:3862] [IsBlockMutated] [validation] IsBlockMutated: bad-witness-nonce-size, CheckWitnessCommitment : invalid witness reserved value size
2024-02-23T11:17:34Z [msghand] [net_processing.cpp:4734] [ProcessMessage] [net] Received mutated block from peer=20
2024-02-23T11:17:34Z [msghand] [net_processing.cpp:1791] [Misbehaving] [net] Misbehaving: peer=20 (0 -> 100) DISCOURAGE THRESHOLD EXCEEDED: mutated block
2024-02-23T11:17:34Z [msghand] [net_processing.cpp:5037] [MaybeDiscourageAndDisconnect] Warning: not punishing noban peer 20!
2024-02-23T11:17:34Z [msghand] [net_processing.cpp:3396] [ProcessMessage] [net] received: getdata (37 bytes) peer=20
2024-02-23T11:17:34Z [msghand] [net_processing.cpp:3998] [ProcessMessage] [net] received getdata (1 invsz) peer=20
2024-02-23T11:17:34Z [msghand] [net_processing.cpp:4001] [ProcessMessage] [net] received getdata for: witness-block 0000000000000000000345e15958b691f5079b84a6d5a3f959a8d84ee7989659 peer=20
2024-02-23T11:17:34Z [msghand] [net.cpp:3784] [PushMessage] [net] sending block (1509078 bytes) peer=20

Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left some initial nits/questions

0f356b0 💬

Show signature

Signature:

untrusted comment: signature from minisign secret key on empty file; verify via: minisign -Vm "${path_to_any_empty_file}" -P RWTRmVTMeKV5noAMqVlsMugDDCyyTSbA3Re5AkUrhvLVln0tSaFWglOw -x "${path_to_this_whole_four_line_signature_blob}"
RUTRmVTMeKV5npGrKx1nqXCw5zeVHdtdYURB/KlyA/LMFgpNCs+SkW9a8N95d+U4AP1RJMi+krxU1A3Yux4bpwZNLvVBKy0wLgM=
trusted comment: 0f356b0b2fb23aef96ed7396890aa36410aa1d59 💬
kBfK9UKECARIswfAVfqnH6nAuO9mA7Tcad0J1J6aCTeTkHT4VK7z3GpY/0Wz6H0SeVaxH2Thgu3Qvz87+QF6CA==

src/validation.cpp Outdated Show resolved Hide resolved
src/validation.cpp Outdated Show resolved Hide resolved
@@ -3758,6 +3814,40 @@ bool HasValidProofOfWork(const std::vector<CBlockHeader>& headers, const Consens
[&](const auto& header) { return CheckProofOfWork(header.GetHash(), header.nBits, consensusParams);});
}

bool IsBlockMutated(const CBlock& block, bool check_witness_root)
Copy link
Member

@maflcko maflcko Feb 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(reply to #29412 (comment))

note: unit tests may use the ASSERT_DEBUG_LOG, as an alternative.

src/validation.cpp Outdated Show resolved Hide resolved
src/test/validation_tests.cpp Show resolved Hide resolved
src/test/validation_tests.cpp Outdated Show resolved Hide resolved
src/test/validation_tests.cpp Show resolved Hide resolved

// The malleation check is ignored; as the transaction tree itself
// already does not permit it, it is impossible to trigger in the
// witness tree.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

future work nit: the mutated arg is never non-nullptr and has no test coverage it seems.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

future work nit: the mutated arg is never non-nullptr and has no test coverage it seems.

I presume the reason is that it can't be mutated and all callers are expected to pass nullptr? Seems fine to remove the arg, but should be fine either way.

fanquake added a commit that referenced this pull request Mar 4, 2024
…t 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 #29412 to revert some of the behavior change that was likely unintentional.

  Based on comments from #29412 (comment)

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

Tree-SHA512: be6204c8cc57b271d55c1d02a5c77d03a37738d91cb5ac164f483b0bab3991c24679c5ff02fbaa52bf37ee625874b63f4c9f7b39ad6fd5f3a25386567a0942e4
glozow pushed a commit to glozow/bitcoin that referenced this pull request Mar 5, 2024
glozow pushed a commit to glozow/bitcoin that referenced this pull request Mar 5, 2024
glozow pushed a commit to glozow/bitcoin that referenced this pull request Mar 5, 2024
glozow pushed a commit to glozow/bitcoin that referenced this pull request Mar 5, 2024
glozow pushed a commit to glozow/bitcoin that referenced this pull request Mar 5, 2024
We preemptively perform a block mutation check before further processing
a block message (similar to early sanity checks on other messsage
types). The main reasons for this change are as follows:

- `CBlock::GetHash()` is a foot-gun without a prior mutation check, as
  the hash returned only commits to the header but not to the actual
  transactions (`CBlock::vtx`) contained in the block.
- We have observed attacks that abused mutated blocks in the past, which
  could have been prevented by simply not processing mutated blocks
  (e.g. bitcoin#27608).

Github-Pull: bitcoin#29412
Rebased-From: 49257c0
glozow pushed a commit to glozow/bitcoin that referenced this pull request Mar 5, 2024
glozow pushed a commit to glozow/bitcoin that referenced this pull request Mar 5, 2024
Slight performance improvement by avoiding duplicate work.

Github-Pull: bitcoin#29412
Rebased-From: 1ec6bbe
glozow pushed a commit to glozow/bitcoin that referenced this pull request Mar 5, 2024
@glozow
Copy link
Member

glozow commented Mar 5, 2024

Backported for 26.x in #29509

@dergoegge
Copy link
Member Author

@achow101 can you add it to #29531 as well? (i think Gloria's 26.x version should be cleanly fit on top of 25.x)

achow101 pushed a commit to achow101/bitcoin that referenced this pull request Mar 5, 2024
achow101 pushed a commit to achow101/bitcoin that referenced this pull request Mar 5, 2024
achow101 pushed a commit to achow101/bitcoin that referenced this pull request Mar 5, 2024
achow101 pushed a commit to achow101/bitcoin that referenced this pull request Mar 5, 2024
We preemptively perform a block mutation check before further processing
a block message (similar to early sanity checks on other messsage
types). The main reasons for this change are as follows:

- `CBlock::GetHash()` is a foot-gun without a prior mutation check, as
  the hash returned only commits to the header but not to the actual
  transactions (`CBlock::vtx`) contained in the block.
- We have observed attacks that abused mutated blocks in the past, which
  could have been prevented by simply not processing mutated blocks
  (e.g. bitcoin#27608).

Github-Pull: bitcoin#29412
Rebased-From: 49257c0
achow101 pushed a commit to achow101/bitcoin that referenced this pull request Mar 5, 2024
achow101 pushed a commit to achow101/bitcoin that referenced this pull request Mar 5, 2024
Slight performance improvement by avoiding duplicate work.

Github-Pull: bitcoin#29412
Rebased-From: 1ec6bbe
achow101 pushed a commit to achow101/bitcoin that referenced this pull request Mar 5, 2024
@achow101 achow101 mentioned this pull request Mar 5, 2024
@achow101
Copy link
Member

achow101 commented Mar 5, 2024

Backported to 25.x in #29531

glozow added a commit that referenced this pull request Mar 11, 2024
c68d4d0 [doc] update manual pages for v26.1rc2 (glozow)
bd715bf [build] bump version to v26.1rc2 (glozow)
b6d006d update release notes 26.1 (glozow)
fce992b fuzz: restrict fopencookie usage to Linux & FreeBSD (fanquake)
40c56a4 test: make sure keypool sizes do not change on `getrawchangeaddress`/`getnewaddress` failures (UdjinM6)
7c82b27 wallet: Avoid updating `ReserveDestination::nIndex` when `GetReservedDestination` fails (UdjinM6)
b5419ce p2p: Don't consider blocks mutated if they don't connect to known prev block (Greg Sanders)
0535c25 [test] IsBlockMutated unit tests (dergoegge)
8141498 [validation] Cache merkle root and witness commitment checks (dergoegge)
0c5c596 [test] Add regression test for #27608 (dergoegge)
2473635 [net processing] Don't process mutated blocks (dergoegge)
50c0b61 [validation] Merkle root malleation should be caught by IsBlockMutated (dergoegge)
aff368f [validation] Introduce IsBlockMutated (dergoegge)
076c67c [refactor] Cleanup merkle root checks (dergoegge)
97a1d0a [validation] Isolate merkle root checks (dergoegge)
4ac0eb5 test: Drop `x` modifier in `fsbridge::fopen` call for mingw builds (Hennadii Stepanov)

Pull request description:

  Includes:
  - #29357
  - #29412
  - #29524
  - #29510
  - #29529

  Also does:
  - update to release notes
  - bump to rc2
  - manpages
  - (no changes to bitcoin.conf)

ACKs for top commit:
  achow101:
    ACK c68d4d0

Tree-SHA512: 2f8c3dd705e3f9f33403b3cc17e8006510ff827d7dbd609b09732a1669964e9b001cfecdc63d8d8daeb8f39c652e1e4ad0aac873d44d259c21803de85688ed2b
kevkevinpal pushed a commit to kevkevinpal/bitcoin that referenced this pull request Mar 13, 2024
An unnecessary check was added to the block mutation tests
in bitcoin#29412 where IsBlockMutated is returning true for the invalid
reasons: we try to check mutation via transaction duplication,
but the merkle root is not updated before the check, therefore
the check fails because the provided root and the computed root
differ, but not because the block contains the same transaction twice.

The check is meaningless so it can be removed.
kevkevinpal pushed a commit to kevkevinpal/bitcoin that referenced this pull request Mar 13, 2024
An unnecessary check was added to the block mutation tests
in bitcoin#29412 where IsBlockMutated is returning true for the invalid
reasons: we try to check mutation via transaction duplication,
but the merkle root is not updated before the check, therefore
the check fails because the provided root and the computed root
differ, but not because the block contains the same transaction twice.

The check is meaningless so it can be removed.
fanquake added a commit that referenced this pull request Mar 22, 2024
27cfda1 doc: Update release notes for 25.2rc2 (Ava Chow)
daba5e2 doc: Update manpages for 25.2rc2 (Ava Chow)
8a0c980 build: Bump to 25.2rc2 (Ava Chow)
cf7d3a8 p2p: Don't consider blocks mutated if they don't connect to known prev block (Greg Sanders)
3eaaafa [test] IsBlockMutated unit tests (dergoegge)
0667441 [validation] Cache merkle root and witness commitment checks (dergoegge)
de97ecf [test] Add regression test for #27608 (dergoegge)
8cc4b24 [net processing] Don't process mutated blocks (dergoegge)
098f07d [validation] Merkle root malleation should be caught by IsBlockMutated (dergoegge)
8804c36 [validation] Introduce IsBlockMutated (dergoegge)
4f5baac [validation] Isolate merkle root checks (dergoegge)
f93be01 test: make sure keypool sizes do not change on `getrawchangeaddress`/`getnewaddress` failures (UdjinM6)
7c08ccf wallet: Avoid updating `ReserveDestination::nIndex` when `GetReservedDestination` fails (UdjinM6)

Pull request description:

  Backport:

  * #29510
  * #29412
  * #29524

ACKs for top commit:
  glozow:
    utACK 27cfda1

Tree-SHA512: 37feadd65d9ea55c0a92c9d2a6f74f87cafed3bc67f8deeaaafc5b7042f954e55ea34816612e1a49088f4f1906f104e00c7c3bec7affd1c1f48220b57a8769c5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet