Lua hook, standalone (L3)
Ready
The hub proposes this hook for the containers it fits, and nothing changes until a player approves the proposal (Hook proposals). The cheat sheet has the steps by hand.
For Lua services run by the lua or luajit command-line interpreter, typically raw TCP services on Copas or HTTP services on lua-http (cqueues). It gets in compose-only: a read-only mount of vulnboxd's hook directory and LUA_INIT pointing at the hook. The image and the source stay untouched. The hook is plain Lua. It reaches the core library through LuaJIT's FFI, or on PUC Lua through a tiny C module that works with every Lua version. Lua inside nginx (OpenResty, Lapis) is L2.
Covers
| Versions | |
|---|---|
| PUC Lua | 5.1–5.4 (loading verified on 5.1, 5.3, 5.4; samples on 5.3 and 5.4), glibc and musl, x86_64 and aarch64 |
| LuaJIT | 2.1 (sample on Alpine) |
| Copas | 4.x (tested 4.7.1): servers added with copas.addserver (one raw session per connection) |
| lua-http | 0.4 (tested on Lua 5.3 / cqueues): http.server.new / .listen, one request per stream; HTTP/1.1 sampled, HTTP/2 streams take the same path (not sampled) |
| LuaSocket | 3.x: connect on TCP objects, socket.connect/connect4/connect6 |
| Luvit | not supported (see Known gaps) |
What the kernel already links, without this hook. Nothing per request: Copas and cqueues run every connection as a coroutine on one thread, so the kernel can't tell which request a fork, file or connection belongs to. Services that fork per connection (inetd/xinetd/socat launching a Lua script) are kernel-exact and need no hook (see X1).
How it gets in
From a registry (local development, or a build with network; the same release as the hook directory): luarocks install vbxtrace 1.0.0-1, then lua -l vbxtrace app.lua; see Local development.
Compose-only. The hub proposes:
services:
<service>:
+ volumes:
+ - /usr/local/lib/vulnboxd/trace:/opt/vbxtrace:ro
+ environment:
+ LUA_INIT: "@/opt/vbxtrace/lua/vbxtrace.lua"lua and luajit run the chunk named by LUA_INIT before the script. If the service already sets LUA_INIT, it can't be appended to (it holds one chunk). The hub moves the old value to VBXTRACE_LUA_INIT_PREV, and the hook runs it right after itself, as Lua would have (@file or inline code). Verified on 5.1, 5.4 and LuaJIT. Lua 5.2–5.4 prefer LUA_INIT_5_x over LUA_INIT; if the service uses one of those, the hub sets that variable the same way instead.
The hub proposes this when it detects any of:
- a rockspec or Lua sources in the service directory;
- a
nickblah/lua,*/lua:or*/luajitimage; lua,lua5.xorluajitin the command;- Copas, lua-http, cqueues or LuaSocket among the dependencies.
The template is hooks/lua/l3/proposals/lua-init.toml. It extends the proposal format with env = { VAR = { set = …, previous_to = … } } for variables that can't be appended to.
What it records
| Event | Emitted from | Join key |
|---|---|---|
request_begin / request_end (raw, index 0) | the handler passed to copas.addserver: begins when Copas starts it for an accepted socket, ends when it returns or raises | the client socket's getsockname/getpeername |
request_begin / request_end (http1 / http2) | the onstream callback of a lua-http server, with the status the stream sent in write_headers | stream:localname()/peername(); request index counted per connection |
context_enter / context_exit | whenever coroutine.resume runs a coroutine that belongs to a request (Copas resumes its handlers this way), and around blocking calls made for a request: os.execute, io.popen, io.open, os.remove, os.rename | the thread (Lua runs one) |
connected | LuaSocket TCP connect / socket.connect returning a connected socket, with the local port and peer | local port |
Nothing else runs on the thread inside a window, so the kernel attributes the fork, file open or connect it sees there to the request.
Context following
- One context per coroutine. It is set on the handler's or
onstream's coroutine when the request starts. - Inheritance. Coroutines created inside a request (
coroutine.create,coroutine.wrap, so alsocopas.addthread) inherit its context. The exceptions are Copas's own long-lived threads (copas_*, started lazily, possibly during a session). - Copas resumes handlers with
coroutine.resume, so each stretch a session runs between two yields is a window. Aftercopas.pauseor a blocking read, the session continues in a new window (the sample'spause). - cqueues (lua-http) resumes coroutines from C, out of the hook's sight. There a window opens only around the blocking calls above. Connects are linked by
connected, and other syscalls of the request by the kernel's own rules. handoffisn't needed: work stays in the process's coroutines.
Known gaps
- cqueues sockets (
cqueues.socket.connect, used by lua-http's client) are not wrapped. Outgoing connections they make get noconnected. - Luvit isn't supported. It doesn't run Lua's command-line interpreter, so
LUA_INITis ignored, and its libuv callbacks aren't coroutines the hook could follow. A source change (requiring the hook first) would load it, but still give no request boundaries. - Other Lua servers (Pegasus, Xavante on Copas, hand-rolled
socket.selectloops) get context only where they go throughcopas.addserver. Xavante and Pegasus on Copas count as raw sessions, one per connection even with keep-alive. io.lines(path),dofile,loadfileand C modules opening files themselves aren't windowed.- Error messages. Wrapped functions are called through locals of the same name, and errors carrying this file's position are re-raised with the caller's. Messages from ordinary calls with bad arguments, and "module not found" from
require, therefore read the same as without the hook. They can still differ in two cases:- the bad call is a tail call (
return io.open(nil)); - the function is handed directly to
pcall(pcall(os.execute, {})).
- the bad call is a tail call (
coroutine.wrapcoroutines carry their context, but their resumptions open no window: the wrapper resumes them in C.
Safety
What is wrapped:
coroutine.create/wrap/resume;os.execute,io.popen,io.open,os.remove,os.rename;- the global
require(to patch LuaSocket, Copas and lua-http when they load); - LuaSocket's TCP
connectandsocket.connect*; copas.addserver,copas.addnamedthread;- lua-http's
server.new/listen(given a copy of the options with a wrappedonstream) and the streams'write_headers.
How it stays invisible:
- Wrappers return exactly what the originals return. Handler errors are re-raised unchanged.
- The hook's own work runs under
pcall. After 5 internal errors it switches itself off. - The hook's setup itself runs under
pcall, so a failure there can never stop the interpreter from running the script. - Windows open only for arguments the original accepts (it then returns instead of raising), so they always close.
- Until a request starts with a tracer attached (
vbx_v1_enabled()), no context exists and wrappers pass straight through. VBXTRACE=0disables the hook (verified; a chained previousLUA_INITstill runs).VBXTRACE_CHAOS=1(tests only) makes every call into the core fail. The samples answer byte-identically with the hook failing, working and absent.
The C module (lua/vbxlua-<arch>.so, for PUC Lua):
- It includes no Lua headers and links no libc. It calls only C API functions with the same symbol and signature in 5.1–5.4, reading numbers through
lua_tolstring. - It finds the core as its DT_NEEDED dependency through
RUNPATH $ORIGIN/.., which resolves to/opt/vbxtrace. - One build per architecture serves every PUC Lua version on glibc and musl.
Overhead has not been measured yet; that's a later phase.
Turning it off
Set VBXTRACE=0 in the service's environment and restart it, or revert the change that added the hook; vulnboxd's rollback covers a hook that breaks a service. Reverting restores the previous LUA_INIT.
Troubleshooting
Are the probes there?
readelf -n /usr/local/lib/vulnboxd/trace/libvbxtrace-x86_64.solists them;sudo bpftrace -l 'usdt:/usr/local/lib/vulnboxd/trace/libvbxtrace-x86_64.so:*'too.Is the hook loaded? In the container:
shLUA_INIT=@/opt/vbxtrace/lua/vbxtrace.lua lua -e 'local v = package.loaded.vbxtrace; print(v and v.active, v and v.backend)'This prints
true cmodule(PUC Lua) ortrue ffi(LuaJIT).grep vbx /proc/<pid>/mapsshows the libraries in the running service.Which links did a flow get? The hub shows each link's method; this package's are
hook:L3.Nothing happens: the service may run Lua through an embedding (nginx, Redis, a game server) or a launcher that doesn't honour
LUA_INIT(Luvit). Or it setsLUA_INIT_5_x, which wins overLUA_INIT.