diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3ef05e0a4..fb2097abd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -71,10 +71,17 @@ jobs: run: sudo rm -f /bin/skopeo /usr/bin/skopeo - name: Free up disk space on runner run: sudo ./ci/clean-gha-runner.sh + - name: Enable fsverity for / + run: sudo tune2fs -O verity $(findmnt -vno SOURCE /) + - name: Install utils + run: sudo apt -y install fsverity - name: Integration tests run: | set -xeu + # Build images to test; TODO investigate doing single container builds + # via GHA and pushing to a temporary registry to share among workflows? sudo podman build -t localhost/bootc -f hack/Containerfile . + sudo podman build -t localhost/bootc-fsverity -f ci/Containerfile.install-fsverity export CARGO_INCREMENTAL=0 # because we aren't caching the test runner bits cargo build --release -p tests-integration df -h / @@ -83,8 +90,9 @@ jobs: df -h / # Nondestructive but privileged tests sudo bootc-integration-tests host-privileged localhost/bootc - # Finally the install-alongside suite + # Install tests sudo bootc-integration-tests install-alongside localhost/bootc + sudo bootc-integration-tests install-fsverity localhost/bootc-fsverity docs: if: ${{ contains(github.event.pull_request.labels.*.name, 'documentation') }} runs-on: ubuntu-latest diff --git a/Cargo.lock b/Cargo.lock index 140b80c73..07b928c69 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -102,6 +102,19 @@ version = "1.0.92" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "74f37166d7d48a0284b99dd824694c26119c700b53bf0d1540cdb147dbdaaf13" +[[package]] +name = "async-compression" +version = "0.4.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df895a515f70646414f4b45c0b79082783b80552b373a68283012928df56f522" +dependencies = [ + "flate2", + "futures-core", + "memchr", + "pin-project-lite", + "tokio", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -176,6 +189,7 @@ dependencies = [ "chrono", "clap", "clap_mangen", + "composefs", "fn-error-context", "hex", "indicatif", @@ -402,6 +416,26 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" +[[package]] +name = "composefs" +version = "0.2.0" +dependencies = [ + "anyhow", + "async-compression", + "clap", + "containers-image-proxy", + "hex", + "indicatif", + "oci-spec", + "regex-automata 0.4.9", + "rustix", + "sha2", + "tar", + "tempfile", + "tokio", + "zstd", +] + [[package]] name = "console" version = "0.15.8" @@ -986,6 +1020,7 @@ dependencies = [ "instant", "number_prefix", "portable-atomic", + "tokio", "unicode-width", ] @@ -1624,8 +1659,8 @@ checksum = "4219d74c6b67a3654a9fbebc4b419e22126d13d2f3c4a07ee0cb61ff79a79619" dependencies = [ "aho-corasick", "memchr", - "regex-automata 0.4.5", - "regex-syntax 0.8.2", + "regex-automata 0.4.9", + "regex-syntax 0.8.5", ] [[package]] @@ -1639,13 +1674,13 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.4.5" +version = "0.4.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5bb987efffd3c6d0d8f5f89510bb458559eab11e4f869acb20bf845e016259cd" +checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" dependencies = [ "aho-corasick", "memchr", - "regex-syntax 0.8.2", + "regex-syntax 0.8.5", ] [[package]] @@ -1656,9 +1691,9 @@ checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" [[package]] name = "regex-syntax" -version = "0.8.2" +version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" [[package]] name = "roff" @@ -1674,9 +1709,9 @@ checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" [[package]] name = "rustix" -version = "0.38.38" +version = "0.38.41" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa260229e6538e52293eeb577aabd09945a09d6d9cc0fc550ed7529056c2e32a" +checksum = "d7f649912bc1495e167a6edee79151c84b1bad49748cb4f1f1167f459f6224f6" dependencies = [ "bitflags 2.4.2", "errno", @@ -2621,9 +2656,9 @@ dependencies = [ [[package]] name = "zstd" -version = "0.13.1" +version = "0.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d789b1514203a1120ad2429eae43a7bd32b90976a7bb8a05f7ec02fa88cc23a" +checksum = "fcf2b778a664581e31e389454a7072dab1647606d44f7feea22cd5abb9c9f3f9" dependencies = [ "zstd-safe", ] diff --git a/ci/Containerfile.install-fsverity b/ci/Containerfile.install-fsverity new file mode 100644 index 000000000..7dc34f545 --- /dev/null +++ b/ci/Containerfile.install-fsverity @@ -0,0 +1,10 @@ +# Enable fsverity at install time +FROM localhost/bootc +RUN < /usr/lib/bootc/install/30-fsverity.toml < All .file objects have fsverity + /// "disabled" => No .file objects have fsverity + /// "inconsistent" => Mixed state + OstreeVerity, +} + /// Hidden, internal only options #[derive(Debug, clap::Subcommand, PartialEq, Eq)] pub(crate) enum InternalsOpts { @@ -293,6 +303,8 @@ pub(crate) enum InternalsOpts { FixupEtcFstab, /// Should only be used by `make update-generated` PrintJsonSchema, + /// Perform consistency checking. + Fsck, /// Perform cleanup actions Cleanup, /// Proxy frontend for the `ostree-ext` CLI. @@ -952,6 +964,20 @@ async fn run_from_opt(opt: Opt) -> Result<()> { ) .await } + InternalsOpts::Fsck => { + let storage = get_storage().await?; + let r = crate::fsck::fsck(&storage).await?; + match r.errors.as_slice() { + [] => {} + errs => { + for err in errs { + eprintln!("error: {err}"); + } + anyhow::bail!("fsck found errors"); + } + } + Ok(()) + } InternalsOpts::FixupEtcFstab => crate::deploy::fixup_etc_fstab(&root), InternalsOpts::PrintJsonSchema => { let schema = schema_for!(crate::spec::Host); diff --git a/lib/src/fsck.rs b/lib/src/fsck.rs new file mode 100644 index 000000000..0a1268b35 --- /dev/null +++ b/lib/src/fsck.rs @@ -0,0 +1,127 @@ +//! # Write deployments merging image with configmap +//! +//! Create a merged filesystem tree with the image and mounted configmaps. + +use std::os::fd::AsFd; +use std::str::FromStr as _; + +use anyhow::Ok; +use anyhow::{Context, Result}; +use camino::Utf8PathBuf; +use cap_std::fs::Dir; +use cap_std_ext::cap_std; +use fn_error_context::context; +use ostree_ext::keyfileext::KeyFileExt; +use ostree_ext::ostree; +use rustix::io::Errno; +use serde::{Deserialize, Serialize}; + +use crate::install::config::Tristate; +use crate::store::{self, Storage}; + +#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] +#[serde(rename_all = "kebab-case")] +pub(crate) enum VerityState { + Enabled, + Disabled, + Inconsistent, +} + +#[derive(Default, Serialize, Deserialize, Debug, PartialEq, Eq)] +pub(crate) struct FsckResult { + pub(crate) errors: Vec, + pub(crate) verity: Option, +} + +/// Check the fsverity state of all regular files in this object directory. +#[context("Computing verity state")] +fn verity_state_of_objects(d: &Dir) -> Result<(u64, u64)> { + let mut enabled = 0; + let mut disabled = 0; + for ent in d.entries()? { + let ent = ent?; + if !ent.file_type()?.is_file() { + continue; + } + let name = ent.file_name(); + let name = name + .into_string() + .map(Utf8PathBuf::from) + .map_err(|_| anyhow::anyhow!("Invalid UTF-8"))?; + let Some("file") = name.extension() else { + continue; + }; + let f = d + .open(&name) + .with_context(|| format!("Failed to open {name}"))?; + let r: Option = + match composefs::fsverity::ioctl::fs_ioc_measure_verity(f.as_fd()) { + Ok(r) => Some(r), + Err(e) if matches!(e.downcast_ref::(), Some(&Errno::NOSYS)) => None, + Err(e) => return Err(e), + }; + drop(f); + if r.is_some() { + enabled += 1; + } else { + disabled += 1; + } + } + Ok((enabled, disabled)) +} + +async fn verity_state_of_all_objects(repo: &ostree::Repo) -> Result<(u64, u64)> { + const MAX_CONCURRENT: usize = 3; + + let repo_config = repo.config(); + let verity_state = { + let (k, v) = store::REPO_VERITY_CONFIG.split_once('.').unwrap(); + repo_config + .optional_string(k, v)? + .map(|v| Tristate::from_str(&v)) + .transpose()? + .unwrap_or_default() + }; + + let repodir = Dir::reopen_dir(&repo.dfd_borrow())?; + + let mut joinset = tokio::task::JoinSet::new(); + let mut results = Vec::new(); + + for ent in repodir.read_dir("objects")? { + while joinset.len() >= MAX_CONCURRENT { + results.push(joinset.join_next().await.unwrap()??); + } + let ent = ent?; + if !ent.file_type()?.is_dir() { + continue; + } + let objdir = ent.open_dir()?; + joinset.spawn_blocking(move || verity_state_of_objects(&objdir)); + } + + while let Some(output) = joinset.join_next().await { + results.push(output??); + } + let r = results.into_iter().fold((0, 0), |mut acc, v| { + acc.0 += v.0; + acc.1 += v.1; + acc + }); + Ok(r) +} + +pub(crate) async fn fsck(storage: &Storage) -> Result { + let mut r = FsckResult::default(); + r.verity = match verity_state_of_all_objects(&storage.repo()).await? { + (0, 0) => None, + (_, 0) => Some(VerityState::Enabled), + (0, _) => Some(VerityState::Disabled), + _ => Some(VerityState::Inconsistent), + }; + if matches!(&r.verity, &Some(VerityState::Inconsistent)) { + r.errors.push("Inconsistent fsverity state".into()); + } + serde_json::to_writer(std::io::stdout().lock(), &r)?; + Ok(r) +} diff --git a/lib/src/install.rs b/lib/src/install.rs index 96ba952df..55f04f7c8 100644 --- a/lib/src/install.rs +++ b/lib/src/install.rs @@ -33,6 +33,7 @@ use cap_std_ext::cmdext::CapStdExtCommandExt; use cap_std_ext::prelude::CapStdExtDirExt; use chrono::prelude::*; use clap::ValueEnum; +use config::Tristate; use fn_error_context::context; use ostree::gio; use ostree_ext::container as ostree_container; @@ -68,6 +69,15 @@ const SELINUXFS: &str = "/sys/fs/selinux"; const EFIVARFS: &str = "/sys/firmware/efi/efivars"; pub(crate) const ARCH_USES_EFI: bool = cfg!(any(target_arch = "x86_64", target_arch = "aarch64")); +const DEFAULT_REPO_CONFIG: &[(&str, &str)] = &[ + // Default to avoiding grub2-mkconfig etc. + ("sysroot.bootloader", "none"), + // Always flip this one on because we need to support alongside installs + // to systems without a separate boot partition. + ("sysroot.bootprefix", "true"), + ("sysroot.readonly", "true"), +]; + /// Kernel argument used to specify we want the rootfs mounted read-write by default const RW_KARG: &str = "rw"; @@ -577,6 +587,7 @@ pub(crate) fn print_configuration() -> Result<()> { #[context("Creating ostree deployment")] async fn initialize_ostree_root(state: &State, root_setup: &RootSetup) -> Result { + let install_config = state.install_config.as_ref(); let sepolicy = state.load_policy()?; let sepolicy = sepolicy.as_ref(); // Load a fd for the mounted target physical root @@ -602,14 +613,19 @@ async fn initialize_ostree_root(state: &State, root_setup: &RootSetup) -> Result crate::lsm::ensure_dir_labeled(rootfs_dir, "boot", None, 0o755.into(), sepolicy)?; } - for (k, v) in [ - // Default to avoiding grub2-mkconfig etc. - ("sysroot.bootloader", "none"), - // Always flip this one on because we need to support alongside installs - // to systems without a separate boot partition. - ("sysroot.bootprefix", "true"), - ("sysroot.readonly", "true"), - ] { + let fsverity = install_config + .and_then(|c| c.fsverity.clone()) + .unwrap_or_default(); + let fsverity_ostree_key = crate::store::REPO_VERITY_CONFIG; + let fsverity_ostree_opt = match fsverity { + Tristate::Disabled => None, + Tristate::Maybe => Some((fsverity_ostree_key, "maybe")), + Tristate::Enabled => Some((fsverity_ostree_key, "yes")), + }; + for (k, v) in DEFAULT_REPO_CONFIG + .iter() + .chain(fsverity_ostree_opt.as_ref()) + { Command::new("ostree") .args(["config", "--repo", "ostree/repo", "set", k, v]) .cwd_dir(rootfs_dir.try_clone()?) diff --git a/lib/src/install/baseline.rs b/lib/src/install/baseline.rs index f9b8795ca..f20d6cc0c 100644 --- a/lib/src/install/baseline.rs +++ b/lib/src/install/baseline.rs @@ -24,6 +24,7 @@ use serde::{Deserialize, Serialize}; use super::MountSpec; use super::RootSetup; use super::State; +use super::Tristate; use super::RUN_BOOTC; use super::RW_KARG; use crate::mount; @@ -147,13 +148,12 @@ pub(crate) fn install_create_rootfs( state: &State, opts: InstallBlockDeviceOpts, ) -> Result { + let install_config = state.install_config.as_ref(); let luks_name = "root"; // Ensure we have a root filesystem upfront let root_filesystem = opts .filesystem - .or(state - .install_config - .as_ref() + .or(install_config .and_then(|c| c.filesystem_root()) .and_then(|r| r.fstype)) .ok_or_else(|| anyhow::anyhow!("No root filesystem specified"))?; @@ -192,7 +192,7 @@ pub(crate) fn install_create_rootfs( } // Use the install configuration to find the block setup, if we have one - let block_setup = if let Some(config) = state.install_config.as_ref() { + let block_setup = if let Some(config) = install_config { config.get_block_setup(opts.block_setup.as_ref().copied())? } else if opts.filesystem.is_some() { // Otherwise, if a filesystem is specified then we default to whatever was @@ -370,8 +370,18 @@ pub(crate) fn install_create_rootfs( None }; + let fsverity = install_config + .and_then(|c| c.fsverity.clone()) + .unwrap_or_default(); + let mkfs_options = match (root_filesystem, fsverity) { + (Filesystem::Ext4, Tristate::Enabled | Tristate::Maybe) => ["-O", "verity"].as_slice(), + _ => [].as_slice(), + } + .iter() + .copied(); + // Initialize rootfs - let root_uuid = mkfs(&rootdev, root_filesystem, "root", opts.wipe, [])?; + let root_uuid = mkfs(&rootdev, root_filesystem, "root", opts.wipe, mkfs_options)?; let rootarg = format!("root=UUID={root_uuid}"); let bootsrc = boot_uuid.as_ref().map(|uuid| format!("UUID={uuid}")); let bootarg = bootsrc.as_deref().map(|bootsrc| format!("boot={bootsrc}")); diff --git a/lib/src/install/config.rs b/lib/src/install/config.rs index b0fe3e76a..1b1a7916d 100644 --- a/lib/src/install/config.rs +++ b/lib/src/install/config.rs @@ -41,6 +41,36 @@ pub(crate) struct BasicFilesystems { // pub(crate) esp: Option, } +#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)] +#[serde(deny_unknown_fields, rename_all = "kebab-case")] +pub(crate) enum Tristate { + #[default] + // The feature is disabled + #[serde(alias = "no", alias = "false")] + Disabled, + // The feature is enabled if supported + #[serde(alias = "maybe")] + Maybe, + // The feature is enabled + #[serde(alias = "yes", alias = "true")] + Enabled, +} + +impl std::str::FromStr for Tristate { + type Err = anyhow::Error; + + fn from_str(s: &str) -> Result { + let r = match s { + // Keep this in sync with ot_keyfile_get_tristate_with_default from ostree + "yes" | "true" | "1" => Tristate::Enabled, + "no" | "false" | "0" => Tristate::Disabled, + "maybe" => Tristate::Maybe, + o => anyhow::bail!("Invalid tristate value: {o}"), + }; + Ok(r) + } +} + /// The serialized [install] section #[derive(Debug, Clone, Serialize, Deserialize, Default)] #[serde(rename = "install", rename_all = "kebab-case", deny_unknown_fields)] @@ -50,6 +80,8 @@ pub(crate) struct InstallConfiguration { /// Enabled block storage configurations pub(crate) block: Option>, pub(crate) filesystem: Option, + /// How we should use fsverity. + pub(crate) fsverity: Option, /// Kernel arguments, applied at installation time #[serde(skip_serializing_if = "Option::is_none")] pub(crate) kargs: Option>, @@ -113,6 +145,7 @@ impl Mergeable for InstallConfiguration { { merge_basic(&mut self.root_fs_type, other.root_fs_type, env); merge_basic(&mut self.block, other.block, env); + merge_basic(&mut self.fsverity, other.fsverity, env); self.filesystem.merge(other.filesystem, env); if let Some(other_kargs) = other.kargs { self.kargs @@ -550,3 +583,29 @@ root-fs-type = "xfs" ) ); } + +#[test] +/// Test parsing fsverity +fn test_fsverity() { + let env = EnvProperties { + sys_arch: "aarch64".to_string(), + }; + let mut c: InstallConfigurationToplevel = toml::from_str( + r##"[install] +root-fs-type = "xfs" +fsverity = "enabled" +"##, + ) + .unwrap(); + let install = c.install.as_ref().unwrap(); + assert_eq!(install.fsverity.as_ref().unwrap(), &Tristate::Enabled); + let o: InstallConfigurationToplevel = toml::from_str( + r##"[install] +fsverity = "optional" +"##, + ) + .unwrap(); + c.install.merge(o.install, &env); + let install = c.install.as_ref().unwrap(); + assert_eq!(install.fsverity.as_ref().unwrap(), &Tristate::Maybe); +} diff --git a/lib/src/lib.rs b/lib/src/lib.rs index d8255499a..dea2b44dd 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -7,6 +7,7 @@ mod boundimage; pub mod cli; pub(crate) mod deploy; +pub(crate) mod fsck; pub(crate) mod generator; mod image; pub(crate) mod journal; diff --git a/lib/src/store/mod.rs b/lib/src/store/mod.rs index 56e142e8b..06ccaf260 100644 --- a/lib/src/store/mod.rs +++ b/lib/src/store/mod.rs @@ -15,6 +15,8 @@ use crate::spec::ImageStatus; mod ostree_container; +pub(crate) const REPO_VERITY_CONFIG: &str = "ex-integrity.fsverity"; + pub(crate) struct Storage { pub sysroot: SysrootLock, run: Dir, diff --git a/ostree-ext/src/ostree_prepareroot.rs b/ostree-ext/src/ostree_prepareroot.rs index de7b84fb7..b98f76bfb 100644 --- a/ostree-ext/src/ostree_prepareroot.rs +++ b/ostree-ext/src/ostree_prepareroot.rs @@ -47,10 +47,14 @@ pub(crate) fn overlayfs_root_enabled(root: &ostree::RepoFile) -> Result { } } +/// An option which can be enabled, disabled, or possibly enabled. #[derive(Debug, PartialEq, Eq)] -enum Tristate { +pub enum Tristate { + /// Enabled Enabled, + /// Disabled Disabled, + /// Maybe Maybe, } diff --git a/tests-integration/src/install.rs b/tests-integration/src/install.rs index d7c046937..7495e3ee1 100644 --- a/tests-integration/src/install.rs +++ b/tests-integration/src/install.rs @@ -133,6 +133,7 @@ pub(crate) fn run_alongside(image: &str, mut testargs: libtest_mimic::Arguments) "grep authorized_keys etc/tmpfiles.d/bootc-root-ssh.conf" ) .run()?; + drop(cwd); Ok(()) }, @@ -170,3 +171,4 @@ pub(crate) fn run_alongside(image: &str, mut testargs: libtest_mimic::Arguments) libtest_mimic::run(&testargs, tests.into()).exit() } +