Repository navigation
Guard the constant-to-literal walks with checkStackSize - #120885
Conversation
Turning a constant into an AST literal walks the value recursively, and for `Dynamic`/`JSON` the nesting is part of the value rather than of the query text, so no parser limit bounds it: a `JSON` path holding a `Dynamic` holding a `JSON` costs three stack frames per level, and the depth comes straight from the binary value. A deeply nested value therefore ran past the stack guard page and killed the process instead of failing the query. Add `checkStackSize` to `getFieldFromColumnForASTLiteralImpl` and `columnConstantToExactLiteralASTImpl`, which turns this into `TOO_DEEP_RECURSION`, matching what the `Field` visitors already report for the same data. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Workflow [PR], commit [861edf3] Summary: ✅
AI ReviewSummaryThis PR adds Missing context / blind spots
Final Verdict✅ Looks good to merge. LLVM Coverage ReportMeasured on commit 861edf3.
Changed lines: Changed C/C++ lines covered: 9/9 (100.00%) · Uncovered code |
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, 18 s compile time in total, 8 of them have a recent master baseline. |
Reading a value as a `Field` walks the column recursively, and a `JSON` path holding a `Dynamic` holding a `JSON` cycles between `ColumnObject`, `ColumnDynamic` and `ColumnVariant` once per nesting level. The nesting is part of the value rather than of the query text, so no parser limit bounds it, and a deeply nested value ran past the stack guard page and killed the process. Add `checkStackSize` to `ColumnObject::operator[]` and `ColumnDynamic::get`, the two entry points of that cycle, so it reports `TOO_DEEP_RECURSION` instead. Found by the AST fuzzer, which mutated the constant from the test added here into `if(<deeply nested Dynamic>, 1, 2)`: constant folding reads the condition as a `Field` and enters the walk. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
# Conflicts: # src/Analyzer/Utils.cpp
… value
A Dynamic value can hold a JSON object, a JSON object's dynamic path is a
Dynamic, and JSON/Object is a legal Variant member (checkAllowedInsideVariant
forbids only Nullable, LowCardinality(Nullable), a nested Variant and a direct
Dynamic). So one value can nest Object -> Dynamic -> Variant -> Object to any
depth, and that depth comes from the data: the declared type here is just
Dynamic, and each level's small type is decoded separately, so no type
complexity or parser limit bounds it.
Reading one row of such a value recurses once per level through five frames:
ColumnObject::operator[] (ColumnObject.cpp:305)
-> ColumnDynamic::operator[] (ColumnDynamic.cpp:318)
-> ColumnDynamic::get (ColumnDynamic.cpp:328)
-> ColumnVariant::get (ColumnVariant.cpp:428)
-> ColumnObject::get (ColumnObject.cpp:327)
-> ColumnObject::operator[] ... repeats
at about 2.8 KB of stack per level. The one checkStackSize() that path already
reaches sits inside convertFieldToType, called at ColumnDynamic.cpp:330 after
variant_col.get(n, res) has returned, so it runs only once the descent has
completed and cannot stop the descent that overflows. Measured on a debug build
with an 8 MB stack: 1500 and 2000 levels report TOO_DEEP_RECURSION from that
call, while 3000 and 4000 levels take SIGSEGV on the way down. An -O2 build
behaves the same (these frames are dominated by Field, String and Object
temporaries rather than spills, so optimisation does not shrink them), and the
official 26.8.10.6-lts build dies the same way at default settings.
Checking the stack at the top of ColumnDynamic::get moves the check onto the way
down, once per level. Every data-driven level of this nesting passes through a
ColumnDynamic per-value method, because a JSON object's dynamic paths are
ColumnDynamic columns and its shared-data values are decoded by
SerializationDynamic, which is already guarded.
ColumnObject and ColumnVariant are deliberately left unguarded. Their nested
types come from the declared type, and DataTypeFactory.cpp:128-130 caps a
declared type's parse depth at 150 (debug) / 300, two orders of magnitude below
the depths that overflow; a probe confirms a deep JSON(a JSON(...)) is rejected
with "Maximum parse depth (150) exceeded" and the server survives. Adding a
guard there would also re-litigate 2fcfc07, which removed exactly that
construct one layer down in ClickHouse#115702 for its per-container-value cost.
About twenty further per-value methods on these three columns (insert*,
compareAt, computeHashInto, updateHashWithValue, serializeValueIntoArena,
compress, byteSizeAt, ...) recurse identically, with no observed overflow and no
reproducer; the reproducers for updateHashWithValue and getValueNameImpl are
pre-empted on this base by the analyzer recursion that ClickHouse#120885 guards, so they
are not covered here.
Alternatives rejected: guarding the entry points (eight-plus sites for one arm,
and it guards the symptom); bounding the nesting depth when the value is built
(a behaviour change that rejects values accepted today, and it cannot protect a
read of data an older server already wrote); guarding the operator[] alias as
well as get (operator[] is get, so it would double-check).
The regression test uses 4000 levels, the depth CI found and a depth measured to
overflow both a debug and an -O2 build. It should not be raised much above that:
from about 8000 levels the type-side guard in IDataType::getDefaultSerialization
fires first and returns the same error code without the column recursion ever
running deep.
Found by the AST fuzzer, STID 4709-36d5:
https://github.com/ClickHouse/ClickHouse/actions/runs/35623644754/job/106452545848
Backport #120885 to 26.9: Guard the constant-to-literal walks with checkStackSize
Backport #120885 to 26.8: Guard the constant-to-literal walks with checkStackSize
Backport #120885 to 26.7: Guard the constant-to-literal walks with checkStackSize
Backport #120885 to 26.3: Guard the constant-to-literal walks with checkStackSize
Turning a constant into an AST literal walks the value recursively. For
Dynamic/JSONthe nesting is part of the value rather than of the query text, so no parser limit bounds it: aJSONpath holding aDynamicholding aJSONcosts three stack frames per level, and the depth comes straight from the binary value. A deeply nested constant therefore ran past the stack guard page and terminated the server process instead of failing the query.checkStackSizeingetFieldFromColumnForASTLiteralImplandcolumnConstantToExactLiteralASTImplturns this intoTOO_DEEP_RECURSION, matching what theFieldvisitors already report for the same data.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a crash on a query with a deeply nested
DynamicorJSONconstant.Workflow [PR]
Sync PR [sync-upstream/pr/120885]
Version info
26.10.1.1859-master(included in26.10and later)26.9.17.8,26.8.25.14,26.7.28.14,26.3.50.5