use std::collections::HashSet;
use crate::mxc_common::filesystem_resolve::FsIntent;
use crate::mxc_common::models::ContainerPolicy;
use crate::mxc_common::models::{ExecutionRequest, NetworkAction, ProxyAddress};
use crate::mxc_common::proxy_env::{is_managed_proxy_key, PROXY_SET_KEYS};
pub(crate) const COMMAND_TAIL: [&str; 3] = ["--", "sh", "-c"];
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct PolicyMount {
pub source: String,
pub destination: String,
pub intent: FsIntent,
pub readonly_overlay: Option<String>,
}
pub(crate) fn policy_mounts(policy: &ContainerPolicy) -> Vec<PolicyMount> {
crate::mxc_common::filesystem_resolve::resolve_mount_order(policy)
.into_iter()
.map(|mount| PolicyMount {
destination: mount.path.clone(),
source: mount.path,
intent: mount.intent,
readonly_overlay: None,
})
.collect()
}
pub(crate) const BASELINE_RO_BIND_PATHS: &[&str] = &[
"/bin",
"/sbin",
"/lib",
"/lib32",
"/lib64",
"/libx32",
"/usr/bin",
"/usr/sbin",
"/usr/lib",
"/usr/lib32",
"/usr/lib64",
"/usr/libx32",
"/usr/libexec",
"/usr/share",
"/etc",
];
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct BaselineLink {
pub path: &'static str,
pub relative_target: String,
}
#[cfg(any(target_os = "linux", test))]
pub(crate) fn baseline_links(
host_link_target: impl Fn(&str) -> std::io::Result<Option<std::path::PathBuf>>,
) -> Result<Vec<BaselineLink>, String> {
let refuse = |reason: String| {
format!(
"{reason}. Refusing to start: mounting it apart from its target would let it \
bypass filesystem.deniedPaths."
)
};
let targets = BASELINE_RO_BIND_PATHS
.iter()
.map(|path| {
host_link_target(path)
.map_err(|error| refuse(format!("could not resolve the host's {path} ({error})")))
})
.collect::<Result<Vec<_>, _>>()?;
let entries = || BASELINE_RO_BIND_PATHS.iter().zip(&targets);
let inside_bound_entry = |target: &std::path::Path| {
entries().any(|(entry, link)| link.is_none() && target.starts_with(entry))
};
let mut links = Vec::new();
for (&path, target) in entries() {
let Some(target) = target
.as_deref()
.filter(|target| inside_bound_entry(target))
else {
continue;
};
let relative_target = std::path::Path::new(path)
.parent()
.and_then(|parent| relative_path(parent, target))
.ok_or_else(|| {
refuse(format!(
"the host's {path} points to {}, which cannot be written as a relative link",
target.display()
))
})?;
links.push(BaselineLink {
path,
relative_target,
});
}
Ok(links)
}
#[cfg(any(target_os = "linux", test))]
fn relative_path(from: &std::path::Path, to: &std::path::Path) -> Option<String> {
if !from.has_root() || !to.has_root() {
return None;
}
let from: Vec<_> = from.components().collect();
let to: Vec<_> = to.components().collect();
let shared = from.iter().zip(&to).take_while(|(a, b)| a == b).count();
let mut parts = vec![".."; from.len() - shared];
for part in &to[shared..] {
parts.push(part.as_os_str().to_str()?);
}
Some(parts.join("/")).filter(|path| !path.is_empty())
}
#[cfg(any(target_os = "linux", test))]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub(crate) struct GrantLink {
pub path: String,
pub target: String,
pub passes_through: Vec<GrantLink>,
pub climbs_out_of: Vec<String>,
}
#[cfg(any(target_os = "linux", test))]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub(crate) struct AliasedGrant {
pub real_path: String,
pub links: Vec<GrantLink>,
pub inner: Option<GrantLink>,
pub climbs_out_of: Vec<String>,
}
#[cfg(any(target_os = "linux", test))]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum SandboxEntry<'a> {
HostBind,
Symlink {
target: &'a str,
},
Closed,
Tmpfs,
Directory,
}
#[cfg(any(target_os = "linux", test))]
#[derive(Debug, Default, PartialEq, Eq)]
pub(crate) struct GrantLinkPlan {
pub args: Vec<String>,
pub unavailable: Vec<GrantLink>,
}
#[cfg(any(target_os = "linux", test))]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum LinkPlacement<'a> {
Create,
Present,
HostLink,
CreateInside,
SandboxLink {
target: &'a str,
},
Blocked,
}
#[cfg(any(target_os = "linux", test))]
pub(crate) fn plan_grant_links(mount_args: &[String], grants: &[AliasedGrant]) -> GrantLinkPlan {
let mut plan = GrantLinkPlan::default();
if grants.is_empty() {
return plan;
}
let layout = sandbox_layout(mount_args);
let place = |path: &str| match &layout {
Some(layout) => link_placement(std::path::Path::new(path), layout),
None => LinkPlacement::Blocked,
};
let mut reachable = std::collections::HashMap::new();
for grant in grants {
for directory in &grant.climbs_out_of {
if !ensure_directory(directory, &place, &mut reachable, &mut plan.args) {
plan.unavailable.push(GrantLink {
path: directory.clone(),
target: grant.real_path.clone(),
..Default::default()
});
}
}
for link in &grant.links {
let next_level;
let (link, placement) = match place(&link.path) {
LinkPlacement::CreateInside => {
next_level = inner_link_under(grant, link, &link.path);
match next_level.as_ref().map(|next| (next, place(&next.path))) {
Some((
next,
placement @ (LinkPlacement::Create
| LinkPlacement::Present
| LinkPlacement::HostLink),
)) => (next, placement),
_ => (link, LinkPlacement::Blocked),
}
}
LinkPlacement::SandboxLink { target } => {
let leads_to = sandbox_link_target(&link.path, target);
if leads_to == link.target {
(link, LinkPlacement::Present)
} else {
next_level = inner_link_under(grant, link, &leads_to);
match next_level.as_ref().map(|next| (next, place(&next.path))) {
Some((next, LinkPlacement::Create)) => (next, LinkPlacement::Create),
_ => (link, LinkPlacement::Blocked),
}
}
}
placement => (link, placement),
};
if reachable.contains_key(link.path.as_str()) {
continue;
}
if !make_reachable(link, placement, &place, &mut reachable, &mut plan.args) {
plan.unavailable.push(link.clone());
}
}
}
plan
}
#[cfg(any(target_os = "linux", test))]
fn make_reachable<'a>(
link: &GrantLink,
placement: LinkPlacement<'a>,
place: &dyn Fn(&str) -> LinkPlacement<'a>,
reachable: &mut std::collections::HashMap<String, bool>,
args: &mut Vec<String>,
) -> bool {
let reaches = match placement {
LinkPlacement::Present => true,
LinkPlacement::Create => match link_text(link) {
Some(text) => {
args.extend(["--symlink".into(), text, link.path.clone()]);
true
}
None => false,
},
LinkPlacement::HostLink => {
link.climbs_out_of
.iter()
.all(|directory| ensure_directory(directory, place, reachable, args))
&& link.passes_through.iter().all(|hop| {
std::path::Path::new(&link.target).starts_with(&hop.target)
&& match reachable.get(hop.path.as_str()).copied() {
Some(reaches) => reaches,
None => make_reachable(hop, place(&hop.path), place, reachable, args),
}
})
}
LinkPlacement::SandboxLink { target } => {
sandbox_link_target(&link.path, target) == link.target
}
LinkPlacement::CreateInside | LinkPlacement::Blocked => false,
};
reachable.insert(link.path.clone(), reaches);
reaches
}
#[cfg(any(target_os = "linux", test))]
fn ensure_directory<'a>(
directory: &str,
place: &dyn Fn(&str) -> LinkPlacement<'a>,
reachable: &mut std::collections::HashMap<String, bool>,
args: &mut Vec<String>,
) -> bool {
if let Some(&exists) = reachable.get(directory) {
return exists;
}
let exists = match place(directory) {
LinkPlacement::Present | LinkPlacement::HostLink | LinkPlacement::CreateInside => true,
LinkPlacement::Create => {
args.extend(["--dir".into(), directory.into()]);
true
}
LinkPlacement::SandboxLink { .. } | LinkPlacement::Blocked => false,
};
reachable.insert(directory.into(), exists);
exists
}
#[cfg(any(target_os = "linux", test))]
fn inner_link_under(grant: &AliasedGrant, link: &GrantLink, directory: &str) -> Option<GrantLink> {
let inner = grant.inner.as_ref()?;
let name = inner.path.strip_prefix(&link.path)?.strip_prefix('/')?;
if name.is_empty() || name.contains('/') {
return None;
}
Some(GrantLink {
path: format!("{}/{name}", directory.trim_end_matches('/')),
target: inner.target.clone(),
..Default::default()
})
}
#[cfg(any(target_os = "linux", test))]
fn sandbox_link_target(location: &str, target: &str) -> String {
let parent = if target.starts_with('/') {
""
} else {
location.rsplit_once('/').map_or("", |(parent, _)| parent)
};
let mut parts = Vec::new();
for part in parent.split('/').chain(target.split('/')) {
match part {
"" | "." => {}
".." => {
parts.pop();
}
part => parts.push(part),
}
}
format!("/{}", parts.join("/"))
}
#[cfg(any(target_os = "linux", test))]
fn sandbox_layout(mount_args: &[String]) -> Option<Vec<(&str, SandboxEntry<'_>)>> {
let mut layout = Vec::new();
let mut overlay_source = None;
let mut args = mount_args.iter().map(String::as_str);
while let Some(option) = args.next() {
let entry = match option {
"--unshare-user" | "--unshare-pid" | "--unshare-ipc" | "--unshare-uts"
| "--unshare-net" | "--die-with-parent" => continue,
"--cap-drop" => {
args.next()?;
continue;
}
"--dev" | "--proc" | "--remount-ro" => SandboxEntry::Closed,
"--tmpfs" => SandboxEntry::Tmpfs,
"--overlay-src" => {
overlay_source = Some(args.next()?);
continue;
}
"--ro-overlay" => {
let destination = args.next()?;
let entry = if overlay_source.take()? == destination {
SandboxEntry::HostBind
} else {
SandboxEntry::Closed
};
layout.push((destination, entry));
continue;
}
"--dir" => SandboxEntry::Directory,
"--symlink" => SandboxEntry::Symlink {
target: args.next()?,
},
"--file" => {
args.next()?;
SandboxEntry::Closed
}
"--bind" | "--ro-bind" | "--ro-bind-try" => {
let source = args.next()?;
let destination = args.next()?;
let entry = if source == destination {
SandboxEntry::HostBind
} else {
SandboxEntry::Closed
};
layout.push((destination, entry));
continue;
}
_ => return None,
};
layout.push((args.next()?, entry));
}
overlay_source.is_none().then_some(layout)
}
#[cfg(any(target_os = "linux", test))]
fn link_placement<'a>(
link: &std::path::Path,
layout: &[(&str, SandboxEntry<'a>)],
) -> LinkPlacement<'a> {
let mut placement = LinkPlacement::Create;
for &(path, entry) in layout {
let path = std::path::Path::new(path);
placement = if link == path {
match entry {
SandboxEntry::HostBind => LinkPlacement::Present,
SandboxEntry::Symlink { target } => LinkPlacement::SandboxLink { target },
SandboxEntry::Tmpfs => LinkPlacement::CreateInside,
SandboxEntry::Directory if placement == LinkPlacement::Create => {
LinkPlacement::CreateInside
}
SandboxEntry::Directory => placement,
SandboxEntry::Closed => LinkPlacement::Blocked,
}
} else if link.starts_with(path) {
match entry {
SandboxEntry::HostBind => LinkPlacement::HostLink,
SandboxEntry::Tmpfs => LinkPlacement::Create,
SandboxEntry::Directory => placement,
SandboxEntry::Symlink { .. } | SandboxEntry::Closed => LinkPlacement::Blocked,
}
} else if path.starts_with(link) && placement == LinkPlacement::Create {
LinkPlacement::CreateInside
} else {
continue;
};
}
placement
}
#[cfg(any(target_os = "linux", test))]
fn link_text(link: &GrantLink) -> Option<String> {
let directory = std::path::Path::new(&link.path).parent()?;
let target = std::path::Path::new(&link.target);
if directory == target {
return Some(".".into());
}
relative_path(directory, target)
}
#[cfg(any(target_os = "linux", test))]
pub(crate) const RESOLVER_DIRS: &[&str] = &[
"/run/systemd/resolve",
"/run/NetworkManager",
"/run/resolvconf",
];
#[cfg(any(target_os = "linux", test))]
pub(crate) const WSL_RESOLV_CONF: &str = "/mnt/wsl/resolv.conf";
pub(crate) const COMPAT_RUN_LINK: &str = "/var/run";
pub(crate) const COMPAT_RUN_TARGET: &str = "/run";
#[cfg(target_os = "linux")]
pub(crate) fn path_intent(
policy: &ContainerPolicy,
target: impl AsRef<std::path::Path>,
) -> Option<FsIntent> {
let target = target.as_ref();
crate::mxc_common::filesystem_resolve::resolve_mount_order(policy)
.iter()
.rfind(|mount| target.starts_with(&mount.path))
.map(|mount| mount.intent)
}
#[cfg(target_os = "linux")]
pub(crate) fn path_is_denied(
policy: &ContainerPolicy,
target: impl AsRef<std::path::Path>,
) -> bool {
path_intent(policy, target) == Some(FsIntent::Denied)
}
#[cfg(any(target_os = "linux", test))]
pub(crate) fn resolver_target_without_matching_mount(
policy: &ContainerPolicy,
target: &str,
) -> bool {
if target == "/"
|| !target.starts_with('/')
|| target.split('/').any(|part| part == "." || part == "..")
{
return false;
}
let mut policy_mounts = policy
.readwrite_paths
.iter()
.chain(&policy.readonly_paths)
.chain(&policy.denied_paths)
.map(String::as_str);
for mount in BASELINE_RO_BIND_PATHS
.iter()
.copied()
.chain(RESOLVER_DIRS.iter().copied())
.chain([WSL_RESOLV_CONF])
.chain(["/dev", "/proc", "/tmp", "/var/run"])
.chain(policy_mounts.clone())
{
if !mount.starts_with('/')
|| mount.split('/').any(|part| part == "." || part == "..")
|| paths_overlap(target, mount)
{
return false;
}
}
!policy_mounts.any(|mount| paths_overlap("/etc/resolv.conf", mount))
}
#[cfg(any(target_os = "linux", test))]
fn paths_overlap(a: &str, b: &str) -> bool {
let a = std::path::Path::new(a);
let b = std::path::Path::new(b);
a.starts_with(b) || b.starts_with(a)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ResolvedNetworkMode {
Isolated,
FirewallEnforced,
ProxyOnly,
}
impl ResolvedNetworkMode {
pub(crate) fn from_request(request: &ExecutionRequest, proxy_active: bool) -> Self {
if proxy_active {
return Self::ProxyOnly;
}
let egress = request.policy.network_egress.as_ref();
if egress.is_none_or(|egress| {
egress.default == NetworkAction::Deny
&& egress.allow.is_empty()
&& egress.deny.is_empty()
}) {
Self::Isolated
} else {
Self::FirewallEnforced
}
}
pub(crate) fn uses_external_userns(self) -> bool {
matches!(self, Self::ProxyOnly | Self::FirewallEnforced)
}
}
pub fn directional_network_rejection(request: &ExecutionRequest) -> Option<&'static str> {
let egress = request.policy.network_egress.as_ref();
let ingress = request.policy.network_ingress.as_ref();
if egress.is_none() && ingress.is_none() {
return None;
}
if let Some(ingress) = ingress {
if ingress.default == NetworkAction::Allow {
return Some(BWRAP_INGRESS_DEFAULT_ALLOW);
}
if ingress.host_loopback == NetworkAction::Allow {
return Some(BWRAP_HOST_LOOPBACK_ALLOW);
}
}
None
}
pub const BWRAP_INGRESS_DEFAULT_ALLOW: &str =
"Bubblewrap: network.ingress.default='allow' is not supported. The sandbox runs in a \
private network namespace reached through slirp, which has no route in until a host \
port is forwarded to it, and the schema carries no port list to forward. A listener \
inside the sandbox is reachable from the sandbox only. Use \
network.ingress.default='deny'.";
pub const BWRAP_HOST_LOOPBACK_ALLOW: &str =
"Bubblewrap: network.ingress.hostLoopback='allow' is not supported. The sandbox's \
loopback belongs to its own network namespace and is not the host's, so the host \
cannot dial a sandbox listener and nothing bridges the two. Use \
network.ingress.hostLoopback='deny'.";
#[cfg(any(target_os = "linux", test))]
pub(crate) const BWRAP_PROXY_DIRECTIONAL_EGRESS: &str =
"Bubblewrap: runtimeConfig.networkProxy cannot be combined with direct network.egress rules \
or default='allow'. \
A proxy resolves to proxy-only egress, whose chain opens the proxy endpoint alone and \
never reads network.egress, so the rules would be dropped in silence. Use \
network.egress.default='deny' with no allow/deny rules (the proxy-only posture), or \
remove runtimeConfig.networkProxy and express the policy with network.egress.";
#[cfg(any(target_os = "linux", test))]
pub(crate) fn proxy_with_egress_rejection(request: &ExecutionRequest) -> Option<&'static str> {
let proxy = &request.policy.network_proxy;
if !proxy.is_enabled() {
return None;
}
request
.policy
.network_egress
.as_ref()
.is_some_and(|egress| {
egress.default != NetworkAction::Deny
|| !egress.allow.is_empty()
|| !egress.deny.is_empty()
})
.then_some(BWRAP_PROXY_DIRECTIONAL_EGRESS)
}
const DEFAULT_PATH: &str = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin";
const DEFAULT_TERM: &str = "xterm-256color";
fn start_directory(request: &ExecutionRequest) -> Option<String> {
Some(request.working_directory.as_str())
.filter(|dir| !dir.is_empty())
.map(crate::mxc_common::models::sandbox_absolute_path)
}
#[cfg(any(target_os = "linux", test))]
pub(crate) fn cwd_without_matching_mount(request: &ExecutionRequest) -> Option<String> {
let dir = start_directory(request)?;
if dir == "/"
|| !request.working_directory.starts_with('/')
|| request
.working_directory
.split('/')
.any(|part| part == "." || part == "..")
{
return None;
}
let mounts = BASELINE_RO_BIND_PATHS
.iter()
.copied()
.chain(["/dev", "/proc", "/tmp", "/var/run"])
.chain(
request
.policy
.readwrite_paths
.iter()
.chain(&request.policy.readonly_paths)
.chain(&request.policy.denied_paths)
.map(String::as_str),
);
for mount in mounts {
if !mount.starts_with('/') || mount.split('/').any(|part| part == "." || part == "..") {
return None;
}
if path_is_within(&dir, mount) || path_is_within(mount, &dir) {
return None;
}
}
Some(dir)
}
#[cfg(any(target_os = "linux", test))]
fn path_is_within(path: &str, parent: &str) -> bool {
std::path::Path::new(path).starts_with(parent)
}
fn default_env(request: &ExecutionRequest) -> Vec<(String, String)> {
let mut entries = vec![("PATH".to_string(), DEFAULT_PATH.to_string())];
if let Some(home) = start_directory(request) {
entries.push(("HOME".to_string(), home));
}
entries.push(("TERM".to_string(), DEFAULT_TERM.to_string()));
entries
}
fn resolved_env(request: &ExecutionRequest) -> Vec<String> {
crate::mxc_common::default_env::resolve_env(request, || default_env(request))
}
pub fn build_args(request: &ExecutionRequest, proxy_address: Option<&ProxyAddress>) -> Vec<String> {
build_args_classified(request, proxy_address, &HashSet::new())
}
pub fn build_args_classified(
request: &ExecutionRequest,
proxy_address: Option<&ProxyAddress>,
denied_files: &HashSet<String>,
) -> Vec<String> {
let network_mode = ResolvedNetworkMode::from_request(request, proxy_address.is_some());
build_args_classified_with_mode(
request,
proxy_address,
denied_files,
network_mode,
None,
&[],
&[],
&policy_mounts(&request.policy),
true,
|_| Vec::new(),
)
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn build_args_classified_with_mode(
request: &ExecutionRequest,
proxy_address: Option<&ProxyAddress>,
denied_files: &HashSet<String>,
network_mode: ResolvedNetworkMode,
seccomp_fd: Option<i32>,
baseline_links: &[BaselineLink],
resolver_args: &[String],
mounts: &[PolicyMount],
private_tmp: bool,
grant_links: impl FnOnce(&[String]) -> Vec<String>,
) -> Vec<String> {
let mut args = Vec::new();
if !network_mode.uses_external_userns() {
args.push("--unshare-user".into());
}
args.extend(["--cap-drop".into(), "ALL".into()]);
args.extend(
["--unshare-pid", "--unshare-ipc", "--unshare-uts"]
.into_iter()
.map(String::from),
);
args.push("--die-with-parent".into());
args.push("--unshare-net".into());
for path in BASELINE_RO_BIND_PATHS {
match baseline_links.iter().find(|link| link.path == *path) {
Some(link) => args.extend([
"--symlink".into(),
link.relative_target.clone(),
(*path).into(),
]),
None => args.extend(["--ro-bind-try".into(), (*path).into(), (*path).into()]),
}
}
args.extend([
"--symlink".into(),
COMPAT_RUN_TARGET.into(),
COMPAT_RUN_LINK.into(),
]);
args.extend(["--dev".into(), "/dev".into()]);
args.extend(["--proc".into(), "/proc".into()]);
if private_tmp {
args.extend(["--tmpfs".into(), "/tmp".into()]);
}
args.extend_from_slice(resolver_args);
let mut denied_directories = Vec::new();
for mount in mounts {
match mount.intent {
FsIntent::ReadWrite => {
args.extend([
"--bind".into(),
mount.source.clone(),
mount.destination.clone(),
]);
}
FsIntent::ReadOnly => match &mount.readonly_overlay {
Some(layer) => args.extend([
"--overlay-src".into(),
layer.clone(),
"--overlay-src".into(),
mount.source.clone(),
"--ro-overlay".into(),
mount.destination.clone(),
]),
None => args.extend([
"--ro-bind".into(),
mount.source.clone(),
mount.destination.clone(),
]),
},
FsIntent::Denied => {
if denied_files.contains(&mount.source) {
args.extend([
"--ro-bind".into(),
"/dev/null".into(),
mount.destination.clone(),
]);
} else {
args.extend(["--tmpfs".into(), mount.destination.clone()]);
denied_directories.push(&mount.destination);
}
}
}
}
let grant_link_args = grant_links(&args);
args.extend(grant_link_args);
for destination in denied_directories {
args.extend(["--remount-ro".into(), destination.clone()]);
}
if let Some(dir) = start_directory(request) {
args.extend(["--chdir".into(), dir]);
}
args.push("--clearenv".into());
for env_str in resolved_env(request) {
if let Some((key, value)) = env_str.split_once('=') {
if proxy_address.is_some() && is_managed_proxy_key(key) {
continue;
}
args.extend(["--setenv".into(), key.into(), value.into()]);
}
}
if let Some(addr) = proxy_address {
let url = addr.to_url();
for key in PROXY_SET_KEYS {
args.extend(["--setenv".into(), (*key).into(), url.clone()]);
}
}
if let Some(fd) = seccomp_fd {
args.extend(["--seccomp".into(), fd.to_string()]);
}
args.extend(COMMAND_TAIL.iter().map(|arg| arg.to_string()));
args.push(request.script_code.clone());
args
}
#[cfg(test)]
mod tests {
use super::*;
fn base_request() -> ExecutionRequest {
ExecutionRequest {
script_code: "echo hello".into(),
working_directory: "/home/user".into(),
..Default::default()
}
}
#[test]
fn security_options_precede_the_command_in_every_network_mode() {
let request = base_request();
let proxy = ProxyAddress::new("127.0.0.1".into(), 3128);
for (mode, proxy_address) in [
(ResolvedNetworkMode::Isolated, None),
(ResolvedNetworkMode::FirewallEnforced, None),
(ResolvedNetworkMode::ProxyOnly, Some(&proxy)),
] {
let args = build_args_classified_with_mode(
&request,
proxy_address,
&HashSet::new(),
mode,
Some(17),
&[],
&[],
&policy_mounts(&request.policy),
true,
|mount_args| {
assert!(
sandbox_layout(mount_args).is_some(),
"{mode:?} capability flags must not invalidate the mount layout"
);
Vec::new()
},
);
assert_eq!(
&args[args.len() - 6..],
["--seccomp", "17", "--", "sh", "-c", "echo hello"],
"{mode:?}"
);
assert_eq!(args.iter().filter(|arg| *arg == "--seccomp").count(), 1);
assert!(!args.iter().any(|arg| arg == "--add-seccomp-fd"));
let command = args.iter().position(|arg| arg == "--").unwrap();
assert_eq!(
args[..command]
.windows(2)
.filter(|pair| *pair == ["--cap-drop", "ALL"])
.count(),
1,
"{mode:?} must explicitly drop every workload capability before exec"
);
assert!(!args.iter().any(|arg| arg == "--cap-add"));
}
}
#[test]
fn resolver_mounts_follow_virtual_filesystems_and_precede_policy() {
let mut request = base_request();
request.policy.denied_paths = vec!["/run".into()];
let resolver = vec![
"--tmpfs".into(),
"/run/resolver".into(),
"--file".into(),
"19".into(),
"/run/resolver/resolv.conf".into(),
"--remount-ro".into(),
"/run/resolver".into(),
];
let args = build_args_classified_with_mode(
&request,
None,
&HashSet::new(),
ResolvedNetworkMode::Isolated,
None,
&[],
&resolver,
&policy_mounts(&request.policy),
true,
|_| Vec::new(),
);
let temporary = args
.windows(2)
.position(|args| args == ["--tmpfs", "/tmp"])
.unwrap();
let file = args
.windows(2)
.position(|args| args == ["--file", "19"])
.unwrap();
let denial = args
.windows(2)
.position(|args| args == ["--tmpfs", "/run"])
.unwrap();
assert!(temporary < file, "private /tmp must not hide the resolver");
assert!(
file < denial,
"the resolver must not override filesystem policy"
);
}
#[test]
fn seccomp_preserves_option_like_operands_in_generated_arguments() {
let mut request = base_request();
request.env = Some(vec![
"FIRST=--seccomp".into(),
"SECOND=--add-seccomp-fd".into(),
"THIRD=--".into(),
]);
request.script_code = "--seccomp".into();
let mut expected = build_args(&request, None);
let command_index = expected.len() - 4;
expected.splice(
command_index..command_index,
["--seccomp".into(), "17".into()],
);
let args = build_args_classified_with_mode(
&request,
None,
&HashSet::new(),
ResolvedNetworkMode::Isolated,
Some(17),
&[],
&[],
&policy_mounts(&request.policy),
true,
|_| Vec::new(),
);
assert_eq!(args, expected);
for (key, value) in [
("FIRST", "--seccomp"),
("SECOND", "--add-seccomp-fd"),
("THIRD", "--"),
] {
assert!(args.windows(3).any(|args| args == ["--setenv", key, value]));
}
assert_eq!(
&args[args.len() - 6..],
["--seccomp", "17", "--", "sh", "-c", "--seccomp"]
);
}
#[test]
fn resolver_target_hint_requires_no_mount_overlap() {
let mut policy = ContainerPolicy::default();
assert!(resolver_target_without_matching_mount(
&policy,
"/opt/private/resolv.conf"
));
for no_warning in [
"/run/systemd/resolve/resolv.conf",
"/var/run/NetworkManager/resolv.conf",
"/mnt/wsl/resolv.conf",
"/etc/resolv.conf",
"/tmp/resolv.conf",
] {
assert!(
!resolver_target_without_matching_mount(&policy, no_warning),
"unexpected advisory for {no_warning}"
);
}
policy.readonly_paths = vec!["/opt/private".into()];
assert!(!resolver_target_without_matching_mount(
&policy,
"/opt/private/resolv.conf"
));
for mount in ["/opt/private/", "/opt//private/"] {
policy.readonly_paths = vec![mount.into()];
assert!(
!resolver_target_without_matching_mount(&policy, "/opt/private/resolv.conf"),
"{mount}"
);
}
policy.readonly_paths = vec!["/opt/private/resolv.conf/child".into()];
assert!(!resolver_target_without_matching_mount(
&policy,
"/opt/private/resolv.conf"
));
policy.readonly_paths = vec!["/opt/private2".into()];
assert!(resolver_target_without_matching_mount(
&policy,
"/opt/private/resolv.conf"
));
policy.readonly_paths = vec!["/".into()];
assert!(!resolver_target_without_matching_mount(
&policy,
"/opt/private/resolv.conf"
));
}
#[test]
fn resolver_diagnostic_recognizes_every_emitted_mount_destination() {
let mut with_policy = base_request();
with_policy.policy.readwrite_paths = vec!["/home/user/data".into()];
with_policy.policy.readonly_paths = vec!["/opt/private".into()];
with_policy.policy.denied_paths = vec!["/srv/blocked".into()];
for request in [base_request(), with_policy] {
let args = build_args(&request, None);
for triple in args.windows(3) {
if matches!(
triple[0].as_str(),
"--ro-bind-try" | "--ro-bind" | "--bind" | "--symlink"
) {
assert!(
!resolver_target_without_matching_mount(&request.policy, &triple[2]),
"unrecognized mount destination: {triple:?}"
);
}
}
for pair in args.windows(2) {
if matches!(pair[0].as_str(), "--dev" | "--proc" | "--tmpfs") {
assert!(
!resolver_target_without_matching_mount(&request.policy, &pair[1]),
"unrecognized virtual mount destination: {pair:?}"
);
}
}
}
}
#[test]
fn cwd_hint_requires_a_nonoverlapping_absolute_path() {
let mut request = base_request();
assert_eq!(
cwd_without_matching_mount(&request).as_deref(),
Some("/home/user")
);
request.working_directory = "/".into();
assert_eq!(cwd_without_matching_mount(&request), None);
request.working_directory = "".into();
assert_eq!(cwd_without_matching_mount(&request), None);
request.working_directory = "home/user".into();
assert_eq!(cwd_without_matching_mount(&request), None);
request.working_directory = "/home/../home/user".into();
assert_eq!(cwd_without_matching_mount(&request), None);
}
#[test]
fn cwd_overlap_is_inconclusive_in_either_direction() {
let mut request = base_request();
for cwd in ["/etc", "/etc/project", "/usr", "/tmp/work", "/var"] {
request.working_directory = cwd.into();
assert_eq!(cwd_without_matching_mount(&request), None, "{cwd}");
}
for mount in [
"/home",
"/home/user",
"/home/user/",
"/home//user/",
"/home/user/data",
"/home//user/data/",
] {
request.working_directory = "/home/user".into();
request.policy.readonly_paths = vec![mount.into()];
assert_eq!(cwd_without_matching_mount(&request), None, "{mount}");
}
for mount in ["/home/user2", "/home/user2/"] {
request.policy.readonly_paths = vec![mount.into()];
assert_eq!(
cwd_without_matching_mount(&request).as_deref(),
Some("/home/user"),
"{mount}"
);
}
request.policy.readonly_paths = vec!["/alias/../home/user".into()];
assert_eq!(cwd_without_matching_mount(&request), None);
request.policy.readonly_paths = vec!["/".into()];
assert_eq!(cwd_without_matching_mount(&request), None);
request.policy.readonly_paths.clear();
request.policy.denied_paths = vec!["/home/user/hidden".into()];
assert_eq!(cwd_without_matching_mount(&request), None);
}
#[test]
fn cwd_proof_uses_the_mounted_baseline_and_virtual_roots() {
let request = base_request();
let args = build_args(&request, None);
for mount in BASELINE_RO_BIND_PATHS {
assert!(
args.windows(3)
.any(|a| { a == ["--ro-bind-try", *mount, *mount] }),
"{mount}"
);
}
for (flag, path) in [
("--dev", "/dev"),
("--proc", "/proc"),
("--tmpfs", "/tmp"),
("--symlink", "/var/run"),
] {
assert!(
args.windows(2).any(|a| a[0] == flag && a[1] == path)
|| (flag == "--symlink"
&& args.windows(3).any(|a| a == ["--symlink", "/run", path])),
"{flag} {path}"
);
}
}
#[test]
fn ambiguous_resolver_paths_and_shadowed_link_do_not_warn() {
let mut policy = ContainerPolicy::default();
for target in ["relative/resolv.conf", "/opt/../secret/resolv.conf", "/"] {
assert!(!resolver_target_without_matching_mount(&policy, target));
}
policy.denied_paths = vec!["/etc".into()];
assert!(!resolver_target_without_matching_mount(
&policy,
"/opt/private/resolv.conf"
));
policy.denied_paths = vec!["/etc//".into()];
assert!(!resolver_target_without_matching_mount(
&policy,
"/opt/private/resolv.conf"
));
policy.denied_paths = vec!["/alias/../etc".into()];
assert!(!resolver_target_without_matching_mount(
&policy,
"/opt/private/resolv.conf"
));
}
#[test]
fn cwd_diagnostic_recognizes_every_emitted_mount_destination() {
let mut request = base_request();
let args = build_args(&request, None);
for triple in args.windows(3) {
if matches!(
triple[0].as_str(),
"--ro-bind-try" | "--ro-bind" | "--bind" | "--symlink"
) {
request.working_directory = triple[2].clone();
assert!(
cwd_without_matching_mount(&request).is_none(),
"unrecognized mount destination: {triple:?}"
);
}
}
for pair in args.windows(2) {
if matches!(pair[0].as_str(), "--dev" | "--proc" | "--tmpfs") {
request.working_directory = pair[1].clone();
assert!(
cwd_without_matching_mount(&request).is_none(),
"unrecognized virtual mount destination: {pair:?}"
);
}
}
}
mod env {
use super::*;
fn request() -> ExecutionRequest {
ExecutionRequest::default()
}
fn value<'a>(entries: &'a [String], key: &str) -> Option<&'a str> {
entries
.iter()
.find_map(|kv| kv.strip_prefix(&format!("{key}=")))
}
#[test]
fn a_direct_sdk_request_gets_the_default_block() {
let r = ExecutionRequest::default();
assert_eq!(value(&resolved_env(&r), "PATH"), Some(DEFAULT_PATH));
}
#[test]
fn an_omitted_env_gets_the_default_block() {
let mut r = request();
r.env = None;
r.working_directory = "/workspace".into();
let entries = resolved_env(&r);
assert_eq!(value(&entries, "PATH"), Some(DEFAULT_PATH));
assert_eq!(value(&entries, "HOME"), Some("/workspace"));
assert_eq!(value(&entries, "TERM"), Some(DEFAULT_TERM));
}
#[test]
fn the_default_path_covers_sbin() {
for dir in ["/usr/sbin", "/sbin", "/usr/bin", "/bin"] {
assert!(
DEFAULT_PATH.split(':').any(|entry| entry == dir),
"{dir} must be on the default PATH"
);
}
}
#[test]
fn an_explicitly_empty_env_stays_empty() {
let mut r = request();
r.env = Some(vec![]);
assert!(resolved_env(&r).is_empty());
}
#[test]
fn a_supplied_env_is_used_verbatim() {
let mut r = request();
r.env = Some(vec!["FOO=bar".into()]);
assert_eq!(resolved_env(&r), vec!["FOO=bar".to_string()]);
}
#[test]
fn inherit_default_env_layers_over_the_default_block() {
let mut r = request();
r.env = Some(vec!["FOO=bar".into(), "PATH=/only/mine".into()]);
r.inherit_default_env = true;
let entries = resolved_env(&r);
assert_eq!(value(&entries, "FOO"), Some("bar"));
assert_eq!(value(&entries, "TERM"), Some(DEFAULT_TERM));
assert_eq!(value(&entries, "PATH"), Some("/only/mine"));
assert_eq!(
entries.iter().filter(|kv| kv.starts_with("PATH=")).count(),
1
);
}
#[test]
fn home_follows_the_directory_the_child_starts_in() {
let mut r = request();
r.env = None;
r.working_directory = "/workspace".into();
assert_eq!(value(&resolved_env(&r), "HOME"), Some("/workspace"));
}
#[test]
fn a_caller_entry_without_a_value_reaches_no_setenv() {
for inherit in [false, true] {
let mut r = base_request();
r.env = Some(vec!["FEATURE_FLAG".into(), "FOO=bar".into()]);
r.inherit_default_env = inherit;
let args = build_args(&r, None);
assert!(
!args.iter().any(|a| a == "FEATURE_FLAG"),
"a valueless entry must not be set (inherit {inherit}): {args:?}"
);
assert!(
args.windows(3)
.any(|w| w[0] == "--setenv" && w[1] == "FOO" && w[2] == "bar"),
"the well-formed entry must survive (inherit {inherit}): {args:?}"
);
}
}
#[test]
fn a_policy_grant_alone_does_not_become_home() {
let mut r = request();
r.env = None;
r.working_directory = String::new();
r.policy.readwrite_paths = vec![std::env::temp_dir().display().to_string()];
assert_eq!(value(&resolved_env(&r), "HOME"), None);
let args = build_args(&r, None);
assert!(
!args.iter().any(|a| a == "--chdir"),
"a policy grant must not chdir the child: {args:?}"
);
}
#[test]
fn home_and_chdir_agree() {
for cwd in ["", "/workspace", "work", "./work", "a/../b", "/x/../y/./z"] {
let mut r = request();
r.env = None;
r.working_directory = cwd.into();
let args = build_args(&r, None);
let chdir = args
.windows(2)
.find(|w| w[0] == "--chdir")
.map(|w| w[1].clone());
let home = value(&resolved_env(&r), "HOME").map(str::to_string);
assert_eq!(
home, chdir,
"HOME must name the directory the child starts in (cwd {cwd:?})"
);
}
}
#[test]
fn a_relative_start_directory_is_anchored_to_the_sandbox_root() {
for (cwd, expected) in [
("work", "/work"),
("./work", "/work"),
("a/../b", "/b"),
("/x/../y/./z", "/y/z"),
] {
let mut r = request();
r.env = None;
r.working_directory = cwd.into();
assert_eq!(value(&resolved_env(&r), "HOME"), Some(expected));
}
}
#[test]
fn the_default_block_reaches_the_argument_list() {
let mut r = base_request();
r.env = None;
let args = build_args(&r, None);
let pos = args
.windows(3)
.position(|w| w[0] == "--setenv" && w[1] == "PATH" && w[2] == DEFAULT_PATH);
assert!(pos.is_some(), "PATH must be set via --setenv: {args:?}");
let clearenv = args.iter().position(|a| a == "--clearenv").unwrap();
assert!(
pos.unwrap() > clearenv,
"--setenv must follow --clearenv, else it is wiped"
);
}
}
#[test]
fn an_omitted_network_policy_is_isolated() {
let request = ExecutionRequest { ..base_request() };
assert_eq!(
ResolvedNetworkMode::from_request(&request, false),
ResolvedNetworkMode::Isolated
);
}
#[test]
fn basic_args_contain_namespace_flags() {
let args = build_args(&base_request(), None);
assert!(args.contains(&"--unshare-user".to_string()));
assert!(args.contains(&"--unshare-pid".to_string()));
assert!(args.contains(&"--unshare-ipc".to_string()));
assert!(args.contains(&"--unshare-uts".to_string()));
}
#[test]
fn basic_args_request_die_with_parent() {
let args = build_args(&base_request(), None);
assert!(args.contains(&"--die-with-parent".to_string()));
}
#[test]
fn a_directional_rule_selects_the_namespace_whose_chain_is_programmed() {
let request = directional_egress_request(NetworkAction::Deny, true);
assert_eq!(
ResolvedNetworkMode::from_request(&request, false),
ResolvedNetworkMode::FirewallEnforced
);
}
#[test]
fn a_ruleless_directional_policy_still_honors_the_inbound_posture() {
let denied = directional_egress_request(NetworkAction::Deny, false);
assert_eq!(
ResolvedNetworkMode::from_request(&denied, false),
ResolvedNetworkMode::Isolated
);
let allowed = directional_egress_request(NetworkAction::Allow, false);
let mode = ResolvedNetworkMode::from_request(&allowed, false);
assert_eq!(mode, ResolvedNetworkMode::FirewallEnforced);
assert!(build_args(&allowed, None).contains(&"--unshare-net".to_string()));
}
#[test]
fn no_directional_posture_shares_the_host_namespace() {
for default in [NetworkAction::Allow, NetworkAction::Deny] {
for with_rule in [false, true] {
let request = directional_egress_request(default, with_rule);
assert!(
build_args(&request, None).contains(&"--unshare-net".to_string()),
"default={default:?} with_rule={with_rule} omitted --unshare-net"
);
}
}
}
#[test]
fn a_directional_section_does_not_divert_a_proxy_run() {
let mut request = directional_egress_request(NetworkAction::Deny, false);
request.policy.network_proxy.address = Some(ProxyAddress::new("127.0.0.1".into(), 3128));
assert_eq!(
ResolvedNetworkMode::from_request(&request, true),
ResolvedNetworkMode::ProxyOnly
);
}
#[test]
fn an_ingress_only_directional_request_still_gets_a_private_namespace() {
use crate::mxc_common::models::NetworkIngressPolicy;
let mut request = base_request();
request.policy.network_ingress = Some(NetworkIngressPolicy::default());
let mode = ResolvedNetworkMode::from_request(&request, false);
assert_eq!(mode, ResolvedNetworkMode::Isolated);
assert!(build_args(&request, None).contains(&"--unshare-net".to_string()));
}
fn directional_egress_request(default: NetworkAction, with_rule: bool) -> ExecutionRequest {
use crate::mxc_common::models::{NetworkEgressPolicy, NetworkRule};
let mut request = base_request();
request.policy.network_egress = Some(NetworkEgressPolicy {
default,
allow: if with_rule {
vec![NetworkRule::default()]
} else {
Vec::new()
},
deny: Vec::new(),
});
request
}
fn directional_request(
ingress_default: NetworkAction,
host_loopback: NetworkAction,
) -> ExecutionRequest {
use crate::mxc_common::models::{NetworkEgressPolicy, NetworkIngressPolicy};
let mut request = base_request();
request.policy.network_egress = Some(NetworkEgressPolicy::default());
request.policy.network_ingress = Some(NetworkIngressPolicy {
default: ingress_default,
host_loopback,
});
request
}
#[test]
fn an_inbound_accepting_directional_posture_is_refused() {
let request = directional_request(NetworkAction::Allow, NetworkAction::Deny);
assert_eq!(
directional_network_rejection(&request),
Some(BWRAP_INGRESS_DEFAULT_ALLOW)
);
}
#[test]
fn a_host_loopback_accepting_posture_is_refused() {
let request = directional_request(NetworkAction::Deny, NetworkAction::Allow);
assert_eq!(
directional_network_rejection(&request),
Some(BWRAP_HOST_LOOPBACK_ALLOW)
);
}
#[test]
fn a_fully_denied_directional_posture_is_accepted() {
let request = directional_request(NetworkAction::Deny, NetworkAction::Deny);
assert_eq!(directional_network_rejection(&request), None);
}
#[test]
fn only_external_runtime_proxies_are_accepted() {
let mut request = directional_egress_request(NetworkAction::Deny, false);
request.policy.network_proxy.address = Some(ProxyAddress::new("127.0.0.1".into(), 3128));
assert!(proxy_with_egress_rejection(&request).is_none());
request.policy.network_egress = Some(crate::mxc_common::models::NetworkEgressPolicy {
default: NetworkAction::Allow,
..Default::default()
});
assert_eq!(
proxy_with_egress_rejection(&request),
Some(BWRAP_PROXY_DIRECTIONAL_EGRESS)
);
}
#[test]
fn filesystem_policy_produces_correct_mounts() {
let mut r = base_request();
r.policy.readwrite_paths = vec!["/workspace".into()];
r.policy.readonly_paths = vec!["/data".into()];
r.policy.denied_paths = vec!["/secrets".into()];
let args = build_args(&r, None);
let rw_pos = args.iter().position(|a| a == "--bind").unwrap();
assert_eq!(args[rw_pos + 1], "/workspace");
assert_eq!(args[rw_pos + 2], "/workspace");
args.windows(3)
.position(|w| w[0] == "--ro-bind" && w[1] == "/data" && w[2] == "/data")
.expect("readonly policy path /data should produce a --ro-bind mount");
let tmpfs_positions: Vec<_> = args
.iter()
.enumerate()
.filter(|(_, a)| *a == "--tmpfs")
.collect();
let secrets_mount = tmpfs_positions
.iter()
.find(|(i, _)| args[i + 1] == "/secrets");
assert!(
secrets_mount.is_some(),
"denied path should be tmpfs-masked"
);
}
fn policy_mount_pos(args: &[String], op: &str, path: &str) -> usize {
let dest_offset = if op == "--tmpfs" { 1 } else { 2 };
(0..args.len())
.rev()
.find(|&i| args[i] == op && args.get(i + dest_offset).map(String::as_str) == Some(path))
.unwrap_or_else(|| panic!("expected `{op} ... {path}` in args: {args:?}"))
}
#[test]
fn deep_denied_child_masks_rw_parent() {
let mut r = base_request();
r.policy.readwrite_paths = vec!["/data".into()];
r.policy.denied_paths = vec!["/data/secrets".into()];
let args = build_args(&r, None);
let parent = policy_mount_pos(&args, "--bind", "/data");
let child = policy_mount_pos(&args, "--tmpfs", "/data/secrets");
assert!(
child > parent,
"denied /data/secrets (pos {child}) must come after rw /data (pos {parent}) \
so it masks the subtree: {args:?}"
);
}
#[test]
fn deep_rw_child_survives_denied_parent() {
let mut r = base_request();
r.policy.readwrite_paths = vec!["/data/secrets".into()];
r.policy.denied_paths = vec!["/data".into()];
let args = build_args(&r, None);
let parent = policy_mount_pos(&args, "--tmpfs", "/data");
let child = policy_mount_pos(&args, "--bind", "/data/secrets");
assert!(
child > parent,
"rw /data/secrets (pos {child}) must come after denied /data (pos {parent}) \
so the deep bind is not shadowed by the mask: {args:?}"
);
}
#[test]
fn denied_directory_is_masked_with_tmpfs() {
let mut r = base_request();
r.policy.denied_paths = vec!["/secrets".into()];
let denied_files = HashSet::new();
let args = build_args_classified(&r, None, &denied_files);
policy_mount_pos(&args, "--tmpfs", "/secrets");
assert!(
args.windows(2)
.any(|w| w[0] == "--remount-ro" && w[1] == "/secrets"),
"a denied directory mask must be read-only: {args:?}"
);
assert!(
args.windows(3)
.all(|w| !(w[0] == "--ro-bind" && w[1] == "/dev/null" && w[2] == "/secrets")),
"a directory denied path must not be masked with /dev/null: {args:?}"
);
}
#[test]
fn denied_file_is_masked_with_dev_null() {
let mut r = base_request();
r.policy.denied_paths = vec!["/etc/shadow".into()];
let denied_files = HashSet::from(["/etc/shadow".to_string()]);
let args = build_args_classified(&r, None, &denied_files);
let pos = args
.windows(3)
.position(|w| w[0] == "--ro-bind" && w[1] == "/dev/null" && w[2] == "/etc/shadow");
assert!(
pos.is_some(),
"a file denied path must be masked with `--ro-bind /dev/null`: {args:?}"
);
assert!(
args.windows(2)
.all(|w| !(w[0] == "--tmpfs" && w[1] == "/etc/shadow")),
"a file denied path must not be tmpfs-masked: {args:?}"
);
}
#[test]
fn mixed_denied_file_and_dir_masks_each_correctly() {
let mut r = base_request();
r.policy.readwrite_paths = vec!["/data".into()];
r.policy.denied_paths = vec!["/data/secret.txt".into(), "/cache".into()];
let denied_files = HashSet::from(["/data/secret.txt".to_string()]);
let args = build_args_classified(&r, None, &denied_files);
let parent = policy_mount_pos(&args, "--bind", "/data");
let file_mask = policy_mount_pos(&args, "--ro-bind", "/data/secret.txt");
assert!(
file_mask > parent,
"deep denied file mask (pos {file_mask}) must come after rw /data (pos {parent}): {args:?}"
);
assert_eq!(args[file_mask + 1], "/dev/null");
policy_mount_pos(&args, "--tmpfs", "/cache");
}
#[test]
fn nested_denied_dir_and_child_file_mask_each_correctly() {
let mut r = base_request();
r.policy.denied_paths = vec!["/data/secret".into(), "/data/secret/key".into()];
let denied_files = HashSet::from(["/data/secret/key".to_string()]);
let args = build_args_classified(&r, None, &denied_files);
let parent = policy_mount_pos(&args, "--tmpfs", "/data/secret");
let child = policy_mount_pos(&args, "--ro-bind", "/data/secret/key");
assert_eq!(
args[child + 1],
"/dev/null",
"nested denied file must be masked with /dev/null: {args:?}"
);
assert!(
child > parent,
"child file mask (pos {child}) must come after parent tmpfs (pos {parent}) \
so it lands inside the masked subtree: {args:?}"
);
}
#[test]
fn denied_tmpfs_masks_are_sealed_after_child_mounts() {
let mut r = base_request();
r.policy.denied_paths = vec!["/data".into(), "/data/nested".into()];
r.policy.readwrite_paths = vec!["/data/allowed/deep".into()];
let args = build_args(&r, None);
let child = policy_mount_pos(&args, "--bind", "/data/allowed/deep");
for path in ["/data", "/data/nested"] {
let mask = policy_mount_pos(&args, "--tmpfs", path);
let remount = args
.windows(2)
.position(|w| w[0] == "--remount-ro" && w[1] == path)
.unwrap_or_else(|| panic!("missing read-only remount for {path}: {args:?}"));
assert!(mask < remount && child < remount, "{args:?}");
}
}
#[test]
fn environment_variables_are_set() {
let mut r = base_request();
r.env = Some(vec!["FOO=bar".into(), "PATH=/usr/bin".into()]);
let args = build_args(&r, None);
assert!(args.contains(&"--clearenv".to_string()));
let foo_pos = args.iter().position(|a| a == "FOO").unwrap();
assert_eq!(args[foo_pos - 1], "--setenv");
assert_eq!(args[foo_pos + 1], "bar");
}
#[test]
fn working_directory_is_set() {
let args = build_args(&base_request(), None);
let chdir_pos = args.iter().position(|a| a == "--chdir").unwrap();
assert_eq!(args[chdir_pos + 1], "/home/user");
}
#[test]
fn command_is_last() {
let args = build_args(&base_request(), None);
let sep = args.iter().position(|a| a == "--").unwrap();
assert_eq!(args[sep + 1], "sh");
assert_eq!(args[sep + 2], "-c");
assert_eq!(args[sep + 3], "echo hello");
}
#[test]
fn empty_working_directory_omits_chdir() {
let mut r = base_request();
r.working_directory = String::new();
let args = build_args(&r, None);
assert!(!args.contains(&"--chdir".to_string()));
}
#[test]
fn policy_mounts_under_tmp_are_not_shadowed_by_standard_tmpfs() {
let mut r = base_request();
r.policy.readwrite_paths = vec!["/tmp/workspace".into()];
r.policy.readonly_paths = vec!["/tmp/data".into()];
r.policy.denied_paths = vec!["/tmp/secrets".into()];
let args = build_args(&r, None);
let tmpfs_tmp_pos = args
.windows(2)
.position(|w| w[0] == "--tmpfs" && w[1] == "/tmp")
.expect("standard --tmpfs /tmp must be present");
let assert_after = |op: &str, target: &str| {
let pos = args
.windows(2)
.position(|w| w[0] == op && w[1] == target)
.unwrap_or_else(|| panic!("missing {} {}", op, target));
assert!(
pos > tmpfs_tmp_pos,
"{} {} (pos {}) must come after --tmpfs /tmp (pos {}) \
or it will be shadowed",
op,
target,
pos,
tmpfs_tmp_pos
);
};
assert_after("--bind", "/tmp/workspace");
assert_after("--ro-bind", "/tmp/data");
assert_after("--tmpfs", "/tmp/secrets");
}
#[test]
fn proxy_active_uses_private_network_and_external_user_namespace() {
let r = base_request();
let addr = ProxyAddress::new("127.0.0.1".into(), 12345);
let args = build_args(&r, Some(&addr));
assert!(
args.contains(&"--unshare-net".to_string()),
"proxy mode must use a private network namespace"
);
assert!(
!args.contains(&"--unshare-user".to_string()),
"the runner supplies proxy mode's pre-created user namespace"
);
}
#[test]
fn proxy_active_injects_env_vars() {
let r = base_request();
let addr = ProxyAddress::new("127.0.0.1".into(), 7777);
let args = build_args(&r, Some(&addr));
for key in &[
"HTTP_PROXY",
"HTTPS_PROXY",
"ALL_PROXY",
"http_proxy",
"https_proxy",
"all_proxy",
] {
let pos = args
.iter()
.position(|a| a == *key)
.unwrap_or_else(|| panic!("missing --setenv {} in {:?}", key, args));
assert_eq!(args[pos - 1], "--setenv");
}
let http_pos = args.iter().position(|a| a == "HTTP_PROXY").unwrap();
assert_eq!(args[http_pos + 1], "http://127.0.0.1:7777");
}
#[test]
fn proxy_active_does_not_exempt_loopback_via_no_proxy() {
let r = base_request();
let addr = ProxyAddress::new("127.0.0.1".into(), 7777);
let args = build_args(&r, Some(&addr));
assert!(
!args.iter().any(|a| a == "NO_PROXY" || a == "no_proxy"),
"proxy mode must not emit NO_PROXY/no_proxy --setenv pairs: {:?}",
args,
);
}
#[test]
fn proxy_active_strips_caller_supplied_proxy_env() {
let mut r = base_request();
r.env = Some(vec![
"FOO=bar".into(),
"HTTP_PROXY=http://attacker.example:9999".into(),
"https_proxy=http://attacker.example:9999".into(),
"ALL_PROXY=http://attacker.example:9999".into(),
"FTP_PROXY=http://attacker.example:9999".into(),
"ftp_proxy=http://attacker.example:9999".into(),
"NO_PROXY=*".into(),
"PATH=/usr/bin".into(),
]);
let addr = ProxyAddress::new("127.0.0.1".into(), 9000);
let args = build_args(&r, Some(&addr));
assert!(
!args.iter().any(|a| a == "http://attacker.example:9999"),
"caller-supplied proxy URL must be stripped"
);
assert!(args.iter().any(|a| a == "FOO"));
assert!(args.iter().any(|a| a == "PATH"));
let http_pos = args.iter().position(|a| a == "HTTP_PROXY").unwrap();
assert_eq!(args[http_pos + 1], "http://127.0.0.1:9000");
for key in ["FTP_PROXY", "ftp_proxy"] {
let pos = args
.iter()
.position(|arg| arg == key)
.unwrap_or_else(|| panic!("missing --setenv {key} in {args:?}"));
assert_eq!(args[pos + 1], "http://127.0.0.1:9000");
}
for key in ["NO_PROXY", "no_proxy"] {
assert!(
!args.iter().any(|arg| arg == key),
"proxy mode must not emit caller-controlled {key}: {args:?}"
);
}
}
#[test]
fn proxy_inactive_leaves_caller_supplied_proxy_env_intact() {
let mut r = base_request();
r.env = Some(vec!["HTTP_PROXY=http://caller.example:8080".into()]);
let args = build_args(&r, None);
let pos = args.iter().position(|a| a == "HTTP_PROXY").unwrap();
assert_eq!(args[pos + 1], "http://caller.example:8080");
}
#[test]
fn baseline_does_not_bind_mount_host_root() {
let args = build_args(&base_request(), None);
let root_bind = args
.windows(3)
.any(|w| (w[0] == "--ro-bind" || w[0] == "--bind") && w[1] == "/" && w[2] == "/");
assert!(
!root_bind,
"baseline must not bind-mount host / into the sandbox; got: {:?}",
args
);
}
#[test]
fn baseline_emits_required_ro_bind_try_paths() {
let args = build_args(&base_request(), None);
let required = [
"/bin",
"/sbin",
"/lib",
"/lib64",
"/usr/bin",
"/usr/lib",
"/usr/share",
"/etc",
];
for path in required {
let found = args
.windows(3)
.any(|w| w[0] == "--ro-bind-try" && w[1] == path && w[2] == path);
assert!(
found,
"baseline must emit `--ro-bind-try {} {}` so sandboxed processes \
can find sh / libc / system config",
path, path
);
}
}
#[test]
fn baseline_does_not_expose_usr_local() {
let args = build_args(&base_request(), None);
let usr_whole = args
.windows(3)
.any(|w| matches!(w[0].as_str(), "--ro-bind" | "--ro-bind-try") && w[1] == "/usr");
assert!(
!usr_whole,
"baseline must bind /usr subpaths individually so /usr/local is \
not implicitly exposed; got: {:?}",
args
);
let usr_local = args.windows(3).any(|w| {
matches!(w[0].as_str(), "--bind" | "--ro-bind" | "--ro-bind-try")
&& w[1] == "/usr/local"
});
assert!(!usr_local, "baseline must not expose /usr/local by default");
}
#[test]
fn baseline_excludes_confidential_paths() {
let args = build_args(&base_request(), None);
for forbidden in [
"/home",
"/root",
"/opt",
"/srv",
"/var",
"/sys",
"/run/user",
"/run/dbus",
] {
let exposed = args.windows(2).any(|w| {
matches!(w[0].as_str(), "--bind" | "--ro-bind" | "--ro-bind-try")
&& w[1] == forbidden
});
assert!(
!exposed,
"baseline must not bind-mount {} — that would re-expose \
confidential host state",
forbidden
);
}
}
#[test]
fn baseline_excludes_resolver_runtime_directories() {
let args = build_args(&base_request(), None);
for path in [
"/run/systemd/resolve",
"/run/NetworkManager",
"/run/resolvconf",
] {
let found = args
.windows(3)
.any(|w| w[0] == "--ro-bind-try" && w[1] == path && w[2] == path);
assert!(!found, "baseline must not expose host IPC from {path}");
}
}
#[test]
fn baseline_recreates_var_run_compat_symlink() {
let args = build_args(&base_request(), None);
let found = args
.windows(3)
.any(|w| w[0] == "--symlink" && w[1] == "/run" && w[2] == "/var/run");
assert!(
found,
"baseline must emit `--symlink /run /var/run` so /etc/resolv.conf \
symlinks routed through /var/run/... resolve; got: {:?}",
args
);
let var_bound = args.windows(2).any(|w| {
matches!(w[0].as_str(), "--bind" | "--ro-bind" | "--ro-bind-try") && w[1] == "/var"
});
assert!(!var_bound, "compat symlink must not bind host /var");
}
#[test]
fn baseline_does_not_bind_unchecked_wsl_resolver_paths() {
let args = build_args(&base_request(), None);
let found = args.windows(3).any(|w| {
w[0] == "--ro-bind-try"
&& w[1] == "/mnt/wsl/resolv.conf"
&& w[2] == "/mnt/wsl/resolv.conf"
});
assert!(
!found,
"the runner must verify the WSL resolver is a regular file"
);
let mnt_whole = args.windows(2).any(|w| {
matches!(w[0].as_str(), "--bind" | "--ro-bind" | "--ro-bind-try")
&& (w[1] == "/mnt" || w[1] == "/mnt/wsl")
});
assert!(
!mnt_whole,
"baseline must not expose /mnt or /mnt/wsl wholesale"
);
}
#[test]
fn baseline_mounts_precede_policy_mounts() {
let mut r = base_request();
r.policy.readwrite_paths = vec!["/etc/policy-writable".into()];
let args = build_args(&r, None);
let baseline_etc = args
.windows(3)
.position(|w| w[0] == "--ro-bind-try" && w[1] == "/etc" && w[2] == "/etc")
.expect("baseline /etc bind missing");
let policy_bind = args
.windows(3)
.position(|w| w[0] == "--bind" && w[1] == "/etc/policy-writable")
.expect("policy bind missing");
assert!(
policy_bind > baseline_etc,
"policy mount at /etc/policy-writable (pos {}) must come after \
baseline /etc bind (pos {}) so the policy mount wins",
policy_bind,
baseline_etc
);
}
fn host(
links: &'static [(&'static str, &'static str)],
) -> impl Fn(&str) -> std::io::Result<Option<std::path::PathBuf>> {
move |path| {
Ok(links
.iter()
.find(|(link, _)| *link == path)
.map(|(_, target)| target.into()))
}
}
fn link(path: &'static str, relative_target: &str) -> BaselineLink {
BaselineLink {
path,
relative_target: relative_target.into(),
}
}
#[test]
fn merged_usr_entries_link_to_their_usr_mounts() {
let debian = baseline_links(host(&[
("/bin", "/usr/bin"),
("/sbin", "/usr/sbin"),
("/lib", "/usr/lib"),
("/lib32", "/usr/lib32"),
("/lib64", "/usr/lib64"),
("/libx32", "/usr/libx32"),
]))
.unwrap();
assert_eq!(
debian,
[
link("/bin", "usr/bin"),
link("/sbin", "usr/sbin"),
link("/lib", "usr/lib"),
link("/lib32", "usr/lib32"),
link("/lib64", "usr/lib64"),
link("/libx32", "usr/libx32"),
]
);
let arch = baseline_links(host(&[
("/bin", "/usr/bin"),
("/sbin", "/usr/bin"),
("/lib", "/usr/lib"),
("/lib64", "/usr/lib"),
("/usr/sbin", "/usr/bin"),
("/usr/lib64", "/usr/lib"),
]))
.unwrap();
assert_eq!(
arch,
[
link("/bin", "usr/bin"),
link("/sbin", "usr/bin"),
link("/lib", "usr/lib"),
link("/lib64", "usr/lib"),
link("/usr/sbin", "bin"),
link("/usr/lib64", "lib"),
]
);
}
#[test]
fn only_links_into_a_bound_entry_become_sandbox_links() {
assert!(baseline_links(|_| Ok(None)).unwrap().is_empty());
let links = baseline_links(host(&[
("/bin", "/usr/bin"),
("/sbin", "/bin/sbin"),
("/lib32", "/opt/lib32"),
("/lib64", "/usr/lib/x86_64-linux-gnu"),
("/usr/lib64", "/lib"),
]))
.unwrap();
assert_eq!(
links,
[
link("/bin", "usr/bin"),
link("/lib64", "usr/lib/x86_64-linux-gnu"),
link("/usr/lib64", "../lib"),
]
);
}
#[test]
fn an_unresolvable_baseline_entry_refuses_to_start() {
let error = baseline_links(|path| match path {
"/lib" => Err(std::io::ErrorKind::PermissionDenied.into()),
_ => Ok(None),
})
.unwrap_err();
assert!(
error.contains("the host's /lib") && error.contains("Refusing to start"),
"{error}"
);
}
#[cfg(unix)]
#[test]
fn a_link_target_that_cannot_be_written_as_a_link_refuses_to_start() {
use std::os::unix::ffi::OsStrExt;
let error = baseline_links(|path| {
Ok((path == "/bin")
.then(|| std::path::PathBuf::from(std::ffi::OsStr::from_bytes(b"/usr/bin/\xff"))))
})
.unwrap_err();
assert!(
error.contains("the host's /bin") && error.contains("Refusing to start"),
"{error}"
);
}
#[test]
fn baseline_links_replace_their_binds_before_policy_mounts() {
let mut request = base_request();
request.policy.readonly_paths = vec!["/bin/ls".into()];
let args = build_args_classified_with_mode(
&request,
None,
&HashSet::new(),
ResolvedNetworkMode::Isolated,
None,
&[link("/bin", "usr/bin")],
&[],
&policy_mounts(&request.policy),
true,
|_| Vec::new(),
);
let symlink = args
.windows(3)
.position(|w| w == ["--symlink", "usr/bin", "/bin"])
.expect("the linked entry must become a symlink");
let grant = args
.windows(3)
.position(|w| w == ["--ro-bind", "/bin/ls", "/bin/ls"])
.expect("policy mount missing");
assert!(
symlink < grant,
"a policy mount through the link must follow it"
);
assert!(!args
.windows(3)
.any(|w| w[0] == "--ro-bind-try" && w[2] == "/bin"));
for path in BASELINE_RO_BIND_PATHS
.iter()
.filter(|path| **path != "/bin")
{
assert!(
args.windows(3)
.any(|w| w == ["--ro-bind-try", *path, *path]),
"{path} must keep its bind"
);
}
}
fn grant_link(path: &str, target: &str) -> GrantLink {
GrantLink {
path: path.into(),
target: target.into(),
..Default::default()
}
}
fn aliased(path: &str, target: &str) -> AliasedGrant {
AliasedGrant {
links: vec![grant_link(path, target)],
..Default::default()
}
}
fn mount(path: &str, intent: FsIntent) -> PolicyMount {
PolicyMount {
source: path.into(),
destination: path.into(),
intent,
readonly_overlay: None,
}
}
fn plan_for(
mounts: &[PolicyMount],
resolver_args: &[String],
grants: &[AliasedGrant],
) -> GrantLinkPlan {
let mut plan = None;
build_args_classified_with_mode(
&base_request(),
None,
&HashSet::new(),
ResolvedNetworkMode::Isolated,
None,
&[link("/bin", "usr/bin")],
resolver_args,
mounts,
true,
|layout| {
let planned = plan_grant_links(layout, grants);
let args = planned.args.clone();
plan = Some(planned);
args
},
);
plan.expect("the arguments include the grant links")
}
#[test]
fn grant_links_are_recreated_only_where_nothing_else_is() {
let mounts = policy_mounts(&ContainerPolicy {
readwrite_paths: vec!["/data/repos/app".into(), "/srv/share".into()],
readonly_paths: vec!["/home/bob".into()],
denied_paths: vec!["/srv".into()],
..Default::default()
});
let created = [
aliased("/home/alice/app", "/data/repos/app"),
aliased("/tmp/work/app", "/data/repos/app"),
aliased("/srv/app", "/srv/share"),
aliased("/data/repos/current", "/data/repos"),
];
let present = [
aliased("/home/bob/app", "/data/repos/app"),
aliased("/bin", "/usr/bin"),
aliased("/etc/alternatives/app", "/data/repos/app"),
];
let unavailable = [
grant_link("/dev/shm", "/run/shm"),
grant_link("/proc/app", "/data/repos/app"),
grant_link("/var/run/app", "/run/app"),
grant_link("/tmp", "/var/tmp"),
grant_link("/usr", "/opt/usr"),
];
let grants: Vec<_> = created
.iter()
.chain(&present)
.cloned()
.chain(unavailable.iter().map(|link| AliasedGrant {
links: vec![link.clone()],
..Default::default()
}))
.chain(created.iter().cloned())
.collect();
let plan = plan_for(&mounts, &[], &grants);
assert_eq!(
plan.args,
[
"--symlink",
"../../data/repos/app",
"/home/alice/app",
"--symlink",
"../../data/repos/app",
"/tmp/work/app",
"--symlink",
"share",
"/srv/app",
"--symlink",
".",
"/data/repos/current",
]
);
assert_eq!(plan.unavailable, unavailable);
}
#[test]
fn a_link_at_the_sandbox_tmp_is_recreated_one_level_down() {
let project = AliasedGrant {
links: vec![grant_link("/tmp", "/var/tmp")],
inner: Some(grant_link("/tmp/project", "/var/tmp/project")),
..Default::default()
};
let mounts = policy_mounts(&ContainerPolicy {
readwrite_paths: vec!["/var/tmp/project".into()],
..Default::default()
});
let plan = plan_for(&mounts, &[], &[project.clone(), project]);
assert_eq!(
plan.args,
["--symlink", "../var/tmp/project", "/tmp/project"]
);
assert_eq!(plan.unavailable, Vec::<GrantLink>::new());
}
#[test]
fn a_grant_of_a_whole_symlinked_tmp_replaces_the_private_tmp() {
let mut request = base_request();
request.policy.readwrite_paths = vec!["/var/tmp".into()];
let grants = [aliased("/tmp", "/var/tmp")];
let args = build_args_classified_with_mode(
&request,
None,
&HashSet::new(),
ResolvedNetworkMode::Isolated,
None,
&[],
&[],
&policy_mounts(&request.policy),
false,
|layout| plan_grant_links(layout, &grants).args,
);
assert!(!args.windows(2).any(|w| w == ["--tmpfs", "/tmp"]));
let position = |expected: &[&str]| {
args.windows(expected.len())
.position(|window| window == expected)
.unwrap_or_else(|| panic!("{expected:?} missing from {args:?}"))
};
assert!(
position(&["--bind", "/var/tmp", "/var/tmp"])
< position(&["--symlink", "var/tmp", "/tmp"])
);
}
#[test]
fn host_links_seen_through_a_grant_get_the_links_they_pass_through() {
let mounts = policy_mounts(&ContainerPolicy {
readwrite_paths: vec!["/home/alice".into(), "/data/app".into()],
..Default::default()
});
let host_link = |path: &str, passes_through: Vec<GrantLink>| GrantLink {
passes_through,
..grant_link(path, "/data/app")
};
let current = host_link("/links/current", Vec::new());
let reachable = [
host_link("/home/alice/app", vec![current.clone()]),
host_link("/home/alice/again", vec![current]),
host_link(
"/home/alice/release",
vec![host_link("/home/alice/current", Vec::new())],
),
];
let unavailable = [
host_link("/home/alice/proc", vec![host_link("/proc/app", Vec::new())]),
host_link(
"/home/alice/sideways",
vec![grant_link("/links/other", "/data/other")],
),
];
let grants: Vec<_> = reachable
.iter()
.chain(&unavailable)
.map(|link| AliasedGrant {
links: vec![link.clone()],
..Default::default()
})
.collect();
let plan = plan_for(&mounts, &[], &grants);
assert_eq!(plan.args, ["--symlink", "../data/app", "/links/current"]);
assert_eq!(plan.unavailable, unavailable);
}
#[test]
fn directories_that_a_path_climbs_out_of_are_created_where_the_sandbox_has_none() {
let mounts = policy_mounts(&ContainerPolicy {
readwrite_paths: vec![
"/home/alice".into(),
"/data/app".into(),
"/srv/shared".into(),
],
..Default::default()
});
let through_staging = AliasedGrant {
real_path: "/data/app".into(),
links: vec![GrantLink {
climbs_out_of: vec!["/data/staging".into(), "/home/alice".into()],
..grant_link("/home/alice/app", "/data/app")
}],
..Default::default()
};
let spelled_with_dots = AliasedGrant {
real_path: "/srv/shared".into(),
links: vec![grant_link("/opt/app", "/srv/app")],
climbs_out_of: vec!["/srv/app".into(), "/data/staging".into(), "/proc/1".into()],
..Default::default()
};
let through_dev = GrantLink {
climbs_out_of: vec!["/dev/shm/staging".into()],
..grant_link("/home/alice/shm", "/data/app")
};
let grants = [
through_staging,
spelled_with_dots,
AliasedGrant {
links: vec![through_dev.clone()],
..Default::default()
},
];
let plan = plan_for(&mounts, &[], &grants);
assert_eq!(
plan.args,
[
"--dir",
"/data/staging",
"--dir",
"/srv/app",
"--symlink",
"../srv/app",
"/opt/app",
]
);
assert_eq!(
plan.unavailable,
[
GrantLink {
target: "/srv/shared".into(),
..grant_link("/proc/1", "")
},
through_dev,
]
);
}
#[test]
fn a_sandbox_link_that_leads_elsewhere_gets_the_next_level_where_it_leads() {
let through_var_run = |real_path: &str| AliasedGrant {
real_path: real_path.into(),
links: vec![grant_link("/var/run", "/data/run")],
inner: Some(grant_link("/var/run/project", "/data/run/project")),
..Default::default()
};
let mounts = policy_mounts(&ContainerPolicy {
readwrite_paths: vec!["/data/run".into(), "/run/app".into()],
..Default::default()
});
let plan = plan_for(
&mounts,
&[],
&[
through_var_run("/data/run/project"),
aliased("/var/run", "/run"),
],
);
assert_eq!(
plan.args,
["--symlink", "../data/run/project", "/run/project"]
);
assert_eq!(plan.unavailable, Vec::<GrantLink>::new());
let whole = AliasedGrant {
inner: None,
..through_var_run("/data/run")
};
let plan = plan_for(&mounts, &[], &[whole]);
assert_eq!(plan.args, Vec::<String>::new());
assert_eq!(plan.unavailable, [grant_link("/var/run", "/data/run")]);
}
#[test]
fn a_link_above_a_sandbox_entry_is_recreated_one_level_down() {
let lib = AliasedGrant {
links: vec![grant_link("/var", "/data/var")],
inner: Some(grant_link("/var/lib", "/data/var/lib")),
..Default::default()
};
let mounts = policy_mounts(&ContainerPolicy {
readwrite_paths: vec!["/data/var/lib/x".into()],
..Default::default()
});
assert_eq!(
plan_for(&mounts, &[], &[lib]).args,
["--symlink", "../data/var/lib", "/var/lib"]
);
}
#[test]
fn links_in_a_readonly_overlay_keep_the_host_alias_layout() {
let mut parent = mount("/data", FsIntent::ReadOnly);
parent.readonly_overlay = Some("/private/deny-mountpoints".into());
let plan = plan_for(&[parent], &[], &[aliased("/data/alias", "/data/project")]);
assert!(plan.args.is_empty());
assert!(plan.unavailable.is_empty());
}
#[test]
fn links_inside_a_bind_of_another_host_path_are_unavailable() {
let mounts = [
PolicyMount {
source: "/var/run/app".into(),
destination: "/run/app".into(),
intent: FsIntent::ReadWrite,
readonly_overlay: None,
},
mount("/data/project", FsIntent::ReadWrite),
];
let alias = aliased("/run/app/alias", "/data/project");
let plan = plan_for(&mounts, &[], std::slice::from_ref(&alias));
assert_eq!(plan.args, Vec::<String>::new());
assert_eq!(plan.unavailable, alias.links);
}
#[test]
fn a_policy_mount_decides_for_the_baseline_directory_it_covers() {
let grants = [aliased("/etc/hosts", "/etc/static/hosts")];
let denied_etc = [
mount("/etc", FsIntent::Denied),
mount("/etc/static/hosts", FsIntent::ReadOnly),
];
assert_eq!(
plan_for(&denied_etc, &[], &grants).args,
["--symlink", "static/hosts", "/etc/hosts"]
);
let writable_etc = [mount("/etc", FsIntent::ReadWrite)];
assert_eq!(
plan_for(&writable_etc, &[], &grants),
GrantLinkPlan::default()
);
}
#[test]
fn grant_links_respect_the_resolver_layout() {
let resolver: Vec<String> = [
"--tmpfs",
"/run/systemd/resolve",
"--symlink",
"../srv/dns",
"/opt/dns",
"--file",
"19",
"/run/systemd/resolve/resolv.conf",
"--dir",
"/run/systemd/resolve/masked",
"--remount-ro",
"/run/systemd/resolve",
]
.map(String::from)
.into();
let mounts = [mount("/run/systemd/resolve/masked", FsIntent::Denied)];
let grants = [
aliased("/opt/dns", "/srv/dns"),
aliased("/run/systemd/resolve/link", "/srv/dns"),
aliased("/run/systemd/resolve/masked/link", "/srv/dns"),
];
let plan = plan_for(&mounts, &resolver, &grants);
assert_eq!(
plan.args,
[
"--symlink",
"../../../../srv/dns",
"/run/systemd/resolve/masked/link"
]
);
assert_eq!(plan.unavailable, grants[1].links);
}
#[test]
fn an_unknown_mount_option_makes_every_grant_link_unavailable() {
for option in ["--unknown-mount", "--overlay-src"] {
let layout: Vec<String> = [option, "/data", "--tmpfs", "/tmp"]
.map(String::from)
.into();
let grant = aliased("/home/alice/app", "/data/repos/app");
let plan = plan_grant_links(&layout, std::slice::from_ref(&grant));
assert_eq!(plan.args, Vec::<String>::new());
assert_eq!(plan.unavailable, grant.links);
}
}
#[test]
fn the_planner_understands_every_mount_argument_emitted_before_the_links() {
let mut request = base_request();
request.policy = ContainerPolicy {
readwrite_paths: vec!["/data/repos/app".into()],
readonly_paths: vec!["/home/alice".into()],
denied_paths: vec!["/home/alice/.ssh".into(), "/home/alice/.env".into()],
..Default::default()
};
let resolver: Vec<String> = [
"--tmpfs",
"/run/resolver",
"--dir",
"/run/resolver/sub",
"--symlink",
"sub",
"/run/resolver/link",
"--file",
"19",
"/run/resolver/resolv.conf",
"--ro-bind",
"/dev/null",
"/run/resolver/masked",
"--remount-ro",
"/run/resolver",
]
.map(String::from)
.into();
for mode in [
ResolvedNetworkMode::Isolated,
ResolvedNetworkMode::FirewallEnforced,
] {
build_args_classified_with_mode(
&request,
None,
&HashSet::from(["/home/alice/.env".to_string()]),
mode,
Some(17),
&[link("/bin", "usr/bin")],
&resolver,
&policy_mounts(&request.policy),
true,
|layout| {
assert!(sandbox_layout(layout).is_some(), "{mode:?}: {layout:?}");
Vec::new()
},
);
}
}
#[test]
fn grant_links_follow_policy_mounts_and_precede_read_only_masks() {
let mut request = base_request();
request.policy.readwrite_paths = vec!["/data/repos/app".into()];
request.policy.denied_paths = vec!["/home/alice".into()];
let link_args = [
"--symlink".to_string(),
"../../data/repos/app".into(),
"/home/alice/app".into(),
];
let args = build_args_classified_with_mode(
&request,
None,
&HashSet::new(),
ResolvedNetworkMode::Isolated,
None,
&[],
&[],
&policy_mounts(&request.policy),
true,
|layout| {
assert!(layout
.windows(3)
.any(|w| w == ["--bind", "/data/repos/app", "/data/repos/app"]));
link_args.to_vec()
},
);
let position = |expected: &[&str]| {
args.windows(expected.len())
.position(|window| window == expected)
.unwrap_or_else(|| panic!("{expected:?} missing from {args:?}"))
};
let link = position(&["--symlink", "../../data/repos/app", "/home/alice/app"]);
assert!(position(&["--bind", "/data/repos/app", "/data/repos/app"]) < link);
assert!(position(&["--tmpfs", "/home/alice"]) < link);
assert!(link < position(&["--remount-ro", "/home/alice"]));
}
}