Wire preserved update control, health evidence and record publication
Rust development slice / linux (push) Failing after 7s
Rust development slice / linux (push) Failing after 7s
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user