Skip to content

Fix integer overflow in processBulkItem bulk length math - #1378

Open
Arawoof06 wants to merge 2 commits into
redis:masterfrom
Arawoof06:bulk-length-size-overflow
Open

Arawoof06 wants to merge 2 commits into
redis:masterfrom
Arawoof06:bulk-length-size-overflow

Conversation

@Arawoof06

@Arawoof06 Arawoof06 commented Oct 8, 2026 •

Copy link
Copy Markdown

processBulkItem parses the bulk string length into a long long but folds it into an unsigned long with bytelen += len+2, so a reply whose length is near LLONG_MAX overflows the signed add, and on LLP64/32-bit builds (where unsigned long is narrower than size_t) bytelen truncates, letting the completeness check pass for bytes that were never received before createString reads len bytes past the buffer. Switching bytelen to size_t and comparing len against the bytes left after the length line keeps the arithmetic in range on every platform. UBSan flags the signed overflow at read.c:490 on the 25-byte reply $9223372036854775807\r\nX\r\n before this change and is clean after, and the existing reply-reader tests still pass.


Note

High Risk
Changes untrusted reply parsing in hiredis; incorrect bounds math could read past the input buffer, though this PR specifically closes that class of bug.

Overview
Fixes integer overflow and truncation when processBulkItem decides whether a RESP bulk string is fully buffered.

read.c: bytelen is now size_t instead of unsigned long. Instead of bytelen += len + 2 and comparing r->pos + bytelen to r->len (which could wrap or truncate for huge declared lengths), the parser computes remaining bytes after the $length\r\n line and only proceeds when (size_t)len <= remaining - 2, then extends bytelen safely.

test.c: Adds a regression test that feeds a bulk header at SIZE_MAX/LLONG_MAX with only one payload byte and expects REDIS_OK with no reply (incomplete bulk), not a mistaken “complete” parse.

Reviewed by Cursor Bugbot for commit 4736b2c. Bugbot is set up for automated code reviews on this repo. Configure here.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Credits must be used to enable repository wide code reviews.

@shogo82148

Copy link
Copy Markdown

I can confirm that this also affects 32-bit Linux, which is a supported platform, not only LLP64/Windows.
(#1333 says pure 32-bit builds are protected by the existing len > SIZE_MAX check, but len == SIZE_MAX and values close to it pass that check.)

On i686, $4294967295\r\nX makes bytelen += len+2 wrap around to a small value, so the incomplete bulk is treated as complete.
createStringObject() then calls hi_malloc(len+1) (= hi_malloc(0)) and memcpys SIZE_MAX bytes into it.

Reproduced in Docker (--platform linux/386, i386/debian:bookworm-slim) with the default reader:

#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "hiredis.h"

int main(void) {
    char msg[64];
    void *reply = NULL;
    redisReader *r = redisReaderCreate();
    snprintf(msg, sizeof msg, "$%llu\r\nX", (unsigned long long)SIZE_MAX);
    redisReaderFeed(r, msg, strlen(msg));
    int ret = redisReaderGetReply(r, &reply);
    printf("ret=%d reply=%p\n", ret, reply);
    redisReaderFree(r);
    return 0;
}
  • v1.5.0 (5bb7f03): segmentation fault. With ASan:
    ERROR: AddressSanitizer: negative-size-param: (size=-1)
        #0 in __interceptor_memcpy
        #1 in createStringObject hiredis.c:154
        #2 in processBulkItem read.c:501
        #3 in processItem read.c:723
        #4 in redisReaderGetReply read.c:846
    
  • With the same approach as this PR (size_t bytelen and comparing len against the remaining buffer): ret=0 reply=(nil), i.e. the reader correctly waits for more data, and all reader tests pass on i686.

This PR doesn't add a test, so here is one that fails (segfaults) on 32-bit before the fix and passes after it.
It runs on every platform (on 64-bit it uses LLONG_MAX, the largest length accepted there):

    test("Bulk length near SIZE_MAX does not wrap around: ");
    {
        unsigned long long huge = (unsigned long long)SIZE_MAX < (unsigned long long)LLONG_MAX ?
            (unsigned long long)SIZE_MAX : (unsigned long long)LLONG_MAX;
        char huge_bulk_reply[100];
        snprintf(huge_bulk_reply, sizeof(huge_bulk_reply), "$%llu\r\nX", huge);
        reader = redisReaderCreate();
        redisReaderFeed(reader, huge_bulk_reply, strlen(huge_bulk_reply));
        ret = redisReaderGetReply(reader,&reply);
        /* The bulk string is incomplete, so no reply must be returned yet. */
        test_cond(ret == REDIS_OK && reply == NULL);
        freeReplyObject(reply);
        redisReaderFree(reader);
    }

Feel free to pick it up into this PR. I can also open a separate PR with it if that's easier.

For context, I found this while auditing Redis::Fast, a Perl client that bundles hiredis.
The investigation, the reproduction and the test above were done with the help of Claude Code.

@Arawoof06

Copy link
Copy Markdown
Author

Good catch on the 32-bit Linux case. I'd only reasoned about the len > SIZE_MAX guard on LLP64, but you're right that len == SIZE_MAX gets through it and wraps bytelen on i686 too. I've pulled your test in as-is (4736b2c). On 64-bit it doesn't crash without the fix, but under UBSan the unpatched reader aborts on it at the len+2 add, and it passes with the patch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants