Skip to content

Cherry pick #117015 to 26.7: Bound the collections read from the Native protocol - #125269

Merged
robot-ch-test-poll merged 19 commits into
backport/26.7/117015from
cherrypick/26.7/117015
Oct 12, 2026
Merged

robot-ch-test-poll merged 19 commits into
backport/26.7/117015from
cherrypick/26.7/117015

Conversation

@robot-ch-test-poll2

Copy link
Copy Markdown
Contributor

Original pull-request #117015

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

alexey-milovidov and others added 18 commits August 29, 2026 05:25
Several fields of the protocol are allocated at a size the peer declares before
the data behind them arrives, and the collections among them had no bound that
the data can plausibly have. In interserver mode `ClientInfo`, the settings and
the externally granted roles of a query are deserialized before its secret hash
is checked.

- The settings and the query parameters of a query are read in a loop that ends
  on an empty name, with no bound on the number of pairs.
- The strings of `ClientInfo` are short metadata - a user name, a host name, a
  quota key, a user agent, a URL - but were allowed a gigabyte each, and the
  number of current roles was allowed a billion entries, which is resized
  before the names are read. The `Hello` packet already bounds its fields.
- The same for the externally granted roles.
- `TablesStatusRequest` and `TablesStatusResponse` allowed a billion tables.
- The obsolete `PartUUIDs` packet let a server make a client read sixteen
  gigabytes off the socket.

`readVectorBinary` now takes the bound as an argument, so a caller that reads
from an untrusted peer can pass what the data can plausibly hold.
`MAX_EXTRA_ROLES` is used in the interserver branch, which is compiled out when
ClickHouse is built without SSL, so `-Wunused-const-variable` fails the build.
A bound on the number of elements is not enough: a `String` element is resized to its declared
size before its value arrives in the same way the collection is, so a peer can declare a single
role name or table identifier of a gigabyte and force the allocation on the same pre-authentication
path.

- `readVectorBinary` now takes a bound for the element as well, and `ClientInfo::read` and the
  externally granted roles of `TCPHandler` pass the identifier bound to it.
- `TablesStatusRequest::read` and `TablesStatusResponse::read` bound the database and the table
  name.
- The receiver-side limits are documented in the `NativeProtocol` specification.
- The test covers the table-status paths, and is renumbered to avoid colliding with the tests
  merged into master.
… protocol

The count bound on a settings or parameters sequence does not cover a single pair: a peer could declare a gigabyte-long setting or parameter name and never send it, forcing the allocation before the interserver hash is checked. A name is an identifier, so `BaseSettingsHelpers::readName` bounds it by the same 64 KiB as the strings of the `Hello` packet.

The values keep the generic 1 GiB bound: a parameter is query-sized data, and the query text itself has to be read at that bound before the `auth_hash` covering it can be checked, so a tighter bound on values would break large parameters without lowering what a peer can make the server allocate.

The test now speaks protocol revision 54459 so it can send a `Parameter` entry, and covers both names. The spec lists the new limit.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…rotocol-collection-limits

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…gtest

Master switched `BaseSettingsHelpers::readString` to `readStringBinaryGrowing` and added
`Settings.ADeclaredLengthOnTheSettingsWireIsNotAnAllocation`, which expects a 64 MiB declared name
to run out of buffer. With the name bound of this PR the name is refused up front with
`TOO_LARGE_STRING_SIZE` instead, so the case now expects that code. `readName` also reads growing,
like `readString`.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…them at 64 KiB

`ClientInfo::read` also decodes the embedded `ClientInfo` of async `Distributed` queue files, and
`ClientInfo` is forwarded to shards from the other interfaces, where e.g. an HTTP `query_id` or
quota key can exceed 64 KiB. A 64 KiB cap made the server unable to drain queue files it wrote
itself. Read the strings with `readStringBinaryGrowing` under the generic bound instead: a declared
size still does not become an allocation on its own, and long legitimate values keep working.

Add a test with a 100 KiB HTTP `query_id` through an async `Distributed` insert.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The unexpected-`Query` recovery path did not skip the externally granted roles that every client
at `DBMS_MIN_PROTOCOL_VERSION_WITH_INTERSERVER_EXTERNALLY_GRANTED_ROLES` sends before the interserver
hash, so the rest of the packet was parsed at wrong offsets. Also read the query id under the same
bound as `processQuery` and the parameters with `readQueryParameters` instead of `Settings::read`.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The count came from the peer and was only checked against `DEFAULT_MAX_STRING_SIZE`, so a client
at revision 54480 or newer could make the server resize the vector to about 4 GiB before any bucket
id arrived. A list of two-level aggregation bucket ids cannot name more than the 256 buckets.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…ead` with gtests

The stateless harness speaks a revision below the one that carries
`current_roles`, so the bound on the role count and on a role name had
no coverage.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…-collection-limits

Bound the collections read from the Native protocol
@robot-ch-test-poll2 robot-ch-test-poll2 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-bugfix Pull request with bugfix, not backported by default labels Oct 11, 2026
…e conflicts

The conflicts come from prerequisites that are not on 26.7: the valueless setting
helpers (`throwValuelessSettingIsNotBool`, `throwValuelessSettingHasValue`,
`formatValueForErrorMessage`), the `http_handler_name` / `http_request_url` fields
of `ClientInfo` and the `setClientVersionFromConnectionIfUnknown` tests. Kept the
26.7 code and applied only the changes of #117015 on top of it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robot-ch-test-poll
robot-ch-test-poll merged commit a704d8e into backport/26.7/117015 Oct 12, 2026
1 check passed
@robot-ch-test-poll
robot-ch-test-poll deleted the cherrypick/26.7/117015 branch October 12, 2026 14:16
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-bugfix Pull request with bugfix, not backported by default pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants