use std::path::PathBuf;
#[cfg(any(target_os = "linux", target_os = "macos", target_os = "freebsd"))]
pub mod privdrop;
#[cfg(target_os = "freebsd")]
mod freebsd;
#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "macos")]
mod macos;
#[cfg(target_os = "windows")]
mod windows;
#[cfg(all(target_os = "linux", feature = "landlock"))]
pub use linux::capabilities;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SandboxMode {
#[default]
BestEffort,
Strict,
Disabled,
}
impl SandboxMode {
pub fn from_flags(no_sandbox: bool, strict: bool) -> Self {
if no_sandbox {
SandboxMode::Disabled
} else if strict {
SandboxMode::Strict
} else {
SandboxMode::BestEffort
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SandboxStatus {
FullyEnforced,
PartiallyEnforced,
#[default]
NotApplied,
Error,
}
impl SandboxStatus {
pub fn label(&self) -> &'static str {
match self {
SandboxStatus::FullyEnforced => "Fully enforced",
SandboxStatus::PartiallyEnforced => "Partially enforced",
SandboxStatus::NotApplied => "Not applied",
SandboxStatus::Error => "Error",
}
}
}
#[derive(Debug, Clone, Default)]
pub struct SandboxConfig {
pub mode: SandboxMode,
pub block_network: bool,
pub read_paths: Vec<PathBuf>,
pub write_paths: Vec<PathBuf>,
#[cfg(any(target_os = "linux", target_os = "macos", target_os = "freebsd"))]
pub drop_uid: Option<privdrop::DropTarget>,
}
#[derive(Debug, Clone, Default)]
pub struct SandboxReport {
pub status: SandboxStatus,
pub message: String,
pub fs_restricted: bool,
pub net_restricted: bool,
pub uid_dropped: bool,
#[cfg(target_os = "linux")]
pub cap_net_raw_dropped: bool,
#[cfg(target_os = "linux")]
pub ebpf_caps_dropped: bool,
#[cfg(target_os = "linux")]
pub scope_restricted: bool,
#[cfg(target_os = "linux")]
pub landlock_available: bool,
#[cfg(target_os = "linux")]
pub landlock_abi: Option<u8>,
#[cfg(target_os = "linux")]
pub no_new_privs: bool,
#[cfg(target_os = "macos")]
pub seatbelt_applied: bool,
#[cfg(target_os = "windows")]
pub privileges_removed: bool,
#[cfg(target_os = "windows")]
pub privileges_removed_count: u32,
#[cfg(target_os = "windows")]
pub job_object_applied: bool,
}
impl SandboxReport {
pub fn from_error(error: &anyhow::Error) -> Self {
Self {
status: SandboxStatus::Error,
message: error.to_string(),
..Self::default()
}
}
}
pub fn apply_sandbox(config: &SandboxConfig) -> anyhow::Result<SandboxReport> {
#[cfg(target_os = "linux")]
return linux::apply(config);
#[cfg(target_os = "macos")]
return macos::apply(config);
#[cfg(target_os = "windows")]
return windows::apply(config);
#[cfg(target_os = "freebsd")]
return freebsd::apply(config);
#[cfg(not(any(
target_os = "linux",
target_os = "macos",
target_os = "windows",
target_os = "freebsd"
)))]
{
log::warn!("Sandboxing not implemented for this platform");
let _ = config;
Ok(SandboxReport {
message: "Sandboxing not implemented for this platform".to_string(),
..SandboxReport::default()
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_mode_from_flags() {
assert_eq!(
SandboxMode::from_flags(false, false),
SandboxMode::BestEffort
);
assert_eq!(SandboxMode::from_flags(false, true), SandboxMode::Strict);
assert_eq!(SandboxMode::from_flags(true, false), SandboxMode::Disabled);
}
#[test]
fn test_report_from_error_defaults_everything_off() {
let report = SandboxReport::from_error(&anyhow::anyhow!("boom"));
assert_eq!(report.status, SandboxStatus::Error);
assert_eq!(report.message, "boom");
assert!(!report.fs_restricted);
assert!(!report.net_restricted);
assert!(!report.uid_dropped);
}
}