Skip to content

Cherry pick #130895 to 26.9: Reject non-default serialization kinds in data received over the native protocol - #131349

Merged
vdimir merged 7 commits into
backport/26.9/130895from
cherrypick/26.9/130895
Nov 18, 2026
Merged

vdimir merged 7 commits into
backport/26.9/130895from
cherrypick/26.9/130895

Conversation

@robot-ch-test-poll

Copy link
Copy Markdown
Contributor

Original pull-request #130895

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

vdimir and others added 6 commits November 11, 2026 17:15
…ve protocol

In the Native protocol the sender chooses each column's serialization kind
(Default, Sparse, Replicated, Detached). The server rebuilt a `ColumnSparse` or
`ColumnReplicated` from a peer-chosen kind while the block kept the declared
dense type, so a downstream consumer that `assert_cast`s the column to the dense
representation hit a type confusion: a debug abort, and in release an unchecked
`static_cast` (out-of-bounds read, heap disclosure, or an implausible
allocation). The asynchronous-insert flush was the original vector, but an
external table or a client scalar wrapping `Sparse`/`Replicated`, and a `Tuple`
carrying a `Replicated` element, reach the same dense consumers.

No legitimate sender needs a non-default kind in data sent to a server: inserts
and forwarded external tables and scalars are materialized before sending, and
the storage representation is re-decided at write time.

- `TCPHandler::initBlockInput` and `TCPHandler::skipDataPacket` accept only the
  default kind in every block a peer sends (`INSERT` and `input` data, external
  tables, scalars), for every peer: `query_kind` is client-declared and an
  initiator forwards client data to shards unchanged.
- `SerializationInfoTuple` rejects `Replicated` as a tuple element in every
  Native reader, as it already did for `Detached`. `Replicated` wraps a whole
  column and is unwrapped only at the top level, so a nested one is a layout no
  writer builds; this also protects a client reading results from a malicious
  server.
- `RemoteQueryExecutor::sendExternalTables` materializes each external table
  before sending it to a shard, because a materialized CTE keeps its `Sparse`
  columns and would otherwise be rejected by the gated shard reader.
- `AsynchronousInsertQueue::pushQueryWithBlock` keeps a defense-in-depth check
  that a queued block carries no wrapped column.
- `SerializationInfo::checkKindStack` validates the stack shape first, then the
  allowed kinds from the outermost one, so a disallowed outer kind is reported
  even when an inner kind is also out of place.

Result and interserver traffic keeps sparse/replicated on the wire: the client's
result reader `Connection::initBlockInput` is unchanged.

Tests: `05617` covers the gate for insert, external table and scalar data;
`05618` covers a materialized CTE over a sparse column sent to a shard;
`04616` and `05473` now reach the `SerializationReplicated` and
`SerializationSparse` validators through a result block served by a fake server,
the only direction a client can still reach them. The `Native` format spec is
updated.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…rmat readers

`NativeInputFormat` and `NativeSchemaReader` create a `NativeReader` with
revision 0, whose layout has no serialization kinds, so the format cannot
receive a `Sparse` or `Replicated` column and the kind set is not consulted
there. Passing `{Default}` explicitly makes the format fail closed if a
revision is ever passed to these readers, as the output format already does
with `client_protocol_version`.

The two reader constructions in `NativeInputFormat` now go through `makeReader`.
In `TCPHandler` the kind set is written inline instead of a named constant.

No behavior change.

Review comment: #130895 (comment)

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…umns

An async `INSERT` without data queues `squashing.getHeader()->cloneWithoutColumns()`
from `TCPHandler::processAsyncInsertQuery`, a block whose elements have no column.
The check of wrapped columns in `AsynchronousInsertQueue::pushQueryWithBlock`
dereferenced every element, so the server got a segmentation fault on such an
insert. The check now skips elements without a column; the flush already skips
blocks without rows.

Found by `03561_async_insert_crash` in Fast test:
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=130895&sha=1fa96f589c48e405cffad930dcb5836aa29381bd&name_0=PR&name_1=Fast%20test
#130895

Co-Authored-By: Claude Fable 5.2 <[email protected]>
- `04616_replicated_serialization_oob_index_native_protocol` goes back to its
  original structure: every block function takes the packet type and returns
  the bytes instead of sending them, and every scenario passes them to the fake
  server. The two new scenarios (a `Replicated` tuple element, a `Replicated`
  column on insert) are kept.
- The fake server that answers `clickhouse-client` with a crafted result block
  moves to `helpers/fake_native_server.py`, shared by `04616` and
  `05473_sparse_truncated_values_native_protocol`.
- `05617_async_insert_native_serialization_kind_gate` loses the sparse `String`
  case: the kind is rejected before any data is read, so the column type does
  not matter and the sparse `UInt64` case covers it.

No scenario of the existing tests is removed.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…lify a test comment

- In `format.mdx` the sentence about `REPLICATED` and `DETACHED` in a `Tuple`
  element stack moves to its own line at the end of its paragraph, and the new
  "Kinds in blocks sent to a server" paragraph is written one sentence per line.
  The paragraphs render the same; the diff against master is easier to read.
- The header comment of `05617_async_insert_native_serialization_kind_gate` is
  reworded in shorter sentences, with the same content.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…arse-type-confusion

Reject non-default serialization kinds in data received over the native protocol
@robot-ch-test-poll robot-ch-test-poll 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 Nov 13, 2026
@robot-ch-test-poll

Copy link
Copy Markdown
Contributor Author

Dear @vdimir, @Avogar, this cherry-pick PR has not been updated for 3d0h0m49s. Please resolve the conflicts to backport #130895, or close this PR if the backport is no longer needed. This PR will be automatically closed after 7 days of inactivity.

The change is taken as in the original (1a44640, #130895); only the following is adapted.

- `src/Formats/NativeReader.h`, `src/Formats/NativeReader.cpp`: the new `allowed_kinds_` parameter and the
  `allowed_kinds(allowed_kinds_)` initializer go after `block_missing_values_`. The master-only `type_cache_`
  parameter and initializer are not added: `NativeReaderTypeCache` (#129831) is not on the branch, and the
  change does not use it. The branch keeps its own `format_settings_ = std::nullopt` default.
- `src/Processors/Formats/Impl/NativeFormat.cpp` (`makeReader`) and `src/Server/TCPHandler.cpp`
  (`initBlockInput`): the `/*type_cache_=*/ nullptr,` argument is dropped, because the constructor on the
  branch has no such parameter. These two files merged without a conflict, but would not compile as merged.
- `tests/queries/0_stateless/04616_replicated_serialization_oob_index_native_protocol.python` and
  `.reference`: the huge-element-count scenario (`replicatedBlockWithHugeElementCount`,
  `hugeElementCountScenario`, its call in `main`, its reference line) is left out. It comes from #129932,
  which is not on the branch: the test there never had the scenario or the `expectException` helper, and
  `SerializationReplicated` has no 'Suspiciously many elements' check. The new
  `replicatedTupleElementScenario` and `replicatedOnInsertScenario` are kept, with their reference lines.
- `tests/queries/0_stateless/05473_sparse_truncated_values_native_protocol.python`, `.reference`, `.sh`:
  left out. The original only edits this test, and it comes from #128363, which is not on the branch
  (modify/delete conflict).
- `docs/reference/interfaces/specs/NativeFormat.mdx`: git applied the hunks of
  `docs/resources/develop-contribute/native-protocol/format.mdx` here, where the same document lives on
  the branch. The added and removed lines are the same as in the original.

Everything else merged without conflicts and has the same changed lines as the original.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@vdimir
vdimir merged commit d2d83e7 into backport/26.9/130895 Nov 18, 2026
2 checks passed
@vdimir
vdimir deleted the cherrypick/26.9/130895 branch November 18, 2026 10:31
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.

3 participants