Preserve exact staged file identities when reopening updater witnesses
Rust development slice / linux (push) Failing after 6s
Rust development slice / linux (push) Failing after 6s
This commit is contained in:
@@ -329,7 +329,73 @@ pub struct StageDescriptor<'a> {
|
||||
pub expected_sha: &'a str,
|
||||
pub length: u64,
|
||||
}
|
||||
/// Original immutable proof bytes from the protected consumed handoff bundle.
|
||||
/// Reopening them supplies held observations only, never dispatch/resume.
|
||||
pub struct BoundStage<'a> {
|
||||
pub role: Role,
|
||||
pub nonce: Uuid,
|
||||
pub original: &'a FileProof,
|
||||
pub staged: &'a FileProof,
|
||||
}
|
||||
impl Staged {
|
||||
pub fn reopen_bound(dir: &Directory, bound: BoundStage<'_>, budget: &Budget) -> Result<Self> {
|
||||
ensure!(
|
||||
Path::new(bound.role.target()).parent() == Some(dir.path.as_path()),
|
||||
"UPDATE_FIXED_ROLE_DIRECTORY"
|
||||
);
|
||||
Self::reopen_held(dir, bound, budget)
|
||||
}
|
||||
fn reopen_held(dir: &Directory, bound: BoundStage<'_>, budget: &Budget) -> Result<Self> {
|
||||
budget.check()?;
|
||||
dir.verify()?;
|
||||
ensure!(
|
||||
!bound.nonce.is_nil()
|
||||
&& valid_digest(&bound.original.sha256)
|
||||
&& valid_digest(&bound.staged.sha256)
|
||||
&& bound.original.bytes > 0
|
||||
&& bound.original.bytes <= 256 * 1024 * 1024
|
||||
&& bound.staged.bytes > 0
|
||||
&& bound.staged.bytes <= 256 * 1024 * 1024
|
||||
&& bound.original.identity.uid == 0
|
||||
&& bound.original.identity.mode & 0o022 == 0
|
||||
&& bound.staged.identity.uid == bound.original.identity.uid
|
||||
&& bound.staged.identity.gid == bound.original.identity.gid
|
||||
&& bound.staged.identity.mode == bound.original.identity.mode
|
||||
&& bound.staged.identity.links == 1
|
||||
&& bound.staged.identity.device == bound.original.identity.device
|
||||
&& bound.staged.identity.inode != bound.original.identity.inode,
|
||||
"UPDATE_REOPEN_PROOF_SHAPE"
|
||||
);
|
||||
let target = Path::new(bound.role.target())
|
||||
.file_name()
|
||||
.context("UPDATE_TARGET")?
|
||||
.to_str()
|
||||
.context("UPDATE_TARGET")?
|
||||
.to_owned();
|
||||
let basename = format!(".rmm-update-{}-{}.stage", bound.nonce, bound.role.label());
|
||||
dir.file(OsStr::new(&target), bound.original, 256 * 1024 * 1024)?;
|
||||
let file = dir.file(OsStr::new(&basename), bound.staged, 256 * 1024 * 1024)?;
|
||||
// A partially applied prior pass is never reconstructed as a stage.
|
||||
dir.absent(OsStr::new(&format!(
|
||||
".rmm-update-{}-{}.previous",
|
||||
bound.nonce,
|
||||
bound.role.label()
|
||||
)))?;
|
||||
dir.absent(OsStr::new(&format!(
|
||||
".rmm-update-{}-{}.rejected",
|
||||
bound.nonce,
|
||||
bound.role.label()
|
||||
)))?;
|
||||
budget.check()?;
|
||||
Ok(Self {
|
||||
role: bound.role,
|
||||
nonce: bound.nonce,
|
||||
basename,
|
||||
file,
|
||||
target,
|
||||
original: bound.original.clone(),
|
||||
})
|
||||
}
|
||||
/// Stage only after the embedding agent has a real fixed approved job and
|
||||
/// authorized download. This function alone grants no stop/swap authority.
|
||||
pub fn write(
|
||||
@@ -611,3 +677,102 @@ impl Drop for LedgerGuard<'_> {
|
||||
unsafe { libc::flock(self.lock.as_raw_fd(), libc::LOCK_UN) };
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod reopen_tests {
|
||||
use super::*;
|
||||
use std::{
|
||||
fs,
|
||||
os::unix::fs::{OpenOptionsExt, PermissionsExt},
|
||||
time::Duration,
|
||||
};
|
||||
struct Fixture(PathBuf);
|
||||
impl Drop for Fixture {
|
||||
fn drop(&mut self) {
|
||||
fs::remove_dir_all(&self.0).unwrap();
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn bound_reopen_rejects_same_byte_name_substitution_and_added_stage_hardlink() {
|
||||
assert_eq!(
|
||||
unsafe { libc::geteuid() },
|
||||
0,
|
||||
"Use isolated Root unit fixture; never a host daemon"
|
||||
);
|
||||
let path = PathBuf::from(format!("/run/rmm-stage-reopen-unit-{}", Uuid::new_v4()));
|
||||
fs::create_dir(&path).unwrap();
|
||||
fs::set_permissions(&path, fs::Permissions::from_mode(0o700)).unwrap();
|
||||
let fixture = Fixture(path);
|
||||
let dir = Directory::open(&fixture.0, 0, true).unwrap();
|
||||
let nonce = Uuid::new_v4();
|
||||
let stage = format!(".rmm-update-{nonce}-agent.stage");
|
||||
for (name, bytes) in [
|
||||
("rmm-agent-remote", b"old public fixture".as_slice()),
|
||||
(stage.as_str(), b"new public fixture".as_slice()),
|
||||
] {
|
||||
let mut file = fs::OpenOptions::new()
|
||||
.create_new(true)
|
||||
.write(true)
|
||||
.mode(0o600)
|
||||
.open(fixture.0.join(name))
|
||||
.unwrap();
|
||||
file.write_all(bytes).unwrap();
|
||||
file.sync_all().unwrap();
|
||||
}
|
||||
let old = dir
|
||||
.inspect(OsStr::new("rmm-agent-remote"), 0, 1024)
|
||||
.unwrap()
|
||||
.proof()
|
||||
.clone();
|
||||
let new = dir
|
||||
.inspect(OsStr::new(&stage), 0, 1024)
|
||||
.unwrap()
|
||||
.proof()
|
||||
.clone();
|
||||
let bound = || BoundStage {
|
||||
role: Role::Agent,
|
||||
nonce,
|
||||
original: &old,
|
||||
staged: &new,
|
||||
};
|
||||
let boot = fs::read_to_string("/proc/sys/kernel/random/boot_id")
|
||||
.unwrap()
|
||||
.trim()
|
||||
.parse()
|
||||
.unwrap();
|
||||
let budget = Budget::new(boot, Duration::from_secs(10)).unwrap();
|
||||
// Public API always enforces the real fixed parent. The shared private
|
||||
// FD-validation core is exercised only below this NEW /run fixture.
|
||||
assert!(Staged::reopen_bound(&dir, bound(), &budget).is_err());
|
||||
let held = Staged::reopen_held(&dir, bound(), &budget).unwrap();
|
||||
assert_eq!(held.proof(), &new);
|
||||
fs::rename(
|
||||
fixture.0.join(&stage),
|
||||
fixture.0.join("held-original-stage"),
|
||||
)
|
||||
.unwrap();
|
||||
let mut replacement = fs::OpenOptions::new()
|
||||
.create_new(true)
|
||||
.write(true)
|
||||
.mode(0o600)
|
||||
.open(fixture.0.join(&stage))
|
||||
.unwrap();
|
||||
replacement.write_all(b"new public fixture").unwrap();
|
||||
replacement.sync_all().unwrap();
|
||||
assert!(Staged::reopen_held(&dir, bound(), &budget).is_err());
|
||||
assert_eq!(held.proof(), &new);
|
||||
drop(replacement);
|
||||
fs::remove_file(fixture.0.join(&stage)).unwrap();
|
||||
fs::rename(
|
||||
fixture.0.join("held-original-stage"),
|
||||
fixture.0.join(&stage),
|
||||
)
|
||||
.unwrap();
|
||||
fs::hard_link(
|
||||
fixture.0.join(&stage),
|
||||
fixture.0.join("unexpected-stage-alias"),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(Staged::reopen_held(&dir, bound(), &budget).is_err());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user