Security #9051
closedlua: init() return table with a non-string/number key crashes rule/output load (lua_next misuse)
0318ef942315135aa6293b6737642a0c465db0a6
Description
- Status: present in
main(9b7938aec); confirmed with ASAN - Class: engine crash (SEGV) during rule/output load from a malformed script
- Component:
src/detect-lua.c(DetectLuaSetupPrime),src/output-lua.c(LuaScriptInit)
Description¶
Both parsers iterate the table returned by a script's init() function with the same stack bug:
src/detect-lua.c:594-597 and src/output-lua.c:494-498
lua_pushnil(luastate);
while (lua_next(luastate, -2)) {
k = lua_tostring(luastate, -2);
if (k == NULL)
continue; /* value left on the stack */
...
}
lua_tostring returns NULL for non-string/non-number keys (boolean, function, userdata, thread). The continue skips the lua_pop of the iteration value, so the next lua_next(luastate, -2) reads the wrong stack slot and the Lua table iterator dereferences a non-table as a Table*. The loop runs after the init lua_pcall has returned, i.e. outside any protected call, so this is an unprotected Lua internal SEGV.
Impact: a rule or output script whose init() returns e.g. {[false]=true} or {[function() end]=1} crashes the engine during signature/config load. Script errors must be contained; a malformed script must not segfault the engine. No traffic is required.
ASAN evidence (main \@ 9b7938aec)¶
Rule script function init(args) return { [false] = true } end:
==32799==ERROR: AddressSanitizer: SEGV on unknown address ...
#0 getgeneric
#1 luaH_next
#2 lua_next
#3 DetectLuaSetupPrime src/detect-lua.c:597
#4 DetectLuaSetup src/detect-lua.c:737
Output script
function init() return { [false] = "streaming" } end:==52101==ERROR: AddressSanitizer: SEGV on unknown address ...
#3 LuaScriptInit src/output-lua.c:496
#4 OutputLuaLogInit src/output-lua.c:761
Fix direction¶
Pop the value before continuing, and reject unexpected keys via a normal error path:
Regression tests¶
lua-load-init-bad-key / lua-output-init-bad-key: init returns tables with boolean, function and userdata keys; assert a clean load error and no crash under ASAN.