Security #9060
closedlua: cached packet/flow/file/signature handles are non-owning and never invalidated -> UAF across calls
8a40b054e13c93629de1ee9bb2294f53bedc7953
acf6ef1c75b443a42d975646da9c42c7cd3c4a0d
Description
- Status: present in
main(9b7938aec); owners/free paths verified in code (not yet reproduced) - Class: use-after-free / cross-object data disclosure from cached non-owning userdata handles
- Component:
suricata.packet(src/util-lua-packetlib.c),suricata.flow(src/util-lua-flowlib.c),suricata.file(src/util-lua-filelib.c),suricata.rule(src/util-lua-rule.c); same class as the dataset handle staleness after rule reload
Description¶
These libs wrap raw non-owning pointers in userdata: Packet* (packet.get()), Flow* (flow.get()), File* (file.get_file()), const Signature* (rule.get_rule(), from PacketAlert->s or the current signature). No handle carries a refcount, generation counter or invalidation hook; __gc only NULLs the wrapper (file/rule have no __gc) and is not called while the handle is still referenced.
A script that stores a handle in a global and uses it on a later call/tx/flow/event keeps a dangling pointer:
- packet:
Packetis returned to the pool after processing (tmqh-packetpool.c:168-215);PacketReinitfreesext_pktand zeroes fields (packet.c:95-170); malloc-backed packets areSCFree@d (@decode.c:298-312) -> cross-packet data, tornpayload/payload_len, UAF. - flow:
FlowClearMemory->FLOW_RECYCLE-> spare pool (flow-manager.c:1091-1104,:1141-1143); spare blocks areFlowFree@d when the pool is trimmed (@flow-spare-pool.c:217-240) -> UAF / cross-flow (tenant) data leak. - file:
FileFreeviaFileContainerRecycle/FileContainerFree/FilesPrune(util-file.c:566,:495,:515,:1175) -> reads through freedname/magic/digest memory. - signature:
SigFreeon rule reload (detect-parse.c:2343,:2403) for output alert scripts (rule/luaxform states are recreated before the prune, output states are not, the same premise as the dataset handle staleness) -> UAF read insid()/msg()/class_description().
Trigger pattern (per lib): cache the handle in log()/match() on the first event and dereference it on the second.
Fix direction¶
Chose one and document it:- Call-scoped contract: document that handles are valid only during the callback that produced them; additionally stop exposing raw pointers where a value copy is enough (packet/flow fields are small).
- Generation/validity token: keep a per-state generation counter bumped on context change and have each handle store the generation at
get()time; methods reject stale handles. - Keep handles but add an engine-side invalidation hook (hard: the script may hold the only reference).
Tests¶
lua-packet-stale-handle, lua-flow-stale-handle, lua-file-stale-handle, lua-rule-stale-signature (all under ASAN; multi-event pcaps + rule reload for the signature case).
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