205 lines
8.4 KiB
Rust
205 lines
8.4 KiB
Rust
//! Independently reverify the retained ORIGINAL request, never an IPC-supplied
|
|
//! approval bit, a replacement request or a renewed start window.
|
|
use anyhow::{Result, ensure};
|
|
use rmm_agent_core::{
|
|
auth::{Binding, Request},
|
|
reconcile::SyncClock,
|
|
};
|
|
use rmm_domain::{
|
|
agent_update::{ParticipantRole, ResolvedAgentUpdate, UpdateDelegation},
|
|
jobs::{Action, ExecutionPermitPayload, JobPayload, hash},
|
|
};
|
|
use rmm_updater_handoff::{Handoff, PinTransition, Service};
|
|
use rmm_updater_linux::Budget;
|
|
|
|
pub struct Original {
|
|
request: Request,
|
|
permit: ExecutionPermitPayload,
|
|
job: JobPayload,
|
|
update: ResolvedAgentUpdate,
|
|
handoff: Handoff,
|
|
delegation: UpdateDelegation,
|
|
start_bytes: Vec<u8>,
|
|
}
|
|
impl Original {
|
|
/// `start_bytes` must come from execution_evidence.start in the SAME
|
|
/// existing guarded ledger. This is validation only; callers must re-read
|
|
/// and compare that immutable row at each subsequent guarded operation.
|
|
pub fn verify(
|
|
binding: &Binding,
|
|
start_bytes: &[u8],
|
|
record: &[u8],
|
|
delegation: UpdateDelegation,
|
|
clock: &impl SyncClock,
|
|
budget: &Budget,
|
|
) -> Result<Self> {
|
|
let result = Self::verify_retained(binding, start_bytes, record, delegation)?;
|
|
result.check_current(binding, clock, budget)?;
|
|
Ok(result)
|
|
}
|
|
/// Historical evidence ONLY. Never supplies current boot/time/owner trust,
|
|
/// health authority, a new lease or permission for any native effect.
|
|
pub fn verify_retained(
|
|
binding: &Binding,
|
|
start_bytes: &[u8],
|
|
record: &[u8],
|
|
delegation: UpdateDelegation,
|
|
) -> Result<Self> {
|
|
ensure!(
|
|
!start_bytes.is_empty()
|
|
&& start_bytes.len() <= 512 * 1024
|
|
&& !record.is_empty()
|
|
&& record.len() <= rmm_updater_handoff::MAX_RECORD_BYTES,
|
|
"UPDATE_ORIGINAL_LIMIT"
|
|
);
|
|
let handoff: Handoff = serde_json::from_slice(record)?;
|
|
handoff.validate()?;
|
|
ensure!(
|
|
handoff.bytes()? == record
|
|
&& delegation.validate()
|
|
&& delegation.handoff_sha256 == hash(record)
|
|
&& delegation.owner_nonce == handoff.owner_nonce
|
|
&& delegation.boot_id == handoff.boot_id
|
|
&& delegation.updater.role == ParticipantRole::LinuxUpdater
|
|
&& delegation.updater.image_sha256 == handoff.updater_sha256
|
|
&& delegation.original_helper.image_sha256 == handoff.predecessor.helper_sha256
|
|
&& u64::try_from(delegation.prepared_at.timestamp())? == handoff.consumed_at,
|
|
"UPDATE_ORIGINAL_DELEGATION"
|
|
);
|
|
let start: serde_json::Value = serde_json::from_slice(start_bytes)?;
|
|
let request: Request = serde_json::from_value(start["request"].clone())?;
|
|
ensure!(
|
|
hash(start_bytes) == handoff.attempt.original_start_witness_sha256
|
|
&& hash(&serde_json::to_vec(&request)?) == handoff.attempt.original_request_sha256,
|
|
"UPDATE_ORIGINAL_START_CHANGED"
|
|
);
|
|
// Original consumption happened at the immutable prepared_at. It must
|
|
// have had a valid live start grant THEN. Current checks below constrain
|
|
// this one already consumed attempt; they grant no new start window.
|
|
let (job, permit) = binding.verify(&request, delegation.prepared_at)?;
|
|
ensure!(
|
|
rmm_agent_core::ledger::start_witness_bytes(
|
|
&permit,
|
|
&request,
|
|
handoff.boot_id,
|
|
delegation.prepared_at
|
|
)? == start_bytes,
|
|
"UPDATE_ORIGINAL_START_CHANGED"
|
|
);
|
|
ensure!(
|
|
job.execution_identity == "root"
|
|
&& permit.action == "update_agent"
|
|
&& Some(handoff.attempt.plan_id) == permit.agent_update_plan_id
|
|
&& handoff.attempt.tenant_id == permit.tenant_id
|
|
&& handoff.attempt.asset_id == permit.asset_id
|
|
&& handoff.attempt.agent_id == permit.agent_id
|
|
&& handoff.attempt.enrollment_generation == permit.enrollment_generation
|
|
&& handoff.attempt.assignment_version == permit.assignment_version
|
|
&& handoff.attempt.job_id == permit.job_id
|
|
&& handoff.attempt.attempt_id == permit.attempt_id
|
|
&& handoff.attempt.grant_id == permit.grant_id
|
|
&& handoff.attempt.authorization_id == permit.authorization_id
|
|
&& handoff.attempt.plan_sha256 == permit.plan_hash
|
|
&& handoff.attempt.job_payload_sha256 == permit.job_payload_hash,
|
|
"UPDATE_ORIGINAL_JOB_BINDING"
|
|
);
|
|
let Action::UpdateAgent { update } = &job.action else {
|
|
anyhow::bail!("UPDATE_ACTION_REQUIRED")
|
|
};
|
|
ensure!(
|
|
rmm_domain::agent_update::handoff_matches_job(&handoff, &job)
|
|
&& update.validate()
|
|
&& update.manifest_sha256 == handoff.target.manifest_sha256
|
|
&& update.predecessor.manifest_sha256 == handoff.predecessor.manifest_sha256
|
|
&& update.predecessor.agent_sha256 == handoff.predecessor.agent_sha256
|
|
&& update.predecessor.helper_sha256 == handoff.predecessor.helper_sha256
|
|
&& update.predecessor.installed_record_sha256 == handoff.predecessor_record_sha256
|
|
&& update.updater.binary_sha256 == handoff.updater_sha256
|
|
&& update.updater.policy_sha256 == handoff.updater_policy_sha256
|
|
&& matches!(handoff.pin_transition, PinTransition::LinuxGeneralFullV1)
|
|
&& (handoff.stop_set
|
|
== [
|
|
Service::LinuxAgent,
|
|
Service::LinuxGeneralHelper,
|
|
Service::LinuxFullHelper
|
|
]
|
|
|| handoff.stop_set
|
|
== [
|
|
Service::LinuxAgent,
|
|
Service::LinuxGeneralHelper,
|
|
Service::LinuxFullHelper,
|
|
Service::LinuxAptHelper
|
|
]),
|
|
"UPDATE_ORIGINAL_CODE_POLICY_BINDING"
|
|
);
|
|
let update = update.as_ref().clone();
|
|
let decoded = update
|
|
.release_envelope
|
|
.decode()
|
|
.map_err(anyhow::Error::msg)?;
|
|
ensure!(
|
|
decoded.manifest.artifacts[0].sha256 == handoff.target.agent_sha256
|
|
&& decoded.manifest.artifacts[1].sha256 == handoff.target.helper_sha256,
|
|
"UPDATE_ORIGINAL_TARGET_PAIR"
|
|
);
|
|
let result = Self {
|
|
request,
|
|
permit,
|
|
job,
|
|
update,
|
|
handoff,
|
|
delegation,
|
|
start_bytes: start_bytes.to_vec(),
|
|
};
|
|
Ok(result)
|
|
}
|
|
pub fn check_current(
|
|
&self,
|
|
binding: &Binding,
|
|
clock: &impl SyncClock,
|
|
budget: &Budget,
|
|
) -> Result<()> {
|
|
budget.check()?;
|
|
ensure!(
|
|
std::fs::read_to_string("/proc/sys/kernel/random/boot_id")?
|
|
.trim()
|
|
.parse::<uuid::Uuid>()?
|
|
== self.handoff.boot_id,
|
|
"UPDATE_ORIGINAL_BOOT_CHANGED"
|
|
);
|
|
let now = clock.now()?;
|
|
ensure!(
|
|
now >= self.delegation.prepared_at
|
|
&& u64::try_from(now.timestamp())? < self.handoff.finish_before
|
|
&& now
|
|
< self.job.start_before
|
|
+ chrono::Duration::seconds(i64::from(self.job.max_runtime_seconds))
|
|
&& self.handoff.finish_before
|
|
<= self.handoff.consumed_at + u64::from(self.job.max_runtime_seconds),
|
|
"UPDATE_ORIGINAL_EXPIRED"
|
|
);
|
|
let (job, permit) = binding.verify(&self.request, self.delegation.prepared_at)?;
|
|
ensure!(
|
|
serde_json::to_vec(&job)? == serde_json::to_vec(&self.job)?
|
|
&& serde_json::to_vec(&permit)? == serde_json::to_vec(&self.permit)?,
|
|
"UPDATE_ORIGINAL_CHANGED"
|
|
);
|
|
Ok(())
|
|
}
|
|
pub fn handoff(&self) -> &Handoff {
|
|
&self.handoff
|
|
}
|
|
pub fn delegation(&self) -> &UpdateDelegation {
|
|
&self.delegation
|
|
}
|
|
pub fn update(&self) -> &ResolvedAgentUpdate {
|
|
&self.update
|
|
}
|
|
pub fn permit(&self) -> &ExecutionPermitPayload {
|
|
&self.permit
|
|
}
|
|
pub fn start_bytes(&self) -> &[u8] {
|
|
&self.start_bytes
|
|
}
|
|
}
|