#1358·hiredis

[Bug]Signed integer overflow in processBulkItem bytelen += len+2 on a RESP bulk length of LLONG_MAX (CWE-190)

Author: 1820893135-pixelCreated Aug 28, 2026Updated Aug 31, 2026

Summary

The RESP bulk-length parser computes bytelen += len+2 (read.c:490) where len is the attacker-controlled bulk length parsed from the input. When len is LLONG_MAX (9223372036854775807), len+2 overflows a signed 64-bit long long — undefined behavior in C. Under a UBSan build (-fno-sanitize-recover=all) the inserted signed-overflow check traps as ud2/SIGILL and the process dies. The same pattern (len+1 into an allocation) is exactly the class that produced CVE-2021-32765 (integer overflow → heap buffer overflow in redisFormatCommandArgv), so this spot deserves a defensive length check even though the current code path does not reach a memory write.

  • Affected versions: hiredis master 29ea279 (post-v1.5.0)
  • Severity: Medium (UBSan-confirmed undefined behavior; deterministic SIGILL in sanitizer builds; state corruption / protocol hang in release builds)
  • CWE: CWE-190 (Integer Overflow or Wraparound)

Detail

Affected code

src/read.c processBulkItem (excerpt, lines 485–491):

c
} else {
    /* Only continue when the buffer contains the entire bulk item. */
    bytelen += len+2; /* include \r\n */          /* read.c:490  <-- overflow */
    if (r->pos+bytelen <= r->len) {
        ...
    }
}

bytelen is declared unsigned long bytelen; (read.c:460), but len is a long long parsed from the input via string2ll:

c
unsigned long bytelen;
...
long long len;
...
if (string2ll(p, bytelen - 2, &len) == REDIS_ERR) {   /* read.c:469 */
    ...
}

The expression len+2 is computed in signed long long arithmetic. When len == LLONG_MAX, LLONG_MAX + 2 is a signed overflow, which is undefined behavior. UBSan instruments the add with a jo (jump-on-overflow) check; under -fno-sanitize-recover=all the trap raises SIGILL.

Why the guard at read.c:475 does not catch it: the check len > (long long)(SIZE_MAX - 2) is compiled out on 64-bit platforms (SIZE_MAX - 2 is ~2^64, always greater than any long long), so a len up to LLONG_MAX passes validation and reaches len+2.

Impact: in a sanitizer build this is a deterministic SIGILL (exit 132). In a release build the overflow is silent: bytelen becomes huge, the subsequent r->pos+bytelen <= r->len length check fails, and the parser falls into a protocol-error path — leaving the reader in an inconsistent state that can deadlock/hang the client. The same len+1-into-allocator pattern is precisely what CVE-2021-32765 exploited, so the arithmetic here is a latent memory-corruption risk at the fix boundary.

Call chain:

redisReaderFeed (read.c:789)
  └─ redisReaderGetReply (read.c:820)
       └─ processItem (read.c:723)
            └─ processBulkItem (read.c:455)
                 └─ bytelen += len+2  (read.c:490)   len = LLONG_MAX -> signed overflow -> UBSan trap

Build the parser fuzzer

Same harness/driver as the sibling report ([hiredis issue 1](https://github.com/redis/hiredis)) — clone 29ea279 and compile harness.c + driver.c + the hiredis sources. Note that the signed-integer-overflow trap is only active under a UBSan build with -fno-sanitize-recover=all (the trap instruction is only inserted with that sanitizer and is skipped otherwise):

bash
git clone https://github.com/redis/hiredis.git
cd hiredis && git checkout 29ea279

# save harness.c and driver.c (see sibling report), then build with UBSan:
clang -O1 -g -fsanitize=undefined -fno-sanitize-recover=all -fno-omit-frame-pointer \
    -I . harness.c driver.c read.c hiredis.c sds.c alloc.c net.c async.c sockcompat.c dict.c \
    -o hiredis_reader_fuzzer

POC (tested on the unmodified source)

Save as poc_bulk_len.bin — a single RESP bulk header with length 9223372036854775807 (= LLONG_MAX):

$9223372036854775807

(hex: 24 39 32 32 33 33 37 32 30 33 36 38 35 34 37 37 35 38 30 37 0d 0a — 22 bytes)

bash
ASAN_OPTIONS=detect_leaks=0 ./hiredis_reader_fuzzer poc_bulk_len.bin

Trigger result

Verified on the unmodified source: the input raises SIGILL deterministically.

Program received signal SIGILL, Illegal instruction.
  • Replay exit code: 132 (SIGILL, sig:04 under AFL++)

  • Deterministic: yes — the 22-byte $9223372036854775807\r\n reproduces reliably (2 independent crash files)

  • Disassembly confirmation of the trap site (read.c:490):

    13520c:  48 89 d0          mov    %rdx,%rax      ; rax = len (LLONG_MAX)
    13520f:  48 83 c0 02       add    $0x2,%rax      ; rax = len + 2  <-- signed overflow
           (jo / UBSan handler follows -> SIGILL)

Suggested fix

Validate the bulk length before the addition, or compute in a wide unsigned type:

c
/* read.c processBulkItem: reject a bulk length whose +2 would overflow */
if (len > (long long)(SIZE_MAX - 2)) {
    __redisReaderSetError(r, REDIS_ERR_PROTOCOL, "Bulk length out of range");
    return REDIS_ERR;
}
/* then compute the size in an unsigned domain: */
size_t bulk_len = (size_t)len + 2;
bytelen = (unsigned long)bulk_len;

Because this is the same arithmetic family as CVE-2021-32765, a defensive length cap (e.g. reject any bulk length above a few GB) is recommended even though the current path does not reach a memory write.