Project

General

Profile

Actions

Security #9053

closed
VJ VJ

lua: security.lua.max-bytes is a per-call budget, not a memory cap (retained memory unbounded)

Security #9053: lua: security.lua.max-bytes is a per-call budget, not a memory cap (retained memory unbounded)

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

Status:
Closed
Priority:
Normal
Assignee:
Target version:
Affected Versions:
Label:
CVE:
Git IDs:

4e933f4cc99f89bb190c5600f22ce30b6e1fd1b7
bafb33d13f9d834fc1bd99b9556873ef5ebb955e

Severity:
LOW
Disclosure Date:

Description

  • Status: present in main (9b7938aec); confirmed with ASAN
  • Class: resource-limit semantics: security.lua.max-bytes is 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)

sb->alloc_limit = sb->alloc_bytes + sb->alloc_limit;

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);
  • luaxform retaining 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)

  1. Document security.lua.max-bytes as a per-call incremental budget (do not change behavior), or
  2. Enforce an absolute live-allocation cap: keep the configured limit fixed instead of adding alloc_bytes in SCLuaSbUpdateBytesLimit() (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.


Subtasks 1 (0 open — 1 closed)

Security #9303: lua: security.lua.max-bytes is a per-call budget, not a memory cap (retained memory unbounded) (8.0.x backport)ClosedVictor JulienActions
Actions

Also available in: PDF Atom