Repository navigation
Conversation
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
|
I can confirm that this also affects 32-bit Linux, which is a supported platform, not only LLP64/Windows. On i686, Reproduced in Docker ( #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;
}
This PR doesn't add a test, so here is one that fails (segfaults) on 32-bit before the fix and passes after it. 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. |
|
Good catch on the 32-bit Linux case. I'd only reasoned about the |
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\nbefore 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
processBulkItemdecides whether a RESP bulk string is fully buffered.read.c:bytelenis nowsize_tinstead ofunsigned long. Instead ofbytelen += len + 2and comparingr->pos + bytelentor->len(which could wrap or truncate for huge declared lengths), the parser computesremainingbytes after the$length\r\nline and only proceeds when(size_t)len <= remaining - 2, then extendsbytelensafely.test.c: Adds a regression test that feeds a bulk header at SIZE_MAX/LLONG_MAX with only one payload byte and expectsREDIS_OKwith 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.