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.
LS Updated by Lukas Sismis 2 months ago
Seems like what my tooling found:
# L4 — flowint/flowvar `register()` NULL-deref from luaxform transform scripts
## Finding
**File:** `src/util-lua-flowintlib.c` (and the identical twin in `src/util-lua-flowvarlib.c`)
```c
static DetectLuaData *GetLuaData(lua_State *luastate) /* lines 32-39 */
{
DetectLuaData *ld;
lua_pushlightuserdata(luastate, (void *)&luaext_key_ld);
lua_gettable(luastate, LUA_REGISTRYINDEX);
ld = lua_touserdata(luastate, -1);
return ld; /* NULL when the registry key was never set */
}
static int LuaFlowintRegister(lua_State *L) /* line 50 */
{
DetectLuaData *ld = GetLuaData(L); /* ld == NULL outside detect-lua rule paths */
...
if (ld->flowints >= DETECT_LUA_MAX_FLOWINTS) { /* line 59: NULL deref */
```
**Bug class:** NULL pointer dereference (CWE-476), remote-triggerable via rule-file
authoring but, more importantly, by any ruleset a Suricata instance is asked to load
together with Lua enabled — the crash is per-packet on the detection path.
## Reachability
The registry key `luaext_key_ld` is only set by `LuaStateSetDetectLuaData()`
(`src/detect-lua.c:161`), called from `DetectLuaRunMatch()` — i.e. only for the
`lua:` *rule keyword*. However, the flowint/flowvar Lua modules are registered as
**builtins** for every sandboxed Lua state (`src/util-lua-builtins.c:53-54`), and the
`luaxform` TRANSFORM (`src/detect-transform-luaxform.c`) creates such sandboxed states
in `DetectLuaxformThreadInit()` (line 202: `SCLuaSbLoadLibs(t->luastate)`), which wires
`require("suricata.flowint")` through `SCLuaSbRequire` (`src/util-lua-sandbox.c:273`)
to `LuaLoadFlowintLib`. The luaxform path **never** sets `luaext_key_ld`.
So a luaxform transform script that calls `flowint.register("name")` (or
`flowvar.register(...)` — same bug, `src/util-lua-flowvarlib.c:35/52`) crashes the
detection thread with SIGSEGV the first time the transform runs on a matching buffer.
This is not catchable by Lua `pcall` — it is a hard C-level fault.
## How it reproduces
`reproducer.sh`:
1. Writes a transform script that does `local fi = require("suricata.flowint")` and
calls `fi.register("boom")` inside `transform()`.
2. Writes a rule: `alert http ... (http.uri; luaxform:transform.lua; content:"EXEC_POST.PHP"; sid:1;)`.
3. Replays `test.pcap` (a plain HTTP `GET /exec_post.php` session, copied from
suricata-verify `tests/lua/lua-transform-01/test.pcap`) through Suricata with
`--set security.lua.allow-rules=true`.
4. Asserts the process dies with SIGSEGV and that the stack trace contains
`LuaFlowintRegister` reached from `TransformLuaxform`.
## Expected signal
Exit code 139 (SIGSEGV) and Suricata's crash stacktrace:
```
Error: suricata: stacktrace:sig 11:LuaFlowintRegister+0x...;...;lua_pcallk+0x...;TransformLuaxform+0x...;InspectionBufferSetupAndApplyTransforms+0x...;GetData+0x...;PrefilterMpm+0x...;DetectRunPrefilterTx+0x...;DetectRun+0x...;Detect+0x...;FlowWorker+0x...;...
```
Verified on both the normal build and the ASAN build at commit `ce022d25d`
(8.0.7-dev). ASAN is the recommended harness (the fault is a read of a small
offset from NULL, so the ASAN report and offset are clean), but the plain
build crashes identically.
## Why bash and not a Suricata-Verify test
The signal is a process crash (SIGSEGV), not an alert/event. SV tests assert
on engine output (eve.json counts etc.) and are driven through the normal
run wrapper; a SEGV in the middle of packet processing kills the run and
SV reports it as an unexpected failure rather than a pass condition. A plain
bash reproducer that checks exit code 139 + the `LuaFlowintRegister` stack
frame is the honest, deterministic harness here.
## Fix direction (for the record)
`LuaFlowintRegister` / `LuaFlowvarRegister` must treat `GetLuaData() == NULL`
as "not in a rule-script context" and return a Lua error
(`return luaL_error(L, "flowint not available in this context")`) instead of
dereferencing. Longer term, luaxform (and other non-detect-lua Lua entry
points) should not expose the flowint/flowvar modules they cannot support,
or the sandbox builtin list should be per-context.
LS Updated by Lukas Sismis 2 months ago
- Affected Versions 8.0.7 added
VJ Updated by Victor Julien about 2 months ago
- Status changed from New to In Progress
- Assignee changed from OISF Dev to Victor Julien
JI Updated by Jason Ish about 1 month ago
- Status changed from In Progress to In Review
MR1382