246 lines
10 KiB
Rust
246 lines
10 KiB
Rust
//! Administrative typed metadata only; no writes, effects, jobs, or readiness.
|
|
#[cfg(all(windows, feature = "system-helper"))]
|
|
mod sdk {
|
|
use anyhow::{Result, ensure};
|
|
use rmm_agent_windows::{helper_ipc as ipc, helper_system, signed_clock, update_state};
|
|
use rmm_domain::{
|
|
agent_update::{ReleaseEnvelope, ReleaseManifest, UpdateKeyringFile},
|
|
agent_update_provisioning::{HealthKeyPin, InstalledReleaseRecord, LocalUpdatePolicy},
|
|
jobs::hash,
|
|
};
|
|
use serde::Deserialize;
|
|
use std::io::Read;
|
|
#[derive(Deserialize)]
|
|
#[serde(tag = "operation", rename_all = "snake_case", deny_unknown_fields)]
|
|
enum Input {
|
|
ReadMetadata {
|
|
updater_sha256: Option<String>,
|
|
},
|
|
RenderPair {
|
|
settings: Vec<u8>,
|
|
agent_sha256: String,
|
|
helper_sha256: String,
|
|
},
|
|
RenderUpdaterClock {
|
|
pair_profile: Vec<u8>,
|
|
updater_sha256: String,
|
|
policy_sha256: String,
|
|
service_configuration_sha256: String,
|
|
qualification_sha256: String,
|
|
},
|
|
CanonicalRelease {
|
|
manifest: ReleaseManifest,
|
|
},
|
|
CanonicalRecord {
|
|
record: InstalledReleaseRecord,
|
|
},
|
|
CanonicalPolicy {
|
|
policy: Box<LocalUpdatePolicy>,
|
|
},
|
|
ValidatePublicBundle {
|
|
record: Vec<u8>,
|
|
policy: Vec<u8>,
|
|
keyring: Vec<u8>,
|
|
health_pin: Vec<u8>,
|
|
},
|
|
ValidateRelease {
|
|
envelope: ReleaseEnvelope,
|
|
},
|
|
}
|
|
fn bytes(raw: Vec<u8>) -> serde_json::Value {
|
|
serde_json::json!({"sha256":hash(&raw),"bytes":raw})
|
|
}
|
|
fn read_metadata(updater_sha256: Option<String>) -> Result<serde_json::Value> {
|
|
use rmm_agent_core::helper_reconcile::BootSource;
|
|
use rmm_agent_windows::update_native::{Budget, Role, files, services};
|
|
let boot = helper_system::WindowsBoot::capture()?;
|
|
let budget = Budget::new(
|
|
&boot,
|
|
boot.current_boot()?,
|
|
std::time::Duration::from_secs(30),
|
|
)?;
|
|
let (settings, _held) = helper_system::read_settings()?;
|
|
let mut running =
|
|
services::Running::observe([settings.agent.clone(), settings.helper.clone()], &budget)?;
|
|
let retained = update_state::RetainedSet::bootstrap_metadata()?;
|
|
let mut observed = Vec::new();
|
|
for role in [
|
|
Role::Agent,
|
|
Role::Helper,
|
|
Role::Settings,
|
|
Role::Pin,
|
|
Role::TimeProfile,
|
|
] {
|
|
observed.push(serde_json::json!({"role":role,"proof":files::observe(role,&budget)?}));
|
|
}
|
|
let updater = if let Some(sha) = updater_sha256 {
|
|
ensure!(ipc::digest(&sha), "BOOTSTRAP_UPDATER_IMAGE");
|
|
let pin = ipc::PeerPin {
|
|
service_name: rmm_agent_windows::update_ipc::SERVICE.into(),
|
|
service_sid: ipc::service_sid(rmm_agent_windows::update_ipc::SERVICE)?,
|
|
user_sid: "S-1-5-18".into(),
|
|
binary: rmm_agent_windows::update_ipc::BINARY.into(),
|
|
binary_sha256: sha,
|
|
};
|
|
let readers = [pin.service_sid.clone()];
|
|
let mut image = ipc::PinnedFile::open(&pin.binary, false, &readers, &[], false)?;
|
|
ensure!(
|
|
image.sha256()? == pin.binary_sha256,
|
|
"BOOTSTRAP_REGISTERED_UPDATER_IMAGE"
|
|
);
|
|
let definition = services::updater_bootstrap_definition(&pin)?;
|
|
image.check()?;
|
|
Some(
|
|
serde_json::json!({"pin":pin,"configuration_sha256":hash(&serde_json::to_vec(&definition)?),"configuration":definition,"running_not_qualified":true}),
|
|
)
|
|
} else {
|
|
None
|
|
};
|
|
running.check(&budget)?;
|
|
Ok(
|
|
serde_json::json!({"schema_version":1,"read_only":true,"boot_id":boot.current_boot()?,
|
|
"binding":settings.binding,"services":running.proofs(),"files":observed,"retained":retained,
|
|
"reader_contract_sha256":hash(update_state::READER_CONTRACT),"updater":updater,
|
|
"readiness_created":false,"target_mutated":false,"qualification_created":false}),
|
|
)
|
|
}
|
|
fn run(input: Input) -> Result<serde_json::Value> {
|
|
Ok(match input {
|
|
Input::ReadMetadata { updater_sha256 } => read_metadata(updater_sha256)?,
|
|
Input::RenderPair {
|
|
settings,
|
|
agent_sha256,
|
|
helper_sha256,
|
|
} => {
|
|
ensure!(
|
|
ipc::digest(&agent_sha256) && ipc::digest(&helper_sha256),
|
|
"BOOTSTRAP_PAIR_DIGESTS"
|
|
);
|
|
let mut s: helper_system::Settings = serde_json::from_slice(&settings)?;
|
|
s.validate()?;
|
|
s.agent.binary_sha256 = agent_sha256;
|
|
s.helper.binary_sha256 = helper_sha256;
|
|
s.validate()?;
|
|
let raw = serde_json::to_vec(&s)?;
|
|
let pin = s.pin()?;
|
|
ensure!(pin.policy_sha256 == hash(&raw), "BOOTSTRAP_CANONICAL_PAIR");
|
|
serde_json::json!({"settings":bytes(raw),"pin":bytes(serde_json::to_vec(&pin)?),"shape_only":true})
|
|
}
|
|
Input::RenderUpdaterClock {
|
|
pair_profile,
|
|
updater_sha256,
|
|
policy_sha256,
|
|
service_configuration_sha256,
|
|
qualification_sha256,
|
|
} => {
|
|
ensure!(
|
|
[
|
|
&updater_sha256,
|
|
&policy_sha256,
|
|
&service_configuration_sha256,
|
|
&qualification_sha256
|
|
]
|
|
.iter()
|
|
.all(|s| ipc::digest(s)),
|
|
"BOOTSTRAP_UPDATER_CLOCK_DIGESTS"
|
|
);
|
|
let profile: signed_clock::Configuration = serde_json::from_slice(&pair_profile)?;
|
|
let signed_clock::Configuration::TrustedOs(p) = profile else {
|
|
anyhow::bail!("BOOTSTRAP_EXPLICIT_ACTIVE_GUEST_PROFILE_REQUIRED")
|
|
};
|
|
ensure!(
|
|
p.schema_version == 2
|
|
&& p.trusted_os_active_guest.qualifications.len() == 2
|
|
&& p.trusted_os_active_guest.qualifications[0].role
|
|
== rmm_time_attestation::Role::Agent
|
|
&& p.trusted_os_active_guest.qualifications[1].role
|
|
== rmm_time_attestation::Role::SystemHelper,
|
|
"BOOTSTRAP_ORIGINAL_PAIR_PROFILE"
|
|
);
|
|
let mut guest = p.trusted_os_active_guest;
|
|
guest.qualifications = vec![signed_clock::RoleQualification {
|
|
role: rmm_time_attestation::Role::Updater,
|
|
binary_sha256: updater_sha256,
|
|
qualification_sha256,
|
|
}];
|
|
let new = signed_clock::UpdaterConfiguration {
|
|
schema_version: 3,
|
|
key_generation: p.key_generation,
|
|
server_instance: p.server_instance,
|
|
binding: p.binding,
|
|
public_keys_hex: p.public_keys_hex,
|
|
updater_policy_sha256: policy_sha256,
|
|
updater_service_configuration_sha256: service_configuration_sha256,
|
|
trusted_os_active_guest: guest,
|
|
};
|
|
serde_json::json!({"profile":bytes(serde_json::to_vec(&new)?),"shape_only":true,"qualification_created":false})
|
|
}
|
|
Input::CanonicalRelease { manifest } => {
|
|
ensure!(manifest.validate(), "BOOTSTRAP_RELEASE_SHAPE");
|
|
bytes(serde_json::to_vec(&manifest)?)
|
|
}
|
|
Input::CanonicalRecord { record } => bytes(record.bytes().map_err(anyhow::Error::msg)?),
|
|
Input::CanonicalPolicy { policy } => {
|
|
ensure!(
|
|
policy.validate() && policy.schema_version == 2,
|
|
"BOOTSTRAP_WINDOWS_POLICY_SHAPE"
|
|
);
|
|
bytes(serde_json::to_vec(&policy)?)
|
|
}
|
|
Input::ValidateRelease { envelope } => {
|
|
let decoded = envelope.decode().map_err(anyhow::Error::msg)?;
|
|
serde_json::json!({"manifest":decoded.manifest,"manifest_sha256":hash(&decoded.payload_bytes),"shape_only_not_signature_verified":true})
|
|
}
|
|
Input::ValidatePublicBundle {
|
|
record,
|
|
policy,
|
|
keyring,
|
|
health_pin,
|
|
} => {
|
|
let installed =
|
|
InstalledReleaseRecord::from_bytes(&record).map_err(anyhow::Error::msg)?;
|
|
let (policy, policy_sha256) =
|
|
LocalUpdatePolicy::from_bytes(&policy).map_err(anyhow::Error::msg)?;
|
|
let keys: UpdateKeyringFile = serde_json::from_slice(&keyring)?;
|
|
let health: HealthKeyPin = serde_json::from_slice(&health_pin)?;
|
|
ensure!(
|
|
keys.validate()
|
|
&& health.validate()
|
|
&& policy.schema_version == 2
|
|
&& policy.initial_record_sha256 == hash(&record)
|
|
&& policy.initial_activation_floor == installed.activation_floor,
|
|
"BOOTSTRAP_WINDOWS_PUBLIC_BUNDLE_SHAPE"
|
|
);
|
|
serde_json::json!({"record_sha256":hash(&record),"policy_sha256":policy_sha256,"keyring_sha256":hash(&keyring),"health_pin_sha256":hash(&health_pin),"shape_only_not_signature_verified":true})
|
|
}
|
|
})
|
|
}
|
|
pub fn main() -> Result<()> {
|
|
let mut raw = Vec::new();
|
|
std::io::stdin()
|
|
.take(512 * 1024 + 1)
|
|
.read_to_end(&mut raw)?;
|
|
ensure!(
|
|
!raw.is_empty() && raw.len() <= 512 * 1024,
|
|
"BOOTSTRAP_METADATA_FRAME_LIMIT"
|
|
);
|
|
println!(
|
|
"{}",
|
|
serde_json::to_string(&run(serde_json::from_slice(&raw)?)?)?
|
|
);
|
|
Ok(())
|
|
}
|
|
}
|
|
#[cfg(all(windows, feature = "system-helper"))]
|
|
fn main() {
|
|
if sdk::main().is_err() {
|
|
eprintln!("WINDOWS_BOOTSTRAP_METADATA_FAILED");
|
|
std::process::exit(1)
|
|
}
|
|
}
|
|
#[cfg(not(all(windows, feature = "system-helper")))]
|
|
fn main() {
|
|
eprintln!("Windows/system-helper required; no operation executed");
|
|
std::process::exit(2)
|
|
}
|