Project

General

Profile

Actions

Bug #9120

closed
LS VJ

lua: SMTP Lua tx userdata use-after-free

Bug #9120: lua: SMTP Lua tx userdata use-after-free

Added by Lukas Sismis 2 months ago. Updated 8 days ago.

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

Description

# L6 — SMTP Lua tx userdata use-after-free

## Finding

**File/line:** `src/util-lua-smtp.c`
- `LuaSmtpGetTx()` (lines 44–75): wraps `state->curr_tx` (a raw
  `SMTPTransaction*`) in a Lua userdata. There is **no lifetime guard**: no
  `__gc` metamethod, no epoch/tx-id check, no flow ownership check.
- `LuaSmtpTxGetMailFrom()` (line 125, also `get_mime_field`, `get_mime_list`,
  `get_rcpt_list`): dereferences `tx->tx->mail_from` unconditionally.

**Bug class:** heap use-after-free (dangling pointer in Lua userdata).

**Lifecycle conflict:** the SMTP transaction wrapped by the userdata is owned
by the app-layer parser. Once a tx is complete in both directions and fully
inspected, `AppLayerParserTransactionsCleanup()` (`src/app-layer-parser.c`,
called per packet from `FlowWorker`) frees it via
`SMTPStateTransactionFree()` → `SMTPTransactionFree()` → `SCFree(tx)`.
Meanwhile, the per-thread Lua state persists across packets
(`DetectLuaThreadData->luastate` lives for the worker thread's lifetime), so a
Lua rule that stores the tx userdata in a **global** keeps using the pointer
after the free. Any later `match()` call that touches the cached userdata
dereferences freed memory.

## How it reproduces

1. One SMTP flow (TCP/25) carrying **two complete emails**, each command in
   its own packet so transaction cleanup runs between them. Two ordering
   details matter (both found by debugging the parser):
   - the server `220` greeting must be the first to-client data, otherwise
     `SMTP_PARSER_STATE_FIRST_REPLY_SEEN` is never set and the tx is never
     freed;
   - the reply to `DATA` must be `354` (a `250` there is
     `DATA_COMMAND_REJECTED` and the tx is never marked complete to-client).
2. After the final `.` of mail #1, the server's `250` completes tx #0 in both
   directions; the next `MAIL FROM` opens tx #1 and cleanup frees tx #0.
3. The Lua rule caches the userdata of tx #0 in a global on its first
   invocation (`init` returns `{packet = true}` so `match()` runs per packet)
   and calls `cached:get_mail_from()` on every subsequent packet.
4. A second plain `content:"body"` rule drives the inspection flags so tx #0
   is eligible for cleanup.

## Expected signal

ASan (build `/home/local/xsismi01/suricata-asan/src/suricata`) aborts with:

```
ERROR: AddressSanitizer: heap-use-after-free ... READ of size 8
    #0 LuaSmtpTxGetMailFrom src/util-lua-smtp.c:125
    #8 DetectLuaRunMatch src/detect-lua.c:178
freed by thread T5 (W#04) here:
    #1 AppLayerParserTransactionsCleanup src/app-layer-parser.c:1025
    #2 FlowWorker src/flow-worker.c:702
```

## Why bash and not Suricata-Verify

The observable is an ASan abort inside the detection worker, not an alert or
an event that the normal build would emit. Suricata-Verify runs the
non-instrumented binary and cannot see the UAF, so the harness is a bash
script driving the ASan build directly.

## How to run

L6 SV test with ASAN

## Fix direction (not implemented here)

Give the userdata a lifetime guard, e.g. store the flow + tx_id in
`struct LuaSmtpTx` instead of (or alongside) the raw pointer and re-resolve
through `SMTPStateGetTxById` in each accessor, or register a `__gc`/epoch
check keyed to the app-layer state's tx list. Compare with how
`util-lua-http.c` provides `get_tx` — same pattern, same latent issue.


Subtasks 1 (0 open — 1 closed)

Bug #9335: lua: SMTP Lua tx userdata use-after-free (8.0.x backport)ClosedVictor JulienActions
Actions

Also available in: PDF Atom