BUG/MAJOR: htx: fix missing header name length check in htx_add_header/trailer
authorWilly Tarreau <w@1wt.eu>
Thu, 26 Aug 2021 14:23:37 +0000 (16:23 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 3 Sep 2021 15:19:24 +0000 (17:19 +0200)
commitac19a3112534668258fb6d1b5e77cb9acc9e348f
tree586286e1aa87b317f09b2eff0b0df920be9404bd
parent9ebc9cceb67c978d6ffa5e5065de4ef9482b9e1a
BUG/MAJOR: htx: fix missing header name length check in htx_add_header/trailer

Ori Hollander of JFrog Security reported that htx_add_header() and
htx_add_trailer() were missing a length check on the header name. While
this does not allow to overwrite any memory area, it results in bits of
the header name length to slip into the header value length and may
result in forging certain header names on the input. The sad thing here
is that a FIXME comment was present suggesting to add the required length
checks :-(

The injected headers are visible to the HTTP internals and to the config
rules, so haproxy will generally stay synchronized with the server. But
there is one exception which is the content-length header field, because
it is already deduplicated on the input, but before being indexed. As
such, injecting a content-length header after the deduplication stage
may be abused to present a different, shorter one on the other side and
help build a request smuggling attack, or even maybe a response splitting
attack. CVE-2021-40346 was assigned to this problem.

As a mitigation measure, it is sufficient to verify that no more than
one such header is present in any message, which is normally the case
thanks to the duplicate checks:

   http-request  deny if { req.hdr_cnt(content-length) gt 1 }
   http-response deny if { res.hdr_cnt(content-length) gt 1 }

This must be backported to all HTX-enabled versions, hence as far as 2.0.
In 2.3 and earlier, the functions are in src/htx.c instead.

Many thanks to Ori for his work and his responsible report!

(cherry picked from commit 3b69886f7dcc3cfb3d166309018e6cfec9ce2c95)
Signed-off-by: Willy Tarreau <w@1wt.eu>
(cherry picked from commit 1fd2566683f6fb66b180ce9c3d9062ddaa81d6d7)
[wt: code is in src/htx.c in 2.3 and older]
Signed-off-by: Willy Tarreau <w@1wt.eu>
src/htx.c