Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ RUN --mount=type=tmpfs,target=/run --mount=type=tmpfs,target=/tmp <<EOF
if [[ "${boot_type}" == "uki" ]]; then
/target-rootfs/usr/bin/bootc container split-kernel-and-rootfs --rootfs /target-rootfs --output /kernel
fi

# TODO: drop once https://src.fedoraproject.org/rpms/filesystem/pull-request/44 or equivalent
# is widely available. For now, manually ensure that /usr has the correct permissions.
# The COPY in the fetch stage below is the layer which creates /usr in the
# final image, so its mode here is applied at directory creation and
# survives the ostree union checkout, which only applies the metadata of
# directories it creates. This also runs before the UKI is sealed in the
# sealed-uki stage, so the embedded composefs digest matches this rootfs.
chmod 0555 /target-rootfs/usr
EOF

FROM scratch as fetch
Expand Down
16 changes: 9 additions & 7 deletions crates/initramfs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,18 +270,17 @@ fn mount_tmpfs_for_overlay() -> Result<OwnedFd> {
}

/// 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: <https://github.com/composefs/composefs-rs/issues/287>
fn build_overlay_fd(
base: impl AsFd,
state: impl AsFd,
source: &str,
upper_mode: rustix::fs::Mode,
mount_attr_flags: Option<MountAttrFlags>,
) -> Result<OwnedFd> {
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")?;
Expand All @@ -306,7 +305,7 @@ fn overlay_state(
source: &str,
mount_attr_flags: Option<MountAttrFlags>,
) -> 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")
}

Expand All @@ -333,7 +332,10 @@ pub fn overlay_transient(
) -> Result<OwnedFd> {
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")]
Expand Down
65 changes: 64 additions & 1 deletion crates/ostree-ext/src/container/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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")?;
Comment on lines +665 to +666
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:
/// - <https://github.com/coreos/fedora-coreos-tracker/issues/2238>
/// - <https://github.com/coreos/fedora-coreos-tracker/issues/2238#issuecomment-6082074286>
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";
Expand Down Expand Up @@ -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(),
Expand Down
27 changes: 27 additions & 0 deletions crates/ostree-ext/src/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down
42 changes: 42 additions & 0 deletions crates/ostree-ext/tests/it/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,40 @@ fn assert_err_contains<T>(r: Result<T>, s: impl AsRef<str>) {
}
}

/// 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::<ostree::RepoFile>().unwrap();
root.ensure_resolved().unwrap();

let usr = root.resolve_relative_path("usr");
let usr = usr.downcast::<ostree::RepoFile>().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<String> =
LazyLock::new(|| match std::env::var_os("TEST_REGISTRY") {
Some(t) => t.to_str().unwrap().to_owned(),
Expand Down Expand Up @@ -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(())
}

Expand Down Expand Up @@ -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(())
}

Expand Down
8 changes: 0 additions & 8 deletions tmt/tests/booted/readonly/040-test-baseconfigs.nu
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
29 changes: 29 additions & 0 deletions tmt/tests/booted/readonly/055-test-root-dirmeta.nu
Original file line number Diff line number Diff line change
@@ -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
Loading