#[cfg(feature = "macos-sandbox")]
mod seatbelt;
use crate::{SandboxConfig, SandboxMode, SandboxReport, SandboxStatus, privdrop};
pub(crate) fn apply(config: &SandboxConfig) -> 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 mut uid_dropped = false;
let mut drop_message = String::new();
if let Some(target) = config.drop_uid {
match privdrop::drop_to(target) {
Ok(()) => {
uid_dropped = true;
drop_message = format!("; root dropped to uid {} gid {}", target.uid, target.gid);
log::info!(
"Dropped root privileges to uid {} gid {} (verified); lsof-fallback \
process attribution is now limited to that user's processes (PKTAP \
attribution unaffected)",
target.uid,
target.gid
);
}
Err(e) => {
if config.mode == SandboxMode::Strict {
return Err(e.context("Strict mode requires the root uid drop to succeed"));
}
log::warn!("Failed to drop root uid/gid: {}", e);
drop_message = format!("; root uid drop failed: {}", e);
}
}
}
#[cfg(feature = "macos-sandbox")]
{
match seatbelt::apply_seatbelt(config) {
Ok(result) => {
let drop_ok = config.drop_uid.is_none() || uid_dropped;
let status = match (result.applied, drop_ok, uid_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: format!("{}{}", result.message, drop_message),
seatbelt_applied: result.applied,
fs_restricted: result.fs_restricted,
net_restricted: result.net_blocked,
uid_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 {
status: if uid_dropped {
SandboxStatus::PartiallyEnforced
} else {
SandboxStatus::NotApplied
},
message: format!("{}{}", msg, drop_message),
uid_dropped,
..SandboxReport::default()
})
}
}
}
#[cfg(not(feature = "macos-sandbox"))]
{
log::warn!("Seatbelt feature not compiled in");
Ok(SandboxReport {
status: if uid_dropped {
SandboxStatus::PartiallyEnforced
} else {
SandboxStatus::NotApplied
},
message: format!("Seatbelt feature not compiled in{}", drop_message),
uid_dropped,
..SandboxReport::default()
})
}
}