#[cfg(target_os = "linux")]
pub(crate) mod linux;
#[cfg(target_os = "macos")]
pub(crate) mod macos;
use std::path::PathBuf;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Limits {
pub memory: Option<u64>,
pub cpu_secs: Option<u64>,
pub open_files: Option<u64>,
}
impl Default for Limits {
fn default() -> Self {
Self { memory: Some(4 << 30), cpu_secs: None, open_files: Some(256) }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Require {
#[default]
Full,
BestEffort,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Policy {
pub read: Vec<PathBuf>,
pub read_exec: Vec<PathBuf>,
pub write: Vec<PathBuf>,
pub limits: Limits,
pub require: Require,
}
impl Policy {
pub fn for_extension(ext_dir: impl Into<PathBuf>, data: impl Into<PathBuf>) -> Self {
let mut read_exec: Vec<PathBuf> = vec![ext_dir.into()];
let mut read = Vec::new();
#[cfg(target_os = "linux")]
{
for p in ["/usr/lib", "/usr/lib64", "/lib", "/lib64", "/usr/libexec", "/nix/store"] {
read_exec.push(p.into());
}
for p in [
"/etc/ld.so.cache",
"/etc/ld.so.preload",
"/etc/localtime",
"/usr/share/zoneinfo",
"/dev/null",
"/dev/urandom",
"/dev/random",
"/sys/devices/system/cpu",
"/sys/fs/cgroup",
] {
read.push(p.into());
}
}
#[cfg(target_os = "macos")]
{
for p in ["/usr/lib", "/System/Library", "/System/Volumes/Preboot/Cryptexes", "/System/Cryptexes", "/Library/Apple/usr/lib"] {
read_exec.push(p.into());
}
for p in ["/dev/null", "/dev/urandom", "/dev/random", "/private/var/db/timezone", "/usr/share/zoneinfo"] {
read.push(p.into());
}
}
Self { read, read_exec, write: vec![data.into()], limits: Limits::default(), require: Require::Full }
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Report {
pub mechanism: &'static str,
pub landlock_abi: Option<u32>,
pub files: bool,
pub network: bool,
pub syscalls: bool,
pub missing: Vec<String>,
pub notes: Vec<String>,
}
impl Report {
pub fn full(&self) -> bool {
self.missing.is_empty()
}
}
pub fn native_allowed() -> bool {
cfg!(any(target_os = "linux", target_os = "macos"))
}
pub fn probe() -> Report {
#[cfg(target_os = "linux")]
{
linux::probe()
}
#[cfg(target_os = "macos")]
{
macos::probe()
}
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
{
Report { mechanism: "none", missing: vec!["native extensions are not supported on this OS".into()], ..Default::default() }
}
}
#[cfg(unix)]
pub(crate) fn cloexec_all_but(keep: std::os::fd::RawFd) {
unsafe {
let mut l: libc::rlimit = std::mem::zeroed();
let max = if libc::getrlimit(libc::RLIMIT_NOFILE, &mut l) == 0 { (l.rlim_cur as u64).min(1 << 16) as i32 } else { 1024 };
for fd in 3..max.max(keep + 1) {
if fd != keep {
let f = libc::fcntl(fd, libc::F_GETFD);
if f >= 0 && f & libc::FD_CLOEXEC == 0 {
libc::fcntl(fd, libc::F_SETFD, f | libc::FD_CLOEXEC);
}
}
}
}
}
#[cfg(unix)]
pub(crate) fn command(
binary: &std::path::Path,
args: &[String],
policy: &Policy,
keep_fd: std::os::fd::RawFd,
env: &[(String, String)],
) -> crate::error::Result<(tokio::process::Command, Report)> {
#[cfg(target_os = "linux")]
let (prep, report, mut cmd) = {
let (p, r) = linux::prepare(policy, keep_fd)?;
(p, r, tokio::process::Command::new(binary))
};
#[cfg(target_os = "macos")]
let (prep, report, mut cmd) = {
let (p, r) = macos::prepare(policy, keep_fd)?;
let mut c = tokio::process::Command::new(macos::SANDBOX_EXEC);
c.arg("-p").arg(&p.profile).arg(binary);
(p, r, c)
};
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
{
let _ = (binary, args, policy, keep_fd, env);
return Err(crate::error::Error::Unsupported("native extensions don't run on this OS".into()));
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
{
cmd.args(args).env_clear().envs(env.iter().map(|(k, v)| (k, v))).kill_on_drop(true).process_group(0);
cmd.stdin(std::process::Stdio::null());
if let Some(dir) = policy.write.first() {
cmd.current_dir(dir);
}
unsafe {
cmd.pre_exec(move || prep.apply());
}
Ok((cmd, report))
}
}