Bug #9061
closedlua: stale tx/tx_id survive a failed AppLayerParserGetTx lookup
Description
- Status: present in
main(9b7938aec); code-confirmed - Class: stale registry pointer -> potential use-after-free read
- Component:
src/detect-lua-extensions.c:56-62(LuaExtensionsMatchSetup)
Description¶
The TX pointer/id in the Lua registry are only written on a successful lookup:
if (det_ctx->tx_id_set) {
if (f && f->alstate) {
void *txptr = AppLayerParserGetTx(f->proto, f->alproto, f->alstate, det_ctx->tx_id);
if (txptr) {
LuaStateSetTX(lua_state, txptr, det_ctx->tx_id);
}
}
}
There is no else-branch clearing TX/tx\_id. If a later call in the same rule state fails the lookup (alproto switch, tx out of range or already freed, alstate swapped), the script's get_tx() helpers wrap the previous tx pointer and tx_id reports the stale id; the protocol libraries then call their FFI getters on a freed/mismatched tx.
Reachable from tx-hook rules and app-layer buffer rules (the buffer path also runs inside the DetectRunTx loop with tx_id_set, detect.c:2397). Mechanism high confidence; exact trigger requires a failed lookup after a successful one.
Fix direction¶
Clear the registry entries when the lookup fails or is not attempted:
void *txptr = NULL;
if (det_ctx->tx_id_set && f && f->alstate)
txptr = AppLayerParserGetTx(...);
LuaStateSetTX(lua_state, txptr, txptr ? det_ctx->tx_id : 0);
Test¶
lua-tx-stale-failed-lookup: force a successful lookup, then a call where AppLayerParserGetTx() returns NULL in the same state; assert get_tx() yields nil (not the previous tx).