Skip to content

Bound the collections read from the Native protocol - #117015

Merged
alexey-milovidov merged 17 commits into
masterfrom
fix-server-native-protocol-collection-limits
Oct 11, 2026
Merged

alexey-milovidov merged 17 commits into
masterfrom
fix-server-native-protocol-collection-limits

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Aug 29, 2026 •

Copy link
Copy Markdown
Member

Closes: https://github.com/ClickHouse/clickhouse-private/issues/71255
Related: #117011

Several fields of the Native 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. The Hello packet already bounds its fields the same way.
  • 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. It also takes a bound for a String element, because an element is resized to its declared size before its value arrives in the same way the collection is: a single role name or table identifier of a gigabyte would otherwise force the allocation on the same path.

The receiver-side limits are documented in the NativeProtocol specification.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

The collections of the Native protocol — the settings and the query parameters of a query, the client information, the granted roles and the table-status lists — are now bounded, so a small packet can no longer make the server allocate memory for data that has not arrived.


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

Version info

  • Merged into: 26.10.1.2158-master (included in 26.10 and later)
  • Backported to: 26.9.17.5, 26.8.25.8, 26.7.29.11, 26.3.50.8

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.
@alexey-milovidov alexey-milovidov added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Aug 29, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 29, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [67236f3]

Summary: ✅


AI Review

Summary

This PR hardens Native TCP parsing against peer-declared size and count amplification: settings and parameter names are capped, ClientInfo strings are read incrementally, role and table-status collections get explicit ceilings, the obsolete PartUUIDs skip is bounded, BlockInfo::out_of_order_buckets is capped, and the protocol doc/tests were updated accordingly. I re-checked the current diff together with all prior discussion threads, and I do not see a remaining contract violation in the code as it stands.

Final Verdict

Status: ✅ Approve

LLVM Coverage Report

Measured on commit 67236f3.

Metric Baseline Current Δ
Lines 88.20% 88.20% +0.00%
Functions 92.10% 92.10% +0.00%
Branches 79.70% 79.70% +0.00%

Changed lines: Changed C/C++ lines covered: 132/147 (89.80%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Aug 29, 2026
`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.
Comment thread src/Interpreters/ClientInfo.cpp Resolved
Comment thread src/Interpreters/TablesStatus.cpp Resolved
Comment thread src/Server/TCPHandler.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Aug 29, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 67236f39a with master 915744a54 (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

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

Job report

@thevar1able thevar1able self-assigned this Aug 31, 2026
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.
@mintlify

mintlify Bot commented Aug 31, 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 Oct 3, 2026, 6:53 PM

Comment thread src/Core/BaseSettings.h Resolved
Comment thread src/Core/Settings.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-protocols Server wire protocols + request/response handling (native TCP, HTTP, MySQL/PostgreSQL wire protoc... label Sep 4, 2026
alexey-milovidov and others added 2 commits September 16, 2026 07:32
… 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]>
Comment thread src/Interpreters/ClientInfo.cpp Resolved
Comment thread src/Server/TCPHandler.cpp Resolved
@groeneai

Copy link
Copy Markdown
Collaborator

There is one Native-protocol collection this PR does not cover, and I would rather ask than open a parallel PR next to it.

BlockInfo field 3 (std::vector<Int32> out_of_order_buckets, gated at DBMS_MIN_REVISION_WITH_OUT_OF_ORDER_BUCKETS_IN_AGGREGATION = 54480) is read through the generic READ_FIELD macro, so it lands in the unparameterised readBinary(V &, ReadBuffer &) at src/IO/ReadHelpers.h:1929. The only guard there is size > DEFAULT_MAX_STRING_SIZE compared against an element count, so it accepts 1073741824 elements and x.resize(size) commits 4 GiB before one payload byte is read. TCPHandler builds its NativeReader with the client-declared client_tcp_protocol_version (src/Server/TCPHandler.cpp:3043) and NativeReader::read calls info.read for every block, so both the revision and the count are peer-controlled.

The legitimate content is at most 4 elements: ConvertingAggregatedToChunksTransform stops delaying buckets at NUM_OOO_BUCKETS = 4 (src/Processors/Transforms/AggregatingTransform.cpp:858 and :935), out of 256 possible bucket ids.

Two measured details that may affect how you want it bounded:

  • max_server_memory_usage does not refuse this one. The vector uses std::allocator, so it is counted through the intercepted operator new, but CurrentMemoryTracker::allocThrow enforces the limit only at min_allocation_size_to_throw_on_memory_limit, whose default of 0 is stored as UInt64::max. The spike is transient, since the element loop then fails on the truncated payload, so the cost is a repeatable 4 GiB commit per packet rather than a leak.
  • The SQL Native path is unaffected: NativeInputFormat passes server_revision = 0 and NativeReader::read skips info.read entirely, so only the TCP path reaches this.

Would you prefer this bounded inside #117015, or should I open a separate PR touching only src/Core/BlockInfo.*? A structural ceiling of 256 in BlockInfo::read is sufficient and cannot reject a valid list, since a bucket id list cannot name more buckets than exist. Either way I will not push to this branch.

@alexey-milovidov

…rotocol-collection-limits

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Comment thread src/IO/ReadHelpers.h Resolved
…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]>
@groeneai

groeneai commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator

All three have a fix; the two merged ones are in your master merge 67236f39a412, while the b6225e33 run predates both:

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 @groeneai thanks, confirmed. The latest run on 67236f39a412 (which includes #123763 and #123636) is green apart from the pending CH Inc sync. The 05237 DBReplicated case is tracked by #123790.

@clickhouse-gh

clickhouse-gh Bot commented Oct 11, 2026

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 31 queries analysed

no significant changes detected. K_source=6 K_base=30 flagged=0/65

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 861f11ef-11ee-4639-8e1e-371c54c4f5d1
  • MIRAI run: 69d69629-206c-4879-a9d7-fcdd581e65cb
  • PR check IDs:
    • clickbench_904899_1791742119
    • clickbench_904905_1791742119
    • clickbench_904911_1791742119
    • tpch_adapted_1_official_904923_1791742119
    • tpch_adapted_1_official_904933_1791742119
    • tpch_adapted_1_official_904970_1791742119

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good.

@alexey-milovidov alexey-milovidov self-assigned this Oct 11, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 11, 2026
Merged via the queue into master with commit a58c638 Oct 11, 2026
149 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-server-native-protocol-collection-limits branch October 11, 2026 21:40
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 11, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 11, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 11, 2026
Backport #117015 to 26.9: Bound the collections read from the Native protocol
clickhouse-gh Bot added a commit that referenced this pull request Oct 12, 2026
Backport #117015 to 26.8: Bound the collections read from the Native protocol
alexey-milovidov added a commit that referenced this pull request Oct 12, 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]>
alexey-milovidov added a commit that referenced this pull request Oct 12, 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]>
robot-ch-test-poll added a commit that referenced this pull request Oct 12, 2026
Cherry pick #117015 to 26.3: Bound the collections read from the Native protocol
robot-ch-test-poll added a commit that referenced this pull request Oct 12, 2026
Cherry pick #117015 to 26.7: Bound the collections read from the Native protocol
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 12, 2026
alexey-milovidov added a commit that referenced this pull request Oct 12, 2026
Backport #117015 to 26.3: Bound the collections read from the Native protocol
clickhouse-gh Bot added a commit that referenced this pull request Oct 12, 2026
Backport #117015 to 26.7: Bound the collections read from the Native protocol
strtgbb pushed a commit to Altinity/ClickHouse that referenced this pull request Oct 12, 2026
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-bugfix Pull request with bugfix, not backported by default 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.

5 participants