Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/interplane-forge/blob/main/infra/test/sandbox-netns.sh # interplane-forge INTERPLANE Forge Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `interplane-forge / infra / test / sandbox-netns.sh` 699 lines ยท 38348 bytes Raw Blame History 1 # No network for the sandboxed server (r5 Part B, netns r2). Sourced by infra/test/sandbox.sh 2 # after its fixture, build and helpers exist (T H DATA PORT CPUS WRAPPER ROOT OMC PID record start 3 # stop fetch paths). 4 PRIV_TCP=$((PORT + 4)) # a TCP listener this run owns (private port range) 5 PRIV_REFUSE=$((PORT + 5)) # a port the refusal check must leave closed 6 PRIV_ABS=forged-r5-abstract-$$ # an abstract unix listener this run owns 7 LTCP= LABS= 8 # The netns head before r2: the controls run its scripts to show what r2 closes. 9 NETNS_OLD=`02001b2a20efbacf8025bcf3dd6c09089f910dfa` 10 # The head before the listener guard: its filter lets the child give the listener away. 11 POISON_OLD=`8f6b4a29f261665f83df81fbebf9b4955b85df5f` 12 13 netns_build() { 14 "$OMC" < infra/spark/forged-listen.om > "$T/forged-listen" || return 1 15 "$OMC" < infra/test/netprobe.om > "$T/netprobe" || return 1 16 "$OMC" < infra/spark/forged-seccomp.om > "$T/forged-seccomp" || return 1 17 chmod 755 "$T/forged-listen" "$T/netprobe" "$T/forged-seccomp" 18 "$T/forged-seccomp" > "$T/forged-seccomp.bpf" || return 1 19 # The listener-guard controls: the filter of the head before the guard, and three mutants of 20 # this one, each missing one rule (netns_poison_mutants). 21 mkdir -p "$T/poison" 22 git show "$POISON_OLD:infra/spark/forged-seccomp.om" > "$T/poison/old.om" || return 1 23 sed '/^denyif 210$/d' infra/spark/forged-seccomp.om > "$T/poison/m-shutdown.om" 24 sed '/^denyif 203$/d' infra/spark/forged-seccomp.om > "$T/poison/m-connect.om" 25 sed -e 's/^allowif 9$/allowif 9\nallowif 15/' -e 's/^jeq 208 0 18$/jeq 208 0 20/' -e 's/^jeq 1 0 10$/jeq 1 0 12/' \ 26 infra/spark/forged-seccomp.om > "$T/poison/m-reuseport.om" 27 local m 28 for m in old m-shutdown m-connect m-reuseport; do 29 if cmp -s infra/spark/forged-seccomp.om "$T/poison/$m.om"; then echo "FAIL $m is the same program"; return 1; fi 30 "$OMC" < "$T/poison/$m.om" > "$T/poison/$m" || return 1 31 chmod 755 "$T/poison/$m" 32 "$T/poison/$m" > "$T/poison/$m.bpf" || return 1 33 done 34 diff infra/spark/forged-seccomp.om "$T/poison/m-reuseport.om" || true 35 # The leak probe is a test instrument in C: seeded Omega has no ioctl and only six socket options. 36 gcc --version | head -1 37 gcc -O2 -Wall -Wextra -Werror -o "$T/leakprobe" infra/test/leakprobe.c || return 1 38 mkdir -p "$T/old-netns" 39 for f in forged-sandbox.sh forged-service.sh forged-public.sh forged-counts.sh; do 40 git show "$NETNS_OLD:infra/spark/$f" > "$T/old-netns/$f" || return 1 41 done 42 sha256sum infra/spark/forged-listen.om infra/test/netprobe.om infra/spark/forged-seccomp.om \ 43 infra/test/leakprobe.c "$T/forged-listen" "$T/netprobe" "$T/forged-seccomp" "$T/forged-seccomp.bpf" \ 44 "$T/leakprobe" "$T"/old-netns/* "$T"/poison/*.om "$T"/poison/*.bpf 45 echo "filter instructions: $(( $(stat -c %s "$T/forged-seccomp.bpf") / 8 ))" 46 } 47 48 private_listeners() { 49 local i 50 "$T/forged-listen" "$PRIV_TCP" /usr/bin/sleep 900 > /dev/null 2>&1 & 51 LTCP=$! 52 python3 -c 'import socket, sys, time 53 s = socket.socket(socket.AF_UNIX) 54 s.bind("\0" + sys.argv[1]); s.listen(); time.sleep(900)' "$PRIV_ABS" & 55 LABS=$! 56 for i in $(seq 1 50); do 57 [[ -n $(ss -ltnH "sport = :$PRIV_TCP") ]] && grep -q "@$PRIV_ABS\$" /proc/net/unix && return 0 58 sleep .1 59 done 60 return 1 61 } 62 private_listeners_stop() { 63 kill "$LTCP" "$LABS" 2>/dev/null || true 64 wait "$LTCP" "$LABS" 2>/dev/null || true 65 } 66 67 # Targets: the local services the review found reachable (connect attempts only, nothing is 68 # sent), this run's own listeners, the host's LAN address, a public address, and the host's 69 # abstract unix sockets. 70 netns_targets() { 71 local lan 72 printf '%s\n' 127.0.0.1:2525 127.0.0.1:1337 127.0.0.1:9200 127.0.0.1:18080 127.0.0.1:18091 \ 73 127.0.0.1:18301 127.0.0.1:22 127.0.0.1:8776 127.0.0.1:18480 100.64.0.2:8790 \ 74 "127.0.0.1:$PRIV_TCP" "@$PRIV_ABS" 1.1.1.1:443 75 lan=$(ip -4 route get 1.1.1.1 2>/dev/null | sed -n 's/.* src \([0-9.]*\).*/\1/p') 76 [[ -z $lan ]] || echo "$lan:22" 77 awk '$8 ~ /^@[[:graph:]]+$/ && length($8) <= 99 {print $8}' /proc/net/unix | grep -v "^@$PRIV_ABS\$" | 78 sort -u | head -24 79 } 80 81 # Two layers, each checked on its own: the shipped wrapper (namespace and socket filter), and a 82 # scratch copy without the filter line (namespace alone). $1: wrapper, $2: layer name. 83 netns_probe_run() { 84 local wrapper=$1 layer=$2 rc=0 out n bad=0 85 mapfile -t targets < <(netns_targets) 86 echo "== $layer: targets ${#targets[@]}: ${targets[*]}" 87 out=$(bash "$wrapper" "$T/netprobe" "${targets[@]}") || rc=$? 88 printf '%s\n' "$out" 89 [[ $rc = 0 ]] || bad=1 90 n=$(grep -c ' refused errno ' <<<"$out") 91 [[ $n = "${#targets[@]}" ]] || { echo "FAIL $n of ${#targets[@]} refused"; bad=1; } 92 ! grep -q ' connected$' <<<"$out" || bad=1 93 [[ $(grep '^interface ' <<<"$out") = 'interface lo' ]] || { echo 'FAIL interfaces other than lo'; bad=1; } 94 if [[ $layer = filter ]]; then 95 # 1 (EPERM): the filter refuses every IPv4 socket, and connect on every socket (listener 96 # guard), so this run's own abstract unix listener is refused before the namespace is asked. 97 grep -qx "127.0.0.1:$PRIV_TCP refused errno 1" <<<"$out" || bad=1 98 grep -qx '1.1.1.1:443 refused errno 1' <<<"$out" || bad=1 99 grep -qx "@$PRIV_ABS refused errno 1" <<<"$out" || bad=1 100 else 101 # 111 (refused) on loopback: the namespace's own lo is up and empty. 101 (unreachable): no route out. 102 grep -qx "127.0.0.1:$PRIV_TCP refused errno 111" <<<"$out" || bad=1 103 grep -qx '1.1.1.1:443 refused errno 101' <<<"$out" || bad=1 104 fi 105 [[ $bad = 0 ]] && echo "PASS $layer: no target reachable from the sandbox; interfaces: lo only" 106 return "$bad" 107 } 108 netns_probe_check() { 109 local bad=0 110 netns_probe_run "$WRAPPER" filter || bad=1 111 grep -v 'args+=(--seccomp "$seccomp_fd")' "$WRAPPER" > "$T/nofilter.sh" 112 ! grep -q -- '--seccomp' <(grep -v '^ *#' "$T/nofilter.sh" | grep 'args+=') || return 1 113 netns_probe_run "$T/nofilter.sh" namespace || bad=1 114 return "$bad" 115 } 116 117 # Control: the same probe with FORGED_NETNS=off reaches this run's own two listeners. 118 netns_control() { 119 local rc=0 out 120 out=$(FORGED_NETNS=off bash "$WRAPPER" "$T/netprobe" "127.0.0.1:$PRIV_TCP" "@$PRIV_ABS" 2>&1) || rc=$? 121 printf '%s\n' "$out" 122 [[ $rc = 1 ]] && grep -qx "127.0.0.1:$PRIV_TCP connected" <<<"$out" && 123 grep -qx "@$PRIV_ABS connected" <<<"$out" && 124 grep -q '!!! WARNING: FORGED_NETNS=off: SANDBOX SHARES THE HOST NETWORK' <<<"$out" 125 } 126 127 # The /app/forged process below a process of this run (never a forged this run did not start). 128 forged_below() { 129 local p c 130 for c in $(pgrep -P "$1"); do 131 if [[ $(tr '\0' ' ' < "/proc/$c/cmdline" 2>/dev/null) = '/app/forged '* ]]; then echo "$c"; return 0; fi 132 forged_below "$c" && return 0 133 done 134 return 1 135 } 136 137 # forged serves on the inherited listener, in a namespace of its own with no listener inside, 138 # under the socket filter. 139 netns_listener_check() { 140 local fpid fd l ino bad=0 sockets=0 141 mv "$T/sandbox-server.log" "$T/sandbox-server.parity.log" 142 start sandbox || return 1 143 fpid=$(forged_below "$PID") || { stop; return 1; } 144 echo "forged pid $fpid: $(tr '\0' ' ' < "/proc/$fpid/cmdline")" 145 grep -q 'serving the listener inherited from the supervisor' "$T/sandbox-server.log" || bad=1 146 grep -q 'no network (private namespace), socket filter on, serving the inherited listener' "$T/sandbox-server.log" || bad=1 147 grep -E '^(Seccomp|Seccomp_filters|NoNewPrivs):' "/proc/$fpid/status" 148 grep -qE '^Seccomp:[[:space:]]+2$' "/proc/$fpid/status" || { echo 'FAIL no seccomp filter on forged'; bad=1; } 149 [[ $(readlink "/proc/$fpid/ns/net") != "$(readlink /proc/self/ns/net)" ]] || { echo 'FAIL shared netns'; bad=1; } 150 [[ $(readlink "/proc/$fpid/fd/0") = /dev/null ]] || { echo 'FAIL descriptor 0 is not /dev/null'; bad=1; } 151 for fd in /proc/$fpid/fd/*; do 152 l=$(readlink "$fd") || continue 153 echo "fd ${fd##*/} -> $l" 154 if [[ $l = socket:* ]]; then sockets=$((sockets+1)); ino=${l#socket:[}; ino=${ino%]}; fi 155 done 156 [[ $sockets = 1 ]] || { echo "FAIL $sockets sockets"; bad=1; } 157 ss -ltnHe "sport = :$PORT" | tee "$T/netns-listener.ss" 158 grep -q "ino:$ino " "$T/netns-listener.ss" || { echo 'FAIL the host listener is not the inherited socket'; bad=1; } 159 echo "listeners inside forged's namespace: $(awk 'NR > 1 && $4 == "0A"' "/proc/$fpid/net/tcp" | wc -l)" 160 [[ $(awk 'NR > 1 && $4 == "0A"' "/proc/$fpid/net/tcp" | wc -l) = 0 ]] || bad=1 161 fetch sandbox || bad=1 162 stop || bad=1 163 mv "$T/sandbox-server.log" "$T/netns-listener-server.log" 164 mv "$T/sandbox-server.parity.log" "$T/sandbox-server.log" 165 sha256sum "$T/netns-listener-server.log" >> "$T/SHA256SUMS" 166 return "$bad" 167 } 168 169 # forged refuses a descriptor 0 that is not 127.0.0.1 and its compiled port, or not a socket. 170 netns_refusal_check() { 171 local rc bad=0 172 rc=0; FORGED_LISTEN_PORT=$PRIV_REFUSE bash "$WRAPPER" "$T/forged" > "$T/netns-refuse.out" 2>&1 || rc=$? 173 cat "$T/netns-refuse.out" 174 [[ $rc = 2 ]] && grep -q 'refusing the inherited listener' "$T/netns-refuse.out" || bad=1 175 rc=0; bash "$WRAPPER" "$T/forged" --inherited-listener < /dev/null > "$T/netns-refuse.out" 2>&1 || rc=$? 176 cat "$T/netns-refuse.out" 177 [[ $rc = 2 ]] && grep -q 'refusing the inherited listener' "$T/netns-refuse.out" || bad=1 178 [[ -z $(ss -ltnH "sport = :$PRIV_REFUSE") ]] || bad=1 179 return "$bad" 180 } 181 182 # FORGED_NETNS=off under the supervisor: loud, and forged, in the host network namespace and with 183 # no socket filter, serves the listener the supervisor holds. 184 netns_off_service_check() { 185 local ok=0 i from fpid samens=0 held=0 l ino out=$T/netns-off-service.forged.log 186 from=$(( $(stat -c %s "$DATA/logs/forged.log" 2>/dev/null || echo 0) + 1 )) 187 FORGED_NETNS=off FORGED_BIN=$T/forged FORGED_PORT=$PORT FORGED_CPUS=$CPUS FORGED_COUNTS_INTERVAL=1800 \ 188 setsid bash infra/spark/forged-service.sh _loop > "$T/netns-off-process.log" 2>&1 & 189 PID=$! 190 for i in $(seq 1 100); do 191 curl --max-time 1 -fsS "http://127.0.0.1:$PORT/" -o /dev/null && { ok=1; break; } 192 sleep .1 193 done 194 ss -ltnpHe "sport = :$PORT" | tee "$T/netns-off-listener.ss" 195 ino=$(grep -o 'ino:[0-9]*' "$T/netns-off-listener.ss" | head -1 | cut -d: -f2) 196 if fpid=$(forged_below "$PID"); then 197 echo "forged pid $fpid: $(tr '\0' ' ' < "/proc/$fpid/cmdline")" 198 echo "net namespace $(readlink "/proc/$fpid/ns/net"), host $(readlink /proc/self/ns/net)" 199 grep -E '^Seccomp:' "/proc/$fpid/status" 200 [[ $(readlink "/proc/$fpid/ns/net") = "$(readlink /proc/self/ns/net)" ]] && samens=1 201 for l in /proc/$fpid/fd/*; do [[ $(readlink "$l") = "socket:[$ino]" ]] && held=1; done 202 echo "forged holds the supervisor's listener (inode $ino): $held" 203 fi 204 stop || return 1 205 tail -c "+$from" "$DATA/logs/forged.log" > "$out" 206 sha256sum "$out" 207 echo "warnings in this run's service log: $(grep -c 'FORGED_NETNS=off: SANDBOX SHARES THE HOST NETWORK' "$out")" 208 [[ $ok = 1 && $samens = 1 && $held = 1 ]] && grep -q '!!! WARNING: FORGED_NETNS=off' "$out" && 209 grep -q 'shared network (FORGED_NETNS=off), no socket filter, serving the inherited listener' "$out" && 210 grep -q 'serving the listener inherited' "$out" 211 } 212 213 # Every network and ioctl system call of the sandboxed server and its children across the parity, 214 # smoke and download routes, plus the signature check and git inside the sandbox. Under the socket 215 # filter none of them may be refused (EPERM): the filter must not change what forged does. 216 netns_trace() { 217 local bad=0 n 218 port_free || return 1 219 FORGED_LISTEN_PORT=$PORT setsid strace -f -e trace=network,ioctl,process,prctl -o "$T/netns-trace.strace" \ 220 bash "$WRAPPER" "$T/forged" > "$T/netns-trace-server.log" 2>&1 & 221 PID=$! 222 for i in $(seq 1 100); do 223 curl --max-time 1 -fsS "http://127.0.0.1:$PORT/" -o /dev/null && break 224 sleep .1 225 done 226 rm -rf "$DATA/archives" 227 mkdir -p "$T/http-trace" 228 fetch trace || bad=1 229 FORGE_SMOKE_PORT=$PORT FORGE_TEST_PORT=$PORT bash infra/smoke.sh local > "$T/netns-trace-smoke.log" 2>&1 || bad=1 230 FORGE_TEST_STORAGE="$H/.radicle/storage" FORGE_TEST_EVIDENCE="$T/http-trace" bash dl/tests/http-check.sh "$PORT" \ 231 > "$T/netns-trace-downloads.log" 2>&1 || bad=1 232 stop || bad=1 233 strace -f -e trace=network,ioctl,process,prctl -o "$T/netns-trace-tools.strace" bash "$WRAPPER" /usr/bin/ssh-keygen -Y verify \ 234 -f "$DATA/allowed" -I fixture -n sandbox -s "$DATA/message.sig" < "$DATA/message" || bad=1 235 # the repository the wrapper binds: the fixture's Radicle copy, or in a store-reading build the store's 236 local gd="$H/.radicle/storage/$RID" 237 [[ ${SANDBOX_STORE:-0} = 1 ]] && gd="$H/interplane-store/projects/cuda-crack/repo.git" 238 strace -f -e trace=network,ioctl,process,prctl -o "$T/netns-trace-git.strace" bash "$WRAPPER" /usr/bin/git \ 239 --git-dir="$gd" log --oneline -1 --all || bad=1 240 sha256sum "$T"/netns-trace*.strace 241 echo '---- network and ioctl calls by name (server and its children)' 242 grep -ohE '^[0-9]+ +[a-z_0-9]+\(' "$T/netns-trace.strace" | awk '{print $2}' | sort | uniq -c 243 echo '---- socket() and connect() calls' 244 grep -hE ' (socket|connect|bind|sendto|sendmsg)\(' "$T"/netns-trace*.strace | sed 's/^[0-9]* *//' | sort | uniq -c 245 echo '---- ioctl requests and results (bash and bwrap outside the filter included)' 246 grep -h ' ioctl(' "$T"/netns-trace*.strace | sed -E 's/^[0-9]+ +//; s/ioctl\([0-9]+, ([A-Z_0-9]+).*= (-?[0-9]+( [A-Z]+)?).*/\1 \2/' | 247 sort | uniq -c 248 echo '---- refused by the filter (EPERM)' 249 # The one expected refusal: glibc's name-cache lookup (connect to /var/run/nscd/socket), which got 250 # ENOENT before the listener guard refused connect; glibc reads the files either way. 251 local nscd='connect\([0-9]+, \{sa_family=AF_UNIX, sun_path="/var/run/nscd/socket"\}, 110\) = -1 EPERM' 252 n=$(grep -h ' = -1 EPERM' "$T"/netns-trace*.strace | grep -cvE "$nscd" || true) 253 grep -h ' = -1 EPERM' "$T"/netns-trace*.strace | sed 's/^[0-9]* *//' | sort | uniq -c 254 echo "name-cache connect refused: $(cat "$T"/netns-trace*.strace | grep -cE "$nscd" || true)" 255 echo "other EPERM results: $n" 256 [[ $n = 0 ]] || bad=1 257 echo '---- the filter covers forged, git and ssh-keygen (seccomp-order.py)' 258 # git and ssh-keygen run as /app/forged in their own traces (the wrapper mounts its program there). 259 python3 infra/test/seccomp-order.py "$T/netns-trace.strace=--inherited-listener" "$T/netns-trace-git.strace=log" \ 260 "$T/netns-trace-tools.strace=-Y" || bad=1 261 grep -q 'execve("/usr/bin/git"' "$T/netns-trace.strace" || { echo 'FAIL forged ran no git in the trace'; bad=1; } 262 # Control: a filter that allows everything. leakprobe then gets answers under a filter, and the 263 # same checker must say so. 264 printf '\x06\x00\x00\x00\x00\x00\xff\x7f' > "$T/allow-all.bpf" 265 FORGED_LISTEN_PORT=$PORT FORGED_SECCOMP=$T/allow-all.bpf strace -f -e trace=network,ioctl,process,prctl \ 266 -o "$T/allow-all.strace" bash "$WRAPPER" "$T/leakprobe" > "$T/allow-all.log" 2>&1 || true 267 grep -E '^leakprobe leaks [1-9]' "$T/allow-all.log" || { echo 'FAIL the allow-all control did not run'; bad=1; } 268 if python3 infra/test/seccomp-order.py "$T/allow-all.strace" > "$T/allow-all.check" 2>&1; then 269 echo 'FAIL the checker passed a filter that allows everything'; bad=1 270 else head -3 "$T/allow-all.check"; echo 'CONTROL allow-all filter: interface answers under the filter caught'; fi 271 return "$bad" 272 } 273 # ---- r2 finding 1: interface questions through the inherited listener and accepted connections. 274 # leakprobe (infra/test/leakprobe.c) asks through descriptor 0 and through one accepted 275 # connection: SIOCGIFCONF, SIOCGIFNAME by index, SIOCGIFADDR, SIOCGIFHWADDR and SIOCGIFNETMASK by 276 # name, SO_BINDTOIFINDEX then SO_BINDTODEVICE, and a netlink RTM_GETADDR dump. Exit 0: no host 277 # interface name or address learned; 1: a leak. $1: the wrapper to run (this tree's, or the old). 278 ifleak_run() { 279 local wrapper=$1 label=$2 rc=0 280 port_free || return 9 281 ( sleep 1; curl -s --max-time 3 "http://127.0.0.1:$PORT/" -o /dev/null ) & 282 FORGED_LISTEN_PORT=$PORT bash "$wrapper" "$T/leakprobe" accept > "$T/ifleak-$label.out" 2>&1 || rc=$? 283 wait 284 # The probe prints learned host names and addresses; the evidence keeps their counts only. 285 sed -E 's/^(LEAK [A-Za-z_-]+) .*/\1 /' "$T/ifleak-$label.out" 286 sha256sum "$T/ifleak-$label.out" 287 rm -f "$T/ifleak-$label.out" 288 return "$rc" 289 } 290 netns_ifleak_check() { 291 local rc=0 out 292 out=$(ifleak_run "$WRAPPER" new) || rc=$? 293 printf '%s\n' "$out" 294 [[ $rc = 0 ]] || return 1 295 grep -qx "getsockname 127.0.0.1:$PORT" <<<"$out" && 296 grep -qx 'SIOCGIFCONF errno 1' <<<"$out" && 297 grep -qx 'SIOCGIFNAME errno 1' <<<"$out" && 298 grep -qx 'SIOCGIFADDR lo errno 1' <<<"$out" && 299 grep -qx 'accepted a connection' <<<"$out" && 300 grep -qx 'accepted-SIOCGIFCONF errno 1' <<<"$out" && 301 grep -qx 'accepted-SO_BINDTOIFINDEX errno 1' <<<"$out" && 302 grep -qx 'SO_BINDTOIFINDEX errno 1' <<<"$out" && 303 grep -qx 'netlink socket errno 1' <<<"$out" && 304 grep -qx 'leakprobe leaks 0' <<<"$out" && 305 echo 'PASS every interface question through the listener and an accepted connection refused' 306 } 307 # Controls: the netns head before r2, and this tree's wrapper with the filter line removed 308 # ($T/nofilter.sh from netns_probe_check), both answer them. 309 netns_ifleak_control() { 310 local rc out w bad=0 311 for w in "$T/old-netns/forged-sandbox.sh:old" "$T/nofilter.sh:nofilter"; do 312 rc=0 313 out=$(ifleak_run "${w%:*}" "${w##*:}") || rc=$? 314 printf '%s\n' "$out" 315 if [[ $rc = 1 ]] && grep -q '^LEAK SIOCGIFCONF ' <<<"$out" && grep -q '^LEAK accepted-SIOCGIFCONF ' <<<"$out" && 316 grep -q '^LEAK SIOCGIFADDR ' <<<"$out" && grep -q '^LEAK SO_BINDTOIFINDEX ' <<<"$out"; then 317 echo "CONTROL ${w##*:} leaks: $(grep -c '^LEAK ' <<<"$out") answers" 318 else 319 echo "FAIL control ${w##*:} did not leak (exit $rc)"; bad=1 320 fi 321 done 322 return "$bad" 323 } 324 325 # The filter file is checked like the binaries: present, outside data and export, whole program. 326 seccomp_guard_check() { 327 local rc bad=0 g=$T/seccomp-guard 328 mkdir -p "$g" 329 cp "$T/leakprobe" "$g/leakprobe" 330 rc=0; FORGED_SECCOMP= bash "$WRAPPER" "$g/leakprobe" > "$g/out" 2>&1 || rc=$? 331 cat "$g/out"; [[ $rc = 2 ]] && grep -q 'missing forged-seccomp.bpf' "$g/out" || bad=1 332 head -c 12 "$T/forged-seccomp.bpf" > "$g/forged-seccomp.bpf" 333 rc=0; FORGED_SECCOMP= bash "$WRAPPER" "$g/leakprobe" > "$g/out" 2>&1 || rc=$? 334 cat "$g/out"; [[ $rc = 2 ]] && grep -q 'not a whole filter program' "$g/out" || bad=1 335 cp "$T/forged-seccomp.bpf" "$DATA/forged-seccomp.bpf" 336 rc=0; FORGED_SECCOMP=$DATA/forged-seccomp.bpf bash "$WRAPPER" "$g/leakprobe" > "$g/out" 2>&1 || rc=$? 337 cat "$g/out"; [[ $rc = 2 ]] && grep -q 'refusing a socket filter inside data or export' "$g/out" || bad=1 338 rm -f "$DATA/forged-seccomp.bpf" 339 return "$bad" 340 } 341 342 # ---- r2 finding 2: descriptors the starter holds never reach the sandbox. A canary file is open 343 # on descriptors 4 and 77 in the process that starts each path; leakprobe lists its descriptors 344 # and reports any above 2 (LEAK fd-inherited) and any that reads the canary (LEAK fd-canary). 345 # $1: directory holding forged-sandbox.sh and forged-service.sh (this tree's, or the old). 346 fd_paths_run() { 347 local dir=$1 label=$2 mode rc from i out bad=0 canary 348 canary="fd-canary-$label-$$-$RANDOM" 349 printf '%s\n' "$canary" > "$T/fd-canary.txt" 350 for mode in direct listen netns-off sandbox-off; do 351 out=$T/fd-$label-$mode.out 352 rc=0 353 case $mode in 354 direct) ( exec 4<"$T/fd-canary.txt" 77<"$T/fd-canary.txt" 355 bash "$dir/forged-sandbox.sh" "$T/leakprobe" "canary=$canary" ) > "$out" 2>&1 || rc=$? ;; 356 listen) ( exec 4<"$T/fd-canary.txt" 77<"$T/fd-canary.txt" 357 FORGED_LISTEN_PORT=$PORT bash "$dir/forged-sandbox.sh" "$T/leakprobe" "canary=$canary" ) > "$out" 2>&1 || rc=$? ;; 358 netns-off) ( exec 4<"$T/fd-canary.txt" 77<"$T/fd-canary.txt" 359 FORGED_NETNS=off bash "$dir/forged-sandbox.sh" "$T/leakprobe" "canary=$canary" ) > "$out" 2>&1 || rc=$? ;; 360 sandbox-off) ( exec 4<"$T/fd-canary.txt" 77<"$T/fd-canary.txt" 361 FORGED_SANDBOX=off bash "$dir/forged-sandbox.sh" "$T/leakprobe" "canary=$canary" ) > "$out" 2>&1 || rc=$? ;; 362 esac 363 echo "== $label $mode exit $rc" 364 grep -E '^(fd |LEAK fd|forged-sandbox|!!!)' "$out" 365 sha256sum "$out" 366 grep -q '^LEAK fd' "$out" && bad=1 367 done 368 # The service path: the supervisor itself holds the canary (its starter's descriptors), plus its 369 # own log pipe and, in r2, the listener it keeps. r2's supervisor is handed a listener on 370 # descriptor 0 by hand.py, so it starts without passing through forged-listen (which would close 371 # the canary itself); the old supervisor takes no listener and is started directly. 372 local hand=() 373 [[ $label = old ]] || hand=(python3 "$T/hand.py" "$PORT") 374 for mode in on netns-off sandbox-off; do 375 out=$T/fd-$label-service-$mode.out 376 from=$(( $(stat -c %s "$DATA/logs/forged.log" 2>/dev/null || echo 0) + 1 )) 377 ( exec 4<"$T/fd-canary.txt" 77<"$T/fd-canary.txt" 378 case $mode in netns-off) export FORGED_NETNS=off;; sandbox-off) export FORGED_SANDBOX=off;; esac 379 FORGED_BIN=$T/leakprobe FORGED_PORT=$PORT FORGED_CPUS=$CPUS FORGED_COUNTS_INTERVAL=1800 \ 380 FORGED_LISTEN_HELPER=$T/forged-listen FORGED_SECCOMP=$T/forged-seccomp.bpf \ 381 exec setsid "${hand[@]}" bash "$dir/forged-service.sh" _loop ) > "$T/fd-$label-service-$mode.process.log" 2>&1 & 382 PID=$! 383 for i in $(seq 1 100); do 384 tail -c "+$from" "$DATA/logs/forged.log" 2>/dev/null | grep -q '^leakprobe leaks ' && break 385 sleep .1 386 done 387 stop || bad=1 388 tail -c "+$from" "$DATA/logs/forged.log" > "$out" 389 # The probe started once or more; judge its first run. 390 awk '/^leakprobe start/ {n++} n == 1' "$out" > "$out.first" 391 echo "== $label service-$mode (first start)" 392 grep -E '^(fd |LEAK fd|forged-sandbox|!!!)' "$out.first" 393 sha256sum "$out" "$out.first" 394 grep -q '^leakprobe leaks ' "$out.first" || { echo "FAIL the probe did not run on service-$mode"; bad=2; } 395 grep -q '^LEAK fd' "$out.first" && bad=1 396 done 397 return "$bad" 398 } 399 netns_fd_check() { 400 local rc=0 401 fd_paths_run "$ROOT/infra/spark" new || rc=$? 402 [[ $rc = 0 ]] && echo 'PASS no starter descriptor reaches the sandbox on any path' 403 return "$rc" 404 } 405 netns_fd_control() { 406 local rc=0 407 fd_paths_run "$T/old-netns" old || rc=$? 408 [[ $rc = 1 ]] && echo 'CONTROL the old head passes starter descriptors through' 409 [[ $rc = 1 ]] 410 } 411 412 # ---- r2 finding 4: the supervisor holds the port for its whole life. 413 # hand.py PORT CMD...: make the 127.0.0.1:PORT listener on descriptor 0 and execute CMD, keeping 414 # every descriptor the caller passed (a stand-in for a starter that leaks them). 415 cat > "$T/hand.py" <<'PY' 416 import os, socket, sys 417 s = socket.socket() 418 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 419 s.bind(('127.0.0.1', int(sys.argv[1]))) 420 s.listen(64) 421 os.dup2(s.fileno(), 0) 422 s.close() 423 os.execvp(sys.argv[2], sys.argv[2:]) 424 PY 425 cat > "$T/racer.py" <<'PY' 426 import socket, sys, time 427 port, seconds = int(sys.argv[1]), float(sys.argv[2]) 428 end, tries = time.monotonic() + seconds, 0 429 while time.monotonic() < end: 430 s = socket.socket() 431 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 432 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) 433 tries += 1 434 try: 435 s.bind(('127.0.0.1', port)) 436 s.listen(1) 437 s.close() 438 print(f'racer WON after {tries} tries') 439 sys.exit(0) 440 except OSError: 441 s.close() 442 time.sleep(.001) 443 print(f'racer lost {tries} tries') 444 sys.exit(1) 445 PY 446 # Kill the running forged 20 times; each time a racer tries to bind the port for 2.5 s. Each forged 447 # runs 5 s first: the supervisor backs off 30 s after five exits within 5 s of their start. 448 race_run() { 449 local svc=$1 label=$2 i fpid won=0 ready 450 FORGED_BIN=$T/forged FORGED_PORT=$PORT FORGED_CPUS=$CPUS FORGED_COUNTS_INTERVAL=1800 \ 451 setsid bash "$svc" _loop > "$T/race-$label.process.log" 2>&1 & 452 PID=$! 453 for i in $(seq 1 20); do 454 ready=0 455 for _ in $(seq 1 150); do 456 if curl --max-time 1 -fs "http://127.0.0.1:$PORT/" -o /dev/null && fpid=$(forged_below "$PID"); then ready=1; break; fi 457 sleep .1 458 done 459 [[ $ready = 1 ]] || { echo "race $i: the service did not come back"; stop; return 2; } 460 sleep 5 461 kill -KILL "$fpid" 462 if python3 "$T/racer.py" "$PORT" 2.5; then won=$((won+1)); echo "race $i: another process took the port" 463 else echo "race $i: the port stayed held"; fi 464 done 465 stop || return 2 466 echo "$label: another process won $won of 20" 467 [[ $won = 0 ]] && return 0 468 return 1 469 } 470 netns_race_check() { race_run infra/spark/forged-service.sh new; } 471 netns_race_control() { 472 local rc=0 473 race_run "$T/old-netns/forged-service.sh" old || rc=$? 474 [[ $rc = 1 ]] 475 } 476 477 # A port already taken at first start: the supervisor does not start, on both entry points. 478 netns_taken_check() { 479 local rc bad=0 occ s=forged-r2-taken-$$ 480 "$T/forged-listen" "$PORT" /usr/bin/sleep 60 > /dev/null 2>&1 & 481 occ=$! 482 for _ in $(seq 1 50); do [[ -n $(ss -ltnH "sport = :$PORT") ]] && break; sleep .1; done 483 rc=0; FORGED_BIN=$T/forged FORGED_PORT=$PORT FORGED_CPUS=$CPUS FORGED_SESSION=$s \ 484 bash infra/spark/forged-service.sh start > "$T/taken-start.out" 2>&1 || rc=$? 485 cat "$T/taken-start.out"; echo "start exit $rc" 486 [[ $rc = 1 ]] && grep -q 'already in use by something else; refusing' "$T/taken-start.out" || bad=1 487 ! tmux has-session -t "=$s" 2>/dev/null || { echo 'FAIL a session started'; tmux kill-session -t "=$s"; bad=1; } 488 rc=0; FORGED_BIN=$T/forged FORGED_PORT=$PORT FORGED_CPUS=$CPUS FORGED_COUNTS_INTERVAL=1800 \ 489 timeout 20 bash infra/spark/forged-service.sh _loop > "$T/taken-loop.out" 2>&1 || rc=$? 490 cat "$T/taken-loop.out"; echo "supervisor loop exit $rc (forged-listen: 3 when the port is taken)" 491 [[ $rc = 3 ]] || bad=1 492 ss -ltnpH "sport = :$PORT" | grep -q "pid=$occ," || { echo 'FAIL the occupier lost the port'; bad=1; } 493 kill "$occ"; wait "$occ" 2>/dev/null 494 return "$bad" 495 } 496 497 # 200 requests across an automatic restart for a new repository set (restart-hold.py). 498 netns_hold_check() { python3 infra/test/restart-hold.py "$T" "$ROOT/infra/spark/forged-service.sh" "$PORT" new; } 499 # Control: the old supervisor restarts forged with the port unbound for a moment. It must restart 500 # within the requests and leave the listener absent in some samples or fail some requests. 501 netns_hold_control() { 502 local rc=0 out 503 out=$(python3 infra/test/restart-hold.py "$T" "$T/old-netns/forged-service.sh" "$PORT" old) || rc=$? 504 printf '%s\n' "$out" 505 [[ $rc = 1 ]] && grep -q 'forged processes during the requests [2-9]' <<<"$out" && 506 grep -Eq 'failed [1-9]|without listener [1-9]' <<<"$out" 507 } 508 509 # ---- listener guard (netns r2 review of `8f6b4a29`, findings 1 and 2, and connect with AF_UNSPEC): 510 # the child must not be able to give away the listener the supervisor holds. seccomp-table.py runs 511 # the filter bytes over a table of calls; listener-poison.py makes the calls in the sandbox on a 512 # listener from the real forged-listen, then a local rival tries the port, then the successor forged 513 # starts on the held listener and 8 connections show who answers. 514 POISON_MODES=(shutdown-rd shutdown-rdwr unspec-connect reuseport) 515 seccomp_table_check() { python3 infra/test/seccomp-table.py "$T/forged-seccomp.bpf"; } 516 # Control: the `8f6b4a29` filter allows exactly the calls the guard refuses, and nothing else changed. 517 seccomp_table_control() { 518 local out rc=0 want got 519 out=$(python3 infra/test/seccomp-table.py "$T/poison/old.bpf") || rc=$? 520 printf '%s\n' "$out" 521 [[ $rc = 1 ]] || return 1 522 want='FAIL SO_REUSEPORT with high bits|FAIL connect (AF_UNSPEC on the listener disconnects it)|FAIL setsockopt SO_REUSEPORT (finding 2)|FAIL shutdown SHUT_RD (finding 1)|FAIL shutdown SHUT_RDWR (finding 1)|FAIL shutdown SHUT_WR|' 523 got=$(grep '^FAIL' <<<"$out" | sed 's/:.*//' | LC_ALL=C sort | tr '\n' '|') 524 [[ $got = "$want" ]] || { echo "FAIL the control failed other rows: $got"; return 1; } 525 echo 'CONTROL `8f6b4a29` filter allows shutdown, connect and SO_REUSEPORT, and only those' 526 } 527 # One run: $1 filter, $2 label, $3 mode. Returns listener-poison.py's exit. 528 poison_run() { 529 local bpf=$1 label=$2 mode=$3 rc=0 i 530 port_free || { echo "FAIL port $PORT busy"; return 9; } 531 "$T/forged-listen" "$PORT" /usr/bin/env HOME="$HOME" PATH=/usr/bin:/bin FORGED_SECCOMP="$bpf" \ 532 FORGED_READ_HOME="$H" FORGED_PATH_HOME="$H" FORGE_DATA="$DATA" FORGED_PUBLIC_DIR="$FORGED_PUBLIC_DIR" \ 533 FORGED_PUBLIC_LIB="$FORGED_PUBLIC_LIB" FORGED_PUBLIC_READER="$FORGED_PUBLIC_READER" \ 534 /usr/bin/python3 "$ROOT/infra/test/listener-poison.py" "$mode" "$label" "$T/poison" "$WRAPPER" \ 535 "$T/leakprobe" "$T/forged" > "$T/poison/$label-$mode.log" 2>&1 || rc=$? 536 cat "$T/poison/$label-$mode.log" 537 for i in $(seq 1 50); do [[ -z $(ss -ltnH "sport = :$PORT") ]] && break; sleep .1; done 538 return "$rc" 539 } 540 netns_poison_check() { 541 local m bad=0 542 for m in "${POISON_MODES[@]}"; do 543 if poison_run "$T/forged-seccomp.bpf" new "$m"; then echo "PASS $m: refused; the listener stayed the supervisor's" 544 else echo "FAIL $m"; bad=1; fi 545 done 546 return "$bad" 547 } 548 # Exit 1, the child's call succeeded, the rival bound the port and took connections: reproduced. 549 poison_open() { 550 local label=$1 mode=$2 rc=0 r try 551 # SO_REUSEPORT splits connections between the two listeners by a hash of the 4-tuple, so under 552 # load all 8 can land on forged by chance (MODELED: 0.5^8, about 1 in 256 if the hash were uniform; not 553 # confirmed by any log line, the one observed failure, gates-r14, shows only rival_got=0/8). Up to 4 tries, each a fresh run 554 # with fresh client ports. A pass needs the full signature (exit 1, the probe call succeeded, the 555 # rival bound and took a connection); a filter that really refuses the call gives probe_errno=1 556 # or a lost bind on every try, so a retry cannot turn a real failure into a pass. 557 for try in 1 2 3 4; do 558 rc=0 559 poison_run "$T/poison/$label.bpf" "$label" "$mode" || rc=$? 560 r=$(cat "$T/poison/poison-$label-$mode.txt" 2>/dev/null || true) 561 if [[ $rc = 1 && $r = *"probe_errno=0 rival=WON "* && $r != *"rival_got=0/8" ]]; then return 0; fi 562 [[ $r = *"probe_errno=0 rival=WON "* && $r = *"rival_got=0/8" ]] || return 1 563 echo "retry $try: rival won the bind but took no connection (hash placed all 8 on forged)" 564 done 565 return 1 566 } 567 netns_poison_control() { 568 local m bad=0 569 for m in "${POISON_MODES[@]}"; do 570 if poison_open old "$m"; then echo "CONTROL `8f6b4a29` $m: the rival took the port and connections" 571 else echo "FAIL control `8f6b4a29` $m did not reproduce"; bad=1; fi 572 done 573 return "$bad" 574 } 575 # Each mutant drops one rule; the table and the kernel must both catch it. 576 netns_poison_mutants() { 577 local bad=0 m mode rc 578 for m in m-shutdown:shutdown-rd m-connect:unspec-connect m-reuseport:reuseport; do 579 mode=${m#*:}; m=${m%%:*} 580 rc=0; python3 infra/test/seccomp-table.py "$T/poison/$m.bpf" > "$T/poison/$m.table" 2>&1 || rc=$? 581 grep -E '^(FAIL|seccomp-table)' "$T/poison/$m.table" || true 582 [[ $rc = 1 ]] || { echo "FAIL mutant $m passed the table"; bad=1; } 583 if poison_open "$m" "$mode"; then echo "MUTANT $m caught: $mode gives the port away" 584 else echo "FAIL mutant $m: $mode did not give the port away"; bad=1; fi 585 done 586 return "$bad" 587 } 588 589 # ---- the systemd unit (infra/spark/interplane-forged.service). A runtime copy of the repo unit runs 590 # this run's forged on the private port: only FORGED_DIR, the data paths, the cores and the 591 # test-only settings the fixture needs (home paths, port, filter, session) differ from the repo file. 592 # The copy lives in $XDG_RUNTIME_DIR/systemd/user (gone at logout) and is removed at the end. 593 UNIT_NAME=forged-gate-$$.service 594 unit_dir() { printf '%s\n' "${XDG_RUNTIME_DIR:-/run/user/$(id -u)}/systemd/user"; } 595 unit_install() { 596 local d u=$1 597 d=$(unit_dir); mkdir -p "$d" "$T/unitdir/out" "$T/unitdir/infra" 598 # Copies, as a deploy installs them: the wrapper resolves FORGED_BIN, and forged-service.sh finds 599 # forged by that path below the supervisor. 600 cp "$T/forged" "$T/forged-listen" "$T/public-read" "$T/forged-seccomp.bpf" "$T/unitdir/out/" 601 ln -sfn "$ROOT/infra/spark" "$T/unitdir/infra/spark" 602 sed -e "s|^Environment=FORGED_DIR=.*|Environment=FORGED_DIR=$T/unitdir|" \ 603 -e "s|^Environment=FORGE_DATA=.*|Environment=FORGE_DATA=$DATA FORGED_PUBLIC_DIR=$FORGED_PUBLIC_DIR FORGED_COUNTS_STATE=$H/forge-counts-state|" \ 604 -e "s|FORGED_CPUS=4,5 |FORGED_CPUS=$CPUS |" \ 605 -e "s| /usr/bin/bash | FORGED_READ_HOME=$H FORGED_PATH_HOME=$H FORGED_PORT=$PORT FORGED_SECCOMP=$T/forged-seccomp.bpf FORGED_SESSION=forged-gate-$$ FORGED_MIRROR_INTERVAL=1 /usr/bin/bash |" \ 606 infra/spark/interplane-forged.service > "$d/$u" 607 diff infra/spark/interplane-forged.service "$d/$u" || true 608 [[ $(diff infra/spark/interplane-forged.service "$d/$u" | grep -c '^>') = 3 ]] || { echo 'FAIL the copy changed more than 3 lines'; return 1; } 609 systemctl --user daemon-reload 610 } 611 unit_remove() { 612 local u=$1 613 systemctl --user stop "$u" 2>/dev/null || true 614 rm -f "$(unit_dir)/$u" 615 systemctl --user daemon-reload 616 systemctl --user reset-failed "$u" 2>/dev/null || true 617 } 618 # A gate stopped by a signal during netns-unit or netns-unit-control (the EXIT trap in sandbox.sh) 619 # must not leave the Restart=always copy behind, restarting this run's forged on the private port. 620 unit_cleanup() { [[ ! -e $(unit_dir)/$UNIT_NAME ]] || unit_remove "$UNIT_NAME"; } 621 unit_prop() { systemctl --user show -p "$2" --value "$1"; } 622 # Wait (at most $2 s) for GET / -> 200 on the private port. 623 unit_up() { 624 local i n=$(( ${1:-30} * 10 )) 625 for i in $(seq 1 "$n"); do 626 [[ $(curl -s -o /dev/null -m 1 -w '%{http_code}' "http://127.0.0.1:$PORT/") = 200 ]] && return 0 627 sleep .1 628 done 629 return 1 630 } 631 # FORGED_SESSION: a tmux session name of this run only, so no script here can reach the live one. 632 svc_cmd() { FORGED_UNIT=$UNIT_NAME FORGE_DATA=$DATA FORGED_PORT=$PORT FORGED_SESSION=forged-gate-$$ bash "$1" "$2"; } 633 netns_unit_check() { 634 local u=$UNIT_NAME bad=0 m0 m1 r0 t0 out i 635 port_free || { echo "FAIL port $PORT busy"; return 1; } 636 systemd-analyze --user verify infra/spark/interplane-forged.service infra/spark/interplane-forged-tunnel.service || bad=1 637 unit_install "$u" || return 1 638 # start through the script: it acts on the unit 639 out=$(svc_cmd "$ROOT/infra/spark/forged-service.sh" start) || bad=1 640 printf '%s\n' "$out" 641 grep -q "^forged up on 127.0.0.1:$PORT " <<<"$out" || { echo 'FAIL start through the unit'; bad=1; } 642 [[ $(unit_prop "$u" ActiveState) = active ]] || { echo 'FAIL unit not active'; bad=1; } 643 out=$(svc_cmd "$ROOT/infra/spark/forged-service.sh" status) || bad=1 644 printf '%s\n' "$out" 645 grep -q "^systemd $u: active " <<<"$out" || { echo 'FAIL status does not name the unit'; bad=1; } 646 grep -q '^pid [0-9]' <<<"$out" || { echo 'FAIL status does not find forged below the supervisor'; bad=1; } 647 ! tmux has-session -t "=forged-gate-$$" 2>/dev/null || { echo 'FAIL a tmux session was started'; bad=1; } 648 grep -o 'forged-sandbox: on;.*' "$DATA/logs/forged.log" | tail -1 649 # kill -9 of the supervisor: systemd restarts the unit by itself. 650 m0=$(unit_prop "$u" MainPID); r0=$(unit_prop "$u" NRestarts); t0=$(date +%s.%N) 651 kill -9 "$m0" 652 unit_up 40 || { echo 'FAIL no answer after kill -9 of the supervisor'; bad=1; } 653 m1=$(unit_prop "$u" MainPID) 654 echo "kill -9 supervisor $m0: back as $m1, restarts $r0 -> $(unit_prop "$u" NRestarts), answered after $(echo "$(date +%s.%N) - $t0" | bc) s" 655 [[ $m1 != "$m0" && $(unit_prop "$u" NRestarts) -gt $r0 ]] || { echo 'FAIL systemd did not restart the supervisor'; bad=1; } 656 # stop through the script: the unit stops and stays stopped. 657 out=$(svc_cmd "$ROOT/infra/spark/forged-service.sh" stop) || bad=1 658 printf '%s\n' "$out" 659 sleep 3 660 echo "after stop: $(unit_prop "$u" ActiveState), port: $(ss -ltnH "sport = :$PORT" | wc -l) listeners" 661 [[ $(unit_prop "$u" ActiveState) = inactive && -z $(ss -ltnH "sport = :$PORT") ]] || { echo 'FAIL stop did not stop the unit'; bad=1; } 662 # A held port: the unit retries with backoff and never gives up; it starts once the port is free. 663 python3 -c 'import socket,sys,time; s=socket.socket(); s.bind(("127.0.0.1",int(sys.argv[1]))); s.listen(1); time.sleep(12)' "$PORT" & 664 local holder=$! 665 sleep .5 666 r0=$(unit_prop "$u" NRestarts) 667 systemctl --user start "$u" 668 sleep 8 669 echo "port held 8 s: $(unit_prop "$u" ActiveState)/$(unit_prop "$u" SubState), restarts $r0 -> $(unit_prop "$u" NRestarts)" 670 # RestartSec=1s: about one attempt a second, and the unit never gives up. 671 [[ $(unit_prop "$u" ActiveState) != failed && $(unit_prop "$u" NRestarts) -ge $((r0 + 4)) && $(unit_prop "$u" NRestarts) -le $((r0 + 9)) ]] || 672 { echo 'FAIL not about one retry per second while the port was held'; bad=1; } 673 journalctl --user -u "$u" --no-pager -o cat | grep -E 'Scheduled restart|exited' | tail -4 || true 674 wait "$holder" 675 unit_up 20 || { echo 'FAIL the unit did not start after the port was freed'; bad=1; } 676 echo "port freed: $(unit_prop "$u" ActiveState), restarts $(unit_prop "$u" NRestarts)" 677 unit_remove "$u" 678 for i in $(seq 1 50); do [[ -z $(ss -ltnH "sport = :$PORT") ]] && break; sleep .1; done 679 [[ $bad = 0 ]] && echo "PASS the unit: start/status/stop through forged-service.sh, kill -9 recovery, held-port backoff" 680 return "$bad" 681 } 682 # Control: `8f6b4a29`'s forged-service.sh stop does not stop the unit; systemd starts the supervisor again. 683 netns_unit_control() { 684 local u=$UNIT_NAME rc=0 r0 685 mkdir -p "$T/old-guard" 686 git show "$POISON_OLD:infra/spark/forged-service.sh" > "$T/old-guard/forged-service.sh" || return 1 687 cp infra/spark/forged-sandbox.sh infra/spark/forged-public.sh infra/spark/forged-counts.sh "$T/old-guard/" 688 unit_install "$u" || return 1 689 systemctl --user start "$u" 690 unit_up 40 || { unit_remove "$u"; return 1; } 691 r0=$(unit_prop "$u" NRestarts) 692 svc_cmd "$T/old-guard/forged-service.sh" stop || true 693 sleep 3 694 echo "after the old stop: $(unit_prop "$u" ActiveState), restarts $r0 -> $(unit_prop "$u" NRestarts), port listeners $(ss -ltnH "sport = :$PORT" | wc -l)" 695 [[ $(unit_prop "$u" ActiveState) = active && $(unit_prop "$u" NRestarts) -gt $r0 ]] || rc=1 696 unit_remove "$u" 697 [[ $rc = 0 ]] && echo 'CONTROL `8f6b4a29` stop: systemd started the supervisor again (the old script cannot stop the unit)' 698 return "$rc" 699 }