]> git.kaiwu.me - haproxy.git/commitdiff
BUG/MINOR: tcpcheck: fix out-of-bounds reads on truncated LDAP bind responses
authorChristopher Faulet <cfaulet@haproxy.com>
Fri, 20 Nov 2026 10:42:17 +0000 (11:42 +0100)
committerChristopher Faulet <cfaulet@haproxy.com>
Fri, 20 Nov 2026 13:23:52 +0000 (14:23 +0100)
The LDAP health-check relies on the min-recv parameter of the expect rule
matching the first byte of the bind response to guarantee enough data are
received before parsing the message. But this parameter is only honored
while data are still expected: on a FIN or a connection error, the last read
is already reached and the rule is evaluated on the received data only. A
truncated response may thus be parsed with fewer bytes than the parser
assumes:

  - With a single byte received, the message size is read one byte past
    the received data.
  - With a message announcing a size lower than 4, the 4-byte comparison
    on the message ID and the bindResponse tag reads up to 3 bytes past
    the received data.

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 LDAP parser now validates the received data itself: the message size
must be received, and the message must be at least 5 bytes to cover the
message ID, the bindResponse tag and its length. Shorter responses are
reported as truncated.

It is a read past the received data, inside the check buffer, so it requires
a non-compliant server, 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/ldap-check.vtc
src/tcpcheck.c

index 2329f681ca91d01a23060f80b8f7fa42d22cded0..2202afdf956b850196eee71b0a4c07bc11a620cd 100644 (file)
@@ -29,6 +29,16 @@ server s4 {
   sendhex "00 04000400"
 } -start
 
+server s5 {
+  recv 14
+  sendhex "30"
+} -start
+
+server s6 {
+  recv 14
+  sendhex "300302010 1"
+} -start
+
 syslog S1 -level notice {
     recv
     expect ~ "[^:\\[ ]\\[${h1_pid}\\]: Health check for server be1/srv succeeded, reason: Layer7 check passed.+info: \"Success\".+check duration: [[:digit:]]+ms, status: 1/1 UP."
@@ -49,6 +59,16 @@ syslog S4 -level notice {
     expect ~ "[^:\\[ ]\\[${h1_pid}\\]: Health check for server be4/srv succeeded, reason: Layer7 check passed.+info: \"Success\".+check duration: [[:digit:]]+ms, status: 1/1 UP."
 } -start
 
+syslog S5 -level notice {
+    recv
+    expect ~ "[^:\\[ ]\\[${h1_pid}\\]: Health check for server be5/srv failed, reason: Layer7 invalid response.+check duration: [[:digit:]]+ms, status: 0/1 DOWN."
+} -start
+
+syslog S6 -level notice {
+    recv
+    expect ~ "[^:\\[ ]\\[${h1_pid}\\]: Health check for server be6/srv failed, reason: Layer7 invalid response.+check duration: [[:digit:]]+ms, status: 0/1 DOWN."
+} -start
+
 haproxy h1 -conf {
     global
     .if feature(THREAD)
@@ -92,9 +112,23 @@ 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 ldap-check
+        server srv ${s5_addr}:${s5_port} check inter 1s rise 1 fall 1
+
+    backend be6
+        log ${S6_addr}:${S6_port} daemon
+        option log-health-checks
+        option ldap-check
+        server srv ${s6_addr}:${s6_port} check inter 1s rise 1 fall 1
+
 } -start
 
 syslog S1 -wait
 syslog S2 -wait
 syslog S3 -wait
 syslog S4 -wait
+syslog S5 -wait
+syslog S6 -wait
index 80fd4ad6846213f53cebbd652c285e2886147428..31f4a6544121df0a70d66687e4fafbeb480926af 100644 (file)
@@ -685,10 +685,11 @@ enum tcpcheck_eval_ret tcpcheck_ldap_expect_bindrsp(struct check *check, struct
         */
        ptr = b_head(&check->bi);
        end = ptr + b_data(&check->bi);
-       ptr++; /* First byte was already matched by the previous expect rule
-               * and at least 14 bytes are available
-               * (see do_parse_ldap_check_opt)
-               */
+       ptr++; /* First byte was already matched by the previous expect rule.
+                * The message size is read first, it must be available.
+                */
+       if (ptr >= end)
+               goto too_short;
 
        /* size of LDAPMessage */
        if (*ptr & 0x80) {
@@ -713,7 +714,7 @@ enum tcpcheck_eval_ret tcpcheck_ldap_expect_bindrsp(struct check *check, struct
                msglen = *ptr;
        ptr += 1 + nbytes;
 
-       if (end - ptr < msglen)
+       if (end - ptr < msglen || msglen < 5)
                goto too_short;
 
        /* http://tools.ietf.org/html/rfc4511#section-4.2.2