Files
rust-discovery/crates/agent-windows/tests/native_acl.rs
T
2026-11-13 16:18:04 +01:00

70 lines
2.5 KiB
Rust

#![cfg(windows)]
use anyhow::{Result, ensure};
use rmm_agent_windows::native::{private_path, service_sid, wide};
use std::{path::Path, ptr};
use windows_sys::Win32::{
Foundation::LocalFree,
Security::{
Authorization::ConvertStringSecurityDescriptorToSecurityDescriptorW,
DACL_SECURITY_INFORMATION, OWNER_SECURITY_INFORMATION, PROTECTED_DACL_SECURITY_INFORMATION,
SetFileSecurityW,
},
};
fn apply(path: &Path, sddl: &str) -> Result<()> {
let text = wide(sddl);
let name = wide(path);
let mut descriptor = ptr::null_mut();
// SAFETY: Windows parses a test-owned SDDL into one owned descriptor.
ensure!(
unsafe {
ConvertStringSecurityDescriptorToSecurityDescriptorW(
text.as_ptr(),
1,
&mut descriptor,
ptr::null_mut(),
)
} != 0,
"TEST_DESCRIPTOR"
);
let result = unsafe {
SetFileSecurityW(
name.as_ptr(),
OWNER_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
| PROTECTED_DACL_SECURITY_INFORMATION,
descriptor,
)
};
unsafe {
LocalFree(descriptor.cast());
}
ensure!(result != 0, "TEST_ACL_APPLY");
Ok(())
}
#[test]
fn actual_windows_acl_rejects_broad_null_and_reparse_paths() {
// Explicit development VM test. Only a new empty temporary directory and
// its dummy nonsecret file are touched; never the running device identity.
let root = std::env::temp_dir().join(format!("rmm-private-acl-{}", uuid::Uuid::new_v4()));
std::fs::create_dir(&root).unwrap();
let sid = service_sid().unwrap();
let strict = format!("O:BAD:P(A;OICI;FA;;;SY)(A;OICI;FA;;;BA)(A;OICI;0x1301bf;;;{sid})");
apply(&root, &strict).unwrap();
private_path(&root, true).unwrap();
let file = root.join("dummy.txt");
std::fs::write(&file, b"nonsecret fixture").unwrap();
private_path(&file, false).unwrap();
assert!(private_path(&file, true).is_err());
apply(&file, "O:BAD:P(A;;FA;;;SY)(A;;FA;;;BA)(A;;FR;;;BU)").unwrap();
assert!(private_path(&file, false).is_err());
apply(&file, "O:BAD:NO_ACCESS_CONTROL").unwrap();
assert!(private_path(&file, false).is_err());
apply(&file, &strict).unwrap();
let link = root.join("dummy-link.txt");
std::os::windows::fs::symlink_file(&file, &link).unwrap();
assert!(private_path(&link, false).is_err());
std::fs::remove_file(link).unwrap();
std::fs::remove_file(file).unwrap();
std::fs::remove_dir(root).unwrap();
}