Bug #9213
closedlua: max-bytes zero is not unlimited and negative values are accepted
Description
Summary¶
Setting security.lua.max-bytes: 0 does not preserve the Lua allocator's documented unlimited-memory behavior during rule or transform execution. The per-call limit update turns zero into a positive cap, leaving no additional allocation budget on the first invocation. Scripts that need more memory than garbage collection can recover can fail despite the unlimited setting.
Negative values are also accepted and converted from a signed configuration integer to uint64_t, producing unintended limits and possible wraparound in the per-call cap calculation.
Cause¶
LuaAlloc() skips its memory-cap checks when alloc_limit == 0, and the SCLuaSbStateNew() contract documents zero as unlimited.
Before commit f89429426a54be29026f9813bf919165bee8e7d9, SCLuaSbUpdateBytesLimit() unconditionally calculated:
With a configured limit of zero, the result is normally the current allocation at the start of the first invocation, rather than the zero sentinel. Further allocations can therefore fail with a memory-limit error. Both lua rules and luaxform use this update path.
For a negative setting such as -1, conversion to uint64_t produces UINT64_MAX; adding the baseline and setup usage wraps the cap to one byte below their sum. Other negative values can produce different unintended caps.
Impact¶
An operator selecting unlimited memory can instead encounter runtime allocation failures. Affected Lua rules fail to match, and failed transforms can leave the original input buffer unchanged. Lua rule errors are reported through warnings and counters; rule loading itself can succeed.
This is a configuration-semantics bug. It does not establish a sandbox escape or memory-corruption vulnerability.
Fix and validation¶
Commit f89429426a54be29026f9813bf919165bee8e7d9 on lua-fixes/v3:
- Keeps
alloc_limitat zero when the configured limit is zero. - Rejects negative
security.lua.max-bytesvalues during setup of both keywords. - Preserves the existing positive-limit calculation.
Matching Suricata-Verify tests are tests/lua-sbx-mem-zero (successful rule evaluation with zero configured) and tests/lua-config-negative-max-bytes (setup errors for both keywords with -1).
The behavior and fix were checked by source inspection against the commit's parent. The tests were inspected but not executed during this review. Stable-branch impact has not been assessed here.
Related work¶
Redmine #9053 covers retained script memory escaping a positive per-call budget. This ticket tracks the separate zero/negative configuration behavior.