Files
glacier-ng/tests/audit_alpine.sh.old2
2026-10-29 09:15:34 -04:00

147 lines
6.7 KiB
Bash

#!/bin/sh
# Fixes from the 2026-10-06 security audit:
# - a package may not ship a top-level files/.glacier-* entry: as a
# symlink, .glacier-fresh made syspkg truncate any file as root;
# .glacier-overwrite granted the package -O; .glacier-manifest
# rewrote its saved manifest;
# - a manifest with @include is refused (libconfig would read any file
# as root), and so is a dependency or conflict entry without a name;
# - what syspkg shows and compares is what the LIBRARY reads from the
# manifest: a decoy comment can't hide that a package replaces one;
# - no CLI argument reaches a format string (gstore's argv[0], gpkg
# -u's operand); gstore refuses a UID that would wrap in C;
# - root deleting a user's tree (gstore -x) never follows a symlink
# swapped in mid-walk (race test, many iterations).
# Runs INSIDE the Dockerfile.alpine_root image:
#
# docker run --rm -v "$PWD/tests/audit_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
P=/srv/pkgs
mkdir -p $P && chmod 755 /srv $P
# raw NAME VER REPO [MANIFEST_EXTRA] -> $P/NAME-VER.gpkg (+ sidecar). The
# package tree is left at $W/NAME-VER for the caller to add to first:
# call raw_dir, edit $D, then raw_pack.
raw_dir() {
W=$(mktemp -d); D=$W/$1-$2
mkdir -p $D/files/usr/bin
printf '%sPKG_NAME = "%s";\nPKG_VER = "%s";\nPKG_REPO = "%s";\nPKG_DEPS = ();\nPKG_CONFS = ();\n' \
"${4:-}" "$1" "$2" "$3" > $D/manifest.gpm.cfg
printf '#!/bin/sh\necho %s %s\n' "$1" "$2" > $D/files/usr/bin/$1
chmod 755 $D/files/usr/bin/$1
RAW=$P/$1-$2.gpkg
}
raw_pack() {
tar -C $W -czf $RAW "$(basename $D)"
echo "sha256:$(sha256sum $RAW | cut -c1-64)" > $RAW.sha256
rm -rf $W
}
inst() { R sh -c "echo y | syspkg -l $*"; }
syspkg -n >/dev/null
echo "important root data" > /root/victim && chmod 600 /root/victim
hr "reserved .glacier-* names"
raw_dir fresh 1.0.0 extra; ln -s /root/victim $D/files/.glacier-fresh; raw_pack
inst $RAW
check "a .glacier-fresh symlink is refused" has "[x] Refusing archive entry 'fresh-1.0.0/files/.glacier-fresh': .glacier-* names are reserved."
check "...exit 1" rc 1
check "...the file it pointed at is untouched" [ "$(cat /root/victim)" = "important root data" ]
check "...nothing installed" sh -c '! grep -q "::fresh::" /glacier/sys/index/current'
check "...no half-made version dir" [ ! -e /glacier/sys/store/extra/fresh/fresh-1.0.0 ]
raw_dir selfo 1.0.0 extra; : > $D/files/.glacier-overwrite; raw_pack
inst $RAW
check "a .glacier-overwrite (self-granted -O) is refused" has "files/.glacier-overwrite': .glacier-* names are reserved."
raw_dir man 1.0.0 extra; printf 'PKG_NAME = "x";\n' > $D/files/.glacier-manifest; raw_pack
inst $RAW
check "a .glacier-manifest is refused" has "files/.glacier-manifest': .glacier-* names are reserved."
raw_dir keep 1.0.0 extra; mkdir -p $D/files/usr/lib; : > $D/files/usr/lib/.keep; : > $D/files/.hidden; raw_pack
inst $RAW
check "other dotfiles still install" installed extra keep
adduser -D tester >/dev/null 2>&1
U=$(id -u tester)
gstore -n "$U" >/dev/null
echo "tester data" > /srv/tester-file && chown tester /srv/tester-file
raw_dir ufresh 1.0.0 extra; ln -s /srv/tester-file $D/files/.glacier-fresh; raw_pack
R su tester -c "echo y | gpkg -l $RAW"
check "gpkg refuses one too" has "files/.glacier-fresh': .glacier-* names are reserved."
check "...leaving the user's file alone" [ "$(cat /srv/tester-file)" = "tester data" ]
hr "manifests"
raw_dir incl 1.0.0 extra '@include "/etc/glacier/glacier.conf"
'; raw_pack
inst $RAW
check "@include in a manifest is refused" has "[x] Cannot read the manifest of $RAW."
check "...exit 1, before the prompt" sh -c "[ $RC = 1 ] && ! echo \"\$0\" | grep -q Proceed" "$OUT"
check "...nothing installed" sh -c '! grep -q "::incl::" /glacier/sys/index/current'
raw_dir incl2 1.0.0 extra ' @include "/etc/glacier/glacier.conf"
'; raw_pack
inst $RAW
check "...indented too" has "[x] Cannot read the manifest of $RAW."
raw_dir mail 1.0.0 extra 'PKG_MAINT = "someone@example.org";
'; raw_pack
inst $RAW
check "an @ elsewhere (an e-mail address) is fine" installed extra mail
raw_dir noname 1.0.0 extra; sed -i 's/PKG_DEPS = ();/PKG_DEPS = ( { PKG_VER = "1.0.0"; } );/' $D/manifest.gpm.cfg; raw_pack
inst $RAW
check "a dependency without a name is refused" has "[x] Cannot read the manifest of $RAW."
raw_dir badconf 1.0.0 extra; sed -i 's#PKG_CONFS = ();#PKG_CONFS = ( { PKG_NAME = "../x"; PKG_VER = "1.0.0"; } );#' $D/manifest.gpm.cfg; raw_pack
inst $RAW
check "a conflict with an unsafe name is refused" has "[x] Cannot read the manifest of $RAW."
raw_dir dup 1.0.0 extra; raw_pack
inst $RAW
raw_dir dup 2.0.0 extra '// PKG_REPO = "community";
'; raw_pack
inst $RAW
check "a decoy PKG_REPO comment can't hide a replacement" has "[x] extra::dup 1.0.0 is already installed."
check "...one version stays in the store" [ "$(ls /glacier/sys/store/extra/dup)" = "dup-1.0.0" ]
hr "format strings and UIDs"
ln -s /app/gstore '/srv/gs%s%s%s%s%n'
R '/srv/gs%s%s%s%s%n' -b "$U" /srv/b.tgz
check "gstore -b run as a name full of %s%n works" rc 0
check "...and prints it nowhere as a format" has "[✓] Successfully created backup for UID $U."
# gpkg -f/-u operands as formats: tests/fetch_alpine.sh (they need a repo).
R gstore -n 4294968296
check "a UID above uid_t's range is refused" has "[x] Invalid UID: 4294968296"
R gstore -x -- -1
R gstore -x -1000
check "a negative UID is refused" sh -c "[ $RC = 1 ]"
check "...and the tester's directory survives" [ -d /glacier/usr/$U ]
hr "races: root never follows a symlink swapped into a user's tree"
mkdir -p /root/vdir && chmod 700 /root/vdir
lost=0
i=0
while [ $i -lt 40 ]; do
j=0; while [ $j -lt 50 ]; do : > /root/vdir/f$j; j=$((j+1)); done
[ -d /glacier/usr/$U ] || gstore -n "$U" >/dev/null
B=/glacier/usr/$U
su tester -c "mkdir -p $B/store/d/a/b/c; j=0; while [ \$j -lt 200 ]; do : > $B/store/d/x\$j; j=\$((j+1)); done"
su tester -c "while :; do mv $B/store/d $B/store/.d 2>/dev/null; ln -s /root/vdir $B/store/d 2>/dev/null; rm -f $B/store/d; mv $B/store/.d $B/store/d 2>/dev/null; done" >/dev/null 2>&1 &
SWAP=$!
gstore -x "$U" >/dev/null 2>&1
kill $SWAP 2>/dev/null; wait $SWAP 2>/dev/null
[ "$(ls /root/vdir | wc -l)" = 50 ] || lost=$((lost+1))
i=$((i+1))
done
check "gstore -x deleted nothing through a swapped symlink (40 runs, $lost with losses)" [ $lost = 0 ]
echo
echo "FAILURES: $FAILS"
[ $FAILS -eq 0 ]