diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-28 21:54:47 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-28 21:54:47 -0300 |
| commit | 30b7fd64b4b819abafd293f4ef45bf8ece3082e2 (patch) | |
| tree | 21646492843985edecd18170816cf73dd698aba3 | |
| parent | 3ad8a8117435124d5df8c1d70de62c32dd97e113 (diff) | |
| download | pardes-30b7fd64b4b819abafd293f4ef45bf8ece3082e2.tar.gz pardes-30b7fd64b4b819abafd293f4ef45bf8ece3082e2.zip | |
kitty media: a FIFO, device or huge file named by t=f, t=t or t=s no longer hangs pardes or swells it (ghostty's loader patched at build time)
A program in a pane that named a FIFO (`mkfifo /tmp/f`, then t=f, t=t, or t=s through /dev/shm) hung the whole editor: ghostty's loader opens the path and only then checks that it is a regular file, and an open for reading waits for a writer. A device was opened too before being refused (an open may do something), and any image file up to ghostty's 400 MB was read and held before a screen's 64 MiB storage could refuse it.
build.zig's patchGhosttyKitty patches src/terminal/kitty/graphics_image.zig with count-checked anchors, as vaxis and mvzr are: on Linux a path is probed with O_PATH (which opens nothing), must be a regular file, and that same inode is reopened through /proc/self/fd; shared memory must be a regular file (stat of /dev/shm/<name> first, then shm_open non-blocking and fstat); max_size is TerminalImages.storage_limit. Which paths are accepted stays ghostty's (kitty's) policy, which the user accepts.
Tests: a FIFO by each medium and through a /dev/shm symlink does not block (a watchdog thread opens it for writing after 1.5 s and the test fails if it had to); /proc, /sys, devices and directories are refused; t=t leaves a tty-graphics-protocol file outside the temp directory, and one behind a /tmp symlink; a 100 MiB file is refused.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
| -rw-r--r-- | build.zig | 75 | ||||
| -rw-r--r-- | test/kitty.zig | 144 |
2 files changed, 219 insertions, 0 deletions
@@ -906,6 +906,7 @@ pub fn build(b: *std.Build) void { const ghostty_vt = dep.module("ghostty-vt"); ghostty_vt_for_snap = ghostty_vt; ghostty_vt.addImport("uucode", uucode_mod); + patchGhosttyKitty(b, io, dep, ghostty_vt); ghostty_core_vt = ghostty_vt; } // override uucode on vaxis LAST so it wins over ghostty's build() wiring. @@ -1956,6 +1957,80 @@ fn installHome(b: *std.Build, artifact: *std.Build.Step.Compile, home_bin: ?[]co /// tracked one by one, since the configure runs again on every build anyway /// (findProgram and gitCommit poison its cache). // ponytail: untracked repo reads; b.dependOnFileContents each one if the configure cache ever stays pure. +/// ghostty's kitty graphics loader (src/terminal/kitty/graphics_image.zig), +/// patched at build time so a fresh fetch keeps it, for what a program in a +/// pane may name with t=f, t=t and t=s (test/kitty.zig): +/// - a path is opened for reading only once it is known to be a regular +/// file: ghostty opens first and checks after, and the open of a FIFO +/// waits for a writer (pardes hung) while a device's open may do +/// something. On Linux an O_PATH probe looks, which opens nothing, and +/// the same inode is reopened through /proc/self/fd. +/// - shared memory is opened non-blocking and must be a regular file, so +/// a FIFO in /dev/shm (or a symlink there to one) neither hangs nor maps. +/// - an image may be no larger than a screen's storage +/// (TerminalImages.storage_limit) rather than ghostty's 400 MB, which a +/// program could make pardes read and hold before refusing it. +fn patchGhosttyKitty(b: *std.Build, io: std.Io, dep: *std.Build.Dependency, vt: *std.Build.Module) void { + const rel = "src/terminal/kitty/graphics_image.zig"; + var src = configureRead(b, io, dep.builder.root, rel, 1 << 22) catch @panic("read ghostty's graphics_image.zig"); + const edits = [_][2][]const u8{ + .{ + "const max_size = 400 * 1024 * 1024; // 400MB", + "const max_size = 64 * 1024 * 1024; // pardes (build.zig): a screen's storage, TerminalImages.storage_limit", + }, + .{ + \\ var file = std.Io.Dir.cwd().openFile( + \\ io, + \\ path, + \\ .{}, + \\ ) catch |err| { + , + \\ var file = pardesOpenRegular(io, path) catch |err| { + }, + .{ + " const fd = std.c.shm_open(pathz, @as(c_int, @bitCast(std.c.O{ .ACCMODE = .RDONLY })), @as(u16, 0));", + \\ // pardes (build.zig): a FIFO there must not hang the open, and + \\ // on Linux nothing but a regular file is opened at all. + \\ if (comptime builtin.target.os.tag == .linux) if (path.len > 0 and path[0] == '/') { + \\ var dev: [std.Io.Dir.max_path_bytes]u8 = undefined; + \\ const full = std.fmt.bufPrint(&dev, "/dev/shm{s}", .{path}) catch return error.InvalidData; + \\ const shm_stat = std.Io.Dir.cwd().statFile(io, full, .{}) catch return error.InvalidData; + \\ if (shm_stat.kind != .file) return error.InvalidData; + \\ }; + \\ const fd = std.c.shm_open(pathz, @as(c_int, @bitCast(std.c.O{ .ACCMODE = .RDONLY, .NONBLOCK = true })), @as(u16, 0)); + }, + .{ + " if (stat.size <= 0) return error.InvalidData;", + " if (stat.kind != .file) return error.InvalidData; // pardes (build.zig)\n if (stat.size <= 0) return error.InvalidData;", + }, + }; + for (edits) |edit| { + if (std.mem.count(u8, src, edit[0]) != 1) std.debug.panic("ghostty's {s} changed at `{s}`: redo its patch in build.zig", .{ rel, edit[0] }); + src = std.mem.replaceOwned(u8, b.allocator, src, edit[0], edit[1]) catch @panic("OOM"); + } + const helper = + \\ + \\/// pardes (build.zig): `path`, opened for reading only once it is known + \\/// to be a regular file. Elsewhere than Linux, ghostty's open as it was. + \\fn pardesOpenRegular(io: std.Io, path: []const u8) !std.Io.File { + \\ if (comptime builtin.target.os.tag != .linux) return std.Io.Dir.cwd().openFile(io, path, .{}); + \\ const probe = try std.Io.Dir.cwd().openFile(io, path, .{ .path_only = true }); + \\ defer probe.close(io); + \\ const stat = try probe.stat(io); + \\ if (stat.kind != .file) return error.NotRegularFile; + \\ var buf: [32]u8 = undefined; + \\ const same = std.fmt.bufPrint(&buf, "/proc/self/fd/{d}", .{probe.handle}) catch unreachable; + \\ return std.Io.Dir.cwd().openFile(io, same, .{}); + \\} + \\ + ; + const files = b.addWriteFiles(); + // The rest of src as it is; fonts are the app's, not the library's. + _ = files.addCopyDirectory(dep.path("src"), "", .{ .exclude_extensions = &.{ "graphics_image.zig", ".ttf", ".otf" } }); + _ = files.add("terminal/kitty/graphics_image.zig", b.fmt("{s}{s}", .{ src, helper })); + vt.root_source_file = files.getDirectory().path(b, "lib_vt.zig"); +} + fn configureRead(b: *std.Build, io: std.Io, root: std.Build.Cache.Path, sub: []const u8, limit: usize) ![]u8 { return root.root_dir.handle.readFileAlloc(io, b.pathJoin(&.{ root.subPathOrDot(), sub }), b.allocator, .limited(limit)); } diff --git a/test/kitty.zig b/test/kitty.zig index 3f9776fa..e760e8d6 100644 --- a/test/kitty.zig +++ b/test/kitty.zig @@ -776,3 +776,147 @@ test "kitty: a placement relative to another (P=, Q=) is drawn at its parent's c r.feed(0, apc("a=d,d=i,i=80,p=1", "")); try std.testing.expectEqual(@as(usize, 0), (try r.placesOf(80)).len); } + +// ---- What t=f, t=t and t=s may make pardes open ---- +// +// A program in a pane names paths on pardes's machine. Which paths ghostty's +// loader accepts is its policy (kitty's, which the user accepts); what pardes +// owns is that none of them hangs or crashes it or blows its memory: a FIFO +// (an open for reading waits for a writer), a device (an open may do +// something), a file no screen could hold. And t=t deletes temporary files +// only. + +extern "c" fn mkfifo(path: [*:0]const u8, mode: std.c.mode_t) c_int; +extern "c" fn symlink(target: [*:0]const u8, link: [*:0]const u8) c_int; +extern "c" fn usleep(us: c_uint) c_int; +extern "c" fn ftruncate(fd: c_int, len: i64) c_int; +extern "c" fn getcwd(buf: [*]u8, size: usize) ?[*:0]u8; + +const Guard = struct { + path: [:0]const u8, + done: std.atomic.Value(bool) = .init(false), + blocked: std.atomic.Value(bool) = .init(false), + + /// After 1.5 s still running, the feed is stuck in an open of the FIFO: + /// open it for writing, which lets that open return. + fn run(g: *Guard) void { + for (0..150) |_| { + if (g.done.load(.acquire)) return; + _ = usleep(10_000); + } + g.blocked.store(true, .release); + const fd = std.c.open(g.path, .{ .ACCMODE = .WRONLY }, @as(std.c.mode_t, 0)); + if (fd >= 0) _ = std.c.close(fd); + } +}; + +/// Feeds `bytes`; whether the feed blocked opening `fifo`. +fn feedBlocks(r: *Rig, bytes: []const u8, fifo: [:0]const u8) !bool { + var g: Guard = .{ .path = fifo }; + const t = try std.Thread.spawn(.{}, Guard.run, .{&g}); + r.feed(0, bytes); + g.done.store(true, .release); + t.join(); + return g.blocked.load(.acquire); +} + +test "kitty: a FIFO named by t=f, t=t or t=s never blocks pardes" { + var r = try Rig.init(1); + defer r.deinit(); + const arena = r.frame.allocator(); + const pid = std.c.getpid(); + const Case = struct { medium: u8, fifo: [:0]const u8, name: []const u8 }; + const file = try std.fmt.allocPrintSentinel(arena, "/tmp/pardes-kitty-{d}-fifo", .{pid}, 0); + const temp = try std.fmt.allocPrintSentinel(arena, "/tmp/tty-graphics-protocol-pardes-{d}-fifo", .{pid}, 0); + const shm_path = try std.fmt.allocPrintSentinel(arena, "/dev/shm/pardes-kitty-{d}-fifo", .{pid}, 0); + const cases = [_]Case{ + .{ .medium = 'f', .fifo = file, .name = file }, + .{ .medium = 't', .fifo = temp, .name = temp }, + .{ .medium = 's', .fifo = shm_path, .name = shm_path["/dev/shm".len..] }, + }; + for (cases) |c| { + _ = std.c.unlink(c.fifo); + if (mkfifo(c.fifo, 0o600) != 0) return error.MkfifoFailed; + defer _ = std.c.unlink(c.fifo); + const cmd = try std.fmt.allocPrint(arena, "\x1b_Ga=t,t={c},i=90,f=24,s=1,v=1;{s}\x1b\\", .{ c.medium, try b64(arena, c.name) }); + const blocked = try feedBlocks(&r, cmd, c.fifo); + if (blocked) std.debug.print("t={c}: pardes blocked opening the FIFO {s}\n", .{ c.medium, c.fifo }); + try std.testing.expect(!blocked); + try std.testing.expect(std.mem.startsWith(u8, try r.take(0), "\x1b_Gi=90;E")); + try std.testing.expect(!r.hasImage(0, 90)); + } +} + +test "kitty: t=s through a /dev/shm symlink to a FIFO does not block pardes either" { + var r = try Rig.init(1); + defer r.deinit(); + const arena = r.frame.allocator(); + const pid = std.c.getpid(); + const fifo = try std.fmt.allocPrintSentinel(arena, "/tmp/pardes-kitty-{d}-shmfifo", .{pid}, 0); + _ = std.c.unlink(fifo); + if (mkfifo(fifo, 0o600) != 0) return error.MkfifoFailed; + defer _ = std.c.unlink(fifo); + const link = try std.fmt.allocPrintSentinel(arena, "/dev/shm/pardes-kitty-{d}-link", .{pid}, 0); + _ = std.c.unlink(link); + if (symlink(fifo, link) != 0) return error.SymlinkFailed; + defer _ = std.c.unlink(link); + const cmd = try std.fmt.allocPrint(arena, "\x1b_Ga=t,t=s,i=91,f=24,s=1,v=1;{s}\x1b\\", .{try b64(arena, link["/dev/shm".len..])}); + try std.testing.expect(!try feedBlocks(&r, cmd, fifo)); + try std.testing.expect(std.mem.startsWith(u8, try r.take(0), "\x1b_Gi=91;E")); +} + +test "kitty: t=f and t=t refuse /proc, /sys, devices and directories" { + var r = try Rig.init(1); + defer r.deinit(); + const arena = r.frame.allocator(); + const refused = [_][]const u8{ "/proc/self/status", "/sys/devices/system/cpu/online", "/dev/null", "/dev/zero", "/dev/urandom", "/tmp", "/" }; + for ([_]u8{ 'f', 't' }) |medium| for (refused) |path| { + // A raw 1x1 pixel needs 3 bytes (S=3): any readable file would do. + r.feed(0, try std.fmt.allocPrint(arena, "\x1b_Ga=t,t={c},i=92,f=24,s=1,v=1,S=3;{s}\x1b\\", .{ medium, try b64(arena, path) })); + try std.testing.expect(std.mem.startsWith(u8, try r.take(0), "\x1b_Gi=92;E")); + try std.testing.expect(!r.hasImage(0, 92)); + }; +} + +test "kitty: t=t deletes only a temporary file: one outside the temp directory, or behind a symlink, stays" { + var r = try Rig.init(1); + defer r.deinit(); + const arena = r.frame.allocator(); + const pid = std.c.getpid(); + var cwd_buf: [4096]u8 = undefined; + const cwd = std.mem.span(getcwd(&cwd_buf, cwd_buf.len) orelse return error.NoCwd); + // Outside /tmp, /dev/shm and $TMPDIR: the build's own cache. + const dir = try std.fmt.allocPrintSentinel(arena, "{s}/.zig-cache/tmp/pardes-kitty-{d}", .{ cwd, pid }, 0); + _ = std.c.mkdir(dir, 0o700); + defer _ = std.c.rmdir(dir); + const kept = try std.fmt.allocPrintSentinel(arena, "{s}/tty-graphics-protocol-kept.rgb", .{dir}, 0); + try writeTemp(kept, &rgb_raw); + defer _ = std.c.unlink(kept); + const link = try std.fmt.allocPrintSentinel(arena, "/tmp/tty-graphics-protocol-pardes-{d}-link", .{pid}, 0); + _ = std.c.unlink(link); + if (symlink(kept, link) != 0) return error.SymlinkFailed; + defer _ = std.c.unlink(link); + for ([_][:0]const u8{ kept, link }) |path| { + r.feed(0, try std.fmt.allocPrint(arena, "\x1b_Ga=t,t=t,i=93,f=24,s=4,v=2;{s}\x1b\\", .{try b64(arena, path)})); + try std.testing.expect(std.mem.startsWith(u8, try r.take(0), "\x1b_Gi=93;E")); + try std.testing.expect(!r.hasImage(0, 93)); + try std.testing.expect(exists(kept)); + } +} + +test "kitty: a file bigger than a screen may hold is refused, not read whole" { + var r = try Rig.init(1); + defer r.deinit(); + const arena = r.frame.allocator(); + // 100 MiB, sparse: 5120x5120 RGBA, past the 64 MiB a screen holds. + const big = try std.fmt.allocPrintSentinel(arena, "/tmp/pardes-kitty-{d}-big.rgba", .{std.c.getpid()}, 0); + const fd = std.c.open(big, .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true }, @as(std.c.mode_t, 0o600)); + if (fd < 0) return error.OpenFailed; + _ = ftruncate(fd, 5120 * 5120 * 4); + _ = std.c.close(fd); + defer _ = std.c.unlink(big); + r.feed(0, try std.fmt.allocPrint(arena, "\x1b_Ga=t,t=f,i=94,f=32,s=5120,v=5120;{s}\x1b\\", .{try b64(arena, big)})); + const got = try r.take(0); + try std.testing.expect(std.mem.startsWith(u8, got, "\x1b_Gi=94;E")); + try std.testing.expect(!r.hasImage(0, 94)); +} |
