diff --git a/native/.cargo/mutants.toml b/native/.cargo/mutants.toml index 91d40041..26db7c48 100644 --- a/native/.cargo/mutants.toml +++ b/native/.cargo/mutants.toml @@ -26,6 +26,10 @@ exclude_re = [ "suidhelper/src/net/vm\\.rs:.*VmNet<'_>::", "suidhelper/src/cli/vm/claim\\.rs:.*SocketPin<'_>::", "suidhelper/src/cli/vm/teardown\\.rs:.*Teardown::unpin_sockets", + # The metadata-device check runs as root against a fake dmsetup; killed by suidhelper's + # a_meta_that_is_not_the_pools_metadata_is_refused_before_any_snapshot_as_root (tests/e2e/ + # fork_delta.rs) in the privileged E2E job. + "suidhelper/src/cli/fork_delta\\.rs:.*replace != with == in ::run$", # Guest network setup runs inside a booted guest; killed by test/e2e/guest_network_test.exs # in the KVM integration job. "guest-agent/src/net\\.rs:.*GuestNet::(from_proc|configure)", diff --git a/native/hyperrs-linux/Cargo.toml b/native/hyperrs-linux/Cargo.toml index 2ec85333..e2e8bc26 100644 --- a/native/hyperrs-linux/Cargo.toml +++ b/native/hyperrs-linux/Cargo.toml @@ -8,7 +8,7 @@ license = "MIT" redundant_field_names = "allow" [dependencies] -derive_more = { version = "2.1.1", features = ["as_ref", "display", "constructor", "from", "into", "try_unwrap"] } +derive_more = { version = "2.1.1", features = ["as_ref", "constructor", "deref", "display", "from", "into", "try_unwrap"] } hyperrs-fs = { path = "../hyperrs-fs" } hyperrs-process = { path = "../hyperrs-process" } netlink-packet-core = "0.9" diff --git a/native/hyperrs-linux/src/dm/dev_path.rs b/native/hyperrs-linux/src/dm/dev_path.rs new file mode 100644 index 00000000..bf8509f8 --- /dev/null +++ b/native/hyperrs-linux/src/dm/dev_path.rs @@ -0,0 +1,65 @@ +use super::name::MAPPER_PREFIX; +use derive_more::{Deref, Into}; +use hyperrs_fs::path::{self, IsAbsolute, SafePathBuf, StrictComponents}; +use std::os::unix::ffi::OsStrExt; +use std::path::{Path, PathBuf}; +use std::str::FromStr; +use thiserror::Error as ThisError; + +// The kernel's `isspace` (lib/ctype.c) classes the Latin-1 byte 0xA0 as a +// space, so `dm_split_args` splits a UTF-8 path through any char holding it. +const KERNEL_SPACE: u8 = 0xa0; + +/// A table `` operand: an absolute, strict path to a non-dm block +/// device that holds no byte the kernel's table tokenizer splits on, so it +/// renders as exactly one operand. +#[derive(Debug, Clone, PartialEq, Eq, Deref, Into)] +#[cfg_attr( + feature = "serde", + derive(serde::Deserialize), + serde(try_from = "PathBuf") +)] +pub struct DevPath(SafePathBuf); + +#[derive(Debug, ThisError)] +pub enum DevPathError { + #[error(transparent)] + Path(#[from] path::ValidationError), + #[error("{}: whitespace would split it into several table operands", .0.display())] + Splits(PathBuf), + #[error("{}: a dm device is a table operand by name, not by path", .0.display())] + DmDevice(PathBuf), +} + +impl TryFrom> for DevPath { + type Error = DevPathError; + + fn try_from(path: SafePathBuf) -> Result { + let bytes = path.as_path().as_os_str().as_bytes(); + if bytes.contains(&KERNEL_SPACE) + || String::from_utf8_lossy(bytes).contains(char::is_whitespace) + { + return Err(DevPathError::Splits(path.into())); + } + if path.as_path().starts_with(Path::new(MAPPER_PREFIX)) { + return Err(DevPathError::DmDevice(path.into())); + } + Ok(Self(path)) + } +} + +impl TryFrom for DevPath { + type Error = DevPathError; + + fn try_from(path: PathBuf) -> Result { + SafePathBuf::try_from(path)?.try_into() + } +} + +impl FromStr for DevPath { + type Err = DevPathError; + + fn from_str(s: &str) -> Result { + PathBuf::from(s).try_into() + } +} diff --git a/native/hyperrs-linux/src/dm/mod.rs b/native/hyperrs-linux/src/dm/mod.rs index d5782fdf..1dd67351 100644 --- a/native/hyperrs-linux/src/dm/mod.rs +++ b/native/hyperrs-linux/src/dm/mod.rs @@ -1,5 +1,6 @@ //! Utilities for operating on linux `dsetup`. +mod dev_path; mod device; mod name; mod table; @@ -8,14 +9,15 @@ pub mod thin_dump; use super::Access; use derive_more::Constructor; +pub use dev_path::{DevPath, DevPathError}; pub use device::{Device, NumberError}; use hyperrs_process::binary::RootSafeBin; pub use name::{DmName, InvalidDmName, OpenDeviceError, ParseDmDeviceError, SizeError}; use std::ffi::OsStr; use std::io; pub use table::{ - DevPath, DmTable, LinearTable, Origin, ParseTableError, Persistence, Role, SnapshotTable, - ThinPoolTable, ThinTable, + DmTable, LinearTable, Origin, ParseTableError, Persistence, Role, SnapshotTable, ThinPoolTable, + ThinTable, }; pub use thin::{ Active, Inactive, OpenThinError, ParseThinIdError, ParseThinMessageError, SnapshotError, Thin, diff --git a/native/hyperrs-linux/src/dm/table.rs b/native/hyperrs-linux/src/dm/table.rs index dc3ebe33..2b4cef62 100644 --- a/native/hyperrs-linux/src/dm/table.rs +++ b/native/hyperrs-linux/src/dm/table.rs @@ -1,14 +1,11 @@ use super::name::MAPPER_PREFIX; +use super::{DevPath, DevPathError}; use super::{DmName, ParseDmDeviceError, ParseThinIdError, ThinId}; use derive_more::{Display, TryUnwrap}; -use hyperrs_fs::path::{self, IsAbsolute, SafePathBuf, StrictComponents}; use std::num::ParseIntError; use std::str::FromStr; use thiserror::Error as ThisError; -/// A table `` operand: an absolute path to a block device. -pub type DevPath = SafePathBuf; - /// Dmsetup tables. See `man dmsetup` for more info, under the `--table` docs. #[derive(Debug, Clone, PartialEq, Eq, Display)] pub enum DmTable { @@ -115,9 +112,9 @@ pub enum ParseTableError { /// A `/dev/mapper/` operand that is not a valid dm name. #[error(transparent)] Device(#[from] ParseDmDeviceError), - /// A backing operand that is not an absolute, strict path. + /// A backing operand that is not a [`DevPath`]. #[error("backing file path: {0}")] - Path(#[from] path::ValidationError), + Path(#[from] DevPathError), /// A sector count, chunk size, block size, or low-water mark that is not a `u64`. #[error("table number: {0}")] Number(#[from] ParseIntError), diff --git a/native/hyperrs-linux/src/dm/thin.rs b/native/hyperrs-linux/src/dm/thin.rs index 1cd659be..c41a472b 100644 --- a/native/hyperrs-linux/src/dm/thin.rs +++ b/native/hyperrs-linux/src/dm/thin.rs @@ -132,6 +132,7 @@ pub enum SnapshotError { #[derive(Debug, Clone)] pub struct ThinPool<'a> { device: Device<'a>, + metadata: DeviceNumber, } #[derive(Debug, Clone, Copy)] @@ -150,21 +151,36 @@ pub struct Thin<'a, S> { impl<'a> ThinPool<'a> { pub(super) fn open(dmsetup: &'a Dmsetup, name: &DmName) -> Result { let table = dmsetup.run([OsStr::new("table"), name.as_ref()])?; - match table.lines().collect::>().as_slice() { - [line] if line.split_whitespace().nth(2) == Some("thin-pool") => Ok(Self { + match Self::pool_metadata(&table) { + Some(metadata) => Ok(Self { device: dmsetup.device(name), + metadata, }), - _ => Err(ThinPoolError::NotAThinPool { + None => Err(ThinPoolError::NotAThinPool { name: name.clone(), table, }), } } + fn pool_metadata(table: &str) -> Option { + match table.lines().collect::>()[..] { + [line] => match line.split_whitespace().collect::>()[..] { + [_, _, "thin-pool", metadata, ..] => metadata.parse().ok(), + _ => None, + }, + _ => None, + } + } + pub fn device(&self) -> &Device<'a> { &self.device } + pub fn metadata(&self) -> DeviceNumber { + self.metadata + } + pub fn create_thin(&self, id: ThinId) -> Result, RunError> { self.send(ThinMessage::CreateThin(id))?; Ok(Thin { @@ -306,7 +322,7 @@ impl<'a> Thin<'a, Active<'a>> { mod tests { use super::{ Inactive, OpenThinError, ParseThinMessageError, SnapshotError, Thin, ThinId, ThinMessage, - ThinPoolError, + ThinPool, ThinPoolError, }; use crate::dm::{DmName, Dmsetup}; use assert_fs::fixture::ChildPath; @@ -409,6 +425,30 @@ mod tests { const POOL_TABLE: &str = "0 100 thin-pool 7:0 7:1 128 0 0"; + #[rstest] + #[case::pool(POOL_TABLE, Some("7:0"))] + #[case::pool_with_trailing_newline("0 100 thin-pool 7:0 7:1 128 0 0\n", Some("7:0"))] + #[case::linear("0 100 linear 7:0 0", None)] + #[case::thin("0 100 thin 7:0 1", None)] + #[case::target_in_another_column("0 thin-pool 100 7:0 7:1 128 0 0", None)] + #[case::metadata_not_a_device_number("0 100 thin-pool /dev/loop0 7:1 128 0 0", None)] + #[case::no_metadata("0 100 thin-pool", None)] + #[case::two_pool_segments( + "0 100 thin-pool 7:0 7:1 128 0 0\n100 100 thin-pool 7:2 7:3 128 0 0", + None + )] + #[case::empty("", None)] + fn only_a_single_thin_pool_segment_names_a_metadata_device( + #[case] table: &str, + #[case] metadata: Option<&str>, + ) { + assert_eq!( + ThinPool::pool_metadata(table), + metadata.map(|m| m.parse().unwrap()), + "{table:?}" + ); + } + fn name(s: &str) -> DmName { s.parse().unwrap() } diff --git a/native/hyperrs-linux/src/dm/thin_dump.rs b/native/hyperrs-linux/src/dm/thin_dump.rs index 84fc2e03..279a4cf4 100644 --- a/native/hyperrs-linux/src/dm/thin_dump.rs +++ b/native/hyperrs-linux/src/dm/thin_dump.rs @@ -91,12 +91,16 @@ impl Superblock { let ranges: Vec = device.mappings.iter().map(Mapping::range).collect(); - let got: u64 = ranges.iter().map(|range| range.length).sum(); + let truncated = |got| MappingsError::Truncated { + expected: device.mapped_blocks, + got, + }; + let got = ranges + .iter() + .try_fold(0u64, |sum, range| sum.checked_add(range.length)) + .ok_or_else(|| truncated(u64::MAX))?; if got != device.mapped_blocks { - return Err(MappingsError::Truncated { - expected: device.mapped_blocks, - got, - }); + return Err(truncated(got)); } Ok(ThinMappings { diff --git a/native/hyperrs-linux/tests/dm_kernel_argv_as_root.rs b/native/hyperrs-linux/tests/dm_kernel_argv_as_root.rs new file mode 100644 index 00000000..f4e7947f --- /dev/null +++ b/native/hyperrs-linux/tests/dm_kernel_argv_as_root.rs @@ -0,0 +1,104 @@ +//! The live counterpart of `dm_kernel_argv_properties`: a table `Dmsetup` +//! creates must make the kernel open the very device each path operand names. +//! Law: for a `DevPath` the type accepts, the dm device built over it is +//! backed by that path's device, never by a different file whose name is a +//! prefix of it. Root only: it attaches loop devices and creates a dm device. + +#![feature(adt_const_params, unsized_const_params)] +#![allow(incomplete_features)] + +use assert_fs::TempDir; +use hyperrs_fs::file::{Any, IsRegularFile, OpenFlags, SafeFile}; +use hyperrs_fs::path::{IsAbsolute, SafePathBuf, StrictComponents}; +use hyperrs_linux::dm::{DevPath, DmName, DmTable, Dmsetup, LinearTable, Origin, RemoveMode}; +use hyperrs_linux::loopdev::{LoopControl, LoopDevice}; +use hyperrs_linux::Access; +use hyperrs_process::binary::RootSafeBin; +use pretty_assertions::assert_eq; +use rstest::rstest; +use std::ffi::OsStr; +use std::fs; +use std::os::unix::ffi::OsStrExt; +use std::os::unix::fs::{symlink, MetadataExt}; +use std::path::{Path, PathBuf}; +use std::process::Command; +use std::time::Duration; + +const DMSETUP: &str = "/usr/sbin/dmsetup"; + +fn attach(file: &Path) -> LoopDevice { + fs::File::create(file).unwrap().set_len(1 << 20).unwrap(); + let path: SafePathBuf = file.to_path_buf().try_into().unwrap(); + let backing = SafeFile::::open(&path, OpenFlags::PATH).unwrap(); + LoopControl::open() + .unwrap() + .attach(&backing, Access::ReadOnly, Duration::from_secs(2)) + .unwrap() +} + +fn major_minor(dev: &Path) -> String { + let rdev = fs::metadata(dev).unwrap().rdev(); + format!( + "{}:{}", + nix::sys::stat::major(rdev), + nix::sys::stat::minor(rdev) + ) +} + +struct Removing(String); + +impl Drop for Removing { + fn drop(&mut self) { + let _ = Command::new(DMSETUP) + .args(["remove", "--retry", &self.0]) + .output(); + } +} + +#[rstest] +#[case::copyright_sign(b"x\xc2\xa9", b"x\xc2")] +#[case::latin_e_acute(b"\xc3\xa9", b"\xc3")] +fn a_linear_table_is_backed_by_the_path_it_names_as_root( + #[case] named: &[u8], + #[case] prefix: &[u8], +) { + if !nix::unistd::geteuid().is_root() { + eprintln!("SKIP: needs root"); + return; + } + let dir = TempDir::new().unwrap(); + let base = fs::canonicalize(dir.path()).unwrap(); + let wanted = attach(&base.join("wanted.img")); + let decoy = attach(&base.join("decoy.img")); + let named = base.join(OsStr::from_bytes(named)); + symlink(wanted.path().as_path(), &named).unwrap(); + symlink(decoy.path().as_path(), base.join(OsStr::from_bytes(prefix))).unwrap(); + + let origin: DevPath = PathBuf::from(&named) + .try_into() + .expect("the type accepts it"); + let dm = Dmsetup::new(RootSafeBin::open(Path::new(DMSETUP)).unwrap()); + let name: DmName = format!("hyperrs-argv-{}", std::process::id()) + .parse() + .unwrap(); + let _guard = Removing(name.to_string()); + let table = DmTable::Linear(LinearTable { + sectors: 8, + origin: Origin::Path(origin), + }); + let device = dm.create(&name, &table, Access::ReadOnly).unwrap(); + + let live = Command::new(DMSETUP) + .args(["table", &name.to_string()]) + .output() + .unwrap(); + let live = String::from_utf8(live.stdout).unwrap(); + device.remove(RemoveMode::Retry).unwrap(); + assert_eq!( + live.split_whitespace().nth(3), + Some(major_minor(wanted.path().as_path()).as_str()), + "the kernel opened {} (decoy is {}) for {named:?}", + live.trim(), + major_minor(decoy.path().as_path()), + ); +} diff --git a/native/hyperrs-linux/tests/dm_kernel_argv_properties.proptest-regressions b/native/hyperrs-linux/tests/dm_kernel_argv_properties.proptest-regressions new file mode 100644 index 00000000..a1fd53dc --- /dev/null +++ b/native/hyperrs-linux/tests/dm_kernel_argv_properties.proptest-regressions @@ -0,0 +1,7 @@ +# Seeds for failure cases proptest has generated in the past. It is +# automatically read and these particular cases re-run before any +# novel cases are generated. +# +# It is recommended to check this file in to source control so that +# everyone who runs the test benefits from these saved cases. +cc c114d108d6e992cde0bcff6ceeac1f49d0eb06eb9f29e0d42822b02b0369753a # shrinks to input = _TheKernelOpensExactlyTheOperandsOfAnyConstructibleTableArgs { t: ThinPool(ThinPoolTable { sectors: 0, metadata: SafePathBuf("/!", PhantomData<(hyperrs_fs::path::IsAbsolute, hyperrs_fs::path::StrictComponents)>), data: SafePathBuf("/\u{e0000}", PhantomData<(hyperrs_fs::path::IsAbsolute, hyperrs_fs::path::StrictComponents)>), block_sectors: 0, low_water: 0 }) } diff --git a/native/hyperrs-linux/tests/dm_kernel_argv_properties.rs b/native/hyperrs-linux/tests/dm_kernel_argv_properties.rs new file mode 100644 index 00000000..114d3b13 --- /dev/null +++ b/native/hyperrs-linux/tests/dm_kernel_argv_properties.rs @@ -0,0 +1,228 @@ +//! What the kernel parses out of a rendered table, not what Rust's tokenizer +//! sees. `dmsetup` reads the ` ` header with C-locale +//! `isspace` and libdm backslash-escapes the params, then the kernel's +//! `dm_split_args` splits them with the kernel's `isspace`, which (lib/ctype.c) +//! also treats byte 0xA0 as a space. Laws: +//! * for every table the types let us construct, the argv the kernel builds +//! is exactly the operands the struct names, byte for byte: a path operand +//! is opened as itself, never as a prefix of itself plus an extra token; +//! * the same holds for every thin-pool message; +//! * tables built only from dm names and loop devices (all the helper ever +//! hands the kernel) are faithful for the whole dm-name charset. + +use hyperrs_linux::dm::{ + DevPath, DmName, DmTable, LinearTable, Origin, Persistence, SnapshotTable, ThinId, ThinMessage, + ThinPoolTable, ThinTable, +}; +use hyperrs_linux::loopdev::LoopDevNum; +use proptest::prelude::*; +use rstest::rstest; +use std::os::unix::ffi::OsStrExt; +use std::path::PathBuf; +use test_strategy::proptest; + +fn kernel_is_space(b: u8) -> bool { + matches!(b, b'\t' | b'\n' | 0x0b | 0x0c | b'\r' | b' ' | 0xa0) +} + +fn kernel_argv(rendered: &str) -> Vec { + rendered + .as_bytes() + .split(|&b| kernel_is_space(b)) + .filter(|t| !t.is_empty()) + .map(|t| t.escape_ascii().to_string()) + .collect() +} + +fn escaped(words: Vec>) -> Vec { + words + .into_iter() + .map(|w| w.escape_ascii().to_string()) + .collect() +} + +fn origin_bytes(o: &Origin) -> Vec { + match o { + Origin::Device(n) => n.path().as_path().as_os_str().as_bytes().to_vec(), + Origin::Path(p) => path_bytes(p), + } +} + +fn path_bytes(p: &DevPath) -> Vec { + p.as_path().as_os_str().as_bytes().to_vec() +} + +fn num(n: impl ToString) -> Vec { + n.to_string().into_bytes() +} + +fn operands(t: &DmTable) -> Vec> { + let mut v = vec![b"0".to_vec()]; + match t { + DmTable::Linear(l) => { + v.extend([num(l.sectors), b"linear".to_vec(), origin_bytes(&l.origin)]); + v.push(b"0".to_vec()); + } + DmTable::Snapshot(s) => v.extend([ + num(s.sectors), + b"snapshot".to_vec(), + origin_bytes(&s.origin), + path_bytes(&s.cow), + num(s.persistence), + num(s.chunk), + ]), + DmTable::ThinPool(p) => v.extend([ + num(p.sectors), + b"thin-pool".to_vec(), + path_bytes(&p.metadata), + path_bytes(&p.data), + num(p.block_sectors), + num(p.low_water), + ]), + DmTable::Thin(t) => { + v.extend([ + num(t.sectors), + b"thin".to_vec(), + t.pool.path().as_path().as_os_str().as_bytes().to_vec(), + num(t.id), + ]); + v.extend(t.origin.as_ref().map(origin_bytes)); + } + } + v +} + +fn component_char() -> impl Strategy { + any::().prop_filter("strict component", |c| { + !matches!(c, '/' | '\0' | ' ' | '\t' | '\n' | '\x0b' | '\x0c' | '\r') + }) +} + +fn component() -> impl Strategy { + prop::collection::vec(component_char(), 1..6) + .prop_map(|cs| cs.into_iter().collect::()) + .prop_filter("not . or ..", |s| s != "." && s != "..") +} + +fn dev_path() -> impl Strategy { + prop::collection::vec(component(), 1..4).prop_filter_map("not a DevPath", |parts| { + PathBuf::from(format!("/{}", parts.join("/"))) + .try_into() + .ok() + }) +} + +fn dm_name() -> impl Strategy { + "[0-9A-Za-z#+.:=@_][0-9A-Za-z#+.:=@_-]{0,40}".prop_filter_map("reserved", |s| s.parse().ok()) +} + +fn thin_id() -> impl Strategy { + (0..=ThinId::MAX).prop_map(|n| ThinId::new(n).unwrap()) +} + +fn persistence() -> impl Strategy { + prop_oneof![Just(Persistence::Persistent), Just(Persistence::Transient)] +} + +fn table(path: BoxedStrategy) -> impl Strategy { + let origin = prop_oneof![ + dm_name().prop_map(Origin::Device), + path.clone().prop_map(Origin::Path) + ]; + prop_oneof![ + (any::(), origin.clone()) + .prop_map(|(sectors, origin)| DmTable::Linear(LinearTable { sectors, origin })), + ( + any::(), + origin.clone(), + path.clone(), + persistence(), + any::() + ) + .prop_map(|(sectors, origin, cow, persistence, chunk)| { + DmTable::Snapshot(SnapshotTable { + sectors, + origin, + cow, + persistence, + chunk, + }) + }), + (any::(), path.clone(), path, any::(), any::()).prop_map( + |(sectors, metadata, data, block_sectors, low_water)| { + DmTable::ThinPool(ThinPoolTable { + sectors, + metadata, + data, + block_sectors, + low_water, + }) + } + ), + (any::(), dm_name(), thin_id(), prop::option::of(origin)).prop_map( + |(sectors, pool, id, origin)| DmTable::Thin(ThinTable { + sectors, + pool, + id, + origin, + }) + ), + ] +} + +fn loop_path() -> impl Strategy { + any::().prop_map(|n| LoopDevNum::from(n).path().unwrap().try_into().unwrap()) +} + +#[proptest(ProptestConfig::with_cases(2048))] +fn the_kernel_opens_exactly_the_operands_of_any_constructible_table( + #[strategy(table(dev_path().boxed()))] t: DmTable, +) { + prop_assert_eq!( + kernel_argv(&t.to_string()), + escaped(operands(&t)), + "{:?}", + t.to_string() + ); +} + +#[proptest(ProptestConfig::with_cases(2048))] +fn tables_of_dm_names_and_loop_devices_are_kernel_faithful( + #[strategy(table(loop_path().boxed()))] t: DmTable, +) { + prop_assert_eq!(kernel_argv(&t.to_string()), escaped(operands(&t))); +} + +#[proptest] +fn a_thin_message_reaches_the_kernel_as_exactly_its_words( + #[strategy(thin_id())] a: ThinId, + #[strategy(thin_id())] b: ThinId, +) { + for (m, words) in [ + (ThinMessage::CreateThin(a), vec![num("create_thin"), num(a)]), + ( + ThinMessage::CreateSnap { id: a, origin: b }, + vec![num("create_snap"), num(a), num(b)], + ), + (ThinMessage::Delete(a), vec![num("delete"), num(a)]), + ( + ThinMessage::ReserveMetadataSnap, + vec![num("reserve_metadata_snap")], + ), + ( + ThinMessage::ReleaseMetadataSnap, + vec![num("release_metadata_snap")], + ), + ] { + prop_assert_eq!(kernel_argv(&m.to_string()), escaped(words)); + } +} + +#[rstest] +#[case::no_break_space("/srv/hyper/x\u{a0}")] +#[case::latin_a_grave("/srv/hyper/\u{e0}.img")] +#[case::three_byte_char("/srv/hyper/\u{8a0}")] +#[case::unicode_whitespace("/srv/hyper/x\u{3000}")] +fn a_path_holding_a_token_separator_is_refused(#[case] path: &str) { + assert!(path.parse::().is_err(), "{path:?}"); +} diff --git a/native/hyperrs-linux/tests/dm_live_as_root.rs b/native/hyperrs-linux/tests/dm_live_as_root.rs new file mode 100644 index 00000000..33892c9c --- /dev/null +++ b/native/hyperrs-linux/tests/dm_live_as_root.rs @@ -0,0 +1,720 @@ +//! `Dmsetup`, `ThinDump` and `LoopControl` against both recording fakes and +//! the real kernel. Root only: `RootSafeBin` demands a root-owned binary, and +//! the live cases create loop and dm devices. Laws: +//! * argv is exactly the documented verb shape, the rendered table and every +//! message travel as ONE argv element, and no caller environment leaks; +//! * a non-zero exit is `RunError::Failed` carrying the trimmed stderr; +//! * `thin_dump` reads the metadata device through the inherited fd it is +//! given, and a mapping total that overflows is refused, never a panic; +//! * a loop device is read-only exactly when attached `ReadOnly`, and +//! auto-detaches once nothing holds it (no leak); +//! * a live table is sized, numbered and protected as requested; +//! * on a real pool: an id is never provisioned twice, a snapshot is the +//! origin frozen at snapshot time, and the dumped mappings of a thin are +//! exactly the blocks written to it (oracle: the set of written blocks); +//! * the metadata snapshot can be held only once at a time. + +#![feature(adt_const_params, unsized_const_params)] +#![allow(incomplete_features)] + +use assert_fs::TempDir; +use hyperrs_fs::dev::DeviceNumber; +use hyperrs_fs::file::{Any, IsBlockDevice, IsRegularFile, OpenFlags, SafeFile}; +use hyperrs_fs::path::{IsAbsolute, SafePathBuf, StrictComponents}; +use hyperrs_linux::dm::thin_dump::{BlockRange, MappingsError, ThinDump}; +use hyperrs_linux::dm::{ + DevPath, Device, DmName, DmTable, Dmsetup, LinearTable, Origin, RemoveMode, RunError, ThinId, + ThinPoolTable, ThinTable, +}; +use hyperrs_linux::loopdev::{LoopControl, LoopDevice}; +use hyperrs_linux::Access; +use hyperrs_process::binary::RootSafeBin; +use pretty_assertions::assert_eq; +use proptest::prelude::*; +use proptest::strategy::ValueTree; +use std::collections::BTreeSet; +use std::fs; +use std::io::{Read, Seek, SeekFrom, Write}; +use std::os::unix::fs::{FileTypeExt, MetadataExt, PermissionsExt}; +use std::path::{Path, PathBuf}; +use std::process::Command; +use std::time::{Duration, Instant}; + +const DMSETUP: &str = "/usr/sbin/dmsetup"; +const THIN_DUMP: &str = "/usr/local/sbin/thin_dump"; +const BLOCK_SECTORS: u64 = 128; +const BLOCK_BYTES: u64 = BLOCK_SECTORS * 512; + +fn is_root() -> bool { + nix::unistd::geteuid().is_root() +} + +macro_rules! require_root { + () => { + if !is_root() { + eprintln!("SKIP: needs root"); + return; + } + }; +} + +fn real(dir: &TempDir) -> PathBuf { + fs::canonicalize(dir.path()).unwrap() +} + +fn install(dir: &Path, name: &str, body: &str) -> PathBuf { + let path = dir.join(name); + fs::write(&path, format!("#!/bin/sh\n{body}")).unwrap(); + fs::set_permissions(&path, fs::Permissions::from_mode(0o755)).unwrap(); + path +} + +fn recorded_argv(path: &Path) -> Vec { + let raw = fs::read(path).unwrap(); + raw.split(|&b| b == 0) + .filter(|s| !s.is_empty()) + .map(|s| String::from_utf8(s.to_vec()).unwrap()) + .collect() +} + +struct FakeDmsetup { + _dir: TempDir, + base: PathBuf, + dmsetup: Dmsetup, +} + +impl FakeDmsetup { + fn new(extra: &str) -> Self { + let dir = TempDir::new().unwrap(); + let base = real(&dir); + let bin = install( + &base, + "dmsetup", + &format!( + "printf '%s\\0' \"$@\" > '{b}/argv'\n\ + env > '{b}/env'\n\ + {extra}\n\ + exit 0\n", + b = base.display() + ), + ); + Self { + dmsetup: Dmsetup::new(RootSafeBin::open(&bin).unwrap()), + _dir: dir, + base, + } + } + + fn argv(&self) -> Vec { + recorded_argv(&self.base.join("argv")) + } + + fn env(&self) -> String { + fs::read_to_string(self.base.join("env")).unwrap() + } +} + +fn name(s: &str) -> DmName { + s.parse().unwrap() +} + +fn dev_path(p: impl AsRef) -> DevPath { + p.as_ref().to_path_buf().try_into().unwrap() +} + +fn table() -> impl Strategy { + let origin = prop_oneof![ + "hyper-[a-z0-9]{1,6}".prop_map(|s| Origin::Device(name(&s))), + "/srv/[a-z0-9_-][a-z0-9._-]{0,7}".prop_map(|s| Origin::Path(dev_path(s))), + ]; + prop_oneof![ + (any::(), origin.clone()) + .prop_map(|(sectors, origin)| DmTable::Linear(LinearTable { sectors, origin })), + (any::(), 0u32..=ThinId::MAX, prop::option::of(origin)).prop_map( + |(sectors, id, origin)| DmTable::Thin(ThinTable { + sectors, + pool: name("hyper-pool"), + id: ThinId::new(id).unwrap(), + origin, + }) + ), + ] +} + +#[test] +fn create_passes_the_whole_table_as_one_argument_and_no_environment_as_root() { + require_root!(); + std::env::set_var("HYPER_LEAK_PROBE", "leaked"); + let fake = FakeDmsetup::new(""); + let mut runner = proptest::test_runner::TestRunner::new(ProptestConfig::with_cases(24)); + runner + .run( + &(table(), "hyper-[a-z0-9]{1,8}", any::()), + |(t, n, ro)| { + let access = if ro { + Access::ReadOnly + } else { + Access::ReadWrite + }; + let dev = fake.dmsetup.create(&name(&n), &t, access).unwrap(); + let mut expected = vec!["create".to_string(), n.clone()]; + if ro { + expected.push("--readonly".into()); + } + expected.extend(["--table".into(), t.to_string()]); + prop_assert_eq!(fake.argv(), expected); + prop_assert_eq!(dev.name().to_string(), n); + prop_assert!(!fake.env().contains("HYPER_LEAK_PROBE"), "{}", fake.env()); + Ok(()) + }, + ) + .unwrap(); +} + +type Verb = fn(&Device) -> Result<(), RunError>; + +#[test] +fn every_device_verb_names_exactly_the_device_as_root() { + require_root!(); + let fake = FakeDmsetup::new(""); + let dev = fake.dmsetup.device(&name("hyper-vol")); + + let verbs: [(Verb, &[&str]); 4] = [ + (|d| d.suspend(), &["suspend", "hyper-vol"]), + (|d| d.resume(), &["resume", "hyper-vol"]), + ( + |d| d.clone().remove(RemoveMode::Once), + &["remove", "hyper-vol"], + ), + ( + |d| d.clone().remove(RemoveMode::Retry), + &["remove", "--retry", "hyper-vol"], + ), + ]; + for (verb, argv) in verbs { + verb(&dev).unwrap(); + assert_eq!(fake.argv(), argv); + } +} + +#[test] +fn a_failing_dmsetup_is_an_error_carrying_its_trimmed_stderr_as_root() { + require_root!(); + let fake = FakeDmsetup::new( + "printf 'looks fine\\n'\nprintf '\\n device-mapper: reload ioctl failed \\n\\n' >&2\nexit 3", + ); + + let err = fake + .dmsetup + .device(&name("hyper-vol")) + .suspend() + .unwrap_err(); + + match err { + RunError::Failed(msg) => assert_eq!(msg, "device-mapper: reload ioctl failed"), + other => panic!("expected Failed, got {other:?}"), + } +} + +#[test] +fn device_number_accepts_only_a_major_minor_pair_as_root() { + require_root!(); + for (out, expected) in [ + ("253:7", Some((253, 7))), + (" 253:7 ", Some((253, 7))), + ("", None), + ("253", None), + ("253:7:1", None), + ("x:7", None), + ("253:-1", None), + ] { + let fake = FakeDmsetup::new(&format!("printf '%s\\n' '{out}'")); + let got = fake.dmsetup.device(&name("hyper-vol")).number(); + assert_eq!( + got.ok().map(|n| (n.major(), n.minor())), + expected, + "dmsetup info printed {out:?}" + ); + } +} + +struct Loop { + device: Option, + path: PathBuf, +} + +impl Loop { + fn attach(file: &Path, access: Access) -> Self { + let control = LoopControl::open().unwrap(); + let path: SafePathBuf = dev_path(file).into(); + let backing = SafeFile::::open(&path, OpenFlags::PATH).unwrap(); + let device = control + .attach(&backing, access, Duration::from_secs(2)) + .unwrap(); + let path = device.path().as_path().to_path_buf(); + Self { + device: Some(device), + path, + } + } + + fn sysfs(&self, attr: &str) -> Option { + let n = self.path.file_name()?.to_str()?; + fs::read_to_string(format!("/sys/block/{n}/{attr}")) + .ok() + .map(|s| s.trim().to_string()) + } + + fn release(&mut self) { + self.device.take(); + } + + fn detached_within(&self, limit: Duration) -> bool { + let deadline = Instant::now() + limit; + while Instant::now() < deadline { + if self.sysfs("loop/backing_file").is_none() { + return true; + } + std::thread::sleep(Duration::from_millis(20)); + } + false + } +} + +fn write_lands(dev: &Path, backing: &Path, fill: u8) -> bool { + let attempt = fs::OpenOptions::new() + .write(true) + .open(dev) + .and_then(|mut f| { + f.write_all(&[fill; 512])?; + f.sync_all() + }); + let landed = fs::read(backing).unwrap()[..512] == [fill; 512]; + assert_eq!(attempt.is_ok(), landed, "write reported {attempt:?}"); + landed +} + +fn sparse(path: &Path, bytes: u64) { + fs::File::create(path).unwrap().set_len(bytes).unwrap(); +} + +#[test] +fn a_loop_is_read_only_exactly_when_asked_and_never_outlives_its_holder_as_root() { + require_root!(); + let dir = TempDir::new().unwrap(); + let file = real(&dir).join("disk.img"); + sparse(&file, 1 << 20); + + for (access, ro) in [(Access::ReadOnly, "1"), (Access::ReadWrite, "0")] { + let mut lo = Loop::attach(&file, access); + assert_eq!(lo.sysfs("ro").as_deref(), Some(ro), "{access:?}"); + assert_eq!(lo.sysfs("loop/autoclear").as_deref(), Some("1")); + assert_eq!( + lo.sysfs("loop/backing_file").map(PathBuf::from), + Some(file.clone()) + ); + let landed = write_lands(&lo.path, &file, 0x7c); + assert_eq!(landed, access == Access::ReadWrite, "{access:?}"); + lo.release(); + assert!( + lo.detached_within(Duration::from_secs(3)), + "{:?} leaked after its last holder closed", + lo.path + ); + } +} + +struct DmGuard(Vec); + +impl Drop for DmGuard { + fn drop(&mut self) { + for n in self.0.iter().rev() { + let _ = Command::new(DMSETUP) + .args(["remove", "--retry", n]) + .output(); + } + } +} + +fn live_dmsetup() -> Option { + RootSafeBin::open(Path::new(DMSETUP)).ok().map(Dmsetup::new) +} + +#[test] +fn a_live_linear_table_is_sized_numbered_and_protected_as_requested_as_root() { + require_root!(); + let Some(dm) = live_dmsetup() else { + eprintln!("SKIP: no root-owned {DMSETUP}"); + return; + }; + let dir = TempDir::new().unwrap(); + let file = real(&dir).join("disk.img"); + let sectors = 2048; + fs::write(&file, vec![0x5a; sectors as usize * 512]).unwrap(); + let mut lo = Loop::attach(&file, Access::ReadOnly); + let n = format!("hyper-t-lin-{}", std::process::id()); + let _guard = DmGuard(vec![n.clone()]); + + let dev = dm + .create( + &name(&n), + &DmTable::Linear(LinearTable { + sectors, + origin: Origin::Path(dev_path(&lo.path)), + }), + Access::ReadOnly, + ) + .unwrap(); + + assert_eq!(dev.name().sectors().unwrap(), sectors); + let meta = fs::metadata(dev.path().as_path()).unwrap(); + assert!(meta.file_type().is_block_device()); + assert_eq!(dev.number().unwrap(), DeviceNumber::from(meta.rdev())); + assert!(!write_lands(dev.path().as_path(), &file, 0x7c)); + let mut head = [0u8; 512]; + fs::File::open(dev.path().as_path()) + .unwrap() + .read_exact(&mut head) + .unwrap(); + assert_eq!(head, [0x5a; 512]); + + let path = dev.path(); + dev.remove(RemoveMode::Retry).unwrap(); + assert!(!path.as_path().exists()); + lo.release(); + assert!(lo.detached_within(Duration::from_secs(3)), "loop leaked"); +} + +struct FakeThinDump { + _dir: TempDir, + base: PathBuf, + dump: ThinDump, +} + +impl FakeThinDump { + fn new(xml: &str) -> Self { + let dir = TempDir::new().unwrap(); + let base = real(&dir); + fs::write(base.join("dump.xml"), xml).unwrap(); + let bin = install( + &base, + "thin_dump", + &format!( + "printf '%s\\0' \"$@\" > '{b}/argv'\n\ + /usr/bin/head -c 16 \"$4\" > '{b}/seen'\n\ + /bin/cat '{b}/dump.xml'\n", + b = base.display() + ), + ); + Self { + dump: ThinDump::new(RootSafeBin::open(&bin).unwrap()), + _dir: dir, + base, + } + } +} + +fn superblock(devices: &str) -> String { + format!( + "{devices}" + ) +} + +fn metadata_device(dir: &Path, marker: &[u8; 16]) -> (Loop, SafeFile) { + let file = dir.join("meta.img"); + let mut contents = marker.to_vec(); + contents.resize(1 << 20, 0); + fs::write(&file, contents).unwrap(); + let lo = Loop::attach(&file, Access::ReadOnly); + let dev = SafeFile::open(&dev_path(&lo.path), OpenFlags::RDONLY).unwrap(); + (lo, dev) +} + +#[test] +fn thin_dump_reads_the_metadata_through_the_inherited_fd_as_root() { + require_root!(); + let fake = FakeThinDump::new(&superblock( + "", + )); + let marker = *b"hyper-meta-probe"; + let (_lo, meta) = metadata_device(&fake.base, &marker); + let id = ThinId::new(5).unwrap(); + + let dumped = fake.dump.dump(&meta, id).unwrap(); + + let argv = recorded_argv(&fake.base.join("argv")); + assert_eq!(argv[..3], ["--metadata-snap", "--dev-id", "5"]); + assert_eq!(argv.len(), 4, "{argv:?}"); + assert!(argv[3].starts_with("/proc/self/fd/"), "{argv:?}"); + assert_eq!(fs::read(fake.base.join("seen")).unwrap(), marker); + assert_eq!( + dumped.parse().unwrap().mappings(id).unwrap().ranges, + [ + BlockRange { + begin: 2, + length: 2 + }, + BlockRange { + begin: 9, + length: 1 + } + ] + ); +} + +#[test] +fn a_mapping_total_that_overflows_is_refused_not_a_panic_as_root() { + require_root!(); + let fake = FakeThinDump::new(&superblock(&format!( + "", + u64::MAX + ))); + let (_lo, meta) = metadata_device(&fake.base, b"0123456789abcdef"); + let id = ThinId::new(5).unwrap(); + let superblock = fake.dump.dump(&meta, id).unwrap().parse().unwrap(); + + let got = std::panic::catch_unwind(|| superblock.mappings(id)); + + assert!( + matches!(got, Ok(Err(MappingsError::Truncated { .. }))), + "{got:?}" + ); +} + +#[test] +fn a_failing_thin_dump_is_an_error_not_an_empty_dump_as_root() { + require_root!(); + let dir = TempDir::new().unwrap(); + let base = real(&dir); + let bin = install( + &base, + "thin_dump", + "printf ''\nprintf ' no metadata snap \\n' >&2\nexit 1\n", + ); + let dump = ThinDump::new(RootSafeBin::open(&bin).unwrap()); + let (_lo, meta) = metadata_device(&base, b"0123456789abcdef"); + + let err = dump.dump(&meta, ThinId::new(1).unwrap()).unwrap_err(); + + assert_eq!(err.to_string(), "thin_dump failed: no metadata snap"); +} + +struct LivePool { + _dir: TempDir, + _meta: Loop, + _data: Loop, + meta_dev: SafeFile, + guard: DmGuard, + pool: DmName, + dm: Dmsetup, +} + +const POOL_DATA_BYTES: u64 = 32 << 20; +const VOLUME_SECTORS: u64 = 16 << 11; +const VOLUME_BLOCKS: u64 = VOLUME_SECTORS / BLOCK_SECTORS; + +impl LivePool { + fn new(tag: &str) -> Option { + let dm = live_dmsetup()?; + let dir = TempDir::new().unwrap(); + let base = real(&dir); + sparse(&base.join("meta.img"), 8 << 20); + sparse(&base.join("data.img"), POOL_DATA_BYTES); + let meta = Loop::attach(&base.join("meta.img"), Access::ReadWrite); + let data = Loop::attach(&base.join("data.img"), Access::ReadWrite); + let pool = format!("hyper-t-{tag}-pool-{}", std::process::id()); + let guard = DmGuard(vec![pool.clone()]); + dm.create( + &name(&pool), + &DmTable::ThinPool(ThinPoolTable { + sectors: POOL_DATA_BYTES / 512, + metadata: dev_path(&meta.path), + data: dev_path(&data.path), + block_sectors: BLOCK_SECTORS, + low_water: 0, + }), + Access::ReadWrite, + ) + .ok()?; + let meta_dev = SafeFile::open(&dev_path(&meta.path), OpenFlags::RDONLY).unwrap(); + Some(Self { + _dir: dir, + _meta: meta, + _data: data, + meta_dev, + guard, + pool: name(&pool), + dm, + }) + } + + fn volume_name(&mut self, suffix: &str) -> DmName { + let n = format!("{}-{suffix}", self.pool); + self.guard.0.push(n.clone()); + name(&n) + } +} + +fn write_block(dev: &Path, block: u64, fill: u8) { + let mut f = fs::OpenOptions::new().write(true).open(dev).unwrap(); + f.seek(SeekFrom::Start(block * BLOCK_BYTES)).unwrap(); + f.write_all(&vec![fill; BLOCK_BYTES as usize]).unwrap(); + f.sync_all().unwrap(); +} + +fn read_block(dev: &Path, block: u64) -> Vec { + let mut f = fs::File::open(dev).unwrap(); + f.seek(SeekFrom::Start(block * BLOCK_BYTES)).unwrap(); + let mut buf = vec![0u8; BLOCK_BYTES as usize]; + f.read_exact(&mut buf).unwrap(); + buf +} + +fn mapped_blocks(pool: &LivePool, id: ThinId) -> BTreeSet { + let thin_pool = pool.dm.thin_pool(&pool.pool).unwrap(); + let dump = ThinDump::new(RootSafeBin::open(Path::new(THIN_DUMP)).unwrap()); + thin_pool.reserve_metadata_snap().unwrap(); + let dumped = dump.dump(&pool.meta_dev, id); + thin_pool.release_metadata_snap().unwrap(); + dumped + .unwrap() + .parse() + .unwrap() + .mappings(id) + .unwrap() + .ranges + .iter() + .flat_map(|r| r.begin..r.begin + r.length) + .collect() +} + +#[test] +fn a_real_pools_ids_snapshots_and_dumped_mappings_obey_their_laws_as_root() { + require_root!(); + if RootSafeBin::<"thin_dump">::open(Path::new(THIN_DUMP)).is_err() { + eprintln!("SKIP: no root-owned {THIN_DUMP}"); + return; + } + let Some(mut live) = LivePool::new("laws") else { + eprintln!("SKIP: dm-thin unavailable"); + return; + }; + let vol_name = live.volume_name("vol"); + let snap_name = live.volume_name("snap"); + let (one, two) = (ThinId::new(1).unwrap(), ThinId::new(2).unwrap()); + let pool = live.dm.thin_pool(&live.pool).unwrap(); + + let vol = pool + .create_thin(one) + .unwrap() + .activate(&vol_name, VOLUME_SECTORS, None) + .unwrap(); + assert!( + pool.create_thin(one).is_err(), + "an id already provisioned was provisioned again" + ); + + let mut runner = proptest::test_runner::TestRunner::deterministic(); + let written: BTreeSet = prop::collection::btree_set(0..VOLUME_BLOCKS, 1..24) + .new_tree(&mut runner) + .unwrap() + .current(); + for &b in &written { + write_block(vol.device().path().as_path(), b, (b % 250) as u8 + 1); + } + assert_eq!(mapped_blocks(&live, one), written); + + let snap = vol.snapshot(two).unwrap(); + let snap = snap.activate(&snap_name, VOLUME_SECTORS, None).unwrap(); + let first = *written.iter().next().unwrap(); + let before = read_block(vol.device().path().as_path(), first); + write_block(vol.device().path().as_path(), first, 0xEE); + let fresh = (0..VOLUME_BLOCKS).find(|b| !written.contains(b)).unwrap(); + write_block(vol.device().path().as_path(), fresh, 0xDD); + + assert_eq!(read_block(snap.device().path().as_path(), first), before); + assert_eq!( + read_block(snap.device().path().as_path(), fresh), + vec![0u8; BLOCK_BYTES as usize] + ); + assert_eq!(mapped_blocks(&live, two), written); + let mut grown = written.clone(); + grown.insert(fresh); + assert_eq!(mapped_blocks(&live, one), grown); + + assert_eq!(pool.open_thin(&vol_name).unwrap().id(), one); + assert_eq!(pool.open_thin(&snap_name).unwrap().id(), two); + + pool.reserve_metadata_snap().unwrap(); + let second = pool.reserve_metadata_snap(); + pool.release_metadata_snap().unwrap(); + assert!(second.is_err(), "the metadata snapshot was reserved twice"); + + snap.deactivate().unwrap().delete().unwrap(); + vol.deactivate().unwrap().delete().unwrap(); + assert!(!vol_name.path().as_path().exists()); + let again = pool.create_thin(one).expect("a deleted id is free again"); + again.delete().unwrap(); +} + +#[test] +fn a_thin_over_an_external_origin_maps_only_what_diverged_as_root() { + require_root!(); + if RootSafeBin::<"thin_dump">::open(Path::new(THIN_DUMP)).is_err() { + eprintln!("SKIP: no root-owned {THIN_DUMP}"); + return; + } + let Some(mut live) = LivePool::new("ext") else { + eprintln!("SKIP: dm-thin unavailable"); + return; + }; + let dir = TempDir::new().unwrap(); + let origin_file = real(&dir).join("origin.img"); + fs::write(&origin_file, vec![0x11; (VOLUME_SECTORS * 512) as usize]).unwrap(); + let origin_loop = Loop::attach(&origin_file, Access::ReadOnly); + let origin_name = live.volume_name("origin"); + live.dm + .create( + &origin_name, + &DmTable::Linear(LinearTable { + sectors: VOLUME_SECTORS, + origin: Origin::Path(dev_path(&origin_loop.path)), + }), + Access::ReadOnly, + ) + .unwrap(); + let vol_name = live.volume_name("vol"); + let id = ThinId::new(3).unwrap(); + let pool = live.dm.thin_pool(&live.pool).unwrap(); + let vol = pool + .create_thin(id) + .unwrap() + .activate( + &vol_name, + VOLUME_SECTORS, + Some(Origin::Device(origin_name.clone())), + ) + .unwrap(); + + assert_eq!( + read_block(vol.device().path().as_path(), 5), + vec![0x11; BLOCK_BYTES as usize] + ); + assert!( + mapped_blocks(&live, id).is_empty(), + "reads must not provision" + ); + write_block(vol.device().path().as_path(), 5, 0x22); + write_block(vol.device().path().as_path(), VOLUME_BLOCKS - 1, 0x33); + + assert_eq!( + mapped_blocks(&live, id), + BTreeSet::from([5, VOLUME_BLOCKS - 1]) + ); + assert_eq!( + fs::read(&origin_file).unwrap(), + vec![0x11; (VOLUME_SECTORS * 512) as usize] + ); + + vol.deactivate().unwrap().delete().unwrap(); +} diff --git a/native/hyperrs-linux/tests/dm_table_wire_properties.proptest-regressions b/native/hyperrs-linux/tests/dm_table_wire_properties.proptest-regressions new file mode 100644 index 00000000..d9b40652 --- /dev/null +++ b/native/hyperrs-linux/tests/dm_table_wire_properties.proptest-regressions @@ -0,0 +1,8 @@ +# Seeds for failure cases proptest has generated in the past. It is +# automatically read and these particular cases re-run before any +# novel cases are generated. +# +# It is recommended to check this file in to source control so that +# everyone who runs the test benefits from these saved cases. +cc da0f31d7af3ccca07c38170ba58f18f369fb6034e6a8315ff1d8e861fb79492d # shrinks to input = _ABackingPathUnderDevMapperKeepsItsRoleAcrossRenderingArgs { name: DmName("hyper-a") } +cc 4021084dbc75c230b1b47d408a02f10d14030163cc01ea52b8451b88200f2444 # shrinks to input = _AnyConstructibleTableRoundTripsThroughItsRenderingArgs { t: ThinPool(ThinPoolTable { sectors: 0, metadata: SafePathBuf("/a", PhantomData<(hyperrs_fs::path::IsAbsolute, hyperrs_fs::path::StrictComponents)>), data: SafePathBuf("/\u{a0}", PhantomData<(hyperrs_fs::path::IsAbsolute, hyperrs_fs::path::StrictComponents)>), block_sectors: 0, low_water: 0 }) } diff --git a/native/hyperrs-linux/tests/dm_table_wire_properties.rs b/native/hyperrs-linux/tests/dm_table_wire_properties.rs new file mode 100644 index 00000000..b583232d --- /dev/null +++ b/native/hyperrs-linux/tests/dm_table_wire_properties.rs @@ -0,0 +1,215 @@ +//! The dm table and thin-message grammars are the text the helper hands to +//! `dmsetup` as root, so their render/parse pair is a wire contract. Laws: +//! * parsing is total: arbitrary token soup never panics, it is accepted or +//! refused; +//! * whatever parses renders canonically: `parse(render(parse(s))) == +//! parse(s)`, and the render has exactly the arity of its target; +//! * every value we can construct renders to text that parses back to the +//! same value, so no operand can smuggle in an extra token or change which +//! operand is a dm device (`devices`) versus a backing path (`paths`); +//! * a thin message renders to at most three tokens and round-trips. + +use hyperrs_linux::dm::{ + DevPath, DmName, DmTable, LinearTable, Origin, Persistence, Role, SnapshotTable, ThinMessage, + ThinPoolTable, ThinTable, +}; +use proptest::prelude::*; +use std::path::PathBuf; +use test_strategy::proptest; + +fn arity(t: &DmTable) -> usize { + match t { + DmTable::Linear(_) => 5, + DmTable::Snapshot(_) | DmTable::ThinPool(_) => 7, + DmTable::Thin(ThinTable { origin, .. }) => 5 + usize::from(origin.is_some()), + } +} + +fn token() -> impl Strategy { + prop_oneof![ + Just("0".to_string()), + Just("linear".to_string()), + Just("snapshot".to_string()), + Just("thin-pool".to_string()), + Just("thin".to_string()), + Just("P".to_string()), + Just("N".to_string()), + Just("/dev/mapper/hyper-a".to_string()), + Just("/dev/mapper/".to_string()), + Just("/dev/mapper".to_string()), + Just("/dev/mapper/control".to_string()), + Just("/srv/hyper/a.img".to_string()), + Just("/srv/./a".to_string()), + Just("7:0".to_string()), + "[0-9]{1,22}", + "[+-]?[0-9]{1,4}", + "/[a-z]{1,4}(/[a-z.]{1,4}){0,3}/?", + "\\PC{1,6}", + ] +} + +fn soup() -> impl Strategy { + prop::collection::vec(token(), 0..9).prop_map(|t| t.join(" ")) +} + +#[proptest(ProptestConfig::with_cases(4096))] +fn parse_is_total_and_whatever_parses_renders_canonically(#[strategy(soup())] s: String) { + if let Ok(t) = s.parse::() { + let rendered = t.to_string(); + prop_assert_eq!(rendered.split(' ').count(), arity(&t), "{}", rendered); + let back: DmTable = rendered.parse().unwrap(); + prop_assert_eq!(&back, &t); + prop_assert_eq!(back.to_string(), rendered); + } +} + +fn component(chars: &'static str) -> impl Strategy { + prop::collection::vec( + prop::sample::select(chars.chars().collect::>()), + 1..6, + ) + .prop_map(String::from_iter) + .prop_filter("dot component", |c| c != "." && c != "..") +} + +const WIDE: &str = "abz09._-:=@#+~é\u{a0}\u{85}\u{2028}\u{3000}\u{feff}\u{1680}"; + +fn dev_path(chars: &'static str) -> impl Strategy { + prop::collection::vec(component(chars), 1..4).prop_filter_map("not strict", |parts| { + PathBuf::from(format!("/{}", parts.join("/"))) + .try_into() + .ok() + }) +} + +fn dm_name() -> impl Strategy { + "hyper-[a-z0-9]{1,8}".prop_map(|s| s.parse().unwrap()) +} + +fn origin(chars: &'static str) -> impl Strategy { + prop_oneof![ + dm_name().prop_map(Origin::Device), + dev_path(chars).prop_map(Origin::Path), + ] +} + +fn table(chars: &'static str) -> impl Strategy { + let persistence = prop_oneof![Just(Persistence::Persistent), Just(Persistence::Transient)]; + prop_oneof![ + (any::(), origin(chars)) + .prop_map(|(sectors, origin)| DmTable::Linear(LinearTable { sectors, origin })), + ( + any::(), + origin(chars), + dev_path(chars), + persistence, + any::() + ) + .prop_map( + |(sectors, origin, cow, persistence, chunk)| DmTable::Snapshot(SnapshotTable { + sectors, + origin, + cow, + persistence, + chunk, + }) + ), + ( + any::(), + dev_path(chars), + dev_path(chars), + any::(), + any::() + ) + .prop_map(|(sectors, metadata, data, block_sectors, low_water)| { + DmTable::ThinPool(ThinPoolTable { + sectors, + metadata, + data, + block_sectors, + low_water, + }) + }), + ( + any::(), + dm_name(), + 0u32..=hyperrs_linux::dm::ThinId::MAX, + prop::option::of(origin(chars)) + ) + .prop_map(|(sectors, pool, id, origin)| DmTable::Thin(ThinTable { + sectors, + pool, + id: hyperrs_linux::dm::ThinId::new(id).unwrap(), + origin, + })), + ] +} + +#[proptest(ProptestConfig::with_cases(2048))] +fn any_constructible_table_round_trips_through_its_rendering(#[strategy(table(WIDE))] t: DmTable) { + let rendered = t.to_string(); + let back = rendered.parse::(); + prop_assert!( + back.is_ok(), + "{:?} does not parse back: {:?}", + rendered, + back + ); + prop_assert_eq!(back.unwrap(), t); +} + +#[proptest] +fn a_path_under_dev_mapper_is_only_ever_a_dm_device(#[strategy(dm_name())] name: DmName) { + let path = PathBuf::from(format!("/dev/mapper/{name}")); + prop_assert!(DevPath::try_from(path).is_err()); + + let t = DmTable::Linear(LinearTable { + sectors: 8, + origin: Origin::Device(name.clone()), + }); + let back: DmTable = t.to_string().parse().unwrap(); + prop_assert_eq!( + back.paths().map(|(role, _)| role).collect::>(), + [] + ); + prop_assert_eq!(back.devices().collect::>(), [&name]); +} + +#[proptest] +fn origin_text_round_trips(#[strategy(origin("abz09._-:=@#+"))] o: Origin) { + let back: Origin = o.to_string().parse().unwrap(); + prop_assert_eq!(back, o); +} + +fn message_token() -> impl Strategy { + prop_oneof![ + Just("create_thin".to_string()), + Just("create_snap".to_string()), + Just("delete".to_string()), + Just("reserve_metadata_snap".to_string()), + Just("release_metadata_snap".to_string()), + Just("16777215".to_string()), + Just("16777216".to_string()), + "[+-]?0{0,3}[0-9]{1,9}", + "\\PC{1,4}", + ] +} + +#[proptest(ProptestConfig::with_cases(4096))] +fn every_accepted_message_renders_to_a_canonical_short_command( + #[strategy(prop::collection::vec(message_token(), 0..5))] tokens: Vec, +) { + let s = tokens.join(" "); + if let Ok(m) = s.parse::() { + let rendered = m.to_string(); + prop_assert!(rendered.split(' ').count() <= 3, "{}", rendered); + prop_assert!( + rendered.split(' ').skip(1).all(|tok| tok + .parse::() + .is_ok_and(|n| n <= hyperrs_linux::dm::ThinId::MAX && n.to_string() == tok)), + "{}", + rendered + ); + prop_assert_eq!(rendered.parse::().unwrap(), m); + } +} diff --git a/native/suidhelper/Cargo.toml b/native/suidhelper/Cargo.toml index 189ccf56..f34b9638 100644 --- a/native/suidhelper/Cargo.toml +++ b/native/suidhelper/Cargo.toml @@ -63,6 +63,10 @@ path = "tests/e2e/fork_delta.rs" name = "e2e_img" path = "tests/e2e/img.rs" +[[test]] +name = "e2e_dm_hostile" +path = "tests/e2e/dm_hostile.rs" + [[test]] name = "config_uid_gid_range" path = "tests/config_uid_gid_range.rs" @@ -75,6 +79,10 @@ path = "tests/config_paths.rs" name = "dm_backing" path = "tests/dm/backing.rs" +[[test]] +name = "cli_fork_delta_copy" +path = "tests/cli/fork_delta_copy.rs" + [lints.clippy] redundant_field_names = "allow" diff --git a/native/suidhelper/src/cli/dm.rs b/native/suidhelper/src/cli/dm.rs index fbe4e997..f61c19d4 100644 --- a/native/suidhelper/src/cli/dm.rs +++ b/native/suidhelper/src/cli/dm.rs @@ -2,7 +2,7 @@ use crate::cli::Op; use crate::config::Config; use crate::dm::{HyperDmName, HyperDmTable, TableError}; use hyperrs_linux::dm::{ - self, DmTable, LinearTable, Origin, RemoveMode, Role, SnapshotTable, ThinTable, + self, DevPath, DmTable, LinearTable, Origin, RemoveMode, Role, SnapshotTable, ThinTable, }; use hyperrs_linux::loopdev::{self, LoopControl}; use hyperrs_linux::Access; @@ -61,6 +61,8 @@ pub enum DmError { FileSize(#[source] nix::Error), #[error("the table has no device to size it from")] Unsized, + #[error(transparent)] + LoopPath(#[from] dm::DevPathError), } impl Op for Remove { @@ -114,7 +116,7 @@ impl HyperDmTable { Role::Metadata | Role::Data => Access::ReadWrite, }; let device = control.attach(backing.file(), role_access, ATTACH_TIMEOUT)?; - let path = device.path().to_owned(); + let path = DevPath::try_from(device.path().to_owned())?; loops.insert(role, device); Ok::<_, DmError>(path) })?; diff --git a/native/suidhelper/src/cli/fork_delta.rs b/native/suidhelper/src/cli/fork_delta.rs index efaa5f8e..832227f3 100644 --- a/native/suidhelper/src/cli/fork_delta.rs +++ b/native/suidhelper/src/cli/fork_delta.rs @@ -3,6 +3,7 @@ use crate::cli::MapperPath; use crate::cli::Op; use crate::config::Config; use crate::dm::{HyperDmName, HyperDmTable, NameError, TableError}; +use hyperrs_fs::dev::DeviceNumber; use hyperrs_fs::file::{self, Any, IsBlockDevice, OpenFlags, SafeFile}; use hyperrs_fs::path; use hyperrs_linux::dm::thin_dump::{ @@ -48,6 +49,11 @@ pub enum ForkDeltaError { Path(#[from] path::ValidationError), #[error("opening metadata device: {0}")] Open(#[from] file::ValidationError), + #[error("{meta} is not the pool's metadata device {pool}")] + NotPoolMetadata { + meta: DeviceNumber, + pool: DeviceNumber, + }, #[error("locking metadata device: {0}")] Lock(#[source] io::Error), #[error(transparent)] @@ -111,10 +117,18 @@ impl Op for ThinForkDelta { }))?; let scratch: HyperDmName = format!("hyper-scratch-{}", self.scratch_id).parse()?; - { + let meta = { let _root = token.elevate()?; let sectors = self.origin.sectors().map_err(DmError::from)?; let pool = dmsetup.thin_pool(&self.pool)?; + let meta = SafeFile::::open(&meta, OpenFlags::RDONLY)?; + let (got, want) = (meta.rdev()?, pool.metadata()); + if got != want { + return Err(ForkDeltaError::NotPoolMetadata { + meta: got, + pool: want, + }); + } let snapshot = match pool.open_thin(&self.parent)?.snapshot(self.scratch_id) { Ok(snapshot) => snapshot, Err(e @ SnapshotError::Resume { created: true, .. }) => { @@ -127,7 +141,8 @@ impl Op for ThinForkDelta { let _ = pool.delete_thin(self.scratch_id); return Err(ForkDeltaError::Activate(e)); } - } + meta + }; let copied = self.copy_scratch(&scratch, target, &dmsetup, &thin_dump, &meta, &mut token); let destroyed = (|| { @@ -152,7 +167,7 @@ impl ThinForkDelta { target: HyperDmTable, dmsetup: &dm::Dmsetup, thin_dump: &dump::ThinDump, - meta: &path::SafePath, + meta: &SafeFile, token: &mut Unprivileged, ) -> Result { let (id, dumped) = { @@ -194,10 +209,9 @@ impl ThinForkDelta { &self, dmsetup: &dm::Dmsetup, thin_dump: &dump::ThinDump, - meta: &path::SafePath, + device: &SafeFile, src: &HyperDmName, ) -> Result<(ThinId, MetadataDump), ForkDeltaError> { - let device = SafeFile::::open(meta, OpenFlags::RDONLY)?; let lock_fd = device .as_fd() .try_clone_to_owned() @@ -211,7 +225,7 @@ impl ThinForkDelta { pool.send(ThinMessage::ReserveMetadataSnap) .map_err(ForkDeltaError::Reserve)?; } - let dumped = thin_dump.dump(&device, id); + let dumped = thin_dump.dump(device, id); let released = pool .send(ThinMessage::ReleaseMetadataSnap) .map_err(ForkDeltaError::Release); @@ -269,9 +283,11 @@ pub fn copy_ranges( fn read_full(r: &mut impl Read, buf: &mut [u8]) -> io::Result { let mut filled = 0; while filled < buf.len() { - match r.read(&mut buf[filled..])? { - 0 => break, - n => filled += n, + match r.read(&mut buf[filled..]) { + Ok(0) => break, + Ok(n) => filled += n, + Err(e) if e.kind() == io::ErrorKind::Interrupted => {} + Err(e) => return Err(e), } } Ok(filled) diff --git a/native/suidhelper/tests/cli/fork_delta_copy.proptest-regressions b/native/suidhelper/tests/cli/fork_delta_copy.proptest-regressions new file mode 100644 index 00000000..087aa6ec --- /dev/null +++ b/native/suidhelper/tests/cli/fork_delta_copy.proptest-regressions @@ -0,0 +1,7 @@ +# Seeds for failure cases proptest has generated in the past. It is +# automatically read and these particular cases re-run before any +# novel cases are generated. +# +# It is recommended to check this file in to source control so that +# everyone who runs the test benefits from these saved cases. +cc 946c5c1fb5e28a2f2d8ded7ddced842c81919ac879b5da87ee5a9500f18ed8f7 # shrinks to input = _InterruptedReadsAreRetriedNotFatalArgs { c: Case { src_len: 7371, block_sectors: 4, ranges: [(7, 1), (7, 5), (3, 5), (2, 4), (2, 2), (2, 4)], chunk: 788 }, reads: [472, 203, 21] } diff --git a/native/suidhelper/tests/cli/fork_delta_copy.rs b/native/suidhelper/tests/cli/fork_delta_copy.rs new file mode 100644 index 00000000..45bd8c83 --- /dev/null +++ b/native/suidhelper/tests/cli/fork_delta_copy.rs @@ -0,0 +1,360 @@ +//! `copy_ranges` turns a thin device's provisioned ranges into writes on the +//! fork's COW target; a skipped or invented byte silently corrupts the forked +//! image. Laws: +//! * oracle: for ANY ranges (unsorted, overlapping, past the device end) the +//! target equals the source on the union of the clipped ranges and is +//! untouched everywhere else; +//! * oracle: `written_chunks == scanned_chunks ==` the sum over ranges of +//! `ceil(available bytes / chunk)`; +//! * metamorphic: short reads, short writes and `Interrupted` reads (all +//! legal per `std::io`) change nothing about the result; +//! * any other I/O error from either side is propagated, never swallowed; +//! * confinement: every read asks for at least one byte and lies wholly +//! inside one listed range, so a source that fails any other read +//! changes nothing. + +use hyper_suidhelper::cli::fork_delta::copy_ranges; +use hyperrs_linux::dm::thin_dump::{BlockRange, ThinMappings}; +use proptest::prelude::*; +use std::io::{self, Cursor, Read, Seek, SeekFrom, Write}; +use test_strategy::proptest; + +const SENTINEL: u8 = 0xAA; +const SECTOR: u64 = 512; + +fn mappings(block_sectors: u64, ranges: &[(u64, u64)]) -> ThinMappings { + ThinMappings { + block_sectors, + ranges: ranges + .iter() + .map(|&(begin, length)| BlockRange { begin, length }) + .collect(), + } +} + +fn clipped(src_len: u64, block_bytes: u64, (begin, length): (u64, u64)) -> (u64, u64) { + let start = (begin * block_bytes).min(src_len); + let end = ((begin + length) * block_bytes).min(src_len); + (start, end) +} + +fn expected_target(src: &[u8], block_sectors: u64, ranges: &[(u64, u64)]) -> Vec { + let bb = block_sectors * SECTOR; + let mut out = vec![SENTINEL; src.len()]; + for &r in ranges { + let (a, b) = clipped(src.len() as u64, bb, r); + out[a as usize..b as usize].copy_from_slice(&src[a as usize..b as usize]); + } + out +} + +fn expected_chunks(src_len: u64, block_sectors: u64, ranges: &[(u64, u64)], chunk: u64) -> u64 { + let bb = block_sectors * SECTOR; + ranges + .iter() + .map(|&r| { + let (a, b) = clipped(src_len, bb, r); + (b - a).div_ceil(chunk) + }) + .sum() +} + +struct Case { + src: Vec, + block_sectors: u64, + ranges: Vec<(u64, u64)>, + chunk: usize, +} + +impl std::fmt::Debug for Case { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Case") + .field("src_len", &self.src.len()) + .field("block_sectors", &self.block_sectors) + .field("ranges", &self.ranges) + .field("chunk", &self.chunk) + .finish() + } +} + +fn case() -> impl Strategy { + (1u64..=4, 1usize..24) + .prop_flat_map(|(block_sectors, blocks)| { + let bb = (block_sectors * SECTOR) as usize; + ( + Just(block_sectors), + prop::collection::vec(any::(), (blocks * bb - bb / 2)..=(blocks * bb)), + prop::collection::vec((0u64..(blocks as u64 + 4), 1u64..6), 0..8), + prop_oneof![1usize..64, 64usize..4096, Just(1 << 20)], + ) + }) + .prop_map(|(block_sectors, src, ranges, chunk)| Case { + src, + block_sectors, + ranges, + chunk, + }) +} + +fn run( + case: &Case, + src: &mut (impl Read + Seek), + dst: &mut (impl Write + Seek), +) -> io::Result<(u64, u64)> { + copy_ranges( + src, + dst, + &mappings(case.block_sectors, &case.ranges), + case.chunk, + ) + .map(|s| (s.scanned_chunks, s.written_chunks)) +} + +#[proptest(ProptestConfig::with_cases(512))] +fn any_ranges_copy_exactly_their_clipped_union_and_count_their_chunks(#[strategy(case())] c: Case) { + let mut dst = Cursor::new(vec![SENTINEL; c.src.len()]); + + let (scanned, written) = run(&c, &mut Cursor::new(c.src.clone()), &mut dst).unwrap(); + + let target = dst.into_inner(); + prop_assert_eq!(target.len(), c.src.len(), "wrote past the device end"); + prop_assert!( + target == expected_target(&c.src, c.block_sectors, &c.ranges), + "target differs from the clipped-union oracle" + ); + let chunks = expected_chunks( + c.src.len() as u64, + c.block_sectors, + &c.ranges, + c.chunk as u64, + ); + prop_assert_eq!(written, chunks); + prop_assert_eq!(scanned, chunks); +} + +struct Choppy { + inner: T, + steps: Vec, + at: usize, + interrupt: bool, +} + +impl Choppy { + fn new(inner: T, steps: Vec, interrupt: bool) -> Self { + Self { + inner, + steps, + at: 0, + interrupt, + } + } + + fn next(&mut self) -> Option { + let step = self.steps[self.at % self.steps.len()]; + self.at += 1; + if self.interrupt && self.at % 2 == 1 { + None + } else { + Some(step) + } + } +} + +impl Read for Choppy { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + match self.next() { + None => Err(io::ErrorKind::Interrupted.into()), + Some(n) => { + let n = n.min(buf.len()); + self.inner.read(&mut buf[..n]) + } + } + } +} + +impl Write for Choppy { + fn write(&mut self, buf: &[u8]) -> io::Result { + match self.next() { + None => Err(io::ErrorKind::Interrupted.into()), + Some(n) => self.inner.write(&buf[..n.min(buf.len())]), + } + } + + fn flush(&mut self) -> io::Result<()> { + self.inner.flush() + } +} + +impl Seek for Choppy { + fn seek(&mut self, pos: SeekFrom) -> io::Result { + self.inner.seek(pos) + } +} + +fn steps() -> impl Strategy> { + prop::collection::vec(1usize..700, 1..8) +} + +fn baseline(c: &Case) -> (Vec, (u64, u64)) { + let mut dst = Cursor::new(vec![SENTINEL; c.src.len()]); + let stats = run(c, &mut Cursor::new(c.src.clone()), &mut dst).unwrap(); + (dst.into_inner(), stats) +} + +#[proptest(ProptestConfig::with_cases(256))] +fn short_reads_and_short_writes_change_nothing( + #[strategy(case())] c: Case, + #[strategy(steps())] reads: Vec, + #[strategy(steps())] writes: Vec, +) { + let mut src = Choppy::new(Cursor::new(c.src.clone()), reads, false); + let mut dst = Choppy::new(Cursor::new(vec![SENTINEL; c.src.len()]), writes, false); + + let stats = run(&c, &mut src, &mut dst).unwrap(); + + let (target, expected_stats) = baseline(&c); + prop_assert!( + dst.inner.into_inner() == target, + "short I/O changed the copy" + ); + prop_assert_eq!(stats, expected_stats); +} + +#[proptest(ProptestConfig::with_cases(128))] +fn interrupted_reads_are_retried_not_fatal( + #[strategy(case())] c: Case, + #[strategy(steps())] reads: Vec, +) { + prop_assume!(!c.ranges.is_empty()); + let mut src = Choppy::new(Cursor::new(c.src.clone()), reads, true); + let mut dst = Cursor::new(vec![SENTINEL; c.src.len()]); + + let got = run(&c, &mut src, &mut dst); + + let (target, expected_stats) = baseline(&c); + prop_assert!(got.is_ok(), "an EINTR read aborted the fork: {:?}", got); + prop_assert_eq!(got.unwrap(), expected_stats); + prop_assert!(dst.into_inner() == target); +} + +struct Failing { + inner: T, + budget: usize, +} + +impl Read for Failing { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + if self.budget == 0 { + return Err(io::Error::other("EIO")); + } + let cap = buf.len().min(self.budget); + let n = self.inner.read(&mut buf[..cap])?; + self.budget -= n; + Ok(n) + } +} + +impl Write for Failing { + fn write(&mut self, buf: &[u8]) -> io::Result { + if self.budget == 0 { + return Err(io::Error::other("EIO")); + } + let n = self.inner.write(&buf[..buf.len().min(self.budget)])?; + self.budget -= n; + Ok(n) + } + + fn flush(&mut self) -> io::Result<()> { + self.inner.flush() + } +} + +impl Seek for Failing { + fn seek(&mut self, pos: SeekFrom) -> io::Result { + self.inner.seek(pos) + } +} + +#[proptest(ProptestConfig::with_cases(256))] +fn an_io_error_on_either_side_is_propagated( + #[strategy(case())] c: Case, + #[strategy(any::())] cut: prop::sample::Index, + fail_reads: bool, +) { + let total: u64 = c + .ranges + .iter() + .map(|&r| { + let (a, b) = clipped(c.src.len() as u64, c.block_sectors * SECTOR, r); + b - a + }) + .sum(); + prop_assume!(total > 0); + let budget = cut.index(total as usize); + + let got = if fail_reads { + let mut src = Failing { + inner: Cursor::new(c.src.clone()), + budget, + }; + run(&c, &mut src, &mut Cursor::new(vec![SENTINEL; c.src.len()])) + } else { + let mut dst = Failing { + inner: Cursor::new(vec![SENTINEL; c.src.len()]), + budget, + }; + run(&c, &mut Cursor::new(c.src.clone()), &mut dst) + }; + + prop_assert!( + got.is_err(), + "an I/O error after {} of {} bytes was swallowed", + budget, + total + ); +} + +struct Confined { + inner: Cursor>, + ranges: Vec<(u64, u64)>, +} + +impl Read for Confined { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + let start = self.inner.position(); + let end = start + buf.len() as u64; + if start < end && self.ranges.iter().any(|&(a, b)| a <= start && end <= b) { + self.inner.read(buf) + } else { + Err(io::Error::other(format!( + "read outside the ranges: {start}..{end}" + ))) + } + } +} + +impl Seek for Confined { + fn seek(&mut self, pos: SeekFrom) -> io::Result { + self.inner.seek(pos) + } +} + +#[proptest(ProptestConfig::with_cases(256))] +fn reads_never_leave_the_listed_ranges(#[strategy(case())] c: Case) { + let bb = c.block_sectors * SECTOR; + let mut src = Confined { + inner: Cursor::new(c.src.clone()), + ranges: c + .ranges + .iter() + .map(|&(begin, length)| (begin * bb, (begin + length) * bb)) + .collect(), + }; + let mut dst = Cursor::new(vec![SENTINEL; c.src.len()]); + + let got = run(&c, &mut src, &mut dst); + + let (target, expected_stats) = baseline(&c); + prop_assert_eq!(got.unwrap(), expected_stats); + prop_assert!(dst.into_inner() == target); +} diff --git a/native/suidhelper/tests/e2e/dm_hostile.rs b/native/suidhelper/tests/e2e/dm_hostile.rs new file mode 100644 index 00000000..af2d83a6 --- /dev/null +++ b/native/suidhelper/tests/e2e/dm_hostile.rs @@ -0,0 +1,497 @@ +//! Hostile operands to the dm ops, driven through the helper binary against a +//! real dm-thin pool. The caller is in the helper's group but may name any +//! device, id or file it likes. Laws: +//! * `thin_fork_delta` reads mappings only from the pool's own metadata +//! device: a `meta` that is not that pool's metadata is refused, never +//! dumped and copied from; +//! * a `scratch_id` already provisioned in the pool is refused and the +//! volume holding it keeps its id and its bytes; +//! * a device the call did not create (a pre-existing `hyper-scratch-N` or +//! `hyper-copy-N`) is never removed by its cleanup; +//! * no caller-chosen path ever reaches the kernel: every path operand of +//! every table the helper hands `dmsetup` is a loop device or a hyper dm +//! device, whatever bytes the caller's file names hold. +#![cfg(feature = "insecure_mode")] + +use assert_fs::TempDir; +use hyperrs_linux::dm::DevPath; +use proptest::prelude::*; +use proptest::strategy::ValueTree; +use proptest::test_runner::{Config as RunnerConfig, TestRunner}; +use rstest::rstest; +use serde_json::{json, Value}; +use std::fs; +use std::io::{Read, Seek, SeekFrom, Write}; +use std::os::unix::ffi::OsStrExt; +use std::os::unix::fs::PermissionsExt; +use std::path::{Path, PathBuf}; +use std::process::Command; + +mod support; +use support::{is_root, run, DmDevice, LoopDevice, MapperDevice}; + +const BLOCK_SECTORS: u64 = 128; +const BLOCK_BYTES: u64 = BLOCK_SECTORS * 512; +const BLOCKS: u64 = 32; +const SIZE: u64 = BLOCKS * BLOCK_BYTES; + +fn first_existing(candidates: &[&str]) -> Option { + candidates.iter().map(PathBuf::from).find(|p| p.exists()) +} + +fn sparse(path: &Path, bytes: u64) { + fs::File::create(path).unwrap().set_len(bytes).unwrap(); +} + +fn write_config(base: &Path, dmsetup: &Path, thin_dump: &Path) -> PathBuf { + let config = base.join("config.toml"); + fs::write( + &config, + format!( + "work_dir = \"{}\"\n[tools]\ndmsetup = \"{}\"\nthin_dump = \"{}\"\n", + base.display(), + dmsetup.display(), + thin_dump.display() + ), + ) + .unwrap(); + fs::set_permissions(&config, fs::Permissions::from_mode(0o644)).unwrap(); + config +} + +fn write_block(dev: &str, block: u64, fill: u8) { + let mut f = fs::OpenOptions::new().write(true).open(dev).unwrap(); + f.seek(SeekFrom::Start(block * BLOCK_BYTES)).unwrap(); + f.write_all(&vec![fill; BLOCK_BYTES as usize]).unwrap(); + f.sync_all().unwrap(); +} + +fn read_all(dev: &str) -> Vec { + let mut buf = Vec::new(); + fs::File::open(dev).unwrap().read_to_end(&mut buf).unwrap(); + buf +} + +fn dm_names() -> Vec { + let out = Command::new("dmsetup").arg("ls").output().unwrap(); + String::from_utf8_lossy(&out.stdout) + .lines() + .filter_map(|l| l.split_whitespace().next().map(str::to_string)) + .collect() +} + +fn live_table(name: &str) -> Option { + let out = Command::new("dmsetup") + .args(["table", name]) + .output() + .ok()?; + out.status + .success() + .then(|| String::from_utf8_lossy(&out.stdout).trim().to_string()) +} + +struct Pool { + name: String, + pool: DmDevice, + meta: LoopDevice, + _data: LoopDevice, +} + +impl Pool { + fn new(base: &Path, name: &str) -> Option { + sparse(&base.join(format!("{name}-meta.img")), 8 << 20); + sparse(&base.join(format!("{name}-data.img")), 64 << 20); + let meta = LoopDevice::attach(&base.join(format!("{name}-meta.img")))?; + let data = LoopDevice::attach(&base.join(format!("{name}-data.img")))?; + let pool = DmDevice::create( + name, + &format!( + "0 {} thin-pool {} {} {BLOCK_SECTORS} 0", + (64u64 << 20) / 512, + meta.path(), + data.path() + ), + )?; + Some(Self { + name: name.to_string(), + pool, + meta, + _data: data, + }) + } + + fn thin(&self, name: &str, id: u32, origin: Option<&str>) -> DmDevice { + assert!(self.pool.message(&format!("create_thin {id}"))); + DmDevice::create( + name, + &format!( + "0 {} thin {} {id}{}", + SIZE / 512, + self.pool.path(), + origin.map(|o| format!(" {o}")).unwrap_or_default() + ), + ) + .expect("thin volume") + } +} + +struct Live { + a: Pool, + origin: MapperDevice, + config: PathBuf, + base: PathBuf, + tag: String, + _dir: TempDir, +} + +impl Live { + fn new(tag: &str) -> Option { + if !is_root() { + return None; + } + let dmsetup = first_existing(&["/usr/sbin/dmsetup", "/sbin/dmsetup"])?; + let thin_dump = first_existing(&["/usr/local/sbin/thin_dump", "/usr/sbin/thin_dump"])?; + let dir = TempDir::new().unwrap(); + let base = fs::canonicalize(dir.path()).unwrap(); + let thin_dump_copy = base.join("thin_dump"); + fs::copy(fs::canonicalize(thin_dump).unwrap(), &thin_dump_copy).unwrap(); + fs::set_permissions(&thin_dump_copy, fs::Permissions::from_mode(0o755)).unwrap(); + let config = write_config(&base, &dmsetup, &thin_dump_copy); + let tag = format!("hyper-e2e-dmh-{tag}-{}", std::process::id()); + let a = Pool::new(&base, &format!("{tag}-pool"))?; + let origin_bytes: Vec = (0..SIZE).map(|i| (i % 239) as u8 | 0x80).collect(); + fs::write(base.join("origin.img"), origin_bytes).unwrap(); + let origin = MapperDevice::linear(&format!("{tag}-origin"), &base.join("origin.img"))?; + Some(Self { + a, + origin, + config, + base, + tag, + _dir: dir, + }) + } + + fn parent(&self, id: u32) -> DmDevice { + self.a.thin( + &format!("{}-parent-{id}", self.tag), + id, + Some(&self.origin.path()), + ) + } + + fn fork(&self, parent: &DmDevice, scratch_id: u32, meta: &str) -> (bool, String, PathBuf) { + let cow = self.base.join(format!("cow-{scratch_id}.img")); + sparse(&cow, 8 << 20); + let out = run( + &self.config, + json!({ + "op": "thin_fork_delta", + "pool": self.a.name, + "meta": meta, + "parent": parent.path().trim_start_matches("/dev/mapper/"), + "scratch_id": scratch_id, + "origin": self.origin.path(), + "cow": cow, + "chunk_sectors": 8, + }), + ) + .get_output() + .clone(); + ( + out.status.success(), + String::from_utf8_lossy(&out.stderr).into_owned(), + cow, + ) + } + + fn replay(&self, cow: &Path) -> Vec { + let lo = LoopDevice::attach(cow).expect("cow loop"); + let replay = DmDevice::create( + &format!("{}-replay", self.tag), + &format!( + "0 {} snapshot {} {} P 8", + SIZE / 512, + self.origin.path(), + lo.path() + ), + ) + .expect("cow replays"); + read_all(&replay.path()) + } +} + +macro_rules! live { + ($tag:expr) => { + match Live::new($tag) { + Some(live) => live, + None => { + eprintln!("SKIP: needs root, dmsetup and thin_dump"); + return; + } + } + }; +} + +#[test] +fn fork_delta_refuses_a_meta_device_that_is_not_the_pools_metadata_as_root() { + let live = live!("meta"); + const SCRATCH: u32 = 700; + let parent = live.parent(1); + write_block(&parent.path(), 2, 0x5a); + let want = read_all(&parent.path()); + + let b = Pool::new(&live.base, &format!("{}-other", live.tag)).expect("second pool"); + let decoy = b.thin(&format!("{}-decoy", live.tag), SCRATCH, None); + write_block(&decoy.path(), 5, 0x33); + drop(decoy); + assert!(b.pool.message("reserve_metadata_snap")); + + let (ok, stderr, cow) = live.fork(&parent, SCRATCH, b.meta.path()); + let _ = b.pool.message("release_metadata_snap"); + let detail = if ok { + format!( + "the fork succeeded and its replay {} the parent", + if live.replay(&cow) == want { + "matches" + } else { + "DIFFERS FROM" + } + ) + } else { + stderr + }; + assert!( + !ok, + "meta {} belongs to another pool, yet: {detail}", + b.meta.path() + ); +} + +#[rstest] +#[case::a_sibling_volumes_id(2)] +#[case::the_parents_own_id(1)] +fn fork_delta_with_a_scratch_id_in_use_leaves_that_volume_untouched_as_root(#[case] scratch: u32) { + let live = live!(&format!("busy{scratch}")); + let parent = live.parent(1); + let sibling = live.a.thin(&format!("{}-sibling", live.tag), 2, None); + write_block(&parent.path(), 3, 0x11); + write_block(&sibling.path(), 4, 0x22); + let victim = if scratch == 1 { &parent } else { &sibling }; + let before = read_all(&victim.path()); + let table = live_table(victim.path().trim_start_matches("/dev/mapper/")); + + let (ok, stderr, _) = live.fork(&parent, scratch, live.a.meta.path()); + + assert!(!ok, "a fork into a provisioned id {scratch} succeeded"); + assert!( + stderr.contains("create_snap"), + "refused for the wrong reason: {stderr}" + ); + assert_eq!( + live_table(victim.path().trim_start_matches("/dev/mapper/")), + table + ); + assert!( + read_all(&victim.path()) == before, + "id {scratch}'s bytes changed" + ); + assert!( + !live.a.pool.message(&format!("create_thin {scratch}")), + "id {scratch} was freed out from under its volume" + ); + assert!( + live.a.pool.message("reserve_metadata_snap") + && live.a.pool.message("release_metadata_snap"), + "the metadata snapshot was left reserved" + ); +} + +#[rstest] +#[case::scratch_name("hyper-scratch", 701)] +#[case::copy_target_name("hyper-copy", 702)] +fn fork_delta_never_removes_a_device_it_did_not_create_as_root( + #[case] prefix: &str, + #[case] scratch: u32, +) { + let live = live!(&prefix[6..]); + let parent = live.parent(1); + write_block(&parent.path(), 0, 0x44); + let squatter_name = format!("{prefix}-{scratch}"); + fs::write(live.base.join("squat.img"), vec![0x99; 1 << 20]).unwrap(); + let squatter = MapperDevice::linear(&squatter_name, &live.base.join("squat.img")) + .expect("pre-existing device"); + let table = live_table(&squatter_name); + + let (ok, stderr, _) = live.fork(&parent, scratch, live.a.meta.path()); + + assert!(!ok, "fork succeeded despite {squatter_name} existing"); + assert_eq!( + live_table(&squatter_name), + table, + "{squatter_name} was removed or replaced; stderr: {stderr}" + ); + assert!(read_all(&squatter.path()) == vec![0x99; 1 << 20]); + let other = if prefix == "hyper-scratch" { + format!("hyper-copy-{scratch}") + } else { + format!("hyper-scratch-{scratch}") + }; + assert!(!dm_names().contains(&other), "{other} outlived the call"); + assert!( + live.a.pool.message(&format!("create_thin {scratch}")), + "scratch id {scratch} was not freed" + ); + assert!(live.a.pool.message(&format!("delete {scratch}"))); +} + +fn kernel_operands(table: &str) -> Vec { + table + .as_bytes() + .split(|&b| matches!(b, b'\t' | b'\n' | 0x0b | 0x0c | b'\r' | b' ' | 0xa0)) + .filter(|t| t.starts_with(b"/")) + .map(|t| t.escape_ascii().to_string()) + .collect() +} + +fn is_kernel_safe_device(operand: &str) -> bool { + let loop_dev = operand + .strip_prefix("/dev/loop") + .is_some_and(|n| !n.is_empty() && n.bytes().all(|b| b.is_ascii_digit())); + let hyper_dm = operand.strip_prefix("/dev/mapper/hyper-").is_some_and(|n| { + n.bytes() + .all(|b| b.is_ascii_alphanumeric() || b"#+-.:=@_".contains(&b)) + }); + loop_dev || hyper_dm +} + +fn hostile_file_name() -> impl Strategy { + let ch = prop_oneof![ + Just('\u{a0}'), + Just('\u{e0}'), + Just('\\'), + Just('#'), + Just(';'), + Just('\''), + Just('"'), + Just('$'), + Just('-'), + any::(), + ] + .prop_filter("strict component", |c| { + !matches!(c, '/' | '\0' | ' ' | '\t' | '\n' | '\x0b' | '\x0c' | '\r') + }); + prop::collection::vec(ch, 1..12) + .prop_map(|cs| format!("f{}", cs.into_iter().collect::())) +} + +fn recording_dmsetup(base: &Path) -> PathBuf { + let path = base.join("dmsetup"); + fs::write( + &path, + format!( + "#!/bin/sh\nfor a in \"$@\"; do printf '%s\\0' \"$a\"; done >> '{}/argv'\nprintf '\\1' >> '{}/argv'\nexit 0\n", + base.display(), + base.display() + ), + ) + .unwrap(); + fs::set_permissions(&path, fs::Permissions::from_mode(0o755)).unwrap(); + path +} + +fn recorded_tables(base: &Path) -> Vec { + let raw = fs::read(base.join("argv")).unwrap_or_default(); + raw.split(|&b| b == 1) + .flat_map(|call| { + let args: Vec<&[u8]> = call.split(|&b| b == 0).collect(); + args.windows(2) + .filter(|w| w[0] == b"--table") + .map(|w| String::from_utf8_lossy(w[1]).into_owned()) + .collect::>() + }) + .collect() +} + +fn request(op: &str, files: &[PathBuf]) -> Value { + match op { + "img_activate" => json!({ + "op": "img_activate", + "name": "hyper-hostile-img", + "base": files[0], + "deltas": files[1..], + "chunk_sectors": 8, + }), + _ => json!({ + "op": "thin_pool_create", + "name": "hyper-hostile-pool", + "metadata": files[0], + "data": files[1], + "block_sectors": BLOCK_SECTORS, + "low_water": 0, + }), + } +} + +#[rstest] +#[case::img_activate("img_activate")] +#[case::thin_pool_create("thin_pool_create")] +fn no_caller_path_ever_reaches_the_kernel_as_root(#[case] op: &str) { + if !is_root() { + eprintln!("SKIP: needs root"); + return; + } + let mut runner = TestRunner::new(RunnerConfig { + cases: 12, + ..RunnerConfig::default() + }); + let names = prop::collection::vec(hostile_file_name(), 2..4); + for _ in 0..12 { + let names = names.new_tree(&mut runner).unwrap().current(); + let dir = TempDir::new().unwrap(); + let base = fs::canonicalize(dir.path()).unwrap(); + let dmsetup = recording_dmsetup(&base); + let config = write_config(&base, &dmsetup, &dmsetup); + let files: Vec = names + .iter() + .enumerate() + .map(|(i, n)| { + let p = base.join(std::ffi::OsStr::from_bytes(format!("{i}{n}").as_bytes())); + sparse(&p, 1 << 20); + p + }) + .collect(); + let req = request(op, &files); + let operands = if op == "img_activate" { + &files[..] + } else { + &files[..2] + }; + let operands_valid = operands + .iter() + .all(|f| DevPath::try_from(f.clone()).is_ok()); + + let out = run(&config, req.clone()).get_output().clone(); + + assert_eq!( + out.status.success(), + operands_valid, + "{req}: {}", + String::from_utf8_lossy(&out.stderr) + ); + let tables = recorded_tables(&base); + assert_eq!( + tables.is_empty(), + !operands_valid, + "{req}: tables {tables:?}" + ); + for table in &tables { + for operand in kernel_operands(table) { + assert!( + is_kernel_safe_device(&operand), + "{req}: caller-derived operand {operand:?} reached the kernel in {table:?}" + ); + } + } + } +} diff --git a/native/suidhelper/tests/e2e/fork_delta.rs b/native/suidhelper/tests/e2e/fork_delta.rs index fa0b73b0..77b2a8c9 100644 --- a/native/suidhelper/tests/e2e/fork_delta.rs +++ b/native/suidhelper/tests/e2e/fork_delta.rs @@ -101,7 +101,7 @@ case "$1" in table) case "$2" in hyper-pool) - if [ -e '{b}/linear' ]; then printf '0 100 linear 7:0 0\n'; else printf '0 100 thin-pool 7:0 7:1 128 0 0\n'; fi ;; + if [ -e '{b}/linear' ]; then printf '0 100 linear 7:0 0\n'; else printf '0 100 thin-pool 7:3 7:1 128 0 0\n'; fi ;; hyper-vm1) if [ -e '{b}/parent-linear' ]; then printf '0 2048 linear 7:0 0\n'; else printf '0 2048 thin 253:1 7\n'; fi ;; *) printf '0 2048 thin 253:1 {SCRATCH_ID}\n' ;; @@ -331,6 +331,20 @@ fn nothing_is_snapshotted_unless_pool_and_parent_check_out_as_root( assert!(!pool.thin_dump_ran()); } +#[rstest] +fn a_meta_that_is_not_the_pools_metadata_is_refused_before_any_snapshot_as_root( + pool: Option, +) { + let pool = require!(pool); + + pool.fork_delta(json!({"meta": "/dev/loop4"})) + .failure() + .stderr(contains("is not the pool's metadata device 7:3")); + + assert_eq!(pool.dmsetup_calls(), [TABLE]); + assert!(!pool.thin_dump_ran()); +} + #[rstest] #[case::system_disk_as_meta(json!({"meta": "/dev/sda"}))] #[case::traversing_meta(json!({"meta": "/dev/loop0/../sda"}))]