Project

General

Profile

Actions

Bug #9054

closed
VJ VJ

lua/dataset: cached Dataset pointer dangles after rule reload -> use-after-free

Bug #9054: lua/dataset: cached Dataset pointer dangles after rule reload -> use-after-free

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, 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)

Dataset *dataset = DatasetFind(name, DATASET_TYPE_STRING);
...
s->set = dataset;

src/util-lua-dataset.c:94 (LuaDatasetAdd)
int r = SCDatasetAdd(s->set, str, str_len);

Static datasets are freed on rule reload:

  • DatasetReload() marks static sets (load set, no save, not from\_yaml) hidden (src/datasets.c:539-560);
  • DatasetPostReloadCleanup() then runs THashShutdown(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

  1. Static dataset from a rule: dataset:isset,dstest,load=/tmp/ds.lst;
  2. Output script setup(): local ds = require("suricata.dataset"); d = ds.new(); d:get("dstest") (cached in a global).
  3. log() calls d:add(payload, #payload).
  4. Run traffic, reload rules (SIGUSR2), run traffic again → SCDatasetAdd() dereferences the freed Dataset/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).


Subtasks 1 (0 open — 1 closed)

Bug #9304: lua/dataset: cached Dataset pointer dangles after rule reload -> use-after-free (8.0.x backport)ClosedVictor JulienActions

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

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

  • Status changed from In Progress to In Review

MR1382

JI Updated by Jason Ish 27 days ago Actions #3

  • Subtask #9304 added

JI Updated by Jason Ish 27 days ago Actions #4

  • Severity set to LOW

JI Updated by Jason Ish 27 days ago Actions #5

  • Tracker changed from Security to Bug
  • Severity deleted (LOW)

JI Updated by Jason Ish 26 days ago Actions #6

  • Status changed from In Review to Closed

JI Updated by Jason Ish 8 days ago Actions #7

  • Private changed from Yes to No
Actions

Also available in: PDF Atom