Skip to content

Commit 413ca98

Browse files
committed
Update std::env::temp_dir to use GetTempPath2 on Windows when available.
As a security measure, Windows 11 introduces a new temporary directory API, GetTempPath2. When the calling process is running as SYSTEM, a separate temporary directory will be returned inaccessible to non-SYSTEM processes. For non-SYSTEM processes the behavior will be the same as before.
1 parent 4e89811 commit 413ca98

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

library/std/src/env.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -578,28 +578,25 @@ pub fn home_dir() -> Option<PathBuf> {
578578
/// may result in "insecure temporary file" security vulnerabilities. Consider
579579
/// using a crate that securely creates temporary files or directories.
580580
///
581-
/// # Unix
581+
/// # Platform-specific behavior
582582
///
583-
/// Returns the value of the `TMPDIR` environment variable if it is
583+
/// On Unix, returns the value of the `TMPDIR` environment variable if it is
584584
/// set, otherwise for non-Android it returns `/tmp`. If Android, since there
585585
/// is no global temporary folder (it is usually allocated per-app), it returns
586586
/// `/data/local/tmp`.
587+
/// On Windows, the behavior is equivalent to that of [`GetTempPath2`][GetTempPath2] /
588+
/// [`GetTempPath`][GetTempPath], which this function uses internally.
589+
/// Note that, this [may change in the future][changes].
587590
///
588-
/// # Windows
589-
///
590-
/// Returns the value of, in order, the `TMP`, `TEMP`,
591-
/// `USERPROFILE` environment variable if any are set and not the empty
592-
/// string. Otherwise, `temp_dir` returns the path of the Windows directory.
593-
/// This behavior is identical to that of [`GetTempPath`][msdn], which this
594-
/// function uses internally.
595-
///
596-
/// [msdn]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettemppatha
591+
/// [changes]: io#platform-specific-behavior
592+
/// [GetTempPath2]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettemppath2a
593+
/// [GetTempPath]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettemppatha
597594
///
598595
/// ```no_run
599596
/// use std::env;
600597
///
601598
/// fn main() {
602-
/// let mut dir = env::temp_dir();
599+
/// let dir = env::temp_dir();
603600
/// println!("Temporary directory: {}", dir.display());
604601
/// }
605602
/// ```

library/std/src/sys/windows/c.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,12 @@ compat_fn! {
11021102
-> () {
11031103
GetSystemTimeAsFileTime(lpSystemTimeAsFileTime)
11041104
}
1105+
1106+
// >= Win11
1107+
// https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettemppath2a
1108+
pub fn GetTempPath2W(nBufferLength: DWORD, lpBuffer: LPCWSTR) -> DWORD {
1109+
GetTempPathW(nBufferLength, lpBuffer)
1110+
}
11051111
}
11061112

11071113
compat_fn! {

library/std/src/sys/windows/os.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ pub fn unsetenv(n: &OsStr) -> io::Result<()> {
275275
}
276276

277277
pub fn temp_dir() -> PathBuf {
278-
super::fill_utf16_buf(|buf, sz| unsafe { c::GetTempPathW(sz, buf) }, super::os2path).unwrap()
278+
super::fill_utf16_buf(|buf, sz| unsafe { c::GetTempPath2W(sz, buf) }, super::os2path).unwrap()
279279
}
280280

281281
#[cfg(not(target_vendor = "uwp"))]

0 commit comments

Comments
 (0)