]> git.kaiwu.me - haproxy.git/commitdiff
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)
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

index 99ab7beeb81c2f731ef3d0ec5f8368aa45b59646..c9852075a363b4a349038bc2f25efb7e63455288 100644 (file)
@@ -452,7 +452,8 @@ int chunk_asciiencode(struct buffer *dst, struct buffer *src, char qc)
 
 /* Compares the string in chunk <chk> with the string in <str> 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 <str> ends is longer than
+ * <str>, 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) {
+                       /* <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 <chk> with the string in
  * <str> 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 <str>
+ * ends is longer than <str>, 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) {
+                       /* <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;