#![forbid(unsafe_code)]
use std::ffi::OsString;
use std::path::PathBuf;
use cant_policy::Policy;
#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "linux")]
pub use linux::elf::{ElfError, Resolved, resolve as resolve_libraries};
#[cfg(target_os = "linux")]
pub use linux::landlock::MIN_ABI;
pub const POLICY_ENV: &str = "CANT_POLICY";
pub const RUNTIME_READ: &[&str] = &[
"/etc/ld.so.cache",
"/etc/ld.so.preload",
"/etc/localtime",
"/usr/share/zoneinfo",
"/usr/lib/locale",
"/usr/share/locale",
"/etc/locale.alias",
"/usr/share/terminfo",
"/usr/lib/terminfo",
"/lib/terminfo",
"/etc/terminfo",
"/dev/zero",
"/dev/urandom",
"/dev/random",
];
pub const RUNTIME_READ_WRITE: &[&str] = &["/dev/null", "/dev/full", "/dev/tty"];
#[derive(Debug, Clone)]
pub struct Plan {
pub cant: String,
pub program: PathBuf,
pub argv0: OsString,
pub args: Vec<OsString>,
pub env: Vec<(OsString, OsString)>,
pub policy: Policy,
pub effective: Policy,
pub nested: bool,
pub missing_libs: Vec<String>,
}
#[derive(Debug, thiserror::Error)]
pub enum LaunchError {
#[error("cants run on Linux only (this is {})", std::env::consts::OS)]
Unsupported,
#[error("unsupported CPU architecture {0}")]
UnsupportedArch(&'static str),
#[cfg(target_os = "linux")]
#[error(transparent)]
Elf(#[from] ElfError),
#[error(
"Landlock is not available on this kernel, so cants cannot be sandboxed; refusing to run (see `cant check`)"
)]
LandlockUnavailable,
#[error(
"this kernel's Landlock ABI is {abi}; at least 3 (Linux 6.2) is required; refusing to run"
)]
LandlockTooOld {
abi: i32,
},
#[error("Landlock setup failed: {0}")]
Landlock(String),
#[error("seccomp setup failed: {0}")]
Seccomp(String),
#[error("cannot create {path}: {source}")]
Create {
path: PathBuf,
source: std::io::Error,
},
#[error("{cant} can't {verb} {shown} (not granted by the enclosing cant)")]
ParentDenies {
cant: String,
verb: &'static str,
shown: String,
},
#[error("malformed {POLICY_ENV}: {0}")]
ParentPolicy(String),
#[error("cannot execute {program}: {source}")]
Exec {
program: PathBuf,
source: std::io::Error,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KernelSupport {
pub landlock_abi: Option<i32>,
pub seccomp_arch: bool,
}
#[cfg(target_os = "linux")]
pub fn plan(
manifest: &cant_manifest::Manifest,
built: cant_policy::Built,
user_args: &[OsString],
parent: Option<&Policy>,
home: &std::path::Path,
) -> Result<Plan, LaunchError> {
linux::plan(manifest, built, user_args, parent, home)
}
#[cfg(not(target_os = "linux"))]
pub fn plan(
_manifest: &cant_manifest::Manifest,
_built: cant_policy::Built,
_user_args: &[OsString],
_parent: Option<&Policy>,
_home: &std::path::Path,
) -> Result<Plan, LaunchError> {
Err(LaunchError::Unsupported)
}
#[cfg(target_os = "linux")]
pub fn exec(plan: Plan, warn: &mut dyn FnMut(&str)) -> LaunchError {
linux::exec(plan, warn)
}
#[cfg(not(target_os = "linux"))]
pub fn exec(_plan: Plan, _warn: &mut dyn FnMut(&str)) -> LaunchError {
LaunchError::Unsupported
}
#[cfg(target_os = "linux")]
pub fn probe_kernel() -> KernelSupport {
KernelSupport {
landlock_abi: linux::landlock::probe_abi(),
seccomp_arch: cfg!(any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "riscv64"
)),
}
}
#[cfg(not(target_os = "linux"))]
pub fn probe_kernel() -> KernelSupport {
KernelSupport {
landlock_abi: None,
seccomp_arch: false,
}
}