Skip to content

Check for stack overflow in Avro reader - #102417

Merged
Avogar merged 10 commits into
masterfrom
stack-overflow-avro-reader
May 19, 2026
Merged

Avogar merged 10 commits into
masterfrom
stack-overflow-avro-reader

Conversation

@Avogar

@Avogar Avogar commented Apr 10, 2026 •

Copy link
Copy Markdown
Member

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

Check for stack overflow in Avro reader during nested types deserialization.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Backported to: 26.4.3.38, 26.3.11.34, 26.2.19.42

@clickhouse-gh

clickhouse-gh Bot commented Apr 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [3c7fb12]

Summary: ❌

job_name test_name status info comment
Bugfix validation (functional tests) FAIL
04093_avro_deeply_nested_schema_stack_overflow FAIL cidb IGNORED
Finish Workflow FAIL
python3 ./ci/jobs/scripts/workflow_hooks/check_report_messages.py FAIL IGNORED

AI Review

Summary

This PR adds stack-depth guards for Avro schema handling by passing MAX_AVRO_SCHEMA_DEPTH into Avro parser entry points and adding checkStackSize in recursive deserializer/schema-conversion paths, plus a regression test with a deeply nested Avro file. The stack-overflow mitigation is now more complete (including Iceberg reader entry points), but the change still introduces a hard backward-incompatible schema-depth enforcement with no compatibility path.

Findings

❌ Blockers

  • [src/Processors/Formats/Impl/AvroRowInputFormat.h:33] The new invariant hard-rejects Avro schemas deeper than 256 via a fixed constant (MAX_AVRO_SCHEMA_DEPTH) and is wired into parsing paths (AvroRowInputFormat, AvroConfluentSchemaRegistry, Iceberg Avro readers). This is a backward-incompatible behavior change for previously accepted inputs (those that parsed on older versions with sufficient stack) with no setting/compatibility control to preserve old behavior during rollout.
  • Suggested fix: introduce a configurable max schema depth setting (safe default retained), wire it through all Avro reader/parser call sites, and make it controllable through compatibility settings for existing workloads.
Final Verdict

Status: ❌ Block
Minimum required actions:

  • Add a compatibility path for the new schema-depth enforcement (for example a setting with safe default, integrated with compatibility) instead of a hard-coded non-overridable limit.

@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 Apr 10, 2026
@clickhouse-gh clickhouse-gh Bot added the submodule changed At least one submodule changed in this PR. label Apr 16, 2026
/// prevent stack overflow on deeply nested schemas (e.g. crafted inputs with
/// thousands of nested arrays/records). Real-world schemas rarely exceed 10-20
/// levels, so 256 is more than enough.
static constexpr size_t MAX_AVRO_SCHEMA_DEPTH = 256;

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.

❌ This introduces a backward-incompatible hard limit for existing Avro inputs/schemas (>256 nesting) with no configurable fallback.

Before this change, acceptance depended on available stack; after this change, those inputs are rejected unconditionally at schema parse time. Per ClickHouse compatibility rules, new enforcement on previously accepted data should be gated (setting/compatibility) or at least made configurable.

Please add an input format setting (for example input_format_avro_max_schema_depth) and wire it here instead of a fixed static constexpr value. That keeps the safe default while allowing compatibility for existing workloads.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Addin the setting will lead to a security issue - setting high value to the limit can lead to a stack overflow. We have to use hard limit. 256 is more than enough for real use cases

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.

This is still a backward-compatibility break for existing Avro inputs that are deeper than 256 levels.

The current code hard-rejects these schemas via MAX_AVRO_SCHEMA_DEPTH in both AvroRowInputFormat and ConfluentSchemaRegistry, while previous versions could still process them when stack limits allowed it. Per ClickHouse compatibility rules, new validation on previously accepted data should be gated (for example via an input-format setting / compatibility) rather than hard-coded.

Please make this limit configurable (safe default is fine), so existing workloads have an explicit compatibility path.

@Avogar

Avogar commented Apr 20, 2026

Copy link
Copy Markdown
Member Author

Bugfix validation (functional tests) - it reproduces only under asan

@alexey-milovidov alexey-milovidov self-assigned this May 12, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member

This was fixed by #105146. Let's update the branch.

alexey-milovidov and others added 3 commits May 17, 2026 20:54
The previous commit only constrained schema depth in `AvroRowInputFormat`
and `ConfluentSchemaRegistry`. The same unbounded schema-parse path
existed in the Iceberg Avro readers:
`AvroForIcebergDeserializer` and `forEachAvroEntry` in `Iceberg/Utils`.

Move `MAX_AVRO_SCHEMA_DEPTH` into `AvroRowInputFormat.h` and apply it
to the two Iceberg call sites so the stack-overflow mitigation covers
every Avro reader entry point.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented May 19, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.20% 84.20% +0.00%
Functions 91.40% 91.40% +0.00%
Branches 76.60% 76.60% +0.00%

Changed lines: 100.00% (34/34) · Uncovered code

Full report · Diff report

@Avogar
Avogar added this pull request to the merge queue May 19, 2026
Merged via the queue into master with commit 689fd4d May 19, 2026
162 of 165 checks passed
@Avogar
Avogar deleted the stack-overflow-avro-reader branch May 19, 2026 15:20
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 19, 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 May 19, 2026
robot-ch-test-poll4 added a commit that referenced this pull request May 19, 2026
Cherry pick #102417 to 26.4: Check for stack overflow in Avro reader
robot-ch-test-poll added a commit that referenced this pull request May 19, 2026
Cherry pick #102417 to 25.8: Check for stack overflow in Avro reader
robot-ch-test-poll added a commit that referenced this pull request May 19, 2026
Cherry pick #102417 to 26.2: Check for stack overflow in Avro reader
robot-ch-test-poll added a commit that referenced this pull request May 19, 2026
Cherry pick #102417 to 26.3: Check for stack overflow in Avro reader
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label May 19, 2026
Avogar added a commit that referenced this pull request May 20, 2026
Backport #102417 to 26.4: Check for stack overflow in Avro reader
Avogar added a commit that referenced this pull request May 20, 2026
The cherry-pick of #102417 resolved the avro submodule conflict to the
wrong commit (477ed40c656b, old API) instead of 3b5d52bdf9f9 (which
adds the `max_depth` parameter to `DataFileReaderBase` and
`compileJsonSchemaFromString`).

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Avogar added a commit that referenced this pull request May 20, 2026
The cherry-pick of #102417 resolved the avro submodule conflict to the
wrong commit (477ed40c656b, old API) instead of 3b5d52bdf9f9 (which
adds the `max_depth` parameter to `DataFileReaderBase` and
`compileJsonSchemaFromString`).

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Avogar added a commit that referenced this pull request May 20, 2026
The cherry-pick of #102417 resolved the avro submodule conflict to the
wrong commit (477ed40c656b, old API) instead of 3b5d52bdf9f9 (which
adds the `max_depth` parameter to `DataFileReaderBase` and
`compileJsonSchemaFromString`).

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Avogar added a commit that referenced this pull request May 21, 2026
Backport #102417 to 26.3: Check for stack overflow in Avro reader
Avogar added a commit that referenced this pull request May 21, 2026
Backport #102417 to 26.2: Check for stack overflow in Avro reader
Avogar added a commit that referenced this pull request May 21, 2026
Backport #102417 to 25.8: Check for stack overflow in Avro reader
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…2417

Backport ClickHouse#102417 to 26.4: Check for stack overflow in Avro reader
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