eBPF-based security platform
testing: check that inodes under a sealed directory are born labelled
Daniel Borkmannca0c8140f9de
diff --git a/Makefile.shirudo b/Makefile.shirudoindex a769b9e5..3d514a71 100644--- a/Makefile.shirudo+++ b/Makefile.shirudo@@ -494,6 +494,7 @@ TEST_HELPERS := testing/helper/test-json-parser \testing/helper/test-register \testing/helper/test-eventlog \testing/helper/test-keys \+ testing/helper/test-seal \$(TPMSIM_HELPER) \testing/helper/test-config \testing/helper/test-bpm-source \@@ -531,6 +532,11 @@ testing/helper/test-keys: testing/helper/test-keys.c util/keys.o \@printf ' CC $@\n'$(Q)$(CC) $(CFLAGS) -o $@ $^ $(REGISTER_LIBS)+testing/helper/test-seal: testing/helper/test-seal.c util/seal.o util/io.o \+ util/log.o+ @printf ' CC $@\n'+ $(Q)$(CC) $(CFLAGS) -o $@ $^+testing/helper/test-eventlog: testing/helper/test-eventlog.c $(REGISTER_OBJS)@printf ' CC $@\n'$(Q)$(CC) $(CFLAGS) -o $@ $^ $(REGISTER_LIBS)diff --git a/testing/helper/test-seal.c b/testing/helper/test-seal.cnew file mode 100644index 00000000..0cea8e9a--- /dev/null+++ b/testing/helper/test-seal.c@@ -0,0 +1,78 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * Drives the util/seal.c creation helpers on paths a shell test names.+ * Subcommand on argv[1]; prints what the test asserts on.+ */++#include <errno.h>+#include <fcntl.h>+#include <stdio.h>+#include <string.h>+#include <sys/xattr.h>+#include <unistd.h>++#include "io.h"+#include "paths.h"+#include "seal.h"++static int append(const char *path, const char *text)+{+ int fd, rc;++ fd = shirudo_seal_open(path, O_WRONLY | O_CREAT | O_APPEND, 0600);+ if (fd < 0)+ return fd;+ rc = shirudo_write_all(fd, text, strlen(text));+ if (close(fd) && !rc)+ rc = -errno;+ return rc;+}++/* The label an inode carries, or none, without following a link. */+static void show_label(const char *path)+{+ char label[128];+ ssize_t n;++ n = lgetxattr(path, SHIRUDO_XATTR_NAME, label, sizeof(label) - 1);+ if (n < 0) {+ printf("label=none\n");+ return;+ }+ label[n] = '\0';+ printf("label=%s\n", label);+}++int main(int argc, char **argv)+{+ int rc;++ if (argc < 3) {+ fprintf(stderr, "usage: %s label|mkdir|open|write|inherit|get "+ "<path> [text]\n", argv[0]);+ return 2;+ }+ if (strcmp(argv[1], "get") == 0) {+ show_label(argv[2]);+ return 0;+ }+ if (strcmp(argv[1], "label") == 0) {+ rc = shirudo_seal_label(argv[2]);+ } else if (strcmp(argv[1], "mkdir") == 0) {+ rc = shirudo_seal_mkdir_p(argv[2], 0700);+ } else if (strcmp(argv[1], "open") == 0 && argc == 4) {+ rc = append(argv[2], argv[3]);+ } else if (strcmp(argv[1], "write") == 0 && argc == 4) {+ rc = shirudo_seal_write_file(argv[2], argv[3], strlen(argv[3]),+ 0600);+ } else if (strcmp(argv[1], "inherit") == 0) {+ rc = shirudo_seal_inherit_path(argv[2]);+ } else {+ fprintf(stderr, "unknown subcommand\n");+ return 2;+ }+ printf("rc=%d\n", rc);+ return rc ? 1 : 0;+}diff --git a/testing/run-target-tests.sh b/testing/run-target-tests.shindex 3fc21bf3..d876141b 100755--- a/testing/run-target-tests.sh+++ b/testing/run-target-tests.sh@@ -35,6 +35,7 @@ for t in \t1126-keys-revoke.sh \tamperproof/t1101-seal.sh \tamperproof/t1132-seal-scratch.sh \+ tamperproof/t1133-seal-init-xattr.sh \tamperproof/t1105-siggate.sh \tamperproof/t1110-lsm-deny.sh \tamperproof/t1112-prog-load-keyring-gate.sh \diff --git a/testing/t0047-seal-create.sh b/testing/t0047-seal-create.shnew file mode 100755index 00000000..5bf77de7--- /dev/null+++ b/testing/t0047-seal-create.sh@@ -0,0 +1,92 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# t0047 -- creating under a sealed directory from userspace: the+# util/seal.c helpers create relative to the opened directory and give+# a new inode the directory's label where the kernel did not, so that+# a file, a directory chain, an atomic replacement and a symlink under+# a labelled directory come out labelled, and nothing outside one does.+# The labelled cases need root on a file system with security xattrs+# and are skipped without.++cd "$(dirname "$0")"+. ./test-lib.sh+H=helper/test-seal+LABEL=94442b45cfcdc59aabc2a91364ed2ae129418e05a5c1985231b9c032e673f469+W=$(mktemp -d)+trap 'rm -rf "$W"' EXIT++test_expect_success 'a directory chain is created in one go' '+ ./$H mkdir "$W/plain/a/b/c" | grep -qx "rc=0" &&+ [ -d "$W/plain/a/b/c" ] &&+ ./$H mkdir "$W/plain/a/b/c" | grep -qx "rc=0"+'++test_expect_success 'a file is replaced atomically and its temporary removed' '+ ./$H write "$W/plain/state" one | grep -qx "rc=0" &&+ [ "$(cat "$W/plain/state")" = one ] &&+ ./$H write "$W/plain/state" two | grep -qx "rc=0" &&+ [ "$(cat "$W/plain/state")" = two ] &&+ test ! -e "$W/plain/state.tmp"+'++test_expect_success 'a file is opened for appending and created on demand' '+ ./$H open "$W/plain/log" first | grep -qx "rc=0" &&+ ./$H open "$W/plain/log" second | grep -qx "rc=0" &&+ [ "$(cat "$W/plain/log")" = firstsecond ]+'++test_expect_success 'a temporary that is a symlink is refused, not followed' '+ ln -s "$W/plain/state" "$W/plain/victim.tmp" &&+ ! ./$H write "$W/plain/victim" x >/dev/null 2>&1 &&+ [ "$(cat "$W/plain/state")" = two ] &&+ test ! -e "$W/plain/victim"+'++test_expect_success 'nothing under an unlabelled directory gets a label' '+ ./$H get "$W/plain/state" | grep -qx "label=none" &&+ ./$H get "$W/plain/a/b/c" | grep -qx "label=none" &&+ ./$H get "$W/plain/log" | grep -qx "label=none"+'++mkdir "$W/sealed"+if ! ./$H label "$W/sealed" >/dev/null 2>&1; then+ echo "# no security xattrs here (root on ext4 or tmpfs needed); the labelled cases are skipped"+ test_done+fi++test_expect_success 'a file written under a labelled directory carries its label' '+ ./$H write "$W/sealed/state" one | grep -qx "rc=0" &&+ ./$H get "$W/sealed/state" | grep -qx "label=$LABEL"+'++test_expect_success 'a file opened for appending there carries it too' '+ ./$H open "$W/sealed/log" x | grep -qx "rc=0" &&+ ./$H get "$W/sealed/log" | grep -qx "label=$LABEL"+'++test_expect_success 'every directory of a chain carries it, and a file below' '+ ./$H mkdir "$W/sealed/a/b" | grep -qx "rc=0" &&+ ./$H get "$W/sealed/a" | grep -qx "label=$LABEL" &&+ ./$H get "$W/sealed/a/b" | grep -qx "label=$LABEL" &&+ ./$H write "$W/sealed/a/b/f" x | grep -qx "rc=0" &&+ ./$H get "$W/sealed/a/b/f" | grep -qx "label=$LABEL"+'++test_expect_success 'a symlink gets the label itself, its target untouched' '+ touch "$W/plain/target" &&+ ln -s "$W/plain/target" "$W/sealed/link" &&+ ./$H inherit "$W/sealed/link" | grep -qx "rc=0" &&+ ./$H get "$W/sealed/link" | grep -qx "label=$LABEL" &&+ ./$H get "$W/plain/target" | grep -qx "label=none"+'++test_expect_success 'a directory that exists already is left as it is' '+ mkdir "$W/sealed/old" &&+ ./$H mkdir "$W/sealed/old/new" | grep -qx "rc=0" &&+ ./$H get "$W/sealed/old" | grep -qx "label=none" &&+ ./$H get "$W/sealed/old/new" | grep -qx "label=none"+'++test_donediff --git a/testing/tamperproof/t1133-seal-init-xattr.sh b/testing/tamperproof/t1133-seal-init-xattr.shnew file mode 100755index 00000000..47f2f6bc--- /dev/null+++ b/testing/tamperproof/t1133-seal-init-xattr.sh@@ -0,0 +1,79 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# 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.++cd "$(dirname "$0")/.."+. ./integration-lib.sh+target_guard++LABEL=94442b45cfcdc59aabc2a91364ed2ae129418e05a5c1985231b9c032e673f469+LOCKS=/var/lib/shirudo/locks+[ -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+try:+ print(os.getxattr(sys.argv[1], "security.bpf.shirudo",+ follow_symlinks=False).decode())+except OSError:+ print("none")' "$1"+}++test_expect_success 'the run directory carries the label' '+ [ "$(label_of /run/shirudo)" = "$LABEL" ]+'++# 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.+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" ]+'++test_expect_success 'and is sealed from that instant: no write, unlink or rename' '+ ! sh -c "echo x > \"$LOCKS/t1133.probe\"" 2>/dev/null &&+ ! rm "$LOCKS/t1133.probe" 2>/dev/null &&+ ! mv "$LOCKS/t1133.probe" "$LOCKS/t1133.moved" 2>/dev/null &&+ [ -e "$LOCKS/t1133.probe" ] &&+ [ ! -s "$LOCKS/t1133.probe" ]+'++test_expect_success 'a new directory is marked in turn: its child needs no reopen' '+ mkdir "$LOCKS/t1133.dir" &&+ [ "$(label_of "$LOCKS/t1133.dir")" = "$LABEL" ] &&+ mkfifo "$LOCKS/t1133.dir/child" &&+ [ "$(label_of "$LOCKS/t1133.dir/child")" = "$LABEL" ]+'++test_expect_success 'a symlink is born labelled as well' '+ ln -s t1133.probe "$LOCKS/t1133.link" &&+ [ "$(label_of "$LOCKS/t1133.link")" = "$LABEL" ]+'++test_expect_success 'an O_TMPFILE under the tree cannot be opened for writing' '+ ! python3 -c "import os,sys; os.open(sys.argv[1], os.O_TMPFILE | os.O_WRONLY, 0o600)" \+ "$LOCKS" 2>/dev/null+'++test_expect_success 'a file created outside the tree stays unlabelled' '+ f=$(mktemp) &&+ [ "$(label_of "$f")" = none ] &&+ rm -f "$f"+'++test_donediff --git a/testing/tamperproof/test.plan.md b/testing/tamperproof/test.plan.mdindex 8928631d..b4895090 100644--- a/testing/tamperproof/test.plan.md+++ b/testing/tamperproof/test.plan.md@@ -44,6 +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. |## Review notes