Repository navigation
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. External sites
ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste |
The v1 message parser in P2PConnection rejected the stream as soon as the first 4 bytes did not match the network magic. bitcoind's V1Transport::readHeader waits for the full 24-byte header before checking the magic. The v2 to v1 downgrade relies on bitcoind's behaviour: an outbound v2 connection is only retried with v1 (V2Transport::ShouldReconnectV1) if at least a v1 header's worth of bytes had been sent before the peer disconnected. If the node's first send reaches the test peer in small pieces, the Python peer could disconnect after just 4 bytes, the node would not retry with v1, and p2p_v2_encrypted.py would time out waiting for the reconnection. Match bitcoind by checking the magic only once the full header has been received.
|
Added a fix for |
b54ef80 to
be88a3b
Compare
|
crACK be88a3b Reviewed the logic; both changes correctly handle partial reads in the test framework:
|
…nder When P2PConnection is the v2 responder, _on_data_v2_handshake() used `not self.v2_state.sent_garbage` to decide whether it still had to send its ellswift key. The garbage length is random in [0, MAX_GARBAGE_LEN], so in about 1 in 4096 connections it is empty and the check stays true after the key has been sent. If the peer's 64-byte key then arrives in more than one piece, complete_handshake() asks for more data, and on the next read the responder runs respond_v2_handshake() again: it generates and sends a second key, and misaligns the received key bytes. Both sides then wait for a garbage terminator that never comes. With the test default peertimeout=999999999 nobody times out, and the test can hang until its own timeout. Check whether our ellswift key has been generated instead.
The node's 64-byte ellswift key can arrive in several chunks. In that case EncryptedP2PState.complete_handshake() returns (0, b"") to signal that more data is needed. The WrongGarbageTerminatorState override bit-flipped the returned bytes unconditionally, which crashed in random_bitflip() on empty input and dropped the connection. Return the "need more data" result unchanged before tampering with it.
be88a3b to
baafaf8
Compare
|
Added two more fixes. The issues surfaced here: #36724 (comment).
When If the peer's 64-byte key then arrives in more than one piece, The fix is to check whether our ellswift key has been generated instead.
This happens when the peer's ellswift key starts with bytes that match the v1 prefix (e.g. the first network magic byte, about 1 in 256 keys) and a read ends inside that matching part. The remaining key bytes are then misaligned, the shared secret is wrong, and the handshake stalls. The fix is to return only the number of bytes consumed in the current call. |
respond_v2_handshake() returned len(self.received_prefix) as the number of bytes consumed, and the caller trims recvbuf by that amount. But received_prefix accumulates across calls, so when the v1 prefix check spans more than one read, too many bytes are dropped from recvbuf. This happens when the peer's ellswift key starts with bytes that match the v1 prefix (e.g. the first network magic byte, about 1 in 256 keys) and a read ends inside that matching part. The remaining key bytes are then misaligned, the shared secret is wrong, and the handshake stalls. Return only the number of bytes consumed in the current call.
|
Tested ACK ba7dde0 Re-reviewed the two added fixes (updating my earlier crACK):
Ran |
|
Additional validation: The fix has been validated as well in a run with the reproducer, see #36724 (comment) |

Two test cases (
p2p_v2_encrypted.pyandp2p_v2_misbehaving.py) can fail in the -- improbable -- case when network reads happen to arrive in smaller chunks.Related to #36726 .
All networking code using sockets must not make assumption about the chunking of the incoming/outgoing data, and should be resilient against any chunking. Normally, the chunking of the sends of the writer dictate the chunking of the receives at the reader, but this is not guaranteed by IP/TCP, intermediate networking elements may change the packet structure. In test using sockets on the same host, this is not very likely, but still possible.
Issues:
p2p_v2_encrypted.py: The v1 message parser inP2PConnectionrejected the stream as soon as the first 4 bytes did not match the network magic. bitcoind'sV1Transport::readHeaderwaits for the full 24-byte header before checking the magic.The v2 to v1 downgrade relies on bitcoind's behaviour: an outbound v2 connection is only retried with v1 (
V2Transport::ShouldReconnectV1) if at least a v1 header's worth of bytes had been sent before the peer disconnected. If the node's first send reaches the test peer in small pieces, the Python peer could disconnect after just 4 bytes, the node would not retry with v1, and p2p_v2_encrypted.py would time out waiting for the reconnection.p2p_v2_misbehaving.py: Fails for similar root cause. In this test, when the 64-byte key could not be read in one call, the empty array is indexed out-of-bounds, leading to a crash.The issues were found -- and can be reproduced -- by using a reproducer patch, that forces all socket sends to use smaller chunking. For the run with the reproducer see #36724 .
The issues are test-code only, the non-test code is correct.
The fixes are:
p2p_v2_encrypted.py: Match bitcoind by checking the magic only once the full header has been received.p2p_v2_misbehaving.py: Handle the short-read case explicitly, return the data read so far.These fixes make the test more robust, even though a failure due to this issue in CI is very unlikely, but not impossible.