diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-08 20:44:43 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-08 21:48:19 -0300 |
| commit | c35251904681f3a195a6ede71dba235463b0e962 (patch) | |
| tree | c9bb7e3c2675c326bd3f94f770951e0c99e228be /src/9p_io.zig | |
| parent | fb2b69bdbbf3cf33706989635ca3416b97667ea7 (diff) | |
| download | pardes-c35251904681f3a195a6ede71dba235463b0e962.tar.gz pardes-c35251904681f3a195a6ede71dba235463b0e962.zip | |
fix: a session whose runtime directory makes its 9P socket's path pass sun_path said so only to std.log, off by default, so a detached one ran with no socket and no word: the check (9p_io.tooLong) gives the words, 9p: the socket path is N bytes, 108 is the most: set XDG_RUNTIME_DIR shorter, to the log, to pane 0's message and, at a --detach start, to stderr, and the session runs on; a unit test pins the check and the message
Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src/9p_io.zig')
| -rw-r--r-- | src/9p_io.zig | 71 |
1 files changed, 59 insertions, 12 deletions
diff --git a/src/9p_io.zig b/src/9p_io.zig index 75f55296..0ba5cba3 100644 --- a/src/9p_io.zig +++ b/src/9p_io.zig @@ -23,7 +23,7 @@ pub fn setCloexec(fd: c_int) void { _ = libc.fcntl(fd, libc.F.SETFD, @as(c_int, 1)); } -pub fn socketDir(buf: *[sun_path_len:0]u8) ?[:0]const u8 { +pub fn socketDir(buf: []u8) ?[:0]const u8 { if (libc.getenv("XDG_RUNTIME_DIR")) |path| return std.fmt.bufPrintSentinel(buf, "{s}", .{std.mem.span(path)}, 0) catch null; const home = libc.getenv("HOME") orelse return null; @@ -864,6 +864,18 @@ pub fn socketPath(buf: *[sun_path_len]u8, dir: []const u8, name: []const u8) ?[: /// (`pardes.turn`) and must give it up while it waits. pub fn listen(io: std.Io, gpa: std.mem.Allocator, core: *pardes.Pardes, named: []const u8, fallback: []const u8, tcp_dial: ?[]const u8) !*Listener { if (comptime !supported) return error.Unsupported; + const entry_name = if (named.len != 0) named else fallback; + // Checked before anything is made: the turn's hooks point at the + // listener from here on, and a path too long for sun_path used to free + // it with them still set, so the editor's next rest called into freed + // memory. The name the socket is first bound at, `<path>.<pid>` (it is + // renamed into place once it listens), must fit too. + refusal = ""; + if (tooLong(entry_name, &refusal_buf)) |words| { + log.warn("{s}", .{words}); + refusal = words; + return error.NameTooLong; + } var dir_buf: [sun_path_len:0]u8 = undefined; const dir = socketDir(&dir_buf) orelse { log.warn("no runtime directory for the socket", .{}); @@ -872,17 +884,9 @@ pub fn listen(io: std.Io, gpa: std.mem.Allocator, core: *pardes.Pardes, named: [ if (!ensureSocketDir(dir)) return error.NoRuntimeDir; // What dead editors left behind goes first, never a live one's. sweepDeadSockets(io, dir); - const entry_name = if (named.len != 0) named else fallback; - // Checked before anything is made: the turn's hooks point at the - // listener from here on, and a path too long for sun_path used to free - // it with them still set, so the editor's next rest called into freed - // memory. The name the socket is first bound at, `<path>.<pid>` (it is - // renamed into place once it listens), must fit too. - var path_check: [sun_path_len]u8 = undefined; var tmp_buf: [sun_path_len]u8 = undefined; - const check = socketPath(&path_check, dir, entry_name); - if (check == null or (std.fmt.bufPrintSentinel(&tmp_buf, "{s}.{d}", .{ check.?, libc.getpid() }, 0) catch null) == null) { - log.warn("no socket for {s}: the path is too long for a unix socket", .{entry_name}); + if (std.mem.indexOfAny(u8, entry_name, "/\x00") != null or entry_name.len == 0) { + log.warn("no socket for {s}: not one path component", .{entry_name}); return error.NameTooLong; } const l = try gpa.create(Listener); @@ -953,6 +957,25 @@ 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; +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, `<socketDir>/pardes-9p-<name>.sock.<pid>` 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 { + 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 +} + /// Whether a live session listens under `name` already: a second /// `--detach=<name>` is refused before anything is made. pub fn taken(name: []const u8) bool { @@ -1252,6 +1275,30 @@ const TempRuntime = struct { } }; +test "a runtime directory that makes the socket's path pass sun_path is refused in words, the editor running on" { + 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("words"); + defer rt.deinit(); + var buf: [160]u8 = undefined; + try testing.expect(tooLong("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; + 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. + try testing.expect(start(testing.io, testing.allocator, p) == null); + const pane = p.panes[0].?; + try testing.expect(std.mem.startsWith(u8, pane.msg[0..pane.msg_len], "9p: the socket path is ")); +} + 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 }); @@ -1712,7 +1759,7 @@ pub fn start(io: std.Io, gpa: std.mem.Allocator, core: *pardes.Pardes) ?*Listene // unreachable: a u32 is at most 10 digits const fallback = std.fmt.bufPrint(&name, "{d}", .{@as(u32, @intCast(libc.getpid()))}) catch unreachable; return listen(io, gpa, core, core.opts.ninep_name, fallback, core.opts.ninep_tcp) catch { - core.reportError(0, "9p listener", error.ListenFailed); + if (refusal.len > 0) core.setMessage(0, refusal) else core.reportError(0, "9p listener", error.ListenFailed); return null; }; } |
