diff --git a/crates/agent-core/src/auth.rs b/crates/agent-core/src/auth.rs index ea13868..c321b13 100644 --- a/crates/agent-core/src/auth.rs +++ b/crates/agent-core/src/auth.rs @@ -52,6 +52,40 @@ impl Binding { request: &Request, now: DateTime, verifier: &impl Ed25519Verifier, + ) -> Result<(JobPayload, ExecutionPermitPayload)> { + let (job, p) = self.verify_signed_request(request, verifier)?; + ensure!( + permit_window(&job, &p) && now >= p.not_before && now < p.start_before, + "PERMIT_TIME" + ); + Ok((job, p)) + } + /// Evidence-only verification. This never supplies a current start window + /// and cannot be substituted for verify_with at an execution boundary. + pub fn verify_historical_for_denial_with( + &self, + request: &Request, + identity: HelperIdentity, + verifier: &impl Ed25519Verifier, + ) -> Result { + let (job, permit) = self.verify_signed_request(request, verifier)?; + ensure!(permit_window(&job, &permit), "PERMIT_TIME"); + ensure!( + job.execution_identity == identity.as_str() + && matches!(&job.action, rmm_domain::jobs::Action::SetServiceState { state, .. } + if matches!(state.as_str(), "running" | "stopped")), + "DENIAL_HELPER_SCOPE" + ); + Ok(VerifiedHistoricalDenial { + request: request.clone(), + permit, + request_sha256: hash(&serde_json::to_vec(request)?), + }) + } + fn verify_signed_request( + &self, + request: &Request, + verifier: &impl Ed25519Verifier, ) -> Result<(JobPayload, ExecutionPermitPayload)> { let job = self.job(&request.job_bytes)?; ensure!( @@ -91,19 +125,25 @@ impl Binding { && p.max_runtime_seconds == job.max_runtime_seconds, "PERMIT_BINDING" ); - ensure!( - p.not_before >= job.not_before - && p.start_before <= job.start_before - && p.not_before < p.start_before - && (p.start_before - p.not_before).num_seconds() <= 60 - && now >= p.not_before - && now < p.start_before, - "PERMIT_TIME" - ); Ok((job, p)) } } +fn permit_window(job: &JobPayload, p: &ExecutionPermitPayload) -> bool { + p.not_before >= job.not_before + && p.start_before <= job.start_before + && p.not_before < p.start_before + && (p.start_before - p.not_before).num_seconds() <= 60 +} + +/// Not serializable or constructible by an IPC caller. Only the deny-only +/// helper path consumes this evidence; it is never an execution authorization. +pub struct VerifiedHistoricalDenial { + pub(crate) request: Request, + pub(crate) permit: ExecutionPermitPayload, + pub(crate) request_sha256: String, +} + #[derive(Clone, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct Request { diff --git a/crates/agent-core/src/helper_reconcile.rs b/crates/agent-core/src/helper_reconcile.rs index 0f57730..43313d1 100644 --- a/crates/agent-core/src/helper_reconcile.rs +++ b/crates/agent-core/src/helper_reconcile.rs @@ -2,7 +2,7 @@ //! an operation or grants authority from an agent-provided ready/unlock flag. //! Both general and APT listeners must use the SAME executor.lock and SQLite DB. use crate::auth::HelperIdentity; -use crate::auth::{Binding, Request}; +use crate::auth::{Binding, Outcome, Request}; use crate::crypto::{DefaultVerifier, Ed25519Verifier}; use anyhow::{Context, Result, ensure}; use chrono::{DateTime, Utc}; @@ -27,6 +27,72 @@ pub trait BootSource { pub trait ExecutorLedgerGuard { fn check(&self, db: &Connection) -> Result<()>; } +/// OS adapter contract, not an IPC assertion. The implementation must pin the +/// live helper PID/creation and ledger/lock file identities, and independently +/// read the fixed protected intent store's immutable known_no_dispatch CAS. +/// The same executor guard stays held throughout this check and the commit. +pub trait NoStartGuard: ExecutorLedgerGuard { + fn live_instance_sha256(&self) -> Result; + fn verify_no_dispatch( + &self, + original: &Request, + anchor: &NoStartAnchor, + ) -> Result; +} +/// Trusted adapter return value only: deliberately has no IPC deserializer. +#[derive(Debug, PartialEq, Eq)] +pub struct NoDispatchObservation { + pub intent_id: Uuid, + pub intent_sha256: String, + pub intent_recorded_at: DateTime, + pub original_boot_id: Uuid, +} +/// Historical ready context, never a new lease or execution authorization. +/// Deserialization alone conveys no authority: every field is checked against +/// the still-held helper instance and the original independently stored history. +#[derive(Clone, Debug, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct NoStartAnchor { + journal_id: Uuid, + checkpoint: u64, + decision_id: Uuid, + connection_generation: u64, + boot_id: Uuid, + binding_sha256: String, + execution_identity: String, + helper_instance_sha256: String, + helper_born_at: DateTime, + sync_request_sha256: String, + signed_response_sha256: String, + ledger_fingerprint: String, + captured_at: DateTime, +} +impl NoStartAnchor { + pub fn journal_id(&self) -> Uuid { + self.journal_id + } + pub fn checkpoint(&self) -> u64 { + self.checkpoint + } + pub fn decision_id(&self) -> Uuid { + self.decision_id + } + pub fn connection_generation(&self) -> u64 { + self.connection_generation + } + pub fn boot_id(&self) -> Uuid { + self.boot_id + } + pub fn helper_instance_sha256(&self) -> &str { + &self.helper_instance_sha256 + } + pub fn sync_request_sha256(&self) -> &str { + &self.sync_request_sha256 + } + pub fn signed_response_sha256(&self) -> &str { + &self.signed_response_sha256 + } +} /// Same two fields as public SyncResponse, without adding a new dependency or /// wire type. RPC glue copies the ORIGINAL bytes/signature, never reserializes. #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] @@ -122,6 +188,183 @@ impl HelperSync { pub fn active_generation(&self) -> Option { self.lease.as_ref().map(|lease| lease.generation) } + /// Capture only an actually confirmed live ready lease, before an intent + /// or fence closes it. Retaining this anchor cannot keep that lease alive. + pub fn no_start_anchor( + &self, + db: &Connection, + guard: &impl NoStartGuard, + generation: u64, + clock: &impl Clock, + boot: &impl BootSource, + ) -> Result { + ensure!( + self.can_start(db, guard, generation, clock, boot), + "DENIAL_READY_ANCHOR_REQUIRED" + ); + let lease = self + .lease + .as_ref() + .context("DENIAL_READY_ANCHOR_REQUIRED")?; + let meta = metadata(db)?; + let (response, request): (Vec, Vec) = db.query_row( + "SELECT response,request FROM helper_sync_decisions WHERE decision_id=?1", + [lease.decision_id.to_string()], + |r| Ok((r.get(0)?, r.get(1)?)), + )?; + let instance = guard.live_instance_sha256()?; + ensure!(digest(&instance), "DENIAL_HELPER_INSTANCE_INVALID"); + let capture: Vec = db.query_row( + "SELECT snapshot FROM helper_sync_captures WHERE checkpoint=?1", + [meta.checkpoint], + |r| r.get(0), + )?; + let capture: Snapshot = serde_json::from_slice(&capture)?; + let anchor = NoStartAnchor { + journal_id: meta.journal_id, + checkpoint: meta.checkpoint, + decision_id: lease.decision_id, + connection_generation: generation, + boot_id: lease.boot_id, + binding_sha256: hash(&serde_json::to_vec(&self.binding)?), + execution_identity: self.identity.as_str().into(), + helper_instance_sha256: instance, + helper_born_at: self.born, + sync_request_sha256: hash(&request), + signed_response_sha256: hash(&response), + ledger_fingerprint: lease.fingerprint.clone(), + captured_at: capture.captured_at, + }; + self.verify_no_start_anchor(db, guard, &anchor, clock, boot)?; + Ok(anchor) + } + /// Deny-only: the grant may be expired, but no historical timestamp is ever + /// passed to an execute verifier. Existing consumption is immutable. A new + /// c/start/no-effect outcome is committed atomically and remains locked for + /// the ordinary independently signed Sync decision. + pub fn abandon_no_start( + &mut self, + db: &mut Connection, + guard: &impl NoStartGuard, + anchor: &NoStartAnchor, + request: &Request, + clock: &impl Clock, + boot: &impl BootSource, + ) -> Result { + self.close(); + guard.check(db)?; + let original = self.binding.verify_historical_for_denial_with( + request, + self.identity, + &self.verifier, + )?; + let tx = db.transaction_with_behavior(TransactionBehavior::Immediate)?; + if let Some(outcome) = crate::ledger::existing_no_start(&tx, &original)? { + // No changed row and no classification of a missing original start. + tx.commit()?; + return Ok(outcome); + } + self.verify_no_start_anchor(&tx, guard, anchor, clock, boot)?; + let observation = guard.verify_no_dispatch(request, anchor)?; + let now = self.verify_no_start_anchor(&tx, guard, anchor, clock, boot)?; + ensure!( + !observation.intent_id.is_nil() + && digest(&observation.intent_sha256) + && observation.original_boot_id == anchor.boot_id + && observation.intent_recorded_at >= anchor.captured_at + && observation.intent_recorded_at <= now, + "DENIAL_INTENT_BINDING" + ); + let outcome = + crate::ledger::record_no_start_in(&tx, &original, anchor.boot_id, now, &observation)?; + guard.check(&tx)?; + ensure!( + guard.live_instance_sha256()? == anchor.helper_instance_sha256, + "DENIAL_HELPER_INSTANCE_CHANGED" + ); + ensure!( + guard.verify_no_dispatch(request, anchor)? == observation, + "DENIAL_INTENT_CHANGED_DURING_COMMIT" + ); + tx.commit()?; + Ok(outcome) + } + fn verify_no_start_anchor( + &self, + db: &Connection, + guard: &impl NoStartGuard, + anchor: &NoStartAnchor, + clock: &impl Clock, + boot: &impl BootSource, + ) -> Result> { + guard.check(db)?; + let now = clock.now()?; + ensure!( + anchor.helper_born_at == self.born + && now >= self.born + && boot.current_boot()? == anchor.boot_id + && !anchor.boot_id.is_nil() + && anchor.execution_identity == self.identity.as_str() + && anchor.binding_sha256 == hash(&serde_json::to_vec(&self.binding)?) + && digest(&anchor.helper_instance_sha256) + && guard.live_instance_sha256()? == anchor.helper_instance_sha256, + "DENIAL_HELPER_CONTEXT_CHANGED" + ); + let meta = metadata(db)?; + ensure!( + meta.fault.is_none() + && meta.journal_id == anchor.journal_id + && meta.checkpoint == anchor.checkpoint + && meta.decision == Some(anchor.decision_id) + && meta.generation == anchor.connection_generation + && ledger_fingerprint(db)? == anchor.ledger_fingerprint, + "DENIAL_HELPER_LINEAGE_CHANGED" + ); + // Exact equality is deliberate: a first implementation refuses even + // legitimate intervening progress rather than inventing lineage links. + verify_checkpoint_repair_history_with( + db, + &self.binding, + None, + self.identity, + &self.verifier, + )?; + let (response, raw): (Vec, Vec) = db.query_row( + "SELECT response,request FROM helper_sync_decisions WHERE decision_id=?1", + [anchor.decision_id.to_string()], + |r| Ok((r.get(0)?, r.get(1)?)), + )?; + ensure!( + hash(&response) == anchor.signed_response_sha256 + && hash(&raw) == anchor.sync_request_sha256, + "DENIAL_READY_BYTES_CHANGED" + ); + let signed: SignedResponse = serde_json::from_slice(&response)?; + let parsed: ReconcileDecisionPayload = serde_json::from_slice(&signed.decision_bytes)?; + // Historical Sync evidence only, never a lease. Its original signature + // and immutable stored request remain mandatory after expiry. + let decision = verify(&self.verifier, &self.binding, &signed, parsed.issued_at)?; + let capture: Vec = db.query_row( + "SELECT snapshot FROM helper_sync_captures WHERE checkpoint=?1", + [anchor.checkpoint], + |r| r.get(0), + )?; + let capture: Snapshot = serde_json::from_slice(&capture)?; + ensure!( + decision.ready + && decision.helper_ready + && decision.decision_id == anchor.decision_id + && decision.boot_id == anchor.boot_id + && decision.helper_journal_id == Some(anchor.journal_id) + && decision.helper_checkpoint == anchor.checkpoint + && decision.connection_generation == anchor.connection_generation + && capture.captured_at == anchor.captured_at + && capture.boot_id == anchor.boot_id + && capture.captured_at <= now, + "DENIAL_CONFIRMED_READY_REQUIRED" + ); + Ok(now) + } /// Every call creates a NEW durable checkpoint challenge and closes the old /// lease. Use apply's returned snapshot for its ACK, not another snapshot(). pub fn snapshot( @@ -494,6 +737,12 @@ impl HelperSync { } } +fn digest(value: &str) -> bool { + value.len() == 64 + && value + .bytes() + .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)) +} fn pinned_key(binding: &Binding, verifier: &impl Ed25519Verifier) -> Result> { let raw = hex::decode(&binding.public_key_hex)?; ensure!(raw.len() == 32, "PINNED_HELPER_KEY_SIZE"); diff --git a/crates/agent-core/src/ledger.rs b/crates/agent-core/src/ledger.rs index 8d05cfa..50d93b7 100644 --- a/crates/agent-core/src/ledger.rs +++ b/crates/agent-core/src/ledger.rs @@ -1,5 +1,5 @@ //! Connection-level ledger rules, with OS path/lock/ACL handling left to adapters. -use crate::auth::{Outcome, Request}; +use crate::auth::{Outcome, Request, VerifiedHistoricalDenial}; use anyhow::{Context, Result, ensure}; use chrono::{DateTime, Utc}; use rmm_domain::jobs::{ExecutionPermitPayload, JobPayload}; @@ -77,6 +77,111 @@ pub fn begin_execution( tx.commit()?; Ok(()) } + +/// Retained evidence only. In particular, a legacy consumption without its +/// original start can never be repaired into a proven nonstart by this path. +pub(crate) fn existing_no_start( + db: &rusqlite::Connection, + original: &VerifiedHistoricalDenial, +) -> Result> { + let p = &original.permit; + type Row = ( + String, + String, + u64, + String, + String, + Option>, + Option>, + Option>, + ); + let mut statement = db.prepare("SELECT c.grant_id,c.agent,c.generation,c.job,c.attempt,c.result,e.start,e.outcome FROM consumed c LEFT JOIN execution_evidence e USING(grant_id) WHERE c.grant_id=?1 OR (c.agent=?2 AND c.generation=?3 AND c.job=?4 AND c.attempt=?5)")?; + let rows = statement + .query_map( + params![ + p.grant_id.to_string(), + p.agent_id.to_string(), + p.enrollment_generation, + p.job_id.to_string(), + p.attempt_id.to_string() + ], + |r| { + Ok(( + r.get(0)?, + r.get(1)?, + r.get(2)?, + r.get(3)?, + r.get(4)?, + r.get(5)?, + r.get(6)?, + r.get(7)?, + )) + }, + )? + .collect::>>()?; + ensure!(rows.len() <= 1, "DENIAL_EXISTING_ATTEMPT_CONFLICT"); + let Some((grant, agent, generation, job, attempt, finished, start, outcome)) = + rows.into_iter().next() + else { + return Ok(None); + }; + ensure!( + grant == p.grant_id.to_string() + && agent == p.agent_id.to_string() + && generation == p.enrollment_generation + && job == p.job_id.to_string() + && attempt == p.attempt_id.to_string(), + "DENIAL_EXISTING_ATTEMPT_CONFLICT" + ); + let Some(start) = start else { + return Ok(Some(Outcome::unknown("HELPER_PREVIOUS_START_UNRESOLVED"))); + }; + let start: serde_json::Value = serde_json::from_slice(&start)?; + let saved: Request = serde_json::from_value(start["request"].clone())?; + ensure!( + serde_json::to_vec(&saved)? == serde_json::to_vec(&original.request)?, + "DENIAL_EXISTING_REQUEST_CONFLICT" + ); + let boot: Uuid = serde_json::from_value(start["previous_boot_id"].clone())?; + ensure!(!boot.is_nil(), "DENIAL_EXISTING_ORIGINAL_BOOT_MISSING"); + let _: DateTime = serde_json::from_value(start["reserved_at"].clone())?; + if let (Some(a), Some(b)) = (&finished, &outcome) { + ensure!( + canonical(a)? == canonical(b)?, + "DENIAL_EXISTING_OUTCOME_CONFLICT" + ); + } + let Some(bytes) = outcome.or(finished) else { + return Ok(Some(Outcome::unknown("HELPER_PREVIOUS_EFFECT_UNRESOLVED"))); + }; + Ok(Some(serde_json::from_slice(&canonical(&bytes)?)?)) +} + +/// Called only inside HelperSync's guarded Immediate transaction after its +/// independent intent/lineage checks. Never finish/unlock here. All three +/// witnesses commit together, so no crash can expose a bare new consumption. +pub(crate) fn record_no_start_in( + db: &rusqlite::Connection, + original: &VerifiedHistoricalDenial, + boot: Uuid, + observed_at: DateTime, + witness: &crate::helper_reconcile::NoDispatchObservation, +) -> Result { + let p = &original.permit; + validate_start_request(p, &original.request)?; + consume_in(db, p)?; + save_start(db, p, &original.request, boot, observed_at)?; + let outcome = Outcome { + state: "failed".into(), + evidence: serde_json::json!({"not_started":true,"postconditions_passed":false, + "deny_only":true,"reservation_only":true,"original_request_sha256":original.request_sha256, + "intent_id":witness.intent_id,"intent_sha256":witness.intent_sha256, + "intent_recorded_at":witness.intent_recorded_at,"observed_at":observed_at}), + ..Outcome::unknown("HELPER_ABANDONED_BEFORE_DISPATCH") + }; + persist_outcome_in(db, p, &outcome)?; + Ok(outcome) +} fn validate_start_request(p: &ExecutionPermitPayload, request: &Request) -> Result<()> { ensure!( request.job_bytes.len() <= 65536 diff --git a/crates/agent-core/tests/helper_reconcile.rs b/crates/agent-core/tests/helper_reconcile.rs index b4e16fd..dcd0943 100644 --- a/crates/agent-core/tests/helper_reconcile.rs +++ b/crates/agent-core/tests/helper_reconcile.rs @@ -47,6 +47,412 @@ impl BootSource for TestBoot { Ok(self.0) } } + +/// Test-only protected-store/peer adapter. These unit checks do not qualify +/// Windows ACLs, a live SCM peer, file identities or the real intent CAS. +struct TestNoStartGuard { + ledger: ExecutorGuard, + instance: String, + request_sha256: String, + anchor_sha256: String, + intent_recorded_at: DateTime, + original_boot_id: Uuid, + dispatch_possible: bool, +} +impl ExecutorLedgerGuard for TestNoStartGuard { + fn check(&self, db: &Connection) -> anyhow::Result<()> { + self.ledger.check(db) + } +} +impl NoStartGuard for TestNoStartGuard { + fn live_instance_sha256(&self) -> anyhow::Result { + Ok(self.instance.clone()) + } + fn verify_no_dispatch( + &self, + original: &Request, + anchor: &NoStartAnchor, + ) -> anyhow::Result { + anyhow::ensure!( + !self.dispatch_possible + && self.request_sha256 == hash(&serde_json::to_vec(original)?) + && self.anchor_sha256 == hash(&serde_json::to_vec(anchor)?), + "TEST_INTENT_NOT_PROVEN" + ); + Ok(NoDispatchObservation { + intent_id: Uuid::from_u128(900), + intent_sha256: hash(b"immutable test-only intent bytes"), + intent_recorded_at: self.intent_recorded_at, + original_boot_id: self.original_boot_id, + }) + } +} + +fn denial_case() -> ( + Fixture, + HelperSync, + NoStartAnchor, + Request, + TestNoStartGuard, +) { + let mut f = Fixture::new(); + f.identity = HelperIdentity::System; + let mut sync = f.sync(); + let capture = f.snapshot(&mut sync, 7); + let raw = f.request(&capture); + let ready = f.decision(&capture, &raw, 7, true); + assert!(f.apply(&mut sync, &ready, &raw).unwrap().ready); + let job = JobPayload { + schema_version: 1, + tenant_id: f.binding.tenant_id, + asset_id: f.binding.asset_id, + agent_id: f.binding.agent_id, + enrollment_generation: 1, + assignment_version: 3, + job_id: Uuid::new_v4(), + attempt_id: Uuid::new_v4(), + action: Action::SetServiceState { + unit: "RustDiscoveryMvpFixture".into(), + state: "running".into(), + }, + execution_identity: "system".into(), + not_before: f.clock.now - Duration::seconds(10), + start_before: f.clock.now + Duration::minutes(5), + max_runtime_seconds: 30, + max_output_bytes: 4096, + }; + let permit = ExecutionPermitPayload { + domain: "rmm.execution-permit".into(), + schema_version: 1, + grant_id: Uuid::new_v4(), + authorization_id: Uuid::new_v4(), + tenant_id: job.tenant_id, + asset_id: job.asset_id, + agent_id: job.agent_id, + enrollment_generation: 1, + assignment_version: 3, + job_id: job.job_id, + attempt_id: job.attempt_id, + plan_hash: hash(b"approved exact service plan"), + job_payload_hash: hash(&encode(&job).unwrap()), + action: job.action.name().into(), + execution_identity: job.execution_identity.clone(), + max_runtime_seconds: 30, + not_before: f.clock.now - Duration::seconds(1), + start_before: f.clock.now + Duration::seconds(59), + key_id: hash(&f.key.verifying_key().to_bytes()), + }; + let bytes = encode(&permit).unwrap(); + let request = Request { + job_bytes: encode(&job).unwrap(), + signature: f.key.sign(&bytes).to_bytes().to_vec(), + permit_bytes: bytes, + plan_hash: permit.plan_hash, + authorization_id: permit.authorization_id, + }; + let mut guard = TestNoStartGuard { + ledger: ExecutorGuard { + directory: f.dir.clone(), + }, + instance: hash(b"same live helper PID/creation/ledger/lock fixture"), + request_sha256: hash(&serde_json::to_vec(&request).unwrap()), + anchor_sha256: String::new(), + intent_recorded_at: f.clock.now, + original_boot_id: f.boot.0, + dispatch_possible: false, + }; + let anchor = sync + .no_start_anchor(&f.db, &guard, 7, &f.clock, &f.boot) + .unwrap(); + guard.anchor_sha256 = hash(&serde_json::to_vec(&anchor).unwrap()); + (f, sync, anchor, request, guard) +} +fn denial_ledger_bytes(db: &Connection) -> Vec { + let mut statement=db.prepare("SELECT c.grant_id,c.agent,c.generation,c.job,c.attempt,c.result,e.start,e.outcome FROM consumed c LEFT JOIN execution_evidence e USING(grant_id) ORDER BY c.grant_id").unwrap(); + type Row = ( + String, + String, + u64, + String, + String, + Option>, + Option>, + Option>, + ); + let rows = statement + .query_map([], |r| { + Ok(( + r.get(0)?, + r.get(1)?, + r.get(2)?, + r.get(3)?, + r.get(4)?, + r.get(5)?, + r.get(6)?, + r.get(7)?, + )) + }) + .unwrap() + .collect::>>() + .unwrap(); + serde_json::to_vec(&rows).unwrap() +} + +#[test] +fn abandon_after_expiry_records_deny_only_evidence_and_requires_normal_signed_unlock() { + let (mut f, mut sync, anchor, request, mut guard) = denial_case(); + let permit: ExecutionPermitPayload = serde_json::from_slice(&request.permit_bytes).unwrap(); + f.clock.now += Duration::seconds(120); + assert!(f.binding.verify(&request, f.clock.now).is_err()); + let outcome = sync + .abandon_no_start(&mut f.db, &guard, &anchor, &request, &f.clock, &f.boot) + .unwrap(); + assert_eq!(outcome.state, "failed"); + assert_eq!(outcome.error_code, "HELPER_ABANDONED_BEFORE_DISPATCH"); + assert_eq!(outcome.evidence["not_started"], true); + assert_eq!(outcome.evidence["postconditions_passed"], false); + assert_eq!( + outcome.evidence["observed_at"], + serde_json::to_value(f.clock.now).unwrap() + ); + assert!( + outcome.stdout.is_empty() + && outcome.stderr.is_empty() + && outcome.exit_code.is_none() + && !outcome.reboot_required + ); + assert_eq!( + f.db.query_row::( + "SELECT count(*) FROM consumed WHERE result IS NULL", + [], + |r| r.get(0) + ) + .unwrap(), + 1 + ); + let before = denial_ledger_bytes(&f.db); + guard.dispatch_possible = true; // An existing outcome is returned, never reclassified. + let repeated = sync + .abandon_no_start(&mut f.db, &guard, &anchor, &request, &f.clock, &f.boot) + .unwrap(); + assert_eq!( + serde_json::to_vec(&outcome).unwrap(), + serde_json::to_vec(&repeated).unwrap() + ); + assert_eq!(denial_ledger_bytes(&f.db), before); + assert!( + rmm_agent_core::ledger::begin_execution( + &mut f.db, + &permit, + &request, + f.boot.0, + f.clock.now + ) + .is_err() + ); + assert!(!sync.can_start(&f.db, &f.guard, 7, &f.clock, &f.boot)); + let capture = f.snapshot(&mut sync, 7); + assert_eq!( + capture.attempts[0].original_request_sha256, + hash(&serde_json::to_vec(&request).unwrap()) + ); + let raw = f.request(&capture); + let decision = f.decision(&capture, &raw, 7, true); + assert!(f.apply(&mut sync, &decision, &raw).unwrap().ready); + assert_eq!( + f.db.query_row::( + "SELECT count(*) FROM consumed WHERE result IS NOT NULL", + [], + |r| r.get(0) + ) + .unwrap(), + 1 + ); + assert!( + rmm_agent_core::ledger::begin_execution( + &mut f.db, + &permit, + &request, + f.boot.0, + f.clock.now + ) + .is_err() + ); +} + +#[test] +fn abandon_storage_failure_rolls_back_consumption_start_and_outcome_together() { + for trigger in [ + "CREATE TRIGGER refuse_start BEFORE INSERT ON execution_evidence BEGIN SELECT RAISE(ABORT,'fixture failure'); END;", + "CREATE TRIGGER refuse_outcome BEFORE UPDATE OF outcome ON execution_evidence BEGIN SELECT RAISE(ABORT,'fixture failure'); END;", + ] { + let (mut f, mut sync, anchor, request, guard) = denial_case(); + f.db.execute_batch(trigger).unwrap(); + assert!( + sync.abandon_no_start(&mut f.db, &guard, &anchor, &request, &f.clock, &f.boot) + .is_err() + ); + for table in ["consumed", "execution_evidence"] { + assert_eq!( + f.db.query_row::(&format!("SELECT count(*) FROM {table}"), [], |r| r + .get(0)) + .unwrap(), + 0 + ); + } + f.db.execute_batch( + "DROP TRIGGER IF EXISTS refuse_start;DROP TRIGGER IF EXISTS refuse_outcome", + ) + .unwrap(); + assert!( + sync.abandon_no_start(&mut f.db, &guard, &anchor, &request, &f.clock, &f.boot) + .is_ok() + ); + } +} + +#[test] +fn abandon_preserves_existing_known_unknown_missing_start_and_foreign_request_rows() { + use rmm_agent_core::{auth::Outcome, ledger}; + for variant in [ + "known", + "unknown", + "missing_start", + "missing_outcome", + "foreign_grant", + "changed_request", + ] { + let (mut f, mut sync, anchor, request, guard) = denial_case(); + let mut stored = request.clone(); + let mut permit: ExecutionPermitPayload = + serde_json::from_slice(&request.permit_bytes).unwrap(); + if variant == "foreign_grant" { + permit.grant_id = Uuid::new_v4(); + } + if variant == "changed_request" { + permit.plan_hash = hash(b"other signed plan"); + stored.plan_hash = permit.plan_hash.clone(); + } + stored.permit_bytes = encode(&permit).unwrap(); + stored.signature = f.key.sign(&stored.permit_bytes).to_bytes().to_vec(); + if variant == "missing_start" { + ledger::consume(&mut f.db, &permit).unwrap(); + } else { + ledger::begin_execution(&mut f.db, &permit, &stored, f.boot.0, f.clock.now).unwrap(); + } + if variant == "known" || variant == "unknown" { + let mut outcome = Outcome::unknown("ORIGINAL_OUTCOME"); + if variant == "known" { + outcome.state = "failed".into(); + } + ledger::persist_outcome(&f.db, &permit, &outcome).unwrap(); + if variant == "known" { + ledger::finish(&f.db, &permit, &outcome).unwrap(); + } + } + let before = denial_ledger_bytes(&f.db); + let response = + sync.abandon_no_start(&mut f.db, &guard, &anchor, &request, &f.clock, &f.boot); + if matches!(variant, "foreign_grant" | "changed_request") { + assert!(response.is_err(), "{variant}"); + } else { + let outcome = response.unwrap(); + assert_eq!( + outcome.state, + if variant == "known" { + "failed" + } else { + "unknown" + }, + "{variant}" + ); + assert_ne!(outcome.error_code, "HELPER_ABANDONED_BEFORE_DISPATCH"); + } + assert_eq!(denial_ledger_bytes(&f.db), before, "{variant}"); + } +} + +#[test] +fn abandon_rejects_wrong_signature_scope_clock_boot_lineage_instance_and_dispatch_proof() { + for variant in [ + "signature", + "tenant", + "root_identity", + "script", + "restarted", + "clock", + "boot", + "instance", + "history", + "checkpoint", + "dispatch", + "future_intent", + ] { + let (mut f, mut sync, anchor, mut request, mut guard) = denial_case(); + match variant { + "signature" => request.signature[0] ^= 1, + "tenant" | "root_identity" | "script" | "restarted" => { + let mut job: JobPayload = serde_json::from_slice(&request.job_bytes).unwrap(); + let mut p: ExecutionPermitPayload = + serde_json::from_slice(&request.permit_bytes).unwrap(); + if variant == "tenant" { + job.tenant_id = Uuid::new_v4(); + p.tenant_id = job.tenant_id; + } + if variant == "root_identity" { + job.execution_identity = "root".into(); + p.execution_identity = "root".into(); + } + if variant == "script" { + job.action = script(); + p.action = job.action.name().into(); + } + if variant == "restarted" { + job.action = Action::SetServiceState { + unit: "RustDiscoveryMvpFixture".into(), + state: "restarted".into(), + }; + } + request.job_bytes = encode(&job).unwrap(); + p.job_payload_hash = hash(&request.job_bytes); + request.permit_bytes = encode(&p).unwrap(); + request.signature = f.key.sign(&request.permit_bytes).to_bytes().to_vec(); + } + "clock" => f.clock.trusted = false, + "boot" => f.boot.0 = Uuid::new_v4(), + "instance" => guard.instance = hash(b"different helper PID or ledger identity"), + "history" => { + f.db.execute("DELETE FROM helper_sync_decisions", []) + .unwrap(); + } + "checkpoint" => { + f.db.execute("UPDATE helper_sync_meta SET checkpoint=checkpoint+1", []) + .unwrap(); + } + "dispatch" => guard.dispatch_possible = true, + "future_intent" => guard.intent_recorded_at = f.clock.now + Duration::seconds(1), + _ => unreachable!(), + } + assert!( + sync.abandon_no_start(&mut f.db, &guard, &anchor, &request, &f.clock, &f.boot) + .is_err(), + "{variant}" + ); + assert_eq!( + f.db.query_row::("SELECT count(*) FROM consumed", [], |r| r.get(0)) + .unwrap(), + 0, + "{variant}" + ); + assert_eq!( + f.db.query_row::("SELECT count(*) FROM execution_evidence", [], |r| r + .get(0)) + .unwrap(), + 0, + "{variant}" + ); + } +} struct Fixture { dir: PathBuf, db: Connection, diff --git a/deploy/core_no_start_acceptance.py b/deploy/core_no_start_acceptance.py new file mode 100644 index 0000000..553e8e0 --- /dev/null +++ b/deploy/core_no_start_acceptance.py @@ -0,0 +1,333 @@ +#!/usr/bin/env python3 +"""Fixed deny-only Core units: local build, Linux VM FIRST, then Windows VM. + +Separate from historical SharedCore profiles. No RMM jobs, daemon replacement, +enrollment, server access or product-state writes. Lost replies allow readback +only. Windows lists are expected until the real Windows process emits them. +""" +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 remote_client_bootstrap as linux +import vm_mvp_acceptance as contract +from windows import client_bootstrap as windows + +ROOT = Path(__file__).resolve().parents[1] +STATE = ROOT / 'devstate/core-no-start' +IMAGE = 'rust-discovery-durable-final-build:local' +REMOTE = '/var/lib/rmm-core-no-start-units' +COUNTS = {'linux': {'contracts': 7, 'helper-reconcile': 26, 'reconcile': 26, 'linux-helper': 21}, + 'windows': {'contracts': 6, 'helper-reconcile': 26, 'reconcile': 26}} +CORE = {'auth.rs': '348db410002e158cd1ec1af0200305ad52f9da5d5092b418bf2e28b83b477b52', + 'ledger.rs': '119a341b8d4050fe99b973be0e7b5866c5d78b32fefc1cd5a741872ff34f022b', + 'helper_reconcile.rs': '29ee5ba342b502e792894d2aca0662a55f4fecf35831ac2212adf7880cacbc1f'} +require = contract.require + + +def sha(path): + return hashlib.sha256(Path(path).read_bytes()).hexdigest() + + +def save(path, value): + path.parent.mkdir(parents=True, mode=0o700, exist_ok=True) + contract.write_private(path, value) + + +def source_files(): + paths = [ROOT / p for p in ('Cargo.toml', 'Cargo.lock', 'rust-toolchain.toml', + 'deploy/core_no_start_acceptance.py', 'deploy/remote_client_bootstrap.py', + 'deploy/vm_access_check.py', 'deploy/vm_mvp_acceptance.py', 'deploy/windows/client_bootstrap.py', + 'tools/apt-transaction/apt_transaction.py', 'tools/apt-transaction/hook', + 'deploy/linux/rmm-apt-helper.service', 'tests/apt_transaction/fixtures/planning-canonical.json')] + paths += list((ROOT / 'crates').glob('*/Cargo.toml')) + for crate in ('agent-core', 'agent-linux', 'privileged-helper', 'domain', 'protocol'): + paths += list((ROOT / 'crates' / crate).rglob('*.rs')) + paths += list((ROOT / 'proto').glob('*.proto')) + result = {str(p.relative_to(ROOT)): sha(p) for p in sorted(set(paths))} + require(all(result['crates/agent-core/src/' + p] == h for p, h in CORE.items()), 'reviewed frozen Core required') + return result + + +def command(args, timeout=300): + done = subprocess.run(args, capture_output=True, timeout=timeout) + require(done.returncode == 0, 'local command failed: ' + done.stderr.decode(errors='replace')[-2500:]) + return done.stdout + + +def names(raw): + return sorted(re.findall(r'^(.+): test\r?$', raw, re.M)) + + +def build(): + os.umask(0o077) + folder = STATE / ('build-' + str(uuid.uuid4())) + folder.mkdir(parents=True, mode=0o700) + before = source_files() + save(folder / 'source-before.json', before) + image = command(['docker', 'image', 'inspect', '--format', '{{.Id}}', IMAGE]).decode().strip() + require(re.fullmatch(r'sha256:[a-f0-9]{64}', image), 'immutable local image') + target = STATE / 'target' + target.mkdir(mode=0o700, exist_ok=True) + common = ['docker', 'run', '--rm', '--network', 'none', '--read-only', '--tmpfs', '/tmp:rw,size=512m', + '-v', str(ROOT) + ':/src:ro', '-v', str(target) + ':/target', '-e', 'CARGO_TARGET_DIR=/target', '--entrypoint', 'cargo', image] + artifacts = {'linux': {}, 'windows': {}} + for platform in COUNTS: + options = ['--manifest-path', '/src/Cargo.toml', '--locked', '--offline'] + packages = ['-p', 'rmm-agent-core'] + if platform == 'windows': + options += ['--target', 'x86_64-pc-windows-gnu'] + else: + packages += ['-p', 'rmm-privileged-helper'] + command(common + ['clippy'] + options + packages + ['--all-targets', '--', '-D', 'warnings']) + core_features = ['--features', 'openssl-verifier'] if platform == 'linux' else [] + raw = command(common + ['test'] + options + ['-p', 'rmm-agent-core'] + core_features + ['--tests', '--release', '--no-run', '--message-format=json']) + if platform == 'linux': + raw += command(common + ['test'] + options + ['-p', 'rmm-privileged-helper', '--test', 'reconcile', '--release', '--no-run', '--message-format=json']) + for line in raw.splitlines(): + try: + value = json.loads(line) + except ValueError: + continue + if value.get('reason') != 'compiler-artifact' or not value.get('executable') or value.get('target', {}).get('kind') != ['test']: + continue + name = value['target']['name'] + if 'rmm-privileged-helper' in value['package_id']: + kind = 'linux-helper' + else: + kind = {'contracts': 'contracts', 'helper_reconcile': 'helper-reconcile', 'reconcile': 'reconcile'}.get(name) + require(kind in COUNTS[platform] and kind not in artifacts[platform], 'exact distinct test artifact') + original = target / Path(value['executable']).relative_to('/target') + destination = folder / platform + destination.mkdir(mode=0o700, exist_ok=True) + linked = destination / (kind + '.linked') + copy = destination / (kind + ('.exe' if platform == 'windows' else '')) + for path in (linked, copy): + shutil.copyfile(original, path) + path.chmod(0o600) + linked_hash = sha(linked) + strip = 'x86_64-w64-mingw32-strip' if platform == 'windows' else 'strip' + command(['docker', 'run', '--rm', '--network', 'none', '--read-only', '-v', str(destination.resolve()) + ':/artifacts', + '--entrypoint', strip, image, '--strip-debug', '/artifacts/' + copy.name]) + require(sha(original) == sha(linked) == linked_hash, 'preserved original test link') + binary = copy.read_bytes() + if platform == 'linux': + require(binary[:6] == b'\x7fELF\x02\x01' and binary[18:20] == b'\x3e\x00', 'Linux x64 ELF') + else: + offset = int.from_bytes(binary[60:64], 'little') + require(binary[:2] == b'MZ' and binary[offset:offset + 6] == b'PE\0\0\x64\x86', 'Windows x64 PE') + artifacts[platform][kind] = dict(path=str(copy.resolve()), sha256=sha(copy), bytes=copy.stat().st_size, + original_link_path=str(linked.resolve()), original_link_sha256=linked_hash, debug_stripped_copy=True) + require(set(artifacts[platform]) == set(COUNTS[platform]), 'all fixed platform artifacts') + expected = {'linux': {}, 'windows': {}} + for kind, item in artifacts['linux'].items(): + raw = command(['docker', 'run', '--rm', '--network', 'none', '--read-only', '--tmpfs', '/tmp:rw,exec,size=128m', + '-v', item['path'] + ':/unit:ro', '--entrypoint', 'python3', image, '-c', + 'import shutil,os,subprocess;shutil.copyfile("/unit","/tmp/unit");os.chmod("/tmp/unit",0o500);' + 'subprocess.run(["/tmp/unit","--list","--format","terse"],check=True);' + 'subprocess.run(["/tmp/unit","--test-threads=1"],check=True)']).decode() + save(folder / ('local-linux-' + kind + '.json'), dict(stdout=raw)) + expected['linux'][kind] = names(raw) + require(len(expected['linux'][kind]) == COUNTS['linux'][kind], 'actual Linux artifact list count') + require(re.search(r'test result: ok\. ' + str(COUNTS['linux'][kind]) + r' passed; 0 failed; 0 ignored; 0 measured; 0 filtered out;', raw), 'all local Linux units execute') + unix_only = 'openssl_and_portable_ed25519_agree_on_current_keys_bytes_and_tampering' + require(unix_only in expected['linux']['contracts'], 'explicit Unix-only parity test') + for kind in artifacts['windows']: + expected['windows'][kind] = [n for n in expected['linux'][kind] if n != unix_only] + require(len(expected['windows'][kind]) == COUNTS['windows'][kind], 'expected Windows compile-profile count') + require(source_files() == before, 'identical sources and runner after build') + manifest = dict(schema_version=1, profile='deny-only-no-start-v1', source_files=before, + source_before_after_equal=True, artifacts=artifacts, test_names=expected, test_counts=COUNTS, + linux_lists_executed_locally=True, linux_openssl_verifier=True, windows_lists_expected_until_native=True, + local_linux_tests_passed=sum(COUNTS['linux'].values()), both_target_clippy_D_warnings=True, + build_image_id=image, build_libc=command(['docker', 'run', '--rm', '--network', 'none', '--read-only', '--entrypoint', 'ldd', image, '--version']).decode().splitlines()[0], + native_linux_vm_executed=False, native_windows_vm_executed=False, rmm_jobs=False, product_state_writes=False) + save(folder / 'manifest.json', manifest) + print(json.dumps(dict(manifest=str((folder / 'manifest.json').resolve()), linux_tests=80, windows_tests_expected=58, native_executed=False))) + + +def verified(path): + value = contract.protected_json(path) + require(value['schema_version'] == 1 and value['profile'] == 'deny-only-no-start-v1' + and value['source_files'] == source_files() and value['source_before_after_equal'] is True + and value['test_counts'] == COUNTS, 'frozen source/runner/profile binding') + for platform, entries in value['artifacts'].items(): + require(set(entries) == set(COUNTS[platform]), 'fixed artifacts') + for kind, item in entries.items(): + require(Path(item['path']) == path.resolve().parent / platform / (kind + ('.exe' if platform == 'windows' else '')) + and Path(item['original_link_path']) == path.resolve().parent / platform / (kind + '.linked'), 'fixed unit artifact paths') + for label, expected in [('path', item['sha256']), ('original_link_path', item['original_link_sha256'])]: + file = Path(item[label]) + require(not file.is_symlink() and file.stat().st_uid == os.getuid() and stat.S_IMODE(file.stat().st_mode) == 0o600 + and sha(file) == expected, 'private exact original/stripped artifact') + require(item['bytes'] == Path(item['path']).stat().st_size and len(value['test_names'][platform][kind]) == COUNTS[platform][kind], 'artifact bytes/list') + return value + + +def linux_case(p): + import resource + import stat + require(os.geteuid() == 0 and os.uname().machine == 'x86_64', 'Linux x64 bootstrap unit qualification') + ident = str(uuid.UUID(p['id'])) + require(ident == p['id'] and p['action'] in ('prepare', 'run', 'read') and set(p['artifacts']) == {'contracts', 'helper-reconcile', 'reconcile', 'linux-helper'}, 'fixed native case') + root = Path('/var/lib/rmm-core-no-start-units') + def safe(path, directory): + s = path.lstat() + require(s.st_uid == 0 and s.st_mode & 0o077 == 0 and not stat.S_ISLNK(s.st_mode) + and (stat.S_ISDIR(s.st_mode) if directory else stat.S_ISREG(s.st_mode)), 'private fixed unit path') + def write(path, value): + fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_WRONLY | os.O_NOFOLLOW, 0o600) + with os.fdopen(fd, 'w') as f: + json.dump(value, f); f.flush(); os.fsync(f.fileno()) + if not root.exists(): root.mkdir(mode=0o700) + safe(root, True); case = root / ident + if p['action'] == 'prepare': + case.mkdir(mode=0o700); write(case / 'intent.json', p); return dict(prepared=True) + safe(case, True); safe(case / 'intent.json', False) + intent = json.loads((case / 'intent.json').read_bytes()) + require(all(p[k] == intent[k] for k in ('id', 'manifest_sha256', 'artifacts', 'names')), 'same native source/artifact intent') + if p['action'] == 'read': + safe(case / 'result.json', False); return json.loads((case / 'result.json').read_bytes()) + write(case / 'started.json', dict(start_once=True, manifest_sha256=p['manifest_sha256'])) + tmp = case / 'tmp'; tmp.mkdir(mode=0o700) + env = {'PATH': '/usr/bin:/bin', 'LANG': 'C.UTF-8', 'TMPDIR': str(tmp), 'RUST_BACKTRACE': '0'} + require(subprocess.run(['/usr/bin/unshare', '--net', '--', '/usr/bin/true'], env=env, capture_output=True, timeout=3).returncode == 0, 'network isolation') + def limits(): + resource.setrlimit(resource.RLIMIT_CORE, (0, 0)); resource.setrlimit(resource.RLIMIT_FSIZE, (1048576, 1048576)); resource.setrlimit(resource.RLIMIT_CPU, (15, 15)) + reports = {} + for kind, item in p['artifacts'].items(): + binary = case / kind; safe(binary, False) + require(hashlib.sha256(binary.read_bytes()).hexdigest() == item['sha256'], 'pinned Linux unit bytes') + binary.chmod(0o500) + listed = subprocess.run(['/usr/bin/unshare', '--net', '--', str(binary), '--list', '--format', 'terse'], env=env, capture_output=True, timeout=5) + require(listed.returncode == 0 and len(listed.stdout) <= 65536, 'bounded native listing') + actual = sorted(line[:-6] for line in listed.stdout.decode().splitlines() if line.endswith(': test')) + require(actual == p['names'][kind], 'exact original unit names before execution') + with (case / (kind + '-stdout.bin')).open('xb') as out, (case / (kind + '-stderr.bin')).open('xb') as err: + os.chmod(out.name, 0o600); os.chmod(err.name, 0o600); timed = False + try: + code = subprocess.run(['/usr/bin/unshare', '--net', '--', str(binary), '--test-threads=1'], env=env, + stdin=subprocess.DEVNULL, stdout=out, stderr=err, timeout=25, preexec_fn=limits).returncode + except subprocess.TimeoutExpired: + timed = True; code = None + output = [(case / (kind + '-' + label + '.bin')).read_bytes() for label in ('stdout', 'stderr')] + require(all(len(v) <= 1048576 for v in output), 'bounded unit output') + reports[kind] = dict(sha256=item['sha256'], names=actual, exit_code=code, timed_out=timed, + stdout_b64=base64.b64encode(output[0]).decode(), stderr_b64=base64.b64encode(output[1]).decode()) + write(case / (kind + '-result.json'), reports[kind]) + result = dict(platform='linux', manifest_sha256=p['manifest_sha256'], reports=reports, + libc=subprocess.run(['/usr/bin/ldd', '--version'], capture_output=True, check=True, timeout=3).stdout.decode().splitlines()[0], product_state_writes=False) + write(case / 'result.json', result) + return result + + +WINDOWS = 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}$'-or$p.linux_proof_sha256-notmatch'^[a-f0-9]{64}$'){throw 'Fixed Linux-first unit case'} +$root='C:\ProgramData\RustDiscoveryBootstrap';$case=Join-Path $root ('core-no-start-'+$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'};$r=@($a.GetAccessRules($true,$true,[Security.Principal.SecurityIdentifier]));if(!$r.Count){throw 'Empty ACL'};foreach($e in $r){if($e.AccessControlType-ne'Allow'-or$e.IdentityReference.Value-notin@('S-1-5-18','S-1-5-32-544')){throw 'Broad unit ACL'}}} +function WriteNew($path,$v){$f=[IO.File]::Open($path,[IO.FileMode]::CreateNew,[IO.FileAccess]::Write,[IO.FileShare]::Read);try{$b=[Text.Encoding]::UTF8.GetBytes(($v|ConvertTo-Json -Depth 12 -Compress));$f.Write($b,0,$b.Length);$f.Flush($true)}finally{$f.Dispose()}} +Safe $root $true +if($p.action-eq'read'){Safe $case $true;$i=Get-Content -LiteralPath (Join-Path $case 'intent.json') -Raw|ConvertFrom-Json;if($i.manifest_sha256-cne$p.manifest_sha256-or$i.linux_proof_sha256-cne$p.linux_proof_sha256){throw 'Original case'};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 native intent'};New-Item -ItemType Directory -Path $case|Out-Null;Safe $case $true;WriteNew (Join-Path $case 'intent.json') $p +function RunUnit($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 start'};try{$o=$c.StandardOutput.ReadToEndAsync();$e=$c.StandardError.ReadToEndAsync();$t=$false;if(!$c.WaitForExit(25000)){$t=$true;$c.Kill();$c.WaitForExit()};if($o.Result.Length-gt1048576-or$e.Result.Length-gt1048576){throw 'Unit output bound'};return @{exit_code=$c.ExitCode;timed_out=$t;stdout_b64=[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($o.Result));stderr_b64=[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($e.Result))}}finally{$c.Dispose()}} +WriteNew (Join-Path $case 'started.json') @{start_once=$true;manifest_sha256=$p.manifest_sha256};$reports=@{} +foreach($kind in @('contracts','helper-reconcile','reconcile')){$a=$p.artifacts.$kind;if($a.sha256-notmatch'^[a-f0-9]{64}$'){throw 'Fixed unit hash'};$folder=Join-Path $root $a.sha256;Safe $folder $true;$exe=Join-Path $folder ('windows-core-'+$kind+'.exe');Safe $exe $false;if((Get-FileHash -LiteralPath $exe -Algorithm SHA256).Hash.ToLowerInvariant()-cne$a.sha256){throw 'Exact unit bytes'};$list=RunUnit $exe '--list --format terse';if($list.exit_code-ne0-or$list.timed_out){throw 'Native unit listing'};$text=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($list.stdout_b64));$actual=@($text-split'\r?\n'|Where-Object{$_-match': test$'}|ForEach-Object{$_-replace': test$',''}|Sort-Object);if(($actual-join"`n")-cne(@($p.names.$kind|Sort-Object)-join"`n")){throw 'All exact native Windows test names before execution'};$value=RunUnit $exe '--test-threads=1';$value.sha256=$a.sha256;$value.names=$actual;WriteNew (Join-Path $case ($kind+'-result.json')) $value;$reports[$kind]=$value} +$v=@{platform='windows';manifest_sha256=$p.manifest_sha256;linux_proof_sha256=$p.linux_proof_sha256;reports=$reports;bootstrap_administrator_units=$true;local_service_proof=$false;product_state_writes=$false};WriteNew (Join-Path $case 'result.json') $v;$v|ConvertTo-Json -Depth 12 -Compress +''' + + +def check_result(manifest, platform, result): + require(result['platform'] == platform and set(result['reports']) == set(COUNTS[platform]), 'actual platform/all artifacts') + for kind, actual in result['reports'].items(): + require(actual['sha256'] == manifest['artifacts'][platform][kind]['sha256'] + and actual['names'] == manifest['test_names'][platform][kind] and actual['exit_code'] == 0 + and actual['timed_out'] is False, 'actual native list/artifact/exit') + out = base64.b64decode(actual['stdout_b64'], validate=True) + err = base64.b64decode(actual['stderr_b64'], validate=True) + require(len(out) <= 1048576 and len(err) <= 1048576 and re.search(rb'test result: ok\. ' + str(COUNTS[platform][kind]).encode() + + rb' passed; 0 failed; 0 ignored; 0 measured; 0 filtered out;', out), 'all exact native units execute, none ignored') + + +def execute(path, platform, case_id=None): + manifest = verified(path) + if case_id is None: + case_id = str(uuid.uuid4()); folder = STATE / case_id + folder.mkdir(mode=0o700) + save(folder / 'intent.json', dict(id=case_id, platform=platform, manifest_path=str(path.resolve()), manifest_sha256=sha(path))) + read = False + else: + case_id = str(uuid.UUID(case_id)); folder = STATE / case_id + intent = contract.protected_json(folder / 'intent.json') + require(intent['platform'] == platform and intent['manifest_sha256'] == sha(path) and (folder / 'started-local.json').exists(), 'same original started case; readback only') + read = True + payload = dict(id=case_id, manifest_sha256=sha(path), artifacts=manifest['artifacts'][platform], names=manifest['test_names'][platform]) + if platform == 'linux': + if not read: + linux.rpc('linux-vm.conf', linux_case, dict(payload, action='prepare')) + cfg = linux.access.load_config(ROOT / 'credentials/linux-vm.conf') + args = ['sftp', '-F', '/dev/null', '-P', cfg.get('port', '22'), '-o', 'StrictHostKeyChecking=yes', '-o', 'BatchMode=yes', + '-o', 'IdentitiesOnly=yes', '-o', 'ForwardAgent=no', '-o', 'ConnectTimeout=10', '-i', str(Path(cfg['ssh_key_path']).expanduser()), '-b', '-', cfg['username'] + '@' + cfg['address']] + batch = ''.join('put ' + json.dumps(item['path']) + ' ' + json.dumps(REMOTE + '/' + case_id + '/' + kind) + '\n' for kind, item in payload['artifacts'].items()) + transfer = subprocess.run(args, input=batch.encode(), capture_output=True, timeout=120) + save(folder / 'upload.json', dict(returncode=transfer.returncode)) + require(transfer.returncode == 0, 'private pinned unit transfer') + save(folder / 'started-local.json', dict(run_once=True)) + result = linux.rpc('linux-vm.conf', linux_case, dict(payload, action='read' if read else 'run')) + else: + proof_path = path.parent / 'linux-passed.json' + proof = contract.protected_json(proof_path) + require(proof['verified'] is True and proof['manifest_sha256'] == sha(path), 'real same-manifest Linux first') + check_result(manifest, 'linux', proof['native']) + payload['linux_proof_sha256'] = sha(proof_path) + if not read: + for kind, item in payload['artifacts'].items(): + windows.upload(Path(item['path']), 'windows-core-' + kind + '.exe') + save(folder / 'started-local.json', dict(run_once=True, linux_proof_sha256=payload['linux_proof_sha256'])) + result = windows.powershell(WINDOWS, dict(payload, action='read' if read else 'run'), timeout=120) + save(folder / ('readback-' + str(uuid.uuid4()) + '.json'), result) + require(result['manifest_sha256'] == sha(path), 'native original manifest') + check_result(manifest, platform, result) + if platform == 'windows': + require(result['linux_proof_sha256'] == payload['linux_proof_sha256'], 'native Linux proof binding') + proof = dict(verified=True, case_id=case_id, platform=platform, manifest_sha256=sha(path), native=result) + destination = path.parent / (platform + '-passed.json') + if destination.exists(): + require(contract.protected_json(destination) == proof, 'retain exact original native proof') + else: + save(destination, proof) + print(json.dumps(dict(verified=True, platform=platform, case_id=case_id, tests=sum(COUNTS[platform].values())))) + + +def main(): + os.umask(0o077) + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('action', choices=('build-local', 'linux', 'windows', 'readback-linux', 'readback-windows')) + parser.add_argument('--manifest', type=Path) + parser.add_argument('--case') + parser.add_argument('--execute-native-tests', action='store_true') + args = parser.parse_args() + if args.action == 'build-local': + require(not (args.manifest or args.case or args.execute_native_tests), 'local build only'); build() + elif args.action.startswith('readback-'): + require(args.manifest and args.case and not args.execute_native_tests, 'original case readback only') + execute(args.manifest, args.action.removeprefix('readback-'), args.case) + else: + require(args.manifest and args.execute_native_tests and not args.case, 'explicit new native unit run') + execute(args.manifest, args.action) + + +if __name__ == '__main__': + try: + main() + except Exception as error: + print('CORE_NO_START_QUALIFICATION_INCOMPLETE:' + type(error).__name__) + raise SystemExit(2) from None diff --git a/docs/handover.md b/docs/handover.md index c0776a6..ac62978 100644 --- a/docs/handover.md +++ b/docs/handover.md @@ -84,7 +84,10 @@ Kein Deployment oder Capabilitysetup. Vorbereiteter Runtimepfad bleibt wegen Reviewbefund zu Vor-Delegation-Consumption gesperrt; dessen Korrektur darf den unabhängigen Helperabgleich nicht aufweichen. Spezifikation1.17 und [Abandonvertrag](windows-system-abandon.md) beschreiben -den zusätzlichen engen deny-only Nichtstart, noch ohne Implementierung/Nativbeleg. +den zusätzlichen engen deny-only Nichtstart. Dessen gemeinsamer Kern ist jetzt +implementiert: lokal80 und nativ Cloud-Linux80 Prüfungen bestanden, Fall +`9d08e552…`, gemeinsamer Build `de37ee99…`; Windows58 noch erwartet. +Windowsadapter weiter im unabhängigen Review, keine Runtime-/Jobqualifikation. [SYSTEM-Vertrag und Nachweise](windows-system-policy.md). Helper-Peer-Kontrollfall `603c2f9d…`: native Windows5-Speicherproben der diff --git a/docs/windows-system-abandon.md b/docs/windows-system-abandon.md index 0c06702..11e85b5 100644 --- a/docs/windows-system-abandon.md +++ b/docs/windows-system-abandon.md @@ -1,11 +1,35 @@ # Windows-SYSTEM: gebundener Nichtstart vor Delegation -Stand 2026-11-14, **verbindlicher Implementierungsvertrag**, Spezifikation1.17. Noch keine -Implementierung, Capability, Installation oder native Abnahme dieses Vertrags. +Stand 2026-11-14, **verbindlicher Implementierungsvertrag**, Spezifikation1.17. +Gemeinsamer Kern implementiert und nativ Linux-first qualifiziert; der +Windowsadapter bleibt im Review. Keine Capability, Installation oder tatsächliche +SYSTEM-Jobabnahme dieses Vertrags. Die normative Präzisierung steht in Spezifikation0.1. Der Notebookhost bleibt Entwicklungsrechner; Ziel bleibt ausschließlich der eigene Windows-Testgast am festgelegten Cloudserver. +## Tatsächlicher Kernnachweis + +Der neue gesonderte Runner `deploy/core_no_start_acceptance.py` bindet59 +Quelldateien und denselben Linux-/Windowsbuild im Manifest +`build-de37ee99-11bf-465b-9519-45ad8f6e5b87` (SHA256 `1bc08203…`). +Lokal bestanden beide Alltargets-Clippy-Ziele und80 Linux-Units. Anschließend +bestanden auf der tatsächlichen Cloud-Linux-VM im Fall +`9d08e552-757c-4bbb-943f-9f0712e442b1` alle80 Prüfungen ohne Skips: +7 Verträge,26 Helper-Sync,26 Agent-Sync und21 Unix-Helperintegration. +Getrennte Netzwerknamespace, leere Prozessumgebung und privates Scratch; +keine RMM-Jobs, Produktdaten, Server- oder Dienständerungen. +Die58 Windows-Prüfungen sind noch **erwartet, nicht nativ ausgeführt**. + +Der Kern prüft historische signierte Originalbytes nur für die Verweigerung; +die aktuelle Startprüfung behält das echte Zeitfenster. Der unabhängige +Adapterguard muss unveränderte Helper-/Dateiidentität und den festen +KnownNoDispatch-Store prüfen. Neue Verbrauchs-, Start- und feste Nichtstart- +Outcomezeilen entstehen gemeinsam in einer SQLite-Transaktion und bleiben bis +zum normalen signierten Sync gesperrt. Bestehende Unknowns, fehlende Starts +und kollidierende Requests werden niemals zu bewiesenem Nichtstart umgeschrieben. +Diese Units ersetzen keinen tatsächlichen Windows-Token-/Pipe-/Clocknachweis. + ## Zweck und unveränderte Grenzen Ein SYSTEM-SCM-Auftrag besitzt bereits einen WebUI-genehmigten festen Plan und