From: Christopher Faulet Date: Thu, 3 Dec 2026 21:02:10 +0000 (+0100) Subject: BUG/MINOR: chunk: stop chunk_str{,case}cmp() at the end of the string X-Git-Url: https://git.kaiwu.me/web/http/doc/static//%22?a=commitdiff_plain;h=HEAD;p=haproxy.git 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. --- diff --git a/src/chunk.c b/src/chunk.c index 99ab7beeb..c9852075a 100644 --- a/src/chunk.c +++ b/src/chunk.c @@ -452,7 +452,8 @@ int chunk_asciiencode(struct buffer *dst, struct buffer *src, char qc) /* Compares the string in chunk with the string in which must be * zero-terminated. Return is the same as with strcmp(). Neither is allowed - * to be null. + * to be null. A chunk containing a NUL byte where ends is longer than + * , hence greater. */ int chunk_strcmp(const struct buffer *chk, const char *str) { @@ -465,6 +466,11 @@ int chunk_strcmp(const struct buffer *chk, const char *str) diff = (unsigned char)0 - (unsigned char)*str; break; } + if (!*str) { + /* ends before the chunk */ + diff = (unsigned char)*s1 ? (unsigned char)*s1 : 1; + break; + } diff = (unsigned char)*(s1++) - (unsigned char)*(str++); } while (!diff); return diff; @@ -472,7 +478,8 @@ int chunk_strcmp(const struct buffer *chk, const char *str) /* Case-insensitively compares the string in chunk with the string in * which must be zero-terminated. Return is the same as with strcmp(). - * Neither is allowed to be null. + * Neither is allowed to be null. A chunk containing a NUL byte where + * ends is longer than , hence greater. */ int chunk_strcasecmp(const struct buffer *chk, const char *str) { @@ -485,6 +492,11 @@ int chunk_strcasecmp(const struct buffer *chk, const char *str) diff = (unsigned char)0 - (unsigned char)*str; break; } + if (!*str) { + /* ends before the chunk */ + diff = (unsigned char)*s1 ? (unsigned char)*s1 : 1; + break; + } diff = (unsigned char)*s1 - (unsigned char)*str; if (unlikely(diff)) { unsigned int l = (unsigned char)*s1;