Repository navigation
Reject out-of-range IntervalKind during binary type decoding - #106261
nikitamikhaylov merged 5 commits into
Conversation
The AST fuzzer (STID 3285-3bba) tripped UndefinedBehaviorSanitizer at
`src/Common/SipHash.h:134:21` with `applying non-zero offset 8 to null
pointer`. The path was:
SerializationInterval::getHash(IntervalKind)
-> SipHash::update(std::string_view sv) # SipHash.h:183
-> SipHash::update(sv.data(), sv.size()) # update(const char *, UInt64)
-> while (data + 8 <= end) # SipHash.h:134:21 (UB)
`IntervalKind::toString` returns `magic_enum::enum_name(kind)`, which on
libcxx returns an empty `std::string_view` with `data() == nullptr` and
`size() == 0` whenever `kind` is not a declared enum value (which the AST
fuzzer can produce by mutation). Even though the `while` loop body is never
entered (the condition `nullptr + 8 <= nullptr` is false), evaluating
`data + 8` on a null pointer with a non-zero offset is undefined behavior
per [expr.add]/4. UBSan flags it via `-fsanitize=pointer-overflow`.
The minimal fix is to short-circuit at the top of
`SipHash::update(const char *, UInt64)` when `size == 0`. Every caller
benefits: an empty update is a true no-op and the hash state is preserved
identically across `update(non_empty)` and
`update(empty); update(non_empty); update(empty)`. The fix is a no-op for
all valid inputs and `ALWAYS_INLINE` keeps the hot path unchanged.
Reproduced locally with a standalone clang-21 `-fsanitize=undefined
-fno-sanitize-recover=all` program calling the same `data + 8` pattern with
`(nullptr, 0)`: it produces the exact same UBSan message as the CI report
(`runtime error: applying non-zero offset 8 to null pointer`) without the
guard and exits cleanly with the guard. A gtest unit test
(`src/Common/tests/gtest_sip_hash.cpp`) exercises the path on ClickHouse's
own ASan+UBSan unit-tests target.
CI report:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=106251&sha=874c4f0fc6fa05c7726121221bd18e4185eda64e&name_0=PR&name_1=AST%20fuzzer%20%28arm_asan_ubsan%29
Co-Authored-By: Claude Opus 4.7 <[email protected]>
|
Pre-PR validation gate (session a) Deterministic local repro? Yes. Minimal standalone reproducer ( Exact same UBSan message format as the CI report ( b) Root cause explained? c) Fix matches root cause? Yes. The guard is at the exact UB site (entry of d) Test intent preserved? / New tests added? No existing tests modified. Added e) Both directions demonstrated? Yes. The standalone repro fails-without-fix (exit 1 + UBSan message) and passes-with-fix (exit 0 + f) Fix is general, not a narrow patch? Yes. The guard is at CI report (PR #106251, AST fuzzer arm_asan_ubsan, STID 3285-3bba): https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=106251&sha=874c4f0fc6fa05c7726121221bd18e4185eda64e&name_0=PR&name_1=AST%20fuzzer%20%28arm_asan_ubsan%29 |
|
cc @Avogar @azat — could you review this? It's a one-line UBSan guard at the entry of |
|
Workflow [PR], commit [1bccbb5] Summary: ✅
AI ReviewSummaryThis PR rejects malformed Final VerdictStatus: ✅ Approve |
azat
left a comment
There was a problem hiding this comment.
Fix the PR according to my coments
| /// An empty `std::string_view` (e.g. returned by `magic_enum::enum_name` for an | ||
| /// out-of-range enum value) has `data() == nullptr` and `size() == 0`. The | ||
| /// pointer-arithmetic below (e.g. `data + 8` in the unrolled loop) is undefined | ||
| /// behavior on a null pointer with a non-zero offset, even when the loop body | ||
| /// is never entered. Short-circuit here to keep the hash state untouched. |
There was a problem hiding this comment.
First, this should be in a commit description not a comment in the code, save this into your memory please
Secondly, how it is possible to get incorrect "kind" in "IntervalKind::toString", likely during deserialization, so let's fix this instead and also do not forget to add a test
| ASSERT_EQ(empty.data(), nullptr); | ||
| ASSERT_EQ(empty.size(), 0u); |
There was a problem hiding this comment.
I know you can generate codex, but please try to be "compact", this is not a test for std::string_view, no need to ASSERTs it's internal state, it is obvious that the internal representation is correct
| /// Regression test for an UndefinedBehaviorSanitizer finding at SipHash.h | ||
| /// reported by the AST fuzzer (STID 3285-3bba): "applying non-zero offset 8 to | ||
| /// null pointer" inside `SipHash::update(const char *, UInt64)`. The trigger | ||
| /// path was `SerializationInterval::getHash` -> `SipHash::update(string_view)` | ||
| /// where the string_view came from `magic_enum::enum_name` on an out-of-range | ||
| /// `IntervalKind::Kind` and therefore had `data() == nullptr`, `size() == 0`. |
There was a problem hiding this comment.
All of this comments should be in commit messages, this comments will be outdated, and for this specific case test name is enough to understand the purpose
…285-3bba-siphash-empty-buffer
`decodeDataType` for `BinaryTypeIndex::Interval` cast a `UInt8` byte from
the input buffer directly to `IntervalKind::Kind` without validating that
the value belonged to the enum (valid range `0x00`..`0x0A`). Anything in
`0x0B`..`0xFF` produced a `DataTypeInterval` whose internal `kind` was
out of range, after which `IntervalKind::toString` returned
`magic_enum::enum_name` on an unknown value. On libcxx that returns
`std::string_view{nullptr, 0}`, and `SerializationInterval::getHash` then
fed that null pointer into `SipHash::update`, where the unrolled loop
computed `data + 8` on a null pointer (UB per `[expr.add]/4`). UBSan
flagged it via `-fsanitize=pointer-overflow` (STID 3285-3bba).
The primary fix is at the deserialization site: reject any byte above
`IntervalKind::Kind::Year` with `INCORRECT_DATA`. A regression test
`04307_03285_3bba_interval_binary_encoding_invalid_kind` feeds three
malformed bytes (`0x0b`, `0x7f`, `0xff`) through
`RowBinaryWithNamesAndTypes` with
`input_format_binary_decode_types_in_binary_format` and asserts each is
rejected.
The previous `SipHash::update(nullptr, 0)` short-circuit is kept as
defense-in-depth. The accompanying comment is shortened, and the
explanatory blocks in `gtest_sip_hash.cpp` plus the `ASSERT_EQ` on
`std::string_view` internals are removed per review feedback.
CI report:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=106251&sha=874c4f0fc6fa05c7726121221bd18e4185eda64e&name_0=PR&name_1=AST%20fuzzer%20%28arm_asan_ubsan%29
Related: ClickHouse#106251
Co-Authored-By: Claude Opus 4.7 <[email protected]>
|
@azat, pivoted to a root-cause fix per your direction. Root cause: Fix (HEAD
All explanatory context now lives in the commit message instead of code comments. PR title and description updated to match the new approach.
|
| ALWAYS_INLINE void update(const char * data, UInt64 size) | ||
| { | ||
| /// Avoid UB from `data + 8` arithmetic below when `data == nullptr`. | ||
| if (size == 0) |
There was a problem hiding this comment.
Regardless initial report "/ClickHouse/src/Common/SipHash.h:134:21: runtime error: applying non-zero offset 8 to null pointer" should we just suppress it, it is not a UB in our case, the loop will break early
|
@azat you're right that the loop never executes for I'd lean toward keeping the
Either way the IntervalKind range check is now the primary fix and closes this report on its own, so the guard isn't load-bearing for STID 3285-3bba; it's just defense-in-depth for any future caller that passes an empty buffer. If you'd still prefer to drop it I'm happy to remove it and rely solely on the deserialization fix. |
|
@groeneai Ok, then let's do the following:
|
@azat asked for the SipHash defense-in-depth tweak (size==0 short-circuit plus the gtest) to be split out of this PR so the user-visible bug fix (out-of-range IntervalKind binary decoding -> INCORRECT_DATA) can be backported independently. The SipHash hunk is now PR ClickHouse#106469. This PR keeps only the IntervalKind range check at the deserialization site plus the regression test. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
@azat done.
PR title and body updated to reflect the IntervalKind-only scope. |
The format spec table in `src/DataTypes/DataTypesBinaryEncoding.h` listed `Year` as `0x1A`, but the canonical value in `IntervalKind::Kind` is `0x0A`, and `decodeDataType` now rejects any byte outside `0x00..0x0A`. Update the spec so producers writing the `Interval` binary encoding aren't told to emit a byte the decoder will reject. Caught by clickhouse-gh[bot] inline review on PR ClickHouse#106261. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Head branch was pushed to by a user without write access
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 6/6 (100.00%) | Lost baseline coverage: none · Uncovered code |
|
CI fully finished on
146 pass / 1 fail / 0 pending / 19 skipping; the single fail is the infra item above. No PR-caused failures, no new flaky tests to track. AI review APPROVED; @ azat APPROVED at 13:48Z. Ready to merge. |
|
Found via ClickGap automated review. Please close or comment if this is incorrect or needs adjustment. TL;DRReject out-of-range IntervalKind during binary type decoding ClickGap verified: reproduced it with the script below on ReproductionBisect
Affects
Suggested next stepThe fix has landed on master; the affected release branches above still need the backport. Component: cc fix PR author: @groeneai ClickGap · Finding: |
`SipHash::update(const char * data, UInt64 size)` evaluates `data + size` and `data + 8` near the start of the unrolled loop. When `data == nullptr` and `size == 0`, the loop body never executes (the condition `nullptr + 8 <= nullptr` is false), but evaluating `data + 8` on a null pointer with non-zero offset is UB per `[expr.add]/4`. UBSan flags this under `-fsanitize=pointer-overflow`. The trigger surfaced via `SerializationInterval::getHash` -> `SipHash::update(std::string_view sv)` -> `update(sv.data(), sv.size())` when an out-of-range `IntervalKind` produced an empty `std::string_view` with `data() == nullptr` from `magic_enum::enum_name` on libcxx (STID 3285-3bba in the AST fuzzer). The primary fix for that specific path lives in PR ClickHouse#106261 (range check at the deserialization site). This change is a narrow precondition for `SipHash::update`: an empty input is a no-op. `ALWAYS_INLINE` keeps the hot path unchanged for non-empty inputs and the single zero-size branch folds away for callers that pass a constant non-zero size. Any future caller that legitimately hands `SipHash::update` a `(nullptr, 0)` pair (e.g. an empty string_view from another magic_enum-style source) will be a no-op instead of re-tripping UBSan. Adds a small gtest (`gtest_sip_hash.cpp`) that: 1. Verifies updating with an empty std::string_view followed by other data and a final empty update does not invoke UB and produces a stable digest. 2. Verifies the empty updates are observably no-ops: the digest equals what you would get hashing the same payload without the empty prefix/suffix updates. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Backport #106261 to 26.3: Reject out-of-range IntervalKind during binary type decoding
…during binary type decoding
Closes: STID 3285-3bba (no public tracker)
Related: #106469
decodeDataTypeinsrc/DataTypes/DataTypesBinaryEncoding.cppforBinaryTypeIndex::Intervalcast aUInt8byte read from the input buffer directly toIntervalKind::Kind(valid range0x00..0x0A) without any range validation. Anything in0x0B..0xFFproduced aDataTypeIntervalwhose internalkindwas out of range.IntervalKind::toStringthen returnedmagic_enum::enum_nameon an unknown value, which on libcxx isstd::string_view{nullptr, 0}, andSerializationInterval::getHashfed that null pointer intoSipHash::update. UBSan flagged thedata + 8evaluation inSipHash::updatevia-fsanitize=pointer-overflow(STID 3285-3bba in the AST fuzzer).This PR rejects out-of-range
IntervalKindbytes at the deserialization site withINCORRECT_DATA. That stops the bad enum value from ever existing, so the downstreamIntervalKind::toString -> magic_enum::enum_name -> empty string_viewpath never executes for crafted/malformed input.The defense-in-depth
SipHash::updatesize==0 short-circuit (and its gtest) was split out per @azat into PR #106469 so this PR remains a focused, backportable bug fix.Audit / symmetric paths
IntervalKindis constructed from external bytes in only one place:DataTypesBinaryEncoding.cpp. Other entry points are safe:ArrowColumnToCHColumn.cpp:151-161— closed-enum switch withUNKNOWN_TYPEfallbackparseIntervalKind.cpp— string-keyword basedparseTimeSeriesTypes.cpp:61— forwards from validated parser pathTest
tests/queries/0_stateless/04307_03285_3bba_interval_binary_encoding_invalid_kind.shfeeds three malformed bytes (0x0B,0x7F,0xFF) throughRowBinaryWithNamesAndTypeswithinput_format_binary_decode_types_in_binary_formatand asserts each is rejected withCode: 117. DB::Exception: Unknown IntervalKind during Interval type decoding: 0xNN. (INCORRECT_DATA).Reproduction (CI)
CI report where the original UBSan trip surfaced: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=106251&sha=874c4f0fc6fa05c7726121221bd18e4185eda64e&name_0=PR&name_1=AST%20fuzzer%20%28arm_asan_ubsan%29
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Reject out-of-range
IntervalKindbytes duringRowBinaryWithNamesAndTypestype decoding (input_format_binary_decode_types_in_binary_format = 1) with a clearINCORRECT_DATAerror instead of constructing aDataTypeIntervalwith an invalid kind that could subsequently trip undefined behavior in hash paths.Documentation entry for user-facing changes
Version info
26.6.1.425(included in26.6and later)26.3.33.44