Repository navigation
Fix LOGICAL_ERROR in server AST fuzzer safety-limit stripping on desynced SETTINGS - #110394
Conversation
…nced SETTINGS removeSettingsFromQuery detached an emptied SETTINGS clause via IAST::reset, which hard-throws LOGICAL_ERROR "AST subtree not found in children" when the owner's settings slot is set but its pointer is not registered in children. The server-side AST fuzzer can produce exactly such structurally-invalid ASTs, and this strip runs before the format-time try/catch that skips them, so the desync aborted the server in debug/sanitizer builds. Detach the slot tolerantly instead (erase the child if present, then clear the slot) in the INSERT, ASTQueryWithOutput and ASTStorage branches, matching the already-tolerant SELECT and BACKUP branches. Adds a gtest reproducing the desync for each affected carrier. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-1:20260714-123500 |
|
cc @evillique — could you review? Small fix to the server-side AST fuzzer's safety-limit settings stripping ( |
|
Workflow [PR], commit [dae698d] Summary: ✅
AI ReviewSummaryThis PR hardens Final VerdictStatus: ✅ Approve LLVM Coverage ReportMeasured on commit dae698d.
Changed lines: Changed C/C++ lines covered: 134/137 (97.81%) · Uncovered code |
ASTStorage::settings is a bare ASTSetQuery* whose only owner is ASTStorage::children. A fuzzer-mutated child list can drop that owning intrusive_ptr while leaving the slot set, so the pointer may dangle. removeSettingsFromQuery's ASTStorage branch dereferenced it unconditionally, a use-after-free. Guard the deref with a pointer-value check that the slot is still backed by children (never dereferencing the possibly-dangling pointer); clear the slot otherwise. The ToleratesSettingsSlotDesyncedFromChildren gtest erased the matching child for the ASTStorage carrier without keeping an owning ASTPtr alive, which freed the node and made the test itself a use-after-free rather than a desynced-but-live slot. Keep an owning ASTPtr for every erased settings node alive for the test body, so the test exercises the desynced-not-freed path (and now the storage guard's backed-by-children branch) without constructing a UAF. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-4:20260714-135900 |
…nced The prior fix blanket-cleared ASTStorage::settings whenever its slot was not backed by the storage's own children. That is too aggressive for a live-but- desynced node: ASTStorage::formatImpl serializes *storage->settings directly, so clearing the slot drops surviving engine settings (e.g. index_granularity) instead of stripping only the safety cap. Decide deref-safety by whole-tree reachability from the root instead. A node reachable through children from the still-alive root is kept alive by that owning intrusive_ptr chain, so it is safe to dereference and strip in place, preserving its surviving settings; this also recognises a node the fuzzer relocated elsewhere in the same tree. A slot not reachable from the root has no live owner (possibly freed) and is cleared without dereferencing, which is safe because such a node is out of the tree and never re-serialized. The reachable set is captured once up front (never touching *ptr). Add a gtest for a reachable-but-storage-desynced ASTStorage with a mixed clause: index_granularity survives, only max_rows_to_read is stripped. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-2:20260714-153900 |
…reachability collectReachableNodes judged the bare storage->settings slot live by pointer-value membership in a whole-tree address set. QueryFuzzer::fuzzMain mutates the tree in place and keeps allocating before the strip runs, so a freed slot's address can be reused by an unrelated live node; the address test then reports the dangling slot as live and we dereference freed memory. Replace it with findOwningChild: search storage->children (the slot's sole owner) for an owning ASTPtr with the same address that casts to ASTSetQuery. An owning child is the node's keep-alive, so a match proves the slot is the same live node and address reuse cannot spoof it. Strip that node in place (preserving surviving engine settings); otherwise fail closed and clear the slot without dereferencing. In production children is the sole owner, so an unbacked slot means freed and clearing loses no real settings. Retarget the gtest to assert the fail-closed contract: a slot not backed by an owning child in storage->children is cleared, not stripped in place. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-1:20260714-170800 |
CI finish ledger — 841ee37The one failure below is a pre-existing trunk flaky, not caused by this PR's diff (stripQuerySettings.cpp + gtest_Parser.cpp only).
Session id: cron:our-pr-ci-monitor:20260714-223000 |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 123/126 (97.62%) · Uncovered code |
CI finish ledger - 686e3a3Every failure below has an owner: a fixing PR, or a full-effort fix task whose
Session id: cron:our-pr-ci-monitor:20260729-083000 |
The run on 65addb3 never executed: praktika's Config Workflow spent its whole 600s budget on CI-database read timeouts (9 "Read timed out" attempts against play.clickhouse.com), was SIGTERMed ("Job timed out: [Config Workflow], timeout [600], exit code [-15]") and then set the 173 dependee jobs to dropped instead of dispatching them. No build or test job ran. That run is already on its second attempt, which the hourly retry workflow does not re-run, and a fork cannot reach the rerun endpoint, so a fresh head sha is the only remaining lever. This commit is empty (tree byte-identical to its parent) and only mints that head. No source change. Co-Authored-By: Claude Opus 5 <[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 units7 translation units recompiled, 6 s compile time in total, 7 of them have a recent master baseline. |
…ests The comment justifying the ASTStorage else-branch claimed storage->children is the sole owner of the settings slot in production, so an unbacked slot always means freed. Nothing guarantees that. The only caller strips right after QueryFuzzer::fuzzMain returns, and master's fuzzer holds an owning ASTPtr for every node it visited (debug_visited_nodes became unordered_map<const IAST *, ASTPtr>, cleared only at the start of the next fuzzMain, after this branch forked). An unbacked slot can therefore still be live, and clearing it then drops that clause whole instead of stripping only the pinned safety caps. The behaviour does not change, because an owning child in storage->children remains the only proof reachable from here that the raw slot still points at that same node. Every address-based alternative fails on the same case: a whole-tree reachable set, an owning-pointer address set, and the fuzzer's own address-keyed map (whose entries are erased and re-inserted as nodes get replaced) can all be satisfied by an unrelated node that reused a freed address, and acting on that would dereference freed memory or mutate the wrong node. So an unbacked slot is still cleared unread; what changes is that the comment states the real contract (liveness unprovable, not disproved) and names the cost of the safe direction. Both tests get a real oracle for that cost. ClearsStorageSettingsSlotNotBackedByAnOwningChild already used a mixed clause (max_rows_to_read = 0, index_granularity = 1024) on a kept-alive unbacked slot; it now also asserts index_granularity is absent from the output. With the else-branch stripping in place instead of clearing, that assertion fails with SETTINGS index_granularity = 1024 still serialized. ToleratesSettingsSlotDesyncedFromChildren could not see whether the slot was detached at all: its setup removes the settings node from children, and both remaining checks (no-throw and settingNamePresent) walk children, so a transform that erased the child but left the owning field set passed. The owner formatters print from that field, so such a query re-serializes as a bare SETTINGS and the fuzzer skips it instead of running it under the caps. The test now asserts each desynced carrier's own slot is null and re-parses the formatted result. Measured with detachChild(IAST &, ASTPtr &) no longer resetting the field: the old test passes, the new one fails with "query slot not detached". It also asserts that the unbacked node comes out untouched, which is the half of "never dereferenced" a test can pin without constructing a use-after-free itself. The refuted-alternative narration moves out of findOwningChild and the test header into this message, where it belongs. Co-Authored-By: Claude Opus 5 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Description
The server-side AST fuzzer strips its safety-limit settings (
max_rows_to_read,max_execution_time, ...) from each fuzzed query so a fuzzedSETTINGS max_rows_to_read = 0cannot lift the pinned resource caps. When stripping empties a SETTINGS clause,removeSettingsFromQuerydetached the clause from its owner viaIAST::reset, which hard-throwsLOGICAL_ERROR "AST subtree not found in children"if the owner's settings slot is set but its pointer is not registered inchildren.The AST fuzzer explicitly can produce such structurally-invalid ASTs (
executeQuery.cpponly skips them at format time, and this strip runs before that guard), so the desync aborted the server in debug/sanitizer builds instead of being skipped. Found by BuzzHouse (amd_tsan):stripFuzzerSafetyLimitSettings->removeSettingsFromQuery(stripQuerySettings.cpp:140) ->IAST::reset(IAST.h:269).CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=109000&sha=8b77fe6ef18a638bcbc188de2e154324262cef13&name_0=PR&name_1=BuzzHouse%20%28amd_tsan%29
Fix: detach the emptied slot tolerantly (erase the child if present, then clear the slot) in the INSERT,
ASTQueryWithOutputandASTStoragebranches, matching the already-tolerant SELECT (setExpression({})) and BACKUP (settings.reset()) branches. Adds a gtest that desyncs the settings slot fromchildrenfor each affected carrier and asserts the strip no longer aborts.Workflow [PR]
Sync PR [sync-upstream/pr/110394]
Version info
26.10.1.782-master(included in26.10and later)