aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / tools / aien-local-ci / tests / proc_boundary_test.sh
| 1 | #!/usr/bin/env bash |
| 2 | # SC-SEC-2: local/offline same-UID proc escape and profile lock regression. |
| 3 | # Run with AIEN_EXEC=... as an operator with sudo -n for the candidate helper. |
| 4 | # No chip device, GPU lock, or gb10 helper is opened. |
| 5 | set -euo pipefail |
| 6 | X=${AIEN_EXEC:-/usr/local/sbin/aien-run-exec} |
| 7 | lock=/run/aien-local-ci/execution.lock |
| 8 | fixture=$(mktemp -d) |
| 9 | tag=sec2-victim-$$ |
| 10 | baseline_pid= host_pid= baseline_job= host_job= exclusive_job= waiting_job= |
| 11 | cleanup() { |
| 12 | [[ -z $baseline_pid ]] || sudo -n kill -TERM "$baseline_pid" 2>/dev/null || true |
| 13 | [[ -z $host_pid ]] || sudo -n kill -TERM "$host_pid" 2>/dev/null || true |
| 14 | [[ -z $waiting_job ]] || kill "$waiting_job" 2>/dev/null || true |
| 15 | [[ -z $exclusive_job ]] || kill "$exclusive_job" 2>/dev/null || true |
| 16 | [[ -z $baseline_job ]] || wait "$baseline_job" 2>/dev/null || true |
| 17 | [[ -z $host_job ]] || wait "$host_job" 2>/dev/null || true |
| 18 | [[ -z $exclusive_job ]] || wait "$exclusive_job" 2>/dev/null || true |
| 19 | [[ -z $waiting_job ]] || wait "$waiting_job" 2>/dev/null || true |
| 20 | rm -rf -- "$fixture" |
| 21 | } |
| 22 | trap cleanup EXIT |
| 23 | |
| 24 | # Wait for the real process and its ready output; no hard-coded host PID assumption. |
| 25 | await_victim() { |
| 26 | local label=$1 logfile=$2 pid= i |
| 27 | for ((i=0; i<100; i++)); do |
| 28 | pid=$(pgrep -u aien-run -f "^${label} 45$" || true) |
| 29 | if [[ -n $pid ]] && grep -q '^READY$' "$logfile"; then printf '%s\n' "$pid"; return 0; fi |
| 30 | sleep .1 |
| 31 | done |
| 32 | printf 'victim never became ready (%s): ' "$label" >&2 |
| 33 | cat "$logfile" >&2 |
| 34 | return 1 |
| 35 | } |
| 36 | |
| 37 | # Old host-profile namespace arrangement: private mounts but the host PID table and proc. |
| 38 | # The victim's /var/tmp is a private mount. A peer cannot use its own /var/tmp to |
| 39 | # reach it, but /proc/<same-UID-pid>/root crosses into the victim's mount namespace. |
| 40 | sudo -n unshare -m -n -i --propagation private -- bash -c ' |
| 41 | mount -t tmpfs -o mode=1777,nosuid,nodev sec2-old-victim /var/tmp |
| 42 | exec setpriv --reuid aien-run --regid aien-run --init-groups --reset-env -- \ |
| 43 | bash -c '\''touch /var/tmp/marker; echo READY; exec -a "$1" sleep 45'\'' _ "$1" |
| 44 | ' old-victim "$tag-old" >"$fixture/old.log" 2>&1 & |
| 45 | baseline_job=$! |
| 46 | baseline_pid=$(await_victim "$tag-old" "$fixture/old.log") |
| 47 | sudo -n unshare -m -n -i --propagation private -- \ |
| 48 | setpriv --reuid aien-run --regid aien-run --init-groups --reset-env -- \ |
| 49 | bash -c 'touch "/proc/$1/root/var/tmp/marker" && kill -CONT "$1"' _ "$baseline_pid" |
| 50 | printf 'PASS old same-UID host proc crosses victim mount and sees victim pid\n' |
| 51 | sudo -n kill -TERM "$baseline_pid" |
| 52 | wait "$baseline_job" 2>/dev/null || true |
| 53 | baseline_pid= baseline_job= |
| 54 | |
| 55 | # Both victim and attacker now use the production helper and its actual host profile. |
| 56 | sudo -n "$X" --cwd /tmp --timeout 60 --profile host -- bash -c \ |
| 57 | 'touch /var/tmp/marker; echo READY; exec -a "$1" sleep 45' _ "$tag-new" \ |
| 58 | >"$fixture/host.log" 2>&1 & |
| 59 | host_job=$! |
| 60 | host_pid=$(await_victim "$tag-new" "$fixture/host.log") |
| 61 | # This same root-held lock must be shared by a second host or qemu job, but deny |
| 62 | # an exclusive contender (the gb10 reservation mode) until the scope is cleaned. |
| 63 | grep -Fq 'host|qemu) lock_mode=-s ;; gb10) lock_mode=-x' "$X" |
| 64 | if sudo -n flock -n -x -- "$lock" true; then |
| 65 | echo 'FAIL exclusive contender entered during host scope' >&2; exit 1 |
| 66 | fi |
| 67 | sudo -n flock -n -s -- "$lock" true |
| 68 | second=$(sudo -n "$X" --cwd /tmp --timeout 10 --profile qemu -- printf 'SECOND\n') |
| 69 | [[ $second == SECOND ]] |
| 70 | printf 'PASS production host and qemu scopes share lock, exclusive contender blocked\n' |
| 71 | |
| 72 | # Actual helper attacker sees only its own PID table and proc mount. Its own |
| 73 | # child remains visible and reachable, including through /proc/<child>/root. |
| 74 | sudo -n "$X" --cwd /tmp --timeout 10 --profile host -- bash -c ' |
| 75 | victim=$1 |
| 76 | if [[ -e /proc/$victim/root ]] || kill -CONT "$victim" 2>/dev/null || \ |
| 77 | touch "/proc/$victim/root/var/tmp/marker" 2>/dev/null; then exit 1; fi |
| 78 | for fd in /proc/self/fd/*; do |
| 79 | [[ $(readlink "$fd") != /run/aien-local-ci/execution.lock ]] || exit 1 |
| 80 | done |
| 81 | sleep 5 & child=$! |
| 82 | kill -0 "$child" && [[ -d /proc/$child/root ]] && touch "/proc/$child/root/var/tmp/own-child" |
| 83 | kill "$child"; wait "$child" 2>/dev/null || true |
| 84 | ' _ "$host_pid" |
| 85 | printf 'PASS private proc rejects peer root and PID, own child remains reachable\n' |
| 86 | sudo -n kill -TERM "$host_pid" |
| 87 | wait "$host_job" 2>/dev/null || true |
| 88 | host_pid= host_job= |
| 89 | sudo -n flock -n -x -- "$lock" true |
| 90 | printf 'PASS exclusive lock obtainable after production scope cleanup\n' |
| 91 | |
| 92 | # Simulate the exclusive reservation without running the gb10 helper or touching |
| 93 | # devices. A host invocation must wait until that reservation is released. |
| 94 | sudo -n flock -x --close -- "$lock" bash -c 'echo HELD; sleep 20' >"$fixture/exclusive.log" 2>&1 & |
| 95 | exclusive_job=$! |
| 96 | for ((i=0; i<100; i++)); do |
| 97 | grep -q '^HELD$' "$fixture/exclusive.log" && break |
| 98 | sleep .1 |
| 99 | done |
| 100 | grep -q '^HELD$' "$fixture/exclusive.log" |
| 101 | sudo -n "$X" --cwd /tmp --timeout 10 --profile host -- printf 'ADMITTED\n' \ |
| 102 | >"$fixture/waiting.log" 2>&1 & |
| 103 | waiting_job=$! |
| 104 | sleep 1 |
| 105 | if grep -q '^ADMITTED$' "$fixture/waiting.log"; then |
| 106 | echo 'FAIL host entered during exclusive reservation' >&2; exit 1 |
| 107 | fi |
| 108 | kill "$exclusive_job" |
| 109 | wait "$exclusive_job" 2>/dev/null || true |
| 110 | exclusive_job= |
| 111 | wait "$waiting_job" |
| 112 | waiting_job= |
| 113 | grep -q '^ADMITTED$' "$fixture/waiting.log" |
| 114 | printf 'PASS exclusive reservation blocks host until release\n' |