]> git.kaiwu.me - haproxy.git/commit
BUG/MINOR: chunk: stop chunk_str{,case}cmp() at the end of the string master
authorChristopher Faulet <cfaulet@haproxy.com>
Thu, 3 Dec 2026 21:02:10 +0000 (22:02 +0100)
committerChristopher Faulet <cfaulet@haproxy.com>
Fri, 4 Dec 2026 13:54:03 +0000 (14:54 +0100)
commit8844e24995df18e86320eaf8e81674e66495ef42
treef95e150415c88180858b38179e63e6f379cdec18
parent1d5b9dfafd34aeb2cb2d1866a22cdf9bc3956a34
BUG/MINOR: chunk: stop chunk_str{,case}cmp() at the end of the string

chunk_strcmp() and chunk_strcasecmp() compare a chunk with a NUL-terminated
string, and only stop on the first difference or at the end of the chunk.
When the chunk contains a NUL byte at the position where the string ends,
both are equal there, so the comparison continues past the end of the
string, reading memory located after it. In addition, the result may be
wrong: "oct\0" may be reported as equal to "oct" if the byte following the
string is zero.

Most callers compare configuration elements, but jwt_decrypt_jwk() does it
on the "kty" field of the JWK, which may come from a variable, and the JSON
decoder turns "\u0000" into a NUL byte. With an ASAN build, this config:

    http-request set-var(txn.jwk) req.fhdr(x-jwk)
    http-request return status 200 hdr x-res "%[req.hdr(x-tok),jwt_decrypt_jwk(txn.jwk)]"

with a request carrying 'x-jwk: {"kty":"oct\u0000","k":"..."}' and any
token using "alg":"dir" reports a global-buffer-overflow in chunk_strcmp()
past the "oct" literal. Without ASAN, the read hits the next constants and
has no other effect than possibly accepting this "kty", and taking the JWK
from client data makes no sense anyway, so the impact is very low.

Let's stop the comparison when reaching the end of the string, and report
the chunk as greater in this case since it is longer.

This exists since the functions were introduced in 1.5 by commit ad8f8e8ff
("MINOR: chunk: provide string compare functions"). It may be backported
to all supported versions.
src/chunk.c