Skip to content
Merged
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
17 changes: 17 additions & 0 deletions crates/kiri-init/src/linux/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,19 @@ pub(super) fn valid_id(id: &str) -> io::Result<()> {
Ok(())
}

/// Creates `cwd` and its parents, 0755, when it is missing. Errors are left
/// to the chdir after the user switch, which reports them.
fn make_workdir(cwd: &std::ffi::CStr) {
// SAFETY: a valid NUL-terminated path.
if unsafe { libc::access(cwd.as_ptr(), libc::F_OK) } == 0 {
return;
}
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(cwd.to_bytes()) };
let _ = fs::DirBuilder::new().recursive(true).mode(0o755).create(dir);
}

fn prepare(argv: StrList<'_>, env: StrList<'_>, cwd: &str, user: &str, hostname: Option<&str>, flags: u32, caps: u64) -> io::Result<Prepared> {
let c = |s: &str| CString::new(s).map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "argument contains NUL"));
let argv = argv.iter().map(c).collect::<io::Result<Vec<_>>>()?;
Expand Down Expand Up @@ -1229,6 +1242,10 @@ fn container_child(p: &Prepared, rootfs: &CStr, io: &ChildStdio) -> ! {
libc::umount2(c".".as_ptr(), libc::MNT_DETACH);
libc::chdir(c"/".as_ptr());
}
// Docker creates a missing working directory (mkdir -p) as root, 0755,
// before it switches user and before the root goes read-only, so both
// `-u 1000 -w /missing` and `--read-only -w /missing` start (#370).
make_workdir(&p.cwd);
if p.readonly_rootfs {
// Only the root itself: volumes, tmpfs and /dev stay as they are.
sys::mount(c"none", c"/", c"none", libc::MS_REMOUNT | libc::MS_BIND | libc::MS_RDONLY, None).unwrap_or_else(|e| fail("read-only root", e));
Expand Down
4 changes: 3 additions & 1 deletion crates/kiri/src/daemon/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,9 @@ pub(super) fn reference_matches(pattern: &str, name: &str) -> bool {

fn images(req: &Request, engine: &Engine) -> Result<Value, Error> {
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!([]));
Expand Down
8 changes: 8 additions & 0 deletions tests/e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -130,6 +132,12 @@ 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
# ... and with a read-only root, made before the root goes read-only, as in Docker.
t workdir-created-readonly "/made/ro" $D run --rm --read-only -w /made/ro alpine pwd
t workdir-created-readonly-nonroot "/made/ro/1000" $D run --rm --read-only -u 1000:1000 -w /made/ro/1000 alpine pwd
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
Expand Down