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

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