Skip to content

fix(sandbox): recover the workspace tmpdir after a killed run - #107

Merged
ltsabadz merged 3 commits into
mainfrom
fix/stale-tmpdir-reclaim
Nov 20, 2026
Merged

ltsabadz merged 3 commits into
mainfrom
fix/stale-tmpdir-reclaim

Conversation

@johayang-amd

@johayang-amd johayang-amd commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

  • AXIS creates {tmpdir} (<workspace>/.axis-tmp) exclusively and removes it when the sandbox stops. A non-interactive axis run killed by SIGTERM (what a caller's timeout sends) or SIGKILL skipped that cleanup, and every later run in that workspace failed with cannot create exclusive tmpdir ... File exists until someone removed the directory by hand.
  • GAS hits this on host AXIS: one timed-out call leaves the lease unable to run anything (silogen/GAS#155).

Technical Details

  • A standalone axis run now 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 leave destroy() without it. Native and MXC sandboxes now keep the process group while wait() holds the child, and destroy() kills it (linux/mod.rs, linux/mxc.rs).
  • On Linux, each sandbox that uses {tmpdir} (bwrap, native including run_as_user, and MXC) holds an exclusive flock on its workspace directory from before the tmpdir is created until after it is removed (claim_tmpdir in linux/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.
  • The claim opens the workspace once, and everything after goes through that descriptor: the lock, the filesystem check, and the removal of a leftover, which follows no symlink (fstatat(AT_SYMLINK_NOFOLLOW), then remove_dir_all anchored at /proc/self/fd/<fd>). For run_as_user the open refuses a symlink in any workspace component, as create_tmpdir_for_identity does, so a workspace that setup would refuse is refused before anything is removed.
  • A run that holds the lock and finds a tmpdir knows no live sandbox on this host owns it, and removes it only when it is a 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.
  • On NFS, SMB/CIFS, FUSE, 9P, Ceph, AFS, Coda and Lustre, or when fstatfs fails, a lock may be local to one host, so a leftover is never removed there and the launch fails as before.
  • A run that does not get the lock fails as before, now saying "another sandbox in this workspace holds it": two sandboxes in one workspace still cannot share a tmpdir. On a filesystem without flock (EOPNOTSUPP, EINVAL) no run can hold the claim, so nothing is removed; any other locking error fails the launch.
  • Docs: 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_user it 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 main 24e3a5d) against that main, each built in its own worktree and target directory with the pinned lxc-exec:

  • DEVELOPMENT.md local checks: cargo fmt --all -- --check, cargo clippy --locked --all-targets -- -D warnings, cargo test --locked, bash scripts/test_security_tier0.sh.
  • Unit tests:
    • 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; for run_as_user a 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 cancelled wait() kills the command's background process; the parent-death monitor does not keep the claim.
  • End to end, axis run with a {tmpdir} policy on the native and MXC backends (network.mode: block):
    • SIGTERM to axis run while sh -c 'sleep 61 & sleep 60; wait' runs: exit status, the background sleep, .axis-tmp;
    • SIGKILL to axis run, then an immediate new run in the same workspace;
    • a second run while the first runs;
    • a planted .axis-tmp symlink.
  • Launch latency: axis run -- /bin/true with a {tmpdir} policy, 2 warm-up runs and 20 runs each, main and this head alternating.

Test Result

  • Local checks: fmt and clippy are clean; cargo test --locked 1055 passed, 0 failed, 6 ignored; tier 0 passed. The native-backend test ran (not skipped).
  • End to end (native and MXC gave the same results):
Case main This head
SIGTERM exit 143, background killed, .axis-tmp left exit 143, background killed, .axis-tmp removed
SIGKILL, then immediate retry exit 125, File exists exit 0
Second run while the first runs exit 125, File exists; first exits 0 exit 125, "another sandbox in this workspace holds it"; first exits 0, tmpdir removed
Planted .axis-tmp symlink exit 125, File exists; link and target kept the same
  • Launch latency (median / p90): native 18.1 / 19.2 ms on main against 17.9 / 18.5 ms; MXC 27.1 / 35.2 ms against 27.4 / 28.8 ms. The claim adds no measurable cost.
  • Not run: a workspace on a network filesystem end to end (the test host has none; the decision is unit-tested with the filesystem types), run_as_user end to end (needs root; unit-tested), macOS (the SIGTERM handler covers every Unix; CI builds it), and Windows, where nothing changes; CI runs both.

@ltsabadz ltsabadz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread crates/axis-cli/src/main.rs Resolved
Comment thread crates/axis-sandbox/src/linux/mxc.rs Resolved
Comment thread crates/axis-cli/src/main.rs Resolved
@johayang-amd

Copy link
Copy Markdown
Collaborator Author

Thanks Luka, all good catches. Fixed in 64aee04:

  • SIGTERM left children running: reproduced on f732efa with the native backend. destroy() now kills the process group even after the SIGTERM handler dropped the pending wait(), on native and MXC. A unit test covers it and fails without the fix.
  • Retry blocked after SIGKILL: reproduced too. The parent-death monitor now closes every inherited descriptor except its pipe, so an immediate retry succeeds.
  • Benchmark: axis run -- /bin/true with a {tmpdir} policy, 20 runs: native 24.1 → 23.8 ms, MXC 59.4 → 56.7 ms median, so no measurable cost. Numbers are in the PR body.

Details are in the PR body, including an end-to-end table for main, f732efa and this head on both backends.

@ltsabadz ltsabadz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_tmpdir directly.
  • Docs. Please note that a leftover .axis-tmp gets 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.

Comment thread crates/axis-sandbox/src/linux/mxc.rs Resolved
Comment thread crates/axis-sandbox/src/linux/mod.rs Resolved
Comment thread crates/axis-cli/src/main.rs Resolved
johayang-amd and others added 3 commits November 20, 2026 12:16
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]>
@johayang-amd
johayang-amd force-pushed the fix/stale-tmpdir-reclaim branch from 64aee04 to 47ced77 Compare November 20, 2026 11:38
@johayang-amd

Copy link
Copy Markdown
Collaborator Author

Thanks Luka, all fair. Rebased onto main and addressed the review in 47ced77; replies inline. In short:

  • Network filesystems: a leftover is never removed there now.
  • After fix(sandbox): allow Linux run_as_user with setuid and setgid capabilities #95: the claim opens the workspace once and locks, checks and removes through that descriptor without following symlinks; for run_as_user the open refuses a linked workspace before anything is removed.
  • Tests through a real backend: a native-backend test covers the leftover, a second sandbox while the first runs, and a third after it.
  • Docs: the tmpdir's lifecycle is in the isolation contract (Lifecycle And Cleanup Semantics).
  • Windows: agreed, a separate PR.

Retested on Linux (native and MXC) against main; the table and launch numbers are in the PR body.

@ltsabadz ltsabadz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@ltsabadz
ltsabadz merged commit a2cdbd8 into main Nov 20, 2026
33 checks passed
@ltsabadz
ltsabadz deleted the fix/stale-tmpdir-reclaim branch November 20, 2026 13:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants