From 9d6a6f0eb6d969a10e60b793fc11b23148ea9e44 Mon Sep 17 00:00:00 2001 From: Kevin Janssen Date: Sat, 14 Nov 2026 03:53:48 +0100 Subject: [PATCH] Qualify Windows IPC client runtime and prepare bounded native providers --- crates/agent-windows/build.rs | 51 +- .../agent-windows/examples/clock_readiness.rs | 5 +- crates/agent-windows/native/clock_rpc.c | 56 +- crates/agent-windows/src/clock.rs | 57 +- crates/agent-windows/src/event_log.rs | 528 ++++++++++++ crates/agent-windows/src/helper_runtime.rs | 223 ++++++ crates/agent-windows/src/lib.rs | 20 +- crates/agent-windows/src/package.rs | 758 ++++++++++++++++++ crates/agent-windows/src/runtime.rs | 229 +++++- crates/domain/src/windows_execution.rs | 159 ++++ deploy/windows/Build-NativeMsvc.ps1 | 274 +++++++ deploy/windows/Install-NativeBuildTools.ps1 | 281 +++++++ deploy/windows/client_bootstrap.py | 4 +- deploy/windows/msvc/README.md | 222 +++++ deploy/windows/msvc/clock_abi_probe.c | 40 + deploy/windows/runtime_feature_upgrade.py | 257 ++++++ deploy/windows/system_helper_bootstrap.py | 390 +++++++++ .../windows/test_runtime_feature_upgrade.py | 49 ++ .../windows/test_system_helper_bootstrap.py | 56 ++ docs/handover.md | 10 + docs/implementation-status.md | 30 + docs/windows-clock-readiness.md | 37 +- docs/windows-feature-runtime-upgrade.md | 84 ++ docs/windows-system-helper-bootstrap.md | 90 +++ 24 files changed, 3880 insertions(+), 30 deletions(-) create mode 100644 crates/agent-windows/src/event_log.rs create mode 100644 crates/agent-windows/src/helper_runtime.rs create mode 100644 crates/agent-windows/src/package.rs create mode 100644 crates/domain/src/windows_execution.rs create mode 100644 deploy/windows/Build-NativeMsvc.ps1 create mode 100644 deploy/windows/Install-NativeBuildTools.ps1 create mode 100644 deploy/windows/msvc/README.md create mode 100644 deploy/windows/msvc/clock_abi_probe.c create mode 100644 deploy/windows/runtime_feature_upgrade.py create mode 100644 deploy/windows/system_helper_bootstrap.py create mode 100644 deploy/windows/test_runtime_feature_upgrade.py create mode 100644 deploy/windows/test_system_helper_bootstrap.py create mode 100644 docs/windows-feature-runtime-upgrade.md create mode 100644 docs/windows-system-helper-bootstrap.md diff --git a/crates/agent-windows/build.rs b/crates/agent-windows/build.rs index 67662b9..3584f83 100644 --- a/crates/agent-windows/build.rs +++ b/crates/agent-windows/build.rs @@ -11,13 +11,58 @@ fn main() { Ok("x86_64"), "Windows clock RPC is qualified only for x86-64" ); + let out = PathBuf::from(env::var_os("OUT_DIR").unwrap()); + let idl = env::current_dir().unwrap().join("native/w32time.idl"); + if env::var("CARGO_CFG_TARGET_ENV").as_deref() == Ok("msvc") { + // MIDL and CL come from the explicitly selected native x64 SDK/MSVC + // environment. `win64` means IA64 in MIDL: AMD64 must be explicit. + println!("cargo:rerun-if-env-changed=RMM_MIDL_EXE"); + let midl = env::var_os("RMM_MIDL_EXE").expect("Select the native SDK's verified midl.exe"); + assert!( + Command::new(midl) + .current_dir(&out) + .args([ + "/nologo", + "/env", + "amd64", + "/Oicf", + "/robust", + "/Zp8", + "/client", + "stub", + "/server", + "none", + "/h", + "w32time.h", + "/cstub", + "w32time_c.c", + ]) + .arg(&idl) + .status() + .expect("Install native Microsoft SDK MIDL; no generated-stub fallback") + .success(), + "Native Microsoft MIDL clock stub generation failed" + ); + // Keep RPC's __try/__except entirely inside native C. Existing C ABI + // assertions remain active; never substitute C++/Rust catch_unwind. + cc::Build::new() + .flag("/std:c11") + .flag("/Zp8") + .warnings_into_errors(true) + .include(&out) + .file(out.join("w32time_c.c")) + .file("native/clock_rpc.c") + .file("native/clock_endpoints.c") + .compile("rmm_clock_rpc"); + println!("cargo:rustc-link-lib=rpcrt4"); + println!("cargo:rustc-link-lib=advapi32"); + return; + } assert_eq!( env::var("CARGO_CFG_TARGET_ENV").as_deref(), Ok("gnu"), - "Windows clock RPC currently requires the qualified GNU/clang toolchain" + "Windows clock RPC supports native MSVC or the qualified GNU/clang toolchain" ); - let out = PathBuf::from(env::var_os("OUT_DIR").unwrap()); - let idl = env::current_dir().unwrap().join("native/w32time.idl"); for args in [ vec!["-h", "-H", "w32time.h"], vec!["-c", "-o", "w32time_c.c"], diff --git a/crates/agent-windows/examples/clock_readiness.rs b/crates/agent-windows/examples/clock_readiness.rs index ad9d72c..fde600e 100644 --- a/crates/agent-windows/examples/clock_readiness.rs +++ b/crates/agent-windows/examples/clock_readiness.rs @@ -161,9 +161,10 @@ mod service { std::thread::sleep(Duration::from_millis(750)); STAGE.store(31, Ordering::SeqCst); let second = clock.report(); + let identity_controls = super::clock::identity_controls(); STAGE.store(40, Ordering::SeqCst); let data = serde_json::to_vec_pretty( - &serde_json::json!({"schema":1,"case_id":case,"service_name":NAME,"token_sid":"S-1-5-19","elevated":token.elevation_flag!=0,"token":token,"endpoints":endpoints,"named_pipe_diagnostic":pipe,"source":"MS-W32T:4.1:Opnum6:ncalrpc:SID19:MUTUAL_AUTH:WINNT:PKT_PRIVACY","read_only":true,"samples":[first,second]}), + &serde_json::json!({"schema":1,"case_id":case,"service_name":NAME,"token_sid":"S-1-5-19","elevated":token.elevation_flag!=0,"token":token,"endpoints":endpoints,"named_pipe_diagnostic":pipe,"identity_controls":identity_controls,"source":"MS-W32T:4.1:Opnum6:ncalrpc:SID19:W32Time:V5_descriptor:MUTUAL_AUTH:WINNT:PKT_PRIVACY","read_only":true,"samples":[first,second]}), )?; ensure!(data.len() < 16384, "READINESS_REPORT_SIZE"); STAGE.store(41, Ordering::SeqCst); @@ -258,6 +259,6 @@ fn main() -> anyhow::Result<()> { fn main() { println!( "{}", - serde_json::to_string(&serde_json::json!({"clock":clock::NativeClock::new().report(),"endpoints":clock::endpoint_report(),"named_pipe_diagnostic":clock::named_pipe_diagnostic()})).unwrap() + serde_json::to_string(&serde_json::json!({"clock":clock::NativeClock::new().report(),"endpoints":clock::endpoint_report(),"named_pipe_diagnostic":clock::named_pipe_diagnostic(),"identity_controls":clock::identity_controls()})).unwrap() ); } diff --git a/crates/agent-windows/native/clock_rpc.c b/crates/agent-windows/native/clock_rpc.c index 7367b42..dca1f1e 100644 --- a/crates/agent-windows/native/clock_rpc.c +++ b/crates/agent-windows/native/clock_rpc.c @@ -5,11 +5,32 @@ #include #include #include +#include #include #include #include "w32time.h" /* Documented public SDK API; older MinGW headers omit its declaration. */ int RPC_ENTRY RpcExceptionFilter(unsigned long exception_code); +/* Older MinGW headers stop at V3. This is the documented x64 V5 SDK layout, + * not a fallback to an older authentication contract. MSVC also checks it + * directly against its native SDK declaration. Windows x64 is the only target. + * https://learn.microsoft.com/windows/win32/api/rpcdce/ns-rpcdce-rpc_security_qos_v5_w + */ +typedef struct { + unsigned long Version, Capabilities, IdentityTracking, ImpersonationType; + unsigned long AdditionalSecurityInfoType; + union { RPC_HTTP_TRANSPORT_CREDENTIALS_W *HttpCredentials; } u; + void *Sid; + unsigned int EffectiveOnly; + void *ServerSecurityDescriptor; +} RmmRpcQosV5; +_Static_assert(sizeof(RmmRpcQosV5) == 56, "x64 RPC QOS V5 ABI"); +_Static_assert(offsetof(RmmRpcQosV5, Sid) == offsetof(RPC_SECURITY_QOS_V3_W, Sid), "SDK QOS prefix ABI"); +_Static_assert(offsetof(RmmRpcQosV5, ServerSecurityDescriptor) == 48, "x64 server descriptor ABI"); +#ifdef _MSC_VER +_Static_assert(sizeof(RmmRpcQosV5) == sizeof(RPC_SECURITY_QOS_V5_W), "native SDK QOS V5 ABI"); +_Static_assert(offsetof(RmmRpcQosV5, ServerSecurityDescriptor) == offsetof(RPC_SECURITY_QOS_V5_W, ServerSecurityDescriptor), "native SDK descriptor ABI"); +#endif typedef struct Allocation { struct Allocation *next; size_t size; } Allocation; typedef struct { Allocation *head; size_t total; } Arena; @@ -106,7 +127,7 @@ uint32_t rmm_clock_query(RmmClockStatus *out) { /* Kernel-authenticated ALPC only. No automatic fallback to named pipes. * The expected SID is fixed locally, never adopted from endpoint metadata. */ -uint32_t rmm_clock_alpc_query(RmmClockStatus *out) { +static uint32_t alpc_query(RmmClockStatus *out, unsigned control) { RPC_IF_ID id = {0}; RPC_EP_INQ_HANDLE inquiry = NULL; RPC_STATUS result; @@ -132,15 +153,39 @@ uint32_t rmm_clock_alpc_query(RmmClockStatus *out) { if (result) {RpcBindingFree(&binding);break;} if (local) { _Alignas(8) BYTE expected_sid[SECURITY_MAX_SID_SIZE]; + _Alignas(8) BYTE service_sid[SECURITY_MAX_SID_SIZE]; + _Alignas(8) BYTE acl_buffer[sizeof(ACL) + sizeof(ACCESS_ALLOWED_ACE) + SECURITY_MAX_SID_SIZE]; + SECURITY_DESCRIPTOR descriptor; + WCHAR domain[64]; + DWORD domain_size = sizeof(domain) / sizeof(domain[0]); + DWORD service_sid_size = sizeof(service_sid); + SID_NAME_USE sid_type; DWORD sid_size = sizeof(expected_sid); - RPC_SECURITY_QOS_V3_W qos = {0}; - if (!CreateWellKnownSid(WinLocalServiceSid, NULL, expected_sid, &sid_size)) result = GetLastError(); + RmmRpcQosV5 qos = {0}; + /* An account-only SID19 binding also accepts other LocalService + * processes, including our network agent. The client-owned V5 + * descriptor therefore requires the separate W32Time service SID. + * No descriptor/identity is obtained from endpoint metadata. + * Controls are fixed read-only diagnostics, never production modes. + */ + if (!CreateWellKnownSid(control == 1 ? WinLocalSystemSid : WinLocalServiceSid, + NULL, expected_sid, &sid_size)) result = GetLastError(); + if (!result && !LookupAccountNameW(NULL, + control == 2 ? L"NT SERVICE\\RmmDiscoveryWindows" : L"NT SERVICE\\W32Time", + service_sid, &service_sid_size, domain, &domain_size, &sid_type)) result = GetLastError(); + if (!result && (!IsValidSid(service_sid) || service_sid_size > sizeof(service_sid))) result = ERROR_INVALID_SID; + if (!result && !InitializeAcl((PACL)acl_buffer, sizeof(acl_buffer), ACL_REVISION)) result = GetLastError(); + if (!result && !AddAccessAllowedAce((PACL)acl_buffer, ACL_REVISION, GENERIC_ALL, service_sid)) result = GetLastError(); + if (!result && !InitializeSecurityDescriptor(&descriptor, SECURITY_DESCRIPTOR_REVISION)) result = GetLastError(); + if (!result && !SetSecurityDescriptorDacl(&descriptor, TRUE, (PACL)acl_buffer, FALSE)) result = GetLastError(); if (!result) { - qos.Version = RPC_C_SECURITY_QOS_VERSION_3; + qos.Version = 5; /* RPC_C_SECURITY_QOS_VERSION_5 */ qos.Capabilities = RPC_C_QOS_CAPABILITIES_MUTUAL_AUTH; qos.IdentityTracking = RPC_C_QOS_IDENTITY_STATIC; qos.ImpersonationType = RPC_C_IMP_LEVEL_IDENTIFY; qos.Sid = expected_sid; + qos.EffectiveOnly = TRUE; + qos.ServerSecurityDescriptor = &descriptor; result = RpcBindingSetAuthInfoExW(binding, NULL, RPC_C_AUTHN_LEVEL_PKT_PRIVACY, RPC_C_AUTHN_WINNT, NULL, RPC_C_AUTHZ_NONE, (RPC_SECURITY_QOS *)&qos); if (!result) result = invoke_status(binding, out); @@ -154,3 +199,6 @@ uint32_t rmm_clock_alpc_query(RmmClockStatus *out) { RpcMgmtEpEltInqDone(&inquiry); return result == RPC_X_NO_MORE_ENTRIES ? EPT_S_NOT_REGISTERED : result; } +uint32_t rmm_clock_alpc_query(RmmClockStatus *out) { return alpc_query(out, 0); } +uint32_t rmm_clock_wrong_account(RmmClockStatus *out) { return alpc_query(out, 1); } +uint32_t rmm_clock_wrong_service(RmmClockStatus *out) { return alpc_query(out, 2); } diff --git a/crates/agent-windows/src/clock.rs b/crates/agent-windows/src/clock.rs index ce89cb2..4db1572 100644 --- a/crates/agent-windows/src/clock.rs +++ b/crates/agent-windows/src/clock.rs @@ -8,7 +8,8 @@ use anyhow::{Result, anyhow, ensure}; use chrono::{DateTime, Utc}; use serde::Serialize; use std::sync::Mutex; -/// Flip only after native SID-bound ALPC, readiness and fail-closed qualification. +/// Flip only after native account AND service-bound ALPC, positive readiness, +/// wrong-account/wrong-service denial and fail-closed qualification. pub const ADAPTER_QUALIFIED: bool = false; const SECOND: u64 = 10_000_000; const MAX_AGE: u64 = 3600 * SECOND; @@ -191,7 +192,7 @@ impl NativeClock { transport: if self.diagnostic { "ncacn_np:W32TIME_ALT:diagnostic_only" } else { - "ncalrpc:SID19:mutual_auth" + "ncalrpc:SID19:W32Time:V5_descriptor:mutual_auth" }, status: None, query_ticks: ticks, @@ -258,6 +259,38 @@ pub fn named_pipe_diagnostic() -> ClockReport { } .report() } +/// 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. +#[derive(Debug, Serialize)] +pub struct IdentityControls { + pub wrong_account_rpc_code: u32, + pub wrong_service_rpc_code: u32, + pub both_access_denied: bool, + pub diagnostic_only: bool, +} +pub fn identity_controls() -> IdentityControls { + #[cfg(windows)] + let codes = { + static WORKER: std::sync::OnceLock> = std::sync::OnceLock::new(); + let worker = WORKER.get_or_init(|| Mutex::new(RpcWorker::new(raw_control))); + match worker.try_lock() { + Ok(mut worker) => ( + worker.call(true, std::time::Duration::from_secs(1)).0, + worker.call(false, std::time::Duration::from_secs(1)).0, + ), + Err(_) => (170, 170), + } + }; + #[cfg(not(windows))] + let codes = (50, 50); + IdentityControls { + wrong_account_rpc_code: codes.0, + wrong_service_rpc_code: codes.1, + both_access_denied: codes == (5, 5), + diagnostic_only: true, + } +} impl rmm_agent_core::reconcile::SyncClock for NativeClock { fn now(&self) -> Result> { NativeClock::now(self) @@ -300,6 +333,26 @@ fn raw_query(diagnostic: bool) -> (u32, Status) { }; (code, status) } +#[cfg(windows)] +fn raw_control(wrong_account: bool) -> (u32, Status) { + #[link(name = "rmm_clock_rpc", kind = "static")] + #[link(name = "rpcrt4")] + #[link(name = "advapi32")] + unsafe extern "C" { + fn rmm_clock_wrong_account(status: *mut Status) -> u32; + fn rmm_clock_wrong_service(status: *mut Status) -> u32; + } + let mut status = Status::default(); + // SAFETY: fixed C ABI, fixed two local negative identities, contained SEH. + let code = unsafe { + if wrong_account { + rmm_clock_wrong_account(&mut status) + } else { + rmm_clock_wrong_service(&mut status) + } + }; + (code, status) +} #[cfg(any(windows, test))] type RpcReply = std::sync::mpsc::SyncSender<(u32, Status)>; #[cfg(any(windows, test))] diff --git a/crates/agent-windows/src/event_log.rs b/crates/agent-windows/src/event_log.rs new file mode 100644 index 0000000..9843093 --- /dev/null +++ b/crates/agent-windows/src/event_log.rs @@ -0,0 +1,528 @@ +//! Bounded, local, fixture-only live Event Log query; not wired into any job. +//! +//! EvtQuery/EvtRender have no per-call timeout. The monotonic deadline below is +//! cooperative between Win32 calls; the eventual executor must also supervise +//! its process. This module never starts another worker or advances a bookmark. +//! API contracts: https://learn.microsoft.com/en-us/windows/win32/api/winevt/nf-winevt-evtquery +//! https://learn.microsoft.com/en-us/windows/win32/api/winevt/nf-winevt-evtnext +//! https://learn.microsoft.com/en-us/windows/win32/api/winevt/nf-winevt-evtrender +//! https://learn.microsoft.com/en-us/windows/win32/api/winevt/ne-winevt-evt_system_property_id +use anyhow::{Result, ensure}; +use chrono::{DateTime, SecondsFormat, Utc}; +use serde::{Deserialize, Serialize}; +use std::sync::atomic::AtomicBool; + +pub const CHANNEL: &str = "Application"; +pub const PROVIDER: &str = "RustDiscovery-MVP-Test"; +const MAX_BYTES: usize = 256 * 1024; +#[cfg(any(windows, test))] +const REPORT_RESERVE: usize = 256; + +#[derive(Clone, Debug, Deserialize, Serialize)] +#[serde(deny_unknown_fields)] +pub struct Query { + pub channel: String, + pub provider: String, + pub start_utc: DateTime, + pub end_utc: DateTime, + pub max_events: u32, + pub max_output_bytes: u32, + pub timeout_ms: u32, +} +impl Query { + fn xpath(&self) -> Result { + ensure!( + self.channel == CHANNEL && self.provider == PROVIDER, + "EVENT_LOG_SOURCE_DENIED" + ); + ensure!( + self.start_utc.timestamp() >= 0 + && self.end_utc > self.start_utc + && self.end_utc - self.start_utc <= chrono::Duration::hours(24), + "EVENT_LOG_WINDOW_INVALID" + ); + ensure!( + (1..=1000).contains(&self.max_events) + && (1024..=MAX_BYTES as u32).contains(&self.max_output_bytes) + && (1..=5000).contains(&self.timeout_ms), + "EVENT_LOG_LIMIT_INVALID" + ); + // Only canonical UTC timestamps enter this fixed expression. Neither a + // caller-supplied XPath, a remote session nor a log-file path exists. + Ok(format!( + "*[System[Provider[@Name='{PROVIDER}'] and TimeCreated[@SystemTime>='{}' and @SystemTime<'{}']]]", + self.start_utc.to_rfc3339_opts(SecondsFormat::Nanos, true), + self.end_utc.to_rfc3339_opts(SecondsFormat::Nanos, true) + )) + } +} + +#[derive(Debug, Serialize)] +pub struct Event { + pub channel: String, + pub provider: String, + pub record_id: u64, + pub event_id: u16, + pub created_utc: DateTime, + /// Exact bounded XML from EvtRenderEventXml, including fixture EventData; + /// no localized publisher DLL/message formatting is performed. + pub xml: String, +} +#[derive(Debug, Serialize)] +pub struct Report { + pub channel: String, + pub provider: String, + pub start_utc: DateTime, + pub end_utc: DateTime, + pub quality: &'static str, + pub completeness: &'static str, + pub truncated: bool, + pub error_code: Option<&'static str>, + pub win32_error: Option, + pub events: Vec, + pub bookmark_updated: bool, + pub deadline_enforcement: &'static str, +} +#[cfg(any(windows, test))] +impl Report { + fn new(q: &Query) -> Self { + Self { + channel: q.channel.clone(), + provider: q.provider.clone(), + start_utc: q.start_utc, + end_utc: q.end_utc, + quality: "valid", + completeness: "partial", + truncated: false, + error_code: None, + win32_error: None, + events: Vec::new(), + bookmark_updated: false, + deadline_enforcement: "cooperative_between_win32_calls", + } + } + fn stop(&mut self, code: &'static str, win32: Option) { + self.truncated = true; + self.error_code = Some(code); + self.win32_error = win32; + self.completeness = if self.events.is_empty() { + "unknown" + } else { + "partial" + }; + if win32 == Some(5) { + self.quality = "permission_denied"; + } else if win32.is_some() { + self.quality = "error"; + } + } + fn push(&mut self, event: Event, limit: usize) -> Result { + self.events.push(event); + if serde_json::to_vec(self)?.len() + REPORT_RESERVE > limit { + self.events.pop(); + self.stop("EVENT_LOG_BYTE_LIMIT", None); + return Ok(false); + } + Ok(true) + } +} + +/// Reads only the fixed local fixture source. No persistent state is modified. +pub fn query(q: &Query, cancelled: &AtomicBool) -> Result { + let xpath = q.xpath()?; + #[cfg(windows)] + { + windows::query(q, &xpath, cancelled) + } + #[cfg(not(windows))] + { + let _ = (xpath, cancelled); + anyhow::bail!("EVENT_LOG_WINDOWS_ONLY") + } +} + +#[cfg(windows)] +mod windows { + use super::*; + use anyhow::Context; + use std::{ + mem::size_of, + ptr, + sync::atomic::Ordering, + time::{Duration, Instant}, + }; + use windows_sys::Win32::{ + Foundation::{ + ERROR_ACCESS_DENIED, ERROR_INSUFFICIENT_BUFFER, ERROR_NO_MORE_ITEMS, ERROR_TIMEOUT, + GetLastError, + }, + System::EventLog::*, + }; + + struct Handle(EVT_HANDLE); + impl Drop for Handle { + fn drop(&mut self) { + if self.0 != 0 { + unsafe { + EvtClose(self.0); + } + } + } + } + fn wide(s: &str) -> Vec { + s.encode_utf16().chain(Some(0)).collect() + } + fn stopped(report: &mut Report, cancelled: &AtomicBool, until: Instant) -> bool { + if cancelled.load(Ordering::SeqCst) { + report.stop("EVENT_LOG_CANCELLED", None); + true + } else if Instant::now() >= until { + report.stop("EVENT_LOG_TIME_LIMIT", None); + true + } else { + false + } + } + fn failure(report: &mut Report, error: u32) { + report.stop( + if error == ERROR_ACCESS_DENIED { + "EVENT_LOG_PERMISSION_DENIED" + } else { + "EVENT_LOG_API_ERROR" + }, + Some(error), + ); + } + // u64 storage provides the alignment required by EVT_VARIANT. All returned + // pointers are read while this owning allocation remains alive. + fn render( + context: EVT_HANDLE, + event: EVT_HANDLE, + flags: u32, + cap: usize, + until: Instant, + cancelled: &AtomicBool, + ) -> Result<(Vec, usize, u32)> { + ensure!( + !cancelled.load(Ordering::SeqCst) && Instant::now() < until, + "EVENT_LOG_TIME_OR_CANCELLED" + ); + let mut used = 0; + let mut count = 0; + let first = unsafe { + EvtRender( + context, + event, + flags, + 0, + ptr::null_mut(), + &mut used, + &mut count, + ) + }; + let error = if first == 0 { + unsafe { GetLastError() } + } else { + 0 + }; + ensure!( + !cancelled.load(Ordering::SeqCst) && Instant::now() < until, + "EVENT_LOG_TIME_OR_CANCELLED" + ); + if first == 0 && error != ERROR_INSUFFICIENT_BUFFER { + return Err(std::io::Error::from_raw_os_error(error as i32).into()); + } + ensure!(first == 0 && used > 0, "EVENT_LOG_RENDER_PROBE"); + if used as usize > cap { + return Err(std::io::Error::from_raw_os_error(ERROR_INSUFFICIENT_BUFFER as i32).into()); + } + let mut buffer = vec![0u64; (used as usize).div_ceil(8)]; + let allocated = buffer.len() * 8; + ensure!( + !cancelled.load(Ordering::SeqCst) && Instant::now() < until, + "EVENT_LOG_TIME_OR_CANCELLED" + ); + let ok = unsafe { + EvtRender( + context, + event, + flags, + allocated as u32, + buffer.as_mut_ptr().cast(), + &mut used, + &mut count, + ) + }; + let error = if ok == 0 { + unsafe { GetLastError() } + } else { + 0 + }; + ensure!( + !cancelled.load(Ordering::SeqCst) && Instant::now() < until, + "EVENT_LOG_TIME_OR_CANCELLED" + ); + if ok == 0 { + return Err(std::io::Error::from_raw_os_error(error as i32).into()); + } + ensure!(used as usize <= allocated, "EVENT_LOG_RENDER_SIZE"); + Ok((buffer, used as usize, count)) + } + fn string(value: &EVT_VARIANT, buffer: &[u64], used: usize) -> Result { + ensure!( + value.Type == EvtVarTypeString as u32, + "EVENT_LOG_PROPERTY_TYPE" + ); + let pointer = unsafe { value.Anonymous.StringVal }; + let address = pointer as usize; + let base = buffer.as_ptr() as usize; + ensure!( + address >= base && address < base + used && address.is_multiple_of(2), + "EVENT_LOG_PROPERTY_POINTER" + ); + let length = (base + used - address) / 2; + let units = unsafe { std::slice::from_raw_parts(pointer, length) }; + let end = units + .iter() + .position(|x| *x == 0) + .context("EVENT_LOG_PROPERTY_TERMINATOR")?; + ensure!(end <= 256, "EVENT_LOG_PROPERTY_LIMIT"); + Ok(String::from_utf16(&units[..end])?) + } + fn read_event( + context: EVT_HANDLE, + event: EVT_HANDLE, + q: &Query, + until: Instant, + cancelled: &AtomicBool, + ) -> Result { + let (buffer, used, count) = render( + context, + event, + EvtRenderEventValues, + 16 * 1024, + until, + cancelled, + )?; + ensure!( + count == EvtSystemPropertyIdEND as u32 + && used >= count as usize * size_of::(), + "EVENT_LOG_PROPERTY_COUNT" + ); + let values = unsafe { + std::slice::from_raw_parts(buffer.as_ptr().cast::(), count as usize) + }; + let provider = string(&values[EvtSystemProviderName as usize], &buffer, used)?; + let channel = string(&values[EvtSystemChannel as usize], &buffer, used)?; + ensure!( + provider == PROVIDER && channel == CHANNEL, + "EVENT_LOG_SOURCE_MISMATCH" + ); + let record = &values[EvtSystemEventRecordId as usize]; + let time = &values[EvtSystemTimeCreated as usize]; + let id = &values[EvtSystemEventID as usize]; + ensure!( + record.Type == EvtVarTypeUInt64 as u32 + && time.Type == EvtVarTypeFileTime as u32 + && id.Type == EvtVarTypeUInt16 as u32, + "EVENT_LOG_PROPERTY_TYPE" + ); + let record_id = unsafe { record.Anonymous.UInt64Val }; + let ticks = unsafe { time.Anonymous.FileTimeVal }; + let unix_ticks = ticks + .checked_sub(116_444_736_000_000_000) + .context("EVENT_LOG_TIMESTAMP")?; + let created_utc = DateTime::from_timestamp( + i64::try_from(unix_ticks / 10_000_000)?, + ((unix_ticks % 10_000_000) * 100) as u32, + ) + .context("EVENT_LOG_TIMESTAMP")?; + ensure!( + record_id > 0 && created_utc >= q.start_utc && created_utc < q.end_utc, + "EVENT_LOG_RECORD_WINDOW" + ); + let (xml, bytes, _) = render( + 0, + event, + EvtRenderEventXml, + (q.max_output_bytes as usize).min(64 * 1024), + until, + cancelled, + )?; + ensure!(bytes >= 2 && bytes.is_multiple_of(2), "EVENT_LOG_XML_SIZE"); + let units = unsafe { std::slice::from_raw_parts(xml.as_ptr().cast::(), bytes / 2) }; + ensure!( + units.last() == Some(&0) && !units[..units.len() - 1].contains(&0), + "EVENT_LOG_XML_TERMINATOR" + ); + Ok(Event { + channel, + provider, + record_id, + event_id: unsafe { id.Anonymous.UInt16Val }, + created_utc, + xml: String::from_utf16(&units[..units.len() - 1])?, + }) + } + pub(super) fn query(q: &Query, xpath: &str, cancelled: &AtomicBool) -> Result { + let mut report = Report::new(q); + let until = Instant::now() + Duration::from_millis(q.timeout_ms as u64); + if stopped(&mut report, cancelled, until) { + return Ok(report); + } + let channel = wide(CHANNEL); + let expression = wide(xpath); + let handle = Handle(unsafe { + EvtQuery( + 0, + channel.as_ptr(), + expression.as_ptr(), + EvtQueryChannelPath | EvtQueryForwardDirection, + ) + }); + if handle.0 == 0 { + failure(&mut report, unsafe { GetLastError() }); + return Ok(report); + } + if stopped(&mut report, cancelled, until) { + return Ok(report); + } + let context = + Handle(unsafe { EvtCreateRenderContext(0, ptr::null(), EvtRenderContextSystem) }); + if context.0 == 0 { + failure(&mut report, unsafe { GetLastError() }); + return Ok(report); + } + loop { + if stopped(&mut report, cancelled, until) { + break; + } + if report.events.len() >= q.max_events as usize { + report.stop("EVENT_LOG_EVENT_LIMIT", None); + break; + } + let mut event = 0; + let mut returned = 0; + let timeout = until + .saturating_duration_since(Instant::now()) + .as_millis() + .min(100) as u32; + let ok = unsafe { EvtNext(handle.0, 1, &mut event, timeout, 0, &mut returned) }; + let error = if ok == 0 { + unsafe { GetLastError() } + } else { + 0 + }; + let event = Handle(event); + if stopped(&mut report, cancelled, until) { + break; + } + if ok == 0 { + if error == ERROR_NO_MORE_ITEMS { + report.completeness = "complete"; + break; + } + if error == ERROR_TIMEOUT { + continue; + } + failure(&mut report, error); + break; + } + ensure!(returned == 1 && event.0 != 0, "EVENT_LOG_NEXT_COUNT"); + let record = read_event(context.0, event.0, q, until, cancelled); + if stopped(&mut report, cancelled, until) { + break; + } + match record { + Ok(record) => { + if !report.push(record, q.max_output_bytes as usize)? { + break; + } + } + Err(error) => { + if let Some(code) = error + .downcast_ref::() + .and_then(std::io::Error::raw_os_error) + { + if code as u32 == ERROR_INSUFFICIENT_BUFFER { + report.stop("EVENT_LOG_EVENT_SIZE_LIMIT", None) + } else { + failure(&mut report, code as u32) + } + } else { + report.quality = "error"; + report.stop("EVENT_LOG_RENDER_OR_RECORD_INVALID", None) + } + break; + } + } + } + ensure!( + serde_json::to_vec(&report)?.len() <= q.max_output_bytes as usize, + "EVENT_LOG_REPORT_LIMIT" + ); + Ok(report) + } +} + +#[cfg(test)] +mod tests { + use super::*; + fn request() -> Query { + let start_utc = DateTime::from_timestamp(1_700_000_000, 0).unwrap(); + Query { + channel: CHANNEL.into(), + provider: PROVIDER.into(), + start_utc, + end_utc: start_utc + chrono::Duration::minutes(5), + max_events: 1000, + max_output_bytes: 4096, + timeout_ms: 1000, + } + } + #[test] + fn only_fixed_source_window_and_finite_limits_are_queryable() { + let q = request(); + let expression = q.xpath().unwrap(); + assert!(expression.len() < 512); + assert!(expression.contains(PROVIDER)); + for case in 0..7 { + let mut q = request(); + match case { + 0 => q.channel = "Security".into(), + 1 => q.provider = "' or 1=1".into(), + 2 => q.end_utc = q.start_utc, + 3 => q.end_utc = q.start_utc + chrono::Duration::hours(25), + 4 => q.max_events = 1001, + 5 => q.max_output_bytes = MAX_BYTES as u32 + 1, + _ => q.timeout_ms = 5001, + }; + assert!(q.xpath().is_err()); + } + } + #[test] + fn serialized_byte_budget_includes_xml_escaping_and_metadata() { + let q = request(); + let mut report = Report::new(&q); + assert!( + !report + .push( + Event { + channel: CHANNEL.into(), + provider: PROVIDER.into(), + record_id: 1, + event_id: 1, + created_utc: q.start_utc, + xml: "\"\\".repeat(2048) + }, + q.max_output_bytes as usize + ) + .unwrap() + ); + assert!(report.events.is_empty()); + assert!(report.truncated); + assert_eq!(report.error_code, Some("EVENT_LOG_BYTE_LIMIT")); + assert!(serde_json::to_vec(&report).unwrap().len() <= q.max_output_bytes as usize); + assert!(!report.bookmark_updated); + } +} diff --git a/crates/agent-windows/src/helper_runtime.rs b/crates/agent-windows/src/helper_runtime.rs new file mode 100644 index 0000000..cf7e3c8 --- /dev/null +++ b/crates/agent-windows/src/helper_runtime.rs @@ -0,0 +1,223 @@ +//! Optional passive helper control client. No SYSTEM action capability is +//! advertised here. Each actual IPC runs on one bounded blocking lane. +use crate::{helper_ipc as ipc, native}; +use anyhow::{Context, Result, ensure}; +use rmm_agent_core::{auth::Binding, helper_reconcile::Snapshot, reconcile::HelperSnapshot}; +use rmm_protocol::{SyncAttempt, SyncResponse}; +use serde_json::json; +use std::{ + io::{Read, Write}, + path::Path, + sync::Arc, + time::Duration, +}; +use tokio::sync::Semaphore; +pub const PIN_PATH: &str = r"C:\ProgramData\RustDiscoverySystem\agent-pin.json"; +const PUBLIC_ROOT: &str = r"C:\ProgramData\RustDiscoverySystem"; +const PROBE_FILE: &str = "helper-probe.json"; +#[derive(Clone)] +pub struct Client { + pin: ipc::Pin, + lane: Arc, +} +impl Client { + /// Missing pin is deliberately passive. A present but invalid pin is refused; + /// no endpoint/key/policy is discovered from the pipe itself. + pub fn discover( + agent_id: &str, + asset_id: &str, + binding: Option<&Binding>, + lane: Arc, + ) -> Result> { + if !Path::new(PIN_PATH).try_exists()? { + return Ok(None); + } + let readers = [ + ipc::service_sid(crate::SERVICE_NAME)?, + ipc::service_sid(ipc::HELPER_SERVICE)?, + ]; + let _root = ipc::PinnedFile::open(Path::new(PUBLIC_ROOT), true, &readers, &[], false)?; + let mut file = ipc::PinnedFile::open(Path::new(PIN_PATH), false, &readers, &[], false)?; + ensure!(file.file.metadata()?.len() <= 16384, "HELPER_PIN_SIZE"); + let mut bytes = Vec::new(); + file.file.read_to_end(&mut bytes)?; + let pin: ipc::Pin = serde_json::from_slice(&bytes)?; + pin.validate()?; + ensure!( + pin.binding.agent_id.to_string() == agent_id + && pin.binding.asset_id.to_string() == asset_id + && pin.binding.enrollment_generation + == binding.map_or(1, |b| b.enrollment_generation), + "HELPER_ENROLLMENT_PIN_MISMATCH" + ); + if let Some(binding) = binding { + ensure!( + ipc::binding_hash(binding)? == ipc::binding_hash(&pin.binding)?, + "HELPER_JOB_BINDING_MISMATCH" + ); + } + Ok(Some(Self { pin, lane })) + } + pub async fn refresh( + agent_id: String, + asset_id: String, + binding: Option, + lane: Arc, + state: std::path::PathBuf, + ) -> Result> { + let permit = lane + .clone() + .try_acquire_owned() + .context("HELPER_CONTROL_BUSY")?; + tokio::task::spawn_blocking(move || { + let _permit = permit; + let result=(|| { + let client=Self::discover(&agent_id,&asset_id,binding.as_ref(),lane)?; + if let Some(client)=&client {client.probe_blocking(&state)?;} + Ok(client) + })(); + if result.is_err() || matches!(&result,Ok(None)) && state.join(PROBE_FILE).try_exists()? { + write_current_probe(&state,&json!({"schema_version":1,"source":"actual_local_service_runtime","agent_process_id":std::process::id(),"agent_id":agent_id,"asset_id":asset_id,"observed_at":chrono::Utc::now(),"authenticated_pipe_exchange":false,"system_capability_advertised":false,"helper_ready":false,"no_jobs":true,"code":"HELPER_NOT_AVAILABLE"}))?; + } + result + }) + .await? + } + fn check_pin(&self) -> Result<()> { + let current = Self::discover( + &self.pin.binding.agent_id.to_string(), + &self.pin.binding.asset_id.to_string(), + Some(&self.pin.binding), + self.lane.clone(), + )? + .context("HELPER_PIN_REMOVED")?; + ensure!( + serde_json::to_vec(¤t.pin)? == serde_json::to_vec(&self.pin)?, + "HELPER_PIN_CHANGED" + ); + Ok(()) + } + pub fn pin_hash(&self) -> &str { + &self.pin.policy_sha256 + } + async fn call(&self, operation: ipc::Operation) -> Result { + let permit = self + .lane + .clone() + .try_acquire_owned() + .context("HELPER_CONTROL_BUSY")?; + let client = self.clone(); + tokio::task::spawn_blocking(move || { + let _permit = permit; + client.check_pin()?; + let call = ipc::Call::new(&client.pin, operation); + let reply = ipc::exchange(&client.pin, &call, Duration::from_secs(5))?; + client.check_pin()?; + Ok(reply) + }) + .await? + } + fn probe_blocking(&self, state: &Path) -> Result<()> { + self.check_pin()?; + let pin = &self.pin; + let call = ipc::Call::new(pin, ipc::Operation::Probe {}); + let reply = ipc::exchange(pin, &call, Duration::from_secs(5))?; + let ipc::ReplyBody::Probe { + clock_qualified, + ready, + ledger, + lock, + } = reply.body + else { + anyhow::bail!("HELPER_PROBE_DENIED"); + }; + let value = json!({"schema_version":1,"source":"actual_local_service_runtime","agent_process_id":std::process::id(),"agent_service":crate::SERVICE_NAME,"agent_id":pin.binding.agent_id,"asset_id":pin.binding.asset_id,"challenge":reply.challenge,"observed_at":chrono::Utc::now(),"helper_service":ipc::HELPER_SERVICE,"verified_helper_user_sid":pin.helper.user_sid,"verified_helper_service_sid":pin.helper.service_sid,"helper_binary_sha256":reply.helper_binary_sha256,"policy_sha256":reply.policy_sha256,"authenticated_pipe_exchange":true,"client_service_pid_verified_by_helper":true,"ledger_file_id":ledger,"executor_lock_file_id":lock,"helper_clock_qualified":clock_qualified,"helper_ready":ready,"system_capability_advertised":false,"no_jobs":true}); + self.check_pin()?; + write_current_probe(state, &value) + } + pub async fn snapshot(&self, generation: u64, boot: uuid::Uuid) -> Result { + let reply = self.call(ipc::Operation::Snapshot { generation }).await?; + let ipc::ReplyBody::Snapshot { snapshot } = reply.body else { + anyhow::bail!("HELPER_SNAPSHOT_DENIED"); + }; + ensure!(snapshot.boot_id == boot, "HELPER_BOOT_MISMATCH"); + Ok(map_snapshot(&snapshot)) + } + pub async fn apply( + &self, + response: &SyncResponse, + request_bytes: Vec, + boot: uuid::Uuid, + ) -> Result { + let reply = self + .call(ipc::Operation::Apply { + response: rmm_agent_core::helper_reconcile::SignedResponse { + decision_bytes: response.decision_bytes.clone(), + signature: response.signature.clone(), + }, + request_bytes, + }) + .await?; + let ipc::ReplyBody::Applied { snapshot, .. } = reply.body else { + anyhow::bail!("HELPER_APPLY_DENIED"); + }; + ensure!(snapshot.boot_id == boot, "HELPER_BOOT_MISMATCH"); + Ok(map_snapshot(&snapshot)) + } +} +fn map_snapshot(s: &Snapshot) -> HelperSnapshot { + HelperSnapshot { + journal_id: s.journal_id, + checkpoint: s.checkpoint, + previous_decision_id: s.previous_decision_id, + attempts: s + .attempts + .iter() + .map(|a| SyncAttempt { + job_id: a.job_id.to_string(), + attempt_id: a.attempt_id.to_string(), + job_payload_hash: a.job_payload_hash.clone(), + grant_id: a.grant_id.to_string(), + consumption: a.consumption.clone(), + state: a.state.clone(), + revision: a.revision, + original_boot_id: a.original_boot_id.to_string(), + outcome_sha256: a.outcome_sha256.clone(), + outcome_bytes: a.outcome_bytes.clone(), + ..Default::default() + }) + .collect(), + } +} +fn write_current_probe(state: &Path, value: &serde_json::Value) -> Result<()> { + native::private_path(state, true)?; + let path = state.join(PROBE_FILE); + if path.try_exists()? { + native::private_path(&path, false)?; + } + let bytes = serde_json::to_vec(value)?; + ensure!(bytes.len() <= 8192, "HELPER_PROBE_REPORT_LIMIT"); + // One fixed create-only staging file bounds even interrupted-report state. + // A leftover from a killed process is refused; it is never overwritten. + let temporary = state.join(".helper-probe.pending"); + let mut file = std::fs::OpenOptions::new() + .write(true) + .create_new(true) + .open(&temporary)?; + let written = (|| { + native::private_path(&temporary, false)?; + file.write_all(&bytes)?; + file.sync_all()?; + Ok::<_, anyhow::Error>(()) + })(); + drop(file); + let result = written.and_then(|()| { + std::fs::rename(&temporary, &path)?; + native::private_path(&path, false) + }); + if result.is_err() { + // Only our successfully create_new-opened staging file is eligible. + let _ = std::fs::remove_file(&temporary); + } + result +} diff --git a/crates/agent-windows/src/lib.rs b/crates/agent-windows/src/lib.rs index 7d49697..2aa4213 100644 --- a/crates/agent-windows/src/lib.rs +++ b/crates/agent-windows/src/lib.rs @@ -1,14 +1,5 @@ //! First Windows slice: passive observations and durable telemetry only. //! No executor, startgrant consumer or privileged helper is advertised. -pub mod journal; -#[cfg(windows)] -pub mod native; -#[cfg(windows)] -pub mod inventory; -#[cfg(windows)] -pub mod service; -#[cfg(windows)] -pub mod process; #[cfg(windows)] pub mod actions; #[cfg(windows)] @@ -16,7 +7,18 @@ pub mod clock; #[cfg(all(windows, feature = "system-helper"))] pub mod helper_ipc; #[cfg(all(windows, feature = "system-helper"))] +pub mod helper_runtime; +#[cfg(all(windows, feature = "system-helper"))] pub mod helper_system; +#[cfg(windows)] +pub mod inventory; +pub mod journal; +#[cfg(windows)] +pub mod native; +#[cfg(windows)] +pub mod process; +#[cfg(windows)] +pub mod service; pub const SERVICE_NAME: &str = "RmmDiscoveryWindows"; pub const CAPABILITIES: &[&str] = &["windows.base/1", "heartbeat/1"]; diff --git a/crates/agent-windows/src/package.rs b/crates/agent-windows/src/package.rs new file mode 100644 index 0000000..b09fea0 --- /dev/null +++ b/crates/agent-windows/src/package.rs @@ -0,0 +1,758 @@ +//! Unwired fixture-only MSI provider. This module confers no authorization. +//! Caller must authenticate its SYSTEM helper, validate signed job/grant+local +//! recipe policy, hold the device/package conflict lock and durably consume the +//! attempt before start(). Cache population is a separate verified RMM artifact +//! operation: this module has no network, URL, UNC, transform or free arguments. +//! +//! Only first install/exact noop of v1.0.0 is implemented. Upgrade and EXE are +//! separate deliveries, not guessed modes of this MSI executor. +//! MSI metadata APIs are synchronous. The caller owns the outer observation +//! deadline; a poll timeout is not a promise that Windows Installer stopped. +//! Do not put this controller in a kill-on-close process-tree executor. +//! https://learn.microsoft.com/en-us/windows/win32/api/msi/nf-msi-msigetproductinfoexw +//! https://learn.microsoft.com/en-us/windows/win32/msi/standard-installer-command-line-options +//! https://learn.microsoft.com/en-us/windows/win32/msi/reboot +//! https://learn.microsoft.com/en-us/windows/win32/msi/error-codes +#[cfg(windows)] +use anyhow::{Result, ensure}; +#[cfg(windows)] +use rmm_domain::windows_execution::ResolvedWindowsMsi; +use serde::Serialize; + +#[derive(Clone, Debug, PartialEq, Eq, Serialize)] +pub struct Snapshot { + pub product_state: Option, + pub product_version: Option, + pub payload_sha256: Option, + pub payload_bytes: Option, +} +impl Snapshot { + #[cfg(windows)] + fn desired(&self, r: &ResolvedWindowsMsi) -> bool { + let p = &r.recipe.postcondition; + self.product_state.as_deref() == Some("5") + && self.product_version.as_deref() == Some(&p.product_version) + && self.payload_sha256.as_deref() == Some(&p.payload_sha256) + && self.payload_bytes == Some(p.payload_bytes) + } + #[cfg(any(windows, test))] + fn absent(&self) -> bool { + self.product_state.is_none() + && self.product_version.is_none() + && self.payload_sha256.is_none() + && self.payload_bytes.is_none() + } +} +#[derive(Debug, Serialize)] +pub struct Observation { + pub recipe_sha256: String, + pub artifact_sha256: String, + pub process_id: Option, + pub state: &'static str, + pub code: &'static str, + pub exit_code: Option, + pub postconditions_passed: bool, + pub already_desired: bool, + pub installer_process_started: bool, + /// None when no transaction-level observation exists; spawning msiexec is + /// not itself evidence that the Windows Installer service began a change. + pub transaction_started: Option, + /// Only the installer's own exit semantics; ambient host reboot state is + /// not inferred from an MSI process exit or from an exact no-op. + pub installer_reboot_required: Option, + pub reboot_initiated: bool, + pub before: Snapshot, + pub after: Option, + pub automatic_retry: bool, +} +#[cfg(any(windows, test))] +fn classification( + exit: u32, + postconditions: bool, +) -> (&'static str, &'static str, Option, bool) { + match exit { + 0 | 3010 if postconditions => ( + "succeeded", + "MSI_POSTCONDITIONS_VERIFIED", + Some(exit == 3010), + false, + ), + 0 | 3010 => ( + "failed", + "MSI_POSTCONDITIONS_FAILED", + Some(exit == 3010), + false, + ), + 1641 => ( + "unknown", + "MSI_UNEXPECTED_REBOOT_INITIATED", + Some(true), + true, + ), + 1618 => ("failed", "MSI_ANOTHER_INSTALLATION_RUNNING", None, false), + _ => ("unknown", "MSI_FAILURE_EFFECT_UNRESOLVED", None, false), + } +} +/// Contract and locally protected policy must agree, independently of the job. +/// The policy hash must come from the helper's protected settings, never a field +/// copied back out of the job being checked. +#[cfg(windows)] +fn validate(r: &ResolvedWindowsMsi, allowed_recipe_sha256: &str) -> Result<()> { + ensure!( + r.validate() && r.recipe_sha256 == allowed_recipe_sha256, + "MSI_RECIPE_NOT_ALLOWED" + ); + Ok(()) +} + +#[cfg(windows)] +mod windows { + use super::*; + use rmm_domain::windows_execution::{MSI_PAYLOAD, MSI_PRODUCT_CODE}; + use sha2::{Digest, Sha256}; + use std::{ + fs::{File, OpenOptions}, + io::{Read, Seek, SeekFrom}, + mem::{size_of, zeroed}, + os::windows::{ + ffi::OsStrExt, + fs::OpenOptionsExt, + io::{AsRawHandle, FromRawHandle}, + }, + path::{Path, PathBuf}, + process::{Child, Command, Stdio}, + ptr, + sync::atomic::{AtomicBool, Ordering}, + time::{Duration, Instant}, + }; + use windows_sys::Win32::{ + Foundation::*, + Security::{Authorization::*, *}, + Storage::FileSystem::*, + System::{ + ApplicationInstallationAndServicing::*, SystemInformation::GetSystemWindowsDirectoryW, + }, + }; + const CACHE: &str = r"C:\ProgramData\RustDiscoverySystem\cache"; + const FIXTURES: &str = r"C:\Program Files\RustDiscoveryFixtures"; + const TRUSTED_INSTALLER: &str = + "S-1-5-80-956008885-3418522649-1831038044-1853292631-2271478464"; + fn wide(s: impl AsRef) -> Vec { + s.as_ref().encode_wide().chain(Some(0)).collect() + } + fn sid(sid: PSID) -> Result { + let mut raw = ptr::null_mut(); + ensure!( + unsafe { ConvertSidToStringSidW(sid, &mut raw) } != 0, + "MSI_SID" + ); + let result = (|| { + let mut n = 0; + while n < 192 && unsafe { *raw.add(n) } != 0 { + n += 1 + } + ensure!(n < 192, "MSI_SID_LIMIT"); + Ok(String::from_utf16(unsafe { + std::slice::from_raw_parts(raw, n) + })?) + })(); + unsafe { + LocalFree(raw.cast()); + } + result + } + fn acl(file: &File, system_binary: bool) -> Result<()> { + let mut owner = ptr::null_mut(); + let mut dacl = ptr::null_mut(); + let mut descriptor = ptr::null_mut(); + ensure!( + unsafe { + GetSecurityInfo( + file.as_raw_handle(), + SE_FILE_OBJECT, + OWNER_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION, + &mut owner, + ptr::null_mut(), + &mut dacl, + ptr::null_mut(), + &mut descriptor, + ) + } == 0, + "MSI_FILE_ACL" + ); + let result = (|| { + ensure!( + !owner.is_null() && !dacl.is_null() && unsafe { (*dacl).AceCount } > 0, + "MSI_NULL_DACL" + ); + let privileged = |s: &str| { + s == "S-1-5-18" || s == "S-1-5-32-544" || (system_binary && s == TRUSTED_INSTALLER) + }; + ensure!(privileged(&sid(owner)?), "MSI_FILE_OWNER"); + for i in 0..unsafe { (*dacl).AceCount } { + let mut raw = ptr::null_mut(); + ensure!( + unsafe { GetAce(dacl, i.into(), &mut raw) } != 0, + "MSI_FILE_ACE" + ); + let ace = unsafe { &*raw.cast::() }; + ensure!( + ace.Header.AceType == 0 + && ace.Header.AceSize as usize >= size_of::(), + "MSI_ACE_TYPE" + ); + if ace.Header.AceFlags & INHERIT_ONLY_ACE as u8 != 0 { + continue; + } + let who = sid(ptr::addr_of!(ace.SidStart).cast_mut().cast())?; + ensure!( + privileged(&who) + || ace.Mask & !(0x0012_00a9 | GENERIC_READ | GENERIC_EXECUTE) == 0, + "MSI_WRITABLE_BY_UNTRUSTED_PRINCIPAL" + ); + } + Ok(()) + })(); + unsafe { + LocalFree(descriptor.cast()); + } + result + } + fn information(file: &File, directory: bool) -> Result { + let mut i: BY_HANDLE_FILE_INFORMATION = unsafe { zeroed() }; + ensure!( + unsafe { GetFileInformationByHandle(file.as_raw_handle(), &mut i) } != 0 + && i.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT == 0 + && (i.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY != 0) == directory, + "MSI_REGULAR_PATH" + ); + Ok(i) + } + fn id(i: &BY_HANDLE_FILE_INFORMATION) -> (u32, u32, u32) { + (i.dwVolumeSerialNumber, i.nFileIndexHigh, i.nFileIndexLow) + } + struct Held { + file: File, + path: PathBuf, + identity: (u32, u32, u32), + _ancestors: Vec, + system_binary: bool, + } + impl Held { + fn open(path: &Path, system_binary: bool) -> Result { + ensure!( + path.is_absolute() + && !path.to_string_lossy().starts_with(r"\\") + && !path.to_string_lossy().contains(".."), + "MSI_LOCAL_PATH" + ); + let mut ancestors = Vec::new(); + for parent in path.ancestors().skip(1) { + let raw = unsafe { + CreateFileW( + wide(parent).as_ptr(), + FILE_READ_ATTRIBUTES, + FILE_SHARE_READ | FILE_SHARE_WRITE, + ptr::null(), + OPEN_EXISTING, + FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, + ptr::null_mut(), + ) + }; + ensure!(raw != INVALID_HANDLE_VALUE, "MSI_ANCESTOR_OPEN"); + let file = unsafe { File::from_raw_handle(raw) }; + information(&file, true)?; + ancestors.push(file); + } + let file = OpenOptions::new() + .read(true) + .share_mode(FILE_SHARE_READ) + .custom_flags(FILE_FLAG_OPEN_REPARSE_POINT) + .open(path)?; + let i = information(&file, false)?; + ensure!(system_binary || i.nNumberOfLinks == 1, "MSI_HARDLINK"); + acl(&file, system_binary)?; + Ok(Self { + file, + path: path.into(), + identity: id(&i), + _ancestors: ancestors, + system_binary, + }) + } + fn check(&self) -> Result<()> { + ensure!( + id(&information(&self.file, false)?) == self.identity, + "MSI_HELD_ID" + ); + acl(&self.file, self.system_binary)?; + let again = Self::open(&self.path, self.system_binary)?; + ensure!(again.identity == self.identity, "MSI_PATH_REPLACED"); + Ok(()) + } + fn hash(&mut self, max: u64) -> Result<(String, u64)> { + self.check()?; + let len = self.file.metadata()?.len(); + ensure!(len > 0 && len <= max, "MSI_FILE_LENGTH"); + self.file.seek(SeekFrom::Start(0))?; + let mut sha = Sha256::new(); + let mut count = 0u64; + let mut buffer = [0u8; 8192]; + loop { + let n = self.file.read(&mut buffer)?; + if n == 0 { + break; + } + count += n as u64; + ensure!(count <= max, "MSI_FILE_GROWTH"); + sha.update(&buffer[..n]); + } + ensure!(count == len, "MSI_FILE_CHANGED"); + self.check()?; + Ok((format!("{:x}", sha.finalize()), count)) + } + } + struct Msi(u32); + impl Drop for Msi { + fn drop(&mut self) { + if self.0 != 0 { + unsafe { + MsiCloseHandle(self.0); + } + } + } + } + fn msi_text(db: u32, query: &str) -> Result { + let mut view = 0; + ensure!( + unsafe { MsiDatabaseOpenViewW(db, wide(query).as_ptr(), &mut view) } == 0, + "MSI_DATABASE_VIEW" + ); + let view = Msi(view); + ensure!( + unsafe { MsiViewExecute(view.0, 0) } == 0, + "MSI_DATABASE_EXECUTE" + ); + let mut record = 0; + ensure!( + unsafe { MsiViewFetch(view.0, &mut record) } == 0, + "MSI_DATABASE_ROW" + ); + let record = Msi(record); + let mut text = [0u16; 256]; + let mut len = 255; + ensure!( + unsafe { MsiRecordGetStringW(record.0, 1, text.as_mut_ptr(), &mut len) } == 0 + && len < 256, + "MSI_DATABASE_STRING" + ); + let mut extra = 0; + let code = unsafe { MsiViewFetch(view.0, &mut extra) }; + let _extra = Msi(extra); + ensure!(code == ERROR_NO_MORE_ITEMS, "MSI_DATABASE_NOT_UNIQUE"); + Ok(String::from_utf16(&text[..len as usize])?) + } + fn no_custom_actions(db: u32) -> Result<()> { + let mut view = 0; + ensure!( + unsafe { + MsiDatabaseOpenViewW( + db, + wide("SELECT `Name` FROM `_Tables` WHERE `Name`='CustomAction'").as_ptr(), + &mut view, + ) + } == 0, + "MSI_TABLE_LIST" + ); + let view = Msi(view); + ensure!(unsafe { MsiViewExecute(view.0, 0) } == 0, "MSI_TABLE_QUERY"); + let mut record = 0; + let code = unsafe { MsiViewFetch(view.0, &mut record) }; + let _record = Msi(record); + if code == ERROR_NO_MORE_ITEMS { + return Ok(()); + } + ensure!(code == 0, "MSI_TABLE_FETCH"); + let mut actions = 0; + ensure!( + unsafe { + MsiDatabaseOpenViewW( + db, + wide("SELECT `Action` FROM `CustomAction`").as_ptr(), + &mut actions, + ) + } == 0, + "MSI_CUSTOM_ACTION_QUERY" + ); + let actions = Msi(actions); + ensure!( + unsafe { MsiViewExecute(actions.0, 0) } == 0, + "MSI_CUSTOM_ACTION_EXECUTE" + ); + let mut action = 0; + let code = unsafe { MsiViewFetch(actions.0, &mut action) }; + let _action = Msi(action); + ensure!(code == ERROR_NO_MORE_ITEMS, "MSI_CUSTOM_ACTIONS_DENIED"); + Ok(()) + } + fn inspect_archive(file: &mut Held, r: &ResolvedWindowsMsi) -> Result<()> { + let (sha, len) = file.hash(262144)?; + ensure!( + sha == r.recipe.artifact.sha256 && len == r.recipe.artifact.bytes, + "MSI_ARTIFACT_HASH" + ); + let mut db = 0; + ensure!( + unsafe { MsiOpenDatabaseW(wide(&file.path).as_ptr(), MSIDBOPEN_READONLY, &mut db) } + == 0, + "MSI_DATABASE_OPEN" + ); + let db = Msi(db); + no_custom_actions(db.0)?; + let cabinet = msi_text(db.0, "SELECT `Cabinet` FROM `Media`")?; + ensure!( + cabinet.starts_with('#') + && cabinet.len() > 1 + && cabinet.len() <= 128 + && cabinet[1..] + .bytes() + .all(|b| b.is_ascii_alphanumeric() || b"_.-".contains(&b)), + "MSI_EMBEDDED_CABINET_REQUIRED" + ); + for (field, value) in [ + ("ProductCode", MSI_PRODUCT_CODE), + ( + "ProductVersion", + r.recipe.postcondition.product_version.as_str(), + ), + ("ALLUSERS", "1"), + ("UpgradeCode", r.recipe.upgrade_code.as_str()), + ] { + ensure!( + msi_text( + db.0, + &format!("SELECT `Value` FROM `Property` WHERE `Property`='{field}'") + )? == value, + "MSI_DATABASE_PROPERTY" + ); + } + let mut summary = 0; + ensure!( + unsafe { MsiGetSummaryInformationW(db.0, ptr::null(), 0, &mut summary) } == 0, + "MSI_SUMMARY" + ); + let summary = Msi(summary); + let mut kind = 0; + let mut value = 0; + let mut time: FILETIME = unsafe { zeroed() }; + let mut text = [0u16; 256]; + let mut len = 255; + ensure!( + unsafe { + MsiSummaryInfoGetPropertyW( + summary.0, + 7, + &mut kind, + &mut value, + &mut time, + text.as_mut_ptr(), + &mut len, + ) + } == 0 + && kind == 30 + && len < 256, + "MSI_ARCHITECTURE_SUMMARY" + ); + ensure!( + String::from_utf16(&text[..len as usize])?.starts_with("x64;"), + "MSI_NOT_X64" + ); + file.check()?; + Ok(()) + } + fn property(name: &str) -> Result> { + let mut value = [0u16; 256]; + let mut len = 255; + let code = unsafe { + MsiGetProductInfoExW( + wide(MSI_PRODUCT_CODE).as_ptr(), + ptr::null(), + MSIINSTALLCONTEXT_MACHINE, + wide(name).as_ptr(), + value.as_mut_ptr(), + &mut len, + ) + }; + if code == ERROR_UNKNOWN_PRODUCT { + return Ok(None); + } + ensure!(code == 0 && len < 256, "MSI_MACHINE_PRODUCT_QUERY"); + Ok(Some(String::from_utf16(&value[..len as usize])?)) + } + pub fn snapshot() -> Result { + let product_state = property("State")?; + let product_version = if product_state.as_deref() == Some("5") { + property("VersionString")? + } else { + None + }; + let (payload_sha256, payload_bytes) = match std::fs::symlink_metadata(MSI_PAYLOAD) { + Ok(_) => { + let mut file = Held::open(Path::new(MSI_PAYLOAD), false)?; + let (sha, len) = file.hash(65536)?; + (Some(sha), Some(len)) + } + Err(e) if e.kind() == std::io::ErrorKind::NotFound => (None, None), + Err(e) => return Err(e.into()), + }; + Ok(Snapshot { + product_state, + product_version, + payload_sha256, + payload_bytes, + }) + } + fn system_root() -> Result { + let mut value = [0u16; 32768]; + let len = unsafe { GetSystemWindowsDirectoryW(value.as_mut_ptr(), value.len() as u32) }; + ensure!(len > 0 && (len as usize) < value.len(), "MSI_SYSTEM_ROOT"); + Ok(PathBuf::from(String::from_utf16(&value[..len as usize])?)) + } + pub struct Prepared { + recipe: ResolvedWindowsMsi, + archive: Held, + msiexec: Held, + _roots: Vec, + before: Snapshot, + system_root: PathBuf, + } + impl Prepared { + pub fn prepare(r: &ResolvedWindowsMsi, allowed_recipe_sha256: &str) -> Result { + validate(r, allowed_recipe_sha256)?; + let mut roots = Vec::new(); + for path in [CACHE, FIXTURES] { + let file = OpenOptions::new() + .read(true) + .share_mode(FILE_SHARE_READ | FILE_SHARE_WRITE) + .custom_flags(FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT) + .open(path)?; + information(&file, true)?; + acl(&file, false)?; + roots.push(file); + } + let mut archive = Held::open( + &Path::new(CACHE).join(format!("{}.msi", r.recipe.artifact.sha256)), + false, + )?; + inspect_archive(&mut archive, r)?; + let system_root = system_root()?; + let msiexec = Held::open(&system_root.join("System32").join("msiexec.exe"), true)?; + let before = snapshot()?; + ensure!( + before.absent() || before.desired(r), + "MSI_EXISTING_PRODUCT_OR_PAYLOAD_CONFLICT" + ); + Ok(Self { + recipe: r.clone(), + archive, + msiexec, + _roots: roots, + before, + system_root, + }) + } + /// Called only after durable original-grant consumption. No change of + /// config, privilege, policy, clock, reboot or foreign process occurs. + pub fn start( + mut self, + timeout: Duration, + cancelled: &AtomicBool, + check_effect_fence: impl FnOnce() -> Result<()>, + ) -> Result { + ensure!( + timeout >= Duration::from_secs(1) + && timeout <= Duration::from_secs(300) + && !cancelled.load(Ordering::SeqCst), + "MSI_START_LIMIT_OR_CANCELLED" + ); + inspect_archive(&mut self.archive, &self.recipe)?; + self.msiexec.check()?; + let before = snapshot()?; + ensure!(before == self.before, "MSI_BASELINE_CHANGED"); + if before.desired(&self.recipe) { + return Ok(Run::Noop(Box::new(Observation { + recipe_sha256: self.recipe.recipe_sha256.clone(), + artifact_sha256: self.recipe.recipe.artifact.sha256.clone(), + process_id: None, + state: "succeeded", + code: "MSI_ALREADY_DESIRED", + exit_code: None, + postconditions_passed: true, + already_desired: true, + installer_process_started: false, + transaction_started: Some(false), + installer_reboot_required: None, + reboot_initiated: false, + before: before.clone(), + after: Some(before), + automatic_retry: false, + }))); + } + ensure!(before.absent(), "MSI_INSTALL_BASELINE_CONFLICT"); + use std::os::windows::process::CommandExt; + check_effect_fence()?; + ensure!( + !cancelled.load(Ordering::SeqCst), + "MSI_CANCELLED_BEFORE_START" + ); + let began = Instant::now(); + let child = Command::new(&self.msiexec.path) + .args([ + std::ffi::OsStr::new("/i"), + self.archive.path.as_os_str(), + std::ffi::OsStr::new("/qn"), + std::ffi::OsStr::new("/norestart"), + std::ffi::OsStr::new("REBOOT=ReallySuppress"), + std::ffi::OsStr::new("ALLUSERS=1"), + ]) + .env_clear() + .env("SystemRoot", &self.system_root) + .current_dir(CACHE) + .stdin(Stdio::null()) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .creation_flags(0x08000000) + .spawn()?; + Ok(Run::Installer(Box::new(Installer { + prepared: self, + child, + began, + timeout, + }))) + } + } + pub enum Run { + Noop(Box), + Installer(Box), + } + pub struct Installer { + prepared: Prepared, + child: Child, + began: Instant, + timeout: Duration, + } + impl Installer { + pub fn process_id(&self) -> u32 { + self.child.id() + } + /// Poll the SAME process only. Timeout/cancel neither kills msiexec nor + /// the system installer service. Caller retains/fences this attempt; + /// another poll is evidence gathering, never a second start. + pub fn poll(&mut self, cancelled: &AtomicBool) -> Result> { + let exit = self.child.try_wait()?; + let code = if let Some(status) = exit { + status.code().map(|n| n as u32) + } else { + None + }; + if exit.is_none() + && !cancelled.load(Ordering::SeqCst) + && self.began.elapsed() < self.timeout + { + return Ok(None); + } + let mut after = None; + let (state, error, reboot, initiated) = if let Some(code) = code { + after = snapshot().ok(); + if after.is_none() && matches!(code, 0 | 3010) { + ( + "unknown", + "MSI_POSTCONDITION_QUERY_FAILED", + Some(code == 3010), + false, + ) + } else { + classification( + code, + after + .as_ref() + .is_some_and(|s| s.desired(&self.prepared.recipe)), + ) + } + } else { + ( + "unknown", + if cancelled.load(Ordering::SeqCst) { + "MSI_CANCELLED_EFFECT_UNRESOLVED" + } else { + "MSI_TIMEOUT_EFFECT_UNRESOLVED" + }, + None, + false, + ) + }; + Ok(Some(Observation { + recipe_sha256: self.prepared.recipe.recipe_sha256.clone(), + artifact_sha256: self.prepared.recipe.recipe.artifact.sha256.clone(), + process_id: Some(self.child.id()), + state, + code: error, + exit_code: code, + postconditions_passed: state == "succeeded", + already_desired: false, + installer_process_started: true, + transaction_started: if code == Some(1618) { + Some(false) + } else { + None + }, + installer_reboot_required: reboot, + reboot_initiated: initiated, + before: self.prepared.before.clone(), + after, + automatic_retry: false, + })) + } + } +} +#[cfg(windows)] +pub use windows::{Installer, Prepared, Run, snapshot}; +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn exit_code_never_substitutes_for_independent_postconditions_or_reboot_authority() { + assert_eq!( + classification(0, true), + ( + "succeeded", + "MSI_POSTCONDITIONS_VERIFIED", + Some(false), + false + ) + ); + assert_eq!(classification(3010, true).2, Some(true)); + assert_eq!(classification(0, false).0, "failed"); + assert_eq!( + classification(1641, true), + ( + "unknown", + "MSI_UNEXPECTED_REBOOT_INITIATED", + Some(true), + true + ) + ); + assert_eq!( + classification(1618, false).1, + "MSI_ANOTHER_INSTALLATION_RUNNING" + ); + assert_eq!(classification(1603, false).0, "unknown"); + let advertised = Snapshot { + product_state: Some("1".into()), + product_version: None, + payload_sha256: None, + payload_bytes: None, + }; + assert!(!advertised.absent()); + } +} diff --git a/crates/agent-windows/src/runtime.rs b/crates/agent-windows/src/runtime.rs index 0ac3670..1caeb85 100644 --- a/crates/agent-windows/src/runtime.rs +++ b/crates/agent-windows/src/runtime.rs @@ -337,6 +337,30 @@ fn open_ledger(directory: &Path, binding: &Binding) -> Result<(Connection, fs::F } Ok((db, file)) } +#[cfg(feature = "system-helper")] +enum HelperControl { + Snapshot( + u64, + tokio::task::JoinHandle>, + ), + Apply( + u64, + tokio::task::JoinHandle>, + ), +} +#[cfg(feature = "system-helper")] +impl HelperControl { + fn finished(&self) -> bool { + match self { + Self::Snapshot(_, t) | Self::Apply(_, t) => t.is_finished(), + } + } + fn abort(self) { + match self { + Self::Snapshot(_, t) | Self::Apply(_, t) => t.abort(), + } + } +} struct Agent { config: Config, identity: EnrollmentResponse, @@ -351,6 +375,23 @@ struct Agent { clock: JobClock, sync: Option, prepared: bool, + #[cfg(feature = "system-helper")] + prepared_request: Option>, + #[cfg(feature = "system-helper")] + helper_lane: Arc, + #[cfg(feature = "system-helper")] + helper: Option, + #[cfg(feature = "system-helper")] + helper_probe: + Option>>>, + #[cfg(feature = "system-helper")] + helper_probe_at: Instant, + #[cfg(feature = "system-helper")] + helper_control: Option, + #[cfg(feature = "system-helper")] + helper_checked: bool, + #[cfg(feature = "system-helper")] + helper_refused: bool, fence: Arc, completed_tx: tokio::sync::mpsc::Sender, completed_rx: tokio::sync::mpsc::Receiver, @@ -364,8 +405,110 @@ impl Agent { sync.close(); } self.prepared = false; + #[cfg(feature = "system-helper")] + { + self.prepared_request = None; + if let Some(control) = self.helper_control.take() { + control.abort(); + } + } self.deferred_permit = None; } + fn helper_control_pending(&self) -> bool { + #[cfg(feature = "system-helper")] + { + self.helper_control.is_some() + || self.helper_probe.is_some() + || !self.helper_checked + || self.helper_refused + } + #[cfg(not(feature = "system-helper"))] + { + false + } + } + #[cfg(feature = "system-helper")] + async fn poll_helper( + &mut self, + channel: &crate::wire_channel::Handle, + generation: u64, + ) -> Result<()> { + if self + .helper_control + .as_ref() + .is_some_and(HelperControl::finished) + { + match self + .helper_control + .take() + .context("HELPER_CONTROL_REQUIRED")? + { + HelperControl::Snapshot(expected, task) => { + ensure!(expected == generation, "HELPER_CHANNEL_CHANGED"); + let snapshot = task.await??; + self.send_sync(channel, generation, Some(&snapshot)).await?; + } + HelperControl::Apply(expected, task) => { + ensure!(expected == generation, "HELPER_CHANNEL_CHANGED"); + let snapshot = task.await??; + self.sync + .as_mut() + .context("SYNC_NOT_CONFIGURED")? + .confirm_helper(&snapshot)?; + self.open_fence(generation)?; + } + } + } + if self + .helper_probe + .as_ref() + .is_some_and(|task| task.is_finished()) + { + let result = self + .helper_probe + .take() + .context("HELPER_PROBE_REQUIRED")? + .await?; + self.helper_probe_at = Instant::now(); + self.helper_checked = true; + match result { + Ok(client) => { + self.helper_refused = false; + if self.helper.as_ref().map(|c| c.pin_hash()) + != client.as_ref().map(|c| c.pin_hash()) + { + self.close_gate(); + } + self.helper = client; + } + Err(error) => { + self.helper_refused = true; + self.helper = None; + self.close_gate(); + return Err(error); + } + } + } + if self.helper_probe.is_none() + && self.helper_control.is_none() + && !self.prepared + && self.running.is_none() + && self.helper_probe_at.elapsed() >= Duration::from_secs(30) + { + let agent = self.identity.agent_id.clone(); + let asset = self.identity.asset_id.clone(); + let binding = self.binding.clone(); + let lane = self.helper_lane.clone(); + let state = self.config.state_directory.clone(); + self.helper_probe = Some(tokio::spawn(async move { + rmm_agent_windows::helper_runtime::Client::refresh( + agent, asset, binding, lane, state, + ) + .await + })); + } + Ok(()) + } fn channel(&self, generation: u64) -> Result { Ok(reconcile::Channel { connection_generation: generation, @@ -374,6 +517,7 @@ impl Agent { } fn ready(&self, generation: u64) -> bool { self.config.enable_jobs + && !self.helper_control_pending() && self.channel(generation).is_ok_and(|channel| { self.sync .as_ref() @@ -461,15 +605,38 @@ impl Agent { generation: u64, ) -> Result<()> { self.fence.close(); + ensure!(!self.helper_control_pending(), "HELPER_CONTROL_NOT_SETTLED"); + #[cfg(feature = "system-helper")] + if let Some(helper) = self.helper.clone() { + ensure!(self.helper_control.is_none(), "HELPER_CONTROL_PENDING"); + let boot = self.channel(generation)?.boot_id; + self.helper_control = Some(HelperControl::Snapshot( + generation, + tokio::spawn(async move { helper.snapshot(generation, boot).await }), + )); + return Ok(()); + } + self.send_sync(channel, generation, None).await + } + async fn send_sync( + &mut self, + channel: &crate::wire_channel::Handle, + generation: u64, + helper: Option<&reconcile::HelperSnapshot>, + ) -> Result<()> { let context = self.channel(generation)?; let sync = self.sync.as_mut().context("SYNC_NOT_CONFIGURED")?; let prepared = sync.prepare( self.journal.connection_mut(), self.ledger.as_ref().context("LEDGER_REQUIRED")?, context, - None, + helper, &self.clock, )?; + #[cfg(feature = "system-helper")] + { + self.prepared_request = Some(prepared.request.encode_to_vec()); + } self.prepared = true; channel.send(&prepared.envelope).await?; Ok(()) @@ -479,14 +646,44 @@ impl Agent { self.prepared = false; let sync = self.sync.as_mut().context("SYNC_NOT_CONFIGURED")?; let applied = sync.apply(self.journal.connection_mut(), &response, &self.clock)?; - ensure!( - applied.helper_instruction.is_none(), - "WINDOWS_HELPER_NOT_CONFIGURED" - ); sync.apply_consumption( self.ledger.as_mut().context("LEDGER_REQUIRED")?, &self.clock, )?; + if let Some(instruction) = applied.helper_instruction { + #[cfg(feature = "system-helper")] + { + ensure!(self.helper_control.is_none(), "HELPER_CONTROL_PENDING"); + let helper = self + .helper + .clone() + .context("WINDOWS_HELPER_NOT_CONFIGURED")?; + let request = self + .prepared_request + .take() + .context("ORIGINAL_SYNC_REQUEST_REQUIRED")?; + let boot = self.channel(generation)?.boot_id; + self.helper_control = Some(HelperControl::Apply( + generation, + tokio::spawn(async move { + helper.apply(&instruction.response, request, boot).await + }), + )); + return Ok(()); + } + #[cfg(not(feature = "system-helper"))] + { + let _ = instruction; + anyhow::bail!("WINDOWS_HELPER_NOT_CONFIGURED"); + } + } + #[cfg(feature = "system-helper")] + { + self.prepared_request = None; + } + self.open_fence(generation) + } + fn open_fence(&self, generation: u64) -> Result<()> { if self.ready(generation) { let deadline = self .sync @@ -797,6 +994,8 @@ impl Agent { tokio::select! { Some(completion)=self.completed_rx.recv()=>{self.completed(generation,completion)?;needs_sync=true;}, _=tick.tick()=>{ + #[cfg(feature = "system-helper")] + if !stop.load(Ordering::SeqCst) && self.poll_helper(&channel,generation).await.is_err() {self.close_gate();needs_sync=self.sync.is_some();} if !stop.load(Ordering::SeqCst) && let Some((pending_generation,permit))=self.deferred_permit.take() { ensure!(pending_generation==generation,"PENDING_PERMIT_GENERATION");self.permit(generation,permit)?; } @@ -809,7 +1008,7 @@ impl Agent { if !stop.load(Ordering::SeqCst) && !busy { if capabilities_at.elapsed()>=Duration::from_secs(60) {capabilities_at=Instant::now();if advertised!=self.capabilities(){return Ok(());}} if self.prepared && sync_at.elapsed()>=Duration::from_secs(20){self.close_gate();needs_sync=true;} - if self.sync.is_some() && !self.prepared && (needs_sync || sync_at.elapsed()>=Duration::from_secs(40) || !self.ready(generation)) { + if self.sync.is_some() && !self.prepared && !self.helper_control_pending() && (needs_sync || sync_at.elapsed()>=Duration::from_secs(40) || !self.ready(generation)) { if self.start_sync(&channel,generation).await.is_err(){self.close_gate();needs_sync=true;}else{needs_sync=false;}sync_at=Instant::now(); } if !needs_sync && !self.prepared && self.ready(generation) && jobs::events(self.journal.connection())?.is_empty() && self.journal.count()?==0 { @@ -936,7 +1135,7 @@ fn service_run(stop: &'static AtomicBool) -> Result<()> { ensure!(!config.enable_jobs || binding.is_some(),"ENABLED_JOBS_REQUIRE_BINDING"); let (ledger,ledger_file)=match &binding {Some(binding)=>{let (db,file)=open_ledger(&config.state_directory,binding)?;(Some(db),Some(file))},None=>(None,None)}; let clock=JobClock{native:Arc::new(rmm_agent_windows::clock::NativeClock::new()),enabled:config.enable_jobs}; - let sync=if config.enable_jobs {binding.clone().map(reconcile::ClientSync::new)} else {None}; + let sync=if config.enable_jobs {binding.clone().map(|binding|reconcile::ClientSync::new(binding).with_helper_identity(reconcile::HelperIdentity::System))} else {None}; let (completed_tx,completed_rx)=tokio::sync::mpsc::channel(1); let mut agent = Agent { config, @@ -946,6 +1145,22 @@ fn service_run(stop: &'static AtomicBool) -> Result<()> { boot, instance: Uuid::new_v4().to_string(), started: Instant::now(), binding, ledger, _ledger_file:ledger_file, clock, sync, + #[cfg(feature="system-helper")] + prepared_request:None, + #[cfg(feature="system-helper")] + helper_lane:Arc::new(tokio::sync::Semaphore::new(1)), + #[cfg(feature="system-helper")] + helper:None, + #[cfg(feature="system-helper")] + helper_probe:None, + #[cfg(feature="system-helper")] + helper_probe_at:Instant::now()-Duration::from_secs(30), + #[cfg(feature="system-helper")] + helper_control:None, + #[cfg(feature="system-helper")] + helper_checked:false, + #[cfg(feature="system-helper")] + helper_refused:false, prepared:false,fence:Arc::new(EffectFence::default()),completed_tx,completed_rx,running:None,deferred_permit:None, }; agent.recover_interrupted()?; diff --git a/crates/domain/src/windows_execution.rs b/crates/domain/src/windows_execution.rs new file mode 100644 index 0000000..0f135fd --- /dev/null +++ b/crates/domain/src/windows_execution.rs @@ -0,0 +1,159 @@ +//! Unexported Windows MSI fixture contract. No legacy Linux codec is changed. +//! This version supports first installation or independently proven exact noop; +//! an upgrade requires a separate reviewed recipe with explicit predecessor +//! products/UpgradeCode semantics. No implicit REINSTALL flags are inferred. +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; +use uuid::Uuid; + +pub const MSI_RECIPE_ID: &str = "rmm-windows-msi-fixture-install-1.0"; +pub const MSI_PRODUCT_CODE: &str = "{3F070B4B-9C08-4D65-BD41-13D81B884AEC}"; +pub const MSI_UPGRADE_CODE: &str = "{C9BF1FB8-E31D-4BD7-BA0D-AB46539BBBAA}"; +pub const MSI_VERSION: &str = "1.0.0"; +pub const MSI_PAYLOAD: &str = r"C:\Program Files\RustDiscoveryFixtures\msi-payload.txt"; +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum Architecture { + X64, +} +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum InstallContext { + Machine, +} +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum MsiCommandProfile { + FixtureQuietNoRebootV1, +} +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct MsiArtifact { + pub artifact_id: Uuid, + pub sha256: String, + pub bytes: u64, +} +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct MsiPostcondition { + pub product_code: String, + pub product_version: String, + pub payload_path: String, + pub payload_sha256: String, + pub payload_bytes: u64, +} +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct WindowsMsiRecipe { + pub schema_version: u32, + pub recipe_id: String, + pub recipe_version: String, + pub platform: String, + pub upgrade_code: String, + pub architecture: Architecture, + pub context: InstallContext, + pub command_profile: MsiCommandProfile, + pub artifact: MsiArtifact, + pub postcondition: MsiPostcondition, + pub no_reboot: bool, +} +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct ResolvedWindowsMsi { + pub recipe: WindowsMsiRecipe, + /// SHA256 of exactly serde_json::to_vec(recipe), not a self-reference. + pub recipe_sha256: String, +} +pub fn digest(value: &str) -> bool { + value.len() == 64 + && value + .bytes() + .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)) +} +impl WindowsMsiRecipe { + pub fn validate(&self) -> bool { + self.schema_version == 1 + && self.recipe_id == MSI_RECIPE_ID + && self.recipe_version == "1" + && self.platform == "windows" + && self.upgrade_code == MSI_UPGRADE_CODE + && self.no_reboot + && !self.artifact.artifact_id.is_nil() + && digest(&self.artifact.sha256) + && (1..=262144).contains(&self.artifact.bytes) + && self.postcondition.product_code == MSI_PRODUCT_CODE + && self.postcondition.product_version == MSI_VERSION + && self.postcondition.payload_path == MSI_PAYLOAD + && digest(&self.postcondition.payload_sha256) + && (1..=65536).contains(&self.postcondition.payload_bytes) + } + pub fn sha256(&self) -> Result { + Ok(format!("{:x}", Sha256::digest(serde_json::to_vec(self)?))) + } +} +impl ResolvedWindowsMsi { + pub fn validate(&self) -> bool { + self.recipe.validate() + && digest(&self.recipe_sha256) + && self + .recipe + .sha256() + .is_ok_and(|sha| sha == self.recipe_sha256) + } +} +#[cfg(test)] +mod tests { + use super::*; + fn recipe() -> WindowsMsiRecipe { + WindowsMsiRecipe { + schema_version: 1, + recipe_id: MSI_RECIPE_ID.into(), + recipe_version: "1".into(), + platform: "windows".into(), + upgrade_code: MSI_UPGRADE_CODE.into(), + architecture: Architecture::X64, + context: InstallContext::Machine, + command_profile: MsiCommandProfile::FixtureQuietNoRebootV1, + artifact: MsiArtifact { + artifact_id: Uuid::new_v4(), + sha256: "ab".repeat(32), + bytes: 1234, + }, + postcondition: MsiPostcondition { + product_code: MSI_PRODUCT_CODE.into(), + product_version: MSI_VERSION.into(), + payload_path: MSI_PAYLOAD.into(), + payload_sha256: "cd".repeat(32), + payload_bytes: 20, + }, + no_reboot: true, + } + } + #[test] + fn immutable_fixture_recipe_rejects_reboot_url_transform_and_implicit_upgrade() { + let recipe = recipe(); + let bound = ResolvedWindowsMsi { + recipe_sha256: recipe.sha256().unwrap(), + recipe: recipe.clone(), + }; + assert!(bound.validate()); + let mut changed = bound.clone(); + changed.recipe.artifact.bytes += 1; + assert!(!changed.validate()); + for case in 0..6 { + let mut r = recipe.clone(); + match case { + 0 => r.no_reboot = false, + 1 => r.postcondition.product_code = Uuid::new_v4().to_string(), + 2 => r.postcondition.product_version = "1.1.0".into(), + 3 => r.postcondition.payload_path = r"\\server\share\payload".into(), + 4 => r.artifact.bytes = 262145, + _ => r.platform = "linux".into(), + }; + assert!(!r.validate()); + } + let mut value = serde_json::to_value(recipe).unwrap(); + value["transforms"] = serde_json::json!(["unapproved.mst"]); + assert!(serde_json::from_value::(value).is_err()); + } +} diff --git a/deploy/windows/Build-NativeMsvc.ps1 b/deploy/windows/Build-NativeMsvc.ps1 new file mode 100644 index 0000000..c99998b --- /dev/null +++ b/deploy/windows/Build-NativeMsvc.ps1 @@ -0,0 +1,274 @@ +#requires -Version 5.1 +<#! +Native development build only. No tool installation, download, service change, +enrollment, RMM job, reboot, certificate-store or persistent policy modification. +Invoke this file in the authorized bootstrap process with RemoteSigned; GPO +precedence is retained. This is no policy exception for the RMM job executor. +Preflight creates a private receipt; Build requires its explicitly selected +source/tool hashes and creates a NEW private case. A failed case is never reused. +!#> +[CmdletBinding()] +param( + [ValidateSet('Preflight','Build')][string]$Mode='Preflight', + [Parameter(Mandatory=$true)][string]$SourceRoot, + [string]$EvidenceRoot=(Join-Path $env:USERPROFILE 'RmmNativeBuilds'), + [string]$ExpectedPreflight, + [string]$RustupExe, + [string]$RustupHome, + [string]$CargoHome, + [string]$ScriptFile=$PSCommandPath, + [ValidatePattern('^10\.0\.26100\.0$')][string]$SdkVersion='10.0.26100.0', + [ValidateRange(10,100)][int]$MinimumFreeGiB=20, + [Guid]$CaseId=([Guid]::NewGuid()) +) +Set-StrictMode -Version Latest +$ErrorActionPreference='Stop' +$Target='x86_64-pc-windows-msvc' +$Toolchain='1.99.1-x86_64-pc-windows-msvc' +$case=$null +$report=[ordered]@{schema_version=1;case_id=$CaseId.ToString();mode=$Mode;completed=$false;stage='initial'; + target=$Target;toolchain=$Toolchain;tool_installation=$false;services_changed=$false;reboot_requested=$false; + jobs_created=$false;clock_qualified=$false;unit_tests_executed=$false;abi_probe_executed=$false} +$savedEnvironment=@{} + +function Require([bool]$Condition,[string]$Code) {if (-not $Condition) {throw $Code}} +function Hash([string]$Path) {return (Get-FileHash -LiteralPath $Path -Algorithm SHA256).Hash.ToLowerInvariant()} +function No-Reparse([string]$Path) { + $item=Get-Item -LiteralPath $Path -Force + while ($null -ne $item) { + Require (($item.Attributes -band [IO.FileAttributes]::ReparsePoint) -eq 0) 'BUILD_REPARSE_PATH' + if ($item -is [IO.FileInfo]) {$item=$item.Directory} else {$item=$item.Parent} + } +} +function Protected-BuildRoot([string]$Path) { + $acl=Get-Acl -LiteralPath $Path + $me=[Security.Principal.WindowsIdentity]::GetCurrent().User.Value + $trusted=@($me,'S-1-5-18','S-1-5-32-544') + Require ($acl.GetOwner([Security.Principal.SecurityIdentifier]).Value -in $trusted) 'BUILD_ROOT_OWNER' + $write=[Security.AccessControl.FileSystemRights]::Write -bor [Security.AccessControl.FileSystemRights]::Delete -bor + [Security.AccessControl.FileSystemRights]::DeleteSubdirectoriesAndFiles -bor + [Security.AccessControl.FileSystemRights]::ChangePermissions -bor [Security.AccessControl.FileSystemRights]::TakeOwnership + foreach ($ace in $acl.Access) { + if ($ace.AccessControlType -eq 'Allow' -and ($ace.FileSystemRights -band $write) -ne 0) { + Require ($ace.IdentityReference.Translate([Security.Principal.SecurityIdentifier]).Value -in $trusted) 'UNTRUSTED_BUILD_ROOT_WRITER' + } + } +} +function New-PrivateDirectory([string]$Path) { + Require (-not (Test-Path -LiteralPath $Path)) 'BUILD_CASE_ALREADY_EXISTS' + [void][IO.Directory]::CreateDirectory($Path) + $acl=New-Object Security.AccessControl.DirectorySecurity + $acl.SetAccessRuleProtection($true,$false) + $me=[Security.Principal.WindowsIdentity]::GetCurrent().User + $acl.SetOwner($me) + foreach ($sid in @($me.Value,'S-1-5-18','S-1-5-32-544') | Select-Object -Unique) { + $rule=New-Object Security.AccessControl.FileSystemAccessRule( + (New-Object Security.Principal.SecurityIdentifier($sid)), + [Security.AccessControl.FileSystemRights]::FullControl, + ([Security.AccessControl.InheritanceFlags]::ContainerInherit -bor [Security.AccessControl.InheritanceFlags]::ObjectInherit), + [Security.AccessControl.PropagationFlags]::None,[Security.AccessControl.AccessControlType]::Allow) + $acl.AddAccessRule($rule) + } + Set-Acl -LiteralPath $Path -AclObject $acl + No-Reparse $Path +} +function Write-New([string]$Path,[string]$Value) { + $stream=[IO.File]::Open($Path,[IO.FileMode]::CreateNew,[IO.FileAccess]::Write,[IO.FileShare]::None) + try {$bytes=(New-Object Text.UTF8Encoding($false)).GetBytes($Value);$stream.Write($bytes,0,$bytes.Length);$stream.Flush($true)} + finally {$stream.Dispose()} +} +function Receipt([string]$Name) {Write-New (Join-Path $case $Name) ($report | ConvertTo-Json -Depth 50)} +function Set-BuildEnvironment([string]$Name,[string]$Value) { + if (-not $savedEnvironment.ContainsKey($Name)) {$savedEnvironment[$Name]=[Environment]::GetEnvironmentVariable($Name,'Process')} + [Environment]::SetEnvironmentVariable($Name,$Value,'Process') +} +function Microsoft-Tool([string]$Path) { + No-Reparse $Path + $sig=Get-AuthenticodeSignature -LiteralPath $Path + Require ($sig.Status -eq 'Valid' -and $null -ne $sig.SignerCertificate -and + $sig.SignerCertificate.Subject -match 'Microsoft Corporation') 'MICROSOFT_TOOL_SIGNATURE_INVALID' + return [ordered]@{path=$Path;sha256=(Hash $Path);signature_status=[string]$sig.Status; + signer_thumbprint=$sig.SignerCertificate.Thumbprint;file_version=(Get-Item -LiteralPath $Path).VersionInfo.FileVersion} +} +function Source-Files([string]$Root) { + $files=@(Get-Item -LiteralPath (Join-Path $Root 'Cargo.toml'),(Join-Path $Root 'Cargo.lock'),(Join-Path $Root 'rust-toolchain.toml')) + foreach ($name in @('crates','proto')) { + $files+=Get-ChildItem -LiteralPath (Join-Path $Root $name) -Recurse -File -Force | + Where-Object {$_.Extension -in @('.rs','.toml','.c','.h','.idl','.proto')} + } + $files+=Get-Item -LiteralPath (Join-Path $Root 'deploy/windows/msvc/clock_abi_probe.c'),(Join-Path $Root 'deploy/windows/Build-NativeMsvc.ps1') + $result=[ordered]@{} + foreach ($item in $files | Sort-Object FullName -Unique) { + No-Reparse $item.FullName + $relative=$item.FullName.Substring($Root.Length+1).Replace('\','/') + Require ($relative -notmatch '(^|/)(credentials|devstate|target|\.git|node_modules)(/|$)') 'NON_SOURCE_BUILD_INPUT' + $result[$relative]=Hash $item.FullName + } + return $result +} +function Same-Map($Expected,$Actual) { + $keys=@($Expected.PSObject.Properties | ForEach-Object {$_.Name}) + if ($Expected -is [Collections.IDictionary]) {$keys=@($Expected.Keys)} + Require ($keys.Count -eq $Actual.Count) 'SOURCE_OR_TOOL_COUNT_CHANGED' + foreach ($key in $keys) { + $old=if ($Expected -is [Collections.IDictionary]) {$Expected[$key]} else {$Expected.$key} + Require ($Actual.Contains($key) -and $old -eq $Actual[$key]) 'SOURCE_OR_TOOL_HASH_CHANGED' + } +} +function Quoted([string]$Value) { + Require ($Value -notmatch '["\x00\r\n]') 'NATIVE_ARGUMENT_ENCODING' + return '"'+($Value -replace '(\\+)$','$1$1')+'"' +} +function Invoke-BuildTool([string]$Exe,[string[]]$Arguments,[string]$Label,[int]$Seconds=1800,[bool]$AllowFailure=$false) { + $out=Join-Path $case ($Label+'.stdout');$err=Join-Path $case ($Label+'.stderr') + $quoted=@($Arguments | ForEach-Object {Quoted $_}) -join ' ' + $process=Start-Process -FilePath $Exe -ArgumentList $quoted -WorkingDirectory $case -PassThru -WindowStyle Hidden -RedirectStandardOutput $out -RedirectStandardError $err + Write-New (Join-Path $case ($Label+'.started.json')) (@{pid=$process.Id;executable_sha256=(Hash $Exe);label=$Label} | ConvertTo-Json) + if (-not $process.WaitForExit($Seconds*1000)) { + # A compiler/build-script timeout is retained for inspection. No blind + # retry, process-tree kill, service action or installation rollback. + throw 'NATIVE_BUILD_TIMEOUT_PROCESS_STATE_RETAINED' + } + $process.Refresh() + Write-New (Join-Path $case ($Label+'.result.json')) (@{exit_code=$process.ExitCode;stdout_sha256=(Hash $out);stderr_sha256=(Hash $err)} | ConvertTo-Json) + if (-not $AllowFailure) {Require ($process.ExitCode -eq 0) ('BUILD_TOOL_FAILED_'+$Label)} + return $out +} + +try { + Require ($env:OS -eq 'Windows_NT' -and [Environment]::Is64BitProcess -and [Environment]::Is64BitOperatingSystem) 'NATIVE_WINDOWS_AMD64_REQUIRED' + Require ($SourceRoot -match '^[A-Za-z]:\\' -and $EvidenceRoot -match '^[A-Za-z]:\\') 'LOCAL_BUILD_DRIVE_REQUIRED' + $SourceRoot=(Get-Item -LiteralPath $SourceRoot).FullName.TrimEnd('\');No-Reparse $SourceRoot + Require ($ScriptFile -eq (Join-Path $SourceRoot 'deploy/windows/Build-NativeMsvc.ps1')) 'RUNNER_FROM_EXACT_SOURCE_BUNDLE_REQUIRED' + Require ($SourceRoot -notmatch '(?i)\\ProgramData\\RustDiscovery|\\Program Files\\RustDiscovery') 'NO_CLIENT_STATE_AS_SOURCE' + if (-not (Test-Path -LiteralPath $EvidenceRoot)) {[void][IO.Directory]::CreateDirectory($EvidenceRoot)} + $EvidenceRoot=(Get-Item -LiteralPath $EvidenceRoot).FullName.TrimEnd('\');No-Reparse $EvidenceRoot + Protected-BuildRoot $EvidenceRoot + Require (-not $EvidenceRoot.StartsWith($SourceRoot+'\',[StringComparison]::OrdinalIgnoreCase) -and + $EvidenceRoot -notmatch '(?i)\\ProgramData\\RustDiscovery|\\Program Files\\RustDiscovery') 'BUILD_EVIDENCE_OUTSIDE_SOURCE_AND_RUNTIME' + $case=Join-Path $EvidenceRoot $CaseId.ToString();New-PrivateDirectory $case + $report.source_root=$SourceRoot;$report.source_files=Source-Files $SourceRoot + Require ([IO.File]::ReadAllText((Join-Path $SourceRoot 'crates/agent-windows/src/clock.rs')) -match + '(?m)^pub const ADAPTER_QUALIFIED: bool = false;\r?$') 'CLOCK_GATE_MUST_REMAIN_CLOSED' + $report.runner_sha256=Hash $ScriptFile + $drive=New-Object IO.DriveInfo([IO.Path]::GetPathRoot($case));$report.free_bytes=$drive.AvailableFreeSpace + $missing=New-Object Collections.Generic.List[string] + if ($drive.AvailableFreeSpace -lt $MinimumFreeGiB*1GB) {$missing.Add('FREE_DISK_BELOW_BOUND')} + foreach ($name in @('CL','_CL_','LINK','_LINK_','RUSTFLAGS','CARGO_ENCODED_RUSTFLAGS','RUSTC_WRAPPER','RUSTC_WORKSPACE_WRAPPER')) { + Require ([string]::IsNullOrEmpty([Environment]::GetEnvironmentVariable($name,'Process'))) 'UNEXPECTED_BUILD_FLAG_OR_WRAPPER' + } + foreach ($name in [Environment]::GetEnvironmentVariables('Process').Keys) { + if ($name -match '^(CFLAGS|CXXFLAGS|CPPFLAGS|LDFLAGS)(_|$)|^CARGO_(BUILD|TARGET_.+)_(RUSTFLAGS|RUSTC_WRAPPER|RUNNER)$') { + Require ([string]::IsNullOrEmpty([Environment]::GetEnvironmentVariable($name,'Process'))) 'UNEXPECTED_NATIVE_BUILD_OVERRIDE' + } + } + $tools=[ordered]@{};$hashes=[ordered]@{} + $tools.cmd=Microsoft-Tool (Join-Path ([Environment]::SystemDirectory) 'cmd.exe') + $vswhere=Join-Path ${env:ProgramFiles(x86)} 'Microsoft Visual Studio/Installer/vswhere.exe' + if (-not (Test-Path -LiteralPath $vswhere)) {$missing.Add('VISUAL_STUDIO_BUILD_TOOLS')} + else { + $tools.vswhere=Microsoft-Tool $vswhere + $instances=@(& $vswhere -products '*' -version '[17.14,19.0)' -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -latest -format json | ConvertFrom-Json) + Require ($LASTEXITCODE -eq 0) 'VSWHERE_QUERY_FAILED' + if ($instances.Count -ne 1) {$missing.Add('MSVC_AMD64_WORKLOAD')} + else { + $instance=$instances[0];$report.visual_studio_version=$instance.installationVersion + $batch=Join-Path $instance.installationPath 'VC/Auxiliary/Build/vcvarsall.bat';No-Reparse $batch + Require ($batch -notmatch '[&|<>^%"!\r\n]') 'DEVELOPER_ENVIRONMENT_PATH' + $report.vcvarsall_sha256=Hash $batch + $environment=@(& $tools.cmd.path /d /s /c ('""'+$batch+'" amd64 '+$SdkVersion+' >nul && set"')) + Require ($LASTEXITCODE -eq 0) 'MSVC_SDK_ENVIRONMENT_UNAVAILABLE' + $allowed=@('PATH','INCLUDE','LIB','LIBPATH','WindowsSdkDir','WindowsSDKVersion','VCToolsInstallDir','VSCMD_ARG_TGT_ARCH') + foreach ($line in $environment) {if ($line -match '^([^=]+)=(.*)$' -and $Matches[1] -in $allowed) {Set-BuildEnvironment $Matches[1] $Matches[2]}} + Require ($env:WindowsSDKVersion.TrimEnd('\') -eq $SdkVersion -and $env:VSCMD_ARG_TGT_ARCH -eq 'x64') 'EXACT_AMD64_SDK_ENVIRONMENT' + foreach ($name in @('cl','link','lib','dumpbin')) { + $path=Join-Path $env:VCToolsInstallDir ('bin/Hostx64/x64/'+$name+'.exe');$tools[$name]=Microsoft-Tool $path + } + $midl=Join-Path $env:WindowsSdkDir ('bin/'+$SdkVersion+'/x64/midl.exe');$tools.midl=Microsoft-Tool $midl + $report.sdk_version=$SdkVersion + } + } + $rustup=if ([string]::IsNullOrEmpty($RustupExe)) {Get-Command rustup.exe -CommandType Application -ErrorAction SilentlyContinue} + else {Require (-not [string]::IsNullOrEmpty($RustupHome) -and -not [string]::IsNullOrEmpty($CargoHome)) 'ISOLATED_RUST_HOMES_REQUIRED'; + No-Reparse $RustupExe;Protected-BuildRoot $RustupHome;Protected-BuildRoot $CargoHome; + Set-BuildEnvironment 'RUSTUP_HOME' $RustupHome;Set-BuildEnvironment 'CARGO_HOME' $CargoHome;@{Source=(Get-Item -LiteralPath $RustupExe).FullName}} + $report.rustup_home=[Environment]::GetEnvironmentVariable('RUSTUP_HOME','Process') + $report.cargo_home=[Environment]::GetEnvironmentVariable('CARGO_HOME','Process') + if ($null -eq $rustup) {$missing.Add('RUSTUP_AND_PINNED_MSVC_TOOLCHAIN')} + else { + No-Reparse $rustup.Source;$tools.rustup=@{path=$rustup.Source;sha256=(Hash $rustup.Source)} + foreach ($name in @('rustc','cargo','rustdoc','clippy-driver','rustfmt')) { + $label='rustup-which-'+$name + $output=Invoke-BuildTool $rustup.Source @('which','--toolchain',$Toolchain,$name) $label 30 $true + $query=Get-Content -LiteralPath (Join-Path $case ($label+'.result.json')) -Raw | ConvertFrom-Json + $path=@(Get-Content -LiteralPath $output) + if ($query.exit_code -ne 0 -or $path.Count -ne 1 -or -not (Test-Path -LiteralPath $path[0])) {$missing.Add('PINNED_RUST_TOOL_'+$name);continue} + No-Reparse $path[0];$tools[$name]=@{path=$path[0];sha256=(Hash $path[0])} + } + if ($tools.Contains('rustc')) { + $version=@(Get-Content -LiteralPath (Invoke-BuildTool $tools.rustc.path @('-vV') 'rust-version' 30)) + Require ('release: 1.99.1' -in $version -and 'host: x86_64-pc-windows-msvc' -in $version) 'PINNED_NATIVE_RUST_VERSION' + $report.rust_version=$version + } + } + foreach ($name in $tools.Keys) {$hashes[$name]=$tools[$name].sha256} + $report.tools=$tools;$report.tool_hashes=$hashes;$report.missing_tools=@($missing);$report.ready=($missing.Count -eq 0) + $report.stage='preflight';Receipt 'preflight.json' + if ($Mode -eq 'Preflight') {Write-Output ('PREFLIGHT ready='+$report.ready+' case='+$CaseId);exit $(if ($report.ready) {0} else {3})} + Require $report.ready 'NATIVE_BUILD_PREREQUISITES_MISSING' + Require (-not [string]::IsNullOrEmpty($ExpectedPreflight)) 'EXPLICIT_PREFLIGHT_RECEIPT_REQUIRED' + No-Reparse $ExpectedPreflight;$expected=Get-Content -LiteralPath $ExpectedPreflight -Raw | ConvertFrom-Json + Require ($expected.ready -eq $true -and $expected.target -eq $Target -and $expected.toolchain -eq $Toolchain -and + $expected.runner_sha256 -eq $report.runner_sha256 -and $expected.rustup_home -eq $report.rustup_home -and + $expected.cargo_home -eq $report.cargo_home) 'EXPECTED_PREFLIGHT_BINDING' + Same-Map $expected.source_files $report.source_files;Same-Map $expected.tool_hashes $hashes + $report.expected_preflight_sha256=Hash $ExpectedPreflight + $snapshot=Join-Path $case 'source';[void][IO.Directory]::CreateDirectory($snapshot) + foreach ($relative in $report.source_files.Keys) { + $destination=Join-Path $snapshot $relative;[void][IO.Directory]::CreateDirectory([IO.Path]::GetDirectoryName($destination)) + [IO.File]::Copy((Join-Path $SourceRoot $relative),$destination,$false) + Require ((Hash $destination) -eq $report.source_files[$relative]) 'IMMUTABLE_SOURCE_COPY_CHANGED' + } + Same-Map $expected.source_files (Source-Files $SourceRoot) + Set-BuildEnvironment 'RUSTUP_TOOLCHAIN' $Toolchain;Set-BuildEnvironment 'RUSTC' $tools.rustc.path;Set-BuildEnvironment 'RUSTDOC' $tools.rustdoc.path + Set-BuildEnvironment 'CARGO_TARGET_DIR' (Join-Path $case 'target');Set-BuildEnvironment 'RMM_MIDL_EXE' $tools.midl.path + Set-BuildEnvironment 'CC_x86_64_pc_windows_msvc' $tools.cl.path;Set-BuildEnvironment 'AR_x86_64_pc_windows_msvc' $tools.lib.path + Set-BuildEnvironment 'CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_LINKER' $tools.link.path + $abi=Join-Path $case 'abi';[void][IO.Directory]::CreateDirectory($abi) + [void](Invoke-BuildTool $tools.midl.path @('/nologo','/env','amd64','/Oicf','/robust','/Zp8','/client','stub','/server','none','/h','w32time.h','/cstub','w32time_c.c','/out',$abi,(Join-Path $snapshot 'crates/agent-windows/native/w32time.idl')) 'midl' 60) + $probe=Join-Path $abi 'clock_abi_probe.exe' + [void](Invoke-BuildTool $tools.cl.path @('/nologo','/std:c11','/Zp8','/W4','/WX','/TC',('/I'+$abi),(Join-Path $snapshot 'deploy/windows/msvc/clock_abi_probe.c'),('/Fo'+(Join-Path $abi 'clock_abi_probe.obj')),('/Fe'+$probe),'/link','rpcrt4.lib') 'abi-compile' 60) + $abiOutput=Invoke-BuildTool $probe @() 'abi-seh' 20 + $abiResult=Get-Content -LiteralPath $abiOutput -Raw | ConvertFrom-Json + Require ($abiResult.amd64 -eq $true -and $abiResult.numeric_size -eq 64 -and $abiResult.wire_size -eq 120 -and + $abiResult.seh_caught -eq 1722 -and $abiResult.network_called -eq $false) 'NATIVE_MIDL_ABI_OR_SEH_FAILED' + $report.abi_seh_verified=$true;$report.abi_probe_executed=$true;$report.abi_probe_sha256=Hash $probe + $report.generated_rpc=@{header_sha256=(Hash (Join-Path $abi 'w32time.h'));client_sha256=(Hash (Join-Path $abi 'w32time_c.c'))} + $manifest=Join-Path $snapshot 'Cargo.toml' + [void](Invoke-BuildTool $tools.cargo.path @('clippy','--manifest-path',$manifest,'--locked','--offline','--target',$Target,'-p','rmm-agent-windows','--features','system-helper','--all-targets','--','-D','warnings') 'clippy') + [void](Invoke-BuildTool $tools.cargo.path @('build','--manifest-path',$manifest,'--locked','--offline','--target',$Target,'-p','rmm-agent-windows','--features','system-helper','--bins','--examples','--release') 'release') + [void](Invoke-BuildTool $tools.cargo.path @('test','--manifest-path',$manifest,'--locked','--offline','--target',$Target,'-p','rmm-agent-windows','--features','system-helper','--lib','--no-run','--message-format=json') 'windows-unit-link') + [void](Invoke-BuildTool $tools.cargo.path @('test','--manifest-path',$manifest,'--locked','--offline','--target',$Target,'-p','rmm-agent-core','--tests','--no-run','--message-format=json') 'core-unit-link') + $artifacts=[ordered]@{} + foreach ($name in @('rmm-agent-windows','rmm-helper-windows')) { + $path=Join-Path $env:CARGO_TARGET_DIR ($Target+'/release/'+$name+'.exe') + $pe=[IO.File]::ReadAllBytes($path);$offset=[BitConverter]::ToInt32($pe,0x3c) + Require ($pe.Length -gt ($offset+24) -and [BitConverter]::ToUInt16($pe,0) -eq 0x5a4d -and [BitConverter]::ToUInt32($pe,$offset) -eq 0x4550 -and + [BitConverter]::ToUInt16($pe,$offset+4) -eq 0x8664) 'NATIVE_AMD64_PE_REQUIRED' + $dependencies=Invoke-BuildTool $tools.dumpbin.path @('/nologo','/dependents',$path) ($name+'-dependents') 30 + $artifacts[$name]=@{path=$path;sha256=(Hash $path);size_bytes=$pe.Length;authenticode_status=[string](Get-AuthenticodeSignature -LiteralPath $path).Status; + dependents_sha256=(Hash $dependencies)} + } + Same-Map $expected.source_files (Source-Files $SourceRoot) + foreach ($name in $tools.Keys) {Require ((Hash $tools[$name].path) -eq $hashes[$name]) 'BUILD_TOOL_CHANGED_DURING_BUILD'} + $report.artifacts=$artifacts;$report.source_before_after_equal=$true;$report.completed=$true;$report.stage='built_not_deployed';Receipt 'result.json' + Write-Output ('PASS native MSVC source build '+$CaseId+'; no runtime installation, clock qualification or RMM job') +} catch { + $report.stage='incomplete';$report.error_class=$_.Exception.GetType().Name + $report.error_code=if ($_.Exception.Message -match '^[A-Z0-9_]{1,96}$') {$_.Exception.Message} else {'BUILD_SETUP_OR_TOOL_FAILED'} + if ($null -ne $case) {try {Receipt ('error-'+[Guid]::NewGuid().ToString()+'.json')} catch {}} + Write-Output ('NATIVE_MSVC_INCOMPLETE case='+$CaseId+'; private receipt retained; no automatic retry') + exit 2 +} finally { + foreach ($name in $savedEnvironment.Keys) {[Environment]::SetEnvironmentVariable($name,$savedEnvironment[$name],'Process')} +} diff --git a/deploy/windows/Install-NativeBuildTools.ps1 b/deploy/windows/Install-NativeBuildTools.ps1 new file mode 100644 index 0000000..a489a9e --- /dev/null +++ b/deploy/windows/Install-NativeBuildTools.ps1 @@ -0,0 +1,281 @@ +#requires -Version 5.1 +<#! +Explicit development-tool bootstrap; no RMM jobs, daemon changes, OS update +command, trust import, persistent execution-policy/PATH modification or reboot. +The already authorized development bootstrap uses process RemoteSigned only; +Machine/User/GPO scopes are never changed and GPOs retain precedence. +Prepare downloads pinned public inputs only. InstallVs/InstallRust each start +one fixed demand-only task; Read never retries. Worker result is durable before +the task exits. A lost reply, timeout, 3010 or failure never restarts installation. +!#> +[CmdletBinding()] +param( + [Parameter(Mandatory=$true)][ValidateSet('Prepare','InstallVs','InstallRust','Read','WorkerVs','WorkerRust')][string]$Phase, + [string]$RootDirectory=(Join-Path $env:USERPROFILE 'RmmNativeTools'), + [string]$CaseDirectory, + [Guid]$CaseId=([Guid]::NewGuid()), + [string]$ScriptFile=$PSCommandPath +) +Set-StrictMode -Version Latest +$ErrorActionPreference='Stop' +$VsVersion='18.11.12405.450' +$VsDirectory='C:\RmmBuildTools' +$Toolchain='1.99.1-x86_64-pc-windows-msvc' +$RustupSha256='38027d51d69a5d05493627a0634dbea52d6cd793c8f072d2373d3f4163eec7db' +$downloads=@( + @{name='vs_BuildTools.exe';sha256='386b5d9a3d6576eab3772c1ecaa7c107ded38146a9c483f41679c194ae1e63b4';max=16MB; + url='https://download.visualstudio.microsoft.com/download/pr/2d4073f5-2775-4a70-8c1d-b50ae62795a7/386b5d9a3d6576eab3772c1ecaa7c107ded38146a9c483f41679c194ae1e63b4/vs_BuildTools.exe'}, + @{name='rustup-init.exe';sha256=$RustupSha256;max=32MB; + url='https://static.rust-lang.org/rustup/archive/1.29.3/x86_64-pc-windows-msvc/rustup-init.exe'}, + @{name='rustup-init.exe.sha256';sha256=$null;max=4096; + url='https://static.rust-lang.org/rustup/archive/1.29.3/x86_64-pc-windows-msvc/rustup-init.exe.sha256'}, + @{name='rust-toolchain.toml.sha256';sha256=$null;max=4096; + url='https://static.rust-lang.org/dist/channel-rust-1.99.1.toml.sha256'} +) +$case=$null +function Require([bool]$Value,[string]$Code) {if (-not $Value) {throw $Code}} +function Hash([string]$Path) {return (Get-FileHash -LiteralPath $Path -Algorithm SHA256).Hash.ToLowerInvariant()} +function Hash-Text([string]$Value) { + $sha=[Security.Cryptography.SHA256]::Create() + try {return ([BitConverter]::ToString($sha.ComputeHash([Text.Encoding]::UTF8.GetBytes($Value)))).Replace('-','').ToLowerInvariant()} + finally {$sha.Dispose()} +} +function No-Reparse([string]$Path) { + $item=Get-Item -LiteralPath $Path -Force + while ($null -ne $item) { + Require (($item.Attributes -band [IO.FileAttributes]::ReparsePoint) -eq 0) 'SETUP_REPARSE_PATH' + if ($item -is [IO.FileInfo]) {$item=$item.Directory} else {$item=$item.Parent} + } +} +function Protected-Directory([string]$Path) { + No-Reparse $Path;$acl=Get-Acl -LiteralPath $Path + $trusted=@([Security.Principal.WindowsIdentity]::GetCurrent().User.Value,'S-1-5-18','S-1-5-32-544') + Require ($acl.GetOwner([Security.Principal.SecurityIdentifier]).Value -in $trusted) 'SETUP_DIRECTORY_OWNER' + $write=[Security.AccessControl.FileSystemRights]::Write -bor [Security.AccessControl.FileSystemRights]::Delete -bor + [Security.AccessControl.FileSystemRights]::ChangePermissions -bor [Security.AccessControl.FileSystemRights]::TakeOwnership -bor + [Security.AccessControl.FileSystemRights]::DeleteSubdirectoriesAndFiles + foreach ($ace in $acl.Access) { + if ($ace.AccessControlType -eq 'Allow' -and ($ace.FileSystemRights -band $write) -ne 0) { + Require ($ace.IdentityReference.Translate([Security.Principal.SecurityIdentifier]).Value -in $trusted) 'UNTRUSTED_SETUP_DIRECTORY_WRITER' + } + } +} +function New-Private([string]$Path) { + Require (-not (Test-Path -LiteralPath $Path)) 'SETUP_CASE_ALREADY_EXISTS' + [void][IO.Directory]::CreateDirectory($Path) + $acl=New-Object Security.AccessControl.DirectorySecurity;$acl.SetAccessRuleProtection($true,$false) + $acl.SetOwner((New-Object Security.Principal.SecurityIdentifier('S-1-5-32-544'))) + foreach ($sid in @('S-1-5-18','S-1-5-32-544')) { + $acl.AddAccessRule((New-Object Security.AccessControl.FileSystemAccessRule( + (New-Object Security.Principal.SecurityIdentifier($sid)),[Security.AccessControl.FileSystemRights]::FullControl, + ([Security.AccessControl.InheritanceFlags]::ContainerInherit -bor [Security.AccessControl.InheritanceFlags]::ObjectInherit), + [Security.AccessControl.PropagationFlags]::None,[Security.AccessControl.AccessControlType]::Allow))) + } + Set-Acl -LiteralPath $Path -AclObject $acl;Protected-Directory $Path +} +function Write-New([string]$Path,$Value) { + $raw=(New-Object Text.UTF8Encoding($false)).GetBytes(($Value | ConvertTo-Json -Depth 30)) + $stream=[IO.File]::Open($Path,[IO.FileMode]::CreateNew,[IO.FileAccess]::Write,[IO.FileShare]::None) + try {$stream.Write($raw,0,$raw.Length);$stream.Flush($true)} finally {$stream.Dispose()} +} +function Read-Json([string]$Name) {No-Reparse (Join-Path $case $Name);return Get-Content -LiteralPath (Join-Path $case $Name) -Raw | ConvertFrom-Json} +function Microsoft-Signature([string]$Path) { + No-Reparse $Path;$sig=Get-AuthenticodeSignature -LiteralPath $Path + Require ($sig.Status -eq 'Valid' -and $null -ne $sig.SignerCertificate -and $sig.SignerCertificate.Subject -match 'Microsoft Corporation') 'SETUP_MICROSOFT_SIGNATURE' + return @{status=[string]$sig.Status;thumbprint=$sig.SignerCertificate.Thumbprint;sha256=(Hash $Path)} +} +function Download($Item) { + $uri=[Uri]$Item.url;$redirects=0 + while ($true) { + Require ($uri.Scheme -eq 'https' -and $uri.Host -in @('download.visualstudio.microsoft.com','static.rust-lang.org')) 'SETUP_DOWNLOAD_ORIGIN' + $request=[Net.HttpWebRequest]::Create($uri);$request.AllowAutoRedirect=$false;$request.UseDefaultCredentials=$false + $request.Timeout=30000;$request.ReadWriteTimeout=30000 + $response=$request.GetResponse() + try { + if ([int]$response.StatusCode -in @(301,302,303,307,308)) { + Require ($redirects -lt 3) 'SETUP_DOWNLOAD_REDIRECT_LIMIT';$redirects++ + $uri=New-Object Uri($uri,$response.Headers['Location']);continue + } + Require ([int]$response.StatusCode -eq 200) 'SETUP_DOWNLOAD_HTTP_STATUS' + $stream=[IO.File]::Open((Join-Path $case $Item.name),[IO.FileMode]::CreateNew,[IO.FileAccess]::Write,[IO.FileShare]::None) + $inputStream=$response.GetResponseStream() + try { + $buffer=New-Object byte[] 65536;$size=0 + while (($count=$inputStream.Read($buffer,0,$buffer.Length)) -gt 0) {$size+=$count;Require ($size -le $Item.max) 'SETUP_DOWNLOAD_SIZE';$stream.Write($buffer,0,$count)} + $stream.Flush($true) + } finally {$inputStream.Dispose();$stream.Dispose()} + Require ($null -eq $Item.sha256 -or (Hash (Join-Path $case $Item.name)) -eq $Item.sha256) 'SETUP_DOWNLOAD_HASH' + return @{name=$Item.name;url=$Item.url;final_url=$uri.AbsoluteUri;sha256=(Hash (Join-Path $case $Item.name));bytes=$size} + } finally {$response.Dispose()} + } +} +function Facts { + $os=Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' + $policies=@(Get-ExecutionPolicy -List | Sort-Object Scope | ForEach-Object {$_.Scope.ToString()+':'+$_.ExecutionPolicy.ToString()}) + $services=@{} + foreach ($name in @('W32Time','RmmDiscoveryWindows','RmmDiscoverySystemHelper')) { + $s=Get-CimInstance Win32_Service -Filter ("Name='"+$name+"'") + $services[$name]=if ($null -eq $s) {$null} else {@{state=$s.State;start_mode=$s.StartMode;pid=$s.ProcessId}} + } + $roots=@(Get-ChildItem Cert:\LocalMachine\Root | ForEach-Object {$_.Thumbprint} | Sort-Object) + $publishers=@(Get-ChildItem Cert:\LocalMachine\TrustedPublisher | ForEach-Object {$_.Thumbprint} | Sort-Object) + return @{boot_ticks=(Get-CimInstance Win32_OperatingSystem).LastBootUpTime.ToUniversalTime().Ticks; + os_build=[string]$os.CurrentBuildNumber;os_ubr=[int]$os.UBR;policies=$policies;services=$services; + root_trust_sha256=(Hash-Text ($roots -join ','));publisher_trust_sha256=(Hash-Text ($publishers -join ',')); + machine_path_sha256=(Hash-Text ([Environment]::GetEnvironmentVariable('Path','Machine'))); + reboot_pending=((Test-Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending') -or + (Test-Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired'))} +} +function Stable-Facts($Before,$After) { + foreach ($key in @('boot_ticks','os_build','os_ubr','root_trust_sha256','publisher_trust_sha256','machine_path_sha256')) {Require ($Before.$key -eq $After.$key) ('SETUP_BOUNDARY_CHANGED_'+$key.ToUpperInvariant())} + Require (($Before.policies -join '|') -eq ($After.policies -join '|')) 'SETUP_EXECUTION_POLICY_CHANGED' + foreach ($name in @('W32Time','RmmDiscoveryWindows','RmmDiscoverySystemHelper')) { + $old=$Before.services.$name;$new=$After.services.$name + Require (($null -eq $old) -eq ($null -eq $new)) 'SETUP_SERVICE_PRESENCE_CHANGED' + if ($null -ne $old) {Require ($old.state -eq $new.state -and $old.start_mode -eq $new.start_mode -and $old.pid -eq $new.pid) 'SETUP_SERVICE_CHANGED'} + } +} +function Validate-Inputs($Prepared) { + Require ((Hash $ScriptFile) -eq $Prepared.script_sha256 -and $Prepared.vs_version -eq $VsVersion -and $Prepared.toolchain -eq $Toolchain) 'SETUP_SOURCE_PLAN_CHANGED' + foreach ($item in $Prepared.downloads) {Require ((Hash (Join-Path $case $item.name)) -eq $item.sha256) 'PREPARED_INSTALLER_CHANGED'} + $sig=Microsoft-Signature (Join-Path $case 'vs_BuildTools.exe') + Require ($sig.thumbprint -eq $Prepared.vs_signer.thumbprint) 'PREPARED_SIGNER_CHANGED' +} +function Verify-Vs { + $where=Join-Path ${env:ProgramFiles(x86)} 'Microsoft Visual Studio/Installer/vswhere.exe' + [void](Microsoft-Signature $where) + $instances=@(& $where -products Microsoft.VisualStudio.Product.BuildTools -version '[18.11,18.12)' -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -format json | ConvertFrom-Json) + Require ($LASTEXITCODE -eq 0 -and $instances.Count -eq 1 -and $instances[0].installationVersion -eq $VsVersion -and $instances[0].installationPath -eq $VsDirectory) 'INSTALLED_VS_VERSION_OR_TARGET' + $sdk=Join-Path ${env:ProgramFiles(x86)} 'Windows Kits/10/bin/10.0.26100.0/x64/midl.exe' + return @{version=$instances[0].installationVersion;path=$VsDirectory;sdk_midl=(Microsoft-Signature $sdk)} +} +function Worker([string]$Kind,$Prepared) { + Require ([Security.Principal.WindowsIdentity]::GetCurrent().User.Value -eq 'S-1-5-18') 'SETUP_FIXED_WORKER_SYSTEM_ACCOUNT' + $intent=Read-Json ($Kind+'-start-intent.json') + Require ($intent.prepared_sha256 -eq (Hash (Join-Path $case 'prepared.json'))) 'SETUP_FIXED_ONCE_INTENT' + Write-New (Join-Path $case ($Kind+'-worker-once.json')) @{kind=$Kind;pid=$PID;prepared_sha256=$intent.prepared_sha256} + $result=[ordered]@{kind=$Kind;completed=$false;reboot_required=$false;stage='worker_entered';rmm_jobs_created=$false;reboot_requested=$false} + $saved=@{} + try { + Validate-Inputs $Prepared;$before=Facts;Stable-Facts $Prepared.before $before + Require (-not $before.reboot_pending) 'SETUP_PREEXISTING_REBOOT_PENDING' + if ($Kind -eq 'vs') { + Require (-not (Test-Path -LiteralPath $VsDirectory)) 'EXISTING_VS_TARGET_NO_RETRY' + $exe=Join-Path $case 'vs_BuildTools.exe' + # Standard Microsoft acquisition/verification only. No custom + # catalog, channel, checksum override or signature-bypass option. + $args=@('--wait','--quiet','--norestart','--noUpdateInstaller','--installPath',$VsDirectory, + '--add','Microsoft.VisualStudio.Workload.VCTools','--add','Microsoft.VisualStudio.Component.VC.Tools.x86.x64', + '--add','Microsoft.VisualStudio.Component.Windows11SDK.26100') + } else { + $vs=Read-Json 'vs-result.json';Require ($vs.completed -eq $true -and $vs.reboot_required -eq $false) 'VS_MUST_COMPLETE_WITHOUT_REBOOT_FIRST' + [void](Verify-Vs) + foreach ($name in @('RUSTUP_HOME','CARGO_HOME')) { + $saved[$name]=[Environment]::GetEnvironmentVariable($name,'Process') + $path=Join-Path $case $name.ToLowerInvariant();Require (-not (Test-Path -LiteralPath $path)) 'EXISTING_RUST_HOME_NO_RETRY' + [Environment]::SetEnvironmentVariable($name,$path,'Process') + } + foreach ($pair in @(@('RUSTUP_DIST_SERVER','https://static.rust-lang.org'),@('RUSTUP_UPDATE_ROOT','https://static.rust-lang.org/rustup'))) { + $saved[$pair[0]]=[Environment]::GetEnvironmentVariable($pair[0],'Process') + [Environment]::SetEnvironmentVariable($pair[0],$pair[1],'Process') + } + $exe=Join-Path $case 'rustup-init.exe' + $args=@('-y','--no-modify-path','--no-update-default-toolchain','--profile','minimal', + '--default-host','x86_64-pc-windows-msvc','--default-toolchain','1.99.1','--component','clippy,rustfmt') + } + $arguments=@($args | ForEach-Object {Require ($_ -notmatch '["\r\n\x00]') 'SETUP_ARGUMENT_ENCODING';'"'+($_ -replace '(\\+)$','$1$1')+'"'}) -join ' ' + $process=Start-Process -FilePath $exe -ArgumentList $arguments -PassThru -WindowStyle Hidden -RedirectStandardOutput (Join-Path $case ($Kind+'.stdout')) -RedirectStandardError (Join-Path $case ($Kind+'.stderr')) + Write-New (Join-Path $case ($Kind+'-installer-process.json')) @{pid=$process.Id;start_ticks=$process.StartTime.ToUniversalTime().Ticks;exe_sha256=(Hash $exe)} + Require ($process.WaitForExit(3600000)) 'SETUP_TIMEOUT_EFFECT_UNKNOWN_NO_RETRY';$process.Refresh();$result.exit_code=$process.ExitCode + $result.reboot_required=($process.ExitCode -in @(3010,1641)) + $after=Facts;$result.after=$after;$result.reboot_required=($result.reboot_required -or $after.reboot_pending);Stable-Facts $Prepared.before $after + if ($process.ExitCode -eq 3010 -or $after.reboot_pending) {$result.stage='blocked_reboot_required';$result.reboot_required=$true} + elseif ($process.ExitCode -eq 1641) {$result.stage='unexpected_reboot_initiated';$result.reboot_required=$true} + else { + Require ($process.ExitCode -eq 0) 'SETUP_INSTALLER_FAILED_NO_RETRY' + if ($Kind -eq 'vs') {$result.installed=Verify-Vs} + else { + $rustup=Join-Path $env:CARGO_HOME 'bin/rustup.exe';$rustc=@(& $rustup which --toolchain $Toolchain rustc) + Require ($LASTEXITCODE -eq 0 -and $rustc.Count -eq 1) 'PINNED_RUSTC_MISSING' + $version=@(& $rustc[0] -vV) + Require ($LASTEXITCODE -eq 0 -and 'release: 1.99.1' -in $version -and 'host: x86_64-pc-windows-msvc' -in $version) 'INSTALLED_RUST_VERSION_OR_HOST' + $result.installed=@{rustup_exe=$rustup;rustup_sha256=(Hash $rustup);rustc_sha256=(Hash $rustc[0]);rustup_home=$env:RUSTUP_HOME;cargo_home=$env:CARGO_HOME;version=$version} + } + $result.stage='installed_verified';$result.completed=$true + } + } catch { + $result.error_class=$_.Exception.GetType().Name;$result.error_code=if ($_.Exception.Message -match '^[A-Z0-9_]{1,96}$') {$_.Exception.Message} else {'SETUP_WORKER_FAILED'} + } finally { + foreach ($name in $saved.Keys) {[Environment]::SetEnvironmentVariable($name,$saved[$name],'Process')} + Write-New (Join-Path $case ($Kind+'-result.json')) $result + Disable-ScheduledTask -TaskName ('RmmNativeTools-'+$Kind+'-'+$Prepared.case_id) -ErrorAction SilentlyContinue | Out-Null + } +} +try { + Require ($env:OS -eq 'Windows_NT' -and [Environment]::Is64BitProcess) 'SETUP_NATIVE_WINDOWS_X64' + Require ((Get-ExecutionPolicy -Scope Process) -eq 'RemoteSigned') 'AUTHORIZED_BOOTSTRAP_PROCESS_REMOTESIGNED_REQUIRED' + $principal=New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent()) + Require ($principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) 'SETUP_ADMIN_REQUIRED' + Require (-not [string]::IsNullOrEmpty($ScriptFile)) 'EXPLICIT_PINNED_SCRIPT_FILE_REQUIRED' + $ScriptFile=(Get-Item -LiteralPath $ScriptFile).FullName;No-Reparse $ScriptFile + if ($Phase -eq 'Prepare') { + Require ($RootDirectory -match '^[A-Za-z]:\\') 'LOCAL_TOOL_ROOT_REQUIRED' + if (-not (Test-Path -LiteralPath $RootDirectory)) {[void][IO.Directory]::CreateDirectory($RootDirectory)} + Protected-Directory $RootDirectory;$case=Join-Path $RootDirectory $CaseId.ToString();New-Private $case + Require (-not (Test-Path -LiteralPath $VsDirectory)) 'EXISTING_VS_TARGET' + $before=Facts;Require (-not $before.reboot_pending) 'SETUP_PREEXISTING_REBOOT_PENDING' + $drive=New-Object IO.DriveInfo([IO.Path]::GetPathRoot($case));Require ($drive.AvailableFreeSpace -ge 30GB) 'SETUP_DISK_BELOW_30GIB' + $savedTls=[Net.ServicePointManager]::SecurityProtocol + try {[Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12;$downloaded=@($downloads | ForEach-Object {Download $_})} + finally {[Net.ServicePointManager]::SecurityProtocol=$savedTls} + $signer=Microsoft-Signature (Join-Path $case 'vs_BuildTools.exe') + $version=(Get-Item -LiteralPath (Join-Path $case 'vs_BuildTools.exe')).VersionInfo + Require ($version.ProductVersion -eq '18.11.4 LTSC' -and $version.FileVersion -eq $VsVersion) 'PINNED_VS_BOOTSTRAPPER_VERSION' + Require ((Get-Content -LiteralPath (Join-Path $case 'rustup-init.exe.sha256') -Raw).Trim().Split(' ')[0] -eq $RustupSha256) 'RUSTUP_OFFICIAL_CHECKSUM' + Require ((Get-Content -LiteralPath (Join-Path $case 'rust-toolchain.toml.sha256') -Raw).Trim().Split(' ')[0] -eq 'fb152be80975c194aa5fc684b2aec3a6e14e2b855efe2faa4e5f8ae6e590f463') 'RUST_TOOLCHAIN_DISTRIBUTION_PIN' + [IO.File]::Copy($ScriptFile,(Join-Path $case 'Install-NativeBuildTools.ps1'),$false) + $prepared=@{schema_version=1;case_id=$CaseId.ToString();script_sha256=(Hash $ScriptFile);before=$before;downloads=$downloaded; + vs_signer=$signer;vs_version=$VsVersion;toolchain=$Toolchain;free_bytes=$drive.AvailableFreeSpace;installers_executed=$false} + Write-New (Join-Path $case 'prepared.json') $prepared + Write-Output ('PREPARED '+$case+'; downloads verified, no installation');exit 0 + } + Require (-not [string]::IsNullOrEmpty($CaseDirectory)) 'EXPLICIT_RETAINED_CASE_REQUIRED' + $case=(Get-Item -LiteralPath $CaseDirectory).FullName;Protected-Directory $case;$prepared=Read-Json 'prepared.json' + Require (([Guid]$prepared.case_id).ToString() -eq (Split-Path $case -Leaf)) 'SAME_PREPARED_CASE' + Validate-Inputs $prepared + if ($Phase -in @('WorkerVs','WorkerRust')) {Worker $(if ($Phase -eq 'WorkerVs') {'vs'} else {'rust'}) $prepared;exit 0} + if ($Phase -eq 'Read') { + $results=@{} + foreach ($kind in @('vs','rust')) { + $name=$kind+'-result.json';$task=Get-ScheduledTask -TaskName ('RmmNativeTools-'+$kind+'-'+$prepared.case_id) -ErrorAction SilentlyContinue + $results[$kind]=@{result=$(if (Test-Path -LiteralPath (Join-Path $case $name)) {Read-Json $name} else {$null});task_state=$(if ($null -eq $task) {'absent'} else {[string]$task.State})} + } + $facts=Facts;$stable=$true;$boundaryError=$null + try {Stable-Facts $prepared.before $facts} catch {$stable=$false;$boundaryError='BOOTSTRAP_BOUNDARY_CHANGED'} + Write-Output (@{case_id=$prepared.case_id;results=$results;facts=$facts;read_only=$true;boundary_verified=$stable;boundary_error=$boundaryError} | ConvertTo-Json -Depth 30);exit 0 + } + $kind=if ($Phase -eq 'InstallVs') {'vs'} else {'rust'} + $before=Facts;Stable-Facts $prepared.before $before;Require (-not $before.reboot_pending) 'SETUP_REBOOT_PENDING' + if ($kind -eq 'rust') {$vs=Read-Json 'vs-result.json';Require ($vs.completed -eq $true -and $vs.reboot_required -eq $false) 'VS_FIRST_NO_REBOOT'} + $intentPath=Join-Path $case ($kind+'-start-intent.json') + Write-New $intentPath @{kind=$kind;prepared_sha256=(Hash (Join-Path $case 'prepared.json'));script_sha256=$prepared.script_sha256} + $taskName='RmmNativeTools-'+$kind+'-'+$prepared.case_id + Require ($null -eq (Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue)) 'EXISTING_TOOL_TASK_NO_RETRY' + $workerFile=Join-Path $case 'Install-NativeBuildTools.ps1';$quotedFile=$workerFile.Replace("'","''");$quotedCase=$case.Replace("'","''") + $workerPhase=if ($kind -eq 'vs') {'WorkerVs'} else {'WorkerRust'} + $command="`$p='$quotedFile';if((Get-FileHash -LiteralPath `$p -Algorithm SHA256).Hash.ToLowerInvariant() -ne '$($prepared.script_sha256)'){throw 'WORKER_HASH'};& `$p -Phase $workerPhase -CaseDirectory '$quotedCase'" + $encoded=[Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($command)) + $powershell=Join-Path ([Environment]::SystemDirectory) 'WindowsPowerShell/v1.0/powershell.exe';[void](Microsoft-Signature $powershell) + $action=New-ScheduledTaskAction -Execute $powershell -Argument ('-NoProfile -NonInteractive -ExecutionPolicy RemoteSigned -EncodedCommand '+$encoded) + $settings=New-ScheduledTaskSettingsSet -MultipleInstances IgnoreNew -RestartCount 0 -ExecutionTimeLimit (New-TimeSpan -Minutes 90) -DisallowHardTerminate -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries + $user=New-ScheduledTaskPrincipal -UserId 'SYSTEM' -LogonType ServiceAccount -RunLevel Highest + Register-ScheduledTask -TaskName $taskName -Action $action -Principal $user -Settings $settings | Out-Null + # Deliberately no trigger or automatic restart: exactly this one demand call. + Start-ScheduledTask -TaskName $taskName + Write-New (Join-Path $case ($kind+'-task-started.json')) @{task_name=$taskName;source_sha256=$prepared.script_sha256;demand_only=$true;automatic_retry=$false} + Write-Output ('STARTED '+$kind+'; read retained case, never repeat install') +} catch { + $errorCode=if ($_.Exception.Message -match '^[A-Z0-9_]{1,96}$') {$_.Exception.Message} else {'NATIVE_TOOL_BOOTSTRAP_FAILED'} + if ($null -ne $case) {try {Write-New (Join-Path $case ('bootstrap-error-'+[Guid]::NewGuid().ToString()+'.json')) @{phase=$Phase;error_class=$_.Exception.GetType().Name;error_code=$errorCode}} catch {}} + Write-Output ('NATIVE_TOOL_SETUP_INCOMPLETE '+$errorCode+'; retain case and readback, no automatic retry');exit 2 +} diff --git a/deploy/windows/client_bootstrap.py b/deploy/windows/client_bootstrap.py index f0bda0d..37345ed 100644 --- a/deploy/windows/client_bootstrap.py +++ b/deploy/windows/client_bootstrap.py @@ -45,7 +45,7 @@ def powershell(script, payload=None, timeout=60, remote_signed=False): UPLOAD = r''' $ErrorActionPreference='Stop' $p=([Console]::In.ReadLine() | ConvertFrom-Json) -if ($p.name -notmatch '^((rmm-agent-windows|windows-tests-(lib|bin|acl)|windows-core-(contracts|reconcile|helper-reconcile))\.exe|Install-PassiveClient\.ps1|PassiveStateProbe\.exe|WindowsTlsProbe\.exe|RuntimeStateProbe\.exe)$') {throw 'Artifact allowlist'} +if ($p.name -notmatch '^((rmm-agent-windows|windows-tests-(lib|bin|acl)|windows-core-(contracts|reconcile|helper-reconcile))\.exe|(Install-PassiveClient|Build-MsiFixture|Build-NativeMsvc|Install-NativeBuildTools)\.ps1|PassiveStateProbe\.exe|WindowsTlsProbe\.exe|RuntimeStateProbe\.exe)$') {throw 'Artifact allowlist'} $root='C:\ProgramData\RustDiscoveryBootstrap' if (!(Test-Path -LiteralPath $root)) { New-Item -ItemType Directory -Path $root | Out-Null @@ -68,7 +68,7 @@ if (Test-Path -LiteralPath $path) {throw 'Artifact already exists'} VERIFY_UPLOAD = r''' $ErrorActionPreference='Stop' $p=([Console]::In.ReadLine() | ConvertFrom-Json) -if ($p.sha256 -notmatch '^[0-9a-f]{64}$' -or $p.name -notmatch '^((rmm-agent-windows|windows-tests-(lib|bin|acl)|windows-core-(contracts|reconcile|helper-reconcile))\.exe|Install-PassiveClient\.ps1|PassiveStateProbe\.exe|WindowsTlsProbe\.exe|RuntimeStateProbe\.exe)$') {throw 'Artifact allowlist'} +if ($p.sha256 -notmatch '^[0-9a-f]{64}$' -or $p.name -notmatch '^((rmm-agent-windows|windows-tests-(lib|bin|acl)|windows-core-(contracts|reconcile|helper-reconcile))\.exe|(Install-PassiveClient|Build-MsiFixture|Build-NativeMsvc|Install-NativeBuildTools)\.ps1|PassiveStateProbe\.exe|WindowsTlsProbe\.exe|RuntimeStateProbe\.exe)$') {throw 'Artifact allowlist'} $path='C:\ProgramData\RustDiscoveryBootstrap\'+$p.sha256+'\'+$p.name $hash=(Get-FileHash -LiteralPath $path -Algorithm SHA256).Hash.ToLowerInvariant() if ($hash -cne $p.sha256) {throw 'Artifact hash'} diff --git a/deploy/windows/msvc/README.md b/deploy/windows/msvc/README.md new file mode 100644 index 0000000..f799bfe --- /dev/null +++ b/deploy/windows/msvc/README.md @@ -0,0 +1,222 @@ +# Native Windows MSVC build + +Prepared 2026-11-14; **not executed on the VM**. No compiler installation, +Windows update, restart, trust-store change, daemon replacement or RMM job was +performed by this preparation. Existing Cloud identities/journals are untouched. + +`Build-NativeMsvc.ps1` is a development build runner, not an installer. Run it in +64-bit Windows PowerShell5.1 from a source bundle containing the exact repository +sources and this script. `Preflight` inventories installed tools and records +private source/tool hashes. `Build` requires an explicitly selected successful +preflight receipt, creates a different private case, and builds an isolated +source copy. It never reuses a partial build case. + +## Required native tools + +- Rust `1.99.1-x86_64-pc-windows-msvc`, including `cargo`, `rustc`, `rustdoc`, + `clippy-driver`, `rustfmt`, supplied by the official rustup installation. +- Installed Visual Studio C++ tools in the selected range `[17.14,19.0)`; + VS2022 or VS2026 is selected by its signed `vswhere.exe`. The actual product, + compiler version, signer thumbprints and tool hashes are recorded. The current + official [VS release history](https://learn.microsoft.com/en-us/visualstudio/releases/2026/release-history) + includes VS2026; the runner does not download a mutable latest bootstrapper. +- The x64 `cl.exe`, `link.exe`, `lib.exe`, `dumpbin.exe`, and Windows SDK + `10.0.26100.0` x64 `midl.exe`. Microsoft signatures must be valid. The selected + SDK is explicit through `vcvarsall.bat amd64 10.0.26100.0`; see Microsoft's + [native build environment](https://learn.microsoft.com/en-us/cpp/build/building-on-the-command-line?view=msvc-170). +- At least20GiB available on the evidence/build volume by default. This is the + runner's conservative admission threshold, not a measured installation size. +- The locked Cargo dependencies must already be available in the build user's + registry cache. Builds are `--locked --offline`. A missing crate is a failed + prerequisite, never a reason for an automatic online retry. An explicit + development preparation can populate the public dependencies with + `cargo +1.99.1-x86_64-pc-windows-msvc fetch --locked --target x86_64-pc-windows-msvc`. + Do not transfer an entire Cargo home containing credentials or configuration. + +The parent's actual read-only VM report +`devstate/windows-client/native-msvc-tools-readonly-20261114.json` now records +103300169728 free bytes, x64, no `vswhere`, no `rustup`, no SDK directories, +W32Time stopped and all execution-policy scopes undefined. No installation was +performed by this preparation. No extra private key, server token or production +signing certificate is needed for source build. +[Rust's MSVC target contract](https://doc.rust-lang.org/rustc/platform-support/windows-msvc.html) +defines native Windows support, Microsoft calling conventions and PE/COFF output. + +## Explicit tool-setup plan, not executed + +If preflight reports missing C++ tools, first select a **fixed-version** Build +Tools bootstrapper from Microsoft's release history, retain its download URL, +SHA-256, ProductVersion, valid Microsoft Authenticode signature and thumbprint. +Verify the intended absent/new install directory and sufficient disk before +starting setup. Reuse a suitable existing installation rather than updating it. +The exact required component IDs are: + +```text +Microsoft.VisualStudio.Workload.VCTools +Microsoft.VisualStudio.Component.VC.Tools.x86.x64 +Microsoft.VisualStudio.Component.Windows11SDK.26100 +``` + +The planned bootstrapper arguments are: + +```text +--wait --passive --norestart --installPath C:\RmmBuildTools +--add Microsoft.VisualStudio.Workload.VCTools +--add Microsoft.VisualStudio.Component.VC.Tools.x86.x64 +--add Microsoft.VisualStudio.Component.Windows11SDK.26100 +``` + +There is no `--force`, repair, OS update, automatic retry or reboot. Persist the +actual setup process/result before returning a bootstrap reply. `0` completes +the setup operation; `3010` means installation requires a restart **before use**, +so stop without building, clearing the flag or implicitly rebooting. `1641` +means a reboot was initiated and violates this no-restart plan. `1618`/`1001` +mean another installation is active; preserve the case and inspect, never kill +the other installer or blindly rerun. A lost SSH reply permits only process/log +readback of that same setup. The build runner itself never launches setup. +[Microsoft component IDs](https://learn.microsoft.com/en-us/visualstudio/install/workload-component-id-vs-build-tools?view=visualstudio), +[parameters and actual exit-code semantics](https://learn.microsoft.com/en-us/visualstudio/install/use-command-line-parameters-to-install-visual-studio?view=vs-2022). + +## Build invocation and evidence + +Planned, not executed: + +```powershell +powershell.exe -NoProfile -ExecutionPolicy RemoteSigned -File .\deploy\windows\Build-NativeMsvc.ps1 -Mode Preflight -SourceRoot C:\Source\rust-discovery +# Review that exact private preflight.json, then use a NEW build case: +powershell.exe -NoProfile -ExecutionPolicy RemoteSigned -File .\deploy\windows\Build-NativeMsvc.ps1 -Mode Build -SourceRoot C:\Source\rust-discovery ` + -ExpectedPreflight C:\Users\Administrator\RmmNativeBuilds\\preflight.json +``` + +The runner imports only selected developer environment values, rejects caller +compiler flags/wrappers, pins the native compiler/linker/MIDL paths, and keeps +each process's stdout/stderr private. It copies only explicit source extensions +under `crates/` and `proto/`, root Cargo/toolchain files and the MSVC runner/probe; +no `credentials/`, `devstate/`, client key material, repository metadata or local +server state. A protected build root and new protected case prevent unrelated +service users from changing the copied inputs. Source/tool hashes must match +before and after the build. + +Before Cargo, Microsoft MIDL generates the real IDL with `/env amd64 /Oicf +/robust /Zp8`; `win64` is intentionally avoided because MIDL documents it as +IA64. The native C probe asserts Microsoft LLP64, numeric Rust ABI64 bytes, +wire-status ABI120 bytes and explicit field offsets. It raises/catches only a +local RPC exception1722 using the same SDK SEH macros/filter; it does not query +W32Time, contact RPC or claim trusted time. There is no vendored WIDL stub or +SEH workaround. [MIDL AMD64/packing](https://learn.microsoft.com/en-us/windows/win32/midl/-env), +[robust NDR checking](https://learn.microsoft.com/en-us/windows/win32/midl/-robust), +[C11 assertions](https://learn.microsoft.com/en-us/cpp/c-language/static-assert-c?view=msvc-170), +[SDK RPC SEH](https://learn.microsoft.com/en-us/windows/win32/rpc/rpctryexcept). + +The product build script adds only the native MSVC branch, preserving the GNU +WIDL/clang branch and the C exception boundary. Both still compile the actual +clock sources and preserve their static assertions; SDK or compiler failure is +fatal. The Clock readiness gate stays unqualified. The separate native +service-SID authentication candidate is not qualified by an ABI or build pass. + +Cargo then checks Windows Clippy with warnings denied, builds agent/helper and +examples in release, and **links without running** Windows/core unit tests. +The runner records AMD64 PE headers, artifact hashes, generated RPC hashes, +dependency listings and actual Authenticode status. Development output may be +unsigned; this is no signing/installer/production-PKI acceptance. Native unit +execution, LocalService/SYSTEM behavior and a preserving daemon deployment +remain separate explicit gates. A timeout retains its exact process/evidence; +no process-tree kill or retry is implied. + +Locally executed preparation checks: PowerShell AST parse; isolated compilation +and Clippy of `build.rs` using locked-version `cc1.11.1`; rustfmt/source checks. +These checks do not prove that Microsoft MIDL, MSVC, the ABI probe or the final +agent/helper builds have run successfully on Windows. + +## Concrete native tool bootstrap, prepared separately + +`Install-NativeBuildTools.ps1` supplies separate `Prepare`, `InstallVs`, +`InstallRust`, and read-only `Read` phases. It is prepared, **not natively +executed** by this work. Both install calls consume create-only start intents +before launching their fixed worker. Each worker runs in its own protected, +demand-only Task Scheduler entry as SYSTEM, without a trigger, automatic retry +or autostart; a second worker entry refuses before installation. This keeps a +long setup independent of the40s SSH/bootstrap reply limit. Results, actual +installer PID and exit code are retained, and the task is disabled after its +worker result. Lost reply/timeout permits readback only. These are explicitly +authorized development-tool bootstrap tasks, never WebUI/RMM jobs or a generic +SYSTEM execution provider. [Task registration](https://learn.microsoft.com/en-us/powershell/module/scheduledtasks/register-scheduledtask?view=windowsserver2025-ps), +[fixed service-account principal](https://learn.microsoft.com/en-us/powershell/module/scheduledtasks/new-scheduledtaskprincipal?view=windowsserver2025-ps). + +Inputs are fixed official TLS URLs with ordinary certificate/name validation: + +| Input | Pinned SHA-256 | +|---|---| +| Microsoft Build Tools18.11.4 LTSC bootstrapper, file version18.11.12405.450 | `386b5d9a3d6576eab3772c1ecaa7c107ded38146a9c483f41679c194ae1e63b4` | +| rustup1.29.3 Windows x64 MSVC bootstrapper | `38027d51d69a5d05493627a0634dbea52d6cd793c8f072d2373d3f4163eec7db` | +| Rust1.99.1 official distribution manifest | `fb152be80975c194aa5fc684b2aec3a6e14e2b855efe2faa4e5f8ae6e590f463` | + +The Microsoft EXE additionally requires valid Microsoft Authenticode and the +exact product/file version. Rustup is checked against its exact +[official archived checksum](https://static.rust-lang.org/rustup/archive/1.29.3/x86_64-pc-windows-msvc/rustup-init.exe.sha256), +with [official archive/install options](https://rust-lang.github.io/rustup/installation/other.html). +Its homes are private case subdirectories; `--no-modify-path`, +`--no-update-default-toolchain`, minimal profile and exactly clippy/rustfmt keep +the developer's default Rust installation/profile/PATH untouched. The fixed +Rust distribution origin is set only for that worker process. Using isolated +homes is an [official rustup option](https://rust-lang.github.io/rustup/installation/index.html). + +VS setup uses only `--wait --quiet --norestart --noUpdateInstaller`, the new +fixed target `C:\RmmBuildTools`, and the three components above. It relies on the +official installer's standard Microsoft metadata/package verification. No +custom channel/catalog, checksum override, certificate import or weaker +verification flag is supplied. Installed VS version must then match exactly +18.11.12405.450 and SDK26100 must be present; drift remains incomplete. Setup's +required transitive development dependencies remain Microsoft's responsibility; +this is no general production-installer trust acceptance. No Windows Update +command is invoked. Boot/OS build, machine trust-store fingerprints, machine +PATH, execution policies, W32Time and both exact RMM service states/PIDs are +observed before/after. Detected changes, unexpected setup version,3010/1641 or +restart need stop completion without resetting/undoing or retrying anything. + +Scope decision2026-11-14: an initial **custom manual catalog-bundle** design was +rejected. Its channel declared catalog size31851991 and hash +`ef56e369d92855a9667cd03602019375f42b3cd2145041df31cacc071a40e1af`; +the exact official URL instead returned18733589 bytes with hash +`805d16fd14a370cbeb6568179950826ba0cdc6e2cffa44936fddc408ef05cba3`. +Those inputs and rejected source remain privately retained; no mismatching +catalog was accepted or installed. The extra manual bundle was our design, +not an AGENTS/product requirement. The selected replacement uses the supported, +independently hash/Authenticode-pinned official EXE and its own verification, +not a checksum exception or undocumented JSON-signature workaround. +Microsoft documents [standard acquisition and fixed-version bootstrappers](https://learn.microsoft.com/en-us/visualstudio/install/use-command-line-parameters-to-install-visual-studio?view=vs-2022). + +Planned, not executed: + +```powershell +powershell.exe -NoProfile -ExecutionPolicy RemoteSigned -File .\deploy\windows\Install-NativeBuildTools.ps1 -Phase Prepare +# Review that exact printed private case and its prepared.json first: +powershell.exe -NoProfile -ExecutionPolicy RemoteSigned -File .\deploy\windows\Install-NativeBuildTools.ps1 -Phase InstallVs -CaseDirectory +powershell.exe -NoProfile -ExecutionPolicy RemoteSigned -File .\deploy\windows\Install-NativeBuildTools.ps1 -Phase Read -CaseDirectory +# Continue only after verified VS exit0 and no restart need: +powershell.exe -NoProfile -ExecutionPolicy RemoteSigned -File .\deploy\windows\Install-NativeBuildTools.ps1 -Phase InstallRust -CaseDirectory +powershell.exe -NoProfile -ExecutionPolicy RemoteSigned -File .\deploy\windows\Install-NativeBuildTools.ps1 -Phase Read -CaseDirectory +``` + +This uses the already authorized **development/bootstrap-only** process option +`-ExecutionPolicy RemoteSigned`, as in the previous Windows dev-access and +passive-client bootstrap. The worker uses that same process option and invokes +the hash-checked `.ps1` file normally with `& $p`; it never executes file content +through `ScriptBlock::Create`. A small encoded hash launcher is only transport +for that ordinary file invocation. There is no `Set-ExecutionPolicy`, registry +policy write, Machine/User/GPO change or Bypass option. Persistent scopes stay +unchanged; process scopes are consistently RemoteSigned and GPO precedence is +retained. This permission does **not** decide or authorize the separate RMM +PowerShell job executor's still-unqualified policy contract. + +After actual successful setup, pass the recorded private homes to the builder: + +```powershell +powershell.exe -NoProfile -ExecutionPolicy RemoteSigned -File .\deploy\windows\Build-NativeMsvc.ps1 -Mode Preflight -SourceRoot C:\Source\rust-discovery ` + -RustupExe \cargo_home\bin\rustup.exe -RustupHome \rustup_home -CargoHome \cargo_home +``` + +Use those same arguments with `-Mode Build -ExpectedPreflight `. +Populate the isolated public Cargo cache explicitly before an offline build; +there is no automatic fetch/retry in the builder. Native setup, native MIDL/ABI, +unit execution and deployment results are still outstanding until actually run. diff --git a/deploy/windows/msvc/clock_abi_probe.c b/deploy/windows/msvc/clock_abi_probe.c new file mode 100644 index 0000000..0c81c67 --- /dev/null +++ b/deploy/windows/msvc/clock_abi_probe.c @@ -0,0 +1,40 @@ +/* Pure native ABI + local SEH qualification. No RPC/network/time query. */ +#define _WIN32_WINNT 0x0a00 +#include +#include +#include +#include +#include +#include "w32time.h" + +typedef struct { + uint32_t leap, stratum, state, flags, last_result, size; + uint64_t last_sync, sync_age; + int64_t root_delay; + uint64_t root_dispersion; + int64_t phase; +} RmmClockStatus; + +_Static_assert(sizeof(void *) == 8, "native AMD64 pointers"); +_Static_assert(sizeof(unsigned long) == 4, "Microsoft LLP64"); +_Static_assert(sizeof(RmmClockStatus) == 64, "Rust numeric clock ABI"); +_Static_assert(offsetof(RmmClockStatus, last_sync) == 24, "Rust FILETIME offset"); +_Static_assert(offsetof(RmmClockStatus, phase) == 56, "Rust phase offset"); +_Static_assert(sizeof(RMM_W32TIME_STATUS) == 120, "MS-W32T x64 wire structure"); +_Static_assert(offsetof(RMM_W32TIME_STATUS, qwLastSyncTicks) == 24, "MS-W32T FILETIME offset"); +_Static_assert(offsetof(RMM_W32TIME_STATUS, toSysPhaseOffset) == 64, "MS-W32T phase offset"); +_Static_assert(offsetof(RMM_W32TIME_STATUS, pEntries) == 112, "MS-W32T array pointer offset"); + +int main(void) { + unsigned long caught = 0; + /* Exercise the same SDK SEH macros/filter as clock_rpc.c. The exception + originates locally; it must terminate in C and never cross into Rust. */ + RpcTryExcept { + RpcRaiseException(RPC_S_SERVER_UNAVAILABLE); + } RpcExcept(RpcExceptionFilter(RpcExceptionCode())) { + caught = RpcExceptionCode(); + } RpcEndExcept + if (caught != RPC_S_SERVER_UNAVAILABLE) return 1; + printf("{\"amd64\":true,\"numeric_size\":64,\"wire_size\":120,\"seh_caught\":%lu,\"network_called\":false}\n", caught); + return 0; +} diff --git a/deploy/windows/runtime_feature_upgrade.py b/deploy/windows/runtime_feature_upgrade.py new file mode 100644 index 0000000..bbde56d --- /dev/null +++ b/deploy/windows/runtime_feature_upgrade.py @@ -0,0 +1,257 @@ +#!/usr/bin/env python3 +"""New passive IPC-client runtime qualification/upgrade; historical V1 stays intact. + +No pin/helper service is installed here. Native unit tests, actual runtime change +and later real LocalService-to-SYSTEM IPC are distinct acceptance steps. +""" +import argparse +import hashlib +import json +import os +from pathlib import Path +import re +import shutil +import subprocess +import uuid + +import runtime_upgrade as previous +import client_bootstrap as cb +import transport_acceptance as base +import auto_transport_acceptance as auto + +require=cb.contract.require +write=previous.write +digest=previous.digest +STATE=cb.EVIDENCE/'runtime-feature-upgrade' +CURRENT=cb.EVIDENCE/'current-runtime.json' +TARGET=previous.TARGET +IMAGE=previous.IMAGE +OLD='f64e81155a4d86089a9dd7e6ec42ffba50b939442386c3187a28769a4de1aa0b' +ORIGINAL='31e360e50b177b4d5729d24e436d4f25aeed7b1a7bd35f2a129841ba1abffd37' + + +def replace_once(source, old, new): + require(source.count(old)==1,'exact historical source transform, no fuzzy replacement') + return source.replace(old,new,1) + + +def upgrade_source(): + source=replace_once(previous.UPGRADE,"$p.old_sha256 -cne '"+ORIGINAL+"'","$p.old_sha256 -cne '"+OLD+"'") + source=replace_once(source,r"$folder='C:\ProgramData\RustDiscoveryBootstrap\runtime-'+$p.id",r"$folder='C:\ProgramData\RustDiscoveryBootstrap\runtime-feature-'+$p.id") + source=replace_once(source,r"$active='C:\ProgramData\RustDiscoveryBootstrap\runtime-active-intent.json'",r"$active='C:\ProgramData\RustDiscoveryBootstrap\runtime-feature-active-intent.json'") + source=replace_once(source,"function Snapshot([string]$expected) {",r'''function Snapshot([string]$expected) { + if ((Test-Path -LiteralPath 'C:\ProgramData\RustDiscoverySystem') -or (Test-Path -LiteralPath 'C:\Program Files\RustDiscovery\rmm-helper-windows.exe') -or $null -ne (Get-Service -Name 'RmmDiscoverySystemHelper' -ErrorAction SilentlyContinue)) {throw 'Complete passive feature upgrade before any helper/pin installation'}''') + return source + + +UPGRADE=upgrade_source() +PREPARE=base.PREPARE_SCRIPT.replace('TransportAcceptance.ps1','RuntimeFeatureUpgrade.ps1') +LAUNCH=base.LAUNCH_SCRIPT.replace('TransportAcceptance.ps1','RuntimeFeatureUpgrade.ps1').replace('RmmTransportPayload','RmmRuntimePayload') + + +def source_files(): + files=previous.source_files() + # Clippy --all-targets also compiles native_acl and EVERY example. Pin + # those, all nested modules and each local crate's optional build script. + for crate in ('agent-windows','agent-core','domain','protocol'): + root=cb.ROOT/'crates'/crate + for path in root.rglob('*'): + if path.is_file() and path.suffix in ('.rs','.c','.h','.idl','.toml'): + relative=str(path.relative_to(cb.ROOT));files[relative]=digest(path) + for path in ('deploy/windows/runtime_upgrade.py','deploy/windows/runtime_feature_upgrade.py'): + files[path]=digest(cb.ROOT/path) + return dict(sorted(files.items())) + + +def stable(manifest,current_sources=True): + require(manifest['schema_version']==1 and manifest['architecture']==TARGET and manifest['default_features'] is True and manifest['system_helper_feature'] is True and manifest['system_helper_client_feature'] is True,'explicit optional IPC-client feature profile') + if current_sources: + require(source_files()==manifest['source_files'],'feature build sources unchanged') + require(re.search(r'^pub const ADAPTER_QUALIFIED: bool = false;$',(cb.ROOT/'crates/agent-windows/src/clock.rs').read_text(),re.M) is not None,'production clock gate must remain closed') + require(manifest['checks']['production_clock_qualified'] is False,'compiled production clock stayed unqualified') + for item in manifest['artifacts'].values():require(digest(item['path'])==item['sha256'],'actual retained linked artifact hash') + + +def build(): + os.umask(0o077);STATE.mkdir(mode=0o700,parents=True,exist_ok=True) + folder=STATE/('build-'+str(uuid.uuid4()));folder.mkdir(mode=0o700) + before=source_files();write(folder,'source-before.json',before) + require(re.search(r'^pub const ADAPTER_QUALIFIED: bool = false;$',(cb.ROOT/'crates/agent-windows/src/clock.rs').read_text(),re.M) is not None,'production clock gate must remain closed') + target=cb.ROOT/'devstate/windows-runtime-feature/target';target.mkdir(mode=0o700,parents=True,exist_ok=True) + script='cargo clippy --locked --offline --target '+TARGET+' -p rmm-agent-windows --features system-helper --all-targets -- -D warnings && cargo build --locked --offline --target '+TARGET+' -p rmm-agent-windows --features system-helper --bin rmm-agent-windows --example runtime_state --release && cargo test --locked --offline --target '+TARGET+' -p rmm-agent-windows --features system-helper --lib --bin rmm-agent-windows --no-run --message-format=json' + raw=previous.command(['docker','run','--rm','--network','none','--read-only','--tmpfs','/tmp:rw,size=512m','-v',str(cb.ROOT)+':/src:ro','-v',str(target)+':/target','-e','CARGO_TARGET_DIR=/target','--entrypoint','/bin/sh',IMAGE,'-c',script]) + require(source_files()==before,'no source drift during feature build') + units={} + for line in raw.decode().splitlines(): + try:item=json.loads(line) + except ValueError:continue + if item.get('reason')=='compiler-artifact' and 'rmm-agent-windows' in item.get('package_id',''): + require('system-helper' in item.get('features',[]),'actual compiled feature profile') + if item.get('profile',{}).get('test') and item.get('executable'): + kinds=item['target']['kind'];key='lib' if 'lib' in kinds else 'bin' if 'bin' in kinds else None + if key:units[key]=target/Path(item['executable']).relative_to('/target') + require(set(units)=={'lib','bin'},'new feature lib and binary unit links') + artifacts={} + for key,path,name,release in [('agent',target/TARGET/'release/rmm-agent-windows.exe','rmm-agent-windows.exe',True),('probe',target/TARGET/'release/examples/runtime_state.exe','RuntimeStateProbe.exe',True),('lib',units['lib'],'windows-tests-lib.exe',False),('bin',units['bin'],'windows-tests-bin.exe',False)]: + out=folder/name;shutil.copyfile(path,out);out.chmod(0o600);original=digest(path) + if key in ('lib','bin'): + previous.command(['docker','run','--rm','--network','none','--read-only','-v',str(folder.resolve())+':/artifacts','--entrypoint','x86_64-w64-mingw32-strip',IMAGE,'--strip-debug','/artifacts/'+name]) + require(digest(path)==original,'retain original unit link') + data=out.read_bytes();offset=int.from_bytes(data[60:64],'little') + require(data[:2]==b'MZ' and data[offset:offset+6]==b'PE\0\0\x64\x86','actual Windows AMD64 artifact') + artifacts[key]=dict(path=str(out.resolve()),name=name,sha256=digest(out),bytes=len(data),release_build=release,original_link_sha256=original) + require(source_files()==before,'source unchanged through artifact preservation') + manifest=dict(schema_version=1,architecture=TARGET,default_features=True,system_helper_feature=True,system_helper_client_feature=True,source_files=before,artifacts=artifacts,checks=dict(cross_clippy_D_warnings=True,release_build=True,windows_unit_link=True,production_clock_qualified=False),native_vm_tested=False) + write(folder,'manifest.json',manifest) + print(json.dumps(dict(manifest=str((folder/'manifest.json').resolve()),artifacts={k:v['sha256'] for k,v in artifacts.items()},native_vm_tested=False))) + + +def qualify(manifest_path): + manifest=cb.contract.protected_json(manifest_path);stable(manifest) + os.umask(0o077);STATE.mkdir(mode=0o700,parents=True,exist_ok=True) + folder=STATE/('qualification-'+str(uuid.uuid4()));folder.mkdir(mode=0o700) + write(folder,'intent.json',dict(manifest_path=str(manifest_path.resolve()),manifest_sha256=digest(manifest_path),source_files=manifest['source_files'],test_runner_sha256=hashlib.sha256(previous.TESTS.encode()).hexdigest(),system_helper_client_feature=True)) + before=auto.scopes();flags=auto.status();write(folder,'server-before.json',dict(counts=before,flags=flags)) + reports={} + for key,count in [('lib',21),('bin',6)]: + item=manifest['artifacts'][key];previous.upload(item) + report=cb.powershell(previous.TESTS,dict(name=item['name'],sha256=item['sha256']),timeout=150) + write(folder,key+'-native.json',report) + names=re.findall(r'^(.+): test\r?$',report['listed']['stdout'],re.M) + summary=re.search(r'test result: ok\. (\d+) passed; 0 failed; 0 ignored; 0 measured; 0 filtered out;',report['result']['stdout']) + require(report['listed']['exit_code']==report['result']['exit_code']==0 and summary is not None and len(names)==int(summary.group(1))==count,'all exact 21+6 new native units, none skipped or filtered') + reports[key]=dict(artifact_sha256=item['sha256'],passed=len(names),test_names=names,native_context='bootstrap_administrator_unit_tests') + stable(manifest) + after=auto.scopes();after_flags=auto.status();require(before==after and flags==after_flags and auto.flags_disabled(flags),'native units have no jobs/policy/fault effect') + write(folder,'result.json',dict(verified=True,completed=True,native_unit_tests=reports,manifest_path=str(manifest_path.resolve()),manifest_sha256=digest(manifest_path),source_files=manifest['source_files'],server_counts=after,server_flags=flags,no_jobs=True,clock_adapter_qualified=False,system_helper_client_feature=True,local_service_runtime_test=False,native_pipe_peer_test=False)) + print(json.dumps(dict(verified=True,qualification=str((folder/'result.json').resolve()),native_tests={k:v['passed'] for k,v in reports.items()},native_pipe_peer_test=False))) + + +def qualified(manifest_path,qualification_path,current_sources=True): + manifest=cb.contract.protected_json(manifest_path);stable(manifest,current_sources) + proof=cb.contract.protected_json(qualification_path) + require(proof.get('verified') is True and proof.get('completed') is True and proof.get('system_helper_client_feature') is True and proof['manifest_path']==str(manifest_path.resolve()) and proof['manifest_sha256']==digest(manifest_path) and proof['source_files']==manifest['source_files'] and proof.get('no_jobs') is True and proof.get('clock_adapter_qualified') is False,'new source-bound native feature qualification') + for key,count in [('lib',21),('bin',6)]: + require(proof['native_unit_tests'][key]['passed']==count and proof['native_unit_tests'][key]['artifact_sha256']==manifest['artifacts'][key]['sha256'],'exact new compiled native unit association') + return manifest,proof + + +def prior_runtime(): + index=cb.contract.protected_json(CURRENT) + require(index.get('type')=='rmm.windows.current-runtime' and index.get('binary_sha256')==OLD and index.get('agent_id')==base.AGENT and index.get('asset_id')==base.ASSET and index.get('transport')=='auto' and index.get('windows_jobs_enabled') is False and index.get('clock_adapter_qualified') is False and not index.get('system_helper_client_feature',False),'accepted original f64 passive runtime') + path=Path(index['evidence']['path']).resolve() + require(path.is_relative_to(cb.EVIDENCE.resolve()) and digest(path)==index['evidence']['sha256'],'exact accepted old runtime evidence') + proof=cb.contract.protected_json(path) + require(proof.get('verified') is True and proof.get('completed') is True and proof.get('restored') is False and proof.get('no_jobs') is True and proof['native']['binary_sha256']==OLD,'actual old passive runtime, not a historical transport artifact') + return index,proof + + +def stage_source(): + raw=UPGRADE.encode();sha=hashlib.sha256(raw).hexdigest();value=cb.powershell(PREPARE,dict(sha256=sha)) + if not value['already_uploaded']: + folder=STATE/'source';folder.mkdir(mode=0o700,parents=True,exist_ok=True);path=folder/(sha+'.ps1') + if not path.exists(): + fd=os.open(path,os.O_CREAT|os.O_EXCL|os.O_WRONLY|os.O_NOFOLLOW,0o600) + with os.fdopen(fd,'wb') as stream:stream.write(raw);stream.flush();os.fsync(stream.fileno()) + require(digest(path)==sha,'exact feature source bytes') + prefix=cb.contract.protected_json(cb.ROOT/'credentials/windows-dev-access.json')['ssh_prefix'] + args=['sftp']+[part for part in prefix[1:-1] if part!='-T']+['-b','-',prefix[-1]] + target='/C:/ProgramData/RustDiscoveryBootstrap/'+sha+'/RuntimeFeatureUpgrade.ps1' + result=subprocess.run(args,input=('put '+json.dumps(str(path.resolve()))+' '+json.dumps(target)+'\n').encode(),capture_output=True,timeout=60) + require(result.returncode==0 and cb.powershell(PREPARE,dict(sha256=sha))['already_uploaded'],'exact staged feature source') + return sha + + +def remote(intent,action): + require(intent['source_sha256']==hashlib.sha256(UPGRADE.encode()).hexdigest(),'immutable exact feature source') + return cb.powershell(LAUNCH,dict(sha256=intent['source_sha256'],payload=dict(intent['remote_payload'],action=action)),timeout=100,remote_signed=True) + + +def upgrade(manifest_path,qualification_path): + manifest,proof=qualified(manifest_path,qualification_path);index,old=prior_runtime() + os.umask(0o077);STATE.mkdir(mode=0o700,parents=True,exist_ok=True);ident=str(uuid.uuid4());folder=STATE/ident;folder.mkdir(mode=0o700) + for key in ('agent','probe'):previous.upload(manifest['artifacts'][key]) + source=stage_source() + payload=dict(id=ident,old_sha256=OLD,new_sha256=manifest['artifacts']['agent']['sha256'],probe_sha256=manifest['artifacts']['probe']['sha256'],config_sha256=old['native']['config_sha256']) + intent=dict(id=ident,type='passive_feature_runtime_upgrade',started_at=cb.contract.now().isoformat(),source_sha256=source,manifest_path=str(manifest_path.resolve()),manifest_sha256=digest(manifest_path),qualification_path=str(qualification_path.resolve()),qualification_sha256=digest(qualification_path),remote_payload=payload,previous_runtime_index=index,previous_runtime_index_sha256=digest(CURRENT),no_jobs=True,no_helper_install=True,no_reboot=True) + write(folder,'intent.json',intent) + before=dict(native=remote(intent,'state'),central=base.observe(ident),counts=auto.scopes(),server=auto.status()) + require(before['counts']==proof['server_counts'] and before['server']==proof['server_flags'] and auto.flags_disabled(before['server']),'unchanged jobs/policies since native units') + require(before['native']['service']['state']=='Running' and before['native']['journal']['pending_inventory_events']==0,'settled retained actual original runtime') + for field in ('binary_sha256','binary_acl_sha256','config_sha256','config_acl_sha256','public_hashes','identity_acls','private_key_metadata'): + require(before['native'][field]==old['native'][field],'accepted old runtime identity/config/ACL continuity') + require(digest(CURRENT)==intent['previous_runtime_index_sha256'],'current runtime index unchanged before mutation') + write(folder,'before.json',before);write(folder,'enter-intent.json',dict(id=ident,stop_swap_start_once=True,source_sha256=source)) + try:write(folder,'entered.json',remote(intent,'enter')) + except Exception as error: + write(folder,'enter-reply-incomplete.json',dict(error_type=type(error).__name__,continuation='finish/readback only; never repeat enter or start'));raise + print('Passive feature runtime transition '+ident,flush=True);finish(ident) + + +def load_case(ident): + require(str(uuid.UUID(ident))==ident,'canonical existing feature case') + folder=STATE/ident;intent=cb.contract.protected_json(folder/'intent.json') + require(intent['id']==ident and intent['type']=='passive_feature_runtime_upgrade' and digest(intent['manifest_path'])==intent['manifest_sha256'] and digest(intent['qualification_path'])==intent['qualification_sha256'],'same original immutable feature case and qualification') + manifest,_=qualified(Path(intent['manifest_path']),Path(intent['qualification_path']),current_sources=False) + return folder,intent,manifest + + +def publish(folder,result_name,intent,binary,restored): + result=folder/result_name + value=dict(schema_version=1,type='rmm.windows.current-runtime',case_id=folder.name,agent_id=base.AGENT,asset_id=base.ASSET,binary_sha256=binary,transport='auto',evidence=dict(path=str(result.resolve()),sha256=digest(result)),windows_jobs_enabled=False,clock_adapter_qualified=False,system_helper_client_feature=not restored) + actual=cb.contract.protected_json(CURRENT) + if actual==value:return + require(digest(CURRENT)==intent['previous_runtime_index_sha256'] or actual.get('case_id')==folder.name,'CAS current runtime: never overwrite another transition') + write(folder,'previous-index-'+str(uuid.uuid4())+'.json',actual) + temporary=CURRENT.with_name('.feature-runtime-'+str(uuid.uuid4())+'.json');write(temporary.parent,temporary.name,value);os.replace(temporary,CURRENT) + + +def finish(ident): + folder,intent,manifest=load_case(ident);restored=(folder/'rollback-intent.json').exists() + before=cb.contract.protected_json(folder/'before.json');observed=remote(intent,'finish');write(folder,'finish-readback-'+str(uuid.uuid4())+'.json',observed) + expected=OLD if restored else manifest['artifacts']['agent']['sha256'] + require(observed.get('completed') is True and observed.get('read_only') is True and observed.get('restored')==restored and observed['native']['binary_sha256']==expected,'actual selected runtime after fresh readback, never historical success replay') + started=cb.contract.protected_json(folder/'rollback-intent.json')['started_at'] if restored else intent['started_at'] + online=base.wait_mode(ident,'wss',cb.contract.timestamp(started),before['central']['device']) + after=remote(intent,'finish');write(folder,'after-readback-'+str(uuid.uuid4())+'.json',after) + require(after.get('restored')==restored and after['native']['binary_sha256']==expected,'same actual selected runtime after native heartbeat wait') + native=after['native'];require(native['journal']['pending_inventory_events']==0 and native['journal']['sequence']>before['native']['journal']['sequence'],'two actual WSS heartbeats/sequence advance and concrete original telemetry ACK') + device=base.read_device();denied=base.foreign_denied() + require(cb.contract.timestamp(device['observed_at'])>=cb.contract.timestamp(intent['started_at']),'actual fresh stored inventory of retained identity') + counts=auto.scopes();flags=auto.status();require(counts==before['counts'] and flags==before['server'] and auto.flags_disabled(flags),'Windows/Linux job counts and server policies/hooks unchanged') + result_name='rollback-completed.json' if restored else 'result.json';path=folder/result_name + value=dict(verified=True,completed=True,restored=restored,id=ident,native=native,online=online,stored_inventory=device,foreign_denied=denied,manifest_path=intent['manifest_path'],manifest_sha256=intent['manifest_sha256'],qualification_sha256=intent['qualification_sha256'],counts=counts,server=flags,no_jobs=True,no_reboot=True,no_clock_change=True,windows_jobs_enabled=False,clock_adapter_qualified=False,system_helper_client_feature=not restored,native_pipe_peer_test=False,configuration_unchanged=True,private_key_read=False,transport='auto',preferred_wire='wss') + if path.exists():require(cb.contract.protected_json(path).get('verified') is True,'retain original completed result') + else:write(folder,result_name,value) + publish(folder,result_name,intent,expected,restored) + print(json.dumps(dict(verified=True,id=ident,system_helper_client_feature=not restored,native_pipe_peer_test=False,windows_jobs_enabled=False))) + + +def rollback(ident): + folder,intent,_=load_case(ident);before=cb.contract.protected_json(folder/'before.json') + require(auto.scopes()==before['counts'] and auto.status()==before['server'],'no intervening jobs/policy changes before explicit rollback') + write(folder,'rollback-intent.json',dict(id=ident,old_binary_sha256=OLD,started_at=cb.contract.now().isoformat(),start_once=True,no_jobs=True)) + try:value=remote(intent,'rollback') + except Exception as error: + write(folder,'rollback-reply-incomplete.json',dict(error_type=type(error).__name__,readback_required=True,no_repeated_start=True));raise + write(folder,'rollback-result.json',value) + require(value.get('restored') is True and value['native']['binary_sha256']==OLD,'actual original f64 binary restored') + finish(ident) + + +def main(): + parser=argparse.ArgumentParser(description=__doc__) + parser.add_argument('phase',choices=['build-local','qualify-native','upgrade-passive','finish','rollback']) + parser.add_argument('--manifest',type=Path);parser.add_argument('--qualification',type=Path);parser.add_argument('--id') + args=parser.parse_args() + if args.phase=='build-local':require(not(args.manifest or args.qualification or args.id),'fresh immutable feature build');build() + elif args.phase=='qualify-native':require(args.manifest and not(args.qualification or args.id),'one new feature manifest');qualify(args.manifest) + elif args.phase=='upgrade-passive':require(args.manifest and args.qualification and not args.id,'same new native feature qualification');upgrade(args.manifest,args.qualification) + else:require(args.id and not(args.manifest or args.qualification),'same original feature case only');{'finish':finish,'rollback':rollback}[args.phase](args.id) + + +if __name__=='__main__': + try:main() + except Exception as error: + print('WINDOWS_FEATURE_RUNTIME_INCOMPLETE:'+type(error).__name__) + raise SystemExit(2) from None diff --git a/deploy/windows/system_helper_bootstrap.py b/deploy/windows/system_helper_bootstrap.py new file mode 100644 index 0000000..a2e82f6 --- /dev/null +++ b/deploy/windows/system_helper_bootstrap.py @@ -0,0 +1,390 @@ +#!/usr/bin/env python3 +"""Create-once passive SYSTEM helper bootstrap. Never creates or approves a job. + +Deployment order: accepted feature-agent upgrade first, this helper second. The +running LocalService agent discovers the protected pin without another restart. +Only its own kernel-authenticated Probe can prove the positive pipe exchange. +A lost mutation reply permits readback only, never a repeated install/start. +""" +import argparse +import hashlib +import json +import os +from pathlib import Path +import re +import subprocess +import uuid + +import client_bootstrap as cb +import transport_acceptance as base + +require = cb.contract.require +STATE = cb.EVIDENCE / 'system-helper-bootstrap' +AGENT_ID = '63ea15b2-0643-403d-b5b4-cba1288dbea3' +ASSET_ID = 'a176520e-8a91-4e93-a78b-8e7620f430e0' +HELPER_SHA = '921cf1822f128c3e37ca4c0250f08309a14d760453fa3b56a589229c6614353e' +UNIT_SHA = 'd98bbd7c96b78f13b3590bc507961821788672326dd2b690050e81e258e64b45' +QUALIFICATION = cb.EVIDENCE / 'helper-unit-qualification/38b0fe75-f6a5-4ad3-ab2d-7f3acf7e9cbd/result.json' +OWN_SOURCES = { + 'crates/agent-windows/src/helper_ipc.rs': '8163e8029b7dfe256d82995036f351c6e241c8e1e184f34129c15ac205052552', + 'crates/agent-windows/src/helper_system.rs': '07908fae9f834f9bd728a10885596d2d997d5e26a5712254de35b9e47dfa7226', + 'crates/agent-windows/src/bin/rmm-helper-windows.rs': 'e87c39bee18dc9375287e4f490f70d6bbd76629884564f76804f2bd9152358a4', +} + +SCRIPT = r''' +$ErrorActionPreference='Stop' +$p=$global:RmmSystemHelperPayload +if ($null -eq $p -or [string]([guid]$p.id) -cne $p.id -or $p.action -notin @('install','start','readback')) {throw 'Fixed passive helper intent'} +foreach($n in @('helper_sha256','agent_sha256','agent_acl_sha256','config_sha256','config_acl_sha256','source_sha256','policy_sha256','pin_sha256')) {if ($p.$n -notmatch '^[0-9a-f]{64}$') {throw 'Exact bootstrap hashes'}} +if ($p.helper_sha256 -cne '921cf1822f128c3e37ca4c0250f08309a14d760453fa3b56a589229c6614353e') {throw 'Native-unit-qualified helper only'} +$name='RmmDiscoverySystemHelper';$agentName='RmmDiscoveryWindows' +$root='C:\ProgramData\RustDiscoverySystem';$state=Join-Path $root 'state' +$settingsPath=Join-Path $root 'helper.json';$pinPath=Join-Path $root 'agent-pin.json' +$binary='C:\Program Files\RustDiscovery\rmm-helper-windows.exe' +$agentBinary='C:\Program Files\RustDiscovery\rmm-agent-windows.exe' +$agentConfig='C:\ProgramData\RustDiscovery\config.json' +$bootstrap='C:\ProgramData\RustDiscoveryBootstrap' +$folder=Join-Path $bootstrap ('system-helper-'+$p.id) +$active=Join-Path $bootstrap 'system-helper-active-intent.json' +$uploaded=Join-Path (Join-Path $bootstrap $p.helper_sha256) 'rmm-helper-windows.exe' +$helperCommand='"C:\Program Files\RustDiscovery\rmm-helper-windows.exe" --service' +$agentCommand='"C:\Program Files\RustDiscovery\rmm-agent-windows.exe" --service --config "C:\ProgramData\RustDiscovery\config.json"' +$sys='S-1-5-18';$admin='S-1-5-32-544' +$agentSid=(New-Object Security.Principal.NTAccount('NT SERVICE',$agentName)).Translate([Security.Principal.SecurityIdentifier]).Value +function Sha([byte[]]$data) { + $h=[Security.Cryptography.SHA256]::Create() + try {return ([BitConverter]::ToString($h.ComputeHash($data))).Replace('-','').ToLowerInvariant()} finally {$h.Dispose()} +} +function Hash([string]$path) {return (Get-FileHash -LiteralPath $path -Algorithm SHA256).Hash.ToLowerInvariant()} +function Sid([string]$name) {return (New-Object Security.Principal.NTAccount('NT SERVICE',$name)).Translate([Security.Principal.SecurityIdentifier]).Value} +function Acl-Hash([string]$path) {return Sha ([Text.Encoding]::UTF8.GetBytes((Get-Acl -LiteralPath $path).Sddl))} +function Check([string]$path,[bool]$directory,[string[]]$readers,[string[]]$writers) { + $item=Get-Item -LiteralPath $path -Force + if ($item.PSIsContainer -ne $directory -or ($item.Attributes -band [IO.FileAttributes]::ReparsePoint)) {throw 'Protected regular path'} + $acl=Get-Acl -LiteralPath $path + if ($acl.GetOwner([Security.Principal.SecurityIdentifier]).Value -notin @($sys,$admin)) {throw 'Protected owner'} + if ($directory -and !$acl.AreAccessRulesProtected) {throw 'Explicit protected directory DACL required'} + $rules=@($acl.GetAccessRules($true,$true,[Security.Principal.SecurityIdentifier]));if (!$rules.Count) {throw 'Empty DACL'} + $seen=@{} + foreach($r in $rules) { + $id=$r.IdentityReference.Value;$rights=[int64]$r.FileSystemRights + if ($r.AccessControlType -ne 'Allow') {throw 'Unexpected deny or inherited DACL'} + if ($id -in @($sys,$admin)+$writers) {if ($rights -ne 2032127) {throw 'Exact writer rights'}} + elseif ($id -in $readers) {if (($rights -band (-bnot 1179817)) -ne 0 -or ($rights -band 131241) -ne 131241) {throw 'Reader must have read-execute only'}} + else {throw 'Unlisted principal'} + $seen[$id]=$true + } + foreach($id in @($sys,$admin)+$readers+$writers) {if (!$seen[$id]) {throw 'Missing intended DACL principal'}} +} +function Protect([string]$path,[bool]$directory,[string[]]$readers,[string[]]$writers) { + if ($directory) {$acl=New-Object Security.AccessControl.DirectorySecurity} else {$acl=New-Object Security.AccessControl.FileSecurity} + $acl.SetAccessRuleProtection($true,$false);$acl.SetOwner((New-Object Security.Principal.SecurityIdentifier($admin))) + $inherit='None';if ($directory) {$inherit='ContainerInherit,ObjectInherit'} + foreach($id in @($sys,$admin)+$writers) {$acl.AddAccessRule((New-Object Security.AccessControl.FileSystemAccessRule((New-Object Security.Principal.SecurityIdentifier($id)),'FullControl',$inherit,'None','Allow')))} + foreach($id in $readers) {$acl.AddAccessRule((New-Object Security.AccessControl.FileSystemAccessRule((New-Object Security.Principal.SecurityIdentifier($id)),'ReadAndExecute',$inherit,'None','Allow')))} + Set-Acl -LiteralPath $path -AclObject $acl;Check $path $directory $readers $writers +} +function Write-New([string]$path,$value) { + $bytes=[Text.Encoding]::UTF8.GetBytes(($value | ConvertTo-Json -Depth 24 -Compress)) + $f=[IO.File]::Open($path,[IO.FileMode]::CreateNew,[IO.FileAccess]::Write,[IO.FileShare]::Read) + try {$f.Write($bytes,0,$bytes.Length);$f.Flush($true)} finally {$f.Dispose()} +} +function Agent-State { + $service=Get-CimInstance Win32_Service -Filter "Name='RmmDiscoveryWindows'" -OperationTimeoutSec 10 + if ($service.State -cne 'Running' -or $service.StartName -cne 'NT AUTHORITY\LocalService' -or $service.PathName -cne $agentCommand -or $service.StartMode -cne 'Auto') {throw 'Original running LocalService agent'} + if ((Hash $agentBinary) -cne $p.agent_sha256 -or (Hash $agentConfig) -cne $p.config_sha256 -or (Acl-Hash $agentBinary) -cne $p.agent_acl_sha256 -or (Acl-Hash $agentConfig) -cne $p.config_acl_sha256) {throw 'Current accepted passive feature agent and ACLs'} + $cfg=Get-Content -LiteralPath $agentConfig -Raw | ConvertFrom-Json + if ((($cfg.PSObject.Properties.Name|Sort-Object)-join ',') -cne 'ca_file,connect_address,enrollment_token_file,server_name,state_directory,transport' -or $cfg.transport -cne 'auto' -or $cfg.server_name -cne 'discovery.ease-technologies.de') {throw 'Unchanged six-field passive auto configuration'} + $ident=Get-Content -LiteralPath 'C:\ProgramData\RustDiscovery\state\identity.json' -Raw | ConvertFrom-Json + if ($ident.agent_id -cne $p.binding.agent_id -or $ident.asset_id -cne $p.binding.asset_id -or $ident.agent_id -cne '63ea15b2-0643-403d-b5b4-cba1288dbea3' -or $ident.asset_id -cne 'a176520e-8a91-4e93-a78b-8e7620f430e0') {throw 'Original enrolled identity'} + $proc=Get-CimInstance Win32_Process -Filter ('ProcessId='+$service.ProcessId) -OperationTimeoutSec 10 + $owner=(Invoke-CimMethod -InputObject $proc -MethodName GetOwnerSid -OperationTimeoutSec 10).Sid + if ($owner -cne 'S-1-5-19' -or $proc.ExecutablePath -cne $agentBinary) {throw 'Actual agent LocalService process'} + return [ordered]@{process_id=$service.ProcessId;creation=$proc.CreationDate.ToUniversalTime().ToString('o');owner_sid=$owner;binary_sha256=(Hash $agentBinary);config_sha256=(Hash $agentConfig);config_acl_sha256=(Acl-Hash $agentConfig);agent_id=$ident.agent_id;asset_id=$ident.asset_id} +} +function Same-Agent($before,$after) { + foreach($field in @('process_id','creation','owner_sid','binary_sha256','config_sha256','config_acl_sha256','agent_id','asset_id')) {if ($before.$field -cne $after.$field) {throw 'No agent restart or identity/configuration mutation allowed'}} +} +function Peer([string]$service,[string]$sid,[string]$user,[string]$path,[string]$hash) { + return [ordered]@{service_name=$service;service_sid=$sid;user_sid=$user;binary=$path;binary_sha256=$hash} +} +function Configurations([string]$helperSid) { + $binding=[ordered]@{tenant_id=$p.binding.tenant_id;asset_id=$p.binding.asset_id;agent_id=$p.binding.agent_id;enrollment_generation=[long]$p.binding.enrollment_generation;assignment_version=[long]$p.binding.assignment_version;public_key_hex=$p.binding.public_key_hex} + $agent=Peer $agentName $agentSid 'S-1-5-19' $agentBinary $p.agent_sha256 + $helper=Peer $name $helperSid $sys $binary $p.helper_sha256 + $settings=[ordered]@{schema_version=1;binding=$binding;agent=$agent;helper=$helper;allowed_services=@();max_runtime_seconds=30;enable_effects=$false;allow_reboot=$false} + # JSON.NET/PowerShell may escape strings differently from serde. The policy + # hash comes from exact Python canonical settings bytes, verified below. + $expected=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($p.settings_base64)) | ConvertFrom-Json + $actual=$settings|ConvertTo-Json -Depth 24 -Compress + if (($expected|ConvertTo-Json -Depth 24 -Compress) -cne $actual) {throw 'Exact fixed passive public settings'} + $settingsBytes=[Convert]::FromBase64String($p.settings_base64) + if ((Sha $settingsBytes) -cne $p.policy_sha256) {throw 'Canonical public policy hash'} + $pin=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($p.pin_base64)) | ConvertFrom-Json + if ($pin.schema_version -ne 1 -or $pin.policy_sha256 -cne $p.policy_sha256 -or ($pin.binding|ConvertTo-Json -Compress) -cne ($binding|ConvertTo-Json -Compress) -or ($pin.agent|ConvertTo-Json -Compress) -cne ($agent|ConvertTo-Json -Compress) -or ($pin.helper|ConvertTo-Json -Compress) -cne ($helper|ConvertTo-Json -Compress)) {throw 'Exact reciprocal public pin'} + return $settingsBytes +} +function Read-Intent { + Check $folder $true @() @() + $intent=Get-Content -LiteralPath (Join-Path $folder 'intent.json') -Raw | ConvertFrom-Json + foreach($field in @('id','helper_sha256','agent_sha256','agent_acl_sha256','config_sha256','config_acl_sha256','source_sha256','policy_sha256','pin_sha256','helper_service_sid','settings_base64','pin_base64')) {if ($intent.payload.$field -cne $p.$field) {throw 'Exact existing passive helper intent'}} + $live=Get-Content -LiteralPath $active -Raw | ConvertFrom-Json + if ($live.id -cne $p.id) {throw 'Another helper bootstrap is active'} + return $intent +} +function Existing-State($intent) { + $agent=Agent-State;Same-Agent $intent.agent $agent + $service=Get-CimInstance Win32_Service -Filter "Name='RmmDiscoverySystemHelper'" -OperationTimeoutSec 10 + if ($null -eq $service) {return [ordered]@{service_exists=$false;agent=$agent;root_exists=(Test-Path -LiteralPath $root);binary_exists=(Test-Path -LiteralPath $binary);no_jobs=$true;native_pipe_peer_test=$false}} + $helperSid=Sid $name + if ((Get-ItemPropertyValue -LiteralPath ('HKLM:\SYSTEM\CurrentControlSet\Services\'+$name) -Name ServiceSidType) -ne 1) {throw 'Unrestricted own helper service SID required'} + if ($service.StartName -notin @('LocalSystem','NT AUTHORITY\SYSTEM') -or $service.PathName -cne $helperCommand -or $service.StartMode -cne 'Manual' -or $service.ServiceType -cne 'Own Process') {throw 'Fixed SYSTEM own-process service configuration'} + Check $root $true @($agentSid,$helperSid) @();Check $state $true @() @($helperSid) + foreach($path in @($settingsPath,$pinPath,$binary)) {Check $path $false @($agentSid,$helperSid) @()} + if ((Hash $binary) -cne $p.helper_sha256 -or (Hash $settingsPath) -cne $p.policy_sha256 -or (Hash $pinPath) -cne $p.pin_sha256) {throw 'Exact retained helper image and public policy'} + [void](Configurations $helperSid) + $owner=$null + if ($service.State -eq 'Running') { + $proc=Get-CimInstance Win32_Process -Filter ('ProcessId='+$service.ProcessId) -OperationTimeoutSec 10 + $owner=(Invoke-CimMethod -InputObject $proc -MethodName GetOwnerSid -OperationTimeoutSec 10).Sid + if ($owner -cne $sys -or $proc.ExecutablePath -cne $binary) {throw 'Actual own SYSTEM helper process'} + } + $ledger=Join-Path $state 'ledger.sqlite';$lock=Join-Path $state 'executor.lock' + foreach($path in @($ledger,$lock)) {if (Test-Path -LiteralPath $path) {Check $path $false @() @($helperSid)}} + $probePath='C:\ProgramData\RustDiscovery\state\helper-probe.json';$probe=$null + if (Test-Path -LiteralPath $probePath) { + $item=Get-Item -LiteralPath $probePath -Force + if ($item.PSIsContainer -or ($item.Attributes -band [IO.FileAttributes]::ReparsePoint) -or $item.Length -gt 8192) {throw 'Bounded current agent IPC witness'} + $acl=Get-Acl -LiteralPath $probePath + if ($acl.GetOwner([Security.Principal.SecurityIdentifier]).Value -notin @($sys,$admin,'S-1-5-19',$agentSid)) {throw 'Own agent IPC witness owner'} + foreach($rule in $acl.GetAccessRules($true,$true,[Security.Principal.SecurityIdentifier])) {if ($rule.AccessControlType -ne 'Allow' -or $rule.IdentityReference.Value -notin @($sys,$admin,'S-1-5-19',$agentSid)) {throw 'Own agent IPC witness DACL'}} + $probe=Get-Content -LiteralPath $probePath -Raw | ConvertFrom-Json + } + return [ordered]@{service_exists=$true;service_state=$service.State;process_id=$service.ProcessId;owner_sid=$owner;helper_service_sid=$helperSid;helper_sha256=(Hash $binary);policy_sha256=(Hash $settingsPath);pin_sha256=(Hash $pinPath);agent=$agent;enable_effects=$false;allow_reboot=$false;allowed_services=@();ledger_exists=(Test-Path -LiteralPath $ledger);executor_lock_exists=(Test-Path -LiteralPath $lock);private_state_acl_sha256=(Acl-Hash $state);agent_probe=$probe;no_jobs=$true;native_system_service_running=($service.State -eq 'Running');native_pipe_peer_test=$false;clock_adapter_qualified=$false} +} +Check $bootstrap $true @() @() +if ($p.binding.agent_id -cne '63ea15b2-0643-403d-b5b4-cba1288dbea3' -or $p.binding.asset_id -cne 'a176520e-8a91-4e93-a78b-8e7620f430e0' -or $p.binding.public_key_hex -notmatch '^[0-9a-f]{64}$' -or $p.binding.enrollment_generation -ne 1 -or $p.binding.assignment_version -le 0) {throw 'Public signing binding, never private enrollment material'} +if ($p.action -eq 'readback') { $intent=Read-Intent;Existing-State $intent | ConvertTo-Json -Depth 24 -Compress;exit 0 } +if ($p.action -eq 'install') { + if ((Test-Path -LiteralPath $folder) -or (Test-Path -LiteralPath $active) -or (Test-Path -LiteralPath $root) -or (Test-Path -LiteralPath $binary) -or $null -ne (Get-Service -Name $name -ErrorAction SilentlyContinue)) {throw 'Unknown existing helper state: readback only, never adopt or reset a ledger'} + Check 'C:\Program Files\RustDiscovery' $true @($agentSid) @() + $before=Agent-State + Check $uploaded $false @() @() + if ((Hash $uploaded) -cne $p.helper_sha256) {throw 'Exact qualified upload'} + # SID derivation is deterministic even before CreateService; no service effects + # are needed to prepare reciprocal configuration. + $helperSid=$p.helper_service_sid + if ($helperSid -notmatch '^S-1-5-80-([0-9]+-){4}[0-9]+$') {throw 'Fixed deterministic helper service SID'} + $settingsBytes=Configurations $helperSid + New-Item -ItemType Directory -Path $folder | Out-Null;Protect $folder $true @() @() + Write-New $active @{id=$p.id;source_sha256=$p.source_sha256;no_jobs=$true} + Write-New (Join-Path $folder 'intent.json') @{payload=$p;agent=$before;no_jobs=$true} + Write-New (Join-Path $folder 'install-attempt.json') @{install_once=$true;no_jobs=$true;enable_effects=$false} + # Every mutation follows the retained intent. No existing path is overwritten. + New-Item -ItemType Directory -Path $root | Out-Null;Protect $root $true @($agentSid,$helperSid) @() + New-Item -ItemType Directory -Path $state | Out-Null;Protect $state $true @() @($helperSid) + $source=[IO.File]::Open($uploaded,[IO.FileMode]::Open,[IO.FileAccess]::Read,[IO.FileShare]::Read) + try {$target=[IO.File]::Open($binary,[IO.FileMode]::CreateNew,[IO.FileAccess]::Write,[IO.FileShare]::Read);try {$source.CopyTo($target);$target.Flush($true)} finally {$target.Dispose()}} finally {$source.Dispose()} + Protect $binary $false @($agentSid,$helperSid) @() + foreach($pair in @(@($settingsPath,$p.settings_base64),@($pinPath,$p.pin_base64))) { + $bytes=[Convert]::FromBase64String($pair[1]);$file=[IO.File]::Open($pair[0],[IO.FileMode]::CreateNew,[IO.FileAccess]::Write,[IO.FileShare]::Read) + try {$file.Write($bytes,0,$bytes.Length);$file.Flush($true)} finally {$file.Dispose()} + Protect $pair[0] $false @($agentSid,$helperSid) @() + } + New-Service -Name $name -BinaryPathName $helperCommand -DisplayName 'RustDiscovery SYSTEM Helper' -StartupType Manual | Out-Null + $sc=Join-Path $env:WINDIR 'System32\sc.exe' + & $sc sidtype $name unrestricted | Out-Null;if ($LASTEXITCODE -ne 0) {throw 'Own helper service SID'} + if ((Sid $name) -cne $helperSid) {throw 'Actual SCM service SID differs from pin'} + $after=Existing-State @{agent=$before} + if ($after.service_state -cne 'Stopped' -or $after.ledger_exists -or $after.executor_lock_exists) {throw 'Installed but deliberately not yet started'} + Write-New (Join-Path $folder 'installed.json') $after + $after|ConvertTo-Json -Depth 24 -Compress;exit 0 +} +$intent=Read-Intent +if (!(Test-Path -LiteralPath (Join-Path $folder 'installed.json'))) {throw 'Complete exact installation receipt required'} +$before=Existing-State $intent +if ($before.service_state -cne 'Stopped' -or $before.ledger_exists -or $before.executor_lock_exists) {throw 'First passive start only; preserve any existing state'} +# Never replay Start-Service after a timeout/lost reply or service failure. +Write-New (Join-Path $folder 'start-attempt.json') @{start_once=$true;no_jobs=$true;enable_effects=$false;allow_reboot=$false} +Start-Service -Name $name +(Get-Service -Name $name).WaitForStatus([ServiceProcess.ServiceControllerStatus]::Running,[TimeSpan]::FromSeconds(20)) +$after=Existing-State $intent +if ($after.service_state -cne 'Running' -or !$after.ledger_exists -or !$after.executor_lock_exists) {throw 'Running own SYSTEM service with retained private ledger/lock required'} +Write-New (Join-Path $folder 'started.json') $after +$after|ConvertTo-Json -Depth 24 -Compress +''' + +PREPARE = base.PREPARE_SCRIPT.replace('TransportAcceptance.ps1', 'SystemHelperBootstrap.ps1') +LAUNCH = base.LAUNCH_SCRIPT.replace('TransportAcceptance.ps1', 'SystemHelperBootstrap.ps1').replace('RmmTransportPayload', 'RmmSystemHelperPayload') +UPLOAD_PREPARE = base.PREPARE_SCRIPT.replace('TransportAcceptance.ps1', 'rmm-helper-windows.exe') + + +def sha(path): + return hashlib.sha256(Path(path).read_bytes()).hexdigest() + + +def canonical(value): + return json.dumps(value, ensure_ascii=False, separators=(',', ':')).encode() + + +def store(folder, name, value): + path = folder / name + fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_WRONLY | os.O_NOFOLLOW, 0o600) + with os.fdopen(fd, 'wb') as out: + out.write(json.dumps(value, indent=2).encode());out.flush();os.fsync(out.fileno()) + return path + + +def private_dir(path): + path.mkdir(mode=0o700, parents=True, exist_ok=True) + require(not path.is_symlink() and path.stat().st_mode & 0o077 == 0, 'private bootstrap evidence directory') + + +def service_sid(name): + # MS-LSAT 3.1.1.1.2 Configurable Translation Database: + # https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-lsat/252cb6b0-2edc-4403-9d75-5b2786ba859e + # SERVICE SID: SHA-1 of uppercase UTF-16LE service name, five + # little-endian 32-bit subauthorities. Native LookupAccountName verifies it. + import struct + digest = hashlib.sha1(name.upper().encode('utf-16le')).digest() + return 'S-1-5-80-' + '-'.join(str(n) for n in struct.unpack('<5I', digest)) + + +def qualified_helper(artifact): + proof = cb.contract.protected_json(QUALIFICATION) + require(proof.get('verified') is True and proof.get('passed') == 21 and proof.get('binary_sha256') == UNIT_SHA and proof.get('no_jobs') is True and proof.get('native_system_helper_service_test') is False and proof.get('native_pipe_peer_test') is False, 'actual 21-unit qualification, no invented service/pipe proof') + require(sha(artifact) == HELPER_SHA, 'exact associated deployable helper artifact; unit executable is different') + for path, digest in OWN_SOURCES.items(): + require(proof['source_files'].get(path) == digest and sha(cb.ROOT / path) == digest, 'qualified helper codec/SCM sources unchanged') + return {'path': str(QUALIFICATION), 'sha256': sha(QUALIFICATION), 'unit_binary_sha256': UNIT_SHA, 'helper_binary_sha256': HELPER_SHA} + + +def current_runtime(): + index_path = cb.EVIDENCE / 'current-runtime.json' + index = cb.contract.protected_json(index_path) + require(index.get('type') == 'rmm.windows.current-runtime' and index.get('schema_version') == 1 and index.get('agent_id') == AGENT_ID and index.get('asset_id') == ASSET_ID and index.get('transport') == 'auto' and index.get('windows_jobs_enabled') is False and index.get('clock_adapter_qualified') is False, 'accepted retained passive runtime index') + evidence = Path(index['evidence']['path']).resolve() + require(evidence.is_relative_to(cb.EVIDENCE.resolve()) and sha(evidence) == index['evidence']['sha256'], 'exact current-runtime evidence') + result = cb.contract.protected_json(evidence) + require(result.get('verified') is True and result.get('completed') is True and result.get('restored') is False and result.get('no_jobs') is True, 'actual accepted current runtime') + require(index.get('system_helper_client_feature') is True and result.get('system_helper_client_feature') is True, 'accepted optional IPC client must precede helper install; old passive f64 is not an IPC client') + native = result['native'] + require(native['binary_sha256'] == index['binary_sha256'] and re.fullmatch('[0-9a-f]{64}', native['config_sha256']), 'actual current binary/configuration association') + return index, {'path': str(index_path), 'sha256': sha(index_path), 'evidence': index['evidence']}, native + + +def binding_file(path): + value = cb.contract.protected_json(path) + fields = ('tenant_id','asset_id','agent_id','enrollment_generation','assignment_version','public_key_hex') + require(set(value) == set(fields) and value['agent_id'] == AGENT_ID and value['asset_id'] == ASSET_ID and value['enrollment_generation'] == 1 and type(value['assignment_version']) is int and value['assignment_version'] > 0 and re.fullmatch('[0-9a-f]{64}', value['public_key_hex']), 'public signing binding, never tokens or private keys') + require(str(uuid.UUID(value['tenant_id'])) == value['tenant_id'] and uuid.UUID(value['tenant_id']).int != 0, 'explicit tenant binding') + return {field:value[field] for field in fields} + + +def configurations(binding, agent_sha): + helper_sid = service_sid('RmmDiscoverySystemHelper') + agent = dict(service_name='RmmDiscoveryWindows',service_sid=service_sid('RmmDiscoveryWindows'),user_sid='S-1-5-19',binary=r'C:\Program Files\RustDiscovery\rmm-agent-windows.exe',binary_sha256=agent_sha) + helper = dict(service_name='RmmDiscoverySystemHelper',service_sid=helper_sid,user_sid='S-1-5-18',binary=r'C:\Program Files\RustDiscovery\rmm-helper-windows.exe',binary_sha256=HELPER_SHA) + settings = dict(schema_version=1,binding=binding,agent=agent,helper=helper,allowed_services=[],max_runtime_seconds=30,enable_effects=False,allow_reboot=False) + raw = canonical(settings);policy = hashlib.sha256(raw).hexdigest() + pin = dict(schema_version=1,binding=binding,policy_sha256=policy,agent=agent,helper=helper) + return raw, canonical(pin), helper_sid, policy + + +def transfer(path, name, digest, preparation): + status = cb.powershell(preparation, {'sha256':digest}) + if not status['already_uploaded']: + prefix = cb.contract.protected_json(cb.ROOT / 'credentials/windows-dev-access.json')['ssh_prefix'] + args = ['sftp'] + [part for part in prefix[1:-1] if part != '-T'] + ['-b','-',prefix[-1]] + target = '/C:/ProgramData/RustDiscoveryBootstrap/' + digest + '/' + name + result = subprocess.run(args,input=('put '+json.dumps(str(path.resolve()))+' '+json.dumps(target)+'\n').encode(),capture_output=True,timeout=120) + require(result.returncode == 0, 'fixed bootstrap SFTP failed') + require(cb.powershell(preparation, {'sha256':digest})['already_uploaded'], 'exact immutable staged artifact') + return digest + + +def prepare(artifact, binding_path): + # All inputs checked and durable locally before the first remote mutation. + qualification = qualified_helper(artifact) + index, runtime, native = current_runtime() + binding = binding_file(binding_path) + raw, pin, helper_sid, policy = configurations(binding, index['binary_sha256']) + import base64 + ident = str(uuid.uuid4());private_dir(STATE);folder = STATE / ident;folder.mkdir(mode=0o700) + source_sha = hashlib.sha256(SCRIPT.encode()).hexdigest() + payload = dict(id=ident,helper_sha256=HELPER_SHA,agent_sha256=index['binary_sha256'],agent_acl_sha256=native['binary_acl_sha256'],config_sha256=native['config_sha256'],config_acl_sha256=native['config_acl_sha256'],source_sha256=source_sha,binding=binding,helper_service_sid=helper_sid,policy_sha256=policy,pin_sha256=hashlib.sha256(pin).hexdigest(),settings_base64=base64.b64encode(raw).decode(),pin_base64=base64.b64encode(pin).decode()) + store(folder,'intent.json',dict(schema_version=1,type='rmm.windows.passive-system-helper',payload=payload,qualification=qualification,current_runtime=runtime,source_file_sha256=sha(__file__),no_jobs=True,helper_enable_effects=False,allow_reboot=False)) + source = folder / 'SystemHelperBootstrap.ps1' + fd = os.open(source, os.O_CREAT|os.O_EXCL|os.O_WRONLY|os.O_NOFOLLOW, 0o600) + with os.fdopen(fd,'wb') as out:out.write(SCRIPT.encode());out.flush();os.fsync(out.fileno()) + store(folder,'upload-attempt.json',{'helper_sha256':HELPER_SHA,'source_sha256':source_sha}) + transfer(artifact, 'rmm-helper-windows.exe', HELPER_SHA, UPLOAD_PREPARE) + transfer(source, 'SystemHelperBootstrap.ps1', source_sha, PREPARE) + store(folder,'prepared.json',{'prepared':True,'source_sha256':source_sha,'helper_sha256':HELPER_SHA,'no_service_changes':True}) + return {'case_id':ident,'prepared':True,'no_service_changes':True} + + +def run(ident, action): + require(str(uuid.UUID(ident)) == ident, 'canonical bootstrap case') + folder = STATE / ident + intent = cb.contract.protected_json(folder / 'intent.json') + require(intent['source_file_sha256'] == sha(__file__) and intent['payload']['source_sha256'] == hashlib.sha256(SCRIPT.encode()).hexdigest(), 'exact prepared runner/source; never silently rebase case') + require(cb.contract.protected_json(folder / 'prepared.json')['prepared'] is True, 'completed source/artifact upload') + index_path = Path(intent['current_runtime']['path']) + require(sha(index_path) == intent['current_runtime']['sha256'], 'accepted runtime index unchanged during bootstrap') + payload = dict(intent['payload'], action='readback' if action == 'verify-peer' else action) + if action not in ('readback','verify-peer'): + store(folder, action+'-attempt.json', {'action_once':action,'source_sha256':payload['source_sha256'],'no_jobs':True}) + else: + # Immutable readbacks can be repeated safely with fresh evidence UUIDs. + store(folder,'readback-intent-'+str(uuid.uuid4())+'.json',{'read_only':True,'no_jobs':True}) + result = cb.powershell(LAUNCH, {'sha256':payload['source_sha256'],'payload':payload}, timeout=90, remote_signed=True) + store(folder,action+'-result-'+str(uuid.uuid4())+'.json',result) + if action == 'verify-peer': + verify_peer(result, payload) + receipt={'verified':True,'case_id':ident,'native_system_helper_service_test':True,'native_pipe_peer_test':True,'source':'actual_local_service_runtime','agent_process_id':result['agent']['process_id'],'helper_process_id':result['process_id'],'challenge':result['agent_probe']['challenge'],'policy_sha256':payload['policy_sha256'],'helper_sha256':HELPER_SHA,'clock_adapter_qualified':False,'system_capability_advertised':False,'no_jobs':True,'native':result} + store(folder,'peer-verified-'+str(uuid.uuid4())+'.json',receipt) + return receipt + return {'case_id':ident,'action':action,'native':result,'no_jobs':True,'native_pipe_peer_test':False} + + +def verify_peer(result, payload): + """The SSH caller only reads a fresh receipt written by the real service. + + PeerProof/require_self inside the accepted Agent SDK and helper bind kernel + pipe PIDs, SCM PIDs, users/service SIDs, creation/aliveness and pinned images. + An Admin process that directly opens the pipe cannot emit this success. + """ + from datetime import datetime, timezone + require(result.get('native_system_service_running') is True and result.get('owner_sid') == 'S-1-5-18' and result.get('enable_effects') is False and result.get('allow_reboot') is False and result.get('allowed_services') == [] and result.get('ledger_exists') is True and result.get('executor_lock_exists') is True, 'actual passive SYSTEM helper with private persistent files') + probe=result.get('agent_probe') or {} + require(probe.get('schema_version') == 1 and probe.get('source') == 'actual_local_service_runtime' and probe.get('agent_process_id') == result['agent']['process_id'] and probe.get('agent_service') == 'RmmDiscoveryWindows' and probe.get('agent_id') == AGENT_ID and probe.get('asset_id') == ASSET_ID, 'actual current LocalService agent wrote the pipe receipt') + require(probe.get('authenticated_pipe_exchange') is True and probe.get('client_service_pid_verified_by_helper') is True and probe.get('verified_helper_user_sid') == 'S-1-5-18' and probe.get('verified_helper_service_sid') == payload['helper_service_sid'] and probe.get('helper_binary_sha256') == HELPER_SHA and probe.get('policy_sha256') == payload['policy_sha256'], 'reciprocal exact role/image/policy pipe proof') + require(probe.get('helper_clock_qualified') is False and probe.get('helper_ready') is False and probe.get('system_capability_advertised') is False and probe.get('no_jobs') is True, 'pipe connectivity cannot open the clock or a job capability') + observed=datetime.fromisoformat(probe['observed_at'].replace('Z','+00:00')) + # Freshness here is only acceptance telemetry, never a trusted job clock. + require(0 <= (datetime.now(timezone.utc)-observed).total_seconds() <= 90, 'fresh actual agent receipt; wait for normal periodic probe') + require(str(uuid.UUID(probe['challenge'])) == probe['challenge'], 'actual challenge UUID') + for name in ('ledger_file_id','executor_lock_file_id'): + value=probe.get(name) or {} + require(type(value.get('volume_serial')) is int and value['volume_serial'] > 0 and isinstance(value.get('file_id'),list) and len(value['file_id']) == 16 and all(type(n) is int and 0 <= n <= 255 for n in value['file_id']) and any(value['file_id']), 'actual helper-held private Windows file identity') + + +def main(): + parser=argparse.ArgumentParser(description=__doc__) + parser.add_argument('action',choices=['prepare','install','start','readback','verify-peer']) + parser.add_argument('--artifact',type=Path) + parser.add_argument('--public-binding',type=Path) + parser.add_argument('--case') + args=parser.parse_args() + if args.action == 'prepare': + require(args.artifact is not None and args.public_binding is not None, 'prepare requires exact artifact and protected PUBLIC binding') + result=prepare(args.artifact,args.public_binding) + else: + require(args.case is not None, 'explicit existing bootstrap case required') + result=run(args.case,args.action) + print(json.dumps(result)) + + +if __name__ == '__main__': + main() diff --git a/deploy/windows/test_runtime_feature_upgrade.py b/deploy/windows/test_runtime_feature_upgrade.py new file mode 100644 index 0000000..52d196d --- /dev/null +++ b/deploy/windows/test_runtime_feature_upgrade.py @@ -0,0 +1,49 @@ +"""Focused source-only guards for the new passive feature-upgrade namespace.""" +from pathlib import Path +import tempfile +import unittest +from unittest.mock import patch +import uuid + +import runtime_feature_upgrade as feature + + +class FeatureUpgradeGuards(unittest.TestCase): + def test_historical_source_derivation_is_exact_and_owns_new_namespace(self): + source=feature.UPGRADE + self.assertIn("$p.old_sha256 -cne '"+feature.OLD+"'",source) + self.assertNotIn("$p.old_sha256 -cne '"+feature.ORIGINAL+"'",source) + self.assertIn("\\runtime-feature-active-intent.json'",source) + self.assertNotIn("\\runtime-active-intent.json'",source) + self.assertIn("\\runtime-feature-'+$p.id",source) + self.assertIn('Complete passive feature upgrade before any helper/pin installation',source) + self.assertIn('Original six-field passive configuration',source) + with self.assertRaises(Exception):feature.replace_once('same same','same','changed') + + def test_old_default_profile_cannot_claim_new_feature_qualification(self): + with self.assertRaises(Exception): + feature.stable(dict(schema_version=1,architecture=feature.TARGET,default_features=True,system_helper_feature=False,system_helper_client_feature=False),current_sources=False) + + def test_restore_then_finish_cannot_publish_historical_new_runtime(self): + with tempfile.TemporaryDirectory() as temporary: + folder=Path(temporary);ident=str(uuid.uuid4()) + feature.write(folder,'rollback-intent.json',{'started_at':'2026-11-14T00:00:00+00:00'}) + feature.write(folder,'before.json',{'fixture':True}) + manifest={'artifacts':{'agent':{'sha256':'11'*32}}} + native={'completed':True,'read_only':True,'restored':False,'native':{'binary_sha256':'11'*32}} + with patch.object(feature,'load_case',return_value=(folder,{},manifest)),patch.object(feature,'remote',return_value=native),patch.object(feature,'publish') as publish: + with self.assertRaises(Exception):feature.finish(ident) + publish.assert_not_called() + + def test_current_runtime_cas_rejects_another_transition(self): + with tempfile.TemporaryDirectory() as temporary: + folder=Path(temporary);current=folder/'index.json' + feature.write(folder,'index.json',{'case_id':str(uuid.uuid4())}) + feature.write(folder,'result.json',{'verified':True}) + with patch.object(feature,'CURRENT',current): + with self.assertRaises(Exception): + feature.publish(folder,'result.json',{'previous_runtime_index_sha256':'00'*32},'11'*32,False) + self.assertEqual(set(path.name for path in folder.iterdir()),{'index.json','result.json'}) + + +if __name__=='__main__':unittest.main() diff --git a/deploy/windows/test_system_helper_bootstrap.py b/deploy/windows/test_system_helper_bootstrap.py new file mode 100644 index 0000000..0c8e51a --- /dev/null +++ b/deploy/windows/test_system_helper_bootstrap.py @@ -0,0 +1,56 @@ +"""Focused local bootstrap guards. No SSH, service or RMM job is executed.""" +import copy +from datetime import datetime, timezone, timedelta +import hashlib +from pathlib import Path +import tempfile +import unittest +from unittest.mock import patch +import uuid + +import system_helper_bootstrap as helper + + +class BootstrapGuards(unittest.TestCase): + def test_passive_public_policy_uses_exact_roles_and_empty_effect_allowlist(self): + import json + binding=dict(tenant_id=str(uuid.uuid4()),asset_id=helper.ASSET_ID,agent_id=helper.AGENT_ID,enrollment_generation=1,assignment_version=1,public_key_hex='11'*32) + raw,pin,sid,policy=helper.configurations(binding,'22'*32) + settings=json.loads(raw) + self.assertEqual(settings['allowed_services'],[]) + self.assertIs(settings['enable_effects'],False) + self.assertIs(settings['allow_reboot'],False) + self.assertEqual(settings['agent']['user_sid'],'S-1-5-19') + self.assertEqual(settings['helper']['user_sid'],'S-1-5-18') + self.assertEqual(settings['helper']['service_sid'],sid) + self.assertEqual(hashlib.sha256(raw).hexdigest(),policy) + self.assertEqual(json.loads(pin)['policy_sha256'],policy) + # MS-LSAT documented ALG vector confirms ordering and byte endianness. + self.assertEqual(helper.service_sid('ALG'),'S-1-5-80-2387347252-3645287876-2469496166-3824418187-3586569773') + + def test_lost_install_reply_cannot_replay_mutation(self): + with tempfile.TemporaryDirectory() as temporary: + state=Path(temporary);ident=str(uuid.uuid4());folder=state/ident;folder.mkdir(mode=0o700) + index=helper.store(state,'index.json',{'fixture':True}) + helper.store(folder,'intent.json',{'source_file_sha256':helper.sha(helper.__file__),'payload':{'id':ident,'source_sha256':hashlib.sha256(helper.SCRIPT.encode()).hexdigest()},'current_runtime':{'path':str(index),'sha256':helper.sha(index)}}) + helper.store(folder,'prepared.json',{'prepared':True}) + with patch.object(helper,'STATE',state),patch.object(helper.cb,'powershell',side_effect=RuntimeError('simulated lost SSH reply')) as rpc: + with self.assertRaisesRegex(RuntimeError,'simulated lost'): + helper.run(ident,'install') + with self.assertRaises(FileExistsError): + helper.run(ident,'install') + self.assertEqual(rpc.call_count,1) + self.assertTrue((folder/'install-attempt.json').exists()) + + def test_admin_or_stale_or_ready_receipt_is_not_positive_pipe_acceptance(self): + payload={'helper_service_sid':helper.service_sid('RmmDiscoverySystemHelper'),'policy_sha256':'33'*32} + probe=dict(schema_version=1,source='actual_local_service_runtime',agent_process_id=100,agent_service='RmmDiscoveryWindows',agent_id=helper.AGENT_ID,asset_id=helper.ASSET_ID,authenticated_pipe_exchange=True,client_service_pid_verified_by_helper=True,verified_helper_user_sid='S-1-5-18',verified_helper_service_sid=payload['helper_service_sid'],helper_binary_sha256=helper.HELPER_SHA,policy_sha256=payload['policy_sha256'],helper_clock_qualified=False,helper_ready=False,system_capability_advertised=False,no_jobs=True,observed_at=datetime.now(timezone.utc).isoformat(),challenge=str(uuid.uuid4()),ledger_file_id={'volume_serial':1,'file_id':[1]*16},executor_lock_file_id={'volume_serial':1,'file_id':[2]*16}) + result=dict(native_system_service_running=True,owner_sid='S-1-5-18',enable_effects=False,allow_reboot=False,allowed_services=[],ledger_exists=True,executor_lock_exists=True,agent={'process_id':100},agent_probe=probe) + helper.verify_peer(result,payload) + for field,value in [('source','administrator_ssh_probe'),('agent_process_id',999),('helper_ready',True),('helper_clock_qualified',True),('policy_sha256','44'*32),('observed_at',(datetime.now(timezone.utc)-timedelta(seconds=100)).isoformat())]: + bad=copy.deepcopy(result);bad['agent_probe'][field]=value + with self.assertRaises(Exception,msg=field):helper.verify_peer(bad,payload) + + +if __name__ == '__main__': + unittest.main() diff --git a/docs/handover.md b/docs/handover.md index 731102d..ed362b4 100644 --- a/docs/handover.md +++ b/docs/handover.md @@ -52,6 +52,16 @@ Zugänge und private Rohbelege bleiben gitignoriert. ## Aktuelle Windowsfortsetzung +Neuester Windowsruntimeindex: Agent `2beed7fa…`, tatsächlicher LocalService +PID5140, Featurefall `cea4d30f…`, davor neue native21+6-Unitqualifikation +`23b0e0aa…`. Gleiche Identität, sechs Konfigurationsfelder, Telemetrie-DB und +Schlüsselmetadaten erhalten; WSS/Heartbeat/ACK/Tenantprobe bestanden. Timeout +im ersten Nur-Lese-Readback ausschließlich durch Lesen desselben Falls geklärt, +kein zweiter Start. Optionaler Helperclient kompiliert/aktiv vorhanden, kein +Signingbinding beim Netzwerkagenten, Jobs/Uhr geschlossen, keine SYSTEM-Caps +oder Execute-Route. Pipe-/Helpernachweis folgt separat. Die unten genannten +`f64e8115…`- und älteren Stände bleiben historische Abnahmen. + Aktueller Linux-Dienststand nach Shared-Core-Fortsetzung: Agent `c9c09de0…`, Helper `51c3142a…`, Provider/Units/Policies unverändert. Eigener Fall `a2423612…` hat beide Binärdateien erhaltend installiert; ein getrennter Nur-Lese-Nachbeleg diff --git a/docs/implementation-status.md b/docs/implementation-status.md index 3ab21dd..8c191b8 100644 --- a/docs/implementation-status.md +++ b/docs/implementation-status.md @@ -1,5 +1,35 @@ # Tatsächlicher Implementierungsstand +## Optionaler Windows-Helperclient nativ qualifiziert und ausgerollt — 2026-11-14 + +Neuer57-Quellen-Featurebuild und eigene native Qualifikation +`23b0e0aa-d5c5-433b-a025-53293d86c1a0` bestanden:21 Library- und6 Binarytests, +keine Skips. Danach erhaltender Dienstwechsel `cea4d30f…` auf Agent `2beed7fa…`: +tatsächlich LocalService PID5140, Auto/WSS, zwei frische Heartbeats und konkretes +Inventar-ACK. Gleiche Geräte-/Zertifikats-/Konfigurations-/ACL- und DB-Identität, +Schlüsselmetadaten erhalten. Windows0/0/0/0 und Linux30/30/26/0, Fremdtenant +verweigert. Nach einem Timeout des ersten Nur-Lese-Readbacks wurde ausschließlich +derselbe Fall gelesen; weder Installation noch Dienststart wiederholt. + +Der Client bindet einen optionalen geschützten festen Pin ohne Änderung der +sechs bisherigen Konfigurationsfelder. Probe, Snapshot und Original-Apply +verwenden getrennte Taskzustände und eine einzige begrenzte Blocking-Lane; +die Session wartet nicht auf laufende Pipearbeit. Unabhängiger Quellreview, +Default-/Feature-Clippy und sieben fokussierte Bootstrap-/Runnerguards bestanden. +Die Frist für Frame-I/O ist keine harte Frist sämtlicher synchroner Windows- +API-Aufrufe. Das neue Flag `system_helper_client_feature=true` beweist den +ausgerollten Client, noch keine Pipe-/SYSTEM-/Signed-Sync-Abnahme. + +Jobs und Uhr bleiben geschlossen; keine Execute-Route oder SYSTEM-Capability. +Native Provider für Eventlog und MSI1.0 sind separat quellimplementiert und +in eigenen Harnesses lokal geprüft, noch unverdrahtet und ohne UI-Ausführung. +Die erste echte MSI-Erzeugung scheiterte im Authoring, ohne Produktinstallation; +Fehlerursache wird unter erhaltenen Fällen eingegrenzt. Native MSVC-Tools fehlen +auf der VM; der Builder und eine ABI-/SEH-Probe sind vorbereitet, kein nativer +MSVC- oder signierter Installernachweis. Einzelheiten: +[Feature-Dienstwechsel](windows-feature-runtime-upgrade.md), +[Helperbootstrap](windows-system-helper-bootstrap.md). + ## Gemeinsamer Kern im tatsächlichen Linux-Dienstbetrieb nachgewiesen — 2026-11-14 Agent `c9c09de0…` und Generalhelper `51c3142a…` laufen jetzt auf der Cloud- diff --git a/docs/windows-clock-readiness.md b/docs/windows-clock-readiness.md index a827180..b345ae3 100644 --- a/docs/windows-clock-readiness.md +++ b/docs/windows-clock-readiness.md @@ -189,7 +189,7 @@ Der entsprechende neue native Durchlauf ist noch offen. ## Implementierter nächster Kandidat: ALPC mit fester Server-SID -`NativeClock::new()` verwendet im aktuellen Quellstand ausschließlich einen +Der zuvor vorbereitete V3-Kandidat verwendete ausschließlich einen lokal per exakter Interface-UUID/Version gefundenen `ncalrpc`-Endpunkt. Die verlangte Server-SID wird lokal als LocalService`S-1-5-19` erzeugt und mittels `RPC_SECURITY_QOS_V3.Sid` und `MUTUAL_AUTH` fest gebunden. WINNT/PacketPrivacy, @@ -227,3 +227,38 @@ für diese Readinessstrecke nicht gestartet oder umkonfiguriert. Ein entsprechen eng begrenzter Start des bestehenden Dienstes bleibt bis zur hierfür vom Root-Agenten angefragten Benutzerentscheidung ausgesetzt. Der neue ALPC-/SID-Kandidat wurde deshalb noch nicht nativ ausgeführt. + +## Weiterer Quellkandidat: zusätzlich getrennte W32Time-Dienstidentität + +Die Kontobindung an LocalService allein trennt W32Time nicht vom Netzwerkagenten, +der ebenfalls SID19 verwendet. Sie darf daher noch keine unabhängige Helper- +Clockqualifikation begründen. Der aktuelle Kandidat verwendet ergänzend die +lokal ermittelte feste `NT SERVICE\W32Time`-SID in einem ausschließlich +clientseitig aufgebauten V5-Serverdescriptor. SID19, PacketPrivacy, Identify und +ALPC bleiben erforderlich; keinerlei schwächerer Fallback. Das ändert keine +OS-DACL, W32Time-Konfiguration oder Kontorichtlinie. + +Die Readinessprobe enthält zwei getrennte begrenzte Nur-Lese-Negativkontrollen: +falsches Serverkonto und falsche Dienst-SID bei demselben erwarteten Konto19. +Nur eine erfolgreiche echte W32Time-Abfrage zusammen mit tatsächlichen +Zugriffsverweigerungen beider Kontrollen kann diesen Kandidaten weiter +qualifizieren. Fehlender Endpoint oder Timeout ist kein Identitätsnachweis. +Ein separater einzelner Diagnoseworker begrenzt auch diese Kontrollen; Timeout +vergiftet ihn ohne Ersatz. Beide Kontrollen bleiben immer diagnostisch und +verleihen selbst bei unerwartetem Erfolg keine Uhrbereitschaft. + +Microsoft beschreibt das Descriptorfeld als Serveridentifikation, aber nicht +alle Einzelheiten seiner Auswertung. Die Dienstbindung ist deshalb ein zu +prüfender Adapterkandidat, keine aus einem erfolgreichen Konfigurationsaufruf +abgeleitete Sicherheitsgarantie. Vor Öffnen des Gates sind seine tatsächlichen +Positiv-/Negativnachweise und die Isolation vom Netzwerkagenten nachzuweisen. +[V5-SDK-Vertrag](https://learn.microsoft.com/en-us/windows/win32/api/rpcdce/ns-rpcdce-rpc_security_qos_v5_w). + +Lokal tatsächlich geprüft: C-Kandidat mit Clang/WindowsGNU und `-Werror` +kompiliert, feste56Byte-V5-ABI/SDK-Prefix-/48Byte-Descriptoroffset-Assertions +bestanden, fünf Clock-Vertragtests gegen den tatsächlichen Core unter Linux +bestanden. Ältere MinGW-Header enthalten V5 nicht; die eng benannte lokale +SDK-Struktur bleibt aufx64 begrenzt und wird beim MSVC-Build zusätzlich direkt +gegen die native SDK-Deklaration geprüft. Dies ersetzt weder einen nativen +MSVC- noch einen tatsächlichen W32Time-Nachweis. W32Time ist weiterhin gestoppt; +Produktgate unverändert `ADAPTER_QUALIFIED=false`. diff --git a/docs/windows-feature-runtime-upgrade.md b/docs/windows-feature-runtime-upgrade.md new file mode 100644 index 0000000..e2dce3d --- /dev/null +++ b/docs/windows-feature-runtime-upgrade.md @@ -0,0 +1,84 @@ +# Optionale IPC-Clientfunktion identitätserhaltend ausrollen + +Stand 2026-11-14: Der neue Runner +`deploy/windows/runtime_feature_upgrade.py` ist **implementiert, neu nativ +qualifiziert und auf der bestehenden Windows-VM erhaltend ausgeführt**. +`runtime_upgrade.py` und seine abgeschlossenen historischen Fälle bleiben +unverändert. Die frühere native Default-/Helperunit-Abnahme ersetzt nicht die +neue Featureabnahme. + +Der Runner erlaubt ausschließlich den belegten Übergang vom bisherigen passiven +Agentbinary `f64e81155a4d86089a9dd7e6ec42ffba50b939442386c3187a28769a4de1aa0b` +zu einem neuen Build mit `system-helper`. Er liest den maßgeblichen +`current-runtime.json` samt unveränderlichem tatsächlichem Upgradebeleg und +vergleicht vor der ersten Wirkung Image, öffentliche Identität, Konfiguration, +ACLs und Metadaten des unexportierten Geräteschlüssels. Die ursprüngliche +Sechsfeldkonfiguration und automatische WSS-/HTTPS-Auswahl bleiben erhalten. +Helper, Pin, Jobs, Uhr- oder Serverpolicy werden dabei nicht installiert/aktiviert. + +Der neue Quell-/Artefaktmanifest bindet auch `helper_runtime`, `helper_ipc`, +`helper_system`, die Helperbinaryquelle und den unabhängigen Shared-Core-Helper. +`build-local` verlangt die weiterhin geschlossene Produktionsuhr, baut offline +und führt Clippy mit `-D warnings` aus. Der Eltern-Orchestrator führt anschließend +`qualify-native` aus: Alle genau 21 neu gebauten Bibliotheks- und sechs Binarytests +müssen auf der Windows-VM bestehen. Die tatsächlichen Artefakthashes und die +Windows-/Linux-Jobzähler sowie Policy-/Faultflags werden davor und danach +verglichen. Das sind Administrator-Bootstrap-Units, kein laufender SYSTEM-Dienst +und kein positiver Pipebeleg. + +Die native Upgradephase benutzt einen eigenen create-only Namensraum +`runtime-feature-{UUID}` mit `runtime-feature-active-intent.json`. Sie überschreibt +den alten V1-Intent nicht. Das feste PowerShellverfahren wird mit engen, +eindeutigen Ersetzungen aus der erhaltenen V1-Quelle abgeleitet. Es erhält +Datenbankidentität, ursprüngliche Tabellen, unbestätigte Telemetriebytes, +Sequenzkontinuität, Geräte-/Zertifikats-/CAidentität und ACLs. Vor Stop, Austausch +und Start stehen dauerhafte Intents. Nach einem unklaren SSH-Ausgang wird +`enter` nicht wiederholt; `finish` liest den tatsächlichen Zustand, ein Rollback +ist eine gesonderte ausdrückliche Aktion mit eigenem einmaligem Startversuch. + +Erst nach zwei tatsächlichen WSS-Heartbeats, bestätigter Inventarübertragung, +Mandantengegenprobe und unveränderten Job-/Servergrenzen schreibt der Runner +`system_helper_client_feature=true` in Ergebnis und aktuellen Runtimeindex. +Der Indexwechsel prüft den erwarteten vorherigen Hash oder denselben bestehenden +Fall; ein fremder Übergang wird nicht überschrieben. Nach Rollback darf ein +historisches Feature-Ergebnis nicht als aktueller Zustand veröffentlicht werden. +Alle Finish-Aufrufe lesen das tatsächliche ausgewählte Image erneut. + +Phasenfolge; tatsächliche Ergebnisse dieses Durchlaufs unten: + +1. `runtime_feature_upgrade.py build-local` +2. `runtime_feature_upgrade.py qualify-native --manifest ` +3. `runtime_feature_upgrade.py upgrade-passive --manifest --qualification ` +4. Bei verlorenem Reply nur `finish --id `; eine ausdrücklich + gewünschte Wiederherstellung verwendet `rollback --id `. +5. Erst danach [den passiven SYSTEM-Helper installieren und die echte + LocalService-Pipeverbindung nachweisen](windows-system-helper-bootstrap.md). + +Tatsächlich lokal ausgeführt: Python-Syntaxprüfung, PowerShell-Parser aller drei +abgeleiteten Quellstrings und vier gezielte Guards bestanden. Diese prüfen +exakte/eindeutige Ableitung, getrennten Namensraum, Ablehnung des alten +Defaultprofils als Featurequalifikation, aktuelle Zustandsprüfung nach Rollback +und CAS-Ablehnung eines fremden Runtimeindex. Keine VM-/Serveraktion und kein +RMM-Job wurde durch diese lokalen Prüfungen ausgelöst. + +## Tatsächlich auf der Windows-VM ausgeführt + +Neuer Buildmanifest `923aa4e8-d1e7-43fb-bffb-6450b2fe233f` bindet57 Quellen. +Lokaler Offline-Releasebuild und Clippy bestanden. Neuer eigener nativer Fall +`23b0e0aa-d5c5-433b-a025-53293d86c1a0`: alle21 Library- und6 Binarytests +bestanden, ohne Skip, als Bootstrapadministrator. Unverdrahtete MSI-/Eventlog- +Quellen sind dadurch nicht als native Provider qualifiziert. + +Anschließend Fall `cea4d30f-06a1-4f80-b483-f8b1bb6b0899`: neues Agentimage +`2beed7fa…`, tatsächlicher LocalService-PID5140, Auto/WSS, ursprüngliche +Konfiguration/ACLs/Identität/Schlüsselmetadaten/Telemetrie-DB erhalten. Zwei +frische Heartbeats, konkretes Inventar-ACK und Fremdtenant-Verweigerung bestanden; +Windows0/0/0/0 und Linux30/30/26/0 erhalten. Der erste nachgelagerte Nur-Lese- +SSH-Aufruf lief in ein Timeout; ausschließlich `finish` desselben Falls wurde +wiederholt, kein zweiter Dienststart oder Binärtausch. Der aktuelle Index bindet +den tatsächlichen verifizierten Abschluss. + +`system_helper_client_feature=true` bezeichnet den kompilierten optionalen +Client, keine SYSTEM-Aktionsfreigabe. Uhr und Jobs bleiben geschlossen; noch +kein Signingbinding/Verbrauchsledger/Jobsverzeichnis beim Netzwerkagenten. +Die tatsächliche Dienst-Pipeverbindung ist ein getrennter nächster Nachweis. diff --git a/docs/windows-system-helper-bootstrap.md b/docs/windows-system-helper-bootstrap.md new file mode 100644 index 0000000..be200fb --- /dev/null +++ b/docs/windows-system-helper-bootstrap.md @@ -0,0 +1,90 @@ +# Passiver SYSTEM-Helper: Installation und echter Agent-IPC-Nachweis + +Stand 2026-11-14: Die folgende Strecke ist **implementiert und lokal +quellqualifiziert, noch nicht auf der Windows-VM ausgeführt**. Die bisherige +native Abnahme von 21 Helper-/Bibliothekstests bleibt ein Unit-Nachweis aus dem +Administrator-Bootstrap; sie belegt weder einen laufenden SYSTEM-Dienst noch eine +erfolgreiche Pipeverbindung des echten LocalService-Agenten. + +Der optionale Build `system-helper` erkennt ausschließlich die geschützte Datei +`C:\ProgramData\RustDiscoverySystem\agent-pin.json`. Die bestehende sechsstellige +Agentkonfiguration bleibt erhalten. Fehlt der Pin, bleibt der Client passiv. +Ungültige, entfernte oder während einer Operation geänderte Pins schließen die +Sync-/Startgrenze. Die öffentliche Signierbindung im Pin ermöglicht keine +Enrollment-/Jobfreigabe und aktiviert nicht `enable_jobs`. + +Die Probe wird im tatsächlichen Agentdienst ausgeführt. Eine gemeinsame, einzelne +Blocking-Lane begrenzt die Pipeaufrufe auf fünf Sekunden; Probe, Snapshot und +Original-Apply haben separate Taskzustände. Die Session holt nur abgeschlossene +Tasks ab und bedient währenddessen Heartbeats und Nachrichten. Der Pin wird vor +und nach der authentifizierten Operation erneut geprüft. Snapshot und Apply +verwenden die bestehenden unabhängigen Helper-/Agent-Sync-Kerne mit expliziter +Identität `system`, unveränderten signierten Entscheidungsbytes und dem +ursprünglichen protobuf-SyncRequest. Erst `confirm_helper` bestätigt den genauen +angewendeten Checkpoint. Diese Integration enthält keine Execute-Route und +bewirbt keine SYSTEM-Fähigkeit. Die unqualifizierte Windows-Uhr bleibt geschlossen. + +Ein aktueller, höchstens 8 KiB großer Beleg liegt privat als +`state\helper-probe.json`. Er enthält die echte Agent-PID, die gebundene +Helperidentität, Challenge und die vom Helper übermittelten Windows-Datei-IDs von +Ledger und Lock. Ein fehlgeschlagener Aufruf ersetzt einen vorherigen Beleg durch +`authenticated_pipe_exchange=false`. Es gibt kein wachsendes Archiv pro Heartbeat. +Eine einzige create-only Stagingdatei begrenzt auch unterbrochene Schreibvorgänge; +ein Überbleibsel nach Prozessabbruch wird nicht überschrieben. Der Zeitstempel +dient nur der Abnahmefrische, niemals als vertrauenswürdige Jobuhr. + +## Geplante native Reihenfolge, nicht ausgeführt + +1. Einen neuen Agentbuild mit `system-helper` nativ qualifizieren und den + bestehenden Agenten identitätserhaltend aktualisieren. Der maßgebliche + `devstate/windows-client/current-runtime.json` und sein gehashter tatsächlicher + Upgradebeleg müssen beide `system_helper_client_feature=true` ausweisen. + Der bisherige passive Build `f64e8115…` reicht dafür nicht aus. +2. Der Eltern-Orchestrator erstellt aus der öffentlichen Servermetadatenbindung + eine private lokale JSON-Datei mit ausschließlich Tenant-/Asset-/Agent-ID, + Generation, Assignmentversion und öffentlichem Signierschlüssel. Keine Tokens, + Geräte- oder privaten Signierschlüssel übertragen. +3. `deploy/windows/system_helper_bootstrap.py prepare` prüft den aktuellen + Runtimebeleg, die 21 tatsächlich bestandenen Unitprüfungen und das damit + zugeordnete unveränderliche Helperartefakt `921cf182…`. Anschließend werden + ausschließlich diese EXE und der gehashte feste Bootstrapquelltext bereitgelegt. +4. `install --case …` legt genau einmal den eigenen Manual-/OWN_PROCESS-Dienst + `RmmDiscoverySystemHelper` als LocalSystem mit unrestricted Service-SID an. + Public-Policy und Pin sind nur für SYSTEM/Administratoren schreibbar und für + beide Dienst-SIDs lesbar. `state` erlaubt ausschließlich + SYSTEM/Administratoren/Helper-SID; der Netzwerkagent erhält keinen Ledgerzugriff. + Die Policy enthält `allowed_services=[]`, `enable_effects=false` und + `allow_reboot=false`. Keine Fixtures, Time-Service-, Recovery- oder + Agentkonfigurationen werden verändert. +5. `start --case …` startet ausschließlich diesen neuen eigenen Dienst genau + einmal. Der laufende LocalService-Agent entdeckt den Pin selbst; kein zusätzlicher + Agentneustart erfolgt. Unklarer SSH-Ausgang oder Startfehler erlaubt ausschließlich + `readback`, keinen erneuten Installations-/Startversuch und kein Ledgerreset. +6. Nach einer normalen periodischen Agentprobe liest `verify-peer --case …` den + frischen aktuellen Dienstbeleg. Es vergleicht dessen echte Agent-PID mit dem + erhaltenen SCM-Prozess, beide Rollen/Service-SIDs, Helperimage, Policy und + Dateiidentitäten. Die beidseitige Kernel-/SCM-/Token-/Imageprüfung stammt aus + den beiden echten Diensten; ein Administrator-SSH-Pipeaufruf ist kein positiver + Nachweis. Uhr, HelperReady und Systemfähigkeiten müssen weiterhin geschlossen sein. + +Alle lokalen und entfernten mutierenden Phasen schreiben vorher create-only +Intents. Bekannte Belege und private Helperzustände werden erhalten. Der Runner +führt keine Servermutation, RMM-Jobs, Freigabe, Reboots oder Kunden-Dienstwirkung +aus. Die WebUI-Genehmigungsgrenze bleibt für spätere tatsächliche Jobs bindend. + +## Tatsächlich ausgeführte lokale Prüfungen + +- Windows-GNU-Clippy mit `-D warnings`, sowohl Default als auch `system-helper`, + für Bibliothek, Agentbinary und Tests: bestanden. +- Python-Syntaxprüfung und drei fokussierte Guards: bestanden. Abgedeckt sind die + leere passive Policy, der dokumentierte Service-SID-Vektor, das Verbot einer + wiederholten Mutation nach verlorenem SSH-Reply und die Ablehnung fremder, + veralteter oder fälschlich Ready meldender Probe-Belege. +- PowerShell-Parser für Haupt-, Stage-, Launch- und Uploadquelltext: bestanden. +- Keine dieser lokalen Prüfungen ersetzt die oben noch ausstehende native Strecke. + +Der deterministische Service-SID folgt +[MS-LSAT 3.1.1.1.2](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-lsat/252cb6b0-2edc-4403-9d75-5b2786ba859e); +nach CreateService prüft der Bootstrap zusätzlich die tatsächliche lokale +Windows-Auflösung. Weiterführende offene Windows-Abnahmen stehen in +[windows-remaining-implementation.md](windows-remaining-implementation.md).