eBPF-based security platform
testing: check the BPF objects selected for each mode
KP Singh076f2727386f
diff --git a/Makefile.shirudo b/Makefile.shirudoindex a4e66e6b..aa82b364 100644--- a/Makefile.shirudo+++ b/Makefile.shirudo@@ -586,6 +586,7 @@ verity-test-helpers: $(VERITY_TEST_HELPERS) build/verity-tests/shirudo-image-fauTEST_HELPERS := $(VERITY_TEST_HELPERS) \testing/helper/test-image-cli \testing/helper/test-image-cleanup \+ testing/helper/test-mode-objects \testing/helper/test-image-config \testing/helper/test-config-image \testing/helper/test-config-update \@@ -814,6 +815,11 @@ testing/helper/test-image-cleanup: testing/helper/test-image-cleanup.c \-Wl,--wrap=shirudo_dm_verity_remove,--wrap=usleep \$(STATIC_LIBS) $(DYNAMIC_LIBS)+testing/helper/test-mode-objects: testing/helper/test-mode-objects.c \+ $(call obj,util/loader.o) $(LIBBPF_A)+ @printf ' CC $@\n'+ $(Q)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ -lelf -lz+test-helpers: $(TEST_HELPERS)# ----- fuzzing ----------------------------------------------------diff --git a/testing/helper/test-mode-objects.c b/testing/helper/test-mode-objects.cnew file mode 100644index 00000000..1e09b1b6--- /dev/null+++ b/testing/helper/test-mode-objects.c@@ -0,0 +1,88 @@+// SPDX-License-Identifier: BSD-2-Clause+/* Copyright (c) 2026 Authors of shirudo */++#include <gelf.h>+#include <stdbool.h>+#include <stdio.h>++#include <bpf/libbpf.h>++#include "util/loader.h"++static int symbols(const char *selection,+ const struct shirudo_embedded_bpf *blob)+{+ Elf_Scn *section = NULL;+ Elf *elf;+ int rc = 0;++ if (elf_version(EV_CURRENT) == EV_NONE)+ return 1;+ elf = elf_memory((char *)blob->data, blob->size);+ if (!elf)+ return 1;+ while ((section = elf_nextscn(elf, section))) {+ GElf_Shdr header;+ Elf_Data *data;+ size_t i;++ if (!gelf_getshdr(section, &header)) {+ rc = 1;+ break;+ }+ if (header.sh_type != SHT_SYMTAB)+ continue;+ data = elf_getdata(section, NULL);+ if (!data || !header.sh_entsize) {+ rc = 1;+ break;+ }+ for (i = 1; i < header.sh_size / header.sh_entsize; i++) {+ GElf_Sym symbol;+ const char *name;++ if (!gelf_getsym(data, i, &symbol)) {+ rc = 1;+ break;+ }+ if (symbol.st_shndx != SHN_UNDEF)+ continue;+ name = elf_strptr(elf, header.sh_link, symbol.st_name);+ if (!name) {+ rc = 1;+ break;+ }+ printf("extern %s %s\n", selection, name);+ }+ }+ elf_end(elf);+ return rc;+}++static int inspect(const char *selection,+ const struct shirudo_embedded_bpf *blob)+{+ struct bpf_program *program;+ struct bpf_object *obj;+ struct bpf_map *map;++ obj = bpf_object__open_mem(blob->data, blob->size, NULL);+ if (!obj)+ return 1;+ printf("blob %s %s\n", selection, blob->name);+ bpf_object__for_each_program(program, obj)+ printf("program %s %s\n", selection,+ bpf_program__name(program));+ bpf_object__for_each_map(map, obj)+ printf("map %s %s\n", selection, bpf_map__name(map));+ bpf_object__close(obj);+ return symbols(selection, blob);+}++int main(void)+{+ return inspect("ordinary-admission", bpf2_blob(false)) ||+ inspect("image-admission", bpf2_blob(true)) ||+ inspect("ordinary-seal", seal_blob(false)) ||+ inspect("image-seal", seal_blob(true));+}diff --git a/testing/run-target-tests.sh b/testing/run-target-tests.shindex 24f29c82..298a98a7 100755--- a/testing/run-target-tests.sh+++ b/testing/run-target-tests.sh@@ -25,6 +25,7 @@ esacfail=0: "${SHIRUDO_TEST_SUITE_FAILURES_FILE:=}"for t in \+ tamperproof/t1138-mode-objects.sh \t1200-target-fixtures.sh \bpm/t1118-bpm-tree.sh \bpm/t1119-bpm-tree-sequential.sh \diff --git a/testing/run-target-verity-tests.sh b/testing/run-target-verity-tests.shindex 205a1001..5e517f51 100755--- a/testing/run-target-verity-tests.sh+++ b/testing/run-target-verity-tests.sh@@ -38,6 +38,7 @@ fail=0# Boot assertions precede updates; the interruption leaves state pending.for t in \tamperproof/t1155-verity-boot-signers.sh \+ tamperproof/t1138-mode-objects.sh \t1200-target-fixtures.sh \t1103-version-gate.sh \t1104-payload-mismatch.sh \diff --git a/testing/t0053-mode-objects.sh b/testing/t0053-mode-objects.shnew file mode 100755index 00000000..7ba5e932--- /dev/null+++ b/testing/t0053-mode-objects.sh@@ -0,0 +1,46 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo++cd "$(dirname "$0")"+. ./test-lib.sh++H=${SHIRUDO_TEST_MODE_OBJECTS_HELPER:-./helper/test-mode-objects}+W=$(mktemp)+trap 'rm -f "$W"' EXIT HUP INT TERM++test_expect_success 'embedded BPF selection keeps image hooks and maps out of ordinary mode' '+ "$H" >"$W" && python3 - "$W" <<"PYTEST"+import sys+from collections import defaultdict++inventory = defaultdict(lambda: defaultdict(set))+for line in open(sys.argv[1]):+ kind, selection, name = line.split()+ inventory[selection][kind].add(name)+extra = {+ "admission": {"shirudo_lsm_file_ioctl",+ "shirudo_lsm_bdev_setintegrity",+ "shirudo_lsm_bdev_free_security"},+ "seal": {"shirudo_seal_mmap_file", "shirudo_seal_file_mprotect"},+}+common_maps = {"shirudo_bpffs_sb", "shirudo_tasks", "shirudo_tamper"}+for kind, blob in (("admission", "bpf2"), ("seal", "seal")):+ ordinary = inventory["ordinary-" + kind]+ image = inventory["image-" + kind]+ assert ordinary["blob"] == {blob}, ordinary+ assert image["blob"] == {blob + "_verity"}, image+ assert ordinary["program"], ordinary+ assert image["program"] == ordinary["program"] | extra[kind], inventory+ assert ordinary["program"].isdisjoint(extra[kind]), ordinary+ assert common_maps <= ordinary["map"], ordinary+ image_maps = {"shirudo_devices", "shirudo_roots"}+ assert common_maps | image_maps <= image["map"], image+ assert image_maps.isdisjoint(ordinary["map"]), ordinary+ assert "bpf_real_data_inode" not in ordinary["extern"], ordinary+ if kind == "seal":+ assert "bpf_real_data_inode" in image["extern"], image+PYTEST+'++test_donediff --git a/testing/tamperproof/t1138-mode-objects.sh b/testing/tamperproof/t1138-mode-objects.shnew file mode 100755index 00000000..cf683121--- /dev/null+++ b/testing/tamperproof/t1138-mode-objects.sh@@ -0,0 +1,24 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo++cd "$(dirname "$0")/.."+. ./integration-lib.sh+target_guard++mode_pins() {+ for pin in devices config_roots file_ioctl_link \+ bdev_integrity_link bdev_free_link seal_mmap_link seal_mprotect_link; do+ case "${SHIRUDO_TEST_MODE:-legacy}" in+ verity) [ -e "/run/shirudo/bpffs/$pin" ] || return 1 ;;+ legacy) [ ! -e "/run/shirudo/bpffs/$pin" ] || return 1 ;;+ *) return 1 ;;+ esac+ done+}++test_expect_success 'only image mode pins image device and executable guards' '+ mode_pins+'++test_done