szn

Bug 449 — killSession / killAllSessions destroy panes without clearing pane.valid

449. isPaneValid can alias an arena-reused address; teardown ordering is load-bearing

Severity: LOW Status: Fixed

Symptoms

None observed. This is a defensive-hardening gap in the stale-pane defence (bug #283/#362): the safety of every cached *Pane after a session kill rests on an ordering convention, not on an explicit invalidation, and the validator itself is an address comparison that cannot detect arena reuse.


Root Cause Analysis

isPaneValid (server.zig:717-726) walks all live sessions/windows/panes and returns p.valid only when p == pane. Two consequences:

  1. valid is only cleared on two teardown paths. Session.killWindow (session.zig:130) and the removePane branch of destroyPane (server.zig:948) set pane.valid = false before deinit. killSession (server.zig:3242-3263) and killAllSessions (server.zig:3265-3281) destroy panes without clearing it — they rely on the pane leaving the live lists, plus clearPaneMouseRefsBulk for the cached mouse pointers.
  2. The check is an address comparison. If the freed pane’s address is later reused by a new Pane (arena reuse), a stale cached pointer compares equal to the new live pane and isPaneValid returns true — blessing a pointer to the wrong object. handlePtyEvent would then feedPty stale udata into the wrong pane (or worse, if the pty fd was also recycled).

What keeps this safe today is the ordering inside killSession/killAllSessions: the pty master fds are removed from the loop (server.zig:3247-3253, 3266-3273) before deinit, so no subsequent poll can deliver an event carrying the freed pane’s udata. That ordering is load-bearing but not enforced by any type or assertion.


Solution

  1. Set pane.valid = false for every pane in killSession/killAllSessions before teardown (matching Session.killWindow), so isPaneValid returns false for freed panes even if the address is reused.
  2. Optionally make isPaneValid fail closed (return false) instead of trusting p.valid when the comparison matches a freshly reused address — though this requires a generation/epoch counter per pane to be robust.
  3. Add a regression test that kills a session and verifies a stale cached *Pane is not treated as live after arena reuse.

Fixed: