1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
//! Resolve the Windows System directory via `GetSystemDirectoryW`.
//!
//! Security-critical: callers locate trusted system binaries (`wpr.exe`,
//! `WindowsSandbox.exe`) under this path, so it must come from the kernel, not
//! `%SystemRoot%`/`%SystemDirectory%`. UAC inherits an unelevated parent's
//! environment, so an env-derived path would let a standard user plant a fake
//! binary and spoof the result. `GetSystemDirectoryW` is published at process
//! creation and cannot be overridden by the env block.
use std::path::{Path, PathBuf};
use std::sync::OnceLock;
/// Resolve the System directory via `GetSystemDirectoryW`, or `None` if the
/// call fails outright (return value 0 — a broken/stripped Windows install;
/// does not happen on a real install).
pub fn resolve_system_directory() -> Option<PathBuf> {
use windows::Win32::System::SystemInformation::GetSystemDirectoryW;
let mut buf = vec![0u16; 260];
// SAFETY: `buf` is initialized and owned for the call; we read only the
// returned prefix.
let mut n = unsafe { GetSystemDirectoryW(Some(&mut buf)) };
if n == 0 {
return None;
}
// `n >= buf.len()`: buffer was too small and `n` is the required size
// (including the NUL) — grow and retry once.
if n as usize >= buf.len() {
buf = vec![0u16; n as usize];
n = unsafe { GetSystemDirectoryW(Some(&mut buf)) };
if n == 0 || n as usize >= buf.len() {
return None;
}
}
Some(PathBuf::from(crate::mxc_common::string_util::from_wide(
&buf[..n as usize],
)))
}
/// Cached System directory, falling back to the (non-env-derived) literal
/// `C:\Windows\System32` only if `GetSystemDirectoryW` fails.
pub fn system_directory() -> &'static Path {
static SYSTEM_DIR: OnceLock<PathBuf> = OnceLock::new();
SYSTEM_DIR
.get_or_init(|| {
resolve_system_directory().unwrap_or_else(|| PathBuf::from(r"C:\Windows\System32"))
})
.as_path()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn system_directory_is_absolute_and_exists() {
let dir = system_directory();
assert!(dir.is_absolute(), "system dir must be absolute: {dir:?}");
assert!(dir.exists(), "system dir must exist: {dir:?}");
}
}