🐛 Refuse a start from a reims-ctl whose binary was updated since

A reims-ctl process carries the guest's kexts and the device's option ROM of
its own build, which a start gives the VM (its OpenCore image, made again
where it holds other builds, or an arm64 VM's config bundle), but the QEMU a
start runs, with the library in it, is the install's. An interface left
open while reims-ctl was updated from another terminal so started VMs with
its older drivers and ROM under the newer library, which can keep the
guest from using its GPU, and said nothing.

reims-ctl now notes its binary's file as it starts (its length, time of last
write and identity: device and inode on macOS and Linux, volume and file index on
Windows), and a start from a process whose binary was replaced since is
refused, in the interface's status line as on the command line:
"reims-ctl was updated since this one started (PATH holds another build
now): quit it and run reims-ctl again. This one carries older macOS drivers
and option ROM than the QEMU it would start, which can keep the guest from
using its GPU". A path that holds nothing now is not taken for another
build. The test installs a build over a noted one both ways an install
can, renamed over it and written in its place, keeping its length and time
of last write.
This commit is contained in:
Anees Iqbal committed 2026-12-02 12:06:26 -08:00
1 parent 1719ebb0bb
commit 467a108fe0
5 files changed
+145

No files matched your search

+1
View File
@@ -41,6 +41,7 @@ use clap::Parser;
fn main() {
quiet_on_closed_stdout();
update::note_binary();
let cli = cli::Cli::parse();
match cli::main(cli) {
Ok(code) => std::process::exit(code),
+7
View File
@@ -136,6 +136,13 @@ pub fn uid() -> u32 {
unsafe { libc::getuid() }
}
/// What tells the file `m` describes, at `_path`, from another put there since: its device and
/// inode, which a new file has its own of.
pub fn file_id(_path: &Path, m: &std::fs::Metadata) -> Option<(u64, u64)> {
use std::os::unix::fs::MetadataExt;
Some((m.dev(), m.ino()))
}
/// The group that owns the file `m` describes.
pub fn gid(m: &std::fs::Metadata) -> Option<u32> {
use std::os::unix::fs::MetadataExt;
+24
View File
@@ -197,6 +197,30 @@ pub fn owned_by_me(_m: &std::fs::Metadata) -> bool {
true
}
/// What tells the file at `path` from another put there since: its volume's serial number and
/// its file index, which a new file has its own of, as std's metadata does not give. Not its
/// creation time: NTFS gives a file made at a name moments after another left it that file's
/// creation time (its "tunneling"), as an update's set-aside copy is made. None where the file
/// cannot be opened to read its attributes.
pub fn file_id(path: &Path, _m: &std::fs::Metadata) -> Option<(u64, u64)> {
use std::os::windows::fs::OpenOptionsExt;
use std::os::windows::io::AsRawHandle;
use windows_sys::Win32::Storage::FileSystem::{
BY_HANDLE_FILE_INFORMATION, FILE_READ_ATTRIBUTES, GetFileInformationByHandle,
};
let f = std::fs::OpenOptions::new()
.access_mode(FILE_READ_ATTRIBUTES)
.open(path)
.ok()?;
let mut info = BY_HANDLE_FILE_INFORMATION::default();
// SAFETY: the handle is the open file's, and the call writes the structure given.
if unsafe { GetFileInformationByHandle(f.as_raw_handle(), &mut info) } == 0 {
return None;
}
let index = (u64::from(info.nFileIndexHigh) << 32) | u64::from(info.nFileIndexLow);
Some((u64::from(info.dwVolumeSerialNumber), index))
}
/// The group that owns the file `m` describes: none on Windows.
pub fn gid(_m: &std::fs::Metadata) -> Option<u32> {
None
+5
View File
@@ -1360,6 +1360,11 @@ pub fn start(
headless: bool,
progress: &mut dyn FnMut(&str),
) -> Result<Started> {
// An interface left open across an update would give the VM its own build's kexts and ROM,
// and start the update's QEMU.
if let Some(exe) = crate::update::binary_replaced() {
bail!("{}", crate::update::replaced_note(&exe));
}
if let Some(p) = pid(vm) {
bail!("{} already runs (pid {p})", vm.name());
}
+108
View File
@@ -17,6 +17,8 @@ use crate::paths;
use anyhow::{Context, Result, bail};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::OnceLock;
use std::time::SystemTime;
/// The branches an update can follow.
#[derive(Clone, Copy, Debug, PartialEq, Eq, clap::ValueEnum)]
@@ -295,6 +297,67 @@ pub fn remove_set_aside(bin: &Path) {
}
}
/// A program's file as reims-ctl tells one build's from another's at the same path: its length,
/// its time of last write and its identity ([`crate::os::file_id`]), the last of which an install
/// that writes the new build anew there changes, whatever times it keeps.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct BinaryFile {
len: u64,
modified: Option<SystemTime>,
id: Option<(u64, u64)>,
}
impl BinaryFile {
pub fn of(path: &Path) -> Option<BinaryFile> {
let m = std::fs::metadata(path).ok()?;
Some(BinaryFile {
len: m.len(),
modified: m.modified().ok(),
id: crate::os::file_id(path, &m),
})
}
}
/// This process's binary as it started ([`note_binary`]): its path, and its file then.
static STARTED_FROM: OnceLock<Option<(PathBuf, BinaryFile)>> = OnceLock::new();
/// Notes this process's binary, its path and its file, as reims-ctl starts, before an update from
/// another terminal can install a new one there ([`binary_replaced`]).
pub fn note_binary() {
STARTED_FROM.get_or_init(|| {
let exe = restart_path(std::env::current_exe().ok()?);
let file = BinaryFile::of(&exe)?;
Some((exe, file))
});
}
/// Where this process's binary has been replaced since it started ([`note_binary`]): the path
/// that holds another build now, as an update run from another terminal leaves it while this
/// interface stays open. None where it was not noted, or its path holds it still, or nothing.
pub fn binary_replaced() -> Option<PathBuf> {
let (exe, then) = STARTED_FROM.get()?.as_ref()?;
replaced(exe, then).then(|| exe.clone())
}
/// Whether the file at `path` is no longer `then`, the file noted there: another file is there
/// now. A path that holds nothing now holds no other build, and is not taken for one.
fn replaced(path: &Path, then: &BinaryFile) -> bool {
BinaryFile::of(path).is_some_and(|now| now != *then)
}
/// What a start refuses where this process's binary, at `exe`, was replaced since it started
/// ([`binary_replaced`]): it carries the guest's kexts and the device's option ROM of its own
/// build, which it would give the VM, and the QEMU it starts, with the library in it, is the
/// install's, the newer build's.
pub fn replaced_note(exe: &Path) -> String {
format!(
"reims-ctl was updated since this one started ({} holds another build now): quit it and \
run reims-ctl again. This one carries older macOS drivers and option ROM than the QEMU \
it would start, which can keep the guest from using its GPU",
paths::tilde(exe)
)
}
/// The binary to run after an update, from the path this process was started from
/// (`std::env::current_exe`), read before the update, which on Windows renames this running
/// binary aside ([`remove_set_aside`]). The update installs the new reims-ctl over that path
@@ -334,6 +397,51 @@ mod tests {
let _ = std::fs::remove_dir_all(&d);
}
/// A binary another is installed over at its path since it was noted, as an update installs
/// the new reims-ctl over the one an open interface runs, is told from the one noted: by its
/// file, whether the new one is written beside it and renamed over it, as cargo install
/// does, or written in its place, and even where it keeps its length and time of last
/// write. The one noted, left as it was, is not; nor is a path that holds nothing now.
#[test]
fn a_binary_installed_over_the_one_noted_is_seen() {
let d = std::env::temp_dir().join(format!("reims-ctl-replaced-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&d);
std::fs::create_dir_all(&d).unwrap();
let bin = d.join("reims-ctl");
std::fs::write(&bin, "build a").unwrap();
let noted = BinaryFile::of(&bin).unwrap();
let kept = replaced(&bin, &noted);
// Written beside it and renamed over it, of the same length and time of last write.
let new = d.join("reims-ctl.new");
std::fs::write(&new, "build b").unwrap();
let f = std::fs::File::options().write(true).open(&new).unwrap();
f.set_modified(std::fs::metadata(&bin).unwrap().modified().unwrap())
.unwrap();
drop(f);
std::fs::rename(&new, &bin).unwrap();
let renamed_over = replaced(&bin, &noted);
let noted = BinaryFile::of(&bin).unwrap();
// Written in its place.
std::fs::write(&bin, "build c, longer").unwrap();
let written_over = replaced(&bin, &noted);
std::fs::remove_file(&bin).unwrap();
let gone = replaced(&bin, &noted);
let _ = std::fs::remove_dir_all(&d);
assert_eq!(
(kept, renamed_over, written_over, gone),
(false, true, true, false),
"(kept, renamed over, written over, gone)"
);
let note = replaced_note(Path::new("/opt/bin/reims-ctl"));
assert!(
note.starts_with(
"reims-ctl was updated since this one started (/opt/bin/reims-ctl holds another \
build now): quit it and run reims-ctl again."
),
"{note}"
);
}
/// A start removes what the installer set aside, reims-ctl's and reims-util's `NAME.old-N`,
/// and leaves the programs themselves and every other file, a near name's among them.
#[test]