Skip to content

Stabilize std::os::unix::process::CommandExt::chroot (process_chroot) - #163067

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

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

Conversation

@joshtriplett

@joshtriplett joshtriplett commented Sep 20, 2026 •

Copy link
Copy Markdown
Member

View all comments

Tracking issue: #141298

@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 20, 2026
@rustbot

rustbot commented Sep 20, 2026

Copy link
Copy Markdown
Collaborator

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
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 6 candidates

@joshtriplett

Copy link
Copy Markdown
Member Author

This has been around since May 2025, without issue. I think it's more than ready to stabilize. Shall we stabilize it?

@rfcbot merge libs

@rust-rfcbot

rust-rfcbot commented Sep 20, 2026 •

Copy link
Copy Markdown
Collaborator

@joshtriplett has proposed to merge this. The next step is review by the rest of the tagged team members:

Concerns:

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

cc @rust-lang/libs-ping: FCP proposed for libs, please feel free to register concerns.
See this document for info about what commands tagged team members can give me.

@rust-rfcbot rust-rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Sep 20, 2026
@joshtriplett joshtriplett added the relnotes Marks issues that should be documented in the release notes of the next release. label Sep 20, 2026
@joshtriplett joshtriplett added the I-libs-nominated Nominated for discussion during a libs team meeting. label Sep 20, 2026
@Mark-Simulacrum

Copy link
Copy Markdown
Member

Are there any test cases for this? I don't think I see any in the original PR (#137759) or in grep for chroot in our code.

I guess we'd need to condition such a test case on being able to run privileged, but I think in CI we can, so it seems valuable. Absent those, have we confirmed this actually works at least manually?

The os::unix::fs::chroot API is documented as:

This typically requires privileges, such as root or a specific capability.

I assume the same applies here? Can we amend the documentation to note that?

That API is also cfg'd away on fuchsia, instead of returning Unsupported like the process one. I assume we want to go with Unsupported as implemented here (and on slightly more targets - fuchsia, vxworks, wasi)?

@ds84182

ds84182 commented Sep 20, 2026 •

Copy link
Copy Markdown

If you use the unshare syscall you can use chroot without needing to run as root (on Linux).

@tgross35

Copy link
Copy Markdown
Member

In addition to the missing tests, there are no examples. They'll have to be no-run but a demo of how you might want to use this would be helpful.

@joboet

joboet commented Sep 21, 2026

Copy link
Copy Markdown
Member

I want to point out that chroot is not specified to be async-signal-safe by POSIX, so we are relying on undocumented platform behaviour here by assuming that it is safe to call after fork.

@clarfonthey clarfonthey removed the I-libs-nominated Nominated for discussion during a libs team meeting. label Sep 22, 2026
@nia-e

nia-e commented Sep 22, 2026

Copy link
Copy Markdown
Member

fwiw, the docs should probably also mention that - strictly speaking - this function is not documented to be async-signal-safe, and we should link to the docs of chroot. otherwise seems good per today's meeting ^^

@rust-rfcbot rust-rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Sep 22, 2026
@rust-rfcbot

Copy link
Copy Markdown
Collaborator

🔔 This is now entering its final comment period, as per the review above. 🔔

@tgross35

Copy link
Copy Markdown
Member

We have requests for tests (if possible), examples, and improved documentation. I think that's all worth a blocking concern, if anybody is willing to raise one on my behalf.

@ChrisDenton

Copy link
Copy Markdown
Member

@rfcbot concern tests, examples and improved documentation

@rust-rfcbot rust-rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Sep 22, 2026
@joshtriplett

Copy link
Copy Markdown
Member Author

I've posted #163180 to update the documentation. That PR 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.

I don't think a runnable test is feasible; our testsuite shouldn't be run as root. On Linux specifically, we could theoretically provide a test that runs via unshare, but we couldn't run that test on any other target that provides chroot.

@Mark-Simulacrum

Copy link
Copy Markdown
Member

I don't think a runnable test is feasible; our testsuite shouldn't be run as root.

I think if we're going to add root-only APIs to std, we should have a test suite that does require root and runs as such. It seems easy to do that in CI. Infra work for that should be pretty trivial.

@ChrisDenton

Copy link
Copy Markdown
Member

At the end of the day it is a fairly trivial wrapper around libc::chroot so I don't think it's too critical to test libc::chroot behaves as libc::chroot should? That's ultimately up to the platform. I guess it may be useful to test we haven't messed up the wrapper but we're just using the common method to convert paths to CStr.

@tgross35

Copy link
Copy Markdown
Member

I want to point out that chroot is not specified to be async-signal-safe by POSIX, so we are relying on undocumented platform behaviour here by assuming that it is safe to call after fork.

Note that this is an issue 8 doc, but chroot was removed in issue 3. I don't see it mentioned at https://pubs.opengroup.org/onlinepubs/7908799/xsh/chroot.html or the list at https://pubs.opengroup.org/onlinepubs/7908799/xsh/sigaction.html so it still wasn't AS-safe, though I wonder if they would have bothered documenting it at that point.

However, as far as I can tell, no popular platform's manpages say this is safe to do, e.g. https://man7.org/linux/man-pages/man2/chroot.2.html makes no mention and https://man7.org/linux/man-pages/man7/signal-safety.7.html only describes POSIX. Are we actually comfortable enabling this code unconditionally if it might be completely broken? I'm hesitant to take pervasiveness of a pattern in C on some platforms as truth that this will be sound on all platforms that have a chroot.

It seems safer to start with Linux and a few other popular platforms that we can confirm are sound, error on spawn otherwise. And leave it up to target maintainers to enable other platforms if they can confirm soundness. Ideally by updating their documentation...

At the end of the day it is a fairly trivial wrapper around libc::chroot so I don't think it's too critical to test libc::chroot behaves as libc::chroot should? That's ultimately up to the platform. I guess it may be useful to test we haven't messed up the wrapper but we're just using the common method to convert paths to CStr.

We could also totally forget to actually call libc::chroot :) or cfg it out by accident, do_exec is a bit of a gnarly function.

@tgross35

Copy link
Copy Markdown
Member

@ChrisDenton

Copy link
Copy Markdown
Member

To be clear, I see no reason why we can't sudo in CI specifically (though probably best not to do that locally) but if we just want to make sure that chroot is called then maybe something like miri can help without needing to actually do a real chroot.

@clarfonthey

Copy link
Copy Markdown
Contributor

There is also the fakechroot utility used by both Debian and Arch Linux, which intercepts libc calls and provides a fake chroot environment: https://linux.die.net/man/1/fakechroot

I would advise using that, at least on targets where we do definitely call out to libc for all relevant file operations.

@nia-e

nia-e commented Sep 23, 2026

Copy link
Copy Markdown
Member

Also for docs, I think this was brought up in the meeting but worth restating publicly here that the docs should specify that it's not actually a security boundary and it's possible to e.g. access directories outside of the chroot ^^

@tgross35

Copy link
Copy Markdown
Member

^ agreed, requested at #163180 (comment)

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

disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. O-unix Operating system: Unix-like proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. relnotes Marks issues that should be documented in the release notes of the next release. 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.

10 participants