Skip to content

Bound the compressed length of oversized LZ4 blocks - #162

Merged
yawkat merged 1 commit into
mainfrom
harden/block-input-stream-oversized-bound
Oct 15, 2026
Merged

yawkat merged 1 commit into
mainfrom
harden/block-input-stream-oversized-bound

Conversation

@yawkat

@yawkat yawkat commented Oct 15, 2026

Copy link
Copy Markdown
Owner

This is a hardening change, not a bug fix.

With withAcceptOversizedBlocks(true), LZ4BlockInputStream accepts LZ4-method blocks whose compressed length is at least the original length. It grows compressedBuffer to the header's compressedLen before reading the payload, and the only upper bound is Integer.MAX_VALUE. GHSA-4v53-57pg-c464 and the javadoc of withAcceptOversizedBlocks already document this trade-off.

The opt-in mode doesn't need an unbounded value. A legitimate LZ4 block can't be larger than the LZ4 compress bound of its original length (LZ4Utils.maxCompressedLength(originalLen), i.e. LZ4_compressBound). originalLen is already capped by the block size in the header (at most 32 MiB). This PR rejects LZ4 blocks whose compressedLen exceeds that bound, in all modes, with the same Stream is corrupted error. Even with oversized blocks enabled, the compressed-buffer allocation is now at most slightly more than the declared block size.

Details:

  • In the default mode this check never fires, because compressedLen < originalLen is already enforced.
  • RAW blocks already require compressedLen == originalLen, so they need no extra check.
  • The noncanonical blocks the option exists for (originalLen <= compressedLen <= bound) are still accepted. The existing testAcceptsInvalidLz4CompressedLengthInOversizedBlocksMode still passes.
  • I updated the javadoc on the class and on Builder.withAcceptOversizedBlocks.

This came up while fuzzing with micronaut-fuzzing.

Test: I added LZ4BlockStreamingTest.testRejectsLz4CompressedLengthAboveCompressBoundInOversizedBlocksMode. It checks that a header with compressedLen of maxCompressedLength(16) + 1 or Integer.MAX_VALUE is rejected in oversized mode before the payload is read. Without the change it fails: the stream tries to read the payload and reports Stream ended prematurely. With the change, LZ4BlockStreamingTest passes.

🤖 Generated with Claude Code

With acceptOversizedBlocks enabled, LZ4BlockInputStream allocated the
compressed buffer for the compressedLen from the block header with no
upper bound other than Integer.MAX_VALUE. An LZ4 block cannot be larger
than the LZ4 compress bound of its original length, so reject LZ4 blocks
whose compressed length exceeds LZ4Utils.maxCompressedLength(originalLen)
in all modes. This limits the allocation in the opt-in mode to slightly
more than the original length, which is itself limited by the block size
in the header. RAW blocks already require compressedLen == originalLen.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@yawkat
yawkat marked this pull request as ready for review October 15, 2026 13:23
@yawkat
yawkat merged commit f83ee9c into main Oct 15, 2026
3 checks passed
@yawkat
yawkat deleted the harden/block-input-stream-oversized-bound branch October 15, 2026 13:23
pstreef added a commit to moderne-backpatch-alliance/backpatch-lz4-java that referenced this pull request Oct 21, 2026
… the compress bound at 1.8.1

refill() allocated the block header's compressedLen (up to 2 GiB) before
reading any payload, so a 21-byte header could force a huge allocation.
An LZ4 block whose compressedLen exceeds
LZ4Utils.maxCompressedLength(originalLen) is now rejected as corrupted
before the allocation. originalLen is already capped at 32 MiB, so the
compressed buffer stays below about 32.1 MiB. No LZ4 encoder produces
such a block, and LZ4BlockOutputStream writes a RAW block whenever
compression does not shrink the data, so every valid stream still reads.

Carries the check of yawkat/lz4-java f83ee9c
("Bound the compressed length of oversized LZ4 blocks (yawkat#162)", on main
after v1.12.0) by Jonas Konrad <me@yawk.at>. Its javadoc hunks describe
the Builder option of bb83dd1
(GHSA-4v53-57pg-c464, v1.11.2), which 1.8.1 does not have; not carried.
bb83dd1 itself is not carried: it also rejects LZ4 blocks with
compressedLen >= originalLen and needs the Builder (new API) for its
opt-out.

Test: upstream's header-only rejection test, built with the public
LZ4BlockInputStream(InputStream) constructor instead of
Builder.withAcceptOversizedBlocks(true).

Upstream-Commit: f83ee9c
Co-authored-by: Jonas Konrad <me@yawk.at>
dongjoon-hyun added a commit to apache/spark that referenced this pull request Oct 28, 2026
### What changes were proposed in this pull request?

This PR aims to upgrade `at.yawk.lz4:lz4-java` to 1.12.1.

### Why are the changes needed?

To bring the security fix of `v1.12.1`, which was released as a security release for [GHSA-48mj-8f7c-7qfm](GHSA-48mj-8f7c-7qfm) (severity: low, availability impact only).

- https://github.com/yawkat/lz4-java/releases/tag/v1.12.1 (2026-10-22)
  - [LZ4BlockInputStream and LZ4FrameInputStream allocate block buffers from the block header before the block data arrives](GHSA-48mj-8f7c-7qfm): the streams now grow their buffers only as block data actually arrives (yawkat/lz4-java#167).
  - [Throw LZ4Exception for input shorter than the length prefix](yawkat/lz4-java#160)
  - [Bound the compressed length of oversized LZ4 blocks](yawkat/lz4-java#162)
  - [Use the buffer limit for the safe decompress(ByteBuffer, int, ByteBuffer, int)](yawkat/lz4-java#161)

Note that Apache Spark's `LZ4CompressionCodec` and `Statistics` read streams via `LZ4BlockInputStream`, which is one of the code paths fixed by `GHSA-48mj-8f7c-7qfm`.

**Full Changelog**: yawkat/lz4-java@v1.12.0...v1.12.1

### Does this PR introduce _any_ user-facing change?

No. There is no behavior change for valid LZ4 streams.

### How was this patch tested?

- Regenerated the dependency manifest with `dev/test-dependencies.sh --replace-manifest`; the only diff is the `lz4-java` line.
- `build/sbt core/compile`
- `build/sbt 'core/testOnly org.apache.spark.io.CompressionCodecSuite org.apache.spark.shuffle.sort.UnsafeShuffleWriterSuite org.apache.spark.FileSuite org.apache.spark.deploy.history.EventLogFileWritersSuite'` (87 tests passed)
- Pass the CIs.

### Was this patch authored or co-authored using generative AI tooling?

Generated-by: Claude Sonnet 5.5

Closes #59870 from dongjoon-hyun/SPARK-60628.

Authored-by: Dongjoon Hyun <dongjoon@apache.org>
Signed-off-by: Dongjoon Hyun <dongjoon@apache.org>
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