Project

General

Profile

Actions

Bug #9074

closed
VJ VJ

lua: flowvar/flowint register() races shared DetectLuaData from thread_init; refcount leak on limit path

Bug #9074: lua: flowvar/flowint register() races shared DetectLuaData from thread_init; refcount leak on limit path

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

Status:
Closed
Priority:
Normal
Assignee:
Target version:
Affected Versions:
Effort:
Difficulty:
Label:

Description

  • Status: present in main (9b7938aec); code-confirmed
  • Class: data race on shared state + refcount leak on the error path
  • Component: src/util-lua-flowvarlib.c:51-66, src/util-lua-flowintlib.c:51-66, src/detect-lua.c:425/452-460

Description

DetectLuaData (ld) is created once per signature at parse time and shared by every per-thread Lua state (detect-lua.c:425 stores it in each state; thread_init() runs per worker at :452-460). register() mutates ld directly:

*flowvar_id = VarNameStoreRegister(name, VAR_TYPE_FLOW_VAR);   /* before the limit check */
if (*flowvar_id == 0) return luaL_error(...);
if (ld->flowvars >= DETECT_LUA_MAX_FLOWVARS) return luaL_error(...);
ld->flowvar[ld->flowvars++] = *flowvar_id;
  1. Two worker threads running thread_init() (or match()) concurrently perform an unsynchronized check-then-append on the same arrays. The 15-entry cap prevents an OOB, but the counter and entries can be duplicated or lost, so script registrations are nondeterministic.
  2. The limit check runs after VarNameStoreRegister, so the 16th registration increments the global name-store refcount but is never recorded in ld; DetectLuaFree (detect-lua.c:805-809) only unregisters entries it knows about, so one refcount/name entry leaks per over-limit call.

No memory corruption (array is capped).

Fix direction

  • Move VarNameStoreRegister after the limit check and unregister on the error path.
  • Serialize ld mutation (mutex) or, preferably, make registration init-only / build ld before threads start and reject register() from thread_init/match.

Tests

lua-multithread-register (register in thread_init under --runmode workers, TSAN) and a 16-registration over-limit case asserting clean failure and no leaked name-store entry (engine restart/teardown).


Subtasks 1 (0 open — 1 closed)

Bug #9324: lua: flowvar/flowint register() races shared DetectLuaData from thread_init; refcount leak on limit path (8.0.x backport)ClosedVictor JulienActions
Actions

Also available in: PDF Atom