|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//! One-listener launch thread for capability-free workload descendants. |
| 5 | +//! |
| 6 | +//! Seccomp filters are per-thread. This launcher installs the networking |
| 7 | +//! listener without TSYNC, then serializes every fork/exec operation on that |
| 8 | +//! thread. Children inherit the filter while the sandbox's broker and |
| 9 | +//! lifecycle threads remain unfiltered. The listener moves to the caller over |
| 10 | +//! an in-process channel; no descriptor handoff syscall or reusable exception |
| 11 | +//! is needed. |
| 12 | +
|
| 13 | +use std::io; |
| 14 | +use std::sync::Arc; |
| 15 | +use std::sync::atomic::{AtomicBool, Ordering}; |
| 16 | +use std::sync::mpsc; |
| 17 | +use std::thread; |
| 18 | + |
| 19 | +use super::seccomp_notify::{NotificationListener, install_workload_listener}; |
| 20 | + |
| 21 | +type LaunchJob = Box<dyn FnOnce() + Send + 'static>; |
| 22 | + |
| 23 | +/// Serialized child-launch executor whose thread owns the inherited listener |
| 24 | +/// filter. |
| 25 | +#[derive(Clone)] |
| 26 | +pub struct WorkloadLauncher { |
| 27 | + jobs: mpsc::SyncSender<LaunchJob>, |
| 28 | + alive: Arc<AtomicBool>, |
| 29 | +} |
| 30 | + |
| 31 | +impl WorkloadLauncher { |
| 32 | + /// Execute one prebuilt spawn operation on the filtered launcher thread. |
| 33 | + /// |
| 34 | + /// The closure must only perform audited launch work. It must not open an |
| 35 | + /// INET socket itself: the launcher is trusted and deliberately has no |
| 36 | + /// notification broker. |
| 37 | + pub fn execute<T: Send + 'static>( |
| 38 | + &self, |
| 39 | + operation: impl FnOnce() -> T + Send + 'static, |
| 40 | + ) -> io::Result<T> { |
| 41 | + if !self.alive.load(Ordering::Acquire) { |
| 42 | + return Err(io::Error::new( |
| 43 | + io::ErrorKind::BrokenPipe, |
| 44 | + "workload launcher is not running", |
| 45 | + )); |
| 46 | + } |
| 47 | + let (result_tx, result_rx) = mpsc::sync_channel(1); |
| 48 | + self.jobs |
| 49 | + .send(Box::new(move || { |
| 50 | + let _ = result_tx.send(operation()); |
| 51 | + })) |
| 52 | + .map_err(|_| io::Error::new(io::ErrorKind::BrokenPipe, "workload launcher stopped"))?; |
| 53 | + result_rx.recv().map_err(|_| { |
| 54 | + io::Error::new( |
| 55 | + io::ErrorKind::BrokenPipe, |
| 56 | + "workload launcher dropped the spawn result", |
| 57 | + ) |
| 58 | + }) |
| 59 | + } |
| 60 | + |
| 61 | + /// Whether the launch thread is still able to accept work. |
| 62 | + #[must_use] |
| 63 | + pub fn is_alive(&self) -> bool { |
| 64 | + self.alive.load(Ordering::Acquire) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +/// Start the only workload launcher and return its listener to an unfiltered |
| 69 | +/// sandbox thread. |
| 70 | +pub fn start() -> io::Result<(WorkloadLauncher, NotificationListener)> { |
| 71 | + let (jobs_tx, jobs_rx) = mpsc::sync_channel::<LaunchJob>(64); |
| 72 | + let (ready_tx, ready_rx) = mpsc::sync_channel(1); |
| 73 | + let alive = Arc::new(AtomicBool::new(true)); |
| 74 | + let thread_alive = alive.clone(); |
| 75 | + thread::Builder::new() |
| 76 | + .name("openshell-workload-launcher".to_string()) |
| 77 | + .spawn(move || { |
| 78 | + match install_workload_listener() { |
| 79 | + Ok(listener) => { |
| 80 | + if ready_tx.send(Ok(listener)).is_err() { |
| 81 | + thread_alive.store(false, Ordering::Release); |
| 82 | + return; |
| 83 | + } |
| 84 | + } |
| 85 | + Err(error) => { |
| 86 | + let _ = ready_tx.send(Err(io::Error::new( |
| 87 | + error.kind(), |
| 88 | + format!("install workload listener: {error}"), |
| 89 | + ))); |
| 90 | + thread_alive.store(false, Ordering::Release); |
| 91 | + return; |
| 92 | + } |
| 93 | + } |
| 94 | + while let Ok(job) = jobs_rx.recv() { |
| 95 | + job(); |
| 96 | + } |
| 97 | + thread_alive.store(false, Ordering::Release); |
| 98 | + }) |
| 99 | + .map_err(|error| io::Error::other(format!("start workload launcher thread: {error}")))?; |
| 100 | + |
| 101 | + let listener = ready_rx.recv().map_err(|_| { |
| 102 | + io::Error::new( |
| 103 | + io::ErrorKind::BrokenPipe, |
| 104 | + "workload launcher exited before publishing its listener", |
| 105 | + ) |
| 106 | + })??; |
| 107 | + Ok(( |
| 108 | + WorkloadLauncher { |
| 109 | + jobs: jobs_tx, |
| 110 | + alive, |
| 111 | + }, |
| 112 | + listener, |
| 113 | + )) |
| 114 | +} |
| 115 | + |
| 116 | +#[cfg(test)] |
| 117 | +#[allow(unsafe_code)] |
| 118 | +mod tests { |
| 119 | + use std::mem::size_of; |
| 120 | + use std::os::fd::{AsRawFd as _, FromRawFd as _, OwnedFd}; |
| 121 | + |
| 122 | + use super::*; |
| 123 | + |
| 124 | + #[test] |
| 125 | + fn one_listener_mediates_launcher_and_inherited_child() { |
| 126 | + let (launcher, listener) = start().expect("start launcher"); |
| 127 | + let executable = std::env::current_exe().expect("test executable"); |
| 128 | + let mut child = launcher |
| 129 | + .execute(move || { |
| 130 | + let mut command = std::process::Command::new(executable); |
| 131 | + command |
| 132 | + .arg("--exact") |
| 133 | + .arg("linux::workload_launcher::tests::inherited_listener_child") |
| 134 | + .arg("--nocapture") |
| 135 | + .env("OPENSHELL_WORKLOAD_LAUNCHER_CHILD", "1"); |
| 136 | + command.spawn() |
| 137 | + }) |
| 138 | + .expect("launcher result") |
| 139 | + .expect("spawn child"); |
| 140 | + let notification = listener.receive().expect("receive child socket"); |
| 141 | + assert_eq!(i64::from(notification.syscall), libc::SYS_socket); |
| 142 | + assert!( |
| 143 | + std::path::Path::new(&format!("/proc/{}/task/{}", child.id(), notification.tid)) |
| 144 | + .exists() |
| 145 | + ); |
| 146 | + // SAFETY: eventfd returns one newly owned descriptor on success. |
| 147 | + let eventfd = unsafe { libc::eventfd(7, libc::EFD_CLOEXEC) }; |
| 148 | + assert!(eventfd >= 0, "eventfd: {}", io::Error::last_os_error()); |
| 149 | + // SAFETY: successful eventfd returned one owned descriptor. |
| 150 | + let eventfd = unsafe { OwnedFd::from_raw_fd(eventfd) }; |
| 151 | + listener |
| 152 | + .add_fd_and_send(notification.id, eventfd.as_raw_fd(), true) |
| 153 | + .expect("inject child descriptor"); |
| 154 | + assert!(child.wait().expect("wait child").success()); |
| 155 | + assert!(launcher.is_alive()); |
| 156 | + assert!(listener.as_raw_fd() >= 0); |
| 157 | + } |
| 158 | + |
| 159 | + #[test] |
| 160 | + fn inherited_listener_child() { |
| 161 | + if std::env::var_os("OPENSHELL_WORKLOAD_LAUNCHER_CHILD").is_none() { |
| 162 | + return; |
| 163 | + } |
| 164 | + // SAFETY: the inherited listener intercepts this scalar socket call |
| 165 | + // and returns the descriptor injected by the parent test. |
| 166 | + let descriptor = unsafe { |
| 167 | + libc::socket( |
| 168 | + libc::AF_INET, |
| 169 | + libc::SOCK_STREAM | libc::SOCK_CLOEXEC, |
| 170 | + libc::IPPROTO_TCP, |
| 171 | + ) |
| 172 | + }; |
| 173 | + assert!(descriptor >= 0, "socket: {}", io::Error::last_os_error()); |
| 174 | + let mut value = 0_u64; |
| 175 | + // SAFETY: the broker injected an eventfd and `value` is live storage. |
| 176 | + let read = unsafe { |
| 177 | + libc::read( |
| 178 | + descriptor, |
| 179 | + std::ptr::addr_of_mut!(value).cast(), |
| 180 | + size_of::<u64>(), |
| 181 | + ) |
| 182 | + }; |
| 183 | + // SAFETY: descriptor is owned by this process. |
| 184 | + unsafe { libc::close(descriptor) }; |
| 185 | + assert_eq!( |
| 186 | + read, |
| 187 | + isize::try_from(size_of::<u64>()).expect("u64 size fits") |
| 188 | + ); |
| 189 | + assert_eq!(value, 7); |
| 190 | + } |
| 191 | +} |
0 commit comments