interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

[Codex Security] Privileged swap helper executes caller-selected Kbuild logic as root

open 53315ae0e01f75fa #1 · opened 2026-11-15 17:08 UTC

Imported from GitHub drakestapleton/CUDApp#1, by drakestapleton

Base ce03c88fb13b7eec798efa9a2953f6e3ed5c72fd · Head c65a5af31bea1d78858662c1f975a266792c8b6a

drakestapleton wrote on 2026-11-15 17:08 UTC

Delegated kernel swaps now isolate root Kbuild execution from caller-selected source trees.

  • swap/cudapp no longer forwards --driver-src and rejects option or environment overrides across sudo.
  • The privileged process selects /usr/src/aien-gpu and requires root ownership, non-writable modes, and symlink-free contents and ancestors.
  • These controls prevent delegated callers from steering root make into arbitrary commands or persistent module code.
  • Direct-root development retains custom-source support, while delegated trusted-source installs retain normal module staging.
  • tests/swap/test_swap_cli.sh adds assertions for overrides, writable entries, symlinks, writable ancestors, and trusted installation.
  • The assessment used static source and diff evidence only; runtime checks were not performed.

Static Verification Checks

  • Delegated override rejection (Passed) — swap/cudapp escalation and sudo re-entry reject driver-source arguments and environment values, closing caller control before make.
  • Trusted tree validation (Passed) — swap/cudapp trusted_driver_source rejects unowned, writable, or symlinked paths and entries, preventing source replacement before copying.
  • Privileged sink confinement (Passed) — swap/cudapp kernel_level receives only the validated fixed tree before make evaluates Kbuild, closing privileged code execution.
  • Legitimate workflows preserved (Passed) — tests/swap/test_swap_cli.sh asserts malicious-source refusals and trusted installation while existing custom-source staging remains covered.
  • Finding: acf_4c3128f91fe3817ebc1c79a8d0714b13

  • Patch: arp_5b066f28466641982a099a2f8de92760
  • Base commit: ce03c88fb13b7eec798efa9a2953f6e3ed5c72fd
  • Patch SHA-256: d751babd02abe9903b6dd3b04f7295fae67be8ee2b7028f4b2ce6651c67414a2

Apply it

git checkout -b patch-53315ae0e01f75fa ce03c88fb13b7eec798efa9a2953f6e3ed5c72fd
curl -fsSL https://interplane.io/drakestapleton/CUDApp/patches/53315ae0e01f75fa.patch | git am

Raw patch (mbox) · Diff

Provenance

This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record:

github-import 1
drakestapleton/CUDApp
1
c65a5af31bea1d78858662c1f975a266792c8b6a
{"kind":"github-import","owner":"drakestapleton","repo":"CUDApp","number":1,"url":"https://github.com/drakestapleton/CUDApp/pull/1","export_sha256":"47b9e2c453935cee8da188bd59c976ababe9290e5e88e31eaf77f752aade7d4c","imported_utc":"2026-11-18T16:39:25Z"}

Changes

diff --git a/swap/cudapp b/swap/cudapp
index be9baf39..2ada1bad 100755
--- a/swap/cudapp
+++ b/swap/cudapp
@@ -19,12 +19,12 @@
 # tests/swap/test_swap_cli.sh runs every level against a fake machine root.
 #
 # Sources: --lib PATH (CUDA++ libcuda.so.1), --driver-src PATH (aien_gpu source), --kdir PATH (kernel build dir).
-# CUDAPP_LIB, CUDAPP_DRIVER_SRC and CUDAPP_KDIR give the defaults for an unprivileged run. Escalation runs
-# `sudo "$0" swap ...` WITHOUT -E and passes the resolved paths as these options, so a sudoers rule naming
-# this script cannot be steered by the caller's environment. Running as root on the real system, the library
-# must be a regular file inside this repository and the kernel build dir must live under /lib/modules or /usr/src.
-# The aien_gpu source is built as root by design (a kernel module is root-equivalent): point it at a tree
-# only root or you can write.
+# CUDAPP_LIB, CUDAPP_DRIVER_SRC and CUDAPP_KDIR give the developer defaults. Escalation runs
+# `sudo "$0" swap ...` WITHOUT -E and passes only the resolved library and kernel-build paths needed by the level.
+# A sudo-originated kernel swap chooses /usr/src/aien-gpu in the privileged process and refuses caller overrides;
+# that tree and every path component must be root-owned, non-writable by group or other, and contain no symlinks.
+# A direct root invocation may still use --driver-src for development. On the real system, the library must be a
+# regular file inside this repository and the kernel build dir must live under /lib/modules or /usr/src.
 set -u
 ROOT=${CUDAPP_ROOT:-}
 STATE=$ROOT/var/lib/cudapp-swap
@@ -32,6 +32,7 @@ SELF=$(readlink -f "$0")
 SELF_DIR=$(cd "$(dirname "$SELF")" && pwd)
 REPO_DIR=$(cd "$SELF_DIR/.." && pwd -P)
 LIB_SRC=${CUDAPP_LIB:-$SELF_DIR/../build/swap/libcuda.so.1}
+DRIVER_SRC_ENV_SET=0; [ "${CUDAPP_DRIVER_SRC+x}" = x ] && DRIVER_SRC_ENV_SET=1
 DRIVER_SRC=${CUDAPP_DRIVER_SRC:-$HOME/workspace/aienos-own-gpu-driver/native/linux/aien-gpu}
 KVER=$(uname -r)
 KDIR=${CUDAPP_KDIR:-/lib/modules/$KVER/build}
@@ -45,7 +46,7 @@ MODDIR=/lib/modules/$KVER/updates/cudapp
 INITRD=/boot/initrd.img-$KVER
 NV_MODULES="nvidia nvidia_uvm nvidia_modeset nvidia_drm"
 
-YES=0; REBOOT=0; HOLDERS_OK=0
+YES=0; REBOOT=0; HOLDERS_OK=0; DRIVER_SRC_OPTION=0
 die()    { echo "cudapp: $*" >&2; exit 1; }
 refuse() { echo "REFUSED: $*" >&2; exit 3; }
 # A machine that schedules GPU work through spark-window (the invoking user has the tool) reboots only inside a window.
@@ -57,6 +58,32 @@ window_host() {
 }
 usage()  { sed -n '3,9p' "$0" | sed 's/^# \{0,1\}//' >&2; exit 2; }
 sha()    { if [ -e "$1" ] || [ -L "$1" ]; then sha256sum < "$1" | cut -d' ' -f1; else echo -; fi; }
+sudo_origin() { [ -n "${SUDO_UID:-}" ] && [ "$SUDO_UID" != 0 ]; }
+trusted_driver_source() {
+    local machine_root=$ROOT src owner=0 stop=/ p bad shown
+    # CUDAPP_ROOT is the host-test machine root. Its files are owned by the test uid, which stands in for uid 0.
+    if [ -n "$machine_root" ]; then
+        machine_root=${machine_root%/}; [ -n "$machine_root" ] || machine_root=/
+        owner=$(id -u); stop=$machine_root
+    fi
+    src=${machine_root%/}/usr/src/aien-gpu
+    [ -d "$src" ] || refuse "trusted aien_gpu source not found at /usr/src/aien-gpu"
+    p=$src
+    while :; do
+        shown=${p#"$machine_root"}; [ -n "$shown" ] || shown=/
+        [ ! -L "$p" ] || refuse "trusted aien_gpu path contains a symlink: $shown"
+        bad=$(find -P "$p" -maxdepth 0 \( ! -uid "$owner" -o -perm /022 \) -print -quit 2>/dev/null) || \
+            refuse "cannot inspect trusted aien_gpu path component $shown"
+        [ -z "$bad" ] || refuse "trusted aien_gpu path is not root-owned and immutable: $shown"
+        [ "$p" = "$stop" ] && break
+        p=${p%/*}; [ -n "$p" ] || p=/
+    done
+    bad=$(find -P "$src" \( -type l -o ! -uid "$owner" -o -perm /022 \) -print -quit 2>/dev/null) || \
+        refuse "cannot inspect trusted aien_gpu source at /usr/src/aien-gpu"
+    shown=${bad#"$machine_root"}
+    [ -z "$bad" ] || refuse "trusted aien_gpu source is not root-owned, immutable, and symlink-free: $shown"
+    DRIVER_SRC=$src
+}
 
 # ---------------------------------------------------------------- facts
 gpu_ids() { # prints "vendor:device" of every NVIDIA PCI function
@@ -254,7 +281,11 @@ while [ $# -gt 0 ]; do
         --in-spark-window) SPARK_WINDOW=1;;
         --lib|--driver-src|--kdir)
             [ $# -ge 2 ] && [ -n "$2" ] || usage
-            case $1 in --lib) LIB_SRC=$2;; --driver-src) DRIVER_SRC=$2;; --kdir) KDIR=$2;; esac
+            case $1 in
+                --lib) LIB_SRC=$2;;
+                --driver-src) DRIVER_SRC=$2; DRIVER_SRC_OPTION=1;;
+                --kdir) KDIR=$2;;
+            esac
             shift;;
         *) usage;;
     esac
@@ -262,14 +293,32 @@ while [ $# -gt 0 ]; do
 done
 case $level in status|user|kernel|all|back) ;; *) usage;; esac
 if [ $YES -eq 1 ] && [ -z "$ROOT" ] && [ "$(id -u)" -ne 0 ]; then
-    # No -E: root gets a clean environment; the three sources travel as explicit, resolved arguments.
+    case $level in
+        kernel|all)
+            [ $DRIVER_SRC_OPTION -eq 0 ] && [ $DRIVER_SRC_ENV_SET -eq 0 ] || \
+                refuse "--driver-src and CUDAPP_DRIVER_SRC are not accepted for a delegated kernel swap; root uses /usr/src/aien-gpu"
+            ;;
+    esac
+    # No -E: root gets a clean environment. Driver source is deliberately selected only after escalation.
     set -- --yes
     [ $REBOOT -eq 1 ] && set -- "$@" --reboot
     [ $HOLDERS_OK -eq 1 ] && set -- "$@" --holders-ok
     [ -n "${SPARK_WINDOW:-}" ] && set -- "$@" --in-spark-window
-    exec sudo "$SELF" swap "$level" "$@" --lib "$(readlink -m "$LIB_SRC")" \
-        --driver-src "$(readlink -m "$DRIVER_SRC")" --kdir "$(readlink -m "$KDIR")"
+    case $level in user|all) set -- "$@" --lib "$(readlink -m "$LIB_SRC")";; esac
+    case $level in kernel|all) set -- "$@" --kdir "$(readlink -m "$KDIR")";; esac
+    exec sudo "$SELF" swap "$level" "$@"
 fi
+# A sudoers rule may permit invoking this helper directly, bypassing the self-escalation branch above. Keep the
+# Kbuild selection inside the privileged process in both forms, and reject rather than ignore caller input.
+case $level in
+    kernel|all)
+        if sudo_origin && { [ "$(id -u)" -eq 0 ] || [ -n "$ROOT" ]; }; then
+            [ $DRIVER_SRC_OPTION -eq 0 ] || refuse "--driver-src is not accepted after sudo; root uses /usr/src/aien-gpu"
+            [ $DRIVER_SRC_ENV_SET -eq 0 ] || refuse "CUDAPP_DRIVER_SRC is not accepted after sudo; root uses /usr/src/aien-gpu"
+            trusted_driver_source
+        fi
+        ;;
+esac
 # As root on the real system, refuse sources outside the places a root install may read from.
 if [ -z "$ROOT" ] && [ "$(id -u)" -eq 0 ]; then
     case $level in
diff --git a/tests/swap/test_swap_cli.sh b/tests/swap/test_swap_cli.sh
index 4013d95e..df762fd8 100755
--- a/tests/swap/test_swap_cli.sh
+++ b/tests/swap/test_swap_cli.sh
@@ -16,7 +16,7 @@ mkroot() { # $1 = dir; builds a fake GB10 machine with NVIDIA's stack installed
     local R=$1
     mkdir -p "$R"/usr/lib/aarch64-linux-gnu "$R"/etc/ld.so.conf.d "$R"/etc/modprobe.d "$R"/etc/modules-load.d \
              "$R"/lib/modules/$KVER/kernel/drivers/video "$R"/boot "$R"/sys/bus/pci/devices/000f:01:00.0 \
-             "$R"/sys/module/nvidia "$R"/dev "$R"/fakebin "$R"/driver-src "$R"/build
+             "$R"/sys/module/nvidia "$R"/dev "$R"/fakebin "$R"/driver-src "$R"/usr/src/aien-gpu "$R"/build
     echo nvidia-libcuda-580 > "$R"/usr/lib/aarch64-linux-gnu/libcuda.so.580.173.02
     ln -s libcuda.so.580.173.02 "$R"/usr/lib/aarch64-linux-gnu/libcuda.so.1
     echo "/usr/lib/aarch64-linux-gnu" > "$R"/etc/ld.so.conf.d/aarch64-linux-gnu.conf
@@ -32,6 +32,8 @@ mkroot() { # $1 = dir; builds a fake GB10 machine with NVIDIA's stack installed
     echo "our-libcuda" > "$R"/build/libcuda.so.1
     echo "obj-m := aien_gpu.o" > "$R"/driver-src/Kbuild
     echo "/* aien_gpu */" > "$R"/driver-src/aien_gpu.c
+    echo "obj-m := aien_gpu.o" > "$R"/usr/src/aien-gpu/Kbuild
+    echo "/* trusted aien_gpu */" > "$R"/usr/src/aien-gpu/aien_gpu.c
     local F="$R"/fakebin
     for c in ldconfig depmod update-initramfs; do
         printf '#!/bin/bash\necho "%s $*" >> "%s/calls.log"\n' "$c" "$R" > "$F/$c"
@@ -59,6 +61,10 @@ run() { # run the tool against root $R
     PATH="$R/fakebin:$PATH" CUDAPP_ROOT="$R" CUDAPP_LIB="$R/build/libcuda.so.1" CUDAPP_DRIVER_SRC="$R/driver-src" \
         CUDAPP_KDIR="$R/lib/modules/$KVER/build" bash "$TOOL" "$@"
 }
+run_delegated() { # simulate root re-entry through sudo; the caller cannot select driver source
+    env -u CUDAPP_DRIVER_SRC PATH="$R/fakebin:$PATH" CUDAPP_ROOT="$R" CUDAPP_LIB="$R/build/libcuda.so.1" \
+        CUDAPP_KDIR="$R/lib/modules/$KVER/build" SUDO_UID=1000 bash "$TOOL" "$@"
+}
 
 T=$(mktemp -d); trap 'rm -rf "$T"' EXIT
 
@@ -157,7 +163,7 @@ check "one back undoes all" [ "$before" = "$(tree_hash "$R")" ]
 out=$(run swap sideways 2>&1); rc=$?
 check "unknown level exits 2" [ $rc -eq 2 ]
 
-# 11. escalation (audit C8): sudo without -E, the sources passed as explicit absolute arguments.
+# 11. escalation (audit C8): sudo without -E, with only the source paths needed by that level.
 #     Fake id (uid 1000) and fake sudo (records argv, runs nothing); no CUDAPP_ROOT, so this is the real-system path.
 S=$T/esc; mkdir -p "$S/bin" "$S/lib"; echo "lib" > "$S/lib/libcuda.so.1"
 printf '#!/bin/bash\necho 1000\n' > "$S/bin/id"
@@ -168,8 +174,43 @@ check "escalation calls sudo" [ -s "$S/sudo.argv" ]
 check "escalation drops -E (no caller environment reaches root)" bash -c "! grep -qx -- '-E' '$S/sudo.argv'"
 check "escalation re-runs this script by absolute path" [ "$(sed -n 1p "$S/sudo.argv")" = "$(readlink -f "$TOOL")" ]
 check "escalation passes --lib as a resolved absolute path" grep -qx -- "$S/lib/libcuda.so.1" "$S/sudo.argv"
-check "escalation passes --kdir and --driver-src" bash -c "grep -qx -- --kdir '$S/sudo.argv' && grep -qx -- --driver-src '$S/sudo.argv'"
-# 12. as root on the real system (fake id prints 0): sources outside the allowed places are refused before any level runs
+check "user-level escalation does not pass unused build paths" bash -c "! grep -qx -- --kdir '$S/sudo.argv' && ! grep -qx -- --driver-src '$S/sudo.argv'"
+: > "$S/sudo.argv"
+env -u CUDAPP_ROOT -u CUDAPP_DRIVER_SRC PATH="$S/bin:$PATH" CUDAPP_KDIR="$S/kdir/../kdir" \
+    bash "$TOOL" swap kernel --yes > /dev/null 2>&1
+check "kernel escalation calls sudo" [ -s "$S/sudo.argv" ]
+check "kernel escalation passes resolved --kdir" grep -qx -- "$(readlink -m "$S/kdir")" "$S/sudo.argv"
+check "kernel escalation does not pass --driver-src" bash -c "! grep -qx -- --driver-src '$S/sudo.argv'"
+: > "$S/sudo.argv"
+out=$(env -u CUDAPP_ROOT PATH="$S/bin:$PATH" bash "$TOOL" swap kernel --yes --driver-src "$S/driver-src" 2>&1); rc=$?
+check "caller-selected driver source is refused before sudo (exit 3)" [ $rc -eq 3 ]
+check "refused driver source never reaches sudo" [ ! -s "$S/sudo.argv" ]
+
+# 12. sudo-originated kernel swaps use only the fixed, immutable source selected by the privileged process.
+R=$T/r12a; mkroot "$R"
+out=$(run_delegated swap kernel --yes --driver-src "$R/driver-src" 2>&1); rc=$?
+check "delegated kernel swap refuses --driver-src (exit 3)" [ $rc -eq 3 ]
+check "delegated --driver-src refusal runs no make" bash -c "! grep -q '^make ' '$R/calls.log' 2>/dev/null"
+R=$T/r12b; mkroot "$R"
+out=$(PATH="$R/fakebin:$PATH" CUDAPP_ROOT="$R" CUDAPP_LIB="$R/build/libcuda.so.1" \
+    CUDAPP_DRIVER_SRC="$R/driver-src" CUDAPP_KDIR="$R/lib/modules/$KVER/build" SUDO_UID=1000 \
+    bash "$TOOL" swap kernel --yes 2>&1); rc=$?
+check "delegated kernel swap refuses CUDAPP_DRIVER_SRC (exit 3)" [ $rc -eq 3 ]
+R=$T/r12c; mkroot "$R"; chmod o+w "$R/usr/src/aien-gpu/Kbuild"
+out=$(run_delegated swap kernel --yes 2>&1); rc=$?
+check "delegated kernel swap refuses writable source files (exit 3)" [ $rc -eq 3 ]
+R=$T/r12d; mkroot "$R"; rm "$R/usr/src/aien-gpu/aien_gpu.c"; ln -s "$R/driver-src/aien_gpu.c" "$R/usr/src/aien-gpu/aien_gpu.c"
+out=$(run_delegated swap kernel --yes 2>&1); rc=$?
+check "delegated kernel swap refuses source symlinks (exit 3)" [ $rc -eq 3 ]
+R=$T/r12e; mkroot "$R"; chmod o+w "$R/usr/src"
+out=$(run_delegated swap kernel --yes 2>&1); rc=$?
+check "delegated kernel swap refuses writable path components (exit 3)" [ $rc -eq 3 ]
+R=$T/r12f; mkroot "$R"
+out=$(run_delegated swap kernel --yes 2>&1); rc=$?
+check "delegated kernel swap accepts immutable /usr/src/aien-gpu" [ $rc -eq 0 ]
+check "delegated kernel swap installs aien_gpu" [ -f "$R/lib/modules/$KVER/updates/cudapp/aien_gpu.ko" ]
+
+# 13. as root on the real system (fake id prints 0): sources outside the allowed places are refused before any level runs
 printf '#!/bin/bash\necho 0\n' > "$S/bin/id"
 out=$(env -u CUDAPP_ROOT PATH="$S/bin:$PATH" bash "$TOOL" swap user --lib "$S/lib/libcuda.so.1" 2>&1); rc=$?
 check "root: --lib outside the repository refused (exit 3)" [ $rc -eq 3 ]
@@ -184,7 +225,7 @@ check "root: refusal names /lib/modules" grep -q "not under /lib/modules" <<<"$o
 out=$(env -u CUDAPP_ROOT PATH="$S/bin:$PATH" bash "$TOOL" swap user --lib "$REPO/LICENSE" 2>&1); rc=$?
 check "root: --lib inside the repository accepted (dry run exits 0)" [ $rc -eq 0 ]
 check "root: accepted dry run changed nothing" grep -q "DRY RUN" <<<"$out"
-# 13. the installed library is pinned: a copy that differs from the planned sha256 is rolled back
+# 14. the installed library is pinned: a copy that differs from the planned sha256 is rolled back
 R=$T/r13; mkroot "$R"; before=$(tree_hash "$R")
 printf '#!/bin/bash\nfor a; do d=$a; done\necho tampered > "$d"\n' > "$R/fakebin/install"; chmod +x "$R/fakebin/install"
 out=$(run swap user --yes 2>&1); rc=$?