A small, fast container runtime for macOS, Windows and Linux
Rust
Containers can listen on any port and ping without root, as on Docker (#19)
Precious Okwu4c3aaefdd502
diff --git a/crates/kiri-init/src/linux/container.rs b/crates/kiri-init/src/linux/container.rsindex 86cfc6a..26ccaa9 100644--- a/crates/kiri-init/src/linux/container.rs+++ b/crates/kiri-init/src/linux/container.rs@@ -222,7 +222,7 @@ pub fn spawn(spec: &RunSpec<'_>, paths: &Paths, net: NetCtx<'_>) -> io::Result<Psuper::cgroup::apply_limits(&cgroup, &spec.limits).map_err(cleanup)?;}let cgroup_fd = fs::File::open(&cgroup).map_err(cleanup)?;- let host = super::hostopts::parse(spec.opts).map_err(cleanup)?;+ let mut host = super::hostopts::parse(spec.opts).map_err(cleanup)?;if spec.flags & run_flags::NET_HOST != 0 && host.sysctls.iter().any(|s| s.ns == super::hostopts::Ns::Net) {return Err(cleanup(io::Error::new(io::ErrorKind::InvalidInput, "net.* sysctls are not allowed in the host network namespace")));}@@ -261,6 +261,12 @@ pub fn spawn(spec: &RunSpec<'_>, paths: &Paths, net: NetCtx<'_>) -> io::Result<Plet nameserver = embedded_dns.then(|| embedded_address(spec, net.dns.relay));prepared.etc_files = write_etc_files(spec, &etc_dir, net.upstream_dns, nameserver).map_err(cleanup)?;prepared.mounts = parse_mounts(spec.mounts).map_err(cleanup)?;+ // A network namespace of its own gets Docker's two defaults: any port+ // and ping without root. Host networking, and `--network container:`+ // (that container already has them), keep what they have.+ if spec.flags & run_flags::NET_HOST == 0 && prepared.net_join.is_none() {+ super::hostopts::add_net_defaults(&mut host.sysctls);+ }let bridged = !spec.net.bridge.is_empty() && spec.flags & (run_flags::NET_HOST | run_flags::NET_NONE) == 0 && prepared.net_join.is_none();let mut go = None;// Sysctls are written from here once the namespaces exist: the childdiff --git a/crates/kiri-init/src/linux/hostopts.rs b/crates/kiri-init/src/linux/hostopts.rsindex 8d71193..4857a4a 100644--- a/crates/kiri-init/src/linux/hostopts.rs+++ b/crates/kiri-init/src/linux/hostopts.rs@@ -183,6 +183,23 @@ pub fn make_nodes(root: BorrowedFd<'_>, nodes: &[Node]) -> io::Result<()> {Ok(())}+/// What Docker writes in every container with a network namespace of its+/// own: a non-root process may listen on any port (nginx or Caddy on 80 as+/// a user) and ping without CAP_NET_RAW. A `--sysctl` for the same key+/// wins, as it does on Docker.+const NET_DEFAULTS: [(&str, &str); 2] = [("net/ipv4/ip_unprivileged_port_start", "0"), ("net/ipv4/ping_group_range", "0 2147483647")];++/// Puts Docker's defaults first in `sysctls`, leaving out any key the+/// container sets itself.+pub fn add_net_defaults(sysctls: &mut Vec<Sysctl>) {+ let defaults = NET_DEFAULTS+ .iter()+ .filter(|(path, _)| !sysctls.iter().any(|s| s.path == *path))+ .map(|(path, value)| Sysctl { ns: Ns::Net, path: (*path).to_owned(), value: (*value).to_owned() });+ let mine: Vec<Sysctl> = defaults.collect();+ sysctls.splice(0..0, mine);+}+/// Writes the sysctls from the agent, inside the container's namespaces:/// /proc/sys shows the calling task's network, IPC and UTS namespace, so/// the agent joins the one each key belongs to, writes, and comes back.@@ -302,6 +319,18 @@ mod tests {buf}+ #[test]+ fn a_container_gets_dockers_port_and_ping_defaults_unless_it_sets_them() {+ let mut none = Vec::new();+ add_net_defaults(&mut none);+ let keys: Vec<(&str, &str)> = none.iter().map(|s| (s.path.as_str(), s.value.as_str())).collect();+ assert_eq!(keys, [("net/ipv4/ip_unprivileged_port_start", "0"), ("net/ipv4/ping_group_range", "0 2147483647")]);+ let mut own = vec![Sysctl { ns: Ns::Net, path: "net/ipv4/ip_unprivileged_port_start".into(), value: "1024".into() }];+ add_net_defaults(&mut own);+ let keys: Vec<(&str, &str)> = own.iter().map(|s| (s.path.as_str(), s.value.as_str())).collect();+ assert_eq!(keys, [("net/ipv4/ping_group_range", "0 2147483647"), ("net/ipv4/ip_unprivileged_port_start", "1024")], "--sysctl wins");+ }+#[test]fn parses_and_refuses() {let buf = list(&["rlimit\t7\t65536\t65536", "sysctl\tnet\tnet/core/somaxconn\t1024", "device\t/dev/fuse\t/dev/fuse\trwm", "devrule\tc\t10\t229\trwm", "group\tvideo"]);diff --git a/tests/e2e.sh b/tests/e2e.shindex 9781fb8..e8aa293 100755--- a/tests/e2e.sh+++ b/tests/e2e.sh@@ -661,6 +661,13 @@ t native-again "aarch64" bash -c "$D pull -q --platform linux/arm64 alpine >/dev$D rmi amd64/alpine:3.22 >/dev/null 2>&1section "security: seccomp, capabilities, read-only"+# Docker's two defaults in a container's own network: any port and ping+# without root. --sysctl wins; host networking keeps the VM's own.+t unpriv-port-start "0" $D run --rm alpine cat /proc/sys/net/ipv4/ip_unprivileged_port_start+t unpriv-ping-range "0 2147483647" $D run --rm alpine cat /proc/sys/net/ipv4/ping_group_range+t unpriv-listen-80 "listening" $D run --rm -u 1000 alpine sh -c 'nc -lk -p 80 >/dev/null 2>&1 & sleep 0.5; netstat -ltn | grep -q ":80 " && echo listening'+t unpriv-sysctl-wins "1024" $D run --rm --sysctl net.ipv4.ip_unprivileged_port_start=1024 alpine cat /proc/sys/net/ipv4/ip_unprivileged_port_start+t unpriv-port-host-net "1024" $D run --rm --network host alpine cat /proc/sys/net/ipv4/ip_unprivileged_port_startt seccomp-on "Seccomp: 2" $D run --rm alpine grep Seccomp: /proc/self/statust seccomp-userns "not permitted" $D run --rm alpine unshare -U truet seccomp-keyctl "not permitted" bash -c "$D run --rm alpine sh -c 'apk add -q keyutils >/dev/null 2>&1; keyctl show 2>&1'"