Skip to content

Fix potential issue with Lua argv caching, module command filter and libc realloc#11652

Merged
oranagra merged 2 commits intoredis:unstablefrom
oranagra:fix_modules_filter_lua_argv_cache
Jan 4, 2023
Merged

Fix potential issue with Lua argv caching, module command filter and libc realloc#11652
oranagra merged 2 commits intoredis:unstablefrom
oranagra:fix_modules_filter_lua_argv_cache

Conversation

@oranagra
Copy link
Member

@oranagra oranagra commented Dec 22, 2022

TLDR: solve a problem introduce 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.

…libc realloc

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 allocatin 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.
@oranagra oranagra requested a review from yossigo December 22, 2022 11:39
@oranagra oranagra merged commit c805212 into redis:unstable Jan 4, 2023
@oranagra oranagra deleted the fix_modules_filter_lua_argv_cache branch January 4, 2023 09:04
oranagra added a commit that referenced this pull request Jan 16, 2023
…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.

(cherry picked from commit c805212)
enjoy-binbin pushed a commit to enjoy-binbin/redis that referenced this pull request Jul 31, 2023
…libc realloc (redis#11652)

TLDR: solve a problem introduced in Redis 7.0.6 (redis#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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Unreleased

Development

Successfully merging this pull request may close these issues.

3 participants