Repository navigation
validate discriminators in Variant binary bulk deserialization - #107991
Conversation
|
Workflow [PR], commit [181a925] Summary: ❌
AI ReviewSummaryThis PR hardens Findings
Tests
Final VerdictStatus: Minimum required action: reject out-of-range discriminators in the subcolumn deserialization paths and cover those paths with focused |
…ation paths Extend the discriminator bounds check from `SerializationVariant` to also cover `ColumnVariant::validateState`, `SerializationVariantElement`, and `SerializationVariantElementNullMap`. Use `INCORRECT_DATA` for native format (untrusted input) and `LOGICAL_ERROR` otherwise. Extract a `checkDiscriminatorValue` helper function to avoid repeating the check-and-throw pattern at every call site. Add tests with pregenerated `.native` files containing corrupted discriminators (both plain and compact granule formats). Related: ClickHouse#107991 Related: ClickHouse/clickhouse-private#59665 Co-Authored-By: Claude Opus 4.6 <[email protected]>
|
Thanks for the contribution! It's a good idea to add this check. I made a few additional changes to add similar check in other places where it's needed and added a few tests. |
| settings.continuous_reading, | ||
| variant_element_state->discriminators_state, | ||
| settings, | ||
| num_variants, |
There was a problem hiding this comment.
Malformed Native input is still accepted when the query reads only Variant subcolumns. In the BASIC path above, the newly-read discriminator bytes are cached at line 189 and the later loops only compare them with variant_discriminator; for Variant(String, UInt64) a byte 2 is treated as "not String" instead of INCORRECT_DATA. The PLAIN compact-granule branch in deserializeCompactDiscriminators has the same equality-only counting at lines 379-383, and SerializationVariantElementNullMap reuses the same pattern for .null subcolumns.
Please validate every discriminator read from the stream before caching/counting it in the subcolumn readers as well, and add a focused regression such as selecting v.String / v.String.null from a corrupted Native block.
There was a problem hiding this comment.
Right, but this is the subcolumn reader, not the full one. The OOB this PR is about lives in the full reader's ++variant_limits[discr] / ++variant_rows_offsets[discr], where a raw discr in [N, 254] indexes a vector sized to num_variants. The subcolumn path here never does that: it only compares discriminators_data[i] == variant_discriminator (the limit/rows_offset counting, the null-map fill, and the final insert loop), so an out-of-range byte is counted as "not this variant" and is never used as an index. No out-of-bounds write. The compact granule it shares is covered now that num_variants flows into readDiscriminatorsGranuleStart. Rejecting the stray byte in the BASIC subcolumn path would be a stricter-input policy rather than a safety fix, with the trade-off that it'd reject blocks the full reader still reads fine.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 151/164 (92.07%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 4 line(s) · Uncovered code |
|
Looks good, thanks for finishing it off. Threading |
|
📊 Cloud Performance Report
This PR only hardens bounds-checking of Variant/Dynamic discriminators during deserialization from the Native format and MergeTree reads. ClickBench and TPC-H use fixed, standard-typed schemas that never exercise the Variant code path, so the five sub-25% deltas cannot plausibly be caused by this change and were downgraded to run-to-run variance. Two TPC-H queries (Q4 +210%, Q7 +785%) exceed the automatic hard-trust threshold and so their verdicts stand, but both had very noisy baseline measurements and hit no Variant path — a re-run is recommended before treating them as real regressions. clickbenchFlagged queries (3 of 43)
q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_official🔴 2 regressed · Flagged queries (4 of 22)
q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. Debug info
|
2038a36
|
Found via ClickGap automated review. Please close or comment if this is incorrect or needs adjustment. TL;DRvalidate discriminators in Variant binary bulk deserialization ClickGap verified: reproduced it with the script below on ReproductionBisect
Affects
Suggested next stepThe fix has landed on master; the affected release branches above still need the backport. Component: cc fix PR reviewer: @Avogar ClickGap · Finding: Triage metadata
|
Backport #107991 to 26.5: validate discriminators in Variant binary bulk deserialization
Backport #107991 to 26.6: validate discriminators in Variant binary bulk deserialization
…t binary bulk deserialization
…t binary bulk deserialization
Resolve conflicts for the 26.3 backport of the Variant discriminator bounds validation. All conflicts come from the serialization pooling infrastructure (`getHash`, static `create`, `ISerialization::pooled`, `supportsPooling`, `allocatedBytes`), which exists on `master` but not on 26.3, where these serializations are still constructed directly via `std::make_shared` with public constructors. The resolution keeps 26.3's construction style and threads `num_variants` through it, dropping the pooling-only declarations and definitions. `SerializationVariantElement` and `SerializationVariantElementNullMap` take `num_variants` as a required constructor argument, since every construction site in the 26.3 tree passes a real count. A defaulted `0` would make `checkDiscriminatorValue` reject every non-NULL discriminator. The subcolumn creators keep their `num_variants_ = 0` defaults as on `master`. Related: #117736 Related: #107991 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Cherry pick #107991 to 26.3: validate discriminators in Variant binary bulk deserialization
Backport #107991 to 26.3: validate discriminators in Variant binary bulk deserialization
…ation paths Extend the discriminator bounds check from `SerializationVariant` to also cover `ColumnVariant::validateState`, `SerializationVariantElement`, and `SerializationVariantElementNullMap`. Use `INCORRECT_DATA` for native format (untrusted input) and `LOGICAL_ERROR` otherwise. Extract a `checkDiscriminatorValue` helper function to avoid repeating the check-and-throw pattern at every call site. Add tests with pregenerated `.native` files containing corrupted discriminators (both plain and compact granule formats). Related: ClickHouse#107991 Related: ClickHouse/clickhouse-private#59665 Co-Authored-By: Claude Opus 4.6 <[email protected]>
…tor-bounds validate discriminators in Variant binary bulk deserialization
PROTO: malformed Native Variant discriminators -> crash (OOB write). Effect in the production build a394e92: its upstream test(s) 04305_variant_corrupted_discriminators_native, 04306_variant_corrupted_compact_discriminator_native 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): 04305_variant_corrupted_discriminators_native,04306_variant_corrupted_compact_discriminator_native. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A
Variantlocal discriminator is aUInt8where255means NULL and any other value must be a valid variant index. When deserializing aVariantcolumn from a Native block,SerializationVariant::deserializeBinaryBulkWithMultipleStreams/deserializeCompactDiscriminatorsread these bytes straight from the input and use them to indexvariant_limitsandvariant_rows_offsets, both sized to the number of variants:For a column with N variants, a crafted block carrying a discriminator in
[N, 254]writes out of bounds. All three discriminator encodings reach the unguarded index: BASIC (full column), and the COMPACT and PLAIN granules of the compact mode.The single-value
deserializeBinarypath already rejects an out-of-range discriminator; the bulk path did not. Fix validates each discriminator where it enters from the stream and throwsINCORRECT_DATAfor anything that is neither NULL nor a real variant index, so the rejection lives in the serializer rather than relying on the caller.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix an out-of-bounds write when reading a
Variantcolumn from a malformed Native block whose discriminators reference a variant index that does not exist.Version info
26.6.4.28,26.3.33.15