Project

General

Profile

Actions

Bug #9052

closed
VJ VJ

output-lua: log() error paths leak a Lua stack slot per failed call

Bug #9052: output-lua: log() error paths leak a Lua stack slot per failed call

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

Status:
Closed
Priority:
Normal
Assignee:
Target version:
Affected Versions:
Effort:
Difficulty:
Label:

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.


Subtasks 1 (0 open — 1 closed)

Bug #9302: output-lua: log() error paths leak a Lua stack slot per failed call (8.0.x backport)ClosedVictor JulienActions

VJ Updated by Victor Julien about 2 months ago Actions #1

  • Status changed from New to In Progress
  • Assignee changed from OISF Dev to Victor Julien

JI Updated by Jason Ish about 1 month ago Actions #2

  • Status changed from In Progress to In Review

MR1382

JI Updated by Jason Ish 27 days ago Actions #3

  • Subtask #9302 added

JI Updated by Jason Ish 27 days ago Actions #4

  • Severity set to LOW

JI Updated by Jason Ish 27 days ago Actions #5

  • GHSA set to GHSA-773v-cvwg-vvr2

JI Updated by Jason Ish 26 days ago Actions #6

  • Tracker changed from Security to Bug
  • Severity deleted (LOW)
  • GHSA deleted (GHSA-773v-cvwg-vvr2)

JI Updated by Jason Ish 26 days ago Actions #7

  • Status changed from In Review to Closed

JI Updated by Jason Ish 8 days ago Actions #8

  • Private changed from Yes to No
Actions

Also available in: PDF Atom