Skip to content

Commit f839490

Browse files
committed
composefs: Preserve 1.16 upgrades with V1-default UKIs
We want to support upgrades from older bootc. Keep a legacy V2 digest in UKIs with a V1-capable initramfs, and select V2-only when resealing a legacy initramfs so the mounted root and deployment state retain the same identity. Determine compatibility from the actual initramfs rather than installed userspace. Preserve strict repository requirements when recovering a non-default boot image; changing serialization must not relax integrity. Assisted-by: AI Signed-off-by: Colin Walters <walters@verbum.org>
1 parent 4162a68 commit f839490

25 files changed

Lines changed: 1658 additions & 210 deletions

‎Dockerfile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ ARG variant
354354
ARG filesystem
355355
ARG seal_state
356356
ARG boot_type
357-
ARG erofs_version=v1
357+
ARG erofs_version=auto
358358
# Install our bootc package (only needed for the compute-composefs-digest command)
359359
RUN --network=none --mount=type=tmpfs,target=/run --mount=type=tmpfs,target=/tmp \
360360
--mount=type=bind,from=packages,src=/,target=/run/packages \

‎Justfile‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ mod bcvk 'bcvk.just'
2727
# sealed → requires boot_type=uki and filesystem with fsverity (ext4/btrfs)
2828
# uki → requires bootloader=systemd
2929

30-
# Output image name
31-
base_img := "localhost/bootc"
30+
# Output image name (override with BOOTC_image to isolate worktree images)
31+
base_img := env("BOOTC_image", "localhost/bootc")
3232
# Synthetic upgrade image for testing
3333
upgrade_img := base_img + "-upgrade"
3434
# Base image with tmt dependencies added, used as the boot source for upgrade tests
@@ -253,6 +253,7 @@ test-upgrade *ARGS: build _build-upgrade-source-image
253253
--karg=enforcing=0)
254254
fi
255255
cargo xtask run-tmt --env=BOOTC_variant={{variant}} \
256+
--env=BOOTC_erofs_version={{erofs_version}} \
256257
--env=BOOTC_test_upgrade_image={{base_img}} \
257258
--upgrade-image={{base_img}} \
258259
"${composefs_args[@]}" \
@@ -303,6 +304,7 @@ test-tmt-baseconfig baseconfig *ARGS:
303304
just variant=composefs baseconfigs={{baseconfig}} _build-upgrade-image
304305
cargo xtask run-tmt \
305306
--env=BOOTC_variant=composefs \
307+
--env=BOOTC_erofs_version={{erofs_version}} \
306308
--env=BOOTC_baseconfigs={{baseconfig}} \
307309
--upgrade-image={{upgrade_img}} \
308310
--composefs-backend \
@@ -511,6 +513,7 @@ _build-upgrade-image:
511513
--build-arg "boot_type={{boot_type}}" \
512514
--build-arg "seal_state={{seal_state}}" \
513515
--build-arg "filesystem={{filesystem}}" \
516+
--build-arg "base={{base_img}}" \
514517
--build-arg "erofs_version={{erofs_version}}" \
515518
--secret=id=secureboot_key,src=target/test-secureboot/db.key \
516519
--secret=id=secureboot_cert,src=target/test-secureboot/db.crt \

‎contrib/packaging/seal-uki‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set -xeuo pipefail
44

55
missing_verity=()
66
dumpfile_args=()
7-
erofs_version=v1
7+
erofs_version=auto
88

99
while [ ! -z "${1:-}" ]; do
1010
case "$1" in
@@ -96,7 +96,10 @@ fi
9696

9797
# Baseline container ukify options
9898
containerukifyargs=(--rootfs "${target}")
99+
if [[ $erofs_version != "auto" ]]; then
100+
containerukifyargs+=(--erofs-version="${erofs_version}")
101+
fi
99102

100103
# Build the UKI using bootc container ukify
101104
# This computes the composefs digest, reads kargs from kargs.d, and invokes ukify
102-
bootc container ukify "${containerukifyargs[@]}" "${kernel_params[@]}" "${missing_verity[@]}" "${dumpfile_args[@]}" --erofs-version="${erofs_version}" -- "${ukifyargs[@]}"
105+
bootc container ukify "${containerukifyargs[@]}" "${kernel_params[@]}" "${missing_verity[@]}" "${dumpfile_args[@]}" -- "${ukifyargs[@]}"

‎crates/initramfs/dracut/module-setup.sh‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ depends() {
1212
}
1313
install() {
1414
local service=bootc-root-setup.service
15+
local features_dir="${initdir}/usr/lib/bootc/initramfs-features"
1516
dracut_install /usr/lib/bootc/initramfs-setup
1617
inst_simple "${systemdsystemunitdir}/${service}"
1718
mkdir -p "${initdir}${systemdsystemunitdir}/initrd-root-fs.target.wants"
@@ -26,4 +27,10 @@ install() {
2627
# dracut --force in a Containerfile RUN layer).
2728
[[ -e /usr/lib/composefs/setup-root-conf.toml ]] && \
2829
inst_simple /usr/lib/composefs/setup-root-conf.toml
30+
31+
# This capability belongs to the generated initramfs, not the build host.
32+
# UKI producers use it to decide whether this initramfs can consume the V1
33+
# composefs digest and its matching state representation.
34+
mkdir -p "${features_dir}"
35+
printf '%s\n' 'composefs-digest-v1 state-v1' > "${features_dir}/composefs-digest-v1"
2936
}

‎crates/initramfs/src/lib.rs‎

Lines changed: 150 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ use serde::Deserialize;
2525
use composefs::{
2626
fsverity::{FsVerityHashValue, Sha512HashValue},
2727
mount::FsHandle,
28+
mount::{MountOptions, VerityRequirement, composefs_fsmount},
2829
mountcompat::{overlayfs_set_fd, overlayfs_set_lower_and_data_fds, prepare_mount},
29-
repository::Repository,
30+
repository::{ImageNotFound, Repository},
3031
};
31-
use composefs_boot::cmdline::ComposefsCmdline;
32+
use composefs_boot::cmdline::{ComposefsCmdline, KARG_COMPOSEFS_DIGEST, KARG_V2, split_cmdline};
3233
use composefs_ctl::composefs;
3334
use composefs_ctl::composefs_boot;
3435

@@ -353,15 +354,89 @@ pub fn mount_composefs_image(
353354
if allow_missing_fsverity {
354355
repo.set_insecure();
355356
}
356-
let rootfs = repo
357-
.mount(name)
358-
.context("Failed to mount composefs image")?;
357+
let (image, enable_verity) = repo.open_image(name)?;
358+
validate_image_verity(enable_verity, allow_missing_fsverity)
359+
.with_context(|| format!("Validating fs-verity for composefs image {name}"))?;
360+
let objects = repo.objects_dir().context("Getting objects directory")?;
361+
let verity = if enable_verity {
362+
VerityRequirement::Required
363+
} else {
364+
VerityRequirement::Disabled
365+
};
366+
let rootfs = composefs_fsmount(
367+
image,
368+
name,
369+
&[objects.as_fd()],
370+
verity,
371+
&MountOptions::default(),
372+
)
373+
.context("Creating filesystem mount")?;
359374

360375
set_mount_readonly(&rootfs)?;
361376

362377
Ok(rootfs)
363378
}
364379

380+
fn parse_composefs_candidates(cmdline: &str) -> Result<Vec<ComposefsCmdline<Sha512HashValue>>> {
381+
let mut candidates = Vec::new();
382+
for token in split_cmdline(cmdline) {
383+
if token.starts_with(&format!("{KARG_COMPOSEFS_DIGEST}="))
384+
|| token.starts_with(&format!("{KARG_V2}="))
385+
{
386+
if let Some(candidate) = ComposefsCmdline::<Sha512HashValue>::from_cmdline(token)? {
387+
candidates.push(candidate);
388+
}
389+
}
390+
}
391+
Ok(candidates)
392+
}
393+
394+
fn validate_image_verity(enable_verity: bool, allow_missing_fsverity: bool) -> Result<()> {
395+
if !allow_missing_fsverity && !enable_verity {
396+
anyhow::bail!("composefs image is not fs-verity sealed");
397+
}
398+
Ok(())
399+
}
400+
401+
fn select_composefs_candidate<T>(
402+
candidates: &[ComposefsCmdline<Sha512HashValue>],
403+
mut mount: impl FnMut(&ComposefsCmdline<Sha512HashValue>) -> Result<Option<T>>,
404+
) -> Result<(T, ComposefsCmdline<Sha512HashValue>)> {
405+
for candidate in candidates {
406+
if let Some(mounted) = mount(candidate)? {
407+
return Ok((mounted, candidate.clone()));
408+
}
409+
}
410+
let tried = candidates
411+
.iter()
412+
.map(|candidate| candidate.digest().to_hex())
413+
.collect::<Vec<_>>()
414+
.join(", ");
415+
anyhow::bail!("no composefs image found (tried: {tried})")
416+
}
417+
418+
fn mount_composefs_candidate(
419+
sysroot: &OwnedFd,
420+
candidate: &ComposefsCmdline<Sha512HashValue>,
421+
allow_missing_fsverity: bool,
422+
) -> Result<Option<OwnedFd>> {
423+
if candidate.is_insecure() && !allow_missing_fsverity {
424+
anyhow::bail!(
425+
"composefs candidate requests insecure fs-verity, but the repository policy is strict"
426+
);
427+
}
428+
429+
match mount_composefs_image(
430+
sysroot,
431+
&candidate.digest().to_hex(),
432+
allow_missing_fsverity,
433+
) {
434+
Ok(rootfs) => Ok(Some(rootfs)),
435+
Err(error) if error.downcast_ref::<ImageNotFound>().is_some() => Ok(None),
436+
Err(error) => Err(error),
437+
}
438+
}
439+
365440
/// Mounts a subdirectory with the specified configuration
366441
#[context("Mounting subdirectory")]
367442
pub fn mount_subdir(
@@ -468,17 +543,27 @@ pub fn setup_root(args: Args) -> Result<()> {
468543
config
469544
};
470545

471-
let composefs_info = ComposefsCmdline::<Sha512HashValue>::from_cmdline(&cmdline)
472-
.context("Failed to parse composefs cmdline")?
473-
.ok_or_else(|| anyhow::anyhow!("No composefs image in cmdline"))?;
474-
475-
let new_root = match &args.root_fs {
476-
Some(path) => open_root_fs(path).context("Failed to clone specified root fs")?,
477-
None => mount_composefs_image(
478-
&sysroot,
479-
&composefs_info.digest().to_hex(),
480-
composefs_info.is_insecure(),
481-
)?,
546+
let candidates =
547+
parse_composefs_candidates(&cmdline).context("Failed to parse composefs cmdline")?;
548+
if candidates.is_empty() {
549+
anyhow::bail!("No composefs image in cmdline");
550+
}
551+
let allow_missing_fsverity = candidates[0].is_insecure();
552+
if candidates
553+
.iter()
554+
.any(|candidate| candidate.is_insecure() != allow_missing_fsverity)
555+
{
556+
anyhow::bail!("composefs candidates have mixed fs-verity policy");
557+
}
558+
559+
let (new_root, composefs_info) = match &args.root_fs {
560+
Some(path) => (
561+
open_root_fs(path).context("Failed to clone specified root fs")?,
562+
candidates[0].clone(),
563+
),
564+
None => select_composefs_candidate(&candidates, |candidate| {
565+
mount_composefs_candidate(&sysroot, candidate, allow_missing_fsverity)
566+
})?,
482567
};
483568

484569
// we need to clone this before the next step to make sure we get the old one
@@ -633,4 +718,53 @@ mod tests {
633718
assert_eq!(config.root.transient, true);
634719
assert_eq!(config.etc.mount, Some(MountType::None));
635720
}
721+
722+
fn v1(digest: Sha512HashValue, insecure: bool) -> String {
723+
ComposefsCmdline::new_v1(digest, insecure).to_cmdline_arg()
724+
}
725+
726+
fn v2(digest: Sha512HashValue, insecure: bool) -> String {
727+
ComposefsCmdline::new_v2(digest, insecure).to_cmdline_arg()
728+
}
729+
730+
#[test]
731+
fn test_composefs_candidate_priority_and_selected_state() {
732+
let v1_digest = Sha512HashValue::EMPTY;
733+
let v2_digest = Sha512HashValue::from_hex("aa".repeat(64)).unwrap();
734+
let cmdline = format!(
735+
"quiet {} {}",
736+
v1(v1_digest.clone(), false),
737+
v2(v2_digest.clone(), false)
738+
);
739+
let candidates = parse_composefs_candidates(&cmdline).unwrap();
740+
let (selected, selected_cmdline) = select_composefs_candidate(&candidates, |candidate| {
741+
Ok((candidate.digest() == &v2_digest).then_some(candidate.digest().to_hex()))
742+
})
743+
.unwrap();
744+
assert_eq!(selected, v2_digest.to_hex());
745+
assert_eq!(selected_cmdline.digest(), &v2_digest);
746+
747+
let (selected, _) = select_composefs_candidate(&candidates, |candidate| {
748+
Ok(Some(candidate.digest().to_hex()))
749+
})
750+
.unwrap();
751+
assert_eq!(selected, v1_digest.to_hex());
752+
}
753+
754+
#[test]
755+
fn test_composefs_candidate_parse_and_policy_errors() {
756+
assert!(parse_composefs_candidates("composefs=not-a-digest").is_err());
757+
assert!(parse_composefs_candidates("composefs.digest=v9-sha512-12:aa").is_err());
758+
759+
let strict = parse_composefs_candidates(&v1(Sha512HashValue::EMPTY, false)).unwrap();
760+
let insecure = parse_composefs_candidates(&v2(Sha512HashValue::EMPTY, true)).unwrap();
761+
assert_ne!(strict[0].is_insecure(), insecure[0].is_insecure());
762+
}
763+
764+
#[test]
765+
fn test_image_verity_policy_is_per_image() {
766+
assert!(validate_image_verity(true, false).is_ok());
767+
assert!(validate_image_verity(false, false).is_err());
768+
assert!(validate_image_verity(false, true).is_ok());
769+
}
636770
}

0 commit comments

Comments
 (0)