#!/bin/sh # The local base rule (REPO_DESIGN.txt sections 3 and 7): # - syspkg -l of a package whose manifest says base needs # FILE.sha256.sig, a signature of that sidecar by a key that # /etc/glacier/repos.conf lists for base; extra stays sidecar-only; # - the repo is the one the LIBRARY reads (a manifest comment can't # make it look like extra); # - a key trusted for another repo, an unlisted key, or a signature of # other bytes is refused; the key is picked by keynum; # - repos.conf is parsed strictly (unknown settings, http://, bad and # duplicate names); it and the keys must be root-owned and not # group/world-writable, ancestors included; a listed key that's # missing stops the run; no repos.conf trusts nothing; # - glacier-bootstrap carries the .sig to syspkg -R; gbuild -k signs. # Runs INSIDE the Dockerfile.alpine_root image (its test key, # glacier-test, is listed for base): # # docker run --rm -v "$PWD/tests/keyring_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'; } R() { OUT=$( ("$@"; echo "rc=$?") 2>&1 | nocolor) RC=$(echo "$OUT" | sed -n 's/^rc=//p' | tail -1) echo "$OUT" | sed '/^rc=/d; s/^/ /' } has() { echo "$OUT" | grep -qF -- "$1"; } lacks() { ! has "$1"; } rc() { [ "$RC" = "$1" ]; } installed() { grep -q "^$1::$2::" /glacier/sys/index/current; } hr() { echo; echo "== $*"; } export PATH=/app:$PATH K=/etc/glacier/keys CONF=/etc/glacier/repos.conf TEST_SEC=/root/glacier-test.sec P=/srv/pkgs mkdir -p $P && chmod 755 /srv $P # Keys: `second` is also listed for base; `extrakey` is in the keyring # but listed only for extra; `stranger` is in no keyring at all. gsign -G -p $K/second.pub -s /root/second.sec -c second >/dev/null gsign -G -p $K/extrakey.pub -s /root/extrakey.sec -c extrakey >/dev/null gsign -G -p /root/stranger.pub -s /root/stranger.sec -c stranger >/dev/null GOOD_CONF='repos = ( { name = "base"; url = "https://pkg.example.org/base"; keys = [ "glacier-test", "second" ]; }, { name = "extra"; url = "file:///srv/extra"; keys = [ "extrakey" ]; } );' good_conf() { echo "$GOOD_CONF" > $CONF; chmod 644 $CONF; } good_conf # pk NAME REPO [KEY.sec]: $P/NAME-1.0.0.gpkg + sidecar, signed if a key is given pk() { rm -f $P/$1-1.0.0.gpkg*; /app/tests/mkpkg -o $P ${3:+-k "$3"} "$1" 1.0.0 "$2" >/dev/null; } inst() { R sh -c "echo y | syspkg -l $*"; } syspkg -n >/dev/null hr "signed and trusted" pk okbase base $TEST_SEC inst $P/okbase-1.0.0.gpkg check "a base package signed by a key listed for base installs" installed base okbase check "...cleanly" has "Completed with no errors." pk okbase2 base /root/second.sec inst $P/okbase2-1.0.0.gpkg check "the second key listed for base works too (picked by keynum)" installed base okbase2 pk plainextra extra inst $P/plainextra-1.0.0.gpkg check "an unsigned extra package installs (sidecar only)" installed extra plainextra hr "refused" pk nosig base inst $P/nosig-1.0.0.gpkg check "an unsigned base package is refused" has "[x] base::nosig is not signed by a key trusted for base." check "...exit 1" rc 1 check "...before the prompt" lacks "Proceed" check "...installing nothing" sh -c '! grep -q "::nosig::" /glacier/sys/index/current' pk strangers base /root/stranger.sec inst $P/strangers-1.0.0.gpkg check "a key in no keyring is refused" has "[x] base::strangers is not signed by a key trusted for base." pk extras base /root/extrakey.sec inst $P/extras-1.0.0.gpkg check "a key trusted only for extra is refused for base" has "[x] base::extras is not signed by a key trusted for base." pk other base $TEST_SEC; pk swapped base cp $P/other-1.0.0.gpkg.sha256.sig $P/swapped-1.0.0.gpkg.sha256.sig inst $P/swapped-1.0.0.gpkg check "a trusted signature of ANOTHER sidecar is refused" has "[x] base::swapped is not signed by a key trusted for base." echo garbage > $P/nosig-1.0.0.gpkg.sha256.sig inst $P/nosig-1.0.0.gpkg check "a malformed .sig is refused the same way" has "[x] base::nosig is not signed by a key trusted for base." inst $P/okbase-1.0.0.gpkg $P/nosig-1.0.0.gpkg $P/strangers-1.0.0.gpkg both() { has "base::nosig is not" && has "base::strangers is not"; } check "one unsigned package refuses the batch, each reported" both check "...and nothing new is staged" lacks "staged from" # A comment the old pattern-matching manifest reader would have taken for # the repo: libconfig (and so gl_install_pkg) says base. w=$(mktemp -d); mkdir -p $w/sneaky-1.0.0/files/usr/bin echo x > $w/sneaky-1.0.0/files/usr/bin/sneaky printf '# PKG_REPO = "extra";\nPKG_NAME = "sneaky";\nPKG_VER = "1.0.0";\nPKG_DESC = "t";\nPKG_MAINT = "t@e";\nPKG_COPYRIGHT = "MIT";\nPKG_REPO = "base";\nPKG_DEPS = ();\nPKG_CONFS = ();\n' > $w/sneaky-1.0.0/manifest.gpm.cfg tar czf $P/sneaky-1.0.0.gpkg -C $w sneaky-1.0.0; rm -rf $w printf 'sha256:%s\n' "$(sha256sum $P/sneaky-1.0.0.gpkg | cut -d' ' -f1)" > $P/sneaky-1.0.0.gpkg.sha256 inst $P/sneaky-1.0.0.gpkg check "a manifest comment can't pass base off as extra" has "[x] base::sneaky is not signed by a key trusted for base." check "...nothing linked into /usr" test ! -e /usr/bin/sneaky hr "repos.conf" pk later base $TEST_SEC rm $CONF inst $P/later-1.0.0.gpkg check "no repos.conf trusts no key" has "[x] base::later is not signed by a key trusted for base." bad_conf() { # bad_conf DESCRIPTION CONTENT printf '%s\n' "$2" > $CONF; chmod 644 $CONF inst $P/later-1.0.0.gpkg check "$1 is refused" has "[x] Malformed /etc/glacier/repos.conf." check "...installing nothing" sh -c '! grep -q "::later::" /glacier/sys/index/current' } bad_conf "not libconfig" 'repos = ( {' bad_conf "an http:// url" 'repos = ( { name = "base"; url = "http://x/base"; keys = [ "glacier-test" ]; } );' bad_conf "an empty https:// url" 'repos = ( { name = "base"; url = "https://"; keys = [ "glacier-test" ]; } );' bad_conf "an unknown entry setting" 'repos = ( { name = "base"; keys = [ "glacier-test" ]; trust = "all"; } );' bad_conf "an unknown top-level setting" 'repos = ( { name = "base"; keys = [ "glacier-test" ]; } ); verify = false;' bad_conf "a duplicate repo" 'repos = ( { name = "base"; keys = [ "glacier-test" ]; }, { name = "base"; keys = [ ]; } );' bad_conf "a key name with a path" 'repos = ( { name = "base"; keys = [ "../../root/stranger" ]; } );' bad_conf "a repo name with a path" 'repos = ( { name = "base"; keys = [ "glacier-test" ]; }, { name = "../x"; keys = [ ]; } );' bad_conf "a missing keys list" 'repos = ( { name = "base"; } );' bad_conf "keys as a string" 'repos = ( { name = "base"; keys = "glacier-test"; } );' bad_conf "a malformed OTHER entry" 'repos = ( { name = "base"; keys = [ "glacier-test" ]; }, { name = "extra"; url = "ftp://x"; keys = [ ]; } );' printf 'repos = ( { name = "base"; keys = [ "glacier-test" ]; } );\n' > $CONF inst $P/later-1.0.0.gpkg check "an entry without url works" installed base later printf 'repos = ( { name = "base"; keys = [ "ghost" ]; } );\n' > $CONF pk later2 base $TEST_SEC inst $P/later2-1.0.0.gpkg check "a listed key that doesn't exist stops the run" has "[x] Cannot read /etc/glacier/keys/ghost.pub." echo "not a key" > $K/ghost.pub inst $P/later2-1.0.0.gpkg check "a listed key that isn't a key stops the run" has "[x] Malformed /etc/glacier/keys/ghost.pub." rm $K/ghost.pub printf 'repos = ( { name = "extra"; keys = [ "glacier-test" ]; } );\n' > $CONF inst $P/later2-1.0.0.gpkg check "a repos.conf without base trusts nothing for base" has "[x] base::later2 is not signed by a key trusted for base." hr "ownership" good_conf untrusted() { # untrusted DESCRIPTION PATH-AT-FAULT REFUSED-FILE inst $P/later2-1.0.0.gpkg check "$1" has "[x] Refusing $3: $2 is not root-owned, or is group/world-writable." check "...installing nothing" sh -c '! grep -q "::later2::" /glacier/sys/index/current' } chmod 666 $CONF untrusted "a world-writable repos.conf" $CONF $CONF chmod 644 $CONF; chown glacier $CONF untrusted "a repos.conf owned by a user" $CONF $CONF chown root $CONF; chmod 777 /etc/glacier untrusted "a world-writable /etc/glacier" /etc/glacier $CONF chmod 755 /etc/glacier; chmod 775 $K untrusted "a group-writable keys directory" $K $K/glacier-test.pub chmod 755 $K; chown glacier $K/glacier-test.pub untrusted "a key owned by a user" $K/glacier-test.pub $K/glacier-test.pub chown root $K/glacier-test.pub # A key that's a root-owned symlink into a user's directory: the REAL # path's ancestors are checked too. mkdir -p /home/glacier/k && cp $K/second.pub /home/glacier/k/ && chown glacier /home/glacier/k mv $K/second.pub /root/second.pub.real; ln -s /home/glacier/k/second.pub $K/second.pub untrusted "a key linked into a user's directory" /home/glacier/k $K/second.pub rm $K/second.pub; mv /root/second.pub.real $K/second.pub inst $P/later2-1.0.0.gpkg check "restored, it installs" installed base later2 hr "glacier-bootstrap carries the signature" mkdir -p /srv/img && chmod 755 /srv/img pk bsbase base $TEST_SEC; pk bsextra extra printf 'NAME=t\nPKG=bsbase 1.0.0\nPKG=bsextra 1.0.0\n' > /srv/t.template R sh -c "echo y | glacier-bootstrap -d $P /srv/t.template /srv/img" check "a signed base package bootstraps" rc 0 check "...into the image" grep -q "^base::bsbase::" /srv/img/glacier/sys/index/current # The signature covers the sidecar's exact bytes: one without a final # newline must reach syspkg as it is, not rewritten. mkdir -p /srv/img3 && chmod 755 /srv/img3 pk bsnonl base printf 'sha256:%s' "$(sha256sum $P/bsnonl-1.0.0.gpkg | cut -d' ' -f1)" > $P/bsnonl-1.0.0.gpkg.sha256 gsign -S -s $TEST_SEC -m $P/bsnonl-1.0.0.gpkg.sha256 >/dev/null printf 'NAME=t\nPKG=bsnonl 1.0.0\n' > /srv/t3.template R sh -c "echo y | glacier-bootstrap -d $P /srv/t3.template /srv/img3" check "a signed sidecar without a final newline bootstraps" grep -q "^base::bsnonl::" /srv/img3/glacier/sys/index/current mkdir -p /srv/img2 && chmod 755 /srv/img2 pk bsbase2 base printf 'NAME=t\nPKG=bsbase2 1.0.0\n' > /srv/t2.template R sh -c "echo y | glacier-bootstrap -d $P /srv/t2.template /srv/img2" check "an unsigned one is refused by syspkg -R (exit 5)" rc 5 check "...saying why" has "[x] base::bsbase2 is not signed by a key trusted for base." check "...installing nothing" sh -c '! grep -q "bsbase2" /srv/img2/glacier/sys/index/current 2>/dev/null' hr "gbuild -k" apk add --no-cache bash >/dev/null 2>&1 || { echo "apk failed"; exit 1; } # Not /app on PATH: glacier-ng's checkout holds a host (glibc) gsha256sum. gb() { env PATH=/usr/local/bin:/usr/bin:/bin /app/gbuild "$@"; } mkdir -p /srv/gb/hello printf 'all:\n\ttrue\ninstall:\n\tmkdir -p $(DESTDIR)/usr/bin && echo hi > $(DESTDIR)/usr/bin/hello\n' > /srv/gb/hello/Makefile R gb -n hello -V 1.0.0 -d t -m t@e -g base -o /srv/gbout -k $TEST_SEC /srv/gb/hello check "gbuild -k succeeds" rc 0 check "...and its signature verifies the sidecar" gsign -V -p $K/glacier-test.pub -m /srv/gbout/hello-1.0.0.gpkg.sha256 inst /srv/gbout/hello-1.0.0.gpkg check "...which syspkg accepts for base" installed base hello R gb -n hello -V 1.0.1 -d t -m t@e -g base -o /srv/gbout -k /root/missing.sec /srv/gb/hello check "gbuild -k with an unreadable key fails" rc 1 check "...before building anything" sh -c '! ls /srv/gbout | grep -q 1.0.1' R gb -n hello -V 1.0.0 -d t -m t@e -g base -o /srv/gbout /srv/gb/hello check "rebuilding without -k leaves no stale signature" test ! -e /srv/gbout/hello-1.0.0.gpkg.sha256.sig echo echo "FAILURES: $FAILS" [ $FAILS -eq 0 ]