Repository navigation
tmpdir: each user works in a private directory of a shared base - #1178
Conversation
`toyos-tmpdir` kept its GLOBAL lock and its roots directly in the base, `$TMPDIR` or `/tmp`, assuming every process sharing a base was one user. On the guest runner host, which runs jobs as `toyos-runner`, the first job (`cargo run -- --ci rows --list`) exited 101 opening `/tmp/toyos-tmp.lock`, which another user's earlier runs had made with mode 644. The same assumption let one user's killed run leave a root in `/tmp` that every other user's sweep then failed to move, panicking their first `TempDir`. Every use of a base now goes through `private(base)`: `<base>/toyos-<euid>/`, made with mode 0700, and refused with a panic naming the path and the reason unless `lstat` shows a directory, not a symlink, owned by the effective user with permission bits exactly 0700. The lock, the roots and a sweep's stuck leftovers live inside it, so a sweep only ever lists this user's roots. In a sticky base no other user can rename or replace the directory once made; one they made first under this user's name is refused rather than used. The rule is the same on every host. On macOS `$TMPDIR` is already a per-user 0700 directory, so the extra level is redundant there but harmless; `/tmp`, the short base, is shared on macOS as on Linux, so the change is not a no-op on either. A short socket path grows by `toyos-<uid>/`, at most 17 bytes, and stays far inside Darwin's 104-byte `sun_path`. `build/ci.rs`'s leftover check reads the private directory of the job's `$TMPDIR` for the lock and names what is left in it or beside it. Closes `issues/a-dead-scratch-root-another-user-owns-panics-every-sweep-of-its-base.md`: another user's roots are never in the directory a sweep lists. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review of #1178 at c4e22de (round 1)ArchitectureThe fix is in the right place. One rule for every base and host is simpler than detecting a base that is already private. Production growth is small: What I verified
FindingsNo blockers. SUGGESTION
VerdictLAND |
Brings #1178 (each user's tmpdir is a private directory of a shared base). No conflict; Cargo.lock merged by itself. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Problem
toyos-tmpdirkept itsGLOBALlock and its roots directly in the base:$TMPDIR, or/tmpforTempDir::short. Its module doc assumed every process sharing a base was one user. The guest runner host runs jobs astoyos-runner, and its first job (cargo run -- --ci rows --list, run 41918010376) exited 101 withopen /tmp/toyos-tmp.lock: Permission denied. Another user's earlier runs had made that file, mode 644. The same assumption is behindissues/a-dead-scratch-root-another-user-owns-panics-every-sweep-of-its-base.md: one user's killed run left a root in/tmpthat every other user's sweep failed to move, which panicked their firstTempDir.Behavior
Every use of a base now goes through
toyos_tmpdir::private(base), which returns<base>/toyos-<euid>/. It creates that directory with mode 0700. It panics, naming the path and the reason, unlesslstatshows a directory (not a symlink) owned by the effective user with permission bits exactly 0700. The lock, the roots and a sweep'sstuck-*leftovers all live inside it./tmp, no other user can rename or replace the directory once it exists. If another user createstoyos-<your uid>first, it is refused with a clear message rather than used.PRIVATE_PREFIX(toyos-) does not start withROOT_PREFIX(toyos-tmp-). A sweep by an older binary on the same host therefore never reads a private directory as an ownerless root.gone_roots(base)andTempDir::adopt(base, pid)still take the shared base and resolve its private directory themselves.build/ci.rs's leftover check now expects the lock inside the job's$TMPDIRprivate directory. It names anything left in that directory or beside it./tmplock and roots from before this change are left alone and no longer read. On the runner host, jan's/tmp/toyos-tmp.lockstops mattering; nobody has to delete it.macOS
macOS's
$TMPDIR(/var/folders/…/T/) is already per-user and 0700 (checked on the dev host), so the extra level is redundant there./tmp, the short base, is shared on macOS just as on Linux, so the change is not a no-op on either. I kept one rule for every base and host instead of detecting an already-private base. Detection would add a second layout and buy only one path component.sun_path
A short path grows by at most
toyos-4294967295/, 17 bytes. The worst case with a 32-bit uid is/private/tmp/toyos-4294967295/toyos-tmp-99999-99/boot-999/uart.sock, 67 bytes on Darwin (limit 104). On Linux, with a 7-digit pid, it is/tmp/toyos-4294967295/toyos-tmp-4194304-99/boot-999/uart.sock, 61 bytes (limit 108).New dependency
libc = "0.2"(already in the workspace lockfile, 0.2.190), forgeteuid. std has no effective-uid API. Every consumer of the crate is a dev-dependency on a Unix host, andbuild/ci.rsalready usesstd::os::unixunconditionally.Tests
Host unit tests in
lib/tmpdir/src/lib.rs:a_private_directory_is_made_this_users_alone: the directory is created, is a directory, is owned by the euid, has mode 700, and a second use accepts it.a_private_directory_another_user_owns_is_refused: covers the two-user case.a_private_directory_with_another_mode_is_refused: mode 755 is refused.a_symlink_in_place_of_a_private_directory_is_refused: a symlink to a valid 0700 directory is refused.In
lib/tmpdir/tests/reclaim.rs,a_root_without_an_owner_goes_and_nothing_else_is_touchednow also places an ownerless root in the shared base and one in another uid's private directory. A real holder process's sweep must leave both alone.Two users on one host. The host can't run as two users, so
private_for(base, uid)takes the user as a parameter. The test calls it as uid+1 on a directory this process owns with mode 0700. Ownership is the only thing that differs, which is exactly what a second user sees when they meet the first user's directory. The OS's own permission checks (sticky/tmp, a 0700 directory) are not re-tested here. The sweep's isolation from other users follows from the sweep listing only the caller's private directory, and the extended reclaim test checks that.Checks (revision c4e22de, macOS aarch64 dev host)
cargo test -p toyos-tmpdir: exit 0 (log).cargo run -- --ci host: exit 1, 107 of 108 steps green (log). "nothing left in $TMPDIR or /tmp" is green under the new layout. The one red step is the build system'scargo test --lib. It failed on the known load flakea_narrow_guest_does_not_overtake_a_wide_one_waiting(issues/a-narrow-guest-test-reads-any-queue-holder-as-the-wide-waiter.md; load average about 9). That test's leftover guest directory then made the lastTempDirdrop in the process, which happened to be inthe_primary_s_fork_checkout_is_moved_to_its_pin, fail to remove its root. Both tests passed alone three times each at the same revision (logs). Neither touches the changed logic beyond taking aTempDir.Records
issues/a-dead-scratch-root-another-user-owns-panics-every-sweep-of-its-base.md. Its durable rule is now the module header's "a base may be shared by every user; nothing in it is".issues/the-tmp-leftover-check-can-name-another-worktrees-killed-run.md. Cross-worktree blame within one user remains open, as doeshost-ci-blames-its-run-for-another-processs-dead-tmp-root.Unresolved / noticed, not changed
build/qemu.rsstill uses fixed shared paths for interactivecargo run:/tmp/toyos-qmp.sock,/tmp/toyos-uart.sock,/tmp/toyos-uart-early.log,/tmp/toyos-qemu-debug.logand/tmp/toyos-qemu-stderr.log. A second user runningcargo runon the runner host would collide with the first. Harness rows useTempDir::shortand are not affected.TempDir::adopthas no caller outside its own test./tmpand macOS's$TMPDIRare not like that; I don't check the base itself.🤖 Generated with Claude Code
Orchestrator note (review S4). Dead roots made before this change, directly in
$TMPDIRas well as in/tmp, are no longer reclaimed by any sweep. On a dev host they may hold boot images; removetoyos-tmp-*there once by hand.toyos-builddepends on the crate as a normal dependency, not only as a dev-dependency; the build is Unix-only, sogeteuidis available everywhere it runs.