Check the buffer size before reading compact theta sketch preamble fields - #536
Merged
leerho merged 1 commit intoOct 3, 2026
Conversation
…elds `compact_theta_sketch_parser::parse` checks that the buffer holds 8 bytes and then, for serial versions 1, 2 and 3, reads `num_entries` (offset 8) and `theta` (offset 16) before checking the size again. A truncated sketch therefore causes an out-of-bounds read, and the bytes read decide whether the sketch is rejected or accepted as empty: a serial version 1 or 2 sketch truncated to 8 bytes is accepted as an empty sketch if the memory after the buffer happens to look like an empty preamble. Check that the whole preamble is present before reading any field from it, which is what the `std::istream` deserializer does implicitly. Found by the AST fuzzer in ClickHouse: ClickHouse/ClickHouse#119595
leerho
approved these changes
Oct 3, 2026
leerho
approved these changes
Oct 3, 2026
leerho
left a comment
Member
There was a problem hiding this comment.
Thank you! This will be included in 5.3.0.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
compact_theta_sketch_parser::parsechecks that the buffer holds 8 bytes, then reads header fields beyond those 8 bytes before checking the size again:num_entries(offset 8),theta(offset 16)num_entries(offset 8)num_entries(offset 8),theta(offset 16)num_entries(offset 8)So
compact_theta_sketch::deserialize(ptr, size)andwrapped_compact_theta_sketch::wrap(ptr, size)read past the buffer when given a truncated sketch (a heap-buffer-overflow under AddressSanitizer). Also, the bytes read after the buffer decide the result. A serial version 1 or 2 sketch truncated to 8 bytes is accepted as an empty sketch if the memory after the buffer looks like an empty preamble. Otherwise it is rejected with an "at least N bytes expected" message where N depends on that memory.This change checks that the whole preamble is present before reading any field from it. The
std::istreamdeserializer already does this implicitly, because it has to read the preamble before it can decide anything. Valid sketches are not affected: a serialized sketch always contains its full preamble, except the 8-byte empty sketches, which take a separate path.The new test case uses buffers that contain exactly the truncated bytes. Without the fix it fails under AddressSanitizer with a heap-buffer-overflow in
compact_theta_sketch_parser_impl.hpp. Without a sanitizer, the result of the serial version 1 and 2 cases depends on the memory after the buffer.The ClickHouse AST fuzzer found this through
CAST(<string> AS AggregateFunction(uniqTheta, UInt64)): ClickHouse/ClickHouse#119595