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

d864f0
Backported for 5.0.3
d864f0
d864f0
d864f0
d864f0
From a4b813d8b844094fcd77c511af596866043b20c8 Mon Sep 17 00:00:00 2001
d864f0
From: "meir@redislabs.com" <meir@redislabs.com>
d864f0
Date: Sun, 13 Jun 2021 14:27:18 +0300
d864f0
Subject: [PATCH] Fix invalid memory write on lua stack overflow
d864f0
 {CVE-2021-32626}
d864f0
MIME-Version: 1.0
d864f0
Content-Type: text/plain; charset=UTF-8
d864f0
Content-Transfer-Encoding: 8bit
d864f0
d864f0
When LUA call our C code, by default, the LUA stack has room for 20
d864f0
elements. In most cases, this is more than enough but sometimes it's not
d864f0
and the caller must verify the LUA stack size before he pushes elements.
d864f0
d864f0
On 3 places in the code, there was no verification of the LUA stack size.
d864f0
On specific inputs this missing verification could have lead to invalid
d864f0
memory write:
d864f0
1. On 'luaReplyToRedisReply', one might return a nested reply that will
d864f0
   explode the LUA stack.
d864f0
2. On 'redisProtocolToLuaType', the Redis reply might be deep enough
d864f0
   to explode the LUA stack (notice that currently there is no such
d864f0
   command in Redis that returns such a nested reply, but modules might
d864f0
   do it)
d864f0
3. On 'ldbRedis', one might give a command with enough arguments to
d864f0
   explode the LUA stack (all the arguments will be pushed to the LUA
d864f0
   stack)
d864f0
d864f0
This commit is solving all those 3 issues by calling 'lua_checkstack' and
d864f0
verify that there is enough room in the LUA stack to push elements. In
d864f0
case 'lua_checkstack' returns an error (there is not enough room in the
d864f0
LUA stack and it's not possible to increase the stack), we will do the
d864f0
following:
d864f0
1. On 'luaReplyToRedisReply', we will return an error to the user.
d864f0
2. On 'redisProtocolToLuaType' we will exit with panic (we assume this
d864f0
   scenario is rare because it can only happen with a module).
d864f0
3. On 'ldbRedis', we return an error.
d864f0
d864f0
(cherry picked from commit d32a3f74f2a343846b50920e95754a955c1a10a9)
d864f0
---
d864f0
 src/scripting.c | 36 ++++++++++++++++++++++++++++++++++++
d864f0
 1 file changed, 36 insertions(+)
d864f0
d864f0
diff --git a/src/scripting.c b/src/scripting.c
d864f0
index db1e4d4b5f1..153b942404e 100644
d864f0
--- a/src/scripting.c
d864f0
+++ b/src/scripting.c
d864f0
@@ -125,6 +125,16 @@ void sha1hex(char *digest, char *script, size_t len) {
d864f0
  */
d864f0
 
d864f0
 char *redisProtocolToLuaType(lua_State *lua, char* reply) {
d864f0
+
d864f0
+    if (!lua_checkstack(lua, 5)) {
d864f0
+        /*
d864f0
+         * Increase the Lua stack if needed, to make sure there is enough room
d864f0
+         * to push 5 elements to the stack. On failure, exit with panic.
d864f0
+         * Notice that we need, in the worst case, 5 elements because redisProtocolToLuaType_Aggregate
d864f0
+         * might push 5 elements to the Lua stack.*/
d864f0
+        serverPanic("lua stack limit reach when parsing redis.call reply");
d864f0
+    }
d864f0
+
d864f0
     char *p = reply;
d864f0
 
d864f0
     switch(*p) {
d864f0
@@ -275,6 +285,17 @@ void luaSortArray(lua_State *lua) {
d864f0
  * ------------------------------------------------------------------------- */
d864f0
 
d864f0
 void luaReplyToRedisReply(client *c, lua_State *lua) {
d864f0
+
d864f0
+    if (!lua_checkstack(lua, 4)) {
d864f0
+        /* Increase the Lua stack if needed to make sure there is enough room
d864f0
+         * to push 4 elements to the stack. On failure, return error.
d864f0
+         * Notice that we need, in the worst case, 4 elements because returning a map might
d864f0
+         * require push 4 elements to the Lua stack.*/
d864f0
+        addReplyErrorFormat(c, "reached lua stack limit");
d864f0
+        lua_pop(lua,1); // pop the element from the stack
d864f0
+        return;
d864f0
+    }
d864f0
+
d864f0
     int t = lua_type(lua,-1);
d864f0
 
d864f0
     switch(t) {
d864f0
@@ -292,6 +313,9 @@ void luaReplyToRedisReply(client *c, lua_State *lua) {
d864f0
          * Error are returned as a single element table with 'err' field.
d864f0
          * Status replies are returned as single element table with 'ok'
d864f0
          * field. */
d864f0
+
d864f0
+        /* Handle error reply. */
d864f0
+        /* we took care of the stack size on function start */
d864f0
         lua_pushstring(lua,"err");
d864f0
         lua_gettable(lua,-2);
d864f0
         t = lua_type(lua,-1);
d864f0
@@ -320,6 +344,7 @@ void luaReplyToRedisReply(client *c, lua_State *lua) {
d864f0
 
d864f0
             lua_pop(lua,1); /* Discard the 'ok' field value we popped */
d864f0
             while(1) {
d864f0
+                /* we took care of the stack size on function start */
d864f0
                 lua_pushnumber(lua,j++);
d864f0
                 lua_gettable(lua,-2);
d864f0
                 t = lua_type(lua,-1);
d864f0
@@ -2231,6 +2256,17 @@ void ldbEval(lua_State *lua, sds *argv, int argc) {
d864f0
 void ldbRedis(lua_State *lua, sds *argv, int argc) {
d864f0
     int j, saved_rc = server.lua_replicate_commands;
d864f0
 
d864f0
+    if (!lua_checkstack(lua, argc + 1)) {
d864f0
+        /* Increase the Lua stack if needed to make sure there is enough room
d864f0
+         * to push 'argc + 1' elements to the stack. On failure, return error.
d864f0
+         * Notice that we need, in worst case, 'argc + 1' elements because we push all the arguments
d864f0
+         * given by the user (without the first argument) and we also push the 'redis' global table and
d864f0
+         * 'redis.call' function so:
d864f0
+         * (1 (redis table)) + (1 (redis.call function)) + (argc - 1 (all arguments without the first)) = argc + 1*/
d864f0
+        ldbLogRedisReply("max lua stack reached");
d864f0
+        return;
d864f0
+    }
d864f0
+
d864f0
     lua_getglobal(lua,"redis");
d864f0
     lua_pushstring(lua,"call");
d864f0
     lua_gettable(lua,-2);       /* Stack: redis, redis.call */