BUG/MEDIUM: pattern: Add a trailing \0 to match strings only if possible
authorChristopher Faulet <cfaulet@haproxy.com>
Tue, 30 Jun 2020 16:52:32 +0000 (18:52 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Fri, 3 Jul 2020 17:03:55 +0000 (19:03 +0200)
commit37790a22a7d1ff27f016bedfd5dfabf48f0726b0
tree9abb09eb22f59b8708ca9f23706a428e3815386f
parent608e6e7f65ddd2d00960e34888fd37cd4c214049
BUG/MEDIUM: pattern: Add a trailing \0 to match strings only if possible

In pat_match_str() and pat_math_beg() functions, a trailing zero is
systematically added at the end of the string, even if the buffer is not large
enough to accommodate it. It is a possible buffer overflow. For instance, when
the alpn is matched against a list of strings, the sample fetch is filled with a
non-null terminated string returned by the SSL library. No trailing zero must be
added at the end of this string, because it is outside the buffer.

So, to fix the bug, a trailing zero is added only if the buffer is large enough
to accommodate it. Otherwise, the sample fetch is duplicated. smp_dup() function
adds a trailing zero to the duplicated string, truncating it if it is too long.

This patch should fix the issue #718. It must be backported to all supported
versions.

(cherry picked from commit b4cf7ab9bc413bbb956e225f903959bff17e4049)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
src/pattern.c