agentmux_common/
debug_privilege.rs1pub fn debug_privilege_enabled() -> bool {
15 imp::enabled()
16}
17
18pub fn refusal_cannot_be_observed(test: &str) -> bool {
22 let skip = debug_privilege_enabled();
23 if skip {
24 eprintln!(
25 "SKIPPED {test}: this process holds SeDebugPrivilege enabled, which opens any process whatever its \
26 access-control list says (an elevated administrator's token with the privilege on, as on a GitHub \
27 Windows runner), so a refusal cannot be observed here"
28 );
29 }
30 skip
31}
32
33#[cfg(not(windows))]
34mod imp {
35 pub(super) fn enabled() -> bool {
36 false
37 }
38}
39
40#[cfg(windows)]
41mod imp {
42 use std::ptr::null_mut;
43
44 use windows_sys::Win32::Foundation::{CloseHandle, HANDLE, LUID};
45 use windows_sys::Win32::Security::{
46 GetTokenInformation, LookupPrivilegeValueW, TokenPrivileges, LUID_AND_ATTRIBUTES, SE_PRIVILEGE_ENABLED,
47 TOKEN_PRIVILEGES, TOKEN_QUERY,
48 };
49 use windows_sys::Win32::System::Threading::{GetCurrentProcess, OpenProcessToken};
50
51 pub(super) fn is_enabled(held: &[LUID_AND_ATTRIBUTES], privilege: LUID) -> bool {
53 held.iter().any(|p| {
54 p.Luid.LowPart == privilege.LowPart && p.Luid.HighPart == privilege.HighPart && p.Attributes & SE_PRIVILEGE_ENABLED != 0
55 })
56 }
57
58 pub(super) fn enabled() -> bool {
59 unsafe {
62 let name: Vec<u16> = "SeDebugPrivilege".encode_utf16().chain(std::iter::once(0)).collect();
63 let mut debug = LUID { LowPart: 0, HighPart: 0 };
64 if LookupPrivilegeValueW(std::ptr::null(), name.as_ptr(), &mut debug) == 0 {
65 return false;
66 }
67 let mut token: HANDLE = null_mut();
68 if OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &mut token) == 0 {
69 return false;
70 }
71 let mut len = 0u32;
72 GetTokenInformation(token, TokenPrivileges, null_mut(), 0, &mut len);
73 let mut buf = vec![0u64; (len as usize).div_ceil(8).max(1)];
74 let got = GetTokenInformation(token, TokenPrivileges, buf.as_mut_ptr().cast(), len, &mut len) != 0;
75 CloseHandle(token);
76 if !got {
77 return false;
78 }
79 let privileges = &*(buf.as_ptr() as *const TOKEN_PRIVILEGES);
80 let held = std::slice::from_raw_parts(privileges.Privileges.as_ptr(), privileges.PrivilegeCount as usize);
81 is_enabled(held, debug)
82 }
83 }
84
85 #[cfg(test)]
86 mod tests {
87 use super::*;
88
89 fn held(luid: u32, attributes: u32) -> LUID_AND_ATTRIBUTES {
90 LUID_AND_ATTRIBUTES { Luid: LUID { LowPart: luid, HighPart: 0 }, Attributes: attributes }
91 }
92
93 #[test]
94 fn only_an_enabled_privilege_counts() {
95 let debug = LUID { LowPart: 20, HighPart: 0 };
96 assert!(is_enabled(&[held(20, SE_PRIVILEGE_ENABLED)], debug), "enabled");
97 assert!(is_enabled(&[held(5, 0), held(20, SE_PRIVILEGE_ENABLED | 1)], debug), "enabled, among others and by default");
98 assert!(!is_enabled(&[held(20, 0)], debug), "present but disabled: the list still applies");
99 assert!(!is_enabled(&[held(21, SE_PRIVILEGE_ENABLED)], debug), "a different privilege");
100 assert!(!is_enabled(&[], debug), "none held");
101 }
102
103 #[test]
104 fn this_process_answers_without_failing() {
105 eprintln!("SeDebugPrivilege enabled in this test process: {}", enabled());
107 }
108 }
109}