Repository navigation
fix(sandbox): recover the workspace tmpdir after a killed run - #107
Conversation
ltsabadz
left a comment
There was a problem hiding this comment.
On the Linux native backend, normal runs, overlapping runs and a planted symlink all behave as expected.
SIGTERM should stop the command and everything it started. Instead, the command's children keep running after AXIS deletes .axis-tmp. After SIGKILL, a retry right away fails for about a second because a leftover AXIS helper process still holds the lock.
Format, clippy, cargo test and the tier-0 script pass, and the two red CI checks fail on main too. Since this changes the launch path, please add benchmark numbers or say why they aren't needed.
|
Thanks Luka, all good catches. Fixed in 64aee04:
Details are in the PR body, including an end-to-end table for |
ltsabadz
left a comment
There was a problem hiding this comment.
Thanks! Took another look, everything works on native linux.
Before merge:
- Shared network drives (blocker), see inline.
- Rebase. This conflicts with main, and after #95 the cleanup would run before the workspace checks (inline on
mxc.rs). Please retest after rebasing. - Tests through a real backend. The new tests only call
claim_tmpdirdirectly. - Docs. Please note that a leftover
.axis-tmpgets deleted and that a second run in the same workspace is refused.
Windows has the same bug. It's fine to leave it out, this can be a separate PR.
AXIS creates {tmpdir} (<workspace>/.axis-tmp) exclusively and removes it
when the sandbox stops. A non-interactive `axis run` that gets SIGTERM, as
a caller's timeout sends, ended without that cleanup, and so does one that
gets SIGKILL. The tmpdir stayed, and every later run in that workspace
failed with "cannot create exclusive tmpdir ... File exists" until someone
removed it by hand.
`axis run` now stops its sandbox on SIGTERM in every mode, as it already
did on a terminal: the command is killed, the tmpdir removed, and AXIS
exits 143. The handler is registered before the sandbox starts.
Each Linux sandbox that uses {tmpdir} also holds an exclusive flock on
its workspace directory from before the tmpdir is created until after it
is removed. The kernel releases it when that AXIS process ends, however
it ends. A run that gets the lock and still finds a tmpdir knows no live
sandbox owns it, and removes it when it is a real directory owned by the
uid the tmpdir is created for; a link or another owner's directory is
left, and the exclusive create still refuses it. A run that does not get
the lock fails as before: two sandboxes in one workspace still cannot
share a tmpdir. Where flock is unsupported nothing is removed.
Co-authored-by: Cursor <[email protected]>
… once Review of the tmpdir claim found that SIGTERM left the command's children running and that a run killed with SIGKILL blocked an immediate retry. - wait() takes the child, so after the SIGTERM handler dropped a pending wait(), destroy() found no child and only disarmed the parent-death monitor. Native and MXC sandboxes now remember the process group while wait() holds the child, and destroy() kills it. - The parent-death monitor inherited the workspace lock and outlives a killed AXIS for up to about a second. It now closes every inherited descriptor except its pipe. - A flock error other than "held" or "unsupported" now fails the launch instead of starting without the claim. - MXC claims the tmpdir after the run_as_user workspace checks, as the native path does, so it never removes a leftover through a workspace it then refuses. - The daemon path of `axis run` is left as on main; #108 removes it.
Review found three ways the leftover removal could reach the wrong directory. After #95 the claim runs before the run_as_user workspace checks, and it removed the leftover by path, so a workspace about to be refused could already have lost a .axis-tmp behind a link. On a network filesystem the lock may be local to one host, so another host's live tmpdir looked like a leftover. The claim now opens the workspace once and does everything through that descriptor: the lock, the filesystem check, and the removal, which follows no link. For run_as_user the open refuses a symlink in any component, as the run_as_user setup does. On NFS, SMB, FUSE, 9P and similar filesystems, or when the type is unknown, a leftover is left in place and the exclusive create refuses it, as before this change. axis run now registers its SIGTERM handler before creating the sandbox, since MXC creates the tmpdir there. The parent-death monitor reuses close_child_fd_range. A native-backend test covers the leftover and a second sandbox, and the close-on-exec test now checks that a spawned command does not keep the lock. The isolation contract describes the tmpdir's lifecycle. Co-authored-by: Cursor <[email protected]>
64aee04 to
47ced77
Compare
|
Thanks Luka, all fair. Rebased onto
Retested on Linux (native and MXC) against |
Motivation
{tmpdir}(<workspace>/.axis-tmp) exclusively and removes it when the sandbox stops. A non-interactiveaxis runkilled by SIGTERM (what a caller's timeout sends) or SIGKILL skipped that cleanup, and every later run in that workspace failed withcannot create exclusive tmpdir ... File existsuntil someone removed the directory by hand.Technical Details
axis runnow stops its sandbox on SIGTERM in every mode, as it already did on a terminal: the command and everything it started are killed, the tmpdir removed, and AXIS exits 143. The handler is registered before the sandbox is created, since MXC creates the tmpdir there (crates/axis-cli/src/main.rs). The daemon path is unchanged; fix(cli): run sandboxes without contacting axisd #108 removes it. On Windows nothing changes.wait()takes the child, so a SIGTERM during the wait used to leavedestroy()without it. Native and MXC sandboxes now keep the process group whilewait()holds the child, anddestroy()kills it (linux/mod.rs,linux/mxc.rs).{tmpdir}(bwrap, native includingrun_as_user, and MXC) holds an exclusiveflockon its workspace directory from before the tmpdir is created until after it is removed (claim_tmpdirinlinux/landlock.rs). The kernel releases the lock when that AXIS process ends, however it ends. The parent-death monitor, which outlives a killed AXIS for up to about a second, closes every descriptor it inherits except its pipe, so it does not keep the lock.fstatat(AT_SYMLINK_NOFOLLOW), thenremove_dir_allanchored at/proc/self/fd/<fd>). Forrun_as_userthe open refuses a symlink in any workspace component, ascreate_tmpdir_for_identitydoes, so a workspace that setup would refuse is refused before anything is removed.fstatfsfails, a lock may be local to one host, so a leftover is never removed there and the launch fails as before.flock(EOPNOTSUPP,EINVAL) no run can hold the claim, so nothing is removed; any other locking error fails the launch.docs/axis-isolation-contract.md(Lifecycle And Cleanup Semantics) now describes the tmpdir's lifecycle: one sandbox per workspace, when a leftover is removed, network filesystems, and Windows.Security impact: none that weakens isolation. The new removal runs only while this process holds the workspace lock, only through the descriptor it locked, only on a directory owned by the expected uid, and never on a network filesystem; it follows no symlink, and for
run_as_userit never runs through a workspace path with a symlink in it. The lock's descriptor is close-on-exec, so the sandboxed command cannot inherit it. Anyone who can open the workspace directory can take the lock and make later runs there fail, which fails closed. Mixed versions in one workspace: an AXIS without this change does not take the lock, so while it runs, a run with this change can remove its live tmpdir, and when it exits, its cleanup removes the live tmpdir of a run with this change. Two such runs already fail on each other today.Test Plan
On a shared Linux test host (x86_64, kernel 6.18, non-root), this head (rebased onto
main24e3a5d) against thatmain, each built in its own worktree and target directory with the pinnedlxc-exec:DEVELOPMENT.mdlocal checks:cargo fmt --all -- --check,cargo clippy --locked --all-targets -- -D warnings,cargo test --locked,bash scripts/test_security_tier0.sh.linux/landlock.rs: a leftover is removed only while no other sandbox holds the claim; a link and another owner's directory are left; nothing below a link inside the leftover is removed; forrun_as_usera linked workspace is refused before anything is removed; a leftover on a network or unknown filesystem is left; a spawned command does not keep the lock (for both ways of opening the workspace).linux/mod.rs: through the native backend, a sandbox replaces a leftover tmpdir, a second sandbox in the workspace is refused while it runs, and a third starts after it;destroy()after a cancelledwait()kills the command's background process; the parent-death monitor does not keep the claim.axis runwith a{tmpdir}policy on the native and MXC backends (network.mode: block):axis runwhilesh -c 'sleep 61 & sleep 60; wait'runs: exit status, the backgroundsleep,.axis-tmp;axis run, then an immediate new run in the same workspace;.axis-tmpsymlink.axis run -- /bin/truewith a{tmpdir}policy, 2 warm-up runs and 20 runs each,mainand this head alternating.Test Result
cargo test --locked1055 passed, 0 failed, 6 ignored; tier 0 passed. The native-backend test ran (not skipped).main.axis-tmpleft.axis-tmpremovedFile existsFile exists; first exits 0.axis-tmpsymlinkFile exists; link and target keptmainagainst 17.9 / 18.5 ms; MXC 27.1 / 35.2 ms against 27.4 / 28.8 ms. The claim adds no measurable cost.run_as_userend to end (needs root; unit-tested), macOS (the SIGTERM handler covers every Unix; CI builds it), and Windows, where nothing changes; CI runs both.