Skip to content

eBPF-based security platform

testing: cover task policy inheritance

John Fastabend37cd0402adaa
diff --git a/Documentation/contribute/testing/testing.rst b/Documentation/contribute/testing/testing.rst
index 51c8a179..c0f6e355 100644
--- a/Documentation/contribute/testing/testing.rst
+++ b/Documentation/contribute/testing/testing.rst
@@ -80,7 +80,8 @@ Test numbers and families
A shell test is named ``t<F><K><NN>-<what>.sh``:
-- ``F`` is the family, which fixes the directory.
+- ``F`` is the numbered family. A family may have multiple named
+ directories when tests need separate VM suites.
- ``K`` is ``0`` for an operator test, which runs on the build host
without root, a kernel or a TPM of its own, and ``1`` for a target
test, which runs as root in the family's VM against an initialized
@@ -120,9 +121,10 @@ A shell test is named ``t<F><K><NN>-<what>.sh``:
- Runtime and config images, settings, keys, activation,
revocation and interruption
* - 6
- - ``functional/tamperproof/``
+ - ``functional/tamperproof/``, ``functional/labels/``
- Attacks on the admission and BPF command policy, bpffs, the
- seal, task authority, the keyrings and the TPM path
+ seal, task authority, task policy labels, the keyrings and the TPM
+ path
* - 7
- ``functional/bpm/``
- Package commands, publishing and the upatch tree
diff --git a/testing/functional/build/t1007-test-vm-cli.sh b/testing/functional/build/t1007-test-vm-cli.sh
index c854e7b1..1ae09624 100755
--- a/testing/functional/build/t1007-test-vm-cli.sh
+++ b/testing/functional/build/t1007-test-vm-cli.sh
@@ -931,7 +931,7 @@ test_expect_success 'the runners split the tests by number' '
! grep -q "/t[0-9]1[0-9][0-9]-" "$WORK/operator-tests" &&
families=$(make -s -C .. -f testing/Makefile help |
sed -n "s/.*families: //p") &&
- [ "$families" = "build signing upatch tpm image tamperproof bpm otel" ] &&
+ [ "$families" = "build signing upatch tpm image labels tamperproof bpm otel" ] &&
for d in functional/*/; do
f=${d#functional/}
case " $families " in *" ${f%/} "*) ;; *) return 1 ;; esac
diff --git a/testing/functional/labels/t6141-policy-inheritance.sh b/testing/functional/labels/t6141-policy-inheritance.sh
new file mode 100644
index 00000000..a0addc44
--- /dev/null
+++ b/testing/functional/labels/t6141-policy-inheritance.sh
@@ -0,0 +1,52 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+#
+# t6141 -- policy bits accumulate from the parent task, the executed file
+# and every interpreter handler pass.
+
+cd "$(dirname "$0")/../.."
+. ./integration-lib.sh
+target_guard
+
+PROBE=/run/shirudo/runtime/usr/bin/shirudo-policy-probe
+[ -x "$PROBE" ] || test_skip_all "runtime policy probe is missing"
+
+WORK=$(mktemp -d) || test_skip_all "cannot make a scratch directory"
+trap 'rm -rf "$WORK"' EXIT HUP INT TERM
+
+cat >"$WORK/b.sh" <<EOF
+#!$PROBE check-policy
+EOF
+cat >"$WORK/a.sh" <<EOF
+#!$PROBE fork-exec-policy
+EOF
+chmod +x "$WORK/a.sh" "$WORK/b.sh"
+"$PROBE" set-policy "$WORK/a.sh" 0x1 ||
+ test_skip_all "scratch filesystem cannot store policy xattrs"
+"$PROBE" set-policy "$WORK/b.sh" 0x2 ||
+ test_skip_all "cannot set the child policy xattr"
+
+cat >"$WORK/interpreter" <<EOF
+#!$PROBE check-policy
+EOF
+cat >"$WORK/file.sh" <<EOF
+#!$WORK/interpreter
+EOF
+chmod +x "$WORK/interpreter" "$WORK/file.sh"
+"$PROBE" set-policy "$WORK/interpreter" 0x8 ||
+ test_skip_all "cannot set the interpreter policy xattr"
+"$PROBE" set-policy "$WORK/file.sh" 0x4 ||
+ test_skip_all "cannot set the original file policy xattr"
+
+test_expect_success 'A policy is inherited by B and ORed with B policy' '
+ SHIRUDO_POLICY_PARENT=0x1 \
+ SHIRUDO_POLICY_CHILD="$WORK/b.sh" \
+ SHIRUDO_POLICY_CHILD_EXPECTED=0x3 "$WORK/a.sh"
+'
+
+test_expect_success 'original file and interpreter policies are ORed' '
+ SHIRUDO_EXPECTED_POLICY=0xc "$WORK/file.sh"
+'
+
+test_done
diff --git a/testing/helper/policy-probe.c b/testing/helper/policy-probe.c
index 0b890aa7..32e5a648 100644
--- a/testing/helper/policy-probe.c
+++ b/testing/helper/policy-probe.c
@@ -15,6 +15,7 @@
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/syscall.h>
+#include <sys/xattr.h>
#include <sys/wait.h>
#include <unistd.h>
@@ -54,6 +55,101 @@ static int check_mark(const char *pin, int expected)
return ret;
}
+static int read_policy(const char *pin, __u64 *active)
+{
+ struct shirudo_task_policy policy = {};
+ union bpf_attr attr = {};
+ int fd, pidfd, rc;
+
+ pidfd = syscall(SYS_pidfd_open, getpid(), 0);
+ if (pidfd < 0)
+ return -errno;
+ attr.pathname = (uintptr_t)pin;
+ attr.file_flags = BPF_F_RDONLY;
+ fd = syscall(SYS_bpf, BPF_OBJ_GET, &attr, sizeof(attr));
+ if (fd < 0) {
+ rc = -errno;
+ close(pidfd);
+ return rc;
+ }
+ memset(&attr, 0, sizeof(attr));
+ attr.map_fd = fd;
+ attr.key = (uintptr_t)&pidfd;
+ attr.value = (uintptr_t)&policy;
+ rc = syscall(SYS_bpf, BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr)) ?
+ -errno : 0;
+ close(fd);
+ close(pidfd);
+ if (!rc)
+ *active = policy.active;
+ return rc;
+}
+
+static int parse_policy(const char *text, __u64 *value)
+{
+ char *end;
+ __u64 parsed;
+
+ errno = 0;
+ parsed = strtoull(text, &end, 0);
+ if (errno || end == text || *end)
+ return -EINVAL;
+ *value = (__u64)parsed;
+ return 0;
+}
+
+static int set_policy(const char *path, const char *text)
+{
+ __u64 value;
+
+ if (parse_policy(text, &value))
+ return 2;
+ return setxattr(path, "security.bpf.shirudo.policy", &value,
+ sizeof(value), 0) ? 1 : 0;
+}
+
+static int check_policy(const char *text)
+{
+ __u64 expected, active = 0;
+
+ if (parse_policy(text, &expected) ||
+ read_policy("/run/shirudo/bpffs/policy_map", &active))
+ return 1;
+ return active != expected;
+}
+
+static int check_policy_env(void)
+{
+ const char *expected = getenv("SHIRUDO_EXPECTED_POLICY");
+
+ return !expected || check_policy(expected);
+}
+
+static int fork_exec_policy_env(void)
+{
+ const char *path = getenv("SHIRUDO_POLICY_CHILD");
+ const char *parent = getenv("SHIRUDO_POLICY_PARENT");
+ const char *expected = getenv("SHIRUDO_POLICY_CHILD_EXPECTED");
+ char *const child_argv[] = { (char *)path, NULL };
+ pid_t child;
+ int status;
+
+ if (!path || !parent || !expected || check_policy(parent))
+ return 1;
+ child = fork();
+ if (child < 0)
+ return 1;
+ if (!child) {
+ if (setenv("SHIRUDO_EXPECTED_POLICY", expected, 1))
+ _exit(127);
+ execv(path, child_argv);
+ _exit(127);
+ }
+ if (waitpid(child, &status, 0) != child)
+ return 1;
+ return !WIFEXITED(status) || WEXITSTATUS(status);
+}
+
static void print_hex(const __u8 *bytes, size_t n)
{
size_t i;
@@ -164,6 +260,12 @@ int main(int argc, char **argv)
printf("%lu\n", getauxval(AT_SECURE));
return 0;
}
+ if (argc == 4 && !strcmp(argv[1], "set-policy"))
+ return set_policy(argv[2], argv[3]);
+ if (argc >= 2 && !strcmp(argv[1], "check-policy"))
+ return check_policy_env();
+ if (argc >= 2 && !strcmp(argv[1], "fork-exec-policy"))
+ return fork_exec_policy_env();
if (argc < 3)
return 2;
if (!strcmp(argv[1], "unlink"))