szn

Bug 221 — Use-after-free in runServerDaemon: default_pane captured across async server.run calls

221. Use-after-free in runServerDaemon: default_pane captured across async server.run calls

File: src/main.zig:205–213 Severity: HIGH Status: ✅ FIXED — re-validate that the pane still exists by walking sessions again immediately before spawn.

var default_pane: ?*@import("window.zig").Pane = null;
for (server.sessions.items) |s| {
    if (s.id == default_session_id) {
        if (s.active_window) |w| {
            default_pane = w.active_pane;
        }
        break;
    }
}
// ... 16 rounds of server.run(1) happen above (lines 193–195)
if (default_pane) |p| {
    try p.spawn(allocator, &[_][]const u8{shell}, null);
    try server.watchPanePty(p);
    p.initPty();
}

The for loop that captures default_pane runs after the first 16 server.run(1) iterations (line 193–195). During those 16 poll cycles, a connected client could issue a command that destroys the session (e.g. kill-session), freeing the pane. The captured default_pane pointer would then be dangling. Unlike handlePtyEvent which calls isPaneValid, this code path has no validation before dereferencing the pointer for spawn, watchPanePty, and initPty.

Fix: Re-validate that the pane still exists (walk sessions → windows → panes) immediately before the spawn call, or capture the pane after the server.run bursts.