Skip to content

Bound the MySQL handshake response - #117003

Merged
alexey-milovidov merged 10 commits into
masterfrom
fix-mysql-ssl-handshake-size-limit
Sep 4, 2026
Merged

alexey-milovidov merged 10 commits into
masterfrom
fix-mysql-ssl-handshake-size-limit

Conversation

@alexey-milovidov

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

Copy link
Copy Markdown
Member

Closes: https://github.com/ClickHouse/clickhouse-private/issues/71252

On the TLS path the MySQL handshake response was read through MySQLPacketPayloadReadBuffer, which follows a chain of maximum-size packets as one logical message, and the fields inside the response are read up to a terminator. A peer that completed the TLS handshake could therefore make the server grow memory before sending any credentials.

Bound the response on both paths: the plaintext path rejects a declared payload larger than 64 KiB before reading it, and the TLS path, where the framing is handled by the packet endpoint, reads through a buffer limited to the same size. A real handshake response is a few hundred bytes.

Additionally, mysql_require_secure_transport was enforced from the client-advertised CLIENT_SSL capability bit rather than from the state of the transport. A client could set that bit in a plaintext HandshakeResponse without ever sending an SSLRequest, stay on the non-TLS branch of finishHandshake and still pass the gate, so authentication proceeded over cleartext. The check now keys off secure_connection, which is set only when the TLS transition actually happened.

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 handshake response of the MySQL wire protocol is now bounded, so a client can no longer make the server grow memory during the handshake. The mysql_require_secure_transport server setting is now enforced from the actual state of the connection instead of a capability bit advertised by the client.


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

Version info

  • Merged into: 26.9.1.692 (included in 26.9 and later)
  • Backported to: 26.8.3.35, 26.7.7.59, 26.6.5.70, 26.3.33.7

On the TLS path the handshake response was read through
`MySQLPacketPayloadReadBuffer`, which follows a chain of maximum-size packets as
one logical message, and the fields inside the response are read up to a
terminator. A peer that completed the TLS handshake could therefore stream data
and make the server grow memory before sending any credentials.

Bound the response on both paths: the plaintext path rejects a declared payload
larger than 64 KiB before reading it, and the TLS path, where the framing is
handled by the packet endpoint, reads through a buffer limited to the same size.
A real handshake response is a few hundred bytes.
@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 [9063240]

Summary: ✅


AI Review

Summary

This PR closes two real MySQL wire protocol gaps: it bounds the pre-authentication HandshakeResponse on both the plaintext and TLS paths, and it enforces mysql_require_secure_transport from the actual TLS state instead of the client-advertised CLIENT_SSL capability. I re-read the current diff together with the existing review threads and surrounding packet/authentication code; the earlier inline findings are fixed on the current head, and I did not find any remaining correctness, security, or coverage issue that warrants a new review comment.

Final Verdict

Status: ✅ Approve

LLVM Coverage Report

Measured on commit 9063240.

Metric Baseline Current Δ
Lines 88.70% 88.70% +0.00%
Functions 92.10% 92.10% +0.00%
Branches 81.00% 81.00% +0.00%

Changed lines: Changed C/C++ lines covered: 27/31 (87.10%) · 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
Comment thread src/Server/MySQLHandler.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Aug 29, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 90632404d with master 6113efbf0 (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

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

Job report

…path

Address review: limiting the underlying stream under `PacketEndpoint` was not
enough. `MySQLPacketPayloadReadBuffer` reports end of file as soon as the stream
below it ends, and `IMySQLReadPacket::readPayload` treats that as "the whole
packet was consumed", so a stream cut off exactly at the limit would make a
truncated packet look like a complete one: a peer could declare a payload of
`0xFFFFFF` and have the parsed prefix end at the limit.

Read the packet header on the TLS path as well, reject a declared payload larger
than the bound before reading it, and then read exactly as many bytes as the
header declares, which mirrors the plaintext path. Add a test case for a packet
that declares a maximum-size payload while carrying a complete, parseable prefix.
Comment thread src/Server/MySQLHandler.cpp Resolved
…ual transport state

`mysql_require_secure_transport` was checked against the client-advertised
`CLIENT_SSL` capability bit. A client can set that bit in a plaintext
`HandshakeResponse` without ever sending an `SSLRequest`: it then stays on the
non-TLS branch of `finishHandshake`, `secure_connection` remains false, and the
gate still lets authentication proceed over cleartext.

Key the check off `secure_connection`, which is set only when the TLS transition
actually happened, and add a focused integration test that drives a plaintext
handshake advertising `CLIENT_SSL` against a server with
`mysql_require_secure_transport`.
The test used the `default` user, which needs a password, so the handshake was
rejected by authentication rather than by the gate. Drive it with a
`no_password` user instead: without the fix the server now replies with an OK
packet on the unencrypted connection, which is exactly the bypass.

@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.

LGTM

@alexey-milovidov alexey-milovidov self-assigned this Sep 4, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 4, 2026
Merged via the queue into master with commit e31d3ca Sep 4, 2026
176 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-mysql-ssl-handshake-size-limit branch September 4, 2026 11:42
@robot-clickhouse robot-clickhouse added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Sep 4, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 4, 2026
clickhouse-gh Bot added a commit that referenced this pull request Sep 4, 2026
Backport #117003 to 26.8: Bound the MySQL handshake response
robot-clickhouse-ci-2 added a commit that referenced this pull request Sep 7, 2026
Cherry pick #117003 to 26.3: Bound the MySQL handshake response
robot-clickhouse-ci-2 added a commit that referenced this pull request Sep 7, 2026
Cherry pick #117003 to 26.6: Bound the MySQL handshake response
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Sep 7, 2026
alexey-milovidov added a commit that referenced this pull request Sep 7, 2026
Backport #117003 to 26.3: Bound the MySQL handshake response
clickhouse-gh Bot added a commit that referenced this pull request Sep 7, 2026
Backport #117003 to 26.6: Bound the MySQL handshake response
clickhouse-gh Bot pushed a commit that referenced this pull request Sep 8, 2026
Two backports left the per-file `ErrorCodes` declarations inconsistent, so
`check_cpp.sh` fails branch-wide:

- Backport #117011 added a `TOO_LARGE_STRING_SIZE` throw to
  `src/IO/ReadHelpers.cpp` without declaring the code in that file (it
  compiles only because `ReadHelpers.h` declares it, which the check does
  not follow). `master` declares it in the `.cpp` too.
- Backport #117003 kept the `SYNTAX_ERROR` declaration in
  `src/Server/MySQLHandler.cpp` but not the code that uses it.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
PedroTadim added a commit that referenced this pull request Sep 9, 2026
Backport #117003 to 26.7: Bound the MySQL handshake response
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Sep 11, 2026
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 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