Skip to content

Cap pre-auth Hello packet strings to 64 KB and limit handshake time - #103284

Merged
CheSema merged 1 commit into
masterfrom
backport/tcp-hello-string-limits
Apr 22, 2026
Merged

CheSema merged 1 commit into
masterfrom
backport/tcp-hello-string-limits

Conversation

@CheSema

@CheSema CheSema commented Apr 21, 2026 •

Copy link
Copy Markdown
Member

An unauthenticated TCP client can allocate excessive server memory per connection by sending a crafted Hello packet with oversized string fields. Each readStringBinary call in receiveHello uses a default max size of 1 GiB (DEFAULT_MAX_STRING_SIZE), and all string fields are read before any authentication check.

This caps all pre-auth readStringBinary calls to 64 KB and adds a wall-clock handshake timeout (handshake_timeout_milliseconds, default 10 000 ms) to prevent slowloris-style attacks.

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):

Cap pre-auth TCP Hello packet strings to 64 KB and add handshake_timeout_milliseconds server setting to limit total handshake time, preventing unauthenticated clients from consuming excessive memory or holding threads indefinitely.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

New server setting handshake_timeout_milliseconds (default: 10000). When set, limits the total wall-clock time for the TCP handshake phase (Hello + Addendum). Connections exceeding this timeout are closed.

Version info

  • Merged into: 26.5.1.10
  • Backported to: 26.4.1.1119, 26.3.10.39, 26.2.17.24, 26.1.12.20

An unauthenticated TCP client can allocate excessive server memory per
connection by sending a crafted Hello packet with oversized string
fields. Each `readStringBinary` call in `receiveHello` uses a default
max size of 1 GiB (`DEFAULT_MAX_STRING_SIZE`), and all string fields
are read before any authentication check.

This caps all pre-auth `readStringBinary` calls to 64 KB using the
existing `max_string_size` parameter, and adds a wall-clock handshake
timeout (`handshake_timeout_milliseconds`) to prevent slowloris-style
attacks from holding a thread indefinitely.

Backport of ClickHouse/clickhouse-private#52882.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Apr 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [18b907f]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_llvm_coverage, 2/3) FAIL
01473_event_time_microseconds FAIL cidb
04078_native_protocol_input_validation FAIL cidb

AI Review

Summary

This PR hardens the TCP pre-auth handshake by capping Hello/addendum string fields to 64 KB and introducing handshake_timeout_milliseconds for wall-clock handshake timeout enforcement. The code and tests look solid overall, but there is one user-facing metadata inconsistency that should be fixed before merge.

PR Metadata

Changelog category (Bug Fix) matches the change. Changelog entry is present and sufficiently specific.
⚠️ The PR body’s documentation text says the default for handshake_timeout_milliseconds is 10000, while code sets the default to 30000.

Exact replacement text (PR body Documentation entry):
New server setting handshake_timeout_milliseconds (default: 30000). When set, limits the total wall-clock time for the TCP handshake phase (Hello + Addendum). Connections exceeding this timeout are closed.

Findings

💡 Nits

  • [src/Core/ServerSettings.cpp:1276] Setting default is 30000, but PR metadata/docs text says 10000, which is misleading for operators.
    • Suggested fix: align PR description/docs text with the code default (or intentionally change the code default and keep both consistent).
ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ⚠️ PR body docs text says default 10000, code default is 30000.
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Final Verdict

Status: ⚠️ Request changes

Minimum required action:

  1. Make the PR metadata/docs text consistent with the actual default of handshake_timeout_milliseconds (30000), or change code and metadata together to the intended default.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Apr 21, 2026
DECLARE(Float, distributed_cache_keep_up_free_connections_ratio, 0.1f, "Soft limit for number of active connection distributed cache will try to keep free. After the number of free connections goes below distributed_cache_keep_up_free_connections_ratio * max_connections, connections with oldest activity will be closed until the number goes above the limit.", 0) \
DECLARE(UInt64, tcp_close_connection_after_queries_num, 0, R"(Maximum number of queries allowed per TCP connection before the connection is closed. Set to 0 for unlimited queries.)", 0) \
DECLARE(UInt64, tcp_close_connection_after_queries_seconds, 0, R"(Maximum lifetime of a TCP connection in seconds before it is closed. Set to 0 for unlimited connection lifetime.)", 0) \
DECLARE(UInt64, handshake_timeout_milliseconds, 30000, R"(Wall-clock timeout in milliseconds for the entire TCP handshake phase (Hello + Addendum). Limits how long an unauthenticated connection can hold a thread. Set to 0 to disable.)", 0) \

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The new setting default here is 30000, but the PR description's "Documentation entry" says default 10000. Please align the PR metadata/docs text with the actual default to avoid user confusion when copying configuration values.

@clickhouse-gh

clickhouse-gh Bot commented Apr 21, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 91.10% 91.10% +0.00%
Branches 76.50% 76.60% +0.10%

Changed lines: 93.75% (60/64) | lost baseline coverage: 3 line(s) · Uncovered code

Full report · Diff report

@Michicosun Michicosun self-assigned this Apr 22, 2026
@CheSema
CheSema added this pull request to the merge queue Apr 22, 2026
Merged via the queue into master with commit b4b50f4 Apr 22, 2026
159 of 161 checks passed
@CheSema
CheSema deleted the backport/tcp-hello-string-limits branch April 22, 2026 10:14
@clickgapai

Copy link
Copy Markdown
Contributor

Hi — this PR may need backporting to 26.3 (LTS), 26.2, 26.1, 25.8 (LTS), but no backport label was found.

Affected code: TCPHandler::receiveHello, receiveAddendum, processClusterNameAndSalt, processUnexpectedHello — core code present in all supported branches.

Why: Security fix: unauthenticated TCP clients can allocate up to 4 GiB per connection (4 strings x 1 GiB each) before any auth check. TCPHandler::receiveHello has existed since the project began (git log shows TCPHandler.cpp added in commit 5b858de, 2020). The vulnerability exists in all supported branches.

If this should be backported, consider adding pr-must-backport or a version-specific label (e.g. v26.3-must-backport). Ignore this if backporting is not applicable.

@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Apr 22, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Apr 22, 2026
robot-ch-test-poll added a commit that referenced this pull request Apr 22, 2026
Cherry pick #103284 to 26.1: Cap pre-auth Hello packet strings to 64 KB and limit handshake time
robot-clickhouse added a commit that referenced this pull request Apr 22, 2026
robot-ch-test-poll added a commit that referenced this pull request Apr 22, 2026
Cherry pick #103284 to 26.2: Cap pre-auth Hello packet strings to 64 KB and limit handshake time
robot-clickhouse added a commit that referenced this pull request Apr 22, 2026
robot-ch-test-poll added a commit that referenced this pull request Apr 22, 2026
Cherry pick #103284 to 26.3: Cap pre-auth Hello packet strings to 64 KB and limit handshake time
robot-clickhouse added a commit that referenced this pull request Apr 22, 2026
robot-ch-test-poll added a commit that referenced this pull request Apr 22, 2026
Cherry pick #103284 to 26.4: Cap pre-auth Hello packet strings to 64 KB and limit handshake time
robot-clickhouse added a commit that referenced this pull request Apr 22, 2026
@robot-clickhouse robot-clickhouse added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Apr 22, 2026
CheSema added a commit that referenced this pull request Apr 27, 2026
The cherry-pick of #103284 dropped the `DECLARE(UInt64,
handshake_timeout_milliseconds, ...)` line from
`src/Core/ServerSettings.cpp` while keeping the matching `extern` in
`src/Server/TCPHandler.cpp`. As a result, all `Build` jobs on this
backport fail with:

    ld.lld-21: error: undefined symbol: DB::ServerSetting::handshake_timeout_milliseconds
    >>> referenced by TCPHandler.cpp:360 (TCPHandler.cpp.o)

Re-add the missing declaration. On `master` and on the `26.4` branch
the same line is already present (it landed earlier via the sync
PR #103269), so this commit only changes the older release branches
where the cherry-pick lost it.
CheSema added a commit that referenced this pull request Apr 27, 2026
The cherry-pick of #103284 dropped the `DECLARE(UInt64,
handshake_timeout_milliseconds, ...)` line from
`src/Core/ServerSettings.cpp` while keeping the matching `extern` in
`src/Server/TCPHandler.cpp`. As a result, all `Build` jobs on this
backport fail with:

    ld.lld-21: error: undefined symbol: DB::ServerSetting::handshake_timeout_milliseconds
    >>> referenced by TCPHandler.cpp:360 (TCPHandler.cpp.o)

Re-add the missing declaration. On `master` and on the `26.4` branch
the same line is already present (it landed earlier via the sync
PR #103269), so this commit only changes the older release branches
where the cherry-pick lost it.
CheSema added a commit that referenced this pull request Apr 27, 2026
The cherry-pick of #103284 dropped the `DECLARE(UInt64,
handshake_timeout_milliseconds, ...)` line from
`src/Core/ServerSettings.cpp` while keeping the matching `extern` in
`src/Server/TCPHandler.cpp`. As a result, all `Build` jobs on this
backport fail with:

    ld.lld-21: error: undefined symbol: DB::ServerSetting::handshake_timeout_milliseconds
    >>> referenced by TCPHandler.cpp:360 (TCPHandler.cpp.o)

Re-add the missing declaration. On `master` and on the `26.4` branch
the same line is already present (it landed earlier via the sync
PR #103269), so this commit only changes the older release branches
where the cherry-pick lost it.
CheSema added a commit that referenced this pull request Apr 27, 2026
Backport #103284 to 26.4: Cap pre-auth Hello packet strings to 64 KB and limit handshake time
CheSema added a commit that referenced this pull request Apr 27, 2026
Backport #103284 to 26.2: Cap pre-auth Hello packet strings to 64 KB and limit handshake time
CheSema added a commit that referenced this pull request Apr 27, 2026
Backport #103284 to 26.1: Cap pre-auth Hello packet strings to 64 KB and limit handshake time
CheSema added a commit that referenced this pull request Apr 27, 2026
Backport #103284 to 26.3: Cap pre-auth Hello packet strings to 64 KB and limit handshake time
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…3284

Backport ClickHouse#103284 to 26.4: Cap pre-auth Hello packet strings to 64 KB and limit handshake time
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v25.12-must-backport v26.2-must-backport v26.3-must-backport v26.4-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants