294 lines
10 KiB
Rust
294 lines
10 KiB
Rust
//! Exact fixed target/stage proofs, captured before atomic consumption and
|
|
//! independently reread under the existing lock before each native operation.
|
|
use anyhow::{Context, Result, ensure};
|
|
use rmm_domain::jobs::hash;
|
|
use rmm_updater_handoff::{FileRole, Handoff};
|
|
use rmm_updater_linux::{
|
|
Budget,
|
|
files::{BoundStage, Directory, FileProof, Role, Staged},
|
|
};
|
|
use serde::{Deserialize, Serialize};
|
|
use std::{ffi::OsStr, path::Path};
|
|
use uuid::Uuid;
|
|
|
|
const ROLES: [Role; 4] = [Role::Agent, Role::Helper, Role::FullConfig, Role::FullPin];
|
|
fn label(role: Role) -> &'static str {
|
|
match role {
|
|
Role::Agent => "agent",
|
|
Role::Helper => "helper",
|
|
Role::FullConfig => "full-config",
|
|
Role::FullPin => "full-pin",
|
|
}
|
|
}
|
|
fn role(role: FileRole) -> Result<Role> {
|
|
match role {
|
|
FileRole::AgentBinary => Ok(Role::Agent),
|
|
FileRole::HelperBinary => Ok(Role::Helper),
|
|
FileRole::LinuxFullConfig => Ok(Role::FullConfig),
|
|
FileRole::LinuxFullPin => Ok(Role::FullPin),
|
|
_ => anyhow::bail!("UPDATE_LINUX_FILE_ROLE"),
|
|
}
|
|
}
|
|
#[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,
|
|
sha256: String,
|
|
}
|
|
impl FileWitnesses {
|
|
/// Exactly Agent, Helper, Fullconfig, Fullpin, all fully staged. Reopening
|
|
/// each fixed stage name also rejects a mislabeled `Staged` object.
|
|
pub fn capture(stages: &[Staged; 4], nonce: Uuid, budget: &Budget) -> Result<Self> {
|
|
ensure!(!nonce.is_nil(), "UPDATE_STAGE_NONCE");
|
|
let mut files = Vec::new();
|
|
for (role, stage) in ROLES.iter().copied().zip(stages) {
|
|
budget.check()?;
|
|
let path = Path::new(role.target());
|
|
let directory =
|
|
Directory::open(path.parent().context("UPDATE_TARGET_PARENT")?, 0, false)?;
|
|
let original = directory.inspect(
|
|
path.file_name().context("UPDATE_TARGET_NAME")?,
|
|
0,
|
|
256 * 1024 * 1024,
|
|
)?;
|
|
directory.file(
|
|
OsStr::new(&format!(".rmm-update-{nonce}-{}.stage", label(role))),
|
|
stage.proof(),
|
|
256 * 1024 * 1024,
|
|
)?;
|
|
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,
|
|
"UPDATE_STAGE_METADATA_CHANGED"
|
|
);
|
|
files.push(Proof {
|
|
role,
|
|
original: original.proof().clone(),
|
|
staged: stage.proof().clone(),
|
|
});
|
|
}
|
|
let digest = hash(&serde_json::to_vec(&Record {
|
|
schema_version: 1,
|
|
owner_nonce: nonce,
|
|
files: files.clone(),
|
|
})?);
|
|
Ok(Self {
|
|
files,
|
|
nonce,
|
|
sha256: digest,
|
|
})
|
|
}
|
|
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
|
|
&& handoff.owner_nonce == self.nonce
|
|
&& handoff.staged_files_sha256 == self.sha256,
|
|
"UPDATE_FILESET_BINDING"
|
|
);
|
|
for (file, plan) in self.files.iter().zip(&handoff.files) {
|
|
ensure!(
|
|
plan.role == file.role.handoff_role()
|
|
&& plan.old_sha256 == file.original.sha256
|
|
&& plan.new_sha256 == file.staged.sha256,
|
|
"UPDATE_FILESET_PLAN_CHANGED"
|
|
);
|
|
}
|
|
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()?;
|
|
let path = Path::new(file.role.target());
|
|
let dir = Directory::open(path.parent().context("UPDATE_TARGET_PARENT")?, 0, false)?;
|
|
dir.file(
|
|
path.file_name().context("UPDATE_TARGET_NAME")?,
|
|
&file.original,
|
|
256 * 1024 * 1024,
|
|
)?;
|
|
dir.file(
|
|
OsStr::new(&format!(
|
|
".rmm-update-{}-{}.stage",
|
|
self.nonce,
|
|
label(file.role)
|
|
)),
|
|
&file.staged,
|
|
256 * 1024 * 1024,
|
|
)?;
|
|
Ok(())
|
|
}
|
|
pub fn verify_replaced(&self, which: FileRole, budget: &Budget) -> Result<()> {
|
|
let file = self.file(which)?;
|
|
budget.check()?;
|
|
let path = Path::new(file.role.target());
|
|
let dir = Directory::open(path.parent().context("UPDATE_TARGET_PARENT")?, 0, false)?;
|
|
dir.file(
|
|
path.file_name().context("UPDATE_TARGET_NAME")?,
|
|
&file.staged,
|
|
256 * 1024 * 1024,
|
|
)?;
|
|
dir.file(
|
|
OsStr::new(&format!(
|
|
".rmm-update-{}-{}.previous",
|
|
self.nonce,
|
|
label(file.role)
|
|
)),
|
|
&file.original,
|
|
256 * 1024 * 1024,
|
|
)?;
|
|
Ok(())
|
|
}
|
|
pub fn verify_saved(&self, which: FileRole, budget: &Budget) -> Result<()> {
|
|
let file = self.file(which)?;
|
|
budget.check()?;
|
|
let path = Path::new(file.role.target());
|
|
let dir = Directory::open(path.parent().context("UPDATE_TARGET_PARENT")?, 0, false)?;
|
|
dir.file(
|
|
OsStr::new(&format!(
|
|
".rmm-update-{}-{}.previous",
|
|
self.nonce,
|
|
label(file.role)
|
|
)),
|
|
&file.original,
|
|
256 * 1024 * 1024,
|
|
)?;
|
|
dir.file(
|
|
OsStr::new(&format!(
|
|
".rmm-update-{}-{}.stage",
|
|
self.nonce,
|
|
label(file.role)
|
|
)),
|
|
&file.staged,
|
|
256 * 1024 * 1024,
|
|
)?;
|
|
ensure!(
|
|
std::fs::symlink_metadata(path)
|
|
.is_err_and(|e| e.kind() == std::io::ErrorKind::NotFound),
|
|
"UPDATE_PUBLISH_TARGET_NOT_ABSENT"
|
|
);
|
|
dir.verify()
|
|
}
|
|
pub fn native_observation_sha256(&self, which: FileRole, replaced: bool) -> Result<String> {
|
|
let file = self.file(which)?;
|
|
Ok(hash(&serde_json::to_vec(if replaced {
|
|
&file.staged
|
|
} else {
|
|
&file.original
|
|
})?))
|
|
}
|
|
pub fn verify_pair(&self, budget: &Budget) -> Result<()> {
|
|
for file in &self.files {
|
|
self.verify_replaced(file.role.handoff_role(), budget)?;
|
|
}
|
|
Ok(())
|
|
}
|
|
fn file(&self, which: FileRole) -> Result<&Proof> {
|
|
let role = role(which)?;
|
|
self.files
|
|
.iter()
|
|
.find(|f| f.role == role)
|
|
.context("UPDATE_FILE_NOT_BOUND")
|
|
}
|
|
}
|