Skip to content

Add std::os::unix::process::CommandExt::chroot to safely chroot a child process #137759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions library/std/src/os/unix/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use cfg_if::cfg_if;

use crate::ffi::OsStr;
use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
use crate::path::Path;
use crate::sealed::Sealed;
use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
use crate::{io, process, sys};
Expand Down Expand Up @@ -197,6 +198,16 @@ pub trait CommandExt: Sealed {
/// ```
#[stable(feature = "process_set_process_group", since = "1.64.0")]
fn process_group(&mut self, pgroup: i32) -> &mut process::Command;

/// Set the root of the child process. This calls `chroot` in the child process before executing
/// the command.
///
/// This happens before changing to the directory specified with `Command::current_dir`, and
/// that directory will be relative to the new root. If no directory has been specified with
/// `Command::current_dir`, this will set the directory to `/`, to avoid leaving the current
/// directory outside the chroot.
Comment on lines +206 to +208
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually, Rust is a thin wrapper around operating system functionality. The idea is that the user can implement chroot themselves if they want a chroot with a working directory outside of it?

#[unstable(feature = "process_chroot", issue = "none")]
fn chroot<P: AsRef<Path>>(&mut self, dir: P) -> &mut process::Command;
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -242,6 +253,11 @@ impl CommandExt for process::Command {
self.as_inner_mut().pgroup(pgroup);
self
}

fn chroot<P: AsRef<Path>>(&mut self, dir: P) -> &mut process::Command {
self.as_inner_mut().chroot(dir.as_ref());
self
}
}

/// Unix-specific extensions to [`process::ExitStatus`] and
Expand Down
13 changes: 13 additions & 0 deletions library/std/src/sys/pal/unix/process/process_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ pub struct Command {

program_kind: ProgramKind,
cwd: Option<CString>,
chroot: Option<CString>,
uid: Option<uid_t>,
gid: Option<gid_t>,
saw_nul: bool,
Expand Down Expand Up @@ -187,6 +188,7 @@ impl Command {
program_kind,
env: Default::default(),
cwd: None,
chroot: None,
uid: None,
gid: None,
saw_nul,
Expand All @@ -211,6 +213,7 @@ impl Command {
program_kind,
env: Default::default(),
cwd: None,
chroot: None,
uid: None,
gid: None,
saw_nul,
Expand Down Expand Up @@ -259,6 +262,12 @@ impl Command {
pub fn pgroup(&mut self, pgroup: pid_t) {
self.pgroup = Some(pgroup);
}
pub fn chroot(&mut self, dir: &Path) {
self.chroot = Some(os2c(dir.as_os_str(), &mut self.saw_nul));
if self.cwd.is_none() {
self.cwd(&OsStr::new("/"));
}
}

#[cfg(target_os = "linux")]
pub fn create_pidfd(&mut self, val: bool) {
Expand Down Expand Up @@ -331,6 +340,10 @@ impl Command {
pub fn get_pgroup(&self) -> Option<pid_t> {
self.pgroup
}
#[allow(dead_code)]
pub fn get_chroot(&self) -> Option<&CStr> {
self.chroot.as_deref()
}

pub fn get_closures(&mut self) -> &mut Vec<Box<dyn FnMut() -> io::Result<()> + Send + Sync>> {
&mut self.closures
Expand Down
10 changes: 10 additions & 0 deletions library/std/src/sys/pal/unix/process/process_unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,15 @@ impl Command {
cvt(libc::setuid(u as uid_t))?;
}
}
if let Some(chroot) = self.get_chroot() {
#[cfg(not(target_os = "fuchsia"))]
cvt(libc::chroot(chroot.as_ptr()))?;
#[cfg(target_os = "fuchsia")]
return Err(io::const_error!(
io::ErrorKind::Unsupported,
"chroot not supported by fuchsia"
));
}
if let Some(cwd) = self.get_cwd() {
cvt(libc::chdir(cwd.as_ptr()))?;
}
Expand Down Expand Up @@ -448,6 +457,7 @@ impl Command {
|| (self.env_saw_path() && !self.program_is_path())
|| !self.get_closures().is_empty()
|| self.get_groups().is_some()
|| self.get_chroot().is_some()
{
return Ok(None);
}
Expand Down
6 changes: 6 additions & 0 deletions library/std/src/sys/pal/unix/process/process_vxworks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ impl Command {
"nul byte found in provided data",
));
}
if self.get_chroot().is_some() {
return Err(io::const_error!(
ErrorKind::Unsupported,
"chroot not supported by vxworks",
));
}
let (ours, theirs) = self.setup_io(default, needs_stdin)?;
let mut p = Process { pid: 0, status: None };

Expand Down
Loading