Repository navigation
Limit recursion depth when deserializing query plans - #126548
Conversation
`ActionsDAG::deserialize` and `deserializeConstant` call each other recursively for every nested lambda capture, and `QueryPlan::deserialize` is called recursively for plans of sets from subqueries. None of them limited the depth, so a crafted serialized query plan received in the `QueryPlan` packet of the native protocol could exhaust the thread stack. Note that such a packet is deserialized even if `process_query_plan_packet` is disabled, to skip it. Limit the nesting depth of lambda captures in `ActionsDAG` and of plans for sets from subqueries to 1000 levels, throwing `TOO_DEEP_RECURSION`, and also call `checkStackSize` in these recursions. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Workflow [PR], commit [853221d] Summary: ✅
AI ReviewSummaryThe PR bounds recursive lambda-capture and set-subquery plan deserialization with depth limits and stack checks, returning Final Verdict✅ Approve |
Regression test `05427_query_plan_deserialize_deep_recursion` for the unbounded-recursion exception in `ActionsDAG::deserialize`. A native-protocol `QueryPlan` packet (packet type 13) can carry an `ActionsDAG` whose lambda-capture constant contains another lambda-capture constant, nested arbitrarily deep. `ActionsDAG::deserialize` and `deserializeConstant` recurse once per level with no depth limit and no `checkStackSize`, so a client that sends a few thousand nested captures exhausts the server stack and the process dies with `SIGSEGV`. The fix caps the nesting depth and adds `checkStackSize`, turning the attack into a `TOO_DEEP_RECURSION` exception while the server keeps serving. The test speaks the raw native TCP protocol (pinned to revision 54482), completes the handshake, sends a `Query` packet with the `QueryPlan` processing stage, then a hand-crafted `QueryPlan` packet whose single `Expression` step holds an `ActionsDAG` with 5000 nested lambda-capture constants. It expects a `TOO_DEEP_RECURSION` exception and verifies the server is still alive afterwards. The packet is only reachable while the server has `process_query_plan_packet` enabled, so a small `tests/config/config.d/query_plan_packet.xml` turns it on for stateless tests. Verified against a fixed ASan build (test passes, `TOO_DEEP_RECURSION`) and against the unfixed 26.10 release binary (server crashes with `Segmentation fault` in the `deserializeConstant` / `ActionsDAG::deserialize` recursion, follow-up query gets `Connection reset by peer`). Co-Authored-By: Claude Opus 5.5 <[email protected]>
…nabled for tests Co-Authored-By: Claude Opus 5.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 units1275 translation units recompiled, 9364 s compile time in total, 1275 of them have a recent master baseline. |
…throw it first in sanitizer builds Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e` exception The `TOO_DEEP_RECURSION` message from `checkStackSize` contained the stack and frame addresses. It can be sent to a client (e.g. when deserializing a `QueryPlan` packet), so keep only the used and maximum stack sizes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A plan with 10 nested lambda captures must not be rejected with `TOO_DEEP_RECURSION`, which distinguishes the depth limit from rejecting every plan. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…erialize-recursion Co-Authored-By: Claude Opus 5.5 <[email protected]>
Backport #126548 to 26.10: Limit recursion depth when deserializing query plans
Backport #126548 to 26.9: Limit recursion depth when deserializing query plans
Cherry pick #126548 to 26.3: Limit recursion depth when deserializing query plans
Cherry pick #126548 to 26.7: Limit recursion depth when deserializing query plans
Backport #126548 to 26.8: Limit recursion depth when deserializing query plans
Backport #126548 to 26.7: Limit recursion depth when deserializing query plans
Backport #126548 to 26.3: Limit recursion depth when deserializing query plans
…alizing query plans
Master now limits the nesting depth of a deserialized ActionsDAG to 1000 levels (ClickHouse#126548), counted through the recursive calls for lambda captures. The predicate of indexHint is deserialized by the same recursion, but the call did not pass the depth, so a QueryPlan packet with nested indexHint nodes was not covered by the limit (only by checkStackSize). Measured on a debug build: chains of 1001, 1500 and 3000 levels were accepted, 5000 was rejected by checkStackSize. Now every chain above 1000 levels gets TOO_DEEP_RECURSION, and 05427 has an indexHint arm at 1500 levels. Also use FunctionIndexHint::tryGet, which ClickHouse#126219 added on master, instead of the same two casts. Co-Authored-By: Claude Opus 5.5 <[email protected]>
ActionsDAG::deserializeanddeserializeConstantcall each other recursively for every nested lambda capture, andQueryPlan::deserializeis called recursively for plans of sets from subqueries. None of them limited the depth, so a crafted serialized query plan received in theQueryPlanpacket of the native protocol could exhaust the thread stack. Now the nesting depth is limited to 1000 levels (TOO_DEEP_RECURSION), andcheckStackSizeis called in these recursions.The test
05427_query_plan_deserialize_deep_recursionspeaks the native protocol directly and sends a hand-craftedQueryPlanpacket with 5000 nested lambda captures (plans generated from SQL cannot be nested that deep without hittingmax_parser_depthfirst).Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a stack overflow on a deeply nested query plan received in the
QueryPlanpacket of the native protocol.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/126548]
Version info
26.11.1.354-master(included in26.11and later)26.10.2.50,26.9.24.6,26.8.33.11,26.7.36.13,26.3.59.4