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

160 lines
8.0 KiB
Bash

#!/bin/sh
# The build sandbox, gsandbox (BUILD_DESIGN.txt section 6; step 2):
# - -X extracts a package's files/ into a new layer, with the install
# rules (a hostile package is refused and leaves nothing);
# - -R runs /.glacier/in/driver as pid 1 of new namespaces, as uid 0
# mapped to the caller: the layers (overlay, read-only below), no
# network, no host files, /.glacier/in read-only, /.glacier/out
# writable, no new privileges; its exit status comes back;
# - as root, -u is required and must not be uid 0; it drops to that
# user first; a user can't pass -u;
# - unsafe paths and layer names are refused.
# Needs user namespaces, overlayfs and /proc mounts inside Docker, and
# a work area that is not Docker's own overlay:
#
# docker run --rm --security-opt seccomp=unconfined \
# --security-opt apparmor=unconfined --security-opt systempaths=unconfined \
# --tmpfs /work:exec -v "$PWD/tests/sandbox_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" ]; }
hr() { echo; echo "== $*"; }
SB=/app/lib/glacier/gsandbox
W=/work
GUID=$(id -u glacier)
mkdir -p $W/pkgs $W/root && chmod 755 $W $W/pkgs
chown glacier:glacier $W/root
/app/tests/mkroot -o $W/pkgs -p musl -p busybox -f /etc/alpine-release testroot 1.0.0 extra >/dev/null
chmod 644 $W/pkgs/*
# as glacier: run a command in the work area
asg() { su glacier -s /bin/sh -c "$*"; }
hr "-X"
R asg "$SB -X -p $W/pkgs/testroot-1.0.0.gpkg -d $W/root/testroot"
check "a user extracts a layer" rc 0
check "...its files, merged /usr" [ -x $W/root/testroot/usr/bin/busybox ]
check "...its symlinks" [ -L $W/root/testroot/usr/bin/sh ]
check "...owned by the user" [ "$(stat -c %u $W/root/testroot/usr/bin/busybox)" = "$GUID" ]
check "...no manifest or markers in the layer" [ ! -e $W/root/testroot/.glacier-manifest ]
R asg "$SB -X -p $W/pkgs/testroot-1.0.0.gpkg -d $W/root/testroot"
check "refused: a layer that exists" sh -c "[ $RC = 1 ]"
check "...saying so" has "[x] Cannot extract $W/pkgs/testroot-1.0.0.gpkg into $W/root/testroot."
check "...and leaving it alone" [ -x $W/root/testroot/usr/bin/busybox ]
mkdir -p $W/evil/evil-1.0.0/files/usr/bin
echo x > $W/evil/evil-1.0.0/files/usr/bin/x
printf 'PKG_NAME = "evil"; PKG_VER = "1.0.0"; PKG_REPO = "extra";\n' > $W/evil/evil-1.0.0/manifest.gpm.cfg
tar czf $W/pkgs/evil-1.0.0.gpkg -C $W/evil evil-1.0.0 --transform 's|files/usr/bin/x|files/../../escape|'
chmod 644 $W/pkgs/evil-1.0.0.gpkg
R asg "$SB -X -p $W/pkgs/evil-1.0.0.gpkg -d $W/root/evil"
check "refused: a package with a .. entry" sh -c "[ $RC = 1 ]"
check "...nothing left behind" sh -c "[ ! -e $W/root/evil ] && [ ! -e $W/root/escape ] && [ ! -e $W/escape ]"
tar czf $W/pkgs/evil-1.0.0.gpkg -C $W/evil evil-1.0.0 --transform 's|files/usr/bin/x|files/.glacier-fresh|'
R asg "$SB -X -p $W/pkgs/evil-1.0.0.gpkg -d $W/root/evil"
check "refused: a reserved .glacier-* name" sh -c "[ $RC = 1 ] && [ ! -e $W/root/evil ]"
hr "-R"
# run DRIVER-TEXT: a fresh work dir, in and out; the build as glacier
run() {
rm -rf $W/w $W/in $W/out
mkdir $W/w $W/in $W/out
printf '%s\n' "$1" > $W/in/driver
echo secret > $W/in/data
chown glacier:glacier $W/w $W/in $W/out $W/in/*
R asg "$SB -R -L $W/root -l testroot -w $W/w -i $W/in -o $W/out"
}
run 'echo "uid=$(id -u) gid=$(id -g) pid=$$ host=$(hostname)"
echo "nnp=$(grep NoNewPrivs /proc/self/status | tr -d "\t ")"
echo "init=$(tr "\0" " " < /proc/1/cmdline)"
echo "net=$(tail -n +3 /proc/net/dev | cut -d: -f1 | tr -d " " | tr "\n" ,)"
echo "root=$(ls -A / | tr "\n" ,)"
echo "in=$(cat /.glacier/in/data)"
touch /.glacier/in/new 2>/dev/null && echo in-writable
echo built > /.glacier/out/pkg
echo changed > /etc/alpine-release 2>/dev/null && echo "layer written through overlay"
ls /work /app /root/glacier-test.sec 2>/dev/null && echo host-visible
wget -q -T 2 -O- http://1.1.1.1 >/dev/null 2>&1 && echo network
echo "env=$(env | sort | tr "\n" ,)"
exit 7'
check "the driver runs" has "uid=0 gid=0 pid=1 host=glacier-build"
check "...its exit status comes back" rc 7
check "...no new privileges" has "nnp=NoNewPrivs:1"
check "...pid 1 of its own pid namespace" has "init=/bin/sh /.glacier/in/driver "
check "...only a loopback" sh -c 'echo "$0" | grep -qx "net=lo,"' "$OUT"
check "...the skeleton and the layer, nothing of the host" has "root=.glacier,bin,build,dev,etc,lib,proc,sbin,tmp,usr,"
check "...the input readable" has "in=secret"
check "...the input read-only" lacks "in-writable"
check "...the output written, owned by the user" sh -c "[ \"\$(cat $W/out/pkg)\" = built ] && [ \"\$(stat -c %u $W/out/pkg)\" = $GUID ]"
check "...writes to the root land in the upper layer" has "layer written through overlay"
check "...not in the layer" sh -c "cmp -s $W/root/testroot/etc/alpine-release /etc/alpine-release"
check "...but in WORK/upper" sh -c "[ \"\$(cat $W/w/upper/etc/alpine-release)\" = changed ]"
check "...no host files" lacks "host-visible"
check "...no network" lacks "network"
check "...a clean environment" has "env=FORCE_UNSAFE_CONFIGURE=1,HOME=/tmp,LC_ALL=C,PATH=/usr/bin:/bin,PWD=/,SHLVL=1,TERM=dumb,TZ=UTC,"
run 'true'
check "a driver that succeeds: 0" rc 0
run 'echo "dev=$(ls /dev | tr "\n" ,)"; echo hi > /dev/null && echo devnull-ok; head -c 4 /dev/urandom | wc -c'
check "/dev has the six devices (tty if the host has it)" sh -c "echo \"\$0\" | grep -Eq 'dev=full,null,random,(tty,)?urandom,zero,'" "$OUT"
check "...usable" has "devnull-ok"
# A layer's directory removed and made again must come back empty:
# overlayfs marks it opaque, which in a user namespace needs userxattr.
run 'rm -rf /etc && mkdir /etc && echo "etc=$(ls -A /etc | tr "\n" ,)"'
check "a layer's directory remade in the root is empty" has "etc="
check "...none of the layer's files show through" lacks "alpine-release"
hr "as root"
run_root() {
rm -rf $W/w $W/in $W/out
mkdir $W/w $W/in $W/out
printf 'echo id=$(id -u); echo x > /.glacier/out/f\n' > $W/in/driver
chown glacier:glacier $W/w $W/in $W/out $W/in/driver
R $SB -R "$@" -L $W/root -l testroot -w $W/w -i $W/in -o $W/out
}
run_root
check "refused: root without -u" has "[x] Refusing to build as root: -u is required."
check "...exit 125" rc 125
check "...nothing ran" [ ! -e $W/out/f ]
run_root -u root
check "refused: -u root" has "[x] Refusing to build as root: its uid or gid is 0."
run_root -u nobody-here
check "refused: an unknown user" has "[x] No user named nobody-here."
run_root -u glacier
check "-u glacier: the build runs" rc 0
check "...as uid 0 inside" has "id=0"
check "...its output owned by glacier" [ "$(stat -c %u $W/out/f)" = "$GUID" ]
R $SB -X -u glacier -p $W/pkgs/testroot-1.0.0.gpkg -d $W/root/second
check "-X -u glacier as root: the layer is glacier's" sh -c "[ $RC = 0 ] && [ \"\$(stat -c %u $W/root/second/usr/bin/busybox)\" = $GUID ]"
R $SB -X -p $W/pkgs/testroot-1.0.0.gpkg -d $W/root/third
check "refused: -X as root without -u" sh -c "[ $RC = 1 ] && [ ! -e $W/root/third ]"
R asg "$SB -X -u glacier -p $W/pkgs/testroot-1.0.0.gpkg -d $W/root/fourth"
check "refused: -u from a user" has "[x] -u needs root."
hr "paths"
rm -rf $W/w && mkdir $W/w && chown glacier $W/w
R asg "$SB -R -L $W/root -l ../root -w $W/w -i $W/in -o $W/out"
check "refused: a layer name with a slash" has "[x] Refusing layer name ../root."
R asg "$SB -R -L $W/root -l testroot -w $W/w,x -i $W/in -o $W/out"
check "refused: a work path with a comma" has "[x] Refusing a path that is not absolute or holds ',', ':' or '\\'."
R asg "$SB -R -L root -l testroot -w $W/w -i $W/in -o $W/out"
check "refused: a relative path" sh -c "[ $RC = 125 ]"
R asg "$SB -R -L $W/root -w $W/w -i $W/in"
check "a missing -o is a usage error" has "usage: gsandbox"
R $SB -h
check "-h prints the help" has "{-R --run}"
echo
if [ $FAILS -eq 0 ]; then echo "ALL PASSED"; else echo "$FAILS FAILED"; fi
[ $FAILS -eq 0 ]