#[cfg(feature = "macos-sandbox")]
mod seatbelt;
use crate::{SandboxConfig, SandboxMode, SandboxReport, drop_root_step};
pub(crate) fn apply(
config: &SandboxConfig,
allow_dns_resolution: bool,
) -> anyhow::Result<SandboxReport> {
if config.mode == SandboxMode::Disabled {
log::info!("Sandbox disabled by configuration");
return Ok(SandboxReport {
message: "Sandbox disabled by configuration".to_string(),
..SandboxReport::default()
});
}
let outcome = drop_root_step(
config,
"lsof-fallback process attribution is now limited to that user's processes \
(PKTAP attribution unaffected)",
)?;
#[cfg(feature = "macos-sandbox")]
{
use crate::SandboxStatus;
match seatbelt::apply_seatbelt(config, allow_dns_resolution) {
Ok(result) => {
let drop_ok = config.drop_uid.is_none() || outcome.dropped;
let status = match (result.applied, drop_ok, outcome.dropped) {
(true, true, _) => SandboxStatus::FullyEnforced,
(true, false, _) | (false, _, true) => SandboxStatus::PartiallyEnforced,
_ => SandboxStatus::NotApplied,
};
if result.applied {
log::info!("Seatbelt: {}", result.message);
} else {
log::warn!("Seatbelt sandbox not applied: {}", result.message);
}
Ok(SandboxReport {
status,
message: outcome.message_after(&result.message),
seatbelt_applied: result.applied,
fs_restricted: result.fs_restricted,
net_restricted: result.net_restricted,
uid_dropped: outcome.dropped,
})
}
Err(e) => {
let msg = format!("Seatbelt application failed: {}", e);
log::warn!("{}", msg);
if config.mode == SandboxMode::Strict {
return Err(e.context("Strict mode requires Seatbelt sandboxing"));
}
Ok(SandboxReport::uid_drop_only(&msg, &outcome))
}
}
}
#[cfg(not(feature = "macos-sandbox"))]
{
let _ = allow_dns_resolution;
if config.mode == SandboxMode::Strict {
anyhow::bail!("Strict mode requires a build with Seatbelt support");
}
log::warn!("Seatbelt feature not compiled in");
Ok(SandboxReport::uid_drop_only(
"Seatbelt feature not compiled in",
&outcome,
))
}
}