Repository navigation
Cherry pick #126548 to 26.7: Limit recursion depth when deserializing query plans - #127047
Merged
robot-clickhouse-ci-2 merged 10 commits intoOct 21, 2026
Merged
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]>
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]>
…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]>
…e-recursion Limit recursion depth when deserializing query plans
Resolve conflicts: on 26.7, `QueryPlan::SerializationFlags` was only forward-declared in `QueryPlan.h` and defined in `Serialization.cpp`. Move the definition into `QueryPlan.h` (as on master) with the new `subquery_nesting_depth` field, because `SetsSerialization.cpp` now copies the flags to increment the nesting depth. Keep only the error codes that 26.7 uses in `SetsSerialization.cpp` plus `TOO_DEEP_RECURSION`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Original pull-request #126548
Do not merge this PR manually
This pull-request is a first step of an automated backporting.
It contains changes similar to calling
git cherry-picklocally.If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.
The check results does not matter at this step - you can safely ignore them.
Before you resolve anything
Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.
Troubleshooting
If the conflicts were resolved in a wrong way
If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:
pr-cherrypicklabel from the PRYou also need to check the Original pull-request for
pr-backports-createdlabel, and delete if it's presented thereThe PR source
The PR is created in the CI job