Skip to main content

agentmux_common/
debug_privilege.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Whether this process holds `SeDebugPrivilege` enabled.
5//!
6//! On Windows a token with that privilege enabled opens any process and any thread whatever the
7//! target's access-control list says, so the control in `process_hardening` cannot refuse it.
8//! An elevated administrator's token has the privilege, usually disabled; a GitHub-hosted
9//! Windows runner's `runneradmin` has it enabled. The tests that watch a hardened process being
10//! refused ask this first and skip where the refusal cannot be seen.
11
12/// Whether this process's own token has `SeDebugPrivilege` enabled (present but disabled does not
13/// count: only an enabled privilege bypasses the list). Always false off Windows.
14pub fn debug_privilege_enabled() -> bool {
15    imp::enabled()
16}
17
18/// For a test that expects another process to be refused access to a hardened one: true, after
19/// printing why, when this process holds `SeDebugPrivilege` enabled and the refusal cannot be
20/// observed. The test returns early on true; on an ordinary token it runs in full.
21pub 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    /// Whether `privilege` is in `held` with the enabled flag set.
52    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        // SAFETY: plain Win32 calls on this process's own token, closed on every path; the buffer
60        // is 8-byte aligned and as long as the call asked for.
61        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            // Which answer depends on the token the tests run under; it must come back either way.
106            eprintln!("SeDebugPrivilege enabled in this test process: {}", enabled());
107        }
108    }
109}