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-hardening.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-hardening.sh` 249 lines ยท 11968 bytes Raw Blame History 1 # Clean environment, resource limits, binary guards, umask and the service log pipe. 2 # Sourced by infra/test/sandbox.sh after its fixture, build and helpers exist 3 # (T H DATA PORT CPUS WRAPPER ROOT OMC PID record start stop paths). 4 CANARY=forged-r5-starter-canary-$$-$RANDOM 5 OLD=`9b2261c789c39b1dbc13b3a98bb23a1b67d7c46b` # r4 head: the scripts before this hardening 6 7 hardening_build() { 8 "$OMC" < infra/test/envprobe.om > "$T/envprobe" || return 1 9 "$OMC" < infra/test/hog.om > "$T/hog" || return 1 10 chmod 755 "$T/envprobe" "$T/hog" 11 sha256sum infra/test/envprobe.om infra/test/hog.om "$T/envprobe" "$T/hog" 12 } 13 14 # The r4 final line kept the starter's environment for bubblewrap, which is pid 1 inside. 15 env_mutant() { 16 sed 's|^exec env -i /usr/bin/setpriv|exec /usr/bin/setpriv|' "$WRAPPER" > "$T/env-mutant.sh" 17 grep -q '^exec /usr/bin/setpriv' "$T/env-mutant.sh" 18 } 19 20 # The r4 service scripts: the old start and _loop, with this tree's wrapper and libraries. 21 # r4 had no listener helper, so these controls run the wrapper with FORGED_NETNS=off, as r4 did. 22 old_service_dir() { 23 mkdir -p "$T/oldsvc" 24 git show "$OLD:infra/spark/forged-service.sh" > "$T/oldsvc/forged-service.sh" || return 1 25 cp infra/spark/forged-sandbox.sh infra/spark/forged-public.sh infra/spark/forged-counts.sh "$T/oldsvc/" 26 } 27 netns_for() { if [[ $1 = "$T/oldsvc" ]]; then echo off; else echo on; fi; } 28 29 # Every process of the service: the pane process, the supervisor and all their descendants. 30 service_tree() { 31 local p c 32 for p in "$@"; do 33 echo "$p" 34 for c in $(pgrep -P "$p" || true); do service_tree "$c"; done 35 done 36 } 37 38 # $1 service script directory, $2 label. Starts a service in a private tmux server with a 39 # canary in the starter's environment, then reads /proc//environ of every process in it. 40 # The tmux socket directory is short (mktemp under /tmp, mode 700): a socket path inside a deep 41 # evidence directory passes the 108-byte unix socket limit and tmux cannot start. 42 service_env_check() { 43 local dir=$1 tdir sess=forged-r5-$2 bad=0 n=0 p pane env 44 tdir=$(mktemp -d /tmp/forged-r5-tmux.XXXXXX) || return 1 45 echo "tmux directory $tdir" 46 if ! env -u TMUX TMUX_TMPDIR="$tdir" FORGED_R5_CANARY="$CANARY" FORGED_SESSION=$sess \ 47 FORGED_PORT=$PORT FORGED_BIN=$T/forged FORGED_CPUS=$CPUS FORGED_COUNTS_INTERVAL=1800 \ 48 FORGED_NETNS="$(netns_for "$dir")" bash "$dir/forged-service.sh" start; then 49 env -u TMUX TMUX_TMPDIR="$tdir" tmux kill-server 2>/dev/null || true 50 rm -rf "$tdir" 51 return 1 52 fi 53 pane=$(env -u TMUX TMUX_TMPDIR="$tdir" tmux list-panes -t "=$sess" -F '#{pane_pid}') 54 for p in $(service_tree "$pane"); do 55 [[ -r /proc/$p/environ ]] || continue 56 env=$(tr '\0' '\n' < "/proc/$p/environ" 2>/dev/null) || continue 57 n=$((n+1)) 58 if grep -qF "$CANARY" <<<"$env"; then 59 echo "LEAK pid $p $(cat "/proc/$p/comm" 2>/dev/null) $(grep -c . <<<"$env") variables"; bad=1 60 else 61 echo "clean pid $p $(cat "/proc/$p/comm" 2>/dev/null) $(grep -c . <<<"$env") variables: $(cut -d= -f1 <<<"$env" | tr '\n' ' ')" 62 fi 63 done 64 curl --max-time 5 -fsS "http://127.0.0.1:$PORT/" -o /dev/null || bad=2 65 env -u TMUX TMUX_TMPDIR="$tdir" FORGED_SESSION=$sess FORGED_PORT=$PORT FORGED_BIN=$T/forged \ 66 bash "$dir/forged-service.sh" stop || bad=3 67 env -u TMUX TMUX_TMPDIR="$tdir" tmux kill-server 2>/dev/null || true 68 rm -rf "$tdir" 69 echo "processes checked $n" 70 [[ $n -ge 3 ]] || bad=4 71 port_free || bad=5 72 # "stop" removes the pid file; later direct wrapper runs need the fixture back. 73 printf 'private direct-launch fixture\n' > "$DATA/forged-supervisor.pid" 74 return "$bad" 75 } 76 77 limit_values_check() { 78 local out 79 out=$(bash "$WRAPPER" /usr/bin/prlimit --noheadings --raw --output RESOURCE,SOFT,HARD) || return 1 80 printf '%s\n' "$out" 81 grep -qx 'NPROC 256 256' <<<"$out" && grep -qx 'AS `4294967296` `4294967296`' <<<"$out" && 82 grep -qx 'FSIZE `1073741824` `1073741824`' <<<"$out" && grep -qx 'NOFILE 1024 1024' <<<"$out" && 83 grep -qx 'CORE 0 0' <<<"$out" 84 } 85 86 fork_hog_check() { 87 local out n 88 out=$(bash "$WRAPPER" "$T/hog" fork) || return 1 89 printf '%s\n' "$out" 90 n=$(sed -n 's/^forks \([0-9]*\) error 11$/\1/p' <<<"$out") 91 # 256 tasks for the owner inside the namespace: bubblewrap pid 1, the hog, and its children. 92 [[ -n $n && $n -ge 250 && $n -lt 256 ]] 93 } 94 95 mem_hog_check() { 96 local rc=0 want got 97 timeout 300 bash "$WRAPPER" /usr/bin/git hash-object --stdin < /dev/zero || rc=$? 98 echo "unbounded input: exit $rc" 99 [[ $rc = 128 ]] || return 1 100 # Control: an ordinary 64 MiB object still hashes, byte-identical to the host. 101 want=$(head -c `67108864` /dev/zero | git hash-object --stdin) 102 got=$(head -c `67108864` /dev/zero | bash "$WRAPPER" /usr/bin/git hash-object --stdin) || return 1 103 echo "64 MiB object: sandbox $got host $want" 104 [[ $got = "$want" ]] 105 } 106 107 file_hog_check() { 108 local rc=0 size 109 bash "$WRAPPER" "$T/hog" file "$H/forge-data/hog.bin" || rc=$? 110 size=$(stat -c %s "$DATA/hog.bin") 111 rm -f "$DATA/hog.bin" 112 echo "exit $rc size $size" 113 [[ $rc = 153 && $size = `1073741824` ]] # 153 = 128 + SIGXFSZ (25) 114 } 115 116 # A normal request load against the sandboxed server, with peak tasks, memory and descriptors. 117 load_check() { 118 local w r p bad=0 tasks peak=0 hwm=0 fds=0 fpid s live wpids=() 119 # Cold archive cache, so the load includes git archive runs inside the sandbox. 120 rm -rf "$DATA/archives" 121 mv "$T/sandbox-server.log" "$T/sandbox-server.parity.log" 122 start sandbox || return 1 123 fpid=$(pgrep -f -n '^/app/forged') || return 1 124 for w in $(seq 1 8); do 125 ( 126 for r in 1 2 3; do 127 for p in "${paths[@]}"; do 128 curl --max-time 60 -fsS -o /dev/null "http://127.0.0.1:$PORT$p" || { echo "FAIL $p"; exit 1; } 129 done 130 done 131 ) > "$T/load-worker-$w.log" 2>&1 & 132 wpids+=($!) 133 done 134 live=1 135 while [[ $live = 1 ]]; do 136 live=0 137 for w in "${wpids[@]}"; do kill -0 "$w" 2>/dev/null && live=1; done 138 tasks=$(service_tree "$PID" | wc -l) 139 (( tasks > peak )) && peak=$tasks 140 s=$(ls "/proc/$fpid/fd" 2>/dev/null | wc -l) 141 (( s > fds )) && fds=$s 142 sleep .05 143 done 144 for w in "${wpids[@]}"; do wait "$w" || bad=1; done 145 for w in $(seq 1 8); do [[ ! -s $T/load-worker-$w.log ]] || { cat "$T/load-worker-$w.log"; bad=1; }; done 146 hwm=$(sed -n 's/^VmHWM:[[:space:]]*//p' "/proc/$fpid/status") 147 echo "requests $((8 * 3 * ${#paths[@]})) peak processes $peak peak forged fds $fds forged VmHWM $hwm" 148 grep '^VmPeak' "/proc/$fpid/status" 149 stop || bad=1 150 mv "$T/sandbox-server.log" "$T/load-server.log" 151 mv "$T/sandbox-server.parity.log" "$T/sandbox-server.log" 152 sha256sum "$T/load-server.log" >> "$T/SHA256SUMS" 153 return "$bad" 154 } 155 156 # A binary or collector inside the data or export directory is refused. 157 guard_check() { 158 local rc bad=0 159 cp "$T/envprobe" "$DATA/inside-bin" 160 ln -sfn "$DATA/inside-bin" "$T/inside-link" 161 for b in "$DATA/inside-bin" "$T/inside-link"; do 162 rc=0; bash "$WRAPPER" "$b" probe > "$T/guard.out" 2>&1 || rc=$? 163 grep -q 'refusing a forged binary inside the writable data directory' "$T/guard.out" && [[ $rc = 2 ]] || 164 { echo "FAIL data guard $b rc=$rc"; bad=1; } 165 done 166 cp "$T/envprobe" "$FORGED_PUBLIC_DIR/inside-bin" 167 rc=0; bash "$WRAPPER" "$FORGED_PUBLIC_DIR/inside-bin" probe > "$T/guard.out" 2>&1 || rc=$? 168 rm -f "$FORGED_PUBLIC_DIR/inside-bin" 169 grep -q 'refusing a forged binary inside the export directory' "$T/guard.out" && [[ $rc = 2 ]] || 170 { echo "FAIL export guard rc=$rc"; bad=1; } 171 # Teeth: the same binary runs under a wrapper with the data guard deleted. 172 grep -v 'inside the writable data directory' "$WRAPPER" > "$T/guard-mutant.sh" 173 rc=0; bash "$T/guard-mutant.sh" "$DATA/inside-bin" probe > "$T/guard.out" 2>&1 || rc=$? 174 grep -q '^processes seen' "$T/guard.out" && [[ $rc = 0 ]] || 175 { echo "FAIL guard mutant did not run the planted binary rc=$rc"; bad=1; } 176 # The count collector refuses at init and at every tick. 177 rc=0 178 ( BIN=$DATA/inside-bin FORGED_BIN=$DATA/inside-bin LOG=$T/guard-counts.log 179 source infra/spark/forged-counts.sh; counts_init ) > "$T/guard.out" 2>&1 || rc=$? 180 grep -q 'refusing collector binary inside writable data or export' "$T/guard.out" && [[ $rc = 2 ]] || 181 { echo "FAIL collector init guard rc=$rc"; cat "$T/guard.out"; bad=1; } 182 rc=0 183 printf 'stale\n' > "$PUBLIC/counts.snapshot" 184 ( LOG=$T/guard-counts.log; BIN=$T/forged 185 source infra/spark/forged-counts.sh; counts_init 186 BIN=$DATA/inside-bin; counts_tick guard 187 [[ -z $counts_pid ]] ) > "$T/guard.out" 2>&1 || rc=$? 188 grep -q 'refusing collector binary inside writable data or export' "$T/guard-counts.log" && 189 [[ $rc = 0 && ! -e $PUBLIC/counts.snapshot ]] || 190 { echo "FAIL collector tick guard rc=$rc"; cat "$T/guard.out" "$T/guard-counts.log"; bad=1; } 191 # The public reader too. 192 cp "$T/public-read" "$DATA/inside-reader" 193 rc=0; ( FORGED_PUBLIC_READER=$DATA/inside-reader; public_init ) > "$T/guard.out" 2>&1 || rc=$? 194 grep -q 'refusing public-read inside writable data or export' "$T/guard.out" && [[ $rc = 2 ]] || 195 { echo "FAIL reader guard rc=$rc"; cat "$T/guard.out"; bad=1; } 196 rm -f "$DATA/inside-bin" "$DATA/inside-reader" "$T/inside-link" 197 [[ $bad = 0 ]] && echo 'PASS binary, collector and reader inside data or export refused; a guard-less mutant runs it' 198 return "$bad" 199 } 200 201 # New files under the data directory are owner-only, whatever umask the starter had. The scan 202 # includes the data root: the fixture makes it 700, so a sandbox chmod of the root shows here too. 203 umask_check() { 204 local m=$1 found bad=0 205 found=$(find "$DATA" -newer "$m" \( -type f -o -type d \) -perm /077 -print) 206 echo "new entries under data since the marker: $(find "$DATA" -newer "$m" \( -type f -o -type d \) | wc -l)" 207 [[ -z $found ]] || { echo "FAIL group or world bits:"; printf '%s\n' "$found"; bad=1; } 208 [[ $(find "$DATA" -newer "$m" -type f | wc -l) -gt 0 ]] || { echo 'FAIL no new files to judge'; bad=1; } 209 ( umask 0002; bash "$WRAPPER" /usr/bin/dash -c 'printf x > "$1"' umask "$H/forge-data/umask-real" ) 210 sed '/^umask 0077$/d' "$WRAPPER" > "$T/umask-mutant.sh" 211 ( umask 0002; bash "$T/umask-mutant.sh" /usr/bin/dash -c 'printf x > "$1"' umask "$H/forge-data/umask-mutant" ) 212 echo "wrapper file mode $(stat -c %a "$DATA/umask-real"), mutant file mode $(stat -c %a "$DATA/umask-mutant")" 213 [[ $(stat -c %a "$DATA/umask-real") = 600 && $(stat -c %a "$DATA/umask-mutant") = 664 ]] || bad=1 214 rm -f "$DATA/umask-real" "$DATA/umask-mutant" 215 # The mirror and the count producer still read what they need. 216 public_sync || bad=1 217 [[ -r $FORGED_PUBLIC_DIR/RIDS.md ]] && cmp "$H/handoffs/radicle/RIDS.md" "$FORGED_PUBLIC_DIR/RIDS.md" || bad=1 218 return "$bad" 219 } 220 221 # The supervisor writes the service log through a pipe, so the file-size limit never applies 222 # to it. A sparse 2 GiB log stands for one that grew past 1 GiB. 223 log_fsize_check() { 224 local dir=$1 label=$2 want=$3 ok=0 i starts kills 225 rm -f "$DATA/logs/forged.log" 226 truncate -s 2G "$DATA/logs/forged.log" 227 FORGED_BIN=$T/forged FORGED_PORT=$PORT FORGED_CPUS=$CPUS FORGED_COUNTS_INTERVAL=1800 \ 228 FORGED_NETNS="$(netns_for "$dir")" \ 229 setsid bash "$dir/forged-service.sh" _loop > "$T/fsize-$label.process.log" 2>&1 & 230 PID=$! 231 for i in $(seq 1 100); do 232 curl --max-time 1 -fsS "http://127.0.0.1:$PORT/" -o /dev/null && break 233 sleep .1 234 done 235 for i in $(seq 1 20); do 236 curl --max-time 5 -fsS "http://127.0.0.1:$PORT/explore" -o /dev/null && ok=$((ok+1)) 237 sleep .1 238 done 239 sleep 1 240 stop || return 1 241 tail -c +`2147483649` "$DATA/logs/forged.log" > "$T/fsize-$label.service.log" 242 rm -f "$DATA/logs/forged.log" 243 : > "$DATA/logs/forged.log" 244 starts=$(grep -c 'forged-sandbox: on' "$T/fsize-$label.service.log" || true) 245 kills=$(grep -c 'rc=153' "$T/fsize-$label.service.log" || true) 246 echo "$label: $ok of 20 requests answered, sandbox starts $starts, SIGXFSZ exits $kills" 247 if [[ $want = keeps ]]; then [[ $ok = 20 && $starts = 1 && $kills = 0 ]] 248 else [[ $kills -ge 1 ]]; fi 249 }