diff --git a/crates/openshell-sandbox/README.md b/crates/openshell-sandbox/README.md index e365721b8e..1f2bce2033 100644 --- a/crates/openshell-sandbox/README.md +++ b/crates/openshell-sandbox/README.md @@ -2,8 +2,9 @@ `openshell-sandbox` owns the Linux workload isolation mechanisms and their kernel qualification probes. The `linux` module contains seccomp notifications and child -filters, task-memory access, process-signal mediation, procfs descriptor lookup, -socket identity, Landlock qualification, and the workload launch thread. +filters, task-memory access, procfs thread-group identity, process-signal and +scheduling/limit mediation, procfs descriptor lookup, socket identity, Landlock +qualification, and the workload launch thread. `sandbox::linux` composes policy enforcement. The top-level `linux` module owns the lower-level mechanisms used by that orchestration and the boundary server. diff --git a/crates/openshell-sandbox/src/linux/child_seccomp.rs b/crates/openshell-sandbox/src/linux/child_seccomp.rs index 6fc9f67f73..1d37444125 100644 --- a/crates/openshell-sandbox/src/linux/child_seccomp.rs +++ b/crates/openshell-sandbox/src/linux/child_seccomp.rs @@ -3,10 +3,13 @@ //! Prepared seccomp self-protection for same-UID workload children. //! -//! The program is built before `fork` and installed by the workload launcher -//! after the child has inherited the network user-notification filter. It does -//! not allocate while installing and deliberately leaves mediated networking -//! syscalls alone so the older `USER_NOTIF` action can still win. +//! The program is built before `fork` and installed in the child after it has +//! inherited a user-notification filter. It does not allocate while +//! installing. The brokered program (`prepare`) deliberately leaves mediated +//! networking syscalls alone so the older `USER_NOTIF` action can still win, +//! and does the same for scheduling and limit calls except those that name the +//! trusted leader. The program for a child without a broker +//! (`prepare_without_broker`) limits those calls to the caller itself. #![allow(unsafe_code)] @@ -128,7 +131,40 @@ pub fn mark_inherited_descriptors_close_on_exec() -> io::Result<()> { /// `rt_sigqueueinfo`, and `SIGCONT` sent with `tgkill` or /// `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 child must inherit [`install_workload_listener`]: this program alone +/// lets those calls reach any other same-UID task. A child without that +/// listener takes [`prepare_without_broker`] instead. +/// +/// [`install_workload_listener`]: crate::linux::seccomp_notify::install_workload_listener pub fn prepare(sandbox_tgid: u32) -> io::Result { + build(sandbox_tgid, Mediation::Brokered) +} + +/// Build the self-protection program for a child no broker mediates. +/// +/// 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. +pub fn prepare_without_broker(sandbox_tgid: u32) -> io::Result { + build(sandbox_tgid, Mediation::Unbrokered) +} + +/// Whether a broker behind the inherited workload listener decides the calls +/// this program cannot narrow alone. +#[derive(Clone, Copy)] +enum Mediation { + /// The child inherits the workload listener. + Brokered, + /// No broker: this program decides alone. + Unbrokered, +} + +fn build(sandbox_tgid: u32, mediation: Mediation) -> io::Result { if sandbox_tgid == 0 { return Err(io::Error::new( io::ErrorKind::InvalidInput, @@ -187,7 +223,6 @@ pub fn prepare(sandbox_tgid: u32) -> io::Result { libc::SYS_setgroups, libc::SYS_sethostname, libc::SYS_setdomainname, - libc::SYS_setpriority, libc::SYS_ioprio_set, ] { append_unconditional_deny(&mut instructions, syscall)?; @@ -210,6 +245,31 @@ pub fn prepare(sandbox_tgid: u32) -> io::Result { ] { append_argument_equal_deny(&mut instructions, syscall, argument, sandbox_tgid)?; } + // 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 + // 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 { + let argument = u32::from(mediated.pid_argument); + match mediation { + Mediation::Brokered => { + append_argument_equal_deny( + &mut instructions, + mediated.number, + argument, + sandbox_tgid, + )?; + } + Mediation::Unbrokered if mediated.has_selector() => { + append_unconditional_deny(&mut instructions, mediated.number)?; + } + Mediation::Unbrokered => { + append_argument_nonzero_deny(&mut instructions, mediated.number, argument)?; + } + } + } for syscall in [libc::SYS_kill, libc::SYS_rt_sigqueueinfo] { // PID zero targets the caller's process group. Deny it even though // OpenShell normally gives each workload a dedicated process group: @@ -222,16 +282,6 @@ pub fn prepare(sandbox_tgid: u32) -> io::Result { append_argument_masked_deny(&mut instructions, libc::SYS_kill, 0, 1 << 31)?; append_argument_masked_deny(&mut instructions, libc::SYS_rt_sigqueueinfo, 0, 1 << 31)?; - for syscall in [ - libc::SYS_prlimit64, - libc::SYS_sched_setaffinity, - libc::SYS_sched_setattr, - libc::SYS_sched_setparam, - libc::SYS_sched_setscheduler, - ] { - append_argument_nonzero_deny(&mut instructions, syscall, 0)?; - } - // The ordinary workload filter is installed after this program and owns // the final ban on further seccomp installation. Blocking it here would // prevent the sandbox from completing the prepared filter stack. @@ -505,6 +555,20 @@ 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( + 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) }; + } if unsafe { libc::fcntl(libc::STDIN_FILENO, libc::F_SETOWN, sandbox_tgid) } != -1 || io::Error::last_os_error().raw_os_error() != Some(libc::EPERM) { @@ -532,6 +596,140 @@ mod tests { assert_eq!(libc::WEXITSTATUS(status), 0); } + #[test] + fn filter_allows_own_group_scheduling_and_limit_calls() { + // SAFETY: the child uses only raw syscalls after fork and exits with + // `_exit`, so it does not run copied Rust cleanup state. + let child = unsafe { libc::fork() }; + assert!(child >= 0, "fork: {}", io::Error::last_os_error()); + if child == 0 { + let sandbox_tgid = unsafe { libc::getppid() }; + let Ok(mut filter) = prepare(u32::try_from(sandbox_tgid).unwrap_or(0)) else { + unsafe { libc::_exit(1) }; + }; + if filter.install().is_err() { + unsafe { libc::_exit(2) }; + } + // SAFETY: every call names this child's own thread group or + // re-applies its current settings, so the kernel performs it. + unsafe { + let mut mask: libc::cpu_set_t = std::mem::zeroed(); + let size = size_of::(); + if libc::sched_getaffinity(0, size, &raw mut mask) != 0 + || libc::sched_setaffinity(libc::getpid(), size, &raw const mask) != 0 + { + libc::_exit(3); + } + // `getpriority` returns -1 both for a valid nice value and on + // error, so a failed read must not be fed back into + // `setpriority` where it would look like a filter regression. + *libc::__errno_location() = 0; + let nice = libc::getpriority(libc::PRIO_PROCESS, 0); + if nice == -1 && *libc::__errno_location() != 0 { + libc::_exit(7); + } + if libc::setpriority(libc::PRIO_PROCESS, libc::getpid().cast_unsigned(), nice) != 0 + { + libc::_exit(4); + } + let mut limits = [0_u64; 2]; + if libc::syscall( + libc::SYS_prlimit64, + libc::getpid(), + libc::c_long::from(libc::RLIMIT_CORE), + std::ptr::null::(), + limits.as_mut_ptr(), + ) != 0 + { + libc::_exit(5); + } + let mut param: libc::sched_param = std::mem::zeroed(); + if libc::sched_getparam(0, &raw mut param) != 0 + || libc::sched_setparam(libc::getpid(), &raw const param) != 0 + { + libc::_exit(6); + } + } + unsafe { libc::_exit(0) }; + } + + let mut status = 0; + // SAFETY: `child` names our live direct child and status is writable. + assert_eq!(unsafe { libc::waitpid(child, &raw mut status, 0) }, child); + assert!(libc::WIFEXITED(status)); + assert_eq!(libc::WEXITSTATUS(status), 0); + } + + /// Without a broker the program alone narrows the target: only `pid == 0` + /// passes, even for the child's own PID, and `setpriority` is 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 + // `_exit`, so it does not run copied Rust cleanup state. + let child = unsafe { libc::fork() }; + assert!(child >= 0, "fork: {}", io::Error::last_os_error()); + if child == 0 { + let sandbox_tgid = unsafe { libc::getppid() }; + let Ok(mut filter) = prepare_without_broker(u32::try_from(sandbox_tgid).unwrap_or(0)) + else { + unsafe { libc::_exit(1) }; + }; + if filter.install().is_err() { + unsafe { libc::_exit(2) }; + } + // SAFETY: the mask and limit buffers are live for each call, which + // re-applies or reads this child's own settings. + unsafe { + let mut mask: libc::cpu_set_t = std::mem::zeroed(); + let size = size_of::(); + if libc::sched_getaffinity(0, size, &raw mut mask) != 0 + || libc::sched_setaffinity(0, size, &raw const mask) != 0 + { + libc::_exit(3); + } + if libc::sched_setaffinity(libc::getpid(), size, &raw const mask) != -1 + || io::Error::last_os_error().raw_os_error() != Some(libc::EPERM) + { + libc::_exit(4); + } + let mut limits = [0_u64; 2]; + if libc::syscall( + libc::SYS_prlimit64, + 0, + libc::c_long::from(libc::RLIMIT_CORE), + std::ptr::null::(), + limits.as_mut_ptr(), + ) != 0 + { + libc::_exit(5); + } + if libc::syscall( + libc::SYS_prlimit64, + libc::getpid(), + libc::c_long::from(libc::RLIMIT_CORE), + std::ptr::null::(), + limits.as_mut_ptr(), + ) != -1 + || io::Error::last_os_error().raw_os_error() != Some(libc::EPERM) + { + 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); + } + } + unsafe { libc::_exit(0) }; + } + + let mut status = 0; + // SAFETY: `child` names our live direct child and status is writable. + assert_eq!(unsafe { libc::waitpid(child, &raw mut status, 0) }, child); + assert!(libc::WIFEXITED(status)); + assert_eq!(libc::WEXITSTATUS(status), 0); + } + #[test] fn filter_preserves_thread_and_process_creation() { if std::env::var_os("OPENSHELL_CHILD_SECCOMP_CREATION_PROBE").is_some() { diff --git a/crates/openshell-sandbox/src/linux/mod.rs b/crates/openshell-sandbox/src/linux/mod.rs index a15b8b6f43..70b4bf62b9 100644 --- a/crates/openshell-sandbox/src/linux/mod.rs +++ b/crates/openshell-sandbox/src/linux/mod.rs @@ -9,6 +9,8 @@ pub mod child_seccomp; pub mod landlock; pub mod proc_fd; +pub mod proc_status; +pub mod process_sched; pub mod process_signal; pub mod seccomp_notify; pub mod socket_confinement; diff --git a/crates/openshell-sandbox/src/linux/proc_status.rs b/crates/openshell-sandbox/src/linux/proc_status.rs new file mode 100644 index 0000000000..1be8830545 --- /dev/null +++ b/crates/openshell-sandbox/src/linux/proc_status.rs @@ -0,0 +1,32 @@ +// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +//! Strict `/proc//status` identity lookups. +//! +//! Signal and scheduling mediation both need the thread group a numeric TID +//! belongs to. The status file names it without following a path the workload +//! can replace. A missing file is reported as ESRCH so callers keep the native +//! "no such process" result for a target that has already exited. + +use std::io; + +/// Resolve the thread group ID that owns `tid`. +/// +/// Returns ESRCH when the task is gone or its status file carries no usable +/// `Tgid:` line, since either case means no live thread group answers for it. +pub(crate) fn thread_group_id(tid: u32) -> io::Result { + let status = std::fs::read_to_string(format!("/proc/{tid}/status")).map_err(|error| { + if error.kind() == io::ErrorKind::NotFound { + io::Error::from_raw_os_error(libc::ESRCH) + } else { + error + } + })?; + status + .lines() + .find_map(|line| { + line.strip_prefix("Tgid:") + .and_then(|value| value.trim().parse::().ok()) + }) + .ok_or_else(|| io::Error::from_raw_os_error(libc::ESRCH)) +} diff --git a/crates/openshell-sandbox/src/linux/process_sched.rs b/crates/openshell-sandbox/src/linux/process_sched.rs new file mode 100644 index 0000000000..f6fdc5aba4 --- /dev/null +++ b/crates/openshell-sandbox/src/linux/process_sched.rs @@ -0,0 +1,250 @@ +// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +//! Broker mediation of scheduling, priority, and resource-limit syscalls. +//! +//! A cBPF filter cannot compare an argument against the caller's own thread +//! group, and `SECCOMP_RET_ERRNO` outranks `SECCOMP_USER_NOTIF`, so an errno +//! deny for these syscalls would bypass the broker instead of narrowing the +//! target. A call that names a target is therefore notified and decided here: +//! the kernel performs the operation when it names the caller's own thread +//! group and it is denied with EPERM otherwise. Continuing matches native +//! semantics, where one workload thread may legitimately reconfigure its own +//! affinity, parameters, scheduler, nice value, or limits, and may do the same +//! for a sibling thread or process in its group. +//! +//! A call with `pid == 0` names only the caller, which cBPF can check, so the +//! listener filter runs it natively and never notifies it (see +//! [`MediatedSyscall::caller_only_arguments`]). Those calls keep the +//! non-interruptible behavior they have without mediation, and the +//! `prlimit64(0, ...)` every workload and pty spawn issues before `exec` does +//! not wait on the broker. +//! +//! A named target waits in the same user-notification round trip as `openat`, +//! `connect`, and `sendto`. A signal handled without `SA_RESTART` can therefore +//! surface as EINTR from a call POSIX defines as non-interruptible, and with +//! `SA_RESTART` the call restarts and re-notifies. The broker answers in +//! microseconds, so the practical exposure is a handler installed without +//! `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` +//! 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. + +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. +/// +/// 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. +const PRIO_PROCESS: u32 = 0; +const PRIO_PGRP: u32 = 1; +const PRIO_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)>, +} + +impl MediatedSyscall { + const fn new(number: i64, pid_argument: u8) -> Self { + Self { + number, + pid_argument, + selector: None, + } + } + + /// `(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 { + self.selector + .into_iter() + .chain(std::iter::once((self.pid_argument, 0))) + } + + /// Whether a zero PID needs a selector argument to name only the caller. + pub(crate) const fn has_selector(&self) -> bool { + self.selector.is_some() + } +} + +/// 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. +/// +/// 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 +/// limit on self-directed calls has to drop that path for its entry too. +pub(crate) const MEDIATED_PROCESS_SCHED_SYSCALLS: &[MediatedSyscall] = &[ + MediatedSyscall::new(libc::SYS_prlimit64, 0), + MediatedSyscall::new(libc::SYS_sched_setaffinity, 0), + 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)), + }, +]; + +/// Return the table entry for one mediated syscall number. +pub(crate) fn mediated_syscall(syscall: i64) -> Option<&'static MediatedSyscall> { + MEDIATED_PROCESS_SCHED_SYSCALLS + .iter() + .find(|mediated| mediated.number == syscall) +} + +/// Whether the broker decides this syscall through [`mediate_process_sched`]. +pub(crate) fn is_mediated(syscall: i64) -> bool { + mediated_syscall(syscall).is_some() +} + +/// Continue one scheduling, priority, or resource-limit notification. +/// +/// `pid == 0`, and `setpriority(PRIO_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. +/// +/// A negative target keeps the errno its syscall returns natively: the +/// `sched_setparam`/`sched_setscheduler`/`sched_setattr` trio rejects it with +/// EINVAL before any lookup, while `setpriority`, `prlimit64`, and +/// `sched_setaffinity` fail their lookup with ESRCH. A target that no longer +/// exists keeps ESRCH too. Any other failure to identify the target, and any +/// failure to identify the caller, fails closed with EPERM. +/// +/// A syscall outside the mediated set cannot be decided here, so it fails +/// closed with ENOSYS as an unhandled notification. +pub fn mediate_process_sched( + listener: &NotificationListener, + notification: Notification, +) -> io::Result<()> { + listener.validate_id(notification.id)?; + 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)), + } + } + let target = scalar_int(notification.args[usize::from(mediated.pid_argument)]); + if target == 0 { + // The caller names itself, so no thread group has to be read. + return listener.respond_continue(notification.id); + } + if target < 0 { + return Err(io::Error::from_raw_os_error(negative_target_errno(syscall))); + } + let caller_group = resolve_caller_group(notification.tid)?; + if resolve_target_group(target.cast_unsigned())? != caller_group { + return Err(io::Error::from_raw_os_error(libc::EPERM)); + } + // Continuing an inspected numeric target has the reuse window documented + // in the module documentation. + listener.respond_continue(notification.id) +} + +/// Return the errno a negative target produces natively for one mediated +/// syscall. +/// +/// The `sched_setparam`, `sched_setscheduler`, and `sched_setattr` entry points +/// reject a negative PID with EINVAL before any process lookup, while the other +/// mediated syscalls reach their lookup and fail with ESRCH. Only a syscall from +/// [`MEDIATED_PROCESS_SCHED_SYSCALLS`] reaches this rule. +fn negative_target_errno(syscall: i64) -> i32 { + match syscall { + libc::SYS_sched_setparam | libc::SYS_sched_setscheduler | libc::SYS_sched_setattr => { + libc::EINVAL + } + _ => libc::ESRCH, + } +} + +/// Resolve the notifying thread's group, failing closed on any error. +/// +/// The caller is blocked in its notification, so a lookup failure never means +/// "no such target"; it means the comparison cannot be made. +fn resolve_caller_group(tid: u32) -> io::Result { + thread_group_id(tid).map_err(|_| io::Error::from_raw_os_error(libc::EPERM)) +} + +/// Resolve a named target's thread group, preserving ESRCH and nothing else. +fn resolve_target_group(tid: u32) -> io::Result { + thread_group_id(tid).map_err(|error| { + if error.raw_os_error() == Some(libc::ESRCH) { + error + } else { + io::Error::from_raw_os_error(libc::EPERM) + } + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + /// No task can hold this TID: it is above the kernel's `PID_MAX_LIMIT`. + const ABSENT_TID: u32 = u32::MAX; + + #[test] + fn caller_lookup_fails_closed_while_target_lookup_keeps_esrch() { + assert_eq!( + resolve_caller_group(ABSENT_TID).unwrap_err().raw_os_error(), + Some(libc::EPERM) + ); + assert_eq!( + resolve_target_group(ABSENT_TID).unwrap_err().raw_os_error(), + Some(libc::ESRCH) + ); + } + + #[test] + fn only_setpriority_needs_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 { + assert_eq!(arguments, [(0, 0)]); + } + } + } +} diff --git a/crates/openshell-sandbox/src/linux/process_signal.rs b/crates/openshell-sandbox/src/linux/process_signal.rs index f2addd54dc..c77a797584 100644 --- a/crates/openshell-sandbox/src/linux/process_signal.rs +++ b/crates/openshell-sandbox/src/linux/process_signal.rs @@ -6,7 +6,18 @@ //! Linux accepts a worker TID for `kill()`, so scalar seccomp checks against the //! sandbox leader do not suffice. Resolve the thread group, exclude the live //! sandbox, and retain the target before delivery. Never continue an inspected -//! numeric PID: it could be reused by a newly created sandbox worker. +//! numeric PID in another group: it could be reused by a newly created sandbox +//! worker. +//! +//! A self-directed signal, one whose target group is the notifying thread's own +//! group, is continued instead of emulated. After the notification wait the +//! kernel executes the syscall natively, so its result and `siginfo` are the +//! workload's own: an errno such as EAGAIN stays visible, `si_pid`/`si_uid` +//! name the workload rather than the broker, and a handler runs exactly when it +//! would natively. Continuing an inspected numeric TID has the same PID-reuse +//! window as the `tkill` resolve-then-continue precedent below: the target is in +//! the caller's own thread group, and the trusted sandbox leader stays excluded +//! by the earlier `retain_signal_target` deny. #![allow(unsafe_code)] @@ -14,7 +25,8 @@ use std::io; use std::os::fd::{AsRawFd, FromRawFd, OwnedFd}; use std::sync::atomic::{AtomicBool, Ordering}; -use crate::linux::seccomp_notify::{Notification, NotificationListener}; +use crate::linux::proc_status::thread_group_id; +use crate::linux::seccomp_notify::{Notification, NotificationListener, scalar_int}; use crate::linux::task_memory; /// Emulate one positive-target kill or `rt_sigqueueinfo` notification. @@ -42,7 +54,7 @@ pub fn mediate_process_signal( } refuse_resume_while_frozen(signal, workload_frozen)?; let target = u32::try_from(target).map_err(|_| io::Error::from_raw_os_error(libc::ESRCH))?; - let retained = retain_signal_target(target, sandbox_tgid)?; + let (retained, target_group) = retain_signal_target(target, sandbox_tgid)?; // SAFETY: all-zero siginfo consists of valid integer/pointer fields. A // queued operation copies the entire object once before it is consumed. let mut info: libc::siginfo_t = unsafe { std::mem::zeroed() }; @@ -62,12 +74,25 @@ pub fn mediate_process_signal( if info.si_code >= 0 || info.si_code == libc::SI_TKILL { return Err(io::Error::from_raw_os_error(libc::EPERM)); } + // Native `rt_sigqueueinfo` overwrites `si_signo` with the syscall + // signal argument; `pidfd_send_signal` rejects a mismatch with + // EINVAL. + info.si_signo = signal; &raw const info } _ => return Err(io::Error::from_raw_os_error(libc::ENOSYS)), }; listener.validate_id(notification.id)?; refuse_resume_while_frozen(signal, workload_frozen)?; + // A self-directed signal is continued so the kernel performs it natively + // once this notification is answered, never delivered through a pidfd while + // the notifying thread is still blocked here. Group-directed self-signals + // also wake every sibling thread in the group, exactly as native delivery + // does; a sibling blocked in another mediated syscall can then observe + // EINTR from it (see the `process_sched` module documentation). + if target_group == thread_group_id(notification.tid)? { + return listener.respond_continue(notification.id); + } // SAFETY: retained owns a live pidfd; info is null or a complete trusted // copy. The kernel targets that process object, never a reused numeric PID. let result = unsafe { @@ -141,16 +166,7 @@ fn refuse_resume_while_frozen(signal: i32, workload_frozen: &AtomicBool) -> io:: Ok(()) } -fn scalar_int(value: u64) -> i32 { - let bytes = value.to_ne_bytes(); - #[cfg(target_endian = "little")] - let scalar = [bytes[0], bytes[1], bytes[2], bytes[3]]; - #[cfg(target_endian = "big")] - let scalar = [bytes[4], bytes[5], bytes[6], bytes[7]]; - i32::from_ne_bytes(scalar) -} - -fn retain_signal_target(tid: u32, sandbox_tgid: u32) -> io::Result { +fn retain_signal_target(tid: u32, sandbox_tgid: u32) -> io::Result<(OwnedFd, u32)> { if sandbox_tgid == 0 { return Err(io::Error::from_raw_os_error(libc::EINVAL)); } @@ -165,29 +181,114 @@ fn retain_signal_target(tid: u32, sandbox_tgid: u32) -> io::Result { } let fd = i32::try_from(fd).map_err(|_| io::Error::other("pidfd does not fit RawFd"))?; // SAFETY: successful pidfd_open transferred this descriptor to the caller. - Ok(unsafe { OwnedFd::from_raw_fd(fd) }) -} - -fn thread_group_id(tid: u32) -> io::Result { - let status = std::fs::read_to_string(format!("/proc/{tid}/status")).map_err(|error| { - if error.kind() == io::ErrorKind::NotFound { - io::Error::from_raw_os_error(libc::ESRCH) - } else { - error - } - })?; - status - .lines() - .find_map(|line| { - line.strip_prefix("Tgid:") - .and_then(|value| value.trim().parse::().ok()) - }) - .ok_or_else(|| io::Error::from_raw_os_error(libc::ESRCH)) + Ok((unsafe { OwnedFd::from_raw_fd(fd) }, target_group)) } #[cfg(test)] mod tests { use super::*; + use std::sync::atomic::AtomicI32; + + /// Value carried in `sigval.sival_ptr` to prove the queued payload survives. + const QUEUED_VALUE: usize = 0x5150; + + /// Write end of the report pipe used by the forked child's handler. + static REPORT_PIPE: AtomicI32 = AtomicI32::new(-1); + + /// Overlay for the `siginfo_t` `_sifields._rt` fields used by `SI_QUEUE`. + /// + /// `__pad0` must be spelled out: glibc places it at offset 12 so that + /// `si_pid` starts at 16, `si_uid` at 20, and the libc `si_value` accessor + /// at 24. The `value` field therefore sits at the offset `si_value` reads. + #[repr(C)] + struct QueuedSiginfo { + signo: libc::c_int, + errno: libc::c_int, + code: libc::c_int, + pad0: libc::c_int, + pid: libc::pid_t, + uid: libc::uid_t, + value: libc::sigval, + } + + extern "C" fn report_queued_signal( + _signal: libc::c_int, + info: *mut libc::siginfo_t, + _context: *mut libc::c_void, + ) { + // SAFETY: an SA_SIGINFO handler receives a live kernel siginfo. + let (signo, value) = unsafe { ((*info).si_signo, (*info).si_value().sival_ptr.addr()) }; + let record = [signo, i32::try_from(value).unwrap_or(-1)]; + // SAFETY: the report descriptor is an inherited pipe write end and + // write is async-signal-safe. + unsafe { + libc::write( + REPORT_PIPE.load(Ordering::Relaxed), + record.as_ptr().cast(), + size_of_val(&record), + ); + } + } + + /// Build a queued-signal `siginfo` whose `si_signo` deliberately differs + /// from the signal passed to `rt_sigqueueinfo`. + fn queued_siginfo() -> libc::siginfo_t { + let mut info: libc::siginfo_t = unsafe { std::mem::zeroed() }; + // SAFETY: `siginfo_t` starts with the three `c_int` fields then the + // `_sifields._rt` fields; the overlay is `repr(C)` and stays in bounds. + unsafe { + (&raw mut info) + .cast::() + .write(QueuedSiginfo { + signo: libc::SIGUSR2, + errno: 0, + code: libc::SI_QUEUE, + pad0: 0, + pid: 0, + uid: 0, + value: libc::sigval { + sival_ptr: std::ptr::without_provenance_mut(QUEUED_VALUE), + }, + }); + } + info + } + + /// Wait in a forked child for one queued `SIGUSR1`. + /// + /// The handler reports the observed `si_signo` and `si_value` through the + /// inherited pipe. Only async-signal-safe libc calls run here and no Rust + /// cleanup follows `fork`. + fn queued_signal_child(read_fd: libc::c_int, write_fd: libc::c_int) -> ! { + // SAFETY: the child owns its inherited descriptors and reports through + // the write end only. + unsafe { + libc::close(read_fd); + REPORT_PIPE.store(write_fd, Ordering::Relaxed); + let mut blocked: libc::sigset_t = std::mem::zeroed(); + let mut empty: libc::sigset_t = std::mem::zeroed(); + libc::sigemptyset(&raw mut blocked); + libc::sigemptyset(&raw mut empty); + libc::sigaddset(&raw mut blocked, libc::SIGUSR1); + libc::sigprocmask(libc::SIG_BLOCK, &raw const blocked, std::ptr::null_mut()); + let mut action: libc::sigaction = std::mem::zeroed(); + action.sa_sigaction = report_queued_signal as *const () as libc::sighandler_t; + action.sa_flags = libc::SA_SIGINFO; + libc::sigemptyset(&raw mut action.sa_mask); + if libc::sigaction(libc::SIGUSR1, &raw const action, std::ptr::null_mut()) != 0 { + libc::_exit(1); + } + // Bound the wait so a missing delivery fails instead of hanging. + libc::alarm(10); + let ready = 1_u8; + if libc::write(write_fd, (&raw const ready).cast(), 1) != 1 { + libc::_exit(2); + } + // Unblocks SIGUSR1 atomically with the wait. + libc::sigsuspend(&raw const empty); + libc::_exit(0); + } + } #[test] fn filtered_worker_cannot_signal_the_sandbox_through_a_tid() { @@ -219,13 +320,130 @@ mod tests { assert_eq!(worker.join().unwrap(), (-1, Some(libc::EPERM))); } + /// A queued signal must reach the target with the requested `si_signo`. + /// + /// Native `rt_sigqueueinfo` overwrites `si_signo` with the syscall signal + /// argument, and `pidfd_send_signal` rejects a mismatched `si_signo` with + /// EINVAL, so mediation must mirror that overwrite. + #[test] + fn queued_signal_preserves_signo_and_value() { + let mut descriptors = [0_i32; 2]; + // SAFETY: pipe fills both entries of the live array with new FDs. + assert_eq!( + unsafe { libc::pipe(descriptors.as_mut_ptr()) }, + 0, + "pipe failed" + ); + let [read_fd, write_fd] = descriptors; + + // SAFETY: the child body runs only async-signal-safe libc calls before + // `_exit`, so the multi-threaded test process is safe to fork. + let child = unsafe { libc::fork() }; + assert!(child >= 0, "fork failed"); + if child == 0 { + queued_signal_child(read_fd, write_fd); + } + // SAFETY: the parent only reads the report pipe. + assert_eq!(unsafe { libc::close(write_fd) }, 0, "close failed"); + + let mut ready = [0_u8; 1]; + // SAFETY: read fills the live readiness buffer from the inherited pipe. + let ready_bytes = unsafe { libc::read(read_fd, ready.as_mut_ptr().cast(), 1) }; + assert_eq!(ready_bytes, 1, "child did not install its signal handler"); + + let (sender, receiver) = std::sync::mpsc::sync_channel(1); + let target = u32::try_from(child).expect("child PID fits u32"); + let worker = std::thread::spawn(move || { + let listener = crate::linux::seccomp_notify::install_workload_listener().unwrap(); + sender.send(listener).unwrap(); + let info = queued_siginfo(); + // SAFETY: `info` outlives the kernel's copy; the worker blocks in + // the notified syscall until the broker responds. + let result = unsafe { + libc::syscall( + libc::SYS_rt_sigqueueinfo, + libc::c_long::from(target), + libc::c_long::from(libc::SIGUSR1), + &raw const info, + ) + }; + let error = if result < 0 { + io::Error::last_os_error().raw_os_error() + } else { + None + }; + (result, error) + }); + + let listener = receiver + .recv_timeout(std::time::Duration::from_secs(5)) + .unwrap(); + let notification = listener.receive().unwrap(); + assert_eq!(i64::from(notification.syscall), libc::SYS_rt_sigqueueinfo); + let mediation = mediate_process_signal( + &listener, + notification, + std::process::id(), + &AtomicBool::new(false), + ); + if let Err(error) = &mediation { + listener + .respond_errno(notification.id, error.raw_os_error().unwrap_or(libc::EPERM)) + .unwrap(); + // SAFETY: the child is this test's direct descendant and would + // otherwise wait out its alarm. + unsafe { libc::kill(child, libc::SIGKILL) }; + } + let worker_result = worker.join().unwrap(); + + let mut status = 0; + // SAFETY: the child is a live direct descendant of this process. + assert_eq!( + unsafe { libc::waitpid(child, &raw mut status, 0) }, + child, + "waitpid failed" + ); + let mut observed = [0_i32; 2]; + // SAFETY: read fills the live record buffer; the parent closed the + // write end, so a child that never reported leaves the pipe at EOF. + let reported = unsafe { + libc::read( + read_fd, + observed.as_mut_ptr().cast(), + size_of_val(&observed), + ) + }; + // SAFETY: this process still owns the read end. + assert_eq!(unsafe { libc::close(read_fd) }, 0, "close failed"); + + assert!( + mediation.is_ok(), + "queued signal mediation failed: {:?}", + mediation.as_ref().err() + ); + assert_eq!(worker_result, (0, None)); + assert!( + libc::WIFEXITED(status) && libc::WEXITSTATUS(status) == 0, + "child did not exit cleanly: {status}" + ); + assert_eq!( + reported, + isize::try_from(size_of_val(&observed)).unwrap(), + "child did not report the delivered siginfo" + ); + assert_eq!(observed[0], libc::SIGUSR1); + assert_eq!(observed[1], i32::try_from(QUEUED_VALUE).unwrap()); + } + #[test] fn retained_child_can_be_signaled_without_numeric_pid_delivery() { let mut child = std::process::Command::new("sleep") .arg("30") .spawn() .unwrap(); - let retained = retain_signal_target(child.id(), std::process::id()).unwrap(); + let (retained, target_group) = + retain_signal_target(child.id(), std::process::id()).unwrap(); + assert_eq!(target_group, child.id()); // SAFETY: this pidfd owns the disposable child launched by this test. assert_eq!( unsafe { @@ -255,6 +473,560 @@ mod tests { assert_eq!(io::Error::last_os_error().raw_os_error(), Some(libc::ESRCH)); } + /// Record one delivered signal through the inherited report pipe. + extern "C" fn note_delivery( + _signal: libc::c_int, + _info: *mut libc::siginfo_t, + _context: *mut libc::c_void, + ) { + let byte = 1_u8; + // SAFETY: the report descriptor is an inherited pipe write end and + // write is async-signal-safe. + unsafe { + libc::write( + REPORT_PIPE.load(Ordering::Relaxed), + (&raw const byte).cast(), + 1, + ); + } + } + + /// Fork a child of the filtered launcher thread. + /// + /// The launcher thread owns the only workload filter, so its child inherits + /// it and its `kill` is notified while the test thread holds the listener. + /// The child body must stay async-signal-safe and never return, so no Rust + /// state is touched in the child after `fork`. + fn launch_filtered_child( + launcher: &crate::linux::workload_launcher::WorkloadLauncher, + child_body: impl FnOnce() + Send + 'static, + ) -> libc::pid_t { + launcher + .execute(move || { + // SAFETY: see the function contract above; `child_body` + // diverges, so only the parent returns through the channel. + let child = unsafe { libc::fork() }; + if child == 0 { + child_body(); + // SAFETY: the child body must not return, but never fall + // through into the launcher's channel code if it does. + unsafe { libc::_exit(1) }; + } + child + }) + .expect("launcher result") + } + + /// Send `SIGUSR1` to the forked child's own process and wait for it. + /// + /// Continuing the notification lets the kernel deliver the signal natively, + /// so the handler runs exactly when it would without mediation. The child + /// still waits on the report pipe so a missing delivery fails instead of + /// passing silently. SIGALRM bounds the wait. + fn self_signal_child(read_fd: libc::c_int, write_fd: libc::c_int) -> ! { + // SAFETY: only async-signal-safe libc calls run here and no Rust + // cleanup follows `fork`. + unsafe { + REPORT_PIPE.store(write_fd, Ordering::Relaxed); + let mut action: libc::sigaction = std::mem::zeroed(); + action.sa_sigaction = note_delivery as *const () as libc::sighandler_t; + action.sa_flags = libc::SA_SIGINFO | libc::SA_RESTART; + libc::sigemptyset(&raw mut action.sa_mask); + if libc::sigaction(libc::SIGUSR1, &raw const action, std::ptr::null_mut()) != 0 { + libc::_exit(1); + } + libc::alarm(10); + let pid = libc::c_long::from(libc::getpid()); + if libc::syscall(libc::SYS_kill, pid, libc::c_long::from(libc::SIGUSR1)) != 0 { + libc::_exit(3); + } + let mut seen = [0_u8; 1]; + if libc::read(read_fd, seen.as_mut_ptr().cast(), 1) != 1 { + libc::_exit(4); + } + libc::_exit(0); + } + } + + /// Idle body of the raw sibling thread: it only waits for signals. + extern "C" fn idle_sibling(_argument: *mut libc::c_void) -> libc::c_int { + loop { + // SAFETY: ppoll with no descriptors and no timeout only blocks + // until a signal handler has run. + unsafe { + libc::syscall( + libc::SYS_ppoll, + std::ptr::null::(), + 0_usize, + std::ptr::null::(), + std::ptr::null::(), + 0_usize, + ); + } + } + } + + /// Send `SIGUSR1` to a sibling thread's TID and wait for its delivery. + /// + /// `kill()` resolves any TID to its thread group, so naming a sibling is a + /// process-directed signal to the child's own group. The sibling is a raw + /// `clone` thread on a mapped stack, because the forked child must not + /// allocate or touch the copied thread runtime. + fn sibling_signal_child(read_fd: libc::c_int, write_fd: libc::c_int) -> ! { + const STACK_SIZE: usize = 64 * 1024; + // SAFETY: only async-signal-safe libc calls run here and no Rust + // cleanup follows `fork`. The mapped stack lives until `_exit`. + unsafe { + REPORT_PIPE.store(write_fd, Ordering::Relaxed); + let mut action: libc::sigaction = std::mem::zeroed(); + action.sa_sigaction = note_delivery as *const () as libc::sighandler_t; + action.sa_flags = libc::SA_SIGINFO | libc::SA_RESTART; + libc::sigemptyset(&raw mut action.sa_mask); + if libc::sigaction(libc::SIGUSR1, &raw const action, std::ptr::null_mut()) != 0 { + libc::_exit(1); + } + libc::alarm(10); + let stack = libc::mmap( + std::ptr::null_mut(), + STACK_SIZE, + libc::PROT_READ | libc::PROT_WRITE, + libc::MAP_PRIVATE | libc::MAP_ANONYMOUS | libc::MAP_STACK, + -1, + 0, + ); + if stack == libc::MAP_FAILED { + libc::_exit(5); + } + let sibling = libc::clone( + idle_sibling, + stack.cast::().add(STACK_SIZE).cast(), + libc::CLONE_VM + | libc::CLONE_FS + | libc::CLONE_FILES + | libc::CLONE_SIGHAND + | libc::CLONE_THREAD + | libc::CLONE_SYSVSEM, + std::ptr::null_mut(), + ); + if sibling <= 0 || sibling == libc::getpid() { + libc::_exit(6); + } + if libc::syscall( + libc::SYS_kill, + libc::c_long::from(sibling), + libc::c_long::from(libc::SIGUSR1), + ) != 0 + { + libc::_exit(3); + } + let mut seen = [0_u8; 1]; + if libc::read(read_fd, seen.as_mut_ptr().cast(), 1) != 1 { + libc::_exit(4); + } + libc::_exit(0); + } + } + + /// Send a queued `SIGUSR1` to the child's own thread group. + /// + /// The queued payload deliberately carries a different `si_signo`: native + /// `rt_sigqueueinfo` overwrites it with the syscall signal argument. + /// Continuing lets the kernel apply that overwrite natively, so delivery + /// only succeeds when the continued syscall keeps its signal number. The + /// child then reads the handler's report and exits with a code that names + /// the failure. + fn self_queued_signal_child(read_fd: libc::c_int, write_fd: libc::c_int) -> ! { + // SAFETY: only async-signal-safe libc calls and stack writes run here; + // no Rust cleanup follows `fork`. + unsafe { + REPORT_PIPE.store(write_fd, Ordering::Relaxed); + let mut action: libc::sigaction = std::mem::zeroed(); + action.sa_sigaction = report_queued_signal as *const () as libc::sighandler_t; + action.sa_flags = libc::SA_SIGINFO | libc::SA_RESTART; + libc::sigemptyset(&raw mut action.sa_mask); + if libc::sigaction(libc::SIGUSR1, &raw const action, std::ptr::null_mut()) != 0 { + libc::_exit(1); + } + libc::alarm(10); + let info = queued_siginfo(); + let pid = libc::c_long::from(libc::getpid()); + if libc::syscall( + libc::SYS_rt_sigqueueinfo, + pid, + libc::c_long::from(libc::SIGUSR1), + &raw const info, + ) != 0 + { + libc::_exit(3); + } + let mut observed = [0_i32; 2]; + let expected = isize::try_from(size_of_val(&observed)).expect("record size fits isize"); + if libc::read( + read_fd, + observed.as_mut_ptr().cast(), + size_of_val(&observed), + ) != expected + { + libc::_exit(4); + } + let expected_value = i32::try_from(QUEUED_VALUE).expect("payload fits i32"); + if observed[0] != libc::SIGUSR1 || observed[1] != expected_value { + libc::_exit(5); + } + libc::_exit(0); + } + } + + /// Queue a blocked real-time signal with the pending queue exhausted. + /// + /// The child lowers its own pending-signal limit with `prlimit64(0, ...)`, + /// which the listener filter runs natively, then queues `SIGRTMIN + 1` + /// to its own process. The blocked real-time signal cannot be delivered + /// inline, so the kernel has to queue it and reports EAGAIN once the queue + /// is full. + fn queue_limited_signal_child() -> ! { + // SAFETY: only async-signal-safe libc calls and stack writes run here; + // no Rust cleanup follows `fork`. + unsafe { + libc::alarm(10); + let signal = libc::SIGRTMIN() + 1; + let mut blocked: libc::sigset_t = std::mem::zeroed(); + libc::sigemptyset(&raw mut blocked); + libc::sigaddset(&raw mut blocked, signal); + if libc::sigprocmask(libc::SIG_BLOCK, &raw const blocked, std::ptr::null_mut()) != 0 { + libc::_exit(1); + } + let limit = libc::rlimit { + rlim_cur: 0, + rlim_max: 0, + }; + if libc::syscall( + libc::SYS_prlimit64, + 0, + libc::RLIMIT_SIGPENDING, + &raw const limit, + std::ptr::null_mut::(), + ) != 0 + { + libc::_exit(2); + } + let mut info: libc::siginfo_t = std::mem::zeroed(); + (&raw mut info) + .cast::() + .write(QueuedSiginfo { + signo: signal, + errno: 0, + code: libc::SI_QUEUE, + pad0: 0, + pid: 0, + uid: 0, + value: libc::sigval { + sival_ptr: std::ptr::null_mut(), + }, + }); + let pid = libc::c_long::from(libc::getpid()); + let result = libc::syscall( + libc::SYS_rt_sigqueueinfo, + pid, + libc::c_long::from(signal), + &raw const info, + ); + let error = *libc::__errno_location(); + libc::_exit(if result == -1 && error == libc::EAGAIN { + 0 + } else { + 3 + }); + } + } + + /// Deliver `SIGTERM` to a process outside the child's thread group. + fn foreign_signal_child(target: u32) -> ! { + // SAFETY: kill takes scalar arguments and the disposable target was + // spawned by the test. + unsafe { + let result = libc::syscall( + libc::SYS_kill, + libc::c_long::from(target), + libc::c_long::from(libc::SIGTERM), + ); + libc::_exit(if result == 0 { 0 } else { 3 }); + } + } + + /// A self-directed kill is continued so the kernel performs it natively. + /// + /// The notifying thread is still blocked in the notification wait when the + /// broker decides, so delivering through a pidfd would interrupt the syscall + /// being emulated. Continuing lets the kernel run the syscall and deliver + /// the signal itself, and the child exits cleanly once its handler reports + /// the delivery. + #[test] + fn self_directed_signal_is_continued_to_the_kernel() { + let mut descriptors = [0_i32; 2]; + // SAFETY: pipe fills both entries of the live array with new FDs. + assert_eq!( + unsafe { libc::pipe(descriptors.as_mut_ptr()) }, + 0, + "pipe failed" + ); + let [read_fd, write_fd] = descriptors; + + let (launcher, listener) = + crate::linux::workload_launcher::start().expect("start launcher"); + let child = launch_filtered_child(&launcher, move || self_signal_child(read_fd, write_fd)); + assert!(child > 0, "fork failed"); + // SAFETY: the child holds its own inherited descriptors. + unsafe { + libc::close(read_fd); + libc::close(write_fd); + } + + let notification = listener.receive().expect("receive self-directed kill"); + assert_eq!(i64::from(notification.syscall), libc::SYS_kill); + assert_eq!( + thread_group_id(notification.tid).unwrap(), + u32::try_from(child).expect("child PID fits u32"), + "notification must come from the filtered child" + ); + mediate_process_signal( + &listener, + notification, + std::process::id(), + &AtomicBool::new(false), + ) + .expect("self-directed mediation"); + + let mut status = 0; + // SAFETY: the child is this test's direct descendant. + assert_eq!( + unsafe { libc::waitpid(child, &raw mut status, 0) }, + child, + "waitpid failed" + ); + assert!( + libc::WIFEXITED(status) && libc::WEXITSTATUS(status) == 0, + "child did not observe its self-directed signal: {status}" + ); + } + + /// A kill that names a sibling thread's TID is self-directed too. + /// + /// The target is not the caller's PID, but it resolves to the caller's own + /// thread group, so the notification is continued exactly as for + /// `getpid()`. The child exits cleanly once its handler reports the + /// delivery. + #[test] + fn sibling_thread_signal_is_continued_to_the_kernel() { + let mut descriptors = [0_i32; 2]; + // SAFETY: pipe fills both entries of the live array with new FDs. + assert_eq!( + unsafe { libc::pipe(descriptors.as_mut_ptr()) }, + 0, + "pipe failed" + ); + let [read_fd, write_fd] = descriptors; + + let (launcher, listener) = + crate::linux::workload_launcher::start().expect("start launcher"); + let child = + launch_filtered_child(&launcher, move || sibling_signal_child(read_fd, write_fd)); + assert!(child > 0, "fork failed"); + // SAFETY: the child holds its own inherited descriptors. + unsafe { + libc::close(read_fd); + libc::close(write_fd); + } + + let notification = listener.receive().expect("receive sibling-directed kill"); + assert_eq!(i64::from(notification.syscall), libc::SYS_kill); + let child_group = u32::try_from(child).expect("child PID fits u32"); + assert_eq!( + thread_group_id(notification.tid).unwrap(), + child_group, + "notification must come from the filtered child" + ); + let target = + u32::try_from(scalar_int(notification.args[0])).expect("target TID is positive"); + assert_ne!( + target, child_group, + "target must be a sibling, not the leader" + ); + assert_eq!( + thread_group_id(target).unwrap(), + child_group, + "sibling must belong to the child's thread group" + ); + mediate_process_signal( + &listener, + notification, + std::process::id(), + &AtomicBool::new(false), + ) + .expect("sibling-directed mediation"); + + let mut status = 0; + // SAFETY: the child is this test's direct descendant. + assert_eq!( + unsafe { libc::waitpid(child, &raw mut status, 0) }, + child, + "waitpid failed" + ); + assert!( + libc::WIFEXITED(status) && libc::WEXITSTATUS(status) == 0, + "child did not observe the signal sent to its sibling: {status}" + ); + } + + /// A self-directed queued signal is continued with its signal number. + /// + /// This is the only path where both rules interact: the payload arrives + /// with a different `si_signo`, which native `rt_sigqueueinfo` overwrites, + /// and the target group is the notifying thread's own. Continuing lets the + /// kernel apply both natively, so the handler must observe `SIGUSR1`. + #[test] + fn self_directed_queued_signal_is_continued_to_the_kernel() { + let mut descriptors = [0_i32; 2]; + // SAFETY: pipe fills both entries of the live array with new FDs. + assert_eq!( + unsafe { libc::pipe(descriptors.as_mut_ptr()) }, + 0, + "pipe failed" + ); + let [read_fd, write_fd] = descriptors; + + let (launcher, listener) = + crate::linux::workload_launcher::start().expect("start launcher"); + let child = launch_filtered_child(&launcher, move || { + self_queued_signal_child(read_fd, write_fd) + }); + assert!(child > 0, "fork failed"); + // SAFETY: the child holds its own inherited descriptors. + unsafe { + libc::close(read_fd); + libc::close(write_fd); + } + + let notification = listener + .receive() + .expect("receive self-directed queued signal"); + assert_eq!(i64::from(notification.syscall), libc::SYS_rt_sigqueueinfo); + assert_eq!( + thread_group_id(notification.tid).unwrap(), + u32::try_from(child).expect("child PID fits u32"), + "notification must come from the filtered child" + ); + mediate_process_signal( + &listener, + notification, + std::process::id(), + &AtomicBool::new(false), + ) + .expect("self-directed queued mediation"); + + let mut status = 0; + // SAFETY: the child is this test's direct descendant. + assert_eq!( + unsafe { libc::waitpid(child, &raw mut status, 0) }, + child, + "waitpid failed" + ); + assert!( + libc::WIFEXITED(status) && libc::WEXITSTATUS(status) == 0, + "child did not observe its own queued signal: {status}" + ); + } + + /// A self-directed queued signal keeps the errno native delivery reports. + /// + /// With the workload's real-time signal queue exhausted the kernel cannot + /// queue the signal and reports EAGAIN. Continuing lets the kernel return + /// that failure to the workload; answering zero on its behalf would report + /// a success the native syscall never gives and silently drop the signal. + #[test] + fn self_directed_signal_reports_native_delivery_errors() { + let (launcher, listener) = + crate::linux::workload_launcher::start().expect("start launcher"); + let child = launch_filtered_child(&launcher, || queue_limited_signal_child()); + assert!(child > 0, "fork failed"); + + let notification = listener + .receive() + .expect("receive self-directed queued signal"); + assert_eq!(i64::from(notification.syscall), libc::SYS_rt_sigqueueinfo); + assert_eq!( + thread_group_id(notification.tid).unwrap(), + u32::try_from(child).expect("child PID fits u32"), + "notification must come from the filtered child" + ); + mediate_process_signal( + &listener, + notification, + std::process::id(), + &AtomicBool::new(false), + ) + .expect("self-directed queued mediation"); + + let mut status = 0; + // SAFETY: the child is this test's direct descendant. + assert_eq!( + unsafe { libc::waitpid(child, &raw mut status, 0) }, + child, + "waitpid failed" + ); + assert!( + libc::WIFEXITED(status) && libc::WEXITSTATUS(status) == 0, + "child did not observe the native EAGAIN: {status}" + ); + } + + /// A process-directed kill of another group must deliver and answer zero. + /// + /// The target is spawned by the unfiltered test thread so the only notified + /// syscall is the child's `kill`; an inverted self-group comparison would + /// answer without delivering and leave the target alive. + #[test] + fn foreign_signal_is_delivered_before_the_answer() { + let mut target = std::process::Command::new("sleep") + .arg("30") + .spawn() + .expect("spawn target"); + let target_pid = target.id(); + + let (launcher, listener) = + crate::linux::workload_launcher::start().expect("start launcher"); + let child = launch_filtered_child(&launcher, move || foreign_signal_child(target_pid)); + assert!(child > 0, "fork failed"); + + let notification = listener.receive().expect("receive foreign kill"); + assert_eq!(i64::from(notification.syscall), libc::SYS_kill); + assert_eq!( + thread_group_id(notification.tid).unwrap(), + u32::try_from(child).expect("child PID fits u32"), + "notification must come from the filtered child" + ); + mediate_process_signal( + &listener, + notification, + std::process::id(), + &AtomicBool::new(false), + ) + .expect("foreign mediation"); + + let mut status = 0; + // SAFETY: the child is this test's direct descendant. + assert_eq!( + unsafe { libc::waitpid(child, &raw mut status, 0) }, + child, + "waitpid failed" + ); + assert!( + libc::WIFEXITED(status) && libc::WEXITSTATUS(status) == 0, + "child did not observe a successful foreign kill: {status}" + ); + assert!(!target.wait().unwrap().success(), "target survived SIGTERM"); + } + #[test] fn rejects_live_sandbox_worker_tid() { let sandbox_tgid = std::process::id(); diff --git a/crates/openshell-sandbox/src/linux/seccomp_notify.rs b/crates/openshell-sandbox/src/linux/seccomp_notify.rs index 665cadcefe..b45dbccd93 100644 --- a/crates/openshell-sandbox/src/linux/seccomp_notify.rs +++ b/crates/openshell-sandbox/src/linux/seccomp_notify.rs @@ -5,8 +5,9 @@ //! //! The wrappers validate notification IDs around every operation and keep raw //! UAPI structures private. Production policy and queueing belong to the -//! sandbox crate; this module owns only the kernel ABI and active conformance -//! probe. +//! sandbox crate; this module owns only the kernel ABI, the active conformance +//! probe, and the workload listener's notification filter, whose per-syscall +//! narrowing comes from the mediators' tables. #![allow(unsafe_code)] @@ -136,6 +137,21 @@ pub struct Notification { pub args: [u64; 6], } +/// Decode one 32-bit scalar syscall argument the way the kernel does. +/// +/// Registers arrive as 64-bit words, but an `int` argument such as a PID, a +/// signal number, or a `PRIO_*` selector is truncated by the kernel. Decoding +/// the same low 32 bits keeps the inspected value equal to the value the kernel +/// acts on, so a sign-extended negative PID is recognized as negative here too. +pub(crate) fn scalar_int(value: u64) -> i32 { + let bytes = value.to_ne_bytes(); + #[cfg(target_endian = "little")] + let scalar = [bytes[0], bytes[1], bytes[2], bytes[3]]; + #[cfg(target_endian = "big")] + let scalar = [bytes[4], bytes[5], bytes[6], bytes[7]]; + i32::from_ne_bytes(scalar) +} + /// Result of exercising the unprivileged notification API under the active /// kernel, outer seccomp profile, and LSM posture. #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -307,8 +323,12 @@ impl NotificationListener { /// Install a non-TSYNC listener filter on the calling thread. /// -/// Only the named syscalls notify. Unexpected architectures are killed, x32 -/// syscalls are killed on x86-64, and all other native syscalls are allowed. +/// Only the named syscalls notify. Some of them keep a native path for +/// argument shapes the broker always allows: `sendto` without a destination, +/// `tgkill`/`rt_tgsigqueueinfo` of any signal but `SIGCONT`, and caller-only +/// scheduling, priority, and resource-limit calls. Unexpected architectures +/// are killed, x32 syscalls are killed on x86-64, and all other native +/// syscalls are allowed. pub fn install_listener(syscalls: &[i64]) -> io::Result { if syscalls.is_empty() { return Err(io::Error::new( @@ -326,7 +346,11 @@ pub fn install_listener(syscalls: &[i64]) -> io::Result { /// launcher thread. /// /// The filter mediates every syscall that can create, select, or materially -/// reconfigure an INET endpoint. Connected `send()`/null-destination +/// 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. @@ -349,6 +373,11 @@ pub fn install_workload_listener() -> io::Result { libc::SYS_openat, libc::SYS_openat2, ]; + syscalls.extend( + crate::linux::process_sched::MEDIATED_PROCESS_SCHED_SYSCALLS + .iter() + .map(|mediated| mediated.number), + ); #[cfg(target_arch = "x86_64")] syscalls.push(libc::SYS_open); install_listener(&syscalls) @@ -688,6 +717,10 @@ fn build_filter(syscalls: &[i64]) -> io::Result> { append_resume_signal_filter(&mut program, syscall)?; continue; } + if let Some(mediated) = crate::linux::process_sched::mediated_syscall(syscall) { + append_named_target_filter(&mut program, syscall, mediated.caller_only_arguments())?; + continue; + } let syscall = u32::try_from(syscall) .map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "negative syscall number"))?; program.extend([ @@ -749,6 +782,42 @@ fn append_resume_signal_filter( Ok(()) } +/// Notify a scheduling, priority, or resource-limit call only when it names a +/// target. A call that can reach only the caller, one where every +/// `(argument, value)` pair in `caller_only` holds, stays in the kernel. +/// +/// Each compared argument is an `int` the kernel truncates, so the low word +/// alone decides, exactly as the broker decodes it. The broker allows these +/// calls unconditionally, so running them natively changes no decision; it +/// keeps them non-interruptible and off the broker round trip. +fn append_named_target_filter( + program: &mut Vec, + syscall: i64, + caller_only: impl Iterator, +) -> io::Result<()> { + let syscall = u32::try_from(syscall) + .map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "negative syscall number"))?; + let conditions: Vec<_> = caller_only.collect(); + let too_long = || io::Error::new(io::ErrorKind::InvalidInput, "caller-only filter too long"); + // Two instructions per compared argument, then the allow and the notify. + let body = u8::try_from(conditions.len() * 2 + 2).map_err(|_| too_long())?; + program.push(jump(BPF_JMP_JEQ_K, syscall, 0, body)); + for (index, &(argument, value)) in conditions.iter().enumerate() { + // A mismatch skips the remaining comparisons and the allow. + let to_notify = + u8::try_from((conditions.len() - index - 1) * 2 + 1).map_err(|_| too_long())?; + program.extend([ + stmt(BPF_LD_W_ABS, argument_word_offset(u32::from(argument), 0)), + jump(BPF_JMP_JEQ_K, value, 0, to_notify), + ]); + } + program.extend([ + stmt(BPF_RET_K, SECCOMP_RET_ALLOW), + stmt(BPF_RET_K, SECCOMP_RET_USER_NOTIF), + ]); + Ok(()) +} + const fn argument_word_offset(argument: u32, word: u32) -> u32 { SECCOMP_DATA_ARGS_OFFSET + argument * 8 + word * 4 } diff --git a/crates/openshell-sandbox/src/main.rs b/crates/openshell-sandbox/src/main.rs index 400478a889..8978659ba7 100644 --- a/crates/openshell-sandbox/src/main.rs +++ b/crates/openshell-sandbox/src/main.rs @@ -434,9 +434,12 @@ fn probe_socket_virtualization() -> Result<()> { .into_diagnostic() .wrap_err("resolve socket probe executable")?; let sandbox_tgid = std::process::id(); - let mut child_hardening = openshell_sandbox::linux::child_seccomp::prepare(sandbox_tgid) - .into_diagnostic() - .wrap_err("prepare socket probe child hardening")?; + // The probe listener below notifies only socket calls, so no broker checks + // scheduling or limit targets for this child. + let mut child_hardening = + openshell_sandbox::linux::child_seccomp::prepare_without_broker(sandbox_tgid) + .into_diagnostic() + .wrap_err("prepare socket probe child hardening")?; let (listener_tx, listener_rx) = mpsc::sync_channel::>(1); let (child_tx, child_rx) = mpsc::sync_channel::>(1); let launcher = std::thread::Builder::new() @@ -2107,8 +2110,8 @@ mod tests { Ok(()) } - /// The production child filter rejects every self-protection probe with - /// EPERM. Running `probe_child_self_protection` against that exact filter + /// The probe child's production filter rejects every self-protection probe + /// with EPERM. Running `probe_child_self_protection` against that exact filter /// keeps each hoisted probe result wired to its own errno check. The test /// re-executes itself in a fresh process so the rlimit and filter changes /// never touch the test harness process. @@ -2123,7 +2126,7 @@ mod tests { prepare_probe_child().expect("prepare probe child"); let tgid = std::process::id(); let sandbox_tgid = libc::pid_t::try_from(tgid).expect("test pid fits pid_t"); - let mut program = openshell_sandbox::linux::child_seccomp::prepare(tgid) + let mut program = openshell_sandbox::linux::child_seccomp::prepare_without_broker(tgid) .expect("prepare production child filter"); program.install().expect("install production child filter"); probe_child_self_protection(sandbox_tgid, -1) diff --git a/crates/openshell-sandbox/src/network_broker.rs b/crates/openshell-sandbox/src/network_broker.rs index 323f1ac715..abbe63df8e 100644 --- a/crates/openshell-sandbox/src/network_broker.rs +++ b/crates/openshell-sandbox/src/network_broker.rs @@ -719,6 +719,9 @@ fn dispatch_notification( &queues.workload_frozen, ); } + if crate::linux::process_sched::is_mediated(syscall) { + return crate::linux::process_sched::mediate_process_sched(&listener, notification); + } if syscall == libc::SYS_socket { return create_socket( ®istry, @@ -3882,4 +3885,596 @@ mod tests { let _wakeup = TcpStream::connect(address).expect("wake the accept loop"); relay.join().expect("accept loop thread panicked"); } + + /// Linux `struct rlimit64` used by the raw `prlimit64` probes. + #[repr(C)] + struct Rlimit64 { + current: u64, + maximum: u64, + } + + /// Linux `struct sched_attr` version 0 used by the raw `sched_setattr` probe. + #[repr(C)] + struct SchedAttr { + size: u32, + sched_policy: u32, + sched_flags: u64, + sched_nice: i32, + sched_priority: u32, + sched_runtime: u64, + sched_deadline: u64, + sched_period: u64, + } + + /// Errno of one failed libc result, or `None` when it succeeded. + fn errno(result: libc::c_long) -> Option { + if result == -1 { + io::Error::last_os_error().raw_os_error() + } else { + None + } + } + + /// Read one task's current nice value, distinguishing `-1` from an error. + /// + /// The selector is always `PRIO_PROCESS`. + /// + /// `getpriority` returns `-1` for both a valid nice level and a failure, so + /// `errno` decides. The caller must not feed an ambiguous failure into + /// `setpriority`, where it would surface as a mediation error instead. + fn read_priority(who: libc::id_t) -> Result { + // SAFETY: `__errno_location` returns this thread's live errno storage + // and `getpriority` reads only scalar arguments. + unsafe { + *libc::__errno_location() = 0; + let nice = libc::getpriority(libc::PRIO_PROCESS, who); + let error = *libc::__errno_location(); + if nice == -1 && error != 0 { + Err(error) + } else { + Ok(nice) + } + } + } + + /// Run `operation` on a filtered launcher thread that a live broker serves. + /// + /// The launcher thread inherits the workload listener, so every mediated + /// syscall inside `operation` blocks until the broker answers it. + fn mediated(operation: impl FnOnce() -> T + Send + 'static) -> T { + let (launcher, listener) = + crate::linux::workload_launcher::start().expect("start workload launcher"); + let _broker = NetworkBroker::start_for_test(listener).expect("start network broker"); + launcher.execute(operation).expect("launcher result") + } + + /// Re-apply the caller's current CPU affinity to `pid`. + /// + /// Re-applying the current mask always succeeds in the kernel and needs no + /// capability, so the result reports only whether mediation let the call + /// through. + fn affinity_errno(pid: libc::pid_t) -> Option { + // SAFETY: the mask is live storage of the declared size and the call + // runs on the launcher thread while the broker answers it. + unsafe { + let mut mask: libc::cpu_set_t = std::mem::zeroed(); + let size = size_of::(); + assert_eq!( + libc::sched_getaffinity(0, size, &raw mut mask), + 0, + "sched_getaffinity: {}", + io::Error::last_os_error() + ); + errno(libc::c_long::from(libc::sched_setaffinity( + pid, + size, + &raw const mask, + ))) + } + } + + /// Every mediated call re-applies the caller's current settings, read back + /// first, so the continued syscall changes nothing and needs no capability. + /// The `pid == 0` calls stay in the kernel and the named ones are continued + /// by the broker; both must succeed. + #[test] + fn scheduling_and_limit_calls_allow_the_callers_thread_group() { + let results = mediated(|| { + // SAFETY: all buffers passed to the kernel are live for the call + // and the launcher thread blocks until the broker answers. + unsafe { + let tid = libc::pid_t::try_from(libc::syscall(libc::SYS_gettid)) + .expect("thread ID fits pid_t"); + let mut param: libc::sched_param = std::mem::zeroed(); + assert_eq!(libc::sched_getparam(0, &raw mut param), 0); + let policy = libc::sched_getscheduler(0); + assert!(policy >= 0, "sched_getscheduler call failed"); + let mut attr: SchedAttr = std::mem::zeroed(); + attr.size = u32::try_from(size_of::()).expect("attribute size fits u32"); + assert_eq!( + libc::syscall( + libc::SYS_sched_getattr, + 0, + &raw mut attr, + size_of::(), + 0, + ), + 0, + "sched_getattr: {}", + io::Error::last_os_error() + ); + let mut limits = Rlimit64 { + current: 0, + maximum: 0, + }; + assert_eq!( + libc::syscall( + libc::SYS_prlimit64, + 0, + libc::c_long::from(libc::RLIMIT_CORE), + std::ptr::null::(), + &raw mut limits, + ), + 0, + "prlimit64 query: {}", + io::Error::last_os_error() + ); + ( + affinity_errno(0), + affinity_errno(tid), + affinity_errno(libc::getpid()), + errno(libc::c_long::from(libc::sched_setparam( + 0, + &raw const param, + ))), + errno(libc::c_long::from(libc::sched_setscheduler( + 0, + policy, + &raw const param, + ))), + errno(libc::syscall( + libc::SYS_sched_setattr, + 0, + &raw const attr, + 0, + )), + errno(libc::syscall( + libc::SYS_prlimit64, + 0, + libc::c_long::from(libc::RLIMIT_CORE), + &raw const limits, + std::ptr::null::(), + )), + errno(libc::syscall( + libc::SYS_prlimit64, + libc::getpid(), + libc::c_long::from(libc::RLIMIT_CORE), + &raw const limits, + std::ptr::null::(), + )), + ) + } + }); + assert_eq!(results, (None, None, None, None, None, None, None, None)); + } + + #[test] + fn setpriority_allows_the_callers_group() { + let (ready_tx, ready_rx) = std::sync::mpsc::sync_channel(1); + let (hold_tx, hold_rx) = std::sync::mpsc::sync_channel::<()>(1); + let sibling = std::thread::spawn(move || { + // SAFETY: gettid has no arguments or side effects. + ready_tx + .send(unsafe { libc::syscall(libc::SYS_gettid) }) + .expect("report sibling tid"); + let _ = hold_rx.recv(); + }); + let sibling_tid = libc::id_t::try_from(ready_rx.recv().expect("sibling tid")) + .expect("sibling tid fits id_t"); + let results = mediated(move || { + // SAFETY: each call names a live task in the caller's thread group + // and re-applies that task's current nice value. A failed read + // fails the test here rather than masquerading as a filter error. + unsafe { + let leader_nice = + read_priority(libc::getpid().cast_unsigned()).expect("read leader nice value"); + let sibling_nice = read_priority(sibling_tid).expect("read sibling nice value"); + let self_nice = read_priority(0).expect("read self nice value"); + ( + errno(libc::c_long::from(libc::setpriority( + libc::PRIO_PROCESS, + 0, + self_nice, + ))), + errno(libc::c_long::from(libc::setpriority( + libc::PRIO_PROCESS, + libc::getpid().cast_unsigned(), + leader_nice, + ))), + errno(libc::c_long::from(libc::setpriority( + libc::PRIO_PROCESS, + sibling_tid, + sibling_nice, + ))), + ) + } + }); + drop(hold_tx); + sibling.join().expect("join sibling"); + assert_eq!(results, (None, None, None)); + } + + /// A call that names only the caller never reaches the broker. + /// + /// No broker serves this listener. A watcher answers ENOSYS to whatever + /// the filter notifies, so a `pid == 0` call that left the native path + /// fails here instead of hanging. A group selector and a named target must + /// still be notified, or the filter would let them run unchecked. + #[test] + fn caller_only_scheduling_and_limit_calls_stay_in_the_kernel() { + let (launcher, listener) = + crate::linux::workload_launcher::start().expect("start workload launcher"); + let (notified_tx, notified_rx) = std::sync::mpsc::channel(); + std::thread::spawn(move || { + while let Ok(notification) = listener.receive() { + let _ = notified_tx.send(i64::from(notification.syscall)); + let _ = listener.respond_errno(notification.id, libc::ENOSYS); + } + }); + let results = launcher + .execute(|| { + // SAFETY: all buffers passed to the kernel are live for the + // call, and every native call re-applies settings read back + // from this launcher thread. + unsafe { + let tid = libc::pid_t::try_from(libc::syscall(libc::SYS_gettid)) + .expect("thread ID fits pid_t"); + let mut param: libc::sched_param = std::mem::zeroed(); + assert_eq!(libc::sched_getparam(0, &raw mut param), 0, "sched_getparam"); + let policy = libc::sched_getscheduler(0); + assert!(policy >= 0, "sched_getscheduler"); + let mut attr: SchedAttr = std::mem::zeroed(); + attr.size = + u32::try_from(size_of::()).expect("attribute size fits u32"); + assert_eq!( + libc::syscall( + libc::SYS_sched_getattr, + 0, + &raw mut attr, + size_of::(), + 0, + ), + 0, + "sched_getattr: {}", + io::Error::last_os_error() + ); + let mut limits = Rlimit64 { + current: 0, + maximum: 0, + }; + assert_eq!( + libc::syscall( + libc::SYS_prlimit64, + 0, + libc::c_long::from(libc::RLIMIT_CORE), + std::ptr::null::(), + &raw mut limits, + ), + 0, + "prlimit64 query: {}", + io::Error::last_os_error() + ); + let nice = read_priority(0).expect("read self nice value"); + ( + affinity_errno(0), + errno(libc::c_long::from(libc::sched_setparam( + 0, + &raw const param, + ))), + errno(libc::c_long::from(libc::sched_setscheduler( + 0, + policy, + &raw const param, + ))), + errno(libc::syscall( + libc::SYS_sched_setattr, + 0, + &raw const attr, + 0, + )), + errno(libc::syscall( + libc::SYS_prlimit64, + 0, + libc::c_long::from(libc::RLIMIT_CORE), + &raw const limits, + std::ptr::null::(), + )), + errno(libc::c_long::from(libc::setpriority( + libc::PRIO_PROCESS, + 0, + nice, + ))), + errno(libc::c_long::from(libc::setpriority(libc::PRIO_PGRP, 0, 0))), + affinity_errno(tid), + ) + } + }) + .expect("launcher result"); + assert_eq!( + results, + ( + None, + None, + None, + None, + None, + None, + Some(libc::ENOSYS), + Some(libc::ENOSYS) + ) + ); + assert_eq!( + notified_rx.try_iter().collect::>(), + [libc::SYS_setpriority, libc::SYS_sched_setaffinity] + ); + } + + #[test] + fn scheduling_and_limit_calls_deny_another_thread_group() { + let mut target = std::process::Command::new("sleep") + .arg("30") + .spawn() + .expect("spawn target"); + let target_tgid = libc::pid_t::try_from(target.id()).expect("target PID fits pid_t"); + let (affinity, limits) = mediated(move || { + // SAFETY: the target is this test's live disposable child, the + // affinity mask is the caller's own, and both limit pointers are + // null, so the continued call would only validate the target. + unsafe { + ( + affinity_errno(target_tgid), + errno(libc::syscall( + libc::SYS_prlimit64, + target_tgid, + libc::c_long::from(libc::RLIMIT_CORE), + std::ptr::null::(), + std::ptr::null::(), + )), + ) + } + }); + let _ = target.kill(); + let _ = target.wait(); + assert_eq!(affinity, Some(libc::EPERM)); + assert_eq!(limits, Some(libc::EPERM)); + } + + /// The broker resolves every `sched_set*` target against the caller's + /// thread group, not only affinity. + /// + /// Both halves run on one notification stream: the caller's own settings + /// are re-applied to its own TID, which is notified unlike `pid 0`, and to + /// a disposable child that shares the + /// test's real user ID. Each continued call would therefore succeed in the + /// kernel, so a syscall missing from the notified set lets the foreign call + /// through, and one missing from the dispatch match fails the self call. + #[test] + fn scheduling_calls_distinguish_own_and_foreign_thread_groups() { + let mut target = std::process::Command::new("sleep") + .arg("30") + .spawn() + .expect("spawn target"); + let target_tgid = libc::pid_t::try_from(target.id()).expect("target PID fits pid_t"); + let results = mediated(move || { + // SAFETY: the parameter, policy, and attributes are read back from + // the launcher thread, and every pointer is live for the call. + unsafe { + let mut param: libc::sched_param = std::mem::zeroed(); + assert_eq!(libc::sched_getparam(0, &raw mut param), 0, "sched_getparam"); + let policy = libc::sched_getscheduler(0); + assert!(policy >= 0, "sched_getscheduler"); + let mut attr: SchedAttr = std::mem::zeroed(); + attr.size = u32::try_from(size_of::()).expect("attribute size fits u32"); + assert_eq!( + libc::syscall( + libc::SYS_sched_getattr, + 0, + &raw mut attr, + size_of::(), + 0, + ), + 0, + "sched_getattr: {}", + io::Error::last_os_error() + ); + let tid = libc::pid_t::try_from(libc::syscall(libc::SYS_gettid)) + .expect("thread ID fits pid_t"); + ( + errno(libc::c_long::from(libc::sched_setparam( + tid, + &raw const param, + ))), + errno(libc::c_long::from(libc::sched_setscheduler( + tid, + policy, + &raw const param, + ))), + errno(libc::syscall( + libc::SYS_sched_setattr, + tid, + &raw const attr, + 0, + )), + errno(libc::c_long::from(libc::sched_setparam( + target_tgid, + &raw const param, + ))), + errno(libc::c_long::from(libc::sched_setscheduler( + target_tgid, + policy, + &raw const param, + ))), + errno(libc::syscall( + libc::SYS_sched_setattr, + target_tgid, + &raw const attr, + 0, + )), + ) + } + }); + let _ = target.kill(); + let _ = target.wait(); + assert_eq!( + results, + ( + None, + None, + None, + Some(libc::EPERM), + Some(libc::EPERM), + Some(libc::EPERM) + ) + ); + } + + #[test] + fn setpriority_denies_other_groups_and_group_selectors() { + let mut target = std::process::Command::new("sleep") + .arg("30") + .spawn() + .expect("spawn target"); + let target_tgid = libc::id_t::try_from(target.id()).expect("target PID fits id_t"); + let (process, group, user) = mediated(move || { + // SAFETY: scalar arguments only; the broker decides before the + // kernel resolves any of the targets. + unsafe { + ( + errno(libc::c_long::from(libc::setpriority( + libc::PRIO_PROCESS, + target_tgid, + 0, + ))), + errno(libc::c_long::from(libc::setpriority(libc::PRIO_PGRP, 0, 0))), + errno(libc::c_long::from(libc::setpriority(libc::PRIO_USER, 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)); + } + + /// Invalid `setpriority` arguments keep the errno the kernel returns. + /// + /// An unknown selector is a usage error the kernel rejects with EINVAL, and + /// a negative PID names no process, so neither is a policy denial. Both + /// calls are decided by the broker before the kernel resolves a target. + #[test] + fn setpriority_reports_native_errno_for_invalid_arguments() { + let results = mediated(|| { + // SAFETY: scalar arguments only; the broker decides before any + // target is resolved, so no call can be applied to a task. + unsafe { + ( + errno(libc::syscall( + libc::SYS_setpriority, + 42, + libc::c_long::from(libc::getpid()), + 0, + )), + errno(libc::syscall( + libc::SYS_setpriority, + libc::c_long::from(libc::PRIO_PROCESS), + -1, + 0, + )), + ) + } + }); + assert_eq!(results, (Some(libc::EINVAL), Some(libc::ESRCH))); + } + + /// A negative target keeps each mediated syscall's native errno. + /// + /// The `sched_setparam`/`sched_setscheduler`/`sched_setattr` trio rejects a + /// negative PID with EINVAL before any process lookup, while affinity, + /// priority, and limit calls fail their lookup with ESRCH. Both are usage + /// errors the broker must not turn into a policy denial. + #[test] + fn negative_targets_keep_each_syscalls_native_errno() { + let results = mediated(|| { + // SAFETY: scalar arguments and live stack buffers only; a negative + // target resolves to no task, so nothing is reconfigured. + unsafe { + let param: libc::sched_param = std::mem::zeroed(); + let mut attr: SchedAttr = std::mem::zeroed(); + attr.size = u32::try_from(size_of::()).expect("attribute size fits u32"); + let mask: libc::cpu_set_t = std::mem::zeroed(); + let mask_size = size_of::(); + ( + errno(libc::c_long::from(libc::sched_setparam( + -1, + &raw const param, + ))), + errno(libc::c_long::from(libc::sched_setscheduler( + -1, + libc::SCHED_OTHER, + &raw const param, + ))), + errno(libc::syscall( + libc::SYS_sched_setattr, + libc::c_long::from(-1), + &raw const attr, + 0, + )), + errno(libc::syscall( + libc::SYS_sched_setaffinity, + libc::c_long::from(-1), + mask_size, + &raw const mask, + )), + errno(libc::syscall( + libc::SYS_setpriority, + libc::c_long::from(libc::PRIO_PROCESS), + libc::c_long::from(-1), + 0, + )), + errno(libc::syscall( + libc::SYS_prlimit64, + libc::c_long::from(-1), + 0, + 0, + 0, + )), + ) + } + }); + assert_eq!( + results, + ( + Some(libc::EINVAL), + Some(libc::EINVAL), + Some(libc::EINVAL), + Some(libc::ESRCH), + Some(libc::ESRCH), + Some(libc::ESRCH), + ) + ); + } + + #[test] + fn scheduling_calls_report_esrch_for_a_vanished_target() { + let mut target = std::process::Command::new("sleep") + .arg("30") + .spawn() + .expect("spawn target"); + let target_tgid = libc::pid_t::try_from(target.id()).expect("target PID fits pid_t"); + target.kill().expect("kill target"); + target.wait().expect("reap target"); + let result = mediated(move || affinity_errno(target_tgid)); + assert_eq!(result, Some(libc::ESRCH)); + } } diff --git a/e2e/rust/e2e-podman.sh b/e2e/rust/e2e-podman.sh index d5a54741b7..3ba8cb2b2f 100755 --- a/e2e/rust/e2e-podman.sh +++ b/e2e/rust/e2e-podman.sh @@ -39,6 +39,7 @@ PODMAN_CI_TESTS=( podman_gateway_start podman_host_gateway provider_token_exchange + sandbox_compat_signals_sched ) if [ -n "${E2E_TEST}" ] && [ -n "${E2E_TEST_SET}" ]; then diff --git a/e2e/rust/tests/sandbox_compat_signals_sched.rs b/e2e/rust/tests/sandbox_compat_signals_sched.rs new file mode 100644 index 0000000000..4bc5a5d308 --- /dev/null +++ b/e2e/rust/tests/sandbox_compat_signals_sched.rs @@ -0,0 +1,191 @@ +// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +//! End-to-end compatibility for scheduling, priority, resource-limit, and +//! signal calls inside a sandbox. +//! +//! The static child filter used to deny these syscalls whatever their target, +//! so `pthread_setaffinity_np`, `taskset -p`, `prlimit(getpid())`, and `nice` +//! failed with EPERM. The broker now mediates them: the caller's own thread +//! group is allowed and every other target stays denied. + +#![cfg(feature = "e2e")] + +use openshell_e2e::harness::sandbox::SandboxGuard; + +const SUCCESS_MARKER: &str = "sandbox_compat_signals_sched=ok"; + +/// Exercise each mediated syscall from inside the workload. +/// +/// Own-group calls must succeed. Calls that name a target re-apply an affinity +/// mask or nice value read back first, which still exercises the mediated +/// syscall while avoiding EINVAL and EPERM on cpuset-restricted runners. For +/// the same reason every denied call would succeed natively: it re-applies +/// current settings to a same-UID process, so only the sandbox can refuse it, +/// and it must do so with EPERM. `os.nice` targets pid 0, the path that never +/// leaves the kernel. +/// +/// The signal handler records a plain counter that the main thread polls +/// instead of using a synchronization primitive: the handler runs in the main +/// thread at a bytecode boundary, so signalling an `Event` whose lock that same +/// thread is holding would deadlock. +/// +/// The queued signal goes to another process, the only path the broker +/// emulates, and its payload carries a different `si_signo` than the syscall +/// argument, which native `rt_sigqueueinfo` overwrites. +fn compat_script() -> &'static str { + r#" +import ctypes, errno, os, platform, resource, signal, struct, subprocess, sys, threading, time + +failures = [] +libc = ctypes.CDLL(None, use_errno=True) + +def report(name, error=None): + if error is None: + print(f"{name}=ok", flush=True) + return + print(f"{name}=fail:{error}", flush=True) + failures.append(name) + +def expect_denied(name, call): + try: + call() + report(name, "call was not denied") + except OSError as error: + report(name, None if error.errno == errno.EPERM else error) + +def affinity_probe(): + tid = threading.get_native_id() + try: + os.sched_setaffinity(tid, os.sched_getaffinity(0)) + report("affinity") + except OSError as error: + report("affinity", error) + +thread = threading.Thread(target=affinity_probe) +thread.start() +thread.join() + +received = 0 + +def on_sigusr1(_signo, _frame): + global received + received += 1 + +def signal_probe(name, target): + seen = received + try: + os.kill(target, signal.SIGUSR1) + except OSError as error: + report(name, error) + return + deadline = time.monotonic() + 10 + while received == seen and time.monotonic() < deadline: + time.sleep(0.01) + report(name, None if received > seen else "timed out waiting for the handler") + +signal.signal(signal.SIGUSR1, on_sigusr1) +signal_probe("signal", os.getpid()) + +# kill() resolves a sibling thread's TID to this process, so it is self-directed too. +release = threading.Event() +sibling = threading.Thread(target=release.wait) +sibling.start() +signal_probe("sibling_signal", sibling.native_id) +release.set() +sibling.join() + +mask = os.sched_getaffinity(0) +bystander = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(60)"]) +expect_denied("foreign_affinity", lambda: os.sched_setaffinity(bystander.pid, mask)) +expect_denied( + "foreign_nice", + lambda: os.setpriority( + os.PRIO_PROCESS, bystander.pid, os.getpriority(os.PRIO_PROCESS, bystander.pid) + ), +) +bystander.kill() +bystander.wait() +expect_denied("parent_affinity", lambda: os.sched_setaffinity(os.getppid(), mask)) + +queue_child = "\n".join([ + "import signal, sys", + "signal.pthread_sigmask(signal.SIG_BLOCK, {signal.SIGUSR2})", + "print('ready', flush=True)", + "info = signal.sigtimedwait({signal.SIGUSR2}, 10)", + "queued = info is not None and info.si_signo == signal.SIGUSR2 and info.si_code == -1", + "sys.exit(0 if queued else 3)", +]) +rt_sigqueueinfo = {"x86_64": 129, "aarch64": 138}.get(platform.machine()) +if rt_sigqueueinfo is None: + report("queued_signal", "no rt_sigqueueinfo number for " + platform.machine()) +else: + child = subprocess.Popen([sys.executable, "-c", queue_child], stdout=subprocess.PIPE, text=True) + child.stdout.readline() + # si_signo is SIGUSR1 on purpose; si_code -1 is SI_QUEUE. + info = ctypes.create_string_buffer(struct.pack("iii", signal.SIGUSR1, 0, -1), 128) + queued = libc.syscall( + ctypes.c_long(rt_sigqueueinfo), + ctypes.c_long(child.pid), + ctypes.c_long(signal.SIGUSR2), + info, + ) + if queued != 0: + report("queued_signal", os.strerror(ctypes.get_errno())) + child.kill() + child.wait() + else: + try: + status = child.wait(timeout=15) + report("queued_signal", None if status == 0 else f"receiver exited {status}") + except subprocess.TimeoutExpired: + report("queued_signal", "receiver timed out") + child.kill() + child.wait() + +try: + own = os.getpid() + os.setpriority(os.PRIO_PROCESS, own, os.getpriority(os.PRIO_PROCESS, own)) + report("own_nice") +except OSError as error: + report("own_nice", error) + +try: + os.nice(1) + report("nice") +except OSError as error: + report("nice", error) + +class Rlimit(ctypes.Structure): + _fields_ = [("current", ctypes.c_ulonglong), ("maximum", ctypes.c_ulonglong)] + +prlimit64 = getattr(libc, "prlimit64", None) +limits = Rlimit() +if prlimit64 is None: + report("prlimit64", "libc does not export prlimit64") +elif prlimit64(os.getpid(), resource.RLIMIT_CORE, None, ctypes.byref(limits)) != 0: + report("prlimit64", os.strerror(ctypes.get_errno())) +elif prlimit64(os.getpid(), resource.RLIMIT_CORE, ctypes.byref(limits), None) != 0: + report("prlimit64", os.strerror(ctypes.get_errno())) +else: + report("prlimit64") + +print("sandbox_compat_signals_sched=" + ("ok" if not failures else "fail"), flush=True) +sys.exit(0 if not failures else 1) +"# +} + +#[tokio::test] +async fn sandbox_allows_own_group_and_denies_foreign_scheduling_and_signals() { + let mut sandbox = SandboxGuard::create(&["--", "python3", "-c", compat_script()]) + .await + .expect("sandbox create"); + + assert!( + sandbox.create_output.contains(SUCCESS_MARKER), + "expected own-group probes to pass and foreign-target probes to be denied:\n{}", + sandbox.create_output + ); + + sandbox.cleanup().await; +}