Skip to content

Cherry pick #107341 to 26.4: Reject deeply nested input in formats to prevent stack overflow - #108165

Merged
robot-ch-test-poll4 merged 19 commits into
backport/26.4/107341from
cherrypick/26.4/107341
Jun 22, 2026
Merged

robot-ch-test-poll4 merged 19 commits into
backport/26.4/107341from
cherrypick/26.4/107341

Conversation

@robot-ch-test-poll4

Copy link
Copy Markdown
Contributor

Original pull-request #107341

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.

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

Algunenano and others added 19 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]>
…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]>
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]>
…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]>
… 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]>
Reject deeply nested input in formats to prevent stack overflow
@robot-ch-test-poll4 robot-ch-test-poll4 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 Jun 22, 2026
@robot-ch-test-poll4
robot-ch-test-poll4 merged commit 5135664 into backport/26.4/107341 Jun 22, 2026
@robot-ch-test-poll4
robot-ch-test-poll4 deleted the cherrypick/26.4/107341 branch June 22, 2026 16:04
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