mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 10:44:38 +00:00
net: reject oversized locators before allocating #35936
No reviewers
Labels
No labels
Android
Backport
Block storage
Brainstorming
Bug
Build system
CI failed
Consensus
Data corruption
Descriptors
Docs
DrahtBot Guix build requested
Feature
Fuzzing
GUI
Good First Review
IPC
Insufficient Review
Kernel
Linux/Unix
Mempool
Mining
Needs Backport (31.x)
Needs Backport (32.x)
Needs Benchmark
Needs Conceptual Review
Needs backport (30.x)
Needs rebase
Needs release note
Net
P2P
PSBT
Privacy
Private Broadcast
Questions and Help
RPC/REST/ZMQ
Refactoring
Resource usage
Review club
Scripts and tools
Settings
TX fees and policy
Tests
Tracking Issue
UTXO Db and Indexes
Up for grabs
Upstream
Utils/log/libs
Validation
Waiting for author
Wallet
Windows
good first issue
interfaces
macOS
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
bitcoin/bitcoin!35936
Loading…
Reference in a new issue
No description provided.
Delete branch "l0rinc/reject-oversized-locators"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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.
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
External sites
Reviews
See the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste
<!--meta-tag:bot-skip-->into the comment that the bot should ignore.Conflicts
Reviewers, 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.
🚧 At least one of the CI tasks failed.
Task
iwyu: https://github.com/bitcoin/bitcoin/actions/runs/31227149534/job/93023654808LLM reason (✨ experimental): CI failed because IWYU reported a headers/include issue (generated “Failure generated from IWYU” and exited non-zero on
src/primitives/block.h).Hints
Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:
Possibly 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.
A sanitizer issue, which can only be found by compiling with the sanitizer and running the
affected test.
An intermittent issue.
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
MAX_SIZE.LIMITED_VECTORonly sees counts thatReadCompactSizeaccepts, andReadCompactSizerange-checks againstMAX_SIZEfirst, throwing a plainstd::ios_base::failure, so a locator advertising more than 33554432 hashes reaches the generic handler inProcessMessages. I ran your test withCOMPACTSIZE(uint64_t{MAX_SIZE} + 1)in place ofCOMPACTSIZE(MAX_SIZE / sizeof(uint256)):getblocksandgetheadersboth end withfDisconnectfalse on 725bf357d9ef652eacd809a1bf2f6f79ec73a0b4. Both counts are the same 5 bytes on the wire.Reading the count without the range check makes
Limitthe only bound, and that case disconnects too:m_sizehas to widen touint64_twith it, sincesize_ttruncates on the 32-bit builds. With that applied the unit suite,p2p_invalid_locator.pyandp2p_invalid_messages.pypass.Reverting only
src/net_processing.cppto 1f53cab and keeping the new test makesnet_tests/oversized_locator_handlingfail onnode.fDisconnectfor both message types.I have tested the code on your head merged onto master
128456b.Is the case above
MAX_SIZEout of scope here on purpose?Untested, but looks fine to me.
I think
s.ignore(4);would be better; we don't doignore(sizeof(..))anywhere else, as far as I can see, and it just seems to be added indirection.I wanted to ignore adding a code comment here explaining the meaning of the magic constant, which
DUMMY_VERSIONalready does.We're basically skipping over the version field which "ignore size of version" documents with code nicely.
Let me know if you feel strongly about it and I'll change it, but I did this deliberately.
ACK 725bf357d9ef652eacd809a1bf2f6f79ec73a0b4
tACK 725bf357d9ef652eacd809a1bf2f6f79ec73a0b4
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
CBlockLocatorthe and serialization code), instead of the existing HEADERS approach that does it locally innet_processing? Should we use one of the two approaches in multiple places for consistency?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.
HEADERSprocessing 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 theCBlockHeaderstructure, but is deserialized as essentially a vector of pairs ofCBlockHeaderand an ignored compactsize value.The difference would be something like:
vs
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_VECTORdirectly in theCBlockLocatorserialization function would be slightly simpler than introducing theLimitedReadfunction, making the non-exceptional path justtry { stream >> locator >> hash_stop; return true; }. Would probably change fuzz test behaviour though.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!
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.
Earlier concerns neither republished nor resolved: 1 suggestion. See the Run archive linked from the Review report for their assessments.
🔵 Suggestion ·
limited_vectortests do not check allocation-free rejectionAlthough the formatter currently rejects before
reserve(), its new tests can still pass if that allocation moves above the size checks.Location:
src/test/serialize_tests.cpp:329(head).Review coverage was incomplete.
Reviewed:
23293a4d8a00· Review report · Previous reportspermalink
Rebased and simplified a bit more. Oversized
getblocksandgetheaderslocators now disconnect before allocation, including counts aboveMAX_SIZE, while unrelated deserialization failures keep their existing behavior. The limit handling stays local to the shared locator reader, preservesLIMITED_VECTOR’s global cap, and avoids the extraCBlockLocatorAPI.