Skip to content

Commit 42138bd

Browse files
committed
feat(isolation): add one-listener workload launcher
Signed-off-by: Drew Newberry <anewberry@nvidia.com>
1 parent a59e4cc commit 42138bd

3 files changed

Lines changed: 242 additions & 0 deletions

File tree

‎crates/openshell-isolation-interface/src/linux/mod.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ pub mod proc_fd;
1212
pub mod seccomp_notify;
1313
pub mod socket_registry;
1414
pub mod task_memory;
15+
pub mod workload_launcher;

‎crates/openshell-isolation-interface/src/linux/seccomp_notify.rs‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const SECCOMP_DATA_ARGS_OFFSET: u32 = 16;
3939
const X32_SYSCALL_BIT: u32 = 0x4000_0000;
4040

4141
const SECCOMP_ADDFD_FLAG_SEND: u32 = 1 << 1;
42+
const SECCOMP_USER_NOTIF_FLAG_CONTINUE: u32 = 1;
4243

4344
const CONNECTED_SEND_FLAGS: u32 =
4445
(libc::MSG_DONTWAIT | libc::MSG_EOR | libc::MSG_MORE | libc::MSG_NOSIGNAL | libc::MSG_OOB)
@@ -181,6 +182,12 @@ pub struct NotificationListener {
181182
}
182183

183184
impl NotificationListener {
185+
/// Raw listener descriptor for readiness integration and diagnostics.
186+
#[must_use]
187+
pub fn as_raw_fd(&self) -> RawFd {
188+
self.fd.as_raw_fd()
189+
}
190+
184191
/// Whether the listener was installed with killable receive waits.
185192
#[must_use]
186193
pub fn wait_killable_recv(&self) -> bool {
@@ -254,6 +261,26 @@ impl NotificationListener {
254261
Ok(())
255262
}
256263

264+
/// Continue a verified local-kernel operation in the notifying task.
265+
///
266+
/// Callers must not use this for an external INET operation or where a
267+
/// mutable workload pointer is part of the authorization decision.
268+
pub fn respond_continue(&self, id: u64) -> io::Result<()> {
269+
self.validate_id(id)?;
270+
let mut response = RawResponse {
271+
id,
272+
val: 0,
273+
error: 0,
274+
flags: SECCOMP_USER_NOTIF_FLAG_CONTINUE,
275+
};
276+
ioctl_ptr(
277+
self.fd.as_raw_fd(),
278+
SECCOMP_IOCTL_NOTIF_SEND,
279+
std::ptr::addr_of_mut!(response).cast(),
280+
)?;
281+
Ok(())
282+
}
283+
257284
/// Atomically inject `source` and complete the notifying syscall with the
258285
/// allocated target FD. The target receives `O_CLOEXEC` when requested.
259286
pub fn add_fd_and_send(
@@ -310,6 +337,29 @@ pub fn install_listener(syscalls: &[i64]) -> io::Result<NotificationListener> {
310337
}
311338
}
312339

340+
/// Install the capability-free workload networking listener on the calling
341+
/// launcher thread.
342+
///
343+
/// The filter mediates every syscall that can create, select, or materially
344+
/// reconfigure an INET endpoint. Connected `send()`/null-destination
345+
/// `sendto()` retains the audited cBPF fast path.
346+
pub fn install_workload_listener() -> io::Result<NotificationListener> {
347+
install_listener(&[
348+
libc::SYS_socket,
349+
libc::SYS_connect,
350+
libc::SYS_bind,
351+
libc::SYS_listen,
352+
libc::SYS_accept,
353+
libc::SYS_accept4,
354+
libc::SYS_sendto,
355+
libc::SYS_sendmsg,
356+
libc::SYS_sendmmsg,
357+
libc::SYS_getpeername,
358+
libc::SYS_getsockname,
359+
libc::SYS_setsockopt,
360+
])
361+
}
362+
313363
/// Run a no-capability conformance probe.
314364
///
315365
/// This uses the production launcher-thread shape: the listener is created on
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
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

Comments
 (0)