fix(executor): revalidate the target disk before partitioning

probe_disks parsed lsblk human-readable sizes as bytes, so every disk
reported 0 B and the size-based identity check was blind; pass -b.

Re-probe the selected disk immediately before the destructive stage and
refuse if it disappeared, became protected (mounted/read-only/live) or
changed size/model since the review. Add regression tests for each case.
This commit is contained in:
Phoebus 2026-09-21 20:01:32 -07:00
parent 7e9fa9c2a3
commit 5ae166e827
2 changed files with 177 additions and 1 deletions

View file

@ -347,10 +347,12 @@ fn live_source_disks(runner: &dyn ProcessRunner) -> Vec<String> {
} }
pub fn probe_disks(runner: &dyn ProcessRunner) -> Vec<Disk> { pub fn probe_disks(runner: &dyn ProcessRunner) -> Vec<Disk> {
// `-b` is required: without it lsblk prints human units ("30G") and the
// byte parse below silently yields 0, so every disk would report 0 B.
let out = capture( let out = capture(
runner, runner,
"lsblk", "lsblk",
&["-dn", "-P", "-o", "NAME,MODEL,SIZE,TRAN,RM,RO"], &["-bdn", "-P", "-o", "NAME,MODEL,SIZE,TRAN,RM,RO"],
); );
let mounts = read_mounts(); let mounts = read_mounts();
let live = live_source_disks(runner); let live = live_source_disks(runner);
@ -480,6 +482,9 @@ mod tests {
assert!(sr.is_protected()); assert!(sr.is_protected());
let vda = disks.iter().find(|d| d.path == "/dev/vda").unwrap(); let vda = disks.iter().find(|d| d.path == "/dev/vda").unwrap();
assert!(!vda.is_protected()); assert!(!vda.is_protected());
// Regression: the byte size must survive parsing (lsblk needs -b).
assert_eq!(vda.size_bytes, 32_212_254_720);
assert_eq!(vda.size_human(), "30.0 GiB");
} }
#[test] #[test]

View file

@ -242,6 +242,74 @@ impl Installer {
} }
} }
/// Re-check the selected target disk immediately before the destructive
/// stage. An approval binds to a verified plan, not to a device name: the
/// device may have disappeared, been mounted, become the live medium, or
/// been replaced under the same name since it was chosen. Refuse to
/// partition unless the disk is present, unprotected and the same device.
pub fn revalidate_target_disk(&self) -> Result<()> {
let disk_path = &self.config.storage.disk;
if disk_path.trim().is_empty() {
return Err(phoebus_executor::ExecError::operation(
"storage_prepare",
"revalidate target disk",
"no target disk is selected",
));
}
let fresh = probe::probe_disks(self.exec.runner.as_ref());
let observed = match fresh.iter().find(|d| &d.path == disk_path) {
Some(d) => d,
None => {
return Err(phoebus_executor::ExecError::operation(
"storage_prepare",
"revalidate target disk",
format!(
"target disk {disk_path} disappeared since it was selected; \
refusing to partition"
),
))
}
};
if let Some(reason) = observed.protection() {
return Err(phoebus_executor::ExecError::operation(
"storage_prepare",
"revalidate target disk",
format!("refusing target {disk_path}: {}", reason.describe()),
));
}
// Device-name reuse: if the same path now describes a different device
// (size or model changed), the earlier review no longer applies.
if let Some(expected) = self.disks.iter().find(|d| &d.path == disk_path) {
if expected.size_bytes != 0
&& observed.size_bytes != 0
&& expected.size_bytes != observed.size_bytes
{
return Err(phoebus_executor::ExecError::operation(
"storage_prepare",
"revalidate target disk",
format!(
"target disk {disk_path} changed size since it was selected \
({} -> {} bytes); refusing to partition",
expected.size_bytes, observed.size_bytes
),
));
}
if let (Some(before), Some(now)) = (&expected.model, &observed.model) {
if before != now {
return Err(phoebus_executor::ExecError::operation(
"storage_prepare",
"revalidate target disk",
format!(
"target disk {disk_path} changed identity since it was \
selected ({before} -> {now}); refusing to partition"
),
));
}
}
}
Ok(())
}
fn storage_prepare( fn storage_prepare(
&mut self, &mut self,
auth: &phoebus_executor::DestructiveAuthorization, auth: &phoebus_executor::DestructiveAuthorization,
@ -256,6 +324,7 @@ impl Installer {
self.state.luks_uuid = Some("<luks-uuid>".into()); self.state.luks_uuid = Some("<luks-uuid>".into());
return Ok(()); return Ok(());
} }
self.revalidate_target_disk()?;
storage::prepare( storage::prepare(
&self.exec, &self.exec,
&self.config, &self.config,
@ -560,6 +629,12 @@ impl Installer {
Ok(()) Ok(())
} }
/// A controlled, reversible re-check used by tests and by callers that want
/// to gate a destructive action on the current disk identity.
pub fn current_disks(&self) -> Vec<phoebus_core::Disk> {
probe::probe_disks(self.exec.runner.as_ref())
}
/// Capture an initial known-good snapshot and point a boot entry at it, so /// Capture an initial known-good snapshot and point a boot entry at it, so
/// recovery does not depend on the (possibly later broken) live root. /// recovery does not depend on the (possibly later broken) live root.
fn create_initial_recovery_point(&self, auth: &phoebus_executor::DestructiveAuthorization) { fn create_initial_recovery_point(&self, auth: &phoebus_executor::DestructiveAuthorization) {
@ -606,3 +681,99 @@ impl Installer {
} }
} }
} }
#[cfg(test)]
mod tests {
use super::*;
use phoebus_core::{Disk, DiskKind, Redactor};
use phoebus_executor::command::{CommandOutput, RecordingRunner};
use phoebus_executor::Logger;
fn lsblk_output(lines: &str) -> CommandOutput {
CommandOutput {
status: 0,
success: true,
stdout: lines.as_bytes().to_vec(),
stderr: Vec::new(),
duration: std::time::Duration::from_millis(1),
}
}
fn installer(target: &str, lsblk: &str) -> Installer {
let runner = RecordingRunner::new();
runner.set_response("lsblk", lsblk_output(lsblk));
let dir = tempfile::tempdir().unwrap().keep();
let logger = Logger::open(dir.join("install.log"), Redactor::new(), false);
let mut config = InstallConfig::default();
config.storage.disk = target.to_string();
let exec = Exec::new(Box::new(runner), logger, false, dir.join("root"), dir);
Installer::new(config, exec)
}
const VDA_OK: &str =
"NAME=\"vda\" MODEL=\"QEMU HARDDISK\" SIZE=\"32212254720\" TRAN=\"virtio\" RM=\"0\" RO=\"0\"\n";
const VDA_READONLY: &str =
"NAME=\"vda\" MODEL=\"QEMU HARDDISK\" SIZE=\"32212254720\" TRAN=\"virtio\" RM=\"0\" RO=\"1\"\n";
const VDB_ONLY: &str =
"NAME=\"vdb\" MODEL=\"OTHER\" SIZE=\"10737418240\" TRAN=\"virtio\" RM=\"0\" RO=\"0\"\n";
#[test]
fn revalidation_accepts_the_same_observed_disk() {
let installer = installer("/dev/vda", VDA_OK);
assert!(installer.revalidate_target_disk().is_ok());
}
#[test]
fn revalidation_refuses_a_disappeared_disk() {
let installer = installer("/dev/vda", VDB_ONLY);
let err = installer.revalidate_target_disk().unwrap_err().to_string();
assert!(err.contains("disappeared"), "{err}");
}
#[test]
fn revalidation_refuses_a_now_protected_disk() {
let installer = installer("/dev/vda", VDA_READONLY);
let err = installer.revalidate_target_disk().unwrap_err().to_string();
assert!(err.contains("read-only"), "{err}");
}
#[test]
fn revalidation_refuses_reused_device_name_with_a_new_size() {
let mut installer = installer("/dev/vda", VDA_OK);
// The reviewed device had a different size; the name now points at a
// different disk.
installer.disks = vec![Disk {
path: "/dev/vda".into(),
model: Some("QEMU HARDDISK".into()),
size_bytes: 8 * 1024 * 1024 * 1024,
transport: Some("virtio".into()),
removable: false,
read_only: false,
mounted: false,
is_block_device: true,
kind: DiskKind::Physical,
live_media: false,
}];
let err = installer.revalidate_target_disk().unwrap_err().to_string();
assert!(err.contains("changed size"), "{err}");
}
#[test]
fn revalidation_refuses_reused_device_name_with_a_new_model() {
let mut installer = installer("/dev/vda", VDA_OK);
installer.disks = vec![Disk {
path: "/dev/vda".into(),
model: Some("A DIFFERENT DISK".into()),
size_bytes: 32_212_254_720,
transport: Some("virtio".into()),
removable: false,
read_only: false,
mounted: false,
is_block_device: true,
kind: DiskKind::Physical,
live_media: false,
}];
let err = installer.revalidate_target_disk().unwrap_err().to_string();
assert!(err.contains("changed identity"), "{err}");
}
}