Skip to content

Cherry pick #116992 to 26.3: Do not read the compressed-buffer slack as LZ4 literal data - #118247

Merged
robot-clickhouse-ci-2 merged 13 commits into
backport/26.3/116992from
cherrypick/26.3/116992
Sep 5, 2026
Merged

robot-clickhouse-ci-2 merged 13 commits into
backport/26.3/116992from
cherrypick/26.3/116992

Conversation

@robot-ch-test-poll

Copy link
Copy Markdown
Contributor

Original pull-request #116992

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

alexey-milovidov and others added 12 commits August 29, 2026 02:08
The LZ4 decoder is given `ADDITIONAL_BYTES_AT_END_OF_BUFFER` (64) bytes of
slack after the compressed payload so that its unconditional wild copies stay
inside allocated memory. That slack is not initialized, and the bound on the
literal copy was expressed against the end of the slack rather than the end of
the payload, so a block declaring a literal longer than the bytes it actually
carries filled the decompressed block from the slack and still reported success.

Bound the literal by the end of the payload instead. The new check is stricter
than the old one, so the wild-copy over-read stays covered, and it no longer
needs to compute the rounded-up copy length.
The three "at least two more bytes are available" guards in `decompressImpl`
were written as `ip + 1 >= input_end`. `ip` is allowed to be exactly
`input_end` at each of them, and then `ip + 1` is two past the end of the
payload, which is out-of-range pointer arithmetic before the guard gets a
chance to reject the block.

All three states are reachable from malformed input, and two of them need only
a single payload byte: `f0` (a literal length of `0x0F` with no continuation
byte) reaches the first guard, `00` reaches the second one through the
`length == 0` shortcut of the 8-byte variant, and `1f000100` reaches the third
one.

Compare the distance instead. Given the `ip <= input_end` invariant that the
loop already maintains, `input_end - ip < 2` is exactly equivalent to the old
condition, so no block changes its verdict - verified over 3.49e9 crafted
blocks (all payloads up to 4 bytes, plus random longer ones, against all three
`copy_amount` variants): identical accept/reject and identical output bytes.
The distance form also generates one or two instructions less per variant,
because the loop already computes `input_end - ip` for the literal bound.
`copy_end = op + length` was formed before `copy_end > output_end` rejected it,
on both the literal and the match path. `length` comes from
`continue_read_length` and is bounded only by `255 * source_size`, so for a
crafted block the pointer is formed far outside the destination object - a
payload of `f0` followed by n bytes of `0xff` puts it about `255 * n` bytes past
`output_end`, measured 16711655 bytes past for n = 65536. That is the same
out-of-range pointer arithmetic that the input-side guards had, and this
function already avoids it deliberately for `match` (see the comment on the
`offset - 1 >= produced` check).

Compare the distance and form `copy_end` only once it is known to be in range.
`op <= output_end` is an invariant of the loop - `op` only ever becomes a
`copy_end` that was just bounded - so `output_end - op` is non-negative and the
new condition is exactly equivalent to the old one. Verified together with the
input-side guards over 3.49e9 crafted blocks against all three `copy_amount`
variants: identical accept/reject and identical output bytes, 0 mismatches.

This costs throughput, unlike the input-side change. Measured with the real
decoder on synthetic data at three compression ratios, best of 6 interleaved
runs, against the tip of this branch before this commit: `-1.3%` for
`copy_amount == 8` and `-2.2%` for `copy_amount == 16`. Relative to the state
before both commits that nets `-1.0%` and `+0.8%` respectively, because the
input-side change was itself worth `+0.3%` and `+3.0%`. Folding the literal's
output and payload bounds into a single `min` was also tried and came out
slightly worse than this, so the cost looks inherent to not letting the
compiler compute `op + length` before the branch.
…riants with a forced-method gtest

`LZ4::decompress` sends every block smaller than 32 KiB to method 0 (`decompressImpl<8>`), so the
functional test `05055_lz4_literal_past_end_of_compressed_block` only ever exercised that variant,
while the new payload-end and output-end bounds are shared by methods 1 (`decompressImpl<16, true>`)
and 2 (`decompressImpl<32>`) as well.

The new `gtest_lz4_decompress_literal_bounds` forces each variant with `LZ4::PerformanceStatistics{method}`
and runs the same crafted blocks through all of them: the literal that declares 40 bytes but carries 8
(the block from the functional test), the one-byte-short boundary, a well-formed literal ending exactly
at the end of the payload for a range of lengths around each copy granularity, a literal that does not
fit into the declared decompressed size, a huge declared literal, a token whose length extension is
missing, and a literal whose match offset is missing. The slack after the payload is filled with a
sentinel so a literal sourced from it can never pass by accident, and every verdict is cross-checked
against the reference `LZ4_decompress_safe`.
…he forced-variant gtest

The suite pinned the literal-side `output_end` and payload-end checks for methods 0, 1 and 2, but no
committed case reached the match-side `length > output_end - op` check: `RejectsMissingMatchOffset` exits
before the match length is decoded, and `gtest_lz4_decompress_branchless` only covers well-formed matches.

Add crafted blocks of 4 literals, a match with offset 1, and a final literal-only sequence, and run them
through the same `LZ4::PerformanceStatistics{method}` parametrization:
- `AcceptsMatchFittingIntoOutput`: the positive control, a match that fills the output up to the trailing
  literals, for every match length class the variants treat differently, cross-checked against
  `LZ4_decompress_safe`.
- `RejectsMatchPastEndOfOutput`: the same blocks decoded into a destination that is 1 to 7 bytes too small
  for the match (but not for the leading literals, so it is the match that is rejected).
- `RejectsHugeDeclaredMatch`: a ~4 KiB match-length extension into a 40-byte destination.
- `RejectsMissingMatchLengthExtension`: the payload ends right after the offset of a token that promises a
  match-length extension.

The harness now also places a canary after the output slack and asserts that the decoder never writes past
`ADDITIONAL_BYTES_AT_END_OF_BUFFER`, whatever its verdict. Without the match-side check, a match that
overruns the output by a few bytes lands inside the slack, the literal-side check then compares against a
negative distance and passes, and the trailing literals are written wholesale past the slack; the return
value alone does not show this because the decoder still runs out of input afterwards. Verified by compiling
the decoder with the match-side check removed: `RejectsMatchPastEndOfOutput` then fails on all three
variants through the canary, while the unmodified decoder passes all 33 cases.

The trailing literals are 200 bytes so that this spill is observable, and they also satisfy the reference
decoder's rules that a block ends with at least 5 literals and that the last match starts at least 12
bytes before the end of the block, so the positive control agrees with `LZ4_decompress_safe` for every
match length from the minimum of 4.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…k-read

Do not read the compressed-buffer slack as LZ4 literal data
@robot-ch-test-poll robot-ch-test-poll added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-critical-bugfix labels Sep 5, 2026
Resolved the conflict in `src/Compression/LZ4_decompress_faster.cpp`: the release branch
declares `UInt8 * copy_end; size_t real_length;` uninitialized, while the cherry-picked change
drops `real_length` (the literal bound is now expressed as a distance against the payload end)
and initializes `copy_end` to `nullptr`. Took the cherry-picked side, which is the point of the
change; the rest of the file merged cleanly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! pr-critical-bugfix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants