use nix::unistd::Pid;
use sandbox_core::{Result, SandboxError};
use std::fs;
pub fn setup_user_namespace(child_pid: Pid, uid: u32, gid: u32) -> Result<()> {
let pid = child_pid.as_raw();
let uid_map = format!("0 {} 1\n", uid);
fs::write(format!("/proc/{}/uid_map", pid), &uid_map).map_err(|e| {
SandboxError::Namespace(format!("Failed to write uid_map for pid {}: {}", pid, e))
})?;
fs::write(format!("/proc/{}/setgroups", pid), "deny\n").map_err(|e| {
SandboxError::Namespace(format!("Failed to write setgroups for pid {}: {}", pid, e))
})?;
let gid_map = format!("0 {} 1\n", gid);
fs::write(format!("/proc/{}/gid_map", pid), &gid_map).map_err(|e| {
SandboxError::Namespace(format!("Failed to write gid_map for pid {}: {}", pid, e))
})?;
Ok(())
}
#[cfg(test)]
mod tests {
#[test]
fn test_setup_user_namespace_invalid_pid() {
let result =
super::setup_user_namespace(nix::unistd::Pid::from_raw(999_999_999), 1000, 1000);
assert!(result.is_err());
}
}