Repository navigation
Fix server crash from recursive Avro schema - #102853
Conversation
A crafted .avro file with a mutually recursive schema (e.g. TypeA -> TypeB -> TypeA) causes avroNodeToDataType(), createDeserializeFn(), and createAction() to recurse infinitely through AVRO_SYMBOLIC nodes, overflowing the stack and crashing the server with SIGSEGV. Any query reading such a file (from S3, HTTP, or local path) triggers the crash without authentication. The fix adds explicit cycle detection to all three vulnerable code paths: - avroNodeToDataType(): tracks record names on the recursion stack via seen_names set - createDeserializeFn(): tracks symbolic names via symbolic_deserialize_guard member - createAction(): shares the same guard as createDeserializeFn createSkipFn() was already protected via symbolic_skip_fn_map (same pattern). Non-recursive re-use of named types (e.g. TypeB used in two different fields of TypeA) is unaffected — the name is removed from the set when leaving the record scope. Closes ClickHouse#102809
|
cc @Avogar @nikitamikhaylov — could you review this? It adds explicit cycle detection for recursive Avro schemas (the vulnerability from #102809). Complementary to #102417 — this catches logical cycles immediately with a targeted error message, while #102417's Session: cron:clickhouse-ci-task-worker:20260415-184500 Pre-PR validation:
|
|
Workflow [PR], commit [9199392] Summary: ❌
AI ReviewSummaryThis PR fixes an actual server exception vector in ClickHouse Rules
Final Verdict
|
|
@groeneai test Does it mean we tested Avro file with recursive schema and parsed it successfully? |
Fixes regression introduced by the initial cycle-detection patch in this PR that broke test `01060_avro` on legitimately recursive Avro schemas such as the `sub1` linked-list-like record in `nested_complex.avsc` (`sub1` has a `b4_null_sub1` field whose type is `union(null, sub1)`). Root cause: in `AvroDeserializer::createAction`, the guard was inserted on every visit to an `AVRO_SYMBOLIC` node, *before* checking `keep_going`. When the user only needs columns at a finite depth and the deeper occurrence of the symbolic reference would have been handled by the safe skip path (`createSkipFn`, which already avoids infinite recursion via the lazy `symbolic_skip_fn_map`), the eager insert still triggered the guard and rejected the schema. Fix: defer the guard to the actually-recursive branch. Take the safe skip path first when `keep_going` is false. Only insert into the path stack when we genuinely recurse into the resolved node, preserving back-edge detection for the case where the user wants to deserialize a column whose path requires infinite unrolling. The regression test `04101_avro_recursive_schema_crash` still catches the original SIGSEGV (checked via `avroNodeToDataTypeImpl` for schema inference and via `createDeserializeFn` on the deserialize path). Verified locally: - `01060_avro`: FAIL -> PASS (same error as reported by @Avogar in CI). - `04101_avro_recursive_schema_crash`: PASS (regression test). - `02592_avro_records_with_same_names`: PASS (no false-positive regression). - 10 consecutive runs of all three tests: 30/30 passed. CI report (failure that motivated this commit): ClickHouse#102853
|
@Avogar thanks for catching that — the failing What was happening
{
"type": "record", "name": "main",
"fields": [
{"name": "a", "type": "long"},
{"name": "b", "type": {
"type": "record", "name": "sub1",
"fields": [
...
{"name": "b4_null_sub1", "type": ["null", "sub1"]} // <- self-reference
]
}}
]
}The test reads column Traversal in
My original patch inserted into The fix (9199392)if (node->type() == avro::AVRO_SYMBOLIC)
{
auto keep_going = std::any_of(header.begin(), header.end(), ...);
auto resolved_node = avro::resolveSymbol(node);
// Safe skip path handles cycles via symbolic_skip_fn_map — no guard needed.
if (!keep_going)
return AvroDeserializer::Action(createSkipFn(resolved_node));
// Only guard the genuinely-recursive path.
const auto & sym_name = node->name().fullname();
if (!symbolic_deserialize_guard.insert(sym_name).second)
throw Exception(..., "Recursive Avro schema is not supported: type '{}' references itself", sym_name);
Action result = createAction(header, resolved_node, current_path);
symbolic_deserialize_guard.erase(sym_name);
return result;
}So: schemas that are truly recursive remain readable as long as the requested column paths are finite. Only the case where the user does want to descend into a cycle (which would require infinite unrolling) still errors out. The Verified locally
Session id: cron:clickhouse-ci-task-worker:20260420-184500 |
LLVM Coverage Report
Changed lines: 76.56% (49/64) · Uncovered code |
251ab53
|
Hi — this PR may need backporting to Affected code: Why: SIGSEGV crash from crafted Avro input is a security vulnerability (no authentication required). The Avro format support and If this should be backported, consider adding |
`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]>
A crafted
.avrofile with a mutually recursive schema (e.g.TypeA → TypeB → TypeA) causesavroNodeToDataType(),createDeserializeFn(), andcreateAction()to recurse infinitely throughAVRO_SYMBOLICnodes, overflowing the stack and crashing the server with SIGSEGV. Any query reading such a file (from S3, HTTP, or local path) triggers the crash — no authentication required.The fix adds explicit cycle detection to all three vulnerable code paths using name-tracking sets that detect back-references immediately (on the first re-entry, not after thousands of stack frames). Non-recursive re-use of named types is unaffected — verified with existing test
02592_avro_records_with_same_names.createSkipFn()was already protected viasymbolic_skip_fn_map— the same defensive pattern.Complementary to #102417 which adds generic
checkStackSize()guards (catches all deep nesting including non-recursive). This PR specifically detects logical cycles with an immediate, targeted error message.Reproduction:
After fix:
Closes #102809
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix server crash (SIGSEGV) when reading Avro files with recursive schemas containing cyclic symbolic type references. Now such schemas are detected and rejected with a clear error message instead of crashing.
Documentation entry for user-facing changes
Version info
26.4.1.1106