]> git.kaiwu.me - haproxy.git/commitdiff
BUG/MINOR: tcpcheck: fix out-of-bounds read on the MySQL server version
authorChristopher Faulet <cfaulet@haproxy.com>
Fri, 20 Nov 2026 10:16:25 +0000 (11:16 +0100)
committerChristopher Faulet <cfaulet@haproxy.com>
Fri, 20 Nov 2026 15:56:26 +0000 (16:56 +0100)
On success, the MySQL health-check stores the server version, read at offset
5 of the received buffer, as the check description. The version is a
NUL-terminated string, but nothing guarantees the string is terminated,
neither inside the received data nor past its end. And the version pointer
is directly passed to set_server_check_status(), which copies it with
strncpy() up to 127 bytes, stopping at the first NUL byte. With a
non-compliant server sending a handshake with an unterminated version and no
NUL byte in the rest of the received data, the copy reads past the received
data, into the remaining content of the buffer chunk, and the stale bytes
are disclosed in the check status, visible in the logs, the stats page and
'show servers state'.

To fix it, the version is bounded to the received data before being copied
into the check status, like the plugin name of an auth switch request. This
is a rework of the out-of-bounds reads fixed by 3bbfc3bd7 ('BUG/MINOR:
tcpcheck: fix out-of-bounds reads on short MySQL error packets'), which
covered the error packets but not the success path.

This is a read past the received data, so it requires a non-compliant
server, which is out of the threat model (servers are trusted). Thus there
is no security impact here, only a potential information leak in the check
status.

This patch should fix the issue #3596. It should be backported to all
supported versions.

reg-tests/checks/mysql-check.vtc
src/tcpcheck.c

index 42ebbb9894c40456806bbd96d3ac9a7fcf72ae67..74fc296603e49a32457344f5fb6f95add2791baa 100644 (file)
@@ -40,6 +40,14 @@ server s6 {
   sendhex "4A0000000A382E302E3139000A0000006F3C025E6249410D00FFFFFF0200FFC715000000000000000000007C182159106E2761144322200063616368696E675F736861325F70617373776F726400"
   recv 69
   sendhex "0700000200000002000000"
+server s12 {
+  sendhex "2C0000000A382E302E31392D6E6F7465726D424242424242424242424242424242424242424242424242424242424242"
+  expect_close
+} -start
+
+syslog S12 -level notice {
+    recv
+    expect ~ "[^:\\[ ]\\[${h1_pid}\\]: Health check for server be12/srv succeeded, reason: Layer7 check passed.+info: \"8.0.19-notermBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\".+check duration: [[:digit:]]+ms, status: 1/1 UP."
 } -start
 
 syslog S1 -level notice {
@@ -142,6 +150,12 @@ haproxy h1 -conf {
         tcp-request content reject
         server srv ${s6_addr}:${s6_port}
 
+    backend be12
+        log ${S12_addr}:${S12_port} daemon
+        option log-health-checks
+        option mysql-check
+        server srv ${s12_addr}:${s12_port} check inter 1s rise 1 fall 1
+
 } -start
 
 syslog S1 -wait
@@ -150,3 +164,4 @@ syslog S3 -wait
 syslog S4 -wait
 syslog S5 -wait
 syslog S6 -wait
+syslog S12 -wait
index 9a15a06f3c59e6f216112452e2a8a43bb0e8a96b..99993a7a103252f5b89959abfd658bd00894841d 100644 (file)
@@ -555,11 +555,10 @@ static enum tcpcheck_eval_ret tcpcheck_mysql_expect_packet(struct check *check,
 {
        enum tcpcheck_eval_ret ret = TCPCHK_EVAL_CONTINUE;
        enum healthcheck_status status;
+       struct ist desc = IST_NULL, version;
        struct buffer *msg = NULL;
-       struct ist desc = IST_NULL;
        unsigned int err = 0, plen = 0;
 
-
        TRACE_ENTER(CHK_EV_TCPCHK_EXP, check);
 
        /* 3 Bytes for the packet length, 1 byte for the sequence id and 1 byte for the type */
@@ -612,9 +611,20 @@ static enum tcpcheck_eval_ret tcpcheck_mysql_expect_packet(struct check *check,
        /* We set the MySQL Version in description for information purpose
         * FIXME : it can be cool to use MySQL Version for other purpose,
         * like mark as down old MySQL server.
+        *
+        * The version is a NUL-terminated string, but a non-compliant server
+        * may not terminate it. It is bounded to the received data to not
+        * rely on the buffer content past its end when it is copied into the
+        * check status.
         */
        status = ((rule->expect.ok_status != HCHK_STATUS_UNKNOWN) ? rule->expect.ok_status : HCHK_STATUS_L7OKD);
-       set_server_check_status(check, status, b_peek(&check->bi, 5));
+       version = iststop(ist2(b_peek(&check->bi, 5), b_data(&check->bi) - 5), '\0');
+       msg = alloc_trash_chunk();
+       if (msg) {
+               chunk_istcat(msg, version);
+               b_putchr(msg, '\0');
+       }
+       set_server_check_status(check, status, (msg ? b_head(msg) : NULL));
 
   out:
        free_trash_chunk(msg);