From 4aff116b849c69e1fe615524fc43583d81e0f7f9 Mon Sep 17 00:00:00 2001 From: Kevin Janssen Date: Sat, 14 Nov 2026 07:43:36 +0100 Subject: [PATCH] Qualify Windows system delegation and crash-safe mirror adapter on native VM --- .../examples/helper_peer_controls.rs | 3 +- crates/agent-windows/examples/system_state.rs | 721 ++++++++++++++++++ crates/agent-windows/src/helper_intent.rs | 414 ++++++++++ crates/agent-windows/src/helper_ipc.rs | 410 ++++++++-- crates/agent-windows/src/helper_runtime.rs | 525 ++++++++++++- crates/agent-windows/src/helper_system.rs | 166 +++- crates/agent-windows/src/runtime.rs | 471 +++++++++++- deploy/windows/client_bootstrap.py | 4 +- .../windows/system_delegation_acceptance.py | 278 +++++++ docs/handover.md | 10 + docs/windows-gates.md | 2 +- docs/windows-system-delegation-source.md | 157 ++++ docs/windows-system-state-witness.md | 69 ++ 13 files changed, 3115 insertions(+), 115 deletions(-) create mode 100644 crates/agent-windows/examples/system_state.rs create mode 100644 crates/agent-windows/src/helper_intent.rs create mode 100644 deploy/windows/system_delegation_acceptance.py create mode 100644 docs/windows-system-delegation-source.md create mode 100644 docs/windows-system-state-witness.md diff --git a/crates/agent-windows/examples/helper_peer_controls.rs b/crates/agent-windows/examples/helper_peer_controls.rs index 7e1cf8a..bfcb529 100644 --- a/crates/agent-windows/examples/helper_peer_controls.rs +++ b/crates/agent-windows/examples/helper_peer_controls.rs @@ -246,10 +246,11 @@ mod platform { // Actor directories are read-only. Bootstrap precreates just the fixed // writable leaves with SYSTEM/Admin ownership, so SID19 cannot gain // owner-implied WRITE_DAC or forge a different actor's receipts. - let _parent = ipc::PinnedFile::open(&directory, true, &roles, &[], false)?; + let _parent = ipc::PinnedFile::open_read_shared(&directory, true, &roles, &[])?; let path = directory.join(name); if folder != "controller" { let mut output = ipc::PinnedFile::open(&path, false, &[], &roles, true)?; + output.limit_writer_rights(0x0012_019f)?; ensure!( output.file.metadata()?.len() == 0, "CONTROL_REPORT_ALREADY_WRITTEN" diff --git a/crates/agent-windows/examples/system_state.rs b/crates/agent-windows/examples/system_state.rs new file mode 100644 index 0000000..d6edf3c --- /dev/null +++ b/crates/agent-windows/examples/system_state.rs @@ -0,0 +1,721 @@ +//! Fixed read-only helper persistence witness for bootstrap preservation only. +//! No keys, request/row values, jobs, migrations, IPC or clock queries are emitted. +#[cfg(windows)] +mod platform { + use anyhow::{Result, ensure}; + use rusqlite::{Connection, OpenFlags, types::ValueRef}; + use serde::Serialize; + use serde_json::json; + use sha2::{Digest, Sha256}; + use std::{ + collections::{BTreeMap, BTreeSet}, + fs::File, + io::Read, + mem::{size_of, zeroed}, + os::windows::io::{AsRawHandle, FromRawHandle, OwnedHandle}, + path::{Path, PathBuf}, + ptr, + time::Duration, + }; + use windows_sys::Win32::{ + Foundation::*, + Security::{Authorization::*, *}, + Storage::FileSystem::*, + System::Threading::*, + }; + + const ROOT: &str = r"C:\ProgramData\RustDiscoverySystem"; + const STATE: &str = r"C:\ProgramData\RustDiscoverySystem\state"; + const LEDGER: &str = r"C:\ProgramData\RustDiscoverySystem\state\ledger.sqlite"; + const LOCK: &str = r"C:\ProgramData\RustDiscoverySystem\state\executor.lock"; + const ACTOR: &str = "S-1-5-80-2738667961-1084496008-3886777835-2738042298-1934696263"; + const HELPER: &str = "S-1-5-80-1378800304-2783697212-3564778230-1646893514-1341434273"; + const MAX_CELL: usize = 1024 * 1024; + const MAX_ROWS: usize = 10000; + const MAX_ROW_BYTES: usize = 32 * 1024 * 1024; + const READ_MASK: u32 = 0x0012_00a9; + + fn wide(value: &Path) -> Vec { + use std::os::windows::ffi::OsStrExt; + value.as_os_str().encode_wide().chain(Some(0)).collect() + } + fn owned(raw: HANDLE) -> Result { + ensure!( + !raw.is_null() && raw != INVALID_HANDLE_VALUE, + "STATE_HANDLE" + ); + // SAFETY: successful CreateFile/OpenToken returned a unique owned handle. + Ok(unsafe { OwnedHandle::from_raw_handle(raw) }) + } + struct Local(*mut std::ffi::c_void); + impl Drop for Local { + fn drop(&mut self) { + // SAFETY: this buffer was allocated by the security APIs for LocalFree. + unsafe { LocalFree(self.0) }; + } + } + fn sid_text(sid: PSID) -> Result { + ensure!( + !sid.is_null() && unsafe { IsValidSid(sid) } != 0, + "STATE_SID" + ); + let mut text = ptr::null_mut(); + ensure!( + unsafe { ConvertSidToStringSidW(sid, &mut text) } != 0, + "STATE_SID" + ); + let _memory = Local(text.cast()); + let mut count = 0; + while count < 256 && unsafe { *text.add(count) } != 0 { + count += 1; + } + ensure!((1..256).contains(&count), "STATE_SID_BOUND"); + Ok(String::from_utf16(unsafe { + std::slice::from_raw_parts(text, count) + })?) + } + fn administrator() -> Result<()> { + let mut thread = ptr::null_mut(); + if unsafe { OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, 1, &mut thread) } != 0 { + let _thread = owned(thread)?; + anyhow::bail!("STATE_IMPERSONATION_FORBIDDEN"); + } + ensure!( + unsafe { GetLastError() } == ERROR_NO_TOKEN, + "STATE_THREAD_TOKEN" + ); + let mut storage = [0usize; 16]; + let mut size = size_of::<[usize; 16]>() as u32; + ensure!( + unsafe { + CreateWellKnownSid( + WinBuiltinAdministratorsSid, + ptr::null_mut(), + storage.as_mut_ptr().cast(), + &mut size, + ) + } != 0, + "STATE_ADMIN_SID" + ); + let mut enabled = 0; + ensure!( + unsafe { + CheckTokenMembership( + ptr::null_mut(), + storage.as_ptr().cast_mut().cast(), + &mut enabled, + ) + } != 0 + && enabled != 0, + "STATE_ADMINISTRATOR_REQUIRED" + ); + Ok(()) + } + + #[derive(Clone, Debug, PartialEq, Eq, Serialize)] + struct FileId { + // Strings preserve every 64-bit digit through PowerShell JSON parsing. + volume_serial: String, + id_hex: String, + } + #[derive(Clone, Debug, PartialEq, Eq, Serialize)] + struct Metadata { + file_id: FileId, + creation_filetime: String, + bytes: u64, + links: u32, + attributes: u32, + owner_sid: String, + acl_sha256: String, + } + #[derive(Clone, Copy, PartialEq, Eq)] + enum Role { + Public, + Private, + IntentDirectory, + IntentLeaf, + } + fn security(raw: HANDLE, role: Role, directory: bool) -> Result<(String, String)> { + let mut owner = ptr::null_mut(); + let mut acl = ptr::null_mut(); + let mut descriptor = ptr::null_mut(); + let parts = + OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION; + ensure!( + unsafe { + GetSecurityInfo( + raw, + SE_FILE_OBJECT, + parts, + &mut owner, + ptr::null_mut(), + &mut acl, + ptr::null_mut(), + &mut descriptor, + ) + } == 0, + "STATE_FILE_SECURITY" + ); + let _descriptor = Local(descriptor); + ensure!( + !owner.is_null() && !acl.is_null() && !descriptor.is_null(), + "STATE_NULL_DACL" + ); + let owner = sid_text(owner)?; + ensure!( + matches!(owner.as_str(), "S-1-5-18" | "S-1-5-32-544"), + "STATE_OWNER" + ); + let mut control = 0; + let mut revision = 0; + ensure!( + unsafe { GetSecurityDescriptorControl(descriptor, &mut control, &mut revision) } != 0 + // The original helper creates ledger/lock with inherited ACEs. + // Their protected parent remains held and checked by run(). + && ((role == Role::Private && !directory) + || control & SE_DACL_PROTECTED != 0) + && unsafe { IsValidAcl(acl) } != 0, + "STATE_PROTECTED_DACL" + ); + ensure!( + unsafe { (*acl).AceCount } > 0 && unsafe { (*acl).AceCount } <= 16, + "STATE_ACL_BOUND" + ); + let mut seen = BTreeSet::new(); + for n in 0..unsafe { (*acl).AceCount } { + let mut raw_ace = ptr::null_mut(); + ensure!( + unsafe { GetAce(acl, n.into(), &mut raw_ace) } != 0, + "STATE_ACE" + ); + let ace = unsafe { &*raw_ace.cast::() }; + ensure!( + ace.Header.AceType == 0 + && ace.Header.AceSize as usize >= size_of::() + && u32::from(ace.Header.AceFlags) & INHERIT_ONLY_ACE == 0, + "STATE_ACE_TYPE" + ); + let sid = ptr::addr_of!(ace.SidStart).cast_mut().cast(); + let name = sid_text(sid)?; + ensure!( + unsafe { GetLengthSid(sid) } as usize + 8 <= ace.Header.AceSize as usize, + "STATE_ACE_SID_SIZE" + ); + let writer = matches!(name.as_str(), "S-1-5-18" | "S-1-5-32-544") + || (role == Role::Private && name == HELPER); + let reader = (role == Role::Public && matches!(name.as_str(), ACTOR | HELPER)) + || (role == Role::IntentDirectory && name == ACTOR); + ensure!( + if writer { + ace.Mask == FILE_ALL_ACCESS + } else if role == Role::IntentLeaf && name == ACTOR { + ace.Mask == 0x0012_019f + } else { + reader && ace.Mask & !READ_MASK == 0 && ace.Mask & 0x0002_0089 == 0x0002_0089 + }, + "STATE_EXACT_FILE_ROLES" + ); + seen.insert(name); + } + ensure!( + seen.contains("S-1-5-18") && seen.contains("S-1-5-32-544"), + "STATE_REQUIRED_FILE_ROLES" + ); + if matches!(role, Role::Public | Role::Private) { + ensure!(seen.contains(HELPER), "STATE_REQUIRED_HELPER_ROLE"); + } + if role != Role::Private { + ensure!(seen.contains(ACTOR), "STATE_REQUIRED_AGENT_READER"); + } + let mut sddl = ptr::null_mut(); + let mut size = 0; + ensure!( + unsafe { + ConvertSecurityDescriptorToStringSecurityDescriptorW( + descriptor, + SDDL_REVISION_1, + parts, + &mut sddl, + &mut size, + ) + } != 0, + "STATE_SECURITY_ENCODING" + ); + let _sddl = Local(sddl.cast()); + ensure!((1..=16384).contains(&size), "STATE_SECURITY_BOUND"); + let text = unsafe { std::slice::from_raw_parts(sddl, size as usize) }; + ensure!(text.last() == Some(&0), "STATE_SECURITY_TERMINATOR"); + let text = String::from_utf16(&text[..text.len() - 1])?; + Ok((owner, format!("{:x}", Sha256::digest(text.as_bytes())))) + } + fn metadata(raw: HANDLE, directory: bool, role: Role) -> Result { + let mut info: BY_HANDLE_FILE_INFORMATION = unsafe { zeroed() }; + let mut id: FILE_ID_INFO = unsafe { zeroed() }; + ensure!( + unsafe { GetFileInformationByHandle(raw, &mut info) } != 0 + && unsafe { + GetFileInformationByHandleEx( + raw, + FileIdInfo, + (&mut id as *mut FILE_ID_INFO).cast(), + size_of::() as u32, + ) + } != 0, + "STATE_FILE_INFORMATION" + ); + ensure!( + info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT == 0 + && (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY != 0) == directory + && (directory || info.nNumberOfLinks == 1), + "STATE_REGULAR_FILE" + ); + let bytes = (u64::from(info.nFileSizeHigh) << 32) | u64::from(info.nFileSizeLow); + ensure!(directory || bytes <= 1024 * 1024 * 1024, "STATE_FILE_SIZE"); + let (owner_sid, acl_sha256) = security(raw, role, directory)?; + Ok(Metadata { + file_id: FileId { + volume_serial: id.VolumeSerialNumber.to_string(), + id_hex: id + .FileId + .Identifier + .iter() + .map(|b| format!("{b:02x}")) + .collect(), + }, + creation_filetime: ((u64::from(info.ftCreationTime.dwHighDateTime) << 32) + | u64::from(info.ftCreationTime.dwLowDateTime)) + .to_string(), + bytes, + links: info.nNumberOfLinks, + attributes: info.dwFileAttributes, + owner_sid, + acl_sha256, + }) + } + fn open(path: &Path, directory: bool, attributes_only: bool) -> Result { + owned(unsafe { + CreateFileW( + wide(path).as_ptr(), + if attributes_only { + FILE_READ_ATTRIBUTES + } else { + GENERIC_READ + }, + FILE_SHARE_READ | FILE_SHARE_WRITE, + ptr::null(), + OPEN_EXISTING, + FILE_FLAG_OPEN_REPARSE_POINT + | if directory { + FILE_FLAG_BACKUP_SEMANTICS + } else { + 0 + }, + ptr::null_mut(), + ) + }) + } + struct Held { + file: File, + path: PathBuf, + directory: bool, + role: Role, + initial: Metadata, + } + impl Held { + fn open(path: &Path, directory: bool, role: Role) -> Result { + for ancestor in path.ancestors().skip(1) { + let handle = open(ancestor, true, true)?; + let mut info: BY_HANDLE_FILE_INFORMATION = unsafe { zeroed() }; + ensure!( + unsafe { GetFileInformationByHandle(handle.as_raw_handle(), &mut info) } != 0 + && info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT == 0 + && info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY != 0, + "STATE_ANCESTOR" + ); + } + let file: File = open(path, directory, false)?.into(); + let initial = metadata(file.as_raw_handle(), directory, role)?; + Ok(Self { + file, + path: path.into(), + directory, + role, + initial, + }) + } + fn check(&self) -> Result<()> { + ensure!( + metadata(self.file.as_raw_handle(), self.directory, self.role)? == self.initial, + "STATE_HELD_FILE_CHANGED" + ); + let current = open(&self.path, self.directory, false)?; + ensure!( + metadata(current.as_raw_handle(), self.directory, self.role)? == self.initial, + "STATE_FILE_PATH_CHANGED" + ); + Ok(()) + } + } + + struct Table { + name: &'static str, + create: &'static str, + query: &'static str, + types: &'static [u8], + } + // Lowercase types allow NULL; uppercase types require a value. All query + // identifiers/orderings are compile-time constants, never DB/user strings. + const TABLES: &[Table] = &[ + Table { + name: "consumed", + create: "CREATE TABLE consumed(grant_id TEXT PRIMARY KEY, agent TEXT NOT NULL, generation INTEGER NOT NULL, job TEXT NOT NULL, attempt TEXT NOT NULL, result BLOB, UNIQUE(agent,generation,job,attempt))", + query: "SELECT grant_id,agent,generation,job,attempt,result FROM consumed INDEXED BY sqlite_autoindex_consumed_1 ORDER BY grant_id LIMIT 10001", + types: b"TTITTb", + }, + Table { + name: "execution_evidence", + create: "CREATE TABLE execution_evidence(grant_id TEXT PRIMARY KEY, start BLOB NOT NULL, outcome BLOB)", + query: "SELECT grant_id,start,outcome FROM execution_evidence INDEXED BY sqlite_autoindex_execution_evidence_1 ORDER BY grant_id LIMIT 10001", + types: b"TBb", + }, + Table { + name: "windows_helper_files", + create: "CREATE TABLE windows_helper_files(id INTEGER PRIMARY KEY CHECK(id=1),ledger BLOB NOT NULL,executor_lock BLOB NOT NULL,binding_sha256 TEXT NOT NULL)", + query: "SELECT id,ledger,executor_lock,binding_sha256 FROM windows_helper_files ORDER BY id LIMIT 10001", + types: b"IBBT", + }, + Table { + name: "helper_sync_meta", + create: "CREATE TABLE helper_sync_meta(id INTEGER PRIMARY KEY CHECK(id=1),journal_id TEXT NOT NULL,checkpoint INTEGER NOT NULL DEFAULT 0,generation INTEGER NOT NULL DEFAULT 0,binding_sha256 TEXT NOT NULL,last_decision TEXT,fault TEXT,agent_journal_id TEXT,agent_checkpoint INTEGER NOT NULL DEFAULT 0)", + query: "SELECT id,journal_id,checkpoint,generation,binding_sha256,last_decision,fault,agent_journal_id,agent_checkpoint FROM helper_sync_meta ORDER BY id LIMIT 10001", + types: b"ITIITtttI", + }, + Table { + name: "helper_sync_rows", + create: "CREATE TABLE helper_sync_rows(grant_id TEXT PRIMARY KEY,revision INTEGER NOT NULL,fingerprint TEXT NOT NULL)", + query: "SELECT grant_id,revision,fingerprint FROM helper_sync_rows INDEXED BY sqlite_autoindex_helper_sync_rows_1 ORDER BY grant_id LIMIT 10001", + types: b"TIT", + }, + Table { + name: "helper_sync_captures", + create: "CREATE TABLE helper_sync_captures(checkpoint INTEGER PRIMARY KEY,snapshot BLOB NOT NULL,fingerprint TEXT NOT NULL,captured_at TEXT NOT NULL)", + query: "SELECT checkpoint,snapshot,fingerprint,captured_at FROM helper_sync_captures ORDER BY checkpoint LIMIT 10001", + types: b"IBTT", + }, + Table { + name: "helper_sync_decisions", + create: "CREATE TABLE helper_sync_decisions(decision_id TEXT PRIMARY KEY,checkpoint INTEGER NOT NULL,response BLOB NOT NULL,request BLOB NOT NULL,fingerprint TEXT NOT NULL)", + query: "SELECT decision_id,checkpoint,response,request,fingerprint FROM helper_sync_decisions INDEXED BY sqlite_autoindex_helper_sync_decisions_1 ORDER BY decision_id LIMIT 10001", + types: b"TIBBT", + }, + ]; + fn normalized(sql: &str) -> String { + sql.chars() + .filter(|c| !c.is_ascii_whitespace()) + .collect::() + .to_ascii_uppercase() + .replace("IFNOTEXISTS", "") + } + fn schema(db: &Connection) -> Result<(BTreeSet, String)> { + let mut entries = Vec::new(); + let mut present = BTreeSet::new(); + let mut indexes = BTreeSet::new(); + // Bound and sort the catalog in Rust, never via a disk-backed SQL sort. + let mut statement = + db.prepare("SELECT type,name,tbl_name,sql FROM sqlite_master LIMIT 33")?; + let mut rows = statement.query([])?; + while let Some(row) = rows.next()? { + ensure!(entries.len() < 32, "STATE_SCHEMA_BOUND"); + let kind: String = row.get(0)?; + let name: String = row.get(1)?; + let parent: String = row.get(2)?; + let sql: Option = row.get(3)?; + ensure!( + name.len() <= 96 + && parent.len() <= 96 + && sql.as_ref().is_none_or(|s| s.len() <= 4096), + "STATE_SCHEMA_BOUND" + ); + if kind == "table" { + let table = TABLES + .iter() + .find(|t| t.name == name) + .ok_or_else(|| anyhow::anyhow!("STATE_UNEXPECTED_TABLE"))?; + ensure!( + parent == name + && sql + .as_ref() + .is_some_and(|s| normalized(s) == normalized(table.create)), + "STATE_TABLE_SCHEMA_CHANGED" + ); + present.insert(name.clone()); + } else { + ensure!( + kind == "index" && sql.is_none() && TABLES.iter().any(|t| t.name == parent), + "STATE_UNEXPECTED_SCHEMA_OBJECT" + ); + indexes.insert((parent.clone(), name.clone())); + } + entries.push((kind, name, parent, sql)); + } + ensure!( + present.contains("consumed") + && present.contains("execution_evidence") + && present.contains("windows_helper_files"), + "STATE_REQUIRED_SCHEMA" + ); + let sync = present + .iter() + .filter(|n| n.starts_with("helper_sync_")) + .count(); + ensure!(sync == 0 || sync == 4, "STATE_PARTIAL_SYNC_SCHEMA"); + let expected: BTreeSet<_> = [ + "consumed", + "execution_evidence", + "helper_sync_rows", + "helper_sync_decisions", + ] + .into_iter() + .filter(|n| present.contains(*n)) + .flat_map(|name| { + let mut names = vec![(name.to_string(), format!("sqlite_autoindex_{name}_1"))]; + if name == "consumed" { + names.push((name.to_string(), "sqlite_autoindex_consumed_2".into())); + } + names + }) + .collect(); + ensure!(indexes == expected, "STATE_INDEX_SCHEMA_CHANGED"); + entries.sort(); + Ok(( + present, + format!("{:x}", Sha256::digest(serde_json::to_vec(&entries)?)), + )) + } + fn table_rows( + db: &Connection, + table: &Table, + remaining: &mut usize, + ) -> Result { + let mut hash = Sha256::new(); + hash.update(b"rmm.bootstrap.helper-table.v1\0"); + hash.update(table.name.as_bytes()); + hash.update([0]); + let mut count = 0usize; + let mut statement = db.prepare(table.query)?; + ensure!( + statement.column_count() == table.types.len(), + "STATE_ROW_COLUMNS" + ); + let mut rows = statement.query([])?; + while let Some(row) = rows.next()? { + ensure!(count < MAX_ROWS, "STATE_ROW_COUNT_BOUND"); + hash.update(b"R"); + for (column, expected) in table.types.iter().enumerate() { + let value = row.get_ref(column)?; + match value { + ValueRef::Null => { + ensure!(expected.is_ascii_lowercase(), "STATE_ROW_NULL"); + hash.update(b"N"); + } + ValueRef::Integer(n) => { + ensure!(expected.eq_ignore_ascii_case(&b'I'), "STATE_ROW_TYPE"); + hash.update(b"I"); + hash.update(n.to_le_bytes()); + } + ValueRef::Text(bytes) | ValueRef::Blob(bytes) => { + let tag = if matches!(value, ValueRef::Text(_)) { + b'T' + } else { + b'B' + }; + ensure!( + expected.eq_ignore_ascii_case(&tag) + && bytes.len() <= MAX_CELL + && bytes.len() <= *remaining, + "STATE_ROW_TYPE_OR_BOUND" + ); + if tag == b'T' { + std::str::from_utf8(bytes)?; + } + *remaining -= bytes.len(); + hash.update([tag]); + hash.update((bytes.len() as u64).to_le_bytes()); + hash.update(bytes); + } + ValueRef::Real(_) => anyhow::bail!("STATE_ROW_TYPE"), + } + } + count += 1; + } + hash.update((count as u64).to_le_bytes()); + Ok(json!({"rows":count,"rows_sha256":format!("{:x}",hash.finalize())})) + } + fn sqlite_handle(db: &Connection) -> Result { + let mut handle: HANDLE = ptr::null_mut(); + // SAFETY: same-thread live SQLite connection, documented native-handle + // output operation only. Borrowed VFS handle is never closed or changed. + let code = unsafe { + rusqlite::ffi::sqlite3_file_control( + db.handle(), + c"main".as_ptr(), + rusqlite::ffi::SQLITE_FCNTL_WIN32_GET_HANDLE, + (&mut handle as *mut HANDLE).cast(), + ) + }; + ensure!( + code == rusqlite::ffi::SQLITE_OK && !handle.is_null() && handle != INVALID_HANDLE_VALUE, + "STATE_SQLITE_NATIVE_HANDLE" + ); + ensure!( + unsafe { rusqlite::ffi::sqlite3_db_readonly(db.handle(), c"main".as_ptr()) } == 1, + "STATE_SQLITE_READONLY" + ); + Ok(handle) + } + fn absent(path: &Path) -> Result { + match std::fs::symlink_metadata(path) { + Ok(_) => Ok(false), + Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(true), + Err(_) => anyhow::bail!("STATE_PATH_QUERY_FAILED"), + } + } + fn intent_metadata() -> Result<(Vec, serde_json::Value)> { + let path = Path::new(ROOT).join("agent-intents"); + if absent(&path)? { + return Ok((Vec::new(), serde_json::Value::Null)); + } + let directory = Held::open(&path, true, Role::IntentDirectory)?; + let names: BTreeSet<_> = std::fs::read_dir(&path)? + .map(|entry| entry.map(|item| item.file_name())) + .collect::>()?; + let expected: BTreeSet<_> = ["system-intents.sqlite", "system-intents.sqlite-journal"] + .into_iter() + .map(std::ffi::OsString::from) + .collect(); + ensure!(names == expected, "STATE_ZERO_INTENT_LEAVES_ONLY"); + let database = Held::open(&path.join("system-intents.sqlite"), false, Role::IntentLeaf)?; + let journal = Held::open( + &path.join("system-intents.sqlite-journal"), + false, + Role::IntentLeaf, + )?; + ensure!( + database.initial.bytes == 0 && journal.initial.bytes == 0, + "STATE_UNINITIALIZED_INTENT_ONLY" + ); + let value = json!({"directory":directory.initial,"leaves":{"database":database.initial,"journal":journal.initial},"sqlite_initialized":false}); + Ok((vec![directory, database, journal], value)) + } + pub fn run() -> Result<()> { + ensure!(std::env::args_os().len() == 1, "STATE_FIXED_NO_ARGUMENTS"); + administrator()?; + let roots = [ + Held::open(Path::new(ROOT), true, Role::Public)?, + Held::open(Path::new(STATE), true, Role::Private)?, + ]; + let ledger = Held::open(Path::new(LEDGER), false, Role::Private)?; + let lock = Held::open(Path::new(LOCK), false, Role::Private)?; + ensure!(lock.initial.bytes <= 4096, "STATE_LOCK_SIZE"); + let settings = Held::open(&Path::new(ROOT).join("helper.json"), false, Role::Public)?; + let pin = Held::open(&Path::new(ROOT).join("agent-pin.json"), false, Role::Public)?; + let (intent_files, intent_store) = intent_metadata()?; + ensure!( + settings.initial.bytes <= 32768 && pin.initial.bytes <= 16384, + "STATE_PUBLIC_FILE_SIZE" + ); + // Refuse every existing sidecar, including a possibly hot rollback + // journal. Never ask an administrator-owned reader to recover it. Check + // the real header before SQLite can consider WAL/shared-memory paths. + for suffix in ["-journal", "-wal", "-shm"] { + ensure!( + absent(Path::new(&format!("{LEDGER}{suffix}")))?, + "STATE_SIDECAR_PRESENT" + ); + } + let mut header = [0u8; 100]; + (&ledger.file).read_exact(&mut header)?; + ensure!( + &header[..16] == b"SQLite format 3\0" && header[16..20] == [16, 0, 1, 1], + "STATE_ROLLBACK_HEADER_REQUIRED" + ); + // No URI/nolock/immutable shortcut: retain normal SQLite locking and + // change detection. Read-only mode refuses a journal needing recovery. + let db = Connection::open_with_flags(LEDGER, OpenFlags::SQLITE_OPEN_READ_ONLY)?; + db.busy_timeout(Duration::from_millis(250))?; + // Connection-only allocation limits, never PRAGMA or file mutations. + unsafe { + rusqlite::ffi::sqlite3_limit( + db.handle(), + rusqlite::ffi::SQLITE_LIMIT_LENGTH, + MAX_CELL as i32, + ); + rusqlite::ffi::sqlite3_limit(db.handle(), rusqlite::ffi::SQLITE_LIMIT_SQL_LENGTH, 4096); + rusqlite::ffi::sqlite3_limit(db.handle(), rusqlite::ffi::SQLITE_LIMIT_COLUMN, 32); + rusqlite::ffi::sqlite3_limit(db.handle(), rusqlite::ffi::SQLITE_LIMIT_ATTACHED, 0); + } + db.execute_batch("BEGIN DEFERRED")?; + ensure!( + metadata(sqlite_handle(&db)?, false, Role::Private)? == ledger.initial, + "STATE_CONNECTED_FILE_CHANGED" + ); + let integrity: String = db.query_row("PRAGMA integrity_check", [], |r| r.get(0))?; + ensure!(integrity == "ok", "STATE_HELPER_INTEGRITY"); + let page_size: u64 = db.query_row("PRAGMA page_size", [], |r| r.get(0))?; + let page_count: u64 = db.query_row("PRAGMA page_count", [], |r| r.get(0))?; + let journal_mode: String = db.query_row("PRAGMA journal_mode", [], |r| r.get(0))?; + ensure!( + page_size == 4096 + && (1..=262144).contains(&page_count) + && page_size * page_count == ledger.initial.bytes + && journal_mode == "delete", + "STATE_HELPER_STORAGE_PROFILE" + ); + let (present, schema_sha256) = schema(&db)?; + let absent_tables: Vec<_> = TABLES + .iter() + .filter(|table| !present.contains(table.name)) + .map(|table| table.name) + .collect(); + let mut tables = BTreeMap::new(); + let mut remaining = MAX_ROW_BYTES; + for table in TABLES.iter().filter(|t| present.contains(t.name)) { + tables.insert(table.name, table_rows(&db, table, &mut remaining)?); + } + ensure!( + metadata(sqlite_handle(&db)?, false, Role::Private)? == ledger.initial, + "STATE_CONNECTED_FILE_CHANGED" + ); + for held in roots + .iter() + .chain([&ledger, &lock, &settings, &pin]) + .chain(intent_files.iter()) + { + held.check()?; + } + if intent_files.is_empty() { + ensure!( + absent(&Path::new(ROOT).join("agent-intents"))?, + "STATE_INTENT_STORE_APPEARED" + ); + } + db.execute_batch("ROLLBACK")?; + let value = json!({"schema_version":1,"type":"rmm.windows.system-state-witness","read_only":true,"bootstrap_administrator":true, + "helper_ledger":{"file":ledger.initial,"integrity":"ok","page_size":page_size,"page_count":page_count,"journal_mode":journal_mode,"schema_sha256":schema_sha256,"tables":tables,"absent_tables":absent_tables,"connected_native_file_id_verified":true}, + "executor_lock":lock.initial,"public_metadata":{"helper_settings":settings.initial,"agent_pin":pin.initial},"intent_store":intent_store, + "private_key_read":false,"private_rows_returned":false,"clock_queried":false,"jobs_created":false}); + let encoded = serde_json::to_string(&value)?; + ensure!(encoded.len() <= 32768, "STATE_REPORT_BOUND"); + println!("{encoded}"); + Ok(()) + } +} +fn main() { + #[cfg(windows)] + if platform::run().is_ok() { + return; + } + // Never print SQLite/OS errors, paths, SQL or private row material. + eprintln!("SYSTEM_STATE_WITNESS_REFUSED"); + std::process::exit(2); +} diff --git a/crates/agent-windows/src/helper_intent.rs b/crates/agent-windows/src/helper_intent.rs new file mode 100644 index 0000000..5e30e2e --- /dev/null +++ b/crates/agent-windows/src/helper_intent.rs @@ -0,0 +1,414 @@ +//! Fixed, preprovisioned confidential delegation journal. Never log request bytes. +use crate::helper_ipc::{self as ipc, FileIdentity, PinnedFile}; +use anyhow::{Result, ensure}; +use chrono::{DateTime, Utc}; +use rmm_agent_core::{ + auth::Request, + helper_reconcile::{NoDispatchObservation, NoStartAnchor}, +}; +use rmm_domain::jobs::{ExecutionPermitPayload, hash}; +use rusqlite::{Connection, OpenFlags, OptionalExtension, TransactionBehavior, params}; +use serde::{Deserialize, Serialize}; +use std::{path::Path, time::Duration}; +use uuid::Uuid; + +pub const ROOT: &str = r"C:\ProgramData\RustDiscoverySystem\agent-intents"; +pub const DB: &str = r"C:\ProgramData\RustDiscoverySystem\agent-intents\system-intents.sqlite"; +const ACTOR_RW: u32 = 0x0012_019f; +const SCHEMA:&str="CREATE TABLE IF NOT EXISTS system_intent_meta(id INTEGER PRIMARY KEY CHECK(id=1),file_identity BLOB NOT NULL); +CREATE TABLE IF NOT EXISTS system_intents(id TEXT PRIMARY KEY,job TEXT NOT NULL,attempt TEXT NOT NULL,grant_id TEXT NOT NULL UNIQUE,body BLOB NOT NULL CHECK(length(body)<=196608),digest TEXT NOT NULL,phase TEXT NOT NULL CHECK(phase IN ('prepared','known_no_dispatch','may_have_dispatched')),UNIQUE(job,attempt)); +CREATE TABLE IF NOT EXISTS system_intent_mirrors(id TEXT PRIMARY KEY,outcome_sha256 TEXT NOT NULL,mirror_observed_at TEXT NOT NULL); +CREATE TABLE IF NOT EXISTS system_intent_denial_resumes(id TEXT NOT NULL,agent_instance TEXT NOT NULL,observed_at TEXT NOT NULL,PRIMARY KEY(id,agent_instance)); +CREATE TRIGGER IF NOT EXISTS system_intents_no_delete BEFORE DELETE ON system_intents BEGIN SELECT RAISE(ABORT,'SYSTEM_INTENT_IMMUTABLE');END; +CREATE TRIGGER IF NOT EXISTS system_intents_monotonic BEFORE UPDATE ON system_intents WHEN NEW.id!=OLD.id OR NEW.job!=OLD.job OR NEW.attempt!=OLD.attempt OR NEW.grant_id!=OLD.grant_id OR NEW.body!=OLD.body OR NEW.digest!=OLD.digest OR OLD.phase!='prepared' OR NEW.phase NOT IN ('known_no_dispatch','may_have_dispatched') BEGIN SELECT RAISE(ABORT,'SYSTEM_INTENT_IMMUTABLE');END; +CREATE TRIGGER IF NOT EXISTS system_mirrors_no_delete BEFORE DELETE ON system_intent_mirrors BEGIN SELECT RAISE(ABORT,'SYSTEM_MIRROR_IMMUTABLE');END; +CREATE TRIGGER IF NOT EXISTS system_mirrors_no_update BEFORE UPDATE ON system_intent_mirrors BEGIN SELECT RAISE(ABORT,'SYSTEM_MIRROR_IMMUTABLE');END; +CREATE TRIGGER IF NOT EXISTS system_denial_resumes_no_delete BEFORE DELETE ON system_intent_denial_resumes BEGIN SELECT RAISE(ABORT,'SYSTEM_DENIAL_RESUME_IMMUTABLE');END; +CREATE TRIGGER IF NOT EXISTS system_denial_resumes_no_update BEFORE UPDATE ON system_intent_denial_resumes BEGIN SELECT RAISE(ABORT,'SYSTEM_DENIAL_RESUME_IMMUTABLE');END;"; +const CLAIM_RESUME: &str = "INSERT OR IGNORE INTO system_intent_denial_resumes(id,agent_instance,observed_at) VALUES(?1,?2,?3)"; +#[derive(Clone, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct Intent { + pub schema_version: u32, + pub id: Uuid, + pub request: Request, + pub anchor: NoStartAnchor, + pub original_boot_id: Uuid, + pub agent_instance: Uuid, + pub connection_generation: u64, + pub recorded_at: DateTime, + pub policy_sha256: String, + pub helper_binary_sha256: String, +} +impl Intent { + pub fn digest(&self) -> Result { + Ok(hash(&serde_json::to_vec(self)?)) + } + pub fn permit(&self) -> Result { + Ok(serde_json::from_slice(&self.request.permit_bytes)?) + } + fn validate(&self) -> Result<()> { + ensure!( + self.schema_version == 1 + && !self.id.is_nil() + && !self.agent_instance.is_nil() + && !self.original_boot_id.is_nil() + && self.original_boot_id == self.anchor.boot_id() + && self.connection_generation == self.anchor.connection_generation() + && ipc::digest(&self.policy_sha256) + && ipc::digest(&self.helper_binary_sha256) + && serde_json::to_vec(self)?.len() <= 196608, + "SYSTEM_INTENT_BINDING" + ); + let p = self.permit()?; + ensure!( + p.execution_identity == "system" + && p.authorization_id == self.request.authorization_id + && p.plan_hash == self.request.plan_hash + && p.job_payload_hash == hash(&self.request.job_bytes), + "SYSTEM_INTENT_REQUEST_BINDING" + ); + Ok(()) + } +} +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub enum Phase { + Prepared, + KnownNoDispatch, + MayHaveDispatched, +} +impl Phase { + fn parse(s: &str) -> Result { + match s { + "prepared" => Ok(Self::Prepared), + "known_no_dispatch" => Ok(Self::KnownNoDispatch), + "may_have_dispatched" => Ok(Self::MayHaveDispatched), + _ => anyhow::bail!("SYSTEM_INTENT_PHASE"), + } + } + fn name(self) -> &'static str { + match self { + Self::Prepared => "prepared", + Self::KnownNoDispatch => "known_no_dispatch", + Self::MayHaveDispatched => "may_have_dispatched", + } + } +} +fn advance(db: &mut Connection, id: Uuid, desired: Phase) -> Result<(usize, Phase)> { + let tx = db.transaction_with_behavior(TransactionBehavior::Immediate)?; + let changed = tx.execute( + "UPDATE system_intents SET phase=?2 WHERE id=?1 AND phase='prepared'", + params![id.to_string(), desired.name()], + )?; + let phase: String = tx.query_row( + "SELECT phase FROM system_intents WHERE id=?1", + [id.to_string()], + |r| r.get(0), + )?; + tx.commit()?; + Ok((changed, Phase::parse(&phase)?)) +} +pub struct Store { + db: Connection, + files: Vec, + identity: FileIdentity, +} +impl Store { + /// Bootstrap, never LocalService, creates both fixed leaves and their ACLs. + /// PERSIST retains the precreated rollback journal, requiring no Actor delete + /// or directory-create rights. A hot journal on the read-only side refuses. + pub fn open(write: bool) -> Result { + let actor = ipc::service_sid(crate::SERVICE_NAME)?; + let root = + PinnedFile::open_read_shared(Path::new(ROOT), true, std::slice::from_ref(&actor), &[])?; + let mut files = vec![root]; + for path in [DB, &format!("{DB}-journal")] { + let mut file = if write { + PinnedFile::open( + Path::new(path), + false, + &[], + std::slice::from_ref(&actor), + true, + )? + } else { + PinnedFile::open_read_shared( + Path::new(path), + false, + &[], + std::slice::from_ref(&actor), + )? + }; + file.limit_writer_rights(ACTOR_RW)?; + files.push(file); + } + let identity = files[1].identity; + let flags = if write { + OpenFlags::SQLITE_OPEN_READ_WRITE + } else { + OpenFlags::SQLITE_OPEN_READ_ONLY + }; + let db = Connection::open_with_flags(DB, flags)?; + db.busy_timeout(Duration::from_millis(250))?; + if write { + rmm_agent_core::ledger::bounded_journal_capacity(&db)?; + let mode: String = db.query_row("PRAGMA journal_mode=PERSIST", [], |r| r.get(0))?; + ensure!(mode == "persist", "SYSTEM_INTENT_JOURNAL_MODE"); + db.execute_batch("PRAGMA synchronous=FULL;PRAGMA temp_store=MEMORY;PRAGMA journal_size_limit=1048576;")?; + db.execute_batch(SCHEMA)?; + db.execute( + "INSERT OR IGNORE INTO system_intent_meta VALUES(1,?1)", + [serde_json::to_vec(&identity)?], + )?; + } + let stored: Vec = db.query_row( + "SELECT file_identity FROM system_intent_meta WHERE id=1", + [], + |r| r.get(0), + )?; + ensure!( + stored == serde_json::to_vec(&identity)?, + "SYSTEM_INTENT_FILE_REPLACED" + ); + ensure!( + db.query_row("PRAGMA integrity_check", [], |r| r.get::<_, String>(0))? == "ok", + "SYSTEM_INTENT_INTEGRITY" + ); + let mut value = Self { + db, + files, + identity, + }; + value.check()?; + Ok(value) + } + pub fn check(&mut self) -> Result<()> { + for file in &mut self.files { + file.check()?; + } + ensure!( + ipc::file_identity(&self.files[1].file)? == self.identity, + "SYSTEM_INTENT_FILE_REPLACED" + ); + Ok(()) + } + pub fn prepare(&mut self, intent: &Intent) -> Result<()> { + self.check()?; + intent.validate()?; + let tx = self + .db + .transaction_with_behavior(TransactionBehavior::Immediate)?; + let count: u64 = tx.query_row("SELECT count(*) FROM system_intents", [], |r| r.get(0))?; + ensure!(count < 10000, "SYSTEM_INTENT_CAPACITY_BLOCKED"); + let busy:bool=tx.query_row("SELECT EXISTS(SELECT 1 FROM system_intents i WHERE NOT EXISTS(SELECT 1 FROM system_intent_mirrors m WHERE m.id=i.id))",[],|r|r.get(0))?; + ensure!(!busy, "SYSTEM_DELEGATION_UNRESOLVED"); + let p = intent.permit()?; + tx.execute("INSERT INTO system_intents(id,job,attempt,grant_id,body,digest,phase) VALUES(?1,?2,?3,?4,?5,?6,'prepared')",params![intent.id.to_string(),p.job_id.to_string(),p.attempt_id.to_string(),p.grant_id.to_string(),serde_json::to_vec(intent)?,intent.digest()?])?; + tx.commit()?; + self.check() + } + pub fn load(&self, id: Uuid) -> Result<(Intent, Phase)> { + let (body, digest, phase): (Vec, String, String) = self.db.query_row( + "SELECT body,digest,phase FROM system_intents WHERE id=?1", + [id.to_string()], + |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?)), + )?; + ensure!( + body.len() <= 196608 && hash(&body) == digest, + "SYSTEM_INTENT_DIGEST" + ); + let intent: Intent = serde_json::from_slice(&body)?; + intent.validate()?; + ensure!(intent.id == id, "SYSTEM_INTENT_ID"); + Ok((intent, Phase::parse(&phase)?)) + } + /// Both cancellation and dispatcher use this same transaction/CAS. A known + /// non-dispatch can never follow the conservative may-dispatch marker. + pub fn decide(&mut self, id: Uuid, dispatch: bool) -> Result { + self.check()?; + let desired = if dispatch { + Phase::MayHaveDispatched + } else { + Phase::KnownNoDispatch + }; + let (changed, phase) = advance(&mut self.db, id, desired)?; + self.check()?; + ensure!( + changed == 1 || phase != Phase::MayHaveDispatched, + "SYSTEM_EXECUTE_REDELIVERY_FORBIDDEN" + ); + Ok(phase) + } + pub fn cancel_before_dispatch(&mut self, id: Uuid) -> Result { + self.check()?; + let (_, phase) = advance(&mut self.db, id, Phase::KnownNoDispatch)?; + self.check()?; + Ok(phase) + } + pub fn pending(&self) -> Result> { + let mut rows=self.db.prepare("SELECT i.id FROM system_intents i WHERE NOT EXISTS(SELECT 1 FROM system_intent_mirrors m WHERE m.id=i.id) ORDER BY i.rowid LIMIT 2")?; + let ids = rows + .query_map([], |r| r.get::<_, String>(0))? + .collect::>>()?; + ensure!(ids.len() <= 1, "SYSTEM_MULTIPLE_PENDING_DELEGATIONS"); + ids.into_iter() + .map(|id| self.load(Uuid::parse_str(&id)?).map(|v| v.0)) + .collect() + } + pub fn by_grant(&self, grant: Uuid) -> Result { + let id: String = self.db.query_row( + "SELECT id FROM system_intents WHERE grant_id=?1", + [grant.to_string()], + |r| r.get(0), + )?; + Ok(self.load(Uuid::parse_str(&id)?)?.0) + } + /// One deny-only attempt per NEW process instance. Original intent/anchor + /// bytes and the historical helper process binding are never rewritten. + pub fn claim_denial_resume( + &mut self, + id: Uuid, + instance: Uuid, + boot: Uuid, + observed_at: DateTime, + ) -> Result { + self.check()?; + let (intent, phase) = self.load(id)?; + ensure!( + phase == Phase::KnownNoDispatch + && intent.original_boot_id == boot + && intent.agent_instance != instance + && !instance.is_nil() + && observed_at >= intent.recorded_at, + "SYSTEM_DENIAL_RESUME_BINDING" + ); + let changed = self.db.execute( + CLAIM_RESUME, + params![ + id.to_string(), + instance.to_string(), + observed_at.to_rfc3339() + ], + )?; + self.check()?; + Ok(changed == 1) + } + pub fn mirrored( + &mut self, + id: Uuid, + outcome_sha256: &str, + observed_at: DateTime, + ) -> Result<()> { + self.check()?; + ensure!(ipc::digest(outcome_sha256), "SYSTEM_MIRROR_HASH"); + let old: Option = self + .db + .query_row( + "SELECT outcome_sha256 FROM system_intent_mirrors WHERE id=?1", + [id.to_string()], + |r| r.get(0), + ) + .optional()?; + if let Some(old) = old { + ensure!(old == outcome_sha256, "SYSTEM_MIRROR_CONFLICT"); + return Ok(()); + } + self.db.execute( + "INSERT INTO system_intent_mirrors VALUES(?1,?2,?3)", + params![id.to_string(), outcome_sha256, observed_at.to_rfc3339()], + )?; + self.check() + } + pub fn verify_no_dispatch( + &mut self, + id: Uuid, + digest: &str, + original: &Request, + anchor: &NoStartAnchor, + ) -> Result { + self.check()?; + let (intent, phase) = self.load(id)?; + ensure!( + phase == Phase::KnownNoDispatch + && intent.digest()? == digest + && serde_json::to_vec(&intent.request)? == serde_json::to_vec(original)? + && serde_json::to_vec(&intent.anchor)? == serde_json::to_vec(anchor)?, + "SYSTEM_NO_DISPATCH_PROOF_INVALID" + ); + self.check()?; + Ok(NoDispatchObservation { + intent_id: id, + intent_sha256: digest.into(), + intent_recorded_at: intent.recorded_at, + original_boot_id: intent.original_boot_id, + }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn cancel_and_dispatch_cas_are_exclusive_and_history_cannot_be_rewritten() { + let mut db = Connection::open_in_memory().unwrap(); + db.execute_batch(SCHEMA).unwrap(); + let cancelled = Uuid::new_v4(); + let dispatched = Uuid::new_v4(); + for id in [cancelled, dispatched] { + db.execute( + "INSERT INTO system_intents VALUES(?1,?1,?1,?1,x'01','immutable','prepared')", + [id.to_string()], + ) + .unwrap(); + } + assert_eq!( + advance(&mut db, cancelled, Phase::KnownNoDispatch).unwrap(), + (1, Phase::KnownNoDispatch) + ); + assert_eq!( + advance(&mut db, cancelled, Phase::MayHaveDispatched).unwrap(), + (0, Phase::KnownNoDispatch) + ); + assert_eq!( + advance(&mut db, dispatched, Phase::MayHaveDispatched).unwrap(), + (1, Phase::MayHaveDispatched) + ); + assert_eq!( + advance(&mut db, dispatched, Phase::KnownNoDispatch).unwrap(), + (0, Phase::MayHaveDispatched) + ); + assert!( + db.execute( + "UPDATE system_intents SET phase='prepared' WHERE id=?1", + [dispatched.to_string()] + ) + .is_err() + ); + assert!( + db.execute( + "UPDATE system_intents SET body=x'02' WHERE id=?1", + [cancelled.to_string()] + ) + .is_err() + ); + assert!( + db.execute( + "DELETE FROM system_intents WHERE id=?1", + [cancelled.to_string()] + ) + .is_err() + ); + let retained: u64 = db + .query_row("SELECT count(*) FROM system_intents", [], |r| r.get(0)) + .unwrap(); + assert_eq!(retained, 2); + let resumed = Uuid::new_v4(); + let args = params![ + cancelled.to_string(), + resumed.to_string(), + Utc::now().to_rfc3339() + ]; + assert_eq!(db.execute(CLAIM_RESUME, args).unwrap(), 1); + assert_eq!(db.execute(CLAIM_RESUME, args).unwrap(), 0); + } +} diff --git a/crates/agent-windows/src/helper_ipc.rs b/crates/agent-windows/src/helper_ipc.rs index 43438c4..26268c6 100644 --- a/crates/agent-windows/src/helper_ipc.rs +++ b/crates/agent-windows/src/helper_ipc.rs @@ -3,7 +3,7 @@ use anyhow::{Context, Result, ensure}; use rmm_agent_core::{ auth::{Binding, Outcome, Request}, - helper_reconcile::{SignedResponse, Snapshot}, + helper_reconcile::{NoStartAnchor, SignedResponse, Snapshot}, }; use rmm_domain::jobs::hash; use serde::{Deserialize, Serialize, de::DeserializeOwned}; @@ -34,6 +34,176 @@ pub const FRAME_LIMIT: usize = 1024 * 1024; pub const CLIENT_PIPE_RIGHTS: u32 = 0x0012_0083; // data R/W, attributes R, RC, synchronize; no bit4 const READ_MASK: u32 = 0x0012_00a9; +/// Static diagnostic contexts only; no caller input or error text is serialized. +#[derive(Clone, Copy, Debug)] +pub enum Stage { + DiscoverPin, + ClientSelfRole, + ClientOpenPipe, + ClientHelperPeer, + ClientSendFrame, + ClientReadReply, + ClientReplyBinding, + ClientReport, + ServerConnect, + ServerReadHeader, + ServerClientPeer, + ServerImpersonate, + ServerVerifyToken, + ServerReadBody, + ServerCallBinding, + ServerRecheckPeer, + ServerReply, + Unclassified, +} +impl Stage { + fn name(self) -> &'static str { + match self { + Self::DiscoverPin => "discover_pin", + Self::ClientSelfRole => "client_self_role", + Self::ClientOpenPipe => "client_open_pipe", + Self::ClientHelperPeer => "client_helper_peer", + Self::ClientSendFrame => "client_send_frame", + Self::ClientReadReply => "client_read_reply", + Self::ClientReplyBinding => "client_reply_binding", + Self::ClientReport => "client_report", + Self::ServerConnect => "server_connect", + Self::ServerReadHeader => "server_read_header", + Self::ServerClientPeer => "server_client_peer", + Self::ServerImpersonate => "server_impersonate", + Self::ServerVerifyToken => "server_verify_client_token", + Self::ServerReadBody => "server_read_body", + Self::ServerCallBinding => "server_call_binding", + Self::ServerRecheckPeer => "server_recheck_peer", + Self::ServerReply => "server_reply", + Self::Unclassified => "unclassified", + } + } +} +impl std::fmt::Display for Stage { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(self.name()) + } +} +#[derive(Clone, Copy, Debug, Serialize)] +pub struct Diagnostic { + pub stage: &'static str, + pub code: &'static str, + #[serde(skip_serializing_if = "Option::is_none")] + pub win32: Option, +} +impl std::fmt::Display for Diagnostic { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(self.code) + } +} +impl std::error::Error for Diagnostic {} +#[derive(Debug)] +struct CapturedWin32(u32); +impl std::fmt::Display for CapturedWin32 { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str("WIN32_CAPTURED") + } +} +impl std::error::Error for CapturedWin32 {} +fn api_failure(code: &'static str) -> anyhow::Error { + let captured = unsafe { GetLastError() }; + anyhow::anyhow!(code).context(CapturedWin32(captured)) +} +fn known_code(text: &str) -> Option<&'static str> { + const CODES: &[&str] = &[ + "HELPER_BINARY_HASH", + "HELPER_CALL_BINDING", + "HELPER_EMPTY_DACL", + "HELPER_EXCESSIVE_WRITER_RIGHTS", + "HELPER_FILE_ID", + "HELPER_FILE_ID_CHANGED", + "HELPER_FILE_INFORMATION", + "HELPER_FILE_TYPE", + "HELPER_FRAME_CAPACITY", + "HELPER_FRAME_EMPTY", + "HELPER_FRAME_LENGTH", + "HELPER_HANDLE", + "HELPER_IMAGE_PARENT", + "HELPER_IMAGE_SIZE", + "HELPER_IMPERSONATED_SELF", + "HELPER_LOCAL_ABSOLUTE_PATH", + "HELPER_NETWORK_AGENT_ADMIN_FORBIDDEN", + "HELPER_NULL_DACL", + "HELPER_PATH_ACE", + "HELPER_PATH_ACE_TYPE", + "HELPER_PATH_OWNER", + "HELPER_PATH_REPLACED", + "HELPER_PATH_SECURITY", + "HELPER_PATH_WRITABLE_OR_FOREIGN", + "HELPER_PEER_RESTARTED", + "HELPER_PEER_TOKEN_QUERY", + "HELPER_PEER_TOKEN_RECHECK", + "HELPER_PIN_PROFILE", + "HELPER_PIPE_CONNECT", + "HELPER_PIPE_EOF", + "HELPER_PIPE_IMPERSONATED_TOKEN", + "HELPER_PIPE_IMPERSONATION", + "HELPER_PIPE_IO", + "HELPER_PIPE_SCM_PID", + "HELPER_PIPE_TIMEOUT", + "HELPER_PIPE_UNAVAILABLE_OR_BUSY", + "HELPER_PROCESS_IDENTITY", + "HELPER_PROCESS_IMAGE", + "HELPER_PROCESS_IMAGE_BINDING", + "HELPER_PROCESS_IMAGE_IDENTITY", + "HELPER_PROCESS_TIME", + "HELPER_PROCESS_TOKEN_IDENTITY", + "HELPER_REPARSE_ANCESTOR", + "HELPER_REPLY_BINDING", + "HELPER_REPLY_OPERATION", + "HELPER_SCM_CONFIG", + "HELPER_SCM_CONFIG_SIZE", + "HELPER_SCM_IMAGE_BINDING", + "HELPER_SCM_MANAGER", + "HELPER_SCM_NOT_RUNNING", + "HELPER_SCM_SERVICE", + "HELPER_SELF_TOKEN", + "HELPER_SERVICE_SID_NOT_ENABLED", + "HELPER_THREAD_TOKEN_UNVERIFIED", + "HELPER_TOKEN_GROUPS_SIZE", + "HELPER_TOKEN_INFORMATION", + "HELPER_TOKEN_SIZE", + "HELPER_TOKEN_USER", + "SYSTEM_LEDGER_OR_LOCK_REPLACED", + "SYSTEM_HELD_FILE_CHANGED", + "SYSTEM_DB_FILE_ID", + "SYSTEM_CALL_BINDING", + ]; + CODES.iter().copied().find(|code| *code == text) +} +pub fn diagnostic(error: &anyhow::Error) -> Diagnostic { + if let Some(value) = error.downcast_ref::() { + return *value; + } + let code = error + .chain() + .find_map(|cause| known_code(&cause.to_string())) + .unwrap_or("UNCLASSIFIED_IO_OR_CODEC"); + Diagnostic { + stage: error + .downcast_ref::() + .copied() + .unwrap_or(Stage::Unclassified) + .name(), + code, + win32: error.downcast_ref::().map(|v| v.0), + } +} +pub fn remote_denial(stage: Stage, code: &str) -> anyhow::Error { + Diagnostic { + stage: stage.name(), + code: known_code(code).unwrap_or("UNCLASSIFIED_REMOTE_DENIAL"), + win32: None, + } + .into() +} + #[derive(Clone, Debug, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct PeerPin { @@ -118,6 +288,12 @@ pub enum Operation { generation: u64, request: Request, }, + Abandon { + anchor: Box, + request: Request, + intent_id: Uuid, + intent_sha256: String, + }, } #[derive(Clone, Serialize, Deserialize)] #[serde(deny_unknown_fields)] @@ -155,10 +331,14 @@ pub enum ReplyBody { snapshot: Snapshot, unlocked_grants: Vec, ready: bool, + no_start_anchor: Option>, }, Executed { outcome: Outcome, }, + Abandoned { + outcome: Outcome, + }, Denied { code: String, }, @@ -191,10 +371,9 @@ pub fn wide(s: impl AsRef) -> Vec { s.as_ref().encode_wide().chain(Some(0)).collect() } fn owned(raw: HANDLE) -> Result { - ensure!( - !raw.is_null() && raw != INVALID_HANDLE_VALUE, - "HELPER_HANDLE" - ); + if raw.is_null() || raw == INVALID_HANDLE_VALUE { + return Err(api_failure("HELPER_HANDLE")); + } Ok(unsafe { OwnedHandle::from_raw_handle(raw) }) } fn sid_text(sid: PSID) -> Result { @@ -323,7 +502,12 @@ pub fn require_self(peer: &PeerPin) -> Result<()> { let token = owned(token)?; verify_token(token.as_raw_handle(), &peer.user_sid, &peer.service_sid) } -fn check_acl(handle: HANDLE, readers: &[String], writers: &[String]) -> Result<()> { +fn check_acl( + handle: HANDLE, + readers: &[String], + writers: &[String], + writer_mask: Option, +) -> Result<()> { let mut owner = ptr::null_mut(); let mut acl = ptr::null_mut(); let mut descriptor = ptr::null_mut(); @@ -365,6 +549,12 @@ fn check_acl(handle: HANDLE, readers: &[String], writers: &[String]) -> Result<( let sid = sid_text(ptr::addr_of!(ace.SidStart).cast_mut().cast())?; let privileged = matches!(sid.as_str(), "S-1-5-18" | "S-1-5-32-544") || writers.contains(&sid); + if writers.contains(&sid) && !matches!(sid.as_str(), "S-1-5-18" | "S-1-5-32-544") { + ensure!( + writer_mask.is_none_or(|mask| ace.Mask & !mask == 0), + "HELPER_EXCESSIVE_WRITER_RIGHTS" + ); + } ensure!( privileged || (readers.contains(&sid) && ace.Mask & !READ_MASK == 0), "HELPER_PATH_WRITABLE_OR_FOREIGN" @@ -384,6 +574,7 @@ pub struct PinnedFile { readers: Vec, writers: Vec, directory: bool, + writer_mask: Option, } impl PinnedFile { pub fn open( @@ -392,6 +583,28 @@ impl PinnedFile { readers: &[String], writers: &[String], write: bool, + ) -> Result { + Self::open_impl(path, directory, readers, writers, write, false) + } + pub(crate) fn open_read_shared( + path: &Path, + directory: bool, + readers: &[String], + writers: &[String], + ) -> Result { + Self::open_impl(path, directory, readers, writers, false, true) + } + pub(crate) fn limit_writer_rights(&mut self, mask: u32) -> Result<()> { + self.writer_mask = Some(mask); + self.check() + } + fn open_impl( + path: &Path, + directory: bool, + readers: &[String], + writers: &[String], + write: bool, + share_write: bool, ) -> Result { ensure!( path.is_absolute() @@ -427,7 +640,12 @@ impl PinnedFile { } else { GENERIC_READ }, - FILE_SHARE_READ | if write { FILE_SHARE_WRITE } else { 0 }, + FILE_SHARE_READ + | if write || share_write { + FILE_SHARE_WRITE + } else { + 0 + }, ptr::null(), OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT @@ -448,6 +666,7 @@ impl PinnedFile { readers: readers.into(), writers: writers.into(), directory, + writer_mask: None, }; value.check()?; Ok(value) @@ -467,7 +686,12 @@ impl PinnedFile { file_identity(&self.file)? == self.identity, "HELPER_FILE_ID_CHANGED" ); - check_acl(self.file.as_raw_handle(), &self.readers, &self.writers)?; + check_acl( + self.file.as_raw_handle(), + &self.readers, + &self.writers, + self.writer_mask, + )?; let raw = unsafe { CreateFileW( wide(&self.path).as_ptr(), @@ -877,10 +1101,30 @@ impl IoEvent { } let mut count = 0; let complete = unsafe { GetOverlappedResult(pipe, &self.overlapped, &mut count, 0) }; - if complete == 0 && unsafe { GetLastError() } == ERROR_IO_INCOMPLETE { - std::process::abort(); + if complete == 0 { + let error = api_failure("HELPER_PIPE_IO"); + if error + .downcast_ref::() + .is_some_and(|v| v.0 == ERROR_IO_INCOMPLETE) + { + std::process::abort(); + } + return Err(error); + } + Ok(count) + } + fn completed(&self, pipe: HANDLE) -> Result { + let mut count = 0; + if unsafe { GetOverlappedResult(pipe, &self.overlapped, &mut count, 0) } == 0 { + let error = api_failure("HELPER_PIPE_IO"); + if error + .downcast_ref::() + .is_some_and(|v| v.0 == ERROR_IO_INCOMPLETE) + { + std::process::abort(); + } + return Err(error); } - ensure!(complete != 0, "HELPER_PIPE_IO"); Ok(count) } } @@ -890,14 +1134,13 @@ fn transfer(pipe: HANDLE, bytes: &mut [u8], write: bool, deadline: Instant) -> R ensure!(Instant::now() < deadline, "HELPER_PIPE_TIMEOUT"); let mut operation = IoEvent::new()?; let part = &mut bytes[offset..]; - let mut done = 0; let ok = unsafe { if write { WriteFile( pipe, part.as_ptr(), part.len().try_into()?, - &mut done, + ptr::null_mut(), &mut operation.overlapped, ) } else { @@ -905,18 +1148,21 @@ fn transfer(pipe: HANDLE, bytes: &mut [u8], write: bool, deadline: Instant) -> R pipe, part.as_mut_ptr(), part.len().try_into()?, - &mut done, + ptr::null_mut(), &mut operation.overlapped, ) } }; let count = if ok != 0 { - done + // ReadFile/WriteFile are always OVERLAPPED here. Their direct + // byte-counter is unspecified for async handles, even on TRUE. + // Obtain completion bytes from this exact held OVERLAPPED instead. + operation.completed(pipe)? } else { - ensure!( - unsafe { GetLastError() } == ERROR_IO_PENDING, - "HELPER_PIPE_IO" - ); + let error = unsafe { GetLastError() }; + if error != ERROR_IO_PENDING { + return Err(anyhow::anyhow!("HELPER_PIPE_IO").context(CapturedWin32(error))); + } operation.wait(pipe, deadline)? }; ensure!(count > 0 && count as usize <= part.len(), "HELPER_PIPE_EOF"); @@ -1000,37 +1246,42 @@ impl Server { } pub fn receive(&self) -> Result> { let pipe = self.pipe.as_raw_handle(); - let mut connect = IoEvent::new()?; + let mut connect = IoEvent::new().context(Stage::ServerConnect)?; let connected = unsafe { ConnectNamedPipe(pipe, &mut connect.overlapped) }; if connected == 0 { let error = unsafe { GetLastError() }; if error == ERROR_IO_PENDING { - if connect - .wait(pipe, Instant::now() + Duration::from_millis(250)) - .is_err() + if let Err(error) = connect.wait(pipe, Instant::now() + Duration::from_millis(250)) { - unsafe { - DisconnectNamedPipe(pipe); + if diagnostic(&error).code == "HELPER_PIPE_TIMEOUT" { + unsafe { + DisconnectNamedPipe(pipe); + }; + return Ok(None); } - return Ok(None); + return Err(error).context(Stage::ServerConnect); } } else { - ensure!(error == ERROR_PIPE_CONNECTED, "HELPER_PIPE_CONNECT"); + if error != ERROR_PIPE_CONNECTED { + return Err( + anyhow::anyhow!("HELPER_PIPE_CONNECT").context(CapturedWin32(error)) + ) + .context(Stage::ServerConnect); + } } } let deadline = Instant::now() + Duration::from_secs(5); - let len = length(pipe, deadline)?; - let mut peer = PeerProof::capture(pipe, &self.pin.agent, true)?; - ensure!( - unsafe { ImpersonateNamedPipeClient(pipe) } != 0, - "HELPER_PIPE_IMPERSONATION" - ); + let len = length(pipe, deadline).context(Stage::ServerReadHeader)?; + let mut peer = + PeerProof::capture(pipe, &self.pin.agent, true).context(Stage::ServerClientPeer)?; + if unsafe { ImpersonateNamedPipeClient(pipe) } == 0 { + return Err(api_failure("HELPER_PIPE_IMPERSONATION")).context(Stage::ServerImpersonate); + } let auth = (|| { let mut token = ptr::null_mut(); - ensure!( - unsafe { OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, 1, &mut token) } != 0, - "HELPER_PIPE_IMPERSONATED_TOKEN" - ); + if unsafe { OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, 1, &mut token) } == 0 { + return Err(api_failure("HELPER_PIPE_IMPERSONATED_TOKEN")); + } let token = owned(token)?; verify_token( token.as_raw_handle(), @@ -1041,17 +1292,17 @@ impl Server { if unsafe { RevertToSelf() } == 0 { std::process::abort(); } - auth?; - require_self(&self.pin.helper)?; - let call: Call = body(pipe, len, deadline)?; - ensure!( - call.schema_version == 1 - && !call.challenge.is_nil() - && call.policy_sha256 == self.pin.policy_sha256 - && binding_hash(&call.binding)? == binding_hash(&self.pin.binding)?, - "HELPER_CALL_BINDING" - ); - peer.check()?; + auth.context(Stage::ServerVerifyToken)?; + require_self(&self.pin.helper).context(Stage::ServerVerifyToken)?; + let call: Call = body(pipe, len, deadline).context(Stage::ServerReadBody)?; + if !(call.schema_version == 1 + && !call.challenge.is_nil() + && call.policy_sha256 == self.pin.policy_sha256 + && binding_hash(&call.binding)? == binding_hash(&self.pin.binding)?) + { + return Err(anyhow::anyhow!("HELPER_CALL_BINDING")).context(Stage::ServerCallBinding); + } + peer.check().context(Stage::ServerRecheckPeer)?; Ok(Some(Received { call, peer })) } pub fn reply(&self, value: &Reply) -> Result<()> { @@ -1060,6 +1311,7 @@ impl Server { value, Instant::now() + Duration::from_secs(5), ) + .context(Stage::ServerReply) } pub fn disconnect(&self) { unsafe { @@ -1070,7 +1322,7 @@ impl Server { /// Blocking SDK call; run on the agent's bounded blocking worker, never heartbeat reactor. pub fn exchange(pin: &Pin, call: &Call, timeout: Duration) -> Result { pin.validate()?; - require_self(&pin.agent)?; + require_self(&pin.agent).context(Stage::ClientSelfRole)?; ensure!( (Duration::from_secs(1)..=Duration::from_secs(125)).contains(&timeout), "HELPER_CALL_TIMEOUT_BOUND" @@ -1087,31 +1339,33 @@ pub fn exchange(pin: &Pin, call: &Call, timeout: Duration) -> Result { ptr::null_mut(), ) }) - .context("HELPER_PIPE_UNAVAILABLE_OR_BUSY")?; - let mut peer = PeerProof::capture(pipe.as_raw_handle(), &pin.helper, false)?; - send(pipe.as_raw_handle(), call, deadline)?; - let len = length(pipe.as_raw_handle(), deadline)?; - let reply: Reply = body(pipe.as_raw_handle(), len, deadline)?; - peer.check()?; - ensure!( - reply.schema_version == 1 - && reply.challenge == call.challenge - && reply.binding_sha256 == binding_hash(&pin.binding)? - && reply.policy_sha256 == pin.policy_sha256 - && reply.helper_binary_sha256 == pin.helper.binary_sha256, - "HELPER_REPLY_BINDING" - ); - ensure!( - matches!( - (&call.operation, &reply.body), - (_, ReplyBody::Denied { .. }) - | (Operation::Probe { .. }, ReplyBody::Probe { .. }) - | (Operation::Snapshot { .. }, ReplyBody::Snapshot { .. }) - | (Operation::Apply { .. }, ReplyBody::Applied { .. }) - | (Operation::Execute { .. }, ReplyBody::Executed { .. }) - ), - "HELPER_REPLY_OPERATION" - ); + .context("HELPER_PIPE_UNAVAILABLE_OR_BUSY") + .context(Stage::ClientOpenPipe)?; + let mut peer = PeerProof::capture(pipe.as_raw_handle(), &pin.helper, false) + .context(Stage::ClientHelperPeer)?; + send(pipe.as_raw_handle(), call, deadline).context(Stage::ClientSendFrame)?; + let len = length(pipe.as_raw_handle(), deadline).context(Stage::ClientReadReply)?; + let reply: Reply = body(pipe.as_raw_handle(), len, deadline).context(Stage::ClientReadReply)?; + peer.check().context(Stage::ClientHelperPeer)?; + if !(reply.schema_version == 1 + && reply.challenge == call.challenge + && reply.binding_sha256 == binding_hash(&pin.binding)? + && reply.policy_sha256 == pin.policy_sha256 + && reply.helper_binary_sha256 == pin.helper.binary_sha256) + { + return Err(anyhow::anyhow!("HELPER_REPLY_BINDING")).context(Stage::ClientReplyBinding); + } + if !matches!( + (&call.operation, &reply.body), + (_, ReplyBody::Denied { .. }) + | (Operation::Probe { .. }, ReplyBody::Probe { .. }) + | (Operation::Snapshot { .. }, ReplyBody::Snapshot { .. }) + | (Operation::Apply { .. }, ReplyBody::Applied { .. }) + | (Operation::Execute { .. }, ReplyBody::Executed { .. }) + | (Operation::Abandon { .. }, ReplyBody::Abandoned { .. }) + ) { + return Err(anyhow::anyhow!("HELPER_REPLY_OPERATION")).context(Stage::ClientReplyBinding); + } Ok(reply) } @@ -1155,6 +1409,14 @@ mod tests { assert!( serde_json::from_str::(r#"{"operation":"probe","ready":true}"#).is_err() ); + let caller = + anyhow::anyhow!("caller-payload-private-marker").context(Stage::ServerReadBody); + let encoded = serde_json::to_string(&diagnostic(&caller)).unwrap(); + assert!(!encoded.contains("caller-payload-private-marker")); + assert_eq!(diagnostic(&caller).stage, "server_read_body"); + assert_eq!(diagnostic(&caller).code, "UNCLASSIFIED_IO_OR_CODEC"); + let remote = remote_denial(Stage::ClientReplyBinding, "caller-payload-private-marker"); + assert_eq!(diagnostic(&remote).code, "UNCLASSIFIED_REMOTE_DENIAL"); } #[test] fn reconfigured_service_cannot_adopt_an_old_running_identity() { diff --git a/crates/agent-windows/src/helper_runtime.rs b/crates/agent-windows/src/helper_runtime.rs index cf7e3c8..85e3837 100644 --- a/crates/agent-windows/src/helper_runtime.rs +++ b/crates/agent-windows/src/helper_runtime.rs @@ -2,7 +2,12 @@ //! 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_agent_core::{ + auth::{Binding, Outcome, Request}, + helper_reconcile::{NoStartAnchor, Snapshot}, + reconcile::HelperSnapshot, +}; +use rmm_domain::jobs::{Action, JobPayload}; use rmm_protocol::{SyncAttempt, SyncResponse}; use serde_json::json; use std::{ @@ -12,6 +17,8 @@ use std::{ time::Duration, }; use tokio::sync::Semaphore; +#[path = "helper_intent.rs"] +pub mod intent; 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"; @@ -20,6 +27,16 @@ pub struct Client { pin: ipc::Pin, lane: Arc, } +pub struct ControlSnapshot { + pub raw: Snapshot, + pub no_start_anchor: Option, +} +pub struct BoundOutcome { + pub job_id: uuid::Uuid, + pub attempt_id: uuid::Uuid, + pub grant_id: uuid::Uuid, + pub outcome: Outcome, +} impl Client { /// Missing pin is deliberately passive. A present but invalid pin is refused; /// no endpoint/key/policy is discovered from the pipe itself. @@ -72,12 +89,13 @@ impl Client { tokio::task::spawn_blocking(move || { let _permit = permit; let result=(|| { - let client=Self::discover(&agent_id,&asset_id,binding.as_ref(),lane)?; + let client=Self::discover(&agent_id,&asset_id,binding.as_ref(),lane).context(ipc::Stage::DiscoverPin)?; 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"}))?; + let diagnostic=result.as_ref().err().map(ipc::diagnostic).unwrap_or(ipc::Diagnostic{stage:"discover_pin",code:"HELPER_PIN_NOT_CONFIGURED",win32:None}); + 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","diagnostic":diagnostic}))?; } result }) @@ -100,7 +118,135 @@ impl Client { pub fn pin_hash(&self) -> &str { &self.pin.policy_sha256 } + pub fn helper_binary_hash(&self) -> &str { + &self.pin.helper.binary_sha256 + } + /// Import only independently authenticated raw helper evidence. c+start is + /// atomic through the existing core API. A crash before outcome persistence + /// stays locked; the next fresh snapshot repairs that exact start idempotently. + pub fn mirror( + &self, + snapshot: &Snapshot, + ledger: &mut rusqlite::Connection, + observed_at: chrono::DateTime, + ) -> Result> { + use rmm_agent_core::{crypto::DalekVerifier, ledger as core_ledger}; + use rusqlite::OptionalExtension; + if !Path::new(intent::DB).try_exists()? { + // Optional provisioning must not block the unprivileged provider. + // Retained SYSTEM consumption, however, may never lose its store. + let mut rows = ledger.prepare("SELECT start FROM execution_evidence")?; + for row in rows.query_map([], |r| r.get::<_, Vec>(0))? { + let value: serde_json::Value = serde_json::from_slice(&row?)?; + let request: Request = serde_json::from_value(value["request"].clone())?; + ensure!( + self.pin.binding.job(&request.job_bytes)?.execution_identity != "system", + "SYSTEM_INTENT_STORE_MISSING" + ); + } + return Ok(Vec::new()); + } + let mut store = intent::Store::open(true)?; + for intent in store.pending()? { + self.pin.binding.verify_historical_for_denial_with( + &intent.request, + rmm_agent_core::auth::HelperIdentity::System, + &DalekVerifier, + )?; + let job = self.pin.binding.job(&intent.request.job_bytes)?; + let permit = intent.permit()?; + ensure!( + self.supports_system(&job) + && intent.policy_sha256 == self.pin.policy_sha256 + && intent.helper_binary_sha256 == self.pin.helper.binary_sha256 + && snapshot.boot_id == intent.original_boot_id + && snapshot.journal_id == intent.anchor.journal_id() + && snapshot.checkpoint >= intent.anchor.checkpoint() + && observed_at >= intent.recorded_at + && observed_at >= snapshot.captured_at, + "SYSTEM_MIRROR_CONTEXT" + ); + let mut matches = snapshot + .attempts + .iter() + .filter(|a| a.job_id == job.job_id && a.attempt_id == job.attempt_id); + let row = matches + .next() + .context("SYSTEM_DELEGATION_NO_HELPER_EVIDENCE")?; + ensure!(matches.next().is_none(), "SYSTEM_DUPLICATE_HELPER_ATTEMPT"); + let outcome = + verified_mirror_row(row, &job, &permit, &intent.request, intent.original_boot_id)?; + let existing:Option<(String,u64,String,String,Vec)>=ledger.query_row("SELECT c.agent,c.generation,c.job,c.attempt,e.start FROM consumed c JOIN execution_evidence e USING(grant_id) WHERE c.grant_id=?1",[permit.grant_id.to_string()],|r|Ok((r.get(0)?,r.get(1)?,r.get(2)?,r.get(3)?,r.get(4)?))).optional()?; + if let Some((agent, generation, j, a, start)) = existing { + let start: serde_json::Value = serde_json::from_slice(&start)?; + let request: Request = serde_json::from_value(start["request"].clone())?; + ensure!( + agent == job.agent_id.to_string() + && generation == job.enrollment_generation + && j == job.job_id.to_string() + && a == job.attempt_id.to_string() + && serde_json::to_vec(&request)? == serde_json::to_vec(&intent.request)? + && start["previous_boot_id"] == intent.original_boot_id.to_string() + && serde_json::from_value::>( + start["reserved_at"].clone() + )? == intent.recorded_at, + "SYSTEM_MIRROR_LOCAL_START_CONFLICT" + ); + } else { + core_ledger::begin_execution( + ledger, + &permit, + &intent.request, + intent.original_boot_id, + intent.recorded_at, + )?; + } + core_ledger::persist_outcome(ledger, &permit, &outcome)?; + store.mirrored(intent.id, &row.outcome_sha256, observed_at)?; + } + let mut unknowns = Vec::new(); + for row in snapshot + .attempts + .iter() + .filter(|row| row.state == "unknown") + { + let intent = store.by_grant(row.grant_id)?; + self.pin.binding.verify_historical_for_denial_with( + &intent.request, + rmm_agent_core::auth::HelperIdentity::System, + &DalekVerifier, + )?; + let job = self.pin.binding.job(&intent.request.job_bytes)?; + let permit = intent.permit()?; + ensure!( + self.supports_system(&job) + && intent.policy_sha256 == self.pin.policy_sha256 + && intent.helper_binary_sha256 == self.pin.helper.binary_sha256 + && snapshot.boot_id == intent.original_boot_id + && snapshot.journal_id == intent.anchor.journal_id() + && snapshot.checkpoint >= intent.anchor.checkpoint(), + "SYSTEM_MIRROR_CONTEXT" + ); + let outcome = + verified_mirror_row(row, &job, &permit, &intent.request, intent.original_boot_id)?; + unknowns.push(BoundOutcome { + job_id: job.job_id, + attempt_id: job.attempt_id, + grant_id: permit.grant_id, + outcome, + }); + } + store.check()?; + Ok(unknowns) + } async fn call(&self, operation: ipc::Operation) -> Result { + self.call_bounded(operation, Duration::from_secs(5)).await + } + async fn call_bounded( + &self, + operation: ipc::Operation, + timeout: Duration, + ) -> Result { let permit = self .lane .clone() @@ -111,17 +257,20 @@ impl Client { 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))?; + let reply = ipc::exchange(&client.pin, &call, timeout)?; client.check_pin()?; Ok(reply) }) .await? } fn probe_blocking(&self, state: &Path) -> Result<()> { - self.check_pin()?; + self.check_pin().context(ipc::Stage::DiscoverPin)?; let pin = &self.pin; let call = ipc::Call::new(pin, ipc::Operation::Probe {}); let reply = ipc::exchange(pin, &call, Duration::from_secs(5))?; + if let ipc::ReplyBody::Denied { code } = &reply.body { + return Err(ipc::remote_denial(ipc::Stage::ClientReplyBinding, code)); + } let ipc::ReplyBody::Probe { clock_qualified, ready, @@ -133,22 +282,123 @@ impl Client { }; 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) + write_current_probe(state, &value).context(ipc::Stage::ClientReport) } - pub async fn snapshot(&self, generation: u64, boot: uuid::Uuid) -> Result { + /// Pure admission shape only; no capability or qualification is asserted. + pub fn supports_system(&self, job: &JobPayload) -> bool { + system_shape(job) + && self.pin.binding.tenant_id == job.tenant_id + && self.pin.binding.asset_id == job.asset_id + && self.pin.binding.agent_id == job.agent_id + && self.pin.binding.enrollment_generation == job.enrollment_generation + && self.pin.binding.assignment_version == job.assignment_version + } + /// Delegate ONCE. A lost reply is unknown and must be recovered by signed + /// Snapshot/Apply, never by resending Execute. A timed-out blocking call + /// retains its one lane permit until it actually returns; no replacement. + pub async fn delegate( + &self, + intent: intent::Intent, + admit: impl Fn() -> Result<()> + Send + 'static, + ) -> Result<()> { + let job = self.pin.binding.job(&intent.request.job_bytes)?; + ensure!( + intent.connection_generation > 0 && self.supports_system(&job), + "SYSTEM_TYPED_SERVICE_REQUIRED" + ); + let timeout = execution_timeout(job.max_runtime_seconds)?; + let permit = self + .lane + .clone() + .try_acquire_owned() + .context("HELPER_CONTROL_BUSY")?; + let client = self.clone(); + tokio::time::timeout( + timeout, + tokio::task::spawn_blocking(move || { + let _permit = permit; + client.check_pin()?; + let mut store = intent::Store::open(true)?; + let (retained, _) = store.load(intent.id)?; + ensure!( + retained.digest()? == intent.digest()?, + "SYSTEM_INTENT_CHANGED" + ); + let phase = store.decide(intent.id, admit().is_ok())?; + let operation = match phase { + intent::Phase::KnownNoDispatch => ipc::Operation::Abandon { + anchor: Box::new(intent.anchor.clone()), + request: intent.request.clone(), + intent_id: intent.id, + intent_sha256: intent.digest()?, + }, + intent::Phase::MayHaveDispatched => { + admit()?; + ipc::Operation::Execute { + generation: intent.connection_generation, + request: intent.request.clone(), + } + } + intent::Phase::Prepared => anyhow::bail!("SYSTEM_INTENT_NOT_DURABLE"), + }; + let call = ipc::Call::new(&client.pin, operation); + let reply = ipc::exchange(&client.pin, &call, timeout)?; + client.check_pin()?; + let outcome = match reply.body { + ipc::ReplyBody::Executed { outcome } + | ipc::ReplyBody::Abandoned { outcome } => outcome, + _ => anyhow::bail!("SYSTEM_HELPER_DELEGATION_DENIED"), + }; + checked_outcome(&job, &outcome)?; + // A reply is never the consumption/mirror witness. Every outcome, + // including a successful reply, requires a fresh raw snapshot. + Ok::<_, anyhow::Error>(()) + }), + ) + .await + .context("SYSTEM_HELPER_REPLY_UNKNOWN")?? + } + pub async fn resume_denial(&self, intent: intent::Intent) -> Result<()> { + let store = intent::Store::open(false)?; + let (retained, phase) = store.load(intent.id)?; + ensure!( + phase == intent::Phase::KnownNoDispatch && retained.digest()? == intent.digest()?, + "SYSTEM_DENIAL_RESUME_BINDING" + ); + let operation = ipc::Operation::Abandon { + anchor: Box::new(intent.anchor.clone()), + request: intent.request.clone(), + intent_id: intent.id, + intent_sha256: intent.digest()?, + }; + // Receipt is persisted by the runtime before this method. A timeout + // cannot release the blocking lane or cause another Abandon this process. + let reply = tokio::time::timeout(Duration::from_secs(5), self.call(operation)) + .await + .context("SYSTEM_ABANDON_REPLY_UNKNOWN")??; + ensure!( + matches!(reply.body, ipc::ReplyBody::Abandoned { .. }), + "SYSTEM_ABANDON_DENIED" + ); + Ok(()) + } + 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)) + Ok(ControlSnapshot { + raw: snapshot, + no_start_anchor: None, + }) } pub async fn apply( &self, response: &SyncResponse, request_bytes: Vec, boot: uuid::Uuid, - ) -> Result { + ) -> Result { let reply = self .call(ipc::Operation::Apply { response: rmm_agent_core::helper_reconcile::SignedResponse { @@ -158,14 +408,112 @@ impl Client { request_bytes, }) .await?; - let ipc::ReplyBody::Applied { snapshot, .. } = reply.body else { + let ipc::ReplyBody::Applied { + snapshot, + no_start_anchor, + .. + } = reply.body + else { anyhow::bail!("HELPER_APPLY_DENIED"); }; ensure!(snapshot.boot_id == boot, "HELPER_BOOT_MISMATCH"); - Ok(map_snapshot(&snapshot)) + if let Some(anchor) = &no_start_anchor { + ensure!( + anchor.journal_id() == snapshot.journal_id + && anchor.checkpoint() == snapshot.checkpoint + && Some(anchor.decision_id()) == snapshot.previous_decision_id + && anchor.boot_id() == boot, + "SYSTEM_ANCHOR_SNAPSHOT_BINDING" + ); + } + Ok(ControlSnapshot { + raw: snapshot, + no_start_anchor: no_start_anchor.map(|a| *a), + }) } } -fn map_snapshot(s: &Snapshot) -> HelperSnapshot { +fn verified_mirror_row( + row: &rmm_agent_core::helper_reconcile::LocalAttempt, + job: &JobPayload, + permit: &rmm_domain::jobs::ExecutionPermitPayload, + request: &Request, + boot: uuid::Uuid, +) -> Result { + use rmm_domain::{ + jobs::hash, + reconciliation::{ReconciledOutcome, canonical_outcome_bytes}, + }; + ensure!( + row.job_id == job.job_id + && row.attempt_id == job.attempt_id + && row.job_payload_hash == permit.job_payload_hash + && row.grant_id == permit.grant_id + && row.authorization_id == permit.authorization_id + && row.plan_hash == permit.plan_hash + && row.action == permit.action + && row.execution_identity == "system" + && row.original_boot_id == boot + && row.original_request_sha256 == hash(&serde_json::to_vec(request)?) + && row.consumption == "consumed" + && !row.outcome_bytes.is_empty(), + "SYSTEM_MIRROR_ORIGINAL_TUPLE" + ); + let canonical: ReconciledOutcome = serde_json::from_slice(&row.outcome_bytes)?; + ensure!( + canonical_outcome_bytes(&canonical)? == row.outcome_bytes + && hash(&row.outcome_bytes) == row.outcome_sha256 + && canonical.state == row.state, + "SYSTEM_MIRROR_CANONICAL_OUTCOME" + ); + let outcome: Outcome = serde_json::from_slice(&row.outcome_bytes)?; + checked_outcome(job, &outcome)?; + Ok(outcome) +} +fn system_shape(job: &JobPayload) -> bool { + job.execution_identity == "system" + && (1..=120).contains(&job.max_runtime_seconds) + && (1..=65536).contains(&job.max_output_bytes) + && matches!(&job.action,Action::SetServiceState{unit,state} if unit.starts_with("RmmDiscoveryFixture")&&unit.len()>"RmmDiscoveryFixture".len()&&unit.len()<=80&&unit.bytes().all(|b|b.is_ascii_alphanumeric()||b"_-".contains(&b))&&matches!(state.as_str(),"running"|"stopped")) +} +fn execution_timeout(seconds: u32) -> Result { + ensure!( + (1..=120).contains(&seconds), + "SYSTEM_EXECUTION_TIMEOUT_BOUND" + ); + Ok(Duration::from_secs(u64::from(seconds) + 5)) +} +pub(super) fn checked_outcome(job: &JobPayload, outcome: &Outcome) -> Result<()> { + ensure!( + matches!( + outcome.state.as_str(), + "succeeded" | "failed" | "cancelled" | "expired" | "unknown" + ) && !outcome.reboot_required + && outcome + .stdout + .len() + .checked_add(outcome.stderr.len()) + .is_some_and(|n| n <= job.max_output_bytes as usize), + "SYSTEM_EXECUTE_OUTCOME_SCOPE" + ); + if outcome.state == "succeeded" { + let Action::SetServiceState { unit, state } = &job.action else { + anyhow::bail!("SYSTEM_TYPED_SERVICE_REQUIRED"); + }; + let wanted = if state == "running" { 4 } else { 1 }; + ensure!( + outcome.exit_code == Some(0) + && !outcome.output_truncated + && outcome.evidence["postconditions_passed"] == true + && outcome.evidence["provider"] == "windows.scm/1" + && outcome.evidence["service"] == unit.as_str() + && outcome.evidence["requested_state"] == wanted + && outcome.evidence["final_status"]["state"] == wanted, + "SYSTEM_SERVICE_POSTCONDITION_BINDING" + ); + } + Ok(()) +} +pub fn map_snapshot(s: &Snapshot) -> HelperSnapshot { HelperSnapshot { journal_id: s.journal_id, checkpoint: s.checkpoint, @@ -221,3 +569,156 @@ fn write_current_probe(state: &Path, value: &serde_json::Value) -> Result<()> { } result } + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn system_route_binds_original_request_shape_budget_and_reply_scope() { + let id = uuid::Uuid::new_v4(); + let now = chrono::Utc::now(); + let mut job = JobPayload { + schema_version: 1, + tenant_id: id, + asset_id: id, + agent_id: id, + enrollment_generation: 1, + assignment_version: 1, + job_id: id, + attempt_id: id, + action: Action::SetServiceState { + unit: "RmmDiscoveryFixtureService".into(), + state: "running".into(), + }, + execution_identity: "system".into(), + not_before: now, + start_before: now + chrono::Duration::seconds(60), + max_runtime_seconds: 30, + max_output_bytes: 1024, + }; + assert!(system_shape(&job)); + for identity in ["root", "agent_service"] { + job.execution_identity = identity.into(); + assert!(!system_shape(&job)); + } + job.execution_identity = "system".into(); + job.action = Action::SetServiceState { + unit: "RmmDiscoveryFixtureService".into(), + state: "restarted".into(), + }; + assert!(!system_shape(&job)); + job.action = Action::SetServiceState { + unit: "RmmDiscoveryWindows".into(), + state: "stopped".into(), + }; + assert!(!system_shape(&job)); + assert_eq!(execution_timeout(120).unwrap(), Duration::from_secs(125)); + assert!(execution_timeout(0).is_err()); + assert!(execution_timeout(121).is_err()); + let mut outcome = Outcome::unknown("LOST_REPLY"); + assert!(checked_outcome(&job, &outcome).is_ok()); + outcome.reboot_required = true; + assert!(checked_outcome(&job, &outcome).is_err()); + outcome.reboot_required = false; + outcome.stdout = vec![0; 1025]; + assert!(checked_outcome(&job, &outcome).is_err()); + job.action = Action::SetServiceState { + unit: "RmmDiscoveryFixtureService".into(), + state: "running".into(), + }; + outcome.stdout.clear(); + outcome.state = "succeeded".into(); + outcome.exit_code = Some(0); + assert!(checked_outcome(&job, &outcome).is_err()); + outcome.evidence = json!({"postconditions_passed":true,"provider":"windows.scm/1","service":"RmmDiscoveryFixtureService","requested_state":4,"final_status":{"state":4}}); + assert!(checked_outcome(&job, &outcome).is_ok()); + outcome.evidence["final_status"]["state"] = json!(1); + assert!(checked_outcome(&job, &outcome).is_err()); + let request = Request { + job_bytes: vec![b' ', b'{', b'}', b'\n'], + permit_bytes: vec![0, 1, 2, 255], + signature: vec![7; 64], + plan_hash: "ab".repeat(32), + authorization_id: id, + }; + let operation = ipc::Operation::Execute { + generation: 73, + request: request.clone(), + }; + let ipc::Operation::Execute { + generation, + request: retained, + } = serde_json::from_slice(&serde_json::to_vec(&operation).unwrap()).unwrap() + else { + panic!("typed execute required") + }; + assert_eq!(generation, 73); + assert_eq!( + serde_json::to_vec(&retained).unwrap(), + serde_json::to_vec(&request).unwrap() + ); + // Pure mirror-binding negatives, not a signature/OS authentication proof. + let permit = rmm_domain::jobs::ExecutionPermitPayload { + domain: "rmm.execution-permit".into(), + schema_version: 1, + grant_id: id, + authorization_id: id, + tenant_id: id, + asset_id: id, + agent_id: id, + enrollment_generation: 1, + assignment_version: 1, + job_id: id, + attempt_id: id, + plan_hash: request.plan_hash.clone(), + job_payload_hash: rmm_domain::jobs::hash(&request.job_bytes), + action: "set_service_state".into(), + execution_identity: "system".into(), + not_before: now, + start_before: now + chrono::Duration::seconds(60), + max_runtime_seconds: 30, + key_id: "cd".repeat(32), + }; + let canonical: rmm_domain::reconciliation::ReconciledOutcome = serde_json::from_value( + serde_json::to_value(Outcome::unknown("RETAINED_UNKNOWN")).unwrap(), + ) + .unwrap(); + let bytes = rmm_domain::reconciliation::canonical_outcome_bytes(&canonical).unwrap(); + let row = rmm_agent_core::helper_reconcile::LocalAttempt { + job_id: id, + attempt_id: id, + job_payload_hash: permit.job_payload_hash.clone(), + grant_id: id, + authorization_id: id, + plan_hash: permit.plan_hash.clone(), + action: permit.action.clone(), + execution_identity: "system".into(), + revision: 1, + original_boot_id: id, + consumption: "consumed".into(), + state: "unknown".into(), + outcome_sha256: rmm_domain::jobs::hash(&bytes), + outcome_bytes: bytes, + original_request_sha256: rmm_domain::jobs::hash(&serde_json::to_vec(&request).unwrap()), + }; + assert!(verified_mirror_row(&row, &job, &permit, &request, id).is_ok()); + for field in [ + "authorization_id", + "original_request_sha256", + "original_boot_id", + "outcome_sha256", + ] { + let mut changed = serde_json::to_value(&row).unwrap(); + changed[field] = if field.ends_with("sha256") { + json!("ef".repeat(32)) + } else { + json!(uuid::Uuid::new_v4()) + }; + let changed = serde_json::from_value(changed).unwrap(); + assert!( + verified_mirror_row(&changed, &job, &permit, &request, id).is_err(), + "{field}" + ); + } + } +} diff --git a/crates/agent-windows/src/helper_system.rs b/crates/agent-windows/src/helper_system.rs index 4582329..a50ec24 100644 --- a/crates/agent-windows/src/helper_system.rs +++ b/crates/agent-windows/src/helper_system.rs @@ -25,7 +25,10 @@ use fs2::FileExt; use rmm_agent_core::{ auth::{Binding, HelperIdentity, Outcome, Request}, crypto::DalekVerifier, - helper_reconcile::{BootSource, ExecutorLedgerGuard, HelperSync}, + helper_reconcile::{ + BootSource, ExecutorLedgerGuard, HelperSync, NoDispatchObservation, NoStartAnchor, + NoStartGuard, + }, ledger, }; use rmm_domain::jobs::{Action, ExecutionPermitPayload, JobPayload, hash}; @@ -275,6 +278,48 @@ impl ExecutorLedgerGuard for LedgerGuard { } } +/// All data other than the immutable intent reference comes from held local +/// handles. The IPC caller cannot manufacture a NoDispatchObservation. +struct DenialGuard<'a> { + ledger: &'a LedgerGuard, + pin: &'a Pin, + intent: Option<(Uuid, &'a str)>, +} +impl ExecutorLedgerGuard for DenialGuard<'_> { + fn check(&self, db: &Connection) -> Result<()> { + ipc::require_self(&self.pin.helper)?; + self.ledger.check(db) + } +} +impl NoStartGuard for DenialGuard<'_> { + fn live_instance_sha256(&self) -> Result { + let pid = std::process::id(); + let peer = ipc::PeerProof::for_service(&self.pin.helper, pid)?; + Ok(hash(&serde_json::to_vec(&( + peer.identity(), + self.ledger.ledger.identity, + self.ledger.lock.identity, + ))?)) + } + fn verify_no_dispatch( + &self, + original: &Request, + anchor: &NoStartAnchor, + ) -> Result { + let (id, digest) = self + .intent + .context("SYSTEM_NO_DISPATCH_REFERENCE_REQUIRED")?; + let mut store = crate::helper_runtime::intent::Store::open(false)?; + let (intent, _) = store.load(id)?; + ensure!( + intent.policy_sha256 == self.pin.policy_sha256 + && intent.helper_binary_sha256 == self.pin.helper.binary_sha256, + "SYSTEM_NO_DISPATCH_PIN_BINDING" + ); + store.verify_no_dispatch(id, digest, original, anchor) + } +} + struct ServiceHandle(SC_HANDLE); impl Drop for ServiceHandle { fn drop(&mut self) { @@ -727,6 +772,7 @@ pub struct SystemHelper { clock: NativeClock, boot: WindowsBoot, sync: Option>, + last_diagnostic: Option, } impl SystemHelper { pub fn open() -> Result { @@ -749,11 +795,59 @@ impl SystemHelper { clock, boot, sync, + last_diagnostic: None, }) } pub fn pin(&self) -> &Pin { &self.pin } + /// One bounded private bootstrap diagnostic, never inventory or payload. + /// Diagnostic failure is intentionally ignored by the service loop; the + /// original security failure still closes Sync and disconnects the pipe. + fn record_pipe_error(&mut self, error: &anyhow::Error) -> Result<()> { + if self + .last_diagnostic + .is_some_and(|at| at.elapsed() < Duration::from_secs(30)) + { + return Ok(()); + } + self.last_diagnostic = Some(Instant::now()); + self.recheck()?; + let path = Path::new(STATE_ROOT).join("pipe-last-error.json"); + let pending = Path::new(STATE_ROOT).join(".pipe-last-error.pending"); + let writers = vec![self.settings.helper.service_sid.clone()]; + if path.try_exists()? { + let _old = PinnedFile::open(&path, false, &[], &writers, false)?; + } + let diagnostic = ipc::diagnostic(error); + let bytes = serde_json::to_vec( + &serde_json::json!({"pid":std::process::id(),"observed_at_utc":chrono::Utc::now(),"diagnostic":diagnostic}), + )?; + ensure!(bytes.len() <= 1024, "SYSTEM_DIAGNOSTIC_BOUND"); + use std::io::Write; + let mut file = OpenOptions::new() + .write(true) + .create_new(true) + .open(&pending)?; + let identity = ipc::file_identity(&file)?; + let result = (|| -> Result<()> { + { + let _check = PinnedFile::open_read_shared(&pending, false, &[], &writers)?; + } + file.write_all(&bytes)?; + file.sync_all()?; + Ok(()) + })(); + drop(file); + result?; + std::fs::rename(&pending, &path)?; + let saved = PinnedFile::open(&path, false, &[], &writers, false)?; + ensure!( + saved.identity == identity, + "SYSTEM_DIAGNOSTIC_FILE_REPLACED" + ); + Ok(()) + } fn recheck(&mut self) -> Result<()> { ipc::require_self(&self.settings.helper)?; for file in &mut self.files { @@ -829,10 +923,33 @@ impl SystemHelper { &self.clock, &self.boot, )?; + let no_start_anchor = if value.ready && self.settings.enable_effects { + let guard = DenialGuard { + ledger: &self.guard, + pin: &self.pin, + intent: None, + }; + let generation = self + .sync + .as_ref() + .unwrap() + .active_generation() + .context("SYSTEM_READY_GENERATION_REQUIRED")?; + Some(Box::new(self.sync.as_ref().unwrap().no_start_anchor( + &self.db, + &guard, + generation, + &self.clock, + &self.boot, + )?)) + } else { + None + }; Ok(ReplyBody::Applied { snapshot: value.snapshot, unlocked_grants: value.unlocked_grants, ready: value.ready && self.settings.enable_effects, + no_start_anchor, }) } Operation::Execute { @@ -841,6 +958,40 @@ impl SystemHelper { } => Ok(ReplyBody::Executed { outcome: self.execute(*generation, request, peer)?, }), + Operation::Abandon { + anchor, + request, + intent_id, + intent_sha256, + } => { + ensure!( + !intent_id.is_nil() && ipc::digest(intent_sha256), + "SYSTEM_NO_DISPATCH_REFERENCE_REQUIRED" + ); + let job = self.settings.binding.job(&request.job_bytes)?; + ensure!( + job.execution_identity == "system" + && job.max_runtime_seconds <= self.settings.max_runtime_seconds + && matches!(&job.action,Action::SetServiceState{unit,state} if matches!(state.as_str(),"running"|"stopped")&&self.settings.allowed_services.iter().any(|r|r.name==*unit)), + "SYSTEM_ABANDON_POLICY_DENIED" + ); + self.sync()?; + let guard = DenialGuard { + ledger: &self.guard, + pin: &self.pin, + intent: Some((*intent_id, intent_sha256)), + }; + Ok(ReplyBody::Abandoned { + outcome: self.sync.as_mut().unwrap().abandon_no_start( + &mut self.db, + &guard, + anchor, + request, + &self.clock, + &self.boot, + )?, + }) + } }; let response = response?; peer.check()?; @@ -1028,19 +1179,22 @@ unsafe extern "system" fn service_main(_: u32, _: *mut *mut u16) { match server.receive() { Ok(Some(mut incoming)) => { let reply = helper.handle(&incoming.call, &mut incoming.peer); - if server.reply(&reply).is_err() - && let Some(sync) = &mut helper.sync - { - sync.close(); + if let Err(error) = server.reply(&reply) { + if let Some(sync) = &mut helper.sync { + sync.close(); + } + server.disconnect(); + let _diagnostic = helper.record_pipe_error(&error); } server.disconnect(); } Ok(None) => {} - Err(_) => { + Err(error) => { if let Some(sync) = &mut helper.sync { sync.close(); } server.disconnect(); + let _diagnostic = helper.record_pipe_error(&error); } } } diff --git a/crates/agent-windows/src/runtime.rs b/crates/agent-windows/src/runtime.rs index 1caeb85..bf8e85a 100644 --- a/crates/agent-windows/src/runtime.rs +++ b/crates/agent-windows/src/runtime.rs @@ -135,14 +135,20 @@ impl SyncClock for JobClock { #[derive(Default)] struct EffectFence { generation: AtomicU64, + helper_generation: AtomicU64, deadline: AtomicI64, } impl EffectFence { fn close(&self) { self.generation.store(0, Ordering::SeqCst); + self.helper_generation.store(0, Ordering::SeqCst); } fn allows(&self, generation: u64, clock: &impl SyncClock) -> bool { - self.generation.load(Ordering::SeqCst) == generation + self.allows_identity(generation, false, clock) + } + fn allows_identity(&self, generation: u64, helper: bool, clock: &impl SyncClock) -> bool { + (!helper || self.helper_generation.load(Ordering::SeqCst) == generation) + && self.generation.load(Ordering::SeqCst) == generation && generation > 0 && clock.now().is_ok_and(|now| { (now + chrono::Duration::seconds(2)).timestamp_millis() @@ -151,6 +157,48 @@ impl EffectFence { } } type Completion = (JobOffer, ExecutionPermitPayload, Outcome); +#[cfg(feature = "system-helper")] +fn needs_bound_outcome( + db: &Connection, + bound: &rmm_agent_windows::helper_runtime::BoundOutcome, +) -> Result { + use rmm_domain::reconciliation::{ReconciledOutcome, canonical_outcome_bytes}; + // Archiving is durable retention only: no ACK, event deletion or unlock. + reconcile::archive_events(db)?; + let (state, revision, grant): (String, u64, String) = db.query_row( + "SELECT state,revision,grant_id FROM job_attempts WHERE job=?1 AND attempt=?2", + rusqlite::params![bound.job_id.to_string(), bound.attempt_id.to_string()], + |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?)), + )?; + let bytes:Option>=db.query_row("SELECT envelope FROM reconcile_results WHERE job=?1 AND attempt=?2 ORDER BY revision DESC LIMIT 1",rusqlite::params![bound.job_id.to_string(),bound.attempt_id.to_string()],|r|r.get(0)).optional()?; + if state == bound.outcome.state + && grant == bound.grant_id.to_string() + && let Some(bytes) = bytes + { + let envelope = Envelope::decode(bytes.as_slice())?; + if let Some(Payload::JobResult(r)) = envelope.payload + && r.revision == revision + && r.grant_id == grant + { + let saved = ReconciledOutcome { + state: r.state, + stdout: r.stdout, + stderr: r.stderr, + exit_code: r.exit_code, + output_truncated: r.output_truncated, + reboot_required: r.reboot_required, + evidence: serde_json::from_str(&r.evidence_json)?, + error_code: r.error_code, + }; + let actual: ReconciledOutcome = + serde_json::from_value(serde_json::to_value(&bound.outcome)?)?; + if canonical_outcome_bytes(&saved)? == canonical_outcome_bytes(&actual)? { + return Ok(false); + } + } + } + Ok(true) +} struct Running { offer: JobOffer, grant: ExecutionPermitPayload, @@ -338,26 +386,40 @@ fn open_ledger(directory: &Path, binding: &Binding) -> Result<(Connection, fs::F Ok((db, file)) } #[cfg(feature = "system-helper")] +type MirrorCompletion = ( + Connection, + Result<( + rmm_agent_windows::helper_runtime::ControlSnapshot, + Vec, + )>, +); +#[cfg(feature = "system-helper")] enum HelperControl { Snapshot( u64, - tokio::task::JoinHandle>, + tokio::task::JoinHandle>, ), Apply( u64, - tokio::task::JoinHandle>, + tokio::task::JoinHandle>, ), + Mirror(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(), + Self::Mirror(_, t) => t.is_finished(), } } - fn abort(self) { + fn abort(self) -> Option { match self { - Self::Snapshot(_, t) | Self::Apply(_, t) => t.abort(), + Self::Snapshot(_, t) | Self::Apply(_, t) => { + t.abort(); + None + } + mirror @ Self::Mirror(..) => Some(mirror), } } } @@ -389,6 +451,8 @@ struct Agent { #[cfg(feature = "system-helper")] helper_control: Option, #[cfg(feature = "system-helper")] + helper_anchor: Option, + #[cfg(feature = "system-helper")] helper_checked: bool, #[cfg(feature = "system-helper")] helper_refused: bool, @@ -408,8 +472,9 @@ impl Agent { #[cfg(feature = "system-helper")] { self.prepared_request = None; + self.helper_anchor = None; if let Some(control) = self.helper_control.take() { - control.abort(); + self.helper_control = control.abort(); } } self.deferred_permit = None; @@ -446,15 +511,69 @@ impl Agent { HelperControl::Snapshot(expected, task) => { ensure!(expected == generation, "HELPER_CHANNEL_CHANGED"); let snapshot = task.await??; - self.send_sync(channel, generation, Some(&snapshot)).await?; + let helper = self + .helper + .clone() + .context("WINDOWS_HELPER_NOT_CONFIGURED")?; + let observed_at = self.clock.now()?; + let mut ledger = self.ledger.take().context("LEDGER_REQUIRED")?; + self.helper_control = Some(HelperControl::Mirror( + generation, + tokio::task::spawn_blocking(move || { + let result = helper + .mirror(&snapshot.raw, &mut ledger, observed_at) + .map(|unknowns| (snapshot, unknowns)); + (ledger, result) + }), + )); + } + HelperControl::Mirror(expected, task) => { + // Always restore ownership, including after reconnect. A + // stale generation may discard its snapshot, never its DB. + let (ledger, result) = task.await?; + self.ledger = Some(ledger); + let (snapshot, unknowns) = result?; + for bound in unknowns { + if needs_bound_outcome(self.journal.connection(), &bound)? { + let bytes: Vec = self.journal.connection().query_row( + "SELECT offer FROM job_attempts WHERE job=?1 AND attempt=?2", + rusqlite::params![ + bound.job_id.to_string(), + bound.attempt_id.to_string() + ], + |r| r.get(0), + )?; + self.record( + generation, + &JobOffer::decode(bytes.as_slice())?, + &bound.grant_id.to_string(), + bound.outcome, + )?; + } + } + if expected != generation { + self.close_gate(); + return Ok(()); + } + self.send_sync( + channel, + generation, + Some(&rmm_agent_windows::helper_runtime::map_snapshot( + &snapshot.raw, + )), + ) + .await?; } HelperControl::Apply(expected, task) => { ensure!(expected == generation, "HELPER_CHANNEL_CHANGED"); let snapshot = task.await??; + self.helper_anchor = snapshot.no_start_anchor; self.sync .as_mut() .context("SYNC_NOT_CONFIGURED")? - .confirm_helper(&snapshot)?; + .confirm_helper(&rmm_agent_windows::helper_runtime::map_snapshot( + &snapshot.raw, + ))?; self.open_fence(generation)?; } } @@ -493,6 +612,7 @@ impl Agent { && self.helper_control.is_none() && !self.prepared && self.running.is_none() + && !self.journal.connection().query_row("SELECT EXISTS(SELECT 1 FROM job_attempts WHERE state IN ('requesting','running','verifying'))",[],|r|r.get::<_,bool>(0))? && self.helper_probe_at.elapsed() >= Duration::from_secs(30) { let agent = self.identity.agent_id.clone(); @@ -516,14 +636,45 @@ impl Agent { }) } fn ready(&self, generation: u64) -> bool { + self.ready_identity(generation, false) + } + fn ready_identity(&self, generation: u64, helper: bool) -> bool { + #[cfg(feature = "system-helper")] + if helper + && !self.helper_anchor.as_ref().is_some_and(|a| { + a.connection_generation() == generation && a.boot_id().to_string() == self.boot + }) + { + return false; + } self.config.enable_jobs && !self.helper_control_pending() && self.channel(generation).is_ok_and(|channel| { self.sync .as_ref() - .is_some_and(|sync| sync.can_start(&channel, false, &self.clock)) + .is_some_and(|sync| sync.can_start(&channel, helper, &self.clock)) }) } + fn supported_job(&self, job: &rmm_domain::jobs::JobPayload) -> bool { + if native::require_local_service().is_err() { + return false; + } + if rmm_agent_windows::actions::supported(job) { + return true; + } + #[cfg(feature = "system-helper")] + { + Path::new(rmm_agent_windows::helper_runtime::intent::DB).is_file() + && self + .helper + .as_ref() + .is_some_and(|helper| helper.supports_system(job)) + } + #[cfg(not(feature = "system-helper"))] + { + false + } + } fn capabilities(&self) -> Vec { let mut caps: Vec = CAPABILITIES.iter().map(|c| c.to_string()).collect(); if self.config.enable_jobs @@ -559,6 +710,72 @@ impl Agent { let Some(binding) = self.binding.clone() else { return Ok(()); }; + #[cfg(feature = "system-helper")] + if Path::new(rmm_agent_windows::helper_runtime::intent::DB).is_file() { + let mut intents = rmm_agent_windows::helper_runtime::intent::Store::open(true)?; + let pending = intents.pending()?; + if let Some(intent) = pending.first() { + let p = intent.permit()?; + let incompatible:bool=self.journal.connection().query_row("SELECT EXISTS(SELECT 1 FROM job_attempts WHERE state IN ('requesting','running','verifying','unknown','awaiting_reboot') AND NOT(job=?1 AND attempt=?2))",rusqlite::params![p.job_id.to_string(),p.attempt_id.to_string()],|r|r.get(0))?; + ensure!(!incompatible, "SYSTEM_PENDING_INTENT_CONFLICT"); + binding.verify_historical_for_denial_with( + &intent.request, + reconcile::HelperIdentity::System, + &rmm_agent_core::crypto::DalekVerifier, + )?; + let bytes: Vec = self.journal.connection().query_row( + "SELECT offer FROM job_attempts WHERE job=?1 AND attempt=?2", + rusqlite::params![p.job_id.to_string(), p.attempt_id.to_string()], + |r| r.get(0), + )?; + let offer = JobOffer::decode(bytes.as_slice())?; + ensure!( + offer.payload_bytes == intent.request.job_bytes + && offer.plan_hash == intent.request.plan_hash + && offer.authorization_id == intent.request.authorization_id.to_string(), + "SYSTEM_PENDING_INTENT_BINDING" + ); + // Even prepared-only intents are never executed after restart. + // Snapshot/explicit bound denial recovery is the only route. + intents.check()?; + let ledger = self.ledger.as_ref().context("LEDGER_REQUIRED")?; + let consumed: bool = ledger.query_row( + "SELECT EXISTS(SELECT 1 FROM consumed WHERE grant_id=?1)", + [p.grant_id.to_string()], + |r| r.get(0), + )?; + let actual = if consumed { + jobs::recover_interrupted_outcome( + ledger, + &binding, + &offer, + &p.grant_id.to_string(), + )? + } else { + None + }; + if let Some(actual) = &actual { + let bound = rmm_agent_windows::helper_runtime::BoundOutcome { + job_id: p.job_id, + attempt_id: p.attempt_id, + grant_id: p.grant_id, + outcome: actual.clone(), + }; + if !needs_bound_outcome(self.journal.connection(), &bound)? { + return Ok(()); + } + } + self.record( + 0, + &offer, + &p.grant_id.to_string(), + actual.unwrap_or_else(|| { + Outcome::unknown("SYSTEM_DELEGATION_INTERRUPTED_REQUIRES_HELPER_EVIDENCE") + }), + )?; + return Ok(()); + } + } let interrupted = jobs::interrupted( self.journal.connection(), self.ledger.as_ref().context("LEDGER_REQUIRED")?, @@ -610,9 +827,38 @@ impl Agent { if let Some(helper) = self.helper.clone() { ensure!(self.helper_control.is_none(), "HELPER_CONTROL_PENDING"); let boot = self.channel(generation)?.boot_id; + let instance = Uuid::parse_str(&self.instance)?; + let clock = self.clock.clone(); self.helper_control = Some(HelperControl::Snapshot( generation, - tokio::spawn(async move { helper.snapshot(generation, boot).await }), + tokio::spawn(async move { + let denial_resume = tokio::task::spawn_blocking(move || -> Result<_> { + if Path::new(rmm_agent_windows::helper_runtime::intent::DB).is_file() { + use rmm_agent_windows::helper_runtime::intent::{Phase, Store}; + let mut store = Store::open(true)?; + if let Some(intent) = store.pending()?.first() { + let (_, phase) = store.load(intent.id)?; + if phase == Phase::KnownNoDispatch + && intent.agent_instance != instance + && store.claim_denial_resume( + intent.id, + instance, + boot, + clock.now()?, + )? + { + return Ok(Some(intent.clone())); + } + } + } + Ok(None) + }) + .await??; + if let Some(intent) = denial_resume { + let _denial = helper.resume_denial(intent).await; + } + helper.snapshot(generation, boot).await + }), )); return Ok(()); } @@ -694,6 +940,14 @@ impl Agent { .deadline .store(deadline.timestamp_millis(), Ordering::SeqCst); self.fence.generation.store(generation, Ordering::SeqCst); + self.fence.helper_generation.store( + if self.ready_identity(generation, true) { + generation + } else { + 0 + }, + Ordering::SeqCst, + ); } Ok(()) } @@ -708,6 +962,19 @@ impl Agent { && running.grant == grant, "EXECUTOR_COMPLETION_BINDING" ); + if grant.execution_identity == "system" { + // No local consumption or definitive result from an IPC outcome. + // Import only a fresh raw helper snapshot before the next Sync. + self.record( + generation, + &offer, + &grant.grant_id.to_string(), + Outcome::unknown("SYSTEM_DELEGATION_REQUIRES_HELPER_SNAPSHOT"), + )?; + self.running = None; + self.close_gate(); + return Ok(()); + } ledger::persist_outcome( self.ledger.as_ref().context("LEDGER_REQUIRED")?, &grant, @@ -725,6 +992,9 @@ impl Agent { &outcome, )?; self.running = None; + if grant.execution_identity == "system" { + self.close_gate(); + } Ok(()) } fn permit(&mut self, generation: u64, permit: ExecutionPermit) -> Result<()> { @@ -764,10 +1034,82 @@ impl Agent { return Ok(()); } let (job, grant) = verified_window(&binding, &request, &self.clock)?; + ensure!(self.supported_job(&job), "UNSUPPORTED_CAPABILITY"); + let system = job.execution_identity == "system"; ensure!( - rmm_agent_windows::actions::supported(&job), - "UNSUPPORTED_CAPABILITY" + self.ready_identity(generation, system), + "SYSTEM_HELPER_SYNC_REQUIRED" ); + let cancelled: bool = self.journal.connection().query_row( + "SELECT cancel_requested FROM job_attempts WHERE job=?1 AND attempt=?2", + rusqlite::params![offer.job_id, offer.attempt_id], + |r| r.get(0), + )?; + #[cfg(feature = "system-helper")] + if system { + use rmm_agent_windows::helper_runtime::intent::{Intent, Store}; + let helper = self + .helper + .clone() + .context("WINDOWS_HELPER_NOT_CONFIGURED")?; + let intent = Intent { + schema_version: 1, + id: Uuid::new_v4(), + request: request.clone(), + anchor: self + .helper_anchor + .clone() + .context("SYSTEM_READY_ANCHOR_REQUIRED")?, + original_boot_id: Uuid::parse_str(&self.boot)?, + agent_instance: Uuid::parse_str(&self.instance)?, + connection_generation: generation, + recorded_at: self.clock.now()?, + policy_sha256: helper.pin_hash().into(), + helper_binary_sha256: helper.helper_binary_hash().into(), + }; + let mut store = Store::open(true)?; + store.prepare(&intent)?; + if cancelled { + store.decide(intent.id, false)?; + } + let cancelled = Arc::new(AtomicBool::new(cancelled)); + let worker_cancel = cancelled.clone(); + let clock = self.clock.clone(); + let fence = self.fence.clone(); + let completed = self.completed_tx.clone(); + let work_offer = offer.clone(); + let work_grant = grant.clone(); + let worker = tokio::spawn(async move { + let _result = helper + .delegate(intent, move || { + ensure!( + !worker_cancel.load(Ordering::SeqCst), + "CANCELLED_BEFORE_DISPATCH" + ); + ensure!( + fence.allows_identity(generation, true, &clock), + "FENCED_BEFORE_DISPATCH" + ); + verified_window(&binding, &request, &clock)?; + Ok(()) + }) + .await; + let _ = completed + .send(( + work_offer, + work_grant, + Outcome::unknown("SYSTEM_DELEGATION_REQUIRES_HELPER_SNAPSHOT"), + )) + .await; + }); + self.running = Some(Running { + offer, + grant, + cancel: cancelled, + worker, + }); + return Ok(()); + } let started_at = self.clock.now()?; let ledger = self.ledger.as_mut().context("LEDGER_REQUIRED")?; ledger::begin_execution( @@ -777,11 +1119,6 @@ impl Agent { Uuid::parse_str(&self.boot)?, started_at, )?; - let cancelled: bool = self.journal.connection().query_row( - "SELECT cancel_requested FROM job_attempts WHERE job=?1 AND attempt=?2", - rusqlite::params![offer.job_id, offer.attempt_id], - |r| r.get(0), - )?; if cancelled { let outcome = jobs::no_start("cancelled", "CANCELLED_BEFORE_EFFECT"); ledger::persist_outcome( @@ -1016,7 +1353,7 @@ impl Agent { if let Some(offer)=jobs::next_at(self.journal.connection(),now)? { let job=self.binding.as_ref().context("JOB_BINDING_REQUIRED")?.job(&offer.payload_bytes)?; if now>=job.start_before {self.record(generation,&offer,"",jobs::no_start("expired","START_DEADLINE_EXPIRED"))?;needs_sync=true;} - else if now-chrono::Duration::seconds(2)>=job.not_before { + else if now-chrono::Duration::seconds(2)>=job.not_before && self.ready_identity(generation,job.execution_identity=="system") { jobs::reserve_request(self.journal.connection(),&offer)?; let request=self.envelope(generation,Payload::ExecutionPermitRequest(ExecutionPermitRequest{job_id:offer.job_id,attempt_id:offer.attempt_id,job_payload_hash:offer.job_payload_hash,assignment_version:job.assignment_version}))?; channel.send(&request).await?; @@ -1039,8 +1376,9 @@ impl Agent { ensure!(self.config.enable_jobs,"UNSUPPORTED_EXECUTION_CAPABILITY"); let binding=self.binding.as_ref().context("JOB_BINDING_REQUIRED")?.clone(); let accepted=self.envelope(generation,Payload::JobAccepted(JobAccepted{job_id:offer.job_id.clone(),attempt_id:offer.attempt_id.clone(),job_payload_hash:offer.job_payload_hash.clone(),journal_proof:rmm_domain::jobs::hash(&offer.encode_to_vec())}))?; + let supported=self.supported_job(&binding.job(&offer.payload_bytes)?); let tx=self.journal.connection_mut().transaction()?; - let fresh=jobs::accept(&tx,&binding,&offer,rmm_agent_windows::actions::supported)?; + let fresh=jobs::accept(&tx,&binding,&offer,|_|supported)?; jobs::event(&tx,&accepted)?;tx.commit()?;needs_sync|=fresh; }, Some(Payload::ExecutionPermit(permit))=>self.permit(generation,permit)?, @@ -1049,6 +1387,11 @@ impl Agent { if let Some((bytes,state,grant))=row { self.journal.connection().execute("UPDATE job_attempts SET cancel_requested=1 WHERE job=?1 AND attempt=?2",rusqlite::params![cancel.job_id,cancel.attempt_id])?; if let Some(running)=&self.running && running.offer.job_id==cancel.job_id && running.offer.attempt_id==cancel.attempt_id {running.cancel.store(true,Ordering::SeqCst);} + #[cfg(feature="system-helper")] + if Path::new(rmm_agent_windows::helper_runtime::intent::DB).is_file() { + let mut store=rmm_agent_windows::helper_runtime::intent::Store::open(true)?; + if let Some(intent)=store.pending()?.first() && intent.permit()?.job_id.to_string()==cancel.job_id && intent.permit()?.attempt_id.to_string()==cancel.attempt_id {store.cancel_before_dispatch(intent.id)?;} + } if state=="accepted" {self.record(generation,&JobOffer::decode(bytes.as_slice())?,&grant,jobs::no_start("cancelled","CANCELLED_BEFORE_START"))?;needs_sync=true;} } let ack=self.envelope(generation,Payload::Ack(Ack{message_id:message.message_id}))?;channel.send(&ack).await?; @@ -1158,6 +1501,8 @@ fn service_run(stop: &'static AtomicBool) -> Result<()> { #[cfg(feature="system-helper")] helper_control:None, #[cfg(feature="system-helper")] + helper_anchor:None, + #[cfg(feature="system-helper")] helper_checked:false, #[cfg(feature="system-helper")] helper_refused:false, @@ -1242,6 +1587,90 @@ pub fn main() -> Result<()> { #[cfg(test)] mod tests { use super::*; + #[cfg(feature = "system-helper")] + #[test] + fn bound_helper_unknown_repairs_generic_crash_gap_without_ack_or_duplicate_result() { + let path = std::env::temp_dir().join(format!("windows-bound-unknown-{}", Uuid::new_v4())); + fs::create_dir(&path).unwrap(); + let journal = Journal::open(&path).unwrap(); + let db = journal.connection(); + let job = Uuid::new_v4(); + let attempt = Uuid::new_v4(); + let grant = Uuid::new_v4(); + let offer = JobOffer { + job_id: job.to_string(), + attempt_id: attempt.to_string(), + ..Default::default() + }; + db.execute("INSERT INTO job_attempts(job,attempt,offer,state,revision,grant_id) VALUES(?1,?2,?3,'unknown',1,?4)",rusqlite::params![offer.job_id,offer.attempt_id,offer.encode_to_vec(),grant.to_string()]).unwrap(); + let original = Envelope { + message_id: Uuid::new_v4().to_string(), + payload: Some(Payload::JobResult(jobs::result( + &offer, + 1, + &grant.to_string(), + Outcome::unknown("GENERIC_REPLY_LOST"), + ))), + ..Default::default() + }; + jobs::event(db, &original).unwrap(); + let mut actual = Outcome::unknown("SYSTEM_PREVIOUS_EFFECT_UNRESOLVED"); + actual.evidence = serde_json::json!({"retained_helper_evidence":true}); + let bound = rmm_agent_windows::helper_runtime::BoundOutcome { + job_id: job, + attempt_id: attempt, + grant_id: grant, + outcome: actual.clone(), + }; + // Represents a crash after actual raw-helper outcome persistence but + // before the corresponding journal write, even if mirror receipt exists. + assert!(needs_bound_outcome(db, &bound).unwrap()); + assert_eq!(jobs::events(db).unwrap().len(), 1); + let revision = jobs::transition(db, &offer, "unknown", &grant.to_string()).unwrap(); + let repaired = Envelope { + message_id: Uuid::new_v4().to_string(), + payload: Some(Payload::JobResult(jobs::result( + &offer, + revision, + &grant.to_string(), + actual, + ))), + ..Default::default() + }; + jobs::event(db, &repaired).unwrap(); + assert!(!needs_bound_outcome(db, &bound).unwrap()); + assert_eq!(jobs::events(db).unwrap().len(), 2); // retention is never ACK + let mut terminal = bound.outcome.clone(); + terminal.state = "failed".into(); + let terminal_bound = rmm_agent_windows::helper_runtime::BoundOutcome { + job_id: job, + attempt_id: attempt, + grant_id: grant, + outcome: terminal.clone(), + }; + let revision = jobs::transition(db, &offer, "failed", &grant.to_string()).unwrap(); + jobs::event( + db, + &Envelope { + message_id: Uuid::new_v4().to_string(), + payload: Some(Payload::JobResult(jobs::result( + &offer, + revision, + &grant.to_string(), + terminal, + ))), + ..Default::default() + }, + ) + .unwrap(); + // Two restarts after the terminal journal committed but before a + // mirror receipt may never request terminal -> terminal transitions. + assert!(!needs_bound_outcome(db, &terminal_bound).unwrap()); + assert!(!needs_bound_outcome(db, &terminal_bound).unwrap()); + assert_eq!(jobs::events(db).unwrap().len(), 3); + drop(journal); + fs::remove_dir_all(path).unwrap(); + } #[test] fn old_passive_configuration_cannot_enable_jobs_or_read_the_clock() { let config:Config=serde_json::from_value(serde_json::json!({"server_name":"example.invalid","connect_address":"127.0.0.1:443","ca_file":"C:/fixture/ca.pem","state_directory":"C:/fixture/state","enrollment_token_file":"C:/fixture/token","transport":"auto"})).unwrap(); @@ -1319,9 +1748,13 @@ mod tests { assert!(!fence.allows(1, &Clock(now))); fence.generation.store(1, Ordering::SeqCst); assert!(fence.allows(1, &Clock(now))); + assert!(!fence.allows_identity(1, true, &Clock(now))); + fence.helper_generation.store(1, Ordering::SeqCst); + assert!(fence.allows_identity(1, true, &Clock(now))); assert!(!fence.allows(2, &Clock(now))); assert!(!fence.allows(1, &Clock(now + chrono::Duration::seconds(1)))); fence.close(); + assert!(!fence.allows_identity(1, true, &Clock(now))); assert!(!fence.allows(1, &Clock(now))); } } diff --git a/deploy/windows/client_bootstrap.py b/deploy/windows/client_bootstrap.py index 37345ed..abfba89 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|Build-MsiFixture|Build-NativeMsvc|Install-NativeBuildTools)\.ps1|PassiveStateProbe\.exe|WindowsTlsProbe\.exe|RuntimeStateProbe\.exe)$') {throw 'Artifact allowlist'} +if ($p.name -notmatch '^((rmm-agent-windows|rmm-helper-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|SystemStateProbe\.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|Build-MsiFixture|Build-NativeMsvc|Install-NativeBuildTools)\.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|rmm-helper-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|SystemStateProbe\.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/system_delegation_acceptance.py b/deploy/windows/system_delegation_acceptance.py new file mode 100644 index 0000000..98e4cb7 --- /dev/null +++ b/deploy/windows/system_delegation_acceptance.py @@ -0,0 +1,278 @@ +#!/usr/bin/env python3 +"""Separate source-bound Windows delegation build/native units; no deployment. + +Historical feature profiles stay unchanged. Unit execution is create-once; +lost replies permit same-case readback only. No RMM requests or approvals. +""" +import argparse +import base64 +import hashlib +import json +import os +from pathlib import Path +import re +import shutil +import stat +import subprocess +import uuid + +import client_bootstrap as cb +import runtime_feature_upgrade as feature +import runtime_upgrade as previous +import auto_transport_acceptance as auto + +require = cb.contract.require +STATE = cb.EVIDENCE / 'system-delegation-acceptance' +CORE = cb.ROOT / 'devstate/core-no-start/build-de37ee99-11bf-465b-9519-45ad8f6e5b87/manifest.json' +CORE_SHA = '1bc082031fcad20d5a519b0004f63d499dbba2ea9ce25d56230d48d2556d474b' +# Final source review must confirm these before a build. No minimum-count proof. +COUNTS = {'lib': 23, 'bin': 7} +NAMES = {'agent': 'rmm-agent-windows.exe', 'helper': 'rmm-helper-windows.exe', + 'probe': 'RuntimeStateProbe.exe', 'system_probe': 'SystemStateProbe.exe', + 'lib': 'windows-tests-lib.exe', 'bin': 'windows-tests-bin.exe'} +TARGET = previous.TARGET + + +def sha(path): + return hashlib.sha256(Path(path).read_bytes()).hexdigest() + + +def save(path, value): + path.parent.mkdir(mode=0o700, parents=True, exist_ok=True) + cb.contract.write_private(path, value) + + +def sources(): + result = feature.source_files() + for path in (cb.ROOT / 'crates').glob('*/Cargo.toml'): + result[str(path.relative_to(cb.ROOT))] = sha(path) + for name in ('deploy/windows/system_delegation_acceptance.py', + 'deploy/windows/client_bootstrap.py', + 'deploy/windows/system_intent_bootstrap.py'): + result[name] = sha(cb.ROOT / name) + return dict(sorted(result.items())) + + +def core_proof(): + require(sha(CORE) == CORE_SHA, 'exact Linux-first core manifest') + manifest = cb.contract.protected_json(CORE) + result = {'manifest_sha256': CORE_SHA} + for platform, count in (('linux', 80), ('windows', 58)): + path = CORE.parent / (platform + '-passed.json') + proof = cb.contract.protected_json(path) + require(proof['verified'] is True and proof['platform'] == platform + and proof['manifest_sha256'] == CORE_SHA, 'actual same-manifest native core proof') + actual = proof['native']['reports'] + require(set(actual) == set(manifest['test_counts'][platform]), 'all core artifacts') + require(sum(len(v['names']) for v in actual.values()) == count, 'full native core lists') + for kind, item in actual.items(): + output = base64.b64decode(item['stdout_b64'], validate=True) + n = manifest['test_counts'][platform][kind] + require(item['exit_code'] == 0 and item['timed_out'] is False + and item['names'] == manifest['test_names'][platform][kind] + and item['sha256'] == manifest['artifacts'][platform][kind]['sha256'] + and re.search(rb'test result: ok\. ' + str(n).encode() + + rb' passed; 0 failed; 0 ignored; 0 measured; 0 filtered out;', output), + 'actual complete native core results') + result[platform] = {'proof_sha256': sha(path), 'case_id': proof['case_id']} + for name, digest in manifest['source_files'].items(): + if name.startswith(('crates/agent-core/', 'crates/domain/', 'crates/protocol/', 'proto/')): + require(sha(cb.ROOT / name) == digest, 'native-qualified common source unchanged') + return result + + +def command(args, timeout=600): + value = subprocess.run(args, capture_output=True, timeout=timeout) + if value.returncode: + folder = STATE / ('local-command-failure-' + str(uuid.uuid4())) + folder.mkdir(mode=0o700, parents=True) + save(folder / 'result.json', {'returncode': value.returncode, 'local_build_only': True}) + for name, data in (('stdout.bin', value.stdout), ('stderr.bin', value.stderr)): + descriptor = os.open(folder / name, os.O_CREAT | os.O_EXCL | os.O_WRONLY | os.O_NOFOLLOW, 0o600) + with os.fdopen(descriptor, 'wb') as stream: + stream.write(data) + require(value.returncode == 0, 'local qualification command failed') + return value.stdout + + +def build(): + os.umask(0o077) + before = sources() + common = core_proof() + require(re.search(r'^pub const ADAPTER_QUALIFIED: bool = false;$', + (cb.ROOT / 'crates/agent-windows/src/clock.rs').read_text(), re.M), + 'production clock remains closed') + folder = STATE / ('build-' + str(uuid.uuid4())) + folder.mkdir(mode=0o700, parents=True) + save(folder / 'source-before.json', before) + target = STATE / 'target' + target.mkdir(mode=0o700, exist_ok=True) + image = command(['docker', 'image', 'inspect', '--format', '{{.Id}}', previous.IMAGE]).decode().strip() + require(re.fullmatch(r'sha256:[a-f0-9]{64}', image), 'immutable build image') + docker = ['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', 'cargo', image] + options = ['--manifest-path', '/src/Cargo.toml', '--locked', '--offline', '--target', TARGET, + '-p', 'rmm-agent-windows'] + command(docker + ['clippy'] + options + ['--all-targets', '--', '-D', 'warnings']) + command(docker + ['clippy'] + options + ['--features', 'system-helper', '--all-targets', '--', '-D', 'warnings']) + command(docker + ['build'] + options + ['--features', 'system-helper', '--bins', '--example', + 'runtime_state', '--example', 'system_state', '--release']) + raw = command(docker + ['test'] + options + ['--features', 'system-helper', '--lib', '--bin', + 'rmm-agent-windows', '--no-run', '--message-format=json']) + units = {} + for line in raw.splitlines(): + try: + item = json.loads(line) + except ValueError: + continue + if item.get('reason') != 'compiler-artifact' or not item.get('profile', {}).get('test') or not item.get('executable'): + continue + if 'rmm-agent-windows' not in item['package_id']: + continue + require('system-helper' in item['features'], 'actual feature unit profile') + kind = 'lib' if 'lib' in item['target']['kind'] else 'bin' + require(kind not in units, 'exact distinct unit artifacts') + units[kind] = target / Path(item['executable']).relative_to('/target') + require(set(units) == set(COUNTS), 'both unit artifacts') + selected = {'agent': (target / TARGET / 'release/rmm-agent-windows.exe', 'rmm-agent-windows.exe'), + 'helper': (target / TARGET / 'release/rmm-helper-windows.exe', 'rmm-helper-windows.exe'), + 'probe': (target / TARGET / 'release/examples/runtime_state.exe', 'RuntimeStateProbe.exe'), + 'system_probe': (target / TARGET / 'release/examples/system_state.exe', 'SystemStateProbe.exe')} + selected.update({kind: (path, 'windows-tests-' + kind + '.exe') for kind, path in units.items()}) + artifacts = {} + for kind, (original, name) in selected.items(): + path = folder / name + shutil.copyfile(original, path) + path.chmod(0o600) + linked_sha = sha(original) + retained = folder / (name + '.linked') + shutil.copyfile(original, retained) + retained.chmod(0o600) + if kind in COUNTS: + 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(sha(original) == sha(retained) == linked_sha, 'original link preserved') + data = path.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', 'Windows AMD64 PE') + artifacts[kind] = {'path': str(path.resolve()), 'name': name, 'sha256': sha(path), 'bytes': len(data), + 'original_link_path': str(retained.resolve()), 'original_link_sha256': linked_sha} + require(sources() == before and core_proof() == common, 'sources and common proofs unchanged throughout build') + manifest = {'schema_version': 1, 'type': 'rmm.windows.system-delegation-build', 'source_files': before, + 'source_before_after_equal': True, 'artifacts': artifacts, 'native_counts_expected': COUNTS, + 'core_native_proofs': common, 'build_image_id': image, 'default_and_feature_alltarget_clippy': True, + 'production_clock_qualified': False, 'native_windows_executed': False} + save(folder / 'manifest.json', manifest) + print(json.dumps({'manifest': str((folder / 'manifest.json').resolve()), 'native_executed': False})) + + +SCRIPT = r''' +$ErrorActionPreference='Stop';$p=([Console]::In.ReadLine()|ConvertFrom-Json) +if([string]([guid]$p.id)-cne$p.id-or$p.action-notin@('run','read')-or$p.manifest_sha256-notmatch'^[a-f0-9]{64}$'){throw 'Fixed native delegation unit case'} +$root='C:\ProgramData\RustDiscoveryBootstrap';$case=Join-Path $root ('delegation-units-'+$p.id) +function Safe($path,$dir){$i=Get-Item -LiteralPath $path -Force;if($i.PSIsContainer-ne$dir-or($i.Attributes-band[IO.FileAttributes]::ReparsePoint)){throw 'Private unit type'};$a=Get-Acl -LiteralPath $path;if($a.GetOwner([Security.Principal.SecurityIdentifier]).Value-notin@('S-1-5-18','S-1-5-32-544')){throw 'Unit owner'};$rules=@($a.GetAccessRules($true,$true,[Security.Principal.SecurityIdentifier]));if(!$rules.Count){throw 'Empty ACL'};foreach($r in $rules){if($r.AccessControlType-ne'Allow'-or$r.IdentityReference.Value-notin@('S-1-5-18','S-1-5-32-544')){throw 'Unit ACL'}}} +function WriteNew($path,$value){$b=[Text.Encoding]::UTF8.GetBytes(($value|ConvertTo-Json -Depth 12 -Compress));$f=[IO.File]::Open($path,'CreateNew','Write','Read');try{$f.Write($b,0,$b.Length);$f.Flush($true)}finally{$f.Dispose()}} +Safe $root $true +if($p.action-eq'read'){Safe $case $true;Safe (Join-Path $case 'intent.json') $false;$i=Get-Content -LiteralPath (Join-Path $case 'intent.json') -Raw|ConvertFrom-Json;if($i.manifest_sha256-cne$p.manifest_sha256){throw 'Original native intent'};Safe (Join-Path $case 'result.json') $false;Get-Content -LiteralPath (Join-Path $case 'result.json') -Raw;exit 0} +if(Test-Path -LiteralPath $case){throw 'Existing unit case: readback only'};New-Item -ItemType Directory -Path $case|Out-Null;Safe $case $true;WriteNew (Join-Path $case 'intent.json') $p +function Run($exe,$arguments){$s=New-Object Diagnostics.ProcessStartInfo;$s.FileName=$exe;$s.Arguments=$arguments;$s.UseShellExecute=$false;$s.CreateNoWindow=$true;$s.RedirectStandardOutput=$true;$s.RedirectStandardError=$true;$c=New-Object Diagnostics.Process;$c.StartInfo=$s;if(!$c.Start()){throw 'Unit process start'};try{$o=$c.StandardOutput.ReadToEndAsync();$e=$c.StandardError.ReadToEndAsync();$timed=$false;if(!$c.WaitForExit(30000)){$timed=$true;$c.Kill();$c.WaitForExit()};if($o.Result.Length-gt65536-or$e.Result.Length-gt65536){throw 'Unit output bound'};return @{exit_code=$c.ExitCode;timed_out=$timed;stdout=$o.Result;stderr=$e.Result}}finally{$c.Dispose()}} +WriteNew (Join-Path $case 'started.json') @{start_once=$true};$reports=@{} +foreach($kind in @('lib','bin')){$a=$p.artifacts.$kind;if($a.sha256-notmatch'^[a-f0-9]{64}$'-or$a.name-cne('windows-tests-'+$kind+'.exe')){throw 'Fixed unit artifact'};$folder=Join-Path $root $a.sha256;Safe $folder $true;$exe=Join-Path $folder $a.name;Safe $exe $false;if((Get-FileHash -LiteralPath $exe -Algorithm SHA256).Hash.ToLowerInvariant()-cne$a.sha256){throw 'Exact native unit hash'};$listed=Run $exe '--list --format terse';$names=@($listed.stdout-split'\r?\n'|Where-Object{$_-match': test$'}|ForEach-Object{$_-replace': test$',''}|Sort-Object);if($listed.exit_code-ne0-or$listed.timed_out-or$names.Count-ne$p.counts.$kind){throw 'Exact native count before tests'};$result=Run $exe '--test-threads=1';$v=@{sha256=$a.sha256;names=$names;listed=$listed;result=$result};WriteNew (Join-Path $case ($kind+'-result.json')) $v;$reports[$kind]=$v} +$value=@{manifest_sha256=$p.manifest_sha256;reports=$reports;bootstrap_administrator_units=$true;local_service_proof=$false;rmm_jobs=$false;product_state_writes=$false};WriteNew (Join-Path $case 'result.json') $value;$value|ConvertTo-Json -Depth 12 -Compress +''' + + +def verified(path, current=True): + value = cb.contract.protected_json(path) + require(value['type'] == 'rmm.windows.system-delegation-build' and value['schema_version'] == 1 + and value['source_before_after_equal'] is True and value['production_clock_qualified'] is False + and value['default_and_feature_alltarget_clippy'] is True and value['native_counts_expected'] == COUNTS, + 'exact delegation build profile') + if current: + require(value['source_files'] == sources() and value['core_native_proofs'] == core_proof(), 'source/native core binding') + require(set(value['artifacts']) == set(NAMES), 'all deployable/test artifacts') + for kind, item in value['artifacts'].items(): + binary, linked = Path(item['path']), Path(item['original_link_path']) + require(item['name'] == NAMES[kind] and binary == path.resolve().parent / NAMES[kind] + and linked == path.resolve().parent / (NAMES[kind] + '.linked'), 'fixed artifact paths and names') + for file in (binary, linked): + info = file.lstat() + require(not file.is_symlink() and stat.S_ISREG(info.st_mode) and info.st_uid == os.getuid() + and stat.S_IMODE(info.st_mode) == 0o600, 'private regular retained artifact') + require(binary.resolve().parent == path.resolve().parent + and linked.resolve().parent == path.resolve().parent + and sha(item['path']) == item['sha256'] + and sha(item['original_link_path']) == item['original_link_sha256'], 'retained exact artifact/link') + return value + + +def native(path, ident=None): + manifest = verified(path, current=ident is None) + if ident is None: + ident = str(uuid.uuid4()) + folder = STATE / ident + folder.mkdir(mode=0o700) + save(folder / 'intent.json', {'id': ident, 'manifest_path': str(path.resolve()), 'manifest_sha256': sha(path), + 'runner_sha256': sha(__file__), 'script_sha256': hashlib.sha256(SCRIPT.encode()).hexdigest()}) + save(folder / 'server-before.json', {'counts': auto.scopes(), 'flags': auto.status()}) + for kind in COUNTS: + previous.upload(manifest['artifacts'][kind]) + save(folder / 'started-local.json', {'start_once': True}) + action = 'run' + else: + ident = str(uuid.UUID(ident)) + folder = STATE / ident + intent = cb.contract.protected_json(folder / 'intent.json') + require(intent['manifest_sha256'] == sha(path) and intent['manifest_path'] == str(path.resolve()) + and intent['runner_sha256'] == sha(__file__) + and intent['script_sha256'] == hashlib.sha256(SCRIPT.encode()).hexdigest() + and (folder / 'started-local.json').exists(), 'same exact started case; readback only') + action = 'read' + result = cb.powershell(SCRIPT, {'id': ident, 'action': action, 'manifest_sha256': sha(path), + 'artifacts': {k: manifest['artifacts'][k] for k in COUNTS}, 'counts': COUNTS}, timeout=100) + save(folder / ('readback-' + str(uuid.uuid4()) + '.json'), result) + require(result['manifest_sha256'] == sha(path) and set(result['reports']) == set(COUNTS), 'exact native manifest/reports') + for kind, actual in result['reports'].items(): + run = actual['result'] + listed = actual['listed'] + require(actual['sha256'] == manifest['artifacts'][kind]['sha256'] and len(actual['names']) == COUNTS[kind] + and listed['exit_code'] == run['exit_code'] == 0 and not listed['timed_out'] and not run['timed_out'] + and len(set(actual['names'])) == COUNTS[kind] + and re.search(r'test result: ok\. ' + str(COUNTS[kind]) + + r' passed; 0 failed; 0 ignored; 0 measured; 0 filtered out;', run['stdout']), + 'every native unit passes, none skipped/filtered') + before = cb.contract.protected_json(folder / 'server-before.json') + require(auto.scopes() == before['counts'] and auto.status() == before['flags'] and auto.flags_disabled(before['flags']), + 'both target counts and closed policies unchanged') + proof = {'verified': True, 'case_id': ident, 'manifest_path': str(path.resolve()), 'manifest_sha256': sha(path), + 'native': result, 'no_jobs': True, 'clock_adapter_qualified': False, 'local_service_proof': False} + destination = folder / 'passed.json' + if destination.exists(): + require(cb.contract.protected_json(destination) == proof, 'retain exact original native proof') + else: + save(destination, proof) + print(json.dumps({'verified': True, 'case_id': ident, 'tests': COUNTS, 'proof': str(destination.resolve())})) + + +def main(): + os.umask(0o077) + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('phase', choices=('build-local', 'native-units', 'readback')) + parser.add_argument('--manifest', type=Path) + parser.add_argument('--case') + args = parser.parse_args() + if args.phase == 'build-local': + require(not (args.manifest or args.case), 'local build only') + build() + else: + require(args.manifest and ((args.phase == 'readback') == bool(args.case)), 'explicit manifest and original readback case') + native(args.manifest, args.case) + + +if __name__ == '__main__': + try: + main() + except Exception as error: + print('WINDOWS_DELEGATION_QUALIFICATION_INCOMPLETE:' + type(error).__name__) + raise SystemExit(2) from None diff --git a/docs/handover.md b/docs/handover.md index 80281e1..93f6955 100644 --- a/docs/handover.md +++ b/docs/handover.md @@ -53,6 +53,16 @@ Zugänge und private Rohbelege bleiben gitignoriert. ## Aktuelle Windowsfortsetzung +Neuer SYSTEM-Delegationsadapter unabhängig quellgeprüft und als gemeinsamer +Build `80655271…`/Manifest `71914e2a…` mit70Quellen gebunden. Nativ Windows +23Lib+7Bin ohne Skips bestanden, Fall `5f95361a…`; RawMirror-Unknown und +wiederholter Terminal-Startup idempotent korrigiert. Statische private Pipe- +Diagnose und dokumentierte OVERLAPPED-Bytezählung korrigiert, ursprüngliche +Nativeursache weiter unbewiesen. SSH-Timeout vor erstem Unitstart nur nach +Readonly-Hash-/Abwesenheitsbeleg als erste noch nicht erreichte Phase fortgesetzt. +Neue Artefakte89004ad3/e6aacbdd **noch nicht installiert**; tatsächliche +Agent-/Helper-Runtime2bee/921c unverändert. [Genauer Quellen-/Native-Stand](windows-system-delegation-source.md). + Eigene Intentablage auf dem Windowsgast einmal vorbereitet und separat nur lesend verifiziert, Fall `f322a4d2…`: geschütztes Verzeichnis und genau zwei Admin-owned0B-Leaves mit Actor-RW/Synchronize ohne Delete/Owner/DACLrechte. diff --git a/docs/windows-gates.md b/docs/windows-gates.md index 8ce04e9..66069f9 100644 --- a/docs/windows-gates.md +++ b/docs/windows-gates.md @@ -11,7 +11,7 @@ bleibt eine eigenständige Abnahme. Dieser Bericht erweitert dessen Umfang nicht | Passiver Client | SCM-Dienst, Service-SID/ACLs, Enrollment/mTLS/WSS, Inventar mit Quelle/Zeit, Heartbeats und Mandantentrennung nativ bestanden; sieben native Rusttests. | Bestehende Identität und Telemetriehistorie bei allen weiteren Schritten erhalten. | | HTTPS/Fallback | Explizites HTTPS und automatischer Fallback nach zwei tatsächlichen mTLS-WSS503 unter LocalService nativ bestanden, jeweils mit frischen Heartbeats und Inventar-ACK. Testhooks geschlossen. Anschließend bestehende VM separat dauerhaft auf `auto` gestellt und bevorzugtes WSS erneut mit zwei frischen Heartbeats/ACK geprüft; Identität und ACLs erhalten, 0 Jobs/Grants. | Transportnachweise abgeschlossen; kein automatischer HTTPS→WSS-Switchback nachgewiesen. Installerdefault ebenfalls `auto`, aber keine neue Installationsabnahme. | | TLS-Lifecycle | Falsche CA, falscher Name und abgelaufenes Clientzertifikat mit gültigen Kontrollen nativ als getrennte Admin-TLS-Proben bestanden. Permanente Identität/Dienst unverändert. | Rotation und Widerruf noch gesondert sicher implementieren und prüfen. Permanente Identität nicht widerrufen oder ersetzen. | -| Signierter Jobkern | Gemeinsamer Core/Helper-Sync und atomare Outcomes nativ zuerst Linux, dann Windows geprüft; letzter Fixturelauf76/54. Default-Windowsdienst mit additiver Journalmigration erhaltend als LocalService aktualisiert; native Unitqualifikation12+6. Jobs bleiben deaktiviert, kein Binding/Ledger angelegt. | Tatsächlichen signierten Dienst-Sync, qualifizierte Uhr und vollständige Startgrenze nachweisen. Passive Migration und Unit-Fixturen ersetzen keinen Jobnachweis. | +| Signierter Jobkern | Gemeinsamer Kern zuletzt nativ Linux80→Windows58 qualifiziert. Neuer enger SYSTEM-Delegationsadapter mit dauerhaftem Intent, deny-only Abandon und unabhängigem RawMirror implementiert; native23+7Units bestanden. Bestehender laufender passiver Client bleibt unverändert; neue Artefakte noch nicht installiert. Jobs bleiben deaktiviert. | Tatsächlichen signierten Dienst-Sync, qualifizierte Uhr und vollständige Startgrenze nachweisen. Units ersetzen keinen LocalService-/SYSTEM- oder Jobnachweis; [Quellenstand](windows-system-delegation-source.md). | | PowerShell | Begrenzter5.1-Executor implementiert; exakte Skriptbytes, atomarer Prozessbaum, Rohbytes und Unknown bei unklarer Wirkung. Server/UI unterstützen feste Plattformvarianten. Keine tatsächliche Ausführung, Richtlinie derzeit geerbt. | Ausstehende konkrete Richtlinienentscheidung beachten, Encoding/Prozessgrenzen unter LocalService tatsächlich qualifizieren; ausschließlich vollständiger Antrag und tatsächlicher WebUI-Klick. | | Privilegierte Aktionen | Eigener SYSTEM-Helper nativ installiert und einmal gestartet, Manual/LocalSystem, leere Aktionsliste und Wirkungen/Reboot gesperrt. Die echte LocalService-Agentprobe scheitert derzeit; kein positiver Peer-/Sync-/Wirkungsnachweis. | Fehler ohne Erweiterung der Sicherheitsgrenze diagnostizieren; gegenseitige SID/PID/Binary-/Dateibindung und unabhängigen Sync nativ prüfen. | | Pakete/Dienste/Logs | Kleines MSI1.0.0 auf der VM erstellt, nur lesend geprüft und nach Download gehasht; keine Installation. Dienst-/Eventlog-/Paketquellen teilweise vorbereitet, keine native Aktionsabnahme. | Unveränderliche Tenantrezepte integrieren; UI-genehmigte MSI-/EXE-Installation/Upgrade/Noop, Dienste und Logs mit unabhängigen Nachbedingungen prüfen. | diff --git a/docs/windows-system-delegation-source.md b/docs/windows-system-delegation-source.md new file mode 100644 index 0000000..52cf16c --- /dev/null +++ b/docs/windows-system-delegation-source.md @@ -0,0 +1,157 @@ +# Windows-SYSTEM-Delegation: Quellenstand + +Stand2026-11-14, Spezifikation1.17. Dies ist ein implementierter **Kandidat zur +Sourceprüfung**, keine native Dienst-/IPC-/SCM-Abnahme. Die produktive Clock- +Qualifikation, Jobkonfiguration, Helperwirkung und veröffentlichten Fähigkeiten +bleiben deaktiviert. Der vorhandene Windowsgast und der endgültige Cloudserver +bleiben die Ziele; der Notebookhost bleibt Entwicklungsrechner. + +## Implementierter Ablauf + +Der Netzwerkagent persistiert vor einer möglichen Übergabe einen eigenen festen +SYSTEM-Intent mit den unveränderten vertraulichen Originalrequestbytes, +Originalgrant, Boot-/Agentinstanz-/Verbindungskontext und dem tatsächlich vom +Helper zurückgegebenen historischen Readyanker. Die Daten werden nicht in +Logs oder MCP-Ausgaben exportiert. Ein Intent ist noch kein Helperverbrauch. + +Dispatcher und Cancellation verwenden dieselbe dauerhafte SQLite-CAS: +`prepared` wird ausschließlich `known_no_dispatch` oder `may_have_dispatched`. +Nach einem May-Marker gibt es weder Execute-Redelivery noch eine Umklassifikation +zu bekanntem Nichtstart. Ein weiterer Start bleibt gesperrt. Lokale Consumption +entsteht nicht mehr vor der SYSTEM-Übergabe. Nach einem IPC-Ausgang wird zunächst +ehrliches Unknown mit dem **Originalgrant** gespeichert. + +Der separate strikt dekodierte Helpervorgang Abandon führt keinen Fixture-SCM- +oder Provideraufruf aus. Die lokale Allowlist wird nur logisch geprüft. Der +geschützte Adapter liest gezielt den tatsächlichen festen Intentstore und dessen +Known-CAS. Der gemeinsame Helperkern prüft unabhängig Signatur, aktuelle echte +Clock, ursprünglichen Boot, exakten alten Readyanker und erhaltene Lineage. +Zusätzlich bindet der Windowsguard eigene Kernel-PID, Prozessstart, Image-Datei-ID +sowie Ledger-/Lock-Datei-IDs. Ein neuer Nichtstart braucht dieselbe Helperinstanz; +neue Instanzwerte ersetzen niemals den alten Anker. Vorhandene Verbräuche und +Outcomes werden nicht überschrieben. + +Ein bereits dauerhaft bekannter Nichtstart darf nach Agentneustart genau einen +deny-only Abandon-Abgleich pro **neuer** Agentinstanz versuchen. Ein eigener +unveränderlicher Resume-Beleg steht vor diesem RPC. Danach folgt immer ein +frischer RawSnapshot. Eine verlorene Antwort erlaubt in derselben Instanz nur +weitere Snapshotabfrage. May-Marker und bloß vorbereitete unterbrochene Intents +bleiben konservativ Unknown/manuell gesperrt. + +## Spiegelung und Crashgrenzen + +Auch eine erfolgreiche Execute-/Abandonantwort ist kein Mirrorbeleg. Der Adapter +verlangt einen frischen unabhängig authentisierten rohen Helpersnapshot und +prüft das vollständige Originaltupel einschließlich Autorisierung, Plan, Aktion, +Identität, Originalboot und Hash des unveränderten Requestcodecs. Dazu kommen +kanonische Outcomebytes/Hash/Zustand, Output-/Rebootgrenzen und bei Erfolg die +exakten SCM-Nachbedingungen. Fehlender Helperversuch wird nicht ergänzt oder +als Nichtstart umgedeutet. + +Die **Helper-Abandontransaktion** schreibt Consumption, Originalstartzeugen und +festen NoEffect-Outcome atomar. Die **Network-Spiegelung** benutzt dagegen die +vorhandenen gemeinsamen APIs: `begin_execution` speichert c+start atomar; +anschließend wird der unabhängig nachgewiesene Outcome persistiert. Ein Crash +dazwischen bleibt gesperrt und wird nur aus einem erneut frisch authentisierten +RawSnapshot fortgesetzt. Eine vorhandene exakt passende Originalzeile wird +idempotent akzeptiert, abweichende Daten werden verweigert. Erst nach vollständiger +Spiegelung darf der unveränderte gemeinsame Sync fortfahren. Keine Missing-Row- +Prüfung wurde für diesen Adapter gelockert. + +Ein tatsächlicher gebundener Helper-Unknown wird vor dem gemeinsamen Sync auch +im Jobjournal kanonisch gespeichert. Ein pauschaler Transport-Unknown darf nicht +neben einem anderen tatsächlichen Unknown im Verbrauchsledger stehen bleiben. +Der Abgleich prüft dafür auch bereits gespiegelte Intents erneut anhand des +frischen RawSnapshot. Beim Startup wird ein schon gespeicherter tatsächlicher +Outcome erhalten; ein identischer terminaler Jobjournalbeleg wird nicht erneut +als Zustandsübergang geschrieben. Der neue enge Regressionsfall bildet Unknown- +Abweichung und wiederholten Startup nach der Mirror-Crashlücke ab. Archivierung +ist keine ACK-Verarbeitung und löscht keine ausstehenden Events. + +Die Network-Reservierungszeit bleibt der tatsächlich gespeicherte Intentzeitpunkt. +`mirror_observed_at` wird separat gespeichert; weder dieser Wert noch der +Snapshotzeitpunkt wird als unbekannte native Helper-/SCM-Startzeit dargestellt. + +## Geschützte Bootstrapgrenze + +`deploy/windows/system_intent_bootstrap.py` wurde nach unabhängiger Sourceprüfung +im Fall `f322a4d2-c537-4842-bf04-658ad8f814b0` tatsächlich auf der Windows-VM +ausgeführt. Quelle SHA256 `7597ac0f…`. Sie erzeugte ausschließlich den festen Ordner +`C:\ProgramData\RustDiscoverySystem\agent-intents` und die beiden vorab angelegten +Leaves `system-intents.sqlite` und `system-intents.sqlite-journal`. SYSTEM/Admins +bleiben Besitzer; der Agent-Service-SID erhält Verzeichnis-RX und auf genau den +Leaves Read/Write/Synchronize, keine Owner-/DACL-/Delete-/DeleteChild-Rechte. +Beide Dateien sind tatsächlich0Bytes lang. Native Owner-/ACLprüfung bestand: +Actor-Maske `0x12019f`, keine Directory-Create-/Delete-/Owner-/DACLrechte. +Ursprüngliche Agent-/Helperprozesse5140/6240, Konfiguration, öffentliche Pins +und private Helper-/Geräteschlüsselmetadaten blieben im Vorher-/Nachhervergleich +erhalten. Keine Dienständerung, Joboperation, SQLiteinitialisierung oder Clockänderung. +Der implementierte Helperadapter soll den Store nur lesend öffnen und gehaltene +Dateiidentitäten prüfen; das ist mit der Dateivorbereitung noch nicht nativ belegt. + +SQLite-PERSIST erhält das vorab angelegte Rollbackjournal, statt dem Actor +Verzeichnisanlage- oder Löschrechte zu geben. WAL und zusätzliche Dateien werden +nicht verwendet. Der Sourceentscheid folgt der [SQLite-PRAGMA-Dokumentation](https://www.sqlite.org/pragma.html#pragma_journal_mode). +Die tatsächliche Verzeichnis-/Leaf-ACL ist jetzt nativ vorbereitet und geprüft. +PERSIST-/Sharing-/Hotjournalfunktion unter dem Agenttoken bleibt separat offen; +zwei leere Dateien sind ausdrücklich kein SQLite-Funktionsnachweis. +Fehlende oder abweichende Provisionierung verweigert die SYSTEM-Strecke; der +passive Standardclient provisioniert oder repariert nichts. + +## Tatsächliche Prüfgrenzen + +Die genehmigte Entwicklungsdiagnose ergänzt im vorhandenen privaten +Agent-Probereport ausschließlich feste Stufen-/Codefelder. Der Helper hält +genau `state/pipe-last-error.json` plus eine feste Pendingdatei und versucht +höchstens einen Schreibzugriff je30Sekunden. Inhalt: PID, UTC-Beobachtungszeit, +statische Stufe/Whitelistcode und optional eine unmittelbar am fehlgeschlagenen +API-Aufruf erfasste numerische Win32fehlernummer. Keine Request-/Binding-/Grant- +oder Tokendaten, kein beliebiger Fehlertext, keine Historie und keine zusätzliche +RMM-/MCP-Inventarfähigkeit. Sicherheitsfehler schließen weiterhin Sync und Pipe; +ein fehlgeschlagenes Revert führt weiterhin zum Prozessabbruch. Ein Fehler der +Diagnostik wird nicht als Sicherheitsausnahme behandelt. + +Im gleichen Kandidaten ist der belegte Source-APIfehler bei OVERLAPPED-I/O +korrigiert: direkte ReadFile-/WriteFile-Bytezähler sind null, tatsächliche +Completionbytes kommen auch bei sofortigem TRUE aus GetOverlappedResult. +Bestehende Deadline-, Cancel- und Pufferlebensdauer-/Abbruchgrenzen bleiben +erhalten. Grundlage: [ReadFile](https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-readfile) +und [WriteFile](https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-writefile). +Dies belegt keine tatsächliche Ursache des früheren nativen Probefehlers. + +Feature- und Defaultprofile wurden lokal für Windows-GNU mit +`cargo clippy --locked --offline --all-targets -- -D warnings` erfolgreich +crossgeprüft; die neue Bootstrapquelle wurde durch den PowerShell-AST-Parser +geprüft. Eine enge neue CAS-Fixture ist implementiert und crosskompiliert. +Windows-Unitlinks wurden erzeugt; dies ist keine native Testausführung. + +Unabhängiger finaler Source-Review bestanden. Ein gemeinsamer Build bindet70 +Quellen, beide Alltargets-Clippy-Profile und sechs erhaltene AMD64-PE-Artefakte: +Agent, Helper, zwei Readonlyprobes und beide Unitprogramme. Manifest +`build-80655271-dbe6-474f-89de-014fd5a67c8d`, SHA256 `71914e2a…`; +private gehashte vollständige Quellenkopie zusätzlich erhalten. +Auf der tatsächlichen Windows-VM bestanden danach23Lib+7Bin-Units ohne Skips, +Fall `5f95361a-5b4e-4fa0-aeeb-414a8dfac613`. Anschließender separater Readback +und Prüfung unveränderter zentraler Zähler/geschlossener Flags bestanden. +Bootstrap-Administrator-Units, kein LocalService-/SYSTEM-Runtimebeweis. + +Der ursprüngliche SSH-Timeout lag nach der Übertragung vor dem ersten Unitstart. +Nur lesend wurden beide vollständigen Binärhashes/-größen und das Fehlen von +Remote-Testfall/Startmarker nachgewiesen. Danach wurde im selben Fall ausschließlich +die erste noch nicht erreichte Testphase mit zusätzlichem dauerhaftem Intent +fortgesetzt; keine erneute Übertragung oder Testwiederholung. Alter Timeout erhalten. + +Noch offen sind die tatsächlichen Agenttoken-/SQLite-/Crash-/Nichtstart-/Mirror- +Nachweise. Die separate positive Agent-/Helper- +Pipeprüfung, TrustedClock und WebUI-genehmigte SYSTEM-SCM-Abnahme bleiben eigene +offene Gates. Frühere native Unit- und passive Transportnachweise werden nicht +als Nachweis dieses neuen Ablaufs umetikettiert. + +Der gemeinsame Kernvertrag ist inzwischen separat nativ zuerst auf Cloud-Linux80, +danach Windows58 Prüfungen qualifiziert; siehe [Abandonvertrag](windows-system-abandon.md). +Die Sourcekorrekturen zu tatsächlichen Helper-Unknowns und wiederholtem Startup +erhalten identische kanonische Ergebnisse samt Originalgrant. Statische private +Pipe-Fehlercodes und der dokumentierte OVERLAPPED-Bytezählerpfad sind korrigiert; +die Ursache des ursprünglichen tatsächlichen Verbindungsfehlers bleibt unbewiesen. +Die neuen Adapterunits sind23Lib+7Bin **nativ bestanden**; eine Installation des +damit gebundenen neuen Agenten/Helpers ist noch nicht erfolgt. diff --git a/docs/windows-system-state-witness.md b/docs/windows-system-state-witness.md new file mode 100644 index 0000000..9152818 --- /dev/null +++ b/docs/windows-system-state-witness.md @@ -0,0 +1,69 @@ +# Fester Nur-Lese-Zeuge für Windows-Helperwartung + +Stand2026-11-14: `examples/system_state.rs` ist implementiert. Windows-GNU- +Example-Clippy mit `-D warnings`, Release-Link und AMD64-PE-Prüfung bestanden +im Defaultprofil. **Keine native Ausführung, Dienstwartung oder Erhaltungsabnahme +durch diesen Quellenstand.** Die gemeinsame Artefaktqualifikation des späteren +Wartungsrunners bleibt separat. + +`SystemStateProbe.exe` hat keine Argumente und benötigt einen tatsächlich +aktivierten Administratorgruppen-Token ohne Threadimpersonation. Der Bootstrap- +Runner bindet den tatsächlichen Aufrufkontext. Es ist kein LocalService-/SYSTEM- +Pipebeweis, keine RMM-Capability und kein Genehmigungsweg. + +Der feste Beobachtungsumfang ist `C:\ProgramData\RustDiscoverySystem`: + +- `state\ledger.sqlite`: ausschließlich lesende SQLite-Transaktion, + Integrität, bekannte Schema-/Indexdefinitionen sowie Zeilenzahl und + deterministischer typisierter Hash je erlaubter Tabelle. Pflicht sind + `consumed`, `execution_evidence` und `windows_helper_files`. Die vier + `helper_sync_*`-Tabellen dürfen gemeinsam fehlen; `absent_tables` meldet das + ausdrücklich. Teilweise oder unbekannte Schemas werden verweigert. +- `state\executor.lock`, `helper.json`, `agent-pin.json`: ausschließlich + Dateimetadaten, Owner-/DACL-Prüfung und ACLhash. Keine Pin-/Request-/Schlüssel- + oder privaten Zeileninhalte in der Ausgabe. +- Optional `agent-intents`: entweder vollständig abwesend oder exakt der + vorprovisionierte Ordner mit zwei regulären, leeren festen Leaves. Nur + Metadaten; keine SQLiteöffnung oder Initialisierung dieser Dateien. Ein + bereits initialisierter Intentstore gehört nicht zu diesem Wartungsprofil. + +Gehaltene Handles, Wiederöffnung des festen Pfads und die tatsächliche native +SQLite-VFS-Dateikennung müssen übereinstimmen. Volume-ID und FILETIME erscheinen +als Dezimalstrings, damit PowerShell alle64Bit erhält. Der Witness akzeptiert +die vom bestehenden Helper erstellten geerbten Datei-ACEs nur unter +dem gehaltenen geschützten State-Verzeichnis, bei weiterhin exakt geprüften +effektiven Writerrollen und SYSTEM-/Admin-Owner. Andere Schutzdeskriptoren werden +nicht passend geändert; Root-, State- und Intentverzeichnisse bleiben geschützt. +Die Datenbankverbindung +bleibt read-only; es gibt weder Migration noch Schreib-PRAGMA, WAL-Override, +`immutable` oder `nolock`. Bestehende Journal-/WAL-/SHM-Sidecars und ein +abweichender Rollbackheader werden vor der SQLiteöffnung verweigert. Kein +Journal wird repariert, gelöscht oder zurückgesetzt. + +Die separate tatsächliche Nur-Lese-Erhebung `acl-readonly-d4795190…` bestätigte +den geschützten State-Ordner mit Administratorowner und die beiden vom Helper +erzeugten SYSTEM-eigenen, ungeschützten Dateien mit genau drei geerbten Full- +Control-ACEs für SYSTEM, Administratoren und Helper-Service-SID. Keine Reparse- +Points, keine Datei-/ACLänderung oder privaten Dateiinhalte gelesen. Die private +Metadatenreceipt hat SHA256 `12e2ca43…`; dies ist **keine native Ausführung** +des neuen vollständigen Witnessprogramms. + +Der Reader begrenzt Dateigröße auf1GiB, Schemaobjekte auf32, Zeilen pro Tabelle +auf10000, einzelne Werte auf1MiB, gehashte Text-/Blobwerte insgesamt auf32MiB +und die JSON-Ausgabe auf32768Byte. Synchrone Dateisystem-/Securityabfragen +besitzen dadurch keine harte Zeitgrenze; der separate Bootstrapcontroller muss +seinen eigenen Witnessprozess begrenzen. Fehler geben ausschließlich +`SYSTEM_STATE_WITNESS_REFUSED` aus. Kein Bytehash einer laufenden Datenbank; +einen solchen darf der Wartungsrunner erst nach bestätigtem Ende beider alten +Dienstprozesse separat erheben. + +Die native Handleabfrage verwendet die dokumentierte reine +[SQLite-WIN32_GET_HANDLE-Operation](https://www.sqlite.org/c3ref/c_fcntl_begin_atomic_write.html). +Der Handle bleibt Eigentum von SQLite. Der Schutzdescriptor wird nur mit +[GetSecurityInfo](https://learn.microsoft.com/en-us/windows/win32/api/aclapi/nf-aclapi-getsecurityinfo) +gelesen; keine Privilegien oder DACLs werden verändert. + +Der bestehende `RuntimeStateProbe.exe` für das Agenttelemetriejournal bleibt +unverändert und wird vom Wartungsrunner gesondert verwendet. Das Notebook +bleibt Entwicklungsrechner; kein Agent, Server, VM-Neustart oder Job wurde +für diesen Quellen-/Crossbuildnachweis gestartet.