diff --git a/crates/openshell-sandbox/src/linux/child_seccomp.rs b/crates/openshell-sandbox/src/linux/child_seccomp.rs index 1d37444125..c3b687a3d6 100644 --- a/crates/openshell-sandbox/src/linux/child_seccomp.rs +++ b/crates/openshell-sandbox/src/linux/child_seccomp.rs @@ -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 @@ -149,7 +150,7 @@ pub fn prepare(sandbox_tgid: u32) -> io::Result { /// 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 { build(sandbox_tgid, Mediation::Unbrokered) } @@ -223,7 +224,6 @@ fn build(sandbox_tgid: u32, mediation: Mediation) -> io::Result io::Result, + /// Selector that must also hold before a zero PID names only the caller. + selector: Option, } impl MediatedSyscall { @@ -70,6 +83,14 @@ 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. /// @@ -77,6 +98,7 @@ impl MediatedSyscall { /// the kernel truncates, so only the low 32 bits of each argument count. pub(crate) fn caller_only_arguments(&self) -> impl Iterator { self.selector + .map(|selector| (selector.argument, selector.process)) .into_iter() .chain(std::iter::once((self.pid_argument, 0))) } @@ -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 @@ -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. @@ -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 @@ -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)]); @@ -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)]); } diff --git a/crates/openshell-sandbox/src/linux/seccomp_notify.rs b/crates/openshell-sandbox/src/linux/seccomp_notify.rs index b45dbccd93..7a613b9132 100644 --- a/crates/openshell-sandbox/src/linux/seccomp_notify.rs +++ b/crates/openshell-sandbox/src/linux/seccomp_notify.rs @@ -349,11 +349,11 @@ pub fn install_listener(syscalls: &[i64]) -> io::Result { /// 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 { #[allow(unused_mut)] // SYS_open is unavailable on some architectures. let mut syscalls = vec![ diff --git a/crates/openshell-sandbox/src/network_broker.rs b/crates/openshell-sandbox/src/network_broker.rs index 761f281ba3..fac8d9e2fd 100644 --- a/crates/openshell-sandbox/src/network_broker.rs +++ b/crates/openshell-sandbox/src/network_broker.rs @@ -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 { + // 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 diff --git a/e2e/rust/tests/sandbox_compat_signals_sched.rs b/e2e/rust/tests/sandbox_compat_signals_sched.rs index 4bc5a5d308..e7c345ada2 100644 --- a/e2e/rust/tests/sandbox_compat_signals_sched.rs +++ b/e2e/rust/tests/sandbox_compat_signals_sched.rs @@ -95,6 +95,15 @@ signal_probe("sibling_signal", sibling.native_id) release.set() sibling.join() +ioprio_numbers = {"x86_64": (251, 252), "aarch64": (30, 31)}.get(platform.machine()) + +def reapply_ioprio(pid): + set_number, get_number = ioprio_numbers + who = ctypes.c_long(pid) + current = libc.syscall(ctypes.c_long(get_number), ctypes.c_long(1), who) + if current < 0 or libc.syscall(ctypes.c_long(set_number), ctypes.c_long(1), who, ctypes.c_long(current)) != 0: + raise OSError(ctypes.get_errno(), os.strerror(ctypes.get_errno())) + 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)) @@ -104,6 +113,10 @@ expect_denied( os.PRIO_PROCESS, bystander.pid, os.getpriority(os.PRIO_PROCESS, bystander.pid) ), ) +if ioprio_numbers is None: + report("foreign_ioprio", "no ioprio numbers for " + platform.machine()) +else: + expect_denied("foreign_ioprio", lambda: reapply_ioprio(bystander.pid)) bystander.kill() bystander.wait() expect_denied("parent_affinity", lambda: os.sched_setaffinity(os.getppid(), mask)) @@ -150,6 +163,13 @@ try: except OSError as error: report("own_nice", error) +try: + reapply_ioprio(0) + reapply_ioprio(os.getpid()) + report("own_ioprio") +except (OSError, TypeError) as error: + report("own_ioprio", error) + try: os.nice(1) report("nice")