diff --git a/Cargo.lock b/Cargo.lock index 3b66764c7a..b38eb186ad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1375,6 +1375,7 @@ dependencies = [ "openssl", "owo-colors", "rustix", + "serde", "tracing", ] diff --git a/Dockerfile b/Dockerfile index 36a36986b2..9a1b81f754 100644 --- a/Dockerfile +++ b/Dockerfile @@ -126,15 +126,18 @@ RUN --mount=type=tmpfs,target=/run --mount=type=tmpfs,target=/tmp \ cd /run/hack/ && SKIP_CONFIGS="${SKIP_CONFIGS}" ./provision-fetch.sh - pkgs_to_install=() + pkgs_to_install=(fsverity-utils) + + . /usr/lib/os-release + + case "${ID}${ID_LIKE:-}" in + *centos*|*rhel*) + # Enable EPEL for sbsigntools and fsverity-utils + dnf -y install epel-release + ;; + esac + if [[ "${seal_state}" == "sealed" ]]; then - . /usr/lib/os-release - case "${ID}${ID_LIKE:-}" in - *centos*|*rhel*) - # Enable EPEL for sbsigntools - dnf -y install epel-release - ;; - esac pkgs_to_install+=(sbsigntools) fi diff --git a/crates/etc-merge/Cargo.toml b/crates/etc-merge/Cargo.toml index ce698250e5..13eba0ef4d 100644 --- a/crates/etc-merge/Cargo.toml +++ b/crates/etc-merge/Cargo.toml @@ -16,6 +16,7 @@ composefs-ctl = { workspace = true } fn-error-context = { workspace = true } owo-colors = { workspace = true } anstream = { workspace = true } +serde = { workspace = true } [lints] workspace = true diff --git a/crates/etc-merge/src/lib.rs b/crates/etc-merge/src/lib.rs index 66d74ad1be..97e752c3b9 100644 --- a/crates/etc-merge/src/lib.rs +++ b/crates/etc-merge/src/lib.rs @@ -1,8 +1,7 @@ //! Lib for /etc merge -#![allow(dead_code)] - use fn_error_context::context; +use serde::{Deserialize, Serialize}; use std::collections::BTreeMap; use std::ffi::OsStr; use std::io::BufReader; @@ -78,14 +77,14 @@ fn stat_eq_ignore_mtime(this: &Stat, other: &Stat) -> bool { return true; } -#[derive(Debug)] +#[derive(Debug, Serialize, Deserialize)] pub struct UnmergablePaths { pub path: PathBuf, pub reason: String, } /// Represents the differences between two directory trees. -#[derive(Debug)] +#[derive(Debug, Serialize, Deserialize)] pub struct Diff { /// Paths that exist in the current /etc but not in the pristine added: Vec, @@ -255,6 +254,8 @@ fn get_modifications( pristine_leaves: &[Leaf], current_leaves: &[Leaf], new: &Directory, + current_etc_fd: &CapStdDir, + pristine_etc_fd: &CapStdDir, mut current_path: PathBuf, diff: &mut Diff, ) -> anyhow::Result<()> { @@ -281,6 +282,8 @@ fn get_modifications( pristine_leaves, current_leaves, new, + current_etc_fd, + pristine_etc_fd, current_path.clone(), diff, )?; @@ -334,61 +337,101 @@ fn get_modifications( } } - Inode::Leaf(leaf_id, _) => match pristine.leaf_id(path) { - Ok(old_leaf_id) => { - let leaf = ¤t_leaves[leaf_id.0]; - let old_leaf = &pristine_leaves[old_leaf_id.0]; + Inode::Leaf(leaf_id, _) => { + match pristine.leaf_id(path) { + Ok(old_leaf_id) => { + let leaf = ¤t_leaves[leaf_id.0]; + let old_leaf = &pristine_leaves[old_leaf_id.0]; - if !stat_eq_ignore_mtime(&old_leaf.stat, &leaf.stat) { - diff.modified.push(current_path.clone()); - check_if_mergable(new, inode, ¤t_path, diff)?; - current_path.pop(); - continue; - } + if !stat_eq_ignore_mtime(&old_leaf.stat, &leaf.stat) { + diff.modified.push(current_path.clone()); + check_if_mergable(new, inode, ¤t_path, diff)?; + current_path.pop(); + continue; + } - match (&old_leaf.content, &leaf.content) { - (Regular(old_meta), Regular(current_meta)) => { - if old_meta.content_hash != current_meta.content_hash { - // File modified in some way - diff.modified.push(current_path.clone()); - check_if_mergable(new, inode, ¤t_path, diff)?; + match (&old_leaf.content, &leaf.content) { + (Regular(old_meta), Regular(current_meta)) => { + let modified = match (&old_meta.verity, ¤t_meta.verity) { + (None, None) => { + old_meta.content_hash != current_meta.content_hash + } + // We have fsverity enabled for one file, but not the other + // This could happen but is unlikely. The file may or may not + // have been modified, compute the content hash to check + (None, Some(_)) | (Some(_), None) => { + let mut pristine_hash = old_meta.content_hash.clone(); + let mut current_hash = current_meta.content_hash.clone(); + + let compute_hash = |mut file: cap_std::fs::File| -> anyhow::Result { + let mut hasher = openssl::hash::Hasher::new( + openssl::hash::MessageDigest::sha256(), + )?; + std::io::copy(&mut file, &mut hasher)?; + Ok(hex::encode(hasher.finish()?)) + }; + + if pristine_hash.len() == 0 { + let file = pristine_etc_fd.open(¤t_path) + .with_context(|| anyhow::anyhow!("Opening {} in pristine etc for computing hash", current_path.display()))?; + pristine_hash = compute_hash(file)?; + } + + if current_hash.len() == 0 { + let file = current_etc_fd.open(¤t_path) + .with_context(|| anyhow::anyhow!("Opening {} in current etc for computing hash", current_path.display()))?; + current_hash = compute_hash(file)?; + } + + pristine_hash != current_hash + } + (Some(old_verity), Some(current_verity)) => { + old_verity != current_verity + } + }; + + if modified { + // File modified in some way + diff.modified.push(current_path.clone()); + check_if_mergable(new, inode, ¤t_path, diff)?; + } } - } - (Symlink(old_link), Symlink(current_link)) => { - if old_link != current_link { - // Symlink modified in some way + (Symlink(old_link), Symlink(current_link)) => { + if old_link != current_link { + // Symlink modified in some way + diff.modified.push(current_path.clone()); + check_if_mergable(new, inode, ¤t_path, diff)?; + } + } + + (Symlink(..), Regular(..)) | (Regular(..), Symlink(..)) => { + // File changed to symlink or vice-versa diff.modified.push(current_path.clone()); check_if_mergable(new, inode, ¤t_path, diff)?; } - } - (Symlink(..), Regular(..)) | (Regular(..), Symlink(..)) => { - // File changed to symlink or vice-versa - diff.modified.push(current_path.clone()); - check_if_mergable(new, inode, ¤t_path, diff)?; + (a, b) => { + unreachable!("{a:?} modified to {b:?}") + } } + } - (a, b) => { - unreachable!("{a:?} modified to {b:?}") - } + Err(ImageError::IsADirectory(..)) => { + // A directory was changed to a file + diff.modified.push(current_path.clone()); + check_if_mergable(new, inode, ¤t_path, diff)?; } - } - Err(ImageError::IsADirectory(..)) => { - // A directory was changed to a file - diff.modified.push(current_path.clone()); - check_if_mergable(new, inode, ¤t_path, diff)?; - } + Err(ImageError::NotFound(..)) => { + // File not found in original /etc, file was added + diff.added.push(current_path.clone()); + check_if_mergable(new, inode, ¤t_path, diff)?; + } - Err(ImageError::NotFound(..)) => { - // File not found in original /etc, file was added - diff.added.push(current_path.clone()); - check_if_mergable(new, inode, ¤t_path, diff)?; + Err(e) => Err(e).with_context(|| format!("Opening pristine {path:?}"))?, } - - Err(e) => Err(e).with_context(|| format!("Opening pristine {path:?}"))?, - }, + } } current_path.pop(); @@ -470,6 +513,8 @@ pub fn compute_diff( pristine_etc_files: &FileSystem, current_etc_files: &FileSystem, new_etc_files: &FileSystem, + current_etc_fd: &CapStdDir, + pristine_etc_fd: &CapStdDir, ) -> anyhow::Result { let mut diff = Diff { added: vec![], @@ -484,6 +529,8 @@ pub fn compute_diff( &pristine_etc_files.leaves, ¤t_etc_files.leaves, &new_etc_files.root, + current_etc_fd, + pristine_etc_fd, PathBuf::new(), &mut diff, )?; @@ -637,17 +684,19 @@ fn recurse_dir( continue; } - // TODO: Another generic here but constrained to Sha256HashValue - // Regarding this, we'll definitely get DigestMismatch error if SHA512 is being used - // So we query the verity again if we get a DigestMismatch error + // NOTE: Currently this path isn't taken in practice (yet) since "current etc" is a mounted + // overlay which doesn't actually have FSVerity enabled on the files and `fsverity measure + // ` fails with `ERROR: FS_IOC_MEASURE_VERITY failed on '': Inappropriate ioctl for device` + // + // Related: https://github.com/bootc-dev/bootc/issues/2627 let measured_verity = - composefs::fsverity::measure_verity_opt::(entry.open()?); + composefs::fsverity::measure_verity_opt::(entry.open()?); let measured_verity = match measured_verity { Ok(mv) => mv.map(|verity| verity.to_hex()), Err(composefs::fsverity::MeasureVerityError::InvalidDigestAlgorithm { .. }) => { - composefs::fsverity::measure_verity_opt::(entry.open()?)? + composefs::fsverity::measure_verity_opt::(entry.open()?)? .map(|verity| verity.to_hex()) } @@ -705,6 +754,7 @@ impl std::fmt::Display for ModificationType { } impl ModificationType { + #[allow(dead_code)] fn symbol(&self) -> &'static str { match self { ModificationType::Added => "+", @@ -1007,6 +1057,8 @@ mod tests { &pristine_etc_files, ¤t_etc_files, new_etc_files.as_ref().unwrap(), + &c, + &p, )?; merge( @@ -1207,6 +1259,8 @@ mod tests { &pristine_etc_files, ¤t_etc_files, &new_etc_files.as_ref().unwrap(), + &c, + &p, )?; merge(&c, ¤t_etc_files, &n, &new_etc_files.unwrap(), &diff)?; @@ -1287,6 +1341,8 @@ mod tests { &pristine_etc_files, ¤t_etc_files, &new_etc_files.as_ref().unwrap(), + &c, + &p, )?; let merge_res = merge(&c, ¤t_etc_files, &n, &new_etc_files.unwrap(), &diff); diff --git a/crates/lib/src/bootc_composefs/finalize.rs b/crates/lib/src/bootc_composefs/finalize.rs index 5e686bc3b6..759354b7bc 100644 --- a/crates/lib/src/bootc_composefs/finalize.rs +++ b/crates/lib/src/bootc_composefs/finalize.rs @@ -52,6 +52,8 @@ pub(crate) async fn get_etc_diff( &new_etc_files .as_ref() .unwrap_or(&FileSystem::new(Stat::uninitialized())), + ¤t_etc, + &pristine_etc, )?; Ok(diff) @@ -134,7 +136,13 @@ pub(crate) async fn composefs_backend_finalize( let new_files = new_files.ok_or_else(|| anyhow::anyhow!("Failed to get dirtree for new etc"))?; - let diff = compute_diff(&pristine_files, ¤t_files, &new_files)?; + let diff = compute_diff( + &pristine_files, + ¤t_files, + &new_files, + ¤t_etc, + &pristine_etc, + )?; merge(¤t_etc, ¤t_files, &new_etc, &new_files, &diff)?; // Remove /etc/.updated from the new deployment so that ConditionNeedsUpdate=|/etc diff --git a/crates/lib/src/cli.rs b/crates/lib/src/cli.rs index 0059f45f82..8a46071c60 100644 --- a/crates/lib/src/cli.rs +++ b/crates/lib/src/cli.rs @@ -940,6 +940,9 @@ pub(crate) enum InternalsOpts { /// Whether to perform the three way merge or not #[clap(long)] merge: bool, + /// Output the diff as json + #[clap(long)] + json: bool, }, #[cfg(feature = "docgen")] /// Dump CLI structure as JSON for documentation generation @@ -2741,6 +2744,7 @@ async fn run_from_opt(opt: Opt) -> Result { current_etc, new_etc, merge, + json, } => { let pristine_etc = Dir::open_ambient_dir(pristine_etc, cap_std::ambient_authority())?; @@ -2754,8 +2758,14 @@ async fn run_from_opt(opt: Opt) -> Result { .as_ref() .ok_or_else(|| anyhow::anyhow!("Failed to get new directory tree"))?; - let diff = compute_diff(&p, &c, &n)?; - print_diff(&diff, &mut std::io::stdout()); + let diff = compute_diff(&p, &c, &n, ¤t_etc, &pristine_etc)?; + + if json { + serde_json::to_writer(std::io::stdout(), &diff) + .context("Writing JSON output")?; + } else { + print_diff(&diff, &mut std::io::stdout()); + } if merge { etc_merge::merge(¤t_etc, &c, &new_etc, &n, &diff)?; diff --git a/tmt/tests/booted/test-etc-merge-conflict.nu b/tmt/tests/booted/test-etc-merge-conflict.nu index d797fffdb1..6209e34bb2 100644 --- a/tmt/tests/booted/test-etc-merge-conflict.nu +++ b/tmt/tests/booted/test-etc-merge-conflict.nu @@ -71,6 +71,33 @@ RUN echo "now a file" > /etc/test-etc-merge-dir # Again verify nothing was staged assert ((bootc status --json | from json | get status.staged) == null) "No deployment should be staged after directory-to-file conflict failure" + + let missingVerityAllowed = bootc status --json | from json | get status.booted.composefs.missingVerityAllowed + + # Test that a change in fsverity on a file actually shows up as modified + if not $missingVerityAllowed { + mkdir /var/etc-test/current /var/etc-test/new /var/etc-test/pristine + echo "hello" | save /var/etc-test/pristine/hello + echo "hello-new" | save /var/etc-test/current/hello + + ^find /var/etc-test/ -type f -exec fsverity enable {} ';' + + let out = bootc internals dir-diff /var/etc-test/pristine/ /var/etc-test/current/ /var/etc-test/new/ --json | from json | get modified + assert equal ($out | length) 1 + assert equal ($out | first) "hello" + + # Test one file with fs-verity enable and other without + rm -rf /var/etc-test + mkdir /var/etc-test/current /var/etc-test/new /var/etc-test/pristine + echo "hello" | save /var/etc-test/pristine/hello + echo "hello-new" | save /var/etc-test/current/hello + + fsverity enable /var/etc-test/pristine/hello + + let out = bootc internals dir-diff /var/etc-test/pristine/ /var/etc-test/current/ /var/etc-test/new/ --json | from json | get modified + assert equal ($out | length) 1 + assert equal ($out | first) "hello" + } tap ok }