diff --git a/Cargo.lock b/Cargo.lock index 9f7b523986..ce3d3cccad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -289,6 +289,7 @@ dependencies = [ "linux-kernel-cmdline", "rustix", "serde", + "tempfile", "toml", "tracing", ] @@ -781,9 +782,9 @@ checksum = "55b672471b4e9f9e95499ea597ff64941a309b2cdbffcc46f2cc5e2d971fd335" [[package]] name = "composefs" -version = "0.9.4" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7678ceb9ed3a1cd466aac8cae1d055d77336c7698fc3d7c8b8cbc5a3a3d33e42" +checksum = "4d7e1513737dcf10ea93c116fe1de51f5160d782cdb78cd4399895069338f47b" dependencies = [ "anyhow", "composefs-ioctls", @@ -809,9 +810,9 @@ dependencies = [ [[package]] name = "composefs-boot" -version = "0.9.4" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c61074a193dda9270830d3598d26b618b0181726c8cf61ab59d3f66924875d5e" +checksum = "5cae88df3ad0f69be4b615b4921fd639c6a1fd1519209f780fd61a8c692fa5ca" dependencies = [ "anyhow", "composefs", @@ -825,9 +826,9 @@ dependencies = [ [[package]] name = "composefs-ctl" -version = "0.9.4" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32bf730c9fe70c9d0a3b9f013547520c2d6d0ebb966a538ac73700a576114532" +checksum = "d0bf6a54f7f6e3db14b83f035a2f8625ad9826de188c861be5c9307e3e503314" dependencies = [ "anyhow", "cap-std-ext", @@ -855,9 +856,9 @@ dependencies = [ [[package]] name = "composefs-fuse" -version = "0.9.4" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a795d7cd30c58b9245122c3a41409c35ca3bf30dc313cfac700662f3a6e6e176" +checksum = "93d031d10f125a677778f3c359295e5046b19a96cb7cf64915a79b24b2da12a7" dependencies = [ "anyhow", "composefs", @@ -871,9 +872,9 @@ dependencies = [ [[package]] name = "composefs-ioctls" -version = "0.9.4" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6aad17a5f5c2c2f667c1c5cb543f6d6a5b5984fab651178eecc62b83008ebe6d" +checksum = "1d1e1dbf64a4a2dda8db3cbdb3109d29274b0cbeedc1ee68fdc8ccfdefc17cb9" dependencies = [ "rustix", "thiserror 2.0.21", @@ -881,9 +882,9 @@ dependencies = [ [[package]] name = "composefs-oci" -version = "0.9.4" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e4f7302aba57a0663087356e3df39d0bf43adf7304d845f5873b5b62974a4123" +checksum = "6402e698c01b839294a2787128a2357c47886054d6d249136a54c80693fdad22" dependencies = [ "anyhow", "async-compression", @@ -905,6 +906,7 @@ dependencies = [ "serde", "serde_json", "tar-core", + "tempfile", "thiserror 2.0.21", "tokio", "tokio-util", @@ -916,9 +918,9 @@ dependencies = [ [[package]] name = "composefs-splitdirfdstream" -version = "0.9.4" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "abe821055dd88f7c2d7e3ee8ac1e54be8b646f676d568c1f37e53c606b911f09" +checksum = "0d9847c8dd85e017d2615c41c46b9af9e7ce3e4f4021b841c6603cc62e184083" dependencies = [ "openssl", "rand 0.10.3", @@ -930,9 +932,9 @@ dependencies = [ [[package]] name = "composefs-storage" -version = "0.9.4" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "999579b801c75fec30b73eb416f40eff07725432ea704b1fb646ad709a1664ba" +checksum = "390fac98c3953f41ec4e65aefc303b1a3e5a2c0e739898471b9ea0cc478f5152" dependencies = [ "anyhow", "base64 0.23.0", @@ -1351,7 +1353,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -1999,7 +2001,7 @@ version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "82cb6a9f675da968c63b6208c641b9dca58fc0133ae53375736b1767b0cab8bd" dependencies = [ - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -2361,7 +2363,7 @@ version = "0.50.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" dependencies = [ - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -3007,7 +3009,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys", - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -3480,7 +3482,7 @@ dependencies = [ "getrandom 0.4.2", "once_cell", "rustix", - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -3490,7 +3492,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "230a1b821ccbd75b185820a1f1ff7b14d21da1e442e22c0863ea5f08771a8874" dependencies = [ "rustix", - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -3499,9 +3501,11 @@ version = "0.1.0" dependencies = [ "anyhow", "bcvk-qemu", + "bootc-initramfs-setup", "camino", "cap-std-ext", "clap", + "composefs-ctl", "data-encoding", "fn-error-context", "indicatif", @@ -3686,6 +3690,7 @@ version = "0.1.44" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100" dependencies = [ + "log", "pin-project-lite", "tracing-attributes", "tracing-core", @@ -4047,7 +4052,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -4372,6 +4377,12 @@ version = "0.8.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fdd20c5420375476fbd4394763288da7eb0cc0b8c11deed431a91562af7335d3" +[[package]] +name = "zcheapstr" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "939f186d9ecebe044315a5cb6d6f78568bbc21152adc954d5a0c2f58bdbc9920" + [[package]] name = "zerocopy" version = "0.8.48" @@ -4406,9 +4417,9 @@ checksum = "b268e58e7c693d7c271f93ffc4ba3b380412554231c85bf61ca7af91042a4112" [[package]] name = "zlink" -version = "0.7.0" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd5961645cac87b613e8b3c6fb1743a567e6459bc8c1d00f7bf9884020fc98d4" +checksum = "198be428106fe2987e3c014daadf976120909f4a765587341c9d4c2624912d61" dependencies = [ "zlink-core", "zlink-smol", @@ -4417,9 +4428,9 @@ dependencies = [ [[package]] name = "zlink-core" -version = "0.7.0" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0314bb8bc5a5693f4625a6ea9681731a5f4bcc0a952bfca28184f268db15dcc2" +checksum = "24a314bfe2e205db0b9cc286abc09b08f34837a06dd709b10ef7cf28bd622e4f" dependencies = [ "futures-util", "itoa", @@ -4432,34 +4443,46 @@ dependencies = [ "tracing", "zlink-idl", "zlink-macros", + "zlink-names", ] [[package]] name = "zlink-idl" -version = "0.7.0" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7990f15bd88a2b37d55c5fc13a1dd662ee98674ec9c6ac904fe46066523932a" +checksum = "9beafa91ab0672e1a2adc828cac4163152156d54046a0cad298807e290153ccf" dependencies = [ - "winnow 0.7.15", + "zlink-names", ] [[package]] name = "zlink-macros" -version = "0.7.0" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2fc623b8323797bf2d48333e7633e143077dd92cc66d9cca170014b66c43b1b5" +checksum = "ad68b7baceb8afe7b0b21e408961f6a9d28fe5f10e02b19ef15b7ac3be39fc90" dependencies = [ "proc-macro2", "quote", "syn 2.0.117", - "zlink-idl", + "zlink-names", +] + +[[package]] +name = "zlink-names" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e2019f3c1186761aee19689ba2f99e659ec7f0d637ad1ee96f22a54434e743a" +dependencies = [ + "serde", + "winnow 0.7.15", + "zcheapstr", ] [[package]] name = "zlink-smol" -version = "0.7.0" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4bc4150b8af92eb0ae7007e0ced3eb2bcf8a7fa49457de2f30b11752ea538f46" +checksum = "43f8b15370cace714c53983a91688004407f897eb1cf9b877b3e9df9330a26aa" dependencies = [ "async-broadcast", "async-channel", @@ -4472,9 +4495,9 @@ dependencies = [ [[package]] name = "zlink-tokio" -version = "0.7.0" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c4fe0f232dd656bcb0594e9646b86c69f6257e3d5194f28715a044c4d10c8fa" +checksum = "a46c4f9f765ecac29e50b21dc5c73185a2c2885a3d1ca1485ff1a97d96bc1538" dependencies = [ "futures-util", "pin-project-lite", diff --git a/Cargo.toml b/Cargo.toml index 7b7b76b207..f6b256827f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,7 +43,7 @@ clap_mangen = { version = "0.3.0" } # pulls), which drags in rustls-webpki/untrusted/webpki-root-certs whose # licenses aren't in our cargo-deny allow list. Re-enable once that's sorted. # See: https://github.com/bootc-dev/bootc/pull/2295 -composefs-ctl = { version = "0.9.4", default-features = false, features = [ +composefs-ctl = { version = "0.10.0", default-features = false, features = [ "pre-6.15", "pre-6.16", "oci", diff --git a/crates/initramfs/Cargo.toml b/crates/initramfs/Cargo.toml index a801b43636..2ea8f95693 100644 --- a/crates/initramfs/Cargo.toml +++ b/crates/initramfs/Cargo.toml @@ -18,6 +18,9 @@ tracing.workspace = true fn-error-context.workspace = true linux-kernel-cmdline = { workspace = true } +[dev-dependencies] +tempfile.workspace = true + [lints] workspace = true diff --git a/crates/initramfs/src/lib.rs b/crates/initramfs/src/lib.rs index ed989c57da..e8aedbb37b 100644 --- a/crates/initramfs/src/lib.rs +++ b/crates/initramfs/src/lib.rs @@ -24,9 +24,9 @@ use serde::Deserialize; use composefs::{ fsverity::{FsVerityHashValue, Sha512HashValue}, - mount::FsHandle, + mount::{FsHandle, MountOptions}, mountcompat::{overlayfs_set_fd, overlayfs_set_lower_and_data_fds, prepare_mount}, - repository::{ImageNotFound, Repository}, + repository::{ImageNotFound, IntegrityPolicy, Repository, VerityState}, }; use composefs_boot::cmdline::{ComposefsCmdline, KARG_COMPOSEFS_DIGEST, KARG_V2}; use composefs_ctl::composefs; @@ -353,7 +353,7 @@ fn open_root_fs(path: &Path) -> Result { /// /// # Arguments /// * sysroot - fd for /sysroot -/// * name - Name of the EROFS image to be mounted +/// * name - Expected fs-verity digest of the EROFS image /// * allow_missing_fsverity - Whether to allow mount without fsverity support #[context("Mounting composefs image")] pub fn mount_composefs_image( @@ -361,16 +361,26 @@ pub fn mount_composefs_image( name: &str, allow_missing_fsverity: bool, ) -> Result { + let digest = Sha512HashValue::from_hex(name).context("Parsing composefs digest")?; // Use open_upgrade to handle upgrades from older composefs-rs versions // that lack meta.json: it infers the algorithm and verity mode from // existing objects, writes meta.json, and opens normally. let (mut repo, _upgraded) = Repository::::open_upgrade(sysroot, "composefs")?; - if allow_missing_fsverity { - repo.set_insecure(); - } - let rootfs = repo - .mount(name) - .context("Failed to mount composefs image")?; + // The cmdline decides the mode, not the repository's mutable metadata: + // meta.json lives on the same unverified filesystem as the images, and + // older installs can legitimately have written it without fs-verity. + // The explicit policy is final for this handle. + let verity = if allow_missing_fsverity { + // Missing fs-verity is tolerated, but an image whose digest can be + // measured still has to match. + VerityState::Disabled + } else { + // fs-verity is required on the image and on every object it serves. + VerityState::Enabled + }; + repo.set_integrity_policy(IntegrityPolicy::Explicit(verity))?; + // mount_digest only ever looks at images/, never at named refs. + let rootfs = repo.mount_digest(&digest, &MountOptions::default())?; set_mount_readonly(&rootfs)?; @@ -381,10 +391,8 @@ fn parse_composefs_candidates(cmdline: &str) -> Result, allow_missing_fsverity: bool, ) -> Result> { - // `allow_missing_fsverity` is repository policy, not a trust decision - // supplied by the kernel command line. A cmdline '?' may only request - // the already-permitted insecure mode; it must never weaken strict policy. + // A candidate must not relax the fs-verity policy selected for this boot. if candidate.is_insecure() && !allow_missing_fsverity { anyhow::bail!( - "composefs candidate requests insecure fs-verity, but the repository policy is strict" + "composefs candidate requests insecure fs-verity, but the boot policy is strict" ); } @@ -669,8 +675,59 @@ pub fn setup_root(args: Args) -> Result<()> { #[cfg(test)] mod tests { + use std::{fs, fs::File}; + + use composefs::{ + fsverity::{Algorithm, CompareVerityError, MeasureVerityError, compute_verity}, + repository::RepositoryConfig, + }; + use super::*; + #[test] + fn test_strict_mount_rejects_unverified_images() -> Result<()> { + // A strict cmdline must not be overridden by the repository's insecure + // mode, and an image under images/refs/ must not stand in for the + // digest whatever the cmdline says. All of these are rejected before + // anything is mounted, so this runs unprivileged and on tmpfs. + let tmp = tempfile::tempdir()?; + let sysroot: OwnedFd = File::open(tmp.path())?.into(); + let (repo, _) = Repository::::init_path( + &sysroot, + "composefs", + RepositoryConfig::new(Algorithm::SHA512).set_insecure(), + )?; + drop(repo); + let images = tmp.path().join("composefs/images"); + fs::create_dir_all(images.join("refs"))?; + let data = b"unverified image"; + let digest = compute_verity::(data).to_hex(); + + fs::write(images.join("refs").join(&digest), data)?; + for allow_missing_fsverity in [false, true] { + let error = + mount_composefs_image(&sysroot, &digest, allow_missing_fsverity).unwrap_err(); + assert!(error.downcast_ref::().is_some(), "{error:#}"); + } + + fs::write(images.join(&digest), data)?; + let error = mount_composefs_image(&sysroot, &digest, false).unwrap_err(); + assert!( + matches!( + error.downcast_ref::(), + Some(CompareVerityError::Measure( + MeasureVerityError::VerityMissing | MeasureVerityError::FilesystemNotSupported + )) + ), + "{error:#}" + ); + + let missing = Sha512HashValue::EMPTY.to_hex(); + let error = mount_composefs_image(&sysroot, &missing, false).unwrap_err(); + assert!(error.downcast_ref::().is_some(), "{error:#}"); + Ok(()) + } + fn parse(toml: &str) -> Config { toml::from_str(toml).expect("TOML parse failed") } diff --git a/crates/lib/src/bootc_composefs/boot.rs b/crates/lib/src/bootc_composefs/boot.rs index 8f1d26d27b..156fd24c58 100644 --- a/crates/lib/src/bootc_composefs/boot.rs +++ b/crates/lib/src/bootc_composefs/boot.rs @@ -2830,7 +2830,7 @@ pub(crate) async fn setup_composefs_boot( let mut repo = open_composefs_repo(&root_setup.physical_root)?; if allow_missing_fsverity { - repo.set_insecure(); + repo.set_insecure()?; } let repo = Arc::new(repo); diff --git a/crates/lib/src/bootc_composefs/repo.rs b/crates/lib/src/bootc_composefs/repo.rs index efcfd7de99..d933c922d8 100644 --- a/crates/lib/src/bootc_composefs/repo.rs +++ b/crates/lib/src/bootc_composefs/repo.rs @@ -231,7 +231,7 @@ pub(crate) fn initialize_composefs_repository( // correctly derives the durable policy from meta.json and does not rewrite // it merely because this session permits missing fs-verity. if allow_missing_fsverity && (created || repository_insecure) { - repo.set_insecure(); + repo.set_insecure()?; } let repo = Arc::new(repo); @@ -561,7 +561,7 @@ pub(crate) async fn pull_composefs_repo( let mut repo = open_composefs_repo(&rootfs_dir).context("Opening composefs repo")?; if allow_missing_fsverity { - repo.set_insecure(); + repo.set_insecure()?; } let repo = Arc::new(repo); @@ -625,7 +625,7 @@ pub(crate) async fn pull_composefs_repo( anyhow::anyhow!("BUG: Arc still has other references after pull completed") })?; if allow_missing_fsverity { - repo.set_insecure(); + repo.set_insecure()?; } Ok(PullRepoResult { diff --git a/crates/lib/src/store/mod.rs b/crates/lib/src/store/mod.rs index 5b89a415ea..b63ed6caa5 100644 --- a/crates/lib/src/store/mod.rs +++ b/crates/lib/src/store/mod.rs @@ -600,7 +600,7 @@ impl BootedStorage { } let mut composefs = ComposefsRepository::open_path(&physical_root, COMPOSEFS)?; if cmdline.allow_missing_fsverity { - composefs.set_insecure(); + composefs.set_insecure()?; } let composefs = Arc::new(composefs); @@ -987,7 +987,7 @@ impl Storage { let composefs = match ComposefsRepository::open_path(&composefs_dir, ".") { Ok(mut repo) => { if !ostree_verity.enabled { - repo.set_insecure(); + repo.set_insecure()?; } repo } @@ -1009,7 +1009,7 @@ impl Storage { // the algorithm and writes meta.json. let (mut repo, _upgraded) = ComposefsRepository::open_upgrade(&composefs_dir, ".")?; if !ostree_verity.enabled { - repo.set_insecure(); + repo.set_insecure()?; } repo } diff --git a/crates/tests-integration/Cargo.toml b/crates/tests-integration/Cargo.toml index 04f2a481ab..3adf498ec6 100644 --- a/crates/tests-integration/Cargo.toml +++ b/crates/tests-integration/Cargo.toml @@ -13,9 +13,11 @@ path = "src/tests-integration.rs" [dependencies] # Workspace dependencies anyhow = { workspace = true } +bootc-initramfs-setup = { path = "../initramfs" } camino = { workspace = true } cap-std-ext = { workspace = true } clap = { workspace = true, features = ["derive","cargo"] } +composefs-ctl.workspace = true fn-error-context = { workspace = true } indoc = { workspace = true } rustix = { workspace = true } diff --git a/crates/tests-integration/README.md b/crates/tests-integration/README.md index 9299670a81..01dff87a75 100644 --- a/crates/tests-integration/README.md +++ b/crates/tests-integration/README.md @@ -13,6 +13,23 @@ run a `podman build` with the bootc git sources. ## Available suites +### `booted composefs-verity` + +Run as root on a test host with composefs support: + +```sh +bootc-integration-tests booted composefs-verity +``` + +This also runs through the `composefs-verity` TMT plan. It creates disposable +repositories in `/var/tmp` and mounts them in a private mount namespace. It +checks that a strict mount verifies the image digest independently of the +repository metadata, ignores `images/refs/`, and gets `EIO` when reading +tampered data objects, and that an explicitly relaxed mount still rejects an +image whose digest does not match. The test skips +filesystems without fs-verity support; it does not modify the host's repository +or boot files. + ### `composefs-bcvk` Intended only right now to be used with a sealed UKI image, diff --git a/crates/tests-integration/src/booted.rs b/crates/tests-integration/src/booted.rs index ec75ce75f8..8b377d558b 100644 --- a/crates/tests-integration/src/booted.rs +++ b/crates/tests-integration/src/booted.rs @@ -13,11 +13,14 @@ use anyhow::{Context, Result}; use clap::Subcommand; use xshell::{Shell, cmd}; +mod composefs_verity; mod switch_zstd_chunked; #[derive(Debug, Subcommand)] #[clap(rename_all = "kebab-case")] pub(crate) enum Opt { + /// Verify the initramfs mount path rejects composefs integrity failures + ComposefsVerity, /// Switch to an image with zstd:chunked compressed layers SwitchZstdChunked, } @@ -25,6 +28,7 @@ pub(crate) enum Opt { pub(crate) fn run(opt: Opt) -> Result<()> { let sh = &Shell::new()?; match opt { + Opt::ComposefsVerity => composefs_verity::run(), Opt::SwitchZstdChunked => switch_zstd_chunked::run(sh), } } diff --git a/crates/tests-integration/src/booted/composefs_verity.rs b/crates/tests-integration/src/booted/composefs_verity.rs new file mode 100644 index 0000000000..5c655ccf56 --- /dev/null +++ b/crates/tests-integration/src/booted/composefs_verity.rs @@ -0,0 +1,292 @@ +// number: 51 +// tmt: +// summary: Enforce composefs image and object integrity independently of repository metadata +// duration: 5m +// adjust: +// - when: running_env != image_mode +// enabled: false +// because: only the image_mode test image installs bootc-tests +// extra: +// skip_if_ostree: true +// +//! Exercise the initramfs mount helper against disposable repositories. The +//! host's boot files and repository are never modified. + +use std::{ + ffi::OsStr, + fs::{self, File}, + io::Read, + os::fd::{AsFd, OwnedFd}, + path::Path, +}; + +use anyhow::{Context, Result, ensure}; +use bootc_initramfs_setup::mount_composefs_image; +use composefs_ctl::composefs::{ + erofs::{ + format::FormatVersion, + writer::{ValidatedFileSystem, mkfs_erofs_versioned}, + }, + fsverity::{ + Algorithm, CompareVerityError, EnableVerityError, FsVerityHashValue, MeasureVerityError, + Sha512HashValue, enable_verity_raw, measure_verity_opt, + }, + mountcompat::prepare_mount, + repository::{ImageNotFound, Repository, RepositoryConfig}, + tree::{FileSystem, Inode, LeafContent, RegularFile, Stat}, +}; +use rustix::{ + fs::{Mode, OFlags, openat}, + io::Errno, + mount::{MountPropagationFlags, mount_change}, + thread::{UnshareFlags, unshare_unsafe}, +}; + +/// How the repository is tampered with after the image has been written. +#[derive(Clone, Copy, Debug)] +enum Tamper { + /// Nothing; the control case. + None, + /// The image is replaced by a byte-identical copy without fs-verity. + Image, + /// Like `Image`, but meta.json also loses fs-verity and a data object is + /// replaced: the state an offline attacker would leave behind. + ImageAndMetadata, + /// A data object is replaced while the image stays intact. + Data, + /// `images/` is repointed at a different image that does carry + /// fs-verity. + ImageSwap, + /// `images/` is removed and `images/refs/` points at a + /// different image that does carry fs-verity. + RefDigest, + /// `images/` is removed and `images/refs/` is a copy of + /// the image without fs-verity. + RefVerity, +} + +/// What `mount_composefs_image` must do with the tampered repository. +#[derive(Clone, Copy, Debug)] +enum Expect { + /// The mount succeeds and `data` reads back its original contents. + Original, + /// The mount succeeds and `data` reads back the replaced contents. + Replaced, + /// The mount succeeds, but reading `data` fails with `EIO`. + ReadEio, + /// The mount is refused because the image has no fs-verity. + VerityMissing, + /// The mount is refused because the image found does not have the + /// requested digest. + DigestMismatch, + /// The mount is refused because no image has the requested digest. + NotFound, +} + +#[derive(Clone, Copy, Debug)] +struct Case { + /// Whether meta.json records fs-verity as enabled. + metadata_verity: bool, + tamper: Tamper, + /// Whether the cmdline carries the `?` marker allowing missing fs-verity. + relaxed: bool, + expect: Expect, +} + +/// A strict cmdline gets the same verdict whatever meta.json says: only the +/// image named by the digest, carrying fs-verity, is ever mounted. +const STRICT: &[(Tamper, Expect)] = &[ + (Tamper::None, Expect::Original), + (Tamper::Image, Expect::VerityMissing), + (Tamper::ImageAndMetadata, Expect::VerityMissing), + (Tamper::Data, Expect::ReadEio), + (Tamper::ImageSwap, Expect::DigestMismatch), + (Tamper::RefDigest, Expect::NotFound), + (Tamper::RefVerity, Expect::NotFound), +]; + +/// A relaxed cmdline tolerates missing fs-verity but still verifies whatever +/// can be measured, and never consults `images/refs/` either. The first +/// element is `metadata_verity`. +const RELAXED: &[(bool, Tamper, Expect)] = &[ + (true, Tamper::Data, Expect::ReadEio), + (false, Tamper::ImageAndMetadata, Expect::Replaced), + (false, Tamper::ImageSwap, Expect::DigestMismatch), + (false, Tamper::RefDigest, Expect::NotFound), + (false, Tamper::RefVerity, Expect::NotFound), +]; + +fn replace(path: &Path, data: &[u8]) -> Result<()> { + let copy = path.with_extension("replacement"); + fs::write(©, data)?; + fs::rename(copy, path)?; + Ok(()) +} + +fn strip_verity(path: &Path) -> Result<()> { + replace(path, &fs::read(path)?)?; + ensure!(measure_verity_opt::(File::open(path)?)?.is_none()); + Ok(()) +} + +fn read_data(root: impl AsFd) -> std::io::Result> { + let mut file = File::from(openat( + root, + "data", + OFlags::RDONLY | OFlags::CLOEXEC, + Mode::empty(), + )?); + let mut data = Vec::new(); + file.read_to_end(&mut data)?; + Ok(data) +} + +fn is_expected_rejection(error: &anyhow::Error, expect: Expect) -> bool { + match expect { + Expect::VerityMissing => matches!( + error.downcast_ref::(), + Some(CompareVerityError::Measure( + MeasureVerityError::VerityMissing + )) + ), + Expect::DigestMismatch => matches!( + error.downcast_ref::(), + Some(CompareVerityError::DigestMismatch { .. }) + ), + Expect::NotFound => error.downcast_ref::().is_some(), + Expect::Original | Expect::Replaced | Expect::ReadEio => false, + } +} + +fn check_case(parent: &Path, case: Case) -> Result<()> { + let tmp = tempfile::tempdir_in(parent)?; + let sysroot: OwnedFd = File::open(tmp.path())?.into(); + let config = RepositoryConfig::new(Algorithm::SHA512); + let config = if case.metadata_verity { + config + } else { + config.set_insecure() + }; + let (repo, _) = Repository::::init_path(&sysroot, "composefs", config)?; + let repo_path = tmp.path().join("composefs"); + ensure!(repo.is_insecure() != case.metadata_verity); + + let contents = b"original data\n".repeat(1024); + let replaced = vec![b'x'; contents.len()]; + let object = repo.ensure_object(&contents)?; + let object_path = repo_path.join("objects").join(object.to_object_pathname()); + let mut stat = Stat::uninitialized(); + stat.st_mode = 0o755; + let mut tree = FileSystem::::new(stat.clone()); + stat.st_mode = 0o644; + let leaf = tree.push_leaf( + stat, + LeafContent::Regular(RegularFile::External(object, contents.len() as u64)), + ); + tree.root.insert(OsStr::new("data"), Inode::leaf(leaf)); + let image = mkfs_erofs_versioned(&ValidatedFileSystem::new(tree.clone())?, FormatVersion::V1); + let digest = repo.write_image(None, &image)?.to_hex(); + let image_link = repo_path.join("images").join(&digest); + let image_path = fs::canonicalize(&image_link)?; + + match case.tamper { + Tamper::None => {} + Tamper::Image => strip_verity(&image_path)?, + Tamper::ImageAndMetadata => { + strip_verity(&repo_path.join("meta.json"))?; + strip_verity(&image_path)?; + replace(&object_path, &replaced)?; + } + Tamper::Data => replace(&object_path, &replaced)?, + Tamper::ImageSwap | Tamper::RefDigest => { + tree.root.stat.st_mtim_sec += 1; + let other_image = + mkfs_erofs_versioned(&ValidatedFileSystem::new(tree)?, FormatVersion::V1); + let swap = matches!(case.tamper, Tamper::ImageSwap); + let reference = (!swap).then_some(digest.as_str()); + let other = repo.write_image(reference, &other_image)?.to_hex(); + ensure!(other != digest); + fs::remove_file(&image_link)?; + if swap { + std::os::unix::fs::symlink(&other, &image_link)?; + } + } + Tamper::RefVerity => { + fs::create_dir_all(repo_path.join("images/refs"))?; + fs::write(repo_path.join("images/refs").join(&digest), &image)?; + fs::remove_file(&image_link)?; + } + } + drop(repo); + + let mounted = mount_composefs_image(&sysroot, &digest, case.relaxed); + let root = match case.expect { + Expect::Original | Expect::Replaced | Expect::ReadEio => prepare_mount(mounted?)?, + Expect::VerityMissing | Expect::DigestMismatch | Expect::NotFound => { + let error = mounted.err().context("a tampered image was mounted")?; + ensure!( + is_expected_rejection(&error, case.expect), + "Unexpected error: {error:#}" + ); + return Ok(()); + } + }; + let read = read_data(&root); + match case.expect { + Expect::Original => ensure!(read? == contents), + Expect::Replaced => ensure!(read? == replaced), + _ => { + let error = read.err().context("a tampered object was readable")?; + ensure!( + error.raw_os_error() == Some(Errno::IO.raw_os_error()), + "Unexpected read error: {error}" + ); + } + } + Ok(()) +} + +pub(crate) fn run() -> Result<()> { + super::tap_begin("composefs initramfs verity enforcement"); + // SAFETY: Only the mount namespace is unshared, not the file descriptor + // table. No descriptors become inaccessible to other threads. + #[allow(unsafe_code)] + unsafe { unshare_unsafe(UnshareFlags::NEWNS) }.context("Creating test mount namespace")?; + mount_change( + "/", + MountPropagationFlags::PRIVATE | MountPropagationFlags::REC, + )?; + // /tmp is commonly tmpfs; /var/tmp is on the persistent data filesystem. + let tmp = tempfile::tempdir_in("/var/tmp")?; + let probe = tmp.path().join("verity-probe"); + fs::write(&probe, b"probe")?; + match enable_verity_raw::(File::open(probe)?) { + Ok(()) => {} + Err(EnableVerityError::FilesystemNotSupported) => { + println!("ok # SKIP /var/tmp filesystem does not support fs-verity"); + return Ok(()); + } + Err(error) => return Err(error.into()), + } + let strict = [true, false].into_iter().flat_map(|metadata_verity| { + STRICT.iter().map(move |&(tamper, expect)| Case { + metadata_verity, + tamper, + relaxed: false, + expect, + }) + }); + let relaxed = RELAXED + .iter() + .map(|&(metadata_verity, tamper, expect)| Case { + metadata_verity, + tamper, + relaxed: true, + expect, + }); + for case in strict.chain(relaxed) { + check_case(tmp.path(), case).with_context(|| format!("{case:?}"))?; + } + super::tap_ok(); + Ok(()) +} diff --git a/tmt/plans/integration.fmf b/tmt/plans/integration.fmf index efeacf5063..0d5c7b0872 100644 --- a/tmt/plans/integration.fmf +++ b/tmt/plans/integration.fmf @@ -352,6 +352,18 @@ execute: - /tmt/tests/tests/test-51-composefs-uki-addons extra-skip_if_ostree: true +/plan-51-composefs-verity: + summary: Enforce composefs image and object integrity independently of repository metadata + discover: + how: fmf + test: + - /tmt/tests/tests/test-51-composefs-verity + adjust: + - when: running_env != image_mode + enabled: false + because: only the image_mode test image installs bootc-tests + extra-skip_if_ostree: true + /plan-51-switch-from-delta: summary: Switch to a derived image using an OCI delta and verify after reboot discover: diff --git a/tmt/tests/tests.fmf b/tmt/tests/tests.fmf index 38094cd514..6eebe20afc 100644 --- a/tmt/tests/tests.fmf +++ b/tmt/tests/tests.fmf @@ -221,6 +221,15 @@ check: duration: 30m test: nu booted/test-composefs-uki-addons.nu +/test-51-composefs-verity: + summary: Enforce composefs image and object integrity independently of repository metadata + duration: 5m + adjust: + - when: running_env != image_mode + enabled: false + because: only the image_mode test image installs bootc-tests + test: bootc-integration-tests booted composefs-verity + /test-51-switch-from-delta: summary: Switch to a derived image using an OCI delta and verify after reboot duration: 30m