From 632a08c8543f0db533efc7bfb6728b50a77ae3d8 Mon Sep 17 00:00:00 2001 From: aur-build Date: Wed, 18 Nov 2026 22:34:16 +0000 Subject: sandbox: the seam between the pipeline and where build code runs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The step machine spawned its own subprocesses, which quietly made "build code runs on this machine, inside a devtools chroot, under §14's sudo rules" a pipeline fact rather than a deployment fact. Every build subprocess now goes through SandboxBackend: acquire a lease, run argv shaped commands (never a shell string), move content-addressed blobs, destroy. The loopback backend is today's behavior wearing the interface - same child_env shim protocol, same captured output - which is why the shim suite passes untouched: a step that cannot tell loopback from a remote sandbox is the property under test. --- src/sandbox/mod.rs | 131 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 src/sandbox/mod.rs (limited to 'src/sandbox/mod.rs') diff --git a/src/sandbox/mod.rs b/src/sandbox/mod.rs new file mode 100644 index 0000000..18c98ad --- /dev/null +++ b/src/sandbox/mod.rs @@ -0,0 +1,131 @@ +//! The sandbox seam: where build code actually runs (§8, remote-builder design). +//! +//! The pipeline executes untrusted recipe code, and the security boundary around +//! it has so far been "a chroot on this machine, with sudo rules" (§14). That +//! boundary is a *deployment* fact, not a pipeline fact: the step machine should +//! not know whether it is talking to a local process, a Kata container on a +//! cluster, or a loopback harness. So every subprocess the pipeline runs goes +//! through this interface instead of spawning one itself. +//! +//! Two implementations exist: +//! +//! * [`loopback::LoopbackBackend`] — today's behavior: commands run as local +//! subprocesses of the hub process, with the §16a shim protocol applied +//! through [`Config::child_env`]. This is what `cargo test` and a single +//! machine with devtools use. +//! * (planned) a Kata/agent-sandbox backend that leases disposable, TTL-bounded +//! sandboxes and speaks the wire protocol; the pipeline cannot tell the +//! difference, which is the entire point of the seam. +//! +//! The unit of isolation is a *sandbox lease*: [`SandboxBackend::acquire`] +//! hands out a handle, steps run commands on it, and the handle is destroyable +//! at any time. Nothing may assume a sandbox outlives its TTL, and nothing may +//! assume two tasks share one. Blob transfer is content-addressed by design: +//! there is no "path on the sandbox" concept in the interface at all, which +//! removes a class of hostile-write bugs that a path-based API would invite. + +use std::io::Read; +use std::path::PathBuf; +use std::sync::Arc; +use std::time::Duration; + +use crate::error::Result; + +pub mod loopback; + +/// A command to run inside a sandbox. Deliberately argv-shaped, not +/// `Command`-shaped: a remote sandbox must never be handed a shell string, and +/// the environment is the backend's business (see `Config::child_env`), not the +/// caller's. +#[derive(Debug, Clone)] +pub struct Cmd { + pub argv: Vec, + pub cwd: Option, +} + +impl Cmd { + pub fn new(argv: I) -> Self + where + I: IntoIterator, + S: Into, + { + Self { argv: argv.into_iter().map(Into::into).collect(), cwd: None } + } + pub fn in_dir(mut self, dir: impl Into) -> Self { + self.cwd = Some(dir.into()); + self + } + /// The program name, for error messages. + pub fn name(&self) -> &str { + self.argv.first().map(String::as_str).unwrap_or("") + } +} + +/// A finished command: exit status plus captured output. `code` is `None` when +/// the child died by signal — `success()` is false either way, matching what +/// `ExitStatus::success` would have said before the seam existed. +#[derive(Debug)] +pub struct Exit { + pub code: Option, + pub stdout: Vec, + pub stderr: Vec, +} + +impl Exit { + pub fn success(&self) -> bool { + self.code == Some(0) + } +} + +/// What a caller asks the pool for. `profile` is the only affinity key warm +/// pools need today (one warm root per build profile, §8.3); `task` is for +/// attribution and rendezvous, `ttl` is the guarantee that the sandbox is dead +/// no matter what this process then does, and `disk_mb` is the admission hint +/// that replaces the hub's local disk-headroom check (§8.2) once builds leave +/// the hub's filesystem. +#[derive(Debug, Clone)] +pub struct SandboxSpec { + pub profile: String, + pub task: String, + pub ttl: Duration, + pub disk_mb: u64, +} + +impl SandboxSpec { + pub fn new(profile: impl Into, task: impl Into, ttl: Duration) -> Self { + Self { profile: profile.into(), task: task.into(), ttl, disk_mb: 0 } + } +} + +/// Pool state for the UI and notices: who is warm, who is busy, what profile. +#[derive(Debug, Clone)] +pub struct SandboxInfo { + pub id: String, + pub profile: String, + pub warm: bool, +} + +/// A leased sandbox. `run` streams output through `on_log` as it arrives and +/// also returns it captured, so a step can parse stdout while the build log +/// stays complete. `put`/`get` move content-addressed blobs in and out — the +/// hub pulls artifacts by digest and never accepts a destination path from a +/// sandbox, which is the same I9 posture the local staging-containment checks +/// already enforce. +pub trait Sandbox: Send + Sync { + fn id(&self) -> &str; + fn run(&self, cmd: &Cmd, on_log: &mut dyn FnMut(&[u8])) -> Result; + fn put(&self, digest: &str, r: &mut dyn Read) -> Result<()>; + fn get(&self, digest: &str) -> Result>; + /// Idempotent, and also happens by itself when the TTL bites. Destroying a + /// sandbox must never be the only thing standing between untrusted code and + /// the world. + fn destroy(&self) -> Result<()>; +} + +/// The pool. `acquire` may return a pre-warmed sandbox, cold-start one, or fail +/// when at quota; callers treat a failed acquire like a blocked build, not a +/// broken one. +pub trait SandboxBackend: Send + Sync { + fn acquire(&self, spec: &SandboxSpec) -> Result>; + fn status(&self) -> Result>; +} -- cgit v1.2.3