Wire preserved update control, health evidence and record publication
Rust development slice / linux (push) Failing after 7s

This commit is contained in:
2026-11-14 23:51:59 +01:00
parent 328e440f73
commit 8e93c9ead5
34 changed files with 7071 additions and 97 deletions
+1 -3
View File
@@ -14,11 +14,9 @@ uuid.workspace = true
libc = "0.2.177"
rusqlite = { version = "0.32.1", features = ["bundled"] }
ed25519-dalek = "=2.2.0"
base64 = "=0.22.1"
rmm-domain = { path = "../domain" }
rmm-agent-core = { path = "../agent-core" }
rmm-updater = { path = "../updater" }
rmm-updater-handoff = { path = "../updater-handoff" }
rmm-updater-linux = { path = "../updater-linux" }
[dev-dependencies]
base64 = "=0.22.1"
+518
View File
@@ -0,0 +1,518 @@
//! Bounded client of the EXISTING fixed Fullhelper control socket. No retry,
//! SQL/event transport, caller paths, process-name or root-UID-only fallback.
use crate::{clock::NativeClock, participants::StableParticipant};
use anyhow::{Context, Result, ensure};
use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD};
use rmm_agent_core::{
reconcile::SyncClock,
update_handoff::{
NativeControlOperation, NativeControlReply, NativeControlRequest, NativeEffect,
NativeEffectClaim,
},
};
use rmm_domain::{
agent_update::{ParticipantRole, UpdateDelegation, UpdateInflightProof, verify_inflight_proof},
reconciliation::{ReconciledOutcome, canonical_outcome_bytes},
};
use rmm_updater_handoff::{Handoff, Phase};
use rmm_updater_linux::{
Budget,
files::Directory,
services::{Service, Systemd},
};
use std::{
io::{Read, Write},
os::{
fd::{AsRawFd, FromRawFd},
unix::{
fs::{FileTypeExt, MetadataExt},
net::UnixStream,
},
},
path::Path,
time::{Duration, Instant},
};
use uuid::Uuid;
const SOCKET: &str = "/run/rmm-helper-maintenance/control.sock";
const MAX_REPLY: usize = 1536 * 1024;
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Generation {
Original,
Replacement,
}
pub struct BridgeContext<'a> {
pub handoff: &'a Handoff,
pub delegation: &'a UpdateDelegation,
pub services: &'a Systemd,
pub updater: &'a StableParticipant,
pub clock: &'a NativeClock,
pub budget: &'a Budget,
pub health_public_key: [u8; 32],
pub generation: Generation,
}
pub struct FullBridge<'a> {
context: BridgeContext<'a>,
prefix: UpdateInflightProof,
poisoned: bool,
finalized: bool,
}
pub struct Reply {
pub proof: Option<UpdateInflightProof>,
pub original_start: Option<Vec<u8>>,
pub claim: Option<NativeEffectClaim>,
pub original_outcome: Option<Vec<u8>>,
}
impl<'a> FullBridge<'a> {
/// Prefix is either the exact initial atomic StopIntent or a complete
/// retained prefix read under the original guarded ledger. Never truncate.
pub fn new(context: BridgeContext<'a>, prefix: UpdateInflightProof) -> Result<Self> {
let (handoff, _) = verify_inflight_proof(&prefix, context.health_public_key)
.map_err(anyhow::Error::msg)?;
ensure!(
handoff.bytes()? == context.handoff.bytes()?
&& prefix.delegation == *context.delegation,
"UPDATE_BRIDGE_PREFIX_BINDING"
);
Ok(Self {
context,
prefix,
poisoned: false,
finalized: false,
})
}
pub fn call(&mut self, method: NativeControlOperation) -> Result<Reply> {
let context = &self.context;
context
.updater
.verify_delegation(context.delegation, context.budget)?;
context.budget.check()?;
let (_, journal) = verify_inflight_proof(&self.prefix, context.health_public_key)
.map_err(anyhow::Error::msg)?;
match &method {
NativeControlOperation::ReadOriginal => {}
NativeControlOperation::ClaimStop {
expected_head_sha256,
..
} => ensure!(
context.generation == Generation::Original
&& journal.phase() == Phase::StopIntent
&& *expected_head_sha256 == self.prefix.head_sha256,
"UPDATE_BRIDGE_CLAIM_PHASE"
),
NativeControlOperation::ObserveStarts { .. } => ensure!(
context.generation == Generation::Replacement
&& journal.phase() == Phase::LedgerReturnIntent,
"UPDATE_BRIDGE_START_PHASE"
),
NativeControlOperation::Health { receipt } => {
ensure!(
context.generation == Generation::Replacement
&& journal.phase() == Phase::ServicesStarted,
"UPDATE_BRIDGE_HEALTH_PHASE"
);
journal.prepare(
u64::try_from(context.clock.now()?.timestamp())?,
rmm_updater_handoff::Evidence::HealthVerified {
receipt: receipt.clone(),
},
)?;
}
NativeControlOperation::Finalize => ensure!(
context.generation == Generation::Replacement
&& journal.phase() == Phase::HealthVerified,
"UPDATE_BRIDGE_FINAL_PHASE"
),
}
let mutating = !matches!(method, NativeControlOperation::ReadOriginal);
ensure!(
!self.finalized && (!mutating || !self.poisoned),
"UPDATE_BRIDGE_NO_RETRY_OR_RESUME"
);
if mutating {
self.poisoned = true;
}
let io_budget = IoBudget {
clock: context.clock,
budget: context.budget,
finish_before: context.handoff.finish_before,
until: Instant::now() + Duration::from_secs(5),
};
io_budget.remaining()?;
ensure!(
std::fs::read_to_string("/proc/sys/kernel/random/boot_id")?
.trim()
.parse::<Uuid>()?
== context.handoff.boot_id,
"UPDATE_BRIDGE_BOOT_CHANGED"
);
let directory = Directory::open(Path::new("/run/rmm-helper-maintenance"), 0, false)?;
let meta = std::fs::symlink_metadata(SOCKET)?;
ensure!(
meta.file_type().is_socket()
&& meta.uid() == 0
&& meta.mode() & 0o007 == 0
&& meta.nlink() == 1,
"UPDATE_BRIDGE_SOCKET_PERMISSIONS"
);
let stream = connect(&io_budget)?;
directory.verify()?;
let after = std::fs::symlink_metadata(SOCKET)?;
ensure!(
meta.dev() == after.dev()
&& meta.ino() == after.ino()
&& meta.mode() == after.mode()
&& meta.uid() == after.uid()
&& meta.gid() == after.gid(),
"UPDATE_BRIDGE_SOCKET_CHANGED"
);
let helper = context.services.observe(Service::Full, context.budget)?;
let peer = helper.authenticate_peer(&stream, context.budget)?;
ensure!(
peer.role == ParticipantRole::LinuxFullHelper,
"UPDATE_BRIDGE_FULL_REQUIRED"
);
match context.generation {
Generation::Original => ensure!(
peer == context.delegation.original_helper,
"UPDATE_BRIDGE_ORIGINAL_HELPER_CHANGED"
),
Generation::Replacement => ensure!(
peer.image_sha256 == context.handoff.target.helper_sha256
&& peer.pid != context.delegation.original_helper.pid,
"UPDATE_BRIDGE_REPLACEMENT_HELPER_CHANGED"
),
}
let request = NativeControlRequest {
operation: "agent_update_control_v1".into(),
schema_version: 1,
challenge: Uuid::new_v4(),
tenant_id: context.handoff.attempt.tenant_id,
asset_id: context.handoff.attempt.asset_id,
agent_id: context.handoff.attempt.agent_id,
enrollment_generation: context.handoff.attempt.enrollment_generation,
assignment_version: context.handoff.attempt.assignment_version,
grant_id: context.handoff.attempt.grant_id,
owner_nonce: context.handoff.owner_nonce,
handoff_sha256: context.delegation.handoff_sha256.clone(),
method,
};
let raw = serde_json::to_vec(&request)?;
ensure!(
!raw.is_empty() && raw.len() <= 32768,
"UPDATE_BRIDGE_REQUEST_LIMIT"
);
let mut io = SocketIo {
stream,
budget: io_budget,
};
io.write_all(&(raw.len() as u32).to_be_bytes())?;
io.write_all(&raw)?;
io.flush()?;
let mut size = [0; 4];
io.read_exact(&mut size)?;
let size = u32::from_be_bytes(size) as usize;
ensure!(size > 0 && size <= MAX_REPLY, "UPDATE_BRIDGE_REPLY_LIMIT");
let mut raw = vec![0; size];
io.read_exact(&mut raw)?;
let reply: NativeControlReply = serde_json::from_slice(&raw)?;
helper.verify(context.budget)?;
ensure!(
reply.schema_version == 1
&& !reply.ready
&& reply.challenge == request.challenge
&& reply.tenant_id == request.tenant_id
&& reply.asset_id == request.asset_id
&& reply.agent_id == request.agent_id
&& reply.enrollment_generation == request.enrollment_generation
&& reply.assignment_version == request.assignment_version
&& reply.grant_id == request.grant_id
&& reply.owner_nonce == request.owner_nonce
&& reply.handoff_sha256 == request.handoff_sha256,
"UPDATE_BRIDGE_REPLY_BINDING"
);
let original_start = reply
.original_start_base64url
.as_deref()
.map(|v| decode(v, 512 * 1024))
.transpose()?;
let original_outcome = reply
.original_outcome_base64url
.as_deref()
.map(|v| decode(v, 512 * 1024))
.transpose()?;
if matches!(request.method, NativeControlOperation::Finalize) {
ensure!(
reply.proof.is_none() && reply.claim.is_none() && original_start.is_none(),
"UPDATE_BRIDGE_FINAL_REPLY"
);
let raw = original_outcome
.as_ref()
.context("UPDATE_BRIDGE_ORIGINAL_OUTCOME_MISSING")?;
let outcome: ReconciledOutcome = serde_json::from_slice(raw)?;
ensure!(
canonical_outcome_bytes(&outcome)? == *raw
&& outcome.state == "succeeded"
&& !outcome.reboot_required,
"UPDATE_BRIDGE_NONCANONICAL_OR_UNSUCCESSFUL_OUTCOME"
);
} else {
ensure!(
original_outcome.is_none(),
"UPDATE_BRIDGE_UNEXPECTED_TERMINAL"
);
let proof = reply
.proof
.as_ref()
.context("UPDATE_BRIDGE_PROOF_MISSING")?;
let (handoff, journal) = verify_inflight_proof(proof, context.health_public_key)
.map_err(anyhow::Error::msg)?;
ensure!(
handoff.bytes()? == context.handoff.bytes()?
&& proof.delegation == *context.delegation
&& proof.events.len() >= self.prefix.events.len()
&& proof.events[..self.prefix.events.len()] == self.prefix.events,
"UPDATE_BRIDGE_PROOF_ROLLBACK_OR_CHANGED"
);
match request.method {
NativeControlOperation::ReadOriginal => ensure!(
reply.claim.is_none() && original_start.is_some(),
"UPDATE_BRIDGE_ORIGINAL_REPLY"
),
NativeControlOperation::ClaimStop {
ref expected_head_sha256,
service,
} => {
let claim = reply
.claim
.as_ref()
.context("UPDATE_BRIDGE_STOP_CLAIM_MISSING")?;
ensure!(
context.generation == Generation::Original
&& journal.phase() == Phase::StopIntent
&& claim.effect() == &NativeEffect::StopService { service }
&& claim.grant_id() == request.grant_id
&& claim.owner_nonce() == request.owner_nonce
&& claim.head_sha256() == expected_head_sha256
&& proof.head_sha256 == *expected_head_sha256
&& original_start.is_none(),
"UPDATE_BRIDGE_STOP_CLAIM_CHANGED"
);
}
NativeControlOperation::ObserveStarts { .. } => ensure!(
context.generation == Generation::Replacement
&& journal.phase() == Phase::ServicesStarted
&& reply.claim.is_none()
&& original_start.is_none(),
"UPDATE_BRIDGE_STARTS_REPLY"
),
NativeControlOperation::Health { .. } => ensure!(
context.generation == Generation::Replacement
&& journal.phase() == Phase::HealthVerified
&& reply.claim.is_none()
&& original_start.is_none(),
"UPDATE_BRIDGE_HEALTH_REPLY"
),
NativeControlOperation::Finalize => unreachable!(),
}
self.prefix = proof.clone();
}
context.budget.check()?;
if matches!(request.method, NativeControlOperation::Finalize) {
self.finalized = true;
}
if mutating {
self.poisoned = false;
}
Ok(Reply {
proof: reply.proof,
claim: reply.claim,
original_start,
original_outcome,
})
}
pub fn head_sha256(&self) -> &str {
&self.prefix.head_sha256
}
pub fn observe_starts(
&mut self,
held: &[rmm_updater_linux::services::HeldService],
) -> Result<Reply> {
ensure!(
held.len() == 3
&& held.iter().map(|s| s.proof().service).eq([
Service::Agent,
Service::General,
Service::Full
]),
"UPDATE_BRIDGE_STARTED_SET"
);
let services = held
.iter()
.map(|s| s.started_proof(self.context.budget))
.collect::<Result<Vec<_>>>()?;
let proofs = held.iter().map(|s| s.proof().clone()).collect::<Vec<_>>();
let observation_sha256 = rmm_domain::jobs::hash(&serde_json::to_vec(&proofs)?);
let reply = self.call(NativeControlOperation::ObserveStarts {
services: services.clone(),
observation_sha256: observation_sha256.clone(),
})?;
for service in held {
service.verify(self.context.budget)?;
}
let proof = reply
.proof
.as_ref()
.context("UPDATE_BRIDGE_STARTED_PROOF_MISSING")?;
let raw = proof.decode().map_err(anyhow::Error::msg)?;
let last: rmm_updater_handoff::Event = serde_json::from_slice(
raw.events
.last()
.context("UPDATE_BRIDGE_STARTED_EVENT_MISSING")?,
)?;
ensure!(
matches!(last.evidence,rmm_updater_handoff::Evidence::ServicesStarted {services:actual,observation_sha256:hash,..}
if actual==services && hash==observation_sha256),
"UPDATE_BRIDGE_STARTED_RESULT_CHANGED"
);
Ok(reply)
}
}
fn decode(value: &str, max: usize) -> Result<Vec<u8>> {
ensure!(
!value.is_empty() && value.len() <= 4 * max.div_ceil(3),
"UPDATE_BRIDGE_BASE64_LIMIT"
);
let raw = URL_SAFE_NO_PAD.decode(value)?;
ensure!(
!raw.is_empty() && raw.len() <= max && URL_SAFE_NO_PAD.encode(&raw) == value,
"UPDATE_BRIDGE_NONCANONICAL_BASE64"
);
Ok(raw)
}
struct IoBudget<'a> {
clock: &'a NativeClock,
budget: &'a Budget,
finish_before: u64,
until: Instant,
}
impl IoBudget<'_> {
fn remaining(&self) -> Result<Duration> {
self.budget.check()?;
let now = self.clock.now()?;
let finish = chrono::DateTime::from_timestamp(i64::try_from(self.finish_before)?, 0)
.context("UPDATE_BRIDGE_FINISH")?;
let original = (finish - now).to_std()?;
let frame = self.until.saturating_duration_since(Instant::now());
let remaining = original.min(frame);
ensure!(!remaining.is_zero(), "UPDATE_BRIDGE_DEADLINE");
Ok(remaining)
}
}
struct SocketIo<'a> {
stream: UnixStream,
budget: IoBudget<'a>,
}
impl Read for SocketIo<'_> {
fn read(&mut self, bytes: &mut [u8]) -> std::io::Result<usize> {
self.stream.set_read_timeout(Some(
self.budget.remaining().map_err(std::io::Error::other)?,
))?;
self.stream.read(bytes)
}
}
impl Write for SocketIo<'_> {
fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
self.stream.set_write_timeout(Some(
self.budget.remaining().map_err(std::io::Error::other)?,
))?;
self.stream.write(bytes)
}
fn flush(&mut self) -> std::io::Result<()> {
self.budget.remaining().map_err(std::io::Error::other)?;
self.stream.flush()
}
}
fn connect(budget: &IoBudget<'_>) -> Result<UnixStream> {
// A blocking Unix connect can wait forever on a full listener backlog.
// Use only the fixed path and an owned nonblocking socket, including during
// connect; never switch to a network endpoint or another helper.
let fd = unsafe {
libc::socket(
libc::AF_UNIX,
libc::SOCK_STREAM | libc::SOCK_CLOEXEC | libc::SOCK_NONBLOCK,
0,
)
};
ensure!(fd >= 0, "UPDATE_BRIDGE_SOCKET_CREATE");
let stream = unsafe { UnixStream::from_raw_fd(fd) };
let mut address: libc::sockaddr_un = unsafe { std::mem::zeroed() };
address.sun_family = libc::AF_UNIX as libc::sa_family_t;
ensure!(
SOCKET.len() < address.sun_path.len(),
"UPDATE_BRIDGE_FIXED_PATH_LIMIT"
);
for (slot, byte) in address.sun_path.iter_mut().zip(SOCKET.bytes()) {
*slot = byte as libc::c_char;
}
let result = unsafe {
libc::connect(
fd,
(&address as *const libc::sockaddr_un).cast(),
std::mem::size_of::<libc::sockaddr_un>() as libc::socklen_t,
)
};
if result < 0 {
let error = std::io::Error::last_os_error();
ensure!(
error.raw_os_error() == Some(libc::EINPROGRESS),
"UPDATE_BRIDGE_CONNECT: {error}"
);
loop {
let remaining = budget.remaining()?;
let mut poll = libc::pollfd {
fd: stream.as_raw_fd(),
events: libc::POLLOUT,
revents: 0,
};
let result = unsafe {
libc::poll(
&mut poll,
1,
i32::try_from(remaining.as_millis().min(200))?.max(1),
)
};
if result < 0 {
let error = std::io::Error::last_os_error();
ensure!(
error.kind() == std::io::ErrorKind::Interrupted,
"UPDATE_BRIDGE_CONNECT_POLL: {error}"
);
continue;
}
if result == 0 {
continue;
}
let mut error: libc::c_int = 0;
let mut length = std::mem::size_of_val(&error) as libc::socklen_t;
ensure!(
unsafe {
libc::getsockopt(
fd,
libc::SOL_SOCKET,
libc::SO_ERROR,
(&mut error as *mut libc::c_int).cast(),
&mut length,
)
} == 0
&& error == 0
&& poll.revents == libc::POLLOUT,
"UPDATE_BRIDGE_CONNECT_FAILED"
);
break;
}
}
budget.remaining()?;
stream.set_nonblocking(false)?;
Ok(stream)
}
+247
View File
@@ -0,0 +1,247 @@
//! One create-only protected handoff witness bundle BEFORE consumption. Its
//! inner raw hashes are bound by the ORIGINAL atomic helper handoff. Reading
//! cannot create a plan, authority, lease, resume or new effect.
use crate::{files::FileWitnesses, retained::RetainedSet};
use anyhow::{Result, ensure};
use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD};
use rmm_domain::agent_update::{ParticipantIdentity, ParticipantRole, UpdateDelegation};
use rmm_updater_handoff::Handoff;
use rmm_updater_linux::files::{Directory, LedgerIdentity};
use rmm_updater_linux::{
Budget,
services::{HeldService, Service},
};
use serde::{Deserialize, Serialize};
use std::{
ffi::OsStr,
fs::{self, OpenOptions},
io::Write,
os::unix::fs::{DirBuilderExt, OpenOptionsExt},
path::{Path, PathBuf},
};
use uuid::Uuid;
const ROOT: &str = "/var/lib/rmm-agent-updater/handoffs";
const MAX_BYTES: usize = 256 * 1024;
#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct Record {
schema_version: u32,
grant_id: Uuid,
owner_nonce: Uuid,
handoff_sha256: String,
handoff_record_base64url: String,
delegation: UpdateDelegation,
files_base64url: String,
retained_base64url: String,
ledger: LedgerIdentity,
}
pub struct Loaded {
pub header: Header,
pub files: FileWitnesses,
pub retained: RetainedSet,
pub ledger: LedgerIdentity,
}
pub struct Header {
pub handoff: Handoff,
pub delegation: UpdateDelegation,
}
pub struct Participants<'a> {
pub delegation: &'a UpdateDelegation,
pub original_full: &'a HeldService,
pub updater: &'a HeldService,
pub budget: &'a Budget,
}
fn path(handoff: &Handoff) -> PathBuf {
Path::new(ROOT).join(handoff.attempt.grant_id.to_string())
}
pub fn write_before_handoff(
handoff: &Handoff,
files: &FileWitnesses,
retained: &RetainedSet,
ledger: &LedgerIdentity,
participants: Participants<'_>,
) -> Result<()> {
handoff.validate()?;
participants.budget.check()?;
participants.original_full.verify(participants.budget)?;
participants.updater.verify(participants.budget)?;
ensure!(
participants.original_full.proof().service == Service::Full
&& participants.updater.proof().service == Service::Updater
&& observed_participant(participants.original_full)?
== participants.delegation.original_helper
&& observed_participant(participants.updater)? == participants.delegation.updater,
"UPDATE_BUNDLE_PARTICIPANTS_CHANGED"
);
validate_delegation(handoff, participants.delegation)?;
files.bind(handoff)?;
retained.verify()?;
ensure!(
retained.sha256() == handoff.retained_state_sha256
&& ledger.sha256()? == handoff.ledger_identity_sha256,
"UPDATE_BUNDLE_BINDING"
);
let parent = Directory::open(Path::new(ROOT), 0, true)?;
let raw = serde_json::to_vec(&Record {
schema_version: 1,
grant_id: handoff.attempt.grant_id,
owner_nonce: handoff.owner_nonce,
handoff_sha256: rmm_domain::jobs::hash(&handoff.bytes()?),
handoff_record_base64url: URL_SAFE_NO_PAD.encode(handoff.bytes()?),
delegation: participants.delegation.clone(),
files_base64url: URL_SAFE_NO_PAD.encode(files.bytes()?),
retained_base64url: URL_SAFE_NO_PAD.encode(retained.bytes()?),
ledger: ledger.clone(),
})?;
ensure!(raw.len() <= MAX_BYTES, "UPDATE_BUNDLE_LIMIT");
// create_dir is exclusive; partial/previous bundles are never reused.
fs::DirBuilder::new().mode(0o700).create(path(handoff))?;
parent.verify()?;
let directory = Directory::open(&path(handoff), 0, true)?;
let mut file = OpenOptions::new()
.create_new(true)
.write(true)
.mode(0o600)
.custom_flags(libc::O_NOFOLLOW | libc::O_CLOEXEC)
.open(path(handoff).join("witnesses.json"))?;
file.write_all(&raw)?;
file.sync_all()?;
let current = directory.inspect(OsStr::new("witnesses.json"), 0, MAX_BYTES as u64)?;
ensure!(
current.proof().sha256 == rmm_domain::jobs::hash(&raw)
&& current.proof().identity.links == 1,
"UPDATE_BUNDLE_WRITE_CHANGED"
);
sync_directory(&path(handoff))?;
sync_directory(Path::new(ROOT))?;
directory.verify()?;
parent.verify()
}
pub fn read_for_handoff(handoff: &Handoff) -> Result<Loaded> {
handoff.validate()?;
let record = read_record(handoff.attempt.grant_id)?;
let header = header(&record)?;
ensure!(
header.handoff.bytes()? == handoff.bytes()?,
"UPDATE_BUNDLE_ORIGINAL_HEADER_CHANGED"
);
let files = FileWitnesses::from_bound_bytes(&decode(&record.files_base64url)?, handoff)?;
let retained = RetainedSet::from_bound_bytes(&decode(&record.retained_base64url)?, handoff)?;
Ok(Loaded {
header,
files,
retained,
ledger: record.ledger,
})
}
pub fn read_header(grant_id: Uuid, owner_nonce: Uuid, handoff_sha256: &str) -> Result<Header> {
ensure!(
!grant_id.is_nil()
&& !owner_nonce.is_nil()
&& rmm_domain::agent_update::digest(handoff_sha256),
"UPDATE_BUNDLE_HEADER_SELECTOR"
);
let record = read_record(grant_id)?;
ensure!(
record.owner_nonce == owner_nonce && record.handoff_sha256 == handoff_sha256,
"UPDATE_BUNDLE_HEADER_BINDING"
);
header(&record)
}
fn read_record(grant_id: Uuid) -> Result<Record> {
ensure!(!grant_id.is_nil(), "UPDATE_BUNDLE_GRANT");
let parent = Directory::open(Path::new(ROOT), 0, true)?;
let directory = Directory::open(&Path::new(ROOT).join(grant_id.to_string()), 0, true)?;
let mut file = directory.inspect(OsStr::new("witnesses.json"), 0, MAX_BYTES as u64)?;
ensure!(
file.proof().identity.mode & 0o777 == 0o600 && file.proof().identity.links == 1,
"UPDATE_BUNDLE_PRIVATE_FILE"
);
let raw = file.bytes(MAX_BYTES as u64)?;
let record: Record = serde_json::from_slice(&raw)?;
ensure!(
serde_json::to_vec(&record)? == raw
&& record.schema_version == 1
&& record.grant_id == grant_id,
"UPDATE_BUNDLE_RECORD_BINDING"
);
directory.file(OsStr::new("witnesses.json"), file.proof(), MAX_BYTES as u64)?;
parent.verify()?;
Ok(record)
}
fn header(record: &Record) -> Result<Header> {
let raw = decode(&record.handoff_record_base64url)?;
let handoff: Handoff = serde_json::from_slice(&raw)?;
ensure!(
handoff.bytes()? == raw
&& rmm_domain::jobs::hash(&raw) == record.handoff_sha256
&& handoff.attempt.grant_id == record.grant_id
&& handoff.owner_nonce == record.owner_nonce
&& record.ledger.sha256()? == handoff.ledger_identity_sha256,
"UPDATE_BUNDLE_HEADER_CHANGED"
);
validate_delegation(&handoff, &record.delegation)?;
ensure!(
rmm_domain::jobs::hash(&decode(&record.files_base64url)?) == handoff.staged_files_sha256
&& rmm_domain::jobs::hash(&decode(&record.retained_base64url)?)
== handoff.retained_state_sha256,
"UPDATE_BUNDLE_INNER_HASH_CHANGED"
);
Ok(Header {
handoff,
delegation: record.delegation.clone(),
})
}
fn validate_delegation(handoff: &Handoff, delegation: &UpdateDelegation) -> Result<()> {
ensure!(
delegation.validate()
&& delegation.handoff_sha256 == rmm_domain::jobs::hash(&handoff.bytes()?)
&& delegation.boot_id == handoff.boot_id
&& delegation.owner_nonce == handoff.owner_nonce
&& delegation.updater.role == ParticipantRole::LinuxUpdater
&& delegation.original_helper.role == ParticipantRole::LinuxFullHelper
&& 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_BUNDLE_DELEGATION_BINDING"
);
Ok(())
}
fn observed_participant(held: &HeldService) -> Result<ParticipantIdentity> {
let proof = held.proof();
let role = match proof.service {
Service::Updater => ParticipantRole::LinuxUpdater,
Service::Full => ParticipantRole::LinuxFullHelper,
_ => anyhow::bail!("UPDATE_BUNDLE_PARTICIPANT_ROLE"),
};
ensure!(proof.process.uid == 0, "UPDATE_BUNDLE_ROOT_PARTICIPANT");
Ok(ParticipantIdentity {
role,
pid: proof.process.pid,
birth: proof.process.birth,
image_sha256: proof.process.image_sha256.clone(),
security_principal: "uid:0".into(),
service_instance_sha256: rmm_domain::jobs::hash(&serde_json::to_vec(proof)?),
})
}
fn decode(value: &str) -> Result<Vec<u8>> {
ensure!(
!value.is_empty() && value.len() <= 4 * 65536usize.div_ceil(3),
"UPDATE_BUNDLE_INNER_LIMIT"
);
let raw = URL_SAFE_NO_PAD.decode(value)?;
ensure!(
!raw.is_empty() && raw.len() <= 65536 && URL_SAFE_NO_PAD.encode(&raw) == value,
"UPDATE_BUNDLE_INNER_BASE64"
);
Ok(raw)
}
fn sync_directory(path: &Path) -> Result<()> {
OpenOptions::new()
.read(true)
.custom_flags(libc::O_DIRECTORY | libc::O_NOFOLLOW | libc::O_CLOEXEC)
.open(path)?
.sync_all()?;
Ok(())
}
+1
View File
@@ -22,6 +22,7 @@ pub struct LedgerFence<'w, 'a, 's> {
}
impl<'w, 'a, 's> LedgerFence<'w, 'a, 's> {
pub fn new(ledger: &'w mut OwnedLedger<'a, 's>) -> Result<Self> {
ledger.qualify_for_effects()?;
ensure!(
ledger.journal()?.phase() == Phase::UpdaterOwnsLedger,
"UPDATE_NO_FENCE_RESUME"
+100 -4
View File
@@ -5,9 +5,9 @@ use rmm_domain::jobs::hash;
use rmm_updater_handoff::{FileRole, Handoff};
use rmm_updater_linux::{
Budget,
files::{Directory, FileProof, Role, Staged},
files::{BoundStage, Directory, FileProof, Role, Staged},
};
use serde::Serialize;
use serde::{Deserialize, Serialize};
use std::{ffi::OsStr, path::Path};
use uuid::Uuid;
@@ -29,12 +29,20 @@ fn role(role: FileRole) -> Result<Role> {
_ => anyhow::bail!("UPDATE_LINUX_FILE_ROLE"),
}
}
#[derive(Serialize)]
#[derive(Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct Proof {
role: Role,
original: FileProof,
staged: FileProof,
}
#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct Record {
schema_version: u32,
owner_nonce: Uuid,
files: Vec<Proof>,
}
pub struct FileWitnesses {
files: Vec<Proof>,
nonce: Uuid,
@@ -63,6 +71,7 @@ impl FileWitnesses {
)?;
ensure!(
stage.proof().identity.links == 1
&& stage.proof().identity.device == original.proof().identity.device
&& stage.proof().identity.uid == original.proof().identity.uid
&& stage.proof().identity.gid == original.proof().identity.gid
&& stage.proof().identity.mode == original.proof().identity.mode,
@@ -74,7 +83,11 @@ impl FileWitnesses {
staged: stage.proof().clone(),
});
}
let digest = hash(&serde_json::to_vec(&files)?);
let digest = hash(&serde_json::to_vec(&Record {
schema_version: 1,
owner_nonce: nonce,
files: files.clone(),
})?);
Ok(Self {
files,
nonce,
@@ -84,6 +97,65 @@ impl FileWitnesses {
pub fn sha256(&self) -> &str {
&self.sha256
}
pub fn bytes(&self) -> Result<Vec<u8>> {
let bytes = serde_json::to_vec(&Record {
schema_version: 1,
owner_nonce: self.nonce,
files: self.files.clone(),
})?;
ensure!(
bytes.len() <= 65536 && hash(&bytes) == self.sha256,
"UPDATE_FILE_WITNESS_CHANGED"
);
Ok(bytes)
}
/// Protected bundle bytes remain observations only. Actual phase-specific
/// path/FD/hash verification is separate and mandatory before any effect.
pub fn from_bound_bytes(raw: &[u8], handoff: &Handoff) -> Result<Self> {
ensure!(
!raw.is_empty() && raw.len() <= 65536 && hash(raw) == handoff.staged_files_sha256,
"UPDATE_FILE_WITNESS_HASH"
);
let record: Record = serde_json::from_slice(raw)?;
ensure!(
record.schema_version == 1
&& record.owner_nonce == handoff.owner_nonce
&& record.files.len() == 4
&& serde_json::to_vec(&record)? == raw,
"UPDATE_FILE_WITNESS_CODEC"
);
for (role, file) in ROLES.iter().zip(&record.files) {
ensure!(*role == file.role, "UPDATE_FILE_WITNESS_ROLE_ORDER");
for proof in [&file.original, &file.staged] {
ensure!(
proof.identity.inode > 0
&& proof.identity.uid == 0
&& proof.identity.links > 0
&& proof.identity.mode & libc::S_IFMT == libc::S_IFREG
&& proof.identity.mode & 0o022 == 0
&& proof.bytes > 0
&& proof.bytes <= 256 * 1024 * 1024
&& rmm_domain::agent_update::digest(&proof.sha256),
"UPDATE_FILE_WITNESS_SHAPE"
);
}
ensure!(
file.staged.identity.links == 1
&& file.staged.identity.device == file.original.identity.device
&& file.original.identity.uid == file.staged.identity.uid
&& file.original.identity.gid == file.staged.identity.gid
&& file.original.identity.mode == file.staged.identity.mode,
"UPDATE_FILE_WITNESS_METADATA"
);
}
let result = Self {
files: record.files,
nonce: record.owner_nonce,
sha256: hash(raw),
};
result.bind(handoff)?;
Ok(result)
}
pub fn bind(&self, handoff: &Handoff) -> Result<()> {
ensure!(
handoff.files.len() == 4
@@ -101,6 +173,30 @@ impl FileWitnesses {
}
Ok(())
}
/// Reopen only the exact preconsumption protected stages. This is not a
/// dispatch or restart path: the live original owner and each Core claim
/// remain independently mandatory before Stop/Swap/Start.
pub fn reopen_stages(&self, handoff: &Handoff, budget: &Budget) -> Result<[Staged; 4]> {
self.bind(handoff)?;
let mut opened = Vec::new();
for proof in &self.files {
let path = Path::new(proof.role.target());
let dir = Directory::open(path.parent().context("UPDATE_TARGET_PARENT")?, 0, false)?;
opened.push(Staged::reopen_bound(
&dir,
BoundStage {
role: proof.role,
nonce: self.nonce,
original: &proof.original,
staged: &proof.staged,
},
budget,
)?);
}
opened
.try_into()
.map_err(|_| anyhow::anyhow!("UPDATE_STAGE_COUNT"))
}
pub fn verify_original_and_stage(&self, which: FileRole, budget: &Budget) -> Result<()> {
let file = self.file(which)?;
budget.check()?;
File diff suppressed because it is too large Load Diff
+157 -9
View File
@@ -3,7 +3,7 @@
//! consumption, schema reset, outcome healing or readiness transition.
use crate::{
clock::NativeClock, files::FileWitnesses, original::Original, participants::StableParticipant,
retained::RetainedSet, trust::TrustState,
retained::RetainedSet, returned::PreparedReservation, trust::TrustState,
};
use anyhow::{Result, ensure};
use rmm_agent_core::{
@@ -22,8 +22,22 @@ use rmm_updater_linux::services::StoppedServices;
use rmm_updater_linux::{Budget, files::LedgerGuard};
use rusqlite::{Connection, OpenFlags};
use std::time::Duration;
fn observation_budget() -> Result<Budget> {
let boot = std::fs::read_to_string("/proc/sys/kernel/random/boot_id")?
.trim()
.parse()?;
Budget::new(boot, Duration::from_secs(5))
}
const LEDGER: &str = "/var/lib/rmm-helper/consumption.sqlite";
fn file_role(role: NativeFileRole) -> rmm_updater_handoff::FileRole {
match role {
NativeFileRole::AgentBinary => rmm_updater_handoff::FileRole::AgentBinary,
NativeFileRole::HelperBinary => rmm_updater_handoff::FileRole::HelperBinary,
NativeFileRole::FullConfig => rmm_updater_handoff::FileRole::LinuxFullConfig,
NativeFileRole::FullPin => rmm_updater_handoff::FileRole::LinuxFullPin,
}
}
pub struct OwnedGuard<'a, 'stopped> {
os: &'a LedgerGuard<'stopped>,
@@ -56,7 +70,7 @@ pub struct OwnedLedger<'a, 'stopped> {
}
impl<'a, 'stopped> OwnedLedger<'a, 'stopped> {
pub fn open(input: OwnedInputs<'a, 'stopped>) -> Result<Self> {
input.os.verify_for_effect(input.budget)?;
input.os.verify_for_effect(&observation_budget()?)?;
ensure!(
input.os.identity().sha256()? == input.original.handoff().ledger_identity_sha256
&& input.retained.sha256() == input.original.handoff().retained_state_sha256,
@@ -93,12 +107,45 @@ impl<'a, 'stopped> OwnedLedger<'a, 'stopped> {
// Replay the complete original retained chain and its durable head.
update_handoff::read(&db, &guard, input.original.permit().grant_id)?;
db.execute_batch("PRAGMA foreign_keys=ON")?;
ensure!(
db.query_row("PRAGMA foreign_keys", [], |r| r.get::<_, bool>(0))?,
"UPDATE_LEDGER_FOREIGN_KEYS_REQUIRED"
);
rmm_agent_core::ledger::bounded_journal_capacity(&db)?;
Ok(Self { db, guard })
}
pub fn journal(&self) -> Result<Journal> {
update_handoff::read(&self.db, &self.guard, self.guard.original.permit().grant_id)
}
pub fn qualify_for_effects(&self) -> Result<()> {
self.guard.check(&self.db)?;
self.guard.original.check_current(
self.guard.binding,
self.guard.clock,
self.guard.budget,
)?;
self.guard
.trust
.recheck(self.guard.original, self.guard.clock)?;
self.guard
.trust
.qualify_history(&self.db, &self.guard, self.guard.binding)
}
pub fn mark_unknown(
&mut self,
observed_at: u64,
reason: rmm_updater_handoff::UnknownReason,
) -> Result<()> {
let journal = self.journal()?;
update_handoff::mark_unknown(
&mut self.db,
&self.guard,
self.guard.original.permit(),
journal.head_sha256(),
observed_at,
reason,
)
}
pub fn append_observed(&mut self, evidence: Evidence) -> Result<()> {
let journal = self.journal()?;
let event = journal.prepare(
@@ -125,6 +172,37 @@ impl<'a, 'stopped> OwnedLedger<'a, 'stopped> {
)
}
pub fn observe_native(&mut self, claim: &NativeEffectClaim, observation: &str) -> Result<()> {
let expected = match claim.effect() {
NativeEffect::StopService { service } => {
let role = match service {
update_handoff::NativeService::Agent => {
rmm_updater_linux::services::Service::Agent
}
update_handoff::NativeService::General => {
rmm_updater_linux::services::Service::General
}
update_handoff::NativeService::Full => {
rmm_updater_linux::services::Service::Full
}
};
let proof = self
.guard
.stopped
.proofs()
.into_iter()
.find(|p| p.service == role)
.ok_or_else(|| anyhow::anyhow!("UPDATE_STOP_WITNESS_MISSING"))?;
hash(&serde_json::to_vec(&proof)?)
}
NativeEffect::SavePredecessor { role } => {
self.native_observation_sha256(file_role(*role), false)?
}
NativeEffect::PublishReplacement { role } => {
self.native_observation_sha256(file_role(*role), true)?
}
_ => anyhow::bail!("UPDATE_OWNED_OBSERVATION_NOT_ALLOWED"),
};
ensure!(expected == observation, "UPDATE_NATIVE_OBSERVATION_CHANGED");
update_handoff::observe_native_effect(
&mut self.db,
&self.guard,
@@ -153,17 +231,66 @@ impl<'a, 'stopped> OwnedLedger<'a, 'stopped> {
pub fn verify_pair(&self) -> Result<()> {
self.guard.files.verify_pair(self.guard.budget)
}
pub fn inflight_proof(&self) -> Result<rmm_domain::agent_update::UpdateInflightProof> {
let journal = self.journal()?;
let mut statement = self.db.prepare(
"SELECT event FROM agent_update_handoff_events WHERE grant_id=?1 ORDER BY sequence",
)?;
let events = statement
.query_map([self.guard.original.permit().grant_id.to_string()], |r| {
r.get(0)
})?
.collect::<rusqlite::Result<Vec<Vec<u8>>>>()?;
rmm_domain::agent_update::UpdateInflightProof::from_raw(
&self.guard.original.handoff().bytes()?,
self.guard.original.delegation().clone(),
&events,
journal.head_sha256().into(),
)
.map_err(anyhow::Error::msg)
}
pub fn prepare_return_and_close(mut self) -> Result<PreparedReservation> {
let journal = self.journal()?;
ensure!(
journal.phase() == Phase::PairVerified,
"UPDATE_RETURN_REQUIRES_PAIR_VERIFIED"
);
let now = self.guard.clock.now()?;
let event = journal.prepare(
u64::try_from(now.timestamp())?,
Evidence::LedgerReturnIntent,
)?;
let claims = update_handoff::prepare_return_and_starts(
&mut self.db,
&self.guard,
self.guard.original.permit().grant_id,
journal.head_sha256(),
&event,
now,
)?;
let proof = self.inflight_proof()?;
let ledger = self.guard.os.identity().clone();
let old_services = self.guard.stopped.proofs();
self.close()?;
Ok(PreparedReservation {
claims,
proof,
ledger,
old_services,
})
}
/// Close the actual SQLite connection BEFORE returning the OS flock. An
/// open/failed-close connection never produces a released-ledger witness.
pub fn close(self) -> Result<()> {
self.guard.check(&self.db)?;
self.db.close().map_err(|(_, e)| e)?;
self.guard.os.verify_for_effect(self.guard.budget)
self.guard.os.verify_for_effect(&observation_budget()?)
}
}
impl ExecutorLedgerGuard for OwnedGuard<'_, '_> {
fn check(&self, db: &Connection) -> Result<()> {
self.os.verify_for_effect(self.budget)?;
let observation = observation_budget()?;
self.os.verify_for_effect(&observation)?;
ensure!(
unsafe { db.handle() } as usize == self.sqlite_handle,
"UPDATE_SQLITE_CONNECTION_CHANGED"
@@ -174,11 +301,16 @@ impl ExecutorLedgerGuard for OwnedGuard<'_, '_> {
|r| r.get(0),
)?;
ensure!(path == LEDGER, "UPDATE_SQLITE_PATH_CHANGED");
self.original
.check_current(self.binding, self.clock, self.budget)?;
self.trust.recheck(self.original, self.clock)?;
Original::verify_retained(
self.binding,
self.original.start_bytes(),
&self.original.handoff().bytes()?,
self.original.delegation().clone(),
)?;
self.trust.recheck_protected()?;
self.trust.recheck_original_binding(self.original)?;
self.updater
.verify_delegation(self.original.delegation(), self.budget)?;
.verify_delegation(self.original.delegation(), &observation)?;
self.retained.verify()?;
let grant = self.original.permit().grant_id.to_string();
let start: Vec<u8> = db.query_row(
@@ -196,7 +328,7 @@ impl ExecutorLedgerGuard for OwnedGuard<'_, '_> {
|r| r.get(0),
)?;
ensure!(unresolved == 0, "UPDATE_OTHER_UNRESOLVED_ATTEMPT");
self.os.verify_for_effect(self.budget)
self.os.verify_for_effect(&observation)
}
fn check_update_owner(
&self,
@@ -217,6 +349,9 @@ impl ExecutorLedgerGuard for OwnedGuard<'_, '_> {
&& delegation == self.original.delegation(),
"UPDATE_OWNED_WRITER_CONTEXT_CHANGED"
);
self.original
.check_current(self.binding, self.clock, self.budget)?;
self.trust.recheck(self.original, self.clock)?;
self.os.verify_for_effect(self.budget)?;
self.updater.verify_delegation(delegation, self.budget)
}
@@ -236,8 +371,17 @@ impl UpdateParticipantGuard for OwnedGuard<'_, '_> {
&& delegation == self.original.delegation(),
"UPDATE_EVENT_ORIGINAL_CHANGED"
);
if matches!(event.evidence, Evidence::Unknown { .. }) {
let observation = observation_budget()?;
self.os.verify_for_effect(&observation)?;
self.updater.verify_delegation(delegation, &observation)?;
self.trust.recheck_protected()?;
self.retained.verify()?;
return Ok(());
}
self.original
.check_current(self.binding, self.clock, self.budget)?;
self.trust.recheck(self.original, self.clock)?;
self.updater.verify_delegation(delegation, self.budget)?;
self.os.verify_for_effect(self.budget)?;
self.retained.verify()?;
@@ -304,6 +448,7 @@ impl UpdateParticipantGuard for OwnedGuard<'_, '_> {
);
self.original
.check_current(self.binding, self.clock, self.budget)?;
self.trust.recheck(self.original, self.clock)?;
self.updater.verify_delegation(delegation, self.budget)?;
self.os.verify_for_effect(self.budget)?;
self.retained.verify()?;
@@ -314,6 +459,9 @@ impl UpdateParticipantGuard for OwnedGuard<'_, '_> {
NativeFileRole::FullPin => rmm_updater_handoff::FileRole::LinuxFullPin,
};
match (effect, stage) {
(NativeEffect::StopService { .. }, NativeEffectStage::Observe) => {
self.stopped.verify_still_stopped(self.budget)?
}
(NativeEffect::SavePredecessor { role: r }, NativeEffectStage::Claim) => {
self.files.verify_original_and_stage(role(r), self.budget)?
}
+5
View File
@@ -1,12 +1,17 @@
//! Concrete, original-attempt-bound Linux worker. No approval or retry path.
#![cfg(target_os = "linux")]
pub mod bridge;
pub mod bundle;
pub mod clock;
pub mod fence;
pub mod files;
pub mod full;
pub mod history;
pub mod ledger;
pub mod original;
pub mod participants;
pub mod record;
pub mod retained;
pub mod returned;
pub mod trust;
+21 -8
View File
@@ -32,6 +32,18 @@ impl Original {
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()
@@ -40,7 +52,6 @@ impl Original {
&& record.len() <= rmm_updater_handoff::MAX_RECORD_BYTES,
"UPDATE_ORIGINAL_LIMIT"
);
budget.check()?;
let handoff: Handoff = serde_json::from_slice(record)?;
handoff.validate()?;
ensure!(
@@ -72,12 +83,8 @@ impl Original {
&request,
handoff.boot_id,
delegation.prepared_at
)? == start_bytes
&& std::fs::read_to_string("/proc/sys/kernel/random/boot_id")?
.trim()
.parse::<uuid::Uuid>()?
== handoff.boot_id,
"UPDATE_ORIGINAL_START_OR_BOOT_CHANGED"
)? == start_bytes,
"UPDATE_ORIGINAL_START_CHANGED"
);
ensure!(
job.execution_identity == "root"
@@ -144,7 +151,6 @@ impl Original {
delegation,
start_bytes: start_bytes.to_vec(),
};
result.check_current(binding, clock, budget)?;
Ok(result)
}
pub fn check_current(
@@ -154,6 +160,13 @@ impl Original {
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
+944
View File
@@ -0,0 +1,944 @@
//! Deterministic record bytes and one live, fixed-namespace publication pass.
//! Neither a builder nor a namespace proof grants authority. The caller's real
//! guard must recheck Original/Health/clock/services and every other protected
//! file before committing each native claim and observation in the SAME ledger.
use crate::original::Original;
use anyhow::{Result, ensure};
use ed25519_dalek::{Signature, VerifyingKey};
use rmm_agent_core::update_handoff::NativeEffect;
use rmm_domain::{
agent_update::ResolvedAgentUpdate,
agent_update_provisioning::{ActivationEvidence, InstalledReleaseRecord, MAX_RECORD_BYTES},
jobs::hash,
};
use rmm_updater_handoff::{
Event, Evidence, HEALTH_DOMAIN, HealthReceipt, Journal, Phase, SignedHealth,
};
use rmm_updater_linux::{
Budget,
files::{Directory, FileProof, Identity},
};
use serde::Serialize;
use std::{
ffi::CString,
fs::{File, Metadata, OpenOptions},
io::Write,
os::{
fd::{AsRawFd, FromRawFd},
unix::fs::{FileExt, MetadataExt, OpenOptionsExt},
},
path::Path,
};
use uuid::Uuid;
const ROOT: &str = "/var/lib/rmm-agent-updater";
const CURRENT: &str = "installed-record.json";
/// Private construction binds all future namespace operations to these exact
/// original inputs. Deliberately neither Clone nor Deserialize nor a resume API.
pub struct BuiltRecord {
record: InstalledReleaseRecord,
raw: Vec<u8>,
prior_raw: Vec<u8>,
old_sha256: String,
sha256: String,
grant_id: Uuid,
owner_nonce: Uuid,
}
impl BuiltRecord {
pub fn build(
original: &Original,
prior_raw: &[u8],
journal: &Journal,
health_event_raw: &[u8],
) -> Result<Self> {
let header = original.handoff();
let handoff_sha256 = hash(&header.bytes()?);
ensure!(
journal.phase() == Phase::HealthVerified
&& journal.handoff_sha256() == handoff_sha256
&& !health_event_raw.is_empty()
&& health_event_raw.len() <= rmm_updater_handoff::MAX_EVENT_BYTES
&& hash(health_event_raw) == journal.head_sha256(),
"UPDATE_RECORD_HEALTH_HEAD"
);
let event: Event = serde_json::from_slice(health_event_raw)?;
ensure!(
serde_json::to_vec(&event)? == health_event_raw
&& event.handoff_sha256 == handoff_sha256
&& event.boot_id == header.boot_id,
"UPDATE_RECORD_HEALTH_EVENT"
);
let Evidence::HealthVerified { receipt: signed } = &event.evidence else {
anyhow::bail!("UPDATE_RECORD_HEALTH_REQUIRED")
};
ensure!(
!signed.payload.is_empty()
&& signed.payload.len() <= 4096
&& signed.signature.len() == 64,
"UPDATE_RECORD_HEALTH_LIMIT"
);
let receipt: HealthReceipt = serde_json::from_slice(&signed.payload)?;
ensure!(
serde_json::to_vec(&receipt)? == signed.payload,
"UPDATE_RECORD_HEALTH_NONCANONICAL"
);
// Journal possession is not independent key trust. Authenticate the
// exact receipt again with the ORIGINAL signed job's external key pin.
let raw: [u8; 32] = hex::decode(&original.update().updater.health_public_key_hex)?
.try_into()
.map_err(|_| anyhow::anyhow!("UPDATE_RECORD_HEALTH_KEY"))?;
let public = VerifyingKey::from_bytes(&raw)?;
ensure!(!public.is_weak(), "UPDATE_RECORD_HEALTH_KEY");
let mut message = HEALTH_DOMAIN.to_vec();
message.extend_from_slice(&signed.payload);
public.verify_strict(&message, &Signature::from_slice(&signed.signature)?)?;
ensure!(
receipt.schema_version == 1
&& receipt.handoff_sha256 == handoff_sha256
&& receipt.owner_nonce == header.owner_nonce
&& receipt.boot_id == header.boot_id
&& receipt.helper_journal_id == header.helper_journal_id
&& receipt.agent_journal_id == header.agent_journal_id
&& receipt.connection_generation > header.connection_generation
&& !receipt.ready
&& receipt.issued_at >= header.consumed_at
&& receipt.issued_at <= event.observed_at
&& event.observed_at < receipt.valid_until
&& receipt.valid_until <= header.finish_before,
"UPDATE_RECORD_HEALTH_BINDING"
);
Self::assemble(
original.update(),
prior_raw,
&handoff_sha256,
signed,
&receipt,
header.attempt.grant_id,
header.owner_nonce,
)
}
fn assemble(
update: &ResolvedAgentUpdate,
prior_raw: &[u8],
handoff_sha256: &str,
signed: &SignedHealth,
receipt: &HealthReceipt,
grant_id: Uuid,
owner_nonce: Uuid,
) -> Result<Self> {
ensure!(
update.validate() && !grant_id.is_nil() && !owner_nonce.is_nil(),
"UPDATE_RECORD_INPUT"
);
let prior = InstalledReleaseRecord::from_bytes(prior_raw).map_err(anyhow::Error::msg)?;
let parent = prior
.release_envelope
.decode()
.map_err(anyhow::Error::msg)?;
let target = update.manifest().map_err(anyhow::Error::msg)?;
let old_sha256 = hash(prior_raw);
ensure!(
!prior.rolled_back
&& old_sha256 == update.predecessor.installed_record_sha256
&& prior.manifest_sha256 == update.predecessor.manifest_sha256
&& parent.manifest.release_id == update.predecessor.release_id
&& parent.manifest.release_sequence == update.predecessor.release_sequence
&& parent.manifest.artifacts[0].sha256 == update.predecessor.agent_sha256
&& parent.manifest.artifacts[1].sha256 == update.predecessor.helper_sha256
&& prior.platform == target.platform
&& target.release_sequence > prior.activation_floor
&& receipt.issued_at >= prior.activated_at,
"UPDATE_RECORD_PREDECESSOR_OR_FLOOR"
);
let record = InstalledReleaseRecord {
schema_version: 1,
profile: "decoder_compatible_handoff_v1".into(),
platform: target.platform,
release_envelope: update.release_envelope.clone(),
manifest_sha256: update.manifest_sha256.clone(),
activation_floor: target.release_sequence,
activated_at: receipt.issued_at,
previous_record_sha256: Some(old_sha256.clone()),
activation_evidence: ActivationEvidence::Updated {
handoff_sha256: handoff_sha256.into(),
signed_health_sha256: hash(&serde_json::to_vec(signed)?),
},
rolled_back: false,
};
let raw = record.bytes().map_err(anyhow::Error::msg)?;
let sha256 = hash(&raw);
ensure!(sha256 != old_sha256, "UPDATE_RECORD_SAME_BYTES");
Ok(Self {
record,
raw,
prior_raw: prior_raw.to_vec(),
old_sha256,
sha256,
grant_id,
owner_nonce,
})
}
pub fn raw(&self) -> &[u8] {
&self.raw
}
pub fn record(&self) -> &InstalledReleaseRecord {
&self.record
}
pub fn sha256(&self) -> &str {
&self.sha256
}
pub fn old_sha256(&self) -> &str {
&self.old_sha256
}
pub fn grant_id(&self) -> Uuid {
self.grant_id
}
pub fn owner_nonce(&self) -> Uuid {
self.owner_nonce
}
}
/// The concrete Fullhelper adapter commits an original Core claim before each
/// rename and its original observation afterwards. No permissive implementation.
pub trait RecordFence {
fn before_effect(
&mut self,
effect: &NativeEffect,
namespace: &RecordNamespaceProof<'_>,
) -> Result<()>;
fn after_effect(
&mut self,
effect: &NativeEffect,
namespace: &RecordNamespaceProof<'_>,
observation_sha256: &str,
) -> Result<()>;
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum RecordPhase {
Prepared,
Saved,
Published,
}
struct HeldDirectory {
protected: Directory,
fd: File,
identity: Identity,
}
fn identity(meta: &Metadata) -> Identity {
Identity {
device: meta.dev(),
inode: meta.ino(),
uid: meta.uid(),
gid: meta.gid(),
mode: meta.mode(),
links: meta.nlink(),
}
}
fn component(value: &str) -> Result<CString> {
ensure!(
!value.is_empty() && value != "." && value != ".." && !value.contains('/'),
"UPDATE_RECORD_COMPONENT"
);
Ok(CString::new(value)?)
}
fn openat(dir: &File, name: &str, flags: i32, mode: u32) -> Result<File> {
let name = component(name)?;
// SAFETY: live held dirfd and one validated NUL-terminated component. Each
// successful descriptor is immediately owned by exactly one File.
let fd = unsafe {
libc::openat(
dir.as_raw_fd(),
name.as_ptr(),
flags | libc::O_NOFOLLOW | libc::O_CLOEXEC,
mode,
)
};
ensure!(
fd >= 0,
"UPDATE_RECORD_OPEN: {}",
std::io::Error::last_os_error()
);
Ok(unsafe { File::from_raw_fd(fd) })
}
impl HeldDirectory {
fn open(path: &Path) -> Result<Self> {
let protected = Directory::open(path, 0, true)?;
let fd = OpenOptions::new()
.read(true)
.custom_flags(libc::O_DIRECTORY | libc::O_NOFOLLOW | libc::O_CLOEXEC)
.open(path)?;
let actual = identity(&fd.metadata()?);
ensure!(
&actual == protected.identity() && actual.mode & 0o777 == 0o700,
"UPDATE_RECORD_DIRECTORY_BINDING"
);
let result = Self {
protected,
fd,
identity: actual,
};
result.verify()?;
Ok(result)
}
fn verify(&self) -> Result<()> {
self.protected.verify()?;
ensure!(
identity(&self.fd.metadata()?) == self.identity,
"UPDATE_RECORD_DIRECTORY_CHANGED"
);
Ok(())
}
fn absent(&self, name: &str) -> Result<()> {
let name = component(name)?;
let mut value = std::mem::MaybeUninit::<libc::stat>::uninit();
let result = unsafe {
libc::fstatat(
self.fd.as_raw_fd(),
name.as_ptr(),
value.as_mut_ptr(),
libc::AT_SYMLINK_NOFOLLOW,
)
};
ensure!(
result < 0 && std::io::Error::last_os_error().raw_os_error() == Some(libc::ENOENT),
"UPDATE_RECORD_DESTINATION_EXISTS"
);
Ok(())
}
fn named(&self, name: &str, held: &RecordFile) -> Result<()> {
self.verify()?;
held.verify()?;
let named = openat(&self.fd, name, libc::O_RDONLY | libc::O_NONBLOCK, 0)?;
ensure!(
file_proof(&named.metadata()?, &held.proof.sha256)? == held.proof,
"UPDATE_RECORD_NAMED_FILE_CHANGED"
);
// A held inode and a same-byte different named inode are distinct.
held.verify()?;
self.verify()
}
fn sync(&self) -> Result<()> {
self.fd.sync_all()?;
Ok(())
}
fn rename_new(&self, from: &str, to: &str) -> Result<()> {
let from = component(from)?;
let to = component(to)?;
let result = unsafe {
libc::renameat2(
self.fd.as_raw_fd(),
from.as_ptr(),
self.fd.as_raw_fd(),
to.as_ptr(),
libc::RENAME_NOREPLACE,
)
};
ensure!(
result == 0,
"UPDATE_RECORD_RENAME: {}",
std::io::Error::last_os_error()
);
self.sync()
}
fn create(&self, name: &str, raw: &[u8]) -> Result<RecordFile> {
self.verify()?;
ensure!(
!raw.is_empty() && raw.len() <= MAX_RECORD_BYTES,
"UPDATE_RECORD_SIZE"
);
let mut file = openat(
&self.fd,
name,
libc::O_RDWR | libc::O_CREAT | libc::O_EXCL,
0o600,
)?;
file.write_all(raw)?;
file.sync_all()?;
self.sync()?;
let held = RecordFile::from_file(file, raw)?;
self.named(name, &held)?;
Ok(held)
}
fn archive(&self, name: &str, raw: &[u8]) -> Result<RecordFile> {
let name_c = component(name)?;
let mut value = std::mem::MaybeUninit::<libc::stat>::uninit();
let result = unsafe {
libc::fstatat(
self.fd.as_raw_fd(),
name_c.as_ptr(),
value.as_mut_ptr(),
libc::AT_SYMLINK_NOFOLLOW,
)
};
if result < 0 {
ensure!(
std::io::Error::last_os_error().raw_os_error() == Some(libc::ENOENT),
"UPDATE_RECORD_ARCHIVE_STAT"
);
return self.create(name, raw);
}
let file = RecordFile::open(self, name, raw)?;
self.named(name, &file)?;
Ok(file)
}
}
fn file_proof(meta: &Metadata, sha256: &str) -> Result<FileProof> {
ensure!(
meta.is_file()
&& meta.uid() == 0
&& meta.mode() & 0o7777 == 0o600
&& meta.nlink() == 1
&& meta.len() > 0
&& meta.len() <= MAX_RECORD_BYTES as u64,
"UPDATE_RECORD_PROTECTED_FILE"
);
Ok(FileProof {
identity: identity(meta),
bytes: meta.len(),
sha256: sha256.into(),
})
}
fn stamp(meta: &Metadata) -> (Identity, u64, i64, i64, i64, i64) {
(
identity(meta),
meta.len(),
meta.mtime(),
meta.mtime_nsec(),
meta.ctime(),
meta.ctime_nsec(),
)
}
struct RecordFile {
fd: File,
proof: FileProof,
}
impl RecordFile {
fn open(dir: &HeldDirectory, name: &str, raw: &[u8]) -> Result<Self> {
ensure!(
!raw.is_empty() && raw.len() <= MAX_RECORD_BYTES,
"UPDATE_RECORD_SIZE"
);
let fd = openat(&dir.fd, name, libc::O_RDONLY | libc::O_NONBLOCK, 0)?;
Self::from_file(fd, raw)
}
fn from_file(fd: File, raw: &[u8]) -> Result<Self> {
ensure!(
!raw.is_empty() && raw.len() <= MAX_RECORD_BYTES,
"UPDATE_RECORD_SIZE"
);
let proof = file_proof(&fd.metadata()?, &hash(raw))?;
ensure!(proof.bytes == raw.len() as u64, "UPDATE_RECORD_LENGTH");
let result = Self { fd, proof };
result.verify()?;
Ok(result)
}
fn verify(&self) -> Result<()> {
let before = self.fd.metadata()?;
ensure!(
file_proof(&before, &self.proof.sha256)? == self.proof,
"UPDATE_RECORD_HELD_FILE_CHANGED"
);
let mut raw = vec![0; self.proof.bytes as usize];
self.fd.read_exact_at(&mut raw, 0)?;
let mut extra = [0; 1];
ensure!(
self.fd.read_at(&mut extra, self.proof.bytes)? == 0
&& hash(&raw) == self.proof.sha256
&& stamp(&before) == stamp(&self.fd.metadata()?),
"UPDATE_RECORD_HELD_BYTES_CHANGED"
);
Ok(())
}
}
pub struct RecordTransition {
built: BuiltRecord,
directory: HeldDirectory,
archives: HeldDirectory,
old: RecordFile,
staged: RecordFile,
archived_old: RecordFile,
archived_new: RecordFile,
stage_name: String,
previous_name: String,
phase: RecordPhase,
poisoned: bool,
}
impl RecordTransition {
/// Call only after independently authorized, still-current original Health.
/// Creates bounded private staging/archive bytes; never changes Current.
pub fn prepare(built: BuiltRecord, budget: &Budget) -> Result<Self> {
Self::prepare_in(Path::new(ROOT), built, budget)
}
fn prepare_in(path: &Path, built: BuiltRecord, budget: &Budget) -> Result<Self> {
budget.check()?;
let directory = HeldDirectory::open(path)?;
let archives = HeldDirectory::open(&path.join("records"))?;
let stage_name = format!(".rmm-record-{}.stage", built.owner_nonce);
let previous_name = format!(".rmm-record-{}.previous", built.owner_nonce);
directory.absent(&stage_name)?;
directory.absent(&previous_name)?;
let old = RecordFile::open(&directory, CURRENT, &built.prior_raw)?;
directory.named(CURRENT, &old)?;
let archived_old =
archives.archive(&format!("{}.json", built.old_sha256), &built.prior_raw)?;
budget.check()?;
let archived_new = archives.archive(&format!("{}.json", built.sha256), &built.raw)?;
budget.check()?;
let staged = directory.create(&stage_name, &built.raw)?;
let result = Self {
built,
directory,
archives,
old,
staged,
archived_old,
archived_new,
stage_name,
previous_name,
phase: RecordPhase::Prepared,
poisoned: false,
};
result.namespace_proof().verify()?;
budget.check()?;
Ok(result)
}
pub fn namespace_proof(&self) -> RecordNamespaceProof<'_> {
RecordNamespaceProof { transition: self }
}
pub fn save_previous(&mut self, fence: &mut impl RecordFence, budget: &Budget) -> Result<()> {
ensure!(
!self.poisoned && self.phase == RecordPhase::Prepared,
"UPDATE_RECORD_NO_SAVE_RESUME"
);
self.poisoned = true;
let effect = NativeEffect::SaveInstalledRecord {
old_sha256: self.built.old_sha256.clone(),
new_sha256: self.built.sha256.clone(),
};
self.namespace_proof().verify()?;
budget.check()?;
fence.before_effect(&effect, &self.namespace_proof())?;
self.namespace_proof().verify()?;
budget.check()?;
self.directory.rename_new(CURRENT, &self.previous_name)?;
self.phase = RecordPhase::Saved;
self.namespace_proof().verify()?;
let observed = self.namespace_proof().observation_sha256()?;
fence.after_effect(&effect, &self.namespace_proof(), &observed)?;
budget.check()?;
self.poisoned = false;
Ok(())
}
pub fn publish(&mut self, fence: &mut impl RecordFence, budget: &Budget) -> Result<()> {
ensure!(
!self.poisoned && self.phase == RecordPhase::Saved,
"UPDATE_RECORD_NO_PUBLISH_RESUME"
);
self.poisoned = true;
let effect = NativeEffect::PublishInstalledRecord {
old_sha256: self.built.old_sha256.clone(),
new_sha256: self.built.sha256.clone(),
};
self.namespace_proof().verify()?;
budget.check()?;
fence.before_effect(&effect, &self.namespace_proof())?;
self.namespace_proof().verify()?;
budget.check()?;
self.directory.rename_new(&self.stage_name, CURRENT)?;
self.phase = RecordPhase::Published;
self.namespace_proof().verify()?;
let observed = self.namespace_proof().observation_sha256()?;
fence.after_effect(&effect, &self.namespace_proof(), &observed)?;
budget.check()?;
self.poisoned = false;
Ok(())
}
}
/// Borrowed held-descriptor proof, not serializable/constructible from IPC and
/// never a flag authorizing a general TrustState skip. verify() checks the whole
/// precise allowed namespace, including both create-only history copies.
pub struct RecordNamespaceProof<'a> {
transition: &'a RecordTransition,
}
impl RecordNamespaceProof<'_> {
pub fn phase(&self) -> RecordPhase {
self.transition.phase
}
pub fn record(&self) -> &BuiltRecord {
&self.transition.built
}
pub fn directory_identity(&self) -> &Identity {
&self.transition.directory.identity
}
pub fn old_file(&self) -> &FileProof {
&self.transition.old.proof
}
pub fn new_file(&self) -> &FileProof {
&self.transition.staged.proof
}
pub fn old_sha256(&self) -> &str {
self.transition.built.old_sha256()
}
pub fn new_sha256(&self) -> &str {
self.transition.built.sha256()
}
pub fn grant_id(&self) -> Uuid {
self.transition.built.grant_id()
}
pub fn owner_nonce(&self) -> Uuid {
self.transition.built.owner_nonce()
}
pub fn verify(&self) -> Result<()> {
let t = self.transition;
t.directory.verify()?;
t.archives.verify()?;
t.old.verify()?;
t.staged.verify()?;
t.archives
.named(&format!("{}.json", t.built.old_sha256), &t.archived_old)?;
t.archives
.named(&format!("{}.json", t.built.sha256), &t.archived_new)?;
ensure!(
t.old.proof.sha256 == t.built.old_sha256
&& t.staged.proof.sha256 == t.built.sha256
&& t.old.proof.identity.inode != t.staged.proof.identity.inode
&& t.old.proof.identity.device == t.staged.proof.identity.device,
"UPDATE_RECORD_PAIR_CHANGED"
);
match t.phase {
RecordPhase::Prepared => {
t.directory.named(CURRENT, &t.old)?;
t.directory.named(&t.stage_name, &t.staged)?;
t.directory.absent(&t.previous_name)?;
}
RecordPhase::Saved => {
t.directory.named(&t.previous_name, &t.old)?;
t.directory.named(&t.stage_name, &t.staged)?;
t.directory.absent(CURRENT)?;
}
RecordPhase::Published => {
t.directory.named(&t.previous_name, &t.old)?;
t.directory.named(CURRENT, &t.staged)?;
t.directory.absent(&t.stage_name)?;
}
}
t.directory.verify()?;
t.archives.verify()
}
pub fn observation_sha256(&self) -> Result<String> {
self.verify()?;
#[derive(Serialize)]
struct Observation<'a> {
grant_id: Uuid,
owner_nonce: Uuid,
phase: RecordPhase,
directory: &'a Identity,
archives: &'a Identity,
old: &'a FileProof,
new: &'a FileProof,
archived_old: &'a FileProof,
archived_new: &'a FileProof,
}
let t = self.transition;
Ok(hash(&serde_json::to_vec(&Observation {
grant_id: self.grant_id(),
owner_nonce: self.owner_nonce(),
phase: t.phase,
directory: &t.directory.identity,
archives: &t.archives.identity,
old: &t.old.proof,
new: &t.staged.proof,
archived_old: &t.archived_old.proof,
archived_new: &t.archived_new.proof,
})?))
}
}
#[cfg(test)]
mod tests {
// Structural record bytes and actual Root-owned scratch IO only. Synthetic
// codec signatures below are NOT accepted by the public authenticated build
// method, never a real release/Health/approval or a simulated target job.
use super::*;
use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD};
use std::{fs, os::unix::fs::DirBuilderExt, path::PathBuf, time::Duration};
struct Fixture(PathBuf);
impl Fixture {
fn new(raw: &[u8]) -> Self {
assert_eq!(
unsafe { libc::geteuid() },
0,
"Run only in the isolated Root container scratch fixture, never as a host daemon"
);
let path = PathBuf::from(format!("/run/rmm-record-unit-{}", Uuid::new_v4()));
fs::DirBuilder::new().mode(0o700).create(&path).unwrap();
fs::DirBuilder::new()
.mode(0o700)
.create(path.join("records"))
.unwrap();
let mut file = OpenOptions::new()
.write(true)
.create_new(true)
.mode(0o600)
.open(path.join(CURRENT))
.unwrap();
file.write_all(raw).unwrap();
file.sync_all().unwrap();
Self(path)
}
}
impl Drop for Fixture {
fn drop(&mut self) {
fs::remove_dir_all(&self.0).unwrap();
}
}
fn budget() -> Budget {
let boot = fs::read_to_string("/proc/sys/kernel/random/boot_id")
.unwrap()
.trim()
.parse()
.unwrap();
Budget::new(boot, Duration::from_secs(10)).unwrap()
}
fn fixture_record() -> BuiltRecord {
let golden: serde_json::Value = serde_json::from_str(include_str!(
"../../../tests/agent_update/codec-golden.json"
))
.unwrap();
let mut update: ResolvedAgentUpdate = serde_json::from_value(
golden["linux_full"]["payload"]["jobs"][0]["payload"]["action"]["update"].clone(),
)
.unwrap();
let mut prior_manifest = update.manifest().unwrap();
prior_manifest.release_id = update.predecessor.release_id;
prior_manifest.release_sequence = update.predecessor.release_sequence;
prior_manifest.artifacts[0].sha256 = update.predecessor.agent_sha256.clone();
prior_manifest.artifacts[1].sha256 = update.predecessor.helper_sha256.clone();
let mut prior_envelope = update.release_envelope.clone();
let payload = serde_json::to_vec(&prior_manifest).unwrap();
prior_envelope.payload_bytes = URL_SAFE_NO_PAD.encode(&payload);
let prior = InstalledReleaseRecord {
schema_version: 1,
profile: "decoder_compatible_handoff_v1".into(),
platform: prior_manifest.platform,
release_envelope: prior_envelope,
manifest_sha256: hash(&payload),
activation_floor: prior_manifest.release_sequence,
activated_at: 100,
previous_record_sha256: None,
activation_evidence: ActivationEvidence::Bootstrap {
qualification_receipt_sha256: "12".repeat(32),
},
rolled_back: false,
};
let prior_raw = prior.bytes().unwrap();
update.predecessor.installed_record_sha256 = hash(&prior_raw);
update.predecessor.manifest_sha256 = prior.manifest_sha256;
let receipt = HealthReceipt {
schema_version: 1,
handoff_sha256: "34".repeat(32),
owner_nonce: Uuid::new_v4(),
boot_id: Uuid::new_v4(),
helper_journal_id: Uuid::new_v4(),
agent_journal_id: Uuid::new_v4(),
connection_generation: 2,
service_processes: vec![],
ledger_identity_sha256: "56".repeat(32),
signed_sync_decision_id: Uuid::new_v4(),
signed_sync_request_sha256: "78".repeat(32),
signed_sync_response_sha256: "9a".repeat(32),
signed_sync_applied_at: 120,
ready: false,
inventory_message_id: Uuid::new_v4(),
inventory_received_at: 120,
inventory_ack_sha256: "bc".repeat(32),
issued_at: 123,
valid_until: 150,
};
let health = SignedHealth {
payload: serde_json::to_vec(&receipt).unwrap(),
signature: vec![0; 64],
};
let result = BuiltRecord::assemble(
&update,
&prior_raw,
&receipt.handoff_sha256,
&health,
&receipt,
Uuid::new_v4(),
receipt.owner_nonce,
)
.unwrap();
assert_eq!(result.record.activated_at, 123);
assert_eq!(
result.record.activation_floor,
update.manifest().unwrap().release_sequence
);
assert_eq!(
result.record.previous_record_sha256.as_deref(),
Some(hash(&prior_raw).as_str())
);
let ActivationEvidence::Updated {
signed_health_sha256,
..
} = &result.record.activation_evidence
else {
panic!("fixture")
};
assert_eq!(
*signed_health_sha256,
hash(&serde_json::to_vec(&health).unwrap())
);
result
}
#[derive(Default)]
struct CheckedFence {
calls: Vec<(bool, RecordPhase)>,
}
impl RecordFence for CheckedFence {
fn before_effect(
&mut self,
effect: &NativeEffect,
proof: &RecordNamespaceProof<'_>,
) -> Result<()> {
proof.verify()?;
let phase = match effect {
NativeEffect::SaveInstalledRecord {
old_sha256,
new_sha256,
} => {
ensure!(
old_sha256 == proof.old_sha256() && new_sha256 == proof.new_sha256(),
"fixture pair"
);
RecordPhase::Prepared
}
NativeEffect::PublishInstalledRecord {
old_sha256,
new_sha256,
} => {
ensure!(
old_sha256 == proof.old_sha256() && new_sha256 == proof.new_sha256(),
"fixture pair"
);
RecordPhase::Saved
}
_ => anyhow::bail!("fixture unexpected effect"),
};
ensure!(proof.phase() == phase, "fixture phase");
self.calls.push((true, phase));
Ok(())
}
fn after_effect(
&mut self,
effect: &NativeEffect,
proof: &RecordNamespaceProof<'_>,
observation: &str,
) -> Result<()> {
let phase = match effect {
NativeEffect::SaveInstalledRecord { .. } => RecordPhase::Saved,
NativeEffect::PublishInstalledRecord { .. } => RecordPhase::Published,
_ => anyhow::bail!("fixture unexpected effect"),
};
ensure!(
proof.phase() == phase && observation == proof.observation_sha256()?,
"fixture observation"
);
self.calls.push((false, phase));
Ok(())
}
}
#[test]
fn separate_record_renames_preserve_original_inode_and_both_raw_archives() {
let built = fixture_record();
let fixture = Fixture::new(&built.prior_raw);
let budget = budget();
let old_raw = built.prior_raw.clone();
let new_raw = built.raw.clone();
let old_hash = built.old_sha256.clone();
let new_hash = built.sha256.clone();
let mut transition = RecordTransition::prepare_in(&fixture.0, built, &budget).unwrap();
let original = transition.namespace_proof().old_file().identity.clone();
let mut fence = CheckedFence::default();
transition.save_previous(&mut fence, &budget).unwrap();
assert!(!fixture.0.join(CURRENT).exists());
assert_eq!(
fs::read(fixture.0.join(&transition.previous_name)).unwrap(),
old_raw
);
transition.publish(&mut fence, &budget).unwrap();
assert_eq!(fs::read(fixture.0.join(CURRENT)).unwrap(), new_raw);
assert_eq!(&transition.namespace_proof().old_file().identity, &original);
assert_eq!(
fs::read(fixture.0.join("records").join(format!("{old_hash}.json"))).unwrap(),
old_raw
);
assert_eq!(
fs::read(fixture.0.join("records").join(format!("{new_hash}.json"))).unwrap(),
new_raw
);
assert_eq!(
fence.calls,
vec![
(true, RecordPhase::Prepared),
(false, RecordPhase::Saved),
(true, RecordPhase::Saved),
(false, RecordPhase::Published)
]
);
assert!(transition.publish(&mut fence, &budget).is_err());
}
#[test]
fn same_byte_inode_substitution_and_foreign_current_block_without_resume() {
let built = fixture_record();
let fixture = Fixture::new(&built.prior_raw);
let budget = budget();
let old_raw = built.prior_raw.clone();
let mut transition = RecordTransition::prepare_in(&fixture.0, built, &budget).unwrap();
fs::rename(fixture.0.join(CURRENT), fixture.0.join("retained-original")).unwrap();
let mut file = OpenOptions::new()
.write(true)
.create_new(true)
.mode(0o600)
.open(fixture.0.join(CURRENT))
.unwrap();
file.write_all(&old_raw).unwrap();
file.sync_all().unwrap();
let mut fence = CheckedFence::default();
assert!(transition.save_previous(&mut fence, &budget).is_err());
assert!(fence.calls.is_empty());
assert!(transition.save_previous(&mut fence, &budget).is_err());
assert_eq!(
fs::read(fixture.0.join("retained-original")).unwrap(),
old_raw
);
let built = fixture_record();
let other = Fixture::new(&built.prior_raw);
let mut transition = RecordTransition::prepare_in(&other.0, built, &budget).unwrap();
transition.save_previous(&mut fence, &budget).unwrap();
let mut foreign = OpenOptions::new()
.write(true)
.create_new(true)
.mode(0o600)
.open(other.0.join(CURRENT))
.unwrap();
foreign.write_all(b"foreign public fixture").unwrap();
foreign.sync_all().unwrap();
let calls = fence.calls.len();
assert!(transition.publish(&mut fence, &budget).is_err());
assert_eq!(fence.calls.len(), calls);
assert_eq!(
fs::read(other.0.join(CURRENT)).unwrap(),
b"foreign public fixture"
);
assert!(transition.publish(&mut fence, &budget).is_err());
}
}
+143 -1
View File
@@ -26,7 +26,8 @@ pub struct RetainedFileSpec {
pub identity: Identity,
pub retention: Retention,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct Stamp {
size: u64,
mtime: i64,
@@ -120,6 +121,59 @@ impl RetainedSet {
pub fn sha256(&self) -> &str {
&self.digest
}
pub fn specs(&self) -> Vec<&RetainedFileSpec> {
self.files.iter().map(|file| &file.spec).collect()
}
/// Exact ORIGINAL spec/stamp bytes. No current timestamp is substituted.
pub fn bytes(&self) -> Result<Vec<u8>> {
let serial: Vec<_> = self
.files
.iter()
.map(|f| {
(
&f.spec,
if f.spec.retention == Retention::ImmutableMetadata {
Some(&f.stamp)
} else {
None
},
)
})
.collect();
let raw = serde_json::to_vec(&serial)?;
ensure!(
raw.len() <= 65536 && hash(&raw) == self.digest,
"UPDATE_RETAINED_WITNESS_CHANGED"
);
Ok(raw)
}
pub fn from_bound_bytes(raw: &[u8], handoff: &rmm_updater_handoff::Handoff) -> Result<Self> {
Self::from_bound_digest(raw, &handoff.retained_state_sha256)
}
fn from_bound_digest(raw: &[u8], original_sha256: &str) -> Result<Self> {
ensure!(
!raw.is_empty() && raw.len() <= 65536 && hash(raw) == original_sha256,
"UPDATE_RETAINED_WITNESS_HASH"
);
let snapshot: Vec<(RetainedFileSpec, Option<Stamp>)> = serde_json::from_slice(raw)?;
ensure!(
!snapshot.is_empty()
&& snapshot.len() <= 64
&& serde_json::to_vec(&snapshot)? == raw
&& snapshot.iter().all(|(spec, stamp)| (spec.retention
== Retention::ImmutableMetadata)
== stamp.is_some()),
"UPDATE_RETAINED_WITNESS_CODEC"
);
// Reopen current files but compare their actual stamps to the ORIGINAL
// canonical bytes. Any changed immutable stamp fails; never refresh it.
let current = Self::capture(snapshot.into_iter().map(|(spec, _)| spec).collect())?;
ensure!(
current.bytes()? == raw && current.digest == original_sha256,
"UPDATE_RETAINED_ORIGINAL_STAMP_CHANGED"
);
Ok(current)
}
pub fn verify(&self) -> Result<()> {
for file in &self.files {
file.verify()?;
@@ -161,3 +215,91 @@ impl HeldState {
self.directory.verify()
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::{fs, io::Write, os::unix::fs::PermissionsExt};
struct Fixture(PathBuf);
impl Drop for Fixture {
fn drop(&mut self) {
fs::remove_dir_all(&self.0).unwrap();
}
}
#[test]
fn reopen_preserves_original_stamps_and_rejects_replaced_state_without_reading_keys() {
assert_eq!(
unsafe { libc::geteuid() },
0,
"Use isolated root-owned unit container, never a host daemon"
);
let path = PathBuf::from(format!(
"/run/rmm-worker-witness-unit-{}",
uuid::Uuid::new_v4()
));
fs::create_dir(&path).unwrap();
fs::set_permissions(&path, fs::Permissions::from_mode(0o700)).unwrap();
let f = Fixture(path);
let mut specs = Vec::new();
for (name, retention) in [
("unit-db", Retention::MutableDatabase),
("public-fixture", Retention::ImmutableMetadata),
] {
let path = f.0.join(name);
let mut file = fs::OpenOptions::new()
.create_new(true)
.write(true)
.mode(0o600)
.open(&path)
.unwrap();
file.write_all(b"public unit fixture").unwrap();
file.sync_all().unwrap();
specs.push(RetainedFileSpec {
path,
parent_uid: 0,
identity: identity(&file.metadata().unwrap()),
retention,
});
}
let captured = RetainedSet::capture(specs).unwrap();
let original = captured.bytes().unwrap();
let sha = captured.sha256().to_owned();
fs::OpenOptions::new()
.append(true)
.open(f.0.join("unit-db"))
.unwrap()
.write_all(b" advanced")
.unwrap();
assert_eq!(
RetainedSet::from_bound_digest(&original, &sha)
.unwrap()
.bytes()
.unwrap(),
original
);
fs::rename(f.0.join("unit-db"), f.0.join("old-db")).unwrap();
let mut replacement = fs::OpenOptions::new()
.create_new(true)
.write(true)
.mode(0o600)
.open(f.0.join("unit-db"))
.unwrap();
replacement
.write_all(b"same name does not preserve identity")
.unwrap();
assert!(RetainedSet::from_bound_digest(&original, &sha).is_err());
drop(replacement);
fs::remove_file(f.0.join("unit-db")).unwrap();
fs::rename(f.0.join("old-db"), f.0.join("unit-db")).unwrap();
assert!(RetainedSet::from_bound_digest(&original, &sha).is_ok());
fs::OpenOptions::new()
.append(true)
.open(f.0.join("public-fixture"))
.unwrap()
.write_all(b" changed")
.unwrap();
assert!(RetainedSet::from_bound_digest(&original, &sha).is_err());
assert!(captured.verify().is_err());
assert_eq!(captured.bytes().unwrap(), original);
}
}
+312
View File
@@ -0,0 +1,312 @@
//! Already committed ReturnIntent and exact once-only start reservations.
//! This path owns NO SQLite connection and never writes beside a running helper.
use crate::{
bridge::FullBridge, clock::NativeClock, files::FileWitnesses, original::Original,
participants::StableParticipant, retained::RetainedSet, trust::TrustState,
};
use anyhow::{Result, ensure};
use rmm_agent_core::{
auth::Binding,
update_handoff::{NativeControlOperation, NativeEffect, NativeService, PreparedReturnClaims},
};
use rmm_domain::{agent_update::UpdateInflightProof, jobs::hash};
use rmm_updater_linux::{
Budget, DurableEffectFence, Effect,
files::{Directory, Identity, LedgerIdentity},
services::{Service, ServiceProof, Systemd},
};
use std::{
fs::OpenOptions,
os::{
fd::AsRawFd,
unix::fs::{MetadataExt, OpenOptionsExt},
},
path::Path,
};
pub(crate) struct Context<'a> {
pub original: &'a Original,
pub binding: &'a Binding,
pub clock: &'a NativeClock,
pub budget: &'a Budget,
pub updater: &'a StableParticipant,
pub retained: &'a RetainedSet,
pub files: &'a FileWitnesses,
pub trust: &'a TrustState,
pub ledger: LedgerIdentity,
pub old_services: Vec<ServiceProof>,
}
pub struct Inputs<'a> {
pub original: &'a Original,
pub binding: &'a Binding,
pub clock: &'a NativeClock,
pub budget: &'a Budget,
pub updater: &'a StableParticipant,
pub retained: &'a RetainedSet,
pub files: &'a FileWitnesses,
pub trust: &'a TrustState,
}
/// Contains no borrow of the old guard: SQLite closes before actual flock
/// release, then the stopped-service value may be consumed by the start path.
pub struct PreparedReservation {
pub(crate) claims: PreparedReturnClaims,
pub(crate) proof: UpdateInflightProof,
pub(crate) ledger: LedgerIdentity,
pub(crate) old_services: Vec<ServiceProof>,
}
impl PreparedReservation {
pub fn bind(self, input: Inputs<'_>) -> Result<Reservation<'_>> {
Reservation::after_closed_connection(
Context {
original: input.original,
binding: input.binding,
clock: input.clock,
budget: input.budget,
updater: input.updater,
retained: input.retained,
files: input.files,
trust: input.trust,
ledger: self.ledger,
old_services: self.old_services,
},
self.claims,
self.proof,
)
}
}
pub struct Reservation<'a> {
context: Context<'a>,
claims: PreparedReturnClaims,
proof: UpdateInflightProof,
}
impl<'a> Reservation<'a> {
pub(crate) fn after_closed_connection(
context: Context<'a>,
claims: PreparedReturnClaims,
proof: UpdateInflightProof,
) -> Result<Self> {
let result = Self {
context,
claims,
proof,
};
result.check()?;
Ok(result)
}
fn check(&self) -> Result<()> {
self.context.original.check_current(
self.context.binding,
self.context.clock,
self.context.budget,
)?;
self.context
.updater
.verify_delegation(self.context.original.delegation(), self.context.budget)?;
self.context.retained.verify()?;
self.context.files.verify_pair(self.context.budget)?;
self.context
.trust
.recheck(self.context.original, self.context.clock)?;
let h = self.context.original.handoff();
self.context.files.bind(h)?;
ensure!(
self.context.retained.sha256() == h.retained_state_sha256
&& self.context.ledger.sha256()? == h.ledger_identity_sha256,
"UPDATE_RETURN_WITNESS_BINDING"
);
let (observed, journal) = rmm_domain::agent_update::verify_inflight_proof(
&self.proof,
self.context.trust.health_public_key(),
)
.map_err(anyhow::Error::msg)?;
ensure!(
observed.bytes()? == h.bytes()?
&& journal.phase() == rmm_updater_handoff::Phase::LedgerReturnIntent
&& self.claims.return_ledger.effect() == &NativeEffect::ReturnLedger,
"UPDATE_RETURN_RESERVATION_BINDING"
);
for (index, claim) in self.claims.starts.iter().enumerate() {
ensure!(
claim.effect()
== &NativeEffect::StartService {
service: [
NativeService::General,
NativeService::Full,
NativeService::Agent
][index]
},
"UPDATE_RETURN_START_ORDER"
);
}
for claim in std::iter::once(&self.claims.return_ledger).chain(self.claims.starts.iter()) {
ensure!(
claim.grant_id() == h.attempt.grant_id
&& claim.owner_nonce() == h.owner_nonce
&& claim.head_sha256() == self.proof.head_sha256
&& u64::try_from(claim.claimed_at().timestamp())? >= h.consumed_at
&& u64::try_from(claim.claimed_at().timestamp())? < h.finish_before,
"UPDATE_RETURN_CLAIM_CHANGED"
);
}
verify_identity_files(&self.context.ledger)?;
Ok(())
}
/// The OS release primitive consumes its real guard, and the borrow on
/// StoppedServices then ends. This fence never closes/unlocks another PID.
pub fn start_fence<'b>(
self,
new: &'b Systemd,
before_agent: &'b mut FullBridge<'a>,
) -> StartFence<'a, 'b> {
StartFence {
reservation: self,
new,
before_agent,
return_checked: false,
next: 0,
pending: None,
poisoned: false,
}
}
pub fn proof(&self) -> &UpdateInflightProof {
&self.proof
}
}
pub struct StartFence<'a, 'b> {
reservation: Reservation<'a>,
new: &'b Systemd,
before_agent: &'b mut FullBridge<'a>,
return_checked: bool,
next: usize,
pending: Option<Service>,
poisoned: bool,
}
impl StartFence<'_, '_> {
pub fn finish(self) -> Result<()> {
ensure!(
!self.poisoned && self.return_checked && self.next == 3 && self.pending.is_none(),
"UPDATE_START_SEQUENCE_INCOMPLETE"
);
Ok(())
}
}
impl DurableEffectFence for StartFence<'_, '_> {
fn before_effect(&mut self, effect: &Effect) -> Result<()> {
ensure!(
!self.poisoned && self.pending.is_none(),
"UPDATE_START_NO_RETRY"
);
self.poisoned = true;
self.reservation.check()?;
match *effect {
Effect::ReturnLedger => {
ensure!(
!self.return_checked && self.next == 0,
"UPDATE_RETURN_ALREADY_USED"
);
self.return_checked = true;
self.poisoned = false;
}
Effect::StartService { service } => {
ensure!(
self.return_checked
&& self.next < 3
&& service == [Service::General, Service::Full, Service::Agent][self.next],
"UPDATE_START_ORDER"
);
if self.next == 0 {
verify_lock_released(&self.reservation.context.ledger)?;
}
if service == Service::Agent {
// The new Full is now alive. A terminal/Unknown original
// or lost owner must deny the remaining Agent start too.
self.before_agent
.call(NativeControlOperation::ReadOriginal)?;
}
self.pending = Some(service);
}
_ => anyhow::bail!("UPDATE_RETURN_FENCE_EFFECT_DENIED"),
}
Ok(())
}
fn after_effect(&mut self, effect: &Effect, observation: &str) -> Result<()> {
let Some(service) = self.pending.take() else {
anyhow::bail!("UPDATE_START_NOT_RESERVED")
};
ensure!(
self.poisoned && *effect == Effect::StartService { service },
"UPDATE_START_CALLBACK_CHANGED"
);
self.reservation.check()?;
let held = self.new.observe(service, self.reservation.context.budget)?;
let prior = self
.reservation
.context
.old_services
.iter()
.find(|s| s.service == service)
.ok_or_else(|| anyhow::anyhow!("UPDATE_START_ORIGINAL_SERVICE_MISSING"))?;
ensure!(
held.proof().enabled == prior.enabled
&& !self
.reservation
.context
.old_services
.iter()
.any(|old| old.process.pid == held.proof().process.pid)
&& hash(&serde_json::to_vec(held.proof())?) == observation,
"UPDATE_START_NATIVE_OBSERVATION_CHANGED"
);
self.next += 1;
self.poisoned = false;
Ok(())
}
}
fn identity(meta: &std::fs::Metadata) -> Identity {
Identity {
device: meta.dev(),
inode: meta.ino(),
uid: meta.uid(),
gid: meta.gid(),
mode: meta.mode(),
links: meta.nlink(),
}
}
fn verify_identity_files(expected: &LedgerIdentity) -> Result<()> {
let dir = Directory::open(Path::new("/var/lib/rmm-helper"), 0, true)?;
for (name, expected) in [
("consumption.sqlite", &expected.ledger),
("executor.lock", &expected.lock),
] {
let file = OpenOptions::new()
.read(true)
.custom_flags(libc::O_NOFOLLOW | libc::O_NONBLOCK | libc::O_CLOEXEC)
.open(Path::new("/var/lib/rmm-helper").join(name))?;
ensure!(
file.metadata()?.is_file() && identity(&file.metadata()?) == *expected,
"UPDATE_RETURN_FILE_IDENTITY_CHANGED"
);
}
dir.verify()
}
fn verify_lock_released(expected: &LedgerIdentity) -> Result<()> {
verify_identity_files(expected)?;
let lock = OpenOptions::new()
.read(true)
.write(true)
.custom_flags(libc::O_NOFOLLOW | libc::O_CLOEXEC)
.open("/var/lib/rmm-helper/executor.lock")?;
ensure!(
identity(&lock.metadata()?) == expected.lock,
"UPDATE_RETURN_LOCK_CHANGED"
);
ensure!(
unsafe { libc::flock(lock.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) } == 0,
"UPDATE_OLD_GUARD_NOT_RELEASED"
);
ensure!(
unsafe { libc::flock(lock.as_raw_fd(), libc::LOCK_UN) } == 0,
"UPDATE_RETURN_PROBE_UNLOCK_FAILED"
);
verify_identity_files(expected)
}
+190 -25
View File
@@ -53,14 +53,26 @@ impl Protected {
}
}
pub struct TrustState {
updater_binding: rmm_domain::agent_update::UpdaterBinding,
predecessor: rmm_domain::agent_update::Predecessor,
protected: Vec<Protected>,
policy: LocalUpdatePolicy,
installed: InstalledReleaseRecord,
installed_raw: Vec<u8>,
keyring: UpdateKeyring,
keyring_file: UpdateKeyringFile,
baseline_signer_revoked: bool,
health_public_key: [u8; 32],
}
impl TrustState {
pub fn load(original: &Original, binding: &Binding, clock: &NativeClock) -> Result<Self> {
let result = Self::load_retained(original, binding)?;
result.recheck(original, clock)?;
Ok(result)
}
/// EXACT ORIGINAL evidence only. The separate historical keyring below
/// never supplies current admission, Health, Finalize or effect authority.
pub fn load_retained(original: &Original, binding: &Binding) -> Result<Self> {
let update = original.update();
let mut protected = Vec::new();
let (file, raw) = Protected::read(Path::new("/etc/rmm-agent-updater/policy.json"), 65536)?;
@@ -88,6 +100,7 @@ impl TrustState {
protected.push(file);
let job_key = hex::decode(&binding.public_key_hex)?;
let mut trusted = Vec::new();
let mut historical_keys = Vec::new();
let mut baseline_key = None;
for entry in &keys.keys {
ensure!(entry.platform == Platform::LinuxX64, "UPDATE_KEY_PLATFORM");
@@ -108,23 +121,30 @@ impl TrustState {
entry.last_sequence,
entry.revoked,
)?);
historical_keys.push(TrustedUpdateKey::new(
public,
ReleasePlatform::LinuxX64,
entry.first_sequence,
entry.last_sequence,
false,
)?);
}
let keyring = UpdateKeyring::new(keys.generation, trusted)?;
let historical = UpdateKeyring::new(keys.generation, historical_keys)?;
let baseline = policy
.initial_baseline
.decode()
.map_err(anyhow::Error::msg)?;
let (baseline_entry, public) = baseline_key.context("UPDATE_BASELINE_KEY_NOT_TRUSTED")?;
ensure!(
!baseline_entry.revoked
&& baseline
.record
.release_envelope
.decode()
.map_err(anyhow::Error::msg)?
.manifest
.release_sequence
>= baseline_entry.first_sequence
baseline
.record
.release_envelope
.decode()
.map_err(anyhow::Error::msg)?
.manifest
.release_sequence
>= baseline_entry.first_sequence
&& baseline
.record
.release_envelope
@@ -161,18 +181,23 @@ impl TrustState {
"UPDATE_INSTALLED_RECORD_CHANGED"
);
let installed = InstalledReleaseRecord::from_bytes(&raw).map_err(anyhow::Error::msg)?;
// This first concrete activation is bootstrap-bound. A future Updated
// predecessor must have its complete guarded Health/record chain proved;
// never accept its mutable fields merely because parsing succeeded.
ensure!(
matches!(
installed.activation_evidence,
ActivationEvidence::Bootstrap { .. }
) && raw == baseline.payload_bytes
&& hash(&raw) == policy.initial_record_sha256
&& installed.activation_floor == policy.initial_activation_floor,
"UPDATE_INSTALLED_CHAIN_NOT_QUALIFIED"
!installed.rolled_back
&& installed.activation_floor >= policy.initial_activation_floor
&& installed.activated_at <= original.handoff().consumed_at,
"UPDATE_INSTALLED_FLOOR_OR_TIME"
);
if matches!(
installed.activation_evidence,
ActivationEvidence::Bootstrap { .. }
) {
ensure!(
raw == baseline.payload_bytes
&& hash(&raw) == policy.initial_record_sha256
&& installed.activation_floor == policy.initial_activation_floor,
"UPDATE_INSTALLED_BASELINE_CHANGED"
);
}
let active = installed
.release_envelope
.decode()
@@ -185,13 +210,13 @@ impl TrustState {
&& active.manifest.artifacts[1].sha256 == update.predecessor.helper_sha256,
"UPDATE_INSTALLED_PREDECESSOR_BINDING"
);
verify_installed_release(&keyring, &installed, keys.generation)?;
verify_installed_release(&historical, &installed, keys.generation)?;
verify(
&keyring,
&historical,
&update.release_envelope,
&TrustedContext {
platform: ReleasePlatform::LinuxX64,
now_utc_seconds: u64::try_from(clock.now()?.timestamp())?,
now_utc_seconds: u64::try_from(original.delegation().prepared_at.timestamp())?,
keyring_generation: keys.generation,
protocol_major: active.manifest.protocol_major,
state_schema: active.manifest.state_schema,
@@ -204,18 +229,35 @@ impl TrustState {
},
)?;
protected.push(file);
let baseline_signer_revoked = baseline_entry.revoked;
Ok(Self {
updater_binding: update.updater.clone(),
predecessor: update.predecessor.clone(),
protected,
policy,
installed,
installed_raw: raw,
keyring,
keyring_file: keys,
baseline_signer_revoked,
health_public_key,
})
}
pub fn recheck(&self, original: &Original, clock: &NativeClock) -> Result<()> {
for file in &self.protected {
file.verify()?;
}
self.recheck_original_binding(original)?;
self.recheck_protected()?;
self.recheck_crypto(original, clock)
}
fn recheck_crypto(&self, original: &Original, clock: &NativeClock) -> Result<()> {
ensure!(
!self.baseline_signer_revoked,
"UPDATE_BASELINE_SIGNER_REVOKED"
);
verify_installed_release(
&self.keyring,
&self.installed,
original.update().updater.keyring_generation,
)?;
let active = self
.installed
.release_envelope
@@ -239,6 +281,129 @@ impl TrustState {
},
)
}
pub fn installed_raw(&self) -> &[u8] {
&self.installed_raw
}
/// Exact typed active-record namespace only. Every other protected name
/// remains mandatory; neither caller booleans nor path wildcards can skip it.
pub fn recheck_record_namespace(
&self,
original: &Original,
clock: &NativeClock,
namespace: &crate::record::RecordNamespaceProof<'_>,
) -> Result<()> {
self.recheck_original_binding(original)?;
self.recheck_protected_record_namespace(original, namespace)?;
self.recheck_crypto(original, clock)
}
pub fn recheck_protected_record_namespace(
&self,
original: &Original,
namespace: &crate::record::RecordNamespaceProof<'_>,
) -> Result<()> {
self.recheck_original_binding(original)?;
namespace.verify()?;
ensure!(
namespace.grant_id() == original.permit().grant_id
&& namespace.owner_nonce() == original.handoff().owner_nonce
&& namespace.old_sha256() == original.handoff().predecessor_record_sha256
&& namespace.old_sha256() == hash(&self.installed_raw),
"UPDATE_RECORD_NAMESPACE_ORIGINAL_BINDING"
);
let mut matched = 0;
for protected in &self.protected {
if protected.name == "installed-record.json"
&& protected.dir.identity() == namespace.directory_identity()
{
ensure!(
protected.file.proof() == namespace.old_file(),
"UPDATE_RECORD_ORIGINAL_HELD_IDENTITY_CHANGED"
);
matched += 1;
} else {
protected.verify()?;
}
}
ensure!(matched == 1, "UPDATE_RECORD_EXACT_PROTECTED_NAME_REQUIRED");
namespace.verify()
}
pub fn recheck_protected(&self) -> Result<()> {
for file in &self.protected {
file.verify()?;
}
Ok(())
}
pub fn recheck_original_binding(&self, original: &Original) -> Result<()> {
ensure!(
original.update().updater == self.updater_binding
&& original.update().predecessor == self.predecessor,
"UPDATE_TRUST_ORIGINAL_CONTEXT_CHANGED"
);
Ok(())
}
/// Full admission/Stop claims call this under THEIR actual guard. The
/// updater repeats it after all readers stop and before the first swap.
/// Never gate Unknown-only adjudication on this admission-oriented check.
pub fn qualify_history(
&self,
db: &rusqlite::Connection,
guard: &impl rmm_agent_core::helper_reconcile::ExecutorLedgerGuard,
binding: &Binding,
) -> Result<()> {
self.recheck_protected()?;
let baseline = self
.policy
.initial_baseline
.decode()
.map_err(anyhow::Error::msg)?;
let baseline_hash = hash(&baseline.payload_bytes);
let mut records = Vec::new();
let mut held = Vec::new();
let mut seen = std::collections::BTreeSet::new();
let mut next = self.installed.previous_record_sha256.clone();
let mut total = self.installed_raw.len() + baseline.payload_bytes.len();
while let Some(digest) = next {
ensure!(
rmm_domain::agent_update::digest(&digest)
&& seen.insert(digest.clone())
&& seen.len() < 64,
"UPDATE_RECORD_ARCHIVE_CHAIN"
);
if digest == baseline_hash {
break;
}
let (file, raw) = Protected::read(
&Path::new("/var/lib/rmm-agent-updater/records").join(format!("{digest}.json")),
32768,
)?;
ensure!(hash(&raw) == digest, "UPDATE_RECORD_ARCHIVE_HASH_CHANGED");
total = total
.checked_add(raw.len())
.context("UPDATE_RECORD_ARCHIVE_SIZE")?;
ensure!(total <= 2 * 1024 * 1024, "UPDATE_RECORD_ARCHIVE_SIZE");
let record = InstalledReleaseRecord::from_bytes(&raw).map_err(anyhow::Error::msg)?;
next = record.previous_record_sha256.clone();
records.push(raw);
held.push(file);
}
let record = crate::history::verify_history(
db,
guard,
binding,
&self.keyring_file,
&self.policy.initial_baseline,
&records,
&self.installed_raw,
)?;
ensure!(
record.bytes().map_err(anyhow::Error::msg)? == self.installed_raw,
"UPDATE_HISTORY_ACTIVE_CHANGED"
);
for file in &held {
file.verify()?;
}
self.recheck_protected()
}
pub fn policy(&self) -> &LocalUpdatePolicy {
&self.policy
}