Repository navigation
Do not read a short uniqTheta state past its buffer - #119598
Conversation
`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
|
Workflow [PR], commit [8f81caf] Summary: ✅
AI ReviewSummaryThe PR hardens Final Verdict
LLVM Coverage ReportMeasured on commit 8f81caf.
Changed lines: Changed C/C++ lines covered: 33/33 (100.00%) · Uncovered code |
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]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units8 translation units recompiled, 36 s compile time in total, 8 of them have a recent master baseline. |
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]>
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=119598&sha=f80bfe34e8991dc9e1c70d238bc8545b49dd977a&name_0=PR&name_1=Upgrade%20check%20%28amd_release%29 ( |
|
The Why the replica was read-only: at 17:13:59 Fixes:
The server-side cause is still on master: after a refused |
|
The separate PR is #123694: |
alexey-milovidov
left a comment
There was a problem hiding this comment.
LGTM and good for backport.
This is a workaround for a bug in Apache Datasketches. Should we fix it there?
Backport #119598 to 26.8: Do not read a short `uniqTheta` state past its buffer
Backport #119598 to 26.9: Do not read a short `uniqTheta` state past its buffer
Backport #119598 to 26.7: Do not read a short `uniqTheta` state past its buffer
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]>
Cherry pick #119598 to 26.3: Do not read a short `uniqTheta` state past its buffer
Backport #119598 to 26.3: Do not read a short `uniqTheta` state past its buffer
…tate past its buffer
…cpp` fix Update `contrib/datasketches-cpp` to a revision where `compact_theta_sketch_parser::parse` checks that the buffer holds the whole preamble before it reads a field from it, and remove the workaround in `ThetaSketchData::read` that computed the same bound ahead of the parser. The parser compares the seed hash of a serial version 3 state before it checks the size, so a short serial version 3 state with a wrong seed hash is now refused with `seed hash mismatch` instead of a size message. Related: ClickHouse#119598 Related: apache/datasketches-cpp#536 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…tate past its buffer
…tate past its buffer
A
uniqThetastate shorter than 24 bytes was read past the end of its buffer while being parsed - an ASan heap-buffer-overflow - and the out-of-bounds bytes then decided the size the parser reported as missing, so the same query answered differently from run to run:compact_theta_sketch_parser::parseverifies that the buffer holds 8 bytes and then reads header fields that lie beyond them before it checks the size again:num_entriesat offset 8 for serial versions 1, 2 and 3, andthetaat offset 16 for serial versions 1 and 2.ThetaSketchData::readhands the parser the exact size, so the state is rejected - but only after those reads have already happened, and anyCASTfrom a string can hand it a buffer of 8 bytes.This change computes, from the first 8 bytes the parser does check for, how many bytes it is going to read 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 on its own - and refuses a shorter state with the parser's own
at least N bytes expected, actual Mwording before handing it over. This is what thestd::istreamdeserializer ofdatasketchesdoes implicitly, since it has to consume those preamble bytes before it can decide anything. The upstream fix is to move eachcheck_memory_sizebefore the field it guards; the pre-check keeps the read in bounds for every version ofdatasketches-cppthis repository pulls in.An earlier revision padded the buffer with zeros instead. That kept the reads in bounds, but it also made an 8-byte serial version 2 state with two preamble longs read
num_entriesas zero and pass as an empty sketch, which accepted malformed input with a wrong result. The test pins that this state is refused now. A state still cannot be refused for being shorter than 24 bytes as such: a sketch holding one entry serializes to 16 bytes and is valid, which the test also covers.The test asserts the size each state reports as missing, which is the part that used to come from outside the buffer. Serial versions 2 and 4 compare the seed hash in the header against the one they compute, so of the four states the fuzzer produced - whose seed hash is zero - only the serial version 1 and 3 ones are refused for their size. The test therefore also covers states carrying the seed hash of the default seed, which reach each of the six reads that happen before the size is checked:
Found by the AST fuzzer on an unrelated pull request (#116647,
AST fuzzer (arm_asan_ubsan), STID 1740-4c22).Closes: #119595
Related: #116647
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Fix an out-of-bounds read when deserializing a
uniqThetastate shorter than 24 bytes, for instance fromCAST(<string> AS AggregateFunction(uniqTheta, ...)).Workflow [PR]
Sync PR [sync-upstream/pr/119598]
Version info
26.10.1.1412-master(included in26.10and later)26.9.10.3,26.8.17.3,26.7.21.3,26.3.42.3