From 4e06be4bb7cb4900f0b6f47137d60e0d5b5a0ae7 Mon Sep 17 00:00:00 2001 From: M3gA-Mind Date: Wed, 11 Nov 2026 18:29:21 +0530 Subject: [PATCH 1/3] Give allow_net bwrap jails a private network through pasta A jail that allows network shared the host's network namespace, so host loopback services and abstract sockets were reachable from it and the jail stayed Process, which native_jail refuses when process isolation is required. Every registry-installed stdio MCP server was refused on Linux for that reason. When a root-owned pasta and nsenter are found and pass a probe, pasta now starts a holder that owns a fresh user and network namespace, and the launcher enters it with nsenter by namespace descriptor before it runs bwrap. pasta closes every inherited descriptor, so it cannot be bwrap's parent; the holder keeps the argument file, grant and seccomp descriptors intact. pasta runs only with no port forwarding, no gateway mapping and DNS through one forwarded address, and the jail gets its own resolv.conf. The holder lives exactly as long as bwrap through --sync-fd, so no pasta outlives a jail, even a killed one. Such a jail reports Kernel. Without pasta nothing changes: the host network is shared and the jail stays Process. slirp4netns is not used: it only attaches to an existing namespace, which Ubuntu's AppArmor user-namespace restriction refuses. --- .github/workflows/ci.yml | 21 ++ crates/tinybox-jail/src/bwrap/args.rs | 12 +- crates/tinybox-jail/src/bwrap/args_tests.rs | 3 + crates/tinybox-jail/src/bwrap/launcher.rs | 27 +- .../tinybox-jail/src/bwrap/launcher_tests.rs | 64 +++- crates/tinybox-jail/src/bwrap/mod.rs | 135 +++++++- crates/tinybox-jail/src/bwrap/mod_tests.rs | 78 ++++- crates/tinybox-jail/src/bwrap/pasta.rs | 295 ++++++++++++++++++ crates/tinybox-jail/src/bwrap/pasta_tests.rs | 141 +++++++++ crates/tinybox-jail/tests/bwrap_isolation.rs | 20 +- crates/tinybox-jail/tests/bwrap_pasta_live.rs | 185 +++++++++++ 11 files changed, 953 insertions(+), 28 deletions(-) create mode 100644 crates/tinybox-jail/src/bwrap/pasta.rs create mode 100644 crates/tinybox-jail/src/bwrap/pasta_tests.rs create mode 100644 crates/tinybox-jail/tests/bwrap_pasta_live.rs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 353c2aa5..0ac01a76 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -44,6 +44,27 @@ jobs: export DBUS_SESSION_BUS_ADDRESS="unix:path=$runtime_dir/bus" TINYBOX_REQUIRE_NATIVE_RESOURCE_TESTS=1 cargo test -j1 -p tinybox-module native_ TINYBOX_REQUIRE_NATIVE_RESOURCE_TESTS=1 cargo test -j1 -p tinybox-module resource_aware_bus_exec_cancellation_reports_events_and_reaps_descendants + bwrap-pasta-live: + name: Bubblewrap private network (pasta) reachability + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 + with: + submodules: recursive + persist-credentials: false + - uses: dtolnay/rust-toolchain@686976e191b89faba57d3206551f0f330d8cb249 + - name: Install bubblewrap, passt (pasta) and util-linux (nsenter) + run: sudo apt-get install -y --no-install-recommends bubblewrap passt util-linux + - name: Prove outbound works and host loopback, gateway and abstract sockets do not + env: + TINYBOX_REQUIRE_BWRAP: "1" + TINYBOX_REQUIRE_PASTA: "1" + run: | + set -euo pipefail + pasta --version | head -1 + cargo test --locked -p tinybox-jail --test bwrap_pasta_live -- --nocapture + cargo test --locked -p tinybox-jail --test bwrap_isolation -- --nocapture + cargo test --locked -p tinybox-jail --test bwrap_fd_leak -- --nocapture docker-network-live: name: Docker Egress and denied network connectivity runs-on: ubuntu-22.04 diff --git a/crates/tinybox-jail/src/bwrap/args.rs b/crates/tinybox-jail/src/bwrap/args.rs index b1557825..518286c3 100644 --- a/crates/tinybox-jail/src/bwrap/args.rs +++ b/crates/tinybox-jail/src/bwrap/args.rs @@ -54,6 +54,9 @@ pub(super) struct Options { /// Bind the jail's grants from descriptors (`--bind-fd`/`--ro-bind-fd`) /// opened here, instead of by path (see [`jail_args_with_fds`]). pub bind_fds: bool, + /// Leave the host's `/etc/resolv.conf` out of the baseline: the jail runs + /// in a private network whose resolver is supplied separately. + pub private_resolv: bool, } /// The first descriptor a grant is handed to `bwrap` on; the launcher keeps @@ -135,8 +138,8 @@ fn replaces_private_mount(path: &Path) -> bool { } /// The baseline mounts: system directories (or their symlinks) and the short -/// `/etc` allow-list. -fn baseline_mounts() -> Vec { +/// `/etc` allow-list, without `/etc/resolv.conf` when `private_resolv`. +fn baseline_mounts(private_resolv: bool) -> Vec { let mut mounts: Vec = Vec::new(); for dir in SYSTEM_DIRS { let path = PathBuf::from(dir); @@ -154,6 +157,9 @@ fn baseline_mounts() -> Vec { }); } for entry in ETC_ENTRIES { + if private_resolv && *entry == "/etc/resolv.conf" { + continue; + } mounts.push(Mount { path: PathBuf::from(entry), kind: Kind::Bind { writable: false }, @@ -217,7 +223,7 @@ pub(super) fn jail_args_with_fds( options: Options, ) -> io::Result { check_paths(jail, cwd)?; - let mut mounts = baseline_mounts(); + let mut mounts = baseline_mounts(options.private_resolv); for (path, writable) in std::iter::once((&jail.root, true)) .chain(jail.read_write.iter().map(|p| (p, true))) .chain(jail.read_only.iter().map(|p| (p, false))) diff --git a/crates/tinybox-jail/src/bwrap/args_tests.rs b/crates/tinybox-jail/src/bwrap/args_tests.rs index 52f832ce..68ba022d 100644 --- a/crates/tinybox-jail/src/bwrap/args_tests.rs +++ b/crates/tinybox-jail/src/bwrap/args_tests.rs @@ -5,6 +5,7 @@ use super::*; const OPTS: Options = Options { disable_userns: true, bind_fds: false, + private_resolv: false, }; fn build(jail: &Jail) -> io::Result> { @@ -57,6 +58,7 @@ fn disable_userns_is_omitted_when_the_probe_found_no_support() -> io::Result<()> Options { disable_userns: false, bind_fds: false, + private_resolv: false, }, )?; assert!(!argv.iter().any(|a| a == "--disable-userns")); @@ -354,6 +356,7 @@ fn a_denial_through_a_baseline_symlink_also_covers_the_link_target() -> io::Resu const FD_OPTS: Options = Options { disable_userns: true, bind_fds: true, + private_resolv: false, }; fn strings(argv: &[OsString]) -> Vec { diff --git a/crates/tinybox-jail/src/bwrap/launcher.rs b/crates/tinybox-jail/src/bwrap/launcher.rs index 5b359ded..8e2eda86 100644 --- a/crates/tinybox-jail/src/bwrap/launcher.rs +++ b/crates/tinybox-jail/src/bwrap/launcher.rs @@ -158,10 +158,23 @@ impl ArgsFile { } } +/// Namespaces the launcher joins before it runs: `nsenter` enters the user +/// and network namespace open on these child descriptors (by descriptor, not +/// by pid, so a reused pid cannot redirect it), and keeps every other +/// descriptor. The script then closes these two, as it closes everything +/// outside its keep list. +#[derive(Debug, Clone, Copy)] +pub(super) struct Enter<'a> { + pub nsenter: &'a Path, + pub user_fd: i32, + pub net_fd: i32, +} + /// The launcher command: an empty environment, the script, `bwrap`, the file, /// how many grant descriptors to keep, then the command to run inside the -/// sandbox and its arguments. +/// sandbox and its arguments. With `enter`, the shell runs under `nsenter`. pub(super) fn command( + enter: Option>, shell: &str, bwrap: &Path, args: &ArgsFile, @@ -169,7 +182,17 @@ pub(super) fn command( program: &std::ffi::OsStr, program_args: &[OsString], ) -> Command { - let mut launcher = Command::new(shell); + let mut launcher = match enter { + Some(enter) => { + let mut nsenter = Command::new(enter.nsenter); + nsenter + .arg(format!("--user=/proc/self/fd/{}", enter.user_fd)) + .arg(format!("--net=/proc/self/fd/{}", enter.net_fd)) + .args(["--preserve-credentials", "--", shell]); + nsenter + } + None => Command::new(shell), + }; launcher .env_clear() .arg("-c") diff --git a/crates/tinybox-jail/src/bwrap/launcher_tests.rs b/crates/tinybox-jail/src/bwrap/launcher_tests.rs index 3869d8bd..fab18e3b 100644 --- a/crates/tinybox-jail/src/bwrap/launcher_tests.rs +++ b/crates/tinybox-jail/src/bwrap/launcher_tests.rs @@ -26,7 +26,16 @@ fn run(shell: &str, body: &str, items: &[&str]) -> io::Result { let bwrap = stand_in(dir.path(), body)?; let argv: Vec = items.iter().map(OsString::from).collect(); let file = ArgsFile::create(&argv, &tmp())?; - let out = command(shell, &bwrap, &file, 0, "prog".as_ref(), &["a".into()]).output()?; + let out = command( + None, + shell, + &bwrap, + &file, + 0, + "prog".as_ref(), + &["a".into()], + ) + .output()?; file.discard(); Ok(String::from_utf8_lossy(&out.stdout).into_owned()) } @@ -81,7 +90,7 @@ fn the_args_file_is_gone_once_the_launcher_has_started_bwrap() -> io::Result<()> let file = ArgsFile::create(&["x".into()], &tmp())?; let path = file.path().to_path_buf(); assert!( - command(shell(), &bwrap, &file, 0, "prog".as_ref(), &[]) + command(None, shell(), &bwrap, &file, 0, "prog".as_ref(), &[]) .status()? .success() ); @@ -94,7 +103,7 @@ fn the_launcher_runs_with_an_empty_environment() -> io::Result<()> { let dir = tempfile::tempdir()?; let bwrap = stand_in(dir.path(), "env")?; let file = ArgsFile::create(&[], &tmp())?; - let out = command(shell(), &bwrap, &file, 0, "prog".as_ref(), &[]).output()?; + let out = command(None, shell(), &bwrap, &file, 0, "prog".as_ref(), &[]).output()?; file.discard(); let text = String::from_utf8_lossy(&out.stdout); assert!( @@ -137,7 +146,7 @@ fn a_launcher_that_cannot_open_the_args_file_fails_closed_without_running_bwrap( dir: dir.path().join("gone"), file: dir.path().join("gone").join("args"), }; - let status = command(shell(), &bwrap, &missing, 0, "prog".as_ref(), &[]).status()?; + let status = command(None, shell(), &bwrap, &missing, 0, "prog".as_ref(), &[]).status()?; assert_eq!(status.code(), Some(126)); assert!(!marker.exists(), "bwrap must not run without its arguments"); Ok(()) @@ -189,7 +198,7 @@ fn the_grant_descriptors_reach_bwrap_and_nothing_else_does() -> io::Result<()> { rustix::io::fcntl_setfd(&stray, rustix::io::FdFlags::empty())?; let bwrap = stand_in(dir.path(), "ls -l /proc/self/fd")?; let file = ArgsFile::create(&[], &tmp())?; - let mut launcher = command(shell, &bwrap, &file, 2, "prog".as_ref(), &[]); + let mut launcher = command(None, shell, &bwrap, &file, 2, "prog".as_ref(), &[]); let mappings = vec![ command_fds::FdMapping { parent_fd: std::fs::File::open(granted.path())?.into(), @@ -219,3 +228,48 @@ fn the_grant_descriptors_reach_bwrap_and_nothing_else_does() -> io::Result<()> { ); Ok(()) } + +#[test] +fn entering_namespaces_puts_nsenter_in_front_by_descriptor() -> io::Result<()> { + let file = ArgsFile::create(&[], &tmp())?; + let enter = Enter { + nsenter: Path::new("/usr/bin/nsenter"), + user_fd: 14, + net_fd: 15, + }; + let launcher = command( + Some(enter), + "/bin/bash", + Path::new("/usr/bin/bwrap"), + &file, + 4, + "prog".as_ref(), + &[], + ); + file.discard(); + assert_eq!(launcher.get_program(), "/usr/bin/nsenter"); + assert_eq!( + launcher.get_envs().count(), + 0, + "no environment reaches nsenter" + ); + let argv: Vec = launcher + .get_args() + .map(|a| a.to_string_lossy().into_owned()) + .collect(); + assert_eq!( + &argv[..6], + [ + "--user=/proc/self/fd/14", + "--net=/proc/self/fd/15", + "--preserve-credentials", + "--", + "/bin/bash", + "-c", + ] + ); + // The launcher keeps only the 4 descriptors from 10, so the namespace + // descriptors on 14 and 15 are closed before bwrap runs. + assert_eq!(argv.get(9).map(String::as_str), Some("4")); + Ok(()) +} diff --git a/crates/tinybox-jail/src/bwrap/mod.rs b/crates/tinybox-jail/src/bwrap/mod.rs index 5e9fc1ec..f2ba11f8 100644 --- a/crates/tinybox-jail/src/bwrap/mod.rs +++ b/crates/tinybox-jail/src/bwrap/mod.rs @@ -1,8 +1,10 @@ //! Linux bubblewrap backend: namespace isolation for untrusted code. //! //! [`BubblewrapBackend`] runs the command under `bwrap` with fresh user, pid, -//! ipc and uts namespaces (and a net namespace unless the jail allows -//! network). The child sees a private mount tree holding only the baseline +//! ipc and uts namespaces, and a network namespace of its own: an empty one +//! when the jail denies network, or one with user-mode networking from `pasta` +//! when it allows network and `pasta` works here (see "Private network"). +//! The child sees a private mount tree holding only the baseline //! system paths plus the jail's grants, so it cannot read an ungranted path, //! cannot see host processes, cannot create nested user namespaces //! (`--disable-userns`), and dies with its parent. @@ -37,17 +39,34 @@ //! backend accepts**. It is reported as `Process`, the closest defined level //! below `Kernel`, but note what it means here: pid, mount, user and ipc //! namespaces, a private filesystem view and the seccomp deny list, with the -//! host *network* shared. That is more than the plain `Process` doc describes and less than +//! host *network* shared (an `allow_net` jail where `pasta` is unavailable). That is more than the plain `Process` doc describes and less than //! `Kernel`; do not read it as "same process table". `Process` with `--disable-userns` support (bubblewrap //! 0.8+), `None` without it. [`JailBackend::isolation_for`] reaches -//! [`IsolationLevel::Kernel`] only for a jail that denies network (a new net -//! namespace). With `allow_net` the host network namespace is shared, so host -//! loopback services and abstract sockets are reachable. Hosts must ask +//! [`IsolationLevel::Kernel`] for a jail that denies network (an empty net +//! namespace), and for one that allows it when +//! [`BubblewrapBackend::networks_privately`] (a `pasta` network). Otherwise an +//! `allow_net` jail shares the host network namespace, so host loopback +//! services and abstract sockets are reachable, and it stays `Process`. Hosts must ask //! [`JailBackend::is_suitable_for_untrusted_jail`]; the per-backend //! `is_suitable_for_untrusted_code` is false by construction. `Kernel` here //! means separate pid, mount, user, ipc and uts (and net) namespaces plus that //! deny list: it does not imply resource limits. //! +//! # Private network +//! +//! When the jail allows network and a trusted `pasta` and `nsenter` are found +//! (at fixed root-owned paths, never on `PATH`) and pass a probe, `pasta` +//! starts a holder that owns a fresh user and network namespace, and the +//! launcher enters it with `nsenter` before running `bwrap` (see `pasta.rs`). +//! `pasta` is started only with no port forwarding, no gateway mapping and DNS +//! through one forwarded address, and the jail gets its own +//! `/etc/resolv.conf` naming it. So outbound connections work, while host +//! loopback services, the host behind the gateway address and the host's +//! abstract sockets are unreachable. The holder lives exactly as long as +//! `bwrap` (`--sync-fd`), so no `pasta` outlives a jail, even one that is +//! killed. `slirp4netns` is not used: it only attaches to an existing +//! namespace, which Ubuntu's `AppArmor` user-namespace restriction refuses. +//! //! # Seccomp //! //! Every jail starts under a seccomp deny list (`seccomp.rs`): kernel keyrings, @@ -106,6 +125,7 @@ use crate::jail::{Jail, JailBackend}; mod args; mod launcher; +mod pasta; mod seccomp; /// Backend name reported by [`BubblewrapBackend`]. @@ -127,6 +147,9 @@ struct Verified { bind_fds: bool, /// The compiled seccomp filters every jail starts under (see `seccomp`). seccomp: Vec>, + /// A working `pasta` and `nsenter`: an `allow_net` jail then gets a + /// private network instead of the host's (see `pasta`). + pasta: Option, } /// Bubblewrap (`bwrap`) namespace jail backend. @@ -191,6 +214,15 @@ impl BubblewrapBackend { self.verified.as_ref().is_some_and(|v| v.bind_fds) } + /// Whether a jail that allows network runs in a private network namespace + /// with user-mode networking from `pasta`, rather than sharing the host's. + /// Then host loopback services and abstract sockets are out of its reach + /// and [`JailBackend::isolation_for`] reports `Kernel` for it too. + #[must_use] + pub fn networks_privately(&self) -> bool { + self.verified.as_ref().is_some_and(|v| v.pasta.is_some()) + } + /// Whether jails start under the seccomp deny list. True whenever the /// backend is available: a `bwrap` that cannot load it is not used. #[must_use] @@ -209,6 +241,7 @@ impl BubblewrapBackend { disable_userns, bind_fds: false, seccomp: Vec::new(), + pasta: None, }), } } @@ -243,12 +276,24 @@ fn verify(program: &Path) -> Option { disable_userns, bind_fds: supports_fd_binds(program), seccomp, + pasta: private_network(program), }); } } None } +/// A `pasta` that really brings up a private network, when there is one and +/// `program` can hold it open (`--sync-fd`) and mount the jail's resolver +/// (`--ro-bind-data`). Without one, an `allow_net` jail shares the host network. +fn private_network(program: &Path) -> Option { + let found = pasta::Pasta::find(is_trusted_binary)?; + let flags = help_lists(program, "--sync-fd") && help_lists(program, "--ro-bind-data"); + let works = flags && found.start().is_ok(); + log::debug!("[cwd_jail:bwrap] pasta private network flags={flags} works={works}"); + works.then_some(found) +} + /// The filters for this kernel and architecture, if `program` can load them. fn seccomp_filters(program: &Path) -> io::Result>> { if !help_lists(program, "--add-seccomp-fd") { @@ -369,7 +414,7 @@ fn run_probe(program: &Path, disable_userns: bool, root: &Path, host_only: &Path let shell = launcher::pick_shell(|path| Path::new(path).is_file()); // Positive evidence is required: a launcher that exits 0 without running // the check (`true`) must not pass. - let out = launcher::command(shell, program, &file, 0, "/bin/sh".as_ref(), &check) + let out = launcher::command(None, shell, program, &file, 0, "/bin/sh".as_ref(), &check) .stdin(Stdio::null()) .stderr(Stdio::null()) .output(); @@ -395,13 +440,19 @@ struct Prepared { /// /// The launcher command carries no environment and its own argv holds only /// paths; the caller's variables and arguments live in the private args file. -fn prepare_command(verified: &Verified, jail: &Jail, cmd: &Command) -> io::Result { +fn prepare_command( + verified: &Verified, + jail: &Jail, + cmd: &Command, + network: Option<&pasta::Network>, +) -> io::Result { let args::JailArgs { mut argv, mut fds } = args::jail_args_with_fds( jail, cmd.get_current_dir(), args::Options { disable_userns: verified.disable_userns, bind_fds: verified.bind_fds, + private_resolv: network.is_some(), }, )?; // Each filter on its own pipe, numbered after the grants. A filter is a @@ -415,6 +466,10 @@ fn prepare_command(verified: &Verified, jail: &Jail, cmd: &Command) -> io::Resul argv.extend(["--add-seccomp-fd".into(), number.to_string().into()]); fds.push((reader.into(), number)); } + let namespaces = match network { + Some(network) => Some(join_network(network, &mut argv, &mut fds)?), + None => None, + }; argv.push("--clearenv".into()); for (key, value) in cmd.get_envs() { if let Some(value) = value { @@ -431,11 +486,28 @@ fn prepare_command(verified: &Verified, jail: &Jail, cmd: &Command) -> io::Resul let file = launcher::ArgsFile::create(&argv, &base)?; let shell = launcher::pick_shell(|path| Path::new(path).is_file()); let program_args: Vec = cmd.get_args().map(OsString::from).collect(); + // The namespace descriptors are numbered after the kept ones, so the + // launcher closes them once `nsenter` has entered. + let keep = fds.len(); + let enter = match (namespaces, verified.pasta.as_ref()) { + (Some((user, net)), Some(pasta)) => { + let user_fd = args::FIRST_GRANT_FD + i32::try_from(keep).map_err(io::Error::other)?; + fds.push((user, user_fd)); + fds.push((net, user_fd + 1)); + Some(launcher::Enter { + nsenter: pasta.nsenter(), + user_fd, + net_fd: user_fd + 1, + }) + } + _ => None, + }; let mut launcher = launcher::command( + enter, shell, &verified.program, &file, - fds.len(), + keep, cmd.get_program(), &program_args, ); @@ -456,6 +528,30 @@ fn prepare_command(verified: &Verified, jail: &Jail, cmd: &Command) -> io::Resul }) } +/// Wires a pasta network into the `bwrap` arguments: the jail's resolver on a +/// pipe (`--ro-bind-data`) and the holder's writer kept by `bwrap` +/// (`--sync-fd`), both numbered after the descriptors already in `fds`. +/// Returns the user and network namespace descriptors for `nsenter`. +fn join_network( + network: &pasta::Network, + argv: &mut Vec, + fds: &mut Vec<(std::os::fd::OwnedFd, i32)>, +) -> io::Result<(std::os::fd::OwnedFd, std::os::fd::OwnedFd)> { + let (user, net, writer) = network.descriptors()?; + let (reader, mut resolv) = std::io::pipe()?; + io::Write::write_all(&mut resolv, pasta::resolv_conf().as_bytes())?; + drop(resolv); + for (fd, flag) in [(reader.into(), "--ro-bind-data"), (writer, "--sync-fd")] { + let number = args::FIRST_GRANT_FD + i32::try_from(fds.len()).map_err(io::Error::other)?; + argv.extend([flag.into(), number.to_string().into()]); + if flag == "--ro-bind-data" { + argv.push("/etc/resolv.conf".into()); + } + fds.push((fd, number)); + } + Ok((user, net)) +} + impl JailBackend for BubblewrapBackend { fn carves_denials(&self) -> bool { true @@ -491,7 +587,9 @@ impl JailBackend for BubblewrapBackend { fn isolation_for(&self, jail: &Jail) -> IsolationLevel { match self.isolation() { - IsolationLevel::Process if !jail.allow_net => IsolationLevel::Kernel, + IsolationLevel::Process if !jail.allow_net || self.networks_privately() => { + IsolationLevel::Kernel + } level => level, } } @@ -519,10 +617,16 @@ impl BubblewrapBackend { .require(self.name(), &jail.constraints()) .map_err(|error| io::Error::new(io::ErrorKind::Unsupported, error))?; log::debug!("[cwd_jail:bwrap] spawning label={}", jail.label); + // An `allow_net` jail gets a private network when `pasta` works; + // dropping `network` on any error below kills `pasta` again. + let network = match &verified.pasta { + Some(pasta) if jail.allow_net => Some(pasta.start()?), + _ => None, + }; let Prepared { launcher: mut wrapper, args, - } = prepare_command(verified, jail, cmd)?; + } = prepare_command(verified, jail, cmd, network.as_ref())?; if captured { wrapper .stdin(Stdio::piped()) @@ -532,7 +636,14 @@ impl BubblewrapBackend { // can signal the whole sandbox. .process_group(0); } - wrapper.spawn().inspect_err(|_| args.discard()) + let child = wrapper.spawn().inspect_err(|_| args.discard())?; + // `bwrap` now holds the holder's writer; drop ours so the network ends + // when the jail does. + drop(wrapper); + if let Some(network) = network { + network.release(); + } + Ok(child) } } diff --git a/crates/tinybox-jail/src/bwrap/mod_tests.rs b/crates/tinybox-jail/src/bwrap/mod_tests.rs index 722b14ed..2546dedb 100644 --- a/crates/tinybox-jail/src/bwrap/mod_tests.rs +++ b/crates/tinybox-jail/src/bwrap/mod_tests.rs @@ -17,6 +17,7 @@ fn verified(program: &str) -> Verified { disable_userns: true, bind_fds: false, seccomp: Vec::new(), + pasta: None, } } @@ -83,7 +84,7 @@ fn secrets_stay_out_of_argv_and_the_launcher_environment() -> io::Result<()> { .env("A", "1") .env_remove("B"); let jail = Jail::new(root.path(), "t"); - let prepared = prepare_command(&verified("/usr/bin/bwrap"), &jail, &cmd)?; + let prepared = prepare_command(&verified("/usr/bin/bwrap"), &jail, &cmd, None)?; // The host-side process: empty environment, argv of paths only. assert_eq!(prepared.launcher.get_envs().count(), 0); let argv = argv_of(&prepared.launcher); @@ -228,3 +229,78 @@ fn the_default_constructor_probes_only_trusted_paths() { backend.is_available() ); } + +#[test] +fn a_private_network_jail_enters_the_holder_and_brings_its_own_resolver() -> io::Result<()> { + let root = tempfile::tempdir()?; + let jail = Jail::new(root.path(), "t"); + let mut backend_state = verified("/usr/bin/bwrap"); + backend_state.pasta = pasta::Pasta::find(|_| true); + let (_reader, writer) = std::io::pipe()?; + let network = pasta::Network::fake( + std::fs::File::open("/proc/self/ns/user")?, + std::fs::File::open("/proc/self/ns/net")?, + writer.into(), + ); + let prepared = prepare_command(&backend_state, &jail, &Command::new("true"), Some(&network))?; + assert_eq!(prepared.launcher.get_program(), "/usr/bin/nsenter"); + let argv = argv_of(&prepared.launcher); + assert!(argv.iter().any(|a| a == "--preserve-credentials")); + let content = std::fs::read(prepared.args.path())?; + let fields: Vec<&[u8]> = content.split(|b| *b == 0).collect(); + // The resolver arrives by descriptor and the holder's writer is kept by + // bwrap; the host's resolv.conf is not bound, and the net stays shared + // with the holder (no --unshare-net). + assert!( + fields + .windows(3) + .any(|w| w[0] == b"--ro-bind-data" && w[2] == b"/etc/resolv.conf") + ); + assert!(fields.iter().any(|f| *f == b"--sync-fd")); + assert!( + !fields + .windows(2) + .any(|w| w[0] == b"--ro-bind-try" && w[1] == b"/etc/resolv.conf") + ); + assert!(!fields.iter().any(|f| *f == b"--unshare-net")); + prepared.args.discard(); + network.release(); + Ok(()) +} + +#[test] +fn only_a_working_pasta_makes_an_allow_net_jail_kernel_isolated() { + let root = std::env::temp_dir(); + let open = Jail::new(&root, "t"); + let without = BubblewrapBackend { + verified: Some(verified("/usr/bin/bwrap")), + }; + assert!(!without.networks_privately()); + assert_eq!(without.isolation_for(&open), IsolationLevel::Process); + let mut with_pasta = verified("/usr/bin/bwrap"); + with_pasta.pasta = pasta::Pasta::find(|_| true); + let with = BubblewrapBackend { + verified: Some(with_pasta), + }; + assert!(with.networks_privately()); + assert_eq!(with.isolation_for(&open), IsolationLevel::Kernel); + // The backend-wide minimum is unchanged: it never promises Kernel. + assert_eq!(with.isolation(), IsolationLevel::Process); +} + +#[test] +fn a_pasta_that_fails_to_start_refuses_the_jail_instead_of_sharing_the_host_network() { + let root = std::env::temp_dir(); + let mut state = verified(&fake_bwrap().to_string_lossy()); + state.pasta = Some(pasta::Pasta::at("/nonexistent/pasta", "/usr/bin/nsenter")); + let backend = BubblewrapBackend { + verified: Some(state), + }; + // The claim is made for a backend whose probe found a working pasta, so + // a launch where pasta then fails must not run the jail at all. + let error = backend + .spawn(&Jail::new(&root, "t"), Command::new("true")) + .err() + .map(|e| e.kind()); + assert_eq!(error, Some(io::ErrorKind::NotFound)); +} diff --git a/crates/tinybox-jail/src/bwrap/pasta.rs b/crates/tinybox-jail/src/bwrap/pasta.rs new file mode 100644 index 00000000..57ed6db3 --- /dev/null +++ b/crates/tinybox-jail/src/bwrap/pasta.rs @@ -0,0 +1,295 @@ +//! A private network for an `allow_net` jail, from `pasta` (passt). +//! +//! Without this, a jail that allows network shares the host's network +//! namespace, so host loopback services and abstract unix sockets are +//! reachable from it. Here `pasta` creates a fresh user and network namespace +//! with user-mode networking out to the internet, and the jail is entered into +//! it. +//! +//! # Why a holder, not `pasta -- bwrap` +//! +//! `pasta` closes every inherited descriptor above 2 when it starts, and the +//! `bwrap` launch depends on inherited descriptors (the argument file, the +//! grant descriptors and the seccomp filters). So `pasta` runs only a holder +//! (`sh -c 'read _'`) that owns the namespaces; the launcher enters them with +//! `nsenter --user --net` by namespace descriptor, which keeps every other +//! descriptor, and then execs `bwrap`. Attaching `pasta` to namespaces `bwrap` +//! created is not an option on Ubuntu: its `AppArmor` user-namespace restriction +//! refuses the `setns`. +//! +//! # What the jail can reach +//! +//! `pasta` is only ever started with [`PASTA_ARGS`]: no port forwarding in +//! either direction (`-T none -U none`, so the jail cannot reach a service +//! bound to the host's loopback), no gateway mapping to the host +//! (`--no-map-gw`), and DNS only through [`DNS_ADDR`], which `pasta` forwards +//! to the host's resolver. The jail gets its own `/etc/resolv.conf` naming +//! that address. Abstract unix sockets belong to the network namespace, so the +//! host's are out of reach too. +//! +//! # Lifetime +//! +//! The holder reads a pipe whose only writer, once the jail has started, is +//! `bwrap` itself (`--sync-fd`). When `bwrap` exits, however it exits, the +//! holder sees end-of-file and exits, and `pasta` exits with it. Before the +//! hand-over the writer is held here, and [`Network`]'s `Drop` kills `pasta`. + +use std::fs::File; +use std::io; +use std::os::fd::OwnedFd; +use std::os::unix::fs::MetadataExt as _; +use std::path::{Path, PathBuf}; +use std::process::{Child, Command, Stdio}; +use std::time::{Duration, Instant}; + +/// Where a trusted `pasta` may live. +const TRUSTED_PASTA: &[&str] = &["/usr/bin/pasta", "/usr/local/bin/pasta", "/bin/pasta"]; + +/// Where a trusted `nsenter` may live. +const TRUSTED_NSENTER: &[&str] = &["/usr/bin/nsenter", "/bin/nsenter", "/usr/local/bin/nsenter"]; + +/// The only way `pasta` is started: no port forwarding, no gateway mapping to +/// the host, DNS forwarded through [`DNS_ADDR`] only. +pub(super) const PASTA_ARGS: &[&str] = &[ + "--config-net", + "--quiet", + "--no-map-gw", + "-T", + "none", + "-U", + "none", + "--dns-forward", + DNS_ADDR, +]; + +/// The jail's resolver: `pasta` forwards DNS sent here to the host's. +pub(super) const DNS_ADDR: &str = "169.254.1.1"; + +/// How long the jail's network may take to come up. +const READY_TIMEOUT: Duration = Duration::from_secs(5); + +/// A `pasta` and an `nsenter` found at trusted paths. +#[derive(Debug, Clone, PartialEq, Eq)] +pub(super) struct Pasta { + program: PathBuf, + nsenter: PathBuf, +} + +/// The jail's `/etc/resolv.conf`. +pub(super) fn resolv_conf() -> String { + format!("nameserver {DNS_ADDR}\n") +} + +impl Pasta { + /// `pasta` and `nsenter` at trusted paths, when both are there. + pub(super) fn find(trusted: impl Fn(&Path) -> bool) -> Option { + let first = |paths: &[&str]| paths.iter().map(PathBuf::from).find(|path| trusted(path)); + Some(Self { + program: first(TRUSTED_PASTA)?, + nsenter: first(TRUSTED_NSENTER)?, + }) + } + + /// The `nsenter` the launcher enters the namespaces with. + pub(super) fn nsenter(&self) -> &Path { + &self.nsenter + } + + /// Starts `pasta` with a holder and waits until its network is up. + /// + /// # Errors + /// The spawn error; `InvalidData` when the holder cannot be identified as + /// `pasta`'s child in a network namespace of its own; `TimedOut` when the + /// network has no default route within [`READY_TIMEOUT`]. + pub(super) fn start(&self) -> io::Result { + let (reader, writer) = io::pipe()?; + let child = Command::new(&self.program) + .args(PASTA_ARGS) + .args(["--", "/bin/sh", "-c", "read _"]) + .env_clear() + .stdin(Stdio::from(OwnedFd::from(reader))) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .spawn()?; + let mut network = Network { + pasta: Some(child), + user: None, + net: None, + writer: Some(writer.into()), + }; + let pasta = network.pasta_pid()?; + let deadline = Instant::now() + READY_TIMEOUT; + let holder = wait_for(deadline, || holder_of(pasta))?; + let (user, net) = open_namespaces(pasta, holder)?; + network.user = Some(user); + network.net = Some(net); + wait_for(deadline, || has_default_route(holder).then_some(()))?; + Ok(network) + } +} + +/// A running `pasta` and the namespaces of its holder. +#[derive(Debug)] +pub(super) struct Network { + pasta: Option, + user: Option, + net: Option, + /// The holder's only writer until it is handed to `bwrap`. + writer: Option, +} + +impl Network { + fn pasta_pid(&self) -> io::Result { + self.pasta + .as_ref() + .map(Child::id) + .ok_or_else(|| io::Error::other("pasta already released")) + } + + /// The descriptors the launcher needs: the holder's user and network + /// namespaces, then the writer `bwrap` keeps with `--sync-fd`. Each is a + /// duplicate; the originals stay here until [`Network::release`]. + /// + /// # Errors + /// The error from duplicating a descriptor. + pub(super) fn descriptors(&self) -> io::Result<(OwnedFd, OwnedFd, OwnedFd)> { + let missing = || io::Error::other("pasta network not started"); + let user = self.user.as_ref().ok_or_else(missing)?.try_clone()?; + let net = self.net.as_ref().ok_or_else(missing)?.try_clone()?; + let writer = self.writer.as_ref().ok_or_else(missing)?.try_clone()?; + Ok((user.into(), net.into(), writer)) + } + + /// After the jail has started: drop this side's copies, so `bwrap` holds + /// the holder's only writer, and reap `pasta` in the background when it + /// exits. + pub(super) fn release(mut self) { + self.writer = None; + self.user = None; + self.net = None; + if let Some(mut pasta) = self.pasta.take() { + std::thread::spawn(move || { + let _ = pasta.wait(); + }); + } + } +} + +#[cfg(test)] +impl Pasta { + /// A `pasta` and `nsenter` at the given paths, without the trust check. + pub(super) fn at(program: impl Into, nsenter: impl Into) -> Self { + Self { + program: program.into(), + nsenter: nsenter.into(), + } + } +} + +#[cfg(test)] +impl Network { + /// A network over the given descriptors with no `pasta` behind it, for + /// tests of how a launch is wired. + pub(super) fn fake(user: File, net: File, writer: OwnedFd) -> Self { + Self { + pasta: None, + user: Some(user), + net: Some(net), + writer: Some(writer), + } + } +} + +impl Drop for Network { + fn drop(&mut self) { + if let Some(mut pasta) = self.pasta.take() { + let _ = pasta.kill(); + let _ = pasta.wait(); + } + } +} + +/// Polls `check` until it returns a value or `deadline` passes. +fn wait_for(deadline: Instant, mut check: impl FnMut() -> Option) -> io::Result { + loop { + if let Some(value) = check() { + return Ok(value); + } + if Instant::now() >= deadline { + return Err(io::Error::new( + io::ErrorKind::TimedOut, + format!( + "pasta did not bring up the jail's network within {} s", + READY_TIMEOUT.as_secs() + ), + )); + } + std::thread::sleep(Duration::from_millis(10)); + } +} + +/// The first child of `pasta`, read from `/proc`. +fn holder_of(pasta: u32) -> Option { + let children = std::fs::read_to_string(format!("/proc/{pasta}/task/{pasta}/children")).ok()?; + first_pid(&children) +} + +/// The first pid in a `/proc//task//children` listing. +pub(super) fn first_pid(children: &str) -> Option { + children.split_whitespace().next()?.parse().ok() +} + +/// The parent pid from a `/proc//status` file. +pub(super) fn parent_pid(status: &str) -> Option { + status + .lines() + .find_map(|line| line.strip_prefix("PPid:")) + .and_then(|value| value.trim().parse().ok()) +} + +/// Whether a `/proc//net/route` table has a default route. +pub(super) fn default_route(table: &str) -> bool { + table + .lines() + .skip(1) + .any(|line| line.split_whitespace().nth(1) == Some("00000000")) +} + +fn has_default_route(holder: u32) -> bool { + std::fs::read_to_string(format!("/proc/{holder}/net/route")).is_ok_and(|t| default_route(&t)) +} + +fn is_child_of(pasta: u32, holder: u32) -> bool { + std::fs::read_to_string(format!("/proc/{holder}/status")) + .is_ok_and(|status| parent_pid(&status) == Some(pasta)) +} + +/// Opens the holder's user and network namespaces, checking before and after +/// that the pid still belongs to `pasta`'s child, and that the network +/// namespace is not this process's own. +fn open_namespaces(pasta: u32, holder: u32) -> io::Result<(File, File)> { + let wrong = |what: &str| { + io::Error::new( + io::ErrorKind::InvalidData, + format!("pasta holder {holder}: {what}"), + ) + }; + if !is_child_of(pasta, holder) { + return Err(wrong("not pasta's child")); + } + let user = File::open(format!("/proc/{holder}/ns/user"))?; + let net = File::open(format!("/proc/{holder}/ns/net"))?; + if !is_child_of(pasta, holder) { + return Err(wrong("exited while its namespaces were opened")); + } + let own = std::fs::metadata("/proc/self/ns/net")?; + let held = net.metadata()?; + if (held.dev(), held.ino()) == (own.dev(), own.ino()) { + return Err(wrong("shares this process's network namespace")); + } + Ok((user, net)) +} + +#[cfg(test)] +#[path = "pasta_tests.rs"] +mod tests; diff --git a/crates/tinybox-jail/src/bwrap/pasta_tests.rs b/crates/tinybox-jail/src/bwrap/pasta_tests.rs new file mode 100644 index 00000000..2f5f4d2b --- /dev/null +++ b/crates/tinybox-jail/src/bwrap/pasta_tests.rs @@ -0,0 +1,141 @@ +//! Unit tests for the pasta private network: the closed-down flags, the +//! `/proc` parsing, and the failure paths that need no `pasta`. + +use super::*; + +#[test] +fn pasta_is_only_ever_started_with_forwarding_and_the_gateway_closed() { + let args = PASTA_ARGS.join(" "); + for required in [ + "--no-map-gw", + "-T none", + "-U none", + "--dns-forward 169.254.1.1", + ] { + assert!(args.contains(required), "missing {required} in {args}"); + } + // Nothing that would open a path back to the host. + for forbidden in [ + "-t", + "-u", + "--map-host-loopback", + "--map-guest-addr", + "auto", + ] { + assert!( + !PASTA_ARGS.contains(&forbidden), + "{forbidden} must never be passed: {args}" + ); + } +} + +#[test] +fn the_jail_resolver_is_the_forwarded_address_only() { + assert_eq!(resolv_conf(), "nameserver 169.254.1.1\n"); +} + +#[test] +fn pasta_and_nsenter_come_only_from_trusted_paths() { + let found = Pasta::find(|path| { + path == Path::new("/usr/bin/pasta") || path == Path::new("/usr/bin/nsenter") + }); + assert_eq!( + found, + Some(Pasta { + program: "/usr/bin/pasta".into(), + nsenter: "/usr/bin/nsenter".into(), + }) + ); + assert_eq!( + found.as_ref().map(Pasta::nsenter), + Some(Path::new("/usr/bin/nsenter")) + ); + // Either one missing means no private network. + assert!(Pasta::find(|path| path.ends_with("pasta")).is_none()); + assert!(Pasta::find(|path| path.ends_with("nsenter")).is_none()); +} + +#[test] +fn proc_listings_are_parsed() { + assert_eq!(first_pid("4182 4190 \n"), Some(4182)); + assert_eq!(first_pid(""), None); + assert_eq!(first_pid("x"), None); + assert_eq!( + parent_pid("Name:\tsh\nState:\tS\nPPid:\t4181\nTracerPid:\t0\n"), + Some(4181) + ); + assert_eq!(parent_pid("Name:\tsh\n"), None); +} + +#[test] +fn only_a_default_route_counts_as_the_network_being_up() { + let header = "Iface\tDestination\tGateway\tFlags\tRefCnt\tUse\tMetric\tMask\n"; + let link = "eth0\t0000010A\t00000000\t0001\t0\t0\t100\t00F0FFFF\n"; + let default = "eth0\t00000000\t0100010A\t0003\t0\t0\t100\t00000000\n"; + assert!(!default_route(header)); + assert!(!default_route(&format!("{header}{link}"))); + assert!(default_route(&format!("{header}{link}{default}"))); + // The header line never counts, whatever it says. + assert!(!default_route("x\t00000000\n")); +} + +#[test] +fn waiting_reports_a_timeout_with_a_clear_error() { + let error = wait_for(Instant::now(), || None::<()>).err(); + assert_eq!( + error.as_ref().map(io::Error::kind), + Some(io::ErrorKind::TimedOut) + ); + assert!( + error.is_some_and(|e| e + .to_string() + .contains("did not bring up the jail's network")), + "the error names what failed" + ); + assert_eq!(wait_for(Instant::now(), || Some(7)).ok(), Some(7)); +} + +#[test] +fn a_pid_that_is_not_pastas_child_is_refused() -> io::Result<()> { + // This process is not a child of pid 1's first child, nor of pid 1 here. + let me = std::process::id(); + let error = open_namespaces(1, me).err(); + assert_eq!(error.map(|e| e.kind()), Some(io::ErrorKind::InvalidData)); + // A real parent/child pair whose namespace is this process's own is + // refused too: the holder must have a network of its own. + let mut child = Command::new("sleep").arg("5").spawn()?; + let refused = open_namespaces(me, child.id()).err(); + child.kill()?; + child.wait()?; + assert_eq!(refused.map(|e| e.kind()), Some(io::ErrorKind::InvalidData)); + Ok(()) +} + +#[test] +fn a_fake_network_hands_out_duplicates_and_releases_without_a_process() -> io::Result<()> { + let (_reader, writer) = io::pipe()?; + let network = Network::fake( + File::open("/proc/self/ns/user")?, + File::open("/proc/self/ns/net")?, + writer.into(), + ); + assert!(network.pasta_pid().is_err()); + let (user, net, _writer) = network.descriptors()?; + let own = std::fs::metadata("/proc/self/ns/net")?; + let dup = File::from(net).metadata()?; + assert_eq!((dup.dev(), dup.ino()), (own.dev(), own.ino())); + drop(user); + network.release(); + Ok(()) +} + +#[test] +fn an_unstarted_network_has_no_descriptors() { + let network = Network { + pasta: None, + user: None, + net: None, + writer: None, + }; + assert!(network.descriptors().is_err()); +} diff --git a/crates/tinybox-jail/tests/bwrap_isolation.rs b/crates/tinybox-jail/tests/bwrap_isolation.rs index 550373fd..d557bc58 100644 --- a/crates/tinybox-jail/tests/bwrap_isolation.rs +++ b/crates/tinybox-jail/tests/bwrap_isolation.rs @@ -27,8 +27,13 @@ fn only_a_jail_with_a_private_network_is_suitable_for_untrusted_code() -> io::Re }; let start = Instant::now(); let root = tempfile::tempdir()?; - // tinymcp's default configuration shares the host network: not suitable. - assert!(!backend.is_suitable_for_untrusted_jail(&jail(root.path()))); + // tinymcp's default configuration allows network. It is suitable only + // when that network is private (pasta); a shared host network is not. + assert_eq!( + backend.is_suitable_for_untrusted_jail(&jail(root.path())), + backend.networks_privately() + && backend.isolation() >= tinybox_core::IsolationLevel::Process + ); // The per-backend answer is the worst case, so it is never true. assert!(!backend.is_suitable_for_untrusted_code()); // With a private network it is suitable, given `--disable-userns` support. @@ -365,12 +370,17 @@ fn network_is_unreachable_when_denied_and_reachable_when_shared() -> io::Result< let (connected, _) = started(&run(&backend, &jail(root.path()).deny_net(), cmd)?)?; assert!(!connected, "connect must fail under deny_net"); - // Control: with the host network shared the same connect succeeds, which - // is exactly the exposure `allow_net` carries. + // With network allowed: under a private (pasta) network the host's + // loopback listener stays unreachable; with the host network shared the + // same connect succeeds, which is exactly the exposure that carries. let mut cmd = Command::new("/usr/bin/env"); cmd.args(["bash", "-c", &connect]); let (connected, _) = started(&run(&backend, &jail(root.path()), cmd)?)?; - assert!(connected, "control connect should reach the host listener"); + assert_eq!( + connected, + !backend.networks_privately(), + "allow_net reaches host loopback only when the host network is shared" + ); done("network", start); Ok(()) } diff --git a/crates/tinybox-jail/tests/bwrap_pasta_live.rs b/crates/tinybox-jail/tests/bwrap_pasta_live.rs new file mode 100644 index 00000000..1e4cf767 --- /dev/null +++ b/crates/tinybox-jail/tests/bwrap_pasta_live.rs @@ -0,0 +1,185 @@ +//! Linux-only, live: an `allow_net` jail on a private `pasta` network reaches +//! the internet but not the host, and leaves no `pasta` behind. +//! +//! These need `bwrap`, `pasta` and `nsenter` and outbound network access, so +//! each skips (with a note) unless the backend reports +//! [`tinybox_jail::BubblewrapBackend::networks_privately`]. With +//! `TINYBOX_REQUIRE_PASTA=1` a missing private network is a failure instead; +//! CI sets it on the job that installs `passt`. +#![cfg(target_os = "linux")] + +use std::io; +use std::net::TcpListener; +use std::os::linux::net::SocketAddrExt as _; +use std::os::unix::fs::MetadataExt as _; +use std::os::unix::net::{SocketAddr, UnixListener}; +use std::path::Path; +use std::process::Command; +use std::time::{Duration, Instant}; + +use tinybox_jail::{BubblewrapBackend, JailBackend, spawn_captured_with}; + +#[allow(dead_code)] +mod support; + +use support::*; + +/// The backend when it networks privately, `None` to skip, or an error when +/// `TINYBOX_REQUIRE_PASTA=1` and it does not. +fn private() -> io::Result> { + let Some(backend) = backend()? else { + return Ok(None); + }; + if backend.networks_privately() { + return Ok(Some(backend)); + } + if std::env::var_os("TINYBOX_REQUIRE_PASTA").is_some_and(|v| v == "1") { + return Err(io::Error::other( + "TINYBOX_REQUIRE_PASTA=1 but bwrap has no private network (install passt; \ + pasta and nsenter must be root-owned at /usr/bin)", + )); + } + eprintln!("skipped: no working pasta private network"); + Ok(None) +} + +#[test] +fn live_a_private_network_reaches_out_but_not_the_host() -> io::Result<()> { + let Some(backend) = private()? else { + return Ok(()); + }; + let start = Instant::now(); + let root = tempfile::tempdir()?; + let loopback = TcpListener::bind("127.0.0.1:0")?; + let port = loopback.local_addr()?.port(); + let name = format!("tinybox-pasta-live-{}", std::process::id()); + let _abstract = UnixListener::bind_addr(&SocketAddr::from_abstract_name(name.as_bytes())?)?; + let script = format!( + r#"echo started +reach() {{ (exec 3<>"/dev/tcp/$1/$2") 2>/dev/null && echo "$3=reached" || echo "$3=refused"; }} +reach example.com 443 outbound +reach 127.0.0.1 {port} loopback +gw=$(ip route 2>/dev/null | awk '/default/{{print $3; exit}}') +reach "${{gw:-0.0.0.0}}" {port} gateway +python3 -c 'import socket,sys; socket.socket(socket.AF_UNIX).connect("\0"+sys.argv[1])' {name} 2>/dev/null \ + && echo abstract=reached || echo abstract=refused +# Only stdio may be open here: the holder's namespace and writer descriptors +# were closed before bwrap ran (3 is this listing's own directory handle). +echo "fds=$(cd /proc/self/fd && ls | sort -n | tr '\n' ' ')""# + ); + let mut cmd = Command::new("/usr/bin/env"); + cmd.args(["bash", "-c", &script]); + let (_, out) = started(&run(&backend, &jail(root.path()), cmd)?)?; + for expected in [ + "outbound=reached", + "loopback=refused", + "gateway=refused", + "abstract=refused", + ] { + assert!( + out.lines().any(|line| line == expected), + "expected {expected} in {out:?}" + ); + } + let fds = out + .lines() + .find_map(|line| line.strip_prefix("fds=")) + .ok_or_else(|| io::Error::other(format!("no fd listing in {out:?}")))?; + assert!( + fds.split_whitespace() + .all(|fd| matches!(fd, "0" | "1" | "2" | "3")), + "only stdio may be open in the jail, got {fds}" + ); + assert!(backend.is_suitable_for_untrusted_jail(&jail(root.path()))); + done("pasta reachability", start); + Ok(()) +} + +#[test] +fn live_killing_a_jail_leaves_no_pasta_behind() -> io::Result<()> { + let Some(backend) = private()? else { + return Ok(()); + }; + let start = Instant::now(); + let root = tempfile::tempdir()?; + let mut cmd = Command::new("/bin/sh"); + cmd.args(["-c", "sleep 30"]); + let jail = jail(root.path()); + backend + .require(&jail) + .map_err(|error| io::Error::new(io::ErrorKind::Unsupported, error))?; + let mut child = spawn_captured_with(&backend, &jail, cmd)?; + let pasta = eventually(|| pasta_of(child.id())) + .ok_or_else(|| io::Error::other("no pasta serves the jail's network"))?; + child.kill()?; + child.wait()?; + assert!( + eventually(|| (!Path::new(&format!("/proc/{pasta}")).exists()).then_some(())).is_some(), + "pasta {pasta} outlived the killed jail" + ); + done("pasta after kill", start); + Ok(()) +} + +#[test] +fn live_a_jail_that_exits_leaves_no_pasta_behind() -> io::Result<()> { + let Some(backend) = private()? else { + return Ok(()); + }; + let start = Instant::now(); + let root = tempfile::tempdir()?; + // Long enough to find its pasta, then a normal exit. + let mut cmd = Command::new("/bin/sh"); + cmd.args(["-c", "sleep 1"]); + let jail = jail(root.path()); + backend + .require(&jail) + .map_err(|error| io::Error::new(io::ErrorKind::Unsupported, error))?; + let child = spawn_captured_with(&backend, &jail, cmd)?; + let pasta = eventually(|| pasta_of(child.id())) + .ok_or_else(|| io::Error::other("no pasta serves the jail's network"))?; + assert!(child.wait_with_output()?.status.success()); + assert!( + eventually(|| (!Path::new(&format!("/proc/{pasta}")).exists()).then_some(())).is_some(), + "pasta {pasta} outlived the jail" + ); + done("pasta after exit", start); + Ok(()) +} + +/// Retries `check` for up to five seconds. +fn eventually(mut check: impl FnMut() -> Option) -> Option { + let deadline = Instant::now() + Duration::from_secs(5); + loop { + if let Some(value) = check() { + return Some(value); + } + if Instant::now() >= deadline { + return None; + } + std::thread::sleep(Duration::from_millis(50)); + } +} + +/// The `pasta` whose holder shares the network namespace of process `jail`. +/// Tests run in parallel, so matching by namespace keeps each to its own. +fn pasta_of(jail: u32) -> Option { + let net = std::fs::metadata(format!("/proc/{jail}/ns/net")).ok()?; + let own = std::fs::metadata("/proc/self/ns/net").ok()?; + if net.ino() == own.ino() { + return None; + } + std::fs::read_dir("/proc") + .ok()? + .filter_map(|entry| entry.ok()?.file_name().to_str()?.parse::().ok()) + .filter(|pid| { + std::fs::read_to_string(format!("/proc/{pid}/comm")).is_ok_and(|c| c.trim() == "pasta") + }) + .find(|pid| { + std::fs::read_to_string(format!("/proc/{pid}/task/{pid}/children")) + .ok() + .and_then(|c| c.split_whitespace().next()?.parse::().ok()) + .and_then(|holder| std::fs::metadata(format!("/proc/{holder}/ns/net")).ok()) + .is_some_and(|holder| holder.ino() == net.ino() && holder.dev() == net.dev()) + }) +} From 2b160c20bbd2fed52c0ba5218ffea90985eca124 Mon Sep 17 00:00:00 2001 From: M3gA-Mind Date: Wed, 11 Nov 2026 19:03:59 +0530 Subject: [PATCH 2/3] ci: lift Ubuntu's user-namespace restriction for the pasta live job ubuntu-latest is rolling to Ubuntu 24.04, where AppArmor denies unprivileged user namespaces to programs without a profile for them, which is every bwrap jail the job starts, so the backend found no working bwrap and every live test failed before reaching pasta. Lift it the way the native-resource job already does, and print the bwrap and pasta versions the run used. --- .github/workflows/ci.yml | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0ac01a76..7f1fa769 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -54,14 +54,24 @@ jobs: persist-credentials: false - uses: dtolnay/rust-toolchain@686976e191b89faba57d3206551f0f330d8cb249 - name: Install bubblewrap, passt (pasta) and util-linux (nsenter) - run: sudo apt-get install -y --no-install-recommends bubblewrap passt util-linux + run: | + set -euo pipefail + sudo apt-get install -y --no-install-recommends bubblewrap passt util-linux + # Ubuntu 24.04 denies unprivileged user namespaces to programs without + # an AppArmor profile for them, which is every bwrap jail here; the + # native-resource job lifts it the same way + # (scripts/ci/prepare-rootless-native-resources.sh). + if sysctl -n kernel.apparmor_restrict_unprivileged_userns >/dev/null 2>&1; then + sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0 + fi + bwrap --version + pasta --version | head -1 - name: Prove outbound works and host loopback, gateway and abstract sockets do not env: TINYBOX_REQUIRE_BWRAP: "1" TINYBOX_REQUIRE_PASTA: "1" run: | set -euo pipefail - pasta --version | head -1 cargo test --locked -p tinybox-jail --test bwrap_pasta_live -- --nocapture cargo test --locked -p tinybox-jail --test bwrap_isolation -- --nocapture cargo test --locked -p tinybox-jail --test bwrap_fd_leak -- --nocapture From 87c539ac6c03aba56cd2098751dee5b6f6ee7ca9 Mon Sep 17 00:00:00 2001 From: M3gA-Mind Date: Wed, 11 Nov 2026 19:08:21 +0530 Subject: [PATCH 3/3] test(bwrap): find pasta under its AVX2 name and bound each probe connect On x86 with AVX2 pasta re-executes itself as pasta.avx2, so the live tests' lookup by process name found no pasta and both lifetime tests failed although the jail ran on its network. Match the name by prefix. A connect to the unmapped gateway is dropped rather than refused, so the reachability probe waited out the TCP timeout (132 s on CI); bound each connect to 5 s. --- crates/tinybox-jail/tests/bwrap_pasta_live.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/crates/tinybox-jail/tests/bwrap_pasta_live.rs b/crates/tinybox-jail/tests/bwrap_pasta_live.rs index 1e4cf767..ad217095 100644 --- a/crates/tinybox-jail/tests/bwrap_pasta_live.rs +++ b/crates/tinybox-jail/tests/bwrap_pasta_live.rs @@ -56,7 +56,7 @@ fn live_a_private_network_reaches_out_but_not_the_host() -> io::Result<()> { let _abstract = UnixListener::bind_addr(&SocketAddr::from_abstract_name(name.as_bytes())?)?; let script = format!( r#"echo started -reach() {{ (exec 3<>"/dev/tcp/$1/$2") 2>/dev/null && echo "$3=reached" || echo "$3=refused"; }} +reach() {{ timeout 5 bash -c 'exec 3<>"/dev/tcp/$0/$1"' "$1" "$2" 2>/dev/null && echo "$3=reached" || echo "$3=refused"; }} reach example.com 443 outbound reach 127.0.0.1 {port} loopback gw=$(ip route 2>/dev/null | awk '/default/{{print $3; exit}}') @@ -173,7 +173,9 @@ fn pasta_of(jail: u32) -> Option { .ok()? .filter_map(|entry| entry.ok()?.file_name().to_str()?.parse::().ok()) .filter(|pid| { - std::fs::read_to_string(format!("/proc/{pid}/comm")).is_ok_and(|c| c.trim() == "pasta") + // On x86 with AVX2, pasta re-executes itself as `pasta.avx2`. + std::fs::read_to_string(format!("/proc/{pid}/comm")) + .is_ok_and(|c| c.trim().starts_with("pasta")) }) .find(|pid| { std::fs::read_to_string(format!("/proc/{pid}/task/{pid}/children"))