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 let mut command = std::process::Command::new("true");
253 // The hook is bound OUTSIDE the `unsafe` block for the same reason as in
254 // `runner.rs`: an `unsafe` block extends lexically into a closure written
255 // inside it, so the inner one was redundant (`unused_unsafe`) and its
256 // SAFETY note unenforced. Bound here, each `unsafe` covers one call.
257 let enter_sandbox = || {
258 // SAFETY: `pre_exec` runs in the forked child, single-threaded,
259 // before `exec` — which is `enter`'s whole requirement.
260 if let Err(err) = unsafe { enter() } {
261 leave_child(exit_code(err));
262 }
263 Ok(())
264 };
265 // SAFETY: `enter` allocates nothing and takes no lock, which is the whole
266 // requirement `pre_exec` imposes. [`leave_child`] (`_exit(2)`) in the child is the
267 // documented way to report a pre-exec failure without unwinding.
268 unsafe {
269 command.pre_exec(enter_sandbox);
270 }
271 let status = command
272 .stdin(std::process::Stdio::null())
273 .stdout(std::process::Stdio::null())
274 .stderr(std::process::Stdio::null())
275 .status();
276 // A probe that could not even be spawned is not a sandbox verdict, and must
277 // not be reported as one.
278 let Ok(status) = status else {
279 return Ok(());
280 };
281 match status.code().and_then(error_from_exit) {
282 Some(err) => Err(err),
283 None => Ok(()),
284 }
285}
286
287#[cfg(test)]
288mod tests {
289 use super::*;
290
291 /// **A `pre_exec` hook leaves by `_exit(2)`, never by `exit(3)`** (found 2026-09-30:
292 /// the gunnar suite hung 2 h 30 min in `a_build_in_the_sandbox_cannot_reach_a_listener_the_host_can`,
293 /// its forked child in the new user namespace, parked on a futex — `enter` failed and
294 /// the hook called `std::process::exit`, which runs the atexit handlers and stdio
295 /// cleanup of a process forked from a MULTI-threaded one: a lock another thread held at
296 /// the fork is never released there; and `spawn` itself waits on the exec pipe, so the
297 /// parent hangs with it). Reproduced deterministically: an atexit handler that stalls
298 /// 3 s, only in a forked child. The control arm (`std::process::exit`) runs it — the
299 /// spawn is held that long; [`leave_child`] runs nothing and exits at once with its code.
300 #[test]
301 fn a_pre_exec_hook_that_fails_leaves_at_once_even_when_an_atexit_handler_would_block() {
302 use std::os::unix::process::CommandExt as _;
303 use std::sync::atomic::{AtomicI32, Ordering};
304 static PARENT: AtomicI32 = AtomicI32::new(0);
305 extern "C" fn blocks_in_a_child() {
306 if std::process::id() as i32 != PARENT.load(Ordering::SeqCst) {
307 std::thread::sleep(std::time::Duration::from_secs(3));
308 }
309 }
310 extern "C" {
311 fn atexit(f: extern "C" fn()) -> i32;
312 }
313 PARENT.store(std::process::id() as i32, Ordering::SeqCst);
314 // SAFETY: registering a plain extern "C" fn with libc's atexit.
315 assert_eq!(unsafe { atexit(blocks_in_a_child) }, 0);
316 // How long the spawn is held, and the code the child left with.
317 let timed = |leave: fn(i32) -> !| -> (std::time::Duration, Option<i32>) {
318 let mut c = std::process::Command::new("true");
319 // SAFETY: the hook only leaves; it is what is under test.
320 unsafe {
321 c.pre_exec(move || leave(97));
322 }
323 let t0 = std::time::Instant::now();
324 let st = c.stdin(std::process::Stdio::null()).stdout(std::process::Stdio::null()).stderr(std::process::Stdio::null()).status().expect("spawned");
325 (t0.elapsed(), st.code())
326 };
327 let (held, code) = timed(|c| std::process::exit(c));
328 assert!(held >= std::time::Duration::from_millis(2500), "the control: exit(3) in the forked child ran the stalling handler (held {held:?}, code {code:?})");
329 let (held, code) = timed(leave_child);
330 assert_eq!(code, Some(97), "leave_child exits with its code");
331 assert!(held < std::time::Duration::from_millis(2500), "leave_child ran an atexit handler: held {held:?}");
332 }
333
334 #[test]
335 fn the_exit_codes_round_trip_and_do_not_collide_with_ordinary_ones() {
336 for err in [
337 SandboxError::UserNamespace,
338 SandboxError::SetGroups,
339 SandboxError::UidMap,
340 SandboxError::GidMap,
341 SandboxError::NetworkNamespace,
342 ] {
343 assert_eq!(error_from_exit(exit_code(err)), Some(err));
344 }
345 // A build that exits 1 is a failed build, not a failed sandbox. This is
346 // the confusion the reserved range exists to prevent.
347 for ordinary in [0, 1, 2, 101, 119] {
348 assert_eq!(error_from_exit(ordinary), None, "code {ordinary}");
349 }
350 }
351
352 #[test]
353 fn the_decimal_renderer_agrees_with_the_formatter() {
354 for value in [0u32, 1, 9, 10, 99, 1000, 65534, u32::MAX] {
355 let mut buf = [0u8; 12];
356 assert_eq!(
357 std::str::from_utf8(render(value, &mut buf)).unwrap(),
358 value.to_string(),
359 "rendering {value}"
360 );
361 }
362 }
363}
364
365#[cfg(test)]
366mod live_tests {
367 use super::*;
368 use std::io::Read as _;
369 use std::os::unix::process::CommandExt as _;
370
371 /// Run `bash -c <script>`, optionally inside the sandbox, and report
372 /// whether it succeeded.
373 fn run(script: &str, sandboxed: bool) -> Option<bool> {
374 let mut command = std::process::Command::new("bash");
375 command.arg("-c").arg(script);
376 if sandboxed {
377 // SAFETY: `enter` allocates nothing and takes no lock.
378 unsafe {
379 command.pre_exec(|| {
380 if let Err(err) = enter() {
381 leave_child(exit_code(err));
382 }
383 Ok(())
384 });
385 }
386 }
387 let status = command
388 .stdin(std::process::Stdio::null())
389 .stdout(std::process::Stdio::null())
390 .stderr(std::process::Stdio::null())
391 .status()
392 .ok()?;
393 // A sandbox that could not be entered is not a verdict about the
394 // script, so it is `None` and the caller skips rather than asserting.
395 if status.code().and_then(error_from_exit).is_some() {
396 return None;
397 }
398 Some(status.success())
399 }
400
401 /// **The sandbox really cuts the network**, and the control says the test
402 /// could have noticed if it did not.
403 ///
404 /// A listener on loopback is the target: inside a fresh network namespace
405 /// there is one interface, `lo`, and it is **down**, so the connect fails.
406 /// Outside, it succeeds — which is the half that makes the first assertion
407 /// mean something rather than being satisfied by a broken script.
408 #[test]
409 fn a_build_in_the_sandbox_cannot_reach_a_listener_the_host_can() {
410 let listener = std::net::TcpListener::bind("127.0.0.1:0").expect("bind");
411 let port = listener.local_addr().expect("addr").port();
412 // Accept in the background so a successful connect completes rather
413 // than sitting in the backlog.
414 std::thread::spawn(move || {
415 for mut stream in listener.incoming().take(4).flatten() {
416 let mut sink = [0u8; 1];
417 let _ = stream.read(&mut sink);
418 }
419 });
420
421 let script = format!("exec 3<>/dev/tcp/127.0.0.1/{port}");
422 let Some(outside) = run(&script, false) else {
423 unreachable!("the unsandboxed arm cannot report a sandbox failure")
424 };
425 assert!(
426 outside,
427 "the control failed: bash could not reach a listener on loopback without any \
428 sandbox, so this test cannot tell a cut network from a broken script"
429 );
430
431 match run(&script, true) {
432 None => eprintln!(
433 "skipped: this kernel does not allow unprivileged user namespaces, so the \
434 sandbox cannot be exercised here. `gunnar runner` refuses to build rather \
435 than building without one."
436 ),
437 Some(inside) => assert!(
438 !inside,
439 "a process inside the build sandbox reached a listener on the host's loopback. \
440 The network namespace is not being entered, and every `network: false` in a \
441 build record this server signs is a lie."
442 ),
443 }
444 }
445
446 /// The sandbox does not break the ordinary case: a build that touches only
447 /// the filesystem still runs, and still runs **as the same user**.
448 #[test]
449 fn a_build_in_the_sandbox_still_owns_the_files_it_writes() {
450 let dir = tempfile::tempdir().expect("tempdir");
451 let path = dir.path().join("written");
452 let script = format!("echo hello > {}", path.display());
453 match run(&script, true) {
454 None => eprintln!("skipped: no unprivileged user namespaces on this kernel"),
455 Some(ok) => {
456 assert!(
457 ok,
458 "a build that only writes a file failed inside the sandbox"
459 );
460 assert_eq!(
461 std::fs::read_to_string(&path).expect("read it back"),
462 "hello\n",
463 "the sandboxed build could not write to the directory it was given. The uid \
464 map is wrong: inside the namespace it must be root and outside it must \
465 still be the same unprivileged user."
466 );
467 }
468 }
469 }
470}