Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions bin/styrojail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ struct Cli {
#[arg(long, value_name = "key:value", value_parser = parse_resource_limit)]
limit: Vec<ResourceLimit>,

/// 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,
Expand Down Expand Up @@ -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)?;
Expand Down
6 changes: 6 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ pub struct ExecutableSpec {

/// An optional out-of-memory score adjustment value.
pub oom_score_adj: Option<i32>,

/// 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)]
Expand Down
10 changes: 10 additions & 0 deletions src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down
138 changes: 138 additions & 0 deletions src/wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
}
Expand Down Expand Up @@ -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};
Expand Down Expand Up @@ -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
})
});
}
}
Loading