Skip to content

Commit 8e95cd7

Browse files
committed
fix(isolation): preserve safe child creation
Signed-off-by: Drew Newberry <anewberry@nvidia.com>
1 parent 5a4cd62 commit 8e95cd7

2 files changed

Lines changed: 160 additions & 9 deletions

File tree

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

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ const F_SETSIG_COMMAND: u32 = 10;
3434
const F_SETOWN_EX_COMMAND: u32 = 15;
3535
const FIOSETOWN_REQUEST: u32 = 0x8901;
3636
const SIOCSPGRP_REQUEST: u32 = 0x8902;
37+
const CLONE_NAMESPACE_FLAGS: u32 = (libc::CLONE_NEWCGROUP
38+
| libc::CLONE_NEWIPC
39+
| libc::CLONE_NEWNET
40+
| libc::CLONE_NEWNS
41+
| libc::CLONE_NEWPID
42+
| libc::CLONE_NEWUSER
43+
| libc::CLONE_NEWUTS) as u32;
3744

3845
/// A prebuilt child filter that can be installed without heap allocation.
3946
pub struct ChildHardeningProgram {
@@ -151,11 +158,17 @@ pub fn prepare(sandbox_tgid: u32) -> io::Result<ChildHardeningProgram> {
151158
libc::SYS_setdomainname,
152159
libc::SYS_setpriority,
153160
libc::SYS_ioprio_set,
154-
libc::SYS_clone3,
155161
] {
156162
append_unconditional_deny(&mut instructions, syscall)?;
157163
}
158164

165+
// glibc falls back from clone3 to clone only for ENOSYS. Returning EPERM
166+
// here breaks pthread_create and posix_spawn on modern glibc. The legacy
167+
// clone path remains available for ordinary process/thread creation, but
168+
// namespace creation is denied from its scalar flags argument.
169+
append_unconditional_errno(&mut instructions, libc::SYS_clone3, libc::ENOSYS)?;
170+
append_argument_masked_deny(&mut instructions, libc::SYS_clone, 0, CLONE_NAMESPACE_FLAGS)?;
171+
159172
for (syscall, argument) in [
160173
(libc::SYS_kill, 0),
161174
(libc::SYS_tgkill, 0),
@@ -164,6 +177,12 @@ pub fn prepare(sandbox_tgid: u32) -> io::Result<ChildHardeningProgram> {
164177
] {
165178
append_argument_equal_deny(&mut instructions, syscall, argument, sandbox_tgid)?;
166179
}
180+
for syscall in [libc::SYS_kill, libc::SYS_rt_sigqueueinfo] {
181+
// PID zero targets the caller's process group. Deny it even though
182+
// OpenShell normally gives each workload a dedicated process group:
183+
// an untrusted child can otherwise rejoin a trusted group first.
184+
append_argument_equal_deny(&mut instructions, syscall, 0, 0)?;
185+
}
167186
// Negative PID arguments target process groups or every signalable
168187
// process. The workload never needs that authority and must not be able
169188
// to include the trusted sandbox workers in a broad signal operation.
@@ -213,6 +232,20 @@ fn append_unconditional_deny(
213232
Ok(())
214233
}
215234

235+
fn append_unconditional_errno(
236+
instructions: &mut Vec<libc::sock_filter>,
237+
syscall: i64,
238+
error: i32,
239+
) -> io::Result<()> {
240+
let syscall = syscall_number(syscall)?;
241+
instructions.extend([
242+
stmt(BPF_LD_W_ABS, SECCOMP_DATA_NR_OFFSET),
243+
jump(BPF_JMP_JEQ_K, syscall, 0, 1),
244+
errno(error),
245+
]);
246+
Ok(())
247+
}
248+
216249
fn append_argument_equal_deny(
217250
instructions: &mut Vec<libc::sock_filter>,
218251
syscall: i64,
@@ -367,6 +400,11 @@ mod tests {
367400
{
368401
unsafe { libc::_exit(4) };
369402
}
403+
if unsafe { libc::kill(0, 0) } != -1
404+
|| io::Error::last_os_error().raw_os_error() != Some(libc::EPERM)
405+
{
406+
unsafe { libc::_exit(10) };
407+
}
370408
let sandbox_group = -unsafe { libc::getpgrp() };
371409
if unsafe { libc::kill(sandbox_group, 0) } != -1
372410
|| io::Error::last_os_error().raw_os_error() != Some(libc::EPERM)
@@ -405,4 +443,63 @@ mod tests {
405443
assert!(libc::WIFEXITED(status));
406444
assert_eq!(libc::WEXITSTATUS(status), 0);
407445
}
446+
447+
#[test]
448+
fn filter_preserves_thread_and_process_creation() {
449+
if std::env::var_os("OPENSHELL_CHILD_SECCOMP_CREATION_PROBE").is_some() {
450+
let mut filter = prepare(std::process::id().saturating_add(1))
451+
.expect("prepare child hardening filter");
452+
filter.install().expect("install child hardening filter");
453+
454+
// A direct clone3 request must report ENOSYS so libc can use its
455+
// established clone fallback.
456+
let result =
457+
unsafe { libc::syscall(libc::SYS_clone3, std::ptr::null::<libc::c_void>(), 0) };
458+
assert_eq!(result, -1);
459+
assert_eq!(
460+
io::Error::last_os_error().raw_os_error(),
461+
Some(libc::ENOSYS)
462+
);
463+
464+
let joined = std::thread::spawn(|| 17_u8)
465+
.join()
466+
.expect("pthread-style child must start");
467+
assert_eq!(joined, 17);
468+
assert!(
469+
std::process::Command::new("/bin/true")
470+
.status()
471+
.expect("posix-spawn-style child must start")
472+
.success()
473+
);
474+
475+
let namespaced = unsafe {
476+
libc::syscall(
477+
libc::SYS_clone,
478+
u64::from(CLONE_NAMESPACE_FLAGS & libc::CLONE_NEWUSER as u32)
479+
| u64::from(libc::SIGCHLD as u32),
480+
0,
481+
0,
482+
0,
483+
0,
484+
)
485+
};
486+
assert_eq!(namespaced, -1);
487+
assert_eq!(io::Error::last_os_error().raw_os_error(), Some(libc::EPERM));
488+
return;
489+
}
490+
491+
let output =
492+
std::process::Command::new(std::env::current_exe().expect("current test executable"))
493+
.arg("--exact")
494+
.arg("linux::child_seccomp::tests::filter_preserves_thread_and_process_creation")
495+
.arg("--nocapture")
496+
.env("OPENSHELL_CHILD_SECCOMP_CREATION_PROBE", "1")
497+
.output()
498+
.expect("run isolated child-hardening probe");
499+
assert!(
500+
output.status.success(),
501+
"isolated probe failed: {}",
502+
String::from_utf8_lossy(&output.stderr)
503+
);
504+
}
408505
}

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

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use std::mem::size_of;
1515
use std::os::fd::{AsRawFd, FromRawFd, OwnedFd, RawFd};
1616
use std::sync::mpsc;
1717
use std::thread;
18+
use std::time::Duration;
1819

1920
const SECCOMP_SET_MODE_FILTER: libc::c_uint = 1;
2021
const SECCOMP_GET_NOTIF_SIZES: libc::c_uint = 3;
@@ -44,6 +45,7 @@ const SECCOMP_USER_NOTIF_FLAG_CONTINUE: u32 = 1;
4445
const CONNECTED_SEND_FLAGS: u32 =
4546
(libc::MSG_DONTWAIT | libc::MSG_EOR | libc::MSG_MORE | libc::MSG_NOSIGNAL | libc::MSG_OOB)
4647
as u32;
48+
const PROBE_NOTIFICATION_TIMEOUT: Duration = Duration::from_secs(5);
4749

4850
const IOC_NRBITS: u32 = 8;
4951
const IOC_TYPEBITS: u32 = 8;
@@ -393,7 +395,13 @@ fn probe_scalar_round_trip() -> io::Result<bool> {
393395
let (listener, wait_killable) = receiver
394396
.recv()
395397
.map_err(|_| io::Error::other("notification launcher disappeared"))?;
396-
let notification = listener.receive()?;
398+
let notification = match receive_probe_notification(&listener) {
399+
Ok(notification) => notification,
400+
Err(error) => {
401+
let _ = launcher.join();
402+
return Err(error);
403+
}
404+
};
397405
if i64::from(notification.syscall) != libc::SYS_getppid {
398406
return Err(io::Error::other("unexpected scalar probe syscall"));
399407
}
@@ -429,8 +437,10 @@ fn probe_addfd_send() -> io::Result<()> {
429437
}
430438
let injected = RawFd::try_from(injected)
431439
.map_err(|_| io::Error::other("injected descriptor does not fit RawFd"))?;
440+
// SAFETY: ADDFD-SEND returned one newly owned descriptor to this task.
441+
let injected = unsafe { OwnedFd::from_raw_fd(injected) };
432442
// SAFETY: `injected` was returned as an open descriptor by the kernel.
433-
let descriptor_flags = unsafe { libc::fcntl(injected, libc::F_GETFD) };
443+
let descriptor_flags = unsafe { libc::fcntl(injected.as_raw_fd(), libc::F_GETFD) };
434444
if descriptor_flags < 0 {
435445
return Err(io::Error::last_os_error());
436446
}
@@ -441,13 +451,11 @@ fn probe_addfd_send() -> io::Result<()> {
441451
// SAFETY: eventfd reads exactly one u64 into a valid aligned pointer.
442452
let read = unsafe {
443453
libc::read(
444-
injected,
454+
injected.as_raw_fd(),
445455
std::ptr::addr_of_mut!(value).cast(),
446456
size_of::<u64>(),
447457
)
448458
};
449-
// SAFETY: close consumes the descriptor returned by the kernel.
450-
unsafe { libc::close(injected) };
451459
let word_size = isize::try_from(size_of::<u64>()).expect("u64 size fits isize");
452460
if read != word_size || value != 7 {
453461
return Err(io::Error::other("injected eventfd was not usable"));
@@ -458,7 +466,13 @@ fn probe_addfd_send() -> io::Result<()> {
458466
let listener = receiver
459467
.recv()
460468
.map_err(|_| io::Error::other("ADDFD launcher disappeared"))?;
461-
let notification = listener.receive()?;
469+
let notification = match receive_probe_notification(&listener) {
470+
Ok(notification) => notification,
471+
Err(error) => {
472+
let _ = launcher.join();
473+
return Err(error);
474+
}
475+
};
462476
if i64::from(notification.syscall) != libc::SYS_socket {
463477
return Err(io::Error::other("unexpected ADDFD probe syscall"));
464478
}
@@ -641,7 +655,13 @@ fn probe_connected_sendto_fast_path() -> io::Result<()> {
641655
let listener = receiver
642656
.recv()
643657
.map_err(|_| io::Error::other("sendto launcher disappeared"))?;
644-
let destination = listener.receive()?;
658+
let destination = match receive_probe_notification(&listener) {
659+
Ok(notification) => notification,
660+
Err(error) => {
661+
let _ = launcher.join();
662+
return Err(error);
663+
}
664+
};
645665
if i64::from(destination.syscall) != libc::SYS_sendto
646666
|| destination.args[4] == 0
647667
|| destination.args[5] == 0
@@ -652,7 +672,13 @@ fn probe_connected_sendto_fast_path() -> io::Result<()> {
652672
}
653673
listener.respond_errno(destination.id, libc::EACCES)?;
654674

655-
let fast_open = listener.receive()?;
675+
let fast_open = match receive_probe_notification(&listener) {
676+
Ok(notification) => notification,
677+
Err(error) => {
678+
let _ = launcher.join();
679+
return Err(error);
680+
}
681+
};
656682
if i64::from(fast_open.syscall) != libc::SYS_sendto
657683
|| fast_open.args[4] != 0
658684
|| fast_open.args[5] != 0
@@ -670,6 +696,34 @@ fn probe_connected_sendto_fast_path() -> io::Result<()> {
670696
Ok(())
671697
}
672698

699+
fn receive_probe_notification(listener: &NotificationListener) -> io::Result<Notification> {
700+
let mut descriptor = libc::pollfd {
701+
fd: listener.as_raw_fd(),
702+
events: libc::POLLIN | libc::POLLHUP,
703+
revents: 0,
704+
};
705+
let timeout = i32::try_from(PROBE_NOTIFICATION_TIMEOUT.as_millis())
706+
.expect("probe timeout fits poll milliseconds");
707+
// SAFETY: descriptor points to one live pollfd for the duration of poll.
708+
let ready = unsafe { libc::poll(&raw mut descriptor, 1, timeout) };
709+
if ready < 0 {
710+
return Err(io::Error::last_os_error());
711+
}
712+
if ready == 0 {
713+
return Err(io::Error::new(
714+
io::ErrorKind::TimedOut,
715+
"seccomp notification probe timed out",
716+
));
717+
}
718+
if descriptor.revents & libc::POLLIN == 0 {
719+
return Err(io::Error::new(
720+
io::ErrorKind::BrokenPipe,
721+
"seccomp notification probe listener closed",
722+
));
723+
}
724+
listener.receive()
725+
}
726+
673727
fn install_listener_with_flags(
674728
syscalls: &[i64],
675729
wait_killable_recv: bool,

0 commit comments

Comments
 (0)