From e4d4bca74ec944cf03eca8a7cad11978c8a0edf2 Mon Sep 17 00:00:00 2001 From: Enrique Saurez Date: Thu, 5 Nov 2026 13:46:05 -0800 Subject: [PATCH] guest: mount the runtime's file systems in a layered root Part of #1104. Add the root's one-time runtime filesystem setup using the existing entry mount primitives. Restrict the root tree before mounting proc, read-only sysfs, devices, devpts, shared memory and run, optionally bind the time status read-only, and unwind a partial setup. Require runtime setup before live shares or mappings so that the run tmpfs cannot hide them. Register that tmpfs with the persistent mapper so later targets beneath it can be created and removed before the runtime mounts. Keep the entry point crate-private until init calls it, preserve existing per-execution behavior, and cover mount ordering, refusal and rollback with fake-kernel and privileged tests. Verify every runtime attachment before recording its path. Retain the mount descriptor when its post-attach check or time-status lookup and detach fail, so rollback and teardown can retry cleanup after a rename without losing ownership of the mount. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 1930f155-69ef-41b6-9495-323b84ac76f7 --- nvx-rs/README.md | 37 +- nvx-rs/public-api/linux.txt | 12 + nvx-rs/src/guest/entry.rs | 32 +- nvx-rs/src/guest/mappings/mapper.rs | 17 +- nvx-rs/src/guest/root.rs | 328 ++++++++++- nvx-rs/src/guest/root/kernel.rs | 33 +- nvx-rs/src/guest/root/kernel/tests.rs | 74 ++- nvx-rs/src/guest/root/share.rs | 38 +- nvx-rs/src/guest/root/tests.rs | 3 + nvx-rs/src/guest/root/tests/privileged.rs | 388 +++++++++++++ nvx-rs/src/guest/root/tests/runtime.rs | 675 ++++++++++++++++++++++ nvx-rs/src/guest/sys/entry.rs | 161 ++++-- nvx-rs/src/guest/sys/mod.rs | 5 +- scripts/test-layout-allowlist.json | 1 + 14 files changed, 1707 insertions(+), 97 deletions(-) create mode 100644 nvx-rs/src/guest/root/tests/privileged.rs create mode 100644 nvx-rs/src/guest/root/tests/runtime.rs diff --git a/nvx-rs/README.md b/nvx-rs/README.md index ff16eb8df..c472d4e83 100644 --- a/nvx-rs/README.md +++ b/nvx-rs/README.md @@ -1548,9 +1548,29 @@ the caller's mappings. `Root::map`: a caller's host mappings, through the same mapper. A share, a child, the export, or a mapping that fails latches the root. -`Root::teardown`: the shares and mappings in reverse order, then the mounts of -an aggregate and of the export, the later first, the overlay, the layers from -the top down, and the upper, with every failed sync or unmount reported. An +`Root::mount_runtime`: the runtime's file systems, which an init mounts once +for its VM, before the root's first live share or mapping, after it makes every +mount of the root's tree `nosuid,nodev`: procfs at `/proc`, sysfs at `/sys`, +read-only, a tmpfs at `/dev` with an execution's device nodes and links, devpts +at `/dev/pts`, and tmpfs at `/dev/shm` and `/run`, with an execution's options, +then the time status's directory, when the caller names one, bound read-only at +`/run/nvx/time`. It refuses a mount point that leads through a symbolic link or +where something is mounted already, and, before it changes anything, a call +while the root holds the runtime's mounts, whose `/dev` and `/dev/pts` a second +restriction would make `nodev`, or a live share or mapping, since the `/run` +tmpfs would hide one beneath `/run` from the teardown. A call that fails +unmounts what it mounted. A later share or mapping beneath `/run` is on that +tmpfs, which the root's mapper owns, as a guest's mapper owns its `/tmp`. +Each attachment is checked at its named target before it is recorded. A runtime +mount whose attachment fails and which cannot be detached keeps its mount +descriptor, so rollback and teardown can retry cleanup without trusting its +original path. + +`Root::teardown`: the shares and mappings in reverse order, a runtime mount whose +attachment failed and which could not be detached, then the mounts of an +aggregate, of the export, and of the runtime, the later first, the overlay, +the layers from the top down, and the upper, with every failed sync or unmount +reported. An ext4 scratch's file system is synced before its unmount, since a mount namespace that copied the root's mounts keeps them, unsynced, after this one unmounts them, and a scratch whose sync fails stays mounted. @@ -2826,6 +2846,17 @@ NVX_BUNDLE=skip cargo test --locked --all-features --lib -- \ --ignored --exact guest::root::tests::a_thread_with_private_tables_finds_its_mount_points_and_options ``` +Ignored tests of `Root::mount_runtime` mount the runtime's file systems on a +temporary tmpfs root, in a thread's private mount namespace, and check their +options, device nodes, and time status, a call's undo, the teardown, and the +order of the runtime and the mappings beneath `/run`. They need root and Linux +6.8 or later; run them as root with: + +```bash +NVX_BUNDLE=skip cargo test --locked --all-features --lib -- \ + --ignored guest::root::tests::privileged +``` + `tests/openvmm_vectors.rs` is an ignored test that compares the copies of OpenVMM's vectors in `tests/vectors/openvmm/` with the files of the OpenVMM submodule, which the `nvx-rs` job cannot check out, and the versions of diff --git a/nvx-rs/public-api/linux.txt b/nvx-rs/public-api/linux.txt index 1f5fa02b3..0b65b008f 100644 --- a/nvx-rs/public-api/linux.txt +++ b/nvx-rs/public-api/linux.txt @@ -1399,6 +1399,8 @@ field nvx::guest::root::Error::Recovery::device (hidden) field nvx::guest::root::Error::Recovery::number (hidden) variant nvx::guest::root::Error::Reserved (hidden) field nvx::guest::root::Error::Reserved::path (hidden) +variant nvx::guest::root::Error::Restrict (hidden) +field nvx::guest::root::Error::Restrict::error (hidden) variant nvx::guest::root::Error::Runtime (hidden) field nvx::guest::root::Error::Runtime::path (hidden) variant nvx::guest::root::Error::Share (hidden) @@ -1440,6 +1442,8 @@ field nvx::guest::root::Mount::Layer::number (hidden) variant nvx::guest::root::Mount::Mapping (hidden) field nvx::guest::root::Mount::Mapping::target (hidden) variant nvx::guest::root::Mount::Overlay (hidden) +variant nvx::guest::root::Mount::Runtime (hidden) +field nvx::guest::root::Mount::Runtime::0 (hidden) variant nvx::guest::root::Mount::Scratch (hidden) variant nvx::guest::root::Mount::Share (hidden) field nvx::guest::root::Mount::Share::tag (hidden) @@ -1469,6 +1473,14 @@ method nvx::guest::root::Root::path (hidden) method nvx::guest::root::Root::runtime (hidden) method nvx::guest::root::Root::teardown (hidden) method nvx::guest::root::Root::upper (hidden) +enum nvx::guest::root::RuntimeMounts (hidden) +variant nvx::guest::root::RuntimeMounts::Dev (hidden) +variant nvx::guest::root::RuntimeMounts::DevPts (hidden) +variant nvx::guest::root::RuntimeMounts::DevShm (hidden) +variant nvx::guest::root::RuntimeMounts::Proc (hidden) +variant nvx::guest::root::RuntimeMounts::Run (hidden) +variant nvx::guest::root::RuntimeMounts::Sys (hidden) +variant nvx::guest::root::RuntimeMounts::TimeStatus (hidden) struct nvx::guest::root::ShareChild (hidden) field nvx::guest::root::ShareChild::access (hidden) field nvx::guest::root::ShareChild::kind (hidden) diff --git a/nvx-rs/src/guest/entry.rs b/nvx-rs/src/guest/entry.rs index b30b1fec2..d373c4514 100644 --- a/nvx-rs/src/guest/entry.rs +++ b/nvx-rs/src/guest/entry.rs @@ -126,6 +126,7 @@ use std::io::{self, Read}; use std::os::unix::ffi::OsStrExt; use std::path::{Path, PathBuf}; +use super::mappings::mapper::{AttachFailure, bind_retained_with}; use super::mappings::{ self, Access, Kernel, Kind, Mapped, Mapping, MappingError, SourceRoot, Step, SystemKernel, TargetRoot, TeardownError, @@ -155,8 +156,9 @@ const UNCHANGED_ID: u32 = u32::MAX; const MACHINE_ID_MODE: u32 = 0o444; /// Where [`Entry::bind_runtime`] binds a generated machine ID in the root. const MACHINE_ID_TARGET: &str = "/etc/machine-id"; -/// Where [`Entry::bind_runtime`] binds the time status in the root. -const TIME_STATUS_TARGET: &str = "/run/nvx/time"; +/// Where [`Entry::bind_runtime`] binds the time status in the root, as a layered root's +/// [`mount_runtime`](super::root::Root::mount_runtime) does. +pub(crate) const TIME_STATUS_TARGET: &str = "/run/nvx/time"; /// Where [`Entry::bind_runtime`] binds the resolver file in the root. const RESOLVER_TARGET: &str = "/etc/resolv.conf"; @@ -876,13 +878,35 @@ fn bind_file( /// Binds the file or directory at `path`, which must be of `kind`, read-only at `target` beneath /// `root`, with `kernel`, through the walk and attach of [`mappings::bind`] from a source root at /// `path`'s parent directory, and returns the bind. -fn bind_runtime_file( +pub(crate) fn bind_runtime_file( kernel: &mut dyn Kernel, root: &TargetRoot, path: &Path, target: &str, kind: Kind, ) -> Result { + let (source, mapping) = runtime_mapping(path, target, kind)?; + mappings::bind_with(kernel, root, &source, &mapping) +} + +/// Binds as [`bind_runtime_file`] does, retaining a failed bind that its caller must detach. +pub(crate) fn bind_runtime_file_retained( + kernel: &mut dyn Kernel, + root: &TargetRoot, + path: &Path, + target: &str, + kind: Kind, +) -> Result { + let (source, mapping) = runtime_mapping(path, target, kind)?; + bind_retained_with(kernel, root, &source, &mapping) +} + +/// Prepares the source root and read-only mapping for a runtime bind. +fn runtime_mapping( + path: &Path, + target: &str, + kind: Kind, +) -> Result<(SourceRoot, Mapping), MappingError> { let source_error = |error: i32| MappingError::System { step: Step::Source, error, @@ -895,7 +919,7 @@ fn bind_runtime_file( let mapping = Mapping::new(name, target, Access::ReadOnly) .map_err(|_| source_error(Errno::EINVAL as i32))? .with_kind(kind); - mappings::bind_with(kernel, root, &source, &mapping) + Ok((source, mapping)) } /// Returns the directory that holds the runtime file at `path`, the current directory for a diff --git a/nvx-rs/src/guest/mappings/mapper.rs b/nvx-rs/src/guest/mappings/mapper.rs index 990f282e0..2e3dde9eb 100644 --- a/nvx-rs/src/guest/mappings/mapper.rs +++ b/nvx-rs/src/guest/mappings/mapper.rs @@ -800,6 +800,16 @@ pub(crate) fn bind_with( source: &SourceRoot, mapping: &Mapping, ) -> Result { + bind_retained_with(kernel, root, source, mapping).map_err(|failure| failure.error) +} + +/// Implements [`bind_with`], retaining a moved mount that could not be detached for its caller. +pub(crate) fn bind_retained_with( + kernel: &mut dyn Kernel, + root: &TargetRoot, + source: &SourceRoot, + mapping: &Mapping, +) -> Result { attach( kernel, root, @@ -813,7 +823,6 @@ pub(crate) fn bind_with( target: mapping.target().to_path_buf(), mount, }) - .map_err(|failure| failure.error) } /// Unmounts `binds`, which [`bind_with`] made beneath `root`, in reverse order, with `kernel`, as a @@ -852,11 +861,11 @@ enum Source<'a> { /// Why an attach failed, with the descriptor of the mapping's mount if a rename moved it and /// detaching it failed too, so that it stays mounted wherever it went. #[derive(Debug)] -struct AttachFailure { +pub(crate) struct AttachFailure { /// Why it failed. - error: MappingError, + pub(crate) error: MappingError, /// The descriptor of the mapping's mount, if it stays mounted. - stray: Option, + pub(crate) stray: Option, } impl From for AttachFailure { diff --git a/nvx-rs/src/guest/root.rs b/nvx-rs/src/guest/root.rs index f65f02ad3..148a7d482 100644 --- a/nvx-rs/src/guest/root.rs +++ b/nvx-rs/src/guest/root.rs @@ -23,11 +23,16 @@ //! the root, [`Root::mount_export`] mounts the host export out of the root, and [`Root::map`] //! applies a caller's host mappings in the root, from the export or another source, through the //! root's [`Mapper`], as [live shares and mappings](#live-shares-and-mappings) describes. +//! - `Root::mount_runtime`, which is crate-private, mounts the runtime's file systems in the root, +//! as an init does once for its VM, before the root's first live share or mapping: it makes +//! every mount of the root's tree `nosuid` and `nodev`, then mounts `/proc`, `/sys`, `/dev` with +//! `/dev/pts` and `/dev/shm`, and `/run`, and binds the guest's time status at `/run/nvx/time`. //! - [`Root::teardown`] unmounts, in order, the live shares and mappings in reverse order, the -//! mounts of an aggregate live share and of the host export, the later first, the overlay, the -//! layers from the top down, and the upper, whose file system it syncs first if the upper is an -//! ext4 scratch. It attempts each, whatever failed before, and reports every sync or unmount -//! that failed. A root that is dropped tears down the same way, but ignores failures. +//! mounts of an aggregate live share, of the host export, and of the runtime, the later first, +//! the overlay, the layers from the top down, and the upper, whose file system it syncs first +//! if the upper is an ext4 scratch. It attempts each, whatever failed before, and reports every +//! sync or unmount that failed. A root that is dropped tears down the same way, but ignores +//! failures. //! //! A failure is an [`Error`] that names what failed. The operations need the guest's root user, //! in the guest's initial mount and cgroup namespaces. They never collect a child's status, @@ -92,12 +97,12 @@ //! [`mappings`](super::mappings) describes, with NVX's container root's reserved targets, //! [`Reserved::container_root`](super::mappings::Reserved::container_root): it walks to each //! target through no symbolic link or magic link; creates each missing component only on the -//! overlay, a directory with mode 0755, or a file child's file with mode 0644, whatever the -//! umask or a default ACL; checks that the target is a directory, or a regular file for a child -//! that is a host file; and attaches each mount with its [`Access`](super::mappings::Access), -//! `nosuid`, and `nodev` set already. The root mounts a share's device at `staging`, with them, -//! then moves it onto its target, and mounts an aggregate's device at `shares`, then attaches a -//! copy of each child's entry there. +//! overlay, or on the runtime's `/run` tmpfs once the root has mounted it, a directory with mode +//! 0755, or a file child's file with mode 0644, whatever the umask or a default ACL; checks that +//! the target is a directory, or a regular file for a child that is a host file; and attaches +//! each mount with its [`Access`](super::mappings::Access), `nosuid`, and `nodev` set already. +//! The root mounts a share's device at `staging`, with them, then moves it onto its target, and +//! mounts an aggregate's device at `shares`, then attaches a copy of each child's entry there. //! [`Root::map`] applies a caller's host [`Mapping`](super::mappings::Mapping)s beneath the //! overlay the same way, from the caller's [`SourceRoot`](super::mappings::SourceRoot). //! @@ -169,6 +174,7 @@ mod share; #[cfg(test)] pub(crate) mod tests; +use std::ffi::CStr; use std::fmt; use std::fs::{self, File}; use std::io; @@ -178,9 +184,10 @@ use std::path::{Path, PathBuf}; use std::time::{Duration, Instant}; use self::kernel::{Kernel, SystemKernel}; +use super::entry::{self, TIME_STATUS_TARGET}; use super::gpt::DeviceError; -use super::mappings::{Mapper, MappingError, below_its_root}; -use super::sys::{self, Errno, MountFlags}; +use super::mappings::{Kind, Mapper, MappingError, TargetRoot, below_its_root}; +use super::sys::{self, Errno, FileDescriptor, FileSystem, Mode, MountFlags, OpenFlags}; use crate::gpt::{Expected, GptError, Partition}; use crate::uuid::Uuid; @@ -235,6 +242,19 @@ const RUNTIME_RESERVED: &[u8] = b",:\\=\0"; /// The directory of the overlay module's parameters, below sysfs, which hold the defaults of the /// overlay's options. const OVERLAY_PARAMETERS: &str = "module/overlay/parameters"; +/// The runtime's file systems, in the order of [`Root::mount_runtime`]'s mounts, each with its +/// directory's name: in the root, or in its `/dev` for `/dev/pts` and `/dev/shm`. +const RUNTIME_FILE_SYSTEMS: [(RuntimeMounts, &CStr, &FileSystem); 6] = [ + (RuntimeMounts::Proc, c"proc", &sys::PROC), + (RuntimeMounts::Sys, c"sys", &sys::SYS), + (RuntimeMounts::Dev, c"dev", &sys::DEV), + (RuntimeMounts::DevPts, c"pts", &sys::DEV_PTS), + (RuntimeMounts::DevShm, c"shm", &sys::DEV_SHM), + (RuntimeMounts::Run, c"run", &sys::RUN), +]; +/// The mode of a directory that [`Root::mount_runtime`] creates for a mount, but for what the +/// umask withholds, as an execution's entry creates it. +const RUNTIME_DIRECTORY_MODE: u32 = 0o755; /// The most bytes of options that `mount(2)` passes a file system whole: one 4 KiB page, the /// smallest into which Linux copies them, less the NUL that ends them. Linux cuts longer options @@ -602,6 +622,7 @@ fn assemble_with( overlay_mode: assembly.overlay, first_partition: None, shares: Vec::new(), + runtime_stray: None, mapper: None, mapped: Vec::new(), latched: false, @@ -679,11 +700,15 @@ pub struct Root { overlay_mode: OverlayMode, /// The bytes of a GPT disk's first partition, without their trailing padding. first_partition: Option>, - /// The mounts of live shares and of the host export that the root unmounts by path, and their - /// mount points, in mount order: an aggregate's, the export's, and a share's that stays at - /// `staging` because its unmount failed. + /// The mounts that the root unmounts by path, and their mount points, in mount order: an + /// aggregate live share's, the host export's, a share's that stays at `staging` because its + /// unmount failed, and the runtime's in the root. shares: Vec<(Mount, PathBuf)>, - /// The mapper of the live shares and mappings in the root, once the root has mounted one. + /// A runtime mount whose attachment failed and which could not be detached during setup or + /// its rollback. + runtime_stray: Option<(RuntimeMounts, FileDescriptor)>, + /// The mapper of the live shares and mappings in the root, once the root has mounted one, whose + /// own mounts are the overlay and the runtime's `/run` tmpfs, if the root held it then. mapper: Option, /// What each of the mapper's mappings mounts, in the mapper's order, then what the one that /// a rename moved and that the mapper failed to detach mounts, if any. @@ -740,11 +765,12 @@ impl Root { /// Unmounts the root's mounts, in order: a live share or a mapping that a rename moved and /// that its mapper failed to detach, which the mapper detaches, the live shares and mappings - /// in reverse order, the mounts of an aggregate live share and of the host export, the later + /// in reverse order, a runtime mount whose attachment failed and which could not be detached, + /// the mounts of an aggregate live share, of the host export, and of the runtime, the later /// first, the overlay, the layers from the top down, and the upper. /// - /// Each unmount is attempted, whatever failed before, without detaching a busy mount but the - /// moved one, and every one that failed is reported, with the mount that it leaves in place. + /// Each unmount is attempted, whatever failed before, without detaching a busy mount except + /// one retained after a failed attachment, and every failure names the mount it leaves in place. /// A [`SourceRoot`](super::mappings::SourceRoot) of the export that the caller still holds /// keeps the export busy. /// @@ -759,6 +785,159 @@ impl Root { self.unmount() } + /// Mounts the runtime's file systems in the root, as an init does once for its VM before it + /// switches into the root. It first makes every mount of the root's tree `nosuid` and `nodev`, + /// then mounts, in order, an execution's file systems, with an execution's options: + /// + /// - procfs at `/proc`, `nosuid`, `nodev`, and `noexec`; + /// - sysfs at `/sys`, read-only, `nosuid`, `nodev`, and `noexec`; + /// - a tmpfs at `/dev`, `nosuid`, with mode 0755, which holds the device nodes `null`, `zero`, + /// `full`, `random`, `urandom`, and `tty`, with mode 0666 whatever the umask, and the links + /// `ptmx`, `fd`, `stdin`, `stdout`, and `stderr`; + /// - devpts at `/dev/pts`, an instance of its own, `nosuid` and `noexec`; + /// - a tmpfs at `/dev/shm`, `nosuid` and `nodev`, with mode 1777; + /// - and a tmpfs at `/run`, `nosuid` and `nodev`, with mode 0755. + /// + /// With a `time_status`, the directory that holds the guest's time status, it then binds that + /// directory read-only, `nosuid`, and `nodev` at `/run/nvx/time`, creating `/run/nvx` and + /// `/run/nvx/time` on the `/run` tmpfs with mode 0755, as an execution's + /// [`Entry::bind_runtime`](super::entry::Entry::bind_runtime) binds it. Without one, it binds + /// nothing there. + /// + /// It opens each mount point beneath the root, through no symbolic link, and creates one that + /// is missing with mode 0755, but for what the umask withholds. A mount point where something + /// is mounted already is refused as [`Error::Occupied`]. So is a call while the root holds a + /// runtime mount, before it changes anything, since restricting the tree again would make an + /// earlier call's `/dev` and `/dev/pts` `nodev`: a second call changes nothing. So is a call + /// while the root holds a live share or a mapping, before it changes anything too, since the + /// `/run` tmpfs would hide one beneath `/run` from the teardown: the runtime comes first. A + /// later share or mapping beneath `/run` is on the `/run` tmpfs, where the root's mapper + /// creates what its target lacks, and the teardown unmounts it before the tmpfs. + /// The mounts belong to the root, which unmounts them, the later first, when it is torn down. + /// If one fails, the call unmounts those that it made, the later first, and returns the + /// failure; one that fails to unmount stays for the teardown. The tree stays `nosuid` and + /// `nodev`. Each attachment is checked at its named target before it is recorded. A runtime + /// mount whose attachment fails and which cannot be detached keeps its mount descriptor, + /// so rollback and teardown can retry the detach without relying on its original path. + #[cfg_attr(not(test), expect(dead_code, reason = "nvx-init calls it (#1115)"))] + pub(crate) fn mount_runtime(&mut self, time_status: Option<&Path>) -> Result<(), Error> { + if let Some((runtime, _)) = &self.runtime_stray { + return Err(Error::Occupied { + path: self.path.join(&runtime.target()[1..]), + }); + } + let made = self.shares.len(); + let mounted = self.mount_runtime_at(time_status); + if mounted.is_err() { + if let Some((runtime, tree)) = self.runtime_stray.take() + && self.kernel.detach(&tree).is_err() + { + self.runtime_stray = Some((runtime, tree)); + } + let mut kept = Vec::new(); + for (mount, path) in self.shares.split_off(made).into_iter().rev() { + let unmounted = + sys::path_c_string(&path).and_then(|target| self.kernel.unmount(&target)); + if unmounted.is_err() { + kept.push((mount, path)); + } + } + self.shares.extend(kept.into_iter().rev()); + } + mounted + } + + /// Makes [`mount_runtime`](Self::mount_runtime)'s mounts, and records each, until one fails. + fn mount_runtime_at(&mut self, time_status: Option<&Path>) -> Result<(), Error> { + // Restricting the tree again would make an earlier call's `/dev` and `/dev/pts` `nodev`. + if let Some((_, path)) = self + .shares + .iter() + .find(|(mount, _)| matches!(mount, Mount::Runtime(_))) + { + return Err(Error::Occupied { path: path.clone() }); + } + // The `/run` tmpfs would hide a share or a mapping beneath `/run` from the teardown. + if let Some( + Mount::Share { target, .. } | Mount::Child { target, .. } | Mount::Mapping { target }, + ) = self.mapped.first() + { + let path = self.path.join(target.strip_prefix("/").unwrap_or(target)); + return Err(Error::Occupied { path }); + } + let root = share::open_mount_point(&self.path).map_err(|error| Error::MountPoint { + path: self.path.clone(), + error: error.into(), + })?; + self.kernel + .restrict_tree(&root) + .map_err(|error| Error::Restrict { + error: error.into(), + })?; + let mut dev = None; + for (runtime, name, file_system) in RUNTIME_FILE_SYSTEMS { + let mount = Mount::Runtime(runtime); + let path = self.path.join(&runtime.target()[1..]); + let parent = match (&dev, runtime) { + (Some(dev), RuntimeMounts::DevPts | RuntimeMounts::DevShm) => dev, + _ => &root, + }; + let target = runtime_target(self.kernel.as_mut(), parent, name, &path)?; + let failed = |error: Errno| Error::Mount { + mount: mount.clone(), + error: error.into(), + }; + let tree = self.kernel.new_mount(file_system).map_err(failed)?; + self.kernel.move_mount(&tree, &target).map_err(failed)?; + let opened = match attached(self.kernel.as_mut(), parent, name, &tree) { + Ok(opened) => opened, + Err(error) => { + if self.kernel.detach(&tree).is_err() { + self.runtime_stray = Some((runtime, tree)); + } + return Err(failed(error)); + } + }; + self.shares.push((mount.clone(), path)); + if runtime == RuntimeMounts::Dev { + self.kernel.create_devices(&opened).map_err(failed)?; + dev = Some(opened); + } + } + if let Some(source) = time_status { + let mount = Mount::Runtime(RuntimeMounts::TimeStatus); + let run = self.path.join("run"); + let mut target = + TargetRoot::open_with(self.kernel.as_mut(), &self.path).map_err(|error| { + Error::MountPoint { + path: self.path.clone(), + error, + } + })?; + // The mount points that the bind lacks are created on the `/run` tmpfs. + target + .add_mount_with(self.kernel.as_mut(), &run) + .map_err(|error| Error::MountPoint { path: run, error })?; + let bound = entry::bind_runtime_file_retained( + self.kernel.as_mut(), + &target, + source, + TIME_STATUS_TARGET, + Kind::Directory, + ); + if let Err(failure) = bound { + self.runtime_stray = failure.stray.map(|tree| (RuntimeMounts::TimeStatus, tree)); + return Err(Error::Mount { + mount, + error: share::io_error(&failure.error), + }); + } + let path = self.path.join(&TIME_STATUS_TARGET[1..]); + self.shares.push((mount, path)); + } + Ok(()) + } + /// Implements [`assemble`]'s steps after the first, waiting for each device until `deadline` /// at the latest, if any, and accepting the upper's `contents`. fn mount_layers( @@ -1121,6 +1300,16 @@ impl Root { }); } } + if let Some((runtime, tree)) = self.runtime_stray.take() + && let Err(error) = self.kernel.detach(&tree) + { + failures.push(Unmount { + mount: Mount::Runtime(runtime), + path: self.path.join(&runtime.target()[1..]), + step: Step::Unmount, + error: error.into(), + }); + } let mut mounts: Vec<(Mount, PathBuf)> = self.shares.drain(..).rev().collect(); mounts.extend(self.overlay.take().map(|path| (Mount::Overlay, path))); mounts.extend( @@ -1183,12 +1372,64 @@ impl fmt::Debug for Root { &self.first_partition.as_ref().map(Vec::len), ) .field("shares", &self.shares) + .field("runtime_stray", &self.runtime_stray) .field("mapped", &self.mapped) .field("latched", &self.latched) .finish_non_exhaustive() } } +/// Opens the directory `name` beneath `directory`, on its mount and through no symbolic link, as +/// a mount point of [`Root::mount_runtime`] at `path`, and creates it if it is missing. +fn runtime_target( + kernel: &mut dyn Kernel, + directory: &FileDescriptor, + name: &CStr, + path: &Path, +) -> Result { + let flags = OpenFlags::O_PATH | OpenFlags::O_DIRECTORY; + let open = + |kernel: &mut dyn Kernel| kernel.open_beneath(directory, name, flags, Mode::empty(), false); + let opened = match open(kernel) { + Err(Errno::ENOENT) => { + let mode = Mode::from_bits_truncate(RUNTIME_DIRECTORY_MODE); + match kernel.create_directory(directory, name, mode) { + Ok(()) | Err(Errno::EEXIST) => open(kernel), + Err(error) => Err(error), + } + } + opened => opened, + }; + opened.map_err(|error| match error { + // Something is mounted there already. + Errno::EXDEV => Error::Occupied { + path: path.to_owned(), + }, + error => Error::MountPoint { + path: path.to_owned(), + error: error.into(), + }, + }) +} + +/// Opens the directory `name` beneath `directory`, into the mount there, which must be the one +/// whose root `tree` refers to. +fn attached( + kernel: &mut dyn Kernel, + directory: &FileDescriptor, + name: &CStr, + tree: &FileDescriptor, +) -> Result { + let mount = kernel.status(tree)?.mount; + let flags = OpenFlags::O_PATH | OpenFlags::O_DIRECTORY; + let opened = kernel.open_beneath(directory, name, flags, Mode::empty(), true)?; + let status = kernel.status(&opened)?; + if status.mount != mount || !status.mount_root { + return Err(Errno::EBUSY); + } + Ok(opened) +} + /// Checks the EROFS image on the distro device `device` for the UUID `expected`, if the caller /// expects one, in its superblock, and reads nothing else. fn check_erofs_uuid(device: &Path, expected: Option) -> Result<(), Error> { @@ -1448,6 +1689,8 @@ pub enum Mount { /// The mapping's target in the root. target: PathBuf, }, + /// One of the runtime's mounts in the root, which `Root::mount_runtime` makes. + Runtime(RuntimeMounts), } impl fmt::Display for Mount { @@ -1471,6 +1714,43 @@ impl fmt::Display for Mount { ) } Self::Mapping { target } => write!(formatter, "the mapping at {}", target.display()), + Self::Runtime(mount) => write!(formatter, "the runtime's {}", mount.target()), + } + } +} + +/// Names one of the runtime's fixed mounts, which `Root::mount_runtime` makes in the root in the +/// order of these variants. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +#[non_exhaustive] +pub enum RuntimeMounts { + /// procfs, at `/proc`. + Proc, + /// sysfs, at `/sys`. + Sys, + /// The tmpfs of the device nodes, at `/dev`. + Dev, + /// devpts, at `/dev/pts`. + DevPts, + /// The tmpfs of shared memory, at `/dev/shm`. + DevShm, + /// The tmpfs of the runtime's state, at `/run`. + Run, + /// The bind of the guest's time status, at `/run/nvx/time`. + TimeStatus, +} + +impl RuntimeMounts { + /// Returns the mount's target in the root. + pub(crate) const fn target(self) -> &'static str { + match self { + Self::Proc => "/proc", + Self::Sys => "/sys", + Self::Dev => "/dev", + Self::DevPts => "/dev/pts", + Self::DevShm => "/dev/shm", + Self::Run => "/run", + Self::TimeStatus => TIME_STATUS_TARGET, } } } @@ -1659,6 +1939,12 @@ pub enum Error { /// The error. error: io::Error, }, + /// The mounts of the root's tree could not be made `nosuid` and `nodev`, before + /// `Root::mount_runtime` mounted anything. + Restrict { + /// The error. + error: io::Error, + }, /// A live share, a child of an aggregate live share, or a mapping was refused or failed to /// attach at its target in the root, once the root had checked its request; nothing of it /// stays mounted, unless the error is a moved mapping that stays mounted. The root refuses @@ -1809,6 +2095,10 @@ impl fmt::Display for Error { path.display() ), Self::Mount { mount, error } => write!(formatter, "failed to mount {mount}: {error}"), + Self::Restrict { error } => write!( + formatter, + "failed to make the root's mounts nosuid and nodev: {error}" + ), Self::Mapping { mount, error } => write!(formatter, "failed to mount {mount}: {error}"), Self::Share(refusal) => refusal.fmt(formatter), } diff --git a/nvx-rs/src/guest/root/kernel.rs b/nvx-rs/src/guest/root/kernel.rs index 32325890d..796eb0b46 100644 --- a/nvx-rs/src/guest/root/kernel.rs +++ b/nvx-rs/src/guest/root/kernel.rs @@ -7,7 +7,7 @@ //! or a read of a GPT disk's partition table at the geometry that its block device reports, and //! the writes to the kernel's control files, the check for a mount point, and the options of a //! mount, whose effects and answers a test simulates, go through a [`Kernel`], which also performs -//! the mapper's operations for the root's live shares and mappings. +//! the mapper's operations for the root's live shares and mappings, and the runtime's mounts. use std::ffi::CStr; use std::fs::{self, File}; @@ -19,7 +19,7 @@ use crate::gpt::{Disk, Expected}; use crate::guest::gpt::{self, DeviceError}; use crate::guest::mappings::Kernel as MappingKernel; use crate::guest::parse::parse_decimal; -use crate::guest::sys::{self, Errno, Mode, MountFlags, OpenFlags}; +use crate::guest::sys::{self, Errno, FileDescriptor, FileSystem, Mode, MountFlags, OpenFlags}; pub(crate) use crate::guest::mappings::SystemKernel; @@ -30,8 +30,9 @@ const MOUNT_TABLE: &str = "/proc/thread-self/mountinfo"; /// unmounts, flushes of a block device's buffers, syncs of a mounted file system, rereads of a /// disk's partition table, writes of a control file, and the sleeps between two polls for a block /// device; the check for a mount point and the options of a mount, which depend on them; since -/// only a block device reports its geometry, the read of a GPT disk's partition table; and the -/// mapper's operations, through which the root mounts its live shares and mappings. +/// only a block device reports its geometry, the read of a GPT disk's partition table; the +/// mapper's operations, through which the root mounts its live shares and mappings; and the new +/// mounts, the restriction of the root's tree, and the device nodes of the runtime's file systems. /// /// [`SystemKernel`] performs them. The tests' fake records the operations, simulates the effect /// of a cgroup's membership file and its own mounts, reads a GPT disk's table from a regular @@ -88,6 +89,18 @@ pub(super) trait Kernel: MappingKernel + Send + Sync { /// Sleeps for `duration`. fn sleep(&mut self, duration: Duration); + + /// Mounts a new instance of `file_system`, detached, as `fsopen`, `fsconfig`, and `fsmount` + /// do, and returns its mount's descriptor, which [`MappingKernel::move_mount`] attaches. + fn new_mount(&mut self, file_system: &FileSystem) -> Result; + + /// Sets `nosuid` and `nodev` on the mount whose root `directory` refers to, and on every + /// mount below it. + fn restrict_tree(&mut self, directory: &FileDescriptor) -> Result<(), Errno>; + + /// Creates the device nodes and the symbolic links of an execution's `/dev` in the directory + /// `dev`, the nodes with mode 0666 whatever the umask. + fn create_devices(&mut self, dev: &FileDescriptor) -> Result<(), Errno>; } impl Kernel for SystemKernel { @@ -171,6 +184,18 @@ impl Kernel for SystemKernel { fn sleep(&mut self, duration: Duration) { std::thread::sleep(duration); } + + fn new_mount(&mut self, file_system: &FileSystem) -> Result { + sys::mount_detached(file_system) + } + + fn restrict_tree(&mut self, directory: &FileDescriptor) -> Result<(), Errno> { + sys::restrict_tree(directory) + } + + fn create_devices(&mut self, dev: &FileDescriptor) -> Result<(), Errno> { + sys::create_devices(dev) + } } /// Returns the options of the file system of the mount whose ID is `mount`, the last field of its diff --git a/nvx-rs/src/guest/root/kernel/tests.rs b/nvx-rs/src/guest/root/kernel/tests.rs index 57b18048d..d4d626790 100644 --- a/nvx-rs/src/guest/root/kernel/tests.rs +++ b/nvx-rs/src/guest/root/kernel/tests.rs @@ -10,7 +10,8 @@ use crate::gpt::{Disk, Expected}; use crate::guest::gpt::DeviceError; use crate::guest::mappings::{Access, Kernel as MappingKernel}; use crate::guest::sys::{ - self, Errno, FileDescriptor, FileKind, FileStatus, Mode, MountAttributes, MountFlags, OpenFlags, + self, Errno, FileDescriptor, FileKind, FileStatus, FileSystem, Mode, MountAttributes, + MountFlags, OpenFlags, }; /// The unique ID of the mount that every path is on that none of the fake kernel's mounts @@ -61,6 +62,8 @@ pub(crate) enum Operation { Detach, /// A read of a mount's options, which its mount point names. Options, + /// A new mount of a file system, which its type names. + NewMount, } /// One operation that the fake kernel recorded, with its paths and options as text. @@ -116,6 +119,20 @@ pub(crate) enum Call { }, /// A detachment of a tree from its mount point, as a lazy unmount does. Detach(String), + /// A new mount of a file system, detached. + NewMount { + /// The file system's type. + kind: String, + /// The file system's parameters, each as `key=value`. + parameters: Vec, + /// The mount's attributes, `MOUNT_ATTR_*`. + attributes: u64, + }, + /// A restriction of a directory's mount and every mount below it to `nosuid` and `nodev`, + /// which the directory names. + RestrictTree(String), + /// A creation of the device nodes and the links of a `/dev`, which its directory names. + CreateDevices(String), } /// The type of the action that runs after each sleep. @@ -125,10 +142,11 @@ type MoveAction = Box; /// The type of the action that runs after each creation of a file, with its path. type CreateAction = Box; -/// A mount that `open_tree` returned a descriptor of. +/// A mount that `open_tree` or a new mount returned a descriptor of. #[derive(Clone, Debug)] struct Tree { - /// The path of the file whose mount the tree is, or copies. + /// The path of the file whose mount the tree is, or copies, or a new mount's file system + /// type. source: PathBuf, /// The kind of that file. kind: FileKind, @@ -475,6 +493,14 @@ fn path(path: &CStr) -> PathBuf { PathBuf::from(OsStr::from_bytes(path.to_bytes())) } +/// Returns a memory file that stands for the tree of number `number`. +fn tree_file(number: usize) -> Result { + let name = format!("nvx-fake-tree-{number}"); + let flags = nix::sys::memfd::MFdFlags::MFD_CLOEXEC; + let file = nix::sys::memfd::memfd_create(name.as_str(), flags)?; + Ok(FileDescriptor::from_owned(file)) +} + /// Returns the path that leads to the file that `descriptor` opened now, from the calling /// thread's descriptors in procfs. fn path_of(descriptor: &FileDescriptor) -> Result { @@ -670,6 +696,43 @@ impl Kernel for FakeKernel { } }); } + + fn new_mount(&mut self, file_system: &FileSystem) -> Result { + let kind = text(file_system.kind); + let parameters = file_system + .parameters + .iter() + .map(|(key, value)| format!("{}={}", text(key), text(value))) + .collect(); + let call = Call::NewMount { + kind: kind.clone(), + parameters, + attributes: file_system.attributes, + }; + self.record(call, Operation::NewMount, Path::new(&kind))?; + let number = self.with(|state| { + let mount = state.next_mount(); + state.trees.push(Tree { + source: PathBuf::from(kind), + kind: FileKind::Directory, + mount, + }); + state.trees.len() - 1 + }); + tree_file(number) + } + + fn restrict_tree(&mut self, directory: &FileDescriptor) -> Result<(), Errno> { + let path = path_of(directory)?; + let call = Call::RestrictTree(path.display().to_string()); + self.record(call, Operation::SetAttributes, &path) + } + + fn create_devices(&mut self, dev: &FileDescriptor) -> Result<(), Errno> { + let path = path_of(dev)?; + let call = Call::CreateDevices(path.display().to_string()); + self.record(call, Operation::Create, &path) + } } impl MappingKernel for FakeKernel { @@ -776,10 +839,7 @@ impl MappingKernel for FakeKernel { }); state.trees.len() - 1 }); - let name = format!("nvx-fake-tree-{number}"); - let flags = nix::sys::memfd::MFdFlags::MFD_CLOEXEC; - let file = nix::sys::memfd::memfd_create(name.as_str(), flags)?; - Ok(FileDescriptor::from_owned(file)) + tree_file(number) } fn set_attributes( diff --git a/nvx-rs/src/guest/root/share.rs b/nvx-rs/src/guest/root/share.rs index 5861a0ddf..321c54a42 100644 --- a/nvx-rs/src/guest/root/share.rs +++ b/nvx-rs/src/guest/root/share.rs @@ -12,7 +12,7 @@ use std::path::{Path, PathBuf}; use super::{ AGGREGATE_DIRECTORY, EXPORT_DIRECTORY, Error, HOSTFS_DIRECTORY, HOSTFS_DIRECTORY_MODE, Mount, - Root, STAGING_DIRECTORY, + Root, RuntimeMounts, STAGING_DIRECTORY, }; use crate::guest::mappings::{ self, Access, Kernel as MappingKernel, Kind, Mapper, Mapping, MappingError, Reserved, @@ -159,8 +159,8 @@ impl Root { /// the targets of the root's other shares and mappings; nothing is mounted for a refused one. /// The device is mounted at `staging` in the runtime directory, which may not be a mount /// point already, with its flags, then the root's mapper moves it onto the target, which it - /// creates on the overlay if it is missing. The mount belongs to the root, which unmounts it - /// first when it is torn down. + /// creates on the overlay, or on the runtime's `/run` tmpfs, if it is missing. The mount + /// belongs to the root, which unmounts it first when it is torn down. /// /// A failure once the target passed its checks latches the root, and unmounts the device from /// `staging`, or leaves it there for the teardown if that unmount fails too. @@ -190,11 +190,12 @@ impl Root { /// Every child is checked first: its name, which no other child has, and its target, as the /// [module](super#live-shares-and-mappings) describes, against the other children's and the /// root's other shares' and mappings' targets. Nothing is mounted unless every child passes. - /// Each target is created on the overlay if it is missing. A child whose entry in the - /// aggregate is missing, or is not of the child's kind, fails to mount, and latches the root, - /// as any failure once the children passed their checks does. The mounts belong to the root, - /// which unmounts them first when it is torn down, the bound children in reverse order, then - /// the aggregate, those of an aggregate that failed part of the way included. + /// Each target is created on the overlay, or on the runtime's `/run` tmpfs, if it is missing. + /// A child whose entry in the aggregate is missing, or is not of the child's kind, fails to + /// mount, and latches the root, as any failure once the children passed their checks does. + /// The mounts belong to the root, which unmounts them first when it is torn down, the bound + /// children in reverse order, then the aggregate, those of an aggregate that failed part of + /// the way included. /// /// A root has one aggregate at most, since every aggregate is mounted at the same place: once /// the root has mounted one, whether or not its children were all bound, another is refused. @@ -496,7 +497,9 @@ impl Root { } /// Returns the kernel and the root's mapper, which it opens beneath the overlay, with NVX's - /// container root's reserved targets, unless the root has one already. + /// container root's reserved targets, unless the root has one already. The mapper's own + /// mounts, on which it creates what a target lacks, are the overlay and, if the root holds + /// it, the runtime's `/run` tmpfs, as a guest's mapper owns its `/tmp`. fn mapper(&mut self) -> Result<(&mut dyn MappingKernel, &mut Mapper), Error> { let mapper = match self.mapper.take() { Some(mapper) => mapper, @@ -508,13 +511,26 @@ impl Root { error, } })?; - Mapper::new(root, Reserved::container_root()) + Mapper::new(self.add_run(root)?, Reserved::container_root()) } }; let mapper = self.mapper.insert(mapper); Ok((self.kernel.as_mut(), mapper)) } + /// Adds the runtime's `/run` tmpfs, if the root holds it, to `root`'s own mounts. + fn add_run(&mut self, mut root: TargetRoot) -> Result { + let run = Mount::Runtime(RuntimeMounts::Run); + if let Some((_, path)) = self.shares.iter().find(|(mount, _)| *mount == run) { + root.add_mount_with(self.kernel.as_mut(), path) + .map_err(|error| Error::MountPoint { + path: path.clone(), + error, + })?; + } + Ok(root) + } + /// Refuses `mount` if a share or a mapping failed before. fn check_latch(&self, mount: &Mount) -> Result<(), Error> { if self.latched { @@ -562,7 +578,7 @@ fn mapping_mount(mapping: &Mapping) -> Mount { } /// Opens the mount point `path` with `O_PATH`, not through a final symbolic link. -fn open_mount_point(path: &Path) -> Result { +pub(super) fn open_mount_point(path: &Path) -> Result { let flags = OpenFlags::O_PATH | OpenFlags::O_DIRECTORY | OpenFlags::O_NOFOLLOW | OpenFlags::O_CLOEXEC; sys::open(&sys::path_c_string(path)?, flags, Mode::empty()) diff --git a/nvx-rs/src/guest/root/tests.rs b/nvx-rs/src/guest/root/tests.rs index 0d89efd1a..fbfa2c59d 100644 --- a/nvx-rs/src/guest/root/tests.rs +++ b/nvx-rs/src/guest/root/tests.rs @@ -17,6 +17,9 @@ use crate::guest::cgroup::Layout; use crate::guest::mappings::{Access, Mapping, SourceRoot}; use crate::guest::sys::MountAttributes; +mod privileged; +mod runtime; + /// The base address of the distro device's MMIO window. const DISTRO: u64 = 0xd000_0000; /// The base address of the scratch device's MMIO window. diff --git a/nvx-rs/src/guest/root/tests/privileged.rs b/nvx-rs/src/guest/root/tests/privileged.rs new file mode 100644 index 000000000..ec01ea39c --- /dev/null +++ b/nvx-rs/src/guest/root/tests/privileged.rs @@ -0,0 +1,388 @@ +//! Tests of [`Root::mount_runtime`] with the kernel itself, which need the privilege to mount and +//! Linux 6.8 or later, for the unique mount IDs. Each runs in a private mount namespace of its own +//! thread, on a tmpfs root below a temporary directory, and they are ignored unless asked for: +//! `cargo test --all-features -- --ignored guest::root::tests::privileged`, as root. +//! +//! They cover what the fake kernel cannot: the runtime's file systems and their options, the +//! device nodes, with their mode whatever the umask, and the links, the time status's read-only +//! bind, the undo of a call that fails part of the way, a second call's refusal, the teardown's +//! unmounts, and the order of the runtime and the mappings beneath `/run`: a call after a mapping +//! is refused and leaves it reachable, and a mapping after the runtime is on the `/run` tmpfs. + +use std::os::unix::fs::FileTypeExt; + +use super::*; + +/// The options of a mount that the tests check, as the kernel's mount table shows them. +const CHECKED: [&str; 5] = ["ro", "rw", "nosuid", "nodev", "noexec"]; + +/// A root, a tmpfs below a temporary directory, and the directory of a time status beside it, in +/// a private mount namespace of the calling thread. +struct Scene { + /// The temporary directory's canonical path. + base: PathBuf, + /// The temporary directory, which the scene removes once it has detached its mounts. + directory: Option, +} + +impl Scene { + /// Returns a scene, or `None` where the tests cannot run: without root, or without unique + /// mount IDs. + fn new() -> Option { + if !nix::unistd::geteuid().is_root() { + report("skipped: the test needs root"); + return None; + } + sys::enter_private_mount_namespace().unwrap(); + let directory = tempfile::tempdir().unwrap(); + let base = directory.path().canonicalize().unwrap(); + let scene = Self { + base, + directory: Some(directory), + }; + let root = scene.path("root"); + fs::create_dir(&root).unwrap(); + let point = sys::path_c_string(&root).unwrap(); + let options = Some(c"mode=0755"); + sys::mount( + Some(c"tmpfs"), + &point, + Some(c"tmpfs"), + MountFlags::empty(), + options, + ) + .unwrap(); + fs::create_dir(scene.path("time")).unwrap(); + fs::write(scene.path("time/state"), b"synchronized").unwrap(); + let flags = OpenFlags::O_PATH | OpenFlags::O_CLOEXEC; + match sys::file_status(&sys::open(&point, flags, Mode::empty()).unwrap()) { + Err(Errno::EOPNOTSUPP) => { + report("skipped: the kernel has no unique mount IDs"); + None + } + status => { + assert!(status.unwrap().mount_root); + Some(scene) + } + } + } + + /// Returns `relative`'s path below the temporary directory. + fn path(&self, relative: &str) -> PathBuf { + self.base.join(relative) + } + + /// Returns a root of the kernel itself at the scene's tmpfs, which it does not own. + fn root(&self) -> Root { + Root { + kernel: Box::new(SystemKernel), + runtime: self.base.clone(), + path: self.path("root"), + overlay: None, + lowers: Vec::new(), + upper: None, + upper_kind: UpperKind::Tmpfs, + overlay_mode: OverlayMode::TrustedLowers, + first_partition: None, + shares: Vec::new(), + runtime_stray: None, + mapper: None, + mapped: Vec::new(), + latched: false, + reservation: None, + } + } +} + +impl Drop for Scene { + /// Detaches the scene's root, with what is mounted below it, then removes the directory. + fn drop(&mut self) { + let _ = nix::mount::umount2(&self.path("root"), nix::mount::MntFlags::MNT_DETACH); + drop(self.directory.take()); + } +} + +/// Writes `message` to the standard error, past the test harness's capture. +fn report(message: &str) { + use std::io::Write; + + let _ = writeln!(std::io::stderr(), "{message}"); +} + +/// Returns the lines of the thread's mount table, each split into its fields. +fn mount_table() -> Vec> { + let table = fs::read_to_string("/proc/thread-self/mountinfo").unwrap(); + let fields = |line: &str| line.split(' ').map(str::to_owned).collect(); + table.lines().map(fields).collect() +} + +/// Returns the file system type of the topmost mount at `path`, and those of its options that +/// the tests check, sorted, if anything is mounted there. +fn shown(path: &Path) -> Option<(String, Vec)> { + let table = mount_table(); + let fields = table + .iter() + .rev() + .find(|fields| Path::new(&fields[4]) == path)?; + let kind = fields + .iter() + .skip_while(|field| *field != "-") + .nth(1) + .unwrap(); + let mut options: Vec = fields[5] + .split(',') + .filter(|option| CHECKED.contains(option)) + .map(str::to_owned) + .collect(); + options.sort(); + Some((kind.clone(), options)) +} + +/// Returns what [`shown`] returns for `path`, where something must be mounted. +fn mounted(path: &Path) -> (String, Vec) { + shown(path).unwrap_or_else(|| panic!("nothing is mounted at {}", path.display())) +} + +/// Returns the root of the mappings' sources beside the scene's root, `source`, with the +/// directories `app` and `state`, each of which holds a file, `data`. +fn sources(scene: &Scene) -> SourceRoot { + for (name, data) in [("app", "mapped"), ("state", "kept")] { + let directory = scene.path("source").join(name); + fs::create_dir_all(&directory).unwrap(); + fs::write(directory.join("data"), data).unwrap(); + } + SourceRoot::open(&scene.path("source")).unwrap() +} + +/// Returns the mount points below `path`, not `path` itself, in the thread's mount namespace. +fn mounts_below(path: &Path) -> Vec { + let points = mount_table() + .into_iter() + .map(|fields| PathBuf::from(&fields[4])); + points + .filter(|point| point.starts_with(path) && point != path) + .collect() +} + +#[test] +#[ignore = "needs root and Linux 6.8 or later"] +fn the_runtime_s_file_systems_have_an_execution_s_options_nodes_and_links() { + let Some(scene) = Scene::new() else { + return; + }; + let base = scene.path("root"); + let path = |relative: &str| base.join(relative); + let mut root = scene.root(); + // The umask is the thread's alone in its mount namespace. + let umask = nix::sys::stat::umask(Mode::from_bits_truncate(0o077)); + let made = root.mount_runtime(Some(&scene.path("time"))); + nix::sys::stat::umask(umask); + made.unwrap(); + + // The root's own mount is `nosuid` and `nodev` now, and each file system has an execution's + // options. + assert_eq!(mounted(&base).1, ["nodev", "nosuid", "rw"]); + for (relative, kind, options) in [ + ("proc", "proc", &["nodev", "noexec", "nosuid", "rw"][..]), + ("sys", "sysfs", &["nodev", "noexec", "nosuid", "ro"]), + ("dev", "tmpfs", &["nosuid", "rw"]), + ("dev/pts", "devpts", &["noexec", "nosuid", "rw"]), + ("dev/shm", "tmpfs", &["nodev", "nosuid", "rw"]), + ("run", "tmpfs", &["nodev", "nosuid", "rw"]), + ] { + let (found, found_options) = mounted(&path(relative)); + assert_eq!(found, kind, "{relative}"); + assert_eq!(found_options, options, "{relative}"); + } + for (relative, mode) in [ + ("dev", 0o755), + ("dev/shm", 0o1777), + ("run", 0o755), + ("run/nvx", 0o755), + ] { + let found = fs::metadata(path(relative)).unwrap().mode() & 0o7777; + assert_eq!(found, mode, "{relative}"); + } + + // The device nodes have mode 0666, whatever the umask, and open on the `/dev` tmpfs. + for (name, major, minor) in [ + ("null", 1, 3), + ("zero", 1, 5), + ("full", 1, 7), + ("random", 1, 8), + ("urandom", 1, 9), + ("tty", 5, 0), + ] { + let node = fs::symlink_metadata(path(&format!("dev/{name}"))).unwrap(); + assert!(node.file_type().is_char_device(), "{name}"); + assert_eq!(node.mode() & 0o7777, 0o666, "{name}"); + assert_eq!(node.rdev(), nix::sys::stat::makedev(major, minor), "{name}"); + } + fs::write(path("dev/null"), b"discarded").unwrap(); + for (name, target) in [ + ("ptmx", "pts/ptmx"), + ("fd", "/proc/self/fd"), + ("stdin", "/proc/self/fd/0"), + ("stdout", "/proc/self/fd/1"), + ("stderr", "/proc/self/fd/2"), + ] { + let link = fs::read_link(path(&format!("dev/{name}"))).unwrap(); + assert_eq!(link, Path::new(target), "{name}"); + } + + // The time status is bound read-only, `nosuid`, and `nodev`. + let time = path("run/nvx/time"); + assert_eq!(mounted(&time).1, ["nodev", "nosuid", "ro"]); + assert_eq!(fs::read(time.join("state")).unwrap(), b"synchronized"); + let written = fs::write(time.join("written"), b""); + assert_eq!( + written.unwrap_err().raw_os_error(), + Some(Errno::EROFS as i32) + ); + + // The teardown unmounts them all. + root.teardown().unwrap(); + assert_eq!(mounts_below(&base), Vec::::new()); +} + +#[test] +#[ignore = "needs root and Linux 6.8 or later"] +fn a_call_that_fails_part_of_the_way_unmounts_what_it_mounted() { + let Some(scene) = Scene::new() else { + return; + }; + let base = scene.path("root"); + // `/run` is a file, which no mount point may be, so its mount fails after `/dev/shm`'s. + fs::write(base.join("run"), b"").unwrap(); + let mut root = scene.root(); + let error = root.mount_runtime(None).unwrap_err(); + assert!( + matches!( + &error, + Error::MountPoint { path, error } + if *path == base.join("run") + && error.raw_os_error() == Some(Errno::ENOTDIR as i32) + ), + "{error:?}" + ); + assert_eq!(mounts_below(&base), Vec::::new()); + + // Once it is a directory, a call mounts them all, and a second changes nothing: the mounts + // keep their options, and the device nodes still open. + fs::remove_file(base.join("run")).unwrap(); + root.mount_runtime(None).unwrap(); + let mounts = mounts_below(&base); + assert_eq!(mounts.len(), 6, "{mounts:?}"); + let options: Vec<_> = mounts.iter().map(|mount| mounted(mount)).collect(); + let error = root.mount_runtime(None).unwrap_err(); + assert!( + matches!(&error, Error::Occupied { path } if *path == base.join("proc")), + "{error:?}" + ); + assert_eq!(mounts_below(&base), mounts); + let kept: Vec<_> = mounts.iter().map(|mount| mounted(mount)).collect(); + let written = fs::write(base.join("dev/null"), b"discarded").map_err(|error| error.kind()); + assert_eq!((kept, written), (options, Ok(()))); + root.teardown().unwrap(); + assert_eq!(mounts_below(&base), Vec::::new()); +} + +#[test] +#[ignore = "needs root and Linux 6.8 or later"] +fn a_call_after_a_mapping_is_refused_and_leaves_it_reachable() { + let Some(scene) = Scene::new() else { + return; + }; + let base = scene.path("root"); + let app = base.join("run/app"); + let mut root = scene.root(); + let mapping = Mapping::new("app", "/run/app", Access::ReadOnly).unwrap(); + root.map(&sources(&scene), &[mapping]).unwrap(); + // The `/run` tmpfs would hide the mapping, which the teardown could then not unmount. Every + // step runs before the one check, and the scene detaches what a failed one leaves. + let first = root.mount_runtime(Some(&scene.path("time"))); + let read = fs::read(app.join("data")).map_err(|error| error.kind()); + let mounts = mounts_below(&base); + let second = root.mount_runtime(None); + let torn = root.teardown().map_err(|error| error.to_string()); + let refused = Err(format!( + "a file system is mounted at {} already", + app.display() + )); + assert_eq!( + ( + first.map_err(|error| error.to_string()), + read, + mounts, + second.map_err(|error| error.to_string()), + torn, + mounts_below(&base), + ), + ( + refused.clone(), + Ok(b"mapped".to_vec()), + vec![app.clone()], + refused, + Ok(()), + Vec::new(), + ) + ); +} + +#[test] +#[ignore = "needs root and Linux 6.8 or later"] +fn mappings_beneath_run_after_the_runtime_are_on_its_tmpfs_and_go_with_it() { + let Some(scene) = Scene::new() else { + return; + }; + let base = scene.path("root"); + let path = |relative: &str| base.join(relative); + let mut root = scene.root(); + root.mount_runtime(Some(&scene.path("time"))).unwrap(); + let mappings = [ + Mapping::new("app", "/run/app", Access::ReadOnly).unwrap(), + Mapping::new("state", "/run/nvx/state", Access::ReadOnly).unwrap(), + ]; + // Every step runs before the one check, and the scene detaches what a failed one leaves. + let mapped = root.map(&sources(&scene), &mappings); + let options = ["run/app", "run/nvx/state"] + .map(|relative| shown(&path(relative)).map(|(_, options)| options)); + let read = ["run/app/data", "run/nvx/state/data", "run/nvx/time/state"] + .map(|relative| fs::read(path(relative)).map_err(|error| error.kind())); + let mounts = mounts_below(&base); + let second = root.mount_runtime(None); + let kept = mounts_below(&base) == mounts; + let torn = root.teardown().map_err(|error| error.to_string()); + // The mount points that the mapper created went with the `/run` tmpfs. + let created = path("run/app").exists(); + let restricted = Some(["nodev", "nosuid", "ro"].map(str::to_owned).to_vec()); + assert_eq!( + ( + mapped.map_err(|error| error.to_string()), + options, + read, + second.map_err(|error| error.to_string()), + kept, + torn, + mounts_below(&base), + created, + ), + ( + Ok(()), + [restricted.clone(), restricted], + [ + Ok(b"mapped".to_vec()), + Ok(b"kept".to_vec()), + Ok(b"synchronized".to_vec()), + ], + Err(format!( + "a file system is mounted at {} already", + path("proc").display() + )), + true, + Ok(()), + Vec::new(), + false, + ) + ); +} diff --git a/nvx-rs/src/guest/root/tests/runtime.rs b/nvx-rs/src/guest/root/tests/runtime.rs new file mode 100644 index 000000000..4800cc3c0 --- /dev/null +++ b/nvx-rs/src/guest/root/tests/runtime.rs @@ -0,0 +1,675 @@ +//! Tests of [`Root::mount_runtime`] with the fake kernel: the tree's restriction before any +//! mount, the runtime's mounts in their order and with their options, the time status's +//! read-only bind or none, the mount points that it creates or refuses, the undo of a call that +//! fails at any of its steps, the teardown's order, the refusal of a call after a share or a +//! mapping, and the shares and mappings beneath `/run` after the runtime. + +use super::*; + +/// `MOUNT_ATTR_RDONLY`. +const RDONLY: u64 = 0x1; +/// `MOUNT_ATTR_NOSUID`. +const NOSUID: u64 = 0x2; +/// `MOUNT_ATTR_NODEV`. +const NODEV: u64 = 0x4; +/// `MOUNT_ATTR_NOEXEC`. +const NOEXEC: u64 = 0x8; + +/// The runtime's mounts, in the order of their mounts. +const RUNTIME: [RuntimeMounts; 7] = [ + RuntimeMounts::Proc, + RuntimeMounts::Sys, + RuntimeMounts::Dev, + RuntimeMounts::DevPts, + RuntimeMounts::DevShm, + RuntimeMounts::Run, + RuntimeMounts::TimeStatus, +]; + +/// Returns a guest, with its root assembled, and the directory of a time status, `time`, beside +/// the runtime directory. +fn assembled() -> (Guest, Root) { + let guest = Guest::new(); + let root = guest.assemble(&guest.assembly()).unwrap(); + fs::create_dir(guest.path("time")).unwrap(); + fs::write(guest.path("time/state"), b"synchronized").unwrap(); + (guest, root) +} + +/// Returns the path of `mount`'s target in the guest's root. +fn point(guest: &Guest, mount: RuntimeMounts) -> PathBuf { + guest.path("run/nvx/rootfs").join(&mount.target()[1..]) +} + +/// Returns the mount points of `mounts` in the guest's root, in reverse order, as the fake +/// kernel records their unmounts. +fn reversed(guest: &Guest, mounts: &[RuntimeMounts]) -> Vec { + let text = |mount: &RuntimeMounts| point(guest, *mount).display().to_string(); + mounts.iter().rev().map(text).collect() +} + +/// Returns the guest's unmounts of the assembly's mounts at a teardown, after those of `first`. +fn then_the_assembly(guest: &Guest, mut first: Vec) -> Vec { + for assembly in ["rootfs", "layers/distro", "scratch"] { + first.push(guest.text(&format!("run/nvx/{assembly}"))); + } + first +} + +/// Returns the refusal of a call to [`Root::mount_runtime`] for what is mounted at `path`. +fn occupied(path: &Path) -> Result<(), String> { + Err(format!( + "a file system is mounted at {} already", + path.display() + )) +} + +/// Returns the root of the mappings' sources beside the runtime directory, `hostfs/root`, a mount +/// of its own, with the directory `0/src`. +fn source(guest: &Guest) -> SourceRoot { + let export = guest.path("hostfs/root"); + fs::create_dir_all(export.join("0/src")).unwrap(); + guest.kernel.add_mount(&export); + SourceRoot::open(&export).unwrap() +} + +/// Returns a recorded new mount of `kind` with `parameters` and `attributes`. +fn new_mount(kind: &str, parameters: &[&str], attributes: u64) -> Call { + Call::NewMount { + kind: kind.to_owned(), + parameters: parameters + .iter() + .map(|¶meter| parameter.to_owned()) + .collect(), + attributes, + } +} + +/// Returns a recorded move of a new mount of `kind` onto `mount`'s target in the guest's root. +fn moved(guest: &Guest, kind: &str, mount: RuntimeMounts) -> Call { + Call::MoveMount { + source: kind.to_owned(), + target: point(guest, mount).display().to_string(), + } +} + +#[test] +fn the_runtime_s_file_systems_are_mounted_in_order_once_the_tree_is_restricted() { + let (guest, mut root) = assembled(); + let assembled = guest.kernel.calls().len(); + root.mount_runtime(Some(&guest.path("time"))).unwrap(); + // The tree is `nosuid` and `nodev` before the first mount, and each file system takes an + // execution's options. + let mut expected = vec![ + Call::RestrictTree(guest.text("run/nvx/rootfs")), + new_mount("proc", &["source=proc"], NOSUID | NODEV | NOEXEC), + moved(&guest, "proc", RuntimeMounts::Proc), + new_mount("sysfs", &["source=sysfs"], RDONLY | NOSUID | NODEV | NOEXEC), + moved(&guest, "sysfs", RuntimeMounts::Sys), + new_mount("tmpfs", &["source=tmpfs", "mode=0755"], NOSUID), + moved(&guest, "tmpfs", RuntimeMounts::Dev), + Call::CreateDevices(guest.text("run/nvx/rootfs/dev")), + new_mount( + "devpts", + &["source=devpts", "ptmxmode=0666", "mode=0620"], + NOSUID | NOEXEC, + ), + moved(&guest, "devpts", RuntimeMounts::DevPts), + new_mount("tmpfs", &["source=tmpfs", "mode=1777"], NOSUID | NODEV), + moved(&guest, "tmpfs", RuntimeMounts::DevShm), + new_mount("tmpfs", &["source=tmpfs", "mode=0755"], NOSUID | NODEV), + moved(&guest, "tmpfs", RuntimeMounts::Run), + ]; + // The time status is bound read-only, `nosuid`, and `nodev`, on the `/run` tmpfs. + expected.extend(attach_calls( + &guest.path("time"), + &point(&guest, RuntimeMounts::TimeStatus), + Access::ReadOnly, + )); + assert_eq!(guest.kernel.calls()[assembled..], expected); + for mount in RUNTIME { + let path = point(&guest, mount); + assert!(guest.kernel.mount_at(&path).is_some(), "{mount:?}"); + } + // The missing mount points are created, `/run/nvx` and `/run/nvx/time` with mode 0755. + for relative in ["run/nvx", "run/nvx/time"] { + let path = guest.path("run/nvx/rootfs").join(relative); + let mode = fs::metadata(path).unwrap().mode() & 0o7777; + assert_eq!(mode, 0o755, "{relative}"); + } + + // The teardown unmounts them, the later first, before the assembly's mounts. + root.teardown().unwrap(); + let mut unmounts = reversed(&guest, &RUNTIME); + for assembly in ["rootfs", "layers/distro", "scratch"] { + unmounts.push(guest.text(&format!("run/nvx/{assembly}"))); + } + assert_eq!(guest.kernel.unmounts(), unmounts); +} + +#[test] +fn without_a_time_status_nothing_is_bound_at_run_nvx_time() { + let (guest, mut root) = assembled(); + root.mount_runtime(None).unwrap(); + let calls = guest.kernel.calls(); + assert!( + !calls + .iter() + .any(|call| matches!(call, Call::OpenTree { .. })), + "{calls:?}" + ); + let time = point(&guest, RuntimeMounts::TimeStatus); + assert_eq!(guest.kernel.mount_at(&time), None); + assert!(!guest.path("run/nvx/rootfs/run/nvx").exists()); + root.teardown().unwrap(); + assert_eq!( + guest.kernel.unmounts()[..6], + reversed(&guest, &RUNTIME[..6]) + ); +} + +#[test] +fn a_failed_mount_unmounts_what_the_call_mounted() { + for (index, failing) in RUNTIME.into_iter().enumerate() { + let (guest, mut root) = assembled(); + let target = point(&guest, failing); + guest + .kernel + .fail(Operation::MoveMount, target, Errno::EPERM); + let unmounted = guest.kernel.unmounts().len(); + let error = root.mount_runtime(Some(&guest.path("time"))).unwrap_err(); + assert!( + matches!( + &error, + Error::Mount { mount: Mount::Runtime(mount), error } + if *mount == failing && error.raw_os_error() == Some(Errno::EPERM as i32) + ), + "{failing:?}: {error:?}" + ); + assert_eq!( + guest.kernel.unmounts()[unmounted..], + reversed(&guest, &RUNTIME[..index]), + "{failing:?}" + ); + for mount in RUNTIME { + let path = point(&guest, mount); + assert_eq!(guest.kernel.mount_at(&path), None, "{mount:?}"); + } + // The teardown has only the assembly's mounts left. + root.teardown().unwrap(); + assert_eq!(guest.kernel.unmounts().len(), unmounted + index + 3); + } +} + +#[test] +fn a_failed_restriction_mounts_nothing_and_other_failed_steps_undo_the_call() { + let (guest, mut root) = assembled(); + let rootfs = guest.path("run/nvx/rootfs"); + guest + .kernel + .fail(Operation::SetAttributes, rootfs, Errno::EPERM); + let calls = guest.kernel.calls().len(); + let error = root.mount_runtime(None).unwrap_err(); + assert_eq!( + error.to_string(), + "failed to make the root's mounts nosuid and nodev: Operation not permitted (os error 1)" + ); + assert_eq!( + guest.kernel.calls()[calls..], + [Call::RestrictTree(guest.text("run/nvx/rootfs"))] + ); + + // The device nodes, once `/dev` is mounted at the image's own directory. + let (guest, mut root) = assembled(); + let dev = point(&guest, RuntimeMounts::Dev); + fs::create_dir(&dev).unwrap(); + guest.kernel.fail(Operation::Create, &dev, Errno::EIO); + let unmounted = guest.kernel.unmounts().len(); + let error = root.mount_runtime(None).unwrap_err(); + assert!( + matches!( + &error, + Error::Mount { mount: Mount::Runtime(RuntimeMounts::Dev), error } + if error.raw_os_error() == Some(Errno::EIO as i32) + ), + "{error:?}" + ); + assert!( + error + .to_string() + .starts_with("failed to mount the runtime's /dev: ") + ); + let undone = reversed(&guest, &RUNTIME[..3]); + assert_eq!(guest.kernel.unmounts()[unmounted..], undone); + + // A new mount, devpts's. + let (guest, mut root) = assembled(); + guest + .kernel + .fail(Operation::NewMount, "devpts", Errno::ENODEV); + let unmounted = guest.kernel.unmounts().len(); + let error = root.mount_runtime(None).unwrap_err(); + assert!( + matches!( + &error, + Error::Mount { + mount: Mount::Runtime(RuntimeMounts::DevPts), + .. + } + ), + "{error:?}" + ); + let undone = reversed(&guest, &RUNTIME[..3]); + assert_eq!(guest.kernel.unmounts()[unmounted..], undone); +} + +#[test] +fn a_mount_point_through_a_symbolic_link_or_with_a_mount_already_is_refused() { + // A link at `/run` is refused once the mounts before it are made, which the call undoes. + let (guest, mut root) = assembled(); + let run = point(&guest, RuntimeMounts::Run); + fs::create_dir(guest.path("elsewhere")).unwrap(); + std::os::unix::fs::symlink(guest.path("elsewhere"), &run).unwrap(); + let unmounted = guest.kernel.unmounts().len(); + let error = root.mount_runtime(None).unwrap_err(); + assert!( + matches!( + &error, + Error::MountPoint { path, error } + if *path == run && error.raw_os_error() == Some(Errno::ELOOP as i32) + ), + "{error:?}" + ); + let undone = reversed(&guest, &RUNTIME[..5]); + assert_eq!(guest.kernel.unmounts()[unmounted..], undone); + assert_eq!(fs::read_dir(guest.path("elsewhere")).unwrap().count(), 0); + + // A second call finds the runtime's mounts, and makes no call: it restricts no tree again. + let (guest, mut root) = assembled(); + root.mount_runtime(None).unwrap(); + let calls = guest.kernel.calls().len(); + let error = root.mount_runtime(None).unwrap_err(); + let proc = point(&guest, RuntimeMounts::Proc); + assert!( + matches!(&error, Error::Occupied { path } if *path == proc), + "{error:?}" + ); + assert_eq!(guest.kernel.calls().len(), calls); + root.teardown().unwrap(); + assert_eq!(guest.kernel.unmounts().len(), 6 + 3); +} + +#[test] +fn a_mount_that_the_undo_fails_to_unmount_stays_for_the_teardown() { + let (guest, mut root) = assembled(); + let pts = point(&guest, RuntimeMounts::DevPts); + let run = point(&guest, RuntimeMounts::Run); + guest.kernel.fail(Operation::MoveMount, run, Errno::EPERM); + guest.kernel.fail(Operation::Unmount, &pts, Errno::EBUSY); + let unmounted = guest.kernel.unmounts().len(); + root.mount_runtime(None).unwrap_err(); + let undone = reversed(&guest, &RUNTIME[..5]); + assert_eq!(guest.kernel.unmounts()[unmounted..], undone); + assert!(guest.kernel.mount_at(&pts).is_some()); + + // A later call finds it, and makes no call. + let calls = guest.kernel.calls().len(); + let error = root.mount_runtime(None).unwrap_err(); + assert!( + matches!(&error, Error::Occupied { path } if *path == pts), + "{error:?}" + ); + assert_eq!(guest.kernel.calls().len(), calls); + + // The teardown unmounts it first. + guest.kernel.restore(Operation::Unmount, &pts); + let unmounted = guest.kernel.unmounts().len(); + root.teardown().unwrap(); + assert_eq!( + guest.kernel.unmounts()[unmounted..], + [ + pts.display().to_string(), + guest.text("run/nvx/rootfs"), + guest.text("run/nvx/layers/distro"), + guest.text("run/nvx/scratch"), + ] + ); +} + +#[test] +fn a_runtime_mount_that_moves_is_detached_before_it_is_recorded() { + for runtime in &RUNTIME[..6] { + let (guest, mut root) = assembled(); + let target = point(&guest, *runtime); + let moved = target.with_file_name("moved"); + let renamed = moved.clone(); + guest.kernel.on_move(move |path| { + if path == target { + fs::rename(&target, &renamed).unwrap(); + fs::create_dir(&target).unwrap(); + } + }); + + let mounted = root.mount_runtime(None); + let refused = matches!( + &mounted, + Err(Error::Mount { + mount: Mount::Runtime(found), + .. + }) if found == runtime + ); + let after_call = guest.kernel.mount_at(&moved); + let torn = root.teardown().map_err(|error| error.to_string()); + let after_teardown = guest.kernel.mount_at(&moved); + assert_eq!( + (refused, after_call, torn.is_ok(), after_teardown), + (true, None, true, None), + "{runtime:?}: {mounted:?}, {torn:?}" + ); + } +} + +#[test] +fn a_runtime_mount_whose_detach_fails_is_owned_until_teardown() { + for runtime in [ + RuntimeMounts::Dev, + RuntimeMounts::DevPts, + RuntimeMounts::Run, + ] { + for persistent in [false, true] { + let (guest, mut root) = assembled(); + let target = point(&guest, runtime); + let original = target.clone(); + let moved = target.with_file_name("moved"); + let renamed = moved.clone(); + guest.kernel.fail(Operation::Detach, &moved, Errno::EBUSY); + guest.kernel.on_move(move |path| { + if path == target { + fs::rename(&target, &renamed).unwrap(); + fs::create_dir(&target).unwrap(); + } + }); + + let error = root.mount_runtime(None).unwrap_err(); + assert!( + matches!( + &error, + Error::Mount { + mount: Mount::Runtime(found), + .. + } if *found == runtime + ), + "{runtime:?}: {error:?}" + ); + assert!(guest.kernel.mount_at(&moved).is_some()); + let calls = guest.kernel.calls().len(); + let error = root.mount_runtime(None).unwrap_err(); + assert!( + matches!(&error, Error::Occupied { path } if *path == original), + "{runtime:?}: {error:?}" + ); + assert_eq!(guest.kernel.calls().len(), calls); + if !persistent { + guest.kernel.restore(Operation::Detach, &moved); + } + let torn = root.teardown(); + if persistent { + let error = torn.unwrap_err(); + assert!( + error.failures().iter().any(|failure| { + failure.mount() == &Mount::Runtime(runtime) + && failure.path() == original + && failure.error().raw_os_error() == Some(Errno::EBUSY as i32) + }), + "{runtime:?}: {error:?}" + ); + } else { + torn.unwrap(); + } + assert_eq!(guest.kernel.mount_at(&moved).is_some(), persistent); + let detaches = guest + .kernel + .calls() + .into_iter() + .filter(|call| matches!(call, Call::Detach(_))) + .count(); + assert_eq!(detaches, 3, "{runtime:?}"); + } + } +} + +#[test] +fn a_time_status_bind_that_moves_and_cannot_detach_stays_owned_until_teardown() { + let (guest, mut root) = assembled(); + let time = point(&guest, RuntimeMounts::TimeStatus); + let parent = time.parent().unwrap().to_path_buf(); + let moved_parent = guest.path("run/nvx/rootfs/run/moved"); + let moved = moved_parent.join("time"); + guest.kernel.fail(Operation::Detach, &moved, Errno::EBUSY); + guest.kernel.on_move(move |target| { + if target == time { + fs::rename(&parent, &moved_parent).unwrap(); + } + }); + + let error = root.mount_runtime(Some(&guest.path("time"))).unwrap_err(); + assert!( + matches!( + &error, + Error::Mount { + mount: Mount::Runtime(RuntimeMounts::TimeStatus), + .. + } + ), + "{error:?}" + ); + assert!(guest.kernel.mount_at(&moved).is_some()); + let calls = guest.kernel.calls().len(); + assert!(matches!( + root.mount_runtime(None), + Err(Error::Occupied { .. }) + )); + assert_eq!(guest.kernel.calls().len(), calls); + guest.kernel.restore(Operation::Detach, &moved); + root.teardown().unwrap(); + assert_eq!(guest.kernel.mount_at(&moved), None); + let detaches = guest + .kernel + .calls() + .into_iter() + .filter(|call| matches!(call, Call::Detach(_))) + .count(); + assert_eq!(detaches, 3); +} + +#[test] +fn a_time_status_bind_whose_lookup_and_detach_fail_reports_its_teardown_failure() { + let (guest, mut root) = assembled(); + let time = point(&guest, RuntimeMounts::TimeStatus); + guest.kernel.fail(Operation::Detach, &time, Errno::EBUSY); + let kernel = guest.kernel.clone(); + let target = time.clone(); + guest.kernel.on_move(move |path| { + if path == target { + kernel.fail(Operation::Status, &target, Errno::EIO); + } + }); + + root.mount_runtime(Some(&guest.path("time"))).unwrap_err(); + let error = root.teardown().unwrap_err(); + assert!( + error.failures().iter().any(|failure| { + failure.mount() == &Mount::Runtime(RuntimeMounts::TimeStatus) + && failure.path() == time + && failure.error().raw_os_error() == Some(Errno::EBUSY as i32) + }), + "{error:?}" + ); + assert!(guest.kernel.mount_at(&time).is_some()); +} + +#[test] +fn a_call_after_a_share_a_child_or_a_mapping_is_refused_before_it_changes_anything() { + let rw = Access::ReadWrite; + // The `/run` tmpfs would hide one beneath `/run` from the teardown, and the runtime comes + // first whatever the target. + for (kind, target) in [ + ("share", "/run/app"), + ("child", "/run/app"), + ("mapping", "/srv"), + ] { + let (guest, mut root) = assembled(); + let path = guest.path("run/nvx/rootfs").join(&target[1..]); + let mut unmounts = vec![path.display().to_string()]; + match kind { + "share" => root.mount_share("app", Path::new(target), rw).unwrap(), + "child" => { + export(&guest, &[("app", ShareKind::Directory)]); + let children = [child("app", target, rw, ShareKind::Directory)]; + root.mount_aggregate("all", rw, &children).unwrap(); + unmounts.push(guest.text("run/nvx/shares")); + } + _ => { + let mapping = Mapping::new("0/src", target, rw).unwrap(); + root.map(&source(&guest), &[mapping]).unwrap(); + } + } + let calls = guest.kernel.calls().len(); + let unmounted = guest.kernel.unmounts().len(); + let first = root.mount_runtime(Some(&guest.path("time"))); + let second = root.mount_runtime(None); + let made = guest.kernel.calls().len() - calls; + // What the call was refused for is still the topmost mount at its target. + let shown = guest.kernel.mount_at(&path).is_some(); + let torn = root.teardown().map_err(|error| error.to_string()); + let observed = ( + first.map_err(|error| error.to_string()), + second.map_err(|error| error.to_string()), + made, + shown, + torn, + guest.kernel.unmounts()[unmounted..].to_vec(), + ); + let expected = ( + occupied(&path), + occupied(&path), + 0, + true, + Ok(()), + then_the_assembly(&guest, unmounts), + ); + assert_eq!(observed, expected, "{kind}"); + } +} + +#[test] +fn shares_and_mappings_beneath_run_after_the_runtime_are_on_its_tmpfs_and_unmounted_first() { + let (guest, mut root) = assembled(); + root.mount_runtime(Some(&guest.path("time"))).unwrap(); + let unmounted = guest.kernel.unmounts().len(); + // The mapper creates what the targets lack on the `/run` tmpfs, one of its own mounts. + let shared = root.mount_share("app", Path::new("/run/app"), Access::ReadWrite); + let mapping = Mapping::new("0/src", "/run/nvx/state", Access::ReadOnly).unwrap(); + let mapped = root.map(&source(&guest), &[mapping]); + let tracked = targets(&root); + // A second call is still refused for the runtime's own mounts first. + let again = root.mount_runtime(None); + let torn = root.teardown().map_err(|error| error.to_string()); + let observed = ( + shared.map_err(|error| error.to_string()), + mapped.map_err(|error| error.to_string()), + tracked, + again.map_err(|error| error.to_string()), + torn, + guest.kernel.unmounts()[unmounted..].to_vec(), + ); + // The teardown unmounts the mapper's mounts, the later first, before the runtime's. + let mut unmounts = vec![ + guest.text("run/nvx/rootfs/run/nvx/state"), + guest.text("run/nvx/rootfs/run/app"), + ]; + unmounts.extend(reversed(&guest, &RUNTIME)); + let expected = ( + Ok(()), + Ok(()), + vec!["/run/app".to_owned(), "/run/nvx/state".to_owned()], + occupied(&point(&guest, RuntimeMounts::Proc)), + Ok(()), + then_the_assembly(&guest, unmounts), + ); + assert_eq!(observed, expected); +} + +#[test] +fn a_run_that_the_mapper_fails_to_add_latches_the_root_and_one_that_an_undo_left_is_its_own() { + // A `/run` whose status fails stops the mapper's opening: nothing is mounted for the share, + // and the root is latched. + let (guest, mut root) = assembled(); + root.mount_runtime(None).unwrap(); + let run = point(&guest, RuntimeMounts::Run); + guest.kernel.fail(Operation::Status, &run, Errno::EIO); + let calls = guest.kernel.calls().len(); + let unmounted = guest.kernel.unmounts().len(); + let error = root + .mount_share("app", Path::new("/run/app"), Access::ReadWrite) + .unwrap_err(); + let later = root + .mount_share("t", Path::new("/srv"), Access::ReadWrite) + .unwrap_err(); + let observed = ( + matches!(&error, Error::MountPoint { path, error } + if *path == run && error.raw_os_error() == Some(Errno::EIO as i32)), + matches!( + later, + Error::Mapping { + error: MappingError::Latched, + .. + } + ), + root.mapper.is_some(), + guest.kernel.calls().len() - calls, + ); + assert_eq!(observed, (true, true, false, 0), "{error:?}"); + root.teardown().unwrap(); + assert_eq!( + guest.kernel.unmounts()[unmounted..], + then_the_assembly(&guest, reversed(&guest, &RUNTIME[..6])) + ); + + // A `/run` that a failed undo left is the mapper's: a share beneath it is mounted there, and + // the teardown unmounts the share first. + let (guest, mut root) = assembled(); + let run = point(&guest, RuntimeMounts::Run); + let time = point(&guest, RuntimeMounts::TimeStatus); + guest.kernel.fail(Operation::MoveMount, &time, Errno::EPERM); + guest.kernel.fail(Operation::Unmount, &run, Errno::EBUSY); + root.mount_runtime(Some(&guest.path("time"))).unwrap_err(); + assert!(guest.kernel.mount_at(&run).is_some()); + guest.kernel.restore(Operation::Unmount, &run); + let unmounted = guest.kernel.unmounts().len(); + let shared = root.mount_share("app", Path::new("/run/app"), Access::ReadWrite); + // A later call is refused for that `/run`, the runtime's mount, and makes no call. + let calls = guest.kernel.calls().len(); + let again = root.mount_runtime(None); + let made = guest.kernel.calls().len() - calls; + let torn = root.teardown().map_err(|error| error.to_string()); + let observed = ( + shared.map_err(|error| error.to_string()), + again.map_err(|error| error.to_string()), + made, + torn, + guest.kernel.unmounts()[unmounted..].to_vec(), + ); + let unmounts = vec![ + guest.text("run/nvx/rootfs/run/app"), + run.display().to_string(), + ]; + let expected = ( + Ok(()), + occupied(&run), + 0, + Ok(()), + then_the_assembly(&guest, unmounts), + ); + assert_eq!(observed, expected); +} diff --git a/nvx-rs/src/guest/sys/entry.rs b/nvx-rs/src/guest/sys/entry.rs index eaad456bd..d27cfef54 100644 --- a/nvx-rs/src/guest/sys/entry.rs +++ b/nvx-rs/src/guest/sys/entry.rs @@ -61,6 +61,9 @@ const MOUNT_ATTR_NOEXEC: u64 = 0x8; /// The attributes of every read-only bind of an entry. const READ_ONLY_BIND: u64 = MOUNT_ATTR_RDONLY | MOUNT_ATTR_NOSUID | MOUNT_ATTR_NODEV | MOUNT_ATTR_NOEXEC; +/// The attributes that every mount of an entry's root takes, as every mount of a layered root's +/// tree does before its runtime file systems: `nosuid` and `nodev`. +const TREE_ATTRIBUTES: u64 = MOUNT_ATTR_NOSUID | MOUNT_ATTR_NODEV; /// `openat2`'s resolution of an entry's paths: below the starting directory, through no symbolic /// link or magic link, and on the starting directory's mount. const BENEATH: u64 = libc::RESOLVE_BENEATH @@ -92,6 +95,69 @@ const DEVICE_LINKS: [(&CStr, &CStr); 5] = [ (c"stdout", c"/proc/self/fd/1"), (c"stderr", c"/proc/self/fd/2"), ]; + +/// A file system that an entry mounts in its root, as a layered root's +/// [`mount_runtime`](crate::guest::root::Root::mount_runtime) does in its own: its type, its +/// string parameters, and its mount's attributes. +#[derive(Debug)] +pub(crate) struct FileSystem { + /// The type, which `fsopen` takes. + pub(crate) kind: &'static CStr, + /// The parameters, which `fsconfig` sets. + pub(crate) parameters: &'static [(&'static CStr, &'static CStr)], + /// The mount's attributes, `MOUNT_ATTR_*`, which `fsmount` sets. + pub(crate) attributes: u64, +} + +/// procfs at `/proc`: `nosuid`, `nodev`, and `noexec`. +pub(crate) const PROC: FileSystem = FileSystem { + kind: c"proc", + parameters: &[(c"source", c"proc")], + attributes: MOUNT_ATTR_NOSUID | MOUNT_ATTR_NODEV | MOUNT_ATTR_NOEXEC, +}; +/// sysfs at `/sys`: read-only, `nosuid`, `nodev`, and `noexec`. +pub(crate) const SYS: FileSystem = FileSystem { + kind: c"sysfs", + parameters: &[(c"source", c"sysfs")], + attributes: READ_ONLY_BIND, +}; +/// The tmpfs at `/dev`: `nosuid`, with mode 0755. +pub(crate) const DEV: FileSystem = FileSystem { + kind: c"tmpfs", + parameters: &[(c"source", c"tmpfs"), (c"mode", c"0755")], + attributes: MOUNT_ATTR_NOSUID, +}; +/// devpts at `/dev/pts`, an instance of its own: `nosuid` and `noexec`, with mode 0666 for +/// `ptmx` and 0620 for the terminals. +pub(crate) const DEV_PTS: FileSystem = FileSystem { + kind: c"devpts", + parameters: &[ + (c"source", c"devpts"), + (c"ptmxmode", c"0666"), + (c"mode", c"0620"), + ], + attributes: MOUNT_ATTR_NOSUID | MOUNT_ATTR_NOEXEC, +}; +/// The tmpfs at `/dev/shm`: `nosuid` and `nodev`, with mode 1777. +pub(crate) const DEV_SHM: FileSystem = FileSystem { + kind: c"tmpfs", + parameters: &[(c"source", c"tmpfs"), (c"mode", c"1777")], + attributes: MOUNT_ATTR_NOSUID | MOUNT_ATTR_NODEV, +}; +/// The tmpfs at `/run`, which a layered root mounts, and an entry does not: `nosuid` and +/// `nodev`, with mode 0755. +pub(crate) const RUN: FileSystem = FileSystem { + kind: c"tmpfs", + parameters: &[(c"source", c"tmpfs"), (c"mode", c"0755")], + attributes: MOUNT_ATTR_NOSUID | MOUNT_ATTR_NODEV, +}; +/// The tmpfs of the source of the masks of an execution as user ID 0: `nosuid`, `nodev`, and +/// `noexec`, with mode 0555. +const MASK_SOURCE: FileSystem = FileSystem { + kind: c"tmpfs", + parameters: &[(c"source", c"tmpfs"), (c"mode", c"0555")], + attributes: MOUNT_ATTR_NOSUID | MOUNT_ATTR_NODEV | MOUNT_ATTR_NOEXEC, +}; /// The procfs entries that an execution as user ID 0 sees read-only. const PROC_READ_ONLY: [&CStr; 5] = [c"sys", c"sysrq-trigger", c"bus", c"fs", c"irq"]; /// The procfs entries that an execution as user ID 0 sees masked: as an empty, read-only file or @@ -409,12 +475,7 @@ fn set_hostname(hostname: &[u8]) -> Result<(), Errno> { /// top of the former root. fn move_root(root: &CStr) -> Result<(), Errno> { nix::unistd::chdir(root)?; - mount_setattr( - libc::AT_FDCWD, - c".", - AT_RECURSIVE, - MOUNT_ATTR_NOSUID | MOUNT_ATTR_NODEV, - )?; + mount_setattr(libc::AT_FDCWD, c".", AT_RECURSIVE, TREE_ATTRIBUTES)?; nix::mount::mount( Some(c"."), c"/", @@ -445,11 +506,7 @@ fn set_umask(mask: libc::mode_t) -> Result { /// `noexec`, and returns its mount's descriptor. fn mount_proc() -> Result { let target = directory_target(libc::AT_FDCWD, c"proc")?; - let proc = new_mount( - c"proc", - &[(c"source", c"proc")], - MOUNT_ATTR_NOSUID | MOUNT_ATTR_NODEV | MOUNT_ATTR_NOEXEC, - )?; + let proc = new_mount(&PROC)?; move_mount(proc, target)?; Ok(proc) } @@ -480,7 +537,7 @@ fn restrict_proc(proc: RawFd, source: RawFd) -> Result<(), Errno> { /// mount's descriptor. fn mount_sys() -> Result { let target = directory_target(libc::AT_FDCWD, c"sys")?; - let sys = new_mount(c"sysfs", &[(c"source", c"sysfs")], READ_ONLY_BIND)?; + let sys = new_mount(&SYS)?; move_mount(sys, target)?; Ok(sys) } @@ -489,11 +546,7 @@ fn mount_sys() -> Result { /// and `noexec`, that holds one empty file and one empty directory, and that is read-only once /// it holds them. It returns the tmpfs's mount descriptor, of which each mask binds one entry. fn mask_source() -> Result { - let source = new_mount( - c"tmpfs", - &[(c"source", c"tmpfs"), (c"mode", c"0555")], - MOUNT_ATTR_NOSUID | MOUNT_ATTR_NODEV | MOUNT_ATTR_NOEXEC, - )?; + let source = new_mount(&MASK_SOURCE)?; let file = descriptor( // SAFETY: the path is a NUL-terminated string, and the mode an integer. unsafe { @@ -538,12 +591,18 @@ fn mask_entry(directory: RawFd, name: &CStr, source: RawFd) -> Result<(), Errno> /// `nodev`, and with mode 1777. fn mount_devices(devices: u8) -> Result<(), Errno> { let target = directory_target(libc::AT_FDCWD, c"dev")?; - let dev = new_mount( - c"tmpfs", - &[(c"source", c"tmpfs"), (c"mode", c"0755")], - MOUNT_ATTR_NOSUID, - )?; + let dev = new_mount(&DEV)?; move_mount(dev, target)?; + populate_devices(dev, devices)?; + let pts = new_mount(&DEV_PTS)?; + move_mount(pts, directory_target(dev, c"pts")?)?; + let shm = new_mount(&DEV_SHM)?; + move_mount(shm, directory_target(dev, c"shm")?) +} + +/// Creates, in the directory `dev`, the device nodes of [`DEVICE_NODES`] that `devices` selects, +/// with mode 0666 but for what the umask withholds, and the symbolic links of [`DEVICE_LINKS`]. +fn populate_devices(dev: RawFd, devices: u8) -> Result<(), Errno> { for (bit, (name, major, minor)) in DEVICE_NODES.iter().enumerate() { if devices & (1 << bit) == 0 { continue; @@ -565,22 +624,35 @@ fn mount_devices(devices: u8) -> Result<(), Errno> { // SAFETY: both strings are NUL-terminated. unit(unsafe { libc::symlinkat(link.as_ptr(), dev, name.as_ptr()) }.into())?; } - let pts = new_mount( - c"devpts", - &[ - (c"source", c"devpts"), - (c"ptmxmode", c"0666"), - (c"mode", c"0620"), - ], - MOUNT_ATTR_NOSUID | MOUNT_ATTR_NOEXEC, - )?; - move_mount(pts, directory_target(dev, c"pts")?)?; - let shm = new_mount( - c"tmpfs", - &[(c"source", c"tmpfs"), (c"mode", c"1777")], - MOUNT_ATTR_NOSUID | MOUNT_ATTR_NODEV, - )?; - move_mount(shm, directory_target(dev, c"shm")?) + Ok(()) +} + +/// Mounts a new instance of `file_system`, detached, and returns its mount's descriptor, which a +/// layered root owns, unlike an entry's child. +pub(crate) fn mount_detached(file_system: &FileSystem) -> Result { + let mount = new_mount(file_system)?; + // SAFETY: `fsmount` returned a new descriptor that nothing else owns. + Ok(unsafe { FileDescriptor::from_raw(mount) }) +} + +/// Sets `nosuid` and `nodev` on the mount whose root `directory` refers to, and on every mount +/// below it, as an entry does on its root. +pub(crate) fn restrict_tree(directory: &FileDescriptor) -> Result<(), Errno> { + let flags = AT_RECURSIVE | libc::AT_EMPTY_PATH as c_uint; + mount_setattr(directory.raw(), c"", flags, TREE_ATTRIBUTES) +} + +/// Creates in the directory `dev` every device node of [`DEVICE_NODES`], with mode 0666 whatever +/// the umask, which an entry's child clears and a layered root's caller keeps, and the symbolic +/// links of [`DEVICE_LINKS`]. +pub(crate) fn create_devices(dev: &FileDescriptor) -> Result<(), Errno> { + populate_devices(dev.raw(), u8::MAX)?; + let mode = nix::sys::stat::Mode::from_bits_truncate(DEVICE_MODE); + let follow = nix::sys::stat::FchmodatFlags::FollowSymlink; + for (name, _, _) in DEVICE_NODES { + nix::sys::stat::fchmodat(dev.borrowed(), name, mode, follow)?; + } + Ok(()) } /// Drops from the bounding set every capability that `capabilities` does not hold, up to the @@ -682,13 +754,14 @@ fn file_kind(descriptor: RawFd) -> Result { Ok(unsafe { status.assume_init() }.st_mode & libc::S_IFMT) } -/// Creates the file system `kind` with the string parameters `parameters`, mounts it detached -/// with the mount attributes `attributes`, and returns the mount's descriptor. -fn new_mount(kind: &CStr, parameters: &[(&CStr, &CStr)], attributes: u64) -> Result { +/// Creates the file system `file_system` with its string parameters, mounts it detached with its +/// mount attributes, and returns the mount's descriptor. +fn new_mount(file_system: &FileSystem) -> Result { // SAFETY: the name is a NUL-terminated string. - let context = - descriptor(unsafe { libc::syscall(libc::SYS_fsopen, kind.as_ptr(), FSOPEN_CLOEXEC) })?; - let mount = configure_and_mount(context, parameters, attributes); + let context = descriptor(unsafe { + libc::syscall(libc::SYS_fsopen, file_system.kind.as_ptr(), FSOPEN_CLOEXEC) + })?; + let mount = configure_and_mount(context, file_system.parameters, file_system.attributes); close(context); mount } diff --git a/nvx-rs/src/guest/sys/mod.rs b/nvx-rs/src/guest/sys/mod.rs index 013db3ab0..439cab0c3 100644 --- a/nvx-rs/src/guest/sys/mod.rs +++ b/nvx-rs/src/guest/sys/mod.rs @@ -89,7 +89,10 @@ pub(crate) use clock::{Clock, clock_time, monotonic_ms, sleep_once, sleep_until_ #[cfg(target_arch = "x86_64")] pub(crate) use clock::{monotonic_ns, syscall_monotonic_nanoseconds}; pub(crate) use discipline::{ClockAdjustment, ClockDiscipline, FrequencyHold, adjust_clock}; -pub(crate) use entry::{EntryChild, file_size}; +pub(crate) use entry::{ + DEV, DEV_PTS, DEV_SHM, EntryChild, FileSystem, PROC, RUN, SYS, create_devices, file_size, + mount_detached, restrict_tree, +}; #[cfg(target_arch = "x86_64")] pub(crate) use fd::close_inherited_descriptors; pub(crate) use fd::{ diff --git a/scripts/test-layout-allowlist.json b/scripts/test-layout-allowlist.json index 5f6b123f1..0ad55db1e 100644 --- a/scripts/test-layout-allowlist.json +++ b/scripts/test-layout-allowlist.json @@ -7,6 +7,7 @@ "nvx-rs/src/guest/entry.rs": 1, "nvx-rs/src/guest/exec.rs": 4, "nvx-rs/src/guest/reseed.rs": 1, + "nvx-rs/src/guest/root.rs": 1, "nvx-rs/src/guest/sys/entry.rs": 3, "nvx-rs/src/guest/sys/spawn.rs": 6, "nvx-rs/src/guest/time/system.rs": 3,