From 8e4aeeb98261746d2972359a7e59c566a2a566e1 Mon Sep 17 00:00:00 2001 From: Precious Okwu Date: Mon, 16 Nov 2026 17:21:24 +0100 Subject: [PATCH] Engine: images list newest first, and a non-root container gets its missing working directory --- crates/kiri-init/src/linux/container.rs | 12 ++++++++++++ crates/kiri/src/daemon/api.rs | 4 +++- tests/e2e.sh | 5 +++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/crates/kiri-init/src/linux/container.rs b/crates/kiri-init/src/linux/container.rs index 8800dc00..2f70f36a 100644 --- a/crates/kiri-init/src/linux/container.rs +++ b/crates/kiri-init/src/linux/container.rs @@ -1366,6 +1366,18 @@ fn finish_child(p: &Prepared, io: &ChildStdio, console: bool) -> ! { } .unwrap_or_else(|e| fail("seccomp", e)); let (uid, gid) = resolve_user(&p.user).unwrap_or_else(|e| fail("user", e)); + // Docker creates a missing working directory (mkdir -p) as root, 0755, + // before it switches user, so a non-root container can still enter it + // (#370). A failure shows up as the chdir's error below. + if uid != 0 { + // SAFETY: `p.cwd` is a NUL-terminated path we own. + if unsafe { libc::access(p.cwd.as_ptr(), libc::F_OK) } != 0 { + use std::os::unix::fs::DirBuilderExt; + // SAFETY: the bytes came from a &str, so they are valid UTF-8. + let dir = unsafe { std::ffi::OsStr::from_encoded_bytes_unchecked(p.cwd.as_bytes()) }; + let _ = fs::DirBuilder::new().recursive(true).mode(0o755).create(dir); + } + } // The user's /etc/group memberships and --group-add, as Docker gives them. let groups = super::hostopts::supplementary_groups(&p.user, uid, &p.host.groups).unwrap_or_else(|e| fail("groups", e)); give_stdio([0, 1, 2], uid).unwrap_or_else(|e| fail("stdio", e)); diff --git a/crates/kiri/src/daemon/api.rs b/crates/kiri/src/daemon/api.rs index c23e0cfd..58c1fe9f 100644 --- a/crates/kiri/src/daemon/api.rs +++ b/crates/kiri/src/daemon/api.rs @@ -473,7 +473,9 @@ pub(super) fn reference_matches(pattern: &str, name: &str) -> bool { fn images(req: &Request, engine: &Engine) -> Result { let f = filters(req, &["reference", "dangling", "label"])?; - let list = engine.store.images()?; + let mut list = engine.store.images()?; + // Newest first, as Docker lists them (#364). + list.sort_by_key(|i| std::cmp::Reverse(time::parse_unix(&i.created))); // Every stored image is tagged, so there are no dangling ones. if f.bool("dangling") == Some(true) { return Ok(json!([])); diff --git a/tests/e2e.sh b/tests/e2e.sh index 92605573..55754f8d 100755 --- a/tests/e2e.sh +++ b/tests/e2e.sh @@ -95,6 +95,8 @@ t pull-alpine "alpine:latest" $D pull alpine:latest t pull-busybox "busybox" $D pull busybox t pull-tag "3.21" $D pull alpine:3.21 t images-list "alpine" $D images +# Newest first, as Docker lists them (#364). +t images-newest-first "sorted" bash -c "$D images --format '{{.CreatedAt}}' | sort -rc && echo sorted" t image-inspect-arch "$($D version -f '{{.Server.Arch}}')" $D image inspect -f '{{.Architecture}}' alpine t image-inspect-cmd "/bin/sh" $D image inspect -f '{{.Config.Cmd}}' alpine code pull-missing 1 $D pull kiri-does-not-exist/nope:none @@ -130,6 +132,9 @@ t env "bar" $D run --rm -e FOO=bar alpine sh -c 'echo $FOO' t env-image "PATH=/usr/local/sbin" $D run --rm alpine env t workdir "/tmp" $D run --rm -w /tmp alpine pwd t workdir-created "/made/here" $D run --rm -w /made/here alpine pwd +# A non-root user gets the missing directory too, made by root as 0755 (#370). +t workdir-created-nonroot "/made/for/1000" $D run --rm -u 1000:1000 -w /made/for/1000 alpine pwd +t workdir-created-nonroot-mode "755 root" $D run --rm -u 1000:1000 -w /made/x alpine stat -c '%a %U' /made/x t user-numeric "uid=1000" $D run --rm -u 1000 alpine id t user-name "uid=70(postgres)" $D run --rm -u postgres postgres:17-alpine id t user-group "gid=0(root)" $D run --rm -u 1000:0 alpine id