Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

100 changes: 98 additions & 2 deletions crates/axis-sandbox/src/linux/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Comment thread
johayang-amd marked this conversation as resolved.
Resolved

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ResolvedIdentity {
Expand Down Expand Up @@ -61,6 +76,7 @@ struct ProcessIds {
real_gid: u32,
effective_gid: u32,
saved_gid: u32,
effective_capabilities: u64,
}

impl ProcessIds {
Expand Down Expand Up @@ -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
));
}
Expand Down Expand Up @@ -152,6 +171,17 @@ fn current_process_ids() -> Result<ProcessIds, String> {
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,
Expand All @@ -160,6 +190,7 @@ fn current_process_ids() -> Result<ProcessIds, String> {
real_gid,
effective_gid,
saved_gid,
effective_capabilities,
})
}

Expand Down Expand Up @@ -451,6 +482,7 @@ mod tests {
real_gid: 0,
effective_gid: 0,
saved_gid: 0,
effective_capabilities: 0,
}
}

Expand All @@ -462,6 +494,7 @@ mod tests {
real_gid,
effective_gid: 0,
saved_gid: 0,
effective_capabilities: 0,
}
}

Expand Down Expand Up @@ -514,6 +547,7 @@ mod tests {
real_gid: 1000,
effective_gid: 1000,
saved_gid: 1000,
effective_capabilities: 0,
},
)
.unwrap_err();
Expand All @@ -534,6 +568,7 @@ mod tests {
real_gid: 2000,
effective_gid: 0,
saved_gid: 3000,
effective_capabilities: 0,
},
"caller UID state",
),
Expand All @@ -546,6 +581,7 @@ mod tests {
real_gid: 2000,
effective_gid: 0,
saved_gid: 3000,
effective_capabilities: 0,
},
"caller UID state",
),
Expand All @@ -558,6 +594,7 @@ mod tests {
real_gid: 2000,
effective_gid: 1000,
saved_gid: 3000,
effective_capabilities: 0,
},
"caller GID state",
),
Expand All @@ -570,6 +607,7 @@ mod tests {
real_gid: 2000,
effective_gid: 0,
saved_gid: 1000,
effective_capabilities: 0,
},
"caller GID state",
),
Expand All @@ -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();
Expand Down
15 changes: 8 additions & 7 deletions crates/axis-sandbox/src/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,
Expand Down
18 changes: 9 additions & 9 deletions crates/axis-sandbox/src/linux/mxc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions crates/axis-sandbox/src/linux/seccomp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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");
Expand Down
4 changes: 3 additions & 1 deletion docs/axis-isolation-contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/linux-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading