eBPF-based security platform
testing: verify a load whose archive copy fails is undone
Daniel Borkmann58f6b6a6bf3f
diff --git a/testing/run-target-tests.sh b/testing/run-target-tests.shindex f813035e..dcdb5c20 100755--- a/testing/run-target-tests.sh+++ b/testing/run-target-tests.sh@@ -77,6 +77,7 @@ for t in \tamperproof/t1159-perf-attach.sh \tamperproof/t1160-umount-above.sh \tamperproof/t1161-bpffs-entries.sh \+ tamperproof/t1162-persist-undo.sh \tamperproof/t1150-verity-devices.sh \tamperproof/t1151-verity-process.sh \tamperproof/t1153-verity-hidden.sh \diff --git a/testing/tamperproof/t1162-persist-undo.sh b/testing/tamperproof/t1162-persist-undo.shnew file mode 100755index 00000000..f239f006--- /dev/null+++ b/testing/tamperproof/t1162-persist-undo.sh@@ -0,0 +1,74 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# t1162 -- a load whose archive copy fails is undone. The archive is+# written after the programs are attached and pinned, and a root that+# fills the file system between the staged copy and the archive copy,+# or at any moment before, got a signed upatch running with no archive+# and no register event: unmeasured, and not restored at the next boot.+# The load must report the persist failure with its pins gone.++cd "$(dirname "$0")/.."+. ./integration-lib.sh+target_guard+command -v python3 >/dev/null 2>&1 || test_skip_all "no python3"+command -v fallocate >/dev/null 2>&1 || test_skip_all "no fallocate"+FIXTURES=${SHIRUDO_TEST_FIXTURES_DIR:?target fixture directory is not set}+NAME=shirudo-test-lifecycle+LOAD="$FIXTURES/$NAME.upatch"+UNLOAD="$FIXTURES/$NAME.unload.upatch"+PIN="/run/shirudo/bpffs/upatches/$NAME"+FILL=/var/tmp/t1162-fill+[ -s "$LOAD" ] && [ -s "$UNLOAD" ] || test_skip_all "missing lifecycle fixture"+[ ! -e "$PIN" ] || test_skip_all "$NAME is loaded"+[ "$(stat -c %d /var/tmp)" = "$(stat -c %d /var/lib/shirudo)" ] ||+ test_skip_all "/var/tmp and /var/lib/shirudo are on different file systems"++# Under the library's errexit a failing step would end the cleanup.+cleanup() {+ rm -f "$FILL".* 2>/dev/null || true+ if [ -e "$PIN" ]; then+ "$SHIRUDO_BIN" upatch unload "$UNLOAD" --no-precheck \+ >/dev/null 2>&1 || true+ fi+}+trap cleanup EXIT++# Leave room for the staged copy and half of another, not for the+# archive copy: reserve that much, fill the rest to the last block+# fallocate takes, then give the reservation back.+fill() {+ size=$(stat -c %s "$LOAD")+ fallocate -l $((size + size / 2)) "$FILL.keep" || return 1+ n=0+ chunk=$((64 * 1024 * 1024))+ while [ "$chunk" -ge 4096 ]; do+ if fallocate -l "$chunk" "$FILL.$n" 2>/dev/null; then+ n=$((n + 1))+ else+ chunk=$((chunk / 2))+ fi+ done+ rm -f "$FILL.keep"+}++test_expect_success 'a load whose archive copy fails is undone, pins included' '+ phase= &&+ { fill && phase=$("$SHIRUDO_BIN" upatch load "$LOAD" --no-precheck \+ --report=- 2>/dev/null | report_field phase); } ; rc=$? ;+ rm -f "$FILL".* &&+ [ "$rc" = 0 ] &&+ [ "$phase" = persist ] &&+ [ ! -e "$PIN" ] &&+ [ ! -e "/var/lib/shirudo/upatches/$NAME.upatch" ]+'++test_expect_success 'the same load succeeds with room again' '+ "$SHIRUDO_BIN" upatch load "$LOAD" --no-precheck >/dev/null 2>&1 &&+ [ -e "$PIN/link" ] &&+ "$SHIRUDO_BIN" upatch unload "$UNLOAD" --no-precheck >/dev/null 2>&1 &&+ [ ! -e "$PIN" ]+'++test_done