From b31d6112ce52352feb2ea274011757d4ff0e67e3 Mon Sep 17 00:00:00 2001 From: Kunwar Atul Date: Tue, 27 Oct 2026 09:29:21 +0530 Subject: [PATCH] jose: Fix NULL pointer dereference and OOB read in compact JWE decoding apr_jose_jwe_make() dereferenced its payload argument unconditionally (jose->cty = payload->cty) even though payload is documented and declared (via __attribute__((nonnull(6))), which excludes the payload parameter) as optional. apr_jose_decode_compact_jwe() always calls it with payload == NULL, since no payload exists yet at that point in decryption. This means every call to apr_jose_decode() on a JWE in Compact Serialization crashes with a NULL pointer dereference, for any well-formed input, not just malformed input. The sibling constructors (apr_jose_jws_make, apr_jose_jwe_json_make, apr_jose_jws_json_make) all correctly guard the same line with `if (payload)`; apr_jose_jwe_make was the only one missing it. Separately, in apr_jose_decode_compact_jwe(), the JWE tag segment's length was computed as `dot - left`, where `dot` is the delimiter found for the *previous* (ciphertext) segment and `left` was just set to `dot + 1` on the line above -- so this expression is always exactly -1. Passed as the signed slen argument to apr_pdecode_base64_binary(), -1 is APR_ENCODE_STRING, which tells the decoder to scan for a NUL terminator instead of respecting a length, so it can read past the end of the real input buffer. The tag is the last segment in Compact Serialization (no trailing delimiter), so its length should be measured against the end of the input, `right`, not against the previous segment's delimiter. This was found and confirmed with a standalone reproduction: calling apr_jose_decode() with a real decrypt callback on a well-formed JWE compact string crashed before reaching the callback; after fixing the first bug, the decrypt callback was reached but reported a tag length that read past the intended segment into adjacent memory; after also fixing the second, the reported tag length matched the actual encoded tag exactly. Neither released APR (<= 1.7.6) nor apr-util (<= 1.6.5) ships the jose module; it exists only on unreleased development branches. --- jose/apr_jose.c | 4 +++- jose/apr_jose_decode.c | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/jose/apr_jose.c b/jose/apr_jose.c index 4c4bd6b6cb..f4a50914ec 100644 --- a/jose/apr_jose.c +++ b/jose/apr_jose.c @@ -144,7 +144,9 @@ APR_DECLARE(apr_jose_t *) apr_jose_jwe_make(apr_jose_t *jose, return NULL; } - jose->cty = payload->cty; + if (payload) { + jose->cty = payload->cty; + } jwe = jose->jose.jwe = apr_palloc(pool, sizeof(apr_jose_jwe_t)); if (!jwe) { diff --git a/jose/apr_jose_decode.c b/jose/apr_jose_decode.c index af10e35a48..d37bb615fd 100644 --- a/jose/apr_jose_decode.c +++ b/jose/apr_jose_decode.c @@ -616,7 +616,7 @@ apr_status_t apr_jose_decode_compact_jwe(apr_jose_t **jose, const char *left, /* tag */ jwe->encryption->tag.data = apr_pdecode_base64_binary(pool, left, - dot - left, APR_ENCODE_BASE64URL, &jwe->encryption->tag.len); + right - left, APR_ENCODE_BASE64URL, &jwe->encryption->tag.len); if (!jwe->encryption->tag.data) { apr_errprintf(&(*jose)->result, pool, NULL, 0, "Syntax error: JWE tag base64url decoding failed at %" APR_SIZE_T_FMT "",