Skip to content

Cherry pick #117015 to 26.3: Bound the collections read from the Native protocol - #125268

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

robot-ch-test-poll merged 19 commits into
backport/26.3/117015from
cherrypick/26.3/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
Resolve the conflicts of the cherry-pick of #117015 to 26.3 by keeping the 26.3 code and applying
only the bounds introduced by the PR:
- `Connection.cpp`: 26.3 still reads `part_uuids` with `readVectorBinary`; the obsolete-packet skip
  path the PR changes does not exist there, so keep the 26.3 code.
- `BaseSettings`: add `MAX_SETTINGS_IN_A_SEQUENCE`, `MAX_SETTING_NAME_SIZE`, `readName` and
  `throwTooManySettings` without the valueless-settings helpers that 26.3 lacks.
- `Settings.cpp`: 26.3 has no `readQueryParameters`; query parameters are read through
  `Settings::read`, which is bounded by the change in `BaseSettings`. Likewise keep
  `skip_settings.read` for the parameters in `TCPHandler::processUnexpectedQuery`.
- `ReadHelpers.h`: add `max_element_size` to the 26.3 `readVectorBinary` signature.
- `ClientInfo.cpp`: read the strings with `readStringBinaryGrowing`; 26.3 has no trailing fields
  and no `current_roles`, so their bounds are dropped.
- `TablesStatus.cpp`: add the bounds without the `getAuthDigest` that 26.3 lacks.
- `gtest_settings.cpp`: keep the 26.3 peak-memory measurement and check `expected_code`.
- Drop `gtest_client_info_read.cpp` and the native protocol doc, which do not exist in 26.3.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Resolved the conflicts with backport/26.3/117015 in b9be8ed. I kept the 26.3 code and applied only this PR's bounds. Several prerequisites from master are not in 26.3, so these parts were dropped: the skip path for obsolete part_uuids in Connection.cpp, readQueryParameters (26.3 reads query parameters through Settings::read, which the new BaseSettings limits already bound), the trailing ClientInfo fields and current_roles, gtest_client_info_read.cpp, and the native protocol doc. The changed translation units compile against the 26.3 tree; I did not run a full build or the tests locally, so CI still has to confirm them.

@robot-ch-test-poll
robot-ch-test-poll merged commit 03eef25 into backport/26.3/117015 Oct 12, 2026
1 check passed
@robot-ch-test-poll
robot-ch-test-poll deleted the cherrypick/26.3/117015 branch October 12, 2026 14:15
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