eBPF-based security platform
init: refuse a kernel that loads modules without checking signatures
Daniel Borkmann4143f6337ecf
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rstindex 84487067..296f08a0 100644--- a/Documentation/contribute/testing/proof-coverage.rst+++ b/Documentation/contribute/testing/proof-coverage.rst@@ -1573,7 +1573,8 @@ a proof covers.:src:`util/image_guards.c`, :src:`util/image_keys.c`,:src:`util/image_runtime.c`, :src:`util/initrd.c`,:src:`util/integrity.c`, :src:`util/java_agent.c`,- :src:`util/keyring.c`, :src:`util/preflight.c`, :src:`util/seal.c`,+ :src:`util/kernel_prereq.c`, :src:`util/keyring.c`,+ :src:`util/preflight.c`, :src:`util/seal.c`,:src:`util/tamper.c`, :src:`util/upatch_maps.c`,:src:`util/upatch_pins.c`, :src:`util/upatch_resolve.c`,:src:`util/upatch_verify.c`diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex a1331ca0..d70957d6 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -568,6 +568,9 @@ signed config image.- A command writes past the file size limit it was started with,outlives an interval timer set before its exec, and refusesanother task's ``prlimit``, counted as ``SealRlimit``+ * - ``t6157-kernel-options``+ - The test kernel, which signs its modules, passes the pre-flight+ check of module signatures``bpm`` (7)~~~~~~~~~~~@@ -844,14 +847,14 @@ signed config image.Unit suites and fuzz targets~~~~~~~~~~~~~~~~~~~~~~~~~~~~-The unit suites in :src:`testing/unit/` cover 35 of the modules in+The unit suites in :src:`testing/unit/` cover 36 of the modules in:src:`util/`: ``attest``, ``bpm_oci``, ``bpm_package``, ``bpm_state``,``bpm_tree``, ``bpm_tree_overlap``, ``bpm_tree_state``, ``chain``,``chain_history``, ``conditions``, ``config_update``, ``crypto_openssl``,``eventlog``, ``format``, ``hex``, ``io``, ``java_agent``, ``json``,``kernel_abi``,``keepalive``, ``keepalive_quote``, ``kernel_build_id``,-``kernel_release``, ``keys``, ``measured``, ``options``,+``kernel_prereq``, ``kernel_release``, ``keys``, ``measured``, ``options``,``paths``, ``string``, ``sysinfo``, ``upatch``, ``upatch_decode``,``upatch_pins``, ``upatch_resolve``, ``upatch_verify`` and ``xdp``, and``overflow`` the arithmetic of@@ -1018,7 +1021,7 @@ Coverage by area``eventlog``. Unit: ``eventlog``, ``io``* - Boot and initrd- ``t1006``, ``t4002``, ``t4101``, ``t5006``, ``t6115``,- ``t6126``+ ``t6126``, ``t6157``. Unit: ``kernel_prereq``* - Config images- ``t4004``, ``t5001`` to ``t5008``, ``t5101`` to ``t5105``,``t6112``, ``t6127``, ``t6130``, ``t6132``. Fuzz: ``config``,diff --git a/Documentation/internals/boot-flow.rst b/Documentation/internals/boot-flow.rstindex 9b2e2631..acc041fb 100644--- a/Documentation/internals/boot-flow.rst+++ b/Documentation/internals/boot-flow.rst@@ -50,7 +50,8 @@ the switch to the root file system. It needs``/etc/shirudo/shirudo.conf``. ``runtime.image`` and``runtime.root_hash`` must be present; the UKI authenticates both.#. Run the pre-flight: root, the BPF LSM active, bpffs, the signed- loader ABI, the certificates readable, ML-DSA-87 and accepted by the+ loader ABI and LSM hooks, module signatures in a kernel that loads+ modules, the certificates readable, ML-DSA-87 and accepted by thekernel, the BPF keyring present, the embedded objects present, theTPM reachable. A failed check ends ``init``.#. Check that ``/`` is an initramfs and binddiff --git a/Documentation/internals/kernel-interfaces.rst b/Documentation/internals/kernel-interfaces.rstindex 64adfe27..34954cee 100644--- a/Documentation/internals/kernel-interfaces.rst+++ b/Documentation/internals/kernel-interfaces.rst@@ -573,6 +573,14 @@ signature against the runtime image's certificates first; seeis handled: the seal then sees no seccomp state. The attach kinds ofupatches can need further options; see :doc:`/upatches/write/attach-kinds`.+The pre-flight check ``module_signatures`` refuses a kernel that builds+modules without ``CONFIG_MODULE_SIG``, and tells it from the running+kernel, since a target need not have ``/proc/config.gz``: from a BTF+function only the option builds, ``__module_address`` for+``CONFIG_MODULES`` and ``mod_verify_sig`` for ``CONFIG_MODULE_SIG``.+Without ``CONFIG_MODULE_SIG``, ``module_sig_check()`` is a stub that+asks no LSM about an unsigned module.+The kernel project configures the test kernel with these options and theadditional options the test fixtures and VM need.diff --git a/Documentation/operate/deploy/initialize.rst b/Documentation/operate/deploy/initialize.rstindex c74770dd..31cf862a 100644--- a/Documentation/operate/deploy/initialize.rst+++ b/Documentation/operate/deploy/initialize.rst@@ -202,13 +202,14 @@ It changes no persistent or enforcement state:OK bpffs_supportedOK signed_loader_kernel_abiOK kernel_lsm_hooks+ OK module_signaturesOK cert_on_diskOK cert_algorithmOK cert_kernel_supportOK kernel_keyringOK embedded_loaderOK tpm2_reachable: TCTI from the config: device:/dev/tpmrm0- 11 of 11 passed+ 12 of 12 passed:doc:`/reference/cli/init` describes each check.diff --git a/Documentation/operate/plan/hardening.rst b/Documentation/operate/plan/hardening.rstindex 7889f13b..462f6c6e 100644--- a/Documentation/operate/plan/hardening.rst+++ b/Documentation/operate/plan/hardening.rst@@ -48,14 +48,16 @@ Kernel configurationwould not load.- no* - Module signatures: ``CONFIG_MODULE_SIG``, with every module the- target loads signed by a key the kernel trusts+ target loads signed by a key the kernel trusts, or+ ``CONFIG_MODULES=n``- Without it the kernel loads a module without asking lockdown, andthe module runs in the kernel with no BPF involved. With it,``bpf2`` refuses an unsigned module from ``init`` on where thekernel does not refuse it first (``CONFIG_MODULE_SIG_FORCE`` or``module.sig_enforce=1``).- - partly: from ``init`` on, an unsigned module is refused and- counted as ``ModuleSignature``+ - init fails: pre-flight ``module_signatures``. From ``init`` on,+ an unsigned module is refused and counted as+ ``ModuleSignature``. The keys the kernel trusts are not checked* - ``CONFIG_STRICT_DEVMEM`` and ``CONFIG_IO_STRICT_DEVMEM``, or no``/dev/mem`` at all- Limits ``/dev/mem`` before ``init``, after which ``bpf2`` refusesdiff --git a/Documentation/operate/plan/interoperability.rst b/Documentation/operate/plan/interoperability.rstindex a91f65bd..ef24706a 100644--- a/Documentation/operate/plan/interoperability.rst+++ b/Documentation/operate/plan/interoperability.rst@@ -291,7 +291,8 @@ Module loading and kernel live patchingit. ``modprobe --force``, which strips the version checks, makes asigned module count as unsigned.:What to do: Build the kernel with ``CONFIG_MODULE_SIG``, as- :doc:`/operate/plan/hardening` lists, and sign every module the+ :doc:`/operate/plan/hardening` lists and the pre-flight check+ ``module_signatures`` requires, and sign every module thetarget loads, livepatch modules included, with a key the kerneltrusts. A signed module runs as kernel code outside every shirudocheck. Unloading adiff --git a/Documentation/operate/plan/system-requirements.rst b/Documentation/operate/plan/system-requirements.rstindex d44ff4c2..18f559c6 100644--- a/Documentation/operate/plan/system-requirements.rst+++ b/Documentation/operate/plan/system-requirements.rst@@ -202,11 +202,16 @@ these options and additional ones that the tests use.* - ``CONFIG_TMPFS``, ``CONFIG_TMPFS_XATTR``- ``/run/shirudo`` carries the same label.+``shirudo init`` also refuses a kernel that builds modules+(``CONFIG_MODULES``) without ``CONFIG_MODULE_SIG``: it loads an unsigned+module without asking the LSM. The pre-flight check+``module_signatures`` tells that from the kernel's BTF, not from+``/proc/config.gz``.+:doc:`/operate/plan/hardening` lists the options the trust model assumes-beyond this table, module signatures, ``CONFIG_COMPAT`` and others, why-kernel lockdown is not among them, and why the dm-verity root-hash check-accepting keys of the kernel's other keyrings does not let them sign a-config.+beyond this table, ``CONFIG_COMPAT`` and others, why kernel lockdown is+not among them, and why the dm-verity root-hash check accepting keys of+the kernel's other keyrings does not let them sign a config.Kernel command line~~~~~~~~~~~~~~~~~~~@@ -372,6 +377,10 @@ Pre-flight checks- The kernel BTF has a ``bpf_lsm_<hook>`` function for every LSMhook the embedded ``bpf2`` and ``seal`` programs attach to, thecarried ``tpm_command`` and ``mm_read_access`` among them.+ * - ``module_signatures``+ - The kernel BTF has no ``__module_address``, so the kernel loads no+ modules, or has ``mod_verify_sig``, which only+ ``CONFIG_MODULE_SIG`` builds.* - ``cert_on_disk``- The primary certificate exists, is a regular file and is notempty.@@ -399,13 +408,14 @@ Run the checks as root on a target:OK bpffs_supportedOK signed_loader_kernel_abiOK kernel_lsm_hooks+ OK module_signaturesOK cert_on_diskOK cert_algorithmOK cert_kernel_supportOK kernel_keyringOK embedded_loaderOK tpm2_reachable: TCTI from the config: device:/dev/tpmrm0- 11 of 11 passed+ 12 of 12 passedA failed check prints ``FAIL`` and its reason, and the command exits 1after ``init: pre-flight failed, <failed> of <total> checks``.diff --git a/Documentation/operate/troubleshoot/troubleshooting.rst b/Documentation/operate/troubleshoot/troubleshooting.rstindex d659825c..a5b6fabe 100644--- a/Documentation/operate/troubleshoot/troubleshooting.rst+++ b/Documentation/operate/troubleshoot/troubleshooting.rst@@ -489,19 +489,21 @@ MeaningOK bpffs_supportedOK signed_loader_kernel_abiOK kernel_lsm_hooks+ OK module_signaturesOK cert_on_diskOK cert_algorithmOK cert_kernel_supportOK kernel_keyringOK embedded_loaderOK tpm2_reachable: TCTI from the config: device:/dev/tpmrm0- 11 of 11 passed+ 12 of 12 passedCausesThe check names the requirement that is missing: root, the BPF LSMin the kernel's active LSM list, bpffs, the signed-loader ABI, an- LSM hook only the kernel project patches add, the certificate and- its algorithm, the kernel keyring, the embedded loader or the TPM.+ LSM hook only the kernel project patches add, module signatures in+ a kernel that loads modules, the certificate and its algorithm, the+ kernel keyring, the embedded loader or the TPM.ActionFix the requirement, see :doc:`/operate/plan/system-requirements`, anddiff --git a/Documentation/reference/cli/init.rst b/Documentation/reference/cli/init.rstindex 70eb11c1..6387525e 100644--- a/Documentation/reference/cli/init.rst+++ b/Documentation/reference/cli/init.rst@@ -97,6 +97,8 @@ Pre-flight checks- The kernel has the signed-loader interface.* - ``kernel_lsm_hooks``- The kernel has every LSM hook the embedded programs attach to.+ * - ``module_signatures``+ - The kernel checks module signatures, or loads no modules.* - ``cert_on_disk``- The primary certificate exists: ``/etc/shirudo/keys/cert.der`` inthe initrd, ``/run/shirudo/runtime/keys/cert.der`` with an active@@ -186,13 +188,14 @@ Run the pre-flight checks on a target:OK bpffs_supportedOK signed_loader_kernel_abiOK kernel_lsm_hooks+ OK module_signaturesOK cert_on_diskOK cert_algorithmOK cert_kernel_supportOK kernel_keyringOK embedded_loaderOK tpm2_reachable: TCTI from the config: device:/dev/tpmrm0- 11 of 11 passed+ 12 of 12 passedRelated-------diff --git a/Documentation/security/security-model.rst b/Documentation/security/security-model.rstindex ba7fdc15..f8ce0759 100644--- a/Documentation/security/security-model.rst+++ b/Documentation/security/security-model.rst@@ -105,10 +105,11 @@ The properties above hold only on a deployment that provides the- An operator host that keeps the signing key safeshirudo checks only some of these. The pre-flight of ``shirudo init``-checks that the BPF LSM is active and that the kernel has the-signed-loader ABI. The target checks a config's signature against the-runtime image's keys itself. It does not check the kernel-configuration or the boot chain (U10).+checks that the BPF LSM is active, that the kernel has the+signed-loader ABI and that it checks module signatures. The target+checks a config's signature against the runtime image's keys itself. It+does not check the rest of the kernel configuration or the boot chain+(U10).What you must do----------------diff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rstindex 98a6e203..bb392ec7 100644--- a/Documentation/security/threat-model.rst+++ b/Documentation/security/threat-model.rst@@ -173,7 +173,8 @@ without the protection.label read, the seal falls back to its marks where a read failsfor the caller (T3).* - A4- - The kernel checks module signatures (``CONFIG_MODULE_SIG``).+ - The kernel checks module signatures (``CONFIG_MODULE_SIG``), or+ loads no modules (``CONFIG_MODULES=n``).- An unsigned module is kernel code of root's choosing. A kernelthat does not enforce signatures itself asks lockdown about one,and ``bpf2`` refuses it from ``init`` on through@@ -186,7 +187,11 @@ without the protection.kernel's lockdown LSM is not needed for them, and where it runsit decides first. A kernel without ``CONFIG_MODULE_SIG`` asksnothing and loads any module.- - No, see U10. Lockdown in confidentiality mode would withhold+ - Yes. The pre-flight check ``module_signatures`` refuses a+ kernel whose BTF shows modules (``__module_address``) without+ ``mod_verify_sig``, which only ``CONFIG_MODULE_SIG`` builds.+ Which keys the kernel trusts for modules is not checked.+ Lockdown in confidentiality mode would withhold``bpf_probe_read_kernel()`` from shirudo's own programs and stop``init``. This is a reading of the kernel source and is nottested.@@ -1130,7 +1135,10 @@ it, an unsigned module joins it, and ``/proc/kcore`` reads it.``lsm/locked_down`` refuses what lockdown in integrity mode refuses, anunsigned module the kernel asks about included (A4), and``/proc/kcore`` with it, to every task, whatever the command line says,-and counts each refusal on the integrity counter of its reason.+and counts each refusal on the integrity counter of its reason. A+kernel that does not ask about an unsigned module, one without+``CONFIG_MODULE_SIG``, fails the pre-flight check+``module_signatures``.Hibernation is refused too, but a resume runs in the initrd before``init``: ``lsm/file_open`` refuses the write of a ``HibernateLocation``EFI variable, which would have the next boot's initrd resume from a@@ -1152,7 +1160,7 @@ naming one. :doc:`integrity-counters` lists the counters.``XmonWrite``, ``BpfWriteUser``, ``DbgWriteKernel``,``RtasErrorInjection``, ``XenUserActions``, ``Kcore``,``HibernateLocation`` and ``Other``-:Tests: ``t4003``, ``t4101``, ``t6126``, ``t6154``+:Tests: ``t4003``, ``t4101``, ``t6126``, ``t6154``, ``t6157``:Residual: R6, R7, R12, U11, U16.. _tm-t7:@@ -2032,11 +2040,11 @@ U9 init can rebind the register after hiding this boot's record``--expect-pcr=9`` with the predicted value.U10 Kernel prerequisites are not checked- :Affects: A4, A5, A9 and A10.- :Consequence: A target without module signatures, with 32-bit- compatibility, with a resume device in its UKI or with writes- under mounted file systems allowed initializes without an error- and without the protection those assumptions give.+ :Affects: A5, A9 and A10.+ :Consequence: A target with 32-bit compatibility, with a resume+ device in its UKI or with writes under mounted file systems+ allowed initializes without an error and without the protection+ those assumptions give.:Status: Confirmed in code: the pre-flight checks none of them.:Meanwhile: Build the kernel and its command line as:doc:`/operate/plan/hardening` describes.@@ -2210,7 +2218,8 @@ The deployer:- Builds the kernel with the signed-loader ABI and the kernel projectpatch set (A3), ``CONFIG_MODULE_SIG`` (A4), ``CONFIG_COMPAT=n`` (A5),the TPM driver without ``i2c-dev`` or ``spidev`` on its bus (A8), and- ``CONFIG_BLK_DEV_WRITE_MOUNTED=n`` (A10)+ ``CONFIG_BLK_DEV_WRITE_MOUNTED=n`` (A10); the pre-flight of ``init``+ refuses a kernel without A4- Boots a signed, measured UKI under Secure Boot whose command linecarries ``lsm=`` with ``bpf``, ``bpf.keyring_unsealed=1`` and``dm_verity.keyring_unsealed=1``, and whose command line, kernel anddiff --git a/Documentation/start/faq.rst b/Documentation/start/faq.rstindex eaa869af..a3ffeaa0 100644--- a/Documentation/start/faq.rst+++ b/Documentation/start/faq.rst@@ -36,11 +36,11 @@ Can root turn shirudo off?Not without the signing key: root cannot load unsigned BPF, detach theenforcement or change shirudo's state. Root with a way into the kernel-itself, such as a kernel exploit or a kernel built without module-signatures (``CONFIG_MODULE_SIG``), which loads any module, goes around-every check made in that kernel. :doc:`/security/security-model` lists-what shirudo protects and where it stops, and-:doc:`/operate/plan/hardening` lists the kernel settings it relies on.+itself, such as a kernel exploit or a module signed with a key the+kernel trusts, goes around every check made in that kernel.+:doc:`/security/security-model` lists what shirudo protects and where it+stops, and :doc:`/operate/plan/hardening` lists the kernel settings it+relies on.Does a target need a TPM?-------------------------diff --git a/Makefile.shirudo b/Makefile.shirudoindex 41ba64f2..25d8fba2 100644--- a/Makefile.shirudo+++ b/Makefile.shirudo@@ -247,6 +247,7 @@ UTIL_SRCS := \util/harden.c \util/preflight.c \util/kernel_abi.c \+ util/kernel_prereq.c \util/log.c \util/io.c \util/tpm.c \diff --git a/testing/functional/tamperproof/t6157-kernel-options.sh b/testing/functional/tamperproof/t6157-kernel-options.shnew file mode 100755index 00000000..4272484f--- /dev/null+++ b/testing/functional/tamperproof/t6157-kernel-options.sh@@ -0,0 +1,36 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# t6157 -- the pre-flight of init checks a kernel option the threat model+# assumes on the running kernel: module signatures. The test kernel,+# which signs its modules, passes it.++cd "$(dirname "$0")/../.."+. ./integration-lib.sh+target_guard+command -v python3 >/dev/null 2>&1 || test_skip_all "no python3"+W=$(mktemp -d)+trap 'rm -rf "$W"' EXIT++# Prints "ok" or "FAIL", then ": <detail>" when there is one, for the+# check $2 of the pre-flight JSON in $1.+check() {+ python3 -c '+import json, sys+d = json.load(open(sys.argv[1], encoding="utf-8"))+for c in d["checks"]:+ if c["name"] == sys.argv[2]:+ print(("ok" if c["ok"] else "FAIL") ++ (": " + c["detail"] if c["detail"] else ""))+ sys.exit(0)+print("missing")+' "$1" "$2"+}++test_expect_success 'the test kernel passes the module signature check' '+ "$SHIRUDO_BIN" init --check-only --json >"$W/preflight.json" &&+ [ "$(check "$W/preflight.json" module_signatures)" = ok ]+'++test_donediff --git a/testing/unit/kernel_prereq.c b/testing/unit/kernel_prereq.cnew file mode 100644index 00000000..69af2315--- /dev/null+++ b/testing/unit/kernel_prereq.c@@ -0,0 +1,89 @@+// SPDX-License-Identifier: BSD-2-Clause+/* Copyright (c) 2026 Authors of shirudo */++#include <string.h>++#include <linux/btf.h>++#include <bpf/btf.h>+#include <bpf/libbpf.h>++#include <shirudo/misc.h>++#include "kernel_prereq.h"+#include "unit.h"++/* A BTF with a function of each name and nothing else of note. */+static struct btf *btf_with(const char *const *names, size_t n)+{+ int ret_type, proto;+ struct btf *btf;+ size_t i;++ btf = btf__new_empty();+ if (libbpf_get_error(btf))+ return NULL;+ ret_type = btf__add_int(btf, "int", 4, BTF_INT_SIGNED);+ proto = btf__add_func_proto(btf, ret_type);+ if (ret_type < 0 || proto < 0)+ goto err;+ for (i = 0; i < n; i++)+ if (btf__add_func(btf, names[i], BTF_FUNC_GLOBAL, proto) < 0)+ goto err;+ return btf;+err:+ btf__free(btf);+ return NULL;+}++static void options_of(const char *const *names, size_t n,+ struct kernel_options *opts)+{+ struct btf *btf = btf_with(names, n);++ ASSERT_TRUE(btf);+ memset(opts, 0xff, sizeof(*opts));+ kernel_prereq_options(btf, opts);+ btf__free(btf);+}++static void options_of_a_kernel_with_all(void)+{+ static const char *const names[] = {+ "__module_address", "mod_verify_sig",+ };+ struct kernel_options opts;++ options_of(names, ARRAY_SIZE(names), &opts);+ EXPECT_TRUE(opts.modules);+ EXPECT_TRUE(opts.module_sig);+}++static void options_of_a_kernel_with_none(void)+{+ static const char *const names[] = { "vfs_read", "module" };+ struct kernel_options opts;++ options_of(names, ARRAY_SIZE(names), &opts);+ EXPECT_FALSE(opts.modules);+ EXPECT_FALSE(opts.module_sig);+}++static void modules_without_signatures(void)+{+ static const char *const names[] = { "__module_address" };+ struct kernel_options opts;++ options_of(names, ARRAY_SIZE(names), &opts);+ EXPECT_TRUE(opts.modules);+ EXPECT_FALSE(opts.module_sig);+}++static const struct unit_case cases[] = {+ UNIT_CASE(options_of_a_kernel_with_all),+ UNIT_CASE(options_of_a_kernel_with_none),+ UNIT_CASE(modules_without_signatures),+ {}+};++UNIT_SUITE(kernel_prereq, cases);diff --git a/util/kernel_prereq.c b/util/kernel_prereq.cnew file mode 100644index 00000000..40f328fe--- /dev/null+++ b/util/kernel_prereq.c@@ -0,0 +1,31 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * What the running kernel shows of the options the threat model assumes,+ * for the pre-flight checks of init.+ */++#include <stdbool.h>++#include <linux/btf.h>++#include <bpf/btf.h>++#include "kernel_prereq.h"++static bool has_func(const struct btf *btf, const char *name)+{+ return btf__find_by_name_kind(btf, name, BTF_KIND_FUNC) > 0;+}++/*+ * Each name is a function only its option builds. Without the option+ * its stub, where it has one, is a static inline, which leaves no BTF+ * function.+ */+void kernel_prereq_options(const struct btf *btf, struct kernel_options *out)+{+ out->modules = has_func(btf, "__module_address");+ out->module_sig = has_func(btf, "mod_verify_sig");+}diff --git a/util/kernel_prereq.h b/util/kernel_prereq.hnew file mode 100644index 00000000..2c4a163f--- /dev/null+++ b/util/kernel_prereq.h@@ -0,0 +1,24 @@+/* SPDX-License-Identifier: BSD-2-Clause */+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * What the running kernel shows of the options the threat model assumes:+ * its BTF, not /proc/config.gz, which a target need not have.+ */++#ifndef SHIRUDO_KERNEL_PREREQ_H+#define SHIRUDO_KERNEL_PREREQ_H++#include <stdbool.h>++struct btf;++struct kernel_options {+ bool modules; /* CONFIG_MODULES */+ bool module_sig; /* CONFIG_MODULE_SIG */+};++/* The options btf shows, the BTF of vmlinux. */+void kernel_prereq_options(const struct btf *btf, struct kernel_options *out);++#endif /* SHIRUDO_KERNEL_PREREQ_H */diff --git a/util/preflight.c b/util/preflight.cindex 0745dc9e..2c1dd664 100644--- a/util/preflight.c+++ b/util/preflight.c@@ -16,6 +16,9 @@#include <linux/keyctl.h>+#include <bpf/btf.h>+#include <bpf/libbpf.h>+#include <shirudo/misc.h>#include <shirudo/string.h>@@ -23,6 +26,7 @@#include "initrd.h"#include "crypto.h"#include "kernel_abi.h"+#include "kernel_prereq.h"#include "keys.h"#include "loader.h"#include "malloc.h"@@ -211,6 +215,37 @@ static void check_lsm_hooks(struct preflight_result *r)}}+static void check_module_signatures(struct preflight_result *r,+ const struct kernel_options *opts)+{+ struct preflight_check *c =+ next_slot(r, PRE_MODULE_SIG, "module_signatures");++ if (!opts)+ fail(r, c, "cannot read the kernel BTF");+ else if (!opts->modules || opts->module_sig)+ pass(c);+ else+ fail(r, c, "the kernel loads modules without checking their "+ "signatures (needs CONFIG_MODULE_SIG=y, or CONFIG_MODULES=n)");+}++/* A BTF that cannot be read fails the check that needs it. */+static void check_kernel_options(struct preflight_result *r)+{+ const struct kernel_options *have = NULL;+ struct kernel_options opts = {};+ struct btf *btf;++ btf = btf__load_vmlinux_btf();+ if (!libbpf_get_error(btf)) {+ kernel_prereq_options(btf, &opts);+ have = &opts;+ btf__free(btf);+ }+ check_module_signatures(r, have);+}+static void check_cert(struct preflight_result *r){struct preflight_check *c =@@ -416,6 +451,7 @@ void preflight_run(struct preflight_result *out)check_bpffs(out);check_signed_loader_abi(out);check_lsm_hooks(out);+ check_kernel_options(out);check_cert(out);check_cert_algorithm(out);check_cert_kernel_algo(out);diff --git a/util/preflight.h b/util/preflight.hindex bf085faa..34d89f97 100644--- a/util/preflight.h+++ b/util/preflight.h@@ -18,6 +18,7 @@ enum preflight_id {PRE_BPFFS,PRE_SIGNED_LOADER_ABI,PRE_LSM_HOOKS,+ PRE_MODULE_SIG,PRE_CERT_ON_DISK,PRE_CERT_ALGORITHM,PRE_CERT_KERNEL_ALGO,