Project

General

Profile

Actions

Security #9050

closed
VJ VJ

lua: flowint/flowvar register and bytevar map NULL-deref when DetectLuaData is unset (output/transform scripts)

Security #9050: lua: flowint/flowvar register and bytevar map NULL-deref when DetectLuaData is unset (output/transform scripts)

Added by Victor Julien 2 months ago. Updated 7 days ago.

Status:
Closed
Priority:
Normal
Assignee:
Target version:
Affected Versions:
Label:
CVE:
Git IDs:

4086dc23f03c247a2780d6c9cf8b6406675aa824

Severity:
LOW
Disclosure Date:

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.


Subtasks 1 (0 open — 1 closed)

Security #9300: lua: flowint/flowvar register and bytevar map NULL-deref when DetectLuaData is unset (output/transform scripts) (8.0.x backport)ClosedVictor JulienActions

LS Updated by Lukas Sismis 2 months ago Actions #1

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 Actions #2

  • Affected Versions 8.0.7 added

VJ Updated by Victor Julien about 2 months ago Actions #3

  • Status changed from New to In Progress
  • Assignee changed from OISF Dev to Victor Julien

JI Updated by Jason Ish about 1 month ago Actions #4

  • Status changed from In Progress to In Review

MR1382

JI Updated by Jason Ish 26 days ago Actions #5

  • Subtask #9300 added

JI Updated by Jason Ish 26 days ago Actions #6

  • Severity set to LOW

JI Updated by Jason Ish 26 days ago Actions #7

  • GHSA set to GHSA-f5fw-cv46-fjpx

JI Updated by Jason Ish 25 days ago Actions #8

  • Git IDs updated (diff)

JI Updated by Jason Ish 25 days ago Actions #9

  • Status changed from In Review to Closed

JI Updated by Jason Ish 7 days ago Actions #10

  • Private changed from Yes to No
Actions

Also available in: PDF Atom