Skip to content

Prevent mutating the global environment pointer in CommandExt::exec and opt to use execve and resolve path manually#157144

Open
asder8215 wants to merge 5 commits into
rust-lang:mainfrom
asder8215:commandext_exec_bug_fix
Open

Prevent mutating the global environment pointer in CommandExt::exec and opt to use execve and resolve path manually#157144
asder8215 wants to merge 5 commits into
rust-lang:mainfrom
asder8215:commandext_exec_bug_fix

Conversation

@asder8215

@asder8215 asder8215 commented May 30, 2026

Copy link
Copy Markdown
Contributor

View all comments

This PR fixes the bug described in #156951 where CommandExt::exec mutating the global environment pointer could cause correctness issue with concurrent exec calls (also this function holds an environment read lock, so it shouldn't cause any writes to the global environment pointer anyways). We do everything within CommandExt::exec via execve than execvp, which means we have to resolve the path manually.

I took reference to what was done in an archived (Feb 2026) upstream mirror of glibc's execvp and execvpe. I also took reference to #55359 on how they implemented the concerned portion of CommandExt::exec with execve. There's also this OpenBSD libc implementation of execvpe that I saw, but I didn't deeply take a look at this one and saw how it differ.

Other than that, I'm unsure how to handle the error ETIMEOUT/TimedOut as glibc breaks parsing through the PATH environment variable value and returns the error while #55359 continues parsing.

Let me know if you also want me to put some of execve code in a helper function or refactor it in other ways.

…nd opt to use execve and resolve path manually
@rustbot rustbot added 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 May 30, 2026
@rustbot

rustbot commented May 30, 2026

Copy link
Copy Markdown
Collaborator

r? @joboet

rustbot has assigned @joboet.
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 8 candidates
  • Random selection from Mark-Simulacrum, joboet

@rust-log-analyzer

This comment has been minimized.

@joboet joboet left a comment

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.

I highly recommend having a look at the POSIX specification – GNU/Linux mostly follows that, but the specification sometimes leaves some things unspecified and we should be careful not to break those.

Also, this is currently unsound, we cannot perform any memory allocation in do_exec, since it's called after fork for the standard spawn path. You probably need to preallocate some memory when creating the Command (or when setting certain parameters).

View changes since this review

Comment thread library/std/src/sys/process/unix/unix.rs Outdated
Comment thread library/std/src/sys/process/unix/unix.rs Outdated
Comment thread library/std/src/sys/process/unix/unix.rs Outdated
None => c"/bin:/usr/bin".to_bytes_with_nul(),
}
}
None => parent_path.as_bytes_with_nul(),

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.

I recommend moving the environment variable lookup here so that it's only performed when actually needed.

Comment thread library/std/src/sys/process/unix/unix.rs Outdated
Comment thread library/std/src/sys/process/unix/unix.rs Outdated
Comment thread library/std/src/sys/process/unix/unix.rs Outdated
Comment thread library/std/src/sys/process/unix/unix.rs Outdated
Comment thread library/std/src/sys/process/unix/unix.rs Outdated
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 30, 2026
@rustbot

rustbot commented May 30, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

…confstr_as_cstring added to get a CString value returned by confstr, added CStringArray::from_ptr, added default_path and shell_argv fields to unix Command, and refactored do_exec code
@rustbot rustbot added the O-unix Operating system: Unix-like label May 31, 2026
@asder8215 asder8215 requested a review from joboet May 31, 2026 05:02
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 31, 2026
Comment thread library/std/src/sys/process/unix/unix.rs Outdated
Comment thread library/std/src/sys/pal/unix/conf.rs Outdated
Comment thread library/std/src/sys/process/unix/unix.rs Outdated
Comment thread library/std/src/sys/process/unix/unix.rs Outdated
Comment thread library/std/src/sys/process/unix/unix.rs Outdated
Comment thread library/std/src/sys/process/unix/unix.rs Outdated
Comment thread library/std/src/sys/process/unix/common.rs Outdated
Comment thread library/std/src/sys/process/unix/common.rs Outdated
Comment thread library/std/src/sys/process/unix/unix.rs Outdated
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 6, 2026
@asder8215 asder8215 requested a review from joboet June 8, 2026 08:36
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 8, 2026
Comment thread library/std/src/sys/process/unix/common/cstring_array.rs Outdated
@rust-log-analyzer

This comment has been minimized.

…om a single passthrough in envp, match on errno values directly instead of ErrorKind, fix shell_argv CStringArray value.
@asder8215 asder8215 force-pushed the commandext_exec_bug_fix branch from 213bb02 to 8319b09 Compare June 8, 2026 09:23
@rust-log-analyzer

This comment has been minimized.

Comment thread library/std/src/sys/process/unix/unix.rs Outdated
Comment thread library/std/src/sys/pal/unix/conf.rs Outdated
Comment thread library/std/src/sys/pal/unix/conf.rs Outdated
assert_failed(index, len);
}

self.ptrs.insert(index, item.into_raw());

@joboet joboet Jun 8, 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.

This leaks the string if insert panics (see #155748, I made this mistake before).

View changes since the review

Comment thread library/std/src/sys/process/unix/common.rs Outdated
Comment thread library/std/src/sys/process/unix/unix.rs Outdated
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 8, 2026
@asder8215 asder8215 requested a review from joboet June 11, 2026 14:44
@asder8215 asder8215 force-pushed the commandext_exec_bug_fix branch from d818868 to 22776c7 Compare June 11, 2026 14:45

@joboet joboet left a comment

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.

It's starting to look quite good!

View changes since this review

Comment thread library/std/src/sys/pal/unix/conf.rs Outdated
Comment thread library/std/src/sys/process/unix/unix.rs Outdated
Comment thread library/std/src/sys/process/unix/unix.rs Outdated
Comment thread library/std/src/sys/process/unix/unix.rs Outdated
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 11, 2026
@asder8215 asder8215 force-pushed the commandext_exec_bug_fix branch from 22776c7 to df73301 Compare June 11, 2026 20:13
@asder8215 asder8215 requested a review from joboet June 11, 2026 20:14
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 11, 2026
Comment thread library/std/src/sys/process/unix/unix.rs Outdated
Comment thread library/std/src/sys/process/unix/unix.rs Outdated
…remove confstr_as_cstr, remove unnecessary shell_argv field/funtions + unused functions from CStringArray, use slice::split to delimit on colons
@asder8215 asder8215 force-pushed the commandext_exec_bug_fix branch from df73301 to 0ecb419 Compare June 11, 2026 22:36
@asder8215 asder8215 requested a review from joboet June 11, 2026 22:40
Comment thread library/std/src/sys/process/unix/unix.rs Outdated
Comment thread library/std/src/sys/process/unix/unix.rs
@asder8215 asder8215 requested a review from joboet June 12, 2026 12:38
Comment thread library/std/src/sys/process/unix/unix.rs Outdated
@asder8215 asder8215 force-pushed the commandext_exec_bug_fix branch from e3d0663 to 9851b0a Compare June 12, 2026 16:19
@asder8215 asder8215 requested a review from joboet June 12, 2026 16:19
@asder8215 asder8215 force-pushed the commandext_exec_bug_fix branch from 9851b0a to b4723b8 Compare June 12, 2026 16:20
…cumentation for returning an error from execve
@asder8215 asder8215 force-pushed the commandext_exec_bug_fix branch from b4723b8 to df00000 Compare June 12, 2026 16:21
@joboet

joboet commented Jun 17, 2026

Copy link
Copy Markdown
Member

Hey T-libs,

This PR makes std responsible for the PATH-lookup of programs not spawned through posix_spawnp (which doesn't apply in cases like if the PATH of the child has changed). Unfortunately, the POSIX standard leaves quite a lot up to the implementation, meaning this could change behaviour on some platforms. I'll need to do some more research to figure out the exact list, but wanted to check with you first whether

  1. You are happy with pursuing this further (CommandExt::exec writes to the global environment pointer while holding only a read lock #156951 cannot be fixed any other way unfortunately).
  2. You think we should use the same lookup logic for the posix_spawn happy path too, to keep behaviour consistent.

@rustbot label +I-libs-nominated

@rustbot rustbot added the I-libs-nominated Nominated for discussion during a libs team meeting. label Jun 17, 2026
@joshtriplett

Copy link
Copy Markdown
Member

Sounds reasonable to me.

@Amanieu

Amanieu commented Jul 1, 2026

Copy link
Copy Markdown
Member

Based on the discussion in the meeting, I'm going to propose FCP on the following:

  • We are happy to move forward with this since it seems to be the only proper solution to the problem.
  • We don't want to change path lookup on posix_spawn. If we discover inconsistency in behavior, we should fix the exec-side path lookup.

@rfcbot merge libs

@rust-rfcbot

rust-rfcbot commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator

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

No concerns currently listed.

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!

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 Jul 1, 2026
@rust-bors

rust-bors Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #158864) made this pull request unmergeable. Please resolve the merge conflicts by rebasing.

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. I-libs-nominated Nominated for discussion during a libs team meeting. 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. 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.

7 participants