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

branch main Branches Tags History

CUDApp / tests / swap / test_swap_cli.sh

392 lines · 24913 bytes Raw Blame History
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]
7set -u
8TOOL=${1:-$(cd "$(dirname "$0")/../.." && pwd)/swap/cudapp}
9PASS=0; FAIL=0
10ok() { PASS=$((PASS+1)); echo "ok $*"; }
11bad() { FAIL=$((FAIL+1)); echo "FAIL $*"; }
12check(){ local name=$1; shift; if "$@"; then ok "$name"; else bad "$name"; fi; }
13
14KVER=7.0.0-1019-nvidia
15mkroot() { # $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}
53tree_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}
58run() { # 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
63T=$(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
66R=$T/r1; mkroot "$R"; echo "/dev/nvidia0: 108356" > "$R/fuser.out"
67out=$(run swap status 2>&1); rc=$?
68check "status exits 0" [ $rc -eq 0 ]
69check "status names GB10" grep -q "GB10 (10de:2e12)" <<<"$out"
70check "status names driver 580.173.02" grep -q "580.173.02" <<<"$out"
71check "status names Secure Boot" grep -q "Secure Boot: disabled" <<<"$out"
72check "status lists holder pid" grep -q "108356" <<<"$out"
73check "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
76R=$T/r2; mkroot "$R"; before=$(tree_hash "$R")
77out=$(run swap user 2>&1); rc=$?
78check "dry run exits 0" [ $rc -eq 0 ]
79check "dry run says DRY RUN" grep -q "DRY RUN" <<<"$out"
80check "dry run lists the ld.so.conf.d file" grep -q "etc/ld.so.conf.d/00000-cudapp-swap.conf" <<<"$out"
81check "dry run changes nothing" [ "$before" = "$(tree_hash "$R")" ]
82check "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
85out=$(run swap user --yes 2>&1); rc=$?
86check "user --yes exits 0" [ $rc -eq 0 ]
87check "our libcuda installed" cmp -s "$R/usr/lib/cudapp/libcuda.so.1" "$R/build/libcuda.so.1"
88check "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"
89check "ldconfig ran" grep -q "^ldconfig" "$R/calls.log"
90check "NVIDIA libcuda untouched" [ "$(cat "$R/usr/lib/aarch64-linux-gnu/libcuda.so.580.173.02")" = nvidia-libcuda-580 ]
91check "backup manifest written" bash -c "ls >/dev/null '$R'/var/lib/cudapp-swap/backup-*-user/manifest.tsv"
92check "status now says user level swapped" bash -c "PATH='$R/fakebin:$PATH' CUDAPP_ROOT='$R' bash '$TOOL' swap status | grep -q 'user level: SWAPPED'"
93out=$(run swap back --yes 2>&1); rc=$?
94check "back exits 0" [ $rc -eq 0 ]
95check "back reports PASS" grep -q "BACK PASS" <<<"$out"
96check "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)
99R=$T/r4; mkroot "$R"; echo "SecureBoot enabled" > "$R/sb.out"
100out=$(run swap kernel --yes 2>&1); rc=$?
101check "kernel refuses under Secure Boot (exit 3)" [ $rc -eq 3 ]
102check "refusal names Secure Boot" grep -q "Secure Boot is enabled" <<<"$out"
103
104# 5. kernel --yes refuses while processes hold /dev/nvidia*, naming them
105R=$T/r5; mkroot "$R"; echo "/dev/nvidia0: 108356 1816" > "$R/fuser.out"; before=$(tree_hash "$R")
106out=$(run swap kernel --yes 2>&1); rc=$?
107check "kernel refuses with holders (exit 3)" [ $rc -eq 3 ]
108check "refusal names the holder" grep -q "108356" <<<"$out"
109check "refusal changed nothing" [ "$before" = "$(tree_hash "$R")" ]
110
111# 6. kernel refuses on a machine without a GB10
112R=$T/r6; mkroot "$R"; echo 0x2b85 > "$R/sys/bus/pci/devices/000f:01:00.0/device"
113out=$(run swap kernel 2>&1); rc=$?
114check "kernel refuses without GB10 (exit 3)" [ $rc -eq 3 ]
115check "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
118R=$T/r7; mkroot "$R"; echo "usr/lib/modules/$KVER/kernel/drivers/video/nvidia.ko" > "$R/lsinitramfs.out"; before=$(tree_hash "$R")
119out=$(run swap kernel 2>&1)
120check "kernel dry run names the reboot" grep -q "reboot" <<<"$out"
121check "kernel dry run changed nothing" [ "$before" = "$(tree_hash "$R")" ]
122out=$(run swap kernel --yes 2>&1); rc=$?
123check "kernel --yes exits 0" [ $rc -eq 0 ]
124MC="$R/etc/modprobe.d/cudapp-swap-kernel.conf"
125for 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"
128done
129check "aien_gpu installed for the running kernel" [ -f "$R/lib/modules/$KVER/updates/cudapp/aien_gpu.ko" ]
130check "aien_gpu loads at boot" grep -qx "aien_gpu" "$R/etc/modules-load.d/cudapp-aien-gpu.conf"
131check "depmod ran for $KVER" grep -q "^depmod -a $KVER" "$R/calls.log"
132check "initramfs rebuilt (NVIDIA was in it)" grep -q "^update-initramfs -u -k $KVER" "$R/calls.log"
133check "nvidia.ko untouched" [ "$(cat "$R/lib/modules/$KVER/kernel/drivers/video/nvidia.ko")" = nvidia.ko ]
134check "never reboots by itself" bash -c "! grep -q reboot '$R/calls.log'"
135out=$(run swap back --yes 2>&1); rc=$?
136check "kernel back exits 0" [ $rc -eq 0 ]
137check "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
140R=$T/r8; mkroot "$R"; run swap user --yes > /dev/null 2>&1
141b=$(ls -d "$R"/var/lib/cudapp-swap/backup-*-user | head -1)
142echo "damaged" > "$b/files/etc/ld.so.cache"
143out=$(run swap back --yes 2>&1); rc=$?
144check "back with a damaged backup exits 1" [ $rc -eq 1 ]
145check "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
148R=$T/r9; mkroot "$R"; before=$(tree_hash "$R")
149run swap all --yes > /dev/null 2>&1; rc=$?
150check "all --yes exits 0" [ $rc -eq 0 ]
151check "all staged the user level" [ -f "$R/etc/ld.so.conf.d/00000-cudapp-swap.conf" ]
152check "all staged the kernel level" [ -f "$R/etc/modprobe.d/cudapp-swap-kernel.conf" ]
153run swap back --yes > /dev/null 2>&1
154check "one back undoes all" [ "$before" = "$(tree_hash "$R")" ]
155
156# 10. unknown level is refused with usage, exit 2
157out=$(run swap sideways 2>&1); rc=$?
158check "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.
162S=$T/esc; mkdir -p "$S/bin" "$S/lib"; echo "lib" > "$S/lib/libcuda.so.1"
163printf '#!/bin/bash\necho 1000\n' > "$S/bin/id"
164printf '#!/bin/bash\nprintf "%%s\\n" "$@" > "%s/sudo.argv"\n' "$S" > "$S/bin/sudo"
165chmod +x "$S/bin"/*
166env -u CUDAPP_ROOT PATH="$S/bin:$PATH" CUDAPP_LIB="$S/lib/../lib/libcuda.so.1" bash "$TOOL" swap user --yes > /dev/null 2>&1
167check "escalation calls sudo" [ -s "$S/sudo.argv" ]
168check "escalation drops -E (no caller environment reaches root)" bash -c "! grep -qx -- '-E' '$S/sudo.argv'"
169check "escalation re-runs this script by absolute path" [ "$(sed -n 1p "$S/sudo.argv")" = "$(readlink -f "$TOOL")" ]
170check "escalation passes --lib as a resolved absolute path" grep -qx -- "$S/lib/libcuda.so.1" "$S/sudo.argv"
171check "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"
173env -u CUDAPP_ROOT -u CUDAPP_DRIVER_SRC -u CUDAPP_KDIR PATH="$S/bin:$PATH" \
174 bash "$TOOL" swap kernel --yes > /dev/null 2>&1
175check "kernel escalation calls sudo" [ -s "$S/sudo.argv" ]
176check "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"
178out=$(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=$?
180check "caller-selected driver source is refused before sudo (exit 3)" [ $rc -eq 3 ]
181check "refused driver source never reaches sudo" [ ! -s "$S/sudo.argv" ]
182check "driver-source refusal names the delegated swap" grep -q "delegated kernel swap" <<<"$out"
183: > "$S/sudo.argv"
184out=$(env -u CUDAPP_ROOT PATH="$S/bin:$PATH" CUDAPP_DRIVER_SRC="$S/lib" \
185 bash "$TOOL" swap kernel --yes 2>&1); rc=$?
186check "CUDAPP_DRIVER_SRC is refused before sudo (exit 3)" [ $rc -eq 3 ]
187check "refused CUDAPP_DRIVER_SRC never reaches sudo" [ ! -s "$S/sudo.argv" ]
188: > "$S/sudo.argv"
189out=$(env -u CUDAPP_ROOT -u CUDAPP_DRIVER_SRC PATH="$S/bin:$PATH" \
190 bash "$TOOL" swap all --yes --kdir "$S" 2>&1); rc=$?
191check "caller-selected kdir is refused before sudo (exit 3)" [ $rc -eq 3 ]
192check "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
194printf '#!/bin/bash\necho 0\n' > "$S/bin/id"
195out=$(env -u CUDAPP_ROOT PATH="$S/bin:$PATH" bash "$TOOL" swap user --lib "$S/lib/libcuda.so.1" 2>&1); rc=$?
196check "root: --lib outside the repository refused (exit 3)" [ $rc -eq 3 ]
197check "root: refusal names the repository" grep -q "outside the CUDA++ repository" <<<"$out"
198REPO=$(cd "$(dirname "$(readlink -f "$TOOL")")/.." && pwd -P)
199out=$(env -u CUDAPP_ROOT PATH="$S/bin:$PATH" CUDAPP_LIB="$S/lib/libcuda.so.1" bash "$TOOL" swap all 2>&1); rc=$?
200check "root: CUDAPP_LIB outside the repository refused for 'all' (exit 3)" [ $rc -eq 3 ]
201out=$(env -u CUDAPP_ROOT PATH="$S/bin:$PATH" bash "$TOOL" swap kernel --kdir "$S" 2>&1); rc=$?
202check "root: --kdir outside /lib/modules and /usr/src refused (exit 3)" [ $rc -eq 3 ]
203check "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)
205out=$(env -u CUDAPP_ROOT PATH="$S/bin:$PATH" bash "$TOOL" swap user --lib "$REPO/LICENSE" 2>&1); rc=$?
206check "root: --lib inside the repository accepted (dry run exits 0)" [ $rc -eq 0 ]
207check "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
209R=$T/r13; mkroot "$R"; before=$(tree_hash "$R")
210printf '#!/bin/bash\nfor a; do d=$a; done\necho tampered > "$d"\n' > "$R/fakebin/install"; chmod +x "$R/fakebin/install"
211out=$(run swap user --yes 2>&1); rc=$?
212check "differing installed library fails (exit 1)" [ $rc -eq 1 ]
213check "failure names the sha256 mismatch" grep -q "sha256 .* != .* planned" <<<"$out"
214check "differing installed library rolled back" [ "$before" = "$(tree_hash "$R")" ]
215rm "$R/fakebin/install"; out=$(run swap user --yes 2>&1)
216check "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.
221lock_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}
230stage_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}
235delegated() { # $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}
241trusted_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
252R=$T/r14a; mkroot "$R"; trusted_src "$R"; stage_make "$R"
253out=$(delegated "$R" swap kernel --yes --driver-src "$R/attacker" 2>&1); rc=$?
254check "delegated kernel swap refuses --driver-src (exit 3)" [ $rc -eq 3 ]
255check "delegated --driver-src refusal names the delegated swap" grep -q "delegated kernel swap" <<<"$out"
256check "delegated --driver-src runs no make" bash -c "! grep -q '^make ' '$R/calls.log' 2>/dev/null"
257check "delegated --driver-src does not install a module" [ ! -e "$R/lib/modules/$KVER/updates/cudapp/aien_gpu.ko" ]
258
259R=$T/r14b; mkroot "$R"; trusted_src "$R"; stage_make "$R"
260out=$(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=$?
262check "delegated kernel swap refuses CUDAPP_DRIVER_SRC (exit 3)" [ $rc -eq 3 ]
263check "delegated CUDAPP_DRIVER_SRC refusal names the delegated swap" grep -q "delegated kernel swap" <<<"$out"
264check "delegated CUDAPP_DRIVER_SRC runs no make" bash -c "! grep -q '^make ' '$R/calls.log' 2>/dev/null"
265
266R=$T/r14c; mkroot "$R"; trusted_src "$R"; stage_make "$R"
267out=$(delegated "$R" swap kernel --yes --kdir "$R/attacker" 2>&1); rc=$?
268check "delegated kernel swap refuses --kdir (exit 3)" [ $rc -eq 3 ]
269check "delegated --kdir refusal names the delegated swap" grep -q "delegated kernel swap" <<<"$out"
270check "delegated --kdir runs no make" bash -c "! grep -q '^make ' '$R/calls.log' 2>/dev/null"
271
272R=$T/r14d; mkroot "$R"; trusted_src "$R"; stage_make "$R"
273chmod o+w "$R/usr/src/aien-gpu/Kbuild"
274out=$(delegated "$R" swap kernel --yes 2>&1); rc=$?
275check "delegated kernel swap refuses a group-or-other-writable source file (exit 3)" [ $rc -eq 3 ]
276check "writable source refusal names the file mode" grep -q "writable" <<<"$out"
277check "writable source file runs no make" bash -c "! grep -q '^make ' '$R/calls.log' 2>/dev/null"
278
279R=$T/r14e; mkroot "$R"; trusted_src "$R"; stage_make "$R"
280rm "$R/usr/src/aien-gpu/aien_gpu.c"
281ln -s "$R/attacker/aien_gpu.c" "$R/usr/src/aien-gpu/aien_gpu.c"
282out=$(delegated "$R" swap kernel --yes 2>&1); rc=$?
283check "delegated kernel swap refuses a symlink in the source (exit 3)" [ $rc -eq 3 ]
284check "source symlink refusal names the symlink" grep -q "symlink" <<<"$out"
285check "source symlink runs no make" bash -c "! grep -q '^make ' '$R/calls.log' 2>/dev/null"
286
287R=$T/r14f; mkroot "$R"; trusted_src "$R"; stage_make "$R"
288rm -rf "$R/usr/src/aien-gpu"
289ln -s "$R/attacker" "$R/usr/src/aien-gpu"
290out=$(delegated "$R" swap kernel --yes 2>&1); rc=$?
291check "delegated kernel swap refuses a symlinked source directory (exit 3)" [ $rc -eq 3 ]
292check "symlinked source directory refusal names the symlink" grep -q "symlink" <<<"$out"
293
294R=$T/r14g; mkroot "$R"; trusted_src "$R"; stage_make "$R"
295chmod o+w "$R/usr/src"
296out=$(delegated "$R" swap kernel --yes 2>&1); rc=$?
297check "delegated kernel swap refuses a writable ancestor (exit 3)" [ $rc -eq 3 ]
298check "writable ancestor refusal names the mode" grep -q "writable" <<<"$out"
299check "writable ancestor runs no make" bash -c "! grep -q '^make ' '$R/calls.log' 2>/dev/null"
300
301R=$T/r14h; mkroot "$R"; trusted_src "$R"; stage_make "$R"
302rm -rf "$R/lib/modules/$KVER/build"
303mkdir -p "$R/attacker-kdir"
304chmod 777 "$R/attacker-kdir"
305echo 'attacker-makefile' > "$R/attacker-kdir/Makefile"
306ln -s "$R/attacker-kdir" "$R/lib/modules/$KVER/build"
307out=$(delegated "$R" swap kernel --yes 2>&1); rc=$?
308check "delegated kernel swap refuses a kdir symlink to a writable tree (exit 3)" [ $rc -eq 3 ]
309check "writable kdir refusal names the kdir" grep -Eq "writable|symlink" <<<"$out"
310check "writable kdir symlink runs no make" bash -c "! grep -q '^make ' '$R/calls.log' 2>/dev/null"
311
312R=$T/r14i; mkroot "$R"; trusted_src "$R"; stage_make "$R"
313rm -rf "$R/lib/modules/$KVER/build"
314mkdir -p "$R/usr/src/linux-headers"
315echo 'trusted-makefile' > "$R/usr/src/linux-headers/Makefile"
316lock_tree "$R/usr/src/linux-headers" "$R"
317ln -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.
319chown -h "$(id -u):$(id -g)" "$R/lib/modules/$KVER/build" 2>/dev/null || true
320out=$(delegated "$R" swap kernel --yes 2>&1); rc=$?
321check "delegated kernel swap accepts a locked kdir symlink (exit 0)" [ $rc -eq 0 ]
322check "delegated kdir symlink build consumed the trusted module source" grep -qx "KBUILD:trusted-marker" "$R/calls.log"
323check "delegated kdir symlink build did not consume the attacker source" bash -c "! grep -q 'KBUILD:attacker-marker' '$R/calls.log'"
324
325R=$T/r14j; mkroot "$R"; trusted_src "$R"; stage_make "$R"
326out=$(delegated "$R" swap kernel --yes 2>&1); rc=$?
327check "delegated kernel swap accepts a locked /usr/src/aien-gpu (exit 0)" [ $rc -eq 0 ]
328check "delegated build consumed the trusted Kbuild" grep -qx "KBUILD:trusted-marker" "$R/calls.log"
329check "delegated build did not consume the attacker Kbuild" bash -c "! grep -q 'KBUILD:attacker-marker' '$R/calls.log'"
330check "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.
333R=$T/r14k; mkroot "$R"; trusted_src "$R"; stage_make "$R"
334rm -rf "$R/lib"
335mkdir -p "$R/usr/lib/modules/$KVER" "$R/usr/src/linux-headers"
336echo 'trusted-makefile' > "$R/usr/src/linux-headers/Makefile"
337ln -s usr/lib "$R/lib"
338ln -s "$R/usr/src/linux-headers" "$R/usr/lib/modules/$KVER/build"
339lock_tree "$R/usr/lib/modules/$KVER" "$R"
340lock_tree "$R/usr/src/linux-headers" "$R"
341out=$(delegated "$R" swap kernel --yes 2>&1); rc=$?
342check "delegated kernel swap follows a /lib -> usr/lib symlink (exit 0)" [ $rc -eq 0 ]
343check "usr-merge build consumed the trusted module source" grep -qx "KBUILD:trusted-marker" "$R/calls.log"
344check "usr-merge build did not consume the attacker source" bash -c "! grep -q 'KBUILD:attacker-marker' '$R/calls.log'"
345check "usr-merge build installed aien_gpu.ko through the /lib symlink" [ -f "$R/usr/lib/modules/$KVER/updates/cudapp/aien_gpu.ko" ]
346
347R=$T/r14l; mkroot "$R"; trusted_src "$R"; stage_make "$R"
348rm -rf "$R/lib"
349mkdir -p "$T/escape/modules/$KVER/build"
350echo 'attacker-makefile' > "$T/escape/modules/$KVER/build/Makefile"
351ln -s "$T/escape" "$R/lib"
352out=$(delegated "$R" swap kernel --yes 2>&1); rc=$?
353check "delegated kernel swap refuses a /lib symlink that leaves the machine root (exit 3)" [ $rc -eq 3 ]
354check "escaping /lib symlink names the machine root" grep -q "outside the machine root" <<<"$out"
355check "escaping /lib symlink runs no make" bash -c "! grep -q '^make ' '$R/calls.log' 2>/dev/null"
356check "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.
360S=$T/esc-self; mkdir -p "$S/bin"
361printf '#!/bin/bash\necho 0\n' > "$S/bin/id"
362chmod +x "$S/bin/id"
363out=$(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=$?
366check "delegated root refuses a user-owned install script (exit 3)" [ $rc -eq 3 ]
367check "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
369R=$T/r15a; mkroot "$R"
370lock_tree "$R/build/libcuda.so.1" "$R"
371chmod g+w "$R/build/libcuda.so.1"
372out=$(delegated "$R" swap user --yes --lib "$R/build/libcuda.so.1" 2>&1); rc=$?
373check "delegated user swap refuses a group-writable --lib (exit 3)" [ $rc -eq 3 ]
374check "writable --lib refusal names the mode" grep -q "writable" <<<"$out"
375check "writable --lib does not install" [ ! -f "$R/usr/lib/cudapp/libcuda.so.1" ]
376
377R=$T/r15b; mkroot "$R"
378lock_tree "$R/build/libcuda.so.1" "$R"
379chmod o+w "$R/build"
380out=$(delegated "$R" swap user --yes --lib "$R/build/libcuda.so.1" 2>&1); rc=$?
381check "delegated user swap refuses a writable --lib ancestor (exit 3)" [ $rc -eq 3 ]
382check "writable --lib ancestor refusal names the mode" grep -q "writable" <<<"$out"
383check "writable --lib ancestor does not install" [ ! -f "$R/usr/lib/cudapp/libcuda.so.1" ]
384
385R=$T/r15c; mkroot "$R"
386lock_tree "$R/build/libcuda.so.1" "$R"
387out=$(delegated "$R" swap user --yes --lib "$R/build/libcuda.so.1" 2>&1); rc=$?
388check "delegated user swap accepts a locked --lib (exit 0)" [ $rc -eq 0 ]
389check "delegated user swap installed the locked library" [ -f "$R/usr/lib/cudapp/libcuda.so.1" ]
390
391echo "swap cli: $PASS passed, $FAIL failed"
392[ $FAIL -eq 0 ]