Blame SOURCES/redis-CVE-2021-32687.patch

499bc1
Backported for 5.0.3
499bc1
499bc1
499bc1
499bc1
From c043ba77cf9bbf73e964fd9b8681c0cc4bd2662e Mon Sep 17 00:00:00 2001
499bc1
From: Oran Agra <oran@redislabs.com>
499bc1
Date: Sun, 26 Sep 2021 15:42:17 +0300
499bc1
Subject: [PATCH] Fix Integer overflow issue with intsets (CVE-2021-32687)
499bc1
499bc1
The vulnerability involves changing the default set-max-intset-entries
499bc1
configuration parameter to a very large value and constructing specially
499bc1
crafted commands to manipulate sets
499bc1
499bc1
(cherry picked from commit 4cb7075edaaf0584c74eb080d838ca8f56c190e3)
499bc1
---
499bc1
 src/intset.c | 4 +++-
499bc1
 src/rdb.c    | 4 +++-
499bc1
 src/t_set.c  | 5 ++++-
499bc1
 3 files changed, 10 insertions(+), 3 deletions(-)
499bc1
499bc1
diff --git a/src/intset.c b/src/intset.c
499bc1
index 4445a5ca6c56..288e19adff18 100644
499bc1
--- a/src/intset.c
499bc1
+++ b/src/intset.c
499bc1
@@ -34,6 +34,7 @@
499bc1
 #include "intset.h"
499bc1
 #include "zmalloc.h"
499bc1
 #include "endianconv.h"
499bc1
+#include "redisassert.h"
499bc1
 
499bc1
 /* Note that these encodings are ordered, so:
499bc1
  * INTSET_ENC_INT16 < INTSET_ENC_INT32 < INTSET_ENC_INT64. */
499bc1
@@ -103,7 +104,8 @@ intset *intsetNew(void) {
499bc1
 
499bc1
 /* Resize the intset */
499bc1
 static intset *intsetResize(intset *is, uint32_t len) {
499bc1
-    uint32_t size = len*intrev32ifbe(is->encoding);
499bc1
+    uint64_t size = (uint64_t)len*intrev32ifbe(is->encoding);
499bc1
+    assert(size <= SIZE_MAX - sizeof(intset));
499bc1
     is = zrealloc(is,sizeof(intset)+size);
499bc1
     return is;
499bc1
 }
499bc1
diff --git a/src/rdb.c b/src/rdb.c
499bc1
index afbbd8ca450c..3c58a1eaf7fb 100644
499bc1
--- a/src/rdb.c
499bc1
+++ b/src/rdb.c
499bc1
@@ -1411,7 +1411,9 @@ robj *rdbLoadObject(int rdbtype, rio *rdb, robj *key) {
499bc1
         if ((len = rdbLoadLen(rdb,NULL)) == RDB_LENERR) return NULL;
499bc1
 
499bc1
         /* Use a regular set when there are too many entries. */
499bc1
-        if (len > server.set_max_intset_entries) {
499bc1
+        size_t max_entries = server.set_max_intset_entries;
499bc1
+        if (max_entries >= 1<<30) max_entries = 1<<30;
499bc1
+        if (len > max_entries) {
499bc1
             o = createSetObject();
499bc1
             /* It's faster to expand the dict to the right size asap in order
499bc1
              * to avoid rehashing */
499bc1
diff --git a/src/t_set.c b/src/t_set.c
499bc1
index f67073fe6bb1..db5a8cb757bb 100644
499bc1
--- a/src/t_set.c
499bc1
+++ b/src/t_set.c
499bc1
@@ -66,7 +66,10 @@ int setTypeAdd(robj *subject, sds value) {
499bc1
             if (success) {
499bc1
                 /* Convert to regular set when the intset contains
499bc1
                  * too many entries. */
499bc1
-                if (intsetLen(subject->ptr) > server.set_max_intset_entries)
499bc1
+                size_t max_entries = server.set_max_intset_entries;
499bc1
+                /* limit to 1G entries due to intset internals. */
499bc1
+                if (max_entries >= 1<<30) max_entries = 1<<30;
499bc1
+                if (intsetLen(subject->ptr) > max_entries)
499bc1
                     setTypeConvert(subject,OBJ_ENCODING_HT);
499bc1
                 return 1;
499bc1
             }