Skip to content

fix(sandbox): allow Linux run_as_user with setuid and setgid capabilities - #95

Merged
johayang-amd merged 4 commits into
mainfrom
fix/linux-sandbox-identity
Nov 15, 2026
Merged

johayang-amd merged 4 commits into
mainfrom
fix/linux-sandbox-identity

Conversation

@amd-balatoth

Copy link
Copy Markdown
Collaborator

Motivation

  • run_as_user on Linux refused every parent that was not root, even when that parent already held CAP_SETUID and CAP_SETGID and could perform the identity switch.
  • The per-user tmpdir was created after workspace preparation, so a run_as_user sandbox could set up its workspace before the identity-owned tmpdir existed.

Technical Details

  • A non-root parent may use run_as_user only when its effective capability set includes both CAP_SETUID and CAP_SETGID. Root is unchanged. A parent with neither still fails closed.
  • Effective capabilities are read from CapEff in /proc/self/status.
  • The identity-owned tmpdir is created before workspace preparation on both the Landlock and MXC paths, and only when the policy needs a tmpdir.
  • capget is on the seccomp whitelist so the sandbox can check that privileges were dropped. It is read-only.

axis run still uses the current daemon-first path, including axisd's caller token. The older change that forced every axis run to execute inline conflicted with that and is not in this branch. The identity checks live in the sandbox, so both the daemon and the standalone fallback use them.

Security impact: a non-root process that already holds CAP_SETUID and CAP_SETGID can now request run_as_user. A process without those capabilities still cannot. capget is added to the sandbox syscall whitelist for read-only capability checks.

Test Plan

  • resolve_run_as_user_accepts_non_root_parent_with_setuid_and_setgid_capabilities covers a non-root parent that holds both capabilities.
  • Existing run_as_user rejection tests still use an empty capability set.
  • The seccomp whitelist test expects syscall 125 (capget).
  • Linux only. This checkout is macOS, so the linux sandbox module is not compiled here.

Test Result

  • Not run locally. crates/axis-sandbox/src/linux is cfg(target_os = "linux"), so these tests cannot run on this Mac.

Made with Cursor

@johayang-amd johayang-amd 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, Balazs. This unblocks every non-root run_as_user caller, and the capability drop holds up: both the native and MXC paths clear the ambient, bounding and process sets after the switch and before exec.

Blocking

  1. The gate needs five capabilities, not two. With only CAP_SETUID and CAP_SETGID the check passes, then the launch fails with EPERM when AXIS assigns .axis-tmp to the target. AXIS also has to chown and chmod the workspace and tmpdir and then reopen them, which needs CAP_CHOWN, CAP_FOWNER and CAP_DAC_OVERRIDE. With CAP_DAC_READ_SEARCH in place of CAP_DAC_OVERRIDE the call runs, but the tmpdir cannot be removed afterwards. It fails closed either way, but late and with a misleading message. I've suggested checking all five up front (inline). Could you also rename resolve_run_as_user_accepts_non_root_parent_with_setuid_and_setgid_capabilities to match, and add a negative case for a caller with only CAP_SETUID and CAP_SETGID?

  2. Docs. Two pages still describe the old rule, and they are not in this diff, so here is proposed text:

    docs/linux-setup.md, the run_as_user row:

    | `run_as_user` | Drops to a configured non-root user and prepares writable workspace state for that user. Every capability is dropped before exec. | The target user must already exist, must not be root, and must not share the caller's UID or GID. The caller must be root, or hold `CAP_SETUID`, `CAP_SETGID`, `CAP_CHOWN`, `CAP_FOWNER` and `CAP_DAC_OVERRIDE` in its effective set, because AXIS assigns the workspace and tmpdir to the target user. |

    docs/axis-isolation-contract.md, under "Process And Identity Semantics":

    - `run_as_user` must not run as root and must fail if the requested identity
      cannot be prepared safely. A non-root caller may request it only when it
      holds `CAP_SETUID`, `CAP_SETGID`, `CAP_CHOWN`, `CAP_FOWNER` and
      `CAP_DAC_OVERRIDE`; AXIS clears every capability set before exec.

Tested: d4172c0 cherry-picked onto main c5023ce, on Halo (Linux 6.18.44, Landlock enabled), in an unprivileged user namespace with the axis_native runtime in standalone mode. The caller was uid 990 and the target was nobody.

  • Before this PR, every non-root caller is refused.
  • With this PR and the five capabilities above, the switch works. Inside the sandbox the process runs as 65534 with zero capabilities, writes outside the workspace are denied, setuid(0) is denied and capget is allowed.
  • A caller with no capabilities is still refused.
  • axis-sandbox --lib: 506 passed. public_create_uses_mxc_linux_backend_by_default failed once in four full runs and passed every time on its own; main passed all four. It looks like a parallel-test flake, not this change.
  • The gated_run_as_user* tier-3 tests need real host root, so I couldn't run them. Under a user namespace they fail the same way on main.

A non-root caller now needs CAP_CHOWN, CAP_FOWNER and CAP_DAC_OVERRIDE
besides CAP_SETUID and CAP_SETGID, because AXIS assigns the workspace and
tmpdir to the target and reopens them. Checking all five up front fails
early with a clear message instead of EPERM at launch. Docs updated.

Co-authored-by: Cursor <[email protected]>
@amd-balatoth

Copy link
Copy Markdown
Collaborator Author

Thanks, @johayang-amd. Addressed in d100f47:

  • Five capabilities: a non-root caller now needs CAP_SETUID, CAP_SETGID, CAP_CHOWN, CAP_FOWNER and CAP_DAC_OVERRIDE, checked up front with the message you suggested.
  • Tests: renamed to resolve_run_as_user_accepts_non_root_parent_with_identity_switch_capabilities; added resolve_run_as_user_rejects_non_root_parent_with_only_setuid_and_setgid and resolve_run_as_user_rejects_non_root_parent_missing_any_identity_switch_capability (each of the five removed in turn is refused).
  • Docs: docs/linux-setup.md and docs/axis-isolation-contract.md use your proposed text.

Tested on Linux: axis-sandbox identity, run_as_user and capability tests pass (16, 16, 10); clippy -D warnings is clean. The full --lib run hangs on this host in fork-heavy netns fixtures, and one seccomp test fails because TMPDIR is not under a world-writable /tmp here; neither touches this change.

johayang-amd
johayang-amd previously approved these changes Nov 13, 2026

@johayang-amd johayang-amd 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 Balazs, this looks right now: the five-capability gate, the negative tests and the docs match what we discussed, and moving the tmpdir ahead of workspace preparation is safe because create_tmpdir_for_identity refuses a workspace with symlink components.

Approving. I ran the new path end to end on halo, since no CI job does. Two small things before merge:

  • CI: the remaining CodeQL alerts are the existing test-only ones, which I've dismissed, and the Dependency audit failure is the new quinn-udp advisory; merging main after #113 lands clears it.

Tested 7efc59d merged onto main 0c53fd6.

  • Linux x86_64 (WSL2): fmt, clippy, cargo test --workspace 995 passed / 0 failed, tier 0, workflow verifier.
  • Halo (Linux 6.18), as root with setpriv dropping the caller to a non-root account that holds only the capabilities under test, run_as_user: nobody:
Case native MXC
root caller runs as nobody, CapEff=0 fails, see below
non-root, all five capabilities runs as nobody, CapEff=0, file owned by nobody, .axis-tmp removed fails, see below
non-root, any one missing (5 cases) refused before launch, exit 125 same
non-root, no capabilities refused, exit 125 same
main, non-root with all five refused (old message) same

On MXC, run_as_user fails for every caller, root included and on main too: lxc-exec runs as the target user and can't read AXIS's 0600 config file (Permission denied). That's an existing bug, not this PR, and worth its own issue.

…d RUSTSEC-2026-0486

Only the two lockfile entries change; every other package, including the
windows-sys versions, stays as locked. cargo deny check advisories licenses
sources passes for the workspace and gui/linux.

Co-authored-by: Cursor <[email protected]>

@johayang-amd johayang-amd 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.

Re-approving: 05b4b7c only bumps quinn-udp and rustix (same as #113/#109), and CI is green. I'll close #113 once this lands.

@johayang-amd
johayang-amd merged commit 61b8e10 into main Nov 15, 2026
33 checks passed
@johayang-amd
johayang-amd deleted the fix/linux-sandbox-identity branch November 15, 2026 12:12
johayang-amd added a commit that referenced this pull request Nov 20, 2026
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]>
ltsabadz pushed a commit that referenced this pull request Nov 20, 2026
* fix(sandbox): recover the workspace tmpdir after a killed run

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]>

* fix(sandbox): stop the whole sandbox on SIGTERM and free the claim at 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.

* fix(sandbox): reclaim the tmpdir through the locked workspace descriptor

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]>

---------

Co-authored-by: Cursor <[email protected]>
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.

3 participants