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 ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
a2901f1 to
725bf35
Compare
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
|
Approach ACK. The dedicated exception subtype is the right shape, and the truncated case it targets is fixed. The same message still leaves the peer connected once the count goes above Reading the count without the range check makes - size_t size = ReadCompactSize(s);
+ const uint64_t size{ReadCompactSize(s, /*range_check=*/false)};
if (size > Limit) {
throw LimitedVectorExceededError{size};
}
- v.reserve(size);
+ v.reserve(static_cast<size_t>(size));
Reverting only I have tested the code on your head merged onto master 128456b. Is the case above |
ajtowns
left a comment
There was a problem hiding this comment.
Untested, but looks fine to me.
|
ACK 725bf35 |
|
tACK 725bf35 |
| template <size_t Limit, typename Stream> | ||
| void LimitedRead(Stream& s) | ||
| { | ||
| s.ignore(sizeof(DUMMY_VERSION)); |
There was a problem hiding this comment.
I'm not very familiar with the serialization code, so I don't have a strong opinion - just a question:
Why did you choose this approach (having to touch CBlockLocator the and serialization code), instead of the existing HEADERS approach that does it locally in net_processing? Should we use one of the two approaches in multiple places for consistency?
There was a problem hiding this comment.
I think I'd have a slight preference towards using the same approach. Adding another exception, to what should just be normal control flow reads a bit clunky.
There was a problem hiding this comment.
HEADERS processing does it manually because there's a discrepancy between the serialize and deserialize formats -- it's serialized as a vector of cblocks with no transactions since sending the message predated the CBlockHeader structure, but is deserialized as essentially a vector of pairs of CBlockHeader and an ignored compactsize value.
The difference would be something like:
try {
locator.LimitedRead<MAX_LOCATOR_SZ>(stream);
stream >> hash_stop;
return true;
} catch (LimitedVectorExceededError& e) {
LogDebug(BCLog::NET, "%s locator size %u > %u, %s", msg_type, e.m_size, MAX_LOCATOR_SZ, node.DisconnectMsg());
node.fDisconnect = true;
return false;
}vs
stream.ignore(4); // dummy version
size_t loc_sz = ReadCompactSize(stream);
if (loc_sz > MAX_LOCATOR_SZ) {
LogDebug(BCLog::NET, "%s locator size %u > %u, %s", msg_type, e.m_size, MAX_LOCATOR_SZ, node.DisconnectMsg());
node.fDisconnect = true;
return false;
}
locator.vHave.reserve(loc_sz);
while (loc_sz-- > 0) {
locator.vHave.emplace_back();
stream >> locator.vHave.back();
}
return true;which seems worse to me. I don't think the "exception" overhead versus "normal control flow" is a loss here, but losing the encapsulation of dummy-version and vector deserialization is something of a loss.
Just using LIMITED_VECTOR directly in the CBlockLocator serialization function would be slightly simpler than introducing the LimitedRead function, making the non-exceptional path just try { stream >> locator >> hash_stop; return true; }. Would probably change fuzz test behaviour though.
There was a problem hiding this comment.
Thanks for the comments, as @ajtowns also mentioned checking the count locally means we can no longer use ordinary vector deserialization since we've consumed the size already and have to reproduce the element-reading loop in net_processing.
I'm also not a fan of exceptions for control flow, but this is arguably an exceptional case - but I don't think it really matters. Let me know if you have strong opinions and concrete alternative suggestions.
1389e2c to
e327916
Compare
l0rinc
left a comment
There was a problem hiding this comment.
Rebased and addressed @jeanpablojp's finding: counts above MAX_SIZE now also disconnect, with regression coverage alongside the original truncated-locator case. The count remains uint64_t through the limit check to avoid truncation on 32-bit builds. Thanks!
| template <size_t Limit, typename Stream> | ||
| void LimitedRead(Stream& s) | ||
| { | ||
| s.ignore(sizeof(DUMMY_VERSION)); |
There was a problem hiding this comment.
Thanks for the comments, as @ajtowns also mentioned checking the count locally means we can no longer use ordinary vector deserialization since we've consumed the size already and have to reproduce the element-reading loop in net_processing.
I'm also not a fan of exceptions for control flow, but this is arguably an exceptional case - but I don't think it really matters. Let me know if you have strong opinions and concrete alternative suggestions.
Record that truncated `getblocks` and `getheaders` locators leave the peer connected when their advertised count exceeds `MAX_LOCATOR_SZ` and generic vector deserialization fails. Also characterize the existing `MAX_SIZE` cap when `LIMITED_VECTOR` has a higher explicit limit.
`LIMITED_VECTOR` throws a generic stream failure when the advertised count exceeds its limit. Add a dedicated subtype so callers can catch that condition without handling unrelated deserialization errors. Check the formatter limit before the generic `MAX_SIZE` cap so callers get the dedicated error even for counts above `MAX_SIZE`. Retain the full `uint64_t` count until after both checks, preserving the reported count on 32-bit builds. Co-authored-by: Anthony Towns <[email protected]> Co-authored-by: JP <[email protected]>
Share locator deserialization and the existing `MAX_LOCATOR_SZ` check between `getblocks` and `getheaders`.
`getblocks` and `getheaders` apply `MAX_LOCATOR_SZ` only after deserializing locator hashes. A truncated locator with an oversized count fails generic vector deserialization before that check, leaving the peer connected. Use `LIMITED_VECTOR` in the shared locator reader to detect the oversized count before allocating hashes. The shared reader catches only `LimitedVectorExceededError` and disconnects the peer. Other deserialization failures keep following the generic non-disconnecting exception path. Complete oversized locators continue to disconnect without discouragement. `p2p_invalid_locator.py` verifies that both messages accept 101 hashes and disconnect at 102. Co-authored-by: Anthony Towns <[email protected]>
e327916 to
23293a4
Compare
|
Rebased and simplified a bit more. Oversized |

Problem:
getblocksandgetheadersenforceMAX_LOCATOR_SZonly after deserializing locator hashes.A truncated locator advertising an oversized count makes generic vector deserialization fail before the disconnect check, leaving the peer connected.
Fix: Read the advertised count before allocating hashes and disconnect when it exceeds
MAX_LOCATOR_SZ, catching only the size-limit error so other deserialization failures remain non-disconnecting.Complete oversized locators continue to disconnect without discouragement.