Skip to content

Reject the Detached serialization kind in data received from a client - #117699

Merged
KochetovNicolai merged 9 commits into
masterfrom
security-fix-1
Sep 21, 2026
Merged

KochetovNicolai merged 9 commits into
masterfrom
security-fix-1

Conversation

@Avogar

@Avogar Avogar commented Sep 2, 2026 •

Copy link
Copy Markdown
Member

A Native TCP client chooses the serialization kind of every column it sends. Kind::DETACHED is an internal transport representation for parallel blocks marshalling: it makes NativeReader build a ColumnBLOB of raw peer-supplied bytes while the block keeps the declared type of the column, and it was applied unconditionally, unlike SPARSE. A client could therefore send a column declared Map(UInt64, UInt64) whose runtime column is a ColumnBLOB, and any later assert_cast<const ColumnMap &> on it — a plain static_cast in release builds — read attacker-controlled bytes.

A reader now declares which kinds the peer may select, with a new ISerialization::KindSet. NativeReader excludes Detached by default; Connection::initBlockInput is the only reader that accepts it, since the server may marshall result blocks and every remote source pipe converts the blobs back. Tuple elements never accept it.

Kind stacks coming off the wire are also checked for shape: they must start with Default, Detached must be outermost, and the COMBINATION encoding may not announce more kinds than exist. docs/reference/interfaces/specs/NativeFormat.mdx is updated.

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 a crash when a client sends a block over the Native TCP protocol that selects the internal Detached serialization kind for a column.


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

Version info

  • Merged into: 26.10.1.338-master (included in 26.10 and later)
  • Backported to: 26.9.1.1626, 26.8.11.7, 26.7.14.19, 26.3.33.133

A Native TCP client chooses the serialization kind of every column of the
blocks it sends. `Kind::DETACHED` is an internal transport representation
for parallel blocks marshalling: it makes `NativeReader` build a
`ColumnBLOB` holding raw peer-supplied bytes, while the block keeps the
declared type of the column. It was applied unconditionally in
`IDataType::wrapSerializationBasedOnKindStack`, unlike `SPARSE` one line
above it.

So a client could send an external table with a column declared
`Map(UInt64, UInt64)` whose runtime column is a `ColumnBLOB`, and
`SELECT mapConcat(x) FROM e` then ran `assert_cast<const ColumnMap &>`
(a plain `static_cast` in release builds) over attacker-controlled bytes.

Make the reader declare which kinds the peer may select, with a new
`ISerialization::KindSet`. `NativeReader` accepts everything except
`Detached` by default, so `TCPHandler`, the exchange sources and the
temporary data streams are all covered without being touched.
`Connection::initBlockInput` is the only reader that accepts every kind,
because the server may marshall result blocks and every remote source
pipe converts the blobs back. Tuple elements never accept `Detached`.

Also validate the shape of a kind stack that comes off the wire: it has
to start with `Default`, `Detached` has to be the outermost kind, and the
`COMBINATION` encoding may not announce more kinds than exist, which
previously allowed building an arbitrarily deep nesting of serializations
from a stream-supplied count. The `COMBINATION` branch now replaces the
kind stack instead of appending to the pre-seeded `{Default}`, which
matches what the writer produces.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@mintlify

mintlify Bot commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

Preview deployment for your docs. Learn more about Mintlify Previews.

Project Status Preview Updated
ClickHouse-docs 🟢 Ready View Preview Sep 20, 2026, 6:29 PM

@clickhouse-gh

clickhouse-gh Bot commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c203d8e]

Summary: ❌

job_name test_name status info comment
Performance Comparison (arm_release, master_head, 1/6) FAIL Performance dashboard
timeseries_to_grid_aggregation_functions #0 FAIL dashboard IGNORED
all_join_opt #0 unstable query history IGNORED

AI Review

Summary

This PR hardens NativeReader against client-selected DETACHED and malformed serialization-kind stacks, updates the Native format spec, and fixes generic ColumnBLOB unwrapping on result consumers. The remaining hole is that the new kind-stack validation is still not type-aware, so some impossible peer-declared stacks are silently accepted and normalized instead of being rejected as malformed input.

Findings

⚠️ Majors

  • [src/DataTypes/Serializations/SerializationInfo.cpp:276-353, src/DataTypes/IDataType.cpp:411-420, src/DataTypes/DataTypeTuple.cpp:360-363] [dismissed by author -- https://github.com/Reject the Detached serialization kind in data received from a client #117699#discussion_r4055461979] checkKindStack only validates generic wrapper ordering and the coarse allowed_kinds set; it never checks that the declared type can actually instantiate every announced kind. Today {Default, Sparse} is still accepted for types that later drop SPARSE silently, such as LowCardinality(...) (supportsSparseSerialization == false), Nullable(Tuple(...)) (SerializationInfoSettings::canUseSparseSerialization rejects it), and Tuple(...) itself (DataTypeTuple::getSerialization explicitly erases top-level SPARSE). That means a client can still send a kind stack no writer emits and have it accepted as plain Default, which contradicts both the new “reject impossible kind stacks” contract and the updated NativeFormat spec. Suggested fix: make validation type-aware before getSerialization, or assert afterwards that every wire-announced kind was consumed by the resulting serialization.
Tests
  • ⚠️ Add one focused regression for the skipped-kind path above, for example a top-level Tuple(UInt64, UInt64) or LowCardinality(UInt64) block with kind stack {Default, Sparse}, and assert INCORRECT_DATA. The new stateless test currently covers ordering and DETACHED, but not the case where an unsupported kind is silently ignored.
Final Verdict

Status: ⚠️ Request changes

Minimum required actions: reject kind stacks whose kinds the declared type cannot instantiate, and add one regression test for that path.

LLVM Coverage Report

Measured on commit c203d8e.

Metric Baseline Current Δ
Lines 88.50% 88.50% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 80.70% 80.70% +0.00%

Changed lines: Changed C/C++ lines covered: 104/106 (98.11%) · Uncovered code

Full report · Diff report

@clickhouse-gh

clickhouse-gh Bot commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing c203d8ea3 with master e3b91968f (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

3133 translation units recompiled, 17042 s compile time in total, 3133 of them have a recent master baseline.

Job report

@clickhouse-gh clickhouse-gh Bot added the comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). label Sep 4, 2026
Comment thread src/Client/Connection.cpp Resolved
Avogar and others added 3 commits September 18, 2026 16:56
Each kind wraps the serialization exactly once, so a repeated kind nests a
column inside its own layout. `{Default, Sparse, Sparse}` builds a
`ColumnSparse` whose values column is itself sparse, and
`ColumnSparse::convertToFullColumnIfSparse` reaches the generic
`IColumn::createWithOffsets`, which materializes through `cloneEmpty` of that
values column - so the result stays sparse where callers expect a full column
of the declared type.

The kind stack is chosen by the peer, so reject the repeat in `checkKindStack`
next to the other shape rules. The `COMBINATION` length bound stays: it refuses
an impossible peer-declared count before reading that many kinds, rather than
restating the shape rule.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`ColumnBLOB` is a transport representation produced by the `Detached`
serialization kind, but it was not handled by the helpers that strip such
representations, so it reached consumers that go on to `assert_cast` the column
to the one its declared type produces. `SerializationNumber::serializeText`,
for example, casts to `ColumnVector`, which in a release build reads the
`ColumnBLOB` as if it were one.

Make it a first-class representation wrapper instead of adding checks at the
consumers: `IColumn::convertToFullColumnIfDetached` joins the existing
`convertToFullColumnIfSparse` / `convertToFullColumnIfReplicated` family, and
both `convertToFullIfWrapped` and `removeSpecialRepresentations` chain it
first, since the BLOB holds the serialized form of everything below it.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
A kind stack describes nested column wrappers, so only a subsequence of
`Default`, `Sparse`, `Replicated`, `Detached` is buildable: `ColumnSparse` can
sit inside `ColumnReplicated` but not the other way round, and nothing wraps a
`ColumnBLOB`. The peer chooses the stack, and any other order describes a
layout no writer produces and that materialization does not unwrap back to the
declared type - `ColumnSparse` over `ColumnReplicated` materializes through
`IColumn::createWithOffsets` into another `ColumnReplicated`.

Check the order instead of the two narrower rules it subsumes, so a repeated
kind and a non-outermost `Detached` are rejected by the same rule. The order is
not the order of the `Kind` enum, whose values are part of the Native format.

`04616_replicated_serialization_oob_index_native_protocol` sends
`{Default, Replicated, Sparse}`, which is now rejected while the kind stack is
read rather than by the non-empty column check in `SerializationReplicated`.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@clickhouse-gh clickhouse-gh Bot added comp-protocols Server wire protocols + request/response handling (native TCP, HTTP, MySQL/PostgreSQL wire protoc... and removed comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). labels Sep 20, 2026
Merged via the queue into master with commit f080e3c Sep 21, 2026
174 of 176 checks passed
@KochetovNicolai
KochetovNicolai deleted the security-fix-1 branch September 21, 2026 11:00
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 21, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Sep 21, 2026
clickhouse-gh Bot added a commit that referenced this pull request Sep 21, 2026
Backport #117699 to 26.9: Reject the Detached serialization kind in data received from a client
robot-clickhouse-ci-2 added a commit that referenced this pull request Sep 21, 2026
Cherry pick #117699 to 26.7: Reject the Detached serialization kind in data received from a client
robot-clickhouse added a commit that referenced this pull request Sep 21, 2026
robot-clickhouse-ci-2 added a commit that referenced this pull request Sep 21, 2026
Cherry pick #117699 to 26.8: Reject the Detached serialization kind in data received from a client
robot-clickhouse added a commit that referenced this pull request Sep 21, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Sep 21, 2026
clickhouse-gh Bot added a commit that referenced this pull request Sep 21, 2026
Backport #117699 to 26.8: Reject the Detached serialization kind in data received from a client
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 removed the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Sep 22, 2026
robot-clickhouse-ci-1 added a commit that referenced this pull request Sep 22, 2026
Cherry pick #117699 to 26.3: Reject the Detached serialization kind in data received from a client
robot-clickhouse added a commit that referenced this pull request Sep 22, 2026
@robot-clickhouse robot-clickhouse added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Sep 22, 2026
clickhouse-gh Bot added a commit that referenced this pull request Sep 22, 2026
Backport #117699 to 26.7: Reject the Detached serialization kind in data received from a client
KochetovNicolai added a commit that referenced this pull request Sep 23, 2026
Backport #117699 to 26.3: Reject the Detached serialization kind in data received from a client
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Sep 25, 2026

This branch was successfully deployed

1 active deployment
staging - docs — c203d8ea Deployed Sep 20, 2026 by mintlify[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-protocols Server wire protocols + request/response handling (native TCP, HTTP, MySQL/PostgreSQL wire protoc... 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.

8 participants