Skip to content

Fix out-of-bounds read in SerializationReplicated on the native protocol - #112331

Merged
Avogar merged 3 commits into
masterfrom
fix-replicated-serialization-oob-index
Aug 7, 2026
Merged

Avogar merged 3 commits into
masterfrom
fix-replicated-serialization-oob-index

Conversation

@Avogar

@Avogar Avogar commented Jul 28, 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):

Fix out-of-bounds read in SerializationReplicated on the native protocol on corrupted indexes data.

Version info

  • Backported to: 26.7.4.11, 26.6.3.26, 26.5.7.35, 26.3.18.18

`SerializationReplicated::deserializeBinaryBulkWithMultipleStreams` read a
client-supplied replicated-index buffer from the native TCP protocol and
validated only the index row count, not the index values. Every
`ColumnReplicated` accessor then dereferences `nested_column[index]` without
a bound, so a crafted `REPLICATED`-serialized column whose indexes point past
the nested ("elements") column produces an out-of-bounds read when the column
is materialized.

Validate every deserialized index against the number of nested elements
(throwing `INCORRECT_DATA`) before attaching the indexes, mirroring the
per-discriminator check already done in `SerializationVariant`.

Reachable only over the native TCP protocol (negotiated revision >=
`DBMS_MIN_REVISION_WITH_REPLICATED_SERIALIZATION`); the format layer builds
`NativeReader` with `server_revision = 0`, so `REPLICATED` is never read there.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 28, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [541c52c]

Summary: ✅


AI Review

Summary

This PR closes the two malformed-input paths that could leave SerializationReplicated with out-of-bounds-capable state over the native TCP protocol: it now rejects out-of-range replicated indexes and truncated nested-element payloads, and the new regression test covers both cases. The implementation itself looks correct, but the change is still not merge-ready because the published Native spec was not updated to describe the stricter REPLICATED decoder contract.

Findings

⚠️ Majors

  • [src/DataTypes/Serializations/SerializationReplicated.cpp:289-303] This PR changes the REPLICATED native decoding contract by rejecting payloads unless every index is < num_elements and the nested elements substream materializes exactly num_elements rows, but docs/en/interfaces/specs/NativeFormat.md still documents only the byte layout and the elements[indexes[i]] reconstruction rule. That leaves the spec incomplete for third-party decoders and future maintainers, and it violates the repo rule that Native format changes must update the spec in the same PR. Suggested fix: extend the Replicated wire format section in docs/en/interfaces/specs/NativeFormat.md to state both validation requirements and that malformed payloads must be rejected.
Final Verdict

Not ready to merge until the Native format spec is updated to match the new REPLICATED decoder behavior.

@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 Jul 28, 2026
`checkDeserializedIndexes` only proved that replicated indexes are less
than the advertised `num_elements`; it never proved that
`nested->deserializeBinaryBulkWithMultipleStreams` actually produced
`num_elements` rows. Primitive bulk readers such as
`SerializationNumber::deserializeBinaryBulk` short-read on EOF instead of
throwing, and `NativeReader::readData` only checks the top-level
`column->size()`, which for `ColumnReplicated` is the index count, not the
nested column size. So a client could send in-range indexes, advertise
`num_elements = N`, truncate the elements payload, and still hand an
out-of-bounds-capable `ColumnReplicated` to the executor.

Verify `column_replicated.getNestedColumn()->size() == num_elements` after
the nested deserialization and throw `INCORRECT_DATA` on mismatch,
mirroring the post-read consistency checks in `SerializationArray` and
`SerializationVariant`. Extend the regression test with a second scenario
covering in-range indexes plus a truncated elements stream.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.50% 86.50% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.70% 78.70% +0.00%

Changed lines: Changed C/C++ lines covered: 25/28 (89.29%) · Uncovered code

Full report · Diff report

@Avogar
Avogar added this pull request to the merge queue Aug 7, 2026
Merged via the queue into master with commit 2b12cd7 Aug 7, 2026
180 checks passed
@Avogar
Avogar deleted the fix-replicated-serialization-oob-index branch August 7, 2026 19:00
@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 Aug 7, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 7, 2026
clickhouse-gh Bot added a commit that referenced this pull request Aug 7, 2026
Backport #112331 to 26.7: Fix out-of-bounds read in SerializationReplicated on the native protocol
@clickgapai

Copy link
Copy Markdown
Contributor

Hi @Avogar @antonio2368 — while reviewing this PR I found the following:

Happy to discuss — close anything that's wrong or already addressed.

Avogar added a commit that referenced this pull request Aug 10, 2026
Backport #112331 to 26.3: Fix out-of-bounds read in SerializationReplicated on the native protocol
Avogar added a commit that referenced this pull request Aug 10, 2026
Backport #112331 to 26.5: Fix out-of-bounds read in SerializationReplicated on the native protocol
Avogar added a commit that referenced this pull request Aug 10, 2026
Backport #112331 to 26.6: Fix out-of-bounds read in SerializationReplicated on the native protocol
@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 Aug 10, 2026
pull Bot pushed a commit to peijunlin2008/ClickHouse that referenced this pull request Aug 13, 2026
`SerializationReplicated::deserializeBinaryBulkWithMultipleStreams` had three
sibling checks that still threw `LOGICAL_ERROR` for conditions decided purely by
untrusted native-protocol bytes:

- a non-empty target column, reachable via a `Sparse`-over-`Replicated` kind
  stack (a fresh `ColumnSparse` always seeds one default value, which is then
  handed to the nested `Replicated` deserialization);
- an index row count that disagrees with the block row count;
- an unsupported index-type width.

In debug and sanitizer builds `LOGICAL_ERROR` is routed to
`abortOnFailedAssertion`, so a crafted block aborts the server; in release builds
it is the wrong error class and trips CI checks that scan logs for `Logical error`.

Change all three to `INCORRECT_DATA`, matching the checks added in ClickHouse#112331 and the
analogous `SerializationLowCardinality` code, and extend the native-protocol
regression test with the three scenarios.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Aug 24, 2026
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Aug 24, 2026
VighneshPath pushed a commit to VighneshPath/ClickHouse that referenced this pull request Sep 16, 2026
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants