summaryrefslogtreecommitdiff
path: root/src/script_lua.c
diff options
context:
space:
mode:
authorOran Agra <oran@redislabs.com>2023-01-04 11:03:55 +0200
committerGitHub <noreply@github.com>2023-01-04 11:03:55 +0200
commitc8052122a2af9b85124a8697488a0c44d66777b8 (patch)
treeaae6b19a45a67a7b5f974e182b799d556d8202fc /src/script_lua.c
parent0ecf6cdc0a81f79e6875191ef9aa4f16c9110223 (diff)
downloadredis-c8052122a2af9b85124a8697488a0c44d66777b8.tar.gz
Fix potential issue with Lua argv caching, module command filter and libc realloc (#11652)
TLDR: solve a problem introduced in Redis 7.0.6 (#11541) with RM_CommandFilterArgInsert being called from scripts, which can lead to memory corruption. Libc realloc can return the same pointer even if the size was changed. The code in freeLuaRedisArgv had an assumption that if the pointer didn't change, then the allocation didn't change, and the cache can still be reused. However, if rewriteClientCommandArgument or RM_CommandFilterArgInsert were used, it could be that we realloced the argv array, and the pointer didn't change, then a consecutive command being executed from Lua can use that argv cache reaching beyond its size. This was actually only possible with modules, since the decision to realloc was based on argc, rather than argv_len.
Diffstat (limited to 'src/script_lua.c')
-rw-r--r--src/script_lua.c22
1 files changed, 11 insertions, 11 deletions
diff --git a/src/script_lua.c b/src/script_lua.c
index c86cdc2d1..365a41fb5 100644
--- a/src/script_lua.c
+++ b/src/script_lua.c
@@ -783,7 +783,7 @@ static void luaReplyToRedisReply(client *c, client* script_client, lua_State *lu
/* ---------------------------------------------------------------------------
* Lua redis.* functions implementations.
* ------------------------------------------------------------------------- */
-void freeLuaRedisArgv(robj **argv, int argc);
+void freeLuaRedisArgv(robj **argv, int argc, int argv_len);
/* Cached argv array across calls. */
static robj **lua_argv = NULL;
@@ -795,7 +795,7 @@ static int lua_argv_size = 0;
static robj *lua_args_cached_objects[LUA_CMD_OBJCACHE_SIZE];
static size_t lua_args_cached_objects_len[LUA_CMD_OBJCACHE_SIZE];
-static robj **luaArgsToRedisArgv(lua_State *lua, int *argc) {
+static robj **luaArgsToRedisArgv(lua_State *lua, int *argc, int *argv_len) {
int j;
/* Require at least one argument */
*argc = lua_gettop(lua);
@@ -809,6 +809,7 @@ static robj **luaArgsToRedisArgv(lua_State *lua, int *argc) {
lua_argv = zrealloc(lua_argv,sizeof(robj*)* *argc);
lua_argv_size = *argc;
}
+ *argv_len = lua_argv_size;
for (j = 0; j < *argc; j++) {
char *obj_s;
@@ -848,7 +849,7 @@ static robj **luaArgsToRedisArgv(lua_State *lua, int *argc) {
* is not a string or an integer (lua_isstring() return true for
* integers as well). */
if (j != *argc) {
- freeLuaRedisArgv(lua_argv, j);
+ freeLuaRedisArgv(lua_argv, j, lua_argv_size);
luaPushError(lua, "Lua redis lib command arguments must be strings or integers");
return NULL;
}
@@ -856,7 +857,7 @@ static robj **luaArgsToRedisArgv(lua_State *lua, int *argc) {
return lua_argv;
}
-void freeLuaRedisArgv(robj **argv, int argc) {
+void freeLuaRedisArgv(robj **argv, int argc, int argv_len) {
int j;
for (j = 0; j < argc; j++) {
robj *o = argv[j];
@@ -878,7 +879,7 @@ void freeLuaRedisArgv(robj **argv, int argc) {
decrRefCount(o);
}
}
- if (argv != lua_argv) {
+ if (argv != lua_argv || argv_len != lua_argv_size) {
/* The command changed argv, scrap the cache and start over. */
zfree(argv);
lua_argv = NULL;
@@ -894,8 +895,7 @@ static int luaRedisGenericCommand(lua_State *lua, int raise_error) {
client* c = rctx->c;
sds reply;
- c->argv = luaArgsToRedisArgv(lua, &c->argc);
- c->argv_len = lua_argv_size;
+ c->argv = luaArgsToRedisArgv(lua, &c->argc, &c->argv_len);
if (c->argv == NULL) {
return raise_error ? luaError(lua) : 1;
}
@@ -977,7 +977,7 @@ static int luaRedisGenericCommand(lua_State *lua, int raise_error) {
cleanup:
/* Clean up. Command code may have changed argv/argc so we use the
* argv/argc of the client instead of the local variables. */
- freeLuaRedisArgv(c->argv, c->argc);
+ freeLuaRedisArgv(c->argv, c->argc, c->argv_len);
c->argc = c->argv_len = 0;
c->user = NULL;
c->argv = NULL;
@@ -1128,8 +1128,8 @@ static int luaRedisAclCheckCmdPermissionsCommand(lua_State *lua) {
serverAssert(rctx); /* Only supported inside script invocation */
int raise_error = 0;
- int argc;
- robj **argv = luaArgsToRedisArgv(lua, &argc);
+ int argc, argv_len;
+ robj **argv = luaArgsToRedisArgv(lua, &argc, &argv_len);
/* Require at least one argument */
if (argv == NULL) return luaError(lua);
@@ -1148,7 +1148,7 @@ static int luaRedisAclCheckCmdPermissionsCommand(lua_State *lua) {
}
}
- freeLuaRedisArgv(argv, argc);
+ freeLuaRedisArgv(argv, argc, argv_len);
if (raise_error)
return luaError(lua);
else