diff options
Diffstat (limited to 'src/program.rs')
| -rw-r--r-- | src/program.rs | 63 |
1 files changed, 56 insertions, 7 deletions
diff --git a/src/program.rs b/src/program.rs index 8c4a565..ca3575c 100644 --- a/src/program.rs +++ b/src/program.rs @@ -5,8 +5,9 @@ //! empty. Objections are JSON lines on standard output; an exit status but 0 is a //! failure. //! -//! `IN` is read-only (for its owner, as root ignores modes). Not yet: a sandbox (no -//! network, nothing writable but `OUT`). +//! In a sandbox (`Runner`), by bubblewrap: everything read-only but `OUT`, no network, +//! namespaces of its own, no capabilities. `IN` is read-only (for its owner) also +//! without. use std::collections::BTreeMap; use std::path::{Path, PathBuf}; @@ -68,15 +69,26 @@ pub struct Outcome { pub out: Scratch, } -/// Runs `program` as `role`, named `name` (a view's or validator's), on `given`, in a -/// scratch directory below `scratch`. +/// Where and how programs run: their scratch directories, and the sandbox. +#[derive(Debug, Clone)] +pub struct Runner { + pub scratch: PathBuf, + /// bubblewrap (`bwrap`), to run programs in a sandbox: everything read-only but + /// `OUT`, no network, namespaces of their own, no capabilities. None runs them as + /// they are. + pub sandbox: Option<PathBuf>, +} + +/// Runs `program` as `role`, named `name` (a view's or validator's), on `given`, by +/// `runner`. pub fn run( program: &Path, role: Role, name: &str, given: &Given, - scratch: &Path, + runner: &Runner, ) -> Result<Outcome> { + let scratch = runner.scratch.as_path(); let what = format!("{} {name} ({})", role_name(role), program.display()); let input = Scratch::new(scratch, "in-")?; let out = Scratch::new(scratch, "out-")?; @@ -105,10 +117,34 @@ pub fn run( } input.set_modes(0o400)?; let program = std::path::absolute(program).unwrap_or_else(|_| program.to_owned()); - let output = Command::new(&program) + let mut command = match &runner.sandbox { + None => { + let mut command = Command::new(&program); + command.current_dir(input.path()); + command + } + Some(bwrap) => { + // By name, from our PATH: the command's environment is empty. + let mut command = Command::new(in_path(bwrap)); + command + .args(["--ro-bind", "/", "/", "--dev", "/dev", "--proc", "/proc"]) + .args(["--tmpfs", "/tmp"]) + // After /tmp, in case they are below it. + .arg("--ro-bind") + .args([input.path(), input.path()]) + .arg("--bind") + .args([out.path(), out.path()]) + .args(["--unshare-all", "--die-with-parent", "--new-session"]) + .args(["--cap-drop", "ALL", "--chdir"]) + .arg(input.path()) + .arg("--") + .arg(&program); + command + } + }; + let output = command .arg(input.path()) .arg(out.path()) - .current_dir(input.path()) .env_clear() .stdin(Stdio::null()) .output() @@ -199,3 +235,16 @@ pub fn files_below(dir: &Path) -> Result<BTreeMap<PathBuf, Vec<u8>>> { } Ok(files) } + +/// `program`, if it is a name without a directory, as found in `PATH`; else as it is. +fn in_path(program: &Path) -> PathBuf { + if program.components().count() != 1 { + return program.to_owned(); + } + std::env::var_os("PATH") + .iter() + .flat_map(std::env::split_paths) + .map(|dir| dir.join(program)) + .find(|path| path.is_file()) + .unwrap_or_else(|| program.to_owned()) +} |
