windows: never adopt a state directory someone else made
A root not owned by SYSTEM or Administrators is moved aside; the root gets SYSTEM as owner and an OWNER RIGHTS entry, so owning a file below it grants nothing. Adds the pinned component manifest. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d678976c2d
commit
cd0bcae4c9
2 files changed
+149
-6
No files matched your search
@@ -69,6 +69,14 @@ impl Paths {
|
||||
/// holds the secrets and the programs the service runs as SYSTEM. Everyone else goes
|
||||
/// through the API.
|
||||
pub fn create_all(&self) -> std::io::Result<()> {
|
||||
// A root someone else made first (ProgramData lets any user make folders) is theirs to
|
||||
// fill with programs this service would run: it is moved aside, not adopted.
|
||||
#[cfg(windows)]
|
||||
if self.root.exists() && !owned_by_system_or_admins(&self.root)? {
|
||||
let aside = self.root.with_extension(format!("untrusted-{}", std::process::id()));
|
||||
tracing::warn!("{} is not owned by SYSTEM or Administrators; moving it to {}", self.root.display(), aside.display());
|
||||
std::fs::rename(&self.root, &aside)?;
|
||||
}
|
||||
std::fs::create_dir_all(&self.root)?;
|
||||
#[cfg(windows)]
|
||||
protect(&self.root)?;
|
||||
@@ -92,8 +100,37 @@ pub fn same_content(path: &Path, data: &[u8]) -> bool {
|
||||
std::fs::read(path).is_ok_and(|old| old == data)
|
||||
}
|
||||
|
||||
/// A protected DACL on `dir`, inherited by everything below it: full control for SYSTEM and
|
||||
/// Administrators, nothing for anyone else.
|
||||
/// Whether `dir`'s owner is SYSTEM or Administrators.
|
||||
#[cfg(windows)]
|
||||
fn owned_by_system_or_admins(dir: &Path) -> std::io::Result<bool> {
|
||||
use std::ffi::c_void;
|
||||
use std::os::windows::ffi::OsStrExt;
|
||||
use std::ptr::null_mut;
|
||||
use windows_sys::Win32::Foundation::{ERROR_SUCCESS, LocalFree};
|
||||
use windows_sys::Win32::Security::Authorization::{GetNamedSecurityInfoW, SE_FILE_OBJECT};
|
||||
use windows_sys::Win32::Security::{
|
||||
IsWellKnownSid, OWNER_SECURITY_INFORMATION, WinBuiltinAdministratorsSid, WinLocalSystemSid,
|
||||
};
|
||||
let path: Vec<u16> = dir.as_os_str().encode_wide().chain(Some(0)).collect();
|
||||
let mut owner: *mut c_void = null_mut();
|
||||
let mut sd: *mut c_void = null_mut();
|
||||
// SAFETY: NUL-terminated path; the owner points into sd, freed after use.
|
||||
unsafe {
|
||||
let rc = GetNamedSecurityInfoW(
|
||||
path.as_ptr(), SE_FILE_OBJECT, OWNER_SECURITY_INFORMATION, &mut owner, null_mut(), null_mut(), null_mut(), &mut sd,
|
||||
);
|
||||
if rc != ERROR_SUCCESS {
|
||||
return Err(std::io::Error::from_raw_os_error(rc as i32));
|
||||
}
|
||||
let ok = IsWellKnownSid(owner, WinLocalSystemSid) != 0 || IsWellKnownSid(owner, WinBuiltinAdministratorsSid) != 0;
|
||||
LocalFree(sd);
|
||||
Ok(ok)
|
||||
}
|
||||
}
|
||||
|
||||
/// SYSTEM as the owner and a protected DACL on `dir`, inherited by everything below it: full
|
||||
/// control for SYSTEM and Administrators, nothing for anyone else, and (OWNER RIGHTS) nothing
|
||||
/// implicit for whoever owns a file below it.
|
||||
#[cfg(windows)]
|
||||
fn protect(dir: &Path) -> std::io::Result<()> {
|
||||
use std::ffi::c_void;
|
||||
@@ -104,10 +141,11 @@ fn protect(dir: &Path) -> std::io::Result<()> {
|
||||
ConvertStringSecurityDescriptorToSecurityDescriptorW, SDDL_REVISION_1, SE_FILE_OBJECT, SetNamedSecurityInfoW,
|
||||
};
|
||||
use windows_sys::Win32::Security::{
|
||||
DACL_SECURITY_INFORMATION, GetSecurityDescriptorDacl, PROTECTED_DACL_SECURITY_INFORMATION, ACL,
|
||||
ACL, DACL_SECURITY_INFORMATION, GetSecurityDescriptorDacl, GetSecurityDescriptorOwner, OWNER_SECURITY_INFORMATION,
|
||||
PROTECTED_DACL_SECURITY_INFORMATION,
|
||||
};
|
||||
|
||||
let sddl: Vec<u16> = "D:P(A;OICI;FA;;;SY)(A;OICI;FA;;;BA)".encode_utf16().chain(Some(0)).collect();
|
||||
let sddl: Vec<u16> = "O:SYD:P(A;OICI;FA;;;SY)(A;OICI;FA;;;BA)(A;OICI;0x0;;;OW)".encode_utf16().chain(Some(0)).collect();
|
||||
let path: Vec<u16> = dir.as_os_str().encode_wide().chain(Some(0)).collect();
|
||||
let mut sd: *mut c_void = null_mut();
|
||||
// SAFETY: NUL-terminated inputs; the descriptor is freed below, and the DACL points into it.
|
||||
@@ -118,11 +156,13 @@ fn protect(dir: &Path) -> std::io::Result<()> {
|
||||
let (mut present, mut defaulted) = (0, 0);
|
||||
let mut dacl: *mut ACL = null_mut();
|
||||
GetSecurityDescriptorDacl(sd, &mut present, &mut dacl, &mut defaulted);
|
||||
let mut owner: *mut c_void = null_mut();
|
||||
GetSecurityDescriptorOwner(sd, &mut owner, &mut defaulted);
|
||||
let rc = SetNamedSecurityInfoW(
|
||||
path.as_ptr(),
|
||||
SE_FILE_OBJECT,
|
||||
DACL_SECURITY_INFORMATION | PROTECTED_DACL_SECURITY_INFORMATION,
|
||||
null_mut(),
|
||||
OWNER_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION | PROTECTED_DACL_SECURITY_INFORMATION,
|
||||
owner,
|
||||
null_mut(),
|
||||
dacl,
|
||||
null_mut(),
|
||||
|
||||
Reference in new issue
Block a user