diff --git a/crates/agent-windows/src/helper_ipc.rs b/crates/agent-windows/src/helper_ipc.rs index 26268c6..b886761 100644 --- a/crates/agent-windows/src/helper_ipc.rs +++ b/crates/agent-windows/src/helper_ipc.rs @@ -7,6 +7,7 @@ use rmm_agent_core::{ }; use rmm_domain::jobs::hash; use serde::{Deserialize, Serialize, de::DeserializeOwned}; +use sha2::{Digest, Sha256}; use std::{ ffi::c_void, fs::File, @@ -29,7 +30,13 @@ use windows_sys::Win32::{ }; pub const HELPER_SERVICE: &str = "RmmDiscoverySystemHelper"; -pub const PIPE_NAME: &str = r"\\.\pipe\RustDiscovery-System-v1"; +// Framing v2 is deliberately a separate endpoint. Old peers cannot send a +// v1 call to this server or interpret the drain receipt as another reply. +pub const PIPE_NAME: &str = r"\\.\pipe\RustDiscovery-System-v2"; +const FRAME_TAG: [u8; 8] = *b"RDPIPC02"; +const DRAIN_TAG: [u8; 8] = *b"RDPACK02"; +const FRAME_HEADER_LEN: usize = 12; +const DRAIN_RECEIPT_LEN: usize = 56; 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; @@ -44,6 +51,7 @@ pub enum Stage { ClientSendFrame, ClientReadReply, ClientReplyBinding, + ClientDrainReceipt, ClientReport, ServerConnect, ServerReadHeader, @@ -54,6 +62,7 @@ pub enum Stage { ServerCallBinding, ServerRecheckPeer, ServerReply, + ServerDrainReceipt, Unclassified, } impl Stage { @@ -66,6 +75,7 @@ impl Stage { Self::ClientSendFrame => "client_send_frame", Self::ClientReadReply => "client_read_reply", Self::ClientReplyBinding => "client_reply_binding", + Self::ClientDrainReceipt => "client_drain_receipt", Self::ClientReport => "client_report", Self::ServerConnect => "server_connect", Self::ServerReadHeader => "server_read_header", @@ -76,6 +86,7 @@ impl Stage { Self::ServerCallBinding => "server_call_binding", Self::ServerRecheckPeer => "server_recheck_peer", Self::ServerReply => "server_reply", + Self::ServerDrainReceipt => "server_drain_receipt", Self::Unclassified => "unclassified", } } @@ -123,6 +134,8 @@ fn known_code(text: &str) -> Option<&'static str> { "HELPER_FRAME_CAPACITY", "HELPER_FRAME_EMPTY", "HELPER_FRAME_LENGTH", + "HELPER_FRAME_VERSION", + "HELPER_DRAIN_RECEIPT", "HELPER_HANDLE", "HELPER_IMAGE_PARENT", "HELPER_IMAGE_SIZE", @@ -1189,27 +1202,60 @@ fn encode(value: &impl Serialize) -> Result> { ensure!(!writer.0.is_empty(), "HELPER_FRAME_EMPTY"); Ok(writer.0) } -fn send(pipe: HANDLE, value: &impl Serialize, deadline: Instant) -> Result<()> { - let mut bytes = encode(value)?; - let mut header = u32::try_from(bytes.len())?.to_be_bytes(); - transfer(pipe, &mut header, true, deadline)?; - transfer(pipe, &mut bytes, true, deadline) +fn frame_header(len: usize) -> Result<[u8; FRAME_HEADER_LEN]> { + ensure!((1..=FRAME_LIMIT).contains(&len), "HELPER_FRAME_LENGTH"); + let mut header = [0; FRAME_HEADER_LEN]; + header[..8].copy_from_slice(&FRAME_TAG); + header[8..].copy_from_slice(&u32::try_from(len)?.to_be_bytes()); + Ok(header) } -fn frame_length(header: [u8; 4]) -> Result { - let len = u32::from_be_bytes(header) as usize; +fn send_bytes(pipe: HANDLE, bytes: &mut [u8], deadline: Instant) -> Result<()> { + let mut header = frame_header(bytes.len())?; + transfer(pipe, &mut header, true, deadline)?; + transfer(pipe, bytes, true, deadline) +} +fn send(pipe: HANDLE, value: &impl Serialize, deadline: Instant) -> Result<()> { + send_bytes(pipe, &mut encode(value)?, deadline) +} +fn frame_length(header: [u8; FRAME_HEADER_LEN]) -> Result { + ensure!(header[..8] == FRAME_TAG, "HELPER_FRAME_VERSION"); + let len = u32::from_be_bytes(header[8..].try_into()?) as usize; ensure!((1..=FRAME_LIMIT).contains(&len), "HELPER_FRAME_LENGTH"); Ok(len) } fn length(pipe: HANDLE, deadline: Instant) -> Result { - let mut header = [0u8; 4]; + let mut header = [0u8; FRAME_HEADER_LEN]; transfer(pipe, &mut header, false, deadline)?; frame_length(header) } -fn body(pipe: HANDLE, len: usize, deadline: Instant) -> Result { +fn body_bytes(pipe: HANDLE, len: usize, deadline: Instant) -> Result> { ensure!((1..=FRAME_LIMIT).contains(&len), "HELPER_FRAME_LENGTH"); let mut bytes = vec![0u8; len]; transfer(pipe, &mut bytes, false, deadline)?; - Ok(serde_json::from_slice(&bytes)?) + Ok(bytes) +} +fn body(pipe: HANDLE, len: usize, deadline: Instant) -> Result { + Ok(serde_json::from_slice(&body_bytes(pipe, len, deadline)?)?) +} +// This fixed-size receipt only acknowledges receipt of these transport bytes. +// It never acknowledges durable evidence, clears state, or permits an effect. +fn drain_receipt(challenge: Uuid, reply_bytes: &[u8]) -> [u8; DRAIN_RECEIPT_LEN] { + let mut receipt = [0; DRAIN_RECEIPT_LEN]; + receipt[..8].copy_from_slice(&DRAIN_TAG); + receipt[8..24].copy_from_slice(challenge.as_bytes()); + receipt[24..].copy_from_slice(&Sha256::digest(reply_bytes)); + receipt +} +fn verify_drain_receipt( + receipt: &[u8; DRAIN_RECEIPT_LEN], + challenge: Uuid, + reply_bytes: &[u8], +) -> Result<()> { + ensure!( + *receipt == drain_receipt(challenge, reply_bytes), + "HELPER_DRAIN_RECEIPT" + ); + Ok(()) } pub struct Server { @@ -1305,13 +1351,24 @@ impl Server { peer.check().context(Stage::ServerRecheckPeer)?; Ok(Some(Received { call, peer })) } - pub fn reply(&self, value: &Reply) -> Result<()> { - send( - self.pipe.as_raw_handle(), - value, - Instant::now() + Duration::from_secs(5), - ) - .context(Stage::ServerReply) + pub fn reply(&self, value: &Reply, peer: &mut PeerProof) -> Result<()> { + // Do not disconnect after WriteFile merely copied bytes into a pipe + // buffer. Wait for a receipt from the same fully pinned peer within + // the original reply deadline, without an unbounded FlushFileBuffers. + let deadline = Instant::now() + Duration::from_secs(5); + peer.check().context(Stage::ServerRecheckPeer)?; + let mut bytes = encode(value).context(Stage::ServerReply)?; + send_bytes(self.pipe.as_raw_handle(), &mut bytes, deadline).context(Stage::ServerReply)?; + let mut receipt = [0; DRAIN_RECEIPT_LEN]; + transfer(self.pipe.as_raw_handle(), &mut receipt, false, deadline) + .context(Stage::ServerDrainReceipt)?; + verify_drain_receipt(&receipt, value.challenge, &bytes) + .context(Stage::ServerDrainReceipt)?; + peer.check().context(Stage::ServerRecheckPeer)?; + if Instant::now() >= deadline { + return Err(anyhow::anyhow!("HELPER_PIPE_TIMEOUT")).context(Stage::ServerDrainReceipt); + } + Ok(()) } pub fn disconnect(&self) { unsafe { @@ -1345,7 +1402,9 @@ pub fn exchange(pin: &Pin, call: &Call, timeout: Duration) -> Result { .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)?; + let reply_bytes = + body_bytes(pipe.as_raw_handle(), len, deadline).context(Stage::ClientReadReply)?; + let reply: Reply = serde_json::from_slice(&reply_bytes).context(Stage::ClientReadReply)?; peer.check().context(Stage::ClientHelperPeer)?; if !(reply.schema_version == 1 && reply.challenge == call.challenge @@ -1366,6 +1425,11 @@ pub fn exchange(pin: &Pin, call: &Call, timeout: Duration) -> Result { ) { return Err(anyhow::anyhow!("HELPER_REPLY_OPERATION")).context(Stage::ClientReplyBinding); } + // Only a complete, authenticated, bound and operation-checked reply gets + // this receipt. Hash the received bytes, never a JSON re-serialization. + let mut receipt = drain_receipt(call.challenge, &reply_bytes); + transfer(pipe.as_raw_handle(), &mut receipt, true, deadline) + .context(Stage::ClientDrainReceipt)?; Ok(reply) } @@ -1388,12 +1452,21 @@ mod tests { #[test] fn untrusted_lengths_are_rejected_before_body_allocation() { for n in [0, (FRAME_LIMIT + 1) as u32, u32::MAX] { - assert!(frame_length(n.to_be_bytes()).is_err()); + let mut header = [0; FRAME_HEADER_LEN]; + header[..8].copy_from_slice(&FRAME_TAG); + header[8..].copy_from_slice(&n.to_be_bytes()); + assert!(frame_length(header).is_err()); + assert!(frame_header(n as usize).is_err()); } - assert_eq!( - frame_length((FRAME_LIMIT as u32).to_be_bytes()).unwrap(), - FRAME_LIMIT - ); + let header = frame_header(FRAME_LIMIT).unwrap(); + assert_eq!(frame_length(header).unwrap(), FRAME_LIMIT); + // Neither a v1 length prefix nor another framing version is accepted. + let mut old = [0; FRAME_HEADER_LEN]; + old[..4].copy_from_slice(&1u32.to_be_bytes()); + assert!(frame_length(old).is_err()); + let mut foreign = header; + foreign[7] = b'1'; + assert!(frame_length(foreign).is_err()); } #[test] fn protocol_does_not_accept_a_shell_operation() { @@ -1417,6 +1490,22 @@ mod tests { 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"); + let challenge = Uuid::from_u128(1); + let raw = br#"{"a":1,"b":2}"#; + let receipt = drain_receipt(challenge, raw); + assert!(verify_drain_receipt(&receipt, challenge, raw).is_ok()); + // Same JSON meaning, different exact bytes must not drain this reply. + assert!(verify_drain_receipt(&receipt, challenge, br#"{"b":2,"a":1}"#).is_err()); + assert!(verify_drain_receipt(&receipt, Uuid::from_u128(2), raw).is_err()); + for offset in [0, 7, 8, 23, 24, DRAIN_RECEIPT_LEN - 1] { + let mut changed = receipt; + changed[offset] ^= 1; + let error = verify_drain_receipt(&changed, challenge, raw) + .unwrap_err() + .context(Stage::ServerDrainReceipt); + assert_eq!(diagnostic(&error).code, "HELPER_DRAIN_RECEIPT"); + assert_eq!(diagnostic(&error).stage, "server_drain_receipt"); + } } #[test] fn reconfigured_service_cannot_adopt_an_old_running_identity() { diff --git a/crates/agent-windows/src/helper_system.rs b/crates/agent-windows/src/helper_system.rs index a50ec24..12b71f6 100644 --- a/crates/agent-windows/src/helper_system.rs +++ b/crates/agent-windows/src/helper_system.rs @@ -1179,7 +1179,7 @@ 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 let Err(error) = server.reply(&reply) { + if let Err(error) = server.reply(&reply, &mut incoming.peer) { if let Some(sync) = &mut helper.sync { sync.close(); } diff --git a/deploy/windows/system_pipe_acceptance.py b/deploy/windows/system_pipe_acceptance.py new file mode 100644 index 0000000..d1288d8 --- /dev/null +++ b/deploy/windows/system_pipe_acceptance.py @@ -0,0 +1,368 @@ +#!/usr/bin/env python3 +"""Source-bound Pipe-v2 pack and create-once native units, never deployment. + +The access root only supplies the existing pinned SSH transport and immutable +native Core proofs. No credentials/devstate symlinks, copies, services or jobs. +""" +import argparse +import base64 +import hashlib +import json +import os +from pathlib import Path +import re +import shutil +import stat +import subprocess +import sys +import uuid + +CODE = Path(__file__).resolve().parents[2] +ACCESS = Path('/home/kevin/Schreibtisch/Coding/rust-discovery') +sys.path.insert(0, str(ACCESS / 'deploy/windows')) +sys.path.insert(0, str(ACCESS / 'deploy')) +import client_bootstrap as cb +import auto_transport_acceptance as auto +import runtime_upgrade as previous + +require = cb.contract.require +BUILD = CODE / 'devstate/windows-pipe-drain-build' +NATIVE = CODE / 'devstate/windows-client/native-pipe-v2' +# These imported transports read their original protected access configuration. +# Their new local upload/error receipts belong to this isolated checkout. +cb.EVIDENCE = CODE / 'devstate/windows-client' +CORE = ACCESS / 'devstate/core-no-start/build-de37ee99-11bf-465b-9519-45ad8f6e5b87/manifest.json' +CORE_SHA = '1bc082031fcad20d5a519b0004f63d499dbba2ea9ce25d56230d48d2556d474b' +LOCAL = CODE / 'devstate/windows-pipe-drain/qualification-1e0f2b22-1e74-41af-b8ed-0a356be054ba/qualification.json' +LOCAL_SHA = 'a7e3c604d4ad19feed0323ac23aa7a0290b6fb93bc57649cafbadbe5f293b1d4' +TARGET = 'x86_64-pc-windows-gnu' +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'} +# Expected V2 native lists: unchanged names, two existing codec cases extended. +TESTS = { + 'lib': sorted([ + 'clock::tests::gate_requires_all_status_and_uncertainty_evidence', + 'clock::tests::production_clock_stays_closed_until_native_adapter_qualification', + 'clock::tests::suspend_closes_until_actual_post_resume_sync', + 'clock::tests::timeout_worker_stays_closed_without_a_second_call', + 'clock::tests::wall_step_and_overflow_fail_closed', + 'helper_ipc::tests::pipe_client_rights_cannot_create_an_instance', + 'helper_ipc::tests::protocol_does_not_accept_a_shell_operation', + 'helper_ipc::tests::reconfigured_service_cannot_adopt_an_old_running_identity', + 'helper_ipc::tests::serialization_refuses_oversized_frame', + 'helper_ipc::tests::untrusted_lengths_are_rejected_before_body_allocation', + 'helper_runtime::intent::tests::cancel_and_dispatch_cas_are_exclusive_and_history_cannot_be_rewritten', + 'helper_runtime::tests::system_route_binds_original_request_shape_budget_and_reply_scope', + 'helper_system::tests::completed_scm_state_cannot_override_elapsed_runtime', + 'helper_system::tests::fixture_recovery_cannot_add_commands_reboots_or_restart_attempts', + 'helper_system::tests::service_adapter_exposes_only_implemented_states', + 'helper_system::tests::single_fixture_cannot_start_services_or_groups_as_dependencies', + 'journal::tests::bounded_existing_journal_preserves_identity_sequence_and_unacked_bytes', + 'journal::tests::durable_reopen_ack_identity_and_single_process', + 'journal::tests::migrates_original_passive_schema_without_rewriting_pending_envelopes', + 'journal::tests::unsupported_page_size_is_refused_without_mutating_existing_data', + 'native::tests::boot_identity_survives_independent_native_queries', + 'native::tests::native_inventory_has_real_windows_sources_and_no_fake_inodes', + 'process::tests::arguments_never_become_shell_code_and_trailing_slashes_are_quoted']), + 'bin': sorted([ + 'https_channel::endpoint_tests::configured_dns_identity_cannot_inject_a_url_or_disable_name_checks', + 'runtime::tests::bound_helper_unknown_repairs_generic_crash_gap_without_ack_or_duplicate_result', + 'runtime::tests::legacy_inventory_keeps_original_bytes_until_concrete_core_ack', + 'runtime::tests::old_passive_configuration_cannot_enable_jobs_or_read_the_clock', + 'runtime::tests::worker_fence_requires_same_generation_and_entire_clock_interval', + 'wire_channel::tests::auto_fallback_classifies_upgrade_refusal_but_never_authentication', + 'wire_channel::tests::explicit_transport_names_cannot_enable_verification_bypasses'])} +COUNTS = {'lib': 23, 'bin': 7} + + +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 private_file(path): + path = Path(path) + require(not path.is_symlink() and not path.parent.is_symlink(), 'private regular path') + info = path.lstat() + require(stat.S_ISREG(info.st_mode) and info.st_uid == os.getuid() + and stat.S_IMODE(info.st_mode) == 0o600, 'private regular owner/mode') + return path + + +def sources(): + require(ACCESS.is_dir() and not ACCESS.is_symlink() and cb.ROOT == ACCESS, + 'original access checkout only; no copied configuration') + names = {'Cargo.toml', 'Cargo.lock', 'rust-toolchain.toml', 'proto/agent.proto', + 'deploy/windows/system_pipe_acceptance.py', 'docs/windows-helper-pipe-drain.md'} + names.update(str(p.relative_to(CODE)) for p in (CODE / 'crates').glob('*/Cargo.toml')) + for crate in ('agent-windows', 'agent-core', 'domain', 'protocol'): + names.update(str(p.relative_to(CODE)) for p in (CODE / 'crates' / crate).rglob('*') + if p.is_file() and p.suffix in ('.rs', '.c', '.h', '.idl', '.toml')) + names.update('crates/agent/src/' + n + '.rs' for n in ('https_channel', 'transport', 'wire_channel')) + require(all((CODE / n).is_file() and not (CODE / n).is_symlink() for n in names), 'complete fixed build sources') + transport = {} + for module in tuple(sys.modules.values()): + raw = getattr(module, '__file__', None) + if raw: + path = Path(raw).resolve() + if path.is_relative_to(ACCESS / 'deploy') and path.suffix == '.py': + transport[str(path.relative_to(ACCESS))] = sha(path) + require('deploy/windows/client_bootstrap.py' in transport, 'actual transport source binding') + return {'code': {n: sha(CODE / n) for n in sorted(names)}, 'access_transport': dict(sorted(transport.items()))} + + +def core_proof(): + require(sha(CORE) == CORE_SHA, 'exact original 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]) + and sum(len(v['names']) for v in actual.values()) == count, 'all native Core reports') + for kind, item in actual.items(): + n = manifest['test_counts'][platform][kind] + output = base64.b64decode(item['stdout_b64'], validate=True) + 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), + 'complete actual native Core result') + 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(CODE / name) == digest, 'isolated common source equals actual native Core proof') + return result + + +def reused_qualification(): + require(sha(LOCAL) == LOCAL_SHA, 'exact retained local qualification') + value = cb.contract.protected_json(LOCAL) + require(value['type'] == 'rmm.windows.pipe-drain-local-qualification' + and value['source_before_after_equal'] is True and value['native_executed'] is False + and value['clock_qualified'] is False and value['native_counts_expected'] == COUNTS + and len(value['commands']) == 4 and all(c['exit_code'] == 0 for c in value['commands']), + 'actual previous cross-Clippy/release/unit-link qualification') + for name, digest in value['source_files'].items(): + require(sha(CODE / name) == digest, 'all original qualified inputs remain unchanged') + for item in list(value['artifacts'].values()) + list(value['unit_links'].values()): + path = Path(item['path']) + target = LOCAL.parent / 'target' + require(path.is_relative_to(target), 'qualified link under private target') + current = target + for part in ('',) + path.relative_to(target).parts: + if part: + current = current / part + info = current.lstat() + require(not stat.S_ISLNK(info.st_mode) and info.st_uid in (0, os.getuid()) + and info.st_mode & 0o022 == 0, 'protected original compiler output') + require(stat.S_ISREG(path.lstat().st_mode) and sha(path) == item['sha256'] + and path.stat().st_size == item['bytes'], 'retained exact qualified links') + require(re.search(r'^pub const ADAPTER_QUALIFIED: bool = false;$', + (CODE / 'crates/agent-windows/src/clock.rs').read_text(), re.M), 'production clock remains closed') + return value + + +def command(args, folder, name): + with (folder / (name + '.stdout')).open('xb') as out, (folder / (name + '.stderr')).open('xb') as err: + result = subprocess.run(args, stdout=out, stderr=err) + save(folder / (name + '.json'), {'exit_code': result.returncode, 'local_only': True, 'arguments': args}) + require(result.returncode == 0, 'local build failed; original stdout/stderr retained') + + +def build(): + before, common, prior = sources(), core_proof(), reused_qualification() + folder = BUILD / ('build-' + str(uuid.uuid4())) + folder.mkdir(mode=0o700, parents=True) + save(folder / 'source-before.json', before) + save(folder / 'reused-qualification.json', {'path': str(LOCAL), 'sha256': LOCAL_SHA, 'value': prior}) + image = prior['build_image'] + require(re.fullmatch(r'sha256:[a-f0-9]{64}', image), 'immutable compiler image') + target = LOCAL.parent / 'target' + # Only these two RELEASE examples were missing. Original Clippy, EXEs and + # unit links are hash-bound above and are deliberately not rerun/rebuilt. + command(['docker', 'run', '--rm', '--network', 'none', '--read-only', '--tmpfs', '/tmp:rw,size=512m', + '-v', str(CODE) + ':/src:ro', '-v', str(target) + ':/target', '-e', 'CARGO_TARGET_DIR=/target', + '--entrypoint', 'cargo', image, 'build', '--manifest-path', '/src/Cargo.toml', '--locked', '--offline', + '--target', TARGET, '-p', 'rmm-agent-windows', '--features', 'system-helper', '--release', + '--example', 'runtime_state', '--example', 'system_state'], folder, 'release-examples') + selected = {'agent': Path(prior['artifacts']['agent']['path']), 'helper': Path(prior['artifacts']['helper']['path']), + 'lib': Path(prior['unit_links']['lib']['path']), 'bin': Path(prior['unit_links']['bin']['path']), + 'probe': target / TARGET / 'release/examples/runtime_state.exe', + 'system_probe': target / TARGET / 'release/examples/system_state.exe'} + artifacts = {} + for kind, original in selected.items(): + output, retained = folder / NAMES[kind], folder / (NAMES[kind] + '.linked') + original_sha = sha(original) + for destination in (retained, output): + shutil.copyfile(original, destination) + destination.chmod(0o600) + if kind in COUNTS: + command(['docker', 'run', '--rm', '--network', 'none', '--read-only', '-v', str(folder) + ':/artifacts', + '--entrypoint', 'x86_64-w64-mingw32-strip', image, '--strip-debug', '/artifacts/' + NAMES[kind]], + folder, 'strip-' + kind) + require(sha(original) == sha(retained) == original_sha, 'original links retained unchanged') + data = output.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(output), 'name': NAMES[kind], 'sha256': sha(output), 'bytes': len(data), + 'original_link_path': str(retained), 'original_link_sha256': original_sha, + 'release_build': kind not in COUNTS, 'debug_stripped': kind in COUNTS} + require(sources() == before and core_proof() == common and reused_qualification() == prior, + 'compiler inputs, original links and native Core proof unchanged before/after') + manifest = {'schema_version': 1, 'type': 'rmm.windows.pipe-v2-build', 'source_files': before, + 'source_before_after_equal': True, 'code_root': str(CODE), 'access_root': str(ACCESS), + 'artifacts': artifacts, 'test_names_expected': TESTS, 'native_counts_expected': COUNTS, + 'core_native_proofs': common, 'reused_local_qualification': {'path': str(LOCAL), 'sha256': LOCAL_SHA}, + 'build_image_id': image, 'transport_version': 2, 'raw_v1_controls_qualified': False, + 'production_clock_qualified': False, 'native_executed': False} + save(folder / 'manifest.json', manifest) + print(json.dumps({'manifest': str(folder / 'manifest.json'), '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 Pipe V2 unit case'} +$root='C:\ProgramData\RustDiscoveryBootstrap';$case=Join-Path $root ('native-pipe-v2-'+$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 unit 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 16 -Compress));$f=[IO.File]::Open($path,'CreateNew','Write','Read');try{$f.Write($b,0,$b.Length);$f.Flush($true)}finally{$f.Dispose()}} +function ReadPrivate($path){Safe $path $false;if((Get-Item -LiteralPath $path).Length-gt196608){throw 'Unit report bound'};return (Get-Content -LiteralPath $path -Raw|ConvertFrom-Json)} +Safe $root $true +if($p.action-eq'read'){ + if(!(Test-Path -LiteralPath $case)){@{case_exists=$false;completed=$false;readback_only=$true}|ConvertTo-Json -Compress;exit 0} + Safe $case $true;$i=ReadPrivate (Join-Path $case 'intent.json');if($i.manifest_sha256-cne$p.manifest_sha256){throw 'Original unit intent'} + $result=Join-Path $case 'result.json';if(Test-Path -LiteralPath $result){ReadPrivate $result|ConvertTo-Json -Depth 16 -Compress;exit 0} + $lists=@{};$reports=@{};foreach($kind in @('lib','bin')){$file=Join-Path $case ('list-'+$kind+'.json');if(Test-Path -LiteralPath $file){$lists[$kind]=ReadPrivate $file};$file=Join-Path $case ('report-'+$kind+'.json');if(Test-Path -LiteralPath $file){$reports[$kind]=ReadPrivate $file}} + @{case_exists=$true;completed=$false;readback_only=$true;started=(Test-Path -LiteralPath (Join-Path $case 'started.json'));lists=$lists;reports=$reports}|ConvertTo-Json -Depth 16 -Compress;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();if(!$c.WaitForExit(2000)){throw 'Own unit process did not exit'}};if(!$o.Wait(2000)-or!$e.Wait(2000)-or$o.Result.Length-gt65536-or$e.Result.Length-gt65536){throw 'Bounded unit output collection'};return @{exit_code=$c.ExitCode;timed_out=$timed;stdout=$o.Result;stderr=$e.Result}}finally{$c.Dispose()}} +$lists=@{};$paths=@{} +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 test 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-or(Get-Item -LiteralPath $exe).Length-ne$a.bytes){throw 'Exact unit artifact'} + $listed=Run $exe '--list --format terse';[string[]]$names=@($listed.stdout-split'\r?\n'|Where-Object{$_-match': test$'}|ForEach-Object{$_-replace': test$',''});[Array]::Sort($names,[StringComparer]::Ordinal) + $value=@{sha256=$a.sha256;names=$names;listed=$listed};WriteNew (Join-Path $case ('list-'+$kind+'.json')) $value + if($listed.exit_code-ne0-or$listed.timed_out-or$names.Count-ne$p.counts.$kind-or(($names-join"`n")-cne(@($p.test_names.$kind)-join"`n"))){throw 'Exact complete native list before ANY tests'};$lists[$kind]=$value;$paths[$kind]=$exe +} +WriteNew (Join-Path $case 'both-lists-verified.json') @{manifest_sha256=$p.manifest_sha256;both_lists_verified_before_any_test=$true} +WriteNew (Join-Path $case 'started.json') @{start_once=$true};$reports=@{} +foreach($kind in @('lib','bin')){$run=Run $paths[$kind] '--test-threads=1';$value=@{sha256=$lists[$kind].sha256;names=$lists[$kind].names;listed=$lists[$kind].listed;result=$run};WriteNew (Join-Path $case ('report-'+$kind+'.json')) $value;$reports[$kind]=$value;if($run.exit_code-ne0-or$run.timed_out){throw 'Native unit execution failed; readback only'}} +$value=@{completed=$true;manifest_sha256=$p.manifest_sha256;reports=$reports;both_lists_verified_before_any_test=$true;bootstrap_administrator_units=$true;local_service_proof=$false;native_pipe_proof=$false;rmm_jobs=$false;product_state_writes=$false};WriteNew (Join-Path $case 'result.json') $value;$value|ConvertTo-Json -Depth 16 -Compress +''' + + +def verified(path, current=True): + path = private_file(path) + require(path.parent.parent == BUILD and path.parent.name.startswith('build-') + and str(uuid.UUID(path.parent.name[6:])) == path.parent.name[6:], 'fixed private build namespace') + value = cb.contract.protected_json(path) + require(value['type'] == 'rmm.windows.pipe-v2-build' and value['schema_version'] == 1 + and value['source_before_after_equal'] is True and value['production_clock_qualified'] is False + and value['transport_version'] == 2 and value['raw_v1_controls_qualified'] is False + and value['native_executed'] is False + and value['reused_local_qualification'] == {'path': str(LOCAL), 'sha256': LOCAL_SHA} + and value['native_counts_expected'] == COUNTS and value['test_names_expected'] == TESTS + and value['code_root'] == str(CODE) and value['access_root'] == str(ACCESS), 'exact V2 build profile') + if current: + require(value['source_files'] == sources() and value['core_native_proofs'] == core_proof(), 'current source/Core proof binding') + require(set(value['artifacts']) == set(NAMES), 'six exact artifacts') + for kind, item in value['artifacts'].items(): + output, original = private_file(item['path']), private_file(item['original_link_path']) + require(output == path.parent / NAMES[kind] and original == path.parent / (NAMES[kind] + '.linked') + and sha(output) == item['sha256'] and output.stat().st_size == item['bytes'] + and sha(original) == item['original_link_sha256'] + and item['release_build'] is (kind not in COUNTS) + and item['debug_stripped'] is (kind in COUNTS), 'exact retained delivery and original link') + return value + + +def native(path, ident=None): + path = path.resolve() + manifest = verified(path, current=ident is None) + if ident is None: + ident = str(uuid.uuid4());folder = NATIVE / ident;folder.mkdir(mode=0o700, parents=True) + save(folder / 'intent.json', {'id': ident, 'manifest_path': str(path), 'manifest_sha256': sha(path), + 'runner_sha256': sha(__file__), 'script_sha256': hashlib.sha256(SCRIPT.encode()).hexdigest()}) + before = {'counts': auto.scopes(), 'flags': auto.status()} + require(auto.flags_disabled(before['flags']), 'closed fault flags before native units') + save(folder / 'server-before.json', before) + for kind in COUNTS: + previous.upload(manifest['artifacts'][kind]) + save(folder / 'started-local.json', {'start_once': True}) + action = 'run' + else: + require(str(uuid.UUID(ident)) == ident, 'canonical original native case') + folder = NATIVE / ident;intent = cb.contract.protected_json(folder / 'intent.json') + require(intent['manifest_sha256'] == sha(path) and intent['manifest_path'] == str(path) + and intent['runner_sha256'] == sha(__file__) + and intent['script_sha256'] == hashlib.sha256(SCRIPT.encode()).hexdigest(), 'same original 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, 'test_names': TESTS}, timeout=150) + save(folder / ('readback-' + str(uuid.uuid4()) + '.json'), result) + if result.get('completed') is not True: + print(json.dumps({'verified': False, 'case_id': ident, 'readback_only': True, 'completed': False, + 'case_exists': result.get('case_exists'), 'tests_repeated': False})) + return + require(result['manifest_sha256'] == sha(path) and set(result['reports']) == set(COUNTS) + and result['both_lists_verified_before_any_test'] is True + and result['rmm_jobs'] is False and result['product_state_writes'] is False + and result['local_service_proof'] is False and result['native_pipe_proof'] is False, + 'actual narrow unit result, no runtime proof') + for kind, actual in result['reports'].items(): + run, listed = actual['result'], actual['listed'] + names = re.findall(r'^(.+): test\r?$', listed['stdout'], re.M) + require(actual['sha256'] == manifest['artifacts'][kind]['sha256'] + and names == actual['names'] == TESTS[kind] and len(set(names)) == COUNTS[kind] + and listed['exit_code'] == run['exit_code'] == 0 and not listed['timed_out'] and not run['timed_out'] + and re.search(r'test result: ok\. ' + str(COUNTS[kind]) + + r' passed; 0 failed; 0 ignored; 0 measured; 0 filtered out;', run['stdout']), + 'every exact named native unit passed, none skipped/filtered') + before = cb.contract.protected_json(folder / 'server-before.json') + require(auto.scopes() == before['counts'] and auto.status() == before['flags'], 'both central counts and closed flags unchanged') + if action == 'run': + verified(path) # Full before/after source and retained-artifact check. + proof = {'verified': True, 'case_id': ident, 'manifest_path': str(path), 'manifest_sha256': sha(path), + 'native': result, 'no_jobs': True, 'clock_adapter_qualified': False, + 'local_service_proof': False, 'native_pipe_proof': False} + destination = folder / 'passed.json' + if destination.exists(): + require(cb.contract.protected_json(destination) == proof, 'retain exact original proof') + else: + save(destination, proof) + print(json.dumps({'verified': True, 'case_id': ident, 'tests': COUNTS, 'proof': str(destination)})) + + +def main(): + os.umask(0o077) + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('phase', choices=('build-local', 'native-units', 'readback')) + parser.add_argument('--access-root', required=True, type=Path) + parser.add_argument('--manifest', type=Path) + parser.add_argument('--case') + args = parser.parse_args() + require(args.access_root == ACCESS and not args.access_root.is_symlink(), 'exact existing access root') + if args.phase == 'build-local': + require(args.manifest is None and args.case is None, 'build only') + build() + else: + require(args.manifest is not None 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_PIPE_V2_QUALIFICATION_INCOMPLETE:' + type(error).__name__) + raise SystemExit(2) from None diff --git a/docs/windows-helper-pipe-drain.md b/docs/windows-helper-pipe-drain.md new file mode 100644 index 0000000..d1c2eaa --- /dev/null +++ b/docs/windows-helper-pipe-drain.md @@ -0,0 +1,112 @@ +# Windows Helper: begrenzte Transportbestätigung + +Dieser Schnitt ist im isolierten Zweig `codex/windows-pipe-drain` implementiert. +Eine native Ausführung des neuen Framings und ein Komponentenupgrade sind noch +nicht nachgewiesen. Die bestehenden Nachweise des Agenten `89004ad3…` und Helpers +`e6aacbdd…` bleiben historische Belege ihres ursprünglichen Framings. + +## Anlass und Beleggrenze + +Beim tatsächlichen Agenten wurde `client_read_reply / HELPER_PIPE_IO / 233` +beobachtet; die Helperdiagnose war nicht vorhanden. Der bisherige Dienst rief +auch nach erfolgreichem Schreiben der Antwort sofort `DisconnectNamedPipe` auf. +Microsoft dokumentiert, dass dabei ungelesene Pipebytes verworfen werden. Das +ist ein konkreter möglicher Fehlerpfad, aber kein Nachweis, dass genau dieser +Pfad die beobachtete Störung verursacht hat. +[DisconnectNamedPipe](https://learn.microsoft.com/en-us/windows/win32/api/namedpipeapi/nf-namedpipeapi-disconnectnamedpipe) + +Eine fehlende Diagnose beweist weder einen erfolgreichen Peercheck noch eine +bestimmte Ablehnung: strukturierte `Denied`-Antworten erzeugen keinen privaten +Pipefehlerbeleg, Connect-Leerlauf kann ohne Beleg enden, und das begrenzte +Diagnoseschreiben selbst kann fehlschlagen. Diese Pfade werden hier nicht +umgedeutet oder gelockert. + +## Framing v2 und Reihenfolge + +Agent und Helper verwenden ausschließlich `\\.\pipe\RustDiscovery-System-v2`. +Jeder JSON-Frame beginnt mit acht ASCIIbytes `RDPIPC02`, gefolgt von vier Bytes +Länge in Big Endian und den bisherigen, maximal 1 MiB großen JSONbytes. +V1 und V2 haben unterschiedliche Endpunkte; zusätzlich verweigert der V2-Parser +alte oder fremde Framingtags. Es gibt keinen stillen Fallback. Beide EXEs müssen +als gemeinsam qualifiziertes, gegenseitig gepinntes Paar bereitgestellt werden. +Die JSONschemas von Call/Reply, `helper.system.v1`, Systempolicy-Schema 1 und +RMM-Jobbytes ändern sich nicht. + +Nach dem vollständigen Lesen der Antwort prüft der Agent weiterhin den tatsächlichen +Helperprozess, Schema, Challenge, Enrollmentbindung, Policyhash, Helperbinaryhash +und die Operation/Antwort-Zuordnung. Erst danach schreibt er genau 56 Bytes: +`RDPACK02` (8), ursprüngliche UUID-Challenge in UUID-Bytefolge (16), SHA-256 der +**exakt empfangenen JSON-Antwortbytes** (32). Es findet keine Re-Serialisierung +für diese Prüfsumme statt. Auch eine vollständig geprüfte `Denied`-Antwort wird +so transportseitig bestätigt, ohne sie in einen Erfolg umzuwandeln. + +Der Helper prüft denselben bereits authentifizierten Peer vor dem Schreiben und +nach dem Lesen der Bestätigung. Schreiben, Empfang und Prüfung der Bestätigung +teilen dieselbe bisherige Antwortdeadline von fünf Sekunden; der Client behält +seine bisherige Gesamtdeadline. Erst danach trennt die Dienstschleife die Pipe. +Fehlende, falsche oder verspätete Bestätigung schließt den bestehenden Syncgate +und trennt die Verbindung über den bisherigen Fehlerpfad; statische Diagnosen +unterscheiden `server_drain_receipt` und `client_drain_receipt`. Es gibt keinen +weiteren Aufruf, Retry oder erneute Ausführung. + +Diese Bestätigung bestätigt nur den Empfang der Antwortbytes. Sie ist kein +JobResult-, Journal-, SignedSync-, Grant-, Freigabe- oder Wirkungsbeleg und löscht +keinen dauerhaften Zustand. Ein bereits ausgeführter Auftrag wird durch einen +fehlenden Transport-ACK nicht zurückgenommen oder wiederholt. + +`FlushFileBuffers` wird nicht verwendet: dieser synchrone Pipeaufruf wartet laut +Microsoft bis zum Lesen aller gepufferten Daten und bietet hier keine passende +begrenzte Warteoperation. +[FlushFileBuffers](https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-flushfilebuffers) + +## Prüfung und verbleibender nativer Nachweis + +Die bestehenden IPC-Codecfälle prüfen zusätzlich fremde/alte Frameversionen, +begrenzte Längen sowie manipulierte ACK-Tags, Challenge und Prüfsumme. Eine andere +JSON-Bytefolge mit gleichem JSON-Inhalt darf den ursprünglichen Frame nicht +bestätigen. Die bestehenden Unitfallzahlen bleiben dadurch unverändert. +Lokale Qualifikation wird separat in einem geschützten `devstate/`-Beleg mit +Quellen- und Artefakthashes erfasst. Cross-Clippy und Linken sind kein nativer +Pipebeleg. Vor einem Zielupgrade sind unabhängiger Quellreview, die nativen +Windows-Units des gleichen Manifests und ein eigener erhaltender Paarupgrade mit +explizitem Backup sowie anschließendem tatsächlichen Agent/Helper-Austausch +notwendig. Alle Produktgates bleiben geschlossen. + +Lokale Ausführung: Default- und `system-helper`-Clippy für alle Windows-GNU-Targets +mit `-D warnings`, Release-Build beider EXEs und Linken beider Unitprogramme waren +erfolgreich. 61 gebundene Quelldateien blieben vor/nach dem Lauf gleich. Die +bestehenden 23 Library- und 7 Binary-Unitfälle sind weiterhin die **erwartete** +native Liste; hier wurde kein Windows-Testprogramm ausgeführt. Geschützter +Beleg: `devstate/windows-pipe-drain/qualification-1e0f2b22-1e74-41af-b8ed-0a356be054ba/qualification.json`. + +## Separater Build-/Unitrunner + +`deploy/windows/system_pipe_acceptance.py` erstellt einen neuen Pack unter +`devstate/windows-pipe-drain-build/`. Er prüft den unveränderten lokalen +Qualifikationsbeleg samt ursprünglichen Links und verwendet die bereits +bestandenen Clippy-/Release-/Unitlinkschritte wieder. Nur die fehlenden beiden +Release-Stateprobes werden ergänzt. Alle sechs Auslieferungsdateien erhalten +eine unveränderte `.linked`-Kopie; ausschließlich die Unit-Auslieferungskopien +werden von Debuginformationen befreit. Neue Quellen einschließlich Runner, +Compilerimage, Cargo-/Protoinputs und tatsächlich verwendeter Bootstrapmodule +sind vor/nach dem Build gebunden. + +`--access-root` muss der ursprüngliche Hauptcheckout sein. Nur dort werden die +vorhandenen geschützten Zugangskonfigurationen für den bereits gepinnten +Bootstraptransport und die tatsächlichen Linux-80-/Windows-58-Corebelege gelesen. +Es werden keine Zugangsdaten oder Main-`devstate`-Bäume kopiert oder verlinkt. +Neue Build-, Upload- und Unitfallbelege gehören zum isolierten Pipeworktree. + +Der Native-Unitfall verwendet den neuen Namespace `native-pipe-v2`. **Beide** +vollständigen nativen Listen müssen exakt den erwarteten 23 Library- und 7 +Binary-Namen entsprechen, bevor irgendein Unitfall ausgeführt wird. Intent, +Listen, Start und einzelne Testergebnisse werden create-only gespeichert. +Nach einem Antwortverlust darf ausschließlich derselbe Fall gelesen werden; +auch ein fehlender oder unvollständiger Fall löst keinen erneuten Start aus. +Die Units laufen als Bootstrapadministrator, nicht als Produkt-LocalService, +und beweisen keinen tatsächlichen Pipeaustausch. Zentral gespeicherte +Gerätezähler und geschlossene Fault-/Policyflags werden vor/nach verglichen. + +Der bestehende rohe `helper_peer_controls::raw`-Probe schreibt weiterhin einen +V1-Header. Sein Ablehnungsresultat ist deshalb **kein** V2-Peernegativnachweis; +Runner und Manifest behaupten ausdrücklich keine solche Qualifikation.