Skip to content

Store the length of the static argv when first allocated. - #1945

Closed
paddybyers wants to merge 2 commits into
redis:unstablefrom
paddybyers:script_argv_fix2
Closed

paddybyers wants to merge 2 commits into
redis:unstablefrom
paddybyers:script_argv_fix2

Conversation

@paddybyers

Copy link
Copy Markdown
Contributor

This fixes a bug introduced by this commit:

8237d88

The length of the static argv is not stored when it is first created; only when extended. On the first occasion this is relatively harmless (since the initialised length was 0), but on any subsequent "first" allocation - eg after having been cleared here: https://github.com/antirez/redis/blob/8237d88f48e0843839eb530c408686d66bcae6ab/src/scripting.c#L352

then a new allocated argv implicitly inherits the length of the last freed one. Where the new argv is smaller than the last freed one, and then later reused without extension at any length greater than its actual length, memory is overwritten.

This fixes issue #1939.

See also related issue #1943.

@paddybyers paddybyers mentioned this pull request Aug 15, 2014
@badboy

badboy commented Aug 15, 2014

Copy link
Copy Markdown
Contributor

Is there a simple test case to add as a regression test?

@paddybyers

Copy link
Copy Markdown
Contributor Author

Is there a simple test case to add as a regression test?

It was crashing in my application but I will try to create a standalone example.

Comment thread src/scripting.c

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@paddybyers how about extracting that line before if condition.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, because then it would also apply in the argv_size >= argc case, which would defeat the whole purpose ...

@paddybyers

Copy link
Copy Markdown
Contributor Author

The following sequence of calls triggers the error condition:

-- allocate an initial argv, length 3
redis.call('SET', 'a', '1')
-- realloc to length 4; following this argv_size == 4
redis.call('MGET', 'a', 'b', 'c')
-- command that triggers rewriteClientCommandVector, resulting in argv == 0, still argv_size == 4
redis.call('EXPIRE', 'a', 0)
-- command that allocates new argv of size 2 (no change to argv_size)
redis.call('GET', 'a')
-- command that requires argv to be 4, but causes no reallocation since argv_size == 4 already
redis.call('MGET', 'a', 'b', 'c')
-- at this point memory overwrite has occurred

Calling this sequence in a loop invariably triggers a hard crash for me. I've added a commit to this PR that includes this as a regression test.

@badboy

badboy commented Aug 17, 2014

Copy link
Copy Markdown
Contributor

Great. I can confirm that the test crashes the server without this patch and works fine with it.

@antirez

antirez commented Sep 1, 2014

Copy link
Copy Markdown
Contributor

Thanks! Merged. I'll release 2.8.14 ASAP, I was planning to delay it since it contains many new small changes after merging a big PR, but it is better to fix known bugs like this instead.

@antirez antirez closed this Sep 1, 2014
@antirez

antirez commented Sep 1, 2014

Copy link
Copy Markdown
Contributor

p.s. what about simplifying the code like that?

diff --git a/src/scripting.c b/src/scripting.c
index 50e1ef1..5cd0b29 100644
--- a/src/scripting.c
+++ b/src/scripting.c
@@ -222,10 +222,7 @@ int luaRedisGenericCommand(lua_State *lua, int raise_error)
     }

     /* Build the arguments vector */
-    if (!argv) {
-        argv = zmalloc(sizeof(robj*)*argc);
-        argv_size = argc;
-    } else if (argv_size < argc) {
+    if (argv_size < argc) {
         argv = zrealloc(argv,sizeof(robj*)*argc);
         argv_size = argc;
     }
@@ -403,6 +400,7 @@ cleanup:
     if (c->argv != argv) {
         zfree(c->argv);
         argv = NULL;
+        argv_size = 0;
     }

@antirez antirez reopened this Sep 1, 2014
@antirez

antirez commented Sep 1, 2014

Copy link
Copy Markdown
Contributor

Reopened in order to discuss the possible code change that simplifies the two if branches into one, however the fix and the regression test are already merged.

@paddybyers

Copy link
Copy Markdown
Contributor Author

p.s. what about simplifying the code like that?

without having tested it, lgtm

@paddybyers

Copy link
Copy Markdown
Contributor Author

I've tested this change and it works.

Commit is here for reference: paddybyers@d0e7877

@antirez

antirez commented Sep 10, 2014

Copy link
Copy Markdown
Contributor

Thanks! Change committed. Closing.

@antirez antirez closed this Sep 10, 2014
antirez added a commit that referenced this pull request Sep 10, 2014
antirez added a commit that referenced this pull request Sep 10, 2014
antirez added a commit that referenced this pull request Sep 10, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants