Skip to content

Cherry pick #119598 to 26.3: Do not read a short uniqTheta state past its buffer - #123730

Merged
robot-ch-test-poll2 merged 15 commits into
backport/26.3/119598from
cherrypick/26.3/119598
Oct 3, 2026
Merged

robot-ch-test-poll2 merged 15 commits into
backport/26.3/119598from
cherrypick/26.3/119598

Conversation

@robot-clickhouse

Copy link
Copy Markdown
Member

Original pull-request #119598

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.

Before you resolve anything

Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.

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 14 commits September 12, 2026 06:21
`compact_theta_sketch_parser::parse` verifies that the buffer holds 8 bytes and
then, for serial versions 1, 2 and 3, reads `num_entries` at offset 8 and `theta`
at offset 16 before it checks the size again. `ThetaSketchData::read` hands it
the exact size, so a short state is rejected - but only after those reads have
already happened, and any `CAST` from a string can hand it 8 bytes:

    SELECT CAST(char(8, 0, 1, 3, 0, 0, 0, 0, 0), 'AggregateFunction(uniqTheta, UInt64)');
    -- Cannot deserialize Theta sketch state: at least 86160 bytes expected, actual 8
    -- Cannot deserialize Theta sketch state: at least 2072 bytes expected, actual 8

An ASan build reports the read as a heap-buffer-overflow; a release build spells
whichever size the neighbouring bytes happen to hold, which is why the same query
answers differently from run to run.

Pad the buffer to the largest offset the parser reads before validating anything,
while still declaring the true length to it: the premature header reads stay
inside the allocation and the parser's own size check rejects the state. The
upstream fix is to move each `check_memory_size` before the field it guards; the
padding is what keeps the read in bounds for every version of `datasketches-cpp`
this repository pulls in.

Found by the AST fuzzer on an unrelated pull request (#116647, STID 1740-4c22).

Closes: #119595
The Fast test build is compiled without `datasketches`, so every query in the
test failed with `Function with name 'uniqTheta' does not exist`
(`UNKNOWN_FUNCTION`). That failure also dropped every other job in the
workflow, including the `Bugfix validation` the `new_tests_check` hook requires,
so `Finish Workflow` failed as well.

Every other `uniqTheta` test carries this tag.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
… validates

The test only reached one of the reads the fix makes safe. Serial versions 2, 3
and 4 compare the seed hash in the header against the one they compute, and all
four states the fuzzer produced carry a zero seed hash, so those three stopped
at `check_seed_hash` and never got as far as `num_entries`. Only serial version
1 computes the seed hash itself instead of comparing it, which is why it was the
only state that read past its buffer at all.

Put the seed hash of the default seed in the header - `204, 147` at offset 6 -
so the states get past that check. An ASan build then reports a
heap-buffer-overflow at each of the four reads that happen before the size is
checked, rather than only the two in serial version 1:

  compact_theta_sketch_parser_impl.hpp:93  `num_entries`, serial version 1
  compact_theta_sketch_parser_impl.hpp:94  `theta`,       serial version 1
  compact_theta_sketch_parser_impl.hpp:109 `num_entries`, serial version 2, two preamble longs
  compact_theta_sketch_parser_impl.hpp:119 `num_entries`, serial version 2, three preamble longs
  compact_theta_sketch_parser_impl.hpp:120 `theta`,       serial version 2, three preamble longs
  compact_theta_sketch_parser_impl.hpp:83  `num_entries`, serial version 3, two preamble longs

The test now prints the size each state reports as missing instead of only
whether it was rejected, because that size is the part that used to come from
outside the buffer. Two of the new states report `16` rather than `24`, which
only the padded buffer makes reproducible.

One of them, serial version 2 with two preamble longs, is accepted as an empty
sketch: with `num_entries` read as zero the parser returns before it looks at
any entry. That is what the padding decides, and it is deliberate - refusing
every state below twenty-four bytes is not an option, since a single-entry
sketch is sixteen bytes long and a valid one. The test pins both this and the
sizes of the short states a `uniqTheta` aggregation really writes, so that a
future minimum-size check cannot silently reject them.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Both comments claimed that keeping the declared size exact means a short state
is "still rejected". That is not quite what happens, and the test now shows the
one case where it does not: a serial version 2 state with two preamble longs
reads `num_entries` as zero, which describes an empty sketch, so the parser
returns before it looks at any entry and the state is accepted as empty. That
is what the zero padding decides, and it is the reproducible half of a choice
that used to depend on whatever followed the allocation.

Also correct the offsets the comment attributes to each serial version - `theta`
at offset 16 is read ahead of its size check by versions 1 and 2, not by
version 3, which checks for 24 bytes before reading it - and say why a state
below the padding cannot simply be refused: a sketch holding one entry is 16
bytes long and valid.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Padding the buffer to 24 bytes kept the premature header reads of
`compact_theta_sketch_parser::parse` in bounds, but it also let a
truncated state pass: a serial version 2 state with two preamble longs
and only 8 bytes read `num_entries` from the synthetic zeros and was
accepted as an empty sketch, where the `std::istream` deserializer of
`datasketches` - which has to consume the second preamble long first -
rejects it. Malformed `AggregateFunction(uniqTheta, ...)` input was thus
accepted with a wrong result, as the AI review pointed out.

Compute instead, from the first 8 bytes the parser does check for, how
many bytes it reads before it checks the size again - 24 for serial
version 1, 16 or 24 for serial version 2 depending on the preamble, 16
for a non-empty serial version 3 state whose preamble does not make the
parser check itself - and refuse a state shorter than that with the
parser's own `at least N bytes expected, actual M` wording before it is
handed over. The buffer is no longer padded, and its true size is the
only one the parser sees.

The test now expects `char(8, 2, 2, 3, 0, 0, 0, 204, 147)` to be refused
for its size, and `char(8, 0, 3, 3, 0, 0, 0, 0, 0)` as well, since the
size is validated before the seed hash is compared. It also runs its
queries through `clickhouse-client` against the running server instead
of starting `clickhouse-local` ten times, which took 37 seconds each
under MSan and failed the flaky check with `Test runs too long`; the
final three aggregations are folded into one query. It takes 2.4
seconds locally now.

Reports: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=119598&sha=02d2a990b0c143c15bf94cc872c4914265eb465d&name_0=PR&name_1=Stateless%20tests%20%28amd_msan%2C%20flaky%20check%29
PR: #119598

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

Do not read a short `uniqTheta` state past its buffer
@robot-clickhouse robot-clickhouse 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 Oct 3, 2026
Resolve the conflict in `src/AggregateFunctions/ThetaSketchData.h`: 26.3 does not
have the `try`/`catch` around `compact_theta_sketch::deserialize` that master has,
so keep the 26.3 structure and add only the header size check of #119598 before
the call, together with the `Common/Exception.h` include and the `CORRUPTED_DATA`
error code declaration it needs. The `compact_theta_sketch_parser::parse` code in
the `datasketches-cpp` version pinned on 26.3 is identical to the one on master,
so the offsets in `headerBytesReadBeforeSizeCheck` apply unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robot-ch-test-poll2
robot-ch-test-poll2 merged commit 41dc2f5 into backport/26.3/119598 Oct 3, 2026
1 check passed
@robot-ch-test-poll2
robot-ch-test-poll2 deleted the cherrypick/26.3/119598 branch October 3, 2026 22:47
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.

3 participants