interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / infra / spark / forged-sandbox.sh
| 1 | #!/usr/bin/env bash |
| 2 | # Host isolation adapter. No root, downloads, service installation, or automatic fallback. |
| 3 | # FORGED_READ_HOME supplies the published inputs. FORGED_PATH_HOME must match the |
| 4 | # paths compiled into the executable (only private test builds override it). |
| 5 | set -euo pipefail |
| 6 | # Data the child creates is private to the owner (no group or world bits). |
| 7 | umask 0077 |
| 8 | # Descriptors. Close everything above 2 the starter left open (a file a shell or test harness |
| 9 | # held, the supervisor's log pipe), on every path below, before anything is opened on purpose. |
| 10 | # Descriptor 0 stays: it is the inherited listener when the supervisor or forged-listen made one. |
| 11 | # The only descriptors opened after this loop are the ones bubblewrap reads and closes itself: |
| 12 | # the synthetic passwd entry and the socket filter. (No redirection on this loop: bash would park |
| 13 | # a copy of the redirected descriptor above 9 for the loop's duration, and the loop would close it. |
| 14 | # bash's own copy of this script is close-on-exec.) |
| 15 | for fd in /proc/self/fd/*; do |
| 16 | fd=${fd##*/} |
| 17 | if [[ $fd =~ ^[0-9]+$ ]] && ((fd > 2)); then eval "exec $fd>&-"; fi |
| 18 | done |
| 19 | bin=${1:-${FORGED_BIN:-$HOME/work/forge/web/server/forged}} |
| 20 | if (($#)); then shift; fi |
| 21 | # A listening socket on descriptor 0 is served by forged in every mode, so the port stays held by |
| 22 | # its long-lived owner (the supervisor) across restarts. |
| 23 | inherit=() |
| 24 | if [[ -S /dev/stdin ]]; then inherit=(--inherited-listener); fi |
| 25 | case ${FORGED_SANDBOX:-on} in |
| 26 | off) echo '!!! WARNING: FORGED_SANDBOX=off: OWNER ACCOUNT EXPOSED; SANDBOX DISABLED !!!' >&2 |
| 27 | exec "$bin" "${inherit[@]}" "$@" ;; |
| 28 | on) ;; |
| 29 | *) echo 'forged-sandbox: FORGED_SANDBOX must be on or off' >&2; exit 2 ;; |
| 30 | esac |
| 31 | die() { echo "forged-sandbox: $*" >&2; exit 2; } |
| 32 | src=${FORGED_READ_HOME:-$HOME} |
| 33 | dst=${FORGED_PATH_HOME:-/home/drakestapleton} |
| 34 | data=${FORGE_DATA:-$HOME/forge-data} |
| 35 | [[ $dst = /* && $dst != / && $dst != *'..'* ]] || die 'invalid compiled home' |
| 36 | bin=$(realpath -e "$bin") |
| 37 | src=$(realpath -e "$src") |
| 38 | data=$(realpath -e "$data") |
| 39 | [[ -f $bin && -x $bin && -d $data ]] || die 'binary or data directory missing' |
| 40 | [[ $data != / && $data != "$HOME" && $data != "$src" ]] || die 'data directory is too broad' |
| 41 | case "$HOME/" in "$data/"*) die 'data directory contains the owner home';; esac |
| 42 | case "$src/" in "$data/"*) die 'data directory contains the published inputs';; esac |
| 43 | # The owner runs this same binary unsandboxed for the count snapshot. A binary the child |
| 44 | # can write would let a compromised child run code as the owner on the next collection. |
| 45 | case "$bin" in "$data"/*) die 'refusing a forged binary inside the writable data directory';; esac |
| 46 | [[ -d $data/logs && ! -L $data/logs && -f $data/forged-supervisor.pid && ! -L $data/forged-supervisor.pid ]] || |
| 47 | die 'supervisor must create real logs/ and forged-supervisor.pid before sandbox start' |
| 48 | PUBLIC=${FORGED_PUBLIC_DIR:-$data/../forge-public} |
| 49 | PUBLIC=$(realpath -e "$PUBLIC") |
| 50 | case "$bin" in "$PUBLIC"/*) die 'refusing a forged binary inside the export directory';; esac |
| 51 | READ_HOME=$src |
| 52 | source "${FORGED_PUBLIC_LIB:-$(dirname "$0")/forged-public.sh}" |
| 53 | rids=$PUBLIC/RIDS.md |
| 54 | # The Radicle build reads the public list at run time; the store-reading build never does (its served |
| 55 | # set is the store's catalog), so a missing RIDS.md must not stop it. |
| 56 | if [[ -z ${FORGED_STORE:-} ]]; then |
| 57 | [[ -f $rids && ! -L $rids ]] || die 'public repository list missing or a symlink' |
| 58 | fi |
| 59 | # The native store (store-reading build, interplane-native DESIGN.md step 2): FORGED_STORE is its |
| 60 | # root on the host. The root is bound read-only, whole (catalog.tsv is replaced by rename on every |
| 61 | # change, so a file bind would keep the old one), at the path the binary was built with: the same |
| 62 | # path, or under the compiled home when it lies under the published-inputs home. ROOT/projects is then |
| 63 | # covered by an empty read-only tmpfs and only the projects the build serves (forged-public.sh |
| 64 | # store_names: public lines, valid names, not on the deny list) are bound back, each read-only, with |
| 65 | # its staging/ (landings in progress) and repo.git.migration/ (import evidence) covered by empty |
| 66 | # tmpfs; import-notes/ is covered the same way and import.tsv and IMPORT-RECEIPT.txt by /dev/null. |
| 67 | # So a private, denied or unlisted project is not in the sandbox at all. A project created after the |
| 68 | # start is bound only at the next start (landings into bound projects are visible at once: the |
| 69 | # project folder is bound, not a snapshot of it). The store holds no key (the signing key is kept |
| 70 | # outside it). Without FORGED_STORE the Radicle repositories of the public list are bound as before. |
| 71 | store= |
| 72 | if [[ -n ${FORGED_STORE:-} ]]; then |
| 73 | [[ $FORGED_STORE = /* && ! -L ${FORGED_STORE%/} ]] || die 'FORGED_STORE must be an absolute path and not a link' |
| 74 | store=$(realpath -e "$FORGED_STORE") || die 'FORGED_STORE missing' |
| 75 | [[ -d $store && -f $store/catalog.tsv && ! -L $store/catalog.tsv ]] || die 'FORGED_STORE has no catalog.tsv' |
| 76 | [[ $store != / && $store != "$HOME" && $store != "$src" ]] || die 'FORGED_STORE is too broad' |
| 77 | case "$store/" in "$data/"*|"$PUBLIC/"*) die 'FORGED_STORE is inside data or export';; esac |
| 78 | case "$data/" in "$store/"*) die 'FORGED_STORE holds the data directory';; esac |
| 79 | case "$bin" in "$store"/*) die 'refusing a forged binary inside the store';; esac |
| 80 | case "$store/" in "$src/"*) store_at=$dst/${store#"$src"/} ;; *) store_at=$store ;; esac |
| 81 | fi |
| 82 | |
| 83 | # Network. By default the server gets a network namespace of its own with nothing in it. It |
| 84 | # serves on a listener made outside by the seeded-Omega helper forged-listen: the service |
| 85 | # supervisor holds one for its whole life and passes it on descriptor 0 to every start; a direct |
| 86 | # start that sets FORGED_LISTEN_PORT has forged-listen make one and exec this wrapper again. bwrap |
| 87 | # keeps descriptors 0, 1 and 2 open. A probe started with neither runs with no network and no |
| 88 | # listener. |
| 89 | # The listener, and every connection forged accepts from it, belongs to the host network |
| 90 | # namespace, so the kernel answers interface questions asked through them about the host. The |
| 91 | # socket filter (forged-seccomp.om, built by run.sh into forged-seccomp.bpf beside the binary) |
| 92 | # refuses those calls inside the sandbox; bwrap installs it just before it executes forged. |
| 93 | fwd=("${inherit[@]}") |
| 94 | seccomp= |
| 95 | case ${FORGED_NETNS:-on} in |
| 96 | off) echo '!!! WARNING: FORGED_NETNS=off: SANDBOX SHARES THE HOST NETWORK; IT CAN CONNECT TO EVERY LOCAL SERVICE !!!' >&2 |
| 97 | net=() |
| 98 | netnote='shared network (FORGED_NETNS=off), no socket filter' |
| 99 | ((${#inherit[@]} == 0)) || netnote+=', serving the inherited listener on descriptor 0' ;; |
| 100 | on) net=(--unshare-net) |
| 101 | netnote='no network (private namespace), socket filter on' |
| 102 | if ((${#inherit[@]})); then |
| 103 | netnote+=', serving the inherited listener on descriptor 0' |
| 104 | elif [[ -n ${FORGED_LISTEN_PORT:-} ]]; then |
| 105 | port=$FORGED_LISTEN_PORT |
| 106 | [[ $port =~ ^[1-9][0-9]{0,4}$ ]] && ((port <= 65535)) || die 'FORGED_LISTEN_PORT must be 1..65535' |
| 107 | listen=$(realpath -e "${FORGED_LISTEN_HELPER:-$(dirname "$bin")/forged-listen}") || |
| 108 | die 'missing forged-listen beside the binary (./run.sh build makes it)' |
| 109 | [[ -f $listen && -x $listen ]] || die 'forged-listen is not an executable file' |
| 110 | case "$listen" in "$data"/*|"$PUBLIC"/*) die 'refusing forged-listen inside data or export';; esac |
| 111 | keep=(HOME="$HOME" PATH=/usr/bin:/bin FORGED_SANDBOX=on FORGED_NETNS=on |
| 112 | FORGED_READ_HOME="$src" FORGED_PATH_HOME="$dst" FORGE_DATA="$data" FORGED_PUBLIC_DIR="$PUBLIC" |
| 113 | FORGED_SALVAGE="${FORGED_SALVAGE:-off}") |
| 114 | keep+=(FORGED_PUBLIC_GIT="${FORGED_PUBLIC_GIT:-off}") |
| 115 | [[ -z ${FORGED_STORE:-} ]] || keep+=(FORGED_STORE="$FORGED_STORE") |
| 116 | [[ -z ${FORGED_PUBLIC_LIB:-} ]] || keep+=(FORGED_PUBLIC_LIB="$FORGED_PUBLIC_LIB") |
| 117 | [[ -z ${FORGED_SECCOMP:-} ]] || keep+=(FORGED_SECCOMP="$FORGED_SECCOMP") |
| 118 | [[ -z ${FORGED_COMMUNITY_PUB:-} ]] || keep+=(FORGED_COMMUNITY_PUB="$FORGED_COMMUNITY_PUB") |
| 119 | exec "$listen" "$port" /usr/bin/env "${keep[@]}" /usr/bin/bash "$(realpath -e "$0")" "$bin" "$@" |
| 120 | fi |
| 121 | # FORGED_SECCOMP: another location, for running a host tool (git, ssh-keygen) in the sandbox. |
| 122 | seccomp=$(realpath -e "${FORGED_SECCOMP:-$(dirname "$bin")/forged-seccomp.bpf}") || |
| 123 | die 'missing forged-seccomp.bpf beside the binary (./run.sh build makes it)' |
| 124 | case "$seccomp" in "$data"/*|"$PUBLIC"/*) die 'refusing a socket filter inside data or export';; esac |
| 125 | [[ -f $seccomp ]] || die 'forged-seccomp.bpf is not a file' |
| 126 | size=$(stat -c %s -- "$seccomp") |
| 127 | ((size >= 8 && size <= 4096 && size % 8 == 0)) || die 'forged-seccomp.bpf is not a whole filter program' |
| 128 | ;; |
| 129 | *) die 'FORGED_NETNS must be on or off' ;; |
| 130 | esac |
| 131 | |
| 132 | args=(--unshare-user --unshare-pid --unshare-ipc --unshare-uts "${net[@]}" |
| 133 | --disable-userns --assert-userns-disabled --cap-drop ALL |
| 134 | --new-session --die-with-parent --clearenv |
| 135 | --setenv FORGED_COUNTS_SNAPSHOT 1 |
| 136 | --setenv HOME /nonexistent --setenv PATH /usr/bin:/bin --setenv LC_ALL C |
| 137 | --setenv FORGE_SOCIAL_DIR "$dst/forge-data/" |
| 138 | --dir /app --ro-bind "$bin" /app/forged --chdir /app |
| 139 | --proc /proc --remount-ro /proc |
| 140 | --ro-bind /etc/localtime /etc/localtime |
| 141 | --dir /dev --dev-bind /dev/null /dev/null --dev-bind /dev/urandom /dev/urandom |
| 142 | --dir /nonexistent |
| 143 | --ro-bind "$PUBLIC" "$dst/handoffs/radicle" |
| 144 | --ro-bind "$PUBLIC" "$dst/handoffs/hive/manager/reviews" |
| 145 | --ro-bind "$PUBLIC" "$dst/handoffs/hive/forge/receipts" |
| 146 | --ro-bind "$PUBLIC" "$dst/.local/state/aien-factory" |
| 147 | --dir "$dst/.radicle/storage" |
| 148 | --bind "$data" "$dst/forge-data" |
| 149 | --ro-bind "$data/logs" "$dst/forge-data/logs" |
| 150 | --ro-bind "$data/forged-supervisor.pid" "$dst/forge-data/forged-supervisor.pid") |
| 151 | if [[ -n $store ]]; then |
| 152 | # Upload and bundle work trees use a separate, bounded filesystem. A restart discards them. |
| 153 | patch_work=$data/patch-work |
| 154 | [[ ! -L $patch_work ]] || die 'patch-work is a symlink' |
| 155 | mkdir -p -m 0700 "$patch_work" || die 'cannot prepare patch-work' |
| 156 | args+=(--size 268435456 --tmpfs "$dst/forge-data/patch-work" --chmod 0700 "$dst/forge-data/patch-work") |
| 157 | [[ -d $store/projects && ! -L $store/projects ]] || die 'FORGED_STORE has no projects folder, or it is a link' |
| 158 | args+=(--ro-bind "$store" "$store_at") |
| 159 | args+=(--tmpfs "$store_at/projects") |
| 160 | if [[ ${FORGED_PUBLIC_GIT:-off} = on ]]; then |
| 161 | [[ -d /var/www/interplane-git && ! -L /var/www/interplane-git ]] || die 'public Git mirror missing' |
| 162 | args+=(--dir /app/public-git) |
| 163 | fi |
| 164 | while IFS= read -r name; do |
| 165 | p=$store/projects/$name |
| 166 | [[ -d $p && ! -L $p ]] || continue |
| 167 | args+=(--ro-bind "$p" "$store_at/projects/$name") |
| 168 | if [[ ${FORGED_PUBLIC_GIT:-off} = on ]]; then |
| 169 | mirror=/var/www/interplane-git/$name.git |
| 170 | if [[ -d $mirror && ! -L $mirror ]]; then |
| 171 | args+=(--ro-bind "$mirror" "/app/public-git/$name.git") |
| 172 | fi |
| 173 | fi |
| 174 | for sub in staging repo.git.migration; do |
| 175 | if [[ -d $p/$sub && ! -L $p/$sub ]]; then |
| 176 | args+=(--tmpfs "$store_at/projects/$name/$sub" --remount-ro "$store_at/projects/$name/$sub") |
| 177 | fi |
| 178 | done |
| 179 | done < <(store_names "$store") |
| 180 | args+=(--remount-ro "$store_at/projects") |
| 181 | if [[ -d $store/import-notes && ! -L $store/import-notes ]]; then |
| 182 | args+=(--tmpfs "$store_at/import-notes" --remount-ro "$store_at/import-notes") |
| 183 | fi |
| 184 | for f in import.tsv IMPORT-RECEIPT.txt; do |
| 185 | if [[ -f $store/$f && ! -L $store/$f ]]; then args+=(--ro-bind /dev/null "$store_at/$f"); fi |
| 186 | done |
| 187 | fi |
| 188 | |
| 189 | # The salvage store (submissions). Off unless FORGED_SALVAGE=on. The store is <data>/salvage/: the |
| 190 | # server keeps drafts, records and the one job file there. Verification is not done in here: the |
| 191 | # collector service (salvage/collector.om) runs outside this sandbox and the server can neither |
| 192 | # start it nor reach it; they share only the store. |
| 193 | case ${FORGED_SALVAGE:-off} in |
| 194 | on) args+=(--setenv INTERPLANE_SALVAGE_DIR "$dst/forge-data/salvage/") |
| 195 | # The community runner hub (community/, interplane-forge docs/COMMUNITY.md): on with salvage |
| 196 | # when <data>/community/ (its writable record store) exists, so switching it on or off is that |
| 197 | # one directory. What the hub only reads, dist/ (the join script, the runner, the compiler, |
| 198 | # SHA256SUMS: what volunteers download) and deny (the operator's list), is a host folder |
| 199 | # outside the data bind (FORGED_COMMUNITY_PUB, default <data>/../forge-community), bound |
| 200 | # read-only at /community directly under the read-only root: no path the server can write or |
| 201 | # rename leads to it (a mount inside a writable directory moves when that directory is |
| 202 | # renamed). It must be a real directory holding a real dist/ and deny, else the sandbox stops. |
| 203 | if [[ -d $data/community ]]; then |
| 204 | cpub=${FORGED_COMMUNITY_PUB:-$data/../forge-community} |
| 205 | [[ -d $cpub && ! -L $cpub ]] || die "$cpub must be a directory" |
| 206 | [[ -d $cpub/dist && ! -L $cpub/dist ]] || die "$cpub/dist must be a directory" |
| 207 | [[ -f $cpub/deny && ! -L $cpub/deny ]] || die "$cpub/deny must be a file" |
| 208 | # not inside the writable data bind, where the server could write it, and not holding it |
| 209 | cpub=$(realpath -e "$cpub") || die "community pub folder unreadable" |
| 210 | rdata=$(realpath -e "$data") || die "data folder unreadable" |
| 211 | case "$cpub/" in "$rdata/"*) die "$cpub is inside the data folder" ;; esac |
| 212 | case "$rdata/" in "$cpub/"*) die "$cpub holds the data folder" ;; esac |
| 213 | args+=(--setenv INTERPLANE_COMMUNITY_DIR "$dst/forge-data/community/" |
| 214 | --setenv INTERPLANE_COMMUNITY_PUB /community/ |
| 215 | --ro-bind "$cpub" /community) |
| 216 | fi ;; |
| 217 | off) ;; |
| 218 | *) die 'FORGED_SALVAGE must be on or off' ;; |
| 219 | esac |
| 220 | |
| 221 | # Same selection as the supervisor digest. Only individually allowed bare repos (the Radicle |
| 222 | # build; a store-reading build reads the store bound above instead). |
| 223 | if [[ -z $store ]]; then |
| 224 | while IFS=$'\t' read -r rid repo identity; do |
| 225 | [[ -n $rid ]] || continue |
| 226 | args+=(--ro-bind "$repo" "$dst/.radicle/storage/$rid") |
| 227 | done < <(public_repos) |
| 228 | fi |
| 229 | |
| 230 | # Executables used by the server, plus prlimit, which sets the resource limits from inside the |
| 231 | # user namespace and then execs forged. The installed Git uses its built-in gzip. |
| 232 | # No shell or general /usr, |
| 233 | # /etc, host /proc, sockets, home, working trees, credentials or SSH agent mount. |
| 234 | programs=(/usr/bin/git /usr/bin/ssh-keygen /usr/bin/prlimit) |
| 235 | [[ -z $store ]] || programs+=(/usr/bin/stat) |
| 236 | for program in "${programs[@]}"; do |
| 237 | [[ -x $program ]] || die "missing $program" |
| 238 | args+=(--ro-bind "$program" "$program") |
| 239 | done |
| 240 | dependencies=$(ldd "${programs[@]}") || die 'ldd failed' |
| 241 | [[ $dependencies != *'not found'* ]] || die 'a runtime library is missing' |
| 242 | mapfile -t libs < <(printf '%s\n' "$dependencies" | awk ' |
| 243 | /=> \// {print $3} $1 ~ /^\// && $2 ~ /^\(/ {print $1}' | sort -u) |
| 244 | ((${#libs[@]})) || die 'could not resolve dynamic libraries' |
| 245 | gconv= |
| 246 | for lib in "${libs[@]}"; do |
| 247 | resolved=$(realpath -e "$lib") |
| 248 | args+=(--ro-bind "$resolved" "$lib") |
| 249 | [[ ${lib##*/} != libc.so.* ]] || gconv=${resolved%/*}/gconv |
| 250 | done |
| 251 | # The patch intake's git am converts a mail written in another charset to UTF-8 (git mailinfo, |
| 252 | # iconv), and glibc loads every such converter, ISO-8859-1 included, from its gconv folder beside |
| 253 | # libc (/usr/lib/aarch64-linux-gnu/gconv here). Without it git am stops with "cannot convert from |
| 254 | # ISO-8859-1 to UTF-8". Read-only system modules, mounted at the same path. |
| 255 | [[ -n $gconv && -d $gconv && ! -L $gconv ]] || die 'missing the glibc gconv folder beside libc' |
| 256 | args+=(--ro-bind "$gconv" "$gconv") |
| 257 | # ssh-keygen looks up its own uid. Supply one synthetic entry, no host account database. |
| 258 | exec {passwd_fd}<<<"forged:x:$(id -u):$(id -g):forged:/nonexistent:/bin/sh" |
| 259 | args+=(--ro-bind-data "$passwd_fd" /etc/passwd) |
| 260 | # Make synthetic ancestors unlistable. The named public files remain traversable. |
| 261 | args+=(--chmod 0111 "$dst" --chmod 0111 "$dst/handoffs" |
| 262 | --chmod 0111 "$dst/.radicle" --remount-ro /) |
| 263 | # Per-process ceilings, set inside the user namespace (RLIMIT_NPROC there counts only the |
| 264 | # sandbox's own tasks; set before bwrap it would count every process of the owner and refuse |
| 265 | # bwrap itself). Measured needs and margins are in SANDBOX-NEEDS.md "Resource limits". |
| 266 | limits=(--nproc=256 --as=4294967296 --fsize=1073741824 --nofile=1024 --core=0) |
| 267 | # The socket filter goes in last, so no helper started above (ldd, awk, the public reader) holds it. |
| 268 | if [[ -n ${seccomp:-} ]]; then |
| 269 | exec {seccomp_fd}<"$seccomp" |
| 270 | args+=(--seccomp "$seccomp_fd") |
| 271 | fi |
| 272 | # Say "on" only when the programs that make it true exist. |
| 273 | [[ -x /usr/bin/bwrap && -x /usr/bin/setpriv ]] || die 'missing /usr/bin/bwrap or /usr/bin/setpriv' |
| 274 | echo "forged-sandbox: on; read-only allow-list, private processes, no_new_privs, clean environment, limits nproc=256 as=4GiB fsize=1GiB nofile=1024 core=0, umask 0077; $netnote" >&2 |
| 275 | # env -i: bubblewrap stays pid 1 inside the sandbox and the child can read /proc/1/environ, so it |
| 276 | # must not carry the starter's environment. bwrap and setpriv read no environment variables. |
| 277 | exec env -i /usr/bin/setpriv --no-new-privs /usr/bin/bwrap "${args[@]}" -- \ |
| 278 | /usr/bin/prlimit "${limits[@]}" -- /app/forged "${fwd[@]}" "$@" |