Security #9048
closedlua: log API allows out of bounds read
3bece75d5b97d60522a1057af7ac1dfb667d42b2
Description
```
SCLogMessageJSON() copies the rendered JSON into the caller's fixed-size stack buffer with
src/util-debug.c:247-248
SCJbClose(js);
memcpy(buffer, SCJbPtr(js), MIN));
SCJbLen() is js.buf.len() (rust/src/jsonbuilder.rs:1137) and does not include a NUL terminator. The destination buffer is char buffer[SC_LOG_MAX_LOG_MSG_LEN] = ""; (src/util-debug.c:655-656, SC_LOG_MAX_LOG_MSG_LEN = 8192 + PATH_MAX + 512 = 12800 on Linux). The aggregate initialization zeroes the whole array, so a render shorter than buffer_size is safe by accident; when SCJbLen(js) >= buffer_size the memcpy fills all 12800 bytes and nothing terminates the string.
Every engine-log sink then consumes it as a C string:
SCLogPrintToStream(): fprintf(fd, "%s\n", msg) (src/util-debug.c:168) — console and file outputs
SCLogPrintToSyslog(): syslog(level, "%s", msg) (src/util-debug.c:195)so the %s scan walks past the end of the stack object until it finds a NUL byte: out-of-bounds stack read. How far it reads is stack-content dependent, so adjacent stack data (other locals, pointers) can end up in the log output, and the scan can crash if it reaches an unmapped page. Any log output configured with type: json (src/util-debug.c:1487-1495) is affected.
The other JSON consumers (fwrite(SCJbPtr(js), SCJbLen(js), ...)) are length-based and safe; this memcpy is the only place that combines a length-bounded copy with a C-string consumer.
Trigger
Any engine log message long enough to be clamped by the SCLog* formatter is sufficient: the macros do vsnprintf(msg, sizeof(msg), fmt, ap) into a 12800-byte buffer (src/util-debug.c:737-745), so a longer message becomes 12799 chars, and the ~145 bytes of JSON framing (timestamp, keys, quotes, thread name) then push SCJbLen past buffer_size. JSON escaping of quotes/backslashes can push shorter messages over the limit as well.
Reachable from Lua through the engine-log API (suricata.log, src/util-lua-log.c), which passes arbitrarily long strings. No default rule/decoder path is known to produce a ~12.8 KB log message, so the issue is latent with respect to traffic alone, but it is deterministic as soon as one such message is logged (Lua script, plugin, or a future call site).
```
I think this found by my local analysis, but it's possible I forgot about someone reporting it.