Trace native Windows clock RPC failure without changing trust checks

This commit is contained in:
2026-11-14 12:57:43 +01:00
parent e6fe4cd285
commit 6b03b222e6
4 changed files with 167 additions and 8 deletions
+82
View File
@@ -259,6 +259,87 @@ pub fn named_pipe_diagnostic() -> ClockReport {
}
.report()
}
/// Diagnostic-only ABI captured before the existing C layout guard. Presence
/// flags are numeric0/1; no remote pointers, names or returned entries escape.
#[derive(Clone, Copy, Debug, Default, Serialize)]
#[repr(C)]
pub struct StatusRpcTrace {
pub stage: u32,
pub native_rpc_return: u32,
pub native_rpc_return_present: u32,
pub rpc_exception: u32,
pub status_present: u32,
pub ul_size: u32,
pub c_entries: u32,
pub p_entries_present: u32,
}
#[derive(Debug, Serialize)]
pub struct StatusRpcDiagnostic {
pub rpc_code: u32,
pub independent_call: bool,
pub trace: Option<StatusRpcTrace>,
pub diagnostic_only: bool,
pub adapter_qualified: bool,
pub trusted: bool,
}
/// The fixed authenticated named-pipe call is observed separately. It never
/// selects a production transport or supplies a trusted timestamp. One worker,
/// one outstanding call and no replacement/retry after a local timeout.
pub fn status_rpc_diagnostic() -> StatusRpcDiagnostic {
#[cfg(windows)]
let (rpc_code, trace) = {
type Reply = std::sync::mpsc::SyncSender<(u32, StatusRpcTrace)>;
type Requests = std::sync::mpsc::SyncSender<Reply>;
static WORKER: std::sync::OnceLock<Mutex<Option<Requests>>> = std::sync::OnceLock::new();
let worker = WORKER.get_or_init(|| {
let (sender, receiver) = std::sync::mpsc::sync_channel::<Reply>(1);
let created = std::thread::Builder::new()
.name("rmm-clock-status-diag".into())
.spawn(move || {
#[link(name = "rmm_clock_rpc", kind = "static")]
#[link(name = "rpcrt4")]
#[link(name = "advapi32")]
unsafe extern "C" {
fn rmm_clock_query_trace(trace: *mut StatusRpcTrace) -> u32;
}
while let Ok(reply) = receiver.recv() {
let mut trace = StatusRpcTrace::default();
// SAFETY: fixed8*u32 C ABI; SEH stays contained inside C.
let code = unsafe { rmm_clock_query_trace(&mut trace) };
let _ = reply.send((code, trace));
}
})
.is_ok();
Mutex::new(created.then_some(sender))
});
if let Ok(mut worker) = worker.try_lock() {
let (reply, receiver) = std::sync::mpsc::sync_channel(1);
let sent = worker
.as_ref()
.is_some_and(|sender| sender.try_send(reply).is_ok());
if sent
&& let Ok((code, trace)) = receiver.recv_timeout(std::time::Duration::from_secs(2))
{
(code, Some(trace))
} else {
*worker = None;
(1818, None)
}
} else {
(170, None)
}
};
#[cfg(not(windows))]
let (rpc_code, trace) = (50, None);
StatusRpcDiagnostic {
rpc_code,
independent_call: true,
trace,
diagnostic_only: true,
adapter_qualified: false,
trusted: false,
}
}
/// Read-only negative controls for the separate native readiness program.
/// Neither control can produce clock authority, even if the OS accepts it.
/// Rejection without a successful genuine status query is not identity proof.
@@ -443,6 +524,7 @@ mod tests {
}
#[test]
fn production_clock_stays_closed_until_native_adapter_qualification() {
assert_eq!(std::mem::size_of::<StatusRpcTrace>(), 32);
assert_eq!(
NativeClock::new().now().unwrap_err().to_string(),
"CLOCK_ADAPTER_UNQUALIFIED"