#![allow(unsafe_code)]
use crate::error::{Error, Result};
use nix::sys::wait::{WaitStatus, waitpid};
use nix::unistd::{ForkResult, Pid, fork, pipe};
use std::ffi::CString;
use std::io::Read;
use std::os::fd::{AsRawFd, OwnedFd};
use std::time::{Duration, Instant};
pub fn jexec_with_output(jid: i32, command: &[&str]) -> Result<(i32, Vec<u8>, Vec<u8>)> {
check_nonempty_command(command)?;
let argv = ExecArgv::new(command)?;
let (stdout_read, stdout_write) = jexec_pipe("stdout")?;
let (stderr_read, stderr_write) = jexec_pipe("stderr")?;
match unsafe { fork() } {
Ok(ForkResult::Parent { child }) => {
drop(stdout_write);
drop(stderr_write);
let readers = OutputReaders::spawn(stdout_read, stderr_read);
let _ = crate::proc::wait_exited(child, true);
kill_process_group(child);
let result = wait_child(child, jid, command);
let (stdout, stderr) = readers.join();
result.map(|code| (code, stdout, stderr))
}
Ok(ForkResult::Child) => piped_child(
ChildPipes {
stdout_read,
stderr_read,
stdout_write,
stderr_write,
},
jid,
&argv,
),
Err(e) => Err(jexec_fork_error(e)),
}
}
pub fn jexec_run(jid: i32, command: &[&str]) -> Result<i32> {
check_nonempty_command(command)?;
let argv = ExecArgv::new(command)?;
match unsafe { fork() } {
Ok(ForkResult::Parent { child }) => wait_child(child, jid, command),
Ok(ForkResult::Child) => {
unsafe {
let null = libc::open(c"/dev/null".as_ptr(), libc::O_RDWR);
if null >= 0 {
libc::dup2(null, 0);
libc::dup2(null, 1);
libc::dup2(null, 2);
if null > 2 {
libc::close(null);
}
}
}
attach_and_exec(jid, &argv);
}
Err(e) => Err(jexec_fork_error(e)),
}
}
struct ExecArgv {
args: Vec<CString>,
ptrs: Vec<*const libc::c_char>,
}
impl ExecArgv {
fn new(command: &[&str]) -> Result<Self> {
let args = command
.iter()
.map(|arg| CString::new(*arg))
.collect::<std::result::Result<Vec<_>, _>>()
.map_err(|e| Error::CommandFailed {
command: "jexec".to_string(),
message: format!("Invalid argument string: {e}"),
})?;
let mut ptrs: Vec<*const libc::c_char> = args.iter().map(|s| s.as_ptr()).collect();
ptrs.push(std::ptr::null());
Ok(Self { args, ptrs })
}
}
fn attach_and_exec(jid: i32, argv: &ExecArgv) -> ! {
unsafe {
libc::closefrom(3);
}
if unsafe { libc::jail_attach(jid) } != 0 {
child_fail(b"jail_attach failed", 1);
}
unsafe {
libc::execvp(argv.args[0].as_ptr(), argv.ptrs.as_ptr());
}
child_fail(b"execvp failed", 127);
}
fn child_fail(what: &[u8], code: libc::c_int) -> ! {
let errno = std::io::Error::last_os_error().raw_os_error().unwrap_or(0);
let mut digits = [0u8; 10];
let mut n = errno.unsigned_abs();
let mut start = digits.len();
loop {
start -= 1;
digits[start] = b'0' + (n % 10) as u8;
n /= 10;
if n == 0 {
break;
}
}
let sep = b": errno ";
unsafe {
libc::write(2, what.as_ptr().cast(), what.len());
libc::write(2, sep.as_ptr().cast(), sep.len());
libc::write(2, digits[start..].as_ptr().cast(), digits.len() - start);
libc::write(2, b"\n".as_ptr().cast(), 1);
libc::_exit(code)
}
}
fn wait_child(child: Pid, jid: i32, command: &[&str]) -> Result<i32> {
exit_code_of(waitpid(child, None), || format!("jexec {jid} {command:?}"))
}
fn exit_code_of(status: nix::Result<WaitStatus>, label: impl FnOnce() -> String) -> Result<i32> {
let message = match status {
Ok(WaitStatus::Exited(_, exit_code)) => return Ok(exit_code),
Ok(WaitStatus::Signaled(_, signal, _)) => format!("Process killed by signal {signal}"),
Ok(status) => format!("Unexpected wait status: {status:?}"),
Err(e) => format!("waitpid failed: {e}"),
};
Err(Error::CommandFailed {
command: label(),
message,
})
}
fn kill_process_group(child: Pid) -> bool {
unsafe { libc::kill(-child.as_raw(), libc::SIGKILL) == 0 }
}
struct OutputReaders {
stdout: std::thread::JoinHandle<Vec<u8>>,
stderr: std::thread::JoinHandle<Vec<u8>>,
}
impl OutputReaders {
fn spawn(stdout: OwnedFd, stderr: OwnedFd) -> Self {
Self {
stdout: std::thread::spawn(move || read_fd_to_end(stdout)),
stderr: std::thread::spawn(move || read_fd_to_end(stderr)),
}
}
fn join(self) -> (Vec<u8>, Vec<u8>) {
(
self.stdout.join().unwrap_or_default(),
self.stderr.join().unwrap_or_default(),
)
}
}
fn check_nonempty_command(command: &[&str]) -> Result<()> {
if command.is_empty() {
return Err(Error::CommandFailed {
command: "jexec".to_string(),
message: "Empty command".to_string(),
});
}
Ok(())
}
fn jexec_fork_error(e: nix::errno::Errno) -> Error {
Error::CommandFailed {
command: "jexec".to_string(),
message: format!("Fork failed: {e}"),
}
}
fn jexec_pipe(label: &str) -> Result<(OwnedFd, OwnedFd)> {
pipe().map_err(|e| Error::CommandFailed {
command: "jexec".to_string(),
message: format!("Failed to create {label} pipe: {e}"),
})
}
fn piped_child(pipes: ChildPipes, jid: i32, argv: &ExecArgv) -> ! {
drop(pipes.stdout_read);
drop(pipes.stderr_read);
unsafe {
libc::setpgid(0, 0);
}
redirect_child_io(pipes.stdout_write, pipes.stderr_write);
attach_and_exec(jid, argv);
}
static REAPER_USERS: std::sync::Mutex<usize> = std::sync::Mutex::new(0);
struct Reaper;
impl Reaper {
#[allow(clippy::significant_drop_tightening)]
fn acquire() -> Result<Self> {
let mut users = REAPER_USERS
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
if *users == 0 {
let ret = unsafe {
libc::procctl(
libc::P_PID as libc::idtype_t,
0, libc::PROC_REAP_ACQUIRE,
std::ptr::null_mut(),
)
};
if ret != 0 {
return Err(Error::CommandFailed {
command: "chroot".to_string(),
message: format!(
"procctl(PROC_REAP_ACQUIRE) failed: {}",
std::io::Error::last_os_error()
),
});
}
}
*users += 1;
Ok(Self)
}
}
impl Drop for Reaper {
fn drop(&mut self) {
let mut users = REAPER_USERS
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
*users -= 1;
if *users == 0 {
unsafe {
libc::procctl(
libc::P_PID as libc::idtype_t,
0,
libc::PROC_REAP_RELEASE,
std::ptr::null_mut(),
);
}
}
}
}
#[repr(C)]
#[allow(clippy::struct_field_names)] #[derive(Clone, Copy)]
struct ReaperPidInfo {
pi_pid: libc::pid_t,
pi_subtree: libc::pid_t,
pi_flags: libc::c_uint,
pi_pad0: [libc::c_uint; 15],
}
#[repr(C)]
#[allow(clippy::struct_field_names)] struct ReaperPids {
rp_count: libc::c_uint,
rp_pad0: [libc::c_uint; 15],
rp_pids: *mut ReaperPidInfo,
}
const _: [(); crate::sys::consts::SIZEOF_PROCCTL_REAPER_PIDINFO] =
[(); std::mem::size_of::<ReaperPidInfo>()];
const _: [(); crate::sys::consts::SIZEOF_PROCCTL_REAPER_PIDS] =
[(); std::mem::size_of::<ReaperPids>()];
const _: [(); crate::sys::consts::OFFSETOF_PROCCTL_REAPER_PIDS_RP_PIDS] =
[(); std::mem::offset_of!(ReaperPids, rp_pids)];
fn kill_subtree(subtree: nix::unistd::Pid) {
#[repr(C)]
#[allow(clippy::struct_field_names)] struct ProcctlReapKill {
rk_sig: libc::c_int,
rk_flags: libc::c_uint,
rk_subtree: libc::pid_t,
rk_killed: libc::c_uint,
rk_fpid: libc::pid_t,
rk_pad0: [libc::c_uint; 15],
}
const _: [(); crate::sys::consts::SIZEOF_PROCCTL_REAPER_KILL] =
[(); std::mem::size_of::<ProcctlReapKill>()];
let mut rk = ProcctlReapKill {
rk_sig: libc::SIGKILL,
rk_flags: crate::sys::consts::REAPER_KILL_SUBTREE,
rk_subtree: subtree.as_raw(),
rk_killed: 0,
rk_fpid: 0,
rk_pad0: [0; 15],
};
unsafe {
libc::procctl(
libc::P_PID as libc::idtype_t,
0,
libc::PROC_REAP_KILL,
(&raw mut rk).cast::<libc::c_void>(),
);
}
}
fn reap_subtree(subtree: nix::unistd::Pid) {
const SLOTS: libc::c_uint = 256;
const EMPTY: ReaperPidInfo = ReaperPidInfo {
pi_pid: 0,
pi_subtree: 0,
pi_flags: 0,
pi_pad0: [0; 15],
};
let mut infos = vec![EMPTY; SLOTS as usize];
loop {
kill_subtree(subtree);
infos.fill(EMPTY);
let mut req = ReaperPids {
rp_count: SLOTS,
rp_pad0: [0; 15],
rp_pids: infos.as_mut_ptr(),
};
let ret = unsafe {
libc::procctl(
libc::P_PID as libc::idtype_t,
0,
libc::PROC_REAP_GETPIDS,
(&raw mut req).cast(),
)
};
if ret != 0 {
return;
}
let ours = infos.iter().filter(|info| {
let flags = info.pi_flags;
flags & crate::sys::consts::REAPER_PIDINFO_VALID != 0
&& flags & crate::sys::consts::REAPER_PIDINFO_CHILD != 0
&& info.pi_subtree == subtree.as_raw()
});
let mut reaped = 0;
for info in ours {
let _ = waitpid(nix::unistd::Pid::from_raw(info.pi_pid), None);
reaped += 1;
}
if reaped == 0 {
return;
}
}
}
struct ChildPipes {
stdout_read: OwnedFd,
stderr_read: OwnedFd,
stdout_write: OwnedFd,
stderr_write: OwnedFd,
}
fn redirect_child_io(stdout_write: OwnedFd, stderr_write: OwnedFd) {
unsafe {
libc::dup2(stdout_write.as_raw_fd(), 1);
libc::dup2(stderr_write.as_raw_fd(), 2);
}
drop(stdout_write);
drop(stderr_write);
}
fn read_fd_to_end(fd: OwnedFd) -> Vec<u8> {
let mut buffer = Vec::new();
let mut file = std::fs::File::from(fd);
file.read_to_end(&mut buffer).ok();
buffer
}
pub fn jexec_with_timeout(
jid: i32,
command: &[&str],
timeout_secs: u64,
) -> Result<(i32, String, String)> {
check_nonempty_command(command)?;
let argv = ExecArgv::new(command)?;
let (stdout_read, stdout_write) = jexec_pipe("stdout")?;
let (stderr_read, stderr_write) = jexec_pipe("stderr")?;
match unsafe { fork() } {
Ok(ForkResult::Parent { child }) => {
drop(stdout_write);
drop(stderr_write);
let readers = OutputReaders::spawn(stdout_read, stderr_read);
let deadline =
(timeout_secs > 0).then(|| Instant::now() + Duration::from_secs(timeout_secs));
while crate::proc::wait_exited(child, false) == Ok(false) {
if deadline.is_some_and(|deadline| Instant::now() > deadline) {
if !kill_process_group(child) {
let _ = nix::sys::signal::kill(child, nix::sys::signal::SIGKILL);
}
let _ = waitpid(child, None);
readers.join();
return Err(Error::JailTimeout(timeout_secs));
}
std::thread::sleep(Duration::from_millis(10));
}
kill_process_group(child);
let status = waitpid(child, None);
let (stdout, stderr) = readers.join();
let exit_code = exit_code_of(status, || format!("jexec {jid} {command:?}"))?;
Ok((
exit_code,
crate::sys::string_from_utf8_lossy(stdout),
crate::sys::string_from_utf8_lossy(stderr),
))
}
Ok(ForkResult::Child) => piped_child(
ChildPipes {
stdout_read,
stderr_read,
stdout_write,
stderr_write,
},
jid,
&argv,
),
Err(e) => Err(jexec_fork_error(e)),
}
}
pub fn chroot_exec(
root_path: &str,
command: &str,
env_vars: &[(String, String)],
) -> Result<(i32, Vec<u8>, Vec<u8>)> {
let (stdout_read, stdout_write) = pipe().map_err(|e| Error::CommandFailed {
command: "chroot".to_string(),
message: format!("Failed to create stdout pipe: {e}"),
})?;
let (stderr_read, stderr_write) = pipe().map_err(|e| Error::CommandFailed {
command: "chroot".to_string(),
message: format!("Failed to create stderr pipe: {e}"),
})?;
let root_cstring = CString::new(root_path).map_err(|e| Error::CommandFailed {
command: "chroot".to_string(),
message: format!("Invalid path: {e}"),
})?;
let arg_cmd = CString::new(command).map_err(|e| Error::CommandFailed {
command: "chroot".to_string(),
message: format!("Invalid command string: {e}"),
})?;
let sh_argv: [*const libc::c_char; 4] = [
c"/bin/sh".as_ptr(),
c"-c".as_ptr(),
arg_cmd.as_ptr(),
std::ptr::null(),
];
let envp = ChildEnv::new(env_vars)?;
let _reaper = Reaper::acquire()?;
match unsafe { fork() } {
Ok(ForkResult::Parent { child }) => {
drop(stdout_write);
drop(stderr_write);
let readers = OutputReaders::spawn(stdout_read, stderr_read);
let _ = crate::proc::wait_exited(child, true);
kill_process_group(child);
let wait_result = waitpid(child, None);
reap_subtree(child);
let (stdout, stderr) = readers.join();
exit_code_of(wait_result, || format!("chroot {root_path}"))
.map(|code| (code, stdout, stderr))
}
Ok(ForkResult::Child) => {
drop(stdout_read);
drop(stderr_read);
redirect_child_io(stdout_write, stderr_write);
unsafe {
libc::closefrom(3);
}
if unsafe { libc::chroot(root_cstring.as_ptr()) } != 0 {
child_fail(b"chroot failed", 1);
}
if unsafe { libc::chdir(c"/".as_ptr()) } != 0 {
child_fail(b"chdir(\"/\") after chroot failed", 1);
}
unsafe {
libc::setpgid(0, 0);
}
unsafe {
libc::execve(c"/bin/sh".as_ptr(), sh_argv.as_ptr(), envp.ptrs.as_ptr());
}
child_fail(b"execve /bin/sh failed", 127);
}
Err(e) => Err(Error::CommandFailed {
command: "chroot".to_string(),
message: format!("Fork failed: {e}"),
}),
}
}
struct ChildEnv {
_vars: Vec<CString>,
ptrs: Vec<*const libc::c_char>,
}
impl ChildEnv {
fn new(env_vars: &[(String, String)]) -> Result<Self> {
use std::os::unix::ffi::OsStrExt;
let invalid = |message: String| Error::CommandFailed {
command: "chroot".to_string(),
message,
};
let overridden = |key: &[u8]| env_vars.iter().any(|(k, _)| k.as_bytes() == key);
let mut vars = Vec::new();
for (key, value) in std::env::vars_os() {
if overridden(key.as_bytes()) {
continue;
}
let mut entry = key.as_bytes().to_vec();
entry.push(b'=');
entry.extend_from_slice(value.as_bytes());
vars.push(CString::new(entry)?);
}
for (i, (key, value)) in env_vars.iter().enumerate() {
if key.is_empty() || key.contains('=') {
return Err(invalid(format!(
"Invalid environment variable name: {key:?}"
)));
}
if env_vars[i + 1..].iter().any(|(k, _)| k == key) {
continue;
}
let entry = CString::new(format!("{key}={value}"))
.map_err(|e| invalid(format!("Invalid environment variable {key}: {e}")))?;
vars.push(entry);
}
let mut ptrs: Vec<*const libc::c_char> = vars.iter().map(|v| v.as_ptr()).collect();
ptrs.push(std::ptr::null());
Ok(Self { _vars: vars, ptrs })
}
}
#[cfg(test)]
mod tests;