Security #9050
closedlua: flowint/flowvar register and bytevar map NULL-deref when DetectLuaData is unset (output/transform scripts)
Description
- Status: present in
main(9b7938aec); confirmed with ASAN - Class: NULL pointer dereference (engine crash) from output/transform Lua scripts
- Component:
src/util-lua-flowintlib.c,src/util-lua-flowvarlib.c,src/util-lua-bytevarlib.c
Description¶
LuaFlowintRegister() (src/util-lua-flowintlib.c:52), LuaFlowvarRegister() (src/util-lua-flowvarlib.c:52) and LuaBytevarMap() (src/util-lua-bytevarlib.c:46) call GetLuaData() and dereference the result without a NULL check:
DetectLuaData *ld = GetLuaData(L);
if (ld->flowints >= DETECT_LUA_MAX_FLOWINTS) { ... } /* flowintlib.c:59 */
GetLuaData() reads the luaext_key_ld registry entry, which is only set by the rule keyword path (LuaStateSetDetectLuaData(), src/detect-lua.c:425). The lua output module creates its state with LuaGetState() = luaL_newstate() + luaL_openlibs() (src/output-lua.c:452-454, 587-589) and never sets the key; src/detect-transform-luaxform.c also never sets it. The builtins are loaded in both cases (SCLuaRequirefBuiltIns), so a script can require("suricata.flowint") and call register().
LuaBytevarGet() checks ld for NULL (util-lua-bytevarlib.c:77) - the register/map paths were simply missed.
ASAN evidence (main \@ 9b7938aec)¶
Output script init() calling require("suricata.flowint").register("x"):
==...==ERROR: AddressSanitizer: SEGV on unknown address ... (pc ... bp ... sp ...)
#0 LuaFlowintRegister src/util-lua-flowintlib.c:59
...
#7 LuaScriptInit src/output-lua.c:479
#8 OutputLuaLogInit src/output-lua.c:761
grep LuaStateSetDetectLuaData = 0 occurrences in src/output-lua.c and src/detect-transform-luaxform.c, so luaxform scripts reach the same crash.
Fix direction¶
NULL-check in all three entry points and raise a Lua error (matching LuaBytevarGet):
DetectLuaData *ld = GetLuaData(L);
if (ld == NULL)
return luaL_error(L, "flowvar/flowint/bytevar API not available in this context");
Alternatively document and enforce that these APIs are rule-only. Regression tests: lua-bytevar-no-detdata (flowint/flowvar/bytevar calls from output and transform scripts) asserting a Lua error and no crash under ASAN.