| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | | use crate::guest::{dev_links, job_env, loopback_up, resolve_program, PATH}; |
| 20 | | use crate::home::Home; |
| 21 | | use crate::microvm::{Outcome, Sink}; |
| 22 | | use anyhow::{bail, Context, Result}; |
| 23 | | use flow_core::api::{Record, SecretInput}; |
| 24 | | use flow_core::{Environment, Job, MountSource, Network, Sandbox}; |
| 25 | | use serde::{Deserialize, Serialize}; |
| 26 | | use std::ffi::CString; |
| 27 | | use std::io::{BufRead, BufReader, Read}; |
| 28 | | use std::path::{Path, PathBuf}; |
| 29 | | use std::sync::{mpsc, OnceLock}; |
| 30 | | use std::time::{Duration, Instant}; |
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | | const JOB_ID: u32 = 1000; |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | 0× | pub fn environment(sandbox: &Sandbox, job: &Job) -> Environment { |
| 40 | | Environment { |
| 41 | 0× | runtime: "syscall".into(), |
| 42 | 0× | parts: vec![ |
| 43 | 0× | ("flow".into(), self_hash().to_string()), |
| 44 | 0× | ("kernel".into(), kernel_release()), |
| 45 | 0× | ("memory_mib".into(), sandbox.memory_mib.to_string()), |
| 46 | 0× | ("cpus".into(), sandbox.cpus.to_string()), |
| 47 | | ] |
| 48 | 0× | .into_iter() |
| 49 | 0× | .chain(job.mounts.iter().map(|m| (format!("mount {}", m.target), format!("{} {} MiB", m.source.as_str(), m.size_mib)))) |
| 50 | 0× | .collect(), |
| 51 | 0× | rootfs: sandbox.rootfs.clone(), |
| 52 | 0× | argv: job.exec.clone(), |
| 53 | 0× | env: job.env.iter().map(|(k, v)| (k.clone(), v.clone())).collect(), |
| 54 | | } |
| 55 | 0× | } |
| 56 | |
|
| 57 | 12× | pub(crate) fn self_hash() -> &'static str { |
| 58 | | static HASH: OnceLock<String> = OnceLock::new(); |
| 59 | 12× | HASH.get_or_init(|| crate::home::hash_file(Path::new("/proc/self/exe")).unwrap_or_else(|_| "unknown".into())) |
| 60 | 12× | } |
| 61 | |
|
| 62 | 0× | fn kernel_release() -> String { |
| 63 | 0× | crate::facts::kernel().unwrap_or_else(|| "unknown".into()) |
| 64 | 0× | } |
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | 3× | pub fn available() -> std::result::Result<(), String> { |
| 71 | 3× | Cgroups::delegated()?; |
| 72 | 0× | let out = std::process::Command::new("/proc/self/exe") |
| 73 | 0× | .arg("__sandbox-probe") |
| 74 | 0× | .stdin(std::process::Stdio::null()) |
| 75 | 0× | .output() |
| 76 | 0× | .map_err(|e| format!("cannot start the probe: {e}"))?; |
| 77 | 0× | if out.status.success() { |
| 78 | 0× | Ok(()) |
| 79 | | } else { |
| 80 | 0× | Err(String::from_utf8_lossy(&out.stderr).trim().to_string()) |
| 81 | | } |
| 82 | 3× | } |
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | 0× | pub fn probe_main() -> i32 { |
| 87 | 0× | let r = (|| -> std::result::Result<(), String> { |
| 88 | 0× | let (uid, gid) = unsafe { (libc::getuid(), libc::getgid()) }; |
| 89 | 0× | enter_namespaces(libc::CLONE_NEWUSER | libc::CLONE_NEWNS, uid, gid) |
| 90 | 0× | .map_err(|e| format!("user namespaces are not allowed for this program ({e}); on Ubuntu, kernel.apparmor_restrict_unprivileged_userns=1 denies them"))?; |
| 91 | 0× | mount("none", "/", "", libc::MS_REC | libc::MS_PRIVATE, "") |
| 92 | 0× | .and_then(|_| mount("tmpfs", "/tmp", "tmpfs", libc::MS_NOSUID | libc::MS_NODEV, "")) |
| 93 | 0× | .map_err(|e| { |
| 94 | 0× | format!( |
| 95 | | "user namespaces give no mount rights here ({e}); on Ubuntu, kernel.apparmor_restrict_unprivileged_userns=1 does this" |
| 96 | | ) |
| 97 | 0× | })?; |
| 98 | 0× | let abi = landlock_abi(); |
| 99 | 0× | if abi < 1 { |
| 100 | 0× | return Err("Landlock is not enabled in this kernel (it must be in the lsm= list)".into()); |
| 101 | 0× | } |
| 102 | 0× | no_new_privs()?; |
| 103 | 0× | seccomp(&[]).map_err(|e| format!("seccomp filters are not available: {e}")) |
| 104 | | })(); |
| 105 | 0× | match r { |
| 106 | 0× | Ok(()) => 0, |
| 107 | 0× | Err(e) => { |
| 108 | 0× | eprintln!("{e}"); |
| 109 | 0× | 1 |
| 110 | | } |
| 111 | | } |
| 112 | 0× | } |
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | | #[derive(Serialize, Deserialize)] |
| 118 | | struct Spec { |
| 119 | | |
| 120 | | rootfs: PathBuf, |
| 121 | | |
| 122 | | root: PathBuf, |
| 123 | | workspace: PathBuf, |
| 124 | | |
| 125 | | cgroup: PathBuf, |
| 126 | | argv: Vec<String>, |
| 127 | | env: Vec<(String, String)>, |
| 128 | | mounts: Vec<MountSpec>, |
| 129 | | loopback: bool, |
| 130 | | secrets: Vec<(String, String)>, |
| 131 | | } |
| 132 | |
|
| 133 | | #[derive(Serialize, Deserialize)] |
| 134 | | struct MountSpec { |
| 135 | | target: String, |
| 136 | | size_mib: u32, |
| 137 | | |
| 138 | | |
| 139 | | dir: Option<PathBuf>, |
| 140 | | } |
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | | #[allow(clippy::too_many_arguments)] |
| 145 | 0× | pub fn run( |
| 146 | 0× | home: &Home, |
| 147 | 0× | sandbox: &Sandbox, |
| 148 | 0× | job: &Job, |
| 149 | 0× | secrets: &[SecretInput], |
| 150 | 0× | workspace: &Path, |
| 151 | 0× | scratch: &Path, |
| 152 | 0× | timeout: Duration, |
| 153 | 0× | sink: &mut dyn Sink, |
| 154 | 0× | name: &str, |
| 155 | 0× | ) -> Result<Outcome> { |
| 156 | 0× | let cgroups = Cgroups::delegated().map_err(|e| anyhow::anyhow!("the syscall tier cannot run here: {e}"))?; |
| 157 | 0× | let image = home.rootfs(&sandbox.rootfs); |
| 158 | 0× | if !image.is_file() { |
| 159 | 0× | bail!("rootfs {} is not on this runner (import it with `flow rootfs import`)", sandbox.rootfs); |
| 160 | 0× | } |
| 161 | 0× | let image = home.verified(&image, &sandbox.rootfs)?; |
| 162 | 0× | let rootfs = unpacked(home, &sandbox.rootfs, &image)?; |
| 163 | 0× | let environment = environment(sandbox, job); |
| 164 | |
|
| 165 | 0× | let mut mounts = Vec::new(); |
| 166 | 0× | for (i, m) in job.mounts.iter().enumerate() { |
| 167 | 0× | let dir = match m.source { |
| 168 | 0× | MountSource::Tmpfs => None, |
| 169 | | MountSource::Disk => { |
| 170 | 0× | let d = disk_dir(scratch, job.mounts.iter().take(i).filter(|m| m.source == MountSource::Disk).count()); |
| 171 | 0× | std::fs::create_dir_all(&d)?; |
| 172 | 0× | Some(d) |
| 173 | | } |
| 174 | | }; |
| 175 | 0× | mounts.push(MountSpec { target: m.target.clone(), size_mib: m.size_mib, dir }); |
| 176 | | } |
| 177 | 0× | let root = scratch.join("root"); |
| 178 | 0× | std::fs::create_dir_all(&root)?; |
| 179 | 0× | let group = cgroups.job(name, sandbox.memory_mib, sandbox.cpus)?; |
| 180 | 0× | let spec = Spec { |
| 181 | 0× | rootfs, |
| 182 | 0× | root, |
| 183 | 0× | workspace: workspace.to_path_buf(), |
| 184 | 0× | cgroup: group.dir.clone(), |
| 185 | 0× | argv: job.exec.clone(), |
| 186 | 0× | env: job.env.iter().map(|(k, v)| (k.clone(), v.clone())).collect(), |
| 187 | 0× | mounts, |
| 188 | 0× | loopback: sandbox.network == Network::Loopback, |
| 189 | 0× | secrets: secrets.iter().map(|s| (s.path.clone(), s.value.clone())).collect(), |
| 190 | | }; |
| 191 | 0× | let spec_path = scratch.join("sandbox.json"); |
| 192 | | { |
| 193 | | use std::io::Write; |
| 194 | | use std::os::unix::fs::OpenOptionsExt; |
| 195 | 0× | let mut f = std::fs::OpenOptions::new().write(true).create_new(true).mode(0o600).open(&spec_path)?; |
| 196 | 0× | f.write_all(&serde_json::to_vec(&spec)?)?; |
| 197 | | } |
| 198 | |
|
| 199 | 0× | let started = Instant::now(); |
| 200 | 0× | let mut child = std::process::Command::new("/proc/self/exe") |
| 201 | 0× | .arg("__sandbox") |
| 202 | 0× | .arg(&spec_path) |
| 203 | 0× | .env_clear() |
| 204 | 0× | .current_dir(scratch) |
| 205 | 0× | .stdin(std::process::Stdio::null()) |
| 206 | 0× | .stdout(std::process::Stdio::piped()) |
| 207 | 0× | .stderr(std::process::Stdio::piped()) |
| 208 | 0× | .spawn() |
| 209 | 0× | .context("start the sandbox")?; |
| 210 | 0× | let (tx, rx) = mpsc::channel::<(bool, Vec<u8>)>(); |
| 211 | 0× | let out = child.stdout.take().unwrap(); |
| 212 | 0× | let err = child.stderr.take().unwrap(); |
| 213 | 0× | let tx2 = tx.clone(); |
| 214 | 0× | let readers = [std::thread::spawn(move || lines(out, true, tx)), std::thread::spawn(move || lines(err, false, tx2))]; |
| 215 | 0× | let mut failed_to_start = false; |
| 216 | 0× | let mut timed_out = false; |
| 217 | 0× | let mut cancelled = false; |
| 218 | 0× | let deadline = started + timeout; |
| 219 | 0× | let t = |s: &Instant| s.elapsed().as_millis() as u64; |
| 220 | 0× | let stop = |child: &mut std::process::Child| { |
| 221 | 0× | let _ = child.kill(); |
| 222 | 0× | group.kill(); |
| 223 | 0× | }; |
| 224 | | |
| 225 | | |
| 226 | 0× | let mut peak_memory_mib = None; |
| 227 | | loop { |
| 228 | 0× | if let Some(m) = crate::facts::peak_rss_mib(child.id()) { |
| 229 | 0× | peak_memory_mib = Some(m); |
| 230 | 0× | } |
| 231 | 0× | match rx.recv_timeout(Duration::from_millis(200)) { |
| 232 | 0× | Ok((stdout, line)) => { |
| 233 | 0× | let text = String::from_utf8_lossy(&line).into_owned(); |
| 234 | 0× | let rec = |stream: &str, text: &str| Record { t: t(&started), stream: stream.into(), text: text.into() }; |
| 235 | 0× | match text.strip_prefix(FAILED) { |
| 236 | 0× | Some(why) if !stdout => { |
| 237 | 0× | failed_to_start = true; |
| 238 | 0× | sink.record(rec("flow", &format!("the sandbox failed to start the job: {why}"))) |
| 239 | | } |
| 240 | 0× | _ => sink.record(rec(if stdout { "out" } else { "err" }, &text)), |
| 241 | | } |
| 242 | | } |
| 243 | 0× | Err(mpsc::RecvTimeoutError::Timeout) => {} |
| 244 | 0× | Err(mpsc::RecvTimeoutError::Disconnected) => break, |
| 245 | | } |
| 246 | 0× | if !timed_out && !cancelled && sink.cancelled() { |
| 247 | 0× | cancelled = true; |
| 248 | 0× | stop(&mut child); |
| 249 | 0× | sink.record(Record { t: t(&started), stream: "flow".into(), text: "stopped: the run was cancelled".into() }); |
| 250 | 0× | } |
| 251 | 0× | if Instant::now() >= deadline && !timed_out && !cancelled { |
| 252 | 0× | timed_out = true; |
| 253 | 0× | stop(&mut child); |
| 254 | 0× | sink.record(Record { |
| 255 | 0× | t: t(&started), |
| 256 | 0× | stream: "flow".into(), |
| 257 | 0× | text: format!("stopped at the time limit of {} s", timeout.as_secs()), |
| 258 | 0× | }); |
| 259 | 0× | } |
| 260 | | } |
| 261 | 0× | let status = child.wait()?; |
| 262 | 0× | for r in readers { |
| 263 | 0× | let _ = r.join(); |
| 264 | 0× | } |
| 265 | 0× | if group.out_of_memory() { |
| 266 | 0× | sink.record(Record { |
| 267 | 0× | t: t(&started), |
| 268 | 0× | stream: "flow".into(), |
| 269 | 0× | text: format!("the job ran out of memory: the sandbox allows {} MiB", sandbox.memory_mib), |
| 270 | 0× | }); |
| 271 | 0× | } |
| 272 | 0× | let exit_code = status.code(); |
| 273 | 0× | if exit_code.is_none() && !timed_out && !cancelled && !failed_to_start { |
| 274 | 0× | sink.record(Record { |
| 275 | 0× | t: t(&started), |
| 276 | 0× | stream: "flow".into(), |
| 277 | 0× | text: format!("the sandbox ended without the job's exit code ({status})"), |
| 278 | 0× | }); |
| 279 | 0× | } |
| 280 | 0× | drop(group); |
| 281 | 0× | let _ = std::fs::remove_file(&spec_path); |
| 282 | | Ok(Outcome { |
| 283 | 0× | exit_code: if timed_out || cancelled { None } else { exit_code }, |
| 284 | 0× | duration_ms: t(&started), |
| 285 | 0× | environment, |
| 286 | 0× | timed_out, |
| 287 | 0× | cancelled, |
| 288 | 0× | peak_memory_mib, |
| 289 | 0× | pruned: Vec::new(), |
| 290 | | }) |
| 291 | 0× | } |
| 292 | |
|
| 293 | |
|
| 294 | 4× | pub fn disk_dir(scratch: &Path, n: usize) -> PathBuf { |
| 295 | 4× | scratch.join(format!("mount-{n}")) |
| 296 | 4× | } |
| 297 | |
|
| 298 | |
|
| 299 | | pub(crate) const FAILED: &str = "\u{1e}F "; |
| 300 | |
|
| 301 | 12× | pub(crate) fn lines(r: impl Read, stdout: bool, tx: mpsc::Sender<(bool, Vec<u8>)>) { |
| 302 | 12× | let mut r = BufReader::new(r); |
| 303 | 12× | let mut buf = Vec::new(); |
| 304 | | loop { |
| 305 | 21× | buf.clear(); |
| 306 | | |
| 307 | 21× | match (&mut r).take(16 * 1024).read_until(b'\n', &mut buf) { |
| 308 | 12× | Ok(0) | Err(_) => break, |
| 309 | | Ok(_) => { |
| 310 | 9× | if buf.last() == Some(&b'\n') { |
| 311 | 9× | buf.pop(); |
| 312 | 9× | } |
| 313 | 9× | if tx.send((stdout, buf.clone())).is_err() { |
| 314 | 0× | break; |
| 315 | 9× | } |
| 316 | | } |
| 317 | | } |
| 318 | | } |
| 319 | 12× | } |
| 320 | |
|
| 321 | |
|
| 322 | |
|
| 323 | |
|
| 324 | |
|
| 325 | 0× | fn unpacked(home: &Home, hash: &str, image: &Path) -> Result<PathBuf> { |
| 326 | 0× | let trees = home.root.join("trees"); |
| 327 | 0× | let dir = trees.join(hash); |
| 328 | 0× | if dir.is_dir() { |
| 329 | 0× | return Ok(dir); |
| 330 | 0× | } |
| 331 | 0× | std::fs::create_dir_all(&trees)?; |
| 332 | 0× | let nanos = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.subsec_nanos()).unwrap_or(0); |
| 333 | 0× | let tmp = trees.join(format!(".{hash}.{}.{nanos}", std::process::id())); |
| 334 | 0× | std::fs::create_dir_all(&tmp)?; |
| 335 | 0× | let out = std::process::Command::new("debugfs") |
| 336 | 0× | .arg("-R") |
| 337 | 0× | .arg(format!("rdump / \"{}\"", tmp.display())) |
| 338 | 0× | .arg(image) |
| 339 | 0× | .output() |
| 340 | 0× | .context("run debugfs (e2fsprogs) to unpack the rootfs")?; |
| 341 | 0× | let empty = std::fs::read_dir(&tmp).map(|mut d| d.next().is_none()).unwrap_or(true); |
| 342 | 0× | if !out.status.success() || empty { |
| 343 | 0× | let _ = crate::workspace::remove_tree(&tmp); |
| 344 | 0× | bail!("unpacking rootfs {}: {}", &hash[..12.min(hash.len())], String::from_utf8_lossy(&out.stderr).trim()); |
| 345 | 0× | } |
| 346 | 0× | match std::fs::rename(&tmp, &dir) { |
| 347 | 0× | Ok(()) => Ok(dir), |
| 348 | | |
| 349 | 0× | Err(_) if dir.is_dir() => { |
| 350 | 0× | let _ = crate::workspace::remove_tree(&tmp); |
| 351 | 0× | Ok(dir) |
| 352 | | } |
| 353 | 0× | Err(e) => Err(e).with_context(|| format!("keep the unpacked rootfs at {}", dir.display())), |
| 354 | | } |
| 355 | 0× | } |
| 356 | |
|
| 357 | |
|
| 358 | |
|
| 359 | |
|
| 360 | |
|
| 361 | |
|
| 362 | |
|
| 363 | | #[derive(Clone)] |
| 364 | | pub struct Cgroups { |
| 365 | | base: PathBuf, |
| 366 | | } |
| 367 | |
|
| 368 | |
|
| 369 | | const MAIN_LEAF: &str = "flow-main"; |
| 370 | |
|
| 371 | | impl Cgroups { |
| 372 | 3× | pub fn delegated() -> std::result::Result<Cgroups, String> { |
| 373 | | static ONCE: OnceLock<std::result::Result<Cgroups, String>> = OnceLock::new(); |
| 374 | 3× | ONCE.get_or_init(Self::set_up).clone() |
| 375 | 3× | } |
| 376 | |
|
| 377 | 3× | fn set_up() -> std::result::Result<Cgroups, String> { |
| 378 | 3× | let own = std::fs::read_to_string("/proc/self/cgroup").map_err(|e| format!("read /proc/self/cgroup: {e}"))?; |
| 379 | 3× | let rel = own |
| 380 | 3× | .lines() |
| 381 | 3× | .find_map(|l| l.strip_prefix("0::")) |
| 382 | 3× | .ok_or("this machine does not use cgroup v2 (the unified hierarchy)")? |
| 383 | 0× | .trim() |
| 384 | 0× | .to_string(); |
| 385 | 0× | let mut base = Path::new("/sys/fs/cgroup").join(rel.trim_start_matches('/')); |
| 386 | 0× | if base.file_name().is_some_and(|n| n == MAIN_LEAF) { |
| 387 | 0× | base.pop(); |
| 388 | 0× | } |
| 389 | 0× | let controllers = std::fs::read_to_string(base.join("cgroup.controllers")).unwrap_or_default(); |
| 390 | 0× | let has = |c: &str| controllers.split_whitespace().any(|x| x == c); |
| 391 | 0× | let delegate_hint = "run the runner as a systemd service with Delegate=yes, or as a user service; for `flow run`, use `systemd-run --user --scope -p Delegate=yes flow run …`"; |
| 392 | 0× | if !has("cpu") || !has("memory") { |
| 393 | 0× | return Err(format!("the cgroup {} has no cpu and memory controllers to give jobs; {delegate_hint}", base.display())); |
| 394 | 0× | } |
| 395 | 0× | let writable = |
| 396 | 0× | |p: &Path| CString::new(p.as_os_str().as_encoded_bytes()).is_ok_and(|c| unsafe { libc::access(c.as_ptr(), libc::W_OK) } == 0); |
| 397 | 0× | if !writable(&base) || !writable(&base.join("cgroup.subtree_control")) || !writable(&base.join("cgroup.procs")) { |
| 398 | 0× | return Err(format!("this runner may not manage its cgroup {}; {delegate_hint}", base.display())); |
| 399 | 0× | } |
| 400 | | |
| 401 | | |
| 402 | 0× | let me = std::process::id().to_string(); |
| 403 | 0× | let others = std::fs::read_to_string(base.join("cgroup.procs")).unwrap_or_default(); |
| 404 | 0× | if others.lines().any(|p| p.trim() != me) { |
| 405 | 0× | return Err(format!("the cgroup {} holds other processes; {delegate_hint}", base.display())); |
| 406 | 0× | } |
| 407 | 0× | let leaf = base.join(MAIN_LEAF); |
| 408 | 0× | if !leaf.is_dir() { |
| 409 | 0× | std::fs::create_dir(&leaf).map_err(|e| format!("make {}: {e}", leaf.display()))?; |
| 410 | 0× | } |
| 411 | 0× | std::fs::write(leaf.join("cgroup.procs"), "0").map_err(|e| format!("move into {}: {e}", leaf.display()))?; |
| 412 | 0× | let want: Vec<&str> = ["cpu", "memory", "pids"].into_iter().filter(|c| has(c)).collect(); |
| 413 | 0× | let enable = want.iter().map(|c| format!("+{c}")).collect::<Vec<_>>().join(" "); |
| 414 | 0× | std::fs::write(base.join("cgroup.subtree_control"), &enable) |
| 415 | 0× | .map_err(|e| format!("enable {enable} in {}: {e} (another process may still be in it; {delegate_hint})", base.display()))?; |
| 416 | 0× | Ok(Cgroups { base }) |
| 417 | 3× | } |
| 418 | |
|
| 419 | | |
| 420 | 0× | pub fn job(&self, name: &str, memory_mib: u32, cpus: u32) -> Result<JobGroup> { |
| 421 | 0× | let dir = self.base.join(name); |
| 422 | 0× | if dir.is_dir() { |
| 423 | 0× | |
| 424 | 0× | drop(JobGroup { dir: dir.clone() }); |
| 425 | 0× | } |
| 426 | 0× | std::fs::create_dir(&dir).with_context(|| format!("make the job's cgroup {}", dir.display()))?; |
| 427 | 0× | let group = JobGroup { dir }; |
| 428 | 0× | let set = |file: &str, value: String| { |
| 429 | 0× | std::fs::write(group.dir.join(file), &value).with_context(|| format!("set {file} to {value} in {}", group.dir.display())) |
| 430 | 0× | }; |
| 431 | 0× | set("memory.max", (u64::from(memory_mib) << 20).to_string())?; |
| 432 | 0× | if group.dir.join("memory.swap.max").exists() { |
| 433 | 0× | set("memory.swap.max", "0".into())?; |
| 434 | 0× | } |
| 435 | 0× | set("cpu.max", format!("{} 100000", u64::from(cpus.max(1)) * 100_000))?; |
| 436 | 0× | Ok(group) |
| 437 | 0× | } |
| 438 | | } |
| 439 | |
|
| 440 | |
|
| 441 | | pub struct JobGroup { |
| 442 | | dir: PathBuf, |
| 443 | | } |
| 444 | |
|
| 445 | | impl JobGroup { |
| 446 | 0× | fn kill(&self) { |
| 447 | 0× | let _ = std::fs::write(self.dir.join("cgroup.kill"), "1"); |
| 448 | 0× | } |
| 449 | |
|
| 450 | 0× | fn out_of_memory(&self) -> bool { |
| 451 | 0× | std::fs::read_to_string(self.dir.join("memory.events")) |
| 452 | 0× | .ok() |
| 453 | 0× | .and_then(|s| s.lines().find_map(|l| l.strip_prefix("oom_kill ")).and_then(|n| n.trim().parse::<u64>().ok())) |
| 454 | 0× | .is_some_and(|n| n > 0) |
| 455 | 0× | } |
| 456 | | } |
| 457 | |
|
| 458 | | impl Drop for JobGroup { |
| 459 | 0× | fn drop(&mut self) { |
| 460 | 0× | self.kill(); |
| 461 | 0× | for _ in 0..100 { |
| 462 | 0× | if std::fs::remove_dir(&self.dir).is_ok() || !self.dir.exists() { |
| 463 | 0× | return; |
| 464 | 0× | } |
| 465 | 0× | std::thread::sleep(Duration::from_millis(50)); |
| 466 | | } |
| 467 | 0× | eprintln!("flow: could not remove the cgroup {}", self.dir.display()); |
| 468 | 0× | } |
| 469 | | } |
| 470 | |
|
| 471 | |
|
| 472 | |
|
| 473 | |
|
| 474 | |
|
| 475 | 0× | pub fn sandbox_main(spec_path: &Path) -> i32 { |
| 476 | 0× | match helper(spec_path) { |
| 477 | 0× | Ok(code) => code, |
| 478 | 0× | Err(e) => { |
| 479 | 0× | eprintln!("{FAILED}{e}"); |
| 480 | 0× | 125 |
| 481 | | } |
| 482 | | } |
| 483 | 0× | } |
| 484 | |
|
| 485 | 0× | fn helper(spec_path: &Path) -> std::result::Result<i32, String> { |
| 486 | 0× | let spec: Spec = serde_json::from_slice(&std::fs::read(spec_path).map_err(|e| format!("read the spec: {e}"))?) |
| 487 | 0× | .map_err(|e| format!("parse the spec: {e}"))?; |
| 488 | 0× | let _ = std::fs::remove_file(spec_path); |
| 489 | 0× | if spec.argv.is_empty() { |
| 490 | 0× | return Err("empty argv".into()); |
| 491 | 0× | } |
| 492 | 0× | std::fs::write(spec.cgroup.join("cgroup.procs"), "0").map_err(|e| format!("join the job's cgroup: {e}"))?; |
| 493 | 0× | let (uid, gid) = unsafe { (libc::getuid(), libc::getgid()) }; |
| 494 | 0× | enter_namespaces( |
| 495 | 0× | libc::CLONE_NEWUSER | libc::CLONE_NEWNS | libc::CLONE_NEWPID | libc::CLONE_NEWNET | libc::CLONE_NEWIPC | libc::CLONE_NEWUTS, |
| 496 | 0× | uid, |
| 497 | 0× | gid, |
| 498 | 0× | )?; |
| 499 | | |
| 500 | | |
| 501 | 0× | let pid = unsafe { libc::fork() }; |
| 502 | 0× | if pid < 0 { |
| 503 | 0× | return Err(format!("fork: {}", std::io::Error::last_os_error())); |
| 504 | 0× | } |
| 505 | 0× | if pid == 0 { |
| 506 | 0× | let code = match init(&spec) { |
| 507 | 0× | Ok(code) => code, |
| 508 | 0× | Err(e) => { |
| 509 | 0× | eprintln!("{FAILED}{e}"); |
| 510 | 0× | 125 |
| 511 | | } |
| 512 | | }; |
| 513 | 0× | unsafe { libc::_exit(code) }; |
| 514 | 0× | } |
| 515 | 0× | Ok(wait_for(pid)) |
| 516 | 0× | } |
| 517 | |
|
| 518 | |
|
| 519 | |
|
| 520 | 0× | fn wait_for(pid: libc::pid_t) -> i32 { |
| 521 | 0× | let mut status = 0; |
| 522 | | loop { |
| 523 | 0× | let r = unsafe { libc::waitpid(-1, &mut status, 0) }; |
| 524 | 0× | if r == pid { |
| 525 | 0× | break; |
| 526 | 0× | } |
| 527 | 0× | if r < 0 && std::io::Error::last_os_error().raw_os_error() != Some(libc::EINTR) { |
| 528 | 0× | return 125; |
| 529 | 0× | } |
| 530 | | } |
| 531 | 0× | if libc::WIFEXITED(status) { |
| 532 | 0× | libc::WEXITSTATUS(status) |
| 533 | | } else { |
| 534 | 0× | 128 + libc::WTERMSIG(status) |
| 535 | | } |
| 536 | 0× | } |
| 537 | |
|
| 538 | |
|
| 539 | |
|
| 540 | 0× | fn enter_namespaces(flags: libc::c_int, uid: u32, gid: u32) -> std::result::Result<(), String> { |
| 541 | 0× | if unsafe { libc::unshare(flags) } != 0 { |
| 542 | 0× | return Err(format!("unshare: {}", std::io::Error::last_os_error())); |
| 543 | 0× | } |
| 544 | 0× | let write = |file: &str, body: String| std::fs::write(file, body).map_err(|e| format!("write {file}: {e}")); |
| 545 | 0× | write("/proc/self/setgroups", "deny".into())?; |
| 546 | 0× | write("/proc/self/uid_map", format!("{JOB_ID} {uid} 1\n"))?; |
| 547 | 0× | write("/proc/self/gid_map", format!("{JOB_ID} {gid} 1\n")) |
| 548 | 0× | } |
| 549 | |
|
| 550 | |
|
| 551 | 0× | fn init(spec: &Spec) -> std::result::Result<i32, String> { |
| 552 | | |
| 553 | 0× | unsafe { libc::prctl(libc::PR_SET_PDEATHSIG, libc::SIGKILL) }; |
| 554 | 0× | let nosuid = libc::MS_NOSUID | libc::MS_NODEV; |
| 555 | 0× | mount("none", "/", "", libc::MS_REC | libc::MS_PRIVATE, "")?; |
| 556 | 0× | let root = spec.root.to_str().ok_or("the root path is not UTF-8")?; |
| 557 | 0× | let at = |p: &str| format!("{root}{p}"); |
| 558 | 0× | bind(spec.rootfs.to_str().ok_or("the rootfs path is not UTF-8")?, root)?; |
| 559 | 0× | for d in ["/src", "/tmp", "/proc", "/dev"] { |
| 560 | 0× | if !Path::new(&at(d)).is_dir() { |
| 561 | 0× | return Err(format!("the image has no {d} directory")); |
| 562 | 0× | } |
| 563 | | } |
| 564 | 0× | remount_bind(root, libc::MS_RDONLY | nosuid)?; |
| 565 | 0× | bind(spec.workspace.to_str().ok_or("the workspace path is not UTF-8")?, &at("/src"))?; |
| 566 | 0× | mount("tmpfs", &at("/tmp"), "tmpfs", nosuid, "mode=1777")?; |
| 567 | 0× | mount("proc", &at("/proc"), "proc", nosuid | libc::MS_NOEXEC, "")?; |
| 568 | | |
| 569 | 0× | mount("tmpfs", &at("/dev"), "tmpfs", libc::MS_NOSUID | libc::MS_NOEXEC, "mode=755")?; |
| 570 | 0× | for d in ["null", "zero", "full", "random", "urandom"] { |
| 571 | 0× | let target = at(&format!("/dev/{d}")); |
| 572 | 0× | std::fs::File::create(&target).map_err(|e| format!("make {target}: {e}"))?; |
| 573 | 0× | bind(&format!("/dev/{d}"), &target)?; |
| 574 | | } |
| 575 | 0× | dev_links(&at("/dev"))?; |
| 576 | 0× | std::fs::create_dir(at("/dev/shm")).map_err(|e| format!("make /dev/shm: {e}"))?; |
| 577 | 0× | mount("tmpfs", &at("/dev/shm"), "tmpfs", nosuid, "mode=1777")?; |
| 578 | 0× | if Path::new(&at("/run")).is_dir() { |
| 579 | 0× | mount("tmpfs", &at("/run"), "tmpfs", nosuid, "mode=755")?; |
| 580 | 0× | } |
| 581 | | |
| 582 | 0× | for m in &spec.mounts { |
| 583 | 0× | let target = at(&m.target); |
| 584 | 0× | std::fs::create_dir_all(&target).map_err(|e| format!("make {}: {e}", m.target))?; |
| 585 | 0× | match &m.dir { |
| 586 | 0× | Some(dir) => bind(dir.to_str().ok_or("a mount path is not UTF-8")?, &target)?, |
| 587 | 0× | None => mount("tmpfs", &target, "tmpfs", nosuid, &format!("size={}m,mode=755", m.size_mib))?, |
| 588 | | } |
| 589 | | } |
| 590 | | |
| 591 | 0× | if !spec.secrets.is_empty() { |
| 592 | 0× | if !Path::new(&at("/run")).is_dir() { |
| 593 | 0× | return Err("the image has no /run, where secrets are mounted".into()); |
| 594 | 0× | } |
| 595 | 0× | let dir = at("/run/secrets"); |
| 596 | 0× | std::fs::create_dir_all(&dir).map_err(|e| format!("mkdir {dir}: {e}"))?; |
| 597 | 0× | mount("tmpfs", &dir, "tmpfs", nosuid | libc::MS_NOEXEC, "size=1m,mode=700")?; |
| 598 | 0× | for (path, value) in &spec.secrets { |
| 599 | 0× | let Some(rel) = path.strip_prefix("/run/secrets/") else { |
| 600 | 0× | return Err(format!("secret path {path:?} is not under /run/secrets/")); |
| 601 | | }; |
| 602 | 0× | let full = format!("{dir}/{rel}"); |
| 603 | 0× | if let Some(parent) = Path::new(&full).parent() { |
| 604 | 0× | std::fs::create_dir_all(parent).map_err(|e| format!("mkdir for {path}: {e}"))?; |
| 605 | 0× | } |
| 606 | 0× | std::fs::write(&full, value.as_bytes()).map_err(|e| format!("write {path}: {e}"))?; |
| 607 | 0× | std::fs::set_permissions(&full, std::os::unix::fs::PermissionsExt::from_mode(0o400)) |
| 608 | 0× | .map_err(|e| format!("chmod {path}: {e}"))?; |
| 609 | | } |
| 610 | 0× | } |
| 611 | 0× | let host = b"flow"; |
| 612 | 0× | unsafe { libc::sethostname(host.as_ptr() as *const libc::c_char, host.len()) }; |
| 613 | 0× | if spec.loopback { |
| 614 | 0× | loopback_up()?; |
| 615 | 0× | } |
| 616 | | |
| 617 | | |
| 618 | 0× | std::env::set_current_dir(root).map_err(|e| format!("cd {root}: {e}"))?; |
| 619 | 0× | let dot = c"."; |
| 620 | 0× | if unsafe { libc::syscall(libc::SYS_pivot_root, dot.as_ptr(), dot.as_ptr()) } != 0 { |
| 621 | 0× | return Err(format!("pivot_root: {}", std::io::Error::last_os_error())); |
| 622 | 0× | } |
| 623 | 0× | if unsafe { libc::umount2(dot.as_ptr(), libc::MNT_DETACH) } != 0 { |
| 624 | 0× | return Err(format!("detach the old root: {}", std::io::Error::last_os_error())); |
| 625 | 0× | } |
| 626 | 0× | std::env::set_current_dir("/src").map_err(|e| format!("cd /src: {e}"))?; |
| 627 | |
|
| 628 | 0× | let vars = job_env(std::fs::read_to_string("/etc/flow/env").ok().as_deref(), &spec.env); |
| 629 | 0× | let path = vars.get("PATH").map(String::as_str).unwrap_or(PATH); |
| 630 | 0× | let program = |
| 631 | 0× | cstr(&resolve_program(&spec.argv[0], path).ok_or_else(|| format!("cannot run {:?}: not found in {path}", spec.argv[0]))?)?; |
| 632 | 0× | let argv: Vec<CString> = spec.argv.iter().map(|a| cstr(a)).collect::<Result<_, _>>()?; |
| 633 | 0× | let env: Vec<CString> = vars.iter().map(|(k, v)| cstr(&format!("{k}={v}"))).collect::<Result<_, _>>()?; |
| 634 | 0× | let mut writable: Vec<String> = ["/src", "/tmp", "/dev"].into_iter().map(String::from).collect(); |
| 635 | 0× | writable.extend(spec.mounts.iter().map(|m| m.target.clone())); |
| 636 | 0× | let pid = unsafe { libc::fork() }; |
| 637 | 0× | if pid < 0 { |
| 638 | 0× | return Err(format!("fork: {}", std::io::Error::last_os_error())); |
| 639 | 0× | } |
| 640 | 0× | if pid == 0 { |
| 641 | | |
| 642 | 0× | let confined = (|| { |
| 643 | 0× | unsafe { |
| 644 | 0× | let null = libc::open(c"/dev/null".as_ptr(), libc::O_RDONLY); |
| 645 | 0× | libc::dup2(null, 0); |
| 646 | 0× | libc::setsid(); |
| 647 | 0× | } |
| 648 | 0× | no_new_privs()?; |
| 649 | 0× | landlock(&writable)?; |
| 650 | 0× | seccomp(DENIED) |
| 651 | | })(); |
| 652 | | unsafe { |
| 653 | 0× | if let Err(e) = confined { |
| 654 | 0× | let msg = format!("{FAILED}{e}\n"); |
| 655 | 0× | libc::write(2, msg.as_ptr() as *const libc::c_void, msg.len()); |
| 656 | 0× | libc::_exit(125); |
| 657 | 0× | } |
| 658 | 0× | let mut a: Vec<*const libc::c_char> = argv.iter().map(|c| c.as_ptr()).collect(); |
| 659 | 0× | a.push(std::ptr::null()); |
| 660 | 0× | let mut e: Vec<*const libc::c_char> = env.iter().map(|c| c.as_ptr()).collect(); |
| 661 | 0× | e.push(std::ptr::null()); |
| 662 | 0× | libc::execve(program.as_ptr(), a.as_ptr(), e.as_ptr()); |
| 663 | 0× | let msg = format!("flow: cannot run {:?}: {}\n", spec.argv[0], std::io::Error::last_os_error()); |
| 664 | 0× | libc::write(2, msg.as_ptr() as *const libc::c_void, msg.len()); |
| 665 | 0× | libc::_exit(127); |
| 666 | | } |
| 667 | 0× | } |
| 668 | 0× | Ok(wait_for(pid)) |
| 669 | 0× | } |
| 670 | |
|
| 671 | 0× | fn cstr(s: &str) -> std::result::Result<CString, String> { |
| 672 | 0× | CString::new(s).map_err(|_| format!("{s:?} contains NUL")) |
| 673 | 0× | } |
| 674 | |
|
| 675 | 0× | fn mount(src: &str, target: &str, fstype: &str, flags: libc::c_ulong, data: &str) -> std::result::Result<(), String> { |
| 676 | 0× | let (s, t, f, d) = (cstr(src)?, cstr(target)?, cstr(fstype)?, cstr(data)?); |
| 677 | 0× | let fstype = if fstype.is_empty() { std::ptr::null() } else { f.as_ptr() }; |
| 678 | 0× | let data = if data.is_empty() { std::ptr::null() } else { d.as_ptr() as *const libc::c_void }; |
| 679 | 0× | if unsafe { libc::mount(s.as_ptr(), t.as_ptr(), fstype, flags, data) } != 0 { |
| 680 | 0× | return Err(format!("mount {src} on {target}: {}", std::io::Error::last_os_error())); |
| 681 | 0× | } |
| 682 | 0× | Ok(()) |
| 683 | 0× | } |
| 684 | |
|
| 685 | 0× | fn bind(src: &str, target: &str) -> std::result::Result<(), String> { |
| 686 | 0× | mount(src, target, "", libc::MS_BIND | libc::MS_REC, "") |
| 687 | 0× | } |
| 688 | |
|
| 689 | |
|
| 690 | |
|
| 691 | 0× | fn remount_bind(target: &str, flags: libc::c_ulong) -> std::result::Result<(), String> { |
| 692 | 0× | let t = cstr(target)?; |
| 693 | 0× | let mut st: libc::statvfs = unsafe { std::mem::zeroed() }; |
| 694 | 0× | if unsafe { libc::statvfs(t.as_ptr(), &mut st) } != 0 { |
| 695 | 0× | return Err(format!("statvfs {target}: {}", std::io::Error::last_os_error())); |
| 696 | 0× | } |
| 697 | 0× | let kept = [ |
| 698 | 0× | (libc::ST_NOSUID, libc::MS_NOSUID), |
| 699 | 0× | (libc::ST_NODEV, libc::MS_NODEV), |
| 700 | 0× | (libc::ST_NOEXEC, libc::MS_NOEXEC), |
| 701 | 0× | (libc::ST_NOATIME, libc::MS_NOATIME), |
| 702 | 0× | (libc::ST_NODIRATIME, libc::MS_NODIRATIME), |
| 703 | 0× | (libc::ST_RELATIME, libc::MS_RELATIME), |
| 704 | 0× | ] |
| 705 | 0× | .into_iter() |
| 706 | 0× | .filter(|(st_flag, _)| st.f_flag & *st_flag != 0) |
| 707 | 0× | .fold(0, |acc, (_, ms)| acc | ms); |
| 708 | 0× | mount("none", target, "", libc::MS_BIND | libc::MS_REMOUNT | flags | kept, "") |
| 709 | 0× | } |
| 710 | |
|
| 711 | 0× | fn no_new_privs() -> std::result::Result<(), String> { |
| 712 | 0× | if unsafe { libc::prctl(libc::PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) } != 0 { |
| 713 | 0× | return Err(format!("no_new_privs: {}", std::io::Error::last_os_error())); |
| 714 | 0× | } |
| 715 | 0× | Ok(()) |
| 716 | 0× | } |
| 717 | |
|
| 718 | |
|
| 719 | |
|
| 720 | | const LANDLOCK_CREATE_RULESET_VERSION: libc::c_uint = 1; |
| 721 | | const LANDLOCK_RULE_PATH_BENEATH: libc::c_int = 1; |
| 722 | | const ACCESS_EXECUTE: u64 = 1; |
| 723 | | const ACCESS_READ_FILE: u64 = 1 << 2; |
| 724 | | const ACCESS_READ_DIR: u64 = 1 << 3; |
| 725 | |
|
| 726 | | const ACCESS_ABI1: u64 = (1 << 13) - 1; |
| 727 | | const ACCESS_REFER: u64 = 1 << 13; |
| 728 | | const ACCESS_TRUNCATE: u64 = 1 << 14; |
| 729 | |
|
| 730 | | #[repr(C)] |
| 731 | | struct RulesetAttr { |
| 732 | | handled_access_fs: u64, |
| 733 | | } |
| 734 | |
|
| 735 | | #[repr(C, packed)] |
| 736 | | struct PathBeneath { |
| 737 | | allowed_access: u64, |
| 738 | | parent_fd: i32, |
| 739 | | } |
| 740 | |
|
| 741 | |
|
| 742 | 0× | fn landlock_abi() -> i64 { |
| 743 | 0× | unsafe { libc::syscall(libc::SYS_landlock_create_ruleset, std::ptr::null::<RulesetAttr>(), 0usize, LANDLOCK_CREATE_RULESET_VERSION) } |
| 744 | 0× | } |
| 745 | |
|
| 746 | |
|
| 747 | 0× | fn landlock(writable: &[String]) -> std::result::Result<(), String> { |
| 748 | 0× | let abi = landlock_abi(); |
| 749 | 0× | if abi < 1 { |
| 750 | 0× | return Err("Landlock is not available".into()); |
| 751 | 0× | } |
| 752 | 0× | let mut all = ACCESS_ABI1; |
| 753 | 0× | if abi >= 2 { |
| 754 | 0× | all |= ACCESS_REFER; |
| 755 | 0× | } |
| 756 | 0× | if abi >= 3 { |
| 757 | 0× | all |= ACCESS_TRUNCATE; |
| 758 | 0× | } |
| 759 | 0× | let attr = RulesetAttr { handled_access_fs: all }; |
| 760 | 0× | let ruleset = unsafe { libc::syscall(libc::SYS_landlock_create_ruleset, &attr, std::mem::size_of::<RulesetAttr>(), 0) }; |
| 761 | 0× | if ruleset < 0 { |
| 762 | 0× | return Err(format!("landlock ruleset: {}", std::io::Error::last_os_error())); |
| 763 | 0× | } |
| 764 | 0× | let ruleset = ruleset as libc::c_int; |
| 765 | 0× | let rule = |path: &str, access: u64| -> std::result::Result<(), String> { |
| 766 | 0× | let p = cstr(path)?; |
| 767 | 0× | let fd = unsafe { libc::open(p.as_ptr(), libc::O_PATH | libc::O_CLOEXEC) }; |
| 768 | 0× | if fd < 0 { |
| 769 | 0× | return Err(format!("open {path} for Landlock: {}", std::io::Error::last_os_error())); |
| 770 | 0× | } |
| 771 | 0× | let beneath = PathBeneath { allowed_access: access, parent_fd: fd }; |
| 772 | 0× | let rc = unsafe { libc::syscall(libc::SYS_landlock_add_rule, ruleset, LANDLOCK_RULE_PATH_BENEATH, &beneath, 0) }; |
| 773 | 0× | unsafe { libc::close(fd) }; |
| 774 | 0× | if rc != 0 { |
| 775 | 0× | return Err(format!("landlock rule for {path}: {}", std::io::Error::last_os_error())); |
| 776 | 0× | } |
| 777 | 0× | Ok(()) |
| 778 | 0× | }; |
| 779 | 0× | rule("/", ACCESS_EXECUTE | ACCESS_READ_FILE | ACCESS_READ_DIR)?; |
| 780 | 0× | for w in writable { |
| 781 | 0× | rule(w, all)?; |
| 782 | | } |
| 783 | 0× | if unsafe { libc::syscall(libc::SYS_landlock_restrict_self, ruleset, 0) } != 0 { |
| 784 | 0× | return Err(format!("landlock restrict: {}", std::io::Error::last_os_error())); |
| 785 | 0× | } |
| 786 | 0× | unsafe { libc::close(ruleset) }; |
| 787 | 0× | Ok(()) |
| 788 | 0× | } |
| 789 | |
|
| 790 | |
|
| 791 | |
|
| 792 | | #[cfg(target_arch = "x86_64")] |
| 793 | | const AUDIT_ARCH: u32 = 0xC000_003E; |
| 794 | | #[cfg(target_arch = "aarch64")] |
| 795 | | const AUDIT_ARCH: u32 = 0xC000_00B7; |
| 796 | | #[cfg(target_arch = "riscv64")] |
| 797 | | const AUDIT_ARCH: u32 = 0xC000_00F3; |
| 798 | |
|
| 799 | |
|
| 800 | |
|
| 801 | |
|
| 802 | | const DENIED: &[libc::c_long] = &[ |
| 803 | | libc::SYS_ptrace, |
| 804 | | libc::SYS_process_vm_readv, |
| 805 | | libc::SYS_process_vm_writev, |
| 806 | | libc::SYS_mount, |
| 807 | | libc::SYS_umount2, |
| 808 | | libc::SYS_pivot_root, |
| 809 | | libc::SYS_open_tree, |
| 810 | | libc::SYS_move_mount, |
| 811 | | libc::SYS_fsopen, |
| 812 | | libc::SYS_fsconfig, |
| 813 | | libc::SYS_fsmount, |
| 814 | | libc::SYS_fspick, |
| 815 | | libc::SYS_bpf, |
| 816 | | libc::SYS_perf_event_open, |
| 817 | | libc::SYS_keyctl, |
| 818 | | libc::SYS_add_key, |
| 819 | | libc::SYS_request_key, |
| 820 | | libc::SYS_init_module, |
| 821 | | libc::SYS_finit_module, |
| 822 | | libc::SYS_delete_module, |
| 823 | | libc::SYS_kexec_load, |
| 824 | | libc::SYS_swapon, |
| 825 | | libc::SYS_swapoff, |
| 826 | | libc::SYS_reboot, |
| 827 | | libc::SYS_io_uring_setup, |
| 828 | | libc::SYS_io_uring_enter, |
| 829 | | libc::SYS_io_uring_register, |
| 830 | | ]; |
| 831 | |
|
| 832 | |
|
| 833 | |
|
| 834 | |
|
| 835 | | const BPF_LD: u16 = 0x00; |
| 836 | | const BPF_W: u16 = 0x00; |
| 837 | | const BPF_ABS: u16 = 0x20; |
| 838 | | const BPF_JMP: u16 = 0x05; |
| 839 | | const BPF_JEQ: u16 = 0x10; |
| 840 | | const BPF_JGE: u16 = 0x30; |
| 841 | | const BPF_K: u16 = 0x00; |
| 842 | | const BPF_RET: u16 = 0x06; |
| 843 | |
|
| 844 | |
|
| 845 | |
|
| 846 | |
|
| 847 | 0× | fn seccomp(denied: &[libc::c_long]) -> std::result::Result<(), String> { |
| 848 | | const LD: u16 = BPF_LD | BPF_W | BPF_ABS; |
| 849 | | const JEQ: u16 = BPF_JMP | BPF_JEQ | BPF_K; |
| 850 | | const RET: u16 = BPF_RET | BPF_K; |
| 851 | 0× | let op = |code: u16, k: u32, jt: u8, jf: u8| libc::sock_filter { code, jt, jf, k }; |
| 852 | 0× | let errno = |e: libc::c_int| libc::SECCOMP_RET_ERRNO | (e as u32 & libc::SECCOMP_RET_DATA); |
| 853 | | |
| 854 | | |
| 855 | 0× | let mut f = vec![op(LD, 4, 0, 0), op(JEQ, AUDIT_ARCH, 1, 0), op(RET, libc::SECCOMP_RET_KILL_PROCESS, 0, 0), op(LD, 0, 0, 0)]; |
| 856 | | #[cfg(target_arch = "x86_64")] |
| 857 | | { |
| 858 | | |
| 859 | | const JGE: u16 = BPF_JMP | BPF_JGE | BPF_K; |
| 860 | 0× | f.push(op(JGE, 0x4000_0000, 0, 1)); |
| 861 | 0× | f.push(op(RET, errno(libc::EPERM), 0, 0)); |
| 862 | | } |
| 863 | 0× | for nr in denied { |
| 864 | 0× | f.push(op(JEQ, *nr as u32, 0, 1)); |
| 865 | 0× | f.push(op(RET, errno(libc::EPERM), 0, 0)); |
| 866 | 0× | } |
| 867 | 0× | f.push(op(JEQ, libc::SYS_socket as u32, 0, 3)); |
| 868 | 0× | f.push(op(LD, 16, 0, 0)); |
| 869 | 0× | f.push(op(JEQ, libc::AF_UNIX as u32, 1, 0)); |
| 870 | 0× | f.push(op(RET, errno(libc::EAFNOSUPPORT), 0, 0)); |
| 871 | 0× | f.push(op(RET, libc::SECCOMP_RET_ALLOW, 0, 0)); |
| 872 | 0× | let prog = libc::sock_fprog { len: f.len() as u16, filter: f.as_mut_ptr() }; |
| 873 | 0× | if unsafe { libc::syscall(libc::SYS_seccomp, libc::SECCOMP_SET_MODE_FILTER, 0, &prog) } != 0 { |
| 874 | 0× | return Err(format!("seccomp: {}", std::io::Error::last_os_error())); |
| 875 | 0× | } |
| 876 | 0× | Ok(()) |
| 877 | 0× | } |
| 878 | |
|
| 879 | | #[cfg(all(test, not(target_os = "android")))] |
| 880 | | mod tests { |
| 881 | | |
| 882 | | #[test] |
| 883 | 1× | fn bpf_opcodes_are_the_kernels() { |
| 884 | | use super::*; |
| 885 | 1× | assert_eq!( |
| 886 | | [BPF_LD, BPF_W, BPF_ABS, BPF_JMP, BPF_JEQ, BPF_JGE, BPF_K, BPF_RET], |
| 887 | 1× | [libc::BPF_LD, libc::BPF_W, libc::BPF_ABS, libc::BPF_JMP, libc::BPF_JEQ, libc::BPF_JGE, libc::BPF_K, libc::BPF_RET] |
| 888 | 8× | .map(|v| v as u16) |
| 889 | | ); |
| 890 | 1× | } |
| 891 | | } |