Bug #9054
closedlua/dataset: cached Dataset pointer dangles after rule reload -> use-after-free
Description
- Status: present in
main(9b7938aec); code-confirmed, repro not yet executed - Class: use-after-free (heap) after rule reload
- Component:
src/util-lua-dataset.c,src/datasets.c,src/detect-engine.c
Description¶
The Lua dataset handle stores a raw, non-owning Dataset*:
src/util-lua-dataset.c:63-67 (LuaDatasetGetRef)
src/util-lua-dataset.c:94 (LuaDatasetAdd)Static datasets are freed on rule reload:
DatasetReload()marks static sets (loadset, nosave, not from\_yaml) hidden (src/datasets.c:539-560);DatasetPostReloadCleanup()then runsTHashShutdown(cur->hash); SCFree(cur);(src/datasets.c:565-585);- both are called from
DetectEngineReload()(src/detect-engine.c:5099,:5153), reached via SIGUSR2 /suricatasc -c reload-rules.
Rule and transform Lua states are per-de\_ctx and are recreated by a reload. Output Lua states are created once at startup (OutputRegisterModule("lua", OutputLuaLogInit), src/output-lua.c:941) and are not reinitialized on reload, so a handle cached by an output script keeps pointing at the freed set. DatasetSearchByName() skips hidden sets (src/datasets.c:121-131), so the script cannot re-resolve the name either.
LuaDatasetGC() also reads s->set->name before NULLing it (util-lua-dataset.c:44-49) - a UAF read when debug logging is enabled.
Trigger¶
- Static dataset from a rule:
dataset:isset,dstest,load=/tmp/ds.lst; - Output script
setup():local ds = require("suricata.dataset"); d = ds.new(); d:get("dstest")(cached in a global). log()callsd:add(payload, #payload).- Run traffic, reload rules (
SIGUSR2), run traffic again →SCDatasetAdd()dereferences the freedDataset/THashTableContext(ASAN heap-use-after-free; probable SEGV).
A narrower race also exists for rule/transform states if a lookup/add overlaps DatasetPostReloadCleanup(), which takes no THash locks (THashShutdown is documented as not thread-safe).
Fix direction¶
- Store the dataset name in the handle and re-resolve it per use (rejecting hidden sets), or
- refcount datasets across reloads, or
- invalidate Lua dataset handles in
DatasetReload().
Tests¶
lua-dataset-reload-stale (pcap → reload → pcap, ASAN, output-script handle), lua-dataset-handle-refresh (post-reload get/add hits the new set).
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