eBPF-based security platform
testing: reject config classification without the expected root
KP Singh90e14bd30db5
diff --git a/Makefile.shirudo b/Makefile.shirudoindex f75d72e9..4c000288 100644--- a/Makefile.shirudo+++ b/Makefile.shirudo@@ -580,6 +580,7 @@ build/verity-tests/shirudo-image-fault: testing/helper/image-fault.c \$(Q)$(CC) $(CFLAGS) $(LDFLAGS) $(RPATH_LDFLAGS) -o $@ \$(filter-out %.h,$^) \-Wl,--wrap=main,--wrap=Esys_PCR_Extend \+ -Wl,--wrap=shirudo_config_root_begin,--wrap=shirudo_config_root_end \$(STATIC_LIBS) $(VERITY_TEST_LIBS)verity-test-helpers: $(VERITY_TEST_HELPERS) build/verity-tests/shirudo-image-faultdiff --git a/testing/helper/image-fault.c b/testing/helper/image-fault.cindex 75a16041..7236306e 100644--- a/testing/helper/image-fault.c+++ b/testing/helper/image-fault.c@@ -1,14 +1,201 @@// SPDX-License-Identifier: BSD-2-Clause/* Copyright (c) 2026 Authors of shirudo */+#include <shirudo/misc.h>++#include <errno.h>+#include <fcntl.h>+#include <linux/fs.h>#include <stdio.h>#include <string.h>+#include <sys/ioctl.h>+#include <sys/syscall.h>+#include <sys/sysmacros.h>#include <unistd.h>+#include <bpf/bpf.h>+#include "image-fault.h"+#include "util/config_image.h"+#include "util/dm_verity.h"+#include "util/identity.h"+#include "util/paths.h"++enum root_test_mode {+ ROOT_NORMAL,+ ROOT_MISSING,+ ROOT_WRONG,+ ROOT_MATCH,+};++static enum root_test_mode root_mode;+static __u32 begin_calls, end_calls;+static int consumed_valid = -1;++int __wrap_shirudo_config_root_begin(const __u8 root[32],+ struct shirudo_config_root_guard *guard)+{+ __u8 wrong[32];++ if (root_mode == ROOT_NORMAL)+ return __real_shirudo_config_root_begin(root, guard);+ begin_calls++;+ if (root_mode == ROOT_MISSING) {+ guard->map_fd = -1;+ guard->task_fd = -1;+ return 0;+ }+ memcpy(wrong, root, sizeof(wrong));+ if (root_mode == ROOT_WRONG)+ wrong[0] ^= 1;+ return __real_shirudo_config_root_begin(wrong, guard);+}++int __wrap_shirudo_config_root_end(struct shirudo_config_root_guard *guard)+{+ struct shirudo_expected_root expected = {};+ int rc = 0, cleanup;++ if (root_mode != ROOT_NORMAL) {+ end_calls++;+ if (guard->map_fd >= 0 &&+ bpf_map_lookup_elem(guard->map_fd, &guard->task_fd,+ &expected))+ rc = -errno;+ consumed_valid = expected.valid;+ }+ cleanup = __real_shirudo_config_root_end(guard);+ return rc ? rc : cleanup;+}++static int check_root_cleared(void)+{+ struct shirudo_expected_root expected = {}, empty = {};+ int map, task, rc;++ map = bpf_obj_get(SHIRUDO_BPFFS_DIR "/config_roots");+ if (map < 0)+ return -errno;+ task = syscall(SYS_pidfd_open, getpid(), 0);+ if (task < 0) {+ rc = -errno;+ goto out;+ }+ rc = bpf_map_lookup_elem(map, &task, &expected) ? -errno : 0;+ if (root_mode == ROOT_MISSING && rc == -ENOENT)+ rc = 0;+ if (!rc && memcmp(&expected, &empty, sizeof(empty)))+ rc = -EUCLEAN;+ close(task);+out:+ close(map);+ return rc;+}++static int check_classification(const struct shirudo_dm_verity *device)+{+ __u32 dev = (major(device->device) << 20) | minor(device->device);+ struct shirudo_device_authority entry;+ __u64 key, next, diskseq;+ __u32 matches = 0;+ int map, block, rc = 0;+ void *cursor = NULL;++ block = open(device->path, O_RDONLY | O_CLOEXEC | O_NOFOLLOW);+ if (block < 0)+ return -errno;+ if (ioctl(block, BLKGETDISKSEQ, &diskseq)) {+ rc = -errno;+ goto out_block;+ }+ map = bpf_obj_get(SHIRUDO_BPFFS_DIR "/devices");+ if (map < 0) {+ rc = -errno;+ goto out_block;+ }+ while (!bpf_map_get_next_key(map, cursor, &next)) {+ if (bpf_map_lookup_elem(map, &next, &entry)) {+ rc = -errno;+ break;+ }+ if (entry.dev == dev)+ printf("device=%u diskseq=%llu record_diskseq=%llu "+ "kind=%u root_match=%d\n", dev,+ (__u64)diskseq,+ (__u64)entry.diskseq, entry.kind,+ !memcmp(entry.root, device->image.root_hash,+ sizeof(entry.root)));+ if (entry.dev == dev && entry.diskseq == diskseq) {+ matches++;+ if (entry.kind != SHIRUDO_DEVICE_CONFIG ||+ memcmp(entry.root, device->image.root_hash, 32)) {+ rc = -EUCLEAN;+ break;+ }+ }+ key = next;+ cursor = &key;+ }+ if (!rc && errno != ENOENT)+ rc = -errno;+ if (!rc && matches != (root_mode == ROOT_MATCH ? 1u : 0u))+ rc = -EACCES;+ printf("classified=%u consumed_valid=%d\n", matches, consumed_valid);+ close(map);+out_block:+ close(block);+ return rc;+}++static int config_root_test(const char *mode, const char *prefix)+{+ struct shirudo_config_image_open opened;+ struct shirudo_dm_verity device;+ bool created = false;+ int rc, cleanup;+ char name[128];++ if (!strcmp(mode, "missing"))+ root_mode = ROOT_MISSING;+ else if (!strcmp(mode, "wrong"))+ root_mode = ROOT_WRONG;+ else if (!strcmp(mode, "match"))+ root_mode = ROOT_MATCH;+ else+ return 2;+ rc = shirudo_config_image_open(prefix, &opened);+ if (rc)+ goto done;+ snprintf(name, sizeof(name), "shirudo-verify-root-test-%ld",+ (long)getpid());+ rc = shirudo_dm_verity_create(&opened, name, &device);+ created = !rc;+ if (!rc && (begin_calls != 1 || end_calls != 1 ||+ consumed_valid != (root_mode == ROOT_WRONG ? 1 : 0)))+ rc = -EUCLEAN;+ if (!rc)+ rc = check_root_cleared();+ if (!rc)+ rc = shirudo_dm_verity_verify(&device, &opened);+ if (!rc)+ rc = check_classification(&device);+ if (created) {+ cleanup = shirudo_dm_verity_remove(&device, true);+ shirudo_dm_verity_close(&device);+ if (!rc)+ rc = cleanup;+ }+ shirudo_config_image_close(&opened);+done:+ if (rc)+ fprintf(stderr, "config root test: %s\n", strerror(-rc));+ return rc ? 1 : 0;+}int __wrap_main(int argc, const char **argv){+ if (argc == 4 && !strcmp(argv[1], "config-root"))+ return config_root_test(argv[2], argv[3]);if (argc != 4 || strcmp(argv[1], "image") ||strcmp(argv[2], "update")) {fprintf(stderr, "usage: shirudo-image-fault "diff --git a/testing/helper/image-fault.h b/testing/helper/image-fault.hindex cbeb3147..8d625ed2 100644--- a/testing/helper/image-fault.h+++ b/testing/helper/image-fault.h@@ -4,8 +4,19 @@#ifndef SHIRUDO_TEST_IMAGE_FAULT_H#define SHIRUDO_TEST_IMAGE_FAULT_H+#include <shirudo/misc.h>+#include <tss2/tss2_esys.h>+#include "util/image_guards.h"++int __real_shirudo_config_root_begin(const __u8 root[32],+ struct shirudo_config_root_guard *guard);+int __wrap_shirudo_config_root_begin(const __u8 root[32],+ struct shirudo_config_root_guard *guard);+int __real_shirudo_config_root_end(struct shirudo_config_root_guard *guard);+int __wrap_shirudo_config_root_end(struct shirudo_config_root_guard *guard);+int __real_main(int argc, const char **argv);int __wrap_main(int argc, const char **argv);TSS2_RC __real_Esys_PCR_Extend(ESYS_CONTEXT *context, ESYS_TR pcr,diff --git a/testing/tamperproof/t1154-verity-config.py b/testing/tamperproof/t1154-verity-config.pyindex b03e68f6..d9944ca8 100644--- a/testing/tamperproof/t1154-verity-config.py+++ b/testing/tamperproof/t1154-verity-config.py@@ -43,6 +43,23 @@ def run(label, *arguments, success=True):return result.stdout+def fault_command():+ runtime = RUNTIME+ directories = []+ for directory, _, files in os.walk(runtime, followlinks=False):+ if any(name.endswith(".so") or ".so." in name for name in files):+ directories.append(directory)+ library_path = ":".join(sorted(directories))+ for name in ("lib64/ld-linux-x86-64.so.2", "lib/ld-linux-aarch64.so.1",+ "lib/ld-linux-riscv64-lp64d.so.1"):+ interpreter = runtime / name+ if interpreter.is_file():+ return [str(interpreter), "--inhibit-cache", "--library-path",+ library_path, str(runtime / "usr/bin/shirudo-image-fault")]+ raise AssertionError("runtime ELF interpreter missing")+++def root(name):data = (IMAGES / (name + ".roothash")).read_bytes()require(len(data) == 64 and all(c in b"0123456789abcdef" for c in data),@@ -192,6 +209,18 @@ def main():(RESULTS / "anchor.json").unlink(missing_ok=True)require(root("config-a") != root("config-b"), "A/B fixtures are identical")snapshot("initial")+ elif mode in ("root-missing", "root-wrong", "root-match"):+ before = snapshot(mode + "-before")+ result = subprocess.run(fault_command() + ["config-root", mode[5:],+ str(IMAGES / "config-a")],+ capture_output=True, text=True, timeout=120)+ (RESULTS / (mode + ".stdout")).write_text(result.stdout)+ (RESULTS / (mode + ".stderr")).write_text(result.stderr)+ require(result.returncode == 0,+ f"root fixture failed: {result.returncode}: {result.stderr}")+ after = snapshot(mode + "-after")+ differences = [key for key in before if before[key] != after[key]]+ require(not differences, f"{mode} changed state: {differences}")elif mode == "builtin":before = snapshot("builtin-before")public = Path(os.environ["SHIRUDO_TEST_KEY_FIXTURES"])diff --git a/testing/tamperproof/t1154-verity-config.sh b/testing/tamperproof/t1154-verity-config.shindex d69dd463..517d810e 100755--- a/testing/tamperproof/t1154-verity-config.sh+++ b/testing/tamperproof/t1154-verity-config.sh@@ -34,6 +34,18 @@ test_expect_success 'current CONFIG and PCR evidence verify' 'python3 tamperproof/t1154-verity-config.py initial "$work"'+test_expect_success 'trusted caller without a root nomination gets no CONFIG classification' '+ python3 tamperproof/t1154-verity-config.py root-missing "$work"+'++test_expect_success 'trusted caller with a wrong root nomination gets no CONFIG classification' '+ python3 tamperproof/t1154-verity-config.py root-wrong "$work"+'++test_expect_success 'exact root nomination classifies CONFIG once and is cleared' '+ python3 tamperproof/t1154-verity-config.py root-match "$work"+'+if [ -f "$SHIRUDO_TEST_KEY_FIXTURES/builtin.der" ]; thentest_expect_success 'kernel built-in certificate accepts a signed config root' 'python3 tamperproof/t1154-verity-config.py builtin "$work"diff --git a/testing/tamperproof/t1158-verity-interruption.py b/testing/tamperproof/t1158-verity-interruption.pyindex 74b3aeff..8a96fd6a 100755--- a/testing/tamperproof/t1158-verity-interruption.py+++ b/testing/tamperproof/t1158-verity-interruption.py@@ -5,7 +5,6 @@import hashlibimport importlib.utilimport json-import osfrom pathlib import Pathimport reimport struct@@ -19,22 +18,6 @@ spec.loader.exec_module(config)RESULTS = config.RESULTS-def fault_command():- runtime = config.RUNTIME- directories = []- for directory, _, files in os.walk(runtime, followlinks=False):- if any(name.endswith(".so") or ".so." in name for name in files):- directories.append(directory)- library_path = ":".join(sorted(directories))- for name in ("lib64/ld-linux-x86-64.so.2", "lib/ld-linux-aarch64.so.1",- "lib/ld-linux-riscv64-lp64d.so.1"):- interpreter = runtime / name- if interpreter.is_file():- return [str(interpreter), "--inhibit-cache", "--library-path",- library_path, str(runtime / "usr/bin/shirudo-image-fault")]- raise AssertionError("runtime ELF interpreter missing")--def pcr(label):result = subprocess.run(["tpm2_pcrread", "sha256:14"], capture_output=True,text=True, timeout=30)@@ -57,7 +40,7 @@ def main():digest = hashlib.sha256(b"shirudo-config-root-v1\0" + struct.pack(">I", 2) +bytes.fromhex(config.root(target))).digest()expected = hashlib.sha256(bytes.fromhex(before["pcr"]) + digest).hexdigest()- result = subprocess.run(fault_command() + ["image", "update",+ result = subprocess.run(config.fault_command() + ["image", "update",str(config.IMAGES / target)],capture_output=True, text=True, timeout=120)(RESULTS / "crash.stdout").write_text(result.stdout)