#!/bin/sh # stack-audit.sh -- audit the "everything from source, no pkg" state of the server. # # stack-audit.sh report; exit 1 if there is any finding # stack-audit.sh --update re-record the checksum baseline (after a deliberate rebuild) # # Checks: # 1 pkg installed pkg packages not in etc/stack-pkg-allow.txt # 2 manifest every entry of etc/stack-manifest.txt: file exists, source repo has tag local/ # 2b lock source commit + file sha256 per manifest entry vs etc/stack-lock.txt (in git) # 2c graph every build step of build/build-all.sh has a manifest line, every dependency names a line, # no step is built before its dependencies (also: stack-audit.sh --check-manifest MANIFEST BUILD-ALL) # 3 linkage no ELF under /opt/wiiu resolves a library from /usr/local/lib # (run with LD_LIBRARY_PATH=/opt/wiiu/lib like the rc scripts do) # and no ELF has an unresolved library ("not found"): such a binary cannot start # 4 runtime no running process has /usr/local/{bin,sbin,lib} mapped or as its program # 6 leftover /usr/local/{lib,share,...} must be empty; bin/sbin only links into /opt/wiiu # 5 drift sha256 of /opt/wiiu/{bin,sbin,libexec} compared with the recorded baseline # Called daily by bin/server-watch.sh, which mails the findings. PATH=/opt/wiiu/bin:/opt/wiiu/sbin:/sbin:/bin:/usr/sbin:/usr/bin ETC=${STACK_ETC:-/usr/local/etc} STATE=${STACK_STATE:-/var/db/stack-audit} GITDIR=${GITDIR:-/srv/git/Server} mkdir -p "$STATE" 2>/dev/null out=$(mktemp /tmp/stack-audit.XXXXXX) || exit 2 trap 'rm -f "$out"' EXIT find_ () { echo "$1" >> "$out"; } # check_manifest MANIFEST BUILD-ALL.SH : the manifest defines the stack and its dependencies; print one line per violation. check_manifest () { _m=$1; _b=$2 [ -r "$_m" ] || { echo "manifest: $_m is not readable"; return; } [ -r "$_b" ] || { echo "manifest: $_b is not readable"; return; } # build order = core tools server ci jdk, as build-all.sh runs "all" _order=$(eval "$(grep -E '^(core|tools|server|ci|jdk)=' "$_b")"; echo "$core $tools $server $ci $jdk") grep -v '^#' "$_m" | grep -v '^[[:space:]]*$' | awk -F'|' -v order="$_order" ' BEGIN { n = split(order, st, " "); for (i = 1; i <= n; i++) pos[st[i]] = i } { if (NF != 4) { print "manifest: line " NR " has " NF " fields (name|file|repo|deps): " $0; next } name[$1] = 1; deps[$1] = $4; line[++c] = $1 } END { for (i = 1; i <= n; i++) if (!(st[i] in name)) print "manifest: build step " st[i] " has no line in stack-manifest.txt" for (i = 1; i <= c; i++) { a = line[i]; m = split(deps[a], d, ",") for (j = 1; j <= m; j++) { if (d[j] == "-" || d[j] == "") continue if (!(d[j] in name)) { print "manifest: " a " depends on " d[j] ", which has no line"; continue } if ((a in pos) && (d[j] in pos) && pos[d[j]] >= pos[a]) print "manifest: " a " is built before its dependency " d[j] " (fix the order in build/build-all.sh)" } } }' } if [ "$1" = "--check-manifest" ]; then r=$(check_manifest "$2" "$3"); if [ -n "$r" ]; then echo "$r"; exit 1; fi echo "manifest: OK (every build step listed, dependencies known and built first)"; exit 0 fi # Scripts and rc files that come from the config repo (bin/, libexec/) legitimately change on every # deploy; the drift baseline covers only what was built from source. sums() { git --git-dir="$GITDIR/dangofactory-server.git" ls-tree -r --name-only master bin libexec 2>/dev/null | sed 's|^[a-z]*/|/opt/wiiu/&|; s|^/opt/wiiu/\(bin\|libexec\)/\(bin\|libexec\)/|/opt/wiiu//|' > "$STATE/managed.list" find /opt/wiiu/bin /opt/wiiu/sbin /opt/wiiu/libexec -type f 2>/dev/null | sort | grep -vxFf "$STATE/managed.list" | xargs sha256 -r 2>/dev/null } # --lock prints the lock file: name, source commit (tag local/), sha256 of the installed file. # Commit the output as etc/stack-lock.txt so git records exactly what was built. if [ "$1" = "--lock" ]; then echo "# name commit sha256 (made by bin/stack-audit.sh --lock)" grep -v '^#' "$ETC/stack-manifest.txt" | while IFS='|' read -r name file repo deps; do [ -n "$name" ] || continue c=-; [ "$repo" != "-" ] && c=$(git --git-dir="$GITDIR/$repo.git" rev-parse -q --verify "refs/tags/local/$repo^{commit}" 2>/dev/null) h=-; [ "$file" != "-" ] && h=$(sha256 -q "$file" 2>/dev/null) echo "$name ${c:--} ${h:--}" done; exit 0 fi if [ "$1" = "--update" ]; then sums > "$STATE/sha256.base" && echo "baseline recorded: $(wc -l < "$STATE/sha256.base") files"; exit 0 fi # 1 pkg allow=$(grep -v '^#' "$ETC/stack-pkg-allow.txt" 2>/dev/null) pkgs=""; [ -x /usr/local/sbin/pkg ] && pkgs=$(pkg query '%n' 2>/dev/null) # pkg itself may be gone for p in $pkgs; do echo "$allow" | grep -qx "$p" || echo "$p" done > "$STATE/pkg-extra.txt" n=$(wc -l < "$STATE/pkg-extra.txt" | tr -d ' ') [ "$n" -gt 0 ] && find_ "pkg: $n package(s) not in the allow list (see $STATE/pkg-extra.txt)" # 2 manifest grep -v '^#' "$ETC/stack-manifest.txt" 2>/dev/null | while IFS='|' read -r name file repo deps; do [ -n "$name" ] || continue [ "$file" = "-" ] || [ -x "$file" ] || [ -f "$file" ] || echo "manifest: $name: $file is missing" if [ "$repo" != "-" ]; then git --git-dir="$GITDIR/$repo.git" rev-parse -q --verify "refs/tags/local/$repo" >/dev/null 2>&1 \ || echo "manifest: $name: source repo $repo has no tag local/$repo" fi done >> "$out" # 2c graph: the deployed manifest against the build order in the repository's build-all.sh git --git-dir="$GITDIR/dangofactory-server.git" show master:build/build-all.sh > "$STATE/build-all.sh" 2>/dev/null && check_manifest "$ETC/stack-manifest.txt" "$STATE/build-all.sh" >> "$out" # 2b lock (etc/stack-lock.txt, tracked in git): source commit and installed file must match if [ -f "$ETC/stack-lock.txt" ]; then "$0" --lock 2>/dev/null | grep -v '^#' | sort > "$STATE/lock.now" grep -v '^#' "$ETC/stack-lock.txt" | sort | diff - "$STATE/lock.now" | grep '^>' | awk '{print $2}' | while read -r n; do echo "lock: $n differs from etc/stack-lock.txt (rebuilt or tag moved?)"; done >> "$out" fi # 3 linkage (ELF executables and shared objects built into /opt/wiiu) LD_LIBRARY_PATH=/opt/wiiu/lib export LD_LIBRARY_PATH find /opt/wiiu -type f \( -perm -u+x -o -name '*.so*' \) 2>/dev/null | while read -r f; do l=$(ldd "$f" 2>&1) echo "$l" | grep -q '/usr/local/lib' && echo "linkage: $f uses /usr/local/lib" # a library that cannot be found = a binary that does not start (e.g. built against a removed pkg) case "$f" in /opt/wiiu/bin/*|/opt/wiiu/sbin/*|/opt/wiiu/libexec/*) ;; *) continue ;; esac # only programs: libs of jdk/capnp resolve via their own dirs echo "$l" | grep -q 'not found' && echo "linkage: $f cannot start: $(echo "$l" | grep -m1 'not found' | sed 's/^[[:space:]]*//' | cut -c1-90)" done >> "$out" unset LD_LIBRARY_PATH # 4 runtime for pid in $(ps -axo pid= 2>/dev/null); do procstat -v "$pid" 2>/dev/null | grep -q ' /usr/local/\(lib\|bin\|sbin\)/' \ && echo "runtime: pid $pid ($(ps -o comm= -p "$pid")) maps /usr/local" done >> "$out" # 6 leftovers: /usr/local holds configuration only (etc/); bin and sbin may only link into /opt/wiiu for d in lib libexec libdata share include; do c=$(find /usr/local/$d -type f 2>/dev/null | head -1) [ -n "$c" ] && echo "leftover: /usr/local/$d still has files (e.g. $c)" >> "$out" done find /usr/local/bin /usr/local/sbin -type f 2>/dev/null | head -5 | while read -r f; do echo "leftover: $f is a real file, not a link into /opt/wiiu"; done >> "$out" # 5 drift if [ -f "$STATE/sha256.base" ]; then sums > "$STATE/sha256.now" diff "$STATE/sha256.base" "$STATE/sha256.now" | grep '^[<>]' | awk '{print $3}' | sort -u | head -20 \ | while read -r f; do echo "drift: $f changed since the baseline"; done >> "$out" else sums > "$STATE/sha256.base"; echo "(baseline created: $(wc -l < "$STATE/sha256.base") files)" fi if [ -s "$out" ]; then cat "$out"; exit 1; fi echo "stack-audit: OK (no pkg beyond allow list, no /usr/local linkage, manifest complete, no drift)" exit 0