diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a77f6f6a..21101897 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -44,6 +44,22 @@ 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 + # The bubblewrap backend's limits ride on the same user manager. A skip + # is a failure here; the second run removes pasta so the shared-network + # refusal is the path tested. + - name: Prove bubblewrap jails hold their resource limits + shell: bash + run: | + set -euo pipefail + uid="$(id -u)" + export XDG_RUNTIME_DIR="/run/user/${uid}" + export DBUS_SESSION_BUS_ADDRESS="unix:path=$XDG_RUNTIME_DIR/bus" + export TINYBOX_REQUIRE_BWRAP=1 TINYBOX_REQUIRE_BWRAP_LIMITS=1 + cargo test --locked -p tinybox-jail --test bwrap_resource_limits -- --nocapture + if dpkg -s passt >/dev/null 2>&1; then sudo apt-get remove --yes passt; fi + TINYBOX_REQUIRE_SHARED_NETWORK=1 cargo test --locked -p tinybox-jail \ + --test bwrap_resource_limits a_hard_limit_is_refused_for_a_jail_sharing_the_host_network \ + -- --nocapture # The real-bubblewrap suites. They skip when bwrap cannot run (Ubuntu 24.04 # blocks unprivileged user namespaces by default), so this job makes bwrap # usable and requires it: a skip here is a failure, not a pass. diff --git a/crates/tinybox-jail/README.md b/crates/tinybox-jail/README.md index 85d1a966..3f4ce98f 100644 --- a/crates/tinybox-jail/README.md +++ b/crates/tinybox-jail/README.md @@ -43,7 +43,7 @@ spawns, never the core process itself. | `crates/tinybox-jail/src/jail.rs` | Core types: the `Jail` description struct (builder plus `canonicalize`/`canonicalize_or_log`) and the `JailBackend` trait (`name`/`is_available`/`spawn`). | | `crates/tinybox-jail/src/detect.rs` | `pick_backend()`: first available OS backend, else an unsupported backend that fails closed. | | `crates/tinybox-jail/src/noop.rs` | `NoopBackend`: no enforcement, plain `Command::spawn`. Always available. | -| `crates/tinybox-jail/src/bwrap/` | Bubblewrap backend (Linux only): user/pid/ipc/uts (+net) namespaces, grants as bind mounts, `deny_read` masked, `--die-with-parent`. `isolation()` is the minimum over configurations (`Process` with `--disable-userns`, else `None`); `Kernel` only per jail without `allow_net` (`isolation_for` / `is_suitable_for_untrusted_jail`; hosts must use the per-jail form). Never looks `bwrap` up on `PATH`; the probe is a sanity check, not authentication, and `with_program` skips the ownership check. The caller's environment and credentials travel in a private `--args` file, not in argv or any host process environment (`launcher.rs`), and inherited descriptors above 2 are closed by that launcher. No seccomp filter and no resource limits yet. Landlock is the fallback. Landlock is not layered inside because the kernel denies `mount` to a Landlock-restricted thread. | +| `crates/tinybox-jail/src/bwrap/` | Bubblewrap backend (Linux only): user/pid/ipc/uts (+net) namespaces, grants as bind mounts, `deny_read` masked, `--die-with-parent`. `isolation()` is the minimum over configurations (`Process` with `--disable-userns`, else `None`); `Kernel` only per jail without `allow_net` (`isolation_for` / `is_suitable_for_untrusted_jail`; hosts must use the per-jail form). Never looks `bwrap` up on `PATH`; the probe is a sanity check, not authentication, and `with_program` skips the ownership check. The caller's environment and credentials travel in a private `--args` file, not in argv or any host process environment (`launcher.rs`), and inherited descriptors above 2 are closed by that launcher. Jails start under a seccomp deny list. CPU, memory and pid limits run through a rootless systemd scope for the controllers the user manager was delegated, and only those are claimed; a hard limit is refused for a jail that shares the host network or is granted `/run` or `/sys` (`scope.rs`). Landlock is the fallback. Landlock is not layered inside because the kernel denies `mount` to a Landlock-restricted thread. | | `crates/tinybox-jail/src/linux.rs` | Landlock backend (Linux only, `landlock` feature, on by default). Applies the ruleset to a dedicated spawn thread so no `unsafe` `pre_exec` is needed. | | `crates/tinybox-jail/src/macos.rs` | Seatbelt backend via `sandbox-exec`. Compiled on every host so the profile renderer is unit-tested everywhere; selected only on macOS. | | `crates/tinybox-jail/src/windows.rs` | `AppContainerBackend`, compiled on Windows only under the one file-scoped `unsafe_code` allowance. Available through `spawn_owned_captured` / `spawn_owned_captured_with`; the legacy `spawn` returns `Unsupported` because it cannot return a waitable `std::process::Child`. | diff --git a/crates/tinybox-jail/src/bwrap/launcher.rs b/crates/tinybox-jail/src/bwrap/launcher.rs index dc7bf474..066e6f04 100644 --- a/crates/tinybox-jail/src/bwrap/launcher.rs +++ b/crates/tinybox-jail/src/bwrap/launcher.rs @@ -208,6 +208,31 @@ pub(super) fn command( launcher } +/// What `systemd-run --user` needs to reach the user manager, and nothing +/// else: the launcher otherwise runs with an empty environment. +const SCOPE_ENVIRONMENT: [&str; 2] = ["XDG_RUNTIME_DIR", "DBUS_SESSION_BUS_ADDRESS"]; + +/// `launcher`, run inside a transient resource scope. `scope` is the +/// `systemd-run … --` prefix (`tinybox_linux::resource_scope_argv`). In scope +/// mode `systemd-run` registers the scope and then execs the launcher in place, +/// so the descriptors mapped onto it reach the launcher unchanged. `None` for +/// an empty prefix. +pub(super) fn in_scope(scope: &[String], launcher: &Command) -> Option { + let (program, prefix) = scope.split_first()?; + let mut scoped = Command::new(program); + scoped + .env_clear() + .args(prefix) + .arg(launcher.get_program()) + .args(launcher.get_args()); + for key in SCOPE_ENVIRONMENT { + if let Some(value) = std::env::var_os(key) { + scoped.env(key, value); + } + } + Some(scoped) +} + #[cfg(test)] #[path = "launcher_tests.rs"] mod tests; diff --git a/crates/tinybox-jail/src/bwrap/launcher_tests.rs b/crates/tinybox-jail/src/bwrap/launcher_tests.rs index 7219704c..3a968749 100644 --- a/crates/tinybox-jail/src/bwrap/launcher_tests.rs +++ b/crates/tinybox-jail/src/bwrap/launcher_tests.rs @@ -295,3 +295,44 @@ fn entering_namespaces_puts_nsenter_in_front_by_descriptor() -> io::Result<()> { assert_eq!(argv.get(9).map(String::as_str), Some("4")); Ok(()) } + +#[test] +fn a_scoped_launcher_runs_under_the_prefix_with_only_the_manager_variables() -> io::Result<()> { + let file = ArgsFile::create(&[], &tmp())?; + let launcher = command( + None, + "/bin/bash", + Path::new("/usr/bin/bwrap"), + &file, + 0, + "prog".as_ref(), + &["arg".into()], + ); + file.discard(); + let prefix: Vec = ["/usr/bin/systemd-run", "--user", "--scope", "--"] + .map(String::from) + .into(); + + let scoped = in_scope(&prefix, &launcher).ok_or_else(|| io::Error::other("no command"))?; + + assert_eq!(scoped.get_program(), "/usr/bin/systemd-run"); + let args: Vec<&std::ffi::OsStr> = scoped.get_args().collect(); + let inner: Vec<&std::ffi::OsStr> = std::iter::once(launcher.get_program()) + .chain(launcher.get_args()) + .collect(); + assert_eq!( + args.get(..3), + Some(&["--user", "--scope", "--"].map(std::ffi::OsStr::new)[..]) + ); + assert_eq!(args.get(3..), Some(&inner[..])); + for (key, _) in scoped.get_envs() { + assert!( + SCOPE_ENVIRONMENT + .iter() + .any(|allowed| key == std::ffi::OsStr::new(allowed)), + "{key:?}" + ); + } + assert!(in_scope(&[], &launcher).is_none()); + Ok(()) +} diff --git a/crates/tinybox-jail/src/bwrap/mod.rs b/crates/tinybox-jail/src/bwrap/mod.rs index 58b10334..7f5285be 100644 --- a/crates/tinybox-jail/src/bwrap/mod.rs +++ b/crates/tinybox-jail/src/bwrap/mod.rs @@ -50,7 +50,7 @@ //! [`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. +//! deny list: it does not imply resource limits (see "Resource limits"). //! //! # Private network //! @@ -79,10 +79,17 @@ //! `bwrap` without `--add-seccomp-fd`, or an architecture other than `x86_64` //! and `aarch64`, leaves the backend unavailable rather than unfiltered. //! +//! # Resource limits +//! +//! A jail's `resources` are applied through a rootless systemd scope around +//! the launcher, for each controller the user manager was delegated, and only +//! those are claimed in [`JailBackend::constraint_support`] (see `scope.rs`). +//! A hard limit is refused for a jail that could reach the user manager and +//! leave its scope: one sharing the host network, or one granted `/run` or +//! `/sys`. Disk is never enforced. +//! //! # Known gaps (not closed in this backend) //! -//! - No CPU, memory or pid limits (`tinybox-linux` offers them through a -//! `systemd-run` scope; not wired here). //! - Check-then-use gap on nested grants, **only where `bwrap` lacks //! `--bind-fd` or the kernel lacks `openat2` (before Linux 5.6)**. Then the //! symlink check on a grant inside a writable directory @@ -135,6 +142,7 @@ use crate::jail::{Jail, JailBackend}; mod args; mod launcher; mod pasta; +mod scope; mod seccomp; /// Backend name reported by [`BubblewrapBackend`]. @@ -159,6 +167,9 @@ struct Verified { /// A working `pasta` and `nsenter`: an `allow_net` jail then gets a /// private network instead of the host's (see `pasta`). pasta: Option, + /// A trusted `systemd-run` whose scope holds CPU, memory and pid limits + /// for the controllers the user manager was delegated (see `scope`). + scope: Option, } /// Bubblewrap (`bwrap`) namespace jail backend. @@ -258,6 +269,7 @@ impl BubblewrapBackend { bind_fds: false, seccomp: Vec::new(), pasta: None, + scope: None, }), } } @@ -293,6 +305,7 @@ fn verify(program: &Path) -> Option { bind_fds: supports_fd_binds(program), seccomp, pasta: private_network(program), + scope: scope::ResourceScope::detect(), }); } } @@ -461,6 +474,7 @@ fn prepare_command( jail: &Jail, cmd: &Command, network: Option<&pasta::Network>, + resource_scope: Option<&[String]>, ) -> io::Result { let args::JailArgs { mut argv, mut fds } = args::jail_args_with_fds( jail, @@ -527,6 +541,11 @@ fn prepare_command( cmd.get_program(), &program_args, ); + // Before the descriptors are mapped: they are mapped onto the outermost + // process, which `systemd-run` replaces with the launcher in place. + if let Some(scoped) = resource_scope.and_then(|prefix| launcher::in_scope(prefix, &launcher)) { + launcher = scoped; + } // Mapped onto 10, 11, … in the child only, after fork: never inheritable // in this process, where another thread could spawn concurrently. let mappings = fds @@ -582,10 +601,13 @@ impl JailBackend for BubblewrapBackend { } fn constraint_support(&self) -> ConstraintSupport { - if self.is_available() { - ConstraintSupport::NONE - .with(Constraint::Filesystem, Enforcement::Enforced) - .with(Constraint::Network, Enforcement::Enforced) + if let Some(verified) = &self.verified { + scope::with_limits( + ConstraintSupport::NONE + .with(Constraint::Filesystem, Enforcement::Enforced) + .with(Constraint::Network, Enforcement::Enforced), + verified.scope.as_ref(), + ) } else { ConstraintSupport::NONE } @@ -632,7 +654,13 @@ impl BubblewrapBackend { self.constraint_support() .require(self.name(), &jail.constraints()) .map_err(|error| io::Error::new(io::ErrorKind::Unsupported, error))?; - log::debug!("[cwd_jail:bwrap] spawning label={}", jail.label); + let resource_scope = + scope::prefix_for(jail, verified.scope.as_ref(), self.networks_privately())?; + log::debug!( + "[cwd_jail:bwrap] spawning label={} limited={}", + jail.label, + resource_scope.is_some() + ); // 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 { @@ -642,7 +670,13 @@ impl BubblewrapBackend { let Prepared { launcher: mut wrapper, args, - } = prepare_command(verified, jail, cmd, network.as_ref())?; + } = prepare_command( + verified, + jail, + cmd, + network.as_ref(), + resource_scope.as_deref(), + )?; if captured { wrapper .stdin(Stdio::piped()) diff --git a/crates/tinybox-jail/src/bwrap/mod_tests.rs b/crates/tinybox-jail/src/bwrap/mod_tests.rs index 2546dedb..806fd399 100644 --- a/crates/tinybox-jail/src/bwrap/mod_tests.rs +++ b/crates/tinybox-jail/src/bwrap/mod_tests.rs @@ -18,6 +18,7 @@ fn verified(program: &str) -> Verified { bind_fds: false, seccomp: Vec::new(), pasta: None, + scope: None, } } @@ -84,7 +85,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, None)?; + let prepared = prepare_command(&verified("/usr/bin/bwrap"), &jail, &cmd, None, 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); @@ -242,7 +243,13 @@ fn a_private_network_jail_enters_the_holder_and_brings_its_own_resolver() -> io: std::fs::File::open("/proc/self/ns/net")?, writer.into(), ); - let prepared = prepare_command(&backend_state, &jail, &Command::new("true"), Some(&network))?; + let prepared = prepare_command( + &backend_state, + &jail, + &Command::new("true"), + Some(&network), + None, + )?; assert_eq!(prepared.launcher.get_program(), "/usr/bin/nsenter"); let argv = argv_of(&prepared.launcher); assert!(argv.iter().any(|a| a == "--preserve-credentials")); @@ -304,3 +311,91 @@ fn a_pasta_that_fails_to_start_refuses_the_jail_instead_of_sharing_the_host_netw .map(|e| e.kind()); assert_eq!(error, Some(io::ErrorKind::NotFound)); } + +#[test] +fn limits_are_claimed_only_for_controllers_the_scope_was_delegated() { + let mut state = verified("/usr/bin/bwrap"); + state.scope = Some(scope::ResourceScope::with_probe( + "/usr/bin/systemd-run", + tinybox_linux::ScopeProbe::from_parts(true, Some("memory pids")), + )); + let backend = BubblewrapBackend { + verified: Some(state), + }; + let support = backend.constraint_support(); + assert_eq!( + support.enforcement(Constraint::Filesystem), + Enforcement::Enforced + ); + assert_eq!( + support.enforcement(Constraint::Memory), + Enforcement::Enforced + ); + assert_eq!(support.enforcement(Constraint::Pids), Enforcement::Enforced); + assert_eq!( + support.enforcement(Constraint::Cpu), + Enforcement::Unsupported + ); + assert_eq!( + support.enforcement(Constraint::Disk), + Enforcement::Unsupported + ); + // Without a scope, as before: no limit is claimed. + assert_eq!( + available() + .constraint_support() + .enforcement(Constraint::Memory), + Enforcement::Unsupported + ); +} + +#[test] +fn a_scoped_launch_starts_systemd_run_and_keeps_the_jail_inside_it() -> io::Result<()> { + let root = tempfile::tempdir()?; + let jail = Jail::new(root.path(), "t").deny_net(); + let prefix: Vec = ["/usr/bin/systemd-run", "--user", "--scope", "--"] + .map(String::from) + .into(); + let prepared = prepare_command( + &verified("/usr/bin/bwrap"), + &jail, + &Command::new("true"), + None, + Some(&prefix), + )?; + assert_eq!(prepared.launcher.get_program(), "/usr/bin/systemd-run"); + let argv = argv_of(&prepared.launcher); + // The launcher shell and then bwrap follow the scope's separator. + let separator = argv.iter().position(|a| a == "--"); + assert_eq!(separator, Some(2)); + assert!(argv.iter().skip(3).any(|a| a == "/usr/bin/bwrap")); + prepared.args.discard(); + Ok(()) +} + +#[test] +fn a_hard_limit_the_scope_cannot_hold_refuses_the_spawn() { + let root = std::env::temp_dir(); + let mut jail = Jail::new(&root, "t") + .deny_net() + .with_hard_requirements([Constraint::Cpu]); + jail.resources = Some(tinybox_core::Resources::DEFAULT); + // No scope at all, and a scope without the cpu controller. + for probe in [None, Some("memory pids")] { + let mut state = verified(&fake_bwrap().to_string_lossy()); + state.scope = probe.map(|controllers| { + scope::ResourceScope::with_probe( + "/usr/bin/systemd-run", + tinybox_linux::ScopeProbe::from_parts(true, Some(controllers)), + ) + }); + let backend = BubblewrapBackend { + verified: Some(state), + }; + let kind = backend + .spawn(&jail, Command::new("true")) + .err() + .map(|e| e.kind()); + assert_eq!(kind, Some(io::ErrorKind::Unsupported), "{probe:?}"); + } +} diff --git a/crates/tinybox-jail/src/bwrap/scope.rs b/crates/tinybox-jail/src/bwrap/scope.rs new file mode 100644 index 00000000..93164c46 --- /dev/null +++ b/crates/tinybox-jail/src/bwrap/scope.rs @@ -0,0 +1,145 @@ +//! CPU, memory and pid limits for a bubblewrap jail, through a rootless +//! systemd scope. +//! +//! The launcher runs under `systemd-run --user --scope` (see +//! `tinybox_linux::resource_scope_argv`), so the whole jail, `bwrap` and +//! everything inside it, lives in one transient cgroup holding the limits. Only +//! a limit whose controller the user manager was delegated is claimed or +//! applied ([`tinybox_linux::ScopeProbe`]). The scope's task count includes the +//! two or three host-side processes `bwrap` itself runs as. +//! +//! A jail that could reach the user manager could start a process outside its +//! scope. The baseline mounts expose neither `/run` (the manager's socket) nor +//! `/sys` (the cgroup tree), but a grant can, and a jail sharing the host +//! network shares its abstract sockets. A *hard* limit is therefore refused +//! for such a jail; a soft one is still applied, as a best effort. + +use std::io; +use std::path::{Path, PathBuf}; + +use tinybox_core::{Constraint, ConstraintSupport, Enforcement}; +use tinybox_linux::{ScopeLimits, ScopeProbe}; + +use crate::jail::Jail; + +/// The constraints a scope can hold. +const LIMITS: [Constraint; 3] = [Constraint::Cpu, Constraint::Memory, Constraint::Pids]; + +/// Where the user manager's socket and the cgroup tree live. +const MANAGER_PATHS: [&str; 2] = ["/run", "/sys"]; + +/// A trusted `systemd-run` and what the user manager behind it was delegated. +#[derive(Debug, Clone)] +pub(super) struct ResourceScope { + systemd_run: PathBuf, + probe: ScopeProbe, +} + +impl ResourceScope { + /// The `systemd-run` at a fixed system location, probed once with the + /// backend. `None` without one: then no limit is claimed. + pub(super) fn detect() -> Option { + let systemd_run = tinybox_linux::trusted_systemd_run_program().ok()?; + Some(Self { + systemd_run, + probe: ScopeProbe::detect(), + }) + } + + /// A scope with a given probe, for tests. + #[cfg(test)] + pub(super) fn with_probe(systemd_run: impl Into, probe: ScopeProbe) -> Self { + Self { + systemd_run: systemd_run.into(), + probe, + } + } + + fn enforcement(&self, constraint: Constraint) -> Enforcement { + self.probe.constraint_support().enforcement(constraint) + } +} + +/// `support` with `Cpu`, `Memory` and `Pids` set to what `scope` can enforce +/// (`Unsupported` without one). +pub(super) fn with_limits( + support: ConstraintSupport, + scope: Option<&ResourceScope>, +) -> ConstraintSupport { + LIMITS.into_iter().fold(support, |support, constraint| { + let level = scope.map_or(Enforcement::Unsupported, |scope| { + scope.enforcement(constraint) + }); + support.with(constraint, level) + }) +} + +/// The `systemd-run … --` prefix to run `jail`'s launcher under, or `None` +/// when the jail asks for no limit this scope can apply. +/// +/// A soft limit the manager was not delegated is left out, and a soft request +/// whose scope command cannot be built runs unlimited. Hard limits the scope +/// cannot enforce never get here: preflight refuses them first. +/// +/// # Errors +/// `Unsupported` when a hard limit is required of a jail that could leave its +/// scope (see the module docs), or when the scope command cannot be built for +/// one. +pub(super) fn prefix_for( + jail: &Jail, + scope: Option<&ResourceScope>, + private_network: bool, +) -> io::Result>> { + let (Some(resources), Some(scope)) = (jail.resources, scope) else { + return Ok(None); + }; + let hard = LIMITS + .iter() + .any(|limit| jail.hard_requirements.contains(limit)); + if hard && let Some(reason) = escape_route(jail, private_network) { + return Err(io::Error::new( + io::ErrorKind::Unsupported, + format!("bubblewrap cannot hold a hard resource limit for this jail: {reason}"), + )); + } + let applied = |constraint, value: u64| { + (scope.enforcement(constraint) == Enforcement::Enforced).then_some(value) + }; + let limits = ScopeLimits { + cpu_millis: applied(Constraint::Cpu, u64::from(resources.cpu_millis)), + memory_bytes: applied(Constraint::Memory, resources.memory_bytes), + pids_max: applied(Constraint::Pids, u64::from(resources.pids_max)), + }; + if limits == ScopeLimits::default() { + return Ok(None); + } + match tinybox_linux::resource_scope_argv(&scope.systemd_run, "bwrap", limits) { + Ok(argv) => Ok(Some(argv)), + Err(error) if !hard => { + log::debug!("[cwd_jail:bwrap] soft limits not applied: {error}"); + Ok(None) + } + Err(error) => Err(io::Error::new(io::ErrorKind::Unsupported, error)), + } +} + +/// How `jail` could reach the user manager, if it could. +fn escape_route(jail: &Jail, private_network: bool) -> Option<&'static str> { + if jail.allow_net && !private_network { + return Some("it shares the host network, whose abstract sockets reach the user manager"); + } + let exposes = |path: &PathBuf| { + MANAGER_PATHS + .iter() + .any(|managed| path.starts_with(managed) || Path::new(managed).starts_with(path)) + }; + std::iter::once(&jail.root) + .chain(&jail.read_only) + .chain(&jail.read_write) + .any(exposes) + .then_some("a grant exposes /run or /sys, where the user manager and the cgroup tree live") +} + +#[cfg(test)] +#[path = "scope_tests.rs"] +mod tests; diff --git a/crates/tinybox-jail/src/bwrap/scope_tests.rs b/crates/tinybox-jail/src/bwrap/scope_tests.rs new file mode 100644 index 00000000..68472e6c --- /dev/null +++ b/crates/tinybox-jail/src/bwrap/scope_tests.rs @@ -0,0 +1,116 @@ +//! Tests for which limits a bubblewrap jail claims and when it applies them. + +use super::*; + +fn delegated(controllers: &str) -> ResourceScope { + ResourceScope::with_probe( + "/nonexistent/systemd-run", + ScopeProbe::from_parts(true, Some(controllers)), + ) +} + +fn limited_jail(root: &Path) -> Jail { + let mut jail = Jail::new(root, "t").deny_net(); + jail.resources = Some(tinybox_core::Resources::DEFAULT); + jail +} + +#[test] +fn only_delegated_controllers_are_claimed_and_none_without_a_scope() { + let support = with_limits(ConstraintSupport::NONE, Some(&delegated("cpu memory"))); + assert_eq!(support.enforcement(Constraint::Cpu), Enforcement::Enforced); + assert_eq!( + support.enforcement(Constraint::Memory), + Enforcement::Enforced + ); + assert_eq!( + support.enforcement(Constraint::Pids), + Enforcement::Unsupported + ); + + let none = with_limits(ConstraintSupport::NONE, None); + for limit in LIMITS { + assert_eq!(none.enforcement(limit), Enforcement::Unsupported); + } +} + +#[test] +fn no_resources_or_no_scope_means_no_scope_command() -> io::Result<()> { + let root = tempfile::tempdir()?; + let unlimited = Jail::new(root.path(), "t").deny_net(); + assert!(prefix_for(&unlimited, Some(&delegated("cpu memory pids")), true)?.is_none()); + assert!(prefix_for(&limited_jail(root.path()), None, true)?.is_none()); + // Nothing delegated: a soft request has nothing to apply. + assert!(prefix_for(&limited_jail(root.path()), Some(&delegated("")), true)?.is_none()); + Ok(()) +} + +#[test] +fn a_soft_request_runs_unlimited_when_the_scope_command_cannot_be_built() -> io::Result<()> { + let root = tempfile::tempdir()?; + // The stand-in `systemd-run` does not exist, so building its command fails. + let prefix = prefix_for( + &limited_jail(root.path()), + Some(&delegated("cpu memory pids")), + true, + )?; + assert!(prefix.is_none()); + Ok(()) +} + +#[test] +fn a_hard_request_fails_closed_when_the_scope_command_cannot_be_built() -> io::Result<()> { + let root = tempfile::tempdir()?; + let jail = limited_jail(root.path()).with_hard_requirements([Constraint::Pids]); + let kind = prefix_for(&jail, Some(&delegated("pids")), true) + .err() + .map(|error| error.kind()); + assert_eq!(kind, Some(io::ErrorKind::Unsupported)); + Ok(()) +} + +#[test] +fn a_hard_limit_is_refused_for_a_jail_sharing_the_host_network() -> io::Result<()> { + let root = tempfile::tempdir()?; + let mut jail = Jail::new(root.path(), "t").with_hard_requirements([Constraint::Memory]); + jail.resources = Some(tinybox_core::Resources::DEFAULT); + assert!(jail.allow_net); + + let error = prefix_for(&jail, Some(&delegated("memory")), false) + .err() + .ok_or_else(|| io::Error::other("a shared-network hard limit was accepted"))?; + assert_eq!(error.kind(), io::ErrorKind::Unsupported); + assert!(error.to_string().contains("host network"), "{error}"); + Ok(()) +} + +#[test] +fn a_hard_limit_is_refused_for_a_jail_granted_the_manager_or_cgroup_paths() -> io::Result<()> { + let root = tempfile::tempdir()?; + for granted in ["/run/user/1000", "/sys/fs/cgroup", "/run"] { + let jail = limited_jail(root.path()) + .add_read_only(granted) + .with_hard_requirements([Constraint::Cpu]); + let error = prefix_for(&jail, Some(&delegated("cpu")), true) + .err() + .ok_or_else(|| io::Error::other(format!("a grant of {granted} was accepted")))?; + assert!( + error.to_string().contains("/run or /sys"), + "{granted}: {error}" + ); + } + Ok(()) +} + +#[test] +fn an_unrelated_grant_and_a_private_network_leave_no_escape_route() -> io::Result<()> { + let root = tempfile::tempdir()?; + let jail = limited_jail(root.path()).add_read_only("/srv/data"); + assert!(escape_route(&jail, true).is_none()); + // `allow_net` is fine once the network is private (pasta). + let mut open = jail.clone(); + open.allow_net = true; + assert!(escape_route(&open, true).is_none()); + assert!(escape_route(&open, false).is_some()); + Ok(()) +} diff --git a/crates/tinybox-jail/tests/bwrap_resource_limits.rs b/crates/tinybox-jail/tests/bwrap_resource_limits.rs new file mode 100644 index 00000000..a729fac0 --- /dev/null +++ b/crates/tinybox-jail/tests/bwrap_resource_limits.rs @@ -0,0 +1,99 @@ +//! Linux-only: a bubblewrap jail's resource limits really hold. +//! +//! These need a working `bwrap` and a user manager delegated the `memory` +//! controller. They skip (with a note) when either is missing, unless +//! `TINYBOX_REQUIRE_BWRAP_LIMITS=1`, in which case a missing scope is a +//! failure. The shared-network refusal needs a host without `pasta`; +//! `TINYBOX_REQUIRE_SHARED_NETWORK=1` makes its presence a failure too. +#![cfg(target_os = "linux")] + +use std::io; +use std::time::Instant; + +use tinybox_core::{Constraint, Enforcement, Resources}; +use tinybox_jail::{BubblewrapBackend, Jail, JailBackend}; + +#[allow(dead_code)] +mod support; + +use support::*; + +/// The backend, when it can hold a memory limit here. +fn limiting_backend() -> io::Result> { + let Some(backend) = backend()? else { + return Ok(None); + }; + if backend.constraint_support().enforcement(Constraint::Memory) == Enforcement::Enforced { + return Ok(Some(backend)); + } + if std::env::var_os("TINYBOX_REQUIRE_BWRAP_LIMITS").is_some_and(|v| v == "1") { + return Err(io::Error::other( + "TINYBOX_REQUIRE_BWRAP_LIMITS=1 but the user manager holds no memory controller", + )); + } + eprintln!("skipped: no user manager delegated the memory controller"); + Ok(None) +} + +fn memory_jail(root: &std::path::Path, memory_bytes: u64) -> Jail { + let mut jail = jail(root) + .deny_net() + .with_hard_requirements([Constraint::Memory]); + jail.resources = Some(Resources { + memory_bytes, + ..Resources::DEFAULT + }); + jail +} + +/// Holds about 128 MiB in the shell's own memory, then reports. +const ALLOCATE: &str = "x=$(head -c 134217728 /dev/zero | tr '\\0' a); echo held ${#x}"; + +#[test] +fn a_hard_memory_limit_kills_a_jail_that_exceeds_it() -> io::Result<()> { + let Some(backend) = limiting_backend()? else { + return Ok(()); + }; + let start = Instant::now(); + let root = tempfile::tempdir()?; + + let (ok, out) = sh( + &backend, + &memory_jail(root.path(), 32 * 1024 * 1024), + ALLOCATE, + )?; + assert!(!ok, "128 MiB fit under a 32 MiB limit: {out}"); + assert!(!out.contains("held"), "{out}"); + + // The same work under a roomy limit completes, so the limit is the cause. + let (ok, out) = sh( + &backend, + &memory_jail(root.path(), 1024 * 1024 * 1024), + ALLOCATE, + )?; + assert!(ok && out.contains("held 134217728"), "{out}"); + done("memory-limit", start); + Ok(()) +} + +#[test] +fn a_hard_limit_is_refused_for_a_jail_sharing_the_host_network() -> io::Result<()> { + let Some(backend) = limiting_backend()? else { + return Ok(()); + }; + if backend.networks_privately() { + // With pasta the network is private, and the limit is allowed. + if std::env::var_os("TINYBOX_REQUIRE_SHARED_NETWORK").is_some_and(|v| v == "1") { + return Err(io::Error::other( + "TINYBOX_REQUIRE_SHARED_NETWORK=1 but pasta gives allow_net jails a private network", + )); + } + eprintln!("skipped: pasta gives allow_net jails a private network here"); + return Ok(()); + } + let root = tempfile::tempdir()?; + let mut shared = jail(root.path()).with_hard_requirements([Constraint::Memory]); + shared.resources = Some(Resources::DEFAULT); + assert_eq!(refused(&backend, &shared), Some(io::ErrorKind::Unsupported)); + Ok(()) +} diff --git a/crates/tinybox-linux/src/lib.rs b/crates/tinybox-linux/src/lib.rs index 5d684917..ed129a15 100644 --- a/crates/tinybox-linux/src/lib.rs +++ b/crates/tinybox-linux/src/lib.rs @@ -47,6 +47,7 @@ mod sandbox; pub use probe::{NamespaceProbe, ScopeProbe, UsernsStatus}; pub use sandbox::{ - NAME, NamespaceSandbox, NativeJailCommand, NativeJailLaunch, WORKSPACE_MOUNT, native_jail_argv, - native_jail_argv_with_programs, trusted_systemd_run_program, + NAME, NamespaceSandbox, NativeJailCommand, NativeJailLaunch, ScopeLimits, WORKSPACE_MOUNT, + native_jail_argv, native_jail_argv_with_programs, resource_scope_argv, + trusted_systemd_run_program, }; diff --git a/crates/tinybox-linux/src/sandbox/args.rs b/crates/tinybox-linux/src/sandbox/args.rs index a8f2bb67..803099fd 100644 --- a/crates/tinybox-linux/src/sandbox/args.rs +++ b/crates/tinybox-linux/src/sandbox/args.rs @@ -650,11 +650,48 @@ fn systemd_run_supports_safe_arguments(systemd_run: &Path) -> std::io::Result, + /// Resident memory limit in bytes; swap is capped at zero alongside it. + pub memory_bytes: Option, + /// Task (process and thread) count limit. + pub pids_max: Option, +} + +/// The `systemd-run --user --scope` command, through its `--`, that runs the +/// program appended after it in a fresh transient scope holding `limits`. +/// +/// The scope is named `tinybox---.scope`. `systemd-run` reaches +/// the user manager through `XDG_RUNTIME_DIR` or `DBUS_SESSION_BUS_ADDRESS`, +/// so the caller must pass those through. Whether the manager can apply each +/// limit is [`crate::ScopeProbe`]'s question, not this one's. +/// +/// # Errors +/// Returns an error unless `systemd_run` is an absolute trusted executable +/// whose command-argument expansion behavior can be verified. +pub fn resource_scope_argv( systemd_run: &Path, - bwrap: &Path, + kind: &str, + limits: ScopeLimits, +) -> std::io::Result> { + let systemd_run = validate_trusted_program(systemd_run)?; + let supports_expand_environment = systemd_run_supports_safe_arguments(&systemd_run)?; + Ok(scope_prefix( + &systemd_run, + kind, + supports_expand_environment, + limits, + )) +} + +pub(super) fn scope_prefix( + systemd_run: &Path, + kind: &str, + supports_expand_environment: bool, + limits: ScopeLimits, ) -> Vec { let mut argv = vec![ systemd_run.display().to_string(), @@ -662,7 +699,7 @@ fn native_bwrap_argv( "--scope".to_owned(), "--quiet".to_owned(), format!( - "--unit=tinybox-native-{}-{}.scope", + "--unit=tinybox-{kind}-{}-{}.scope", std::process::id(), NATIVE_SCOPE_SEQUENCE.fetch_add(1, std::sync::atomic::Ordering::Relaxed) ), @@ -670,20 +707,39 @@ fn native_bwrap_argv( if supports_expand_environment { argv.push("--expand-environment=no".into()); } - if let Some(value) = request.memory_bytes { + if let Some(value) = limits.memory_bytes { argv.push(format!("--property=MemoryMax={value}")); argv.push("--property=MemorySwapMax=0".to_owned()); // Keep the scope present long enough for its supervisor and observers // to report the kernel OOM event and complete bounded cleanup. argv.push("--property=OOMPolicy=continue".to_owned()); } - if let Some(value) = request.cpu_millis { + if let Some(value) = limits.cpu_millis { argv.push(format!("--property=CPUQuota={}%", cpu_quota_u64(value))); } - if let Some(value) = request.pids_max { + if let Some(value) = limits.pids_max { argv.push(format!("--property=TasksMax={value}")); } argv.push("--".to_owned()); + argv +} + +fn native_bwrap_argv( + request: &NativeJailCommand, + supports_expand_environment: bool, + systemd_run: &Path, + bwrap: &Path, +) -> Vec { + let mut argv = scope_prefix( + systemd_run, + "native", + supports_expand_environment, + ScopeLimits { + cpu_millis: request.cpu_millis, + memory_bytes: request.memory_bytes, + pids_max: request.pids_max, + }, + ); argv.push(bwrap.display().to_string()); for flag in [ "--unshare-user", diff --git a/crates/tinybox-linux/src/sandbox/mod.rs b/crates/tinybox-linux/src/sandbox/mod.rs index 02158e7f..850601bb 100644 --- a/crates/tinybox-linux/src/sandbox/mod.rs +++ b/crates/tinybox-linux/src/sandbox/mod.rs @@ -13,8 +13,8 @@ mod args; mod env_file; pub use args::{ - NativeJailCommand, NativeJailLaunch, WORKSPACE_MOUNT, native_jail_argv, - native_jail_argv_with_programs, trusted_systemd_run_program, + NativeJailCommand, NativeJailLaunch, ScopeLimits, WORKSPACE_MOUNT, native_jail_argv, + native_jail_argv_with_programs, resource_scope_argv, trusted_systemd_run_program, }; /// The name this sandbox registers under. diff --git a/crates/tinybox-linux/src/sandbox/mod_tests.rs b/crates/tinybox-linux/src/sandbox/mod_tests.rs index 2c8b30f4..f9141cb1 100644 --- a/crates/tinybox-linux/src/sandbox/mod_tests.rs +++ b/crates/tinybox-linux/src/sandbox/mod_tests.rs @@ -465,6 +465,58 @@ fn systemd_scope_environment_flag_is_added_only_when_supported() { assert_eq!(legacy.get(3).map(String::as_str), Some("--quiet")); } +#[test] +fn a_resource_scope_sets_only_the_limits_asked_for_and_ends_at_its_separator() { + let systemd_run = Path::new("/usr/bin/systemd-run"); + let only_pids = args::scope_prefix( + systemd_run, + "bwrap", + false, + args::ScopeLimits { + pids_max: Some(16), + ..args::ScopeLimits::default() + }, + ); + assert_eq!( + only_pids.first().map(String::as_str), + Some("/usr/bin/systemd-run") + ); + // The unit is `tinybox-bwrap--.scope`, a systemd name, not a path. + let unit_name = |arg: &String| { + arg.strip_prefix("--unit=tinybox-bwrap-") + .and_then(|rest| rest.strip_suffix(".scope")) + .is_some() + }; + assert!(only_pids.iter().any(unit_name)); + assert!(has(&only_pids, "--property=TasksMax=16")); + assert!(!only_pids.iter().any(|arg| arg.contains("MemoryMax"))); + assert!(!only_pids.iter().any(|arg| arg.contains("CPUQuota"))); + assert_eq!(only_pids.last().map(String::as_str), Some("--")); + + let all = args::scope_prefix( + systemd_run, + "bwrap", + true, + args::ScopeLimits { + cpu_millis: Some(1), + memory_bytes: Some(64), + pids_max: Some(8), + }, + ); + for property in [ + "--property=MemoryMax=64", + "--property=MemorySwapMax=0", + "--property=CPUQuota=0.1%", + "--property=TasksMax=8", + "--expand-environment=no", + ] { + assert!(has(&all, property), "{property} in {all:?}"); + } + // Two scopes started by one process never share a unit name. + let unit = |argv: &[String]| argv.iter().find(|arg| arg.starts_with("--unit=")).cloned(); + assert_ne!(unit(&only_pids), unit(&all)); +} + #[test] fn systemd_environment_flag_probe_distinguishes_legacy_and_fails_closed() { assert_eq!(