Skip to content

Fix heap-buffer-overflow in T64 and abort in Multiple decompression codecs - #99680

Merged
yakov-olkhovskiy merged 11 commits into
masterfrom
fix/compression-codec-bounds-checks
Mar 19, 2026
Merged

yakov-olkhovskiy merged 11 commits into
masterfrom
fix/compression-codec-bounds-checks

Conversation

@motsc

@motsc motsc commented Mar 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Two crash-triggering bugs found by new libFuzzer targets (t64_decompress_fuzzer and multiple_decompress_fuzzer):

CompressionCodecT64 — heap-buffer-overflow

decompressData<T, bool> in CompressionCodecT64.cpp had two missing bounds checks:

  1. Before memcpy(dst, src, bytes_to_skip) at the start — no check that bytes_to_skip <= bytes_size, allowing a read past the compressed source buffer.
  2. Before memcpy(&min, src, sizeof(MinMaxType)) — no check that bytes_size >= 16 (the min/max header size), causing an OOB read on short inputs.

Crash reproducer (10-byte fuzzer input): 85 68 85 3b 8d 85 0a fd 81 2c

CompressionCodecMultiple — process abort

doDecompressData in CompressionCodecMultiple.cpp had two missing bounds checks:

  1. No check that compression_methods_size + 1 <= source_size before constructing PODArray from &source[n+1]..&source[m]. When n >= m (reversed range), PODArray attempts a ~2^64-byte allocation, which aborts the process via abortOnFailedAssertion — uncatchable by catch(...).
  2. No check that source_size >= COMPRESSED_BLOCK_HEADER_SIZE (9 bytes) before calling readDecompressedBlockSize, which reads at source[5..8].

Crash reproducer (9-byte fuzzer input): 00 00 00 00 00 00 db 00 9a

Changes

  • src/Compression/CompressionCodecT64.cpp: two bounds checks with Exception throws
  • src/Compression/CompressionCodecMultiple.cpp: two bounds checks with Exception throws
  • src/Compression/tests/gtest_compressionCodec.cpp: ASSERT_THROW regression tests for both crash reproducers

Test plan

  • T64Test::DecompressMalformedInput — verifies the 2-byte T64 reproducer throws Exception
  • CompressionCodecMultipleTest::DecompressMalformedInput — verifies the 1-byte Multiple reproducer throws Exception
  • T64Test::TranscodeRawInput — existing round-trip test still passes for all integer types

Changelog category

Bug Fix

Changelog entry

Fixed heap-buffer-overflow in CompressionCodecT64 and process abort in CompressionCodecMultiple when decompressing malformed compressed data. Both issues were found by new libFuzzer targets. The codecs now throw an exception instead of crashing.

Version info

  • Merged into: 26.3.1.858 (included in 26.3 and later)
  • Backported to: 25.8.29.23

motsc and others added 2 commits March 16, 2026 17:13
`CompressionCodecT64::doDecompressData` had two heap-buffer-overflow
vulnerabilities discovered via fuzzing:
- `memcpy(dst, src, bytes_to_skip)` at the start had no check that
  `bytes_size >= bytes_to_skip`, allowing a read past the compressed
  source buffer
- The header read (`memcpy(&min, src, sizeof(MinMaxType))`) had no check
  that `bytes_size >= header_size` (16 bytes), causing OOB reads on
  short inputs

`CompressionCodecMultiple::doDecompressData` had two vulnerabilities:
- No check that `compression_methods_size + 1 <= source_size` before
  constructing the `PODArray` from `&source[n+1]..&source[m]`; if
  `n >= m`, the reversed range caused the allocator to attempt a
  ~2^64-byte allocation, aborting the process
- No check that `source_size >= COMPRESSED_BLOCK_HEADER_SIZE` (9 bytes)
  before calling `readDecompressedBlockSize`, which reads at
  `source[5..8]`

Crash reproducer for T64 (9 bytes): `85 68 85 3b 8d 85 0a fd 81 2c`
Crash reproducer for Multiple (9 bytes): `00 00 00 00 00 00 db 00 9a`
Add two `ASSERT_THROW` regression tests that reproduce the exact crash
inputs found by fuzzing:

- `T64Test::DecompressMalformedInput` — 2-byte payload `81 2c` with
  `decompressed_size = 26757` triggered a heap-buffer-overflow in
  `decompressData` because `bytes_to_skip > bytes_size` with no bounds
  check before the trailing-bytes `memcpy`.

- `CompressionCodecMultipleTest::DecompressMalformedInput` — 1-byte
  payload `9a` claiming 154 codecs triggered a process abort in
  `doDecompressData` because `PODArray` was constructed from a reversed
  pointer range (`&source[155]..&source[1]`), causing a ~2^64-byte
  allocation attempt.

Both inputs now throw `Exception` instead of crashing.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
alexey-milovidov and others added 2 commits March 17, 2026 01:35
The test `04004_alter_modify_column_ttl_without_type` uses `today()` which
makes the test non-deterministic and can cause failures around midnight
or in different timezones.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Resolve conflict in gtest_compressionCodec.cpp by keeping both the
T64/Multiple malformed-input regression tests and the new ALP codec
tests added on master.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Mar 17, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2de7f1b]

Summary: ✅


AI Review

Summary

This PR hardens malformed-input handling in CompressionCodecT64 and CompressionCodecMultiple by adding missing bounds checks, and adds focused regression tests for both newly guarded paths. I did not find additional correctness, safety, or performance issues beyond already-discussed review threads.

ClickHouse Rules

Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
Safe rollout ✅
Compilation time ✅

Final Verdict

  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Mar 17, 2026
if (compressed_buf.size() >= 1_GiB)
throw Exception(decompression_error_code, "Too large compressed size: {}", compressed_buf.size());

if (source_size < COMPRESSED_BLOCK_HEADER_SIZE)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ The new short-header guard checks source_size, but this variable is the outer block size and no longer tracks the current compressed_buf after each decompression step.

On later iterations, compressed_buf.data() can still contain fewer than COMPRESSED_BLOCK_HEADER_SIZE bytes while source_size remains larger, so readDecompressedBlockSize(compressed_buf.data()) may still read past the end.

Please validate compressed_buf.size() here instead of source_size.

@motsc motsc Mar 17, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am thinking source_size is the right check here. It tracks the actual content length in compressed_buf, not the buffer's allocated size. After the swap at line 124, compressed_buf.size() includes the padding from line 120, so it's always >= source_size. Switching to compressed_buf.size() would be a weaker guard — it'd let through cases that source_size < COMPRESSED_BLOCK_HEADER_SIZE already catches.

@yakov-olkhovskiy yakov-olkhovskiy self-assigned this Mar 17, 2026
alexey-milovidov and others added 3 commits March 17, 2026 06:42
… checks

Cover the two additional guards added in the fix commit:

- `T64Test::DecompressMalformedInputShortHeader` — payload present but
  shorter than the 16-byte min/max header required by T64
  (`bytes_size < header_size`, Bug 1b).

- `CompressionCodecMultipleTest::DecompressMalformedInputShortBlockHeader` —
  compressed data after the methods list is shorter than
  `COMPRESSED_BLOCK_HEADER_SIZE` (9), triggering the OOB read (Bug 1d).

Also rename existing tests to clarify which guard each covers, and fix
`CompressionCodecMultiple` construction to use the byte-code path instead of
`get("Multiple", {})` which throws `Codec Multiple cannot be specified directly`.
…cks' into fix/compression-codec-bounds-checks
@clickhouse-gh

clickhouse-gh Bot commented Mar 19, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 83.80% 83.70% -0.10%
Functions 23.90% 23.90% +0.00%
Branches 76.30% 76.30% +0.00%

PR changed lines: PR changed-lines coverage: 90.22% (83/92, 0 noise lines excluded)
Diff coverage report
Uncovered code

@yakov-olkhovskiy
yakov-olkhovskiy added this pull request to the merge queue Mar 19, 2026
Merged via the queue into master with commit 1ff2f55 Mar 19, 2026
159 checks passed
@yakov-olkhovskiy
yakov-olkhovskiy deleted the fix/compression-codec-bounds-checks branch March 19, 2026 12:59
@robot-ch-test-poll1 robot-ch-test-poll1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 19, 2026
@Algunenano Algunenano self-assigned this Jul 17, 2026
@Algunenano Algunenano added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Jul 17, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Jul 17, 2026
robot-clickhouse-ci-2 added a commit that referenced this pull request Jul 17, 2026
Cherry pick #99680 to 25.8: Fix heap-buffer-overflow in T64 and abort in Multiple decompression codecs
robot-clickhouse added a commit that referenced this pull request Jul 17, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Jul 17, 2026
Algunenano added a commit that referenced this pull request Jul 20, 2026
Backport #99680 to 25.8: Fix heap-buffer-overflow in T64 and abort in Multiple decompression codecs
ASSERT_THROW(codec->decompress(source, source_size, dest.data()), Exception);
}

TEST(CompressionCodecMultipleTest, DecompressMalformedInputReversedRange)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both tests below do not hit the check this PR introduced because we set decompressed_size = 0

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

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants