An attacker who can run scripting commands can free the Lua interpreter state out from under the script debugger's cached command table, leaving a dangling pointer that is dereferenced on the next debugger command. This yields a reliable crash of the server process, and can be escalated to control of an indirect call by reclaiming the freed heap chunk with attacker-controlled bytes.
This issue affects the 9.x line. The defect was introduced when the Lua scripting engine was moved into a Valkey module. The 8.0.x and 8.1.x lines use the previous non-module debugger and are not affected.
ldbGenerateDebuggerCommandsArray in src/modules/lua/debug_lua.c builds
the interactive debugger command table once and caches it in a file-scope
static, binding the then-current lua_State pointer into each command
descriptor's context field. The table is never rebuilt or invalidated.
SCRIPT FLUSH ASYNC recreates the scripting engine, creating the
replacement interpreter state before freeing the previous one on the
lazyfree thread. Nothing clears the cached command table, so its context
is left pointing at a freed chunk. Any subsequent debugger command
dereferences it.
Because the freed chunk lives on the ordinary allocator heap, it can be reclaimed with attacker-controlled bytes and shaped into a counterfeit interpreter state, at which point the next allocation performed by the interpreter calls the allocator function pointer stored inside that state.
A second use-after-free in ldbLogStackValue is fixed in the same change:
the logged module string was freed while the debugger log list still held
a reference to it.
You can mitigate this issue by using Valkey ACLs to restrict the SCRIPT
command, in particular SCRIPT DEBUG, if your application doesn't intend
to use the script debugger.
The defect was independently reported by Trung Nguyen (@everping) of CyStack, by Phuc Nguyen Dinh (pucagit) of CyStack, and Han Sang-Kyung (toky1219@gmail.com).
The fix was authored by https://github.com/Baraa-Hasheesh.