Repository navigation
Bound the MySQL handshake response - #117003
Conversation
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.
|
Workflow [PR], commit [9063240] Summary: ✅
AI ReviewSummaryThis PR closes two real MySQL wire protocol gaps: it bounds the pre-authentication Final VerdictStatus: ✅ Approve LLVM Coverage ReportMeasured on commit 9063240.
Changed lines: Changed C/C++ lines covered: 27/31 (87.10%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 11 s compile time in total, 7 of them have a recent master baseline. |
…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.
…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.
Backport #117003 to 26.8: Bound the MySQL handshake response
Cherry pick #117003 to 26.3: Bound the MySQL handshake response
Cherry pick #117003 to 26.6: Bound the MySQL handshake response
Backport #117003 to 26.3: Bound the MySQL handshake response
Backport #117003 to 26.6: Bound the MySQL handshake response
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]>
Backport #117003 to 26.7: Bound the MySQL handshake response
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_transportwas enforced from the client-advertisedCLIENT_SSLcapability bit rather than from the state of the transport. A client could set that bit in a plaintextHandshakeResponsewithout ever sending anSSLRequest, stay on the non-TLS branch offinishHandshakeand still pass the gate, so authentication proceeded over cleartext. The check now keys offsecure_connection, which is set only when the TLS transition actually happened.Changelog category (leave one):
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_transportserver 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
26.9.1.692(included in26.9and later)26.8.3.35,26.7.7.59,26.6.5.70,26.3.33.7