Skip to content

Commit 33304c3

Browse files
committed
fix(sandbox): preserve process launcher fallbacks
Signed-off-by: Drew Newberry <anewberry@nvidia.com>
1 parent 0e36df2 commit 33304c3

1 file changed

Lines changed: 44 additions & 31 deletions

File tree

  • crates/openshell-sandbox/src/sandbox/linux

‎crates/openshell-sandbox/src/sandbox/linux/seccomp.rs‎

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ pub fn apply_supervisor_prelude() -> Result<()> {
7171
pub fn apply(policy: &SandboxPolicy) -> Result<()> {
7272
let allow_inet = matches!(policy.network.mode, NetworkMode::Proxy | NetworkMode::Allow);
7373
let main_filter = build_filter(allow_inet)?;
74-
let clone3_filter = build_clone3_filter()?;
74+
let compatibility_filter = build_compatibility_filter()?;
7575

7676
set_no_new_privs()?;
77-
apply_runtime_filters(&main_filter, &clone3_filter)?;
77+
apply_runtime_filters(&main_filter, &compatibility_filter)?;
7878

7979
Ok(())
8080
}
@@ -147,35 +147,36 @@ fn compile_filter(
147147
filter.try_into().into_diagnostic()
148148
}
149149

150-
/// Build a minimal BPF filter that blocks clone3 with ENOSYS.
150+
/// Build a minimal BPF filter for unavailable process APIs.
151151
///
152152
/// This is a separate filter from the main one because seccomp BPF cannot
153153
/// dereference the `struct clone_args *` pointer that clone3 takes as arg 0,
154154
/// so we cannot selectively block `CLONE_NEWUSER`. We block clone3
155155
/// unconditionally with ENOSYS so glibc falls back to the older clone
156156
/// syscall (where flags are a direct register argument and CAN be filtered).
157157
///
158-
/// glibc's clone3 wrapper checks for ENOSYS specifically — EPERM would be
159-
/// treated as a hard failure and propagated to the caller instead of
160-
/// triggering the clone fallback.
161-
fn build_clone3_filter() -> Result<seccompiler::BpfProgram> {
158+
/// glibc's clone3 wrapper and process launchers that opportunistically use
159+
/// pidfds check for ENOSYS specifically. EPERM is treated as a hard policy
160+
/// failure instead of triggering their portable fallback paths.
161+
fn build_compatibility_filter() -> Result<seccompiler::BpfProgram> {
162162
let mut rules: BTreeMap<i64, Vec<SeccompRule>> = BTreeMap::new();
163163
rules.entry(libc::SYS_clone3).or_default();
164+
rules.entry(libc::SYS_pidfd_open).or_default();
164165
compile_filter(rules, SeccompAction::Errno(libc::ENOSYS as u32))
165166
}
166167

167168
/// Install the sandbox seccomp filters in the required order.
168169
///
169170
/// Order matters:
170-
/// 1. Install the dedicated clone3 filter first so it can still call
171+
/// 1. Install the compatibility filter first so it can still call
171172
/// `seccomp(SECCOMP_SET_MODE_FILTER)`.
172173
/// 2. Install the main filter second. It blocks further seccomp filter
173174
/// installation with `EPERM`, preserving the original hardening intent.
174175
fn apply_runtime_filters(
175176
main_filter: seccompiler::BpfProgramRef<'_>,
176-
clone3_filter: seccompiler::BpfProgramRef<'_>,
177+
compatibility_filter: seccompiler::BpfProgramRef<'_>,
177178
) -> Result<()> {
178-
apply_filter(clone3_filter).into_diagnostic()?;
179+
apply_filter(compatibility_filter).into_diagnostic()?;
179180
apply_filter(main_filter).into_diagnostic()?;
180181
Ok(())
181182
}
@@ -226,8 +227,9 @@ fn build_filter_rules(allow_inet: bool) -> Result<BTreeMap<i64, Vec<SeccompRule>
226227
rules.entry(libc::SYS_process_vm_readv).or_default();
227228
// Cross-process memory write (symmetric with process_vm_readv).
228229
rules.entry(libc::SYS_process_vm_writev).or_default();
229-
// Process handle acquisition, fd theft, and signalling via pidfd.
230-
rules.entry(libc::SYS_pidfd_open).or_default();
230+
// Process fd theft and signalling via pidfd. pidfd_open is made
231+
// unavailable with ENOSYS by the compatibility filter so runtimes can
232+
// fall back without gaining a handle to the trusted sandbox boundary.
231233
rules.entry(libc::SYS_pidfd_getfd).or_default();
232234
rules.entry(libc::SYS_pidfd_send_signal).or_default();
233235
// Async I/O subsystem with extensive CVE history.
@@ -278,7 +280,7 @@ fn build_filter_rules(allow_inet: bool) -> Result<BTreeMap<i64, Vec<SeccompRule>
278280
0, // flags argument
279281
libc::CLONE_NEWUSER as u64,
280282
)?;
281-
// clone3 is handled by a separate filter — see build_clone3_filter().
283+
// clone3 is handled by the ENOSYS compatibility filter.
282284

283285
// seccomp(SECCOMP_SET_MODE_FILTER) would let sandboxed code replace the active filter.
284286
let condition = SeccompCondition::new(
@@ -425,7 +427,6 @@ mod tests {
425427
libc::SYS_bpf,
426428
libc::SYS_process_vm_readv,
427429
libc::SYS_process_vm_writev,
428-
libc::SYS_pidfd_open,
429430
libc::SYS_pidfd_getfd,
430431
libc::SYS_pidfd_send_signal,
431432
libc::SYS_io_uring_setup,
@@ -551,19 +552,25 @@ mod tests {
551552
}
552553

553554
#[test]
554-
fn clone3_filter_compiles_and_blocks_clone3() {
555-
let bpf = build_clone3_filter();
556-
assert!(bpf.is_ok(), "clone3 ENOSYS filter should compile");
555+
fn compatibility_filter_compiles() {
556+
let bpf = build_compatibility_filter();
557+
assert!(
558+
bpf.is_ok(),
559+
"process API compatibility filter should compile"
560+
);
557561
}
558562

559563
#[test]
560-
fn clone3_not_in_main_filter() {
561-
// clone3 must NOT be in the main filter; it has its own ENOSYS filter.
564+
fn compatibility_syscalls_are_not_in_main_filter() {
565+
// These APIs must NOT be in the EPERM filter; the compatibility filter
566+
// reports them unavailable so process launchers can fall back.
562567
let filter_rules = build_filter_rules(true).unwrap();
563-
assert!(
564-
!filter_rules.contains_key(&libc::SYS_clone3),
565-
"clone3 should not be in the main filter — it uses a separate ENOSYS filter"
566-
);
568+
for syscall in [libc::SYS_clone3, libc::SYS_pidfd_open] {
569+
assert!(
570+
!filter_rules.contains_key(&syscall),
571+
"syscall {syscall} should use the ENOSYS compatibility filter"
572+
);
573+
}
567574
}
568575

569576
// --- Behavioral tests ---
@@ -610,10 +617,10 @@ mod tests {
610617

611618
unsafe fn install_runtime_filters_in_child(
612619
main_filter: &seccompiler::BpfProgram,
613-
clone3_filter: &seccompiler::BpfProgram,
620+
compatibility_filter: &seccompiler::BpfProgram,
614621
) {
615622
libc::prctl(libc::PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
616-
if let Err(err) = apply_runtime_filters(main_filter, clone3_filter) {
623+
if let Err(err) = apply_runtime_filters(main_filter, compatibility_filter) {
617624
let msg = format!("failed to install runtime seccomp filters: {err}\n");
618625
libc::write(2, msg.as_ptr().cast(), msg.len());
619626
libc::_exit(1);
@@ -701,13 +708,13 @@ mod tests {
701708
// clone3 uses a separate filter that returns ENOSYS (not EPERM) so
702709
// glibc falls back to clone.
703710
let main_filter = build_filter(true).unwrap();
704-
let clone3_filter = build_clone3_filter().unwrap();
705-
// Apply in the same order as apply(): clone3 filter first, main filter second.
711+
let compatibility_filter = build_compatibility_filter().unwrap();
712+
// Apply in the same order as apply(): compatibility filter first, main filter second.
706713
let pid = unsafe { libc::fork() };
707714
assert!(pid >= 0, "fork failed");
708715
if pid == 0 {
709716
unsafe {
710-
install_runtime_filters_in_child(&main_filter, &clone3_filter);
717+
install_runtime_filters_in_child(&main_filter, &compatibility_filter);
711718
let ret = libc::syscall(libc::SYS_clone3, 0 as libc::c_ulong, 0 as libc::c_ulong);
712719
let errno = *libc::__errno_location();
713720
if ret == -1 && errno == libc::ENOSYS {
@@ -727,17 +734,23 @@ mod tests {
727734
);
728735
}
729736

737+
#[test]
738+
fn behavioral_pidfd_open_returns_enosys() {
739+
let filter = build_compatibility_filter().unwrap();
740+
unsafe { assert_blocked_in_child(&filter, libc::SYS_pidfd_open, libc::ENOSYS) };
741+
}
742+
730743
#[test]
731744
fn behavioral_third_filter_install_blocked_after_startup() {
732745
let main_filter = build_filter(true).unwrap();
733-
let clone3_filter = build_clone3_filter().unwrap();
734-
let third_filter = build_clone3_filter().unwrap();
746+
let compatibility_filter = build_compatibility_filter().unwrap();
747+
let third_filter = build_compatibility_filter().unwrap();
735748

736749
let pid = unsafe { libc::fork() };
737750
assert!(pid >= 0, "fork failed");
738751
if pid == 0 {
739752
unsafe {
740-
install_runtime_filters_in_child(&main_filter, &clone3_filter);
753+
install_runtime_filters_in_child(&main_filter, &compatibility_filter);
741754
match apply_filter(&third_filter) {
742755
Err(seccompiler::Error::Seccomp(e))
743756
if e.raw_os_error() == Some(libc::EPERM) =>

0 commit comments

Comments
 (0)