Files
rust-discovery/crates/agent-windows/src/echo_completion.rs
T

465 lines
19 KiB
Rust

//! Append-only retirement of the additional development echo restriction.
//! Preserves all original scope, journal, fault history and consumption bytes.
use anyhow::{Context, Result, ensure};
use chrono::{DateTime, Utc};
use prost::Message;
use rmm_agent_core::{
auth::{Binding, Request},
crypto::{DalekVerifier, Ed25519Verifier},
};
use rmm_domain::{
checkpoint_repair::{SignedReceipt, inventory_sha256},
jobs::{ExecutionPermitPayload, JobPayload, hash},
windows_echo_completion::{Receipt, Witness, successful_outcome},
windows_echo_probe::{ProbeReceipt, ProbeScope},
};
use rmm_protocol::JobOffer;
use rusqlite::{Connection, TransactionBehavior, params};
use serde_json::Value;
const TABLE: &str = "windows_echo_scope_completion";
const DDL: &str = "CREATE TABLE windows_echo_scope_completion(id INTEGER PRIMARY KEY CHECK(id=1),original_receipt BLOB NOT NULL,witness_sha256 TEXT NOT NULL,applied_at TEXT NOT NULL)";
fn schema() -> [String; 4] {
[DDL.into(),
"CREATE TRIGGER windows_echo_completion_no_replace BEFORE INSERT ON windows_echo_scope_completion WHEN EXISTS(SELECT 1 FROM windows_echo_scope_completion) BEGIN SELECT RAISE(ABORT,'WINDOWS_ECHO_COMPLETION_IMMUTABLE'); END".into(),
"CREATE TRIGGER windows_echo_completion_no_change BEFORE UPDATE ON windows_echo_scope_completion BEGIN SELECT RAISE(ABORT,'WINDOWS_ECHO_COMPLETION_IMMUTABLE'); END".into(),
"CREATE TRIGGER windows_echo_completion_no_delete BEFORE DELETE ON windows_echo_scope_completion BEGIN SELECT RAISE(ABORT,'WINDOWS_ECHO_COMPLETION_IMMUTABLE'); END".into()]
}
fn verify(binding: &Binding, bytes: &[u8], signature: &[u8]) -> Result<()> {
let b = &binding.public_key_hex;
ensure!(
b.len() == 64 && b.is_ascii() && bytes.len() <= 65536 && signature.len() == 64,
"completion pin/size"
);
let key = (0..64)
.step_by(2)
.map(|i| u8::from_str_radix(&b[i..i + 2], 16))
.collect::<std::result::Result<Vec<_>, _>>()?;
ensure!(
DalekVerifier.verify(&key, bytes, signature)?,
"completion original signature"
);
Ok(())
}
fn receipt(raw: &[u8], binding: &Binding) -> Result<Receipt> {
ensure!(raw.len() <= 131072, "completion envelope size");
let signed: SignedReceipt = serde_json::from_slice(raw)?;
verify(binding, &signed.payload, &signed.signature)?;
let r: Receipt = serde_json::from_slice(&signed.payload)?;
ensure!(r.valid(r.issued_at), "completion historical profile");
let key = (0..64)
.step_by(2)
.map(|i| u8::from_str_radix(&binding.public_key_hex[i..i + 2], 16))
.collect::<std::result::Result<Vec<_>, _>>()?;
ensure!(r.key_id == hash(&key), "completion signing key");
Ok(r)
}
fn matches_original(r: &Receipt, scope: &ProbeScope, original: &[u8]) -> Result<()> {
let old: SignedReceipt = serde_json::from_slice(original)?;
let p: ProbeReceipt = serde_json::from_slice(&old.payload)?;
ensure!(
&r.witness.scope == scope
&& r.witness.probe_receipt_id == p.receipt_id
&& r.witness.probe_receipt_sha256 == hash(&old.payload)
&& r.witness.probe_signature_sha256 == hash(&old.signature),
"completion retained scope"
);
Ok(())
}
pub fn installed(
db: &Connection,
binding: &Binding,
scope: &ProbeScope,
original: &[u8],
) -> Result<bool> {
let exists: bool = db.query_row(
"SELECT EXISTS(SELECT 1 FROM sqlite_schema WHERE name=?)",
[TABLE],
|r| r.get(0),
)?;
if !exists {
return Ok(false);
}
let names = [
TABLE,
"windows_echo_completion_no_replace",
"windows_echo_completion_no_change",
"windows_echo_completion_no_delete",
];
for (name, expected) in names.iter().zip(schema()) {
let actual: String =
db.query_row("SELECT sql FROM sqlite_schema WHERE name=?", [name], |r| {
r.get(0)
})?;
ensure!(actual == expected, "completion schema");
}
let (raw, digest): (Vec<u8>, String) = db.query_row(
"SELECT original_receipt,witness_sha256 FROM windows_echo_scope_completion WHERE id=1",
[],
|r| Ok((r.get(0)?, r.get(1)?)),
)?;
let r = receipt(&raw, binding)?;
matches_original(&r, scope, original)?;
ensure!(digest == r.witness_sha256, "completion archive digest");
Ok(true)
}
/// Independent read of original native start, signatures and exact successful outcome.
pub fn witness(
db: &Connection,
consumption: &Connection,
helper: &Connection,
binding: &Binding,
helper_binding: &Binding,
) -> Result<Witness> {
let (scope, original) =
crate::echo_probe::archive(db, binding)?.context("original echo scope missing")?;
let old: SignedReceipt = serde_json::from_slice(&original)?;
verify(helper_binding, &old.payload, &old.signature)?;
let probe: ProbeReceipt = serde_json::from_slice(&old.payload)?;
ensure!(
(
helper_binding.tenant_id,
helper_binding.asset_id,
helper_binding.agent_id,
helper_binding.enrollment_generation,
helper_binding.assignment_version
) == (
binding.tenant_id,
binding.asset_id,
binding.agent_id,
binding.enrollment_generation,
binding.assignment_version
),
"completion independent helper identity"
);
for journal in [db, consumption, helper] {
let check: String = journal.query_row("PRAGMA integrity_check", [], |r| r.get(0))?;
ensure!(check == "ok", "completion journal integrity");
}
let fault: Option<String> =
db.query_row("SELECT fault FROM reconcile_meta WHERE id=1", [], |r| {
r.get(0)
})?;
ensure!(fault.is_none(), "current reconciliation fault");
let jobs: u64 = db.query_row("SELECT count(*) FROM job_attempts", [], |r| r.get(0))?;
ensure!(jobs == 1, "completion requires exact native single probe");
let (offer, state, grant): (Vec<u8>, String, String) = db.query_row(
"SELECT offer,state,grant_id FROM job_attempts WHERE job=? AND attempt=?",
params![scope.job_id.to_string(), scope.attempt_id.to_string()],
|r| Ok((r.get(0)?, r.get(1)?, r.get(2)?)),
)?;
let offer = JobOffer::decode(offer.as_slice())?;
let job: JobPayload = serde_json::from_slice(&offer.payload_bytes)?;
ensure!(
state == "succeeded" && crate::echo_probe::permits_offer(&scope, &offer, &job),
"native completed echo offer"
);
let count: u64 = consumption.query_row("SELECT count(*) FROM consumed", [], |r| r.get(0))?;
let evidence: u64 =
consumption.query_row("SELECT count(*) FROM execution_evidence", [], |r| r.get(0))?;
let helper_count: u64 = helper.query_row("SELECT count(*) FROM consumed", [], |r| r.get(0))?;
ensure!(
count == 1 && evidence == 1 && helper_count == 0,
"completion original consumption inventory"
);
let (start,outcome,result):(Vec<u8>,Vec<u8>,Vec<u8>)=consumption.query_row("SELECT e.start,e.outcome,c.result FROM consumed c JOIN execution_evidence e USING(grant_id) WHERE c.grant_id=? AND c.agent=? AND c.generation=? AND c.job=? AND c.attempt=?",params![grant,binding.agent_id.to_string(),binding.enrollment_generation,scope.job_id.to_string(),scope.attempt_id.to_string()],|r|Ok((r.get(0)?,r.get(1)?,r.get(2)?)))?;
let start: Value = serde_json::from_slice(&start)?;
let request: Request = serde_json::from_value(start["request"].clone())?;
verify(binding, &request.permit_bytes, &request.signature)?;
verify(helper_binding, &request.permit_bytes, &request.signature)?;
let g: ExecutionPermitPayload = serde_json::from_slice(&request.permit_bytes)?;
ensure!(
g.domain == "rmm.execution-permit"
&& g.schema_version == 1
&& g.agent_update_plan_id.is_none()
&& g.start_before - g.not_before <= chrono::Duration::seconds(60)
&& request.job_bytes == offer.payload_bytes
&& request.authorization_id.to_string() == offer.authorization_id
&& request.plan_hash == scope.plan_hash
&& g.grant_id.to_string() == grant
&& g.plan_hash == scope.plan_hash
&& g.authorization_id == request.authorization_id
&& g.job_payload_hash == scope.job_payload_hash
&& (g.tenant_id, g.asset_id, g.agent_id, g.job_id, g.attempt_id)
== (
scope.tenant_id,
scope.asset_id,
scope.agent_id,
scope.job_id,
scope.attempt_id
)
&& (g.enrollment_generation, g.assignment_version)
== (scope.enrollment_generation, scope.assignment_version)
&& g.execution_identity == "agent_service"
&& g.action == "run_script"
&& g.not_before >= scope.not_before
&& g.start_before <= scope.start_before
&& g.not_before < g.start_before
&& g.max_runtime_seconds == scope.max_runtime_seconds,
"completion retained signed grant"
);
let outcome: Value = serde_json::from_slice(&outcome)?;
let result: Value = serde_json::from_slice(&result)?;
ensure!(
outcome == result && successful_outcome(&outcome),
"completion exact native outcome"
);
let boot_id = serde_json::from_value(start["previous_boot_id"].clone())?;
let w = Witness {
scope,
probe_receipt_id: probe.receipt_id,
probe_receipt_sha256: hash(&old.payload),
probe_signature_sha256: hash(&old.signature),
grant_id: g.grant_id,
grant_payload_sha256: hash(&request.permit_bytes),
grant_signature_sha256: hash(&request.signature),
outcome_sha256: inventory_sha256(&outcome)?,
boot_id,
};
ensure!(w.valid(), "completion witness profile");
Ok(w)
}
pub struct LocalPair<'a> {
pub consumption: &'a Connection,
pub helper: &'a Connection,
pub binding: &'a Binding,
pub helper_binding: &'a Binding,
}
pub fn apply(
db: &mut Connection,
pair: LocalPair<'_>,
raw: &[u8],
boot: uuid::Uuid,
now: DateTime<Utc>,
) -> Result<()> {
let LocalPair {
consumption,
helper,
binding,
helper_binding,
} = pair;
let r = receipt(raw, binding)?;
let signed: SignedReceipt = serde_json::from_slice(raw)?;
verify(helper_binding, &signed.payload, &signed.signature)?;
let tx = db.transaction_with_behavior(TransactionBehavior::Immediate)?;
let expected = witness(&tx, consumption, helper, binding, helper_binding)?;
ensure!(
r.witness == expected && expected.boot_id == boot,
"completion differs from native evidence"
);
let (scope, original) = crate::echo_probe::archive(&tx, binding)?.context("original scope")?;
if installed(&tx, binding, &scope, &original)? {
let prior: Vec<u8> = tx.query_row(
"SELECT original_receipt FROM windows_echo_scope_completion WHERE id=1",
[],
|r| r.get(0),
)?;
ensure!(prior == raw, "different completion already applied");
return Ok(());
}
ensure!(r.valid(now), "completion expired or premature");
for sql in schema() {
tx.execute_batch(&sql)?;
}
tx.execute(
"INSERT INTO windows_echo_scope_completion VALUES(1,?,?,?)",
params![raw, r.witness_sha256, now.to_rfc3339()],
)?;
crate::echo_probe::retire_triggers(&tx)?;
ensure!(
crate::echo_probe::installed(&tx, binding)?.is_none(),
"completion did not retire additional scope"
);
tx.commit()?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use ed25519_dalek::{Signer, SigningKey};
use rmm_domain::{
jobs::{Action, PlanPayload, PlannedJob, WindowsExecutionProfile},
windows_echo_probe as echo,
};
use uuid::Uuid;
#[test]
fn scope_retirement_is_append_only_and_does_not_rewrite_history() -> Result<()> {
let key = SigningKey::from_bytes(&[24; 32]);
let now = Utc::now();
let binding = Binding {
tenant_id: echo::TENANT,
asset_id: echo::ASSET,
agent_id: echo::AGENT,
enrollment_generation: 1,
assignment_version: 1,
public_key_hex: key
.verifying_key()
.to_bytes()
.iter()
.map(|b| format!("{b:02x}"))
.collect(),
};
let job = JobPayload {
schema_version: 1,
tenant_id: echo::TENANT,
asset_id: echo::ASSET,
agent_id: echo::AGENT,
enrollment_generation: 1,
assignment_version: 1,
job_id: Uuid::new_v4(),
attempt_id: Uuid::new_v4(),
action: Action::RunScript {
interpreter: "powershell".into(),
script_utf8: echo::SCRIPT.into(),
arguments: vec![],
expected_stdout: Some(echo::OUTPUT.into()),
windows_execution_profile: Some(WindowsExecutionProfile::PowerShell51RemoteSigned),
privileged_script_profile: None,
},
execution_identity: "agent_service".into(),
not_before: now,
start_before: now + chrono::Duration::minutes(15),
max_runtime_seconds: 60,
max_output_bytes: 65536,
};
let job_bytes = serde_json::to_vec(&job)?;
let plan = PlanPayload {
schema_version: 1,
tenant_id: echo::TENANT,
plan_id: Uuid::new_v4(),
principal: format!("automation:{}", echo::TENANT),
reason: "fixture".into(),
jobs: vec![PlannedJob {
payload: job.clone(),
job_payload_hash: hash(&job_bytes),
script_sha256: Some(hash(echo::SCRIPT.as_bytes())),
}],
max_parallel: 1,
halt_after_failures: 1,
recipe_references: vec![],
script_revision: None,
artifact_references: vec![],
};
let scope = ProbeScope::from_plan(&plan, Uuid::new_v4()).context("fixture fixed scope")?;
let probe = ProbeReceipt {
domain: echo::DOMAIN.into(),
schema_version: 1,
request_id: Uuid::new_v4(),
receipt_id: Uuid::new_v4(),
witness: Value::Null,
witness_sha256: "0".repeat(64),
scope: scope.clone(),
scope_sha256: scope.digest().unwrap(),
job_payload_bytes: job_bytes.clone(),
key_id: hash(&key.verifying_key().to_bytes()),
issued_at: now,
valid_until: now + chrono::Duration::minutes(10),
confirmed_by: echo::TEST_PRINCIPAL.into(),
approval_origin: "dev_test_browser_simulation".into(),
testsimulation: true,
};
let probe_payload = serde_json::to_vec(&probe)?;
let old = SignedReceipt {
signature: key.sign(&probe_payload).to_bytes().to_vec(),
payload: probe_payload,
};
let original = serde_json::to_vec(&old)?;
let mut db = Connection::open_in_memory()?;
db.execute_batch("CREATE TABLE job_attempts(job TEXT,attempt TEXT,offer BLOB,state TEXT);CREATE TABLE original_history(raw BLOB);INSERT INTO original_history VALUES(x'00ff2a')")?;
let tx = db.transaction()?;
crate::echo_probe::install(&tx, &scope, &job_bytes, &original)?;
tx.commit()?;
let w = Witness {
scope: scope.clone(),
probe_receipt_id: probe.receipt_id,
probe_receipt_sha256: hash(&old.payload),
probe_signature_sha256: hash(&old.signature),
grant_id: Uuid::new_v4(),
grant_payload_sha256: "1".repeat(64),
grant_signature_sha256: "2".repeat(64),
outcome_sha256: "3".repeat(64),
boot_id: Uuid::new_v4(),
};
let r = Receipt {
domain: rmm_domain::windows_echo_completion::DOMAIN.into(),
schema_version: 1,
receipt_id: Uuid::new_v4(),
witness_sha256: w.digest().unwrap(),
witness: w,
key_id: hash(&key.verifying_key().to_bytes()),
issued_at: now,
valid_until: now + chrono::Duration::minutes(10),
confirmed_by: echo::TEST_PRINCIPAL.into(),
approval_origin: "dev_test_browser_simulation".into(),
testsimulation: true,
ready: false,
helper_ready: false,
unlock: false,
};
let seal = |r: &Receipt| -> Result<Vec<u8>> {
let payload = serde_json::to_vec(r)?;
Ok(serde_json::to_vec(&SignedReceipt {
signature: key.sign(&payload).to_bytes().to_vec(),
payload,
})?)
};
let raw = seal(&r)?;
for field in 0..4 {
let mut bad = r.clone();
match field {
0 => bad.ready = true,
1 => bad.helper_ready = true,
2 => bad.unlock = true,
_ => bad.testsimulation = false,
};
assert!(receipt(&seal(&bad)?, &binding).is_err());
}
assert!(
receipt(
&raw,
&Binding {
public_key_hex: "00".repeat(32),
..binding.clone()
}
)
.is_err()
);
assert!(!installed(&db, &binding, &scope, &original)?);
let tx = db.transaction()?;
for sql in schema() {
tx.execute_batch(&sql)?;
}
tx.execute(
"INSERT INTO windows_echo_scope_completion VALUES(1,?,?,?)",
params![raw, r.witness_sha256, now.to_rfc3339()],
)?;
crate::echo_probe::retire_triggers(&tx)?;
tx.commit()?;
assert!(installed(&db, &binding, &scope, &original)?);
crate::echo_probe::check(&db, &scope, &job_bytes, &original)?;
assert_eq!(
db.query_row("SELECT raw FROM original_history", [], |r| r
.get::<_, Vec<u8>>(0))?,
vec![0, 255, 42]
);
for sql in [
"DELETE FROM windows_echo_scope_completion",
"UPDATE windows_echo_scope_completion SET applied_at='other'",
"INSERT OR REPLACE INTO windows_echo_scope_completion SELECT * FROM windows_echo_scope_completion",
"DELETE FROM windows_echo_probe_scope",
] {
assert!(db.execute(sql, []).is_err());
}
let tx = db.transaction()?;
tx.execute_batch("DROP TRIGGER windows_echo_completion_no_change")?;
assert!(installed(&tx, &binding, &scope, &original).is_err());
tx.rollback()?;
let mut changed = scope.clone();
changed.attempt_id = Uuid::new_v4();
assert!(installed(&db, &binding, &changed, &original).is_err());
Ok(())
}
}