Implement guarded Linux updater runtime and compatible baseline tooling
Rust development slice / linux (push) Failing after 8s
Rust development slice / linux (push) Failing after 8s
This commit is contained in:
@@ -113,6 +113,7 @@ pub struct Directory {
|
||||
fd: File,
|
||||
path: PathBuf,
|
||||
identity: Identity,
|
||||
agent_state: bool,
|
||||
}
|
||||
impl Directory {
|
||||
/// All ancestors are held while opening their child with O_NOFOLLOW. A
|
||||
@@ -143,13 +144,46 @@ impl Directory {
|
||||
identity: Identity::of(&m),
|
||||
fd,
|
||||
path: path.to_path_buf(),
|
||||
agent_state: false,
|
||||
})
|
||||
}
|
||||
/// Read/stage-input traversal only, rooted at the fixed private agent state.
|
||||
/// Install roots continue using the stricter root-ancestor constructor.
|
||||
/// Every descendant is opened relative to a held fd, without symlinks, and
|
||||
/// must remain private UID993 state; no network-selected ancestor is trusted.
|
||||
pub fn open_agent_state(path: &Path) -> Result<Self> {
|
||||
let base = Path::new("/var/lib/rmm-agent-remote");
|
||||
let suffix = path.strip_prefix(base).context("UPDATE_AGENT_STATE_ROOT")?;
|
||||
let root = Self::open(base, 993, true)?;
|
||||
let mut fd = root.fd.try_clone()?;
|
||||
for component in suffix.components() {
|
||||
let Component::Normal(name) = component else {
|
||||
anyhow::bail!("UPDATE_AGENT_STATE_COMPONENT")
|
||||
};
|
||||
fd = openat(&fd, name, libc::O_RDONLY | libc::O_DIRECTORY, 0)?;
|
||||
let m = fd.metadata()?;
|
||||
ensure!(
|
||||
m.is_dir() && m.uid() == 993 && m.mode() & 0o077 == 0,
|
||||
"UPDATE_AGENT_STATE_PRIVATE_DIRECTORY"
|
||||
);
|
||||
}
|
||||
root.verify()?;
|
||||
Ok(Self {
|
||||
identity: Identity::of(&fd.metadata()?),
|
||||
fd,
|
||||
path: path.into(),
|
||||
agent_state: true,
|
||||
})
|
||||
}
|
||||
pub fn identity(&self) -> &Identity {
|
||||
&self.identity
|
||||
}
|
||||
pub fn verify(&self) -> Result<()> {
|
||||
let current = Self::open(&self.path, self.identity.uid, false)?;
|
||||
let current = if self.agent_state {
|
||||
Self::open_agent_state(&self.path)?
|
||||
} else {
|
||||
Self::open(&self.path, self.identity.uid, false)?
|
||||
};
|
||||
ensure!(
|
||||
current.identity == self.identity
|
||||
&& Identity::of(&self.fd.metadata()?) == self.identity,
|
||||
@@ -198,7 +232,7 @@ impl Directory {
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
fn absent(&self, basename: &OsStr) -> Result<()> {
|
||||
pub fn absent(&self, basename: &OsStr) -> Result<()> {
|
||||
let value = name(basename)?;
|
||||
let mut m = std::mem::MaybeUninit::<libc::stat>::uninit();
|
||||
// SAFETY: live dirfd, valid basename, properly sized writable stat.
|
||||
@@ -251,6 +285,13 @@ impl HeldFile {
|
||||
pub fn proof(&self) -> &FileProof {
|
||||
&self.proof
|
||||
}
|
||||
/// A held read-only, hash-verified fd for bounded streaming into a stage.
|
||||
/// The destination independently verifies the final length/hash/EOF again.
|
||||
pub fn reader(&mut self) -> Result<&mut File> {
|
||||
self.verify(self.proof.bytes)?;
|
||||
self.fd.seek(SeekFrom::Start(0))?;
|
||||
Ok(&mut self.fd)
|
||||
}
|
||||
pub fn verify(&mut self, max: u64) -> Result<()> {
|
||||
let m = self.fd.metadata()?;
|
||||
ensure!(
|
||||
@@ -544,6 +585,9 @@ pub struct Installed {
|
||||
new: FileProof,
|
||||
}
|
||||
impl Installed {
|
||||
pub fn role(&self) -> Role {
|
||||
self.role
|
||||
}
|
||||
pub fn old(&self) -> &FileProof {
|
||||
&self.old
|
||||
}
|
||||
@@ -564,10 +608,14 @@ impl Installed {
|
||||
dir.file(OsStr::new(&self.backup), &self.old, 256 * 1024 * 1024)?;
|
||||
let displaced = self.backup.replace(".previous", ".rejected");
|
||||
dir.absent(OsStr::new(&displaced))?;
|
||||
let effect = Effect::RestorePredecessor { role: self.role };
|
||||
fence.before_effect(&effect)?;
|
||||
let save = Effect::SaveRejectedCandidate { role: self.role };
|
||||
fence.before_effect(&save)?;
|
||||
guard.verify_for_effect(budget)?;
|
||||
dir.rename_new(OsStr::new(&self.target), OsStr::new(&displaced))?;
|
||||
dir.file(OsStr::new(&displaced), &self.new, 256 * 1024 * 1024)?;
|
||||
fence.after_effect(&save, &digest(&serde_json::to_vec(&self.new)?))?;
|
||||
let effect = Effect::RestorePredecessor { role: self.role };
|
||||
fence.before_effect(&effect)?;
|
||||
// No existing target can be overwritten; both code generations remain.
|
||||
guard.verify_for_effect(budget)?;
|
||||
dir.rename_new(OsStr::new(&self.backup), OsStr::new(&self.target))?;
|
||||
|
||||
Reference in New Issue
Block a user