Skip to content

Bound text index posting list deserialization - #115222

Merged
CurtizJ merged 1 commit into
ClickHouse:masterfrom
CurtizJ:text-index-safe-deserialization
Aug 18, 2026
Merged

CurtizJ merged 1 commit into
ClickHouse:masterfrom
CurtizJ:text-index-safe-deserialization

Conversation

@CurtizJ

@CurtizJ CurtizJ commented Aug 17, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixed an out-of-bounds read while deserializing corrupted posting lists of a text index.


Workflow [PR]
Sync PR [sync-upstream/pr/115222]

Version info

  • Merged into: 26.8.1.1632 (included in 26.8 and later)
  • Backported to: 26.7.6.20, 26.6.4.47, 26.3.24.4

`PostingListCodecNone::decode` handed the payload to `PostingList::read`, which
wraps `roaring_bitmap_portable_deserialize` and trusts the container headers
found inside that payload. A container claiming more values than the declared
size holds made the deserializer read past the end of the buffer and return the
leaked bytes as row ids, so a corrupted or forged text index file - for example
one delivered by `RESTORE TABLE` - turned into a heap out-of-bounds read of up
to ~24 KiB per query.

Use `PostingList::readSafe`, which bounds every container read by the declared
size, as `DeleteBitmap`, `AggregateFunctionGroupBitmapData` and
`PuffinBlockInputFormat` already do. It is free: croaring's `read` is
`readSafe` with `maxbytes = SIZE_MAX`.

ClickHouse/clickhouse-private#69184

Co-Authored-By: Claude Opus 5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [eebc3ff]

Summary: ❌

job_name test_name status info comment
Stress test (arm_asan_ubsan) FAIL
UndefinedBehaviorSanitizer: undefined behavior (STID: 2881-3dbd) FAIL cidb IGNORED

AI Review

Summary

This PR switches uncompressed text-index posting list deserialization from PostingList::read to PostingList::readSafe and adds a focused gtest for truncated Roaring payloads. The inner over-read is addressed, but the deserializer still trusts the outer payload length enough to allocate arbitrary memory on corrupted .pst data, so I cannot clear it as fully hardened yet.

Findings

⚠️ Majors

  • [src/Storages/MergeTree/MergeTreeIndexTextPostingListCodec.cpp:244] readSafe only checks the inner Roaring containers after readContiguousBytes has already materialized num_bytes bytes. A forged posting-list block can still advertise a multi-gigabyte length prefix and force buffer.resize(num_bytes) before EOF/corruption is detected, turning malformed text-index data into a query-time memory blow-up. The same helper is also used by SegmentedPostingListCodec::decode for compressed postings, so the outer-size invariant is still broken across codecs. Suggested fix: validate the outer payload length against a trusted block boundary before allocating the scratch buffer, and thread that bound through both the uncompressed and bitpacking decode paths.
Final Verdict

Changes requested.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.80% 86.80% +0.00%
Functions 92.00% 92.00% +0.00%
Branches 79.20% 79.20% +0.00%

Changed lines: Changed C/C++ lines covered: 38/38 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Aug 17, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing eebc3ff35 with master c28352e98 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes
Binary Master PR Δ
programs/clickhouse-stripped 701.49 MiB 698.47 MiB -3.02 MiB (-0.43%)

Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction.

Compile time of recompiled translation units

7 translation units recompiled, 10 s compile time in total, 7 of them have a recent master baseline.

Job report

@CurtizJ CurtizJ self-assigned this Aug 18, 2026
@CurtizJ
CurtizJ added this pull request to the merge queue Aug 18, 2026
Merged via the queue into ClickHouse:master with commit b69c8ef Aug 18, 2026
183 of 185 checks passed
@CurtizJ
CurtizJ deleted the text-index-safe-deserialization branch August 18, 2026 14:34
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 18, 2026
@CurtizJ CurtizJ added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Aug 25, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Aug 25, 2026
@CurtizJ

CurtizJ commented Aug 25, 2026

Copy link
Copy Markdown
Member Author

@groeneai fix conflicts in all cherry-picks created for this PR.

@groeneai

Copy link
Copy Markdown
Collaborator

@CurtizJ I looked at all five. The textual conflict is misleading on every one of them, so I want to flag two things before opening PRs.

25.8 (#116306) cannot take this PR. There is no text index on that branch: MergeTreeIndexText and IPostingListCodec are absent from src/Storages/MergeTree/ (0 files each, vs 17 and 5 on 26.3). 25.8 still uses GinIndexStore, whose analogous unbounded reads are GinIndexPostingsList::read() at GinIndexStore.cpp:248 and :255. Fixing that is a different patch in different code, not a conflict resolution.

On 26.3/26.5/26.6/26.7 the conflicted function is not the code that reads. There, PostingListCodecNone::decode is an empty stub (void decode(ReadBuffer &, PostingList &) const override {}), and the 3-argument decode(..., PaddedPODArray<char> & buffer) does not exist (0 declarations in IPostingListCodec.h on all four; 2 on master). So resolving that file to just the one-line change compiles and does nothing. Taking the incoming side does not compile at all: it references readContiguousBytes, SegmentedPostingListCodec and encodePostingsInBlocks, none of which exist on those branches.

The equivalent unbounded deserialization on those branches is in MergeTreeIndexText.cpp, PostingsSerialization::deserialize, at two sites, with num_bytes already in scope as the bound:

branch sites
26.3 209, 216
26.5 217, 224
26.6 257, 264
26.7 323, 330

so the port is PostingList::read(p) to PostingList::readSafe(p, num_bytes) at both. readSafe(const char *, size_t) is available: the contrib/croaring pin is identical on those four and master (025ae3f), and deserialization_buffer is std::vector<char> on all of them.

The gtest cannot be ported as written, since it drives codec.decode(in, postings, buffer) and depends on PostingListCodecNone doing the read. I would rewrite it against PostingsSerialization on those branches; say the word if you would rather drop it and keep the backports source-only.

Two asks: is the 25.8 GinIndexStore fix in scope for you here, or should #116306 just be closed? And rewrite the test or drop it?

One mechanical note: I have no push access to cherrypick/* (maintainerCanModify is false on all five), so I will deliver each as a PR from my fork against the backport/<version>/115222 base, the way #114814 was done, and link it from the corresponding cherry-pick.

robot-clickhouse-ci-2 added a commit that referenced this pull request Aug 26, 2026
Cherry pick #115222 to 26.3: Bound text index posting list deserialization
robot-clickhouse-ci-2 added a commit that referenced this pull request Aug 26, 2026
Cherry pick #115222 to 26.5: Bound text index posting list deserialization
robot-clickhouse-ci-2 added a commit that referenced this pull request Aug 26, 2026
Cherry pick #115222 to 26.6: Bound text index posting list deserialization
robot-clickhouse-ci-2 added a commit that referenced this pull request Aug 26, 2026
Cherry pick #115222 to 26.7: Bound text index posting list deserialization
@robot-ch-test-poll robot-ch-test-poll added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Aug 26, 2026
CurtizJ added a commit that referenced this pull request Aug 26, 2026
Backport #115222 to 26.5: Bound text index posting list deserialization
CurtizJ added a commit that referenced this pull request Aug 26, 2026
Backport #115222 to 26.3: Bound text index posting list deserialization
CurtizJ pushed a commit that referenced this pull request Aug 27, 2026
… codec

`PostingsSerialization`'s constructor asserts its codec is non-null
(`chassert(posting_list_codec)`, `MergeTreeIndexText.cpp:218`), and the rewritten
`gtest_text_index_postings_deserialization.cpp` on this branch passes `nullptr`.
The first `decodePostingsInPlace` call in `RoundTrip` therefore aborts inside the
constructor, before `deserialize` is entered. That is why `Unit tests (asan_ubsan)`
and `Unit tests (tsan)` fail on the backport while every `Build` job is green
(`chassert` is inert in release), why the `function_prop_fuzzer` variants pass
(they filter this suite out), and why `TruncatedPayloadRejected` never reported.

The test's comment is accurate about `deserialize`: with `header = 0` the
uncompressed branch never consults the codec. The constructor asserts first,
though, so the precondition has to be satisfied regardless.

A `Type::None` codec keeps coverage identical, because `PostingListCodecNone::decode`
is a no-op: had the codec been consulted, `RoundTrip` would deserialize an empty
posting list and fail on cardinality. It passes, so the assertions still traverse
the `PostingList::readSafe` bounding this backport adds. Reverting that bound while
keeping this change makes `TruncatedPayloadRejected` fail all four assertions, so
the repaired test still catches the defect it was written for.

`MergeTreeIndexText.h` includes only `IPostingListCodec.h`, so the codec header has
to be included here; the version of this test on `master` carries the same include.

Related: #115222

Co-Authored-By: Claude Opus 5 <[email protected]>
CurtizJ added a commit that referenced this pull request Aug 29, 2026
Backport #115222 to 26.7: Bound text index posting list deserialization
CurtizJ added a commit that referenced this pull request Aug 29, 2026
Backport #115222 to 26.6: Bound text index posting list deserialization
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.

5 participants