Files
rust-discovery/crates/agent-windows/src/native_boot.rs
T
2026-11-14 04:54:20 +01:00

171 lines
6.8 KiB
Rust

//! UNWIRED diagnostic candidate, never a job-capable BootSource.
//!
//! Microsoft's public NtQuerySystemInformation contract explicitly permits API
//! changes and recommends dynamic lookup. It does NOT specify class90 or the
//! BootIdentifier layout used below. The fixed32-byte/GUID-prefix hypothesis
//! therefore remains unqualified, even when samples agree on Windows11/26300.
//! No CIM timestamp, wall clock, uptime subtraction or registry counter is used.
//! https://learn.microsoft.com/en-us/windows/win32/api/winternl/nf-winternl-ntquerysysteminformation
//! https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/nf-wdm-rtlgetversion
//! https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getfirmwaretype
use serde::Serialize;
use uuid::Uuid;
pub const CANDIDATE_BUILD: u32 = 26300;
#[derive(Clone, Debug, Serialize)]
pub struct Observation {
pub source: &'static str,
pub quality: &'static str,
pub code: &'static str,
pub windows_build: Option<u32>,
pub nt_status: Option<i32>,
pub returned_bytes: Option<u32>,
pub candidate_identifier: Option<Uuid>,
pub firmware_type: Option<u32>,
pub job_binding_qualified: bool,
pub public_class_90_abi_documented: bool,
}
impl Observation {
fn unsupported(code: &'static str) -> Self {
Self {
source: "ntdll/NtQuerySystemInformation/class90/experimental32",
quality: "unsupported",
code,
windows_build: None,
nt_status: None,
returned_bytes: None,
candidate_identifier: None,
firmware_type: None,
job_binding_qualified: false,
public_class_90_abi_documented: false,
}
}
}
#[cfg(any(windows, test))]
fn decode(status: i32, returned: u32, bytes: [u8; 32], firmware: u32) -> Option<Uuid> {
if status != 0
|| returned != 32
|| !matches!(firmware, 1 | 2)
|| u32::from_le_bytes(bytes[16..20].try_into().ok()?) != firmware
{
return None;
}
let id = Uuid::from_bytes_le(bytes[..16].try_into().ok()?);
(!id.is_nil()).then_some(id)
}
/// Actual bounded native read, but no qualification/capability follows from it.
pub fn observe_candidate() -> Observation {
#[cfg(windows)]
{
windows::observe()
}
#[cfg(not(windows))]
{
Observation::unsupported("BOOT_CANDIDATE_WINDOWS_ONLY")
}
}
#[cfg(windows)]
mod windows {
use super::*;
use std::mem::{size_of, zeroed};
use windows_sys::Win32::System::{
LibraryLoader::{GetModuleHandleW, GetProcAddress},
SystemInformation::{
GetFirmwareType, GetNativeSystemInfo, OSVERSIONINFOEXW, PROCESSOR_ARCHITECTURE_AMD64,
SYSTEM_INFO,
},
};
type RtlVersion = unsafe extern "system" fn(*mut OSVERSIONINFOEXW) -> i32;
type Query = unsafe extern "system" fn(i32, *mut core::ffi::c_void, u32, *mut u32) -> i32;
pub(super) fn observe() -> Observation {
let mut out = Observation::unsupported("BOOT_CANDIDATE_API_UNAVAILABLE");
// ntdll is already loaded by the OS. No path/search-based DLL loading.
let name: Vec<u16> = "ntdll.dll".encode_utf16().chain(Some(0)).collect();
let module = unsafe { GetModuleHandleW(name.as_ptr()) };
if module.is_null() {
return out;
}
let Some(version) = (unsafe { GetProcAddress(module, c"RtlGetVersion".as_ptr().cast()) })
else {
return out;
};
let Some(query) =
(unsafe { GetProcAddress(module, c"NtQuerySystemInformation".as_ptr().cast()) })
else {
return out;
};
// Public entrypoint calling conventions; class90's returned layout is
// explicitly experimental and checked as bytes, not a dereferenced ABI.
let version: RtlVersion = unsafe { std::mem::transmute(version) };
let query: Query = unsafe { std::mem::transmute(query) };
let mut os: OSVERSIONINFOEXW = unsafe { zeroed() };
os.dwOSVersionInfoSize = size_of::<OSVERSIONINFOEXW>() as u32;
if unsafe { version(&mut os) } != 0 {
return out;
}
out.windows_build = Some(os.dwBuildNumber);
let mut system: SYSTEM_INFO = unsafe { zeroed() };
unsafe { GetNativeSystemInfo(&mut system) };
if os.dwMajorVersion != 10
|| os.dwMinorVersion != 0
|| os.dwBuildNumber != CANDIDATE_BUILD
|| os.wProductType != 1
|| unsafe { system.Anonymous.Anonymous.wProcessorArchitecture }
!= PROCESSOR_ARCHITECTURE_AMD64
{
out.code = "BOOT_CANDIDATE_BUILD_OR_ARCHITECTURE_UNSUPPORTED";
return out;
}
let mut firmware = 0;
if unsafe { GetFirmwareType(&mut firmware) } == 0 {
out.code = "BOOT_CANDIDATE_FIRMWARE_CROSSCHECK_UNAVAILABLE";
return out;
}
// Fixed aligned32-byte capacity; no remote length/allocation or fallback.
let mut data = [0u64; 4];
let mut returned = 0;
let status = unsafe { query(90, data.as_mut_ptr().cast(), 32, &mut returned) };
out.nt_status = Some(status);
out.returned_bytes = Some(returned);
out.firmware_type = Some(firmware as u32);
if status == 0xC0000022u32 as i32 {
out.quality = "permission_denied";
out.code = "BOOT_CANDIDATE_ACCESS_DENIED";
return out;
}
let mut bytes = [0u8; 32];
for (i, value) in data.iter().enumerate() {
bytes[i * 8..i * 8 + 8].copy_from_slice(&value.to_ne_bytes());
}
if let Some(id) = decode(status, returned, bytes, firmware as u32) {
out.candidate_identifier = Some(id);
out.quality = "candidate_unqualified";
out.code = "BOOT_CANDIDATE_REQUIRES_ABI_AND_NATIVE_LIFECYCLE_QUALIFICATION";
} else {
out.code = "BOOT_CANDIDATE_STATUS_LENGTH_OR_LAYOUT_UNSUPPORTED";
}
// Neither raw BootFlags nor unrelated native memory is exposed.
out
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn candidate_layout_and_status_never_imply_job_qualification() {
let id = Uuid::parse_str("12345678-9abc-4def-8123-456789abcdef").unwrap();
let mut bytes = [0u8; 32];
bytes[..16].copy_from_slice(&id.to_bytes_le());
bytes[16..20].copy_from_slice(&2u32.to_le_bytes());
assert_eq!(decode(0, 32, bytes, 2), Some(id));
assert_eq!(decode(-1, 32, bytes, 2), None);
assert_eq!(decode(0, 31, bytes, 2), None);
assert_eq!(decode(0, 32, bytes, 1), None);
bytes[..16].fill(0);
assert_eq!(decode(0, 32, bytes, 2), None);
let out = Observation::unsupported("TEST");
assert!(!out.job_binding_qualified);
assert!(!out.public_class_90_abi_documented);
}
}