use std::path::{Path, PathBuf};
use anyhow::{Context, Result};
use serde_json::Value;
use crate::config::short_hash;
use crate::state::State;
const MOBY: &str = include_str!("seccomp/moby-default-v0.2.4.json");
const RULES: [&str; 2] = [
r#"{"names":["clone","unshare"],"action":"SCMP_ACT_ALLOW","args":[{"index":0,"value":268435456,"valueTwo":268435456,"op":"SCMP_CMP_MASKED_EQ"}],"excludes":{"caps":["CAP_SYS_ADMIN"],"arches":["s390","s390x"]}}"#,
r#"{"names":["clone"],"action":"SCMP_ACT_ALLOW","args":[{"index":0,"value":536870912,"valueTwo":536870912,"op":"SCMP_CMP_MASKED_EQ"}],"excludes":{"caps":["CAP_SYS_ADMIN"],"arches":["s390","s390x"]}}"#,
];
pub fn profile() -> String {
let mut profile: Value = serde_json::from_str(MOBY).expect("vendored seccomp profile is valid JSON");
let syscalls = profile["syscalls"].as_array_mut().expect("vendored seccomp profile has `syscalls`");
syscalls.extend(RULES.map(|r| serde_json::from_str::<Value>(r).expect("seccomp rule is valid JSON")));
serde_json::to_string(&profile).expect("a JSON value serializes")
}
pub fn ensure_file() -> Result<PathBuf> {
let state = State::path()?;
ensure_file_in(state.parent().context("state path has no parent")?)
}
pub(crate) fn ensure_file_in(dir: &Path) -> Result<PathBuf> {
let profile = profile();
let path = dir.join(format!("seccomp-{}.json", short_hash(&profile)));
if path.exists() {
return Ok(path);
}
std::fs::create_dir_all(dir).with_context(|| format!("cannot create {}", dir.display()))?;
let tmp = dir.join(format!(".seccomp.json.{}.tmp", std::process::id()));
std::fs::write(&tmp, profile).with_context(|| format!("cannot write {}", tmp.display()))?;
std::fs::rename(&tmp, &path).with_context(|| format!("cannot write {}", path.display()))?;
Ok(path)
}
#[cfg(test)]
mod tests {
use super::*;
fn syscalls(json: &str) -> Vec<Value> {
serde_json::from_str::<Value>(json).unwrap()["syscalls"].as_array().unwrap().clone()
}
#[test]
fn keeps_every_moby_rule_and_appends_ours() {
let moby = syscalls(MOBY);
let ours = syscalls(&profile());
assert_eq!(ours.len(), moby.len() + 2);
assert_eq!(ours[..moby.len()], moby[..]);
let full: Value = serde_json::from_str(&profile()).unwrap();
let vendored: Value = serde_json::from_str(MOBY).unwrap();
for key in ["defaultAction", "defaultErrnoRet", "archMap"] {
assert_eq!(full[key], vendored[key], "{key}");
}
}
#[test]
fn rules_allow_newuser_and_newpid_without_sys_admin() {
let ours = syscalls(&profile());
let excludes = serde_json::json!({"caps": ["CAP_SYS_ADMIN"], "arches": ["s390", "s390x"]});
let rule = |names: Value, flag: u64| {
serde_json::json!({
"names": names,
"action": "SCMP_ACT_ALLOW",
"args": [{"index": 0, "value": flag, "valueTwo": flag, "op": "SCMP_CMP_MASKED_EQ"}],
"excludes": excludes,
})
};
let n = ours.len();
assert_eq!(ours[n - 2], rule(serde_json::json!(["clone", "unshare"]), 0x1000_0000));
assert_eq!(ours[n - 1], rule(serde_json::json!(["clone"]), 0x2000_0000));
}
#[test]
fn file_is_named_by_hash_and_written_once() {
let dir = std::env::temp_dir().join(format!("devsandbox-seccomp-test-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
let path = ensure_file_in(&dir).unwrap();
assert_eq!(path, dir.join(format!("seccomp-{}.json", short_hash(&profile()))));
assert_eq!(std::fs::read_to_string(&path).unwrap(), profile());
std::fs::write(&path, "kept").unwrap();
assert_eq!(ensure_file_in(&dir).unwrap(), path);
assert_eq!(std::fs::read_to_string(&path).unwrap(), "kept");
assert_eq!(std::fs::read_dir(&dir).unwrap().count(), 1, "no temp file left");
std::fs::remove_dir_all(&dir).unwrap();
}
#[test_utils::docker_test(docker_only)]
fn allows_unprivileged_userns_with_docker() -> Result<(), &'static str> {
let image = "alpine:3.20";
let pulled = crate::test_support::runtime_cmd().args(["pull", "-q", image]).output().map_err(|_| "cannot run docker")?;
if !pulled.status.success() {
return Err("cannot pull alpine:3.20");
}
let dir = std::env::temp_dir().join(format!("devsandbox-seccomp-docker-test-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
crate::test_support::with_cleanup(
|| drop(std::fs::remove_dir_all(&dir)),
|| {
let path = ensure_file_in(&dir).map_err(|_| "cannot write the profile")?;
let unshare = |opts: &[String]| {
let out = crate::test_support::runtime_cmd()
.args(["run", "--rm", "--user", "1000:1000"])
.args(opts)
.args([image, "unshare", "-Ur", "true"])
.output()
.unwrap();
(out.status.success(), String::from_utf8_lossy(&out.stderr).into_owned())
};
let (ok, stderr) = unshare(&[]);
assert!(!ok, "docker's default profile allowed it: {stderr}");
let (ok, stderr) = unshare(&["--security-opt".into(), format!("seccomp={}", path.display())]);
assert!(ok, "ours didn't: {stderr}");
Ok(())
},
)
}
}