]> git.kaiwu.me - haproxy.git/commitdiff
BUG/MINOR: tcpcheck: fix out-of-bounds reads on truncated SPOP hello frames
authorChristopher Faulet <cfaulet@haproxy.com>
Fri, 20 Nov 2026 10:42:38 +0000 (11:42 +0100)
committerChristopher Faulet <cfaulet@haproxy.com>
Fri, 20 Nov 2026 13:23:52 +0000 (14:23 +0100)
The SPOP health-check parser reads the frame header, then dispatches on the
frame type with a read right after the 11-byte header, and decodes the K/V
items of the payload. Nothing prevents these reads to go past the received
data:

  - With a frame announcing no payload (flen == 0) and a received data
    exactly covering the header, the frame type is read one byte past
    the received data. It is fully deterministic and does not even
    require the connection to be closed by the server.
  - When a K/V item key name ends exactly at the end of the received
    data, the type byte of the item value is read one byte past it.
  - The frame length check may also overflow: it is computed as
    4 + flen on a 32-bit value read from the wire, which wraps for a
    length close to UINT32_MAX.

The bytes past the received data are stale buffer content: the parsing and
the check result are then driven by bytes never sent by the server.

The SPOP parser now validates the received data itself: a frame with no
payload is invalid (both the AGENT-HELLO and AGENT-DISCONNECT frames carry
at least one K/V item), the length comparison is made overflow-safe, and the
type byte of every K/V item value is only read when the payload is not fully
consumed. Truncated frames are reported as invalid instead of being parsed
from bytes never received.

It is a read past the received data, inside the check buffer, so it requires
a non-compliant agent, which is out of the threat model (servers are
trusted). There is no security impact, only a check status that depends on
stale data.

It should be backported to all supported versions.

(Reported-by: Gleb Lizunov)

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

index cd0d3fe8d884b4138016d68ec11b9cb2bab85f38..67160dc51ca0977ab9eca85aa55628315a023878 100644 (file)
@@ -27,6 +27,11 @@ server s4 {
   sendhex "00000014 65 00000001 0000 0776657273696F6E 0803312E30"
 } -start
 
+server s5 {
+  recv 82
+  sendhex "00000000 65 00000001 0000"
+} -start
+
 syslog S1 -level notice {
     recv
     expect ~ "[^:\\[ ]\\[${h1_pid}\\]: Health check for server be1/srv succeeded, reason: Layer7 check passed.+info: \"SPOA server is ok\".+check duration: [[:digit:]]+ms, status: 1/1 UP."
@@ -47,6 +52,11 @@ syslog S4 -level notice {
     expect ~ "[^:\\[ ]\\[${h1_pid}\\]: Health check for server be4/srv failed, reason: Layer7 invalid response.+info: \"unsupported version\".+check duration: [[:digit:]]+ms, status: 0/1 DOWN."
 } -start
 
+syslog S5 -level notice {
+    recv
+    expect ~ "[^:\\[ ]\\[${h1_pid}\\]: Health check for server be5/srv failed, reason: Layer7 invalid response.+info: \"invalid frame received\".+check duration: [[:digit:]]+ms, status: 0/1 DOWN."
+} -start
+
 haproxy h1 -conf {
     global
     .if feature(THREAD)
@@ -90,9 +100,16 @@ haproxy h1 -conf {
         tcp-request content reject
         server srv ${s1_addr}:${s1_port}
 
+    backend be5
+        log ${S5_addr}:${S5_port} daemon
+        option log-health-checks
+        option spop-check
+        server srv ${s5_addr}:${s5_port} check inter 1s rise 1 fall 1
+
 } -start
 
 syslog S1 -wait
 syslog S2 -wait
 syslog S3 -wait
 syslog S4 -wait
+syslog S5 -wait
index 31f4a6544121df0a70d66687e4fafbeb480926af..9a15a06f3c59e6f216112452e2a8a43bb0e8a96b 100644 (file)
@@ -825,8 +825,14 @@ enum tcpcheck_eval_ret tcpcheck_spop_expect_hello(struct check *check, struct tc
        if (*(ptr+9) != 0 || *(ptr+10) != 0)
                goto invalid_frame;
 
-       if (b_data(&check->bi) < 4 + flen)
+       /* the length is read from the wire, make the comparison overflow-safe,
+        * and a frame with no payload is invalid: both the AGENT-HELLO and
+        * AGENT-DISCONNECT frames carry at least one K/V item.
+        */
+       if (flen > b_data(&check->bi) - 4)
                goto too_short;
+       if (!flen)
+               goto invalid_frame;
 
        ptr += 11;
        end = b_tail(&check->bi);
@@ -835,7 +841,7 @@ enum tcpcheck_eval_ret tcpcheck_spop_expect_hello(struct check *check, struct tc
                while (ptr < end) {
                        char  *str;
                        uint64_t sz;
-                       int ret;
+                       int type, ret;
 
                        /* Decode the item key */
                        ret = spoe_decode_buffer(&ptr, end, &str, &sz);
@@ -844,7 +850,9 @@ enum tcpcheck_eval_ret tcpcheck_spop_expect_hello(struct check *check, struct tc
 
                        /* Check "status-code" K/V item */
                        if (isteq(ist2(str, sz), ist("status-code"))) {
-                               int type = *ptr++;
+                               if (ptr == end)
+                                       goto invalid_frame;
+                               type = *ptr++;
 
                                /* The value must be an integer */
                                if ((type & SPOP_DATA_T_MASK) != SPOP_DATA_T_INT32 &&
@@ -861,7 +869,9 @@ enum tcpcheck_eval_ret tcpcheck_spop_expect_hello(struct check *check, struct tc
 
                        /* Check "message" K/V item */
                        else if (isteq(ist2(str, sz), ist("message"))) {
-                               int type = *ptr++;
+                               if (ptr == end)
+                                       goto invalid_frame;
+                               type = *ptr++;
 
                                /* The value must be a string */
                                if ((type & SPOP_DATA_T_MASK) != SPOP_DATA_T_STR)
@@ -885,7 +895,7 @@ enum tcpcheck_eval_ret tcpcheck_spop_expect_hello(struct check *check, struct tc
         while (ptr < end) {
                 char  *str;
                 uint64_t sz;
-                int ret;
+                int type, ret;
 
                 /* Decode the item key */
                 ret = spoe_decode_buffer(&ptr, end, &str, &sz);
@@ -894,7 +904,9 @@ enum tcpcheck_eval_ret tcpcheck_spop_expect_hello(struct check *check, struct tc
 
                 /* Check "version" K/V item */
                if (isteq(ist2(str, sz), ist("version"))) {
-                        int type = *ptr++;
+                       if (ptr == end)
+                               goto invalid_frame;
+                       type = *ptr++;
 
                         /* The value must be a string */
                         if ((type & SPOP_DATA_T_MASK) != SPOP_DATA_T_STR)
@@ -914,7 +926,9 @@ enum tcpcheck_eval_ret tcpcheck_spop_expect_hello(struct check *check, struct tc
                }
                 /* Check "max-frame-size" K/V item */
                 else if (isteq(ist2(str, sz), ist("max-frame-size"))) {
-                        int type = *ptr++;
+                       if (ptr == end)
+                               goto invalid_frame;
+                       type = *ptr++;
 
                         /* The value must be integer */
                         if ((type & SPOP_DATA_T_MASK) != SPOP_DATA_T_INT32 &&
@@ -932,7 +946,9 @@ enum tcpcheck_eval_ret tcpcheck_spop_expect_hello(struct check *check, struct tc
                }
                /* Check "capabilities" K/V item */
                 else if (isteq(ist2(str, sz), ist("capabilities"))) {
-                        int type = *ptr++;
+                       if (ptr == end)
+                               goto invalid_frame;
+                       type = *ptr++;
 
                         /* The value must be a string */
                         if ((type & SPOP_DATA_T_MASK) != SPOP_DATA_T_STR)