spark-supervisor
Process supervisor and health monitor for sovereign daemons
Brought by drakestapleton
spark-supervisor / tests / sandbox.sh
| 1 | #!/bin/sh |
| 2 | # sandbox.sh: qualify the installed aien-supervise-root sandbox on this host. Root only: |
| 3 | # sudo -n sh tests/sandbox.sh |
| 4 | # Needs: the helper and aien-watch installed under /usr/local/libexec/aien-supervise, the system |
| 5 | # user aien-sup-test (useradd --system --no-create-home --shell /usr/sbin/nologin aien-sup-test), |
| 6 | # python3.12. It writes only /etc/aien-supervise/services/sup-*.svc, /etc/aien-supervise/test-secret, |
| 7 | # /opt/aien-sup-test and /tmp/aien-sup-hostmarker, removes them afterwards, and touches no other |
| 8 | # service. Prints one line per case and SANDBOX PASS|FAIL cases=N fail=N not_run=N. |
| 9 | set -u |
| 10 | here=$(cd "$(dirname "$0")" && pwd) |
| 11 | H=/usr/local/libexec/aien-supervise/aien-supervise-root |
| 12 | S=/etc/aien-supervise/services |
| 13 | T=/opt/aien-sup-test |
| 14 | [ "$(id -u)" = 0 ] || { echo "sandbox.sh: run as root" >&2; exit 2; } |
| 15 | id aien-sup-test >/dev/null 2>&1 || { echo "SANDBOX NOT_RUN: user aien-sup-test missing"; exit 3; } |
| 16 | U=$(id -u aien-sup-test); G=$(id -g aien-sup-test) |
| 17 | pass=0; fail=0; notrun=0 |
| 18 | ok() { pass=$((pass+1)); echo "PASS $*"; } |
| 19 | bad() { fail=$((fail+1)); echo "FAIL $*"; } |
| 20 | nr() { notrun=$((notrun+1)); echo "NOT_RUN $*"; } |
| 21 | cleanup() { |
| 22 | rm -f "$S"/sup-*.svc /etc/aien-supervise/test-secret /tmp/aien-sup-hostmarker |
| 23 | rm -rf "$T" |
| 24 | for t in $(nft list tables 2>/dev/null | awk '$3 ~ /^aiens_sup_/ {print $3}'); do nft delete table inet "$t"; done |
| 25 | for c in /sys/fs/cgroup/aien-supervise/sup-*; do [ -d "$c" ] && rmdir "$c" 2>/dev/null; done |
| 26 | } |
| 27 | trap cleanup EXIT |
| 28 | |
| 29 | install -d -o root -g root -m 0755 "$T" |
| 30 | install -o root -g root -m 0644 "$here/probe.py" "$T/probe.py" |
| 31 | for ns in ipc uts pid cgroup mnt; do echo "$ns $(readlink /proc/1/ns/$ns)"; done > "$T/hostns" |
| 32 | chmod 0644 "$T/hostns" |
| 33 | ( umask 077; echo s3cret-probe > /etc/aien-supervise/test-secret ) |
| 34 | touch /tmp/aien-sup-hostmarker |
| 35 | listen=$(ss -ltnH 'sport = :18096' | wc -l) |
| 36 | |
| 37 | base() { |
| 38 | cat <<EOF |
| 39 | exec /usr/bin/python3.12 -I $T/probe.py $U 512 0027 64 $T/hostns |
| 40 | type oneshot |
| 41 | restart no |
| 42 | user aien-sup-test |
| 43 | env PROBE_ENV=set |
| 44 | umask 0027 |
| 45 | ro $T |
| 46 | secret probe-secret /etc/aien-supervise/test-secret |
| 47 | net filter |
| 48 | allow 127.0.0.1 |
| 49 | net-default drop |
| 50 | memory-max 67108864 |
| 51 | memory-swap-max 0 |
| 52 | tasks-max 16 |
| 53 | cpu-quota 50 |
| 54 | nofile 512 |
| 55 | namespaces no |
| 56 | syscalls @system-service |
| 57 | syscall-error eperm |
| 58 | families unix inet inet6 |
| 59 | mdwe yes |
| 60 | EOF |
| 61 | } |
| 62 | put() { cat > "$S/$1.svc"; chmod 0644 "$S/$1.svc"; chown root:root "$S/$1.svc"; } |
| 63 | check_open() { grep -q "^CHECK $1 open" "$2"; } |
| 64 | |
| 65 | # A. Full sandbox: every check confined. |
| 66 | base | put sup-probe |
| 67 | "$H" run sup-probe > /tmp/sup-a.out 2>&1; rc=$? |
| 68 | if [ $rc = 0 ] && grep -q '^PROBE CONFINED' /tmp/sup-a.out; then ok "A sandbox: $(grep '^PROBE' /tmp/sup-a.out)"; else bad "A sandbox rc=$rc: $(grep -E ' open |^PROBE|aien-supervise-root' /tmp/sup-a.out | tr '\n' ';')"; fi |
| 69 | [ "$listen" -gt 0 ] || nr "A IPAddressAllow.loopback: nothing listens on 127.0.0.1:18096 (check reads open)" |
| 70 | if nft list table inet aiens_sup_probe 2>/dev/null | grep -q "meta skuid $U ip daddr 127.0.0.1 accept"; then ok "A nft table aiens_sup_probe holds the skuid allow rule"; else bad "A nft table missing"; fi |
| 71 | |
| 72 | # B. Control: same probe, same user, no sandbox. Every kind-d check must read open. The rules of |
| 73 | # run A are keyed by this uid, so its table is dropped first (one uid per service is the contract). |
| 74 | nft delete table inet aiens_sup_probe 2>/dev/null |
| 75 | setpriv --reuid="$U" --regid="$G" --clear-groups -- /usr/bin/python3.12 -I "$T/probe.py" "$U" 512 0027 64 "$T/hostns" > /tmp/sup-b.out 2>&1 |
| 76 | miss=$(awk '$1=="CHECK" && $4=="d" && $3!="open" {print $2}' /tmp/sup-b.out | tr '\n' ' ') |
| 77 | n=$(awk '$1=="CHECK" && $4=="d"' /tmp/sup-b.out | wc -l) |
| 78 | if [ -z "$miss" ] && [ "$n" -gt 30 ]; then ok "B control: all $n kind-d checks read open without the sandbox"; else bad "B control: kind-d checks not open without sandbox: $miss (n=$n)"; fi |
| 79 | |
| 80 | # C. One directive weakened at a time: its check must read open. |
| 81 | mut() { # name check sed-expression |
| 82 | base | sed "$3" | put sup-mut |
| 83 | "$H" run sup-mut > /tmp/sup-c.out 2>&1 |
| 84 | if check_open "$2" /tmp/sup-c.out; then ok "C $1 removed -> $2 open"; else bad "C $1 removed but $2 not open: $(grep "CHECK $2 " /tmp/sup-c.out)"; fi |
| 85 | } |
| 86 | mut memory-max MemoryMax '/^memory-max/d' |
| 87 | mut tasks-max TasksMax '/^tasks-max/d' |
| 88 | mut cpu-quota CPUQuota '/^cpu-quota/d' |
| 89 | mut mdwe MemoryDenyWriteExecute.mmap 's/^mdwe yes/mdwe no/' |
| 90 | mut namespaces RestrictNamespaces.unshare 's/^namespaces no/namespaces yes/' |
| 91 | mut families RestrictAddressFamilies '/^families/d' |
| 92 | mut nofile LimitNOFILE '/^nofile/d' |
| 93 | mut umask UMask '/^umask/d' |
| 94 | mut secret ImportCredential '/^secret/d' |
| 95 | if python3 -c 'import socket;socket.create_connection(("1.1.1.1",443),2)' 2>/dev/null; then |
| 96 | nft delete table inet aiens_sup_mut 2>/dev/null |
| 97 | mut net IPAddressDeny.outbound 's/^net filter/net host/' |
| 98 | else nr "C net: host has no route to 1.1.1.1:443, cannot show the open case"; fi |
| 99 | |
| 100 | # D. syscall-error kill: the first denied syscall ends the probe with SIGSYS (31). |
| 101 | base | sed 's/^syscall-error eperm/syscall-error kill/' | put sup-kill |
| 102 | "$H" run sup-kill > /tmp/sup-d.out 2>&1; rc=$? |
| 103 | if [ $rc = 159 ] || [ $rc = 31 ]; then ok "D syscall-error kill: probe ended with status $rc (SIGSYS)"; else bad "D syscall-error kill: rc=$rc"; fi |
| 104 | |
| 105 | # E. Refusals: exit 2, nothing ran (no PROBE line). |
| 106 | refuse() { # label svc-name |
| 107 | "$H" run "$2" > /tmp/sup-e.out 2>&1; rc=$? |
| 108 | if [ $rc = 2 ] && ! grep -q '^PROBE' /tmp/sup-e.out; then ok "E refused: $1 ($(tr '\n' ' ' < /tmp/sup-e.out | cut -c1-90))"; else bad "E not refused: $1 rc=$rc"; fi |
| 109 | } |
| 110 | base | put sup-r1; chmod 0664 "$S/sup-r1.svc"; refuse "group-writable service file" sup-r1 |
| 111 | base | put sup-r2; chown aien-sup-test "$S/sup-r2.svc"; refuse "service file not root-owned" sup-r2 |
| 112 | base | sed 's/^user aien-sup-test/user root/' | put sup-r3; refuse "user root" sup-r3 |
| 113 | base | sed "s/^user aien-sup-test/user $(id -un 1000)/" | put sup-r4; refuse "uid 1000 operator account" sup-r4 |
| 114 | base | sed '$a groups docker' | put sup-r5; refuse "supplementary group docker" sup-r5 |
| 115 | cp /usr/bin/true /tmp/sup-true; chmod 0755 /tmp/sup-true |
| 116 | base | sed 's|^exec .*|exec /tmp/sup-true|' | put sup-r6; refuse "exec under world-writable /tmp" sup-r6 |
| 117 | base | sed 's|^exec /usr/bin/python3.12|exec /usr/bin/python3|' | put sup-r7; refuse "exec through a symlink" sup-r7 |
| 118 | base | sed '$a rw /etc/aien-supervise' | put sup-r8; refuse "rw outside /var /srv /run" sup-r8 |
| 119 | base | sed '$a ro /home/operator' | put sup-r9; refuse "ro under /home" sup-r9 |
| 120 | base | sed '$a bogus-key 1' | put sup-r10; refuse "unknown key" sup-r10 |
| 121 | base | sed '/^syscalls/d' | put sup-r11; refuse "no syscalls line" sup-r11 |
| 122 | base | sed '$a env LD_PRELOAD=/tmp/x.so' | put sup-r12; refuse "LD_ environment" sup-r12 |
| 123 | base | sed '$a ro /opt/../etc' | put sup-r13; refuse "path with .." sup-r13 |
| 124 | base | sed '$a allow 1.2.3.4;flush' | put sup-r14; refuse "nft injection in allow" sup-r14 |
| 125 | refuse "missing service file" sup-none |
| 126 | "$H" run ../etc/x > /tmp/sup-e.out 2>&1; [ $? = 2 ] && ok "E refused: bad name ../etc/x" || bad "E bad name accepted" |
| 127 | "$H" run Sup-Probe > /tmp/sup-e.out 2>&1; [ $? = 2 ] && ok "E refused: upper-case name" || bad "E upper-case name accepted" |
| 128 | setpriv --reuid="$U" --regid="$G" --clear-groups -- "$H" run sup-probe > /tmp/sup-e.out 2>&1; rc=$? |
| 129 | [ $rc = 2 ] && grep -q 'must run as root' /tmp/sup-e.out && ok "E refused: non-root caller" || bad "E non-root caller rc=$rc" |
| 130 | rm -f /tmp/sup-true |
| 131 | |
| 132 | # F. Lifecycle: a long-running service; a second run is refused while it runs; stop (SIGTERM to the |
| 133 | # cgroup) ends it; kill (cgroup.kill) ends a TERM-ignoring one. |
| 134 | printf 'exec /usr/bin/sleep 300\nuser aien-sup-test\nsyscalls @system-service\nnet none\n' | put sup-life |
| 135 | "$H" run sup-life > /tmp/sup-f.out 2>&1 & bg=$! |
| 136 | sleep 1 |
| 137 | if grep -q '^populated 1' /sys/fs/cgroup/aien-supervise/sup-life/cgroup.events; then ok "F running service is in cgroup aien-supervise/sup-life"; else bad "F cgroup not populated"; fi |
| 138 | "$H" run sup-life > /tmp/sup-f2.out 2>&1; rc=$? |
| 139 | [ $rc = 2 ] && grep -q 'already running' /tmp/sup-f2.out && ok "F second run refused while running" || bad "F second run rc=$rc" |
| 140 | "$H" stop sup-life; wait $bg; rc=$? |
| 141 | [ $rc = 143 ] && ok "F stop: service ended by SIGTERM (status 143)" || bad "F stop: status $rc" |
| 142 | printf 'exec /usr/bin/python3.12 -I %s/ignterm.py\nuser aien-sup-test\nsyscalls @system-service\nro %s\n' "$T" "$T" | put sup-life |
| 143 | printf 'import signal, time\nsignal.signal(signal.SIGTERM, signal.SIG_IGN)\ntime.sleep(300)\n' > "$T/ignterm.py"; chmod 0644 "$T/ignterm.py" |
| 144 | "$H" run sup-life > /tmp/sup-f.out 2>&1 & bg=$! |
| 145 | sleep 1.5 |
| 146 | "$H" stop sup-life; sleep 1 |
| 147 | if kill -0 $bg 2>/dev/null; then ok "F TERM-ignoring service survives stop"; else bad "F TERM-ignoring service ended on stop"; fi |
| 148 | "$H" kill sup-life; rc_k=$?; wait $bg; rc=$? |
| 149 | [ $rc_k = 0 ] && [ $rc = 137 ] && ok "F kill: cgroup.kill ended it (status 137)" || bad "F kill: helper rc=$rc_k status=$rc" |
| 150 | |
| 151 | total=$((pass+fail+notrun)) |
| 152 | if [ $fail = 0 ]; then v=PASS; else v=FAIL; fi |
| 153 | echo "SANDBOX $v cases=$total pass=$pass fail=$fail not_run=$notrun" |
| 154 | [ $fail = 0 ] |