processInput reads freed MessageReader.buf after a detach bindingSeverity: HIGH
Status: ✅ FIXED
Files: src/server/server.zig:2496, src/server/server.zig:1135, src/server/server.zig:1437, src/server/message_reader.zig:43
Verification status: confirmed by reading live source during the fix.
MessageReader.bufis an inline[8192]u8(message_reader.zig:16) andtryParsereturnsdata = self.buf[5..len](:43), so the packet body does alias the heap reader’s storage.removeClientdestroys that reader (server.zig:2669). The only remaining question was reachability, and the detach path inexecuteActionis plainly reachable fromprocessInput.
MessageReader.buf is an inline [8192]u8 field of a heap-allocated
MessageReader (message_reader.zig:43), and tryParse returns a Packet
whose data slice points into it.
The stdin_data handler forwards that slice straight to the parser:
// src/server/server.zig:2592-2597
.stdin_data => {
self.current_client_fd = fd;
self.processInput(pkt.data) catch |err| { ... };
},
processInput then indexes the buffer per byte:
// src/server/server.zig:1437
const byte = buf[i];
and only re-checks the client at the end of handleClient. If a keybinding
mapped to .detach fires inside the same packet (e.g. C-b d), the action
reaches removeClient(cfd) (:1135), which destroys the reader:
self.allocator.destroy(entry.value); // server.zig:2673
processInput then continues its while (i < buf.len) loop and reads freed
memory for every remaining byte of the packet.
The existing test sends only the two-byte "\x02d", so i == buf.len
immediately after detach and the use-after-free is never exercised.
The packet body is copied into server-owned storage once per packet, before any handler runs:
// src/server/server.zig — handleClient
while (try reader.tryParse()) |pkt| {
// bug #521: pkt.data aliases the MessageReader's inline buffer, and a
// handler reached from this packet can remove this client, freeing the
// reader while the body is still being read — a detach keybinding inside
// the same stdin packet is the easy case. Work on a server-owned copy so
// no handler can read freed memory.
const data = try self.stablePacketBody(pkt.data);
...
}
stablePacketBody appends into a reusable Server.packet_scratch buffer
(clearRetainingCapacity + appendSlice), so after warmup the copy costs no
allocation. Every use of the body inside the loop — dispatchCommand,
processInput, the resize and cell_size arms — now reads data instead of
pkt.data.
Fixing this at the copy site rather than inside processInput also closes the
same hazard for the .command arm, where dispatchCommand parses the body and
the command it runs may be detach-client.
The existing post-handler guards (if (!self.client_readers.contains(fd)) return;
— bugs #428, #443, #448) are retained; they cover the reader.consume(pkt) and
response-write paths, which this fix does not touch.
packet body is copied out of the reader before dispatch — bug #521
(src/server/server.zig) builds a stdin_data packet in a heap MessageReader,
asserts the copy lives at a different address from pkt.data, then destroys
the reader and asserts the copy still reads back the original bytes — which is
exactly the sequence a mid-packet detach produces.
Verified with the full suite on Zig 0.16.0 and 0.17.0: 1045/1045 passing on both.
#428 fixed the deferred reader.consume(pkt) dereference after a handler freed
the reader. This is the same root cause reaching a different read site: the
bytes processInput walks for the remainder of the packet. The fix for #428
(the post-handler client_readers.contains(fd) guard) cannot cover it, because
the read happens inside the handler.