Skip to content

eBPF-based security platform

testing: verify kexec is refused

Daniel Borkmanne198db09c3da
diff --git a/contrib/build-kernel-package.sh b/contrib/build-kernel-package.sh
index b3d490e4..bdd5e069 100755
--- a/contrib/build-kernel-package.sh
+++ b/contrib/build-kernel-package.sh
@@ -460,6 +460,8 @@ CONFIG_NET_9P=y
CONFIG_NET_9P_VIRTIO=y
CONFIG_9P_FS=y
CONFIG_OVERLAY_FS=y
+CONFIG_KEXEC=y
+CONFIG_KEXEC_FILE=y
CONFIG_MISC_FILESYSTEMS=y
CONFIG_SQUASHFS=y
CONFIG_SQUASHFS_ZLIB=y
diff --git a/testing/run-target-tests.sh b/testing/run-target-tests.sh
index 6af6cbb5..4d78e123 100755
--- a/testing/run-target-tests.sh
+++ b/testing/run-target-tests.sh
@@ -81,6 +81,7 @@ for t in \
tamperproof/t1163-foreign-state-root.sh \
tamperproof/t1164-overlay-copy-up.sh \
tamperproof/t1165-tpm-identity.sh \
+ tamperproof/t1166-kexec.sh \
tamperproof/t1150-verity-devices.sh \
tamperproof/t1151-verity-process.sh \
tamperproof/t1153-verity-hidden.sh \
diff --git a/testing/tamperproof/t1166-kexec.py b/testing/tamperproof/t1166-kexec.py
new file mode 100755
index 00000000..328355ed
--- /dev/null
+++ b/testing/tamperproof/t1166-kexec.py
@@ -0,0 +1,37 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+#
+# One kexec load by each system call, nothing executed: kexec_load with
+# no segments, which unloads, and kexec_file_load of the given image
+# followed by its unload. Prints the errno of each, 0 for success.
+
+import ctypes
+import os
+import platform
+import sys
+
+NR = {
+ "x86_64": (246, 320),
+ "aarch64": (104, 294),
+ "riscv64": (104, 294),
+}
+KEXEC_FILE_UNLOAD = 0x1
+KEXEC_FILE_NO_INITRAMFS = 0x4
+
+libc = ctypes.CDLL(None, use_errno=True)
+nr_load, nr_file_load = NR[platform.machine()]
+
+ret = libc.syscall(nr_load, ctypes.c_ulong(0), ctypes.c_ulong(0), None,
+ ctypes.c_ulong(0))
+load_errno = ctypes.get_errno() if ret < 0 else 0
+
+fd = os.open(sys.argv[1], os.O_RDONLY)
+ret = libc.syscall(nr_file_load, fd, -1, ctypes.c_ulong(1), b"\0",
+ ctypes.c_ulong(KEXEC_FILE_NO_INITRAMFS))
+file_errno = ctypes.get_errno() if ret < 0 else 0
+if ret == 0:
+ libc.syscall(nr_file_load, -1, -1, ctypes.c_ulong(0), None,
+ ctypes.c_ulong(KEXEC_FILE_UNLOAD))
+os.close(fd)
+print(load_errno, file_errno)
diff --git a/testing/tamperproof/t1166-kexec.sh b/testing/tamperproof/t1166-kexec.sh
new file mode 100755
index 00000000..70c1d2f2
--- /dev/null
+++ b/testing/tamperproof/t1166-kexec.sh
@@ -0,0 +1,42 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+#
+# t1166 -- a kexec hands the machine to a kernel and an initrd of root's
+# choosing with the TPM as it is: PCRs 9 and 11 keep this boot's values,
+# the register keeps this boot's history, and the evidence of this boot
+# vouches for whatever runs after. Both load system calls must be
+# refused and counted as Kexec. The probe loads nothing it does not
+# unload again and executes nothing.
+
+cd "$(dirname "$0")/.."
+. ./integration-lib.sh
+target_guard
+command -v python3 >/dev/null 2>&1 || test_skip_all "no python3"
+IMAGE=
+for candidate in /boot/vmlinuz /boot/vmlinuz-*; do
+ if [ -f "$candidate" ]; then
+ IMAGE=$candidate
+ break
+ fi
+done
+[ -n "$IMAGE" ] || test_skip_all "no kernel image under /boot"
+EPERM=1
+ENOSYS=38
+set -- $(python3 tamperproof/t1166-kexec.py "$IMAGE")
+[ "$1" != "$ENOSYS" ] && [ "$2" != "$ENOSYS" ] ||
+ test_skip_all "the kernel has no kexec"
+
+counter() {
+ "$SHIRUDO_BIN" status --json 2>/dev/null |
+ python3 -c "import json,sys; print(json.load(sys.stdin)[\"tamper_attempts\"][\"counters\"][\"Kexec\"])"
+}
+
+test_expect_success 'kexec_load and kexec_file_load are refused and counted' '
+ before=$(counter) &&
+ set -- $(python3 tamperproof/t1166-kexec.py "$IMAGE") &&
+ [ "$1" = "$EPERM" ] && [ "$2" = "$EPERM" ] &&
+ [ "$(counter)" -ge $((before + 2)) ]
+'
+
+test_done