Bug #9052
closedoutput-lua: log() error paths leak a Lua stack slot per failed call
Description
- Status: present in
main(9b7938aec); code-confirmed - Class: unbounded Lua stack/state growth on the output-script error path
- Component:
src/output-lua.c(all output log callbacks)
Description¶
All seven output log paths run lua_pcall and, on failure, log the error object without popping it:
src/output-lua.c:99-102, 159-162, 217-220, 266-269, 311-314, 341-344, 394-397
int retval = lua_pcall(td->lua_ctx->luastate, 1, 0, 0);
if (retval != 0) {
SCLogInfo("failed to run script: %s", lua_tostring(td->lua_ctx->luastate, -1));
}
On error, lua_pcall leaves exactly one value (the error) on the stack; the code does not pop it, and the next call pushes the log function and its argument table on top. A script that raises on every log() (e.g. a typo, a missing field, a runtime error on some traffic) therefore grows the shared Lua state by one value per event, indefinitely.
The output state is created with luaL_newstate() (output-lua.c:589) and is deliberately not subject to security.lua.max-bytes / max-instructions, so nothing bounds the growth until engine restart. Long pcap replays or long-running deployments with a broken output script are affected.
Rule-side scripts are not affected: DetectLuaRunMatch drains the stack on both success and error paths (detect-lua.c:201-203, 231-233).
Fix direction¶
Pop the error after logging (or reset the stack):
if (retval != 0) {
SCLogInfo("failed to run script: %s", lua_tostring(td->lua_ctx->luastate, -1));
lua_pop(td->lua_ctx->luastate, 1);
}
Apply at all seven sites. Consider a shared helper for the call/log/pop sequence.
Regression test¶
lua-output-log-error-stack: output script whose log() always raises; run a pcap with many events; assert the engine stays stable (state/stack does not grow; e.g. check lua_gettop via a debug build or monitor RSS/LSAN) and errors are still reported.
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