diff --git a/crates/initramfs/src/lib.rs b/crates/initramfs/src/lib.rs index 7c9af14d7..708a25387 100644 --- a/crates/initramfs/src/lib.rs +++ b/crates/initramfs/src/lib.rs @@ -38,6 +38,9 @@ use linux_kernel_cmdline::utf8::Cmdline; // mount_setattr syscall support const MOUNT_ATTR_RDONLY: u64 = 0x00000001; +const MOUNT_ATTR_NOSUID: u64 = 0x00000002; +const MOUNT_ATTR_NODEV: u64 = 0x00000004; +const MOUNT_ATTR_NOSYMFOLLOW: u64 = 0x00200000; #[repr(C)] struct MountAttr { @@ -91,6 +94,36 @@ pub fn set_mount_tree_readonly(fd: impl AsFd) -> Result<()> { mount_setattr(fd, libc::AT_EMPTY_PATH | libc::AT_RECURSIVE, &attr) } +/// Clear the read-only attribute on a detached mount without changing its children. +pub fn clear_mount_readonly(fd: impl AsFd) -> Result<()> { + let attr = MountAttr { + attr_set: 0, + attr_clr: MOUNT_ATTR_RDONLY, + propagation: 0, + userns_fd: 0, + }; + mount_setattr(fd, libc::AT_EMPTY_PATH, &attr) +} + +/// Set a mount tree read-only, nosuid, nodev, and prevent symlink traversal within it. +/// +/// A bind-mounted image tree is not a chroot: absolute and escaping symlinks +/// can otherwise resolve against the caller's root. `nosymfollow` keeps the +/// exposed tree confined to the image while leaving the underlying image +/// untouched. `nosuid` also prevents set-user-ID and set-group-ID bits in the +/// image from affecting processes that access the mounted tree. This deliberately +/// does not set `noexec`, so image executables remain runnable. +#[context("Setting mount tree readonly and nosymfollow")] +pub fn set_mount_tree_readonly_nosymfollow(fd: impl AsFd) -> Result<()> { + let attr = MountAttr { + attr_set: MOUNT_ATTR_RDONLY | MOUNT_ATTR_NOSUID | MOUNT_ATTR_NODEV | MOUNT_ATTR_NOSYMFOLLOW, + attr_clr: 0, + propagation: 0, + userns_fd: 0, + }; + mount_setattr(fd, libc::AT_EMPTY_PATH | libc::AT_RECURSIVE, &attr) +} + /// Types of mounts supported by the configuration #[derive(Clone, Copy, Debug, Deserialize, PartialEq)] #[serde(rename_all = "lowercase")] diff --git a/crates/lib/src/cli.rs b/crates/lib/src/cli.rs index 8a46071c6..b41b810fb 100644 --- a/crates/lib/src/cli.rs +++ b/crates/lib/src/cli.rs @@ -785,6 +785,10 @@ pub(crate) enum ImageOpts { /// The image to pull image: String, }, + /// Mount content from the booted or staged deployment image read-only. + Mount(crate::image_mount::MountOpts), + /// Unmount the named non-root mount (non-recursive; bootc provenance is not checked). + Unmount(crate::image_mount::UnmountOpts), /// Wrapper for selected `podman image` subcommands in bootc storage. #[clap(subcommand)] Cmd(ImageCmdOpts), @@ -2521,6 +2525,8 @@ async fn run_from_opt(opt: Opt) -> Result { .pull_from_host_storage(&image) .await } + ImageOpts::Mount(opts) => crate::image_mount::mount(opts).await, + ImageOpts::Unmount(opts) => crate::image_mount::unmount(opts), ImageOpts::Cmd(opt) => { let storage = get_storage().await?; let imgstore = storage.get_ensure_imgstore()?; diff --git a/crates/lib/src/image_mount.rs b/crates/lib/src/image_mount.rs new file mode 100644 index 000000000..6d220f2b6 --- /dev/null +++ b/crates/lib/src/image_mount.rs @@ -0,0 +1,856 @@ +//! Read-only access to the image content of a booted or staged deployment. +//! +//! This deliberately does not assemble deployment state: in particular, it +//! never bind-mounts `/var` or the deployment's mutable `/etc` over the image. + +use std::os::unix::fs::MetadataExt; +use std::{ + os::fd::{AsFd, AsRawFd, OwnedFd}, + path::PathBuf, + thread, +}; + +use anyhow::{Context, Result, bail, ensure}; +use camino::{Utf8Path, Utf8PathBuf}; +use cap_std_ext::cap_std::{ambient_authority, fs::Dir}; +use clap::{Args, ValueEnum}; +use composefs_ctl::composefs::fsverity::{FsVerityHashValue, Sha512HashValue}; +use ostree::gio; +use ostree_ext::{ostree, ostree_prepareroot, sysroot::SysrootLock}; +use rustix::fs::{AtFlags, CWD, Mode, OFlags, StatxFlags, openat, statx}; +use rustix::mount::{ + MoveMountFlags, OpenTreeFlags, UnmountFlags, move_mount, open_tree, unmount as unmount_path, +}; +use rustix::{ + mount::{MountPropagationFlags, mount_change}, + thread::{UnshareFlags, unshare_unsafe}, +}; + +#[derive(Debug, Clone, Copy, ValueEnum, PartialEq, Eq)] +#[clap(rename_all = "lowercase")] +pub(crate) enum Deployment { + Booted, + Staged, +} + +#[derive(Debug, Args, PartialEq, Eq)] +pub(crate) struct MountOpts { + /// Select exactly the booted deployment or the pending staged deployment. + #[arg(long, value_enum)] + pub(crate) deployment: Deployment, + + /// Absolute directory path inside the selected image to expose (`/` selects its root when + /// supported). OSTree checkout fallback rejects `/` and maps `/etc` to pristine `/usr/etc`. + /// No path component may be a symlink. + #[arg(long, value_parser = crate::cli::parse_absolute_path)] + pub(crate) source: Utf8PathBuf, + + /// Existing empty absolute directory to mount the selected image content on. + #[arg(long, value_parser = crate::cli::parse_absolute_path)] + pub(crate) target: Utf8PathBuf, +} + +#[derive(Debug, Args, PartialEq, Eq)] +pub(crate) struct UnmountOpts { + /// Named mountpoint to unmount (non-recursive; bootc provenance is not checked). + #[arg(value_parser = crate::cli::parse_absolute_path)] + pub(crate) target: Utf8PathBuf, +} + +struct MountedImageRoot { + tree: OwnedFd, + // Keep the repository/sysroot locked while cloning and attaching its tree. + _storage: StorageGuard, + // A non-composefs OSTree checkout has deployment-local /etc. Source + // selection maps /etc to pristine image content in /usr/etc instead. + checkout: bool, +} + +enum StorageGuard { + Composefs { + _repo: crate::store::ComposefsRepository, + }, + Ostree { + // Drop the lock before closing the detached mount it was opened through. + _sysroot: SysrootLock, + _mount: OwnedFd, + }, +} + +pub(crate) async fn mount(opts: MountOpts) -> Result<()> { + source_relative_path(&opts.source)?; + let composefs_booted = crate::bootc_composefs::status::composefs_booted()?; + // Select only from the active backend. These lookups are deliberately + // narrower than `status::get_host()`: they do not initialize storage, + // migrate repository metadata, configure the ESP, or change namespaces. + let image = if let Some(booted) = composefs_booted { + let (id, allow_missing_fsverity) = match opts.deployment { + Deployment::Booted => (booted.digest.to_string(), booted.allow_missing_fsverity), + Deployment::Staged => ( + staged_composefs_deployment_id(&booted.digest)?, + booted.allow_missing_fsverity, + ), + }; + mount_composefs_image(&opts, id, allow_missing_fsverity)? + } else { + mount_ostree_image(&opts).await? + }; + let MountedImageRoot { + tree, + _storage, + checkout, + } = image; + + let source_path = select_source_path(checkout, &opts.source)?; + bootc_initramfs_setup::set_mount_tree_readonly(&tree) + .context("Making selected deployment image read-only")?; + + let image_root = Dir::reopen_dir(&tree).context("Opening selected image root")?; + let source = open_source(&image_root, &source_path)?; + // Open once and retain this descriptor through validation and attachment. + // This avoids checking one pathname and mounting on a replacement object. + let (target, target_parent) = open_mount_target(&opts.target)?; + ensure_not_root_target(&target, &opts.target)?; + let source_metadata = source.metadata().context("Inspecting image source")?; + let target_metadata = target + .metadata() + .with_context(|| format!("Inspecting mount target {}", opts.target))?; + ensure!( + source_metadata.is_dir() && target_metadata.is_dir(), + "image source {} and target {} must both be directories", + opts.source, + opts.target + ); + + // `tree` is a detached mount. Kernel 6.12 rejects open_tree() paths which + // resolve through a directory fd inside an unattached mount. Attach it in + // an isolated worker namespace before cloning the already-validated source + // fd; never change mount visibility in the caller's namespace. + drop(image_root); + let source_tree = clone_source_in_staging_namespace(tree, source) + .with_context(|| format!("Cloning image source {}", opts.source))?; + bootc_initramfs_setup::set_mount_tree_readonly_nosymfollow(&source_tree) + .context("Making selected image content read-only and preventing symlink traversal")?; + + // Check immediately before attachment so a changed target cannot silently + // hide newly-added contents or stack another mount. + ensure_directory_empty(&target, &opts.target)?; + if let Some(parent) = target_parent.as_ref() { + ensure_not_mountpoint(&target, parent, &opts.target)?; + } + + move_mount( + &source_tree, + "", + &target, + "", + MoveMountFlags::MOVE_MOUNT_F_EMPTY_PATH | MoveMountFlags::MOVE_MOUNT_T_EMPTY_PATH, + ) + .with_context(|| format!("Attaching image source {} at {}", opts.source, opts.target))?; + + Ok(()) +} + +/// Temporarily attach a detached image mount in a private worker namespace so +/// that the kernel can clone a validated directory fd from inside it. +#[allow(unsafe_code)] +fn clone_source_in_staging_namespace(tree: OwnedFd, source: std::fs::File) -> Result { + thread::Builder::new() + .name("bootc-image-mount-stage".to_owned()) + .spawn(move || -> Result { + // A dedicated OS thread is required because mount namespace + // unsharing applies to the calling thread, not the whole process. + // SAFETY: This is a dedicated worker thread and only CLONE_NEWNS + // is requested (not CLONE_FILES), so the operation cannot alter + // the calling thread's namespace or invalidate shared fd tables. + unsafe { unshare_unsafe(UnshareFlags::NEWNS) } + .context("Unsharing image mount staging thread namespace")?; + mount_change( + c"/", + MountPropagationFlags::PRIVATE | MountPropagationFlags::REC, + ) + .context("Making image mount staging namespace private")?; + + let directory = + tempfile::tempdir().context("Creating image mount staging directory")?; + let mut attachment = TemporaryMountAttachment { + path: directory.path().to_owned(), + _directory: directory, + mounted: false, + }; + move_mount( + &tree, + "", + CWD, + attachment.path.as_path(), + MoveMountFlags::MOVE_MOUNT_F_EMPTY_PATH, + ) + .context("Attaching image mount in staging namespace")?; + attachment.mounted = true; + + // Use AT_EMPTY_PATH only after the fd has a path in this namespace. + // Do not recurse: image-mount source selection has always cloned + // only the selected mount, not its mounted children. + let cloned = open_tree( + &source, + "", + OpenTreeFlags::OPEN_TREE_CLONE + | OpenTreeFlags::OPEN_TREE_CLOEXEC + | OpenTreeFlags::AT_EMPTY_PATH, + ) + .context("Cloning validated image source fd"); + + // Drop all handles into the attached mount before its synchronous, + // non-recursive unmount. The detached clone (if successful) remains + // alive independently and is returned to the caller. + drop(source); + drop(tree); + let unmounted = attachment.unmount(); + match (cloned, unmounted) { + (Ok(cloned), Ok(())) => Ok(cloned), + (Err(error), Ok(())) => Err(error), + (Ok(_), Err(error)) => Err(error), + (Err(error), Err(unmount_error)) => Err(error.context(format!( + "Also failed to unmount staging attachment: {unmount_error:#}" + ))), + } + }) + .context("Starting image mount staging thread")? + .join() + .map_err(|_| anyhow::anyhow!("Image mount staging thread panicked"))? +} + +/// Normal-unmount guard for the temporary staging attachment. Drop retries +/// synchronously during error unwinding; it intentionally never lazy-detaches. +struct TemporaryMountAttachment { + path: PathBuf, + _directory: tempfile::TempDir, + mounted: bool, +} + +impl TemporaryMountAttachment { + fn unmount(&mut self) -> Result<()> { + if self.mounted { + unmount_path(&self.path, UnmountFlags::empty()) + .context("Unmounting image mount staging attachment")?; + self.mounted = false; + } + Ok(()) + } +} + +impl Drop for TemporaryMountAttachment { + fn drop(&mut self) { + if let Err(error) = self.unmount() { + tracing::error!("Failed to unmount image staging attachment: {error:#}"); + } + } +} + +/// Read the composefs staged selector without constructing `BootedStorage`. +/// The staged ID is valid only when it names a distinct deployment with local +/// composefs state and a parseable origin file. +fn staged_composefs_deployment_id(booted_id: &str) -> Result { + use crate::composefs_consts::{ + COMPOSEFS_STAGED_DEPLOYMENT_FNAME, COMPOSEFS_TRANSIENT_STATE_DIR, + }; + + let staged_path = + format!("{COMPOSEFS_TRANSIENT_STATE_DIR}/{COMPOSEFS_STAGED_DEPLOYMENT_FNAME}"); + let staged_json = std::fs::read_to_string(&staged_path) + .with_context(|| format!("Reading staged composefs deployment selector {staged_path}"))?; + let id = staged_composefs_id_from_json(&staged_json, booted_id)?; + + let sysroot = Dir::open_ambient_dir("/sysroot", ambient_authority()) + .context("Opening booted system sysroot")?; + let origin = crate::bootc_composefs::state::read_origin(&sysroot, &id)?.ok_or_else(|| { + anyhow::anyhow!("No composefs state or origin for staged deployment {id}") + })?; + let boot_type = origin + .get::( + crate::composefs_consts::ORIGIN_KEY_BOOT, + crate::composefs_consts::ORIGIN_KEY_BOOT_TYPE, + ) + .ok_or_else(|| { + anyhow::anyhow!("Staged composefs deployment {id} has no boot type in its origin") + })?; + crate::bootc_composefs::boot::BootType::try_from(boot_type.as_str()) + .context("Parsing staged composefs deployment boot type")?; + Ok(id) +} + +fn staged_composefs_id_from_json(contents: &str, booted_id: &str) -> Result { + let staged: crate::bootc_composefs::status::StagedDeployment = + serde_json::from_str(contents).context("Parsing staged composefs deployment selector")?; + let id = Sha512HashValue::from_hex(&staged.depl_id) + .context("Parsing staged composefs deployment ID")? + .to_hex(); + ensure!(id != booted_id, "No staged composefs deployment found"); + Ok(id) +} + +fn mount_composefs_image( + opts: &MountOpts, + id: String, + allow_missing_fsverity: bool, +) -> Result { + let sysroot = Dir::open_ambient_dir("/sysroot", ambient_authority()) + .context("Opening booted system sysroot")?; + let mut repo = crate::bootc_composefs::repo::open_composefs_repo(&sysroot) + .context("Opening existing composefs repository")?; + if allow_missing_fsverity { + repo.set_insecure()?; + } + let tree = repo + .mount(&id) + .with_context(|| format!("Mounting {:?} composefs deployment {id}", opts.deployment))?; + Ok(MountedImageRoot { + tree, + _storage: StorageGuard::Composefs { _repo: repo }, + checkout: false, + }) +} + +async fn mount_ostree_image(opts: &MountOpts) -> Result { + // OSTree's sysroot lock creates/opens a lock file, but /sysroot is normally + // mounted read-only. Clone the mount so that only this private mount view + // can be made writable; never call prepare_for_write(), which changes the + // mount namespace and would hide the image mount from the caller. + let sysroot_dir = Dir::open_ambient_dir("/sysroot", ambient_authority()) + .context("Opening booted system sysroot")?; + let sysroot_mount = open_tree( + &sysroot_dir, + ".", + OpenTreeFlags::OPEN_TREE_CLONE + | OpenTreeFlags::AT_RECURSIVE + | OpenTreeFlags::OPEN_TREE_CLOEXEC, + ) + .context("Cloning booted system sysroot mount")?; + bootc_initramfs_setup::clear_mount_readonly(&sysroot_mount) + .context("Making cloned booted system sysroot writable for OSTree locking")?; + + // OSTree opens the supplied path during load and uses its resulting + // directory fd for the genuine inter-process lock. Keep sysroot_mount + // alive for that fd and lock's full lifetime. + let path = format!("/proc/self/fd/{}", sysroot_mount.as_raw_fd()); + let sysroot_file = gio::File::for_path(path); + let sysroot = ostree::Sysroot::new(Some(&sysroot_file)); + sysroot + .load(gio::Cancellable::NONE) + .context("Loading booted OSTree sysroot")?; + let sysroot = SysrootLock::new_from_sysroot(&sysroot) + .await + .context("Locking booted OSTree sysroot")?; + sysroot + .load(gio::Cancellable::NONE) + .context("Reloading locked booted OSTree sysroot")?; + + // libostree identifies the booted deployment, and exposes runtime staged + // deployments, only when its Sysroot points at the current root (/), not + // the physical backing sysroot (/sysroot). Select from that view while + // holding the lock above. A staged deployment can be absent from the + // locked backing sysroot's deployment list, but its deployment path still + // names content beneath /sysroot; keep reads on the attached /sysroot + // mount for compatibility with overlayfs/kernel lower paths. + let booted_sysroot = ostree::Sysroot::new_default(); + booted_sysroot + .load(gio::Cancellable::NONE) + .context("Loading current OSTree root")?; + let selected_deployment = match opts.deployment { + Deployment::Booted => booted_sysroot + .require_booted_deployment() + .context("No booted OSTree deployment found")? + .clone(), + Deployment::Staged => { + let booted = booted_sysroot + .require_booted_deployment() + .context("No booted OSTree deployment found")?; + booted_sysroot + .deployments() + .into_iter() + .find(|deployment| deployment.is_staged() && deployment.osname() == booted.osname()) + .ok_or_else(|| anyhow::anyhow!("No staged OSTree deployment found"))? + } + }; + // Keep composefs lower/data paths rooted on the attached /sysroot mount. + // On older supported kernels (including 6.12), overlayfs cannot clone a + // lowerpath reached through the detached mount used for OSTree locking. + let deployment_path = sysroot.deployment_dirpath(&selected_deployment); + let deployment_dir = sysroot_dir + .open_dir(deployment_path.as_str()) + .with_context(|| format!("Opening selected OSTree deployment {deployment_path}"))?; + let repo = sysroot_dir + .open_dir("ostree/repo") + .context("Opening booted OSTree repository")?; + let config = ostree_prepareroot::load_config_from_root(&deployment_dir) + .context("Loading selected deployment's prepare-root.conf")?; + let (tree, checkout) = match ostree_prepareroot::mount_composefs( + &deployment_dir, + &repo, + selected_deployment.csum().as_str(), + config.as_ref(), + ) + .context("Mounting selected OSTree deployment image")? + { + Some(tree) => (tree, false), + None => ( + open_tree( + &sysroot_dir, + deployment_path.as_str(), + OpenTreeFlags::OPEN_TREE_CLONE | OpenTreeFlags::OPEN_TREE_CLOEXEC, + ) + .context("Cloning selected OSTree deployment checkout")?, + true, + ), + }; + Ok(MountedImageRoot { + tree, + _storage: StorageGuard::Ostree { + _sysroot: sysroot, + _mount: sysroot_mount, + }, + checkout, + }) +} + +fn select_source_path(checkout: bool, source: &Utf8Path) -> Result { + if !checkout { + return Ok(source.to_owned()); + } + ensure!( + source != Utf8Path::new("/"), + "OSTree checkout fallback does not support --source /: refusing to expose deployment-local /etc" + ); + if source == Utf8Path::new("/etc") || source.starts_with("/etc/") { + let suffix = source + .strip_prefix("/etc") + .expect("/etc source prefix was checked"); + let mut selected = Utf8PathBuf::from("/usr/etc"); + if !suffix.as_str().is_empty() { + selected.push(suffix); + } + return Ok(selected); + } + Ok(source.to_owned()) +} + +fn open_source(root: &Dir, source: &Utf8Path) -> Result { + let relative = source_relative_path(source)?; + let components = relative + .components() + .filter_map(|component| match component { + camino::Utf8Component::Normal(component) => Some(component), + _ => None, + }) + .collect::>(); + + let mut parent: Option = None; + if components.is_empty() { + let fd = openat( + root, + ".", + OFlags::PATH | OFlags::DIRECTORY | OFlags::CLOEXEC, + Mode::empty(), + ) + .context("Opening image root")?; + return Ok(std::fs::File::from(fd)); + } + + for (index, component) in components.iter().enumerate() { + let fd = match &parent { + Some(parent) => openat( + parent, + *component, + OFlags::PATH | OFlags::NOFOLLOW | OFlags::CLOEXEC, + Mode::empty(), + ), + None => openat( + root, + *component, + OFlags::PATH | OFlags::NOFOLLOW | OFlags::CLOEXEC, + Mode::empty(), + ), + } + .with_context(|| format!("Opening image source component {component} in {source}"))?; + let file = std::fs::File::from(fd); + let metadata = file + .metadata() + .with_context(|| format!("Inspecting image source component {component}"))?; + ensure!( + !metadata.file_type().is_symlink(), + "image source path components must not be symlinks: {source}" + ); + if index + 1 < components.len() { + ensure!( + metadata.is_dir(), + "image source path component {component} is not a directory" + ); + parent = Some(file.into()); + } else { + return Ok(file); + } + } + unreachable!("non-empty component list returns from the loop") +} + +fn source_relative_path(source: &Utf8Path) -> Result { + ensure!( + source.is_absolute(), + "image source must be absolute: {source}" + ); + for component in source.components() { + if matches!(component, camino::Utf8Component::ParentDir) { + bail!("image source must not contain `..`: {source}"); + } + } + source + .strip_prefix("/") + .map(|path| path.to_owned()) + .with_context(|| format!("Invalid absolute image source {source}")) +} + +pub(crate) fn unmount(opts: UnmountOpts) -> Result<()> { + // Resolve through directory descriptors so parent path components cannot + // be swapped for symlinks. Keep the parent, but close the target before + // unmounting: an open descriptor to the mount root makes it busy. + let (target, parent) = open_path_no_symlinks(&opts.target, false)?; + ensure_not_root_target(&target, &opts.target)?; + let parent = parent.ok_or_else(|| anyhow::anyhow!("Cannot unmount filesystem root"))?; + ensure!( + mount_id(&target)? != mount_id(&parent)?, + "{} is not a mountpoint", + opts.target + ); + let name = opts + .target + .file_name() + .ok_or_else(|| anyhow::anyhow!("Invalid unmount target {}", opts.target))?; + let fd_path = format!("/proc/self/fd/{}/{}", parent.as_raw_fd(), name); + drop(target); + unmount_path(fd_path.as_str(), UnmountFlags::NOFOLLOW) + .with_context(|| format!("Unmounting {}", opts.target))?; + Ok(()) +} + +fn open_mount_target(path: &Utf8Path) -> Result<(std::fs::File, Option)> { + open_path_no_symlinks(path, true) +} + +/// Resolve an absolute path one component at a time, retaining the parent +/// descriptor so checks and mount attachment refer to the same directory. +fn open_path_no_symlinks( + path: &Utf8Path, + final_directory: bool, +) -> Result<(std::fs::File, Option)> { + ensure!(path.is_absolute(), "target path must be absolute: {path}"); + ensure!( + path.components().all(|component| !matches!( + component, + camino::Utf8Component::ParentDir | camino::Utf8Component::Prefix(_) + )), + "target path must not contain `..`: {path}" + ); + let components = path + .components() + .filter_map(|component| match component { + camino::Utf8Component::RootDir => None, + camino::Utf8Component::Normal(component) => Some(component.to_owned()), + camino::Utf8Component::CurDir => None, + camino::Utf8Component::ParentDir | camino::Utf8Component::Prefix(_) => unreachable!(), + }) + .collect::>(); + + let root = openat( + CWD, + "/", + OFlags::PATH | OFlags::DIRECTORY | OFlags::CLOEXEC, + Mode::empty(), + ) + .context("Opening filesystem root")?; + let mut parent = std::fs::File::from(root); + if components.is_empty() { + ensure!(!final_directory, "mount target must not be filesystem root"); + return Ok((parent, None)); + } + + for (index, component) in components.iter().enumerate() { + let final_component = index + 1 == components.len(); + let mut flags = OFlags::PATH | OFlags::NOFOLLOW | OFlags::CLOEXEC; + if !final_component || final_directory { + flags |= OFlags::DIRECTORY; + } + let fd = openat(&parent, component.as_str(), flags, Mode::empty()) + .with_context(|| format!("Opening target path component {component} in {path}"))?; + let current = std::fs::File::from(fd); + let metadata = current + .metadata() + .with_context(|| format!("Inspecting target path component {component} in {path}"))?; + ensure!( + !metadata.file_type().is_symlink(), + "target path components must not be symlinks: {path}" + ); + if final_component { + return Ok((current, Some(parent))); + } + parent = current; + } + unreachable!("non-empty component list returns from the loop") +} + +fn ensure_directory_empty(target: &impl AsFd, path: &Utf8Path) -> Result<()> { + let mut entries = Dir::reopen_dir(target) + .with_context(|| format!("Opening mount target directory {path}"))? + .entries()?; + ensure!( + entries.next().is_none(), + "mount target {path} must be empty" + ); + Ok(()) +} + +/// Compare Linux mount IDs for a directory and its parent. A different ID +/// means the target is already a mountpoint, including an overmount. +fn ensure_not_mountpoint(target: &impl AsFd, parent: &impl AsFd, path: &Utf8Path) -> Result<()> { + let target_mount_id = mount_id(target)?; + let parent_mount_id = mount_id(parent)?; + ensure!( + target_mount_id == parent_mount_id, + "mount target {path} is already a mountpoint" + ); + Ok(()) +} + +fn mount_id(fd: &impl AsFd) -> Result { + let stat = statx(fd.as_fd(), "", AtFlags::EMPTY_PATH, StatxFlags::MNT_ID) + .context("Reading mount ID with statx")?; + ensure!( + stat.stx_mask & StatxFlags::MNT_ID.bits() == StatxFlags::MNT_ID.bits(), + "kernel did not provide mount IDs via statx (STATX_MNT_ID is required)" + ); + Ok(stat.stx_mnt_id) +} + +fn ensure_not_root_target(target: &std::fs::File, path: &Utf8Path) -> Result<()> { + let root = openat(CWD, "/", OFlags::PATH | OFlags::CLOEXEC, Mode::empty()) + .context("Opening filesystem root")?; + let root = std::fs::File::from(root); + let target_metadata = target + .metadata() + .with_context(|| format!("Inspecting target {path}"))?; + let root_metadata = root.metadata().context("Inspecting filesystem root")?; + ensure!( + target_metadata.dev() != root_metadata.dev() + || target_metadata.ino() != root_metadata.ino(), + "Refusing to mount or unmount the root filesystem" + ); + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::{ + Deployment, MountOpts, UnmountOpts, ensure_directory_empty, ensure_not_mountpoint, + ensure_not_root_target, open_mount_target, open_path_no_symlinks, open_source, + select_source_path, source_relative_path, + }; + use crate::cli::{ImageOpts, InstallOpts, Opt}; + use camino::{Utf8Path, Utf8PathBuf}; + use cap_std_ext::cap_std::{ambient_authority, fs::Dir}; + use clap::Parser; + use std::os::unix::fs::symlink; + + #[test] + fn parses_image_mount_and_unmount_without_changing_install_mount() { + let parsed = Opt::try_parse_from([ + "bootc", + "image", + "mount", + "--deployment", + "staged", + "--source", + "/usr", + "--target", + "/mnt/image", + ]) + .unwrap(); + assert!(matches!( + parsed, + Opt::Image(ImageOpts::Mount(MountOpts { + deployment: Deployment::Staged, + .. + })) + )); + assert!(matches!( + Opt::try_parse_from(["bootc", "image", "unmount", "/mnt/image"]).unwrap(), + Opt::Image(ImageOpts::Unmount(UnmountOpts { .. })) + )); + assert!(matches!( + Opt::try_parse_from([ + "bootc", + "install", + "mount", + "--sysroot", + "/sysroot", + "--latest", + "/mnt/installed" + ]) + .unwrap(), + Opt::Install(InstallOpts::Mount(_)) + )); + } + + #[test] + fn source_paths_must_stay_beneath_image_root() { + for path in ["relative", "/usr/../etc", "/../../etc"] { + assert!(source_relative_path(Utf8Path::new(path)).is_err(), "{path}"); + } + assert_eq!( + source_relative_path(Utf8Path::new("/usr/bin")) + .unwrap() + .as_str(), + "usr/bin" + ); + assert_eq!( + source_relative_path(Utf8Path::new("/")).unwrap().as_str(), + "" + ); + } + + #[test] + fn refuses_root_as_mount_or_unmount_target() { + let (root, _) = open_path_no_symlinks(Utf8Path::new("/"), false).unwrap(); + assert!(ensure_not_root_target(&root, Utf8Path::new("/")).is_err()); + + let temp = tempfile::tempdir().unwrap(); + let target = Utf8PathBuf::from_path_buf(temp.path().to_owned()).unwrap(); + let (target_fd, _) = open_path_no_symlinks(&target, false).unwrap(); + assert!(ensure_not_root_target(&target_fd, &target).is_ok()); + + let root_alias = temp.path().join("root-alias"); + symlink("/", &root_alias).unwrap(); + let root_alias = Utf8PathBuf::from_path_buf(root_alias).unwrap(); + assert!(open_path_no_symlinks(&root_alias, false).is_err()); + } + + #[test] + fn source_lookup_rejects_symlink_components_and_preserves_file_directory_matches() { + let temp = tempfile::tempdir().unwrap(); + std::fs::create_dir(temp.path().join("real-dir")).unwrap(); + std::fs::write(temp.path().join("real-file"), b"image").unwrap(); + symlink("/", temp.path().join("absolute-link")).unwrap(); + symlink("real-file", temp.path().join("file-link")).unwrap(); + let root = + cap_std_ext::cap_std::fs::Dir::open_ambient_dir(temp.path(), ambient_authority()) + .unwrap(); + + assert!(open_source(&root, Utf8Path::new("/absolute-link/etc")).is_err()); + assert!(open_source(&root, Utf8Path::new("/file-link")).is_err()); + assert!( + open_source(&root, Utf8Path::new("/real-file")) + .unwrap() + .metadata() + .unwrap() + .is_file() + ); + assert!( + open_source(&root, Utf8Path::new("/real-dir")) + .unwrap() + .metadata() + .unwrap() + .is_dir() + ); + assert!( + open_source(&root, Utf8Path::new("/")) + .unwrap() + .metadata() + .unwrap() + .is_dir() + ); + } + + #[test] + fn checkout_sources_map_etc_to_pristine_image_content() { + let temp = tempfile::tempdir().unwrap(); + let root = temp.path(); + std::fs::create_dir_all(root.join("etc/subdir")).unwrap(); + std::fs::create_dir_all(root.join("usr/etc/subdir")).unwrap(); + std::fs::create_dir_all(root.join("etc-other")).unwrap(); + std::fs::create_dir_all(root.join("usr/lib")).unwrap(); + std::fs::write(root.join("etc/marker"), b"deployment-local").unwrap(); + std::fs::write(root.join("usr/etc/marker"), b"image-pristine").unwrap(); + std::fs::write(root.join("etc/subdir/marker"), b"deployment-local-child").unwrap(); + std::fs::write(root.join("usr/etc/subdir/marker"), b"image-pristine-child").unwrap(); + std::fs::write(root.join("etc-other/marker"), b"etc-other").unwrap(); + + let image = Dir::open_ambient_dir(root, ambient_authority()).unwrap(); + assert_eq!(image.read("etc/marker").unwrap(), b"deployment-local"); + // Resolve through open_source as the mount path does, then read the + // marker relative to that selected source directory. + let read_source_marker = |source: &str| { + let selected = select_source_path(true, Utf8Path::new(source)).unwrap(); + let dir = Dir::reopen_dir(&image).unwrap(); + let source = open_source(&dir, &selected).unwrap(); + let source = Dir::reopen_dir(&source).unwrap(); + source.read("marker").unwrap() + }; + assert_eq!(read_source_marker("/etc"), b"image-pristine"); + assert_eq!(read_source_marker("/etc/subdir"), b"image-pristine-child"); + assert_eq!(read_source_marker("/etc-other"), b"etc-other"); + assert_eq!( + select_source_path(true, Utf8Path::new("/usr")).unwrap(), + Utf8Path::new("/usr") + ); + assert_eq!( + select_source_path(false, Utf8Path::new("/etc")).unwrap(), + Utf8Path::new("/etc") + ); + assert_eq!( + select_source_path(false, Utf8Path::new("/")).unwrap(), + Utf8Path::new("/") + ); + assert!(select_source_path(true, Utf8Path::new("/")).is_err()); + } + + #[test] + fn mount_target_must_be_empty_and_have_no_symlink_components() { + let temp = tempfile::tempdir().unwrap(); + let target_path = Utf8PathBuf::from_path_buf(temp.path().join("empty")).unwrap(); + std::fs::create_dir(&target_path).unwrap(); + let (target, parent) = open_mount_target(&target_path).unwrap(); + ensure_directory_empty(&target, &target_path).unwrap(); + ensure_not_mountpoint(&target, parent.as_ref().unwrap(), &target_path).unwrap(); + + std::fs::write(target_path.join("existing"), b"content").unwrap(); + assert!(ensure_directory_empty(&target, &target_path).is_err()); + + let linked_target = temp.path().join("linked-target"); + symlink(&target_path, &linked_target).unwrap(); + let linked_target = Utf8PathBuf::from_path_buf(linked_target).unwrap(); + assert!(open_mount_target(&linked_target).is_err()); + + let linked_parent = temp.path().join("linked-parent"); + symlink(temp.path(), &linked_parent).unwrap(); + let nested_target = Utf8PathBuf::from_path_buf(linked_parent.join("empty")).unwrap(); + assert!(open_mount_target(&nested_target).is_err()); + } + + #[test] + fn staged_composefs_selector_requires_a_valid_distinct_digest() { + let booted = "aa".repeat(64); + let staged = "bb".repeat(64); + let valid_json = format!(r#"{{"depl_id":"{staged}","finalization_locked":false}}"#); + assert_eq!( + super::staged_composefs_id_from_json(&valid_json, &booted).unwrap(), + staged + ); + assert!(super::staged_composefs_id_from_json(&valid_json, &staged).is_err()); + assert!(super::staged_composefs_id_from_json("{}", &booted).is_err()); + assert!( + super::staged_composefs_id_from_json( + r#"{"depl_id":"not-a-digest","finalization_locked":false}"#, + &booted + ) + .is_err() + ); + } +} diff --git a/crates/lib/src/lib.rs b/crates/lib/src/lib.rs index 07e644186..4747bc6c9 100644 --- a/crates/lib/src/lib.rs +++ b/crates/lib/src/lib.rs @@ -81,6 +81,7 @@ pub(crate) mod fsck; pub(crate) mod generator; mod glyph; mod image; +mod image_mount; mod install; pub(crate) mod journal; mod k8sapitypes; diff --git a/docs/src/bootc-experimental-image.7.md b/docs/src/bootc-experimental-image.7.md index e86a7c8e3..33a95b59b 100644 --- a/docs/src/bootc-experimental-image.7.md +++ b/docs/src/bootc-experimental-image.7.md @@ -15,6 +15,52 @@ then be managed independently; used as a base image, pushed to a registry, etc. Run `bootc image copy-to-storage --help` for more options. +## Mounting deployment image content + +`bootc image mount` exposes a path from the locally available booted or staged +deployment image at an existing empty directory. The source must be a directory; +`--source /` selects the image root when supported. It refuses target paths with +symlink components and targets that are already mountpoints. It does not pull an +image or merge deployment state: `/var` is never mounted from host state. For an +OSTree checkout fallback, root is unsupported and fails closed because a +non-recursive root mount would expose deployment-local `/etc`. Instead, `/etc` +and descendants select the image's pristine `/usr/etc` and descendants. Other +subpaths, including `/usr`, remain available on older kernels; this avoids the +detached child-mount operation that older kernels do not support. Composefs and +OSTree composefs trees retain normal source-path semantics, including root. +The target is checked immediately before attachment; callers must prevent +concurrent changes to the target directory while the command runs. + +```sh +bootc image mount --deployment booted --source /usr --target /mnt/booted-usr +bootc image unmount /mnt/booted-usr +``` + +Use `--deployment staged` to select the pending deployment from the active +backend: OSTree selects its staged deployment, while composefs reads the +existing `/run/composefs/staged-deployment` selector. Staged selection does not +discover transitions across backends. If the active backend has no staged +deployment, the command fails instead of guessing an image. This lookup does +not initialize storage, migrate repository metadata, configure the ESP, or +change mount namespaces. + +The mounted content is read-only, `nosuid`, and `nodev`. It intentionally +remains executable: the mount does not use `noexec`, so direct executable +paths in the mounted image can be run. Where supported, the image root itself +(`--source /`) is a valid directory source. Read-only, `nosuid`, `nodev`, and `nosymfollow` do not +disable execution. However, `nosymfollow` prevents following symlinks in the +mounted tree, so a symlink path to an executable will not resolve, even when +its target is also inside the image. The requested source path itself must +also contain no symlink components; this prevents source lookup from escaping +the image before the mount is created. Unmount is non-recursive and operates on +the opened target; it fails if the target is not a mount root or if child mounts +prevent a safe unmount. `bootc image unmount TARGET` is a general-purpose +`umount TARGET`: it unmounts the named non-root mount, whether or not bootc +created it. It does not track or verify bootc provenance, so take care to name +only the mount you intend to remove. Mount attachment follows the propagation +behavior of the target mount namespace; use a private mount namespace if +propagation is undesired. + Example workflow: ``` @@ -25,4 +71,3 @@ FROM localhost/bootc $ podman build -t localhost/bootc-custom . $ bootc switch --transport containers-storage localhost/bootc-custom ``` - diff --git a/tmt/plans/integration.fmf b/tmt/plans/integration.fmf index b7d1e932f..6399143ca 100644 --- a/tmt/plans/integration.fmf +++ b/tmt/plans/integration.fmf @@ -441,6 +441,13 @@ execute: - /tmt/tests/tests/test-63-install-shared-propagation extra-fixme_skip_if_composefs: true +/plan-64-image-mount: + summary: Verify booted and staged deployment image mounts + discover: + how: fmf + test: + - /tmt/tests/tests/test-64-image-mount + /plan-66-encrypted-swap-ordering: summary: Verify a random-key encrypted swap boots without an ordering cycle discover: diff --git a/tmt/tests/booted/test-64-image-mount.nu b/tmt/tests/booted/test-64-image-mount.nu new file mode 100644 index 000000000..b07ccefd5 --- /dev/null +++ b/tmt/tests/booted/test-64-image-mount.nu @@ -0,0 +1,225 @@ +# number: 64 +# tmt: +# summary: Verify booted and staged deployment image mounts +# duration: 30m + +use std assert +use tap.nu + +def assert_mount_options [target] { + let options = ( + findmnt --mountpoint $target --json + | from json + | get filesystems.0.options + | split row "," + ) + # findmnt's options field reports these VFS mount options, including nosymfollow. + for option in [ro nosuid nodev nosymfollow] { + assert ( + $options | any { |value| $value == $option } + ) $"($target) should have the ($option) mount option" + } + assert (not ($options | any { |value| $value == "noexec" })) $"($target) should not have noexec" +} + +def cleanup_targets [targets] { + $targets | each { |target| + let result = (do -i { bootc image unmount $target } | complete) + if $result.exit_code != 0 { + $"($target): unmount failed: ($result.stderr); mount target left in place" + } + } | where { |failure| $failure != null } +} + +def mount_image [deployment source target] { + let result = (do -i { + bootc image mount --deployment $deployment --source $source --target $target + } | complete) + if $result.exit_code != 0 { + error make { + msg: $"Failed to mount ($deployment) image source ($source) at ($target): ($result.stderr)" + } + } +} + +def get_secure_boot_state [] { + let secure_boot_var = "/sys/firmware/efi/efivars/SecureBoot-8be4df61-93ca-11d2-aa0d-00e098032b8c" + let read_result = (do -i { ^env LC_ALL=C od -An -t u1 $secure_boot_var } | complete) + if $read_result.exit_code != 0 { + if ($read_result.stderr | str contains $"od: ($secure_boot_var): No such file or directory") { + return "unknown" + } + error make { + msg: $"Failed to read Secure Boot EFI variable ($secure_boot_var): ($read_result.stderr)" + } + } + let bytes = try { + $read_result.stdout | split words | each { into int } + } catch { + error make { msg: $"Failed to parse Secure Boot EFI variable ($secure_boot_var)" } + } + if ($bytes | length) != 5 { + error make { + msg: $"Unexpected Secure Boot EFI variable size at ($secure_boot_var): expected 5 bytes, got (($bytes | length))" + } + } + let enabled = ($bytes | last) + if not ($enabled in [0 1]) { + error make { msg: $"Invalid Secure Boot EFI variable value at ($secure_boot_var): ($enabled)" } + } + if $enabled == 1 { "enabled" } else { "disabled" } +} + +def verify_image_mounts [base deployment_etc_marker staged_marker staged_marker_content staged_fixture] { + let sysroot_options = ( + findmnt --mountpoint /sysroot --json + | from json + | get filesystems.0.options + ) + let root_target = ($base | path join "root") + let usr_target = ($base | path join "usr") + let staged_target = ($base | path join "staged") + mut mounted = [] + let result = try { + mkdir $root_target $usr_target $staged_target + + let marker_result = (do -i { ^touch $deployment_etc_marker } | complete) + assert equal $marker_result.exit_code 0 $"failed to create deployment-local /etc marker: ($marker_result.stderr)" + let root_result = (do -i { + bootc image mount --deployment booted --source / --target $root_target + } | complete) + if $root_result.exit_code == 0 { + $mounted = ($mounted | append $root_target) + let root_os_release = ($root_target | path join "usr/lib/os-release") + let mounted_deployment_marker = ($root_target | path join "etc" ($deployment_etc_marker | path basename)) + assert ($root_os_release | path exists) "booted root mount should contain usr/lib/os-release" + assert (not ($mounted_deployment_marker | path exists)) "image root must not expose deployment-local /etc" + assert_mount_options $root_target + } else { + assert ($root_result.stderr | str contains "OSTree checkout fallback does not support --source /") $"unexpected root mount failure: ($root_result.stderr)" + print "SKIP: image-root mount is unsupported for OSTree checkout fallback" + } + mount_image booted /usr $usr_target + $mounted = ($mounted | append $usr_target) + + let usr_os_release = ($usr_target | path join "lib/os-release") + assert ($usr_os_release | path exists) "/usr mount should contain lib/os-release" + let booted_staged_marker = ($usr_target | path join "share" ($staged_marker | path basename)) + assert (not ($booted_staged_marker | path exists)) "booted /usr must not contain the staged image marker" + + assert_mount_options $usr_target + if ($mounted | any { |target| $target == $root_target }) { + let root_os_release = ($root_target | path join "usr/lib/os-release") + assert equal (open $root_os_release | into string) (open $usr_os_release | into string) + } + + let write_path = ($usr_target | path join ".bootc-image-mount-write-test") + let write_result = (do -i { ^touch $write_path } | complete) + assert ($write_result.exit_code != 0) "writes to mounted image content should fail" + assert (not ($write_path | path exists)) "failed write should not create a file" + + # The mount is intentionally not noexec; verify an image executable runs. + let true_path = ($usr_target | path join "bin/true") + let executable_result = (do -i { ^$true_path } | complete) + assert equal $executable_result.exit_code 0 "mounted /usr/bin/true should be executable" + + if $staged_fixture { + mount_image staged /usr $staged_target + $mounted = ($mounted | append $staged_target) + let staged_os_release = ($staged_target | path join "lib/os-release") + assert ($staged_os_release | path exists) "staged /usr mount should contain lib/os-release" + let staged_marker_path = ($staged_target | path join "share" ($staged_marker | path basename)) + assert ($staged_marker_path | path exists) "staged /usr mount should contain the staged image marker" + assert equal (open $staged_marker_path | into string) $staged_marker_content "staged image marker content should match exactly" + assert_mount_options $staged_target + } + let sysroot_options_after = ( + findmnt --mountpoint /sysroot --json + | from json + | get filesystems.0.options + ) + assert equal $sysroot_options_after $sysroot_options "image mounts should not change /sysroot mount options" + true + } catch { |error| $error } + + { + result: $result + mounted: $mounted + } +} + +let base = (^mktemp -d /run/bootc-image-mount.XXXXXX | str trim) +let deployment_etc_marker = $"/etc/.bootc-image-mount-deployment-local-(($base | path basename))" +let staged_marker = $"/usr/share/bootc-image-mount-(($base | path basename))" +let staged_tag = $"localhost/bootc-image-mount:(($base | path basename))" +let staged_marker_content = "staged image mount marker" + +let test_outcome = try { + let booted_status = (bootc status --json | from json) + let booted_digest = $booted_status.status.booted.image.imageDigest + let booted_composefs = ($booted_status.status.booted.composefs? | default null) + let is_composefs_uki = if $booted_composefs == null { + false + } else { + (($booted_composefs.bootType? | default "" | str downcase) == "uki") + } + let secure_boot_state = if $is_composefs_uki { + get_secure_boot_state + } else { + null + } + assert (not ($staged_marker | path exists)) "staged image marker must be absent from booted /usr before building the fixture" + let staged_fixture = if $is_composefs_uki and $secure_boot_state != "disabled" { + if $secure_boot_state == "enabled" { + print "SKIP: Secure Boot is enabled for the composefs UKI; tap's staged fixture is unsealed and cannot be signed; booted checks still run" + } else { + print "SKIP: Secure Boot state is unknown because the EFI variable is missing; skipping the unsealed composefs UKI staged fixture; booted checks still run" + } + false + } else { + bootc image copy-to-storage + let containerfile = (tap make_uki_containerfile $"FROM localhost/bootc as base\nRUN printf '%s' '($staged_marker_content)' > ($staged_marker)") + let build_result = (do -i { $containerfile | podman build --network=none -t $staged_tag $base -f - } | complete) + assert equal $build_result.exit_code 0 $"failed to build staged image fixture: ($build_result.stderr)" + + let switch_result = (do -i { bootc switch --transport containers-storage $staged_tag } | complete) + assert equal $switch_result.exit_code 0 $"failed to stage local image fixture: ($switch_result.stderr)" + + let staged_status = (bootc status --json | from json).status.staged + assert ($staged_status != null) "bootc switch should create a staged deployment" + assert equal $staged_status.image.image.transport containers-storage "staged image should use containers-storage transport" + assert equal $staged_status.image.image.image $staged_tag "staged image should use the derived local image tag" + assert (($staged_status.image.imageDigest? | default "") != "") "staged image digest should be reported" + assert (($booted_digest | default "") != "") "booted image digest should be reported" + assert ($staged_status.image.imageDigest != $booted_digest) "staged image digest should differ from the booted image digest" + true + } + verify_image_mounts $base $deployment_etc_marker $staged_marker $staged_marker_content $staged_fixture +} catch { |error| { result: $error, mounted: [] } } +let mount_cleanup_errors = (cleanup_targets $test_outcome.mounted) +mut cleanup_errors = $mount_cleanup_errors +if ($deployment_etc_marker | path exists) { + let marker_cleanup = (do -i { ^rm -- $deployment_etc_marker } | complete) + if $marker_cleanup.exit_code != 0 { + $cleanup_errors = ($cleanup_errors | append $"($deployment_etc_marker): failed to remove deployment-local /etc marker: ($marker_cleanup.stderr)") + } +} +if (($cleanup_errors | length) == 0) { + let remove_result = (do -i { ^rm --recursive $base } | complete) + if $remove_result.exit_code == 0 { + $cleanup_errors = [] + } else { + $cleanup_errors = [$"($base): failed to remove test directory: ($remove_result.stderr)"] + } +} else { + $cleanup_errors = ($cleanup_errors | append $"($base): directory retained because cleanup failed") +} + +if (($cleanup_errors | length) > 0) { + error make { + msg: $"Failed to clean up image mounts: ($cleanup_errors); test result: ($test_outcome.result)" + } +} +if $test_outcome.result != true { + error make { msg: $"Image mount assertions failed: ($test_outcome.result)" } +} diff --git a/tmt/tests/tests.fmf b/tmt/tests/tests.fmf index 21aab2ea6..ecda3eb7f 100644 --- a/tmt/tests/tests.fmf +++ b/tmt/tests/tests.fmf @@ -286,6 +286,11 @@ check: duration: 30m test: nu booted/test-install-shared-propagation.nu +/test-64-image-mount: + summary: Verify booted and staged deployment image mounts + duration: 30m + test: nu booted/test-64-image-mount.nu + /test-66-encrypted-swap-ordering: summary: Verify a random-key encrypted swap boots without an ordering cycle duration: 30m