Skip to content

Limit recursion depth when deserializing query plans - #126548

Merged
alexey-milovidov merged 8 commits into
masterfrom
fix-actions-dag-deserialize-recursion
Oct 20, 2026
Merged

alexey-milovidov merged 8 commits into
masterfrom
fix-actions-dag-deserialize-recursion

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Oct 18, 2026 •

Copy link
Copy Markdown
Member

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. Now the nesting depth is limited to 1000 levels (TOO_DEEP_RECURSION), and checkStackSize is called in these recursions.

The test 05427_query_plan_deserialize_deep_recursion speaks the native protocol directly and sends a hand-crafted QueryPlan packet with 5000 nested lambda captures (plans generated from SQL cannot be nested that deep without hitting max_parser_depth first).

Changelog category (leave one):

  • Critical Bug Fix (crash, data loss, RBAC)

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 QueryPlan packet of the native protocol.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/126548]

Version info

  • Merged into: 26.11.1.354-master (included in 26.11 and later)
  • Backported to: 26.10.2.50, 26.9.24.6, 26.8.33.11, 26.7.36.13, 26.3.59.4

`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]>
@clickhouse-gh

clickhouse-gh Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [853221d]

Summary: ✅


AI Review

Summary

The PR bounds recursive lambda-capture and set-subquery plan deserialization with depth limits and stack checks, returning TOO_DEEP_RECURSION for excessive nesting. No actionable findings remain on 853221d8753375c80b6263f5949a7f1a760c51e1. The crafted-packet regression passes in Fast test.

Final Verdict

✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. labels Oct 18, 2026
Comment thread src/Interpreters/ActionsDAG.cpp Resolved
alexey-milovidov and others added 2 commits October 18, 2026 19:52
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]>
Comment thread src/Interpreters/ActionsDAG.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 853221d87 with master bfaa2a05e (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

1275 translation units recompiled, 9364 s compile time in total, 1275 of them have a recent master baseline.

Job report

alexey-milovidov and others added 5 commits October 19, 2026 00:05
…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]>

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good.

@alexey-milovidov alexey-milovidov self-assigned this Oct 20, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 20, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 20, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 20, 2026
Merged via the queue into master with commit 230993d Oct 20, 2026
179 of 180 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-actions-dag-deserialize-recursion branch October 20, 2026 22:07
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 20, 2026
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 20, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 21, 2026
Backport #126548 to 26.10: Limit recursion depth when deserializing query plans
clickhouse-gh Bot added a commit that referenced this pull request Oct 21, 2026
Backport #126548 to 26.9: Limit recursion depth when deserializing query plans
robot-clickhouse-ci-2 added a commit that referenced this pull request Oct 21, 2026
Cherry pick #126548 to 26.3: Limit recursion depth when deserializing query plans
robot-clickhouse-ci-2 added a commit that referenced this pull request Oct 21, 2026
Cherry pick #126548 to 26.7: Limit recursion depth when deserializing query plans
@robot-clickhouse robot-clickhouse added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 21, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 21, 2026
Backport #126548 to 26.8: Limit recursion depth when deserializing query plans
clickhouse-gh Bot added a commit that referenced this pull request Oct 21, 2026
Backport #126548 to 26.7: Limit recursion depth when deserializing query plans
alexey-milovidov added a commit that referenced this pull request Oct 21, 2026
Backport #126548 to 26.3: Limit recursion depth when deserializing query plans
strtgbb pushed a commit to Altinity/ClickHouse that referenced this pull request Oct 22, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Oct 31, 2026
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants