From 52d9227a4843adc260b801da6472d0a7ef95dd93 Mon Sep 17 00:00:00 2001 From: Rolv Apneseth Date: Wed, 28 Oct 2026 09:01:36 +0000 Subject: [PATCH 1/3] Dockerfile: chmod 0555 /usr This is a workaround until the filesystem package ships the correct permissions for /usr, to match the expected permissions for root. The chmod must be in the layer that creates /usr in the final image (the fetch stage COPY), because the union checkout in the merge drops metadata-only changes from later layers. Signed-off-by: Rolv Apneseth --- Dockerfile | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Dockerfile b/Dockerfile index 9a1b81f75..1361e8791 100644 --- a/Dockerfile +++ b/Dockerfile @@ -108,6 +108,15 @@ RUN --mount=type=tmpfs,target=/run --mount=type=tmpfs,target=/tmp < Date: Wed, 28 Oct 2026 10:31:19 +0000 Subject: [PATCH 2/3] ostree-ext: preserve directory metadata from OCI layers When building a derived image from OCI layers, the ostree `UnionFiles` checkout preserves directory metadata (mode, uid, gid) from the first layer that created the directory. This means later layers that only modify metadata (e.g. `RUN chmod 0555 /usr`) are silently lost. OCI semantics and composefs-rs require last-layer-wins, but this should be handled by [3]. For root specifically, copy the mode, uid and gid from /usr (matching the approach by composefs-rs [1]) since OCI images don't reliably include root directory metadata, and container runtimes disagree on how to apply it when present. Unlike composefs-rs, mtime and xattrs are not copied. ostree dirmeta has no mtime field, and the SELinux relabelling pass assigns the root its own label. This should fix systemd-tmpfiles failures on read-only root systems with systemd v261+. See: [2] Assisted-by: llm Signed-off-by: Rolv Apneseth [1]: https://docs.rs/composefs/0.9.4/composefs/generic_tree/struct.FileSystem.html#method.copy_root_metadata_from_usr [2]: https://github.com/coreos/fedora-coreos-tracker/issues/2238 [3]: https://github.com/bootc-dev/bootc/pull/2236 --- crates/ostree-ext/src/container/store.rs | 65 ++++++++++++++++++- crates/ostree-ext/src/fixture.rs | 27 ++++++++ crates/ostree-ext/tests/it/main.rs | 42 ++++++++++++ .../booted/readonly/040-test-baseconfigs.nu | 8 --- .../booted/readonly/055-test-root-dirmeta.nu | 29 +++++++++ 5 files changed, 162 insertions(+), 9 deletions(-) create mode 100644 tmt/tests/booted/readonly/055-test-root-dirmeta.nu diff --git a/crates/ostree-ext/src/container/store.rs b/crates/ostree-ext/src/container/store.rs index 9316d8547..82ac78256 100644 --- a/crates/ostree-ext/src/container/store.rs +++ b/crates/ostree-ext/src/container/store.rs @@ -158,7 +158,8 @@ use oci_spec::image::{ }; use ocidir::oci_spec::distribution::Reference; use ostree::prelude::{Cast, FileEnumeratorExt, FileExt, ToVariant}; -use ostree::{gio, glib}; +use ostree::{DirMetaParsed, gio, glib}; +use rustix::fs::{Gid, Mode, OFlags, Uid}; use std::collections::{BTreeMap, BTreeSet, HashMap}; use std::fmt::Write as _; use std::iter::FromIterator; @@ -642,6 +643,62 @@ fn cleanup_root(root: &Dir) -> Result<()> { Ok(()) } +/// Apply directory mode and ownership from an ostree DirMeta to a filesystem +/// directory. +/// +/// We intentionally skip xattrs here since the SELinux relabelling pass in +/// `write_dfd_to_mtree` overwrites `security.selinux` on every directory, +/// and no other directory xattrs are meaningful in practice. +fn apply_dirmeta( + dir: impl rustix::fd::AsFd, + metadata: &ostree::DirMetaParsed, + should_fchown: bool, +) -> Result<()> { + if should_fchown { + rustix::fs::fchown( + &dir, + Some(Uid::from_raw(metadata.uid)), + Some(Gid::from_raw(metadata.gid)), + ) + .context("fchown")?; + } + let mode = Mode::from_raw_mode(metadata.mode); + rustix::fs::fchmod(&dir, mode).context("fchmod")?; + Ok(()) +} + +/// Copy the `/usr` directory mode and ownership to the root, like composefs-rs's +/// `copy_root_metadata_from_usr`. OCI images don't reliably include root dir +/// metadata, so layer root metadata (including an explicit `.` entry) is +/// intentionally overridden. Must run before the merged tree is committed, so +/// the root still gets its own SELinux label (root_t). +/// +/// More generally, the union checkout only applies the metadata of +/// directories it creates, so a derived layer which only changes a +/// directory's mode or ownership (e.g. `RUN chmod 0555 /usr`) is silently +/// lost. This only covers the root, which is what deployed systems need. +/// Handling that in general is deferred to the composefs-based commit +/// synthesis (https://github.com/bootc-dev/bootc/pull/2236). +/// +/// See: +/// - +/// - +fn normalize_root_dirmeta(root: &Dir, should_fchown: bool) -> Result<()> { + let usr = root.symlink_metadata("usr").context("looking up /usr")?; + anyhow::ensure!(usr.is_dir(), "image has no /usr directory"); + // `root` may be an `O_PATH` fd, on which fchmod/fchown fail with EBADF. + let flags = OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC; + let rootfd = rustix::fs::openat(root, ".", flags, Mode::empty()).context("opening root")?; + + let metadata = DirMetaParsed { + uid: usr.uid(), + gid: usr.gid(), + mode: usr.mode(), + xattrs: vec![], + }; + apply_dirmeta(rootfd, &metadata, should_fchown).context("applying /usr metadata to root") +} + impl ImageImporter { /// The metadata key used in ostree commit metadata to serialize const CACHED_KEY_MANIFEST_DIGEST: &'static str = "ostree-ext.cached.manifest-digest"; @@ -1431,6 +1488,12 @@ impl ImageImporter { cleanup_root(&root_dir)?; + // Apply the layer uid/gid only for bare checkouts, where the + // checked-out files carry real ownership. + let should_fchown = checkout_mode == ostree::RepoCheckoutMode::None; + normalize_root_dirmeta(&root_dir, should_fchown) + .context("normalizing root dir metadata")?; + let mt = ostree::MutableTree::new(); repo.write_dfd_to_mtree( (*td).as_raw_fd(), diff --git a/crates/ostree-ext/src/fixture.rs b/crates/ostree-ext/src/fixture.rs index 7b8e024f6..aa53ee123 100644 --- a/crates/ostree-ext/src/fixture.rs +++ b/crates/ostree-ext/src/fixture.rs @@ -1013,6 +1013,33 @@ impl NonOstreeFixture { let bw = self.src_oci.create_gzip_layer(None)?; let mut bw = tar::Builder::new(bw); + // Emit explicit entries for every directory, like a real layer tar + // does. The tar import re-stamps the ancestors of any directory it + // has to synthesize to the default 0755, so the 0555 on /usr below + // would be lost when the files under it are imported. + let mut dirs = std::collections::BTreeSet::new(); + for def in FileDef::iter_from(CONTENTS_V0) { + let def = def.unwrap(); + let mut p: &Utf8Path = &def.path; + while let Some(parent) = p.parent() { + if !parent.as_str().is_empty() { + dirs.insert(parent.to_owned()); + } + p = parent; + } + } + for d in &dirs { + let mode = if d.as_str() == "usr" { 0o555 } else { 0o755 }; + FileDef { + uid: 0, + gid: 0, + mode, + path: Cow::Owned(d.clone()), + xattrs: Default::default(), + ty: FileDefType::Directory, + } + .append_tar(&mut bw)?; + } for def in FileDef::iter_from(CONTENTS_V0) { let def = def.unwrap(); def.append_tar(&mut bw)?; diff --git a/crates/ostree-ext/tests/it/main.rs b/crates/ostree-ext/tests/it/main.rs index 8f973d3c4..d82c1586c 100644 --- a/crates/ostree-ext/tests/it/main.rs +++ b/crates/ostree-ext/tests/it/main.rs @@ -60,6 +60,40 @@ fn assert_err_contains(r: Result, s: impl AsRef) { } } +/// Verify that a commit's root directory mode/uid/gid matches `/usr`. +/// +/// Returns the mode if successful. +fn assert_root_dirmeta_matches_usr(repo: &ostree::Repo, commit: &str) -> u32 { + let root = repo.read_commit(commit, gio::Cancellable::NONE).unwrap().0; + let root = root.downcast::().unwrap(); + root.ensure_resolved().unwrap(); + + let usr = root.resolve_relative_path("usr"); + let usr = usr.downcast::().unwrap(); + usr.ensure_resolved().unwrap(); + + let query = |f: &ostree::RepoFile| { + let info = f + .query_info( + "unix::mode,unix::uid,unix::gid", + gio::FileQueryInfoFlags::NOFOLLOW_SYMLINKS, + gio::Cancellable::NONE, + ) + .unwrap(); + ( + info.attribute_uint32("unix::mode"), + info.attribute_uint32("unix::uid"), + info.attribute_uint32("unix::gid"), + ) + }; + + let root_meta = query(&root); + let usr_meta = query(&usr); + assert_eq!(root_meta, usr_meta, "root dirmeta should match /usr"); + + root_meta.0 & 0o7777 +} + static TEST_REGISTRY: LazyLock = LazyLock::new(|| match std::env::var_os("TEST_REGISTRY") { Some(t) => t.to_str().unwrap().to_owned(), @@ -709,6 +743,7 @@ async fn test_export_as_container_nonderived() -> Result<()> { let reimported = fixture.must_import(&dest).await?; let reimport_ls = fixture::ostree_ls(fixture.destrepo(), &reimported.merge_commit).unwrap(); similar_asserts::assert_eq!(initimport_ls, reimport_ls); + assert_root_dirmeta_matches_usr(fixture.destrepo(), &reimported.merge_commit); Ok(()) } @@ -1653,6 +1688,13 @@ async fn test_non_ostree() -> Result<()> { similar_asserts::assert_eq!(&src_manifest, &dest_manifest); } assert_eq!(imp.manifest_digest, src_digest); + + let root_mode = assert_root_dirmeta_matches_usr(fixture.destrepo(), &imp.merge_commit); + assert_eq!( + root_mode, 0o555, + "root mode ({root_mode:#o}) matches /usr but is not 0o555" + ); + Ok(()) } diff --git a/tmt/tests/booted/readonly/040-test-baseconfigs.nu b/tmt/tests/booted/readonly/040-test-baseconfigs.nu index a8f6e510d..29ee0e3f1 100644 --- a/tmt/tests/booted/readonly/040-test-baseconfigs.nu +++ b/tmt/tests/booted/readonly/040-test-baseconfigs.nu @@ -21,10 +21,6 @@ for config in $configs { let fstype = $mnt.filesystems.0.fstype assert equal $fstype "overlay" "/etc should be mounted as overlay when etc-transient is active" - print "# Checking / has 755 permissions (overlay upper dir must not block traversal)" - let perms = (stat -c "%a" / | str trim) - assert equal $perms "755" "/ should have 755 permissions" - print "# Checking /etc is writable (transient overlay)" let result = (do { ^touch /etc/bootc-baseconfig-test } | complete) assert equal $result.exit_code 0 "/etc should be writable with etc-transient" @@ -36,10 +32,6 @@ for config in $configs { let fstype = $mnt.filesystems.0.fstype assert equal $fstype "overlay" "/ should be mounted as overlay when root-transient is active" - print "# Checking / has 755 permissions" - let perms = (stat -c "%a" / | str trim) - assert equal $perms "755" "/ should have 755 permissions" - print "# Checking / is writable (transient overlay)" let result = (do { ^touch /bootc-baseconfig-root-test } | complete) assert equal $result.exit_code 0 "/ should be writable with root-transient" diff --git a/tmt/tests/booted/readonly/055-test-root-dirmeta.nu b/tmt/tests/booted/readonly/055-test-root-dirmeta.nu new file mode 100644 index 000000000..376422d8a --- /dev/null +++ b/tmt/tests/booted/readonly/055-test-root-dirmeta.nu @@ -0,0 +1,29 @@ +# Verify that the root dir's uid, gid, and permissions match /usr. +# +# See: https://github.com/coreos/fedora-coreos-tracker/issues/2238 +use std assert +use tap.nu + +tap begin "verify root dir metadata matches /usr" + +let root_meta = (stat -c "mode: %a, gid: %g, uid: %u" / | str trim) +let usr_meta = (stat -c "mode: %a, gid: %g, uid: %u" /usr | str trim) + +print $"# root: ($root_meta)" +print $"# /usr: ($usr_meta)" + +assert equal $root_meta $usr_meta $"root dir metadata \(($root_meta)\) should match /usr \(($usr_meta)\)" + +# When this system was upgraded from a published base image (test-upgrade flow), +# the import ran with the older bootc from that image, which predates the +# directory metadata preservation. Both / and /usr end up 0755 there. +# Only assert the strict mode on systems imported by a bootc that has the fix. +# TODO: once published base images ship the metadata fix, compare +# /var/bootc-pre-upgrade-version against the fixed version instead (like 031 does). +if not ("/var/bootc-pre-upgrade-version" | path exists) { + assert equal $root_meta "mode: 555, gid: 0, uid: 0" $"root dir metadata \(($root_meta)\) should match expected \(mode: 555, gid: 0, uid: 0\)" +} else { + print "# skipping strict 0555 check: imported by pre-fix bootc during upgrade" +} + +tap ok From 871b536f10b8f1a474ed03be82499ac7e1e5d349 Mon Sep 17 00:00:00 2001 From: Rolv Apneseth Date: Fri, 30 Oct 2026 17:11:58 +0000 Subject: [PATCH 3/3] initramfs: match overlay upper dir mode to lower root fs overlayfs takes the merged directory metadata from the upper dir, not the lower [1]. Since we were creating the upper with a hardcoded 0755, every transient overlay showed 0755 for the merged directory, hiding whatever mode the image committed. This matters most for the merged root (root-transient) and /usr overlay. With the previous commit in ostree-ext copying /usr metadata to the root commit, images can now ship / as 0555. The hardcoded 0755 upper masked that after the overlay. Create the upper dir with the lower root's mode so the merged view keeps the image permissions. Persistent overlays (/etc) keep the previous hard-coded 0755. This goes further than composefs-rs so that transient roots match the committed metadata. Assisted-by: llm Signed-off-by: Rolv Apneseth [1]: https://github.com/composefs/composefs-rs/issues/287 --- crates/initramfs/src/lib.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/crates/initramfs/src/lib.rs b/crates/initramfs/src/lib.rs index 7c9af14d7..9690314db 100644 --- a/crates/initramfs/src/lib.rs +++ b/crates/initramfs/src/lib.rs @@ -270,18 +270,17 @@ fn mount_tmpfs_for_overlay() -> Result { } /// Build an overlayfs fsmount fd from an existing state dir (upper+work). -/// -/// upper is 0755: the merged view inherits permissions from upperdir, so 0700 -/// would make the mountpoint inaccessible to non-root processes. work is -/// kernel-internal and never visible; 0700 is fine. +/// The merged view takes its dir metadata from the upper, hence the explicit +/// upper mode. The work dir is kernel-internal; 0700 is fine. /// See: fn build_overlay_fd( base: impl AsFd, state: impl AsFd, source: &str, + upper_mode: rustix::fs::Mode, mount_attr_flags: Option, ) -> Result { - let upper = ensure_dir(state.as_fd(), "upper", Some(0o755.into()))?; + let upper = ensure_dir(state.as_fd(), "upper", Some(upper_mode))?; let work = ensure_dir(state.as_fd(), "work", Some(0o700.into()))?; let overlayfs = FsHandle::open("overlay")?; @@ -306,7 +305,7 @@ fn overlay_state( source: &str, mount_attr_flags: Option, ) -> Result<()> { - let fs = build_overlay_fd(&base, state, source, mount_attr_flags)?; + let fs = build_overlay_fd(&base, state, source, 0o755.into(), mount_attr_flags)?; mount_at_wrapper(fs, base, ".").context("Moving mount") } @@ -333,7 +332,10 @@ pub fn overlay_transient( ) -> Result { let tmpfs = mount_tmpfs_for_overlay()?; let state = prepare_mount(tmpfs)?; - build_overlay_fd(base, state, source, mount_attr_flags) + // The merged dir metadata comes from the upper, so match the lower root + let stat = rustix::fs::fstat(base.as_fd())?; + let upper_mode = rustix::fs::Mode::from_raw_mode(stat.st_mode); + build_overlay_fd(base, state, source, upper_mode, mount_attr_flags) } #[context("Opening rootfs")]