Skip to content

Reject deeply nested input in formats to prevent stack overflow - #107341

Merged
Algunenano merged 17 commits into
ClickHouse:masterfrom
Algunenano:msgpack-schema-recursion
Jun 22, 2026
Merged

Algunenano merged 17 commits into
ClickHouse:masterfrom
Algunenano:msgpack-schema-recursion

Conversation

@Algunenano

@Algunenano Algunenano commented Jun 12, 2026 •

Copy link
Copy Markdown
Member

Several input formats build a ClickHouse data type from input-controlled nesting (schema or value) without bounding the recursion depth. A small, deeply nested file could therefore overflow the native stack and crash the server — a denial of service reachable just by reading a crafted file. This bounds the recursion across the affected formats:

  • MsgPack, BSON, ORC and Parquet schema inference, and JSON inference now reject nesting beyond max_parser_depth (or hit a checkStackSize backstop) instead of recursing unboundedly. The Parquet check is unconditional, since the previous definition-level guard was bypassed by chains of required groups.
  • Poco::JSON::Parser recursed with unlimited depth by default, which affected DeltaLake/Iceberg/Paimon metadata parsing and their REST/Unity catalogs, the Avro/Confluent schema registry, and other call sites that parse untrusted JSON through it. It now has a bounded default nesting depth (callers needing more can opt in via setDepth).

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):

Fixed possible server stack overflow (crash) when reading a deeply nested schema or value in the MsgPack, BSON, ORC, Parquet, JSON, DeltaLake, Iceberg and Paimon formats. Such deeply nested input is now rejected with an exception.

Version info

  • Merged into: 26.6.1.1124
  • Backported to: 26.5.4.3, 26.4.5.62, 26.3.15.4, 25.8.25.32

Algunenano and others added 9 commits June 11, 2026 17:48
MsgPack schema inference recursed over nested arrays/maps with no depth
limit. `msgpack::unpack` builds the object tree iteratively on the heap,
so a tiny, deeply nested object overflowed the native stack and crashed
the process, both in `MsgPackSchemaReader::getDataType` and, for moderate
depths, in the shared `makeNullableRecursively` walk over the inferred
type.

`getDataType` now rejects nesting beyond `max_parser_depth` early (before
building the type) with `TOO_DEEP_RECURSION`, keeping `checkStackSize` as
a backstop, and no longer self-compares the first array element (which
recursed the whole type via `DataTypeArray::equals`). The shared
inference walks `adjustNullableRecursively`, `checkIfTypeIsComplete`, and
`transformFinalInferredJSONTypeIfNeededImpl` now call `checkStackSize`.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Add a case feeding two deep objects so multi-row type merging
(`chooseResultColumnType` -> `IDataType::equals`) is exercised with a
raised `max_parser_depth` and nullable finalization. It must still be
rejected as `TOO_DEEP_RECURSION`, never crash.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
BSONEachRow schema inference recursed over nested documents/arrays in
`getDataTypesFromBSONDocument` <-> `getDataTypeFromBSONField` with no
depth limit, so a tiny, deeply nested BSON document overflowed the native
stack and crashed the process.

`getDataTypesFromBSONDocument` now rejects nesting beyond `max_parser_depth`
early (before building the type) with `TOO_DEEP_RECURSION`, threading a
depth through `getDataTypeFromBSONField`, and keeps `checkStackSize` as a
backstop. The row reader `readField` also gets a `checkStackSize` guard.

Same class as the MsgPack schema-inference fix.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
`parseORCType` recursed over nested ORC LIST/MAP/STRUCT types with no
depth limit and ignored `max_parser_depth`, and the ORC library does not
bound nesting. A small but deeply nested ORC schema made schema inference
recurse and then walk a pathologically deep type, burning a core for tens
of seconds (uncancellable) at default settings, and could overflow the
native stack.

`parseORCType` now rejects nesting beyond `max_parser_depth` early with
`TOO_DEEP_RECURSION` (depth threaded through the LIST/MAP/STRUCT
recursion; callers pass the setting), with `checkStackSize` as a backstop.
The data-read path `readColumnFromORCColumn` also gets a `checkStackSize`.

Same class as the MsgPack and BSON schema-inference fixes.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
`SchemaConverter::processSubtree` recurses over nested groups, but its only
depth guard checked the definition level (`def == UINT8_MAX`), which is
incremented only for OPTIONAL/REPEATED nodes. A chain of REQUIRED
(non-nullable) groups never advances `def`, so it bypassed the guard and
overflowed the native stack (SIGSEGV) at default settings.

Track the real recursion depth unconditionally and reject nesting beyond
`max_parser_depth` with `TOO_DEEP_RECURSION`, with `checkStackSize` as a
backstop. `Reader::formOutputColumn` (read path) also gets a `checkStackSize`.

Same class as the MsgPack, BSON, and ORC schema-inference fixes.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
JSON value inference (`tryInferDataTypeForSingleFieldImpl`,
`tryInferArray`/`tryInferTuple`/`tryInferMapOrObject`, `tryReadJSONObject`)
was guarded only by `depth > max_parser_depth`. That limit is user-tunable,
so raising `max_parser_depth` let a deeply nested JSON value overflow the
native stack (SIGSEGV) during inference.

Add a `checkStackSize` backstop at the recursion entry points, matching
the already-correct `skipJSONFieldImpl`, so deep nesting is always
rejected as `TOO_DEEP_RECURSION` regardless of `max_parser_depth`.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
DeltaLake and Iceberg parse table-metadata schema JSON with
`Poco::JSON::Parser`, which defaults to unlimited nesting depth. A deeply
nested metadata/schema document overflowed the native stack inside Poco's
recursive parser (SIGSEGV) - reachable at default settings for Iceberg, and
for DeltaLake's C++ path with `allow_experimental_delta_kernel_rs=0`.

`Poco::JSON::Parser::setDepth` existed but was a no-op (the value was stored
but never checked), so wire the depth limit into Poco's `handleArray`/
`handleObject` recursion: it now throws once nesting exceeds the configured
depth, but only when a limit is set (default stays unlimited, so other Poco
JSON users are unaffected). The Delta/Iceberg metadata parsers now call
`setDepth(DBMS_DEFAULT_MAX_PARSER_DEPTH)`, so deep schemas are rejected with
a clean JSON exception instead of crashing.

Same class as the MsgPack/BSON/ORC/Parquet/JSON schema-recursion fixes.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
PaimonTableClient parses the (attacker-controlled) schema and snapshot
metadata JSON with `Poco::JSON::Parser` at the default unlimited depth, so
a deeply nested Paimon metadata file overflowed the native stack inside
Poco's recursive parser (SIGSEGV) - reachable at default settings via
`paimonLocal` and the other `paimon*` table functions.

Call `setDepth(DBMS_DEFAULT_MAX_PARSER_DEPTH)` at both parse sites, like the
Delta/Iceberg fix, so deep metadata is rejected with a clean JSON exception.
Extends the datalake regression test with a Paimon case.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Defense-in-depth follow-up to the per-format depth fixes. `Poco::JSON::Parser`
defaulted to unlimited nesting depth, and many sites parse untrusted JSON
through it without calling `setDepth` - datalake metadata (Delta/Iceberg/
Paimon) and their REST/Unity catalogs, the Confluent/Avro schema registry,
HTTP/cloud responses, and ~30 other sites across src/. Any of them could
overflow the native stack on a deeply nested document.

Rather than chase each call site, default `ParserImpl`'s depth to
`JSON_DEFAULT_DEPTH` (1000) so every Poco JSON parse is bounded; callers that
genuinely need more can opt in via `setDepth`. The enforcement itself was
wired into Poco's `handleArray`/`handleObject` recursion in the previous
commit; this just changes the default. 1000 is far above any legitimate JSON
(real nesting is well under 100, and ClickHouse's JSON type uses simdjson/
rapidjson, not Poco), and part metadata (`serialization.json`) is a flat
structure, so internal use is unaffected (verified by a nested-type part
round-trip).

This subsumes the explicit Delta/Iceberg/Paimon `setDepth` calls added
earlier, which are removed here as redundant; the datalake regression test
(04340) now passes via the default and additionally covers catalogs, schema
registry, and the other Poco JSON parsers.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jun 12, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [1661248]

Summary: ✅


AI Review

Summary

This PR bounds recursion while inferring or reading deeply nested MsgPack, BSONEachRow, ORC, Parquet, JSONEachRow, and datalake metadata inputs, including the default Poco::JSON::Parser path. The current code addresses the prior max_parser_depth=0 and explicit-schema ORC review threads, and I found no new changed-line findings that need inline comments.

Missing context / blind spots
  • ⚠️ The direct Praktika PR report for commit 16612487ba64606592cb5b185e4f0007d929b310 returned zero test-result rows, and I did not run the new stateless tests locally because this checkout has no usable build directory. A green CI run for the added 04335-04341 regression tests would close this gap.
Final Verdict

Status: ✅ 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! labels Jun 12, 2026
…pth)

prepareFileReader maps each requested column onto the file schema with
traverseDownORCTypeByName and updateIncludeTypeIds, which recurse the
file-controlled nested LIST/MAP/STRUCT tree before any stripe (and before the
readColumnFromORCColumn guard) is reached. This is not independently
exploitable: the recursion depth is bounded by the explicit CH type, whose
depth is in turn bounded by the SQL parser (max_parser_depth plus its own
checkStackSize) before it can reach these helpers. Add checkStackSize to both
anyway, matching readColumnFromORCColumn, so every walk over a file-controlled
type tree has a backstop. Includes an explicit-schema ORC read regression test.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@azat azat self-assigned this Jun 12, 2026
Algunenano and others added 2 commits June 15, 2026 13:14
The flaky check killed the server on the ORC deep-recursion test: a deeply
nested ORC schema (8000 levels) overflowed the native stack inside the Apache
ORC C++ library's convertType, which builds the orc::Type tree by recursing
over the file footer before any ClickHouse code runs. The OOM killer then
reaped the server. parseORCType (ClickHouse) cannot prevent this because it runs
on the already-built tree.

Bump contrib/orc to bound the footer type-tree nesting depth and reject a
crafted deeply nested schema with a ParseError instead of crashing.

Rework 04337 accordingly: use a moderate depth (which the library builds fine)
with a lowered max_parser_depth to exercise the parseORCType guard
deterministically, plus a deeper file to exercise the new ORC library limit.

Also address review feedback on BSONEachRow: move checkStackSize out of the hot
per-field readField into the recursive container readers (readArray/readTuple/
readMap), so flat fields do not pay for it.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@Algunenano

Copy link
Copy Markdown
Member Author

It'd be great if GitHub picked the last push to the branch

@clickhouse-gh clickhouse-gh Bot added the submodule changed At least one submodule changed in this PR. label Jun 15, 2026
…ests diagnostic

max_parser_depth=0 means "unlimited" by contract (the SQL parser uses
max_depth > 0 && depth > max_depth). The explicit depth limits added for
MsgPack, BSONEachRow, ORC and Parquet schema inference used a bare
depth > max_parser_depth, so a profile with max_parser_depth=0 turned them into
"reject everything" and broke schema inference for these formats. Guard the
explicit limit with max_parser_depth != 0, keeping checkStackSize as the
backstop, and add a max_parser_depth=0 regression case to each.

Rework the deep-recursion tests to print the actual query output on mismatch
instead of an opaque OK/FAIL, so a CI failure is diagnosable.

Reduce the datalake test nesting from 50000 to 2000: it only needs to exceed
the parser depth limit (1000) to be rejected, and the much larger Delta
schemaString was stressing memory under sanitizer builds.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Comment thread src/Formats/SchemaInferenceUtils.cpp Resolved
… MSan

The JSON schema-inference depth checks (tryReadJSONObject,
tryInferDataTypeForSingleFieldImpl) used a bare depth > max_parser_depth, so a
profile with max_parser_depth=0 (documented as unlimited) rejected even scalar
JSON inference with TOO_DEEP_RECURSION. Guard both with max_parser_depth != 0,
keeping checkStackSize as the backstop, and add a max_parser_depth=0 regression
case to the JSON test.

Tag the datalake test no-msan: the delta-kernel-rs library is not built with
MSan, so deltaLakeLocal is not registered there and the delta case fails with
UNKNOWN_FUNCTION.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@Algunenano
Algunenano requested a review from azat June 17, 2026 08:47
robot-ch-test-poll4 added a commit that referenced this pull request Jun 22, 2026
Cherry pick #107341 to 26.4: Reject deeply nested input in formats to prevent stack overflow
robot-clickhouse added a commit that referenced this pull request Jun 22, 2026
robot-ch-test-poll4 added a commit that referenced this pull request Jun 22, 2026
Cherry pick #107341 to 26.5: Reject deeply nested input in formats to prevent stack overflow
robot-clickhouse added a commit that referenced this pull request Jun 22, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 22, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Jun 22, 2026
Algunenano added a commit that referenced this pull request Jun 23, 2026
Backport #107341 to 26.5: Reject deeply nested input in formats to prevent stack overflow
Algunenano added a commit that referenced this pull request Jun 23, 2026
Backport #107341 to 26.4: Reject deeply nested input in formats to prevent stack overflow
robot-clickhouse-ci-2 added a commit that referenced this pull request Jun 23, 2026
Cherry pick #107341 to 25.8: Reject deeply nested input in formats to prevent stack overflow
robot-clickhouse added a commit that referenced this pull request Jun 23, 2026
robot-clickhouse-ci-2 added a commit that referenced this pull request Jun 23, 2026
Cherry pick #107341 to 26.3: Reject deeply nested input in formats to prevent stack overflow
robot-clickhouse added a commit that referenced this pull request Jun 23, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Jun 23, 2026
Algunenano added a commit that referenced this pull request Jun 23, 2026
Backport #107341 to 26.3: Reject deeply nested input in formats to prevent stack overflow
Algunenano added a commit that referenced this pull request Jun 23, 2026
Backport #107341 to 25.8: Reject deeply nested input in formats to prevent stack overflow
bytehalo-infra added a commit to sentioxyz/ClickHouse that referenced this pull request Sep 25, 2026
PROTO: deeply nested MsgPack/BSON/ORC/Parquet/JSON/data-lake input -> crash (stack overflow).
Effect in the production build a394e92: its upstream test(s) 04335_msgpack_schema_inference_deep_recursion, 04336_bson_schema_inference_deep_recursion, 04337_orc_schema_inference_deep_recursion, 04338_parquet_schema_inference_deep_recursion, 04339_json_schema_inference_deep_recursion fail on it and pass on official 26.8.8.8, run on isolated servers (evidence/fix_ci_20260925/upstream_repro/server_triage_PROD263_vs_UP2688.tsv).
Upstream patch unchanged, with its test(s): 04335_msgpack_schema_inference_deep_recursion,04336_bson_schema_inference_deep_recursion,04337_orc_schema_inference_deep_recursion,04338_parquet_schema_inference_deep_recursion,04339_json_schema_inference_deep_recursion.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
zoomxi pushed a commit to zoomxi/ClickHouse that referenced this pull request Oct 9, 2026
`AvroSchemaReader::avroNodeToDataTypeImpl` follows named type references,
so its recursion depth is the length of the reference chain, not the JSON
nesting that contrib/avro bounds with `MAX_AVRO_SCHEMA_DEPTH`. A valid file
whose schema chains 100000 named records, each defined in its own array
field, made `DESC file(..., Avro)` recurse ~200000 frames and overflow the
stack (SIGSEGV on 26.8.18.2 server and on master, after ~35 s and 13 GB).

The per-level `checkStackSize()` added by ClickHouse#102417 ended up in the
non-recursive wrapper `avroNodeToDataType` when ClickHouse#102853 split the
recursion into `avroNodeToDataTypeImpl`, so it ran once per root column.

Count the recursion depth against `max_parser_depth` (0 = unlimited) and
throw `TOO_DEEP_RECURSION`, as ClickHouse#107341 does for MsgPack, BSON, ORC, Parquet
and JSON schema inference, and keep `checkStackSize()` per level as a
backstop for a raised or unlimited limit. The limit is passed by all three
callers (schema inference, the Variant read path and the Iceberg manifest
reader), and `max_parser_depth` is added to the Avro/AvroConfluent schema
cache key, so a schema inferred under a higher limit is not served to a
query with a lower one (same as a839800 did for ORC).

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

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 submodule changed At least one submodule changed in this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants