334 lines
14 KiB
Rust
334 lines
14 KiB
Rust
//! Operator-provisioned native qualification intersected with a live authenticated helper.
|
|
//! A source build or a permissive policy by itself cannot publish SYSTEM capabilities.
|
|
use crate::{
|
|
helper_ipc::{self as ipc, Pin, PinnedFile},
|
|
helper_system,
|
|
};
|
|
use anyhow::{Result, ensure};
|
|
use serde::Deserialize;
|
|
use std::{
|
|
collections::BTreeSet,
|
|
io::Read,
|
|
path::{Path, PathBuf},
|
|
};
|
|
use uuid::Uuid;
|
|
|
|
pub const PATH: &str = r"C:\ProgramData\RustDiscoverySystem\system-qualification.json";
|
|
pub const RESULT_PATH: &str = r"C:\ProgramData\RustDiscoverySystem\system-native-result.json";
|
|
#[derive(Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
struct Qualification {
|
|
schema_version: u32,
|
|
tenant_id: Uuid,
|
|
asset_id: Uuid,
|
|
agent_id: Uuid,
|
|
enrollment_generation: u64,
|
|
assignment_version: u64,
|
|
agent_binary_sha256: String,
|
|
helper_binary_sha256: String,
|
|
policy_sha256: String,
|
|
native_result_sha256: String,
|
|
qualification_reference: Uuid,
|
|
actions: Vec<String>,
|
|
}
|
|
pub fn qualified(
|
|
pin: &Pin,
|
|
clock_qualified: bool,
|
|
) -> Result<Vec<String>> {
|
|
if !clock_qualified {
|
|
return Ok(vec![]);
|
|
}
|
|
pin.validate()?;
|
|
let (settings, _handles) = helper_system::read_settings()?;
|
|
ensure!(serde_json::to_vec(&settings.pin()?)? == serde_json::to_vec(pin)?, "system qualification pin");
|
|
if settings.windows_update_only() || settings.managed_scripts() {
|
|
// The caller has verified the live enrolled peer and its independent clock.
|
|
let mut caps = vec![
|
|
"helper.system.v1".into(),
|
|
format!("helper.binary.sha256:{}", pin.helper.binary_sha256),
|
|
format!("helper.policy.sha256:{}", pin.policy_sha256),
|
|
"system:run_script".into(),
|
|
];
|
|
if settings.windows_update_max_runtime_seconds.is_some() {
|
|
caps.push(rmm_domain::jobs::WINDOWS_UPDATE_LONG_CAPABILITY.into());
|
|
}
|
|
if settings.managed_scripts() {
|
|
caps.push(rmm_domain::jobs::FULL_MAINTENANCE_CAPABILITY.into());
|
|
if let Ok(id) = crate::release_service::managed_script_release(pin) {
|
|
caps.push(format!("script.helper.release:{id}"));
|
|
}
|
|
}
|
|
return Ok(caps);
|
|
}
|
|
let ordinary = load_ordinary(pin);
|
|
let (q, result_bytes) = match ordinary {
|
|
Ok(sources) => sources,
|
|
Err(error) => match load_catalog(pin) {
|
|
Ok(sources) => sources,
|
|
Err(catalog_error) => {
|
|
if !Path::new(PATH).try_exists()? && !catalog_directory(pin)?.try_exists()? {
|
|
return Ok(vec![]);
|
|
}
|
|
if catalog_directory(pin)?.try_exists()? {
|
|
return Err(catalog_error);
|
|
}
|
|
return Err(error);
|
|
}
|
|
},
|
|
};
|
|
let evidence: serde_json::Value = serde_json::from_slice(&result_bytes)?;
|
|
ensure!(
|
|
evidence["schema_version"] == 1
|
|
&& evidence["platform"] == "windows-x86_64"
|
|
&& evidence["qualification_reference"] == q.qualification_reference.to_string()
|
|
&& evidence["helper_binary_sha256"] == q.helper_binary_sha256
|
|
&& evidence["validated_actions"] == serde_json::to_value(&q.actions)?
|
|
&& evidence["status"] == "passed",
|
|
"system native qualification evidence profile"
|
|
);
|
|
ensure!(
|
|
serde_json::to_vec(&settings.pin()?)? == serde_json::to_vec(pin)?
|
|
&& settings.enable_effects,
|
|
"system qualification effective policy"
|
|
);
|
|
if !q.actions.iter().any(|action| action == "run_script") {
|
|
return Ok(vec![]);
|
|
}
|
|
let general = settings.allowed_script_profiles.as_deref() ==
|
|
Some(&[rmm_domain::jobs::PrivilegedScriptProfile::FullMaintenanceV1]);
|
|
let windows_update = settings.windows_update_max_runtime_seconds.is_some();
|
|
ensure!(settings.schema_version >= 3 && (general || windows_update), "system script qualification policy");
|
|
let mut caps = vec![
|
|
"helper.system.v1".into(),
|
|
format!("helper.binary.sha256:{}", pin.helper.binary_sha256),
|
|
format!("helper.policy.sha256:{}", pin.policy_sha256),
|
|
"system:run_script".into(),
|
|
];
|
|
if general { caps.push(rmm_domain::jobs::FULL_MAINTENANCE_CAPABILITY.into()); }
|
|
if windows_update { caps.push(rmm_domain::jobs::WINDOWS_UPDATE_LONG_CAPABILITY.into()); }
|
|
Ok(caps)
|
|
}
|
|
fn read(path: &Path, max: u64) -> Result<Vec<u8>> {
|
|
let readers = [
|
|
ipc::service_sid(crate::SERVICE_NAME)?,
|
|
ipc::service_sid(ipc::HELPER_SERVICE)?,
|
|
];
|
|
let mut dir = PinnedFile::open_read_shared(
|
|
path.parent().expect("fixed qualification parent"),
|
|
true,
|
|
&readers,
|
|
&[],
|
|
)?;
|
|
let mut file = PinnedFile::open(path, false, &readers, &[], false)?;
|
|
ensure!(
|
|
file.file.metadata()?.len() <= max,
|
|
"system qualification file limit"
|
|
);
|
|
let mut raw = vec![];
|
|
(&mut file.file).take(max + 1).read_to_end(&mut raw)?;
|
|
ensure!(raw.len() as u64 <= max, "system qualification file limit");
|
|
file.check()?;
|
|
dir.check()?;
|
|
Ok(raw)
|
|
}
|
|
fn validate(q: &Qualification, pin: &Pin) -> Result<()> {
|
|
ensure!(
|
|
q.schema_version == 1
|
|
&& (
|
|
q.tenant_id,
|
|
q.asset_id,
|
|
q.agent_id,
|
|
q.enrollment_generation,
|
|
q.assignment_version
|
|
) == (
|
|
pin.binding.tenant_id,
|
|
pin.binding.asset_id,
|
|
pin.binding.agent_id,
|
|
pin.binding.enrollment_generation,
|
|
pin.binding.assignment_version
|
|
)
|
|
&& q.agent_binary_sha256 == pin.agent.binary_sha256
|
|
&& q.helper_binary_sha256 == pin.helper.binary_sha256
|
|
&& q.policy_sha256 == pin.policy_sha256
|
|
&& ipc::digest(&q.native_result_sha256)
|
|
&& !q.qualification_reference.is_nil()
|
|
&& !q.actions.is_empty()
|
|
&& q.actions.len() <= 4
|
|
&& q.actions.iter().collect::<BTreeSet<_>>().len() == q.actions.len()
|
|
&& q.actions.iter().all(|a| matches!(
|
|
a.as_str(),
|
|
"run_script" | "set_service_state" | "ensure_package" | "reboot"
|
|
)),
|
|
"system qualification binding"
|
|
);
|
|
Ok(())
|
|
}
|
|
fn load_ordinary(pin: &Pin) -> Result<(Qualification, Vec<u8>)> {
|
|
let raw = read(Path::new(PATH), 16384)?;
|
|
let q: Qualification = serde_json::from_slice(&raw)?;
|
|
validate(&q, pin)?;
|
|
let result = read(Path::new(RESULT_PATH), 65536)?;
|
|
ensure!(
|
|
rmm_domain::jobs::hash(&result) == q.native_result_sha256,
|
|
"system native qualification evidence hash"
|
|
);
|
|
Ok((q, result))
|
|
}
|
|
/// Fixed per-device MVP catalog, provisioned only from actual qualification.
|
|
/// It is public metadata/deny-only capability evidence, never a job lease.
|
|
pub fn catalog_directory(pin: &Pin) -> Result<PathBuf> {
|
|
ensure!(
|
|
ipc::digest(&pin.agent.binary_sha256)
|
|
&& ipc::digest(&pin.helper.binary_sha256)
|
|
&& ipc::digest(&pin.policy_sha256),
|
|
"system catalog digest"
|
|
);
|
|
let key = rmm_domain::jobs::hash(&serde_json::to_vec(&(
|
|
&pin.agent.binary_sha256,
|
|
&pin.helper.binary_sha256,
|
|
&pin.policy_sha256,
|
|
))?);
|
|
Ok(Path::new(r"C:\ProgramData\RustDiscoverySystem\system-qualification-catalog").join(key))
|
|
}
|
|
#[derive(Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
struct CatalogEvidence {
|
|
schema_version: u32,
|
|
source_manifest_sha256: String,
|
|
qualification_sha256: String,
|
|
native_result_sha256: String,
|
|
start_witness_sha256: String,
|
|
outcome_sha256: String,
|
|
approval_audit_sha256: String,
|
|
plan_id: Uuid,
|
|
plan_sha256: String,
|
|
job_id: Uuid,
|
|
attempt_id: Uuid,
|
|
grant_id: Uuid,
|
|
authorization_id: Uuid,
|
|
operation_id: Uuid,
|
|
approval_origin: String,
|
|
testsimulation: bool,
|
|
}
|
|
fn load_catalog(pin: &Pin) -> Result<(Qualification, Vec<u8>)> {
|
|
use rmm_domain::jobs::hash;
|
|
let directory = catalog_directory(pin)?;
|
|
let q_raw = read(&directory.join("qualification.json"), 16384)?;
|
|
let q: Qualification = serde_json::from_slice(&q_raw)?;
|
|
validate(&q, pin)?;
|
|
let result = read(&directory.join("native-result.json"), 65536)?;
|
|
let source_manifest = read(&directory.join("source-manifest.json"), 512 * 1024)?;
|
|
let source_document:serde_json::Value=serde_json::from_slice(&source_manifest)?;
|
|
ensure!(source_document.as_array().is_some_and(|a|!a.is_empty())
|
|
|| source_document.as_object().is_some_and(|a|!a.is_empty()),"system catalog source manifest");
|
|
let evidence_raw = read(&directory.join("prequalification.json"), 16384)?;
|
|
let e: CatalogEvidence = serde_json::from_slice(&evidence_raw)?;
|
|
ensure!(
|
|
e.schema_version == 1
|
|
&& e.source_manifest_sha256 == hash(&source_manifest)
|
|
&& e.qualification_sha256 == hash(&q_raw)
|
|
&& e.native_result_sha256 == hash(&result)
|
|
&& e.native_result_sha256 == q.native_result_sha256
|
|
&& [
|
|
e.plan_id,
|
|
e.job_id,
|
|
e.attempt_id,
|
|
e.grant_id,
|
|
e.authorization_id,
|
|
e.operation_id
|
|
]
|
|
.iter()
|
|
.all(|id| !id.is_nil())
|
|
&& ipc::digest(&e.plan_sha256)
|
|
&& matches!(
|
|
(e.testsimulation, e.approval_origin.as_str()),
|
|
(true, "dev_test_browser_simulation") | (false, "human_webui")
|
|
),
|
|
"system catalog qualification provenance"
|
|
);
|
|
let start = read(&directory.join("start-witness.json"), 512 * 1024)?;
|
|
let outcome = read(&directory.join("powershell-outcome.json"), 128 * 1024)?;
|
|
let audit = read(&directory.join("approval-audit.json"), 65536)?;
|
|
ensure!(
|
|
hash(&start) == e.start_witness_sha256
|
|
&& hash(&outcome) == e.outcome_sha256
|
|
&& hash(&audit) == e.approval_audit_sha256,
|
|
"system catalog actual evidence bytes"
|
|
);
|
|
let start_value: serde_json::Value = serde_json::from_slice(&start)?;
|
|
let request: rmm_agent_core::auth::Request =
|
|
serde_json::from_value(start_value["request"].clone())?;
|
|
let at: chrono::DateTime<chrono::Utc> =
|
|
serde_json::from_value(start_value["reserved_at"].clone())?;
|
|
let boot: Uuid = serde_json::from_value(start_value["previous_boot_id"].clone())?;
|
|
let (job, permit) = pin.binding.verify(&request, at)?;
|
|
ensure!(
|
|
rmm_agent_core::ledger::start_witness_bytes(&permit, &request, boot, at)? == start
|
|
&& (
|
|
job.job_id,
|
|
job.attempt_id,
|
|
permit.grant_id,
|
|
permit.authorization_id
|
|
) == (e.job_id, e.attempt_id, e.grant_id, e.authorization_id)
|
|
&& permit.plan_hash == e.plan_sha256
|
|
&& job.execution_identity == "system"
|
|
&& job.action.full_maintenance(),
|
|
"system catalog actual original SYSTEM job"
|
|
);
|
|
let rmm_domain::jobs::Action::RunScript {
|
|
interpreter,
|
|
script_utf8,
|
|
..
|
|
} = &job.action
|
|
else {
|
|
anyhow::bail!("system catalog PowerShell qualification required")
|
|
};
|
|
let parsed: rmm_domain::reconciliation::ReconciledOutcome = serde_json::from_slice(&outcome)?;
|
|
ensure!(
|
|
rmm_domain::reconciliation::canonical_outcome_bytes(&parsed)? == outcome
|
|
&& parsed.state == "succeeded"
|
|
&& parsed.exit_code == Some(0)
|
|
&& parsed.error_code.is_empty()
|
|
&& !parsed.output_truncated
|
|
&& !parsed.reboot_required
|
|
&& interpreter == "powershell"
|
|
&& parsed.evidence["postconditions_passed"] == true
|
|
&& parsed.evidence["execution_sid"] == "S-1-5-18"
|
|
&& parsed.evidence["privileged_script_profile"] == "full_maintenance_v1"
|
|
&& parsed.evidence["script_sha256"] == hash(script_utf8.as_bytes())
|
|
&& parsed.evidence["execution_context"]["schema_version"] == 1
|
|
&& parsed.evidence["execution_context"]["tenant_id"]
|
|
== pin.binding.tenant_id.to_string()
|
|
&& parsed.evidence["execution_context"]["asset_id"] == pin.binding.asset_id.to_string()
|
|
&& parsed.evidence["execution_context"]["agent_id"] == pin.binding.agent_id.to_string()
|
|
&& parsed.evidence["execution_context"]["agent_binary_sha256"]
|
|
== pin.agent.binary_sha256
|
|
&& parsed.evidence["execution_context"]["helper_binary_sha256"]
|
|
== pin.helper.binary_sha256
|
|
&& parsed.evidence["execution_context"]["policy_sha256"] == pin.policy_sha256
|
|
&& parsed.evidence["execution_context"]["binding_sha256"]
|
|
== ipc::binding_hash(&pin.binding)?
|
|
&& parsed.evidence["execution_context"]["rechecked_at_finish"] == true,
|
|
"system catalog actual PowerShell outcome"
|
|
);
|
|
let audit: serde_json::Value = serde_json::from_slice(&audit)?;
|
|
ensure!(
|
|
audit["tenant_id"] == pin.binding.tenant_id.to_string()
|
|
&& audit["event_type"] == "plan_authorization"
|
|
&& audit["event_id"]
|
|
.as_str()
|
|
.is_some_and(|s| Uuid::parse_str(s).is_ok_and(|id| !id.is_nil()))
|
|
&& audit["principal"]
|
|
.as_str()
|
|
.is_some_and(|s| !s.is_empty() && s.len() <= 256)
|
|
&& audit["data"]["plan_id"] == e.plan_id.to_string()
|
|
&& audit["data"]["plan_hash"] == e.plan_sha256
|
|
&& audit["data"]["operation_id"] == e.operation_id.to_string()
|
|
&& audit["data"]["decision"] == "approve"
|
|
&& audit["data"]["approval_origin"] == e.approval_origin
|
|
&& audit["data"]["testsimulation"] == e.testsimulation,
|
|
"system catalog actual WebUI approval audit"
|
|
);
|
|
Ok((q, result))
|
|
}
|