diff options
| -rw-r--r-- | src/9p_io.zig | 49 | ||||
| -rw-r--r-- | src/main.zig | 11 |
2 files changed, 42 insertions, 18 deletions
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, `<path>.<pid>` (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, `<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 { +/// gets one, 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, 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 }); diff --git a/src/main.zig b/src/main.zig index b6f57873..c36c4eb7 100644 --- a/src/main.zig +++ b/src/main.zig @@ -85,7 +85,8 @@ const help_text = \\ --9p=<name> name the default 9P socket. Without this flag, \\ the name is the detached session name or pid. \\ The socket is under $XDG_RUNTIME_DIR, falling - \\ back to ~/.local/state/pardes. + \\ back to ~/.local/state/pardes: a directory + \\ of the user's alone (mode 700), or none. \\ --9p-tcp=<dial> also serve 9P at tcp!<numeric-ip>!<port>. \\ --mount=<name>=<dial> mount a 9P session or Unix/TCP endpoint under /n/<name>. \\ Repeat for more mounts; os and self are reserved. @@ -746,11 +747,11 @@ fn nativeMain(std_init: std.process.Init) !void { // Said where it was started (stderr, as the log says it): no pane // anyone sees would show it. The session runs on, with no 9P. var pid_buf: [16]u8 = undefined; - var words_buf: [160]u8 = undefined; + var words_buf: [256]u8 = undefined; const entry = if (opts.ninep_name.len != 0) opts.ninep_name else std.fmt.bufPrint(&pid_buf, "{d}", .{std.c.getpid()}) catch unreachable; // a pid fits 16 - if (ninep_io.tooLong(entry, &words_buf)) |words| { - var line_buf: [200]u8 = undefined; - std.Io.File.stderr().writeStreamingAll(init.io, std.fmt.bufPrint(&line_buf, "pardes: {s}\n", .{words}) catch unreachable) catch {}; // 160 fits 200 + if (ninep_io.noSocket(entry, &words_buf)) |words| { + var line_buf: [300]u8 = undefined; + std.Io.File.stderr().writeStreamingAll(init.io, std.fmt.bufPrint(&line_buf, "pardes: {s}\n", .{words}) catch unreachable) catch {}; // 256 fits 300 } // Nobody's size yet: one roomy enough for a script's panes; `size` // sets another, and an attached terminal or window its own. |
