eBPF-based security platform
testing: stabilize filesystem seal target tests
John Fastabendccd7379bfcdd
diff --git a/testing/tamperproof/t1101-seal.sh b/testing/tamperproof/t1101-seal.shindex 5987f3d2..6a723637 100644--- a/testing/tamperproof/t1101-seal.sh+++ b/testing/tamperproof/t1101-seal.sh@@ -17,8 +17,13 @@ FIXTURES=$SHIRUDO_TEST_FIXTURES_DIR[ -s "$ARCHIVE" ] || test_skip_all "no archived basic upatch"before=$(sha256sum "$CERT" | cut -d' ' -f1)archive_before=$(sha256sum "$ARCHIVE" | cut -d' ' -f1)-WORK=$(mktemp -d)-replacement=/var/lib/shirudo/upatches/.t1101-replacement.$$+WORK=$(mktemp -d /root/.t1101.XXXXXX) ||+ test_skip_all "cannot make a scratch directory"+[ "$(stat -c %d "$WORK")" = "$(stat -c %d "$ARCHIVE")" ] || {+ rm -rf "$WORK"+ test_skip_all "no unlabelled scratch directory on the archive filesystem"+}+replacement="$WORK/replacement"trap 'rm -f "$replacement"; rm -rf "$WORK"' EXITtest_expect_success 'seal denies a non-shirudo write to a labelled file' 'diff --git a/testing/tamperproof/t1133-seal-init-xattr.sh b/testing/tamperproof/t1133-seal-init-xattr.shindex 47f2f6bc..c24007f3 100755--- a/testing/tamperproof/t1133-seal-init-xattr.sh+++ b/testing/tamperproof/t1133-seal-init-xattr.sh@@ -5,44 +5,52 @@# t1133 -- labelling at creation (bpf/seal.c inode_init_security): an# inode created under a labelled directory the seal has seen is born# with the label, whoever creates it, and is sealed from that instant.-# The probes are made under /var/lib/shirudo/locks, which nothing-# enumerates, and stay there: a sealed inode cannot be removed by the-# test. Non-destructive otherwise.+# Per-run probes live directly under /var/lib/shirudo and stay there:+# sealed inodes cannot be removed by the test. Non-destructive otherwise.cd "$(dirname "$0")/..". ./integration-lib.shtarget_guardLABEL=94442b45cfcdc59aabc2a91364ed2ae129418e05a5c1985231b9c032e673f469-LOCKS=/var/lib/shirudo/locks+LOCKS=/var/lib/shirudo/locks.t1133.$(cat /proc/sys/kernel/random/uuid)[ -e /run/shirudo/bpffs/seal_inode_init_security_link ] ||test_skip_all "the seal does not label at creation on this target"label_of() {python3 -c '-import os, sys+import errno, os, systry:print(os.getxattr(sys.argv[1], "security.bpf.shirudo",follow_symlinks=False).decode())-except OSError:- print("none")' "$1"+except OSError as e:+ print("unsupported" if e.errno == errno.EOPNOTSUPP else "none")' "$1"}-test_expect_success 'the run directory carries the label' '- [ "$(label_of /run/shirudo)" = "$LABEL" ]-'+RUN_LABEL=$(label_of /run/shirudo)+if [ "$RUN_LABEL" = unsupported ]; then+ echo "# SKIP /run does not support security xattrs"+else+ test_expect_success 'the run directory carries the label' '+ [ "$(label_of /run/shirudo)" = "$LABEL" ]+ '+fi-# Opening the directory is what marks it; the walk at init did, and so-# does this, in case the inode was evicted since. The create succeeds-# and the write open that follows it in the same call is what the seal-# refuses, so the shell's status is not looked at.+# Opening the directory records its label for inode_init_security.+# The write open is expected to fail after inode creation; test it in+# a child shell so set -e does not skip the xattr assertion.test_expect_success 'a file root creates under a sealed directory is born labelled' 'ls /var/lib/shirudo >/dev/null &&mkdir -p "$LOCKS" &&ls "$LOCKS" >/dev/null &&- { : > "$LOCKS/t1133.probe"; } 2>/dev/null;- [ -e "$LOCKS/t1133.probe" ] &&- [ "$(label_of "$LOCKS/t1133.probe")" = "$LABEL" ]+ if ( : > "$LOCKS/t1133.probe" ) 2>/dev/null; then+ :+ fi+ label=$(label_of "$LOCKS/t1133.probe")+ [ -e "$LOCKS/t1133.probe" ] && [ "$label" = "$LABEL" ] || {+ echo "probe label: $label; directory label: $(label_of "$LOCKS")" >&2+ false+ }'test_expect_success 'and is sealed from that instant: no write, unlink or rename' '@@ -71,7 +79,7 @@ test_expect_success 'an O_TMPFILE under the tree cannot be opened for writing' ''test_expect_success 'a file created outside the tree stays unlabelled' '- f=$(mktemp) &&+ f=$(mktemp /root/t1133.XXXXXX) &&[ "$(label_of "$f")" = none ] &&rm -f "$f"'diff --git a/testing/tamperproof/t1134-seal-task.sh b/testing/tamperproof/t1134-seal-task.shindex 18cdb0f1..c21fa329 100755--- a/testing/tamperproof/t1134-seal-task.sh+++ b/testing/tamperproof/t1134-seal-task.sh@@ -28,7 +28,9 @@ try:os.close(fd)except OSError:pass' "$W/fifo" 2>/dev/null- [ -n "$PID" ] && wait "$PID" 2>/dev/null+ if [ -n "$PID" ]; then+ wait "$PID" 2>/dev/null || :+ firm -rf "$W"}trap cleanup EXITdiff --git a/testing/tamperproof/test.plan.md b/testing/tamperproof/test.plan.mdindex fc2d905e..bbe4b94f 100644--- a/testing/tamperproof/test.plan.md+++ b/testing/tamperproof/test.plan.md@@ -44,7 +44,7 @@ checks that Shirudo can still export the pre-existing Python exec record.| TP-22 | Root puts the public seal label on a file, or runs a binary a mounted file system labels, and then writes, renames, unlinks or relabels a labelled file | Covered | `t1132-seal-scratch.sh` | Only Shirudo may set the label, and the seal admits the executable `init` recorded, not one that carries the label. A failure is a bypass of the seal. || TP-23 | Root renames an unlabelled file over a labelled file and so replaces it | Covered | `t1132-seal-scratch.sh` | The seal checks the destination of a rename as well as its source; only the recorded executable may be replaced, as a package upgrade does. || TP-24 | Root truncates, chmods or chowns a labelled file without opening it | Covered | `t1132-seal-scratch.sh` | `truncate(2)` changes the contents without a write open, and `chmod` can make a labelled file unreadable to a caller that may still write it; the seal guards attribute changes like writes. |-| TP-25 | Root creates a file, directory, symlink or O_TMPFILE under a labelled directory | Covered | `t1133-seal-init-xattr.sh` | The inode is born labelled and sealed from that instant; a child of a new directory needs no reopen; the run directory is labelled; a file outside the tree is not. |+| TP-25 | Root creates a file, directory, symlink or O_TMPFILE under a labelled directory | Covered | `t1133-seal-init-xattr.sh` | The inode is born labelled and sealed from that instant; a child of a new directory needs no reopen; the run directory is labelled when its filesystem supports security xattrs; a file outside the tree is not. || TP-26 | Root attaches a debugger to a running shirudo task with `PTRACE_ATTACH` | Covered | `t1134-seal-task.sh` | The seal refuses with `EACCES`, Yama's `EPERM` would not do; the task is unharmed and finishes. || TP-27 | Root binds a perf event to a running shirudo task | Covered | `t1134-seal-task.sh` | `perf_event_open` on the task is refused with `EACCES`; the same event on the caller's own task is the positive control. || TP-28 | Root renames a pin in the private bpffs, or renames one over the lsm/bpf link's pin | Covered | `t1135-pin-rename.sh` | Both are refused with `EPERM`; the pins stay and enforcement survives. Before `lsm/inode_rename` the rename over a pin dropped the link with no guard firing. |