Repository navigation
fix(sandbox): allow Linux run_as_user with setuid and setgid capabilities - #95
Conversation
Co-authored-by: Cursor <[email protected]>
johayang-amd
left a comment
There was a problem hiding this comment.
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
-
The gate needs five capabilities, not two. With only
CAP_SETUIDandCAP_SETGIDthe check passes, then the launch fails withEPERMwhen AXIS assigns.axis-tmpto the target. AXIS also has to chown and chmod the workspace and tmpdir and then reopen them, which needsCAP_CHOWN,CAP_FOWNERandCAP_DAC_OVERRIDE. WithCAP_DAC_READ_SEARCHin place ofCAP_DAC_OVERRIDEthe 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 renameresolve_run_as_user_accepts_non_root_parent_with_setuid_and_setgid_capabilitiesto match, and add a negative case for a caller with onlyCAP_SETUIDandCAP_SETGID? -
Docs. Two pages still describe the old rule, and they are not in this diff, so here is proposed text:
docs/linux-setup.md, therun_as_userrow:| `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 andcapgetis allowed. - A caller with no capabilities is still refused.
axis-sandbox --lib: 506 passed.public_create_uses_mxc_linux_backend_by_defaultfailed once in four full runs and passed every time on its own;mainpassed 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 onmain.
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]>
|
Thanks, @johayang-amd. Addressed in d100f47:
Tested on Linux: |
Co-authored-by: Cursor <[email protected]>
There was a problem hiding this comment.
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 --workspace995 passed / 0 failed, tier 0, workflow verifier. - Halo (Linux 6.18), as root with
setprivdropping 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]>
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]>
* 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]>
Motivation
run_as_useron Linux refused every parent that was not root, even when that parent already heldCAP_SETUIDandCAP_SETGIDand could perform the identity switch.run_as_usersandbox could set up its workspace before the identity-owned tmpdir existed.Technical Details
run_as_useronly when its effective capability set includes bothCAP_SETUIDandCAP_SETGID. Root is unchanged. A parent with neither still fails closed.CapEffin/proc/self/status.capgetis on the seccomp whitelist so the sandbox can check that privileges were dropped. It is read-only.axis runstill uses the current daemon-first path, including axisd's caller token. The older change that forced everyaxis runto 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_SETUIDandCAP_SETGIDcan now requestrun_as_user. A process without those capabilities still cannot.capgetis 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_capabilitiescovers a non-root parent that holds both capabilities.run_as_userrejection tests still use an empty capability set.capget).linuxsandbox module is not compiled here.Test Result
crates/axis-sandbox/src/linuxiscfg(target_os = "linux"), so these tests cannot run on this Mac.Made with Cursor