Fix SYSTEM script directory sharing and record original failed VM attempt
Rust development slice / linux (push) Failing after 7s
Rust development slice / linux (push) Failing after 7s
This commit is contained in:
@@ -0,0 +1,255 @@
|
||||
//! Read-only, fixed MVP acceptance witness. No execution or journal mutation.
|
||||
#[cfg(windows)]
|
||||
fn run() -> anyhow::Result<()> {
|
||||
use anyhow::{Result, ensure};
|
||||
use rmm_agent_core::{
|
||||
auth::{Binding, Outcome, Request},
|
||||
crypto::DalekVerifier,
|
||||
};
|
||||
use rmm_domain::jobs::{Action, hash};
|
||||
use rusqlite::{Connection, OpenFlags, OptionalExtension, params};
|
||||
use serde_json::{Value, json};
|
||||
use std::{path::Path, time::Duration};
|
||||
use windows_sys::Win32::{
|
||||
Foundation::CloseHandle,
|
||||
Security::{GetTokenInformation, TOKEN_ELEVATION, TOKEN_QUERY, TokenElevation},
|
||||
System::Threading::{GetCurrentProcess, OpenProcessToken},
|
||||
};
|
||||
ensure!(std::env::args().count() == 1, "FIXED_READ_ONLY_ARGUMENTS");
|
||||
let mut token = std::ptr::null_mut();
|
||||
ensure!(
|
||||
unsafe { OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &mut token) } != 0,
|
||||
"ADMIN_TOKEN"
|
||||
);
|
||||
let mut elevation = TOKEN_ELEVATION { TokenIsElevated: 0 };
|
||||
let mut size = 0;
|
||||
let okay = unsafe {
|
||||
GetTokenInformation(
|
||||
token,
|
||||
TokenElevation,
|
||||
&mut elevation as *mut _ as *mut _,
|
||||
std::mem::size_of::<TOKEN_ELEVATION>() as u32,
|
||||
&mut size,
|
||||
)
|
||||
};
|
||||
unsafe { CloseHandle(token) };
|
||||
ensure!(
|
||||
okay != 0 && elevation.TokenIsElevated != 0,
|
||||
"ADMIN_REQUIRED"
|
||||
);
|
||||
fn open(path: &str) -> Result<Connection> {
|
||||
ensure!(Path::new(path).is_file(), "ORIGINAL_JOURNAL_REQUIRED");
|
||||
let db = Connection::open_with_flags(
|
||||
path,
|
||||
OpenFlags::SQLITE_OPEN_READ_ONLY | OpenFlags::SQLITE_OPEN_NO_MUTEX,
|
||||
)?;
|
||||
db.busy_timeout(Duration::from_millis(500))?;
|
||||
Ok(db)
|
||||
}
|
||||
|
||||
let job_id = "94047873-5088-433b-bc1b-490d3eafe0da";
|
||||
let attempt_id = "f071e6d6-8635-42f7-92fa-533c681edd8b";
|
||||
let grant_id = "97e705b8-fb2b-4158-84d9-d90595d1ce6e";
|
||||
let plan_hash = "9afc2bacefd937199bf6407c12b1f7c5cad933cd875514f651afcca9eb9fae43";
|
||||
let script_hash = "880930d2b5d903a8ee8f4a9d3acad6162ef994b1822d7381c8468230bcf4a86c";
|
||||
let raw = std::fs::read(r"C:\ProgramData\RustDiscovery\binding.json")?;
|
||||
ensure!(raw.len() <= 8192, "PUBLIC_BINDING_BOUND");
|
||||
let binding: Binding = serde_json::from_slice(&raw)?;
|
||||
ensure!(
|
||||
binding.tenant_id.to_string() == "11111111-1111-4111-8111-111111111111"
|
||||
&& binding.asset_id.to_string() == "a176520e-8a91-4e93-a78b-8e7620f430e0"
|
||||
&& binding.agent_id.to_string() == "63ea15b2-0643-403d-b5b4-cba1288dbea3"
|
||||
&& binding.enrollment_generation == 1
|
||||
&& binding.assignment_version == 1,
|
||||
"ORIGINAL_DEVICE_BINDING"
|
||||
);
|
||||
fn evidence(
|
||||
db: &Connection,
|
||||
binding: &Binding,
|
||||
job_id: &str,
|
||||
attempt_id: &str,
|
||||
grant_id: &str,
|
||||
plan_hash: &str,
|
||||
script_hash: &str,
|
||||
) -> Result<Value> {
|
||||
let count: u64 = db.query_row(
|
||||
"SELECT count(*) FROM consumed WHERE job=?1 OR attempt=?2 OR grant_id=?3",
|
||||
params![job_id, attempt_id, grant_id],
|
||||
|r| r.get(0),
|
||||
)?;
|
||||
ensure!(count <= 1, "NO_DUPLICATE_CONSUMPTION");
|
||||
type Row = (Option<Vec<u8>>, Option<Vec<u8>>, Option<Vec<u8>>);
|
||||
let row:Option<Row>=db.query_row("SELECT e.start,e.outcome,c.result FROM consumed c LEFT JOIN execution_evidence e USING(grant_id) WHERE c.grant_id=?1 AND c.job=?2 AND c.attempt=?3 AND c.agent='63ea15b2-0643-403d-b5b4-cba1288dbea3' AND c.generation=1",params![grant_id,job_id,attempt_id],|r|Ok((r.get(0)?,r.get(1)?,r.get(2)?))).optional()?;
|
||||
let Some((start, outcome, finished)) = row else {
|
||||
ensure!(count == 0, "CONSUMPTION_BINDING_MISMATCH");
|
||||
return Ok(json!({"consumption_count":0,"start_count":0,"outcome_count":0}));
|
||||
};
|
||||
let mut report = json!({"consumption_count":count,"start_count":u8::from(start.is_some()),"outcome_count":u8::from(outcome.is_some()),"finished_present":finished.is_some(),"outcome_equals_finished":outcome==finished});
|
||||
if let Some(start) = start {
|
||||
ensure!(start.len() <= 131072, "START_BOUND");
|
||||
let saved: Value = serde_json::from_slice(&start)?;
|
||||
let request: Request = serde_json::from_value(saved["request"].clone())?;
|
||||
let reserved = serde_json::from_value(saved["reserved_at"].clone())?;
|
||||
let (job, permit) = binding.verify_with(&request, reserved, &DalekVerifier)?;
|
||||
ensure!(
|
||||
job.job_id.to_string() == job_id
|
||||
&& job.attempt_id.to_string() == attempt_id
|
||||
&& permit.grant_id.to_string() == grant_id
|
||||
&& request.plan_hash == plan_hash
|
||||
&& job.execution_identity == "system"
|
||||
&& job.max_runtime_seconds == 30
|
||||
&& job.max_output_bytes == 4096
|
||||
&& job.action.full_maintenance(),
|
||||
"SIGNED_ORIGINAL_SYSTEM_REQUEST"
|
||||
);
|
||||
let Action::RunScript { script_utf8, .. } = &job.action else {
|
||||
anyhow::bail!("SYSTEM_SCRIPT_REQUIRED")
|
||||
};
|
||||
ensure!(
|
||||
hash(script_utf8.as_bytes()) == script_hash,
|
||||
"EXACT_APPROVED_SCRIPT"
|
||||
);
|
||||
report["original_signature_verified"] = json!(true);
|
||||
report["request_sha256"] = json!(hash(&serde_json::to_vec(&request)?));
|
||||
report["start_sha256"] = json!(hash(&start));
|
||||
report["reserved_at"] = saved["reserved_at"].clone();
|
||||
report["original_boot_id"] = saved["previous_boot_id"].clone();
|
||||
}
|
||||
if let Some(outcome) = outcome {
|
||||
ensure!(outcome.len() <= 131072, "OUTCOME_BOUND");
|
||||
let result: Outcome = serde_json::from_slice(&outcome)?;
|
||||
report["outcome_sha256"] = json!(hash(&outcome));
|
||||
report["state"] = json!(result.state);
|
||||
report["error_code"] = json!(result.error_code);
|
||||
report["exit_code"] = json!(result.exit_code);
|
||||
report["stdout_bytes"] = json!(result.stdout.len());
|
||||
report["stderr_bytes"] = json!(result.stderr.len());
|
||||
report["stdout_sha256"] = json!(hash(&result.stdout));
|
||||
report["expected_stdout_matches"] =
|
||||
json!(result.stdout == b"Windows-System-MVP:S-1-5-18\r\n");
|
||||
report["output_truncated"] = json!(result.output_truncated);
|
||||
report["evidence"] = result.evidence;
|
||||
}
|
||||
Ok(report)
|
||||
}
|
||||
let agent = open(r"C:\ProgramData\RustDiscovery\state\telemetry.sqlite")?;
|
||||
let consumption = open(r"C:\ProgramData\RustDiscovery\state\consumption.sqlite")?;
|
||||
let helper = open(r"C:\ProgramData\RustDiscoverySystem\state\ledger.sqlite")?;
|
||||
let intents = open(r"C:\ProgramData\RustDiscoverySystem\agent-intents\system-intents.sqlite")?;
|
||||
let agent_result = evidence(
|
||||
&consumption,
|
||||
&binding,
|
||||
job_id,
|
||||
attempt_id,
|
||||
grant_id,
|
||||
plan_hash,
|
||||
script_hash,
|
||||
)?;
|
||||
let helper_result = evidence(
|
||||
&helper,
|
||||
&binding,
|
||||
job_id,
|
||||
attempt_id,
|
||||
grant_id,
|
||||
plan_hash,
|
||||
script_hash,
|
||||
)?;
|
||||
let state: Option<(String, Option<String>)> = agent
|
||||
.query_row(
|
||||
"SELECT state,grant_id FROM job_attempts WHERE job=?1 AND attempt=?2",
|
||||
params![job_id, attempt_id],
|
||||
|r| Ok((r.get(0)?, r.get(1)?)),
|
||||
)
|
||||
.optional()?;
|
||||
let initialized: bool = intents.query_row(
|
||||
"SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type='table' AND name='system_intents')",
|
||||
[],
|
||||
|r| r.get(0),
|
||||
)?;
|
||||
let mut intent_result = json!({"initialized":initialized,"count":0});
|
||||
if initialized {
|
||||
let count: u64 = intents.query_row(
|
||||
"SELECT count(*) FROM system_intents WHERE job=?1 OR attempt=?2 OR grant_id=?3",
|
||||
params![job_id, attempt_id, grant_id],
|
||||
|r| r.get(0),
|
||||
)?;
|
||||
ensure!(count <= 1, "NO_DUPLICATE_INTENT");
|
||||
type IntentRow = (Vec<u8>, String, String, u32, Option<String>);
|
||||
let row:Option<IntentRow>=intents.query_row("SELECT i.body,i.digest,i.phase,i.cancel_requested,m.outcome_sha256 FROM system_intents i LEFT JOIN system_intent_mirrors m USING(id) WHERE i.job=?1 AND i.attempt=?2 AND i.grant_id=?3",params![job_id,attempt_id,grant_id],|r|Ok((r.get(0)?,r.get(1)?,r.get(2)?,r.get(3)?,r.get(4)?))).optional()?;
|
||||
if let Some((body, digest, phase, cancelled, mirror)) = row {
|
||||
ensure!(
|
||||
body.len() <= 196608 && hash(&body) == digest,
|
||||
"INTENT_BODY_HASH"
|
||||
);
|
||||
let v: Value = serde_json::from_slice(&body)?;
|
||||
let request: Request = serde_json::from_value(v["request"].clone())?;
|
||||
let p: rmm_domain::jobs::ExecutionPermitPayload =
|
||||
serde_json::from_slice(&request.permit_bytes)?;
|
||||
let (job, permit) = binding.verify_with(&request, p.not_before, &DalekVerifier)?;
|
||||
ensure!(
|
||||
job.job_id.to_string() == job_id
|
||||
&& job.attempt_id.to_string() == attempt_id
|
||||
&& permit.grant_id.to_string() == grant_id
|
||||
&& request.plan_hash == plan_hash
|
||||
&& job.execution_identity == "system"
|
||||
&& v["policy_sha256"]
|
||||
== "cfd126d383ec0a948a73270a8362bd835d79341a079be05ce50088f0dbc5791c"
|
||||
&& v["helper_binary_sha256"]
|
||||
== "0502d6c87bd74c99f1665f3cc4b9d74b8e31d0f829d2ce1a1597d72e9be1e726",
|
||||
"ORIGINAL_INTENT_REQUEST_POLICY_CODE"
|
||||
);
|
||||
intent_result = json!({"initialized":true,"count":count,"phase":phase,"cancel_requested":cancelled,"body_sha256":digest,"original_signature_verified":true,"request_sha256":hash(&serde_json::to_vec(&request)?),"recorded_at":v["recorded_at"],"connection_generation":v["connection_generation"],"original_boot_id":v["original_boot_id"],"mirror_outcome_sha256":mirror});
|
||||
} else {
|
||||
ensure!(count == 0, "INTENT_BINDING_MISMATCH");
|
||||
}
|
||||
}
|
||||
use rmm_agent_windows::helper_ipc::{FileIdentity, file_identity};
|
||||
let actual_ledger = file_identity(&std::fs::File::open(
|
||||
r"C:\ProgramData\RustDiscoverySystem\state\ledger.sqlite",
|
||||
)?)?;
|
||||
let actual_lock = file_identity(&std::fs::File::open(
|
||||
r"C:\ProgramData\RustDiscoverySystem\state\executor.lock",
|
||||
)?)?;
|
||||
let actual_intents = file_identity(&std::fs::File::open(
|
||||
r"C:\ProgramData\RustDiscoverySystem\agent-intents\system-intents.sqlite",
|
||||
)?)?;
|
||||
let (a, b, c): (Vec<u8>, Vec<u8>, String) = helper.query_row(
|
||||
"SELECT ledger,executor_lock,binding_sha256 FROM windows_helper_files WHERE id=1",
|
||||
[],
|
||||
|r| Ok((r.get(0)?, r.get(1)?, r.get(2)?)),
|
||||
)?;
|
||||
ensure!(
|
||||
serde_json::from_slice::<FileIdentity>(&a)? == actual_ledger
|
||||
&& serde_json::from_slice::<FileIdentity>(&b)? == actual_lock
|
||||
&& c == hash(&serde_json::to_vec(&binding)?),
|
||||
"ORIGINAL_HELPER_FILE_IDENTITIES"
|
||||
);
|
||||
if initialized {
|
||||
let stored: Vec<u8> = intents.query_row(
|
||||
"SELECT file_identity FROM system_intent_meta WHERE id=1",
|
||||
[],
|
||||
|r| r.get(0),
|
||||
)?;
|
||||
ensure!(
|
||||
serde_json::from_slice::<FileIdentity>(&stored)? == actual_intents,
|
||||
"ORIGINAL_INTENT_FILE_ID"
|
||||
);
|
||||
}
|
||||
println!(
|
||||
"{}",
|
||||
json!({"schema_version":1,"read_only":true,"job_id":job_id,"attempt_id":attempt_id,"grant_id":grant_id,"plan_hash":plan_hash,"script_sha256":script_hash,"local_job":state,"agent":agent_result,"helper":helper_result,"intent":intent_result,"original_helper_file_ids_verified":true,"helper_ledger_file_id":actual_ledger,"intent_file_id":actual_intents,"tenant_id":binding.tenant_id,"asset_id":binding.asset_id,"agent_id":binding.agent_id,"enrollment_generation":binding.enrollment_generation,"assignment_version":binding.assignment_version,"public_binding_sha256":hash(&raw),"private_key_read":false,"execution_requested":false})
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
#[cfg(windows)]
|
||||
fn main() {
|
||||
if run().is_err() {
|
||||
eprintln!("SYSTEM_SCRIPT_WITNESS_REFUSED");
|
||||
std::process::exit(1)
|
||||
}
|
||||
}
|
||||
#[cfg(not(windows))]
|
||||
fn main() {
|
||||
eprintln!("Windows only");
|
||||
std::process::exit(1)
|
||||
}
|
||||
Reference in New Issue
Block a user