Skip to content

Bound declared uncompressed length in framed stream chunks - #737

Open
iliasabk wants to merge 1 commit into
xerial:mainfrom
iliasabk:fix/framed-uncompressed-length-cap
Open

iliasabk wants to merge 1 commit into
xerial:mainfrom
iliasabk:fix/framed-uncompressed-length-cap

Conversation

@iliasabk

Copy link
Copy Markdown

Issue

Fixes #730 — unbounded allocation from an attacker-declared uncompressed length in SnappyFramedInputStream.ensureBuffer() (CWE-770, CVSS 5.3), reported by August829.

Root cause

ensureBuffer() reads the declared uncompressed length from the fully attacker-controlled Snappy length-prefix inside a compressed-data frame and sizes the direct ByteBuffer + byte[] decompression buffers with it — before the frame's CRC32C is verified. getFrameMetaData() bounds only the compressed chunk length (~16 MiB via the 3-byte chunk header); the declared uncompressed length had no ceiling at all, so a ~23-byte frame could drive a multi-GB allocation (OOM DoS of any process decompressing untrusted x-snappy-framed input).

Fix

Reject a declared uncompressed length above SnappyInputStream.MAX_CHUNK_SIZE (512 MiB) — the ceiling upstream already uses for the sibling block-format class since the CVE-2023-43642 fix. A frame declaring more than that can only be hostile: conforming x-snappy-framed writers emit at most 64 KiB uncompressed per chunk, and snappy-java's own SnappyFramedOutputStream hard-limits blockSize to MAX_BLOCK_SIZE.

Choosing the 512 MiB bound rather than the 64 KiB spec limit keeps the reader tolerant of non-conforming producers, matching the existing test suite (testLargerFrames_* writes single frames that decompress to 100–500 KiB).

Tests

New regression test testDeclaredUncompressedLengthExceedsMaxBlockSize crafts a framed stream whose Snappy block declares 1 GiB uncompressed followed by a single literal byte; pre-fix it allocates ~1 GiB and fails on checksum, post-fix it throws SnappyIOException(INVALID_CHUNK_SIZE) before any allocation.

./sbt "testOnly org.xerial.snappy.SnappyFramedStreamTest": 18/18 pass.

SnappyFramedInputStream.ensureBuffer() trusted the attacker-controlled
Snappy length-prefix when sizing decompression buffers: a ~23-byte frame
could declare a multi-GB uncompressed length and force direct-ByteBuffer
and byte[] allocations of that size before the frame CRC32C is checked
(CWE-770, issue xerial#730). The compressed chunk length is bounded (~16 MiB)
but the declared uncompressed length had no ceiling at all.

Reject declared lengths above SnappyInputStream.MAX_CHUNK_SIZE (512 MiB),
the ceiling already used for the sibling block-format class since the
CVE-2023-43642 fix. Frames declaring more than that can only be hostile:
conforming writers emit at most 64 KiB per chunk, and larger
non-conforming producers stay within the bound.
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.

snappy-java through 1.1.10.8 Uncontrolled Resource Allocation via SnappyFramedInputStream

1 participant