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
CUDApp / tests / swap / test_swap_cli.sh
| 1 | #!/bin/bash |
| 2 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | # Host test of `cudapp swap` on a fake machine root (no root rights, no GPU, nothing outside a temp dir touched). |
| 4 | # Every system command the tool runs (ldconfig, depmod, update-initramfs, lsinitramfs, mokutil, fuser, make, modinfo, |
| 5 | # uname) is a fake on PATH that logs its call, so the test sees exactly what the tool would do. |
| 6 | # bash tests/swap/test_swap_cli.sh [path/to/cudapp] |
| 7 | set -u |
| 8 | TOOL=${1:-$(cd "$(dirname "$0")/../.." && pwd)/swap/cudapp} |
| 9 | PASS=0; FAIL=0 |
| 10 | ok() { PASS=$((PASS+1)); echo "ok $*"; } |
| 11 | bad() { FAIL=$((FAIL+1)); echo "FAIL $*"; } |
| 12 | check(){ local name=$1; shift; if "$@"; then ok "$name"; else bad "$name"; fi; } |
| 13 | |
| 14 | KVER=7.0.0-1019-nvidia |
| 15 | mkroot() { # $1 = dir; builds a fake GB10 machine with NVIDIA's stack installed |
| 16 | local R=$1 |
| 17 | mkdir -p "$R"/usr/lib/aarch64-linux-gnu "$R"/etc/ld.so.conf.d "$R"/etc/modprobe.d "$R"/etc/modules-load.d \ |
| 18 | "$R"/lib/modules/$KVER/kernel/drivers/video "$R"/boot "$R"/sys/bus/pci/devices/000f:01:00.0 \ |
| 19 | "$R"/sys/module/nvidia "$R"/dev "$R"/fakebin "$R"/driver-src "$R"/build |
| 20 | echo nvidia-libcuda-580 > "$R"/usr/lib/aarch64-linux-gnu/libcuda.so.580.173.02 |
| 21 | ln -s libcuda.so.580.173.02 "$R"/usr/lib/aarch64-linux-gnu/libcuda.so.1 |
| 22 | echo "/usr/lib/aarch64-linux-gnu" > "$R"/etc/ld.so.conf.d/aarch64-linux-gnu.conf |
| 23 | echo "original-cache" > "$R"/etc/ld.so.cache |
| 24 | echo "nvidia.ko" > "$R"/lib/modules/$KVER/kernel/drivers/video/nvidia.ko |
| 25 | echo "dep-original" > "$R"/lib/modules/$KVER/modules.dep |
| 26 | echo "alias-original" > "$R"/lib/modules/$KVER/modules.alias |
| 27 | echo "initrd-original" > "$R"/boot/initrd.img-$KVER |
| 28 | echo 0x10de > "$R"/sys/bus/pci/devices/000f:01:00.0/vendor |
| 29 | echo 0x2e12 > "$R"/sys/bus/pci/devices/000f:01:00.0/device |
| 30 | echo 580.173.02 > "$R"/sys/module/nvidia/version |
| 31 | touch "$R"/dev/nvidia0 "$R"/dev/nvidiactl |
| 32 | echo "our-libcuda" > "$R"/build/libcuda.so.1 |
| 33 | echo "obj-m := aien_gpu.o" > "$R"/driver-src/Kbuild |
| 34 | echo "/* aien_gpu */" > "$R"/driver-src/aien_gpu.c |
| 35 | local F="$R"/fakebin |
| 36 | for c in ldconfig depmod update-initramfs; do |
| 37 | printf '#!/bin/bash\necho "%s $*" >> "%s/calls.log"\n' "$c" "$R" > "$F/$c" |
| 38 | done |
| 39 | # ldconfig also rewrites the cache (as the real one does), so the restore must bring the old bytes back |
| 40 | printf '#!/bin/bash\necho "ldconfig $*" >> "%s/calls.log"\necho "cache-with-cudapp" > "%s/etc/ld.so.cache"\n' "$R" "$R" > "$F/ldconfig" |
| 41 | printf '#!/bin/bash\necho "depmod $*" >> "%s/calls.log"\necho "dep-new" > "%s/lib/modules/%s/modules.dep"\n' "$R" "$R" $KVER > "$F/depmod" |
| 42 | printf '#!/bin/bash\necho "update-initramfs $*" >> "%s/calls.log"\necho "initrd-new" > "%s/boot/initrd.img-%s"\n' "$R" "$R" $KVER > "$F/update-initramfs" |
| 43 | printf '#!/bin/bash\ncat "%s/lsinitramfs.out" 2>/dev/null\n' "$R" > "$F/lsinitramfs" |
| 44 | printf '#!/bin/bash\ncat "%s/sb.out" 2>/dev/null || echo "SecureBoot disabled"\n' "$R" > "$F/mokutil" |
| 45 | printf '#!/bin/bash\ncat "%s/fuser.out" 2>/dev/null; exit 0\n' "$R" > "$F/fuser" |
| 46 | printf '#!/bin/bash\necho %s\n' $KVER > "$F/uname" |
| 47 | # make: "builds" aien_gpu.ko in M=dir |
| 48 | printf '#!/bin/bash\necho "make $*" >> "%s/calls.log"\nfor a in "$@"; do case $a in M=*) echo "aien_gpu.ko built" > "${a#M=}/aien_gpu.ko";; esac; done\n' "$R" > "$F/make" |
| 49 | printf '#!/bin/bash\necho "vermagic: %s SMP preempt mod_unload modversions aarch64"\n' $KVER > "$F/modinfo" |
| 50 | mkdir -p "$R"/lib/modules/$KVER/build |
| 51 | chmod +x "$F"/* |
| 52 | } |
| 53 | tree_hash() { # hashes of every file and link under the fake root, minus the tool's own state and the fake tools |
| 54 | (cd "$1" && find . \( -path ./var/lib/cudapp-swap -o -path ./fakebin -o -name calls.log -o -path ./driver-src -o -path ./build \) -prune -o \ |
| 55 | \( -type f -o -type l \) -print | LC_ALL=C sort | while read -r f; do |
| 56 | if [ -L "$f" ]; then echo "L $f $(readlink "$f")"; else echo "F $f $(sha256sum < "$f" | cut -c1-16)"; fi; done) |
| 57 | } |
| 58 | run() { # run the tool against root $R |
| 59 | PATH="$R/fakebin:$PATH" CUDAPP_ROOT="$R" CUDAPP_LIB="$R/build/libcuda.so.1" CUDAPP_DRIVER_SRC="$R/driver-src" \ |
| 60 | CUDAPP_KDIR="$R/lib/modules/$KVER/build" bash "$TOOL" "$@" |
| 61 | } |
| 62 | |
| 63 | T=$(mktemp -d); trap 'rm -rf "$T"' EXIT |
| 64 | |
| 65 | # 1. status names the GPU, the driver, Secure Boot, the holders and which libcuda.so.1 the loader finds |
| 66 | R=$T/r1; mkroot "$R"; echo "/dev/nvidia0: 108356" > "$R/fuser.out" |
| 67 | out=$(run swap status 2>&1); rc=$? |
| 68 | check "status exits 0" [ $rc -eq 0 ] |
| 69 | check "status names GB10" grep -q "GB10 (10de:2e12)" <<<"$out" |
| 70 | check "status names driver 580.173.02" grep -q "580.173.02" <<<"$out" |
| 71 | check "status names Secure Boot" grep -q "Secure Boot: disabled" <<<"$out" |
| 72 | check "status lists holder pid" grep -q "108356" <<<"$out" |
| 73 | check "status says NVIDIA libcuda is in use" grep -q "libcuda.so.1: NVIDIA" <<<"$out" |
| 74 | |
| 75 | # 2. user without --yes is a dry run: prints the plan, changes nothing |
| 76 | R=$T/r2; mkroot "$R"; before=$(tree_hash "$R") |
| 77 | out=$(run swap user 2>&1); rc=$? |
| 78 | check "dry run exits 0" [ $rc -eq 0 ] |
| 79 | check "dry run says DRY RUN" grep -q "DRY RUN" <<<"$out" |
| 80 | check "dry run lists the ld.so.conf.d file" grep -q "etc/ld.so.conf.d/00000-cudapp-swap.conf" <<<"$out" |
| 81 | check "dry run changes nothing" [ "$before" = "$(tree_hash "$R")" ] |
| 82 | check "dry run runs no ldconfig" [ ! -s "$R/calls.log" ] |
| 83 | |
| 84 | # 3. user --yes installs our lib first in the loader's search, keeps a dated backup; back restores every hash |
| 85 | out=$(run swap user --yes 2>&1); rc=$? |
| 86 | check "user --yes exits 0" [ $rc -eq 0 ] |
| 87 | check "our libcuda installed" cmp -s "$R/usr/lib/cudapp/libcuda.so.1" "$R/build/libcuda.so.1" |
| 88 | check "ld.so.conf.d entry points at /usr/lib/cudapp" grep -qx "/usr/lib/cudapp" "$R/etc/ld.so.conf.d/00000-cudapp-swap.conf" |
| 89 | check "ldconfig ran" grep -q "^ldconfig" "$R/calls.log" |
| 90 | check "NVIDIA libcuda untouched" [ "$(cat "$R/usr/lib/aarch64-linux-gnu/libcuda.so.580.173.02")" = nvidia-libcuda-580 ] |
| 91 | check "backup manifest written" bash -c "ls >/dev/null '$R'/var/lib/cudapp-swap/backup-*-user/manifest.tsv" |
| 92 | check "status now says user level swapped" bash -c "PATH='$R/fakebin:$PATH' CUDAPP_ROOT='$R' bash '$TOOL' swap status | grep -q 'user level: SWAPPED'" |
| 93 | out=$(run swap back --yes 2>&1); rc=$? |
| 94 | check "back exits 0" [ $rc -eq 0 ] |
| 95 | check "back reports PASS" grep -q "BACK PASS" <<<"$out" |
| 96 | check "back restores every hash" [ "$before" = "$(tree_hash "$R")" ] |
| 97 | |
| 98 | # 4. kernel refuses with Secure Boot on (an unsigned module would be refused at boot) |
| 99 | R=$T/r4; mkroot "$R"; echo "SecureBoot enabled" > "$R/sb.out" |
| 100 | out=$(run swap kernel --yes 2>&1); rc=$? |
| 101 | check "kernel refuses under Secure Boot (exit 3)" [ $rc -eq 3 ] |
| 102 | check "refusal names Secure Boot" grep -q "Secure Boot is enabled" <<<"$out" |
| 103 | |
| 104 | # 5. kernel --yes refuses while processes hold /dev/nvidia*, naming them |
| 105 | R=$T/r5; mkroot "$R"; echo "/dev/nvidia0: 108356 1816" > "$R/fuser.out"; before=$(tree_hash "$R") |
| 106 | out=$(run swap kernel --yes 2>&1); rc=$? |
| 107 | check "kernel refuses with holders (exit 3)" [ $rc -eq 3 ] |
| 108 | check "refusal names the holder" grep -q "108356" <<<"$out" |
| 109 | check "refusal changed nothing" [ "$before" = "$(tree_hash "$R")" ] |
| 110 | |
| 111 | # 6. kernel refuses on a machine without a GB10 |
| 112 | R=$T/r6; mkroot "$R"; echo 0x2b85 > "$R/sys/bus/pci/devices/000f:01:00.0/device" |
| 113 | out=$(run swap kernel 2>&1); rc=$? |
| 114 | check "kernel refuses without GB10 (exit 3)" [ $rc -eq 3 ] |
| 115 | check "refusal names the device found" grep -q "10de:2b85" <<<"$out" |
| 116 | |
| 117 | # 7. kernel --yes stages the switch (module, hold-back, autoload, initramfs when NVIDIA is in it); back restores hashes |
| 118 | R=$T/r7; mkroot "$R"; echo "usr/lib/modules/$KVER/kernel/drivers/video/nvidia.ko" > "$R/lsinitramfs.out"; before=$(tree_hash "$R") |
| 119 | out=$(run swap kernel 2>&1) |
| 120 | check "kernel dry run names the reboot" grep -q "reboot" <<<"$out" |
| 121 | check "kernel dry run changed nothing" [ "$before" = "$(tree_hash "$R")" ] |
| 122 | out=$(run swap kernel --yes 2>&1); rc=$? |
| 123 | check "kernel --yes exits 0" [ $rc -eq 0 ] |
| 124 | MC="$R/etc/modprobe.d/cudapp-swap-kernel.conf" |
| 125 | for m in nvidia nvidia_uvm nvidia_modeset nvidia_drm; do |
| 126 | check "hold-back blacklists $m" grep -qx "blacklist $m" "$MC" |
| 127 | check "hold-back stops explicit modprobe $m" grep -qx "install $m /bin/false" "$MC" |
| 128 | done |
| 129 | check "aien_gpu installed for the running kernel" [ -f "$R/lib/modules/$KVER/updates/cudapp/aien_gpu.ko" ] |
| 130 | check "aien_gpu loads at boot" grep -qx "aien_gpu" "$R/etc/modules-load.d/cudapp-aien-gpu.conf" |
| 131 | check "depmod ran for $KVER" grep -q "^depmod -a $KVER" "$R/calls.log" |
| 132 | check "initramfs rebuilt (NVIDIA was in it)" grep -q "^update-initramfs -u -k $KVER" "$R/calls.log" |
| 133 | check "nvidia.ko untouched" [ "$(cat "$R/lib/modules/$KVER/kernel/drivers/video/nvidia.ko")" = nvidia.ko ] |
| 134 | check "never reboots by itself" bash -c "! grep -q reboot '$R/calls.log'" |
| 135 | out=$(run swap back --yes 2>&1); rc=$? |
| 136 | check "kernel back exits 0" [ $rc -eq 0 ] |
| 137 | check "kernel back restores every hash (initrd, modules.dep, conf files)" [ "$before" = "$(tree_hash "$R")" ] |
| 138 | |
| 139 | # 8. negative control for the back check: a backup copy damaged on disk must make back FAIL, not PASS |
| 140 | R=$T/r8; mkroot "$R"; run swap user --yes > /dev/null 2>&1 |
| 141 | b=$(ls -d "$R"/var/lib/cudapp-swap/backup-*-user | head -1) |
| 142 | echo "damaged" > "$b/files/etc/ld.so.cache" |
| 143 | out=$(run swap back --yes 2>&1); rc=$? |
| 144 | check "back with a damaged backup exits 1" [ $rc -eq 1 ] |
| 145 | check "back names the file that does not match" grep -q "MISMATCH.*etc/ld.so.cache" <<<"$out" |
| 146 | |
| 147 | # 9. all = user + kernel, and one back undoes both |
| 148 | R=$T/r9; mkroot "$R"; before=$(tree_hash "$R") |
| 149 | run swap all --yes > /dev/null 2>&1; rc=$? |
| 150 | check "all --yes exits 0" [ $rc -eq 0 ] |
| 151 | check "all staged the user level" [ -f "$R/etc/ld.so.conf.d/00000-cudapp-swap.conf" ] |
| 152 | check "all staged the kernel level" [ -f "$R/etc/modprobe.d/cudapp-swap-kernel.conf" ] |
| 153 | run swap back --yes > /dev/null 2>&1 |
| 154 | check "one back undoes all" [ "$before" = "$(tree_hash "$R")" ] |
| 155 | |
| 156 | # 10. unknown level is refused with usage, exit 2 |
| 157 | out=$(run swap sideways 2>&1); rc=$? |
| 158 | check "unknown level exits 2" [ $rc -eq 2 ] |
| 159 | |
| 160 | # 11. escalation (audit C8): sudo without -E, the sources passed as explicit absolute arguments. |
| 161 | # Fake id (uid 1000) and fake sudo (records argv, runs nothing); no CUDAPP_ROOT, so this is the real-system path. |
| 162 | S=$T/esc; mkdir -p "$S/bin" "$S/lib"; echo "lib" > "$S/lib/libcuda.so.1" |
| 163 | printf '#!/bin/bash\necho 1000\n' > "$S/bin/id" |
| 164 | printf '#!/bin/bash\nprintf "%%s\\n" "$@" > "%s/sudo.argv"\n' "$S" > "$S/bin/sudo" |
| 165 | chmod +x "$S/bin"/* |
| 166 | env -u CUDAPP_ROOT PATH="$S/bin:$PATH" CUDAPP_LIB="$S/lib/../lib/libcuda.so.1" bash "$TOOL" swap user --yes > /dev/null 2>&1 |
| 167 | check "escalation calls sudo" [ -s "$S/sudo.argv" ] |
| 168 | check "escalation drops -E (no caller environment reaches root)" bash -c "! grep -qx -- '-E' '$S/sudo.argv'" |
| 169 | check "escalation re-runs this script by absolute path" [ "$(sed -n 1p "$S/sudo.argv")" = "$(readlink -f "$TOOL")" ] |
| 170 | check "escalation passes --lib as a resolved absolute path" grep -qx -- "$S/lib/libcuda.so.1" "$S/sudo.argv" |
| 171 | check "user escalation does not pass --kdir or --driver-src" bash -c "! grep -qx -- --kdir '$S/sudo.argv' && ! grep -qx -- --driver-src '$S/sudo.argv'" |
| 172 | : > "$S/sudo.argv" |
| 173 | env -u CUDAPP_ROOT -u CUDAPP_DRIVER_SRC -u CUDAPP_KDIR PATH="$S/bin:$PATH" \ |
| 174 | bash "$TOOL" swap kernel --yes > /dev/null 2>&1 |
| 175 | check "kernel escalation calls sudo" [ -s "$S/sudo.argv" ] |
| 176 | check "kernel escalation does not pass --driver-src or --kdir" bash -c "! grep -qx -- --driver-src '$S/sudo.argv' && ! grep -qx -- --kdir '$S/sudo.argv'" |
| 177 | : > "$S/sudo.argv" |
| 178 | out=$(env -u CUDAPP_ROOT -u CUDAPP_DRIVER_SRC -u CUDAPP_KDIR PATH="$S/bin:$PATH" \ |
| 179 | bash "$TOOL" swap kernel --yes --driver-src "$S/lib" 2>&1); rc=$? |
| 180 | check "caller-selected driver source is refused before sudo (exit 3)" [ $rc -eq 3 ] |
| 181 | check "refused driver source never reaches sudo" [ ! -s "$S/sudo.argv" ] |
| 182 | check "driver-source refusal names the delegated swap" grep -q "delegated kernel swap" <<<"$out" |
| 183 | : > "$S/sudo.argv" |
| 184 | out=$(env -u CUDAPP_ROOT PATH="$S/bin:$PATH" CUDAPP_DRIVER_SRC="$S/lib" \ |
| 185 | bash "$TOOL" swap kernel --yes 2>&1); rc=$? |
| 186 | check "CUDAPP_DRIVER_SRC is refused before sudo (exit 3)" [ $rc -eq 3 ] |
| 187 | check "refused CUDAPP_DRIVER_SRC never reaches sudo" [ ! -s "$S/sudo.argv" ] |
| 188 | : > "$S/sudo.argv" |
| 189 | out=$(env -u CUDAPP_ROOT -u CUDAPP_DRIVER_SRC PATH="$S/bin:$PATH" \ |
| 190 | bash "$TOOL" swap all --yes --kdir "$S" 2>&1); rc=$? |
| 191 | check "caller-selected kdir is refused before sudo (exit 3)" [ $rc -eq 3 ] |
| 192 | check "refused kdir never reaches sudo" [ ! -s "$S/sudo.argv" ] |
| 193 | # 12. as root on the real system (fake id prints 0): sources outside the allowed places are refused before any level runs |
| 194 | printf '#!/bin/bash\necho 0\n' > "$S/bin/id" |
| 195 | out=$(env -u CUDAPP_ROOT PATH="$S/bin:$PATH" bash "$TOOL" swap user --lib "$S/lib/libcuda.so.1" 2>&1); rc=$? |
| 196 | check "root: --lib outside the repository refused (exit 3)" [ $rc -eq 3 ] |
| 197 | check "root: refusal names the repository" grep -q "outside the CUDA++ repository" <<<"$out" |
| 198 | REPO=$(cd "$(dirname "$(readlink -f "$TOOL")")/.." && pwd -P) |
| 199 | out=$(env -u CUDAPP_ROOT PATH="$S/bin:$PATH" CUDAPP_LIB="$S/lib/libcuda.so.1" bash "$TOOL" swap all 2>&1); rc=$? |
| 200 | check "root: CUDAPP_LIB outside the repository refused for 'all' (exit 3)" [ $rc -eq 3 ] |
| 201 | out=$(env -u CUDAPP_ROOT PATH="$S/bin:$PATH" bash "$TOOL" swap kernel --kdir "$S" 2>&1); rc=$? |
| 202 | check "root: --kdir outside /lib/modules and /usr/src refused (exit 3)" [ $rc -eq 3 ] |
| 203 | check "root: refusal names /lib/modules" grep -q "not under /lib/modules" <<<"$out" |
| 204 | # positive control: a regular file inside the repository passes the check (dry run, nothing changed) |
| 205 | out=$(env -u CUDAPP_ROOT PATH="$S/bin:$PATH" bash "$TOOL" swap user --lib "$REPO/LICENSE" 2>&1); rc=$? |
| 206 | check "root: --lib inside the repository accepted (dry run exits 0)" [ $rc -eq 0 ] |
| 207 | check "root: accepted dry run changed nothing" grep -q "DRY RUN" <<<"$out" |
| 208 | # 13. the installed library is pinned: a copy that differs from the planned sha256 is rolled back |
| 209 | R=$T/r13; mkroot "$R"; before=$(tree_hash "$R") |
| 210 | printf '#!/bin/bash\nfor a; do d=$a; done\necho tampered > "$d"\n' > "$R/fakebin/install"; chmod +x "$R/fakebin/install" |
| 211 | out=$(run swap user --yes 2>&1); rc=$? |
| 212 | check "differing installed library fails (exit 1)" [ $rc -eq 1 ] |
| 213 | check "failure names the sha256 mismatch" grep -q "sha256 .* != .* planned" <<<"$out" |
| 214 | check "differing installed library rolled back" [ "$before" = "$(tree_hash "$R")" ] |
| 215 | rm "$R/fakebin/install"; out=$(run swap user --yes 2>&1) |
| 216 | check "installed sha256 recorded next to the backup" bash -c "grep -q \"\$(sha256sum < '$R/build/libcuda.so.1' | cut -d' ' -f1) /usr/lib/cudapp/libcuda.so.1\" '$R'/var/lib/cudapp-swap/backup-*-user/installed.sha256" |
| 217 | |
| 218 | # 14. Delegated kernel build (SUDO_UID set, fake machine root). Root must build /usr/src/aien-gpu |
| 219 | # and the running kernel's build dir, never the caller's tree. Ownership in the fixture is |
| 220 | # the test uid, which stands in for uid 0. |
| 221 | lock_tree() { # $1 = trusted file or directory; drop group/other write on it and on ancestors inside $2 |
| 222 | local p=$1 stop=$2 |
| 223 | chmod -R u+rwX,go-w "$p" |
| 224 | while :; do |
| 225 | chmod u+rwx,go-w "$p" |
| 226 | [ "$p" = "$stop" ] && break |
| 227 | p=$(dirname "$p") |
| 228 | done |
| 229 | } |
| 230 | stage_make() { # record the Kbuild the module build actually consumed |
| 231 | local R=$1 |
| 232 | printf '#!/bin/bash\necho "make $*" >> "%s/calls.log"\nfor a in "$@"; do case $a in M=*) d=${a#M=}; echo "KBUILD:$(head -1 "$d/Kbuild")" >> "%s/calls.log"; echo built > "$d/aien_gpu.ko";; esac; done\n' "$R" "$R" > "$R/fakebin/make" |
| 233 | chmod +x "$R/fakebin/make" |
| 234 | } |
| 235 | delegated() { # $1 = fake root; remaining args are the tool's |
| 236 | local R=$1; shift |
| 237 | env -u CUDAPP_DRIVER_SRC -u CUDAPP_KDIR PATH="$R/fakebin:$PATH" CUDAPP_ROOT="$R" \ |
| 238 | CUDAPP_LIB="$R/build/libcuda.so.1" SUDO_UID=1000 SUDO_USER=fixture \ |
| 239 | bash "$TOOL" "$@" |
| 240 | } |
| 241 | trusted_src() { # $1 = fake root. A locked tree whose Kbuild is not the attacker's. |
| 242 | local R=$1 |
| 243 | mkdir -p "$R/usr/src/aien-gpu" "$R/attacker" |
| 244 | printf 'trusted-marker\nobj-m := aien_gpu.o\n' > "$R/usr/src/aien-gpu/Kbuild" |
| 245 | echo '/* trusted */' > "$R/usr/src/aien-gpu/aien_gpu.c" |
| 246 | printf 'attacker-marker\nobj-m := aien_gpu.o\n' > "$R/attacker/Kbuild" |
| 247 | echo '/* attacker */' > "$R/attacker/aien_gpu.c" |
| 248 | lock_tree "$R/usr/src/aien-gpu" "$R" |
| 249 | [ -d "$R/lib/modules/$KVER" ] && [ ! -L "$R/lib" ] && lock_tree "$R/lib/modules/$KVER" "$R" |
| 250 | } |
| 251 | |
| 252 | R=$T/r14a; mkroot "$R"; trusted_src "$R"; stage_make "$R" |
| 253 | out=$(delegated "$R" swap kernel --yes --driver-src "$R/attacker" 2>&1); rc=$? |
| 254 | check "delegated kernel swap refuses --driver-src (exit 3)" [ $rc -eq 3 ] |
| 255 | check "delegated --driver-src refusal names the delegated swap" grep -q "delegated kernel swap" <<<"$out" |
| 256 | check "delegated --driver-src runs no make" bash -c "! grep -q '^make ' '$R/calls.log' 2>/dev/null" |
| 257 | check "delegated --driver-src does not install a module" [ ! -e "$R/lib/modules/$KVER/updates/cudapp/aien_gpu.ko" ] |
| 258 | |
| 259 | R=$T/r14b; mkroot "$R"; trusted_src "$R"; stage_make "$R" |
| 260 | out=$(PATH="$R/fakebin:$PATH" CUDAPP_ROOT="$R" CUDAPP_LIB="$R/build/libcuda.so.1" \ |
| 261 | CUDAPP_DRIVER_SRC="$R/attacker" SUDO_UID=1000 bash "$TOOL" swap kernel --yes 2>&1); rc=$? |
| 262 | check "delegated kernel swap refuses CUDAPP_DRIVER_SRC (exit 3)" [ $rc -eq 3 ] |
| 263 | check "delegated CUDAPP_DRIVER_SRC refusal names the delegated swap" grep -q "delegated kernel swap" <<<"$out" |
| 264 | check "delegated CUDAPP_DRIVER_SRC runs no make" bash -c "! grep -q '^make ' '$R/calls.log' 2>/dev/null" |
| 265 | |
| 266 | R=$T/r14c; mkroot "$R"; trusted_src "$R"; stage_make "$R" |
| 267 | out=$(delegated "$R" swap kernel --yes --kdir "$R/attacker" 2>&1); rc=$? |
| 268 | check "delegated kernel swap refuses --kdir (exit 3)" [ $rc -eq 3 ] |
| 269 | check "delegated --kdir refusal names the delegated swap" grep -q "delegated kernel swap" <<<"$out" |
| 270 | check "delegated --kdir runs no make" bash -c "! grep -q '^make ' '$R/calls.log' 2>/dev/null" |
| 271 | |
| 272 | R=$T/r14d; mkroot "$R"; trusted_src "$R"; stage_make "$R" |
| 273 | chmod o+w "$R/usr/src/aien-gpu/Kbuild" |
| 274 | out=$(delegated "$R" swap kernel --yes 2>&1); rc=$? |
| 275 | check "delegated kernel swap refuses a group-or-other-writable source file (exit 3)" [ $rc -eq 3 ] |
| 276 | check "writable source refusal names the file mode" grep -q "writable" <<<"$out" |
| 277 | check "writable source file runs no make" bash -c "! grep -q '^make ' '$R/calls.log' 2>/dev/null" |
| 278 | |
| 279 | R=$T/r14e; mkroot "$R"; trusted_src "$R"; stage_make "$R" |
| 280 | rm "$R/usr/src/aien-gpu/aien_gpu.c" |
| 281 | ln -s "$R/attacker/aien_gpu.c" "$R/usr/src/aien-gpu/aien_gpu.c" |
| 282 | out=$(delegated "$R" swap kernel --yes 2>&1); rc=$? |
| 283 | check "delegated kernel swap refuses a symlink in the source (exit 3)" [ $rc -eq 3 ] |
| 284 | check "source symlink refusal names the symlink" grep -q "symlink" <<<"$out" |
| 285 | check "source symlink runs no make" bash -c "! grep -q '^make ' '$R/calls.log' 2>/dev/null" |
| 286 | |
| 287 | R=$T/r14f; mkroot "$R"; trusted_src "$R"; stage_make "$R" |
| 288 | rm -rf "$R/usr/src/aien-gpu" |
| 289 | ln -s "$R/attacker" "$R/usr/src/aien-gpu" |
| 290 | out=$(delegated "$R" swap kernel --yes 2>&1); rc=$? |
| 291 | check "delegated kernel swap refuses a symlinked source directory (exit 3)" [ $rc -eq 3 ] |
| 292 | check "symlinked source directory refusal names the symlink" grep -q "symlink" <<<"$out" |
| 293 | |
| 294 | R=$T/r14g; mkroot "$R"; trusted_src "$R"; stage_make "$R" |
| 295 | chmod o+w "$R/usr/src" |
| 296 | out=$(delegated "$R" swap kernel --yes 2>&1); rc=$? |
| 297 | check "delegated kernel swap refuses a writable ancestor (exit 3)" [ $rc -eq 3 ] |
| 298 | check "writable ancestor refusal names the mode" grep -q "writable" <<<"$out" |
| 299 | check "writable ancestor runs no make" bash -c "! grep -q '^make ' '$R/calls.log' 2>/dev/null" |
| 300 | |
| 301 | R=$T/r14h; mkroot "$R"; trusted_src "$R"; stage_make "$R" |
| 302 | rm -rf "$R/lib/modules/$KVER/build" |
| 303 | mkdir -p "$R/attacker-kdir" |
| 304 | chmod 777 "$R/attacker-kdir" |
| 305 | echo 'attacker-makefile' > "$R/attacker-kdir/Makefile" |
| 306 | ln -s "$R/attacker-kdir" "$R/lib/modules/$KVER/build" |
| 307 | out=$(delegated "$R" swap kernel --yes 2>&1); rc=$? |
| 308 | check "delegated kernel swap refuses a kdir symlink to a writable tree (exit 3)" [ $rc -eq 3 ] |
| 309 | check "writable kdir refusal names the kdir" grep -Eq "writable|symlink" <<<"$out" |
| 310 | check "writable kdir symlink runs no make" bash -c "! grep -q '^make ' '$R/calls.log' 2>/dev/null" |
| 311 | |
| 312 | R=$T/r14i; mkroot "$R"; trusted_src "$R"; stage_make "$R" |
| 313 | rm -rf "$R/lib/modules/$KVER/build" |
| 314 | mkdir -p "$R/usr/src/linux-headers" |
| 315 | echo 'trusted-makefile' > "$R/usr/src/linux-headers/Makefile" |
| 316 | lock_tree "$R/usr/src/linux-headers" "$R" |
| 317 | ln -s "$R/usr/src/linux-headers" "$R/lib/modules/$KVER/build" |
| 318 | # The symlink itself is mode 777 on Linux; its owner is the fixture uid. Parent stays locked. |
| 319 | chown -h "$(id -u):$(id -g)" "$R/lib/modules/$KVER/build" 2>/dev/null || true |
| 320 | out=$(delegated "$R" swap kernel --yes 2>&1); rc=$? |
| 321 | check "delegated kernel swap accepts a locked kdir symlink (exit 0)" [ $rc -eq 0 ] |
| 322 | check "delegated kdir symlink build consumed the trusted module source" grep -qx "KBUILD:trusted-marker" "$R/calls.log" |
| 323 | check "delegated kdir symlink build did not consume the attacker source" bash -c "! grep -q 'KBUILD:attacker-marker' '$R/calls.log'" |
| 324 | |
| 325 | R=$T/r14j; mkroot "$R"; trusted_src "$R"; stage_make "$R" |
| 326 | out=$(delegated "$R" swap kernel --yes 2>&1); rc=$? |
| 327 | check "delegated kernel swap accepts a locked /usr/src/aien-gpu (exit 0)" [ $rc -eq 0 ] |
| 328 | check "delegated build consumed the trusted Kbuild" grep -qx "KBUILD:trusted-marker" "$R/calls.log" |
| 329 | check "delegated build did not consume the attacker Kbuild" bash -c "! grep -q 'KBUILD:attacker-marker' '$R/calls.log'" |
| 330 | check "delegated build installed aien_gpu.ko" [ -f "$R/lib/modules/$KVER/updates/cudapp/aien_gpu.ko" ] |
| 331 | |
| 332 | # Ubuntu's /lib is a symlink to usr/lib. The kernel build dir is reached through that link. |
| 333 | R=$T/r14k; mkroot "$R"; trusted_src "$R"; stage_make "$R" |
| 334 | rm -rf "$R/lib" |
| 335 | mkdir -p "$R/usr/lib/modules/$KVER" "$R/usr/src/linux-headers" |
| 336 | echo 'trusted-makefile' > "$R/usr/src/linux-headers/Makefile" |
| 337 | ln -s usr/lib "$R/lib" |
| 338 | ln -s "$R/usr/src/linux-headers" "$R/usr/lib/modules/$KVER/build" |
| 339 | lock_tree "$R/usr/lib/modules/$KVER" "$R" |
| 340 | lock_tree "$R/usr/src/linux-headers" "$R" |
| 341 | out=$(delegated "$R" swap kernel --yes 2>&1); rc=$? |
| 342 | check "delegated kernel swap follows a /lib -> usr/lib symlink (exit 0)" [ $rc -eq 0 ] |
| 343 | check "usr-merge build consumed the trusted module source" grep -qx "KBUILD:trusted-marker" "$R/calls.log" |
| 344 | check "usr-merge build did not consume the attacker source" bash -c "! grep -q 'KBUILD:attacker-marker' '$R/calls.log'" |
| 345 | check "usr-merge build installed aien_gpu.ko through the /lib symlink" [ -f "$R/usr/lib/modules/$KVER/updates/cudapp/aien_gpu.ko" ] |
| 346 | |
| 347 | R=$T/r14l; mkroot "$R"; trusted_src "$R"; stage_make "$R" |
| 348 | rm -rf "$R/lib" |
| 349 | mkdir -p "$T/escape/modules/$KVER/build" |
| 350 | echo 'attacker-makefile' > "$T/escape/modules/$KVER/build/Makefile" |
| 351 | ln -s "$T/escape" "$R/lib" |
| 352 | out=$(delegated "$R" swap kernel --yes 2>&1); rc=$? |
| 353 | check "delegated kernel swap refuses a /lib symlink that leaves the machine root (exit 3)" [ $rc -eq 3 ] |
| 354 | check "escaping /lib symlink names the machine root" grep -q "outside the machine root" <<<"$out" |
| 355 | check "escaping /lib symlink runs no make" bash -c "! grep -q '^make ' '$R/calls.log' 2>/dev/null" |
| 356 | check "escaping /lib symlink did not consume the attacker makefile" bash -c "! grep -q 'attacker-makefile' '$R/calls.log' 2>/dev/null" |
| 357 | |
| 358 | # 15. Delegated install path. Real root walks $SELF to / as uid 0; a user-owned |
| 359 | # checkout must be refused. The fixture walks --lib inside the fake root. |
| 360 | S=$T/esc-self; mkdir -p "$S/bin" |
| 361 | printf '#!/bin/bash\necho 0\n' > "$S/bin/id" |
| 362 | chmod +x "$S/bin/id" |
| 363 | out=$(env -u CUDAPP_ROOT -u CUDAPP_DRIVER_SRC -u CUDAPP_KDIR -u CUDAPP_LIB \ |
| 364 | PATH="$S/bin:$PATH" SUDO_UID=1000 SUDO_USER=fixture \ |
| 365 | bash "$TOOL" swap kernel --yes 2>&1); rc=$? |
| 366 | check "delegated root refuses a user-owned install script (exit 3)" [ $rc -eq 3 ] |
| 367 | check "user-owned script refusal names a trusted path" grep -Eq "trusted path is not owned by uid 0|trusted path is writable by group or other|trusted path contains a symlink" <<<"$out" |
| 368 | |
| 369 | R=$T/r15a; mkroot "$R" |
| 370 | lock_tree "$R/build/libcuda.so.1" "$R" |
| 371 | chmod g+w "$R/build/libcuda.so.1" |
| 372 | out=$(delegated "$R" swap user --yes --lib "$R/build/libcuda.so.1" 2>&1); rc=$? |
| 373 | check "delegated user swap refuses a group-writable --lib (exit 3)" [ $rc -eq 3 ] |
| 374 | check "writable --lib refusal names the mode" grep -q "writable" <<<"$out" |
| 375 | check "writable --lib does not install" [ ! -f "$R/usr/lib/cudapp/libcuda.so.1" ] |
| 376 | |
| 377 | R=$T/r15b; mkroot "$R" |
| 378 | lock_tree "$R/build/libcuda.so.1" "$R" |
| 379 | chmod o+w "$R/build" |
| 380 | out=$(delegated "$R" swap user --yes --lib "$R/build/libcuda.so.1" 2>&1); rc=$? |
| 381 | check "delegated user swap refuses a writable --lib ancestor (exit 3)" [ $rc -eq 3 ] |
| 382 | check "writable --lib ancestor refusal names the mode" grep -q "writable" <<<"$out" |
| 383 | check "writable --lib ancestor does not install" [ ! -f "$R/usr/lib/cudapp/libcuda.so.1" ] |
| 384 | |
| 385 | R=$T/r15c; mkroot "$R" |
| 386 | lock_tree "$R/build/libcuda.so.1" "$R" |
| 387 | out=$(delegated "$R" swap user --yes --lib "$R/build/libcuda.so.1" 2>&1); rc=$? |
| 388 | check "delegated user swap accepts a locked --lib (exit 0)" [ $rc -eq 0 ] |
| 389 | check "delegated user swap installed the locked library" [ -f "$R/usr/lib/cudapp/libcuda.so.1" ] |
| 390 | |
| 391 | echo "swap cli: $PASS passed, $FAIL failed" |
| 392 | [ $FAIL -eq 0 ] |