Repository navigation
Prevent mutating the global environment pointer in CommandExt::exec and opt to use execve and resolve path manually
#157144
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
base: main
Are you sure you want to change the base?
Changes from all commits
a8b72e0
db368d4
f8894f5
09f496d
fc53d39
32aa0fd
45e2ed2
4359483
07cf726
c6475f9
1a70ae0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,20 +6,18 @@ pub fn page_size() -> usize { | |
| unsafe { libc::sysconf(libc::_SC_PAGESIZE) as usize } | ||
| } | ||
|
|
||
| /// Returns the value for [`confstr(key, ...)`][posix_confstr]. Currently only | ||
| /// used on Darwin, but should work on any unix (in case we need to get | ||
| /// `_CS_PATH` or `_CS_V[67]_ENV` in the future). | ||
| /// Returns the value for [`confstr(key, ...)`][posix_confstr]. | ||
| /// | ||
| /// [posix_confstr]: | ||
| /// https://pubs.opengroup.org/onlinepubs/9799919799/functions/confstr.html | ||
| #[cfg(target_vendor = "apple")] | ||
| #[cfg(any(target_vendor = "apple", all(target_os = "linux", target_env = "gnu")))] | ||
| pub fn confstr( | ||
| key: crate::ffi::c_int, | ||
| size_hint: Option<usize>, | ||
| ) -> crate::io::Result<crate::ffi::OsString> { | ||
| use crate::ffi::OsString; | ||
| ) -> crate::io::Result<crate::ffi::CString> { | ||
| use alloc_crate::ffi::CString; | ||
|
|
||
| use crate::io; | ||
| use crate::os::unix::ffi::OsStringExt; | ||
|
|
||
| let mut buf: Vec<u8> = Vec::with_capacity(0); | ||
| let mut bytes_needed_including_nul = size_hint | ||
|
|
@@ -61,11 +59,17 @@ pub fn confstr( | |
| unsafe { | ||
| buf.set_len(bytes_needed_including_nul); | ||
| // Remove the NUL-terminator. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment is no longer true. |
||
| let last_byte = buf.pop(); | ||
| let last_byte = buf.last(); | ||
| // ... and smoke-check that it *was* a NUL-terminator. | ||
| assert_eq!(last_byte, Some(0), "`confstr` provided a string which wasn't nul-terminated"); | ||
| assert_eq!( | ||
| last_byte.copied(), | ||
| Some(0), | ||
| "`confstr` provided a string which wasn't nul-terminated" | ||
| ); | ||
| }; | ||
| Ok(OsString::from_vec(buf)) | ||
| // SAFETY: From above, we have confirmed that the buffer | ||
| // contains a nul terminating byte. | ||
|
Comment on lines
+70
to
+71
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's not the only precondition, |
||
| Ok(unsafe { CString::from_vec_with_nul_unchecked(buf) }) | ||
| } | ||
|
|
||
| #[cfg(all(target_os = "linux", target_env = "gnu"))] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -83,6 +83,33 @@ cfg_select! { | |
| } | ||
| } | ||
|
|
||
| /// Retrieves the fallback path *specifically* used by each platform's `execvp` | ||
| /// implementation. | ||
| fn get_default_path() -> CString { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Apologies for the repeat ping, previous message is label outdated due to new commits) @joboet see my comment earlier #157144 (comment) Unresolved question here is should we just directly use
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
@psumbera for Solaris, @xingxue-ibm for AIX: Do these platforms have constants like Probably doing a libc release in 1-2 weeks, as a heads up if there's something to add there.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AIX's
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @xingxue-ibm does AIX invoke the shell (e.g. if not i.e. (To note for reviewers, if
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I checked the current Solaris ON libc sources. Solaris defines When
Therefore, the closest emulation is to obtain Solaris does handle For Oracle's documentation also confirms that the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I don't have access to the AIX system source code, but I checked on AIX 7.2 by running tests and disassembling some routines. Here are my findings: A FIXME in both places works for me, with the AIX handling in a follow-up PR.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @xingxue-ibm Got you and thank you for testing If there's no reliance on
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, I am not suggesting removing the existing On Solaris, when C code is compiled with This is separate from what the already-compiled Solaris libc implementation of The base To preserve the behavior of the old unsafe extern "C" {
#[link_name = "confstr"]
fn solaris_confstr_base(
name: libc::c_int,
buf: *mut libc::c_char,
len: libc::size_t,
) -> libc::size_t;
}That private function and The shell choice is a separate issue. For an ordinary Rust process, where
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, I think I understand how this would work, and thank you for taking the time to clarify this with me! In regards to this: I suppose here, would we do something similar as done for unsafe extern "C" {
static __xpg4: i32;
}From reading illumos' code, I'm assuming Or is there a better/correct approach than the above? To kinda recap how I think it would work in my head, I would modify The
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, for Solaris, reading I checked the current Solaris 11.4 ON sources.
#[cfg(target_os = "solaris")]
fn is_xpg4() -> bool {
unsafe extern "C" {
#[link_name = "__xpg4"]
static XPG4_MODE: libc::c_int;
}
// SAFETY: __xpg4 is an exported process-wide libc data symbol.
unsafe { XPG4_MODE != 0 }
}This can remain a private Solaris implementation detail in There are a few details I would change in the proposed layout:
execve(shell_path, [c"sh", file, args..].as_ptr(), envp)where
So the |
||
| #[cfg(all(target_os = "linux", target_env = "gnu"))] | ||
| use crate::sys::conf::confstr; | ||
| cfg_select! { | ||
| all(target_os = "linux", target_env = "gnu") => { | ||
| confstr(libc::_CS_PATH, None).unwrap_or(CString::default()) | ||
| } | ||
| any( | ||
| all(target_os = "linux", not(target_env = "gnu")), | ||
| target_vendor = "apple", | ||
| target_os = "android", | ||
| target_os = "openbsd", | ||
| target_os = "freebsd", | ||
| target_os = "dragonfly", | ||
| ) => { | ||
| // SAFETY: libc::_PATH_DEFPATH is a nul-terminated char pointer | ||
| unsafe { CString::from(CStr::from_ptr(libc::_PATH_DEFPATH)) } | ||
| } | ||
| target_os = "aix" => { | ||
| CString::new(":/bin:/usr/bin").expect("the provided string should not have a nul byte") | ||
| } | ||
| _ => CString::default(), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer this to |
||
| } | ||
| } | ||
|
|
||
| //////////////////////////////////////////////////////////////////////////////// | ||
| // Command | ||
| //////////////////////////////////////////////////////////////////////////////// | ||
|
|
@@ -91,6 +118,7 @@ pub struct Command { | |
| program: CString, | ||
| args: CStringArray, | ||
| env: CommandEnv, | ||
| default_path: CString, | ||
|
|
||
| program_kind: ProgramKind, | ||
| cwd: Option<CString>, | ||
|
|
@@ -168,10 +196,12 @@ impl Command { | |
| let program = os2c(program, &mut saw_nul); | ||
| let mut args = CStringArray::with_capacity(1); | ||
| args.push(program.clone()); | ||
|
|
||
| Command { | ||
| program, | ||
| args, | ||
| env: Default::default(), | ||
| default_path: get_default_path(), | ||
| program_kind, | ||
| cwd: None, | ||
| chroot: None, | ||
|
|
@@ -271,6 +301,10 @@ impl Command { | |
| self.env.does_clear() | ||
| } | ||
|
|
||
| pub fn get_default_path(&self) -> &CStr { | ||
| &self.default_path | ||
| } | ||
|
|
||
| pub fn get_resolved_envs(&self) -> CommandResolvedEnvs { | ||
| CommandResolvedEnvs::new(self.env.capture()) | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
View changes since the review