From: Martin Mirchev Date: Wed, 18 Nov 2026 03:25:19 +0000 (+0800) Subject: BUG/MINOR: net_helper: check the TCP option length before reading its value X-Git-Url: https://git.kaiwu.me/http/%7B@url%7D?a=commitdiff_plain;h=493ad9d11104a7c3650cf434ac8dffa7cf5177c8;p=haproxy.git BUG/MINOR: net_helper: check the TCP option length before reading its value 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. --- diff --git a/reg-tests/converter/net_helper.vtc b/reg-tests/converter/net_helper.vtc new file mode 100644 index 000000000..85d6450cb --- /dev/null +++ b/reg-tests/converter/net_helper.vtc @@ -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 diff --git a/src/net_helper.c b/src/net_helper.c index 4cfa457cb..6a0c7d7d3 100644 --- a/src/net_helper.c +++ b/src/net_helper.c @@ -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;