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
Comparing 84b524cadaaa8bc9b556adfaea9d6dd2e0d67a66 ... 21d8de285f713adf09e0d6ba6db2b37139d0674e
- elementwise: host gate locks the 1d tail chunk at its own byte count
- swap: a delegated root build no longer runs caller-selected Kbuild
6 files changed, +549 -18
docs/runtime/omega_runtime_api.internal
| @@ -137,6 +137,7 @@ fn omega_gpu_elementwise_codegen_ir # kernel builder used by the library and its | ||
| 137 | 137 | fn omega_gpu_elementwise_last_error # serving-daemon internals; aien-sovereign-core does not bind it; candidate EXPERIMENTAL |
| 138 | 138 | fn omega_gpu_elementwise_test_set_mutant # TEST ONLY: test hook (mutant, simulator, split or counter) for the library's own batteries |
| 139 | 139 | fn omega_gpu_elementwise_test_set_mutant_level # TEST ONLY: test hook (mutant, simulator, split or counter) for the library's own batteries |
| 140 | +fn omega_gpu_elementwise_test_stage_bytes # TEST ONLY: host probe of the 1d chunk byte count; does not launch | |
| 140 | 141 | fn omega_gpu_engine_test_force_block # TEST ONLY: a blocked process must stay blocked because the GPU may still be running |
| 141 | 142 | fn omega_gpu_engine_test_reset_block # TEST ONLY: a blocked process must stay blocked because the GPU may still be running |
| 142 | 143 | fn omega_gpu_engine_test_set_clock # TEST ONLY: a blocked process must stay blocked because the GPU may still be running |
src/omega_gpu_elementwise_api.c
| @@ -1117,29 +1117,77 @@ int omega_gpu_rope_f32(uint32_t heads, uint32_t head_dim, const float *v, | ||
| 1117 | 1117 | return rc; |
| 1118 | 1118 | } |
| 1119 | 1119 | |
| 1120 | +/* Bytes this chunk may stage. rec[0] is this chunk's count (av_ew_rec caps a | |
| 1121 | + * 1d chunk at 131072). The plan's p[10] is that cap for the whole n, so a | |
| 1122 | + * shorter tail must not reuse it: swiglu_131073's last chunk is 4 bytes, and | |
| 1123 | + * copying p[10] (524288) wrote past the caller's buffer (97fc9eb3). */ | |
| 1124 | +static int ew_1d_extent(uint32_t remaining, uint64_t record_count, uint32_t *count, size_t *bytes) { | |
| 1125 | + if (record_count == 0 || record_count > remaining || record_count > (uint64_t)SIZE_MAX / 4u) | |
| 1126 | + return OMEGA_GPU_EW_BAD_ARGS; | |
| 1127 | + *count = (uint32_t)record_count; | |
| 1128 | + *bytes = (size_t)record_count * 4u; | |
| 1129 | + return OMEGA_GPU_EW_OK; | |
| 1130 | +} | |
| 1131 | + | |
| 1132 | +/* TEST ONLY. When set, run_1d records each chunk's byte count and does not | |
| 1133 | + * open the device or touch the caller buffers. The host gate reads it back. */ | |
| 1134 | +static int g_stage_probe; | |
| 1135 | +static size_t g_stage_bytes[8]; | |
| 1136 | +static uint32_t g_stage_n; | |
| 1137 | + | |
| 1120 | 1138 | static int run_1d(OmegaGpuEwOp op, uint32_t n, const void *a, const void *b, void *c, OmegaGpuEwInfo *info) { |
| 1121 | 1139 | uint64_t t0 = now_ns(); |
| 1122 | 1140 | uint64_t p[16]={n};int plan_rc=ap_plan(AP_EW,2,p); |
| 1123 | 1141 | if(plan_rc)return plan_rc; |
| 1124 | 1142 | Slot *k = NULL; |
| 1125 | - int rc = begin(op, &k, info); | |
| 1126 | - if (rc != OMEGA_GPU_EW_OK) return rc; | |
| 1143 | + int rc = OMEGA_GPU_EW_OK; | |
| 1144 | + int locked = 0; | |
| 1145 | + if (!g_stage_probe) { | |
| 1146 | + rc = begin(op, &k, info); | |
| 1147 | + if (rc != OMEGA_GPU_EW_OK) return rc; | |
| 1148 | + locked = 1; | |
| 1149 | + } | |
| 1127 | 1150 | for (uint32_t i0 = 0; i0 < n && rc == OMEGA_GPU_EW_OK; ) { |
| 1128 | 1151 | uint64_t rec[8]; |
| 1129 | 1152 | if (av_ew_rec(0, n - i0, 0, 0, rec)) { rc = OMEGA_GPU_EW_BAD_ARGS; break; } |
| 1130 | - uint32_t ni = (uint32_t)rec[0]; | |
| 1153 | + uint32_t ni = 0; | |
| 1131 | 1154 | uint32_t grid = (uint32_t)rec[1]; |
| 1155 | + size_t bytes = 0; | |
| 1156 | + if (ew_1d_extent(n - i0, rec[0], &ni, &bytes) != OMEGA_GPU_EW_OK) { | |
| 1157 | + rc = OMEGA_GPU_EW_BAD_ARGS; | |
| 1158 | + break; | |
| 1159 | + } | |
| 1132 | 1160 | uint32_t params[4] = { ni, 0, 0, 0 }; |
| 1133 | - size_t bytes = (size_t)ni * 4; /* this chunk's bytes: the plan's p[10] is not per chunk */ | |
| 1161 | + if (g_stage_probe) { | |
| 1162 | + if (g_stage_n < (uint32_t)(sizeof g_stage_bytes / sizeof g_stage_bytes[0])) | |
| 1163 | + g_stage_bytes[g_stage_n] = bytes; | |
| 1164 | + g_stage_n++; | |
| 1165 | + i0 += ni; | |
| 1166 | + continue; | |
| 1167 | + } | |
| 1134 | 1168 | rc = run_abc(k, (const uint8_t *)a + (size_t)i0 * 4, bytes, b ? (const uint8_t *)b + (size_t)i0 * 4 : a, bytes, |
| 1135 | 1169 | (uint8_t *)c + (size_t)i0 * 4, bytes, (uint32_t)rec[6], grid, params, info); |
| 1136 | 1170 | i0 += ni; |
| 1137 | 1171 | } |
| 1138 | - UNLOCK(); | |
| 1172 | + if (locked) UNLOCK(); | |
| 1139 | 1173 | info_end(info, t0); |
| 1140 | 1174 | return rc; |
| 1141 | 1175 | } |
| 1142 | 1176 | |
| 1177 | +int omega_gpu_elementwise_test_stage_bytes(uint32_t n, size_t *bytes, uint32_t cap, uint32_t *nchunks) { | |
| 1178 | + static const uint32_t dummy = 0; | |
| 1179 | + if (!bytes || !nchunks || cap == 0 || n == 0) return OMEGA_GPU_EW_BAD_ARGS; | |
| 1180 | + g_stage_n = 0; | |
| 1181 | + g_stage_probe = 1; | |
| 1182 | + int rc = run_1d(OMEGA_GPU_EW_EX2, n, &dummy, NULL, (void *)&dummy, NULL); | |
| 1183 | + g_stage_probe = 0; | |
| 1184 | + if (rc != OMEGA_GPU_EW_OK) return rc; | |
| 1185 | + if (g_stage_n > cap) return OMEGA_GPU_EW_TOO_LARGE; | |
| 1186 | + for (uint32_t i = 0; i < g_stage_n; i++) bytes[i] = g_stage_bytes[i]; | |
| 1187 | + *nchunks = g_stage_n; | |
| 1188 | + return OMEGA_GPU_EW_OK; | |
| 1189 | +} | |
| 1190 | + | |
| 1143 | 1191 | int omega_gpu_swiglu_f32(uint32_t n, const float *gate, const float *up, float *out, OmegaGpuEwInfo *info) { |
| 1144 | 1192 | if (!gate || !up || !out) return OMEGA_GPU_EW_BAD_ARGS; |
| 1145 | 1193 | return run_1d(OMEGA_GPU_EW_SWIGLU, n, gate, up, out, info); |
src/omega_gpu_elementwise_api.h
| @@ -170,6 +170,11 @@ void omega_gpu_elementwise_test_set_mutant(int op); | ||
| 170 | 170 | * corrupted) that the next launch of `op` builds; lets a chip test observe what a missing |
| 171 | 171 | * reconvergence does (the simulator must have flagged it first). */ |
| 172 | 172 | void omega_gpu_elementwise_test_set_mutant_level(int op, int level); |
| 173 | +/* TEST ONLY. Byte count run_1d stages for each chunk of an n-element call. | |
| 174 | + * Does not open a device or read the caller's buffers. bytes[i] is chunk i. | |
| 175 | + * The plan's full-chunk length is not reused for a shorter tail. */ | |
| 176 | +int omega_gpu_elementwise_test_stage_bytes(uint32_t n, size_t *bytes, uint32_t cap, | |
| 177 | + uint32_t *nchunks); | |
| 173 | 178 | |
| 174 | 179 | const char *omega_gpu_elementwise_rc_name(int rc); |
| 175 | 180 | void omega_gpu_elementwise_cache_clear(void); |
swap/cudapp
| @@ -19,13 +19,24 @@ | ||
| 19 | 19 | # tests/swap/test_swap_cli.sh runs every level against a fake machine root. |
| 20 | 20 | # |
| 21 | 21 | # Sources: --lib PATH (CUDA++ libcuda.so.1), --driver-src PATH (aien_gpu source), --kdir PATH (kernel build dir). |
| 22 | -# CUDAPP_LIB, CUDAPP_DRIVER_SRC and CUDAPP_KDIR give the defaults for an unprivileged run. Escalation runs | |
| 23 | -# `sudo "$0" swap ...` WITHOUT -E and passes the resolved paths as these options, so a sudoers rule naming | |
| 24 | -# this script cannot be steered by the caller's environment. Running as root on the real system, the library | |
| 25 | -# must be a regular file inside this repository and the kernel build dir must live under /lib/modules or /usr/src. | |
| 26 | -# The aien_gpu source is built as root by design (a kernel module is root-equivalent): point it at a tree | |
| 27 | -# only root or you can write. | |
| 22 | +# CUDAPP_LIB, CUDAPP_DRIVER_SRC and CUDAPP_KDIR are the defaults for a run that is not a delegated root build. | |
| 23 | +# Escalation runs `sudo "$0" swap ...` WITHOUT -E. A sudoers rule that names only this script rejects the | |
| 24 | +# caller's environment (BASH_ENV, SUDO_UID, CUDAPP_DRIVER_SRC, sudo -E); it still allows arguments. Those | |
| 25 | +# arguments must not choose the tree make runs as root. A delegated kernel swap (SUDO_UID set to a non-root | |
| 26 | +# user, or this script's own sudo re-entry) builds /usr/src/aien-gpu and /lib/modules/$(uname -r)/build. | |
| 27 | +# Both must be owned by root and not writable by group or other, and neither may carry an ACL. The module | |
| 28 | +# source must contain no symlink. /lib -> usr/lib and the kernel's build symlink are followed when the link | |
| 29 | +# is owned by root and the resolved directory stays under /lib/modules, /usr/lib/modules or /usr/src. | |
| 30 | +# make and cc are taken from a fixed PATH whose directories and resolved binaries are root-owned and | |
| 31 | +# not group/other-writable, so a caller cannot plant a compiler via PATH, CC or MAKEFLAGS. | |
| 32 | +# A root shell with SUDO_UID unset may still pass --driver-src and --kdir; a shell started by sudo keeps | |
| 33 | +# SUDO_UID, so unset it before a custom-source build. Do not grant SETENV on the sudoers rule. | |
| 34 | +# The sudoers rule must name a root-owned install path. Delegated mode refuses unless this script | |
| 35 | +# ($SELF) and every ancestor is owned by root and not group/other-writable. A delegated user or all | |
| 36 | +# swap applies the same ownership check to --lib and its ancestors. | |
| 28 | 37 | set -u |
| 38 | +DRIVER_SRC_ENV_SET=0; [ "${CUDAPP_DRIVER_SRC+x}" = x ] && DRIVER_SRC_ENV_SET=1 | |
| 39 | +KDIR_ENV_SET=0; [ "${CUDAPP_KDIR+x}" = x ] && KDIR_ENV_SET=1 | |
| 29 | 40 | ROOT=${CUDAPP_ROOT:-} |
| 30 | 41 | STATE=$ROOT/var/lib/cudapp-swap |
| 31 | 42 | SELF=$(readlink -f "$0") |
| @@ -46,6 +57,7 @@ INITRD=/boot/initrd.img-$KVER | ||
| 46 | 57 | NV_MODULES="nvidia nvidia_uvm nvidia_modeset nvidia_drm" |
| 47 | 58 | |
| 48 | 59 | YES=0; REBOOT=0; HOLDERS_OK=0 |
| 60 | +DRIVER_SRC_OPTION=0; KDIR_OPTION=0 | |
| 49 | 61 | die() { echo "cudapp: $*" >&2; exit 1; } |
| 50 | 62 | refuse() { echo "REFUSED: $*" >&2; exit 3; } |
| 51 | 63 | # A machine that schedules GPU work through spark-window (the invoking user has the tool) reboots only inside a window. |
| @@ -57,6 +69,235 @@ window_host() { | ||
| 57 | 69 | } |
| 58 | 70 | usage() { sed -n '3,9p' "$0" | sed 's/^# \{0,1\}//' >&2; exit 2; } |
| 59 | 71 | sha() { if [ -e "$1" ] || [ -L "$1" ]; then sha256sum < "$1" | cut -d' ' -f1; else echo -; fi; } |
| 72 | +# sudo sets SUDO_UID itself. A sudoers rule without SETENV rejects `sudo SUDO_UID=...`; do not trust a | |
| 73 | +# value the caller could have kept only when SETENV is granted (that grant also allows BASH_ENV). | |
| 74 | +sudo_origin() { [ -n "${SUDO_UID:-}" ] && [ "$SUDO_UID" != 0 ]; } | |
| 75 | +caller_selected_build() { | |
| 76 | + [ "$DRIVER_SRC_OPTION" -eq 1 ] || [ "$KDIR_OPTION" -eq 1 ] || \ | |
| 77 | + [ "$DRIVER_SRC_ENV_SET" -eq 1 ] || [ "$KDIR_ENV_SET" -eq 1 ] | |
| 78 | +} | |
| 79 | +delegated_run() { | |
| 80 | + sudo_origin && { [ "$(id -u)" -eq 0 ] || [ -n "$ROOT" ]; } | |
| 81 | +} | |
| 82 | +delegated_build() { | |
| 83 | + case $level in kernel|all) ;; *) return 1;; esac | |
| 84 | + delegated_run | |
| 85 | +} | |
| 86 | +# A sudoers rule that names this script would otherwise exec a user-writable checkout as root. | |
| 87 | +# On the real system, $SELF and (for user|all) the resolved --lib must be root-owned all the way | |
| 88 | +# to /. The fixture (CUDAPP_ROOT set) cannot walk $SELF: the tool under test lives outside the | |
| 89 | +# fake machine root. It still walks --lib inside that root, with the fixture uid standing in for 0. | |
| 90 | +require_delegated_install() { | |
| 91 | + local owner stop lib | |
| 92 | + if [ -n "$ROOT" ]; then | |
| 93 | + stop=${ROOT%/} | |
| 94 | + owner=$(stat -c %u "$stop") || refuse "cannot stat the test machine root" | |
| 95 | + case $level in | |
| 96 | + user|all) | |
| 97 | + lib=$(readlink -e "$LIB_SRC") || refuse "--lib $LIB_SRC does not exist" | |
| 98 | + [ -f "$lib" ] || refuse "--lib $lib is not a regular file" | |
| 99 | + case $lib in | |
| 100 | + "$stop"|"$stop"/*) ;; | |
| 101 | + *) refuse "--lib $lib is outside the test machine root";; | |
| 102 | + esac | |
| 103 | + walk_owned "$lib" "$stop" "$owner" | |
| 104 | + LIB_SRC=$lib | |
| 105 | + ;; | |
| 106 | + esac | |
| 107 | + return 0 | |
| 108 | + fi | |
| 109 | + walk_owned "$SELF" / 0 | |
| 110 | + case $level in | |
| 111 | + user|all) | |
| 112 | + lib=$(readlink -e "$LIB_SRC") || refuse "--lib $LIB_SRC does not exist" | |
| 113 | + case $lib in "$REPO_DIR"/*) ;; *) refuse "--lib $lib is outside the CUDA++ repository $REPO_DIR";; esac | |
| 114 | + [ -f "$lib" ] || refuse "--lib $lib is not a regular file" | |
| 115 | + walk_owned "$lib" / 0 | |
| 116 | + LIB_SRC=$lib | |
| 117 | + ;; | |
| 118 | + esac | |
| 119 | +} | |
| 120 | +# $1 path, $2 owner uid. A symlink's mode is ignored by the kernel, so only its owner is checked here. | |
| 121 | +# An ACL can grant write without setting the mode bits, and this helper has no getfacl. GNU ls prints '+' | |
| 122 | +# after the mode when an ACL is present ('.' is a security context and is left alone). Any ACL is refused. | |
| 123 | +path_owner_mode() { | |
| 124 | + local p=$1 owner=$2 uid mode perm mark | |
| 125 | + [ -e "$p" ] || [ -L "$p" ] || refuse "trusted path does not exist: $p" | |
| 126 | + uid=$(stat -c %u "$p") || refuse "cannot stat $p" | |
| 127 | + [ "$uid" = "$owner" ] || refuse "trusted path is not owned by uid $owner: $p" | |
| 128 | + if [ -L "$p" ]; then return 0; fi | |
| 129 | + mode=$(stat -c %a "$p") || refuse "cannot stat $p" | |
| 130 | + perm=$((8#$mode & 8#022)) | |
| 131 | + [ "$perm" -eq 0 ] || refuse "trusted path is writable by group or other: $p" | |
| 132 | + mark=$(ls -ld "$p" | awk '{print $1}') || refuse "cannot list $p" | |
| 133 | + case $mark in ??????????+*) refuse "trusted path has an ACL: $p";; esac | |
| 134 | +} | |
| 135 | +# Collapse "." and ".." without following symlinks. | |
| 136 | +normalize() { | |
| 137 | + local in=$1 part out= | |
| 138 | + case $in in /*) ;; *) in=/$in;; esac | |
| 139 | + while [ "$in" != / ] && [ -n "$in" ]; do | |
| 140 | + in=${in#/} | |
| 141 | + part=${in%%/*} | |
| 142 | + if [ "$part" = "$in" ]; then in=; else in=${in#*/}; fi | |
| 143 | + case $part in | |
| 144 | + ''|.) ;; | |
| 145 | + ..) [ -n "$out" ] && out=${out%/*};; | |
| 146 | + *) out=$out/$part;; | |
| 147 | + esac | |
| 148 | + done | |
| 149 | + [ -n "$out" ] || out=/ | |
| 150 | + printf '%s\n' "$out" | |
| 151 | +} | |
| 152 | +# Walk $1 up through $2 inclusive. No component may be a symlink. | |
| 153 | +walk_owned() { | |
| 154 | + local p=$1 stop=$2 owner=$3 | |
| 155 | + while :; do | |
| 156 | + [ ! -L "$p" ] || refuse "trusted path contains a symlink: $p" | |
| 157 | + path_owner_mode "$p" "$owner" | |
| 158 | + [ "$p" = "$stop" ] && break | |
| 159 | + [ "$p" = / ] && break | |
| 160 | + p=${p%/*}; [ -n "$p" ] || p=/ | |
| 161 | + done | |
| 162 | +} | |
| 163 | +apply_delegated_sources() { | |
| 164 | + local owner stop src kd target shown | |
| 165 | + if sudo_origin && [ "$EUID" -eq 0 ] && [ -n "$ROOT" ]; then | |
| 166 | + refuse "CUDAPP_ROOT is refused for a delegated root swap" | |
| 167 | + fi | |
| 168 | + if caller_selected_build; then | |
| 169 | + refuse "a delegated kernel swap cannot take --driver-src, --kdir, CUDAPP_DRIVER_SRC or CUDAPP_KDIR; root uses /usr/src/aien-gpu and /lib/modules/<uname -r>/build" | |
| 170 | + fi | |
| 171 | + # The fixture tests are not root. A real delegated root build also pins make and cc. | |
| 172 | + if [ "$EUID" -eq 0 ]; then | |
| 173 | + require_trusted_make_path | |
| 174 | + fi | |
| 175 | + if [ -n "$ROOT" ]; then | |
| 176 | + stop=${ROOT%/} | |
| 177 | + owner=$(stat -c %u "$stop") || refuse "cannot stat the test machine root" | |
| 178 | + src=$stop/usr/src/aien-gpu | |
| 179 | + kd=$stop/lib/modules/$KVER/build | |
| 180 | + else | |
| 181 | + stop=/ | |
| 182 | + owner=0 | |
| 183 | + src=/usr/src/aien-gpu | |
| 184 | + kd=/lib/modules/$KVER/build | |
| 185 | + fi | |
| 186 | + [ -d "$src" ] || [ -L "$src" ] || refuse "trusted aien_gpu source not found at /usr/src/aien-gpu" | |
| 187 | + walk_owned "$src" "$stop" "$owner" | |
| 188 | + shown=$(find -P "$src" \( -type l -o ! -uid "$owner" -o -perm /022 -o ! \( -type f -o -type d \) \) -print -quit 2>/dev/null) || \ | |
| 189 | + refuse "cannot inspect trusted aien_gpu source at /usr/src/aien-gpu" | |
| 190 | + [ -z "$shown" ] || refuse "trusted aien_gpu source must contain no symlink, be owned by uid $owner, and not be writable by group or other: $shown" | |
| 191 | + [ -f "$src/Kbuild" ] && [ ! -L "$src/Kbuild" ] || refuse "trusted aien_gpu source has no regular Kbuild at /usr/src/aien-gpu" | |
| 192 | + while IFS= read -r shown; do | |
| 193 | + [ -n "$shown" ] || continue | |
| 194 | + refuse "trusted aien_gpu source has an ACL: $shown" | |
| 195 | + done < <(find -P "$src" \( -type f -o -type d \) -exec ls -ld {} + | awk 'substr($1, 11, 1) == "+" { print $NF; exit }') | |
| 196 | + # /lib is a symlink to usr/lib on a merged /usr, and the kernel build dir is often itself a symlink | |
| 197 | + # into /usr/src. Follow those links. Each link is owned by the trusted uid, and every real directory | |
| 198 | + # on the resolved path is owned by that uid and not group/other-writable. The result stays under the | |
| 199 | + # machine root, and on the real system under /lib/modules, /usr/lib/modules or /usr/src. | |
| 200 | + [ -e "$kd" ] || [ -L "$kd" ] || refuse "kernel headers for $KVER not found at /lib/modules/$KVER/build" | |
| 201 | + follow_trusted "$kd" "$stop" "$owner" | |
| 202 | + target=$RESOLVED | |
| 203 | + if [ -z "$ROOT" ]; then | |
| 204 | + case $target in /lib/modules/*|/usr/lib/modules/*|/usr/src/*) ;; *) refuse "kernel build dir $target is not under /lib/modules or /usr/src";; esac | |
| 205 | + fi | |
| 206 | + [ -d "$target" ] || refuse "kernel build dir $target is not a directory" | |
| 207 | + DRIVER_SRC=$src | |
| 208 | + KDIR=$target | |
| 209 | +} | |
| 210 | +# Resolve $1 by following symlinks. Sets RESOLVED. Components at or above $2 are the trust boundary | |
| 211 | +# (the fixture root in a test, / on the machine) and are not walked. A link that leaves $2 is refused. | |
| 212 | +follow_trusted() { | |
| 213 | + local path=$1 stop=$2 owner=$3 | |
| 214 | + local rest cur part next link dest suffix old hops=0 | |
| 215 | + if [ "$stop" = / ]; then | |
| 216 | + case $path in /*) ;; *) refuse "trusted path is outside the machine root: $path";; esac | |
| 217 | + else | |
| 218 | + case $path in | |
| 219 | + "$stop"|"$stop"/*) ;; | |
| 220 | + *) refuse "trusted path is outside the machine root: $path";; | |
| 221 | + esac | |
| 222 | + fi | |
| 223 | + if [ "$stop" = / ]; then | |
| 224 | + rest=${path#/} | |
| 225 | + cur= | |
| 226 | + else | |
| 227 | + path_owner_mode "$stop" "$owner" | |
| 228 | + rest=${path#"$stop"} | |
| 229 | + rest=${rest#/} | |
| 230 | + cur=$stop | |
| 231 | + fi | |
| 232 | + while [ -n "$rest" ]; do | |
| 233 | + part=${rest%%/*} | |
| 234 | + if [ "$part" = "$rest" ]; then rest=; else rest=${rest#*/}; fi | |
| 235 | + case $part in | |
| 236 | + ''|.) continue;; | |
| 237 | + ..) | |
| 238 | + if [ "$cur" = "$stop" ] || [ -z "$cur" ] || [ "$cur" = / ]; then | |
| 239 | + refuse "trusted path climbs above its root: $path" | |
| 240 | + fi | |
| 241 | + cur=${cur%/*} | |
| 242 | + [ -n "$cur" ] || cur=/ | |
| 243 | + continue;; | |
| 244 | + esac | |
| 245 | + if [ -z "$cur" ] || [ "$cur" = / ]; then next=/$part; else next=$cur/$part; fi | |
| 246 | + if [ -L "$next" ]; then | |
| 247 | + hops=$((hops + 1)) | |
| 248 | + [ "$hops" -le 16 ] || refuse "trusted path has too many symlinks: $path" | |
| 249 | + path_owner_mode "$next" "$owner" | |
| 250 | + link=$(readlink "$next") || refuse "cannot read symlink $next" | |
| 251 | + case $link in | |
| 252 | + /*) dest=$link;; | |
| 253 | + *) dest=$(dirname "$next")/$link;; | |
| 254 | + esac | |
| 255 | + dest=$(normalize "$dest") | |
| 256 | + old=$rest | |
| 257 | + if [ "$stop" = / ]; then | |
| 258 | + rest=${dest#/} | |
| 259 | + cur= | |
| 260 | + else | |
| 261 | + case $dest in | |
| 262 | + "$stop"|"$stop"/*) ;; | |
| 263 | + *) refuse "trusted path resolves outside the machine root: $dest";; | |
| 264 | + esac | |
| 265 | + suffix=${dest#"$stop"} | |
| 266 | + suffix=${suffix#/} | |
| 267 | + rest=$suffix | |
| 268 | + cur=$stop | |
| 269 | + fi | |
| 270 | + if [ -n "$old" ]; then | |
| 271 | + if [ -n "$rest" ]; then rest=$rest/$old; else rest=$old; fi | |
| 272 | + fi | |
| 273 | + continue | |
| 274 | + fi | |
| 275 | + path_owner_mode "$next" "$owner" | |
| 276 | + cur=$next | |
| 277 | + done | |
| 278 | + [ -n "$cur" ] || cur=/ | |
| 279 | + RESOLVED=$cur | |
| 280 | +} | |
| 281 | +# Directories and binaries a delegated root build may execute. Caller environment is not consulted. | |
| 282 | +require_trusted_make_path() { | |
| 283 | + local d tool path dirs | |
| 284 | + TRUSTED_PATH_OK= | |
| 285 | + IFS=: read -r -a dirs <<'EOF' | |
| 286 | +/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin | |
| 287 | +EOF | |
| 288 | + for d in "${dirs[@]}"; do | |
| 289 | + [ -e "$d" ] || [ -L "$d" ] || continue | |
| 290 | + follow_trusted "$d" / 0 | |
| 291 | + if [ -n "$TRUSTED_PATH_OK" ]; then TRUSTED_PATH_OK=$TRUSTED_PATH_OK:$RESOLVED | |
| 292 | + else TRUSTED_PATH_OK=$RESOLVED; fi | |
| 293 | + done | |
| 294 | + [ -n "$TRUSTED_PATH_OK" ] || refuse "no trusted directory on the make PATH" | |
| 295 | + for tool in make cc; do | |
| 296 | + path=$(PATH=$TRUSTED_PATH_OK command -v "$tool") || refuse "$tool is not on the trusted PATH" | |
| 297 | + follow_trusted "$path" / 0 | |
| 298 | + [ -f "$RESOLVED" ] && [ ! -L "$RESOLVED" ] || refuse "$tool does not resolve to a regular file: $path" | |
| 299 | + done | |
| 300 | +} | |
| 60 | 301 | |
| 61 | 302 | # ---------------------------------------------------------------- facts |
| 62 | 303 | gpu_ids() { # prints "vendor:device" of every NVIDIA PCI function |
| @@ -196,8 +437,15 @@ kernel_level() { | ||
| 196 | 437 | backup_file "$MODDIR/aien_gpu.ko"; backup_file "$MODCONF"; backup_file "$MODLOAD" |
| 197 | 438 | for m in "$ROOT"/lib/modules/$KVER/modules.*; do [ -e "$m" ] && backup_file "${m#$ROOT}"; done |
| 198 | 439 | nv_in_initramfs && backup_file "$INITRD" |
| 199 | - local B; B=$(mktemp -d) && cp -r "$DRIVER_SRC"/. "$B"/ || die "copy of driver source failed" | |
| 200 | - make -C "$KDIR" M="$B" modules > "$BK/build.log" 2>&1 || { cat "$BK/build.log" >&2; die "aien_gpu build failed (log $BK/build.log)"; } | |
| 440 | + local B; B=$(mktemp -d) && cp -a -- "$DRIVER_SRC"/. "$B"/ || die "copy of driver source failed" | |
| 441 | + # A delegated root build must not inherit CC, MAKEFLAGS or LD_PRELOAD from `sudo VAR=value`. | |
| 442 | + # sudoers without SETENV already rejects those assignments; this is the same boundary for make. | |
| 443 | + if [ "$EUID" -eq 0 ] && sudo_origin; then | |
| 444 | + env -i PATH="$TRUSTED_PATH_OK" HOME="${HOME:-/root}" \ | |
| 445 | + make -C "$KDIR" M="$B" modules > "$BK/build.log" 2>&1 || { cat "$BK/build.log" >&2; die "aien_gpu build failed (log $BK/build.log)"; } | |
| 446 | + else | |
| 447 | + make -C "$KDIR" M="$B" modules > "$BK/build.log" 2>&1 || { cat "$BK/build.log" >&2; die "aien_gpu build failed (log $BK/build.log)"; } | |
| 448 | + fi | |
| 201 | 449 | local vm; vm=$(modinfo -F vermagic "$B/aien_gpu.ko" 2>/dev/null; modinfo "$B/aien_gpu.ko" 2>/dev/null | awk '/^vermagic/{print $2}') |
| 202 | 450 | grep -q "$KVER" <<<"$vm" || die "built module vermagic '$vm' does not match running kernel $KVER" |
| 203 | 451 | mkdir -p "$ROOT$MODDIR" && install -m 0644 "$B/aien_gpu.ko" "$ROOT$MODDIR/aien_gpu.ko" || die "module install failed" |
| @@ -254,7 +502,11 @@ while [ $# -gt 0 ]; do | ||
| 254 | 502 | --in-spark-window) SPARK_WINDOW=1;; |
| 255 | 503 | --lib|--driver-src|--kdir) |
| 256 | 504 | [ $# -ge 2 ] && [ -n "$2" ] || usage |
| 257 | - case $1 in --lib) LIB_SRC=$2;; --driver-src) DRIVER_SRC=$2;; --kdir) KDIR=$2;; esac | |
| 505 | + case $1 in | |
| 506 | + --lib) LIB_SRC=$2;; | |
| 507 | + --driver-src) DRIVER_SRC=$2; DRIVER_SRC_OPTION=1;; | |
| 508 | + --kdir) KDIR=$2; KDIR_OPTION=1;; | |
| 509 | + esac | |
| 258 | 510 | shift;; |
| 259 | 511 | *) usage;; |
| 260 | 512 | esac |
| @@ -262,13 +514,27 @@ while [ $# -gt 0 ]; do | ||
| 262 | 514 | done |
| 263 | 515 | case $level in status|user|kernel|all|back) ;; *) usage;; esac |
| 264 | 516 | if [ $YES -eq 1 ] && [ -z "$ROOT" ] && [ "$(id -u)" -ne 0 ]; then |
| 265 | - # No -E: root gets a clean environment; the three sources travel as explicit, resolved arguments. | |
| 517 | + # No -E: root gets a clean environment. The module source and the kernel build dir are chosen | |
| 518 | + # again in the root process; they are not arguments a delegated caller can set. | |
| 519 | + case $level in | |
| 520 | + kernel|all) | |
| 521 | + if caller_selected_build; then | |
| 522 | + refuse "a delegated kernel swap cannot take --driver-src, --kdir, CUDAPP_DRIVER_SRC or CUDAPP_KDIR; root uses /usr/src/aien-gpu and /lib/modules/<uname -r>/build" | |
| 523 | + fi | |
| 524 | + ;; | |
| 525 | + esac | |
| 266 | 526 | set -- --yes |
| 267 | 527 | [ $REBOOT -eq 1 ] && set -- "$@" --reboot |
| 268 | 528 | [ $HOLDERS_OK -eq 1 ] && set -- "$@" --holders-ok |
| 269 | 529 | [ -n "${SPARK_WINDOW:-}" ] && set -- "$@" --in-spark-window |
| 270 | - exec sudo "$SELF" swap "$level" "$@" --lib "$(readlink -m "$LIB_SRC")" \ | |
| 271 | - --driver-src "$(readlink -m "$DRIVER_SRC")" --kdir "$(readlink -m "$KDIR")" | |
| 530 | + case $level in user|all) set -- "$@" --lib "$(readlink -m "$LIB_SRC")";; esac | |
| 531 | + exec sudo "$SELF" swap "$level" "$@" | |
| 532 | +fi | |
| 533 | +if delegated_run; then | |
| 534 | + require_delegated_install | |
| 535 | + if delegated_build; then | |
| 536 | + apply_delegated_sources | |
| 537 | + fi | |
| 272 | 538 | fi |
| 273 | 539 | # As root on the real system, refuse sources outside the places a root install may read from. |
| 274 | 540 | if [ -z "$ROOT" ] && [ "$(id -u)" -eq 0 ]; then |
tests/gpu_elementwise_test.c
| @@ -112,6 +112,23 @@ static int sm121dis_check(OmegaGpuEwOp op, const char *name, const char *const * | ||
| 112 | 112 | } |
| 113 | 113 | |
| 114 | 114 | static void host_only(void) { |
| 115 | + /* swiglu_131073's tail is one element. Staging the plan's p[10] (524288 bytes) | |
| 116 | + * wrote past the caller buffer; the chunk's own length is 4. No device. */ | |
| 117 | + { | |
| 118 | + size_t b[4] = {0}; | |
| 119 | + uint32_t nch = 99; | |
| 120 | + CHECK(omega_gpu_elementwise_test_stage_bytes(131073, b, 4, &nch) == OMEGA_GPU_EW_OK | |
| 121 | + && nch == 2 && b[0] == 524288u && b[1] == 4u, | |
| 122 | + "n=131073 stages a 524288-byte chunk then a 4-byte tail (chunks %u %zu %zu)", nch, b[0], b[1]); | |
| 123 | + CHECK(b[1] != 524288u, "negative control: the tail is not the plan's full-chunk length"); | |
| 124 | + nch = 99; | |
| 125 | + CHECK(omega_gpu_elementwise_test_stage_bytes(131072, b, 4, &nch) == OMEGA_GPU_EW_OK | |
| 126 | + && nch == 1 && b[0] == 524288u, "a full chunk stages its own 524288 bytes (chunks %u %zu)", nch, b[0]); | |
| 127 | + nch = 99; | |
| 128 | + CHECK(omega_gpu_elementwise_test_stage_bytes(1, b, 4, &nch) == OMEGA_GPU_EW_OK | |
| 129 | + && nch == 1 && b[0] == 4u, "one element stages 4 bytes (chunks %u %zu)", nch, b[0]); | |
| 130 | + CHECK(omega_gpu_elementwise_test_stage_bytes(0, b, 4, &nch) == OMEGA_GPU_EW_BAD_ARGS, "n=0 stage probe refused"); | |
| 131 | + } | |
| 115 | 132 | CHECK(omega_blackwell_verify_codegen_fixtures_fb1cut4() == 0, "word fixtures MUFU_EX2 / BAR_SYNC / LDS32 / STS32 (rc %d)", omega_blackwell_verify_codegen_fixtures_fb1cut4()); |
| 116 | 133 | float a[256] = {0}, b[256] = {0}, c[256] = {0}; |
| 117 | 134 | CHECK(omega_gpu_rmsnorm_f32(0, 128, a, b, 1e-5f, c, NULL) == OMEGA_GPU_EW_BAD_ARGS, "rows=0 refused"); |
tests/swap/test_swap_cli.sh
| @@ -168,7 +168,28 @@ check "escalation calls sudo" [ -s "$S/sudo.argv" ] | ||
| 168 | 168 | check "escalation drops -E (no caller environment reaches root)" bash -c "! grep -qx -- '-E' '$S/sudo.argv'" |
| 169 | 169 | check "escalation re-runs this script by absolute path" [ "$(sed -n 1p "$S/sudo.argv")" = "$(readlink -f "$TOOL")" ] |
| 170 | 170 | check "escalation passes --lib as a resolved absolute path" grep -qx -- "$S/lib/libcuda.so.1" "$S/sudo.argv" |
| 171 | -check "escalation passes --kdir and --driver-src" bash -c "grep -qx -- --kdir '$S/sudo.argv' && grep -qx -- --driver-src '$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" ] | |
| 172 | 193 | # 12. as root on the real system (fake id prints 0): sources outside the allowed places are refused before any level runs |
| 173 | 194 | printf '#!/bin/bash\necho 0\n' > "$S/bin/id" |
| 174 | 195 | out=$(env -u CUDAPP_ROOT PATH="$S/bin:$PATH" bash "$TOOL" swap user --lib "$S/lib/libcuda.so.1" 2>&1); rc=$? |
| @@ -194,5 +215,178 @@ check "differing installed library rolled back" [ "$before" = "$(tree_hash "$R") | ||
| 194 | 215 | rm "$R/fakebin/install"; out=$(run swap user --yes 2>&1) |
| 195 | 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" |
| 196 | 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 | + | |
| 197 | 391 | echo "swap cli: $PASS passed, $FAIL failed" |
| 198 | 392 | [ $FAIL -eq 0 ] |