1#[cfg(target_os = "linux")]
20pub(crate) mod linux;
21#[cfg(target_os = "macos")]
22pub(crate) mod macos;
23
24use std::path::PathBuf;
25
26#[derive(Debug, Clone, Copy, PartialEq, Eq)]
28pub struct Limits {
29 pub memory: Option<u64>,
31 pub cpu_secs: Option<u64>,
33 pub open_files: Option<u64>,
35}
36
37impl Default for Limits {
38 fn default() -> Self {
39 Self { memory: Some(4 << 30), cpu_secs: None, open_files: Some(256) }
40 }
41}
42
43#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
45pub enum Require {
46 #[default]
49 Full,
50 BestEffort,
52}
53
54#[derive(Debug, Clone, Default, PartialEq, Eq)]
56pub struct Policy {
57 pub read: Vec<PathBuf>,
59 pub read_exec: Vec<PathBuf>,
61 pub write: Vec<PathBuf>,
63 pub limits: Limits,
65 pub require: Require,
67}
68
69impl Policy {
70 pub fn for_extension(ext_dir: impl Into<PathBuf>, data: impl Into<PathBuf>) -> Self {
74 let mut read_exec: Vec<PathBuf> = vec![ext_dir.into()];
75 let mut read = Vec::new();
76 #[cfg(target_os = "linux")]
77 {
78 for p in ["/usr/lib", "/usr/lib64", "/lib", "/lib64", "/usr/libexec", "/nix/store"] {
79 read_exec.push(p.into());
80 }
81 for p in [
82 "/etc/ld.so.cache",
83 "/etc/ld.so.preload",
84 "/etc/localtime",
85 "/usr/share/zoneinfo",
86 "/dev/null",
87 "/dev/urandom",
88 "/dev/random",
89 "/sys/devices/system/cpu",
90 "/sys/fs/cgroup",
91 ] {
92 read.push(p.into());
93 }
94 }
95 #[cfg(target_os = "macos")]
96 {
97 for p in ["/usr/lib", "/System/Library", "/System/Volumes/Preboot/Cryptexes", "/System/Cryptexes", "/Library/Apple/usr/lib"] {
98 read_exec.push(p.into());
99 }
100 for p in ["/dev/null", "/dev/urandom", "/dev/random", "/private/var/db/timezone", "/usr/share/zoneinfo"] {
101 read.push(p.into());
102 }
103 }
104 Self { read, read_exec, write: vec![data.into()], limits: Limits::default(), require: Require::Full }
105 }
106}
107
108#[derive(Debug, Clone, Default, PartialEq, Eq)]
110pub struct Report {
111 pub mechanism: &'static str,
113 pub landlock_abi: Option<u32>,
115 pub files: bool,
117 pub network: bool,
119 pub syscalls: bool,
121 pub missing: Vec<String>,
123 pub notes: Vec<String>,
126}
127
128impl Report {
129 pub fn full(&self) -> bool {
131 self.missing.is_empty()
132 }
133}
134
135pub fn native_allowed() -> bool {
137 cfg!(any(target_os = "linux", target_os = "macos"))
138}
139
140pub fn probe() -> Report {
142 #[cfg(target_os = "linux")]
143 {
144 linux::probe()
145 }
146 #[cfg(target_os = "macos")]
147 {
148 macos::probe()
149 }
150 #[cfg(not(any(target_os = "linux", target_os = "macos")))]
151 {
152 Report { mechanism: "none", missing: vec!["native extensions are not supported on this OS".into()], ..Default::default() }
153 }
154}
155
156#[cfg(unix)]
158pub(crate) fn cloexec_all_but(keep: std::os::fd::RawFd) {
159 unsafe {
161 let mut l: libc::rlimit = std::mem::zeroed();
162 let max = if libc::getrlimit(libc::RLIMIT_NOFILE, &mut l) == 0 { (l.rlim_cur as u64).min(1 << 16) as i32 } else { 1024 };
163 for fd in 3..max.max(keep + 1) {
164 if fd != keep {
165 let f = libc::fcntl(fd, libc::F_GETFD);
166 if f >= 0 && f & libc::FD_CLOEXEC == 0 {
167 libc::fcntl(fd, libc::F_SETFD, f | libc::FD_CLOEXEC);
168 }
169 }
170 }
171 }
172}
173
174#[cfg(unix)]
177pub(crate) fn command(
178 binary: &std::path::Path,
179 args: &[String],
180 policy: &Policy,
181 keep_fd: std::os::fd::RawFd,
182 env: &[(String, String)],
183) -> crate::error::Result<(tokio::process::Command, Report)> {
184 #[cfg(target_os = "linux")]
185 let (prep, report, mut cmd) = {
186 let (p, r) = linux::prepare(policy, keep_fd)?;
187 (p, r, tokio::process::Command::new(binary))
188 };
189 #[cfg(target_os = "macos")]
190 let (prep, report, mut cmd) = {
191 let (p, r) = macos::prepare(policy, keep_fd)?;
192 let mut c = tokio::process::Command::new(macos::SANDBOX_EXEC);
193 c.arg("-p").arg(&p.profile).arg(binary);
194 (p, r, c)
195 };
196 #[cfg(not(any(target_os = "linux", target_os = "macos")))]
197 {
198 let _ = (binary, args, policy, keep_fd, env);
199 return Err(crate::error::Error::Unsupported("native extensions don't run on this OS".into()));
200 }
201 #[cfg(any(target_os = "linux", target_os = "macos"))]
202 {
203 cmd.args(args).env_clear().envs(env.iter().map(|(k, v)| (k, v))).kill_on_drop(true).process_group(0);
204 cmd.stdin(std::process::Stdio::null());
205 if let Some(dir) = policy.write.first() {
206 cmd.current_dir(dir);
207 }
208 unsafe {
210 cmd.pre_exec(move || prep.apply());
211 }
212 Ok((cmd, report))
213 }
214}