From 1d8ec0b1cc2f05d23cde6a105dcd8da5737bf3ac Mon Sep 17 00:00:00 2001 From: Alex David Date: Sun, 19 Oct 2025 11:19:48 -0700 Subject: [PATCH] Lock down GPU combinator This is one of the original combinators I wrote for myself before even open sourcing jail.nix. It was exposing all of `/sys` which is unnecessary. With this change, only `/sys/dev/char` is shared, (which should only contain symlinks), and any PCI devices that contain a `drm` directory. This seems to be the most reliable way I can tell of finding GPUs from testing various combinations of distros and GPUs. The other signal seems to be if `class` is `0x030000`, although I struggled to find documentation where these classes are defined, and if they are in-fact constant across all linux systems. Anyway, this is a massive improvement over binding all of `/sys`, and hopefully not a breaking change for anyone. If anyone runs into issues, or has suggestions on better approaches I would love to hear it. --- lib/combinators/gpu.nix | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/combinators/gpu.nix b/lib/combinators/gpu.nix index aaa0541..93d27ba 100644 --- a/lib/combinators/gpu.nix +++ b/lib/combinators/gpu.nix @@ -1,6 +1,7 @@ { combinators, ... }: let inherit (combinators) + add-runtime compose include-once noescape @@ -12,12 +13,34 @@ in { sig = "Permission"; doc = '' - Exposes the gpu to jailed application. + Exposes GPUs to jailed application. + + This currently only binds PCI GPUs. If you need to expose GPUs over other + interfaces, or a GPU in PCI domain other than `0000`, please [open an + issue](https://todo.sr.ht/~alexdavid/jail.nix). ''; impl = include-once "gpu" (compose [ + (add-runtime '' + # jail.nix currently only supports binding GPUs in PCI domain 0000, if + # you need other domains or interfaces, please open an issue! + DOMAIN=0000 + function bind-gpus { + for DEV in "$1"/"$DOMAIN":*; do + if [ -d "$DEV" ]; then + if [ -e "$DEV/drm" ]; then + RUNTIME_ARGS+=(--ro-bind "$DEV" "$DEV") + else + # This may be a bridge, look for GPUs within: + bind-gpus "$DEV" + fi + fi + done + } + bind-gpus /sys/devices/pci"$DOMAIN":00 + '') + (readonly "/sys/dev/char") (runtime-deep-ro-bind (noescape "/run/opengl-driver")) (runtime-deep-ro-bind (noescape "/run/opengl-driver-32")) - (readonly (noescape "/sys")) (unsafe-add-raw-args "--dev-bind /dev/dri /dev/dri") ]); }