Skip to content

Fix server crash on a deeply nested Field in a binary encoded type - #122667

Merged
Avogar merged 4 commits into
masterfrom
security-fix-6
Sep 29, 2026
Merged

Avogar merged 4 commits into
masterfrom
security-fix-6

Conversation

@Avogar

@Avogar Avogar commented Sep 28, 2026 •

Copy link
Copy Markdown
Member

Decoding a binary encoded Field recursed into itself for the container fields with no bound of any kind. The nesting depth is a property of the data rather than of the query — a Dynamic value carries its own type, and the parameters of an AggregateFunction type are Fields — so a deeply nested parameter ran past the stack guard page and terminated the server process instead of failing the query. It was reachable on the ordinary read path of a Dynamic column, so a single INSERT ... FORMAT RowBinary was enough.

Both directions of the binary Field encoding now call checkStackSize in their recursive branches, matching what FieldVisitorWriteBinary and the other Field visitors already do, so deep values are reported as TOO_DEEP_RECURSION.

In addition, decodeDataTypeImpl counts decoded nodes against input_format_binary_max_type_complexity but handed off to decodeField without the counter, so the limit stopped at the type/field boundary and a small type header could still expand into an arbitrary amount of work. The counter is now threaded through decodeAggregateFunction into decodeField, so Field nodes are charged to the same budget. The single argument decodeField keeps its signature and stays unlimited, which preserves the property (pinned by 04303_binary_max_type_complexity_input_only) that reading already stored values is never limited by the setting.

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 server crash caused by unbounded recursion when decoding a deeply nested value inside a binary encoded type, for example an AggregateFunction parameter in a Dynamic value read from RowBinary or Native.


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

Version info

  • Merged into: 26.10.1.996-master (included in 26.10 and later)
  • Backported to: 26.9.9.23, 26.8.16.28, 26.7.20.19, 26.3.40.12

Avogar and others added 3 commits September 28, 2026 11:29
`decodeField` recursed into itself for the container fields with no bound of
any kind, and the nesting depth is a property of the data rather than of the
query: a `Dynamic` value carries its own type, and the parameters of an
`AggregateFunction` type are Fields. A deeply nested parameter therefore ran
past the stack guard page instead of failing the query.

Add `checkStackSize` to the recursive branches of both directions, matching
what `FieldVisitorWriteBinary` and the other Field visitors already do, so
deep values are reported as `TOO_DEEP_RECURSION`.

`decodeDataTypeImpl` counts decoded nodes against
`input_format_binary_max_type_complexity`, but handed off to `decodeField`
without the counter, so the limit stopped at the type/field boundary and a
tiny type header could still expand into an arbitrary amount of work. Thread
the counter through `decodeAggregateFunction` into `decodeField` so that
Field nodes are charged to the same budget. The single argument `decodeField`
keeps its signature and stays unlimited, which preserves the property that
reading already stored values is never limited by the setting.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
# Conflicts:
#	src/Core/FormatFactorySettings.h
05219 is taken by several tests on master now.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@clickhouse-gh clickhouse-gh Bot added the comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). label Sep 28, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6010df9]

Summary: ✅


AI Review

Summary

This PR closes the unbounded-recursion hole in binary Field encoding/decoding for deeply nested values inside binary-encoded types, and the code paths look sound overall. I did not find a remaining correctness issue in the fix itself, but there is one user-facing documentation detail to tighten before merge.

Findings

💡 Nits

  • [src/Core/FormatFactorySettings.h:945] The updated input_format_binary_max_type_complexity description mentions only AggregateFunction, but the new limit also applies to SimpleAggregateFunction because both binary type tags decode through decodeAggregateFunction. The new regression test exercises that SimpleAggregateFunction path already, so the setting text is now slightly inaccurate.
    Suggested fix: mention both AggregateFunction and SimpleAggregateFunction in the description.
Final Verdict

One nit to fix in the user-facing setting description; otherwise the recursion and complexity-limit changes look correct.

LLVM Coverage Report

Measured on commit 6010df9.

Metric Baseline Current Δ
Lines 87.90% 87.90% +0.00%
Functions 91.80% 91.70% -0.10%
Branches 79.30% 79.30% +0.00%

Changed lines: Changed C/C++ lines covered: 72/72 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! labels Sep 28, 2026
@Algunenano Algunenano self-assigned this Sep 28, 2026

@Algunenano Algunenano left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

The two guards race: whichever of the node counter and the stack check is
reached first decides the error code, and the stack allowance depends on the
build - Thread Sanitizer permits only 5% of the stack, so a thousand decode
frames exhaust it and the query fails with `TOO_DEEP_RECURSION` instead of
the expected `INCORRECT_DATA`.

Lower the budget in the two queries that assert the limit so it trips after
about ten nodes, which no build can turn into an exhausted stack.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
)", 0) \
DECLARE(UInt64, input_format_binary_max_type_complexity, 1000, R"(
Max type nodes when decoding binary types (not depth, but total count). `Map(String, UInt32)` = 3 nodes. Protects against malicious inputs. 0 = unlimited.
Max type nodes when decoding binary types (not depth, but total count). `Map(String, UInt32)` = 3 nodes. Parameters of `AggregateFunction` types count as one node per value, including nested ones. Protects against malicious inputs. 0 = unlimited.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

input_format_binary_max_type_complexity now applies to SimpleAggregateFunction too, not only AggregateFunction. Both BinaryTypeIndex::AggregateFunction and BinaryTypeIndex::SimpleAggregateFunction decode through decodeAggregateFunction, and the new regression test in 05291_binary_encoded_field_deep_recursion.sql is actually exercising the SimpleAggregateFunction case.

As written, system.settings will tell users that only AggregateFunction(...) payloads are charged here, which is now inaccurate. Please mention both surfaces in this description.

@clickhouse-gh clickhouse-gh Bot added comp-data-types Type system & core datatypes used across query planning/execution. and removed comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). labels Sep 28, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 28, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 6010df92f with master 2bfd00884 (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

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

Median compile-time ratio to the baselines is ×1.17 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost +14.0 s (+17%) in total before that adjustment.

Job report

@Avogar
Avogar added this pull request to the merge queue Sep 29, 2026
Merged via the queue into master with commit 5ac346e Sep 29, 2026
149 checks passed
@Avogar
Avogar deleted the security-fix-6 branch September 29, 2026 13:20
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 29, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Sep 29, 2026
PedroTadim added a commit that referenced this pull request Oct 2, 2026
Cherry pick #122667 to 26.3: Fix server crash on a deeply nested Field in a binary encoded type
PedroTadim added a commit that referenced this pull request Oct 2, 2026
Cherry pick #122667 to 26.7: Fix server crash on a deeply nested Field in a binary encoded type
PedroTadim added a commit that referenced this pull request Oct 2, 2026
Cherry pick #122667 to 26.8: Fix server crash on a deeply nested Field in a binary encoded type
PedroTadim added a commit that referenced this pull request Oct 2, 2026
Cherry pick #122667 to 26.9: Fix server crash on a deeply nested Field in a binary encoded type
robot-clickhouse added a commit that referenced this pull request Oct 2, 2026
robot-clickhouse added a commit that referenced this pull request Oct 2, 2026
robot-clickhouse added a commit that referenced this pull request Oct 2, 2026
robot-clickhouse added a commit that referenced this pull request Oct 2, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 2, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 2, 2026
Backport #122667 to 26.8: Fix server crash on a deeply nested Field in a binary encoded type
clickhouse-gh Bot added a commit that referenced this pull request Oct 2, 2026
Backport #122667 to 26.7: Fix server crash on a deeply nested Field in a binary encoded type
clickhouse-gh Bot added a commit that referenced this pull request Oct 2, 2026
Backport #122667 to 26.9: Fix server crash on a deeply nested Field in a binary encoded type
PedroTadim added a commit that referenced this pull request Oct 2, 2026
Backport #122667 to 26.3: Fix server crash on a deeply nested Field in a binary encoded type
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-data-types Type system & core datatypes used across query planning/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