From 30b7fd64b4b819abafd293f4ef45bf8ece3082e2 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 28 Oct 2026 21:54:47 -0300 Subject: 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/ 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 --- build.zig | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) (limited to 'build.zig') diff --git a/build.zig b/build.zig index 2f1c45e1..034d7284 100644 --- a/build.zig +++ b/build.zig @@ -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)); } -- cgit v1.3