Skip to content
Open
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
49 changes: 28 additions & 21 deletions crates/openshell-sandbox/src/linux/child_seccomp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,9 @@ pub fn mark_inherited_descriptors_close_on_exec() -> io::Result<()> {
/// `rt_tgsigqueueinfo`: Linux accepts nonleader TIDs for these operations, so a
/// static TGID comparison alone cannot protect future sandbox worker threads.
/// The same comparison covers the trusted leader as the target of `prlimit64`,
/// the `sched_set*` calls, and `setpriority`, which are otherwise mediated by
/// the broker so the workload can still reconfigure its own thread group.
/// the `sched_set*` calls, `setpriority`, and `ioprio_set`, which are otherwise
/// mediated by the broker so the workload can still reconfigure its own thread
/// group.
///
/// The child must inherit [`install_workload_listener`]: this program alone
/// lets those calls reach any other same-UID task. A child without that
Expand All @@ -149,7 +150,7 @@ pub fn prepare(sandbox_tgid: u32) -> io::Result<ChildHardeningProgram> {
/// Nothing can check a named scheduling, priority, or resource-limit target
/// for such a child, so the program decides those calls alone: `prlimit64` and
/// the `sched_set*` calls are limited to `pid == 0`, the caller itself, and
/// `setpriority` is denied.
/// `setpriority` and `ioprio_set` are denied.
pub fn prepare_without_broker(sandbox_tgid: u32) -> io::Result<ChildHardeningProgram> {
build(sandbox_tgid, Mediation::Unbrokered)
}
Expand Down Expand Up @@ -223,7 +224,6 @@ fn build(sandbox_tgid: u32, mediation: Mediation) -> io::Result<ChildHardeningPr
libc::SYS_setgroups,
libc::SYS_sethostname,
libc::SYS_setdomainname,
libc::SYS_ioprio_set,
] {
append_unconditional_deny(&mut instructions, syscall)?;
}
Expand All @@ -248,7 +248,7 @@ fn build(sandbox_tgid: u32, mediation: Mediation) -> io::Result<ChildHardeningPr
// The broker-mediated scheduling, priority, and resource-limit calls are
// still denied against the trusted leader as defense in depth. Their PID
// position comes from the shared table, so `setpriority(which, who, nice)`
// correctly compares argument 1 rather than the `PRIO_*` selector. Without
// correctly compares argument 1 rather than the `which` selector. Without
// a broker only `pid == 0` passes, and a call that also needs a selector
// to name the caller is denied outright.
for mediated in crate::linux::process_sched::MEDIATED_PROCESS_SCHED_SYSCALLS {
Expand Down Expand Up @@ -555,19 +555,23 @@ mod tests {
{
unsafe { libc::_exit(11) };
}
// `setpriority` carries its PID in argument 1, so the trusted
// leader deny is pinned separately from the argument-0 calls.
if unsafe {
libc::syscall(
// `setpriority` and `ioprio_set` carry their PID in argument 1, so
// the trusted leader deny is pinned separately from the argument-0
// calls.
for (syscall, which, exit_code) in [
(
libc::SYS_setpriority,
libc::c_long::from(libc::PRIO_PROCESS),
libc::c_long::from(sandbox_tgid),
0,
)
} != -1
|| io::Error::last_os_error().raw_os_error() != Some(libc::EPERM)
{
unsafe { libc::_exit(12) };
12,
),
(libc::SYS_ioprio_set, 1, 13),
] {
if unsafe { libc::syscall(syscall, which, libc::c_long::from(sandbox_tgid), 0) }
!= -1
|| io::Error::last_os_error().raw_os_error() != Some(libc::EPERM)
{
unsafe { libc::_exit(exit_code) };
}
}
if unsafe { libc::fcntl(libc::STDIN_FILENO, libc::F_SETOWN, sandbox_tgid) } != -1
|| io::Error::last_os_error().raw_os_error() != Some(libc::EPERM)
Expand Down Expand Up @@ -661,7 +665,8 @@ mod tests {
}

/// Without a broker the program alone narrows the target: only `pid == 0`
/// passes, even for the child's own PID, and `setpriority` is denied.
/// passes, even for the child's own PID, and `setpriority` and `ioprio_set`
/// are denied.
#[test]
fn filter_without_broker_limits_scheduling_and_limit_calls_to_pid_zero() {
// SAFETY: the child uses only raw syscalls after fork and exits with
Expand Down Expand Up @@ -714,10 +719,12 @@ mod tests {
{
libc::_exit(6);
}
if libc::syscall(libc::SYS_setpriority, 0, 0, 0) != -1
|| io::Error::last_os_error().raw_os_error() != Some(libc::EPERM)
{
libc::_exit(7);
for syscall in [libc::SYS_setpriority, libc::SYS_ioprio_set] {
if libc::syscall(syscall, 0, 0, 0) != -1
|| io::Error::last_os_error().raw_os_error() != Some(libc::EPERM)
{
libc::_exit(7);
}
}
}
unsafe { libc::_exit(0) };
Expand Down
110 changes: 73 additions & 37 deletions crates/openshell-sandbox/src/linux/process_sched.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
//! `SA_RESTART` that fires during a named-target call.
//!
//! This mediation continues an inspected numeric target. Linux offers no
//! pidfd-equivalent for `sched_*`, `prlimit64`, or `setpriority`, so the broker
//! resolves the target through procfs and continues the syscall with that
//! numeric TID. A PID reused between the resolution and kernel execution could
//! therefore redirect the call onto a foreign task of the same UID. The
//! deviation is accepted: it is the same risk class as the `tkill`
//! pidfd-equivalent for `sched_*`, `prlimit64`, `setpriority`, or `ioprio_set`,
//! so the broker resolves the target through procfs and continues the syscall
//! with that numeric TID. A PID reused between the resolution and kernel
//! execution could therefore redirect the call onto a foreign task of the same
//! UID. The deviation is accepted: it is the same risk class as the `tkill`
//! resolve-then-continue precedent in `process_signal`, the trusted sandbox
//! leader is a live PID the static `sandbox_tgid` equal-deny refuses, and the
//! window is a single broker round trip.
Expand All @@ -42,23 +42,36 @@ use std::io;
use crate::linux::proc_status::thread_group_id;
use crate::linux::seccomp_notify::{Notification, NotificationListener, scalar_int};

/// `setpriority` selectors from the Linux UAPI.
/// `setpriority` and `ioprio_set` selectors from the Linux UAPI.
///
/// libc types these constants differently per C library (`c_uint` on glibc,
/// `c_int` on musl), so the decoded selector is compared against local values.
/// libc types the `PRIO_*` constants differently per C library (`c_uint` on
/// glibc, `c_int` on musl), and exports no `IOPRIO_WHO_*`, so the decoded
/// selector is compared against local values.
const PRIO_PROCESS: u32 = 0;
const PRIO_PGRP: u32 = 1;
const PRIO_USER: u32 = 2;
const IOPRIO_WHO_PROCESS: u32 = 1;
const IOPRIO_WHO_PGRP: u32 = 2;
const IOPRIO_WHO_USER: u32 = 3;

/// The `which` argument that decides what a PID argument of zero names.
#[derive(Clone, Copy)]
struct Selector {
argument: u8,
/// Value that selects a single process, the only target the broker allows.
process: u32,
/// Values that select a group or a user, which stay denied.
group_or_user: [u32; 2],
}

/// One scheduling, priority, or resource-limit syscall mediated by the broker.
pub(crate) struct MediatedSyscall {
/// Native syscall number.
pub(crate) number: i64,
/// Argument that carries the target PID.
pub(crate) pid_argument: u8,
/// `(argument, value)` that must also hold before a zero PID names only
/// the caller.
selector: Option<(u8, u32)>,
/// Selector that must also hold before a zero PID names only the caller.
selector: Option<Selector>,
}

impl MediatedSyscall {
Expand All @@ -70,13 +83,22 @@ impl MediatedSyscall {
}
}

const fn with_selector(number: i64, selector: Selector) -> Self {
Self {
number,
pid_argument: 1,
selector: Some(selector),
}
}

/// `(argument, value)` pairs that all hold exactly when the call can reach
/// only the calling thread or process.
///
/// The listener filter runs such a call natively. Every value is an `int`
/// the kernel truncates, so only the low 32 bits of each argument count.
pub(crate) fn caller_only_arguments(&self) -> impl Iterator<Item = (u8, u32)> {
self.selector
.map(|selector| (selector.argument, selector.process))
.into_iter()
.chain(std::iter::once((self.pid_argument, 0)))
}
Expand All @@ -89,12 +111,12 @@ impl MediatedSyscall {

/// Scheduling, priority, and resource-limit syscalls mediated by the broker.
///
/// `setpriority(which, who, nice)` is the exception to the PID position: its
/// target is argument 1, and `who == 0` names the caller only under
/// `PRIO_PROCESS`. The notified set, its native `pid == 0` path, the broker
/// dispatch, the static child filter, and the decode below all derive from
/// this table, so adding a syscall is a one-place edit and the target position
/// cannot diverge between them.
/// `setpriority(which, who, nice)` and `ioprio_set(which, who, ioprio)` are the
/// exception to the PID position: their target is argument 1, and `who == 0`
/// names the caller only under the single-process selector. The notified set,
/// its native `pid == 0` path, the broker dispatch, the static child filter,
/// and the decode below all derive from this table, so adding a syscall is a
/// one-place edit and the target position cannot diverge between them.
///
/// The native `pid == 0` path is policy, not only decode: the listener filter
/// allows a caller-only call before the broker sees it. A future broker-side
Expand All @@ -105,11 +127,22 @@ pub(crate) const MEDIATED_PROCESS_SCHED_SYSCALLS: &[MediatedSyscall] = &[
MediatedSyscall::new(libc::SYS_sched_setattr, 0),
MediatedSyscall::new(libc::SYS_sched_setparam, 0),
MediatedSyscall::new(libc::SYS_sched_setscheduler, 0),
MediatedSyscall {
number: libc::SYS_setpriority,
pid_argument: 1,
selector: Some((0, PRIO_PROCESS)),
},
MediatedSyscall::with_selector(
libc::SYS_setpriority,
Selector {
argument: 0,
process: PRIO_PROCESS,
group_or_user: [PRIO_PGRP, PRIO_USER],
},
),
MediatedSyscall::with_selector(
libc::SYS_ioprio_set,
Selector {
argument: 0,
process: IOPRIO_WHO_PROCESS,
group_or_user: [IOPRIO_WHO_PGRP, IOPRIO_WHO_USER],
},
),
];

/// Return the table entry for one mediated syscall number.
Expand All @@ -126,18 +159,19 @@ pub(crate) fn is_mediated(syscall: i64) -> bool {

/// Continue one scheduling, priority, or resource-limit notification.
///
/// `pid == 0`, and `setpriority(PRIO_PROCESS, 0, ...)`, names the caller. The
/// `pid == 0`, and `setpriority(PRIO_PROCESS, 0, ...)` or
/// `ioprio_set(IOPRIO_WHO_PROCESS, 0, ...)`, names the caller. The
/// listener filter runs those calls natively, so they are not expected here;
/// one that arrives anyway is allowed without reading a thread group, since
/// the comparison is vacuous. A named target is resolved through procfs and
/// must share the caller's thread group.
///
/// `setpriority` accepts only `PRIO_PROCESS`. `PRIO_USER` with `who = 0` would
/// touch every process of the shared workload UID, and `PRIO_PGRP` with
/// `who = 0` names a process group an untrusted child could rejoin before
/// calling, exactly as the static kill pid-zero deny reasons. Both stay denied
/// with EPERM, while a selector the kernel does not define keeps the native
/// EINVAL.
/// `setpriority` and `ioprio_set` accept only the single-process selector. A
/// user selector with `who = 0` would touch every process of the shared
/// workload UID, and a group selector with `who = 0` names a process group an
/// untrusted child could rejoin before calling, exactly as the static kill
/// pid-zero deny reasons. Both stay denied with EPERM, while a selector the
/// kernel does not define keeps the native EINVAL.
///
/// A negative target keeps the errno its syscall returns natively: the
/// `sched_setparam`/`sched_setscheduler`/`sched_setattr` trio rejects it with
Expand All @@ -156,13 +190,13 @@ pub fn mediate_process_sched(
let syscall = i64::from(notification.syscall);
let mediated =
mediated_syscall(syscall).ok_or_else(|| io::Error::from_raw_os_error(libc::ENOSYS))?;
if syscall == libc::SYS_setpriority {
match scalar_int(notification.args[0]).cast_unsigned() {
PRIO_PROCESS => {}
PRIO_PGRP | PRIO_USER => {
return Err(io::Error::from_raw_os_error(libc::EPERM));
}
_ => return Err(io::Error::from_raw_os_error(libc::EINVAL)),
if let Some(selector) = mediated.selector {
let which = scalar_int(notification.args[usize::from(selector.argument)]).cast_unsigned();
if selector.group_or_user.contains(&which) {
return Err(io::Error::from_raw_os_error(libc::EPERM));
}
if which != selector.process {
return Err(io::Error::from_raw_os_error(libc::EINVAL));
}
}
let target = scalar_int(notification.args[usize::from(mediated.pid_argument)]);
Expand Down Expand Up @@ -237,11 +271,13 @@ mod tests {
}

#[test]
fn only_setpriority_needs_a_selector_to_name_the_caller() {
fn only_the_priority_calls_need_a_selector_to_name_the_caller() {
for mediated in MEDIATED_PROCESS_SCHED_SYSCALLS {
let arguments: Vec<_> = mediated.caller_only_arguments().collect();
if mediated.number == libc::SYS_setpriority {
assert_eq!(arguments, [(0, PRIO_PROCESS), (1, 0)]);
} else if mediated.number == libc::SYS_ioprio_set {
assert_eq!(arguments, [(0, IOPRIO_WHO_PROCESS), (1, 0)]);
} else {
assert_eq!(arguments, [(0, 0)]);
}
Expand Down
10 changes: 5 additions & 5 deletions crates/openshell-sandbox/src/linux/seccomp_notify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,11 @@ pub fn install_listener(syscalls: &[i64]) -> io::Result<NotificationListener> {
/// reconfigure an INET endpoint, and every scheduling, priority, and
/// resource-limit call whose target must be checked against the caller's
/// thread group. The same calls with a caller-only target (`pid == 0`, and
/// `PRIO_PROCESS` for `setpriority`) run natively. Connected
/// `send()`/null-destination
/// `sendto()` retains the audited cBPF fast path. `accept`/`accept4` run
/// natively: the broker binds every INET socket to loopback before injecting
/// it, and accepted sockets inherit their listener's binding.
/// the single-process selector for `setpriority` and `ioprio_set`) run
/// natively. Connected `send()`/null-destination `sendto()` retains the audited
/// cBPF fast path. `accept`/`accept4` run natively: the broker binds every INET
/// socket to loopback before injecting it, and accepted sockets inherit their
/// listener's binding.
pub fn install_workload_listener() -> io::Result<NotificationListener> {
#[allow(unused_mut)] // SYS_open is unavailable on some architectures.
let mut syscalls = vec![
Expand Down
69 changes: 69 additions & 0 deletions crates/openshell-sandbox/src/network_broker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4496,6 +4496,75 @@ mod tests {
assert_eq!(user, Some(libc::EPERM));
}

const IOPRIO_WHO_PROCESS: libc::c_long = 1;
const IOPRIO_WHO_PGRP: libc::c_long = 2;
const IOPRIO_WHO_USER: libc::c_long = 3;

/// Read one task's I/O priority and re-apply it through `ioprio_set`.
///
/// Re-applying the current value needs no capability, so the result
/// reports only whether mediation let the call through.
fn reapply_ioprio(who: libc::c_long) -> Option<i32> {
// SAFETY: both calls take scalar arguments only.
unsafe {
let ioprio = libc::syscall(libc::SYS_ioprio_get, IOPRIO_WHO_PROCESS, who);
assert!(ioprio >= 0, "ioprio_get: {}", io::Error::last_os_error());
errno(libc::syscall(
libc::SYS_ioprio_set,
IOPRIO_WHO_PROCESS,
who,
ioprio,
))
}
}

#[test]
fn ioprio_set_allows_the_callers_group() {
let results = mediated(|| {
// SAFETY: gettid and getpid take no arguments.
let (tid, pid) = unsafe {
(
libc::syscall(libc::SYS_gettid),
libc::c_long::from(libc::getpid()),
)
};
(reapply_ioprio(0), reapply_ioprio(pid), reapply_ioprio(tid))
});
assert_eq!(results, (None, None, None));
}

#[test]
fn ioprio_set_denies_other_groups_and_group_selectors() {
let mut target = std::process::Command::new("sleep")
.arg("30")
.spawn()
.expect("spawn target");
let target_tgid = libc::c_long::from(target.id());
let (process, group, user, unknown) = mediated(move || {
// SAFETY: scalar arguments only; the broker decides before the
// kernel resolves any of the targets.
unsafe {
(
errno(libc::syscall(
libc::SYS_ioprio_set,
IOPRIO_WHO_PROCESS,
target_tgid,
0,
)),
errno(libc::syscall(libc::SYS_ioprio_set, IOPRIO_WHO_PGRP, 0, 0)),
errno(libc::syscall(libc::SYS_ioprio_set, IOPRIO_WHO_USER, 0, 0)),
errno(libc::syscall(libc::SYS_ioprio_set, 42, 0, 0)),
)
}
});
let _ = target.kill();
let _ = target.wait();
assert_eq!(process, Some(libc::EPERM));
assert_eq!(group, Some(libc::EPERM));
assert_eq!(user, Some(libc::EPERM));
assert_eq!(unknown, Some(libc::EINVAL));
}

/// Invalid `setpriority` arguments keep the errno the kernel returns.
///
/// An unknown selector is a usage error the kernel rejects with EINVAL, and
Expand Down
Loading
Loading