Files
2026-10-29 09:15:34 -04:00

71 lines
4.1 KiB
Bash

#!/bin/sh
# Install-time integrity verification (SECURITY_DESIGN.txt section 4):
# missing sidecar == mismatch == refused, for syspkg and gpkg alike;
# packages are verified and installed from a private copy, which is
# always cleaned up. Runs INSIDE the Dockerfile.alpine_root image:
#
# ./run_docker.sh Dockerfile.alpine_root # builds glacier-alpine_root
# docker run --rm -v "$PWD/tests/verify_alpine.sh:/t.sh:ro" glacier-alpine_root sh /t.sh
FAILS=0
check() { d=$1; shift; if "$@"; then echo "PASS: $d"; else echo "FAIL: $d"; FAILS=$((FAILS+1)); fi; }
nocolor() { sed 's/\x1b\[[0-9;]*m//g'; }
sidecar() { printf 'sha256:%s\n' "$(sha256sum "$1" | cut -d' ' -f1)" > "$1.sha256"; gsign -S -s /root/glacier-test.sec -m "$1.sha256" >/dev/null; }
no_leftovers() { [ -z "$(ls -d /tmp/gpkg-verify.* 2>/dev/null)" ]; }
W=/srv/pkgs; mkdir -p $W; cp /app/pkgs/*.gpkg /app/kilo-0.0.1.gpkg $W/ 2>/dev/null; chmod 755 /srv $W; chmod 644 $W/*
cd /app && ./syspkg -n >/dev/null
echo "=== 1. no sidecar: refused before the prompt"
out=$(echo y | ./syspkg -l $W/kilo-0.0.1.gpkg 2>&1 | nocolor); echo "$out"
check "refused, says no sidecar" sh -c "echo '$out' | grep -q 'Refusing $W/kilo-0.0.1.gpkg: no sidecar'"
check "never reached the prompt" sh -c "! echo '$out' | grep -q Proceed"
check "nothing installed" test ! -e /glacier/sys/store/extra/kilo
check "no private copies left" no_leftovers
echo "=== 2. valid sidecar: installs"
sidecar $W/kilo-0.0.1.gpkg
echo y | ./syspkg -l $W/kilo-0.0.1.gpkg 2>&1 | nocolor | tail -2
check "installed" test -d /glacier/sys/store/extra/kilo
check "message names the ORIGINAL path" sh -c "echo y | ./syspkg -l $W/kilo-0.0.1.gpkg 2>&1 | grep -q 'staged from $W/kilo-0.0.1.gpkg'"
check "no private copies left" no_leftovers
echo "=== 3. tampered package, stale sidecar: refused"
cp $W/musl-1.2.5.gpkg $W/musl-t.gpkg; sidecar $W/musl-t.gpkg; printf 'x' >> $W/musl-t.gpkg
out=$(echo y | ./syspkg -l $W/musl-t.gpkg 2>&1 | nocolor); echo "$out"
check "refused as hash mismatch" sh -c "echo '$out' | grep -q 'hash mismatch'"
check "musl not installed" test ! -e /glacier/sys/store/base/musl
echo "=== 4. malformed sidecar: refused"
cp $W/sbase-0.0.1.gpkg $W/sbase-m.gpkg; sha256sum $W/sbase-m.gpkg > $W/sbase-m.gpkg.sha256 # coreutils format, not sha256:<hex>
out=$(echo y | ./syspkg -l $W/sbase-m.gpkg 2>&1 | nocolor); echo "$out"
check "refused as malformed" sh -c "echo '$out' | grep -q 'malformed sidecar'"
echo "=== 5. one bad package refuses the whole batch"
sidecar $W/sbase-0.0.1.gpkg
out=$(echo y | ./syspkg -l $W/sbase-0.0.1.gpkg $W/musl-1.2.5.gpkg 2>&1 | nocolor); echo "$out"
check "batch refused (musl has no sidecar)" sh -c "echo '$out' | grep -q 'Refusing $W/musl-1.2.5.gpkg'"
check "sbase NOT installed either" test ! -e /glacier/sys/store/core/sbase
echo "=== 6. declining the prompt still cleans up"
echo n | ./syspkg -l $W/sbase-0.0.1.gpkg >/dev/null 2>&1
check "no private copies left after 'n'" no_leftovers
echo "=== 7. swapping the original after verification changes nothing"
# Install reads only the private copy: corrupt the original while syspkg
# waits at the prompt, then answer y. The verified bytes get installed.
sidecar $W/musl-1.2.5.gpkg
( sleep 1; printf 'garbage' > $W/musl-1.2.5.gpkg; sleep 1; echo y ) | ./syspkg -l $W/musl-1.2.5.gpkg 2>&1 | nocolor | tail -1
check "musl installed from the verified copy" test -e /glacier/sys/store/base/musl/musl-1.2.5/usr/lib/libc.so
echo "=== 8. gpkg (user scope) enforces the same"
cp /app/kilo-0.0.1.gpkg /srv/userkilo.gpkg; chmod 644 /srv/userkilo.gpkg
su glacier -c "doas /app/gstore -n" >/dev/null 2>&1
out=$(su glacier -c "cd /app && echo y | ./gpkg -l /srv/userkilo.gpkg" 2>&1 | nocolor); echo "$out"
check "gpkg refuses without sidecar" sh -c "echo '$out' | grep -q 'no sidecar'"
sidecar /srv/userkilo.gpkg
su glacier -c "cd /app && echo y | ./gpkg -l /srv/userkilo.gpkg" 2>&1 | nocolor | tail -1
check "gpkg installs with sidecar" test -e /glacier/usr/1000/store/extra/kilo -o -n "$(su glacier -c 'ls /glacier/usr/1000/store/extra 2>/dev/null')"
check "no private copies left" no_leftovers
echo; echo "FAILURES: $FAILS"; [ $FAILS -eq 0 ]