diff --git a/CHANGELOG.md b/CHANGELOG.md index 2af5e59..f7b28c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,19 @@ # Changelog +## Unreleased + +* `state.RootLock.acquire` no longer truncates the file behind a hard link + planted at `.lock`. It opens the state root `O_DIRECTORY|O_NOFOLLOW` and + both lock files relative to it with `O_NOFOLLOW`, creating them mode + 0600; a lock file name that is not a regular file with one link is + refused before it is opened, so a FIFO or symbolic link there is never + followed; and once both locks are held, the gate must still be a regular + file whose one link is `.lock` before it is truncated. +* A refused gate is the new `RootLock.AcquireError.GateNotPrivate` + (`NotPrivate` stays the sidecar's), a symbolic link as the state root is + `NotDir`, and `LockOps.open_sidecar` takes the state root's descriptor + and a name. + ## 0.8.1 * `atomic` reads the umask from `/proc/self/status` instead of setting it to diff --git a/src/state.zig b/src/state.zig index 63877c7..d175b1e 100644 --- a/src/state.zig +++ b/src/state.zig @@ -132,6 +132,12 @@ test "a file that is not private is not read" { /// the gate is held, because closing any descriptor for it drops this /// process's record locks; it is released before the gate. Neither file is /// ever unlinked. +/// +/// Others may be able to write the state root. The root is opened without +/// following a symbolic link as its last component, both files through its +/// descriptor without following one at their names, and each is trusted +/// only as a regular file with one link: a FIFO planted at a name is never +/// opened, and the file behind a planted hard link is never truncated. pub const RootLock = struct { gate: fsx.fd_t, posix: fsx.fd_t, @@ -140,7 +146,17 @@ pub const RootLock = struct { /// parent's lock. owner: linux.pid_t, - pub const AcquireError = Error || error{ Busy, NotPrivate, OutOfMemory }; + pub const AcquireError = Error || error{ + Busy, + /// STATE_ROOT/.lock.posix is not a regular file with one link. + NotPrivate, + /// STATE_ROOT/.lock is not a regular file with one link. + GateNotPrivate, + OutOfMemory, + }; + + const gate_name = ".lock"; + const sidecar_name = ".lock.posix"; fn takeFlock(fd: fsx.fd_t, deadline_ms: ?i64) AcquireError!void { const d = deadline_ms orelse { @@ -187,23 +203,29 @@ pub const RootLock = struct { /// Lock STATE_ROOT/.lock and STATE_ROOT/.lock.posix, waiting up to /// `timeout_ms` (null: no limit). The gate then records this pid. + /// + /// STATE_ROOT is created when absent and opened O_DIRECTORY|O_NOFOLLOW; + /// a symbolic link there is NotDir. Each lock file is opened relative to + /// that descriptor with O_NOFOLLOW, created mode 0600, only when its name + /// is absent or a regular file with one link, and its descriptor must be + /// that file. The gate is checked again once both locks are held, right + /// before it is truncated. Allocates nothing; `gpa` stays for source + /// compatibility. pub fn acquire(gpa: Allocator, state_root: []const u8, timeout_ms: ?u32) AcquireError!RootLock { + _ = gpa; const atomic = @import("atomic.zig"); - try atomic.makedirs(state_root); + const root = withoutTrailingSlashes(state_root); + try atomic.makedirs(root); const deadline: ?i64 = if (timeout_ms) |t| nowMs() + t else null; - const gate_path = try std.fs.path.join(gpa, &.{ state_root, ".lock" }); - defer gpa.free(gate_path); - const posix_path = try gpa.print("{s}.posix", .{gate_path}); - defer gpa.free(posix_path); - const gate = try sys.openat(fsx.cwd, gate_path, .{ .ACCMODE = .RDWR, .CREAT = true, .NOFOLLOW = true }, 0o644); + const dir = try fsx.openDir(fsx.cwd, root, false); + defer sys.close(dir); + const gate = (try openLockFile(dir, gate_name, realOpenLock)) orelse return error.GateNotPrivate; errdefer sys.close(gate); try takeFlock(gate, deadline); - var zb: [sys.path_max:0]u8 = undefined; - const posix: fsx.fd_t = @intCast(try lockCall(lock_ops.open_sidecar, .{(try sys.z(&zb, posix_path)).ptr})); + const posix = (try openLockFile(dir, sidecar_name, lock_ops.open_sidecar)) orelse return error.NotPrivate; errdefer sys.close(posix); - const st = try fsx.fstat(posix); - if (!st.isReg() or st.nlink != 1) return error.NotPrivate; try takeRecord(posix, deadline); + if (!try isSoleLink(dir, gate_name, gate)) return error.GateNotPrivate; _ = try lockCall(lock_ops.ftruncate, .{ gate, @as(i64, 0) }); var buf: [24]u8 = undefined; const line = std.mem.print(&buf, "{d}\n", .{linux.getpid()}) catch unreachable; @@ -211,6 +233,48 @@ pub const RootLock = struct { return .{ .gate = gate, .posix = posix, .owner = linux.getpid() }; } + /// `dir/name` opened read-write through `open`; null unless the name is + /// absent or a regular file with one link before the open, so a FIFO, + /// device or symbolic link there is never opened, and the descriptor is + /// that file after it. + fn openLockFile(dir: fsx.fd_t, name: [:0]const u8, open: *const fn (dir: fsx.fd_t, name: [*:0]const u8) usize) Error!?fsx.fd_t { + if (fsx.statAt(dir, name, false)) |st| { + if (!st.isReg() or st.nlink != 1) return null; + } else |err| switch (err) { + error.NotFound => {}, + else => |e| return e, + } + const fd: fsx.fd_t = @intCast(lockCall(open, .{ dir, name.ptr }) catch |err| switch (err) { + error.SymLink, error.IsDir => return null, + else => |e| return e, + }); + errdefer sys.close(fd); + if (try isSoleLink(dir, name, fd)) return fd; + sys.close(fd); + return null; + } + + /// Whether `fd` is a regular file whose one link is `dir/name`. The name + /// is looked up as well as the descriptor: a planted hard link unlinked + /// while the lock is awaited leaves the descriptor on the other name's + /// file, with one link. + fn isSoleLink(dir: fsx.fd_t, name: []const u8, fd: fsx.fd_t) Error!bool { + const st = try fsx.fstat(fd); + if (!st.isReg() or st.nlink != 1) return false; + const at = fsx.statAt(dir, name, false) catch |err| switch (err) { + error.NotFound => return false, + else => |e| return e, + }; + return at.dev == st.dev and at.ino == st.ino and at.nlink == 1; + } + + /// A trailing slash makes the kernel follow a final symbolic link even + /// under O_NOFOLLOW. "/" stays "/". + fn withoutTrailingSlashes(path: []const u8) []const u8 { + const trimmed = std.mem.trimEnd(u8, path, "/"); + return if (trimmed.len == 0 and path.len != 0) "/" else trimmed; + } + /// Unlock the record lock, close its descriptor, then unlock and close /// the gate. Both descriptors are closed whatever an unlock returns; a /// forked child only closes them. @@ -234,7 +298,7 @@ pub const LockOps = struct { fcntl: *const fn (fd: fsx.fd_t, cmd: i32, arg: usize) usize = realFcntl, ftruncate: *const fn (fd: fsx.fd_t, len: i64) usize = realFtruncate, write: *const fn (fd: fsx.fd_t, bytes: []const u8) usize = realWrite, - open_sidecar: *const fn (path: [*:0]const u8) usize = realOpenSidecar, + open_sidecar: *const fn (dir: fsx.fd_t, name: [*:0]const u8) usize = realOpenLock, }; var test_lock_ops: LockOps = .{}; @@ -253,8 +317,8 @@ fn realFtruncate(fd: fsx.fd_t, len: i64) usize { fn realWrite(fd: fsx.fd_t, bytes: []const u8) usize { return linux.write(fd, bytes.ptr, bytes.len); } -fn realOpenSidecar(path: [*:0]const u8) usize { - return linux.openat(fsx.cwd, path, .{ .ACCMODE = .RDWR, .CREAT = true, .NOFOLLOW = true, .CLOEXEC = true }, 0o644); +fn realOpenLock(dir: fsx.fd_t, name: [*:0]const u8) usize { + return linux.openat(dir, name, .{ .ACCMODE = .RDWR, .CREAT = true, .NOFOLLOW = true, .NONBLOCK = true, .CLOEXEC = true }, 0o600); } /// One of `lock_ops`, retrying on EINTR. diff --git a/src/state_test.zig b/src/state_test.zig index 4ae7665..eaf6a4d 100644 --- a/src/state_test.zig +++ b/src/state_test.zig @@ -37,10 +37,11 @@ fn err(e: linux.E) usize { return @bitCast(-@as(isize, @backingInt(e))); } -fn fdCount() usize { +fn fdCount(gpa: std.mem.Allocator) usize { const fd = fsx.openDir(fsx.cwd, "/proc/self/fd", true) catch unreachable; defer sys.close(fd); - var it = fsx.DirIter.init(A, fd) catch unreachable; + var it = fsx.DirIter.init(gpa, fd) catch unreachable; + defer it.deinit(gpa); var n: usize = 0; while (it.next() catch null) |_| n += 1; return n - 1; // the directory's own descriptor @@ -129,7 +130,7 @@ fn eioFtruncate(_: fsx.fd_t, _: i64) usize { fn eioWrite(_: fsx.fd_t, _: []const u8) usize { return err(.IO); } -fn eioOpen(_: [*:0]const u8) usize { +fn eioOpen(_: fsx.fd_t, _: [*:0]const u8) usize { return err(.IO); } @@ -137,7 +138,7 @@ test "acquisition errors release the descriptors and the gate" { const root = begin(); defer end(root); inline for (.{ "flock", "fcntl", "ftruncate", "write" }) |target| { - const before = fdCount(); + const before = fdCount(A); if (comptime std.mem.eql(u8, target, "flock")) state.lock_ops.flock = eioFlock; if (comptime std.mem.eql(u8, target, "fcntl")) state.lock_ops.fcntl = eioFcntl; if (comptime std.mem.eql(u8, target, "ftruncate")) state.lock_ops.ftruncate = eioFtruncate; @@ -147,7 +148,7 @@ test "acquisition errors release the descriptors and the gate" { return error.TestUnexpectedResult; } else |_| {} state.lock_ops.* = .{}; - try testing.expectEqual(before, fdCount()); + try testing.expectEqual(before, fdCount(A)); try acquireAndRelease(root); } } @@ -181,11 +182,11 @@ test "a release error still closes both layers" { const root = begin(); defer end(root); const holder = try RootLock.acquire(A, root, 0); - const before = fdCount(); + const before = fdCount(A); state.lock_ops.fcntl = eioFcntl; holder.release(); state.lock_ops.* = .{}; - try testing.expectEqual(before - 2, fdCount()); + try testing.expectEqual(before - 2, fdCount(A)); try acquireAndRelease(root); } @@ -214,3 +215,182 @@ test "a timed acquire does not wait forever" { const ms = (t1.sec - t0.sec) * 1000 + @divTrunc(t1.nsec - t0.nsec, 1_000_000); try testing.expect(ms >= 150 and ms < 5000); } + +// ---- files planted at the lock names --------------------------------------- + +/// A fresh state root under .zig-cache; `removeRoot` deletes and frees it. +fn scratchRoot(gpa: std.mem.Allocator) ![]u8 { + state.lock_ops.* = .{}; + var cwd_buf: [sys.path_max]u8 = undefined; + const cwd = try fsx.realpath(fsx.cwd, ".", &cwd_buf); + counter += 1; + const root = try gpa.print("{s}/.zig-cache/lock-test-{d}-{d}", .{ cwd, linux.getpid(), counter }); + errdefer gpa.free(root); + try atomic.makedirs(root); + return root; +} + +fn removeRoot(gpa: std.mem.Allocator, root: []u8) void { + state.lock_ops.* = .{}; + fsx.deleteTree(gpa, fsx.cwd, root) catch {}; + gpa.free(root); +} + +fn createFile(dir: fsx.fd_t, name: []const u8, bytes: []const u8) !void { + const fd = try sys.openat(dir, name, .{ .ACCMODE = .WRONLY, .CREAT = true, .EXCL = true }, 0o600); + defer sys.close(fd); + try sys.writeAll(fd, bytes); +} + +fn expectContents(gpa: std.mem.Allocator, dir: fsx.fd_t, name: []const u8, want: []const u8) !void { + const f = try fsx.openRegular(dir, name, false); + defer sys.close(f.fd); + const got = try fsx.readAll(gpa, f.fd, 4096); + defer gpa.free(got); + try testing.expectEqualStrings(want, got); +} + +/// The opens of one file from `init` on, as inotify reports them. +const OpenWatch = struct { + fd: fsx.fd_t, + + fn init(gpa: std.mem.Allocator, root: []const u8, name: []const u8) !OpenWatch { + const path = try gpa.print("{s}/{s}", .{ root, name }); + defer gpa.free(path); + var zb: [sys.path_max:0]u8 = undefined; + const fd: fsx.fd_t = @intCast(try sys.call(linux.inotify_init1, .{@as(u32, linux.IN.NONBLOCK | linux.IN.CLOEXEC)})); + errdefer sys.close(fd); + _ = try sys.call(linux.inotify_add_watch, .{ fd, (try sys.z(&zb, path)).ptr, @as(u32, linux.IN.OPEN) }); + return .{ .fd = fd }; + } + + fn deinit(w: OpenWatch) void { + sys.close(w.fd); + } + + fn opened(w: OpenWatch) bool { + var buf: [256]u8 align(@alignOf(linux.inotify_event)) = undefined; + return linux.errno(linux.read(w.fd, &buf, buf.len)) == .SUCCESS; + } +}; + +/// The state root the flocks below plant in. +var plant_dir: fsx.fd_t = -1; + +/// A flock that first gives the gate a second link, as can be done while the +/// gate is awaited. +fn linkThenFlock(fd: fsx.fd_t, op: i32) usize { + _ = linux.linkat(plant_dir, ".lock", plant_dir, "alias", 0); + return linux.flock(fd, op); +} + +/// A flock that first moves the gate to another name, where its file keeps +/// one link: what a planted hard link leaves when it is unlinked during the +/// wait. +fn renameThenFlock(fd: fsx.fd_t, op: i32) usize { + _ = linux.renameat(plant_dir, ".lock", plant_dir, "victim"); + return linux.flock(fd, op); +} + +test "a gate hard-linked to another file is refused and the file keeps its bytes" { + const gpa = testing.allocator; + const root = try scratchRoot(gpa); + defer removeRoot(gpa, root); + const dir = try fsx.openDir(fsx.cwd, root, false); + defer sys.close(dir); + try createFile(dir, "victim", "secret"); + _ = try sys.call(linux.linkat, .{ dir, "victim", dir, ".lock", @as(u32, 0) }); + try testing.expectError(error.GateNotPrivate, RootLock.acquire(gpa, root, 0)); + try expectContents(gpa, dir, "victim", "secret"); +} + +test "a hard link planted while the gate is awaited stops the truncate" { + const gpa = testing.allocator; + const root = try scratchRoot(gpa); + defer removeRoot(gpa, root); + const dir = try fsx.openDir(fsx.cwd, root, false); + defer sys.close(dir); + try createFile(dir, ".lock", "secret"); + plant_dir = dir; + state.lock_ops.flock = linkThenFlock; + const before = fdCount(gpa); + try testing.expectError(error.GateNotPrivate, RootLock.acquire(gpa, root, 0)); + try testing.expectEqual(before, fdCount(gpa)); + try expectContents(gpa, dir, "alias", "secret"); + state.lock_ops.* = .{}; + try sys.unlinkat(dir, "alias", 0); + const l = try RootLock.acquire(gpa, root, 0); + l.release(); +} + +test "a gate moved to another name while it is awaited is not truncated" { + const gpa = testing.allocator; + const root = try scratchRoot(gpa); + defer removeRoot(gpa, root); + const dir = try fsx.openDir(fsx.cwd, root, false); + defer sys.close(dir); + try createFile(dir, ".lock", "secret"); + plant_dir = dir; + state.lock_ops.flock = renameThenFlock; + try testing.expectError(error.GateNotPrivate, RootLock.acquire(gpa, root, 0)); + try expectContents(gpa, dir, "victim", "secret"); +} + +fn expectFifoRefused(gpa: std.mem.Allocator, root: []const u8, dir: fsx.fd_t, name: [:0]const u8, want: RootLock.AcquireError) !void { + _ = try sys.call(linux.mknodat, .{ dir, name.ptr, @as(linux.mode_t, linux.S.IFIFO | 0o600), @as(linux.dev_t, 0) }); + defer sys.unlinkat(dir, name, 0) catch {}; + const watch = try OpenWatch.init(gpa, root, name); + defer watch.deinit(); + try testing.expectError(want, RootLock.acquire(gpa, root, 0)); + try testing.expect(!watch.opened()); +} + +test "a FIFO at either lock name is refused without being opened" { + const gpa = testing.allocator; + const root = try scratchRoot(gpa); + defer removeRoot(gpa, root); + const dir = try fsx.openDir(fsx.cwd, root, false); + defer sys.close(dir); + try expectFifoRefused(gpa, root, dir, ".lock", error.GateNotPrivate); + try expectFifoRefused(gpa, root, dir, ".lock.posix", error.NotPrivate); +} + +test "a symbolic link at either lock name or as the state root is refused" { + const gpa = testing.allocator; + const root = try scratchRoot(gpa); + defer removeRoot(gpa, root); + const dir = try fsx.openDir(fsx.cwd, root, false); + defer sys.close(dir); + try createFile(dir, "victim", "secret"); + { + const watch = try OpenWatch.init(gpa, root, "victim"); + defer watch.deinit(); + _ = try sys.call(linux.symlinkat, .{ "victim", dir, ".lock" }); + try testing.expectError(error.GateNotPrivate, RootLock.acquire(gpa, root, 0)); + try sys.unlinkat(dir, ".lock", 0); + _ = try sys.call(linux.symlinkat, .{ "victim", dir, ".lock.posix" }); + try testing.expectError(error.NotPrivate, RootLock.acquire(gpa, root, 0)); + try sys.unlinkat(dir, ".lock.posix", 0); + try testing.expect(!watch.opened()); + } + try expectContents(gpa, dir, "victim", "secret"); + + _ = try sys.call(linux.mkdirat, .{ dir, "real", @as(linux.mode_t, 0o700) }); + _ = try sys.call(linux.symlinkat, .{ "real", dir, "link" }); + for ([_][]const u8{ "link", "link/" }) |name| { + const via = try gpa.print("{s}/{s}", .{ root, name }); + defer gpa.free(via); + try testing.expectError(error.NotDir, RootLock.acquire(gpa, via, 0)); + } + try testing.expectError(error.NotFound, fsx.statAt(dir, "real/.lock", false)); +} + +test "both lock files are created mode 0600" { + const gpa = testing.allocator; + const root = try scratchRoot(gpa); + defer removeRoot(gpa, root); + const l = try RootLock.acquire(gpa, root, 0); + defer l.release(); + try testing.expectEqual(@as(u32, 0o600), (try fsx.fstat(l.gate)).perm()); + try testing.expectEqual(@as(u32, 0o600), (try fsx.fstat(l.posix)).perm()); +}