Skip to content

Cherry pick #117699 to 26.8: Reject the Detached serialization kind in data received from a client - #121320

Merged
robot-clickhouse-ci-2 merged 11 commits into
backport/26.8/117699from
cherrypick/26.8/117699
Sep 21, 2026
Merged

robot-clickhouse-ci-2 merged 11 commits into
backport/26.8/117699from
cherrypick/26.8/117699

Conversation

@robot-clickhouse-ci-2

Copy link
Copy Markdown
Contributor

Original pull-request #117699

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Before you resolve anything

Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

Avogar and others added 10 commits September 2, 2026 13:57
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]>
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]>
Reject the Detached serialization kind in data received from a client
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-critical-bugfix labels Sep 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! pr-critical-bugfix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants