Skip to content

Commit b3a6d11

Browse files
committed
[MDEV-31585] Stop trusting or relying on client identifying information sent prior to the TLS handshake
The server has heretofore improperly mishandled—and TRUSTED—information sent in the plaintext login request packet sent prior to the TLS handshake. As a result of this, the client is *forced* to send excessive and exploitable identifying information in the pre-TLS-handshake plaintext login packet. That client-side vulnerability is CONC-654. This modifies the server to stop relying on any of the information in the pre-TLS-handshake plaintext login packet EXCEPT for the single bit that tells it that a TLS handshake will follow. It furthermore adds a capability bit to the server greeting packet, which informs the client that it is safe to send a bare-bones dummy packet containing ONLY the instruction that a TLS handshake will follow: /* This capability is set if: * * - The CLIENT knows how to send a truncated 2-byte SSLRequest * packet, containing no information other than the CLIENT_SSL flag * which is necessary to trigger the TLS handshake, and to send its * complete capability flags and other identifying information after * the TLS handshake. * - The SERVER knows how to receive this truncated 2-byte SSLRequest * packet, and to receive the client's complete capability bits * after the TLS handshake. * */ #define CLIENT_CAN_SSL_V2 (1ULL << 37) Because the client cannot safely send the SSL_V2 SSLRequest packet unless the server has advertised support for it in its (plaintext) Server Greeting packet, an active MITM could strip the CLIENT_CAN_SSL_V2 bit from that Server Greeting packet. This downgrade attack will force the client to continue exhibiting the CONC-654 vulnerability. The server is also modified to detect this case and abort the connection; this won't fix the one-time client information leakage of the CONC-654 vulnerability, but it is intended to discourage the MITM attack by making it highly visible. All new code of the whole pull request, including one or several files that are either new files or modified ones, are contributed under the BSD-new license. I am contributing on behalf of my employer Amazon Web Services, Inc.
1 parent fc45d4e commit b3a6d11

File tree

3 files changed

+90
-23
lines changed

3 files changed

+90
-23
lines changed

include/mysql_com.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,20 @@ enum enum_indicator_type
274274
/* Client no longer needs EOF packet */
275275
#define CLIENT_DEPRECATE_EOF (1ULL << 24)
276276

277+
/* This capability is set if:
278+
*
279+
* - The CLIENT knows how to send a truncated 2-byte SSLRequest
280+
* packet, containing no information other than the CLIENT_SSL flag
281+
* which is necessary to trigger the TLS handshake, and to send its
282+
* complete capability flags and other identifying information after
283+
* the TLS handshake.
284+
* - The SERVER knows how to receive this truncated 2-byte SSLRequest
285+
* packet, and to receive the client's complete capability bits
286+
* after the TLS handshake.
287+
*
288+
*/
289+
#define CLIENT_CAN_SSL_V2 (1ULL << 37)
290+
277291
#define CLIENT_PROGRESS_OBSOLETE (1ULL << 29)
278292
#define CLIENT_SSL_VERIFY_SERVER_CERT_OBSOLETE (1ULL << 30)
279293
/*

libmariadb

sql/sql_acl.cc

Lines changed: 75 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12761,6 +12761,7 @@ static bool send_server_handshake_packet(MPVIO_EXT *mpvio,
1276112761
if (ssl_acceptor_fd)
1276212762
{
1276312763
thd->client_capabilities |= CLIENT_SSL;
12764+
thd->client_capabilities |= CLIENT_CAN_SSL_V2; /* See parse_client_handshake_packet */
1276412765
}
1276512766

1276612767
if (data_len)
@@ -13253,30 +13254,31 @@ static ulong parse_client_handshake_packet(MPVIO_EXT *mpvio,
1325313254
*/
1325413255
DBUG_ASSERT(net->read_pos[pkt_len] == 0);
1325513256

13256-
ulonglong client_capabilities= uint2korr(net->read_pos);
13257-
compile_time_assert(sizeof(client_capabilities) >= 8);
13258-
if (client_capabilities & CLIENT_PROTOCOL_41)
13259-
{
13260-
if (pkt_len < 32)
13261-
DBUG_RETURN(packet_error);
13262-
client_capabilities|= ((ulong) uint2korr(net->read_pos+2)) << 16;
13263-
if (!(client_capabilities & CLIENT_MYSQL))
13264-
{
13265-
// it is client with mariadb extensions
13266-
ulonglong ext_client_capabilities=
13267-
(((ulonglong)uint4korr(net->read_pos + 28)) << 32);
13268-
client_capabilities|= ext_client_capabilities;
13269-
}
13270-
}
13271-
13272-
/* Disable those bits which are not supported by the client. */
13273-
compile_time_assert(sizeof(thd->client_capabilities) >= 8);
13274-
thd->client_capabilities&= client_capabilities;
13257+
ushort first_two_bytes_of_client_capabilities= uint2korr(net->read_pos);
13258+
bool pre_tls_client_packet_is_ssl_v2= (pkt_len==2 && first_two_bytes_of_client_capabilities == CLIENT_SSL);
13259+
bool pre_tls_client_packet_wants_ssl= !!(first_two_bytes_of_client_capabilities & CLIENT_SSL);
13260+
13261+
if (pre_tls_client_packet_wants_ssl)
13262+
{
13263+
/* Client wants to use TLS. This SSLRequest packet, sent in
13264+
* plaintext before the TLS handshake, is basically just a vestige
13265+
* that triggers the server (us) to start the TLS handshake.
13266+
*
13267+
* We ignore everything else in this pre-TLS packet, even though
13268+
* older clients send much of the same information that they will
13269+
* re-send over the TLS channel.
13270+
*
13271+
* This pre-TLS packet is untrustworthy AND if the server acts on
13272+
* its content, that FORCES the client to send more information
13273+
* in the clear.
13274+
*/
1327513275

13276-
DBUG_PRINT("info", ("client capabilities: %llu", thd->client_capabilities));
13277-
if (thd->client_capabilities & CLIENT_SSL)
13278-
{
1327913276
unsigned long errptr __attribute__((unused));
13277+
if (pre_tls_client_packet_is_ssl_v2)
13278+
DBUG_PRINT("info", ("client sent SSL_V2 SSLRequest packet (2 bytes with only TLS/SSL bit set)"));
13279+
else
13280+
DBUG_PRINT("info", ("client sent old SSLRequest packet (%ld bytes including TLS/SSL bit; capabilities & 0xffff == 0x%04x)",
13281+
pkt_len, first_two_bytes_of_client_capabilities));
1328013282

1328113283
/* Do the SSL layering. */
1328213284
if (!ssl_acceptor_fd)
@@ -13297,6 +13299,10 @@ static ulong parse_client_handshake_packet(MPVIO_EXT *mpvio,
1329713299
DBUG_PRINT("info", ("Immediately following IO layer change: vio_type=%s",
1329813300
safe_vio_type_name(thd->net.vio)));
1329913301

13302+
/* Now we are using TLS. The client will resend its REAL
13303+
* handshake packet, containing complete credentials and
13304+
* capability information.
13305+
*/
1330013306
DBUG_PRINT("info", ("Reading user information over SSL layer"));
1330113307
pkt_len= my_net_read(net);
1330213308
if (unlikely(pkt_len == packet_error || pkt_len < NORMAL_HANDSHAKE_SIZE))
@@ -13305,8 +13311,55 @@ static ulong parse_client_handshake_packet(MPVIO_EXT *mpvio,
1330513311
pkt_len));
1330613312
DBUG_RETURN(packet_error);
1330713313
}
13314+
13315+
/* Re-load the FIRST TWO BYTES of the capabilities from the packet sent over TLS. */
13316+
first_two_bytes_of_client_capabilities = uint2korr(net->read_pos);
13317+
}
13318+
13319+
ulonglong client_capabilities= (ulonglong) first_two_bytes_of_client_capabilities;
13320+
compile_time_assert(sizeof(client_capabilities) >= 8);
13321+
13322+
DBUG_PRINT("info", ("client capabilities: %llu", thd->client_capabilities));
13323+
if (client_capabilities & CLIENT_PROTOCOL_41)
13324+
{
13325+
if (pkt_len < 32)
13326+
DBUG_RETURN(packet_error);
13327+
client_capabilities|= ((ulong) uint2korr(net->read_pos+2)) << 16;
13328+
if (!(client_capabilities & CLIENT_MYSQL))
13329+
{
13330+
// it is client with mariadb extensions
13331+
ulonglong ext_client_capabilities=
13332+
(((ulonglong)uint4korr(net->read_pos + 28)) << 32);
13333+
client_capabilities|= ext_client_capabilities;
13334+
}
13335+
}
13336+
bool post_tls_client_packet_indicates_ssl_v2= (client_capabilities & CLIENT_CAN_SSL_V2);
13337+
13338+
if (pre_tls_client_packet_wants_ssl
13339+
&& post_tls_client_packet_indicates_ssl_v2
13340+
&& !pre_tls_client_packet_is_ssl_v2)
13341+
{
13342+
/* 1. We told the client in our server greeting that we support the pre-TLS client packet containing only the TLS/SSL flag,
13343+
* CLIENT_CAN_SSL_V2. [Server greeting packet is sent in the clear, may be MITM'ed en route to the client.]
13344+
* 2. The client told us in its pre-TLS SSLRequest packet that it wants to use SSL. (CLIENT_SSL flag)
13345+
* 3. The client told us in its post-TLS packet that it too supports the pre-TLS client packet containing only the TLS/SSL flag,
13346+
* CLIENT_CAN_SSL_V2. [We received this information via TLS; assuming the client validated our server certificate
13347+
* to avoid a 2-sided TLS MITM, we know that this packet is authentically from the client.]
13348+
* 4. Nevertheless, the client DID NOT SEND us an SSL_V2-style SSLRequest packet.
13349+
*
13350+
* The only way this can happen is if the client is being downgraded by an active MITM attacker which
13351+
* disables the CLIENT_CAN_SSL_V2 bit in our server greeting packet.
13352+
*/
13353+
sql_print_warning("Aborting connection %lld because it is being actively MITM'ed to downgrade TLS security (attacker "
13354+
"is stripping the CLIENT_CAN_SSL_V2 bit from our server capabilities)",
13355+
thd->thread_id);
13356+
DBUG_RETURN(packet_error);
1330813357
}
1330913358

13359+
/* Disable those bits which are not supported by the client. */
13360+
compile_time_assert(sizeof(thd->client_capabilities) >= 8);
13361+
thd->client_capabilities&= client_capabilities;
13362+
1331013363
if (client_capabilities & CLIENT_PROTOCOL_41)
1331113364
{
1331213365
thd->max_client_packet_length= uint4korr(net->read_pos+4);

0 commit comments

Comments
 (0)