eBPF-based security platform
testing: check image bootstrap settings
KP Singh6708993d9f09
diff --git a/Makefile.shirudo b/Makefile.shirudoindex 707ece8f..f75d72e9 100644--- a/Makefile.shirudo+++ b/Makefile.shirudo@@ -793,10 +793,12 @@ testing/helper/test-image-keys: testing/helper/test-image-keys.c \$(Q)$(CC) $(CFLAGS) -o $@ $^ $(REGISTER_LIBS)testing/helper/test-image-config: testing/helper/test-image-config.c \- util/config.o util/image_config.o util/io.o util/log.o util/malloc.o util/hex.o \+ util/image_bootstrap.o util/hex.o util/config_measure.o \+ util/crypto_openssl.o util/config.o util/image_config.o \+ util/io.o util/log.o util/malloc.o \util/string.o@printf ' CC $@\n'- $(Q)$(CC) $(CFLAGS) -o $@ $^+ $(Q)$(CC) $(CFLAGS) -o $@ $^ $(LIBCRYPTO_LIBS) $(LIBSSL_LIBS)testing/helper/test-image-cli: testing/helper/test-image-cli.c builtin-bpm.c \$(filter-out $(call obj,builtin-bpm.o),$(BUILTIN_OBJS)) \diff --git a/testing/helper/test-image-config.c b/testing/helper/test-image-config.cindex 9db18397..7fe5baf7 100644--- a/testing/helper/test-image-config.c+++ b/testing/helper/test-image-config.c@@ -9,6 +9,27 @@#include "util/config.h"#include "util/image_config.h"+#include "util/image_bootstrap.h"+#include "util/hex.h"++static int read_bootstrap(int argc, char **argv)+{+ struct shirudo_image_bootstrap boot = {};+ char root[65];+ int rc;++ rc = shirudo_image_bootstrap_read(argv[2], &boot, true);+ if (!rc && argc == 4)+ rc = shirudo_image_bootstrap_read(argv[3], &boot, false);+ if (!rc) {+ hex_encode(boot.runtime_root, 32, root);+ printf("runtime=%s\nroot=%s\nconfig=%s\ncertificate=%s\npcr=%u\n",+ boot.runtime, root, boot.config, boot.certificate,+ boot.pcr);+ }+ printf("rc=%d\n", rc);+ return rc ? 1 : 0;+}int main(int argc, char **argv){@@ -18,6 +39,8 @@ int main(int argc, char **argv)if (argc < 3 || argc > 4)return 2;+ if (!strcmp(argv[1], "bootstrap"))+ return read_bootstrap(argc, argv);fd = open(argv[2], O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);if (fd < 0)return 1;diff --git a/testing/t0054-image-bootstrap.sh b/testing/t0054-image-bootstrap.shnew file mode 100755index 00000000..f0d40191--- /dev/null+++ b/testing/t0054-image-bootstrap.sh@@ -0,0 +1,93 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo++cd "$(dirname "$0")"+. ./test-lib.sh++H=${SHIRUDO_TEST_IMAGE_CONFIG_HELPER:-./helper/test-image-config}+[ -x "$H" ] || test_skip_all "test-image-config helper not built"+WORK=$(mktemp -d)+trap 'rm -rf "$WORK"' EXIT HUP INT TERM++root=0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef+printf "runtime.image = /var/lib/shirudo/runtime\nruntime.root_hash = %s\n" \+ "$root" >"$WORK/initrd.conf"+printf "config.image = /var/lib/shirudo/images/initial\nconfig.pcr = 15\n" \+ >"$WORK/runtime.conf"+printf "config.verification_key = /run/shirudo/runtime/etc/shirudo/config-cert.der\n" \+ >>"$WORK/runtime.conf"++test_expect_success 'runtime settings preserve the root authenticated by the initrd' '+ "$H" bootstrap "$WORK/initrd.conf" "$WORK/runtime.conf" >"$WORK/result" &&+ grep -qx "runtime=/var/lib/shirudo/runtime" "$WORK/result" &&+ grep -qx "root=$root" "$WORK/result" &&+ grep -qx "config=/var/lib/shirudo/images/initial" "$WORK/result" &&+ grep -qx "certificate=/run/shirudo/runtime/etc/shirudo/config-cert.der" "$WORK/result" &&+ grep -qx "pcr=15" "$WORK/result"+'++test_expect_success 'runtime settings cannot replace the executable image or its root' '+ for setting in "runtime.image = /var/lib/shirudo/other" \+ "runtime.root_hash = $root"; do+ printf "%s\n" "$setting" >"$WORK/bad.conf" &&+ ! "$H" bootstrap "$WORK/initrd.conf" "$WORK/bad.conf" || exit 1+ done+'++test_expect_success 'duplicate runtime paths and hashes are rejected in the initrd' '+ for setting in "runtime.image = /var/lib/shirudo/other" \+ "runtime.root_hash = $root"; do+ cp "$WORK/initrd.conf" "$WORK/bad.conf" &&+ printf "%s\n" "$setting" >>"$WORK/bad.conf" &&+ ! "$H" bootstrap "$WORK/bad.conf" || exit 1+ done+'++test_expect_success 'image paths reject relative paths and parent traversal' '+ for path in relative /var/../outside /var/./image /var//image /var/.. /; do+ for key in runtime.image config.image; do+ printf "%s = %s\n" "$key" "$path" >"$WORK/bad.conf" &&+ ! "$H" bootstrap "$WORK/bad.conf" || exit 1+ done+ done+'++test_expect_success 'config verification certificates must come from the runtime image' '+ for path in /etc/cert.der /run/shirudo/runtime-other/cert.der \+ /run/shirudo/runtime/../cert.der; do+ printf "config.verification_key = %s\n" "$path" >"$WORK/bad.conf" &&+ ! "$H" bootstrap "$WORK/initrd.conf" "$WORK/bad.conf" || exit 1+ done+'++test_expect_success 'config PCR rejects boot PCRs and resettable PCRs' '+ for pcr in 9 11 16 23 24 -1 invalid 99999999999999999999999999; do+ printf "config.pcr = %s\n" "$pcr" >"$WORK/bad.conf" &&+ ! "$H" bootstrap "$WORK/initrd.conf" "$WORK/bad.conf" || exit 1+ done+'++test_expect_success 'unknown image settings and malformed lines are rejected' '+ for setting in "runtime.unknown = value" "config.unknown = value" "missing equals"; do+ printf "%s\n" "$setting" >"$WORK/bad.conf" &&+ ! "$H" bootstrap "$WORK/bad.conf" || exit 1+ done+'++test_expect_success 'malformed hashes and oversized lines are rejected' '+ for hash in short "${root}00" "${root%?}x"; do+ printf "runtime.root_hash = %s\n" "$hash" >"$WORK/bad.conf" &&+ ! "$H" bootstrap "$WORK/bad.conf" || exit 1+ done &&+ python3 -c "print(\"#\" + \"x\" * 5000)" >"$WORK/bad.conf" &&+ ! "$H" bootstrap "$WORK/bad.conf"+'++test_expect_success 'unrelated settings remain available to the regular config reader' '+ printf "# comment\neventlog.max_bytes = 12345\n\n" >"$WORK/other.conf" &&+ "$H" bootstrap "$WORK/initrd.conf" "$WORK/other.conf" >"$WORK/result" &&+ grep -qx "root=$root" "$WORK/result"+'++test_done