From c194643398890eea2073842e3c80460b62298c2d Mon Sep 17 00:00:00 2001 From: Benjamin Leggett Date: Mon, 26 Oct 2026 14:20:29 -0400 Subject: [PATCH] fix(jail): explicitly mark all non-stdio fds as close-on-exec --- bin/styrojail.rs | 7 +++ src/config.rs | 6 +++ src/runner.rs | 10 ++++ src/wrap.rs | 138 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 161 insertions(+) diff --git a/bin/styrojail.rs b/bin/styrojail.rs index f158def..78ab3d5 100644 --- a/bin/styrojail.rs +++ b/bin/styrojail.rs @@ -50,6 +50,11 @@ struct Cli { #[arg(long, value_name = "key:value", value_parser = parse_resource_limit)] limit: Vec, + /// Pass N fds after stdio (3 through 3 + N - 1) through to the jailed program. + /// By default, the jailed program gets no fds besides stdio. + #[arg(long, value_name = "N", default_value_t = 0)] + preserve_fds: u32, + /// The program being jailed #[arg(value_name = "PROGRAM")] program: String, @@ -203,6 +208,8 @@ fn main() -> Result<()> { builder = builder.push_resource_limit(&lim.key, &lim.value); } + builder = builder.set_preserve_fds(cli.preserve_fds); + let req = builder.to_request(); let runner = Runner::new(&cli.styrolite_bin); runner.exec(req)?; diff --git a/src/config.rs b/src/config.rs index aa8368a..295cfd7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -94,6 +94,12 @@ pub struct ExecutableSpec { /// An optional out-of-memory score adjustment value. pub oom_score_adj: Option, + + /// How many fds after stdio (3 through 3 + N - 1) stay open across `execvpe()`, like + /// runc's `--preserve-fds`. By default, every fd above stdio is closed when `execvpe()` + /// succeeds. + #[serde(default)] + pub preserve_fds: u32, } #[derive(Default, Debug, Serialize, Deserialize)] diff --git a/src/runner.rs b/src/runner.rs index d47643b..f3152e7 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -90,6 +90,11 @@ impl AttachRequestBuilder { self } + pub fn set_preserve_fds(mut self, preserve_fds: u32) -> AttachRequestBuilder { + self.config.exec.preserve_fds = preserve_fds; + self + } + pub fn push_environment(mut self, key: &str, value: &str) -> AttachRequestBuilder { if self.config.exec.environment.is_none() { self.config.exec.environment = BTreeMap::new().into(); @@ -221,6 +226,11 @@ impl CreateRequestBuilder { self } + pub fn set_preserve_fds(mut self, preserve_fds: u32) -> CreateRequestBuilder { + self.config.exec.preserve_fds = preserve_fds; + self + } + pub fn set_oom_score_adj(mut self, score: i32) -> CreateRequestBuilder { self.config.exec.oom_score_adj = Some(score); self diff --git a/src/wrap.rs b/src/wrap.rs index 123138e..b2b1ca5 100644 --- a/src/wrap.rs +++ b/src/wrap.rs @@ -714,6 +714,40 @@ impl ExecutableSpec { env::set_current_dir(wd.clone())?; } + // Only stdio and the preserved fds may cross execve, so mark every other fd + // close-on-exec. This runs before seccomp so that a workload filter cannot refuse + // it. + let first_cloexec_fd = 3u32.saturating_add(self.preserve_fds); + let ret = unsafe { + libc::syscall( + libc::SYS_close_range, + first_cloexec_fd as libc::c_uint, + libc::c_uint::MAX, + libc::CLOSE_RANGE_CLOEXEC, + ) + }; + if ret < 0 { + let err = Error::last_os_error(); + // CLOSE_RANGE_CLOEXEC needs Linux 5.11. Older kernels answer ENOSYS, or EINVAL + // on 5.9 and 5.10, which have close_range() without the flag. + if !matches!(err.raw_os_error(), Some(libc::ENOSYS) | Some(libc::EINVAL)) { + bail!("failed to mark inherited fds close-on-exec: {err}"); + } + for entry in + fs::read_dir("/proc/self/fd").context("failed to list fds to mark close-on-exec")? + { + let fd: u32 = entry?.file_name().to_string_lossy().parse()?; + if fd >= first_cloexec_fd + && unsafe { libc::fcntl(fd as c_int, libc::F_SETFD, libc::FD_CLOEXEC) } < 0 + { + bail!( + "failed to mark fd {fd} close-on-exec: {}", + Error::last_os_error() + ); + } + } + } + if self.no_new_privs { self.set_no_new_privs()?; } @@ -1137,6 +1171,7 @@ mod tests { use crate::config::AttachRequest; use crate::config::{Capabilities, CreateRequest, ExecutableSpec}; use crate::namespace::Namespace; + use crate::seccomp::SeccompFilter; use crate::unshare::unshare; use nix::sys::wait::{WaitStatus, waitpid}; use nix::unistd::{ForkResult, fork, geteuid}; @@ -1426,4 +1461,107 @@ mod tests { assert_eq!(request.attach_cgroup_target().expect("target"), None); } + + /// An fd the caller left open without O_CLOEXEC must not reach the workload. + #[test] + fn execute_does_not_leak_inherited_fds() { + assert!(unsafe { + in_child(|| { + let fd = libc::open(c"/dev/null".as_ptr(), libc::O_RDONLY); + // dup2() clears FD_CLOEXEC on the new fd. + if fd < 0 || libc::dup2(fd, 9) < 0 { + return 1; + } + let spec = ExecutableSpec { + executable: Some("/bin/sh".to_string()), + arguments: Some(vec![ + "-c".to_string(), + "test ! -e /proc/self/fd/9".to_string(), + ]), + ..Default::default() + }; + let _ = spec.execute(); + 2 + }) + }); + } + + /// With `preserve_fds = 1`, fd 3 reaches the workload and fd 9 still does not. + #[test] + fn execute_keeps_preserved_fds() { + assert!(unsafe { + in_child(|| { + let fd = libc::open(c"/dev/null".as_ptr(), libc::O_RDONLY); + if fd < 0 || libc::dup2(fd, 3) < 0 || libc::dup2(fd, 9) < 0 { + return 1; + } + let spec = ExecutableSpec { + executable: Some("/bin/sh".to_string()), + arguments: Some(vec![ + "-c".to_string(), + "test -e /proc/self/fd/3 && test ! -e /proc/self/fd/9".to_string(), + ]), + preserve_fds: 1, + ..Default::default() + }; + let _ = spec.execute(); + 2 + }) + }); + } + + /// A seccomp filter that answers close_range() with ENOSYS stands in for a kernel older + /// than 5.9, which forces the /proc/self/fd fallback. + #[test] + fn execute_marks_fds_without_close_range() { + assert!(unsafe { + in_child(|| { + let fd = libc::open(c"/dev/null".as_ptr(), libc::O_RDONLY); + if fd < 0 || libc::dup2(fd, 3) < 0 || libc::dup2(fd, 9) < 0 { + return 1; + } + if libc::prctl(libc::PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0 { + return 2; + } + let filter = SeccompFilter { + instructions: vec![ + // Load seccomp_data.nr. + ((libc::BPF_LD | libc::BPF_W | libc::BPF_ABS) as u16, 0, 0, 0), + ( + (libc::BPF_JMP | libc::BPF_JEQ | libc::BPF_K) as u16, + 0, + 1, + libc::SYS_close_range as u32, + ), + ( + (libc::BPF_RET | libc::BPF_K) as u16, + 0, + 0, + libc::SECCOMP_RET_ERRNO | libc::ENOSYS as u32, + ), + ( + (libc::BPF_RET | libc::BPF_K) as u16, + 0, + 0, + libc::SECCOMP_RET_ALLOW, + ), + ], + }; + if filter.install().is_err() { + return 3; + } + let spec = ExecutableSpec { + executable: Some("/bin/sh".to_string()), + arguments: Some(vec![ + "-c".to_string(), + "test -e /proc/self/fd/3 && test ! -e /proc/self/fd/9".to_string(), + ]), + preserve_fds: 1, + ..Default::default() + }; + let _ = spec.execute(); + 4 + }) + }); + } }