interplane.Get Omega
Read in your language

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

branch main Branches Tags History

aien-sovereign-core-mv / tools / aien-local-ci / tests / proc_boundary_test.sh

114 lines · 5320 bytes Raw Blame History
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.
5set -euo pipefail
6X=${AIEN_EXEC:-/usr/local/sbin/aien-run-exec}
7lock=/run/aien-local-ci/execution.lock
8fixture=$(mktemp -d)
9tag=sec2-victim-$$
10baseline_pid= host_pid= baseline_job= host_job= exclusive_job= waiting_job=
11cleanup() {
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}
22trap cleanup EXIT
23
24# Wait for the real process and its ready output; no hard-coded host PID assumption.
25await_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.
40sudo -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 &
45baseline_job=$!
46baseline_pid=$(await_victim "$tag-old" "$fixture/old.log")
47sudo -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"
50printf 'PASS old same-UID host proc crosses victim mount and sees victim pid\n'
51sudo -n kill -TERM "$baseline_pid"
52wait "$baseline_job" 2>/dev/null || true
53baseline_pid= baseline_job=
54
55# Both victim and attacker now use the production helper and its actual host profile.
56sudo -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 &
59host_job=$!
60host_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.
63grep -Fq 'host|qemu) lock_mode=-s ;; gb10) lock_mode=-x' "$X"
64if sudo -n flock -n -x -- "$lock" true; then
65 echo 'FAIL exclusive contender entered during host scope' >&2; exit 1
66fi
67sudo -n flock -n -s -- "$lock" true
68second=$(sudo -n "$X" --cwd /tmp --timeout 10 --profile qemu -- printf 'SECOND\n')
69[[ $second == SECOND ]]
70printf '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.
74sudo -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"
85printf 'PASS private proc rejects peer root and PID, own child remains reachable\n'
86sudo -n kill -TERM "$host_pid"
87wait "$host_job" 2>/dev/null || true
88host_pid= host_job=
89sudo -n flock -n -x -- "$lock" true
90printf '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.
94sudo -n flock -x --close -- "$lock" bash -c 'echo HELD; sleep 20' >"$fixture/exclusive.log" 2>&1 &
95exclusive_job=$!
96for ((i=0; i<100; i++)); do
97 grep -q '^HELD$' "$fixture/exclusive.log" && break
98 sleep .1
99done
100grep -q '^HELD$' "$fixture/exclusive.log"
101sudo -n "$X" --cwd /tmp --timeout 10 --profile host -- printf 'ADMITTED\n' \
102 >"$fixture/waiting.log" 2>&1 &
103waiting_job=$!
104sleep 1
105if grep -q '^ADMITTED$' "$fixture/waiting.log"; then
106 echo 'FAIL host entered during exclusive reservation' >&2; exit 1
107fi
108kill "$exclusive_job"
109wait "$exclusive_job" 2>/dev/null || true
110exclusive_job=
111wait "$waiting_job"
112waiting_job=
113grep -q '^ADMITTED$' "$fixture/waiting.log"
114printf 'PASS exclusive reservation blocks host until release\n'