Skip to content

Fix out-of-bounds read on truncated compact Variant discriminators - #120252

Merged
Avogar merged 1 commit into
masterfrom
fix-variant-deserialisation
Sep 17, 2026
Merged

Avogar merged 1 commit into
masterfrom
fix-variant-deserialisation

Conversation

@rienath

@rienath rienath commented Sep 15, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixed an out-of-bounds read when reading a Variant column from a truncated Native stream using compact discriminators serialization.


Workflow [PR]
Sync PR [sync-upstream/pr/120252]

Version info

  • Backported to: 26.9.1.1596, 26.8.7.13, 26.7.14.3, 26.3.33.120

@rienath rienath added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Sep 15, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [d575724]

Summary: ❌

job_name test_name status info comment
Build profile diff ERROR

AI Review

Summary

The PR hardens Variant deserialization for truncated COMPACT discriminator streams by checking that each PLAIN granule yields the expected number of discriminator bytes and by adding regression coverage in Native and MergeTree reads. I do not think it is ready to merge yet: the subcolumn reader still accepts out-of-range discriminators as if they were just "other variants", and the canonical NativeFormat spec was not updated alongside this COMPACT Variant behavior change.

Findings

❌ Blockers

  • [src/DataTypes/Serializations/SerializationVariantElement.cpp:326-333] Variant subcolumn decoding still never validates the discriminators it just read. A corrupt PLAIN COMPACT granule like [100] for Variant(UInt8) makes SELECT v.UInt8 insert a default and SELECT v.UInt8.null insert 1 instead of rejecting the stream, so corruption turns into silent wrong results.
    Suggested fix: mirror the discriminator bounds check from SerializationVariant.cpp before counting rows in this loop.

⚠️ Majors

  • [src/DataTypes/Serializations/SerializationVariant.cpp:725-729] This PR changes Native COMPACT Variant decoding, but docs/reference/interfaces/specs/NativeFormat.mdx still says that only BASIC Variant discriminators are specified. The repo requires Native-format changes to update the spec in the same PR, so the COMPACT granule layout and the short-read rejection contract need to be documented there.
Final Verdict

Changes requested.

LLVM Coverage Report

Measured on commit d575724.

Metric Baseline Current Δ
Lines 88.40% 88.40% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 80.50% 80.60% +0.10%

Changed lines: Changed C/C++ lines covered: 19/21 (90.48%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-json-datatype JSON/Dynamic/Object datatype: semi-structured column storage, typed and dynamic paths, sub-object... labels Sep 15, 2026
SerializationNumber<ColumnVariant::Discriminator>::create()->deserializeBinaryBulk(discriminators, *stream, limit_in_granule, 0);
size_t start = discriminators_data.size() - limit_in_granule;
size_t start = discriminators_data.size();
SerializationNumber<ColumnVariant::Discriminator>::deserializeBinaryBulk(discriminators_data, *stream, limit_in_granule);

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 still diverges from the top-level Variant reader: the freshly read discriminators are never bounds-checked before being interpreted. A corrupt PLAIN compact granule like [100] for Variant(UInt8) makes SELECT v.UInt8 fall through to the variant_limit == 0 path and materialize a default, and SELECT v.UInt8.null materialize 1, instead of rejecting the stream as INCORRECT_DATA. Please mirror the discriminator bounds check here before turning these bytes into row counts.

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.

To reach that, we'd need a corrupt part with either checksums off or somehow bypassed. Does not hurt, but I am not sure it's needed in this security fix, especially considering that I will backport it. If human reviewer finds this useful, I will change top-level reader too

SerializationNumber<ColumnVariant::Discriminator>::create()->deserializeBinaryBulk(discriminators, *stream, limit_in_granule, 0);
size_t start = discriminators_data.size() - limit_in_granule;
size_t start = discriminators_data.size();
SerializationNumber<ColumnVariant::Discriminator>::deserializeBinaryBulk(discriminators_data, *stream, limit_in_granule);

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 PR changes Native Variant COMPACT decoding, but the canonical spec in docs/reference/interfaces/specs/NativeFormat.mdx still says that only BASIC mode is specified for Variant. Repo policy for Native-format work is to keep the spec in sync in the same PR, so the COMPACT granule layout and the requirement to reject truncated discriminator payloads need to be documented there as well.

@rienath rienath Sep 16, 2026 •

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.

We do not need to specify each invalid input check in native format description

@Avogar
Avogar added this pull request to the merge queue Sep 17, 2026
Merged via the queue into master with commit e39b387 Sep 17, 2026
687 of 692 checks passed
@Avogar
Avogar deleted the fix-variant-deserialisation branch September 17, 2026 13:56
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 17, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Sep 17, 2026
clickhouse-gh Bot added a commit that referenced this pull request Sep 17, 2026
Backport #120252 to 26.9: Fix out-of-bounds read on truncated compact `Variant` discriminators
clickhouse-gh Bot added a commit that referenced this pull request Sep 17, 2026
Backport #120252 to 26.8: Fix out-of-bounds read on truncated compact `Variant` discriminators
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Sep 18, 2026
PedroTadim added a commit that referenced this pull request Sep 20, 2026
Resolved the conflict in `SerializationVariant::deserializeCompactDiscriminators` and
`SerializationVariantElement::deserializeCompactDiscriminators`. Master dropped the
`rows_offset` parameter and moved to the static
`SerializationNumber::deserializeBinaryBulk` overload that takes the data container;
26.3 has neither, so the branch's call shape is kept and only the actual fix is applied:
take `start` before the read and reject a short read with `CANNOT_READ_ALL_DATA`,
instead of computing `start` as `size() - limit_in_granule`, which underflows and reads
out of bounds when the stream is truncated.

Verified on the 26.3.33.24 release binary that the three truncated `Native` streams in
the new test currently fail with `INCORRECT_DATA` from reading past the buffer (for
example `Invalid discriminator value 194 (num_variants = 1)`), so the test exercises the
patched path on this branch, and that the rest of the test already matches its reference.

Original pull-request #120252
Cherry-pick pull-request #120607

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
PedroTadim added a commit that referenced this pull request Sep 20, 2026
Resolved the conflict in `SerializationVariant::deserializeCompactDiscriminators` and
`SerializationVariantElement::deserializeCompactDiscriminators`. Master dropped the
`rows_offset` parameter and moved to the static
`SerializationNumber::deserializeBinaryBulk` overload that takes the data container;
26.7 has neither, so the branch's call shape is kept and only the actual fix is applied:
take `start` before the read and reject a short read with `CANNOT_READ_ALL_DATA`,
instead of computing `start` as `size() - limit_in_granule`, which underflows and reads
out of bounds when the stream is truncated.

Verified on the 26.7.13.12 release binary that the three truncated `Native` streams in
the new test currently fail with `INCORRECT_DATA` from reading past the buffer (for
example `Invalid discriminator value 194 (num_variants = 1)`), so the test exercises the
patched path on this branch, and that the rest of the test already matches its reference.

Original pull-request #120252
Cherry-pick pull-request #120609

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
robot-clickhouse added a commit that referenced this pull request Sep 20, 2026
robot-clickhouse added a commit that referenced this pull request Sep 20, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Sep 20, 2026
alexey-milovidov added a commit that referenced this pull request Sep 20, 2026
Backport #120252 to 26.3: Fix out-of-bounds read on truncated compact `Variant` discriminators
clickhouse-gh Bot added a commit that referenced this pull request Sep 20, 2026
Backport #120252 to 26.7: Fix out-of-bounds read on truncated compact `Variant` discriminators
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-json-datatype JSON/Dynamic/Object datatype: semi-structured column storage, typed and dynamic paths, sub-object... pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants