Skip to content

Add argv[0] fallback for current_exe() on Linux when /proc is unavailable#153603

Closed
Ecordonnier wants to merge 1 commit intorust-lang:mainfrom
Ecordonnier:eco/fix-current_exe
Closed

Add argv[0] fallback for current_exe() on Linux when /proc is unavailable#153603
Ecordonnier wants to merge 1 commit intorust-lang:mainfrom
Ecordonnier:eco/fix-current_exe

Conversation

@Ecordonnier
Copy link
Copy Markdown

When /proc/self/exe is not accessible (e.g., in containers with masked /proc, chroot environments, or systemd services with ProtectProc=invisible), current_exe() will now fall back to parsing argv[0] and searching PATH.

This brings the Linux implementation in line with existing Rust stdlib patterns on Fuchsia, Solaris/illumos, and AIX, which also use argv[0] when direct kernel APIs are unavailable.

Fallback strategy:

  1. Try /proc/self/exe (fast path, existing behavior)
  2. On NotFound error, retrieve argv[0] from env::args()
  3. If argv[0] is absolute, use it directly
  4. If argv[0] contains '/', resolve it as relative path against getcwd()
  5. Otherwise, search PATH for the executable (checking execute permissions)
  6. Return NotFound error if executable not found in PATH

This handles all common invocation patterns:

  • Absolute path: /usr/bin/program
  • Relative path: ./program or ../bin/program
  • PATH lookup: program (searches directories in PATH)

This maintains backward compatibility: behavior is unchanged when /proc is accessible.

Fixes #46090

@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 Mar 9, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 9, 2026

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 8 candidates

@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@Ecordonnier Ecordonnier force-pushed the eco/fix-current_exe branch from 681b21f to 23dfde3 Compare March 9, 2026 13:28
@Mark-Simulacrum Mark-Simulacrum added the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label Mar 22, 2026
@Mark-Simulacrum
Copy link
Copy Markdown
Member

Nominating for libs-api to discuss whether we want this or not. #152269 has some good discussion that is worth reading for that discussion, in particular #152269 (comment).

@Ecordonnier
Copy link
Copy Markdown
Author

Nominating for libs-api to discuss whether we want this or not. #152269 has some good discussion that is worth reading for that discussion, in particular #152269 (comment).

Please also consider #46090 in the discussion. The comments in #152269 support the opinion that the corner-case of /proc not being present doesn't exist, while #46090 shows a real-world example of this corner-case.

@rust-bors

This comment has been minimized.

…able

When /proc/self/exe is not accessible (e.g., in containers with masked /proc,
chroot environments, or systemd services with ProtectProc=invisible),
current_exe() will now fall back to parsing argv[0] and searching PATH.

This brings the Linux implementation in line with existing Rust stdlib patterns
on Fuchsia, Solaris/illumos, and AIX, which also use argv[0] when direct kernel
APIs are unavailable.

Fallback strategy:
1. Try /proc/self/exe (fast path, existing behavior)
2. On NotFound error, retrieve argv[0] from env::args()
3. If argv[0] is absolute, use it directly
4. If argv[0] contains '/', resolve it as relative path against getcwd()
5. Otherwise, search PATH for the executable (checking execute permissions)
6. Return NotFound error if executable not found in PATH

This handles all common invocation patterns:
- Absolute path: `/usr/bin/program`
- Relative path: `./program` or `../bin/program`
- PATH lookup: `program` (searches directories in PATH)

This maintains backward compatibility: behavior is unchanged when /proc is
accessible.

Signed-off-by: Etienne Cordonnier <ecordonnier@snap.com>
@Ecordonnier Ecordonnier force-pushed the eco/fix-current_exe branch from 23dfde3 to 70771c2 Compare March 24, 2026 08:03
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 24, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@Mark-Simulacrum Mark-Simulacrum added S-waiting-on-t-libs-api Status: Awaiting decision from T-libs-api and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 29, 2026
@nia-e
Copy link
Copy Markdown
Member

nia-e commented Apr 1, 2026

We discussed this in last week's @rust-lang/libs-api meeting and found the possible implications too finnicky, and we'd rather just require /proc to be mounted for this method. A PR to the docs to state this may be helpful, though. Thanks!

@nia-e nia-e closed this Apr 1, 2026
@oech3
Copy link
Copy Markdown

oech3 commented Apr 1, 2026

Oh no

@Ecordonnier
Copy link
Copy Markdown
Author

We discussed this in last week's @rust-lang/libs-api meeting and found the possible implications too finnicky, and we'd rather just require /proc to be mounted for this method. A PR to the docs to state this may be helpful, though. Thanks!

Can you please give some details about the finnicky implications? Especially considering that the AIX implementation in the same file already does it this way.

@nia-e
Copy link
Copy Markdown
Member

nia-e commented Apr 1, 2026

This could be a security concern where current_exe is being invoked e.g. in a SUID binary. If an implementor explicitly is okay with this fallback, argv is exposed through std::env::args

@Ecordonnier
Copy link
Copy Markdown
Author

This could be a security concern where current_exe is being invoked e.g. in a SUID binary. If an implementor explicitly is okay with this fallback, argv is exposed through std::env::args

And this is not a security concern for the platforms like AIX where this behavior is already implemented in the same file?

@Ecordonnier
Copy link
Copy Markdown
Author

This could be a security concern where current_exe is being invoked e.g. in a SUID binary. If an implementor explicitly is okay with this fallback, argv is exposed through std::env::args

What is the security concern with suid? argv[0] being modified?

@the8472
Copy link
Copy Markdown
Member

the8472 commented Apr 1, 2026

Tier3 targets do get less attention and have less experience on tap. Maybe it is less secure, or maybe there are mitigating circumstances how argv is populated on AIX, we don't know. Either way T3 targets usually don't directly inform choices for T1 targets.

What is the security concern with suid? argv[0] being modified?

That a less privileged parent can pass arbitrary values as argv0, yes.

@oech3
Copy link
Copy Markdown

oech3 commented Apr 2, 2026

Would you consider supporting AT_EXECFN at least on Linux(Android) if hijacked argv[0] is the reason to reject this PR?

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

Labels

I-libs-api-nominated Nominated for discussion during a libs-api team meeting. O-unix Operating system: Unix-like S-waiting-on-t-libs-api Status: Awaiting decision from T-libs-api 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.

rustc "unexpected panic"; no /proc/self/exe inside a debootstrap chroot

7 participants