From 24b8178d7ba0db8b0ec38769f859e9f47afbfa62 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sun, 8 Nov 2026 21:13:29 -0300 Subject: fix: a session whose runtime directory was not the user's alone ran with no 9P socket and no word, which read as a detached session parked before its listener (a bash recipe's mkdir -m 700 -p dir/sub leaves dir at the umask's 755; smoke.py chmods it): the socket check (9p_io.noSocket, the long path's too) says no socket: must be a directory of yours alone (mode 755, owner 1000): chmod 700 it, to the log, pane 0's message and a --detach start's stderr, and --9p's help says the directory must be mode 700; a unit test opens the directory to others Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/9p_io.zig | 49 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 13 deletions(-) (limited to 'src/9p_io.zig') diff --git a/src/9p_io.zig b/src/9p_io.zig index 0ba5cba3..62c38521 100644 --- a/src/9p_io.zig +++ b/src/9p_io.zig @@ -871,7 +871,7 @@ pub fn listen(io: std.Io, gpa: std.mem.Allocator, core: *pardes.Pardes, named: [ // memory. The name the socket is first bound at, `.` (it is // renamed into place once it listens), must fit too. refusal = ""; - if (tooLong(entry_name, &refusal_buf)) |words| { + if (noSocket(entry_name, &refusal_buf)) |words| { log.warn("{s}", .{words}); refusal = words; return error.NameTooLong; @@ -957,23 +957,27 @@ pub fn listen(io: std.Io, gpa: std.mem.Allocator, core: *pardes.Pardes, named: [ return l; } -/// `tooLong`'s words, kept for `start`'s message. -var refusal_buf: [160]u8 = undefined; +/// `noSocket`'s words, kept for `start`'s message. +var refusal_buf: [256]u8 = undefined; var refusal: []const u8 = ""; /// Why a session named `name` gets no socket, in words in `buf` (null: it -/// fits, or there is no directory, said where it is looked for): the name -/// it is first bound at, `/pardes-9p-.sock.` and its -/// 0, must fit sun_path. Asked at a detached start too, whose stderr is -/// the only place anyone reads it. -pub fn tooLong(name: []const u8, buf: []u8) ?[]const u8 { +/// gets one, or there is no directory, said where it is looked for): the +/// name it is first bound at, `/pardes-9p-.sock.` and +/// its 0, must fit sun_path, and the directory (made here when missing) +/// must be the user's alone. Asked at a detached start too, whose stderr +/// is the only place anyone reads it. +pub fn noSocket(name: []const u8, buf: []u8) ?[]const u8 { var dir_buf: [4096]u8 = undefined; const dir = socketDir(&dir_buf) orelse return null; var digits: [16]u8 = undefined; const pid = std.fmt.bufPrint(&digits, "{d}", .{libc.getpid()}) catch unreachable; // a pid fits 16 const n = dir.len + "/".len + prefix.len + name.len + ".sock.".len + pid.len + 1; - if (n <= sun_path_len) return null; - return std.fmt.bufPrint(buf, "9p: the socket path is {d} bytes, {d} is the most: set XDG_RUNTIME_DIR shorter", .{ n, sun_path_len }) catch unreachable; // two numbers fit 160 + if (n > sun_path_len) + return std.fmt.bufPrint(buf, "9p: the socket path is {d} bytes, {d} is the most: set XDG_RUNTIME_DIR shorter", .{ n, sun_path_len }) catch unreachable; // two numbers fit 256 + if (ensureSocketDir(dir)) return null; + const facts = statNoFollow(dir) orelse return std.fmt.bufPrint(buf, "9p: no socket: {s} is not there and could not be made", .{dir[0..@min(dir.len, 120)]}) catch unreachable; // 120 bytes of path fit 256 + return std.fmt.bufPrint(buf, "9p: no socket: {s} must be a directory of yours alone (mode {o}, owner {d}): chmod 700 it", .{ dir[0..@min(dir.len, 120)], facts.mode & 0o7777, facts.uid }) catch unreachable; // 120 bytes of path and three numbers fit 256 } /// Whether a live session listens under `name` already: a second @@ -1282,15 +1286,15 @@ test "a runtime directory that makes the socket's path pass sun_path is refused var rt: TempRuntime = .{}; try rt.init("words"); defer rt.deinit(); - var buf: [160]u8 = undefined; - try testing.expect(tooLong("x", &buf) == null); + var buf: [256]u8 = undefined; + try testing.expect(noSocket("x", &buf) == null); // A directory of 91 bytes (the ledger's was 83, a pid's digits from the // edge): with a 1-byte name and a pid the socket's path passes sun_path. var long_buf: [128:0]u8 = undefined; const long = try std.fmt.bufPrintSentinel(&long_buf, "{s}/{s}", .{ std.mem.sliceTo(&rt.directory, 0), &@as([66]u8, @splat('d')) }, 0); try testing.expectEqual(@as(usize, 91), long.len); try testing.expectEqual(@as(c_int, 0), setenv("XDG_RUNTIME_DIR", long, 1)); - const words = tooLong("x", &buf) orelse return error.NotRefused; + const words = noSocket("x", &buf) orelse return error.NotRefused; try testing.expect(std.mem.startsWith(u8, words, "9p: the socket path is ")); try testing.expect(std.mem.endsWith(u8, words, " bytes, 108 is the most: set XDG_RUNTIME_DIR shorter")); // The session's own words, where its pane shows them; no listener. @@ -1299,6 +1303,25 @@ test "a runtime directory that makes the socket's path pass sun_path is refused try testing.expect(std.mem.startsWith(u8, pane.msg[0..pane.msg_len], "9p: the socket path is ")); } +test "a runtime directory open to others is refused in words, never a silent session with no socket" { + if (comptime !supported) return error.SkipZigTest; + const p = try pardes.Pardes.init(testing.allocator, .{ .tty_only = true }); + defer p.deinit(); + var rt: TempRuntime = .{}; + try rt.init("open"); + defer rt.deinit(); + // `mkdir -m 700 -p dir/sub` leaves dir itself at the umask's 755. + try testing.expectEqual(@as(c_int, 0), libc.chmod(&rt.directory, 0o755)); + var buf: [256]u8 = undefined; + const words = noSocket("x", &buf) orelse return error.NotRefused; + try testing.expect(std.mem.indexOf(u8, words, "must be a directory of yours alone (mode 755") != null); + try testing.expect(start(testing.io, testing.allocator, p) == null); + const pane = p.panes[0].?; + try testing.expect(std.mem.indexOf(u8, pane.msg[0..pane.msg_len], "chmod 700 it") != null); + try testing.expectEqual(@as(c_int, 0), libc.chmod(&rt.directory, 0o700)); + try testing.expect(noSocket("x", &buf) == null); +} + test "a socket path too long for sun_path is no listener, and leaves the editor's turn as it was" { if (comptime !supported) return error.SkipZigTest; const p = try pardes.Pardes.init(testing.allocator, .{ .tty_only = true }); -- cgit v1.3