]> git.kaiwu.me - haproxy.git/commitdiff
BUG/MINOR: net_helper: check the TCP option length before reading its value
authorMartin Mirchev <mirchevmartin2203@gmail.com>
Wed, 18 Nov 2026 03:25:19 +0000 (11:25 +0800)
committerWilly Tarreau <w@1wt.eu>
Fri, 20 Nov 2026 11:36:57 +0000 (12:36 +0100)
tcp_fullhdr_find_opt() only checks that an option fits in the TCP header
and has a length of at least 2, so the tcp.options.mss, tcp.options.tsval
and tcp.options.wscale converters read their value from the bytes that
follow the option when its length field is too small. With a 24-byte
header ending with "NOP NOP 02 02" followed by the payload 0x12 0x34,
tcp.options.mss returns 4660, and with "NOP NOP 03 02" followed by 0xaa,
tcp.options.wscale returns 170. When the option is the last one and
nothing follows the header, the value is read past the end of the input
sample (by up to 4 bytes for tsval).

This patch makes these 3 converters ignore the option when its length is
not the one defined for it (4 for MSS, 10 for TimeStamp, 3 for Window
Scale), as when the option is absent, so that they return 0 in this case. A new
reg-test, reg-tests/converter/net_helper.vtc, checks these cases.

This should be backported to 3.4.

reg-tests/converter/net_helper.vtc [new file with mode: 0644]
src/net_helper.c

diff --git a/reg-tests/converter/net_helper.vtc b/reg-tests/converter/net_helper.vtc
new file mode 100644 (file)
index 0000000..85d6450
--- /dev/null
@@ -0,0 +1,40 @@
+varnishtest "net_helper converters Test"
+
+feature cmd "$HAPROXY_PROGRAM -cc 'version_atleast(3.4-dev0)'"
+feature ignore_unknown_macro
+
+haproxy h1 -conf {
+    global
+    .if feature(THREAD)
+        thread-groups 1
+    .endif
+
+    defaults
+       mode http
+       timeout connect "${HAPROXY_TEST_TIMEOUT-5s}"
+       timeout client  "${HAPROXY_TEST_TIMEOUT-5s}"
+       timeout server  "${HAPROXY_TEST_TIMEOUT-5s}"
+
+    frontend fe
+       bind "fd@${fe}"
+
+       # 24-byte TCP headers: 20 bytes followed by 4 bytes of options,
+       # then possibly some payload.
+       http-request return status 200
+       http-after-response set-header mss-ok "%[bin(00010050000000000000000060020000ffff000002040578),tcp.options.mss]"
+       http-after-response set-header mss-len2 "%[bin(00010050000000000000000060020000ffff0000010102021234),tcp.options.mss]"
+       http-after-response set-header ws-ok "%[bin(00010050000000000000000060020000ffff000001030307),tcp.options.wscale]"
+       http-after-response set-header ws-len2 "%[bin(00010050000000000000000060020000ffff000001010302aa),tcp.options.wscale]"
+       http-after-response set-header tsval-len2 "%[bin(00010050000000000000000060020000ffff00000101080212345678),tcp.options.tsval]"
+} -start
+
+client c1 -connect ${h1_fe_sock} {
+       txreq -url "/"
+       rxresp
+       expect resp.status == 200
+       expect resp.http.mss-ok == "1400"
+       expect resp.http.mss-len2 == "0"
+       expect resp.http.ws-ok == "7"
+       expect resp.http.ws-len2 == "0"
+       expect resp.http.tsval-len2 == "0"
+} -run
index 4cfa457cbc1f98179c1028c1886e0d6f90b72b00..6a0c7d7d311b3a111bc733682c90bba70fbaed6f 100644 (file)
@@ -499,6 +499,10 @@ static int sample_conv_tcp_options_mss(const struct arg *arg_p, struct sample *s
        if (!len)
                return 0;
 
+       /* the MSS option is always 4 bytes long */
+       if (ofs && (uchar)smp->data.u.str.area[ofs + 1] != 4)
+               ofs = 0;
+
        smp->data.u.sint = ofs ? read_n16(smp->data.u.str.area + ofs + 2) : 0;
        smp->data.type = SMP_T_SINT;
        smp->flags &= ~SMP_F_CONST;
@@ -551,6 +555,10 @@ static int sample_conv_tcp_options_tsval(const struct arg *arg_p, struct sample
        if (!len)
                return 0;
 
+       /* the TimeStamp option is always 10 bytes long */
+       if (ofs && (uchar)smp->data.u.str.area[ofs + 1] != 10)
+               ofs = 0;
+
        smp->data.u.sint = ofs ? read_n32(smp->data.u.str.area + ofs + 2) : 0;
        smp->data.type = SMP_T_SINT;
        smp->flags &= ~SMP_F_CONST;
@@ -569,6 +577,10 @@ static int sample_conv_tcp_options_wscale(const struct arg *arg_p, struct sample
        if (!len)
                return 0;
 
+       /* the Window Scale option is always 3 bytes long */
+       if (ofs && (uchar)smp->data.u.str.area[ofs + 1] != 3)
+               ofs = 0;
+
        smp->data.u.sint = ofs ? (uchar)smp->data.u.str.area[ofs + 2] : 0;
        smp->data.type = SMP_T_SINT;
        smp->flags &= ~SMP_F_CONST;