interplane.Get Omega
Read in your language

spark-supervisor

Process supervisor and health monitor for sovereign daemons

Brought by drakestapleton

branch main Branches Tags History

spark-supervisor / tests / sandbox.sh

154 lines · 8950 bytes Raw Blame History
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.
9set -u
10here=$(cd "$(dirname "$0")" && pwd)
11H=/usr/local/libexec/aien-supervise/aien-supervise-root
12S=/etc/aien-supervise/services
13T=/opt/aien-sup-test
14[ "$(id -u)" = 0 ] || { echo "sandbox.sh: run as root" >&2; exit 2; }
15id aien-sup-test >/dev/null 2>&1 || { echo "SANDBOX NOT_RUN: user aien-sup-test missing"; exit 3; }
16U=$(id -u aien-sup-test); G=$(id -g aien-sup-test)
17pass=0; fail=0; notrun=0
18ok() { pass=$((pass+1)); echo "PASS $*"; }
19bad() { fail=$((fail+1)); echo "FAIL $*"; }
20nr() { notrun=$((notrun+1)); echo "NOT_RUN $*"; }
21cleanup() {
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}
27trap cleanup EXIT
28
29install -d -o root -g root -m 0755 "$T"
30install -o root -g root -m 0644 "$here/probe.py" "$T/probe.py"
31for ns in ipc uts pid cgroup mnt; do echo "$ns $(readlink /proc/1/ns/$ns)"; done > "$T/hostns"
32chmod 0644 "$T/hostns"
33( umask 077; echo s3cret-probe > /etc/aien-supervise/test-secret )
34touch /tmp/aien-sup-hostmarker
35listen=$(ss -ltnH 'sport = :18096' | wc -l)
36
37base() {
38 cat <<EOF
39exec /usr/bin/python3.12 -I $T/probe.py $U 512 0027 64 $T/hostns
40type oneshot
41restart no
42user aien-sup-test
43env PROBE_ENV=set
44umask 0027
45ro $T
46secret probe-secret /etc/aien-supervise/test-secret
47net filter
48allow 127.0.0.1
49net-default drop
50memory-max 67108864
51memory-swap-max 0
52tasks-max 16
53cpu-quota 50
54nofile 512
55namespaces no
56syscalls @system-service
57syscall-error eperm
58families unix inet inet6
59mdwe yes
60EOF
61}
62put() { cat > "$S/$1.svc"; chmod 0644 "$S/$1.svc"; chown root:root "$S/$1.svc"; }
63check_open() { grep -q "^CHECK $1 open" "$2"; }
64
65# A. Full sandbox: every check confined.
66base | put sup-probe
67"$H" run sup-probe > /tmp/sup-a.out 2>&1; rc=$?
68if [ $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)"
70if 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).
74nft delete table inet aiens_sup_probe 2>/dev/null
75setpriv --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
76miss=$(awk '$1=="CHECK" && $4=="d" && $3!="open" {print $2}' /tmp/sup-b.out | tr '\n' ' ')
77n=$(awk '$1=="CHECK" && $4=="d"' /tmp/sup-b.out | wc -l)
78if [ -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.
81mut() { # 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}
86mut memory-max MemoryMax '/^memory-max/d'
87mut tasks-max TasksMax '/^tasks-max/d'
88mut cpu-quota CPUQuota '/^cpu-quota/d'
89mut mdwe MemoryDenyWriteExecute.mmap 's/^mdwe yes/mdwe no/'
90mut namespaces RestrictNamespaces.unshare 's/^namespaces no/namespaces yes/'
91mut families RestrictAddressFamilies '/^families/d'
92mut nofile LimitNOFILE '/^nofile/d'
93mut umask UMask '/^umask/d'
94mut secret ImportCredential '/^secret/d'
95if 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/'
98else 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).
101base | sed 's/^syscall-error eperm/syscall-error kill/' | put sup-kill
102"$H" run sup-kill > /tmp/sup-d.out 2>&1; rc=$?
103if [ $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).
106refuse() { # 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}
110base | put sup-r1; chmod 0664 "$S/sup-r1.svc"; refuse "group-writable service file" sup-r1
111base | put sup-r2; chown aien-sup-test "$S/sup-r2.svc"; refuse "service file not root-owned" sup-r2
112base | sed 's/^user aien-sup-test/user root/' | put sup-r3; refuse "user root" sup-r3
113base | sed "s/^user aien-sup-test/user $(id -un 1000)/" | put sup-r4; refuse "uid 1000 operator account" sup-r4
114base | sed '$a groups docker' | put sup-r5; refuse "supplementary group docker" sup-r5
115cp /usr/bin/true /tmp/sup-true; chmod 0755 /tmp/sup-true
116base | sed 's|^exec .*|exec /tmp/sup-true|' | put sup-r6; refuse "exec under world-writable /tmp" sup-r6
117base | sed 's|^exec /usr/bin/python3.12|exec /usr/bin/python3|' | put sup-r7; refuse "exec through a symlink" sup-r7
118base | sed '$a rw /etc/aien-supervise' | put sup-r8; refuse "rw outside /var /srv /run" sup-r8
119base | sed '$a ro /home/operator' | put sup-r9; refuse "ro under /home" sup-r9
120base | sed '$a bogus-key 1' | put sup-r10; refuse "unknown key" sup-r10
121base | sed '/^syscalls/d' | put sup-r11; refuse "no syscalls line" sup-r11
122base | sed '$a env LD_PRELOAD=/tmp/x.so' | put sup-r12; refuse "LD_ environment" sup-r12
123base | sed '$a ro /opt/../etc' | put sup-r13; refuse "path with .." sup-r13
124base | sed '$a allow 1.2.3.4;flush' | put sup-r14; refuse "nft injection in allow" sup-r14
125refuse "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"
128setpriv --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"
130rm -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.
134printf '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=$!
136sleep 1
137if 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"
142printf 'exec /usr/bin/python3.12 -I %s/ignterm.py\nuser aien-sup-test\nsyscalls @system-service\nro %s\n' "$T" "$T" | put sup-life
143printf '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=$!
145sleep 1.5
146"$H" stop sup-life; sleep 1
147if 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
151total=$((pass+fail+notrun))
152if [ $fail = 0 ]; then v=PASS; else v=FAIL; fi
153echo "SANDBOX $v cases=$total pass=$pass fail=$fail not_run=$notrun"
154[ $fail = 0 ]