Security #9053
closedlua: security.lua.max-bytes is a per-call budget, not a memory cap (retained memory unbounded)
4e933f4cc99f89bb190c5600f22ce30b6e1fd1b7
bafb33d13f9d834fc1bd99b9556873ef5ebb955e
Description
- Status: present in
main(9b7938aec); confirmed with ASAN - Class: resource-limit semantics:
security.lua.max-bytesis a per-call budget, not a cap; retained script memory is unbounded - Component:
src/util-lua-sandbox.c(SCLuaSbUpdateBytesLimit),src/detect-lua.c,src/detect-transform-luaxform.c
Description¶
Every rule/transform call resets, restores and then raises the allocation limit around the script invocation:
src/detect-lua.c:258/275-276, :323/333-334, :356/365-366; src/detect-transform-luaxform.c:325/344-345
const uint64_t cfg_limit = SCLuaSbResetBytesLimit(L); /* save + disable */
... push buffer/args ...
SCLuaSbRestoreBytesLimit(L, cfg_limit); /* restore */
SCLuaSbUpdateBytesLimit(L); /* limit = alloc_bytes + cfg_limit */
SCLuaSbUpdateBytesLimit() (src/util-lua-sandbox.c:349-355)
sets the cap to the current live allocation plus the configured budget. Because alloc_bytes counts everything the Lua GC has not yet reclaimed - including values a script keeps alive in module-level tables - each call permanently raises the effective cap by the retained bytes. A script that stores one allocation per call therefore retains unbounded memory although security.lua.max-bytes is configured.
Evidence (ASAN build, max-bytes: 20000)¶
- rule
match()retaining 10 KB per call:RETAIN 1...RETAIN 8(\~80 KB retained), no memory-limit error (/tmp/ws2/out.log); luaxformretaining 10 KB per call:RETAINX 1...RETAINX 8, no error (/tmp/ws2b.log);- control: a single 30 KB allocation in the first call correctly fails with
Lua script failed to run successfully: memory limit exceeded(/tmp/ws2ctl.log) - so the per-call check itself works.
The knob therefore bounds per-call allocation, not script memory. This is either a documentation bug (the config implies a cap) or an implementation bug (retained memory should count against an absolute cap); a decision is needed.
Fix direction (options)¶
- Document
security.lua.max-bytesas a per-call incremental budget (do not change behavior), or - Enforce an absolute live-allocation cap: keep the configured limit fixed instead of adding
alloc_bytesinSCLuaSbUpdateBytesLimit()(scripts that legitimately grow state then need a higher configured value), or add a separate absolute cap.
Option 2 is preferable if the config is meant as a containment control; option 1 requires an explicit user-guide statement and keeping the current reset/update contract.
Regression tests¶
lua-sandbox-bytes-retention: retain 10 KB per call under max-bytes: 20000; assert the configured semantics (either the cap holds across calls, or the documented per-call budget is pinned). Include the single-30 KB control.
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