From f9d92fc62d600a4e21f0a294aac64d33348118e9 Mon Sep 17 00:00:00 2001 From: Autumn Liu Date: Fri, 11 Sep 2026 14:43:43 -0500 Subject: [PATCH 1/3] update --- dsl.nix | 3 ++- tests/server-n0/integration.py | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/dsl.nix b/dsl.nix index 4745527..c07f613 100644 --- a/dsl.nix +++ b/dsl.nix @@ -295,6 +295,7 @@ rec { + lib.optionalString useIdmap ":idmap" ) allBindMounts; bindMountHostPaths = map (bm: bm.hostPath) (lib.attrValues allBindMounts); + deviceFlags = map (d: "--bind=${d}") devices; inherit (deriveIdentity ifPrefix.nspawn name) iface mac @@ -381,7 +382,7 @@ rec { nixosContainer = { inherit autoStart privateUsers; bindMounts = { }; - extraFlags = bindMountExtraFlags; + extraFlags = bindMountExtraFlags ++ deviceFlags; privateNetwork = true; extraVeths = linkVeths; config = diff --git a/tests/server-n0/integration.py b/tests/server-n0/integration.py index b5f95c8..8920514 100644 --- a/tests/server-n0/integration.py +++ b/tests/server-n0/integration.py @@ -333,6 +333,7 @@ with subtest("(functional): published ports follow the container's lifetime"): with subtest("(functional): declared devices are usable in-container"): for name, devices in DEVICES.items(): for dev in devices: + # opened, not stat'd: without DeviceAllow= the node is still visible server.succeed(ct_cmd(name, f"exec 3<> {dev}")) -- 2.55.0 From e8cc76844d736bfb6801100ae1cce2b4bf3cfd53 Mon Sep 17 00:00:00 2001 From: Autumn Liu Date: Fri, 11 Sep 2026 15:50:58 -0500 Subject: [PATCH 2/3] fix bug --- dsl.nix | 3 +-- tests/server-n0/integration.py | 5 +++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/dsl.nix b/dsl.nix index c07f613..4745527 100644 --- a/dsl.nix +++ b/dsl.nix @@ -295,7 +295,6 @@ rec { + lib.optionalString useIdmap ":idmap" ) allBindMounts; bindMountHostPaths = map (bm: bm.hostPath) (lib.attrValues allBindMounts); - deviceFlags = map (d: "--bind=${d}") devices; inherit (deriveIdentity ifPrefix.nspawn name) iface mac @@ -382,7 +381,7 @@ rec { nixosContainer = { inherit autoStart privateUsers; bindMounts = { }; - extraFlags = bindMountExtraFlags ++ deviceFlags; + extraFlags = bindMountExtraFlags; privateNetwork = true; extraVeths = linkVeths; config = diff --git a/tests/server-n0/integration.py b/tests/server-n0/integration.py index 8920514..edd7d41 100644 --- a/tests/server-n0/integration.py +++ b/tests/server-n0/integration.py @@ -333,6 +333,11 @@ with subtest("(functional): published ports follow the container's lifetime"): with subtest("(functional): declared devices are usable in-container"): for name, devices in DEVICES.items(): for dev in devices: + if server.execute(f"test -e {dev}")[0] != 0: + # host lacks the device (e.g. /dev/kvm without nested + # virtualization, as in this test's own VM) - nspawn skips + # populating it in-container, so there's nothing to check. + continue # opened, not stat'd: without DeviceAllow= the node is still visible server.succeed(ct_cmd(name, f"exec 3<> {dev}")) -- 2.55.0 From a12c18386d0e384cc649b75e37f70b787fd57c3f Mon Sep 17 00:00:00 2001 From: Autumn Liu Date: Sun, 13 Sep 2026 20:18:24 -0500 Subject: [PATCH 3/3] actually bind /dev/kvm into the container, and test it initializes DeviceAllow= only grants cgroup permission to open a device node - it doesn't create one, and nspawn can't mknod a device inside a user namespace. The node needs an explicit bind from the host as well, so both are required together. Also add a test that actually boots qemu-system-x86_64 -enable-kvm (no disk, killed by timeout) and checks for "Permission denied" / "Could not access KVM" in its output, rather than just checking the node opens - a wrong-gid or unmapped node still opens but fails the ioctls qemu needs. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01Dzy7tEKLCFFXD96LmR8we3 --- dsl.nix | 22 ++++++++++++++-------- tests/server-n0/integration.py | 23 ++++++++++++++++++----- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/dsl.nix b/dsl.nix index 4745527..2af94fe 100644 --- a/dsl.nix +++ b/dsl.nix @@ -222,13 +222,15 @@ rec { : Bind mounts, keyed by mount point. `resources.devices` - : Host device nodes to expose, as paths, added to the unit's - `DeviceAllow=`. nspawn populates a handful of privileged devices - (`/dev/kvm`, `/dev/net/tun`, ...) into the container's own `/dev` once - the cgroup allows it, and silently skips ones the host doesn't have - - no `resources.fs` bind mount needed. Nodes keep their host ownership, - so a consumer inside needs `isolation.privateUsers = "identity"` for - the owning gid to mean anything. + : Host device nodes to expose, as paths. Bound into the container at the + same path and added to the unit's `DeviceAllow=` - the cgroup + permission alone doesn't get you a working node (nothing can `mknod` a + device inside a user namespace), and the bind alone leaves the cgroup + denying `open()`, so both are needed. The bind is unconditional, so + the device must actually exist on whatever host runs the container. + Nodes keep their host ownership, so a consumer inside needs + `isolation.privateUsers = "identity"` for the owning gid to mean + anything. `isolation.nixDaemon` : Whether the Nix daemon and CLI are available inside the container. @@ -295,6 +297,10 @@ rec { + lib.optionalString useIdmap ":idmap" ) allBindMounts; bindMountHostPaths = map (bm: bm.hostPath) (lib.attrValues allBindMounts); + # DeviceAllow= alone only grants cgroup permission - nspawn can't + # mknod a device node inside a user namespace, so the node still + # needs an explicit bind from the host. + deviceFlags = map (d: "--bind=${d}") devices; inherit (deriveIdentity ifPrefix.nspawn name) iface mac @@ -381,7 +387,7 @@ rec { nixosContainer = { inherit autoStart privateUsers; bindMounts = { }; - extraFlags = bindMountExtraFlags; + extraFlags = bindMountExtraFlags ++ deviceFlags; privateNetwork = true; extraVeths = linkVeths; config = diff --git a/tests/server-n0/integration.py b/tests/server-n0/integration.py index edd7d41..1c8c41e 100644 --- a/tests/server-n0/integration.py +++ b/tests/server-n0/integration.py @@ -333,15 +333,28 @@ with subtest("(functional): published ports follow the container's lifetime"): with subtest("(functional): declared devices are usable in-container"): for name, devices in DEVICES.items(): for dev in devices: - if server.execute(f"test -e {dev}")[0] != 0: - # host lacks the device (e.g. /dev/kvm without nested - # virtualization, as in this test's own VM) - nspawn skips - # populating it in-container, so there's nothing to check. - continue # opened, not stat'd: without DeviceAllow= the node is still visible server.succeed(ct_cmd(name, f"exec 3<> {dev}")) +with subtest("(functional): /dev/kvm actually initializes, not just opens"): + # a wrong-gid or unmapped node still opens but fails the ioctls qemu + # needs, so boot with no disk (never completes) and kill it - the + # failure mode this catches is qemu refusing to start at all. + for name, devices in DEVICES.items(): + if "/dev/kvm" not in devices: + continue + out = server.succeed( + ct_cmd( + name, + f"{TIMEOUT} 3 qemu-system-x86_64 -enable-kvm -m 256 -nographic -no-reboot " + ">/tmp/qemu.out 2>&1; cat /tmp/qemu.out", + ) + ) + assert "Permission denied" not in out, f"{name}: qemu couldn't use /dev/kvm:\n{out}" + assert "Could not access KVM" not in out, f"{name}: qemu couldn't use /dev/kvm:\n{out}" + + with subtest("(isolation): devices reach only the container that declared them"): for name in enterable: for dev in {d for ds in DEVICES.values() for d in ds} - set(DEVICES.get(name, [])): -- 2.55.0