Skip to content

csnode: validate a CompressedRegion before decompressing it - #84

Open
EnzinoBB wants to merge 1 commit into
CREDITSCOM:masterfrom
CreditsCommunity:official/compressed-region
Open

EnzinoBB wants to merge 1 commit into
CREDITSCOM:masterfrom
CreditsCommunity:official/compressed-region

Conversation

@EnzinoBB

@EnzinoBB EnzinoBB commented Nov 1, 2026

Copy link
Copy Markdown

Why this PR

cs::Compressor::decompress() trusts the region it is given. In Node::getBlockReply the region and its declared binary size come from a peer's message, so a malformed block reply can make a syncing node attempt an allocation of an arbitrary size (an uncaught exception that ends the process) or read through a null pointer, before anything in it is verified. Found while auditing the node and reported to the Credits team.

What changes

csnode/include/csnode/compressor.hpp, Compressor::decompress() returns an empty result, as it already does when LZ4 fails, for:

  • an empty region;
  • a compressed region whose declared binary size exceeds INT_MAX (LZ4 works with int sizes) or 256 times the compressed size (LZ4 cannot expand data more than 255:1).

Callers already handle an empty result ("No pools found").

Effect on the network

None for honest traffic: no region produced by Compressor::compress() is affected, so every valid block reply decompresses exactly as before. Only malformed input is refused.

Tests

tests/csnode/compressor_tests.cpp: Compressor.RejectsBinarySizeLz4CannotProduce (threw std::bad_alloc before) and Compressor.RejectsEmptyRegion (crashed before). Built and run on 4a95aaa (GCC 13).

Same change in the community fork: CreditsCommunity#4, running on mainnet community nodes.

Compressor::decompress() trusts the region it is given. In
Node::getBlockReply that region, and its binary size, are read straight from
a peer's packet (operator>>(IDataStream&, CompressedRegion&)), so one packet
can take down a syncing node:

- binarySize is an arbitrary size_t. bytes.resize(binarySize) throws
  std::length_error/std::bad_alloc, nothing on the transport thread catches
  it, and the node terminates.
- An empty region (data() == nullptr, size() == 0) takes the "not compressed"
  branch with size = 0 - 1 and reads from nullptr + 1: SIGSEGV.

Reject both before touching memory and return T{}, as decompress() already
does when LZ4 fails. Callers already handle an empty result ("No pools
found").

- An empty region is rejected.
- A compressed region is rejected when binarySize exceeds INT_MAX (LZ4 works
  with int sizes) or 256 x the compressed size. LZ4 cannot expand data more
  than 255:1, so no region produced by compress() is affected.

Tests (tests/csnode/compressor_tests.cpp):
- Compressor.RejectsBinarySizeLz4CannotProduce: before the fix it throws
  std::bad_alloc.
- Compressor.RejectsEmptyRegion: before the fix it crashes with SIGSEGV.
nodetests: 112/112 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