diff --git a/crates/kiri-init/src/linux/container.rs b/crates/kiri-init/src/linux/container.rs index 0dc67695..1972305c 100644 --- a/crates/kiri-init/src/linux/container.rs +++ b/crates/kiri-init/src/linux/container.rs @@ -129,6 +129,10 @@ struct Prepared { init: bool, /// Ulimits, groups and device nodes from the `opts` lines. host: HostApplied, + /// The child's end of the pipe that says its root is in place (mounts + /// made, `pivot_root` done), so a start is not reported before an exec + /// can join its mount namespace (#311). + root_ready: Option, } /// The parts of [`super::hostopts::HostOpts`] the child applies itself. @@ -298,6 +302,11 @@ pub fn spawn(spec: &RunSpec<'_>, paths: &Paths, net: NetCtx<'_>) -> io::Result

, paths: &Paths, net: NetCtx<'_>) -> io::Result

, paths: &Paths, net: NetCtx<'_>) -> io::Result

e, } }; + // Do not report the start until the child's root is in place: before + // `pivot_root`, an exec joining its mount namespace gets the agent's + // `/etc/passwd`, and an image user such as `dev` is unknown (#311). + let mut ready = [0u8; 1]; + let got = loop { + // SAFETY: reading one byte into a local buffer from a pipe we own. + let n = unsafe { libc::read(root_ready.as_raw_fd(), ready.as_mut_ptr().cast(), 1) }; + if n >= 0 { + break n; + } + let e = io::Error::last_os_error(); + if e.kind() != io::ErrorKind::Interrupted { + return Err(kill_child(e)); + } + }; + if got != 1 { + return Err(kill_child(io::Error::other("the container exited before its root was ready"))); + } let mut veths = Vec::new(); // Not bridged, but with sysctls to write: no network to wait for. if let Some(go) = go.take_if(|_| !bridged) { @@ -771,6 +801,7 @@ fn prepare(argv: StrList<'_>, env: StrList<'_>, cwd: &str, user: &str, hostname: shm_size: 64 << 20, init: flags & run_flags::INIT != 0, host: HostApplied::default(), + root_ready: None, }) } @@ -1164,6 +1195,13 @@ fn container_child(p: &Prepared, rootfs: &CStr, io: &ChildStdio) -> ! { if p.net_none { sys::loopback_up().unwrap_or_else(|e| fail("loopback", e)); } + if let Some(done) = &p.root_ready { + // SAFETY: one byte to a pipe we own: root, mounts and namespaces + // are in place, so an exec can join them now. + if unsafe { libc::write(done.as_raw_fd(), b"r".as_ptr().cast(), 1) } != 1 { + fail("ready", io::Error::last_os_error()); + } + } if let Some(ready) = &p.net_ready { let mut b = [0u8; 1]; // SAFETY: reading one byte into a local buffer. diff --git a/tests/e2e.sh b/tests/e2e.sh index 46888f9c..9e7a19bc 100755 --- a/tests/e2e.sh +++ b/tests/e2e.sh @@ -565,6 +565,10 @@ t vol-copyup-owner "same:redis:redis 755" bash -c "i=\$($D run --rm redis:7-alpi t vol-copyup-tree "same" bash -c "i=\$($D run --rm alpine sh -c 'cd /etc/apk && { find . -exec stat -c \"%n %A %u %g\" {} +; find . -type f -exec md5sum {} +; } | sort | md5sum'); v=\$($D run --rm -v tapk:/etc/apk alpine sh -c 'cd /etc/apk && { find . -exec stat -c \"%n %A %u %g\" {} +; find . -type f -exec md5sum {} +; } | sort | md5sum'); [ \"\$i\" = \"\$v\" ] && echo same" t vol-copyup-not-over "1 mine" bash -c "$D run --rm -v tkeep:/usr/share/nginx/html nginx:alpine sh -c 'rm /usr/share/nginx/html/*; echo mine > /usr/share/nginx/html/mine' && $D run --rm -v tkeep:/usr/share/nginx/html nginx:alpine sh -c 'echo \$(ls /usr/share/nginx/html | wc -l) \$(cat /usr/share/nginx/html/mine)'" t redis-volume "PONG OK running" bash -c "$D run -d --name rv -v tredis:/data redis:7-alpine >/dev/null; for i in \$(seq 50); do $D exec rv redis-cli ping 2>/dev/null | grep -q PONG && break; sleep 0.2; done; p=\$($D exec rv redis-cli ping); s=\$($D exec rv redis-cli save); sleep 1; echo \$p \$s \$($D inspect -f '{{.State.Status}}' rv)" +# A fresh named volume is filled from the image while the container starts. +# `docker run -d` must not return before the container's root is in place, or +# an immediate exec resolves users against the host's /etc/passwd (#311). +t volume-user-ready "passed passed passed" bash -c "$D run --name ready-seed alpine sh -c 'adduser -D dev && mkdir /payload && dd if=/dev/zero of=/payload/blob bs=1M count=64' >/dev/null && $D commit -c 'USER dev' ready-seed kiri-test/ready:1 >/dev/null && $D rm -f ready-seed >/dev/null; for i in 1 2 3; do v=ready-vol-\$i; c=\$($D run -d --mount type=volume,source=\$v,target=/payload kiri-test/ready:1 sleep 60); if $D exec --user dev \$c id >/dev/null 2>&1; then printf 'passed '; else printf 'failed '; fi; $D rm -f \$c >/dev/null; $D volume rm \$v >/dev/null 2>&1; done; $D rmi kiri-test/ready:1 >/dev/null; echo" # A volume over part of a bind (`./:/app` and `/app/node_modules`) is filled # from the image, as Docker does, never from the host's folder under it. t vol-copyup-under-bind "index.html" bash -c "h=\$(mktemp -d); mkdir \$h/html; echo mac > \$h/html/from-host.txt; $D run --rm -v \$h:/usr/share/nginx -v /usr/share/nginx/html nginx:alpine sh -c 'cd /usr/share/nginx/html; [ -e from-host.txt ] && echo from the host || ls'; rm -rf \$h" diff --git a/tests/linux-host.sh b/tests/linux-host.sh index 5cc94e80..d07494fc 100755 --- a/tests/linux-host.sh +++ b/tests/linux-host.sh @@ -75,6 +75,10 @@ t stats "web" "docker stats --no-stream --format '{{.Name}}'" t network-dns "PONG" "docker network create lhn >/dev/null 2>&1; docker run -d --name db --network lhn redis:7-alpine >/dev/null; sleep 1; docker run --rm --network lhn redis:7-alpine redis-cli -h db ping" t bind-host-path "3." "docker run --rm -v /etc:/h:ro alpine cat /h/alpine-release" t volume "kept" "docker run --rm -v lhv:/d alpine sh -c 'echo kept > /d/f' && docker run --rm -v lhv:/d alpine cat /d/f" +# A fresh named volume is filled from the image while the container starts. +# `docker run -d` must not return before the container's root is in place, or +# an immediate exec resolves users against the host's /etc/passwd (#311). +t volume-user-ready "passed passed passed" "docker run --name ready-seed alpine sh -c 'adduser -D dev && mkdir /payload && dd if=/dev/zero of=/payload/blob bs=1M count=64' >/dev/null && docker commit -c 'USER dev' ready-seed kiri-test/ready:1 >/dev/null && docker rm -f ready-seed >/dev/null; for i in 1 2 3; do v=ready-vol-\$i; c=\$(docker run -d --mount type=volume,source=\$v,target=/payload kiri-test/ready:1 sleep 60); if docker exec --user dev \$c id >/dev/null 2>&1; then printf 'passed '; else printf 'failed '; fi; docker rm -f \$c >/dev/null; docker volume rm \$v >/dev/null 2>&1; done; docker rmi kiri-test/ready:1 >/dev/null; echo" t memory-limit "67108864" "docker run --rm --memory 64m alpine cat /sys/fs/cgroup/memory.max" t seccomp "Seccomp: 2" "docker run --rm alpine grep Seccomp: /proc/self/status" t udp "10.9.8.7" "docker run -d --name dns -p 15354:53/udp alpine sh -c 'apk add -q dnsmasq >/dev/null 2>&1; dnsmasq -k --address=/kiri.test/10.9.8.7' >/dev/null; for i in \$(seq 40); do out=\$(nslookup -port=15354 kiri.test 127.0.0.1 2>/dev/null) && break; sleep 0.5; done; echo \"\$out\" | grep -o 10.9.8.7" diff --git a/tests/wsl.sh b/tests/wsl.sh index 01df8e47..0265e52d 100755 --- a/tests/wsl.sh +++ b/tests/wsl.sh @@ -55,6 +55,10 @@ t cp-then-start "failed 0" "f=0; for i in 1 2 3 4 5; do id=\$(docker create alpi t commit-renamed-dir "kept" "docker rm -f kiri-rename >/dev/null 2>&1; docker run --name kiri-rename alpine mv /etc/apk /etc/apk2; docker commit kiri-rename kiri-rename-test >/dev/null; docker rm -f kiri-rename >/dev/null; docker run --rm kiri-rename-test sh -c 'test -f /etc/apk2/repositories && echo kept'; docker rmi kiri-rename-test >/dev/null" t network-dns "PONG" "docker network create wn >/dev/null 2>&1; docker run -d --name db --network wn redis:7-alpine >/dev/null; sleep 2; docker run --rm --network wn redis:7-alpine redis-cli -h db ping" t volume "kept" "docker run --rm -v wv:/d alpine sh -c 'echo kept > /d/f' && docker run --rm -v wv:/d alpine cat /d/f" +# A fresh named volume is filled from the image while the container starts. +# `docker run -d` must not return before the container's root is in place, or +# an immediate exec resolves users against the host's /etc/passwd (#311). +t volume-user-ready "passed passed passed" "docker run --name ready-seed alpine sh -c 'adduser -D dev && mkdir /payload && dd if=/dev/zero of=/payload/blob bs=1M count=64' >/dev/null && docker commit -c 'USER dev' ready-seed kiri-test/ready:1 >/dev/null && docker rm -f ready-seed >/dev/null; for i in 1 2 3; do v=ready-vol-\$i; c=\$(docker run -d --mount type=volume,source=\$v,target=/payload kiri-test/ready:1 sleep 60); if docker exec --user dev \$c id >/dev/null 2>&1; then printf 'passed '; else printf 'failed '; fi; docker rm -f \$c >/dev/null; docker volume rm \$v >/dev/null 2>&1; done; docker rmi kiri-test/ready:1 >/dev/null; echo" t seccomp "Seccomp: 2" "docker run --rm alpine grep Seccomp: /proc/self/status" t cleanup "web" "docker rm -f web db" # The agent (kiri-init under the daemon) dies with nobody asking (#50):