Skip to content

Add more documentation for chroot, including an example and a manpage link - #163180

Open
joshtriplett wants to merge 1 commit into
rust-lang:mainfrom
joshtriplett:chroot-docs
Open

joshtriplett wants to merge 1 commit into
rust-lang:mainfrom
joshtriplett:chroot-docs

Conversation

@joshtriplett

Copy link
Copy Markdown
Member

As requested in #163067 .

This adds an example, adds a manpage link, adds a mention that it requires
privileges, and adds an explanation that POSIX doesn't require it to be
async-signal-safe but it's safe in practice on our targets.

@rustbot rustbot added O-unix Operating system: Unix-like S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Sep 22, 2026
@rustbot

rustbot commented Sep 22, 2026

Copy link
Copy Markdown
Collaborator

r? @JohnTitor

rustbot has assigned @JohnTitor.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @ChrisDenton, libs
  • @ChrisDenton, libs expanded to 13 candidates
  • Random selection from 7 candidates

@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job test-pr-check-2 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)

Comment on lines 207 to +239
/// 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
/// [`process::Command::current_dir`], and that directory will be relative to the new root.
///
/// If no directory has been specified with [`process::Command::current_dir`], this will set the
/// directory to `/`, to avoid leaving the current directory outside the chroot. (This is an
/// intentional difference from the underlying `chroot` system call.)
///
/// This typically requires privileges, such as root or a specific capability.
///
/// Note that according to the POSIX standard, `chroot` is not required to be async-signal-safe,
/// and functions called between `fork` and `exec` must be async-signal-safe. However, in
/// practice, the ability to `fork` then `chroot` then `exec` is a capability widely provided by
/// Rust targets and relied upon by numerous programs.
///
/// See the [`chroot`] manual page for more details.
///
/// [`chroot`]: https://man7.org/linux/man-pages/man2/chroot.2.html
///
/// # Examples
///
#[cfg_attr(target_family = "unix", doc = "```no_run")]
#[cfg_attr(not(target_family = "unix"), doc = "```ignore (needs unix)")]
/// use std::process::Command;
/// use std::os::unix::process::CommandExt;
///
/// Command::new("contained")
/// .chroot("/sandbox")
/// .spawn()?
/// .wait()?;
#[doc = "```"]

@tgross35 tgross35 Sep 22, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should probably also document this important bit of the manpage:

In particular, it is not intended to be used for any kind of security purpose, neither to fully sandbox a process nor to restrict filesystem system calls.

So maybe also "other_root" rather than "sandbox" in the example

View changes since the review

/// Command::new("contained")
/// .chroot("/sandbox")
/// .spawn()?
/// .wait()?;

@tgross35 tgross35 Sep 22, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Needs # Ok::<_, Box<dyn std::error::Error>>(()) at the end like the others

View changes since the review

Comment on lines +219 to +222
/// Note that according to the POSIX standard, `chroot` is not required to be async-signal-safe,
/// and functions called between `fork` and `exec` must be async-signal-safe. However, in
/// practice, the ability to `fork` then `chroot` then `exec` is a capability widely provided by
/// Rust targets and relied upon by numerous programs.

@tgross35 tgross35 Sep 22, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe not worth mentioning posix specifically? Since chroot hasn't been defined there since 2001

View changes since the review

@tgross35

Copy link
Copy Markdown
Member

r? me

@rustbot rustbot assigned tgross35 and unassigned JohnTitor Sep 22, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

O-unix Operating system: Unix-like S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants