Skip to content

Commit 2a59b59

Browse files
Rolv-Apnesethcgwalters
authored andcommitted
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 <rolv.apneseth@gmail.com> [1]: composefs/composefs-rs#287
1 parent a417a15 commit 2a59b59

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

‎crates/initramfs/src/lib.rs‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -270,18 +270,17 @@ fn mount_tmpfs_for_overlay() -> Result<OwnedFd> {
270270
}
271271

272272
/// Build an overlayfs fsmount fd from an existing state dir (upper+work).
273-
///
274-
/// upper is 0755: the merged view inherits permissions from upperdir, so 0700
275-
/// would make the mountpoint inaccessible to non-root processes. work is
276-
/// kernel-internal and never visible; 0700 is fine.
273+
/// The merged view takes its dir metadata from the upper, hence the explicit
274+
/// upper mode. The work dir is kernel-internal; 0700 is fine.
277275
/// See: <https://github.com/composefs/composefs-rs/issues/287>
278276
fn build_overlay_fd(
279277
base: impl AsFd,
280278
state: impl AsFd,
281279
source: &str,
280+
upper_mode: rustix::fs::Mode,
282281
mount_attr_flags: Option<MountAttrFlags>,
283282
) -> Result<OwnedFd> {
284-
let upper = ensure_dir(state.as_fd(), "upper", Some(0o755.into()))?;
283+
let upper = ensure_dir(state.as_fd(), "upper", Some(upper_mode))?;
285284
let work = ensure_dir(state.as_fd(), "work", Some(0o700.into()))?;
286285

287286
let overlayfs = FsHandle::open("overlay")?;
@@ -306,7 +305,7 @@ fn overlay_state(
306305
source: &str,
307306
mount_attr_flags: Option<MountAttrFlags>,
308307
) -> Result<()> {
309-
let fs = build_overlay_fd(&base, state, source, mount_attr_flags)?;
308+
let fs = build_overlay_fd(&base, state, source, 0o755.into(), mount_attr_flags)?;
310309
mount_at_wrapper(fs, base, ".").context("Moving mount")
311310
}
312311

@@ -333,7 +332,10 @@ pub fn overlay_transient(
333332
) -> Result<OwnedFd> {
334333
let tmpfs = mount_tmpfs_for_overlay()?;
335334
let state = prepare_mount(tmpfs)?;
336-
build_overlay_fd(base, state, source, mount_attr_flags)
335+
// The merged dir metadata comes from the upper, so match the lower root
336+
let stat = rustix::fs::fstat(base.as_fd())?;
337+
let upper_mode = rustix::fs::Mode::from_raw_mode(stat.st_mode);
338+
build_overlay_fd(base, state, source, upper_mode, mount_attr_flags)
337339
}
338340

339341
#[context("Opening rootfs")]

0 commit comments

Comments
 (0)