diff --git a/Cargo.lock b/Cargo.lock index a4f0347..eb34e2d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1923,9 +1923,9 @@ dependencies = [ [[package]] name = "quinn-udp" -version = "0.5.14" +version = "0.5.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "addec6a0dcad8a8d96a771f815f0eaf55f9d1805756410b39f5fa81332574cbd" +checksum = "af66907df18639dcf4db56ca65490cabc4b27a97dbadd96f2926cca73298f016" dependencies = [ "cfg_aliases", "libc", @@ -2183,9 +2183,9 @@ checksum = "94300abf3f1ae2e2b8ffb7b58043de3d399c73fa6f4b73826402a5c457614dbe" [[package]] name = "rustix" -version = "1.1.4" +version = "1.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6fe4565b9518b83ef4f91bb47ce29620ca828bd32cb7e408f0062e9930ba190" +checksum = "e5e76742e5294153d4dde1207dbec5bdd81eef6fabdc0d2118babadabfd858aa" dependencies = [ "bitflags", "errno", diff --git a/crates/axis-sandbox/src/linux/identity.rs b/crates/axis-sandbox/src/linux/identity.rs index cf65a70..b09a462 100644 --- a/crates/axis-sandbox/src/linux/identity.rs +++ b/crates/axis-sandbox/src/linux/identity.rs @@ -10,6 +10,21 @@ use std::os::unix::io::RawFd; use std::path::{Component, Path, PathBuf}; const SANDBOX_TMPDIR_NAME: &str = ".axis-tmp"; +const CAP_CHOWN: u32 = 0; +const CAP_DAC_OVERRIDE: u32 = 1; +const CAP_FOWNER: u32 = 3; +const CAP_SETGID: u32 = 6; +const CAP_SETUID: u32 = 7; + +// run_as_user chowns and chmods the workspace and tmpdir to the target +// identity, then reopens them; a non-root caller needs all five. +const fn identity_switch_capabilities() -> u64 { + (1_u64 << CAP_CHOWN) + | (1_u64 << CAP_DAC_OVERRIDE) + | (1_u64 << CAP_FOWNER) + | (1_u64 << CAP_SETGID) + | (1_u64 << CAP_SETUID) +} #[derive(Debug, Clone, PartialEq, Eq)] pub(crate) struct ResolvedIdentity { @@ -61,6 +76,7 @@ struct ProcessIds { real_gid: u32, effective_gid: u32, saved_gid: u32, + effective_capabilities: u64, } impl ProcessIds { @@ -105,9 +121,12 @@ fn resolve_run_as_user_with_process_ids( )); } - if process_ids.effective_uid != 0 { + let required_capabilities = identity_switch_capabilities(); + if process_ids.effective_uid != 0 + && process_ids.effective_capabilities & required_capabilities != required_capabilities + { return Err(format!( - "run_as_user '{username}' requires root or equivalent setuid capability; current effective UID is {}", + "run_as_user '{username}' requires root, or CAP_SETUID, CAP_SETGID, CAP_CHOWN, CAP_FOWNER and CAP_DAC_OVERRIDE; current effective UID is {}", process_ids.effective_uid )); } @@ -152,6 +171,17 @@ fn current_process_ids() -> Result { std::io::Error::last_os_error() )); } + let status = std::fs::read_to_string("/proc/self/status") + .map_err(|error| format!("cannot inspect process capabilities: {error}"))?; + let effective_capabilities = status + .lines() + .find_map(|line| line.strip_prefix("CapEff:")) + .map(str::trim) + .ok_or_else(|| "cannot inspect process capabilities: CapEff is missing".to_string()) + .and_then(|value| { + u64::from_str_radix(value, 16) + .map_err(|error| format!("cannot inspect process capabilities: {error}")) + })?; Ok(ProcessIds { real_uid, @@ -160,6 +190,7 @@ fn current_process_ids() -> Result { real_gid, effective_gid, saved_gid, + effective_capabilities, }) } @@ -451,6 +482,7 @@ mod tests { real_gid: 0, effective_gid: 0, saved_gid: 0, + effective_capabilities: 0, } } @@ -462,6 +494,7 @@ mod tests { real_gid, effective_gid: 0, saved_gid: 0, + effective_capabilities: 0, } } @@ -514,6 +547,7 @@ mod tests { real_gid: 1000, effective_gid: 1000, saved_gid: 1000, + effective_capabilities: 0, }, ) .unwrap_err(); @@ -534,6 +568,7 @@ mod tests { real_gid: 2000, effective_gid: 0, saved_gid: 3000, + effective_capabilities: 0, }, "caller UID state", ), @@ -546,6 +581,7 @@ mod tests { real_gid: 2000, effective_gid: 0, saved_gid: 3000, + effective_capabilities: 0, }, "caller UID state", ), @@ -558,6 +594,7 @@ mod tests { real_gid: 2000, effective_gid: 1000, saved_gid: 3000, + effective_capabilities: 0, }, "caller GID state", ), @@ -570,6 +607,7 @@ mod tests { real_gid: 2000, effective_gid: 0, saved_gid: 1000, + effective_capabilities: 0, }, "caller GID state", ), @@ -595,6 +633,64 @@ mod tests { assert_eq!(resolved.username, "sandbox-user"); } + #[test] + fn resolve_run_as_user_accepts_non_root_parent_with_identity_switch_capabilities() { + let lookup = fake_lookup(&[("sandbox-user", 2000, 2000)]); + let process_ids = non_root_process_ids(identity_switch_capabilities()); + + let resolved = + resolve_run_as_user_with_process_ids("sandbox-user", &lookup, process_ids).unwrap(); + + assert_eq!(resolved.uid, 2000); + assert_eq!(resolved.gid, 2000); + } + + #[test] + fn resolve_run_as_user_rejects_non_root_parent_with_only_setuid_and_setgid() { + let lookup = fake_lookup(&[("sandbox-user", 2000, 2000)]); + let process_ids = non_root_process_ids((1_u64 << CAP_SETUID) | (1_u64 << CAP_SETGID)); + + let err = + resolve_run_as_user_with_process_ids("sandbox-user", &lookup, process_ids).unwrap_err(); + + assert!(err.contains("CAP_CHOWN, CAP_FOWNER and CAP_DAC_OVERRIDE")); + } + + #[test] + fn resolve_run_as_user_rejects_non_root_parent_missing_any_identity_switch_capability() { + let lookup = fake_lookup(&[("sandbox-user", 2000, 2000)]); + for cap in [ + CAP_CHOWN, + CAP_DAC_OVERRIDE, + CAP_FOWNER, + CAP_SETGID, + CAP_SETUID, + ] { + let process_ids = + non_root_process_ids(identity_switch_capabilities() & !(1_u64 << cap)); + + let err = resolve_run_as_user_with_process_ids("sandbox-user", &lookup, process_ids) + .unwrap_err(); + + assert!( + err.contains("requires root"), + "capability {cap} was not required" + ); + } + } + + fn non_root_process_ids(effective_capabilities: u64) -> ProcessIds { + ProcessIds { + real_uid: 1000, + effective_uid: 1000, + saved_uid: 1000, + real_gid: 1000, + effective_gid: 1000, + saved_gid: 1000, + effective_capabilities, + } + } + #[test] fn prepare_workspace_rejects_symlink_workspace() { let temp = tempfile::tempdir().unwrap(); diff --git a/crates/axis-sandbox/src/linux/mod.rs b/crates/axis-sandbox/src/linux/mod.rs index 61e08bd..c3e590c 100644 --- a/crates/axis-sandbox/src/linux/mod.rs +++ b/crates/axis-sandbox/src/linux/mod.rs @@ -1708,6 +1708,13 @@ impl SandboxImpl for LinuxSandbox { })?; let tmpdir_required = landlock::policy_uses_tmpdir(&self.config.policy.filesystem); if let Some(identity) = &resolved_identity { + if tmpdir_required { + crate::sandbox::record_startup_result(&trace, "support_files.tmpdir", || { + identity::create_tmpdir_for_identity(&self.config.workspace_dir, identity) + .map_err(|e| SandboxError::IsolationFailed(format!("run_as_user: {e}"))) + })?; + self.tmpdir_active = true; + } crate::sandbox::record_startup_result( &trace, "support_files.run_as_user_workspace", @@ -1719,13 +1726,7 @@ impl SandboxImpl for LinuxSandbox { } let prepared_landlock = crate::sandbox::record_startup_result(&trace, "filesystem_setup.landlock", || { - if let (Some(identity), true) = (&resolved_identity, tmpdir_required) { - if let Err(e) = - identity::create_tmpdir_for_identity(&self.config.workspace_dir, identity) - { - return Err(SandboxError::IsolationFailed(format!("run_as_user: {e}"))); - } - self.tmpdir_active = true; + if resolved_identity.is_some() && tmpdir_required { landlock::prepare_landlock_with_tmpdir_setup( &self.config.policy.filesystem, &self.config.workspace_dir, diff --git a/crates/axis-sandbox/src/linux/mxc.rs b/crates/axis-sandbox/src/linux/mxc.rs index e950903..203eab3 100644 --- a/crates/axis-sandbox/src/linux/mxc.rs +++ b/crates/axis-sandbox/src/linux/mxc.rs @@ -959,6 +959,15 @@ impl MxcLinuxSandbox { let mut tmpdir_active = false; let tmpdir_required = super::landlock::policy_uses_tmpdir(&config.policy.filesystem); if let Some(identity) = &resolved_identity { + if tmpdir_required { + crate::sandbox::record_startup_result(&trace, "support_files.tmpdir", || { + super::identity::create_tmpdir_for_identity(&config.workspace_dir, identity) + .map_err(|err| { + SandboxError::IsolationFailed(format!("MXC run_as_user: {err}")) + }) + })?; + tmpdir_active = true; + } crate::sandbox::record_startup_result( &trace, "support_files.run_as_user_workspace", @@ -969,15 +978,6 @@ impl MxcLinuxSandbox { }) }, )?; - if tmpdir_required { - crate::sandbox::record_startup_result(&trace, "support_files.tmpdir", || { - super::identity::create_tmpdir_for_identity(&config.workspace_dir, identity) - .map_err(|err| { - SandboxError::IsolationFailed(format!("MXC run_as_user: {err}")) - }) - })?; - tmpdir_active = true; - } } else if tmpdir_required { crate::sandbox::record_startup_result(&trace, "support_files.tmpdir", || { super::landlock::create_tmpdir(&config.workspace_dir) diff --git a/crates/axis-sandbox/src/linux/seccomp.rs b/crates/axis-sandbox/src/linux/seccomp.rs index bd1783e..a50a01c 100644 --- a/crates/axis-sandbox/src/linux/seccomp.rs +++ b/crates/axis-sandbox/src/linux/seccomp.rs @@ -159,6 +159,7 @@ const WHITELIST: &[(u32, &str)] = &[ (110, "getppid"), (111, "getpgrp"), (124, "getsid"), + (125, "capget"), // read-only capability introspection for verifying privilege drop (131, "sigaltstack"), (137, "statfs"), (138, "fstatfs"), @@ -907,6 +908,7 @@ mod tests { assert!(nrs.contains(&0), "read missing"); assert!(nrs.contains(&1), "write missing"); assert!(nrs.contains(&59), "execve missing"); + assert!(nrs.contains(&125), "capget missing"); assert!(nrs.contains(&231), "exit_group missing"); assert!(nrs.contains(&9), "mmap missing"); assert!(nrs.contains(&56), "clone missing"); diff --git a/docs/axis-isolation-contract.md b/docs/axis-isolation-contract.md index 7984558..8acb38a 100644 --- a/docs/axis-isolation-contract.md +++ b/docs/axis-isolation-contract.md @@ -138,7 +138,9 @@ Required behavior: - Environment filtering must remove inherited provider secrets and inherited proxy credentials unless AXIS intentionally adds sanitized values. - `run_as_user` must not run as root and must fail if the requested identity - cannot be prepared safely. + 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. - `identity: isolated` is portable intent for a backend-owned isolated token or account. It must not be implemented by guessing a host username. Windows MXC maps it to the actual least-privilege ProcessContainer token; legacy diff --git a/docs/linux-setup.md b/docs/linux-setup.md index 5e77f38..df16ab6 100644 --- a/docs/linux-setup.md +++ b/docs/linux-setup.md @@ -59,7 +59,7 @@ is unavailable for the default provider, AXIS fails before running the command. | `network.mode: allow` | Uses host networking while still applying filesystem, seccomp, identity, timeout, and requested resource policy. | No endpoint policies may be configured. Requested resource limits still need enforcement support. | | `network.mode: proxy` | Starts an AXIS CONNECT proxy behind a netns boundary that rejects direct egress. | `ip`, `iptables`, and either native `CAP_NET_ADMIN` or the optional AXIS netns helper. Kernel-log audit evidence additionally needs readable `/dev/kmsg`. | | Resource limits | `max_processes`, `max_memory_mb`, and `cpu_rate_percent` are enforced through cgroups v2 when available. | Writable cgroups v2. Memory-only rlimit fallback is documented; process-count rlimit fallback requires a dedicated `run_as_user`; CPU quota has no rlimit fallback. | -| `run_as_user` | Drops to a configured non-root user and prepares writable workspace state for that user. | The target user must already exist, must not be root, and must be usable by the current caller. | +| `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. | | Bubblewrap fallback | Can provide block-mode fallback when Landlock is unavailable. | A safe root-owned system `bwrap` executable. It is not a proxy-mode fallback unless proxy reachability is also implemented. | `0` for `max_processes`, `max_memory_mb`, or `cpu_rate_percent` means that