Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/tinybox-linux/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
mod probe;
mod sandbox;

pub use probe::{NamespaceProbe, UsernsStatus};
pub use probe::{NamespaceProbe, ScopeProbe, UsernsStatus};

pub use sandbox::{
NAME, NamespaceSandbox, NativeJailCommand, NativeJailLaunch, WORKSPACE_MOUNT, native_jail_argv,
Expand Down
4 changes: 4 additions & 0 deletions crates/tinybox-linux/src/probe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

use std::path::{Path, PathBuf};

mod scope;

pub use scope::ScopeProbe;

/// Whether an unprivileged process may create a user namespace.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UsernsStatus {
Expand Down
113 changes: 113 additions & 0 deletions crates/tinybox-linux/src/probe/scope.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
//! Whether a rootless systemd scope can really hold CPU, memory and pid limits.
//!
//! A trusted `systemd-run` on disk is not enough. The limits are applied by the
//! user's own systemd manager, which must be running and reachable, and which
//! can only set a limit for a cgroup controller it was delegated. Without that
//! a scope either fails to start or starts with the limit silently missing.
//! [`ScopeProbe`] reads both before any limit is claimed.

use tinybox_core::{Constraint, ConstraintSupport, Enforcement};

/// Which resource limits a rootless systemd scope can enforce here.
///
/// Each field is true only when the user manager is reachable and was
/// delegated that controller.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct ScopeProbe {
/// The `cpu` controller can be limited.
pub cpu: bool,
/// The `memory` controller can be limited.
pub memory: bool,
/// The `pids` controller can be limited.
pub pids: bool,
}

impl ScopeProbe {
/// Probe this machine: the user manager's socket under `$XDG_RUNTIME_DIR`
/// (or the session bus socket), and the controllers listed in
/// `/sys/fs/cgroup/user.slice/user-<uid>.slice/user@<uid>.service/cgroup.controllers`.
#[must_use]
pub fn detect() -> Self {
let Some(uid) = current_uid() else {
return Self::default();
};
let controllers = std::fs::read_to_string(format!(
"/sys/fs/cgroup/user.slice/user-{uid}.slice/user@{uid}.service/cgroup.controllers"
))
.ok();
Self::from_parts(manager_socket_exists(), controllers.as_deref())
}

/// Build a probe from what was read: whether a manager socket exists, and
/// the space-separated contents of the user manager's `cgroup.controllers`.
#[must_use]
pub fn from_parts(manager: bool, controllers: Option<&str>) -> Self {
let has = |name: &str| {
manager
&& controllers.is_some_and(|list| list.split_whitespace().any(|item| item == name))
};
Self {
cpu: has("cpu"),
memory: has("memory"),
pids: has("pids"),
}
}

/// `Cpu`, `Memory` and `Pids`: each `Enforced` only when its controller is
/// delegated to a reachable manager, otherwise `Unsupported`.
#[must_use]
pub fn constraint_support(&self) -> ConstraintSupport {
let level = |delegated: bool| {
if delegated {
Enforcement::Enforced
} else {
Enforcement::Unsupported
}
};
ConstraintSupport::NONE
.with(Constraint::Cpu, level(self.cpu))
.with(Constraint::Memory, level(self.memory))
.with(Constraint::Pids, level(self.pids))
}
}

/// The real uid, read from the owner of `/proc/self`.
#[cfg(unix)]
fn current_uid() -> Option<u32> {
use std::os::unix::fs::MetadataExt as _;
std::fs::metadata("/proc/self").ok().map(|meta| meta.uid())
}

#[cfg(not(unix))]
fn current_uid() -> Option<u32> {
None
}

/// `$XDG_RUNTIME_DIR/systemd/private`, or the session bus's `unix:path=`
/// socket, which `systemd-run --user` can reach the manager through.
#[cfg(unix)]
fn manager_socket_exists() -> bool {
use std::os::unix::fs::FileTypeExt as _;
let is_socket = |path: std::path::PathBuf| {
path.is_absolute() && std::fs::metadata(path).is_ok_and(|meta| meta.file_type().is_socket())
};
let private = std::env::var_os("XDG_RUNTIME_DIR")
.map(|dir| std::path::PathBuf::from(dir).join("systemd/private"));
let bus = std::env::var("DBUS_SESSION_BUS_ADDRESS")
.ok()
.and_then(|address| {
address
.strip_prefix("unix:path=")
.map(std::path::PathBuf::from)
});
private.into_iter().chain(bus).any(is_socket)
}

#[cfg(not(unix))]
fn manager_socket_exists() -> bool {
false
}

#[cfg(test)]
#[path = "scope_tests.rs"]
mod tests;
51 changes: 51 additions & 0 deletions crates/tinybox-linux/src/probe/scope_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//! The scope probe claims a limit only for a delegated controller.

use super::*;

#[test]
fn each_limit_follows_its_own_delegated_controller() {
let probe = ScopeProbe::from_parts(true, Some("cpuset cpu io memory\n"));
let support = probe.constraint_support();

assert_eq!(support.enforcement(Constraint::Cpu), Enforcement::Enforced);
assert_eq!(
support.enforcement(Constraint::Memory),
Enforcement::Enforced
);
assert_eq!(
support.enforcement(Constraint::Pids),
Enforcement::Unsupported
);
}

#[test]
fn nothing_is_claimed_without_a_reachable_manager() {
let probe = ScopeProbe::from_parts(false, Some("cpu memory pids"));

assert_eq!(probe, ScopeProbe::default());
for constraint in [Constraint::Cpu, Constraint::Memory, Constraint::Pids] {
assert_eq!(
probe.constraint_support().enforcement(constraint),
Enforcement::Unsupported
);
}
}

#[test]
fn nothing_is_claimed_without_a_delegated_cgroup() {
// cgroup v1, or no user manager cgroup at all.
let probe = ScopeProbe::from_parts(true, None);

assert!(!probe.cpu && !probe.memory && !probe.pids);
assert_eq!(
probe.constraint_support().enforcement(Constraint::Memory),
Enforcement::Unsupported
);
}

#[test]
fn a_controller_name_must_match_exactly() {
// `cpuset` is not `cpu`.
let probe = ScopeProbe::from_parts(true, Some("cpuset"));
assert!(!probe.cpu);
}
27 changes: 24 additions & 3 deletions crates/tinybox-module/src/tinybus_module/native_jail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,14 +301,20 @@ fn unavailable() -> Error {
struct NativeResourceBackend {
expected: Mutex<Option<Vec<OsString>>>,
requested: Mutex<Option<tinybox_bus::NativeJailResourceRequest>>,
scope: tinybox_linux::ScopeProbe,
launch: Mutex<Option<tinybox_linux::NativeJailLaunch>>,
}

impl NativeResourceBackend {
fn new() -> Self {
Self::with_scope(tinybox_linux::ScopeProbe::detect())
}

fn with_scope(scope: tinybox_linux::ScopeProbe) -> Self {
Self {
expected: Mutex::new(None),
requested: Mutex::new(None),
scope,
launch: Mutex::new(None),
}
}
Expand Down Expand Up @@ -369,14 +375,16 @@ impl JailBackend for NativeResourceBackend {

fn constraint_support(&self) -> tinybox_core::ConstraintSupport {
use tinybox_core::{Constraint, ConstraintSupport, Enforcement};
let scope = self.scope.constraint_support();
ConstraintSupport::NONE
.with(Constraint::Filesystem, Enforcement::Enforced)
.with(Constraint::Network, Enforcement::Enforced)
// `prepare_with_resources` refuses a request that denies them.
.with(Constraint::Subprocess, Enforcement::Unsupported)
.with(Constraint::Cpu, Enforcement::Enforced)
.with(Constraint::Memory, Enforcement::Enforced)
.with(Constraint::Pids, Enforcement::Enforced)
// Only what the user manager was delegated a controller for.
.with(Constraint::Cpu, scope.enforcement(Constraint::Cpu))
.with(Constraint::Memory, scope.enforcement(Constraint::Memory))
.with(Constraint::Pids, scope.enforcement(Constraint::Pids))
.with(Constraint::Disk, Enforcement::Unsupported)
}

Expand Down Expand Up @@ -439,6 +447,19 @@ fn prepare_with_resources(
{
return Err(invalid());
}
// A limit the scope cannot apply is refused, never dropped.
let support = backend.constraint_support();
if [
(resources.cpu_millis, tinybox_core::Constraint::Cpu),
(resources.memory_bytes, tinybox_core::Constraint::Memory),
(resources.pids_max, tinybox_core::Constraint::Pids),
]
.into_iter()
.any(|(limit, constraint)| {
limit.is_some() && support.enforcement(constraint) != tinybox_core::Enforcement::Enforced
}) {
return Err(unavailable());
}
backend.bind_resources(resources)?;
let jail = prepare(root, &request.jail, command, backend)?;
if request.jail.allow_net || !request.jail.allow_subprocess {
Expand Down
41 changes: 41 additions & 0 deletions crates/tinybox-module/src/tinybus_module/native_jail_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,47 @@ fn requested_native_resource_limits_are_refused_before_spawn()
Ok(())
}

#[test]
fn a_limit_whose_controller_is_not_delegated_is_refused_not_dropped()
-> std::result::Result<(), Box<dyn std::error::Error>> {
// A user manager that was delegated cpu and memory, but not pids.
let scope = tinybox_linux::ScopeProbe::from_parts(true, Some("cpu memory"));
let backend = super::NativeResourceBackend::with_scope(scope);
let support = backend.constraint_support();
assert_eq!(
support.enforcement(tinybox_core::Constraint::Cpu),
tinybox_core::Enforcement::Enforced
);
assert_eq!(
support.enforcement(tinybox_core::Constraint::Pids),
tinybox_core::Enforcement::Unsupported
);

let root = tempfile::tempdir()?;
let mut command = tinybox_core::ExecRequest::new(["target-program"]);
command.cwd = Some(root.path().into());
let original = command.clone();
// Everything else about the request is acceptable, so only the missing
// controller can refuse it.
let mut jail = config();
jail.allow_subprocess = true;
let request = NativeJailRequest {
jail,
resources: Some(tinybox_bus::NativeJailResourceRequest {
cpu_millis: Some(1000),
memory_bytes: None,
pids_max: Some(16),
disk_bytes: None,
}),
};
let error = prepare_with_resources(root.path(), &request, &mut command, &backend)
.err()
.ok_or("a pids limit with no pids controller must be refused")?;
assert_eq!(error.wire_name(), tinybox_bus::UNSUPPORTED_OPERATION);
assert_eq!(command.argv, original.argv);
Ok(())
}

#[cfg(not(target_os = "linux"))]
#[test]
fn contract_21_resource_requests_refuse_before_spawn_on_non_linux()
Expand Down
7 changes: 5 additions & 2 deletions docs/specs/constraint-preflight.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ coarse lifecycle capabilities do not imply resource enforcement.
| --- | --- | --- | --- | --- | --- | --- | --- |
| Docker | Enforced | Enforced | Enforced | Enforced | Enforced | Unsupported | Unsupported |
| Namespaces | Enforced | Enforced | Enforced with cgroups | Enforced with cgroups | Enforced with cgroups | Unsupported | Unsupported |
| Native resource jail (contract 2.1) | Enforced | Enforced | Enforced | Enforced | Enforced | Unsupported | Unsupported |
| Native resource jail (contract 2.1) | Enforced | Enforced | Enforced when delegated | Enforced when delegated | Enforced when delegated | Unsupported | Unsupported |
| MicroVM | Enforced | Enforced | BestEffort | BestEffort | Unsupported | Unsupported | Unsupported |
| Landlock | Enforced when full filesystem ABI available | Unsupported | Unsupported | Unsupported | Unsupported | Unsupported | Unsupported |
| Seatbelt | BestEffort when present | BestEffort when present | Unsupported | Unsupported | Unsupported | Unsupported | BestEffort when present |
| Noop / unavailable platform | Unsupported | Unsupported | Unsupported | Unsupported | Unsupported | Unsupported | Unsupported |

The native resource jail allows subprocesses always, and claims kernel
The native resource jail claims CPU, memory or pids only when the user's
systemd manager is reachable and was delegated that cgroup controller
(`tinybox_linux::ScopeProbe`); a requested limit it cannot apply is refused.
It allows subprocesses always, and claims kernel
isolation only when its `bwrap` supports `--disable-userns`. The MicroVM has no
network interface, so it refuses any egress.

Expand Down
Loading