Skip to content

csdb: bound Pool parsing by the bytes received (hashing length, element counts) - #85

Open
EnzinoBB wants to merge 2 commits into
CREDITSCOM:masterfrom
CreditsCommunity:official/pool-parse-bounds
Open

EnzinoBB wants to merge 2 commits into
CREDITSCOM:masterfrom
CreditsCommunity:official/pool-parse-bounds

Conversation

@EnzinoBB

@EnzinoBB EnzinoBB commented Nov 1, 2026

Copy link
Copy Markdown

Why this PR

Two fields of a serialized block are trusted before they are checked against the data actually received. A block from a peer (a block reply while syncing) can therefore make the node read past its buffer when the block hash is computed, or reserve memory for far more transactions or new wallets than the data can hold, which ends the process with an uncaught exception. Found while auditing the node and reported to the Credits team.

What changes

csdb/src/pool.cpp:

  • Pool::priv::hashingLengthFits(): Pool::from_binary(), Pool::hash_from_binary() and Pool::from_lz4_byte_stream() reject data whose hashing length exceeds its size, with the result of any other parse error (an invalid Pool, an empty PoolHash). The check cannot live in get_hashed_data(), where the stream size is the unread remainder.
  • getTransactions() and getNewWallets() reserve at most as many elements as bytes remain to parse (every element takes at least one byte). The parse loop already fails at the first missing element.

Effect on the network

None for honest traffic: the hashing length of a valid pool is the size of its own serialized prefix, and for valid data the reserve is exactly what it was. Every valid block parses and hashes as before; only malformed blocks are refused.

Tests

tests/csnode/pool_tests.cpp: Pool.RejectsHashingLengthPastData (crashed before), Pool.AcceptsSerializedPool, Pool.HugeTransactionsCountDoesNotAllocate and Pool.HugeNewWalletsCountDoesNotAllocate (threw before). Built and run on 4a95aaa (GCC 13).

Same change in the community fork: CreditsCommunity#5 and #7, running on mainnet community nodes.

EnzinoBB and others added 2 commits November 1, 2026 12:15
Pool::from_binary(), Pool::hash_from_binary() and
Pool::from_lz4_byte_stream() read hashingLength_ from the bytes being parsed,
then updateHash() hashes hashingLength_ bytes of them. Nothing checked that
the length fits, so a peer-supplied block (e.g. a BlockReply while syncing)
with a large hashing length makes the node read past the buffer: SIGSEGV,
or a hash over unrelated memory.

Reject such data in the three entry points that know the full buffer size,
with the same result as any other parse error: an invalid Pool or an empty
PoolHash.

A legitimate pool cannot be affected: hashingLength_ is, by construction, the
size of the pool's own serialized prefix (Pool::priv::put), so it is always
smaller than the pool's data.

Note: the check cannot live in get_hashed_data(), because ibstream::size()
is the number of bytes still unread, not the total size. A first attempt
there rejected valid pools, and the PoolCache serialization tests caught it.

Tests (tests/csnode/pool_tests.cpp):
- Pool.RejectsHashingLengthPastData: before the fix it crashes with SIGSEGV.
- Pool.AcceptsSerializedPool: guards against rejecting valid pools.
nodetests: 112/112 pass.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Pool::priv::getTransactions() and getNewWallets() reserve() as many elements
as the uint32 count read from the data. A peer-supplied block (e.g. a
BlockReply while syncing) with a count near 2^32 asks for tens of GB:
std::bad_alloc/length_error escapes the transport thread and the node
terminates.

Reserve at most the number of bytes still unread, because every element
takes at least one of them. For valid data this reserves exactly what it did
before. For forged data the parse loop fails at the first missing element, as
it already did, and the pool is invalid.

The other counts in Pool parsing are uint8, so they are left as they are.

Tests (tests/csnode/pool_tests.cpp):
- Pool.HugeTransactionsCountDoesNotAllocate: before the fix from_binary throws.
- Pool.HugeNewWalletsCountDoesNotAllocate: before the fix from_binary throws.
nodetests: 116/116 pass.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant