Skip to content

Cherry pick #126548 to 26.3: Limit recursion depth when deserializing query plans - #127046

Merged
robot-clickhouse-ci-2 merged 10 commits into
backport/26.3/126548from
cherrypick/26.3/126548
Oct 21, 2026
Merged

robot-clickhouse-ci-2 merged 10 commits into
backport/26.3/126548from
cherrypick/26.3/126548

Conversation

@robot-clickhouse

Copy link
Copy Markdown
Member

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-pick locally.
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:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

alexey-milovidov and others added 9 commits October 17, 2026 23:53
`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]>
…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]>
…e-recursion

Limit recursion depth when deserializing query plans
@robot-clickhouse robot-clickhouse added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-critical-bugfix labels Oct 20, 2026
Resolve conflicts by adapting the fix to the 26.3 API: there is no
`max_type_complexity` parameter in `ActionsDAG::deserialize` and
`QueryPlan::deserialize`, and `QueryPlan::SerializationFlags` was an empty
struct defined in `Serialization.cpp`. Move its definition into `QueryPlan.h`
(as on master) so that `SetsSerialization.cpp` can copy it and bump
`subquery_nesting_depth`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robot-clickhouse-ci-2
robot-clickhouse-ci-2 merged commit d7930f7 into backport/26.3/126548 Oct 21, 2026
1 check passed
@robot-clickhouse-ci-2
robot-clickhouse-ci-2 deleted the cherrypick/26.3/126548 branch October 21, 2026 11:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! pr-critical-bugfix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants