rust/library/std/src/sys/windows
bors 2885c47482 Auto merge of #87704 - ChrisDenton:win-resolve-exe, r=yaahc
Windows: Resolve `process::Command` program without using the current directory

Currently `std::process::Command` searches many directories for the executable to run, including the current directory. This has lead to a [CVE for `ripgrep`](https://cve.circl.lu/cve/CVE-2021-3013) but presumably other command line utilities could be similarly vulnerable if they run commands. This was [discussed on the internals forum](https://internals.rust-lang.org/t/std-command-resolve-to-avoid-security-issues-on-windows/14800). Also discussed was [which directories should be searched](https://internals.rust-lang.org/t/windows-where-should-command-new-look-for-executables/15015).

EDIT: This PR originally removed all implicit paths. They've now been added back as laid out in the rest of this comment.

## Old Search Strategy

The old search strategy is [documented here][1]. Additionally Rust adds searching the child's paths (see also #37519). So the full list of paths that were searched was:

1. The directories that are listed in the child's `PATH` environment variable.
2. The directory from which the application loaded.
3. The current directory for the parent process.
4. The 32-bit Windows system directory.
5. The 16-bit Windows system directory.
6. The Windows directory.
7. The directories that are listed in the PATH environment variable.

## New Search Strategy

The new strategy removes the current directory from the searched paths.

1. The directories that are listed in the child's PATH environment variable.
2. The directory from which the application loaded.
3. The 32-bit Windows system directory.
4. The Windows directory.
5. The directories that are listed in the parent's PATH environment variable.

Note that it also removes the 16-bit system directory, mostly because there isn't a function to get it. I do not anticipate this being an issue in modern Windows.

## Impact

Removing the current directory should fix CVE's like the one linked above. However, it's possible some Windows users of affected Rust CLI applications have come to expect the old behaviour.

This change could also affect small Windows-only script-like programs that assumed the current directory would be used. The user would need to use `.\file.exe` instead of the bare application name.

This PR could break tests, especially those that test the exact output of error messages (e.g. Cargo) as this does change the error messages is some cases.

[1]: https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa#parameters
2021-11-20 18:23:11 +00:00
..
alloc Rework std::sys::windows::alloc 2021-03-26 12:38:26 +01:00
args Update Windows arg parsing tests 2021-08-08 22:11:29 +01:00
c Windows error codes: Add two missing ones 2021-06-18 18:51:53 +01:00
os std: move "mod tests/benches" to separate files 2020-08-31 02:56:59 +00:00
path windows: Return the "Not Found" error when a path is empty 2021-11-17 03:11:14 +09:00
process Windows: Resolve Command program without using the current directory 2021-10-31 16:32:34 +00:00
alloc.rs Move std::sys_common::alloc to std::sys::common 2021-04-14 13:24:10 +02:00
args.rs Implement modern Windows arg parsing 2021-08-08 22:11:30 +01:00
c.rs Windows: Resolve Command program without using the current directory 2021-10-31 16:32:34 +00:00
cmath.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
compat.rs Fix calling convention for CRT startup 2021-01-31 08:49:23 -08:00
condvar.rs No longer put windows condvars in a box. 2020-10-02 09:47:08 +02:00
env.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
fs.rs Auto merge of #89174 - ChrisDenton:automatic-verbatim-paths, r=dtolnay 2021-10-30 07:21:21 +00:00
handle.rs I/O safety. 2021-08-19 12:02:39 -07:00
io.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
memchr.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
mod.rs ErrorKind: Windows: Fix tidy 2021-06-18 19:30:55 +01:00
mutex.rs Fix may not to appropriate might not or must not 2021-07-29 01:15:20 -04:00
net.rs Run the #85441 regression test on MSVC only 2021-10-02 22:16:23 +03:00
os.rs Add comment documenting why we can't use a simpler solution 2021-10-22 09:55:32 -04:00
os_str.rs Capitalize safety comments 2020-09-08 22:26:44 -04:00
path.rs Auto merge of #87704 - ChrisDenton:win-resolve-exe, r=yaahc 2021-11-20 18:23:11 +00:00
pipe.rs I/O safety. 2021-08-19 12:02:39 -07:00
process.rs Windows: Resolve Command program without using the current directory 2021-10-31 16:32:34 +00:00
rand.rs Use BCryptGenRandom instead of RtlGenRandom on Windows. 2021-10-15 13:22:28 +02:00
rwlock.rs Multiple improvements to RwLocks 2021-06-01 09:07:55 +02:00
stack_overflow.rs Apply clippy suggestions 2021-10-09 18:56:01 +02:00
stack_overflow_uwp.rs Move all cleanup to sys::cleanup 2021-04-22 10:44:44 +02:00
stdio.rs Fix ctrl-c causing reads of stdin to return empty on Windows. 2021-10-01 08:53:13 -07:00
stdio_uwp.rs Reason safety for unsafe blocks for uwp stdin 2021-09-23 07:29:52 +08:00
thread.rs Rename std:🧵:available_onccurrency to std:🧵:available_parallelism 2021-09-28 14:59:33 +02:00
thread_local_dtor.rs Run destructors from existing tls callback 2021-11-01 15:19:49 +00:00
thread_local_key.rs Run destructors from existing tls callback 2021-11-01 15:19:49 +00:00
thread_parker.rs Resolve DLL imports at CRT startup, not on demand 2021-01-29 10:41:49 -08:00
time.rs Implement the same optimization in windows/time 2020-10-08 17:04:32 -07:00