eBPF-based security platform
testing: cover upatch policy labels
John Fastabenda0566cb5c957
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex 11ee2736..c1b14215 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -177,6 +177,9 @@ signed config image.- A live ``uprobe.multi`` load, refusal after a build ID change* - ``t3108-status-many``- ``status --json`` with eighty loaded upatches lists all of them+ * - ``t3109-policy-labels``+ - A loaded upatch labels executable and ordinary files, ORing+ multiple policy bits with a preexisting xattr``tpm`` (4)~~~~~~~~~~~@@ -358,6 +361,11 @@ signed config image.- Repeated peeks and concurrent drains* - ``t6140-status-keyrings``- Status reports both keyrings with each certificate's frozen key+ * - ``t6141-policy-inheritance``+ - Task policy accumulates across parent exec, child exec and+ interpreter handlers+ * - ``t6142-policy-socket-inheritance``+ - Task policy is inherited by a listener and its accepted socket``bpm`` (7)~~~~~~~~~~~@@ -383,6 +391,9 @@ signed config image.* - ``t7005-bpm-publish``- ``bpm build``, ``bpm push`` with a signed index, ``bpm treeappend``+ * - ``t7006-policy-labels``+ - Beefile policy declarations are serialized into the signed+ upatch section* - ``t7101-bpm-tree``- Tree sync with a selector* - ``t7102-bpm-tree-sequential``@@ -575,13 +586,17 @@ Coverage by area- ``t4004``, ``t5001`` to ``t5008``, ``t5101`` to ``t5105``,``t6112``, ``t6127``, ``t6130``, ``t6132``. Fuzz: ``config``* - Packages and repositories- - ``t7001``, ``t7002``, ``t7003``, ``t7004``, ``t7005``. Fuzz:+ - ``t7001``, ``t7002``, ``t7003``, ``t7004``, ``t7005``,+ ``t7006``. Fuzz:``beefile``, ``bpm-index``, ``bpm-package``, ``bpm-tar``,``oci-http``, ``oci-manifest``, ``oci-ref``, ``registry-auth``* - upatch tree- ``t3011``, ``t5004``, ``t7005``, ``t7101``, ``t7102``,``t7103``, ``t9105``. Fuzz: ``tree``, ``tree-state``,``kconfig``. Unit: ``kernel_build_id``, ``kernel_release``+ * - Policy labels+ - Upatch labels and Beefile serialization: ``t3109``, ``t7006``.+ Task and socket inheritance: ``t6141``, ``t6142``* - Telemetry- ``t6139``, ``t8001`` to ``t8006``, ``t8101`` to ``t8105``,``t9101`` to ``t9105``. Fuzz: ``otel``, ``serializer``diff --git a/Documentation/contribute/testing/testing.rst b/Documentation/contribute/testing/testing.rstindex 04b77f33..dee2a44c 100644--- a/Documentation/contribute/testing/testing.rst+++ b/Documentation/contribute/testing/testing.rst@@ -111,7 +111,7 @@ A shell test is named ``t<F><K><NN>-<what>.sh``:* - 3- ``functional/upatch/``- The ``.upatch`` container, attach metadata, decoders,- serializers, load, unload and ``upatch get``+ serializers, signed policy labels, load, unload and ``upatch get``* - 4- ``functional/tpm/``- TPM structures, the measurement register, the eventlog,@@ -123,11 +123,12 @@ A shell test is named ``t<F><K><NN>-<what>.sh``:* - 6- ``functional/tamperproof/``, ``functional/labels/``- Attacks on the admission and BPF command policy, bpffs, the- seal, task authority, task and socket policy labels, the keyrings- and the TPM path+ seal, task authority, task and socket policy inheritance, the+ keyrings and the TPM path* - 7- ``functional/bpm/``- - Package commands, publishing and the upatch tree+ - Package commands, Beefile policy labels, publishing and the+ upatch tree* - 8- ``functional/otel/``- Map export, the status keepalive, OTLP transport and TLS policydiff --git a/testing/Makefile b/testing/Makefileindex 294d349e..4a5a9e3b 100644--- a/testing/Makefile+++ b/testing/Makefile@@ -134,6 +134,7 @@ TEST_VM_BUNDLE_SOURCES := $(sort $(wildcard \$(ROOT)/systemd/*.timer $(ROOT)/testing/fixtures/*.c $(ROOT)/testing/fixtures/*.lua \$(ROOT)/testing/fixtures/*.conf $(ROOT)/testing/fixtures/*.sh \$(ROOT)/testing/fixtures/*.h \+ $(ROOT)/testing/fixtures/policy-labels/* \$(ROOT)/testing/fixtures/decoders/* \$(ROOT)/upatches/kernel-obs/*.c $(ROOT)/upatches/kernel-obs/*.h $(ROOT)/upatches/kernel-obs/*.list \$(ROOT)/upatches/kernel-obs/*.py $(ROOT)/upatches/kernel-obs/Beefile \diff --git a/testing/fixtures/policy-labels/Beefile b/testing/fixtures/policy-labels/Beefilenew file mode 100644index 00000000..2189f39f--- /dev/null+++ b/testing/fixtures/policy-labels/Beefile@@ -0,0 +1,22 @@+[package]+name = "policy-labels"+description = "policy label tests"+license = "GPL-2.0 OR BSD-2-Clause"++[[upatches]]+source = "policy-labels.bpf.c"++[[upatches.policy]]+id = "executable"+bit = 0x1+paths = ["/tmp/shirudo-policy-label-test/executable"]++[[upatches.policy]]+id = "ordinary-a"+bit = 0x2+paths = ["/tmp/shirudo-policy-label-test/ordinary"]++[[upatches.policy]]+id = "ordinary-b"+bit = 0x4+paths = ["/tmp/shirudo-policy-label-test/ordinary"]diff --git a/testing/fixtures/policy-labels/policy-labels.bpf.c b/testing/fixtures/policy-labels/policy-labels.bpf.cnew file mode 100644index 00000000..9d005337--- /dev/null+++ b/testing/fixtures/policy-labels/policy-labels.bpf.c@@ -0,0 +1,12 @@+// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause+#include "vmlinux.h"+#include <bpf/bpf_helpers.h>+#include <bpf/bpf_tracing.h>++char _license[] SEC("license") = "Dual BSD/GPL";++SEC("fentry/do_nanosleep")+int BPF_PROG(shirudo_test_policy_labels)+{+ return 0;+}diff --git a/testing/functional/bpm/t7006-policy-labels.sh b/testing/functional/bpm/t7006-policy-labels.shnew file mode 100755index 00000000..8606e96c--- /dev/null+++ b/testing/functional/bpm/t7006-policy-labels.sh@@ -0,0 +1,57 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# t7006 -- bpm build turns Beefile policy declarations into the signed+# policy-label section consumed by upatch load.++cd "$(dirname "$0")/../.."+. ./test-lib.sh++S=../shirudo+SHB=../tools/shirudo-build/shirudo-build+CHECK=helper/test-upatch-prog-metadata++command -v clang >/dev/null 2>&1 || test_skip_all "no clang"+command -v openssl >/dev/null 2>&1 || test_skip_all "no openssl"+command -v tar >/dev/null 2>&1 || test_skip_all "no tar"+[ -x "$S" ] || test_skip_all "shirudo not built"+[ -x "$SHB" ] || test_skip_all "shirudo-build not built"+[ -x "$CHECK" ] || test_skip_all "upatch metadata helper not built"+[ -e ../vendor/linux/vmlinux.h ] || test_skip_all "no vendor/linux/vmlinux.h"+openssl list -public-key-algorithms 2>/dev/null | grep -qi 'ML-DSA' ||+ test_skip_all "openssl without ML-DSA (needs OpenSSL >= 3.5)"++REPO=$(cd .. && pwd)+S="$REPO/shirudo"+WORK=$(mktemp -d)+trap 'rm -rf "$WORK"' EXIT HUP INT TERM++"$S" genkey --keys-dir "$WORK/keys" >/dev/null 2>&1 ||+ test_skip_all "genkey failed"+mkdir -p "$WORK/pkg"+cp fixtures/policy-labels/Beefile \+ fixtures/policy-labels/policy-labels.bpf.c "$WORK/pkg/"+ln -s "$REPO/vendor" "$WORK/pkg/vendor"+ln -s "$REPO/bpf" "$WORK/pkg/bpf"+case "$SHB" in+/*) B="$SHB" ;;+*) B="$REPO/${SHB#../}" ;;+esac++export SHIRUDO_BUILD="$B"+( cd "$WORK" && "$S" bpm build pkg --version 1.0.0 \+ --keys-dir "$WORK/keys" --vmlinux "$REPO/vendor/linux/vmlinux.h" ) \+ >"$WORK/build.out" 2>&1 || {+ cat "$WORK/build.out" >&2+ test_skip_all "bpm build failed"+}++test_expect_success 'Beefile policy entries are carried in the signed upatch' '+ tar -xOf "$WORK/policy-labels-1.0.0.bee" \+ policy-labels-1.0.0/upatches/policy-labels.upatch \+ >"$WORK/policy-labels.upatch" &&+ "$CHECK" "$WORK/policy-labels.upatch" policy-labels+'++test_donediff --git a/testing/functional/upatch/t3109-policy-labels.sh b/testing/functional/upatch/t3109-policy-labels.shnew file mode 100755index 00000000..e4a2beff--- /dev/null+++ b/testing/functional/upatch/t3109-policy-labels.sh@@ -0,0 +1,56 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# t3109 -- a signed policy section labels executables and ordinary files,+# ORing multiple declarations with an existing file xattr.++cd "$(dirname "$0")/../.."+. ./integration-lib.sh+target_guard++FIXTURES=${SHIRUDO_TEST_FIXTURES_DIR:?target fixture directory is not set}+LOAD="$FIXTURES/shirudo-test-policy-labels.upatch"+UNLOAD="$FIXTURES/shirudo-test-policy-labels.unload.upatch"+PROBE=/run/shirudo/runtime/usr/bin/shirudo-policy-probe+WORK=/tmp/shirudo-policy-label-test+PIN=/run/shirudo/bpffs/upatches/policy-labels/link++restore()+{+ if [ -e "$PIN" ]; then+ "$SHIRUDO_BIN" upatch unload "$UNLOAD" --no-precheck \+ >/dev/null 2>&1 || true+ fi+ rm -rf "$WORK"+}+trap restore EXIT HUP INT TERM++[ -s "$LOAD" ] || test_skip_all "missing policy-label fixture"+[ -s "$UNLOAD" ] || test_skip_all "missing policy-label unload fixture"+[ -x "$PROBE" ] || test_skip_all "runtime policy probe is missing"+rm -rf "$WORK"+mkdir -p "$WORK"+printf '#!/bin/sh\nexit 0\n' >"$WORK/executable"+printf 'ordinary file\n' >"$WORK/ordinary"+chmod 0755 "$WORK/executable"+"$PROBE" set-policy "$WORK/ordinary" 0x8 ||+ test_skip_all "scratch filesystem cannot store policy xattrs"++test_expect_success 'upatch labels executable and ordinary files without clearing existing bits' '+ "$SHIRUDO_BIN" upatch load "$LOAD" --no-precheck >/dev/null 2>&1 &&+ python3 - "$WORK/executable" "$WORK/ordinary" <<'"'"'PY'"'"'+import os+import sys++def policy(path):+ value = os.getxattr(path, "security.bpf.shirudo.policy")+ assert len(value) == 8, (path, len(value))+ return int.from_bytes(value, byteorder=sys.byteorder)++assert policy(sys.argv[1]) == 0x1+assert policy(sys.argv[2]) == 0xe+PY+'++test_donediff --git a/testing/helper/test-upatch-prog-metadata.c b/testing/helper/test-upatch-prog-metadata.cindex f6aa00b9..8a368a95 100644--- a/testing/helper/test-upatch-prog-metadata.c+++ b/testing/helper/test-upatch-prog-metadata.c@@ -160,6 +160,43 @@ static int expect_uprobe_multi(const struct upatch *u,return cursor == end ? 0 : fail("trailing uprobe metadata");}+static int expect_policy_labels(const struct upatch *u)+{+ static const char *const policies[] = {+ "executable", "ordinary-a", "ordinary-b",+ };+ static const char *const paths[] = {+ "/tmp/shirudo-policy-label-test/executable",+ "/tmp/shirudo-policy-label-test/ordinary",+ "/tmp/shirudo-policy-label-test/ordinary",+ };+ static const __u64 bits[] = { 1, 2, 4 };+ struct shirudo_upatch_policy_label_entry entry;+ struct shirudo_upatch_policy_label_header header;+ const __u8 *raw, *cursor;+ __u32 len;+ size_t count = ARRAY_SIZE(bits);++ raw = upatch_signed_policy_labels(u, &len);+ if (!raw || len != sizeof(header) + count * sizeof(entry))+ return fail("policy label section length mismatch");+ memcpy(&header, raw, sizeof(header));+ if (le32toh(header.magic) != SHIRUDO_UPATCH_POLICY_LABELS_MAGIC ||+ le32toh(header.version) != SHIRUDO_UPATCH_POLICY_LABELS_VERSION ||+ le32toh(header.count) != count || header.reserved)+ return fail("policy label header mismatch");+ cursor = raw + sizeof(header);+ for (size_t i = 0; i < count; i++) {+ memcpy(&entry, cursor, sizeof(entry));+ cursor += sizeof(entry);+ if (le64toh(entry.bit) != bits[i] ||+ strcmp(entry.policy, policies[i]) ||+ strcmp(entry.path, paths[i]))+ return fail("policy label entry mismatch");+ }+ return cursor == raw + len ? 0 : fail("trailing policy label data");+}+int main(int argc, char **argv){struct upatch u;@@ -204,6 +241,8 @@ int main(int argc, char **argv)elseret = expect_uprobe_multi(&u, argv[3], argv[4],(__u32)count);+ } else if (strcmp(mode, "policy-labels") == 0 && argc == 3) {+ ret = expect_policy_labels(&u);} else {ret = fail("unknown metadata check mode");}diff --git a/testing/vm/build-bundle.sh b/testing/vm/build-bundle.shindex ee457d95..9f8cf665 100755--- a/testing/vm/build-bundle.sh+++ b/testing/vm/build-bundle.sh@@ -590,6 +590,32 @@ EOF)}+build_policy_labels_fixture()+{+ local source_dir="$FIXTURE_WORK/policy-labels"+ local package="$FIXTURE_WORK/policy-labels-1.0.0.bee"++ mkdir -p "$source_dir"+ cp "$REPO_ROOT/testing/fixtures/policy-labels/Beefile" \+ "$REPO_ROOT/testing/fixtures/policy-labels/policy-labels.bpf.c" \+ "$source_dir/"+ ln -s "$REPO_ROOT/vendor" "$source_dir/vendor"+ ln -s "$REPO_ROOT/bpf" "$source_dir/bpf"+ (+ cd "$FIXTURE_WORK"+ SHIRUDO_BUILD="$REPO_ROOT/tools/shirudo-build/shirudo-build" \+ "$REPO_ROOT/shirudo" bpm build "$source_dir" \+ --version 1.0.0 --keys-dir "$FIXTURE_WORK/trusted" \+ --vmlinux "$REPO_ROOT/vendor/linux/vmlinux.h"+ )+ tar -xOf "$package" \+ policy-labels-1.0.0/upatches/policy-labels.upatch \+ >"$TMP_DIR/fixtures/shirudo-test-policy-labels.upatch"+ tar -xOf "$package" \+ policy-labels-1.0.0/upatches/policy-labels.unload.upatch \+ >"$TMP_DIR/fixtures/shirudo-test-policy-labels.unload.upatch"+}+log "building target-test fixtures on the operator host"mkdir -p "$TMP_DIR/fixtures/decoders" "$FIXTURE_WORK/trusted" \"$FIXTURE_WORK/foreign" "$FIXTURE_WORK/own" "$FIXTURE_WORK/third"@@ -619,6 +645,7 @@ build_fixture scope-cgroup.bpf.c shirudo-test-scope-cgroup "$FIXTURE_WORK/trustebuild_fixture scope-kprobe.bpf.c shirudo-test-scope-kprobe "$FIXTURE_WORK/trusted"build_fixture basic.bpf.c shirudo-test-foreign "$FIXTURE_WORK/foreign"build_fixture basic.bpf.c shirudo-test-own-keyring "$FIXTURE_WORK/own"+build_policy_labels_fixturebuild_obs_fixture "$FIXTURE_WORK/trusted"basic_payload="$($REPO_ROOT/shirudo upatch info \