Skip to main content

gunnar_server/
sandbox.rs

1//! **The build sandbox: no network, by construction** (roadmap phase 3).
2//!
3//! `BuildRecord::network` is a claim a builder makes about itself, and the
4//! provenance type says so out loud. This is what turns the claim into a fact:
5//! the build runs in a Linux **network namespace of its own**, with nothing in
6//! it but a loopback interface that is down. There is no route to anywhere, so
7//! a build that tries to fetch a dependency fails rather than succeeding
8//! quietly and making the record a lie.
9//!
10//! # How it works unprivileged, which is the whole trick
11//!
12//! Creating a network namespace needs `CAP_SYS_ADMIN`. An ordinary user does
13//! not have it — but a user who first creates a **user** namespace is root
14//! *inside* it, and root inside a user namespace may create the other kinds.
15//! So the child does, in order, before `exec`:
16//!
17//! 1. `unshare(CLONE_NEWUSER)` — now it is uid 65534 (overflow) with no map;
18//! 2. `deny` to `/proc/self/setgroups` — required before a uid map may be
19//!    written by an unprivileged process, and it is what stops the namespace
20//!    being used to drop groups;
21//! 3. `0 <euid> 1` to `/proc/self/uid_map` and the same for gid — so inside the
22//!    namespace it is root, and outside it is still the same unprivileged user
23//!    it always was. **No privilege is gained**: every file access is still
24//!    checked against the real uid.
25//! 4. `unshare(CLONE_NEWNET)` — allowed now, and the point of all of it.
26//!
27//! # Everything here runs between `fork` and `exec`
28//!
29//! Which is the most constrained place in Unix: the child has one thread, and
30//! any lock another thread held at `fork` is held for ever. So there is no
31//! allocation and no formatting — `rustix`'s `open`/`write` over stack buffers,
32//! and the map integers rendered by hand. That is why this file looks the way it
33//! does; it is not style.
34//!
35//! `rustix` and not `libc`, deliberately: the workspace chose it because on
36//! `x86_64-linux` its backend is raw syscalls written in Rust, which is what
37//! keeps the musl appliance free of a `PT_INTERP`. Reaching for `libc` to get
38//! one syscall rustix already spells would have undone that.
39//!
40//! # What it does not do
41//!
42//! No mount namespace, no seccomp filter, no cgroup. A build can still read the
43//! whole filesystem it is run as, and can still spend the machine's CPU. Those
44//! are the next things and they are named rather than implied: this module
45//! closes **network** hermeticity, which is the one that decides whether a
46//! dependency came from the vendored tree or from the internet.
47
48/// Everything the sandbox can fail at, as a number the child can report.
49///
50/// An enum rather than a message, because the failing code runs after `fork`
51/// and cannot allocate one. The parent turns it back into a sentence.
52#[derive(Debug, Clone, Copy, PartialEq, Eq)]
53pub enum SandboxError {
54    UserNamespace,
55    SetGroups,
56    UidMap,
57    GidMap,
58    NetworkNamespace,
59}
60
61impl SandboxError {
62    fn from_raw(code: i32) -> Option<SandboxError> {
63        Some(match code {
64            1 => SandboxError::UserNamespace,
65            2 => SandboxError::SetGroups,
66            3 => SandboxError::UidMap,
67            4 => SandboxError::GidMap,
68            5 => SandboxError::NetworkNamespace,
69            _ => return None,
70        })
71    }
72
73    /// What an operator can act on.
74    pub fn explain(self) -> &'static str {
75        match self {
76            SandboxError::UserNamespace => {
77                "this kernel refused to create a user namespace. Unprivileged user namespaces \
78                 may be disabled (`kernel.unprivileged_userns_clone=0`, or an AppArmor/Docker \
79                 profile). Without one, an ordinary user cannot make a network namespace and \
80                 the build cannot be made hermetic."
81            }
82            SandboxError::SetGroups => {
83                "writing `deny` to /proc/self/setgroups failed, so no uid map may be written"
84            }
85            SandboxError::UidMap => "writing /proc/self/uid_map failed",
86            SandboxError::GidMap => "writing /proc/self/gid_map failed",
87            SandboxError::NetworkNamespace => {
88                "this kernel refused to create a network namespace inside the user namespace"
89            }
90        }
91    }
92}
93
94/// Turn the child's exit status back into a sandbox failure, if it is one.
95///
96/// The child exits with a small reserved code when it could not enter the
97/// sandbox, **before** the build command runs. Reserved rather than shared with
98/// the build's own exit codes, so "the sandbox failed" is never mistaken for
99/// "the build failed" — the two need different people.
100pub const SANDBOX_EXIT_BASE: i32 = 120;
101
102/// The exit code a child uses when the sandbox could not be entered.
103pub fn exit_code(error: SandboxError) -> i32 {
104    SANDBOX_EXIT_BASE
105        + match error {
106            SandboxError::UserNamespace => 1,
107            SandboxError::SetGroups => 2,
108            SandboxError::UidMap => 3,
109            SandboxError::GidMap => 4,
110            SandboxError::NetworkNamespace => 5,
111        }
112}
113
114/// **Leave a forked child from its `pre_exec` hook**: `_exit(2)`, which ends the process
115/// and runs nothing. NOT `std::process::exit` (libc `exit(3)`): that runs the atexit
116/// handlers and the stdio cleanup of a process forked from a MULTI-threaded one, and a
117/// lock another thread held at the fork is never released there — the child parks on a
118/// futex for good (the gunnar suite, 2026-09-30: 2 h 30 min in the sandbox test; a server
119/// whose sandbox cannot be entered would hold that build forever).
120pub fn leave_child(code: i32) -> ! {
121    extern "C" {
122        fn _exit(status: i32) -> !;
123    }
124    // SAFETY: _exit(2) is async-signal-safe and takes no lock; it never returns.
125    unsafe { _exit(code) }
126}
127
128/// The failure a child's exit status describes, if it is a sandbox one.
129pub fn error_from_exit(code: i32) -> Option<SandboxError> {
130    if !(SANDBOX_EXIT_BASE..SANDBOX_EXIT_BASE + 16).contains(&code) {
131        return None;
132    }
133    SandboxError::from_raw(code - SANDBOX_EXIT_BASE)
134}
135
136/// Write `bytes` to `path`, allocating nothing.
137///
138/// Called between `fork` and `exec`, where nothing may allocate or take a lock.
139/// `rustix::fs::open` puts the path on the stack for a short one, and the write
140/// is one syscall.
141fn write_proc(path: &str, bytes: &[u8]) -> bool {
142    let Ok(fd) = rustix::fs::open(path, rustix::fs::OFlags::WRONLY, rustix::fs::Mode::empty())
143    else {
144        return false;
145    };
146    // One write, and it must take everything: `/proc` map files reject a
147    // partial write rather than continuing it, so a short write is a failure
148    // and not something to loop on.
149    matches!(rustix::io::write(&fd, bytes), Ok(n) if n == bytes.len())
150}
151
152/// Render `value` into `buf` as decimal, returning the slice used.
153///
154/// Hand-rolled because `format!` allocates and this runs after `fork`.
155fn render(value: u32, buf: &mut [u8; 12]) -> &[u8] {
156    if value == 0 {
157        buf[0] = b'0';
158        return &buf[..1];
159    }
160    let mut at = buf.len();
161    let mut value = value;
162    while value > 0 {
163        at -= 1;
164        buf[at] = b'0' + (value % 10) as u8;
165        value /= 10;
166    }
167    // Shift to the front so the caller gets a slice starting at zero.
168    let len = buf.len() - at;
169    buf.copy_within(at.., 0);
170    &buf[..len]
171}
172
173/// Enter a user namespace and then a network namespace of our own.
174///
175/// # Safety
176///
177/// Must be called between `fork` and `exec`, from the child. `unshare` moves
178/// the whole *process* into new namespaces, which is unsound while other
179/// threads are running; a forked child has exactly one thread, which is what
180/// makes this sound there and nowhere else. It also allocates nothing and takes
181/// no lock — see the module docs for why that matters in the same place.
182pub unsafe fn enter() -> Result<(), SandboxError> {
183    use rustix::thread::UnshareFlags;
184
185    let uid = rustix::process::geteuid().as_raw();
186    let gid = rustix::process::getegid().as_raw();
187
188    // `unshare_unsafe`, and the name is right: `unshare` changes the whole
189    // PROCESS's namespaces, which is unsound in a multi-threaded program. Here
190    // the process is a freshly forked child with exactly one thread, on its way
191    // to `exec` — the one place where it is sound, and the reason this function
192    // may only be called from `pre_exec`.
193    if unsafe { rustix::thread::unshare_unsafe(UnshareFlags::NEWUSER) }.is_err() {
194        return Err(SandboxError::UserNamespace);
195    }
196    // Required before an unprivileged uid map may be written, and it is what
197    // stops a user namespace being a way to drop supplementary groups.
198    if !write_proc("/proc/self/setgroups", b"deny") {
199        return Err(SandboxError::SetGroups);
200    }
201
202    // `0 <uid> 1` — root inside, the same user outside. No privilege is gained:
203    // every file access is still checked against the real uid.
204    let mut map = [0u8; 32];
205    let mut digits = [0u8; 12];
206    let mut at = 0usize;
207    map[at] = b'0';
208    at += 1;
209    map[at] = b' ';
210    at += 1;
211    let rendered = render(uid, &mut digits);
212    map[at..at + rendered.len()].copy_from_slice(rendered);
213    at += rendered.len();
214    map[at..at + 2].copy_from_slice(b" 1");
215    at += 2;
216    if !write_proc("/proc/self/uid_map", &map[..at]) {
217        return Err(SandboxError::UidMap);
218    }
219
220    let mut at = 0usize;
221    map[at] = b'0';
222    at += 1;
223    map[at] = b' ';
224    at += 1;
225    let rendered = render(gid, &mut digits);
226    map[at..at + rendered.len()].copy_from_slice(rendered);
227    at += rendered.len();
228    map[at..at + 2].copy_from_slice(b" 1");
229    at += 2;
230    if !write_proc("/proc/self/gid_map", &map[..at]) {
231        return Err(SandboxError::GidMap);
232    }
233
234    // The point of all of it.
235    if unsafe { rustix::thread::unshare_unsafe(UnshareFlags::NEWNET) }.is_err() {
236        return Err(SandboxError::NetworkNamespace);
237    }
238    Ok(())
239}
240
241/// Is an unprivileged sandbox available on this kernel at all?
242///
243/// Asked by forking a child that does nothing but try to enter one. **Not**
244/// inferred from a sysctl: there are three separate ways a distribution can
245/// forbid this (the sysctl, an AppArmor profile, a seccomp filter in a
246/// container), and reading one of them would answer for one of the three.
247pub fn available() -> Result<(), SandboxError> {
248    use std::os::unix::process::CommandExt as _;
249    // `true(1)` and not this binary: re-execing the server to probe would run
250    // whatever `main` does with no arguments, and the probe must do nothing at
251    // all beyond entering the namespace.
252    // A32-14, a process by design: the namespace probe runs `true(1)`, which does nothing, to learn whether this box may enter a namespace at all.
253    let mut command = std::process::Command::new("true");
254    // The hook is bound OUTSIDE the `unsafe` block for the same reason as in
255    // `runner.rs`: an `unsafe` block extends lexically into a closure written
256    // inside it, so the inner one was redundant (`unused_unsafe`) and its
257    // SAFETY note unenforced. Bound here, each `unsafe` covers one call.
258    let enter_sandbox = || {
259        // SAFETY: `pre_exec` runs in the forked child, single-threaded,
260        // before `exec` — which is `enter`'s whole requirement.
261        if let Err(err) = unsafe { enter() } {
262            leave_child(exit_code(err));
263        }
264        Ok(())
265    };
266    // SAFETY: `enter` allocates nothing and takes no lock, which is the whole
267    // requirement `pre_exec` imposes. [`leave_child`] (`_exit(2)`) in the child is the
268    // documented way to report a pre-exec failure without unwinding.
269    unsafe {
270        command.pre_exec(enter_sandbox);
271    }
272    let status = command
273        .stdin(std::process::Stdio::null())
274        .stdout(std::process::Stdio::null())
275        .stderr(std::process::Stdio::null())
276        .status();
277    // A probe that could not even be spawned is not a sandbox verdict, and must
278    // not be reported as one.
279    let Ok(status) = status else {
280        return Ok(());
281    };
282    match status.code().and_then(error_from_exit) {
283        Some(err) => Err(err),
284        None => Ok(()),
285    }
286}
287
288#[cfg(test)]
289mod tests {
290    use super::*;
291
292    /// **A `pre_exec` hook leaves by `_exit(2)`, never by `exit(3)`** (found 2026-09-30:
293    /// the gunnar suite hung 2 h 30 min in `a_build_in_the_sandbox_cannot_reach_a_listener_the_host_can`,
294    /// its forked child in the new user namespace, parked on a futex — `enter` failed and
295    /// the hook called `std::process::exit`, which runs the atexit handlers and stdio
296    /// cleanup of a process forked from a MULTI-threaded one: a lock another thread held at
297    /// the fork is never released there; and `spawn` itself waits on the exec pipe, so the
298    /// parent hangs with it). Reproduced deterministically: an atexit handler that stalls
299    /// 3 s, only in a forked child. The control arm (`std::process::exit`) runs it — the
300    /// spawn is held that long; [`leave_child`] runs nothing and exits at once with its code.
301    #[test]
302    fn a_pre_exec_hook_that_fails_leaves_at_once_even_when_an_atexit_handler_would_block() {
303        use std::os::unix::process::CommandExt as _;
304        use std::sync::atomic::{AtomicI32, Ordering};
305        static PARENT: AtomicI32 = AtomicI32::new(0);
306        extern "C" fn blocks_in_a_child() {
307            if std::process::id() as i32 != PARENT.load(Ordering::SeqCst) {
308                std::thread::sleep(std::time::Duration::from_secs(3));
309            }
310        }
311        extern "C" {
312            fn atexit(f: extern "C" fn()) -> i32;
313        }
314        PARENT.store(std::process::id() as i32, Ordering::SeqCst);
315        // SAFETY: registering a plain extern "C" fn with libc's atexit.
316        assert_eq!(unsafe { atexit(blocks_in_a_child) }, 0);
317        // How long the spawn is held, and the code the child left with.
318        let timed = |leave: fn(i32) -> !| -> (std::time::Duration, Option<i32>) {
319            let mut c = std::process::Command::new("true");
320            // SAFETY: the hook only leaves; it is what is under test.
321            unsafe {
322                c.pre_exec(move || leave(97));
323            }
324            let t0 = std::time::Instant::now();
325            let st = c.stdin(std::process::Stdio::null()).stdout(std::process::Stdio::null()).stderr(std::process::Stdio::null()).status().expect("spawned");
326            (t0.elapsed(), st.code())
327        };
328        let (held, code) = timed(|c| std::process::exit(c));
329        assert!(held >= std::time::Duration::from_millis(2500), "the control: exit(3) in the forked child ran the stalling handler (held {held:?}, code {code:?})");
330        let (held, code) = timed(leave_child);
331        assert_eq!(code, Some(97), "leave_child exits with its code");
332        assert!(held < std::time::Duration::from_millis(2500), "leave_child ran an atexit handler: held {held:?}");
333    }
334
335    #[test]
336    fn the_exit_codes_round_trip_and_do_not_collide_with_ordinary_ones() {
337        for err in [
338            SandboxError::UserNamespace,
339            SandboxError::SetGroups,
340            SandboxError::UidMap,
341            SandboxError::GidMap,
342            SandboxError::NetworkNamespace,
343        ] {
344            assert_eq!(error_from_exit(exit_code(err)), Some(err));
345        }
346        // A build that exits 1 is a failed build, not a failed sandbox. This is
347        // the confusion the reserved range exists to prevent.
348        for ordinary in [0, 1, 2, 101, 119] {
349            assert_eq!(error_from_exit(ordinary), None, "code {ordinary}");
350        }
351    }
352
353    #[test]
354    fn the_decimal_renderer_agrees_with_the_formatter() {
355        for value in [0u32, 1, 9, 10, 99, 1000, 65534, u32::MAX] {
356            let mut buf = [0u8; 12];
357            assert_eq!(
358                std::str::from_utf8(render(value, &mut buf)).unwrap(),
359                value.to_string(),
360                "rendering {value}"
361            );
362        }
363    }
364}
365
366#[cfg(test)]
367mod live_tests {
368    use super::*;
369    use std::io::Read as _;
370    use std::os::unix::process::CommandExt as _;
371
372    /// Run `bash -c <script>`, optionally inside the sandbox, and report
373    /// whether it succeeded.
374    fn run(script: &str, sandboxed: bool) -> Option<bool> {
375        let mut command = std::process::Command::new("bash");
376        command.arg("-c").arg(script);
377        if sandboxed {
378            // SAFETY: `enter` allocates nothing and takes no lock.
379            unsafe {
380                command.pre_exec(|| {
381                    if let Err(err) = enter() {
382                        leave_child(exit_code(err));
383                    }
384                    Ok(())
385                });
386            }
387        }
388        let status = command
389            .stdin(std::process::Stdio::null())
390            .stdout(std::process::Stdio::null())
391            .stderr(std::process::Stdio::null())
392            .status()
393            .ok()?;
394        // A sandbox that could not be entered is not a verdict about the
395        // script, so it is `None` and the caller skips rather than asserting.
396        if status.code().and_then(error_from_exit).is_some() {
397            return None;
398        }
399        Some(status.success())
400    }
401
402    /// **The sandbox really cuts the network**, and the control says the test
403    /// could have noticed if it did not.
404    ///
405    /// A listener on loopback is the target: inside a fresh network namespace
406    /// there is one interface, `lo`, and it is **down**, so the connect fails.
407    /// Outside, it succeeds — which is the half that makes the first assertion
408    /// mean something rather than being satisfied by a broken script.
409    #[test]
410    fn a_build_in_the_sandbox_cannot_reach_a_listener_the_host_can() {
411        let listener = std::net::TcpListener::bind("127.0.0.1:0").expect("bind");
412        let port = listener.local_addr().expect("addr").port();
413        // Accept in the background so a successful connect completes rather
414        // than sitting in the backlog.
415        std::thread::spawn(move || {
416            for mut stream in listener.incoming().take(4).flatten() {
417                let mut sink = [0u8; 1];
418                let _ = stream.read(&mut sink);
419            }
420        });
421
422        let script = format!("exec 3<>/dev/tcp/127.0.0.1/{port}");
423        let Some(outside) = run(&script, false) else {
424            unreachable!("the unsandboxed arm cannot report a sandbox failure")
425        };
426        assert!(
427            outside,
428            "the control failed: bash could not reach a listener on loopback without any \
429             sandbox, so this test cannot tell a cut network from a broken script"
430        );
431
432        match run(&script, true) {
433            None => eprintln!(
434                "skipped: this kernel does not allow unprivileged user namespaces, so the \
435                 sandbox cannot be exercised here. `gunnar runner` refuses to build rather \
436                 than building without one."
437            ),
438            Some(inside) => assert!(
439                !inside,
440                "a process inside the build sandbox reached a listener on the host's loopback. \
441                 The network namespace is not being entered, and every `network: false` in a \
442                 build record this server signs is a lie."
443            ),
444        }
445    }
446
447    /// The sandbox does not break the ordinary case: a build that touches only
448    /// the filesystem still runs, and still runs **as the same user**.
449    #[test]
450    fn a_build_in_the_sandbox_still_owns_the_files_it_writes() {
451        let dir = tempfile::tempdir().expect("tempdir");
452        let path = dir.path().join("written");
453        let script = format!("echo hello > {}", path.display());
454        match run(&script, true) {
455            None => eprintln!("skipped: no unprivileged user namespaces on this kernel"),
456            Some(ok) => {
457                assert!(
458                    ok,
459                    "a build that only writes a file failed inside the sandbox"
460                );
461                assert_eq!(
462                    std::fs::read_to_string(&path).expect("read it back"),
463                    "hello\n",
464                    "the sandboxed build could not write to the directory it was given. The uid \
465                     map is wrong: inside the namespace it must be root and outside it must \
466                     still be the same unprivileged user."
467                );
468            }
469        }
470    }
471}