phoebus_os/installer-rs/crates/phoebus-executor/tests/isolation_acceptance.rs
2026-10-12 04:27:15 -07:00

183 lines
6.2 KiB
Rust

//! Negative acceptance tests. Run explicitly on a Linux host with working
//! user namespaces, Bubblewrap and a systemd user manager. Only disposable
//! fixture files in the repository and /tmp are created.
use phoebus_executor::agent_jail::{prepare_jail, AgentJailConfig};
use std::process::Command;
#[test]
#[ignore = "requires real Bubblewrap user namespaces"]
fn developer_jail_denies_private_files_environment_processes_and_network() {
let temp = tempfile::tempdir().unwrap();
let home = temp.path().join("home");
let workspace = home.join("workspace");
std::fs::create_dir_all(home.join(".ssh")).unwrap();
std::fs::create_dir(&workspace).unwrap();
std::fs::write(home.join("private"), "synthetic credential").unwrap();
std::fs::write(home.join(".ssh/key"), "synthetic key").unwrap();
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let port = listener.local_addr().unwrap().port();
let script = format!(
r#"
import os, socket
home = {home:?}
ws = {workspace:?}
assert 'PHOEBUS_SYNTHETIC_CREDENTIAL' not in os.environ
for path in [home+'/private', home+'/.ssh/key', '/proc/{pid}/environ']:
try:
open(path).read()
except (FileNotFoundError, PermissionError):
pass
else:
raise AssertionError('private path readable: '+path)
assert os.statvfs('/etc').f_flag & os.ST_RDONLY
with open(ws+'/allowed', 'w') as f: f.write('workspace allowed')
s = socket.socket()
s.settimeout(1)
try:
s.connect(('127.0.0.1', {port}))
except OSError:
pass
else:
raise AssertionError('host network reachable')
print('denials passed')
"#,
home = home.to_string_lossy(),
workspace = workspace.to_string_lossy(),
pid = std::process::id()
);
let config = AgentJailConfig {
workspace: workspace.clone(),
allow_network: false,
allow_nix_store: false,
extra_masked_subdirs: vec![],
};
let jail = prepare_jail(&config, &home, "/usr/bin/python3", &["-c".into(), script]).unwrap();
// Replace the reviewed pathname after preparation. The jail must bind the
// original directory, never the credential directory behind the new link.
let original = home.join("original-workspace");
std::fs::rename(&workspace, &original).unwrap();
std::os::unix::fs::symlink(home.join(".ssh"), &workspace).unwrap();
let out = Command::new("bwrap")
.args(&jail.argv)
.env("PHOEBUS_SYNTHETIC_CREDENTIAL", "synthetic")
.output()
.unwrap();
assert!(
out.status.success(),
"{}\n{}",
String::from_utf8_lossy(&out.stdout),
String::from_utf8_lossy(&out.stderr)
);
assert_eq!(
std::fs::read_to_string(original.join("allowed")).unwrap(),
"workspace allowed"
);
assert_eq!(
std::fs::read_to_string(home.join("private")).unwrap(),
"synthetic credential"
);
}
#[test]
#[ignore = "requires real systemd user namespace hardening"]
fn model_unit_denies_home_environment_processes_and_writes() {
// This location is beneath the account's home in the development checkout,
// so ProtectHome must hide it. No account credential is read by the probe.
let root = std::env::var_os("PHOEBUS_ISOLATION_FIXTURE_ROOT")
.map(std::path::PathBuf::from)
.unwrap_or_else(|| {
std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.ancestors()
.nth(3)
.unwrap()
.to_path_buf()
});
let fixture = tempfile::Builder::new()
.prefix(".isolation-fixture-")
.tempdir_in(&root)
.unwrap();
let private = fixture.path().join("private");
std::fs::write(&private, "synthetic").unwrap();
let home =
phoebus_executor::personal::home_for_uid(phoebus_executor::personal::effective_uid())
.unwrap();
assert!(
private.starts_with(home),
"acceptance fixture must be under the account home"
);
let script = format!(
r#"
import os
assert 'PHOEBUS_SYNTHETIC_CREDENTIAL' not in os.environ
for path in [{private:?}, '/proc/{pid}/environ']:
try:
open(path).read()
except (FileNotFoundError, PermissionError):
pass
else:
raise AssertionError('private path readable: '+path)
assert os.statvfs('/etc').f_flag & os.ST_RDONLY
print('denials passed')
"#,
private = private.to_string_lossy(),
pid = std::process::id()
);
let args = phoebus_executor::sandbox::user_argv("/usr/bin/python3", &["-c".into(), script]);
let out = Command::new("systemd-run")
.args(args)
.env("PHOEBUS_SYNTHETIC_CREDENTIAL", "synthetic")
.output()
.unwrap();
assert!(
out.status.success(),
"{}\n{}",
String::from_utf8_lossy(&out.stdout),
String::from_utf8_lossy(&out.stderr)
);
assert!(String::from_utf8_lossy(&out.stdout).contains("denials passed"));
}
#[test]
#[ignore = "requires root and a real systemd system manager"]
fn model_system_unit_denies_root_home_environment_and_writes() {
assert_eq!(
phoebus_executor::personal::effective_uid(),
0,
"run this probe only as root in a disposable VM"
);
let fixture = tempfile::Builder::new()
.prefix(".phoebus-isolation-")
.tempdir_in("/root")
.unwrap();
let private = fixture.path().join("private");
std::fs::write(&private, "synthetic").unwrap();
let script = format!(
r#"
import os
assert 'PHOEBUS_SYNTHETIC_CREDENTIAL' not in os.environ
assert os.geteuid() != 0
try:
open({private:?}).read()
except (FileNotFoundError, PermissionError):
pass
else:
raise AssertionError('root home readable')
assert os.statvfs('/etc').f_flag & os.ST_RDONLY
print('denials passed')
"#,
private = private.to_string_lossy()
);
let args = phoebus_executor::sandbox::argv("/usr/bin/python3", &["-c".into(), script]);
let out = Command::new("systemd-run")
.args(args)
.env("PHOEBUS_SYNTHETIC_CREDENTIAL", "synthetic")
.output()
.unwrap();
assert!(
out.status.success(),
"{}\n{}",
String::from_utf8_lossy(&out.stdout),
String::from_utf8_lossy(&out.stderr)
);
assert!(String::from_utf8_lossy(&out.stdout).contains("denials passed"));
}