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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
//! Cross-process coordination primitives shared by `plm.exe` and the
//! `wxc-exec --audit` driver.
use std::time::Duration;
/// Maximum time the wxc-exec console-control handler waits to acquire
/// the DACL cleanup slot before allowing Windows to terminate the process.
/// The persistent elevated PLM child observes owner death independently.
pub const CTRL_HANDLER_DRAIN_TIMEOUT: Duration = Duration::from_secs(2);
/// Windows-only shared implementation of the `Global\Mxc_Plm_Audit`
/// named-mutex singleton. Both `plm.exe` and `wxc-exec --audit`
/// serialize on the same name so two concurrent PLM traces can't share
/// the single NT Kernel Logger session.
#[cfg(target_os = "windows")]
pub mod singleton {
use std::sync::atomic::{AtomicIsize, Ordering};
use windows::Win32::Foundation::HANDLE;
/// Distinguishes a fresh acquisition from ownership inherited after
/// the previous process terminated while holding the mutex.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum AcquireOutcome {
Acquired,
Abandoned,
}
/// Outcome of `try_acquire`. Callers translate to their own error
/// type (anyhow / String / etc).
pub enum AcquireError {
/// Another process already holds the singleton mutex.
AlreadyHeld,
/// `CreateMutexW` failed for a non-conflict reason.
CreateFailed(windows::core::Error),
/// The fixed name was pre-created by an untrusted principal.
UntrustedExisting,
/// Reading the existing mutex security descriptor failed.
SecurityQueryFailed(windows::Win32::Foundation::WIN32_ERROR),
}
/// Attempt to acquire the host-wide PLM audit mutex, stashing the
/// raw handle in `slot` so both `Drop`-based release and the
/// pre-`ExitProcess` cleanup can find it.
///
/// Uses the `CreateMutexW` + `WaitForSingleObject(0)` two-step
/// pattern rather than `CreateMutexW(bInitialOwner=true)` so we
/// correctly detect the "previous owner crashed without
/// releasing" case (Windows surfaces this as `WAIT_ABANDONED_0`
/// on the wait, never on the create). Treating an abandoned
/// mutex as `AlreadyHeld` would leave a stale singleton forever
/// after any PLM crash and force operators to reboot. Durable uncertain
/// cleanup state is tracked separately by a protected recovery marker;
/// abandonment never authorizes blind cancellation.
pub fn try_acquire(slot: &AtomicIsize) -> Result<AcquireOutcome, AcquireError> {
use windows::Win32::Foundation::CloseHandle;
let handle = create_or_open_trusted_mutex()?;
match try_acquire_handle(handle) {
Ok(outcome) => {
slot.store(handle.0 as isize, Ordering::SeqCst);
Ok(outcome)
}
Err(error) => {
unsafe {
let _ = CloseHandle(handle);
}
Err(error)
}
}
}
fn create_or_open_trusted_mutex() -> Result<HANDLE, AcquireError> {
use windows::core::w;
use windows::Win32::Foundation::{GetLastError, ERROR_ALREADY_EXISTS};
use windows::Win32::System::Threading::CreateMutexW;
let descriptor = OwnedSecurityDescriptor::new()?;
let attributes = descriptor.attributes();
let handle = unsafe { CreateMutexW(Some(&attributes), false, w!("Global\\Mxc_Plm_Audit")) }
.map_err(AcquireError::CreateFailed)?;
if unsafe { GetLastError() } == ERROR_ALREADY_EXISTS && !has_trusted_owner(handle)? {
unsafe {
let _ = windows::Win32::Foundation::CloseHandle(handle);
}
return Err(AcquireError::UntrustedExisting);
}
Ok(handle)
}
fn has_trusted_owner(handle: HANDLE) -> Result<bool, AcquireError> {
use windows::Win32::Foundation::{LocalFree, ERROR_SUCCESS, HLOCAL};
use windows::Win32::Security::Authorization::{GetSecurityInfo, SE_KERNEL_OBJECT};
use windows::Win32::Security::{
IsWellKnownSid, WinBuiltinAdministratorsSid, WinLocalSystemSid,
OWNER_SECURITY_INFORMATION, PSECURITY_DESCRIPTOR, PSID,
};
let mut owner = PSID::default();
let mut descriptor = PSECURITY_DESCRIPTOR::default();
let result = unsafe {
GetSecurityInfo(
handle,
SE_KERNEL_OBJECT,
OWNER_SECURITY_INFORMATION,
Some(&mut owner),
None,
None,
None,
Some(&mut descriptor),
)
};
if result != ERROR_SUCCESS {
return Err(AcquireError::SecurityQueryFailed(result));
}
let trusted = unsafe {
bool::from(IsWellKnownSid(owner, WinBuiltinAdministratorsSid))
|| bool::from(IsWellKnownSid(owner, WinLocalSystemSid))
};
unsafe {
let _ = LocalFree(Some(HLOCAL(descriptor.0)));
}
Ok(trusted)
}
struct OwnedSecurityDescriptor(windows::Win32::Security::PSECURITY_DESCRIPTOR);
impl OwnedSecurityDescriptor {
fn new() -> Result<Self, AcquireError> {
use windows::core::{w, PCWSTR};
use windows::Win32::Security::Authorization::{
ConvertStringSecurityDescriptorToSecurityDescriptorW, SDDL_REVISION_1,
};
use windows::Win32::Security::PSECURITY_DESCRIPTOR;
let mut descriptor = PSECURITY_DESCRIPTOR::default();
unsafe {
ConvertStringSecurityDescriptorToSecurityDescriptorW(
PCWSTR(w!("O:BAG:BAD:P(A;;GA;;;SY)(A;;GA;;;BA)").as_ptr()),
SDDL_REVISION_1,
&mut descriptor,
None,
)
}
.map_err(AcquireError::CreateFailed)?;
Ok(Self(descriptor))
}
fn attributes(&self) -> windows::Win32::Security::SECURITY_ATTRIBUTES {
windows::Win32::Security::SECURITY_ATTRIBUTES {
nLength: std::mem::size_of::<windows::Win32::Security::SECURITY_ATTRIBUTES>()
as u32,
lpSecurityDescriptor: self.0 .0,
bInheritHandle: false.into(),
}
}
}
impl Drop for OwnedSecurityDescriptor {
fn drop(&mut self) {
use windows::Win32::Foundation::{LocalFree, HLOCAL};
if !self.0 .0.is_null() {
unsafe {
let _ = LocalFree(Some(HLOCAL(self.0 .0)));
}
}
}
}
fn try_acquire_handle(handle: HANDLE) -> Result<AcquireOutcome, AcquireError> {
use windows::Win32::Foundation::{WAIT_ABANDONED, WAIT_OBJECT_0, WAIT_TIMEOUT};
use windows::Win32::System::Threading::WaitForSingleObject;
match unsafe { WaitForSingleObject(handle, 0) } {
WAIT_OBJECT_0 => Ok(AcquireOutcome::Acquired),
WAIT_ABANDONED => Ok(AcquireOutcome::Abandoned),
WAIT_TIMEOUT => Err(AcquireError::AlreadyHeld),
other => {
let thread_err = windows::core::Error::from_thread();
Err(AcquireError::CreateFailed(if thread_err.code().is_err() {
thread_err
} else {
windows::core::Error::from_hresult(windows::core::HRESULT(other.0 as i32))
}))
}
}
}
/// Release the singleton if `slot` holds a live handle. Idempotent:
/// safe to call from `Drop`, from explicit pre-`process::exit`
/// cleanup, and from error paths.
pub fn release(slot: &AtomicIsize) {
let raw = slot.swap(0, Ordering::SeqCst);
if raw != 0 {
let handle = windows::Win32::Foundation::HANDLE(raw as *mut _);
unsafe {
let _ = windows::Win32::System::Threading::ReleaseMutex(handle);
let _ = windows::Win32::Foundation::CloseHandle(handle);
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
// ---- ctrl-handler drain budget --------------------------------------
// Pin the OS-budget invariant. Windows imposes a hard ~5s kill
// timer on `CTRL_CLOSE_EVENT` / `CTRL_LOGOFF_EVENT` /
// `CTRL_SHUTDOWN_EVENT` handlers, so the DACL cleanup wait must
// remain comfortably below that budget.
#[test]
fn ctrl_handler_drain_timeout_respects_os_budget() {
assert!(
CTRL_HANDLER_DRAIN_TIMEOUT <= Duration::from_millis(4500),
"CTRL_HANDLER_DRAIN_TIMEOUT ({CTRL_HANDLER_DRAIN_TIMEOUT:?}) must stay under \
the ~5s OS kill budget for CTRL_CLOSE/LOGOFF/SHUTDOWN"
);
}
}