eBPF-based security platform
preflight: check the kernel has every LSM hook the programs attach to
Daniel Borkmann56bb68ceb52b
diff --git a/Documentation/internals/kernel-interfaces.rst b/Documentation/internals/kernel-interfaces.rstindex 84aa81f6..c2bfe124 100644--- a/Documentation/internals/kernel-interfaces.rst+++ b/Documentation/internals/kernel-interfaces.rst@@ -111,6 +111,10 @@ BTF and requires:The pre-flight runs it as ``signed_loader_kernel_abi``, and every``upatch load`` and ``upatch unload`` without ``--no-precheck`` runs itbefore ``BPF_PROG_LOAD``. A failure is ``kernel_abi_unsupported``.+Beside it the pre-flight runs ``kernel_lsm_hooks``: it reads the hooks+of every ``lsm`` and ``lsm.s`` program from the embedded ``bpf2`` and+``seal`` objects and refuses a kernel whose BTF has no+``bpf_lsm_<hook>`` for one, naming the hooks it lacks.``lsm/bpf_prog_load`` compares ``sig.verdict`` with ``BPF_SIG_VERIFIED``and ``sig.keyring_type`` with ``BPF_SIG_KEYRING_BPF``; these enum valuesare compiled into ``bpf2`` from the ``vmlinux.h`` of the build.diff --git a/Documentation/operate/deploy/initialize.rst b/Documentation/operate/deploy/initialize.rstindex eb130e2b..ab5ee5ef 100644--- a/Documentation/operate/deploy/initialize.rst+++ b/Documentation/operate/deploy/initialize.rst@@ -190,13 +190,14 @@ It changes no persistent or enforcement state:OK bpf_lsm_activeOK bpffs_supportedOK signed_loader_kernel_abi+ OK kernel_lsm_hooksOK cert_on_diskOK cert_algorithmOK cert_kernel_supportOK kernel_keyringOK embedded_loaderOK tpm2_reachable: TCTI from the config: device:/dev/tpmrm0- 10 of 10 passed+ 11 of 11 passed:doc:`/reference/cli/init` describes each check.diff --git a/Documentation/operate/plan/system-requirements.rst b/Documentation/operate/plan/system-requirements.rstindex 43e0e1bb..e910220c 100644--- a/Documentation/operate/plan/system-requirements.rst+++ b/Documentation/operate/plan/system-requirements.rst@@ -115,7 +115,10 @@ Identify the kernel``bpf-next-kernel-build``.- ``shirudo init --check-only`` checks the ABI of the running kernelfrom its BTF (``signed_loader_kernel_abi``). A load on a kernel- without it fails with ``kernel_abi_unsupported``.+ without it fails with ``kernel_abi_unsupported``. It also checks that+ the kernel has every LSM hook the embedded programs attach to+ (``kernel_lsm_hooks``), and names the hooks a kernel built without+ the carried patches lacks.- The test harness builds bpf-next by default. It also builds releasetags and refuses any tag older than v7.4. It applies the patches in:src:`kernel/` to either. A release kernel still needs ``kernel/0001``.@@ -369,6 +372,10 @@ Pre-flight checks* - ``signed_loader_kernel_abi``- The kernel BTF has the fields of the signed-loader ABI with theexpected types.+ * - ``kernel_lsm_hooks``+ - The kernel BTF has a ``bpf_lsm_<hook>`` function for every LSM+ hook the embedded ``bpf2`` and ``seal`` programs attach to, the+ carried ``tpm_command`` and ``mm_read_access`` among them.* - ``cert_on_disk``- The primary certificate exists, is a regular file and is notempty.@@ -395,13 +402,14 @@ Run the checks as root on a target:OK bpf_lsm_activeOK bpffs_supportedOK signed_loader_kernel_abi+ OK kernel_lsm_hooksOK cert_on_diskOK cert_algorithmOK cert_kernel_supportOK kernel_keyringOK embedded_loaderOK tpm2_reachable: TCTI from the config: device:/dev/tpmrm0- 10 of 10 passed+ 11 of 11 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 a360ecfe..12f145a3 100644--- a/Documentation/operate/troubleshoot/troubleshooting.rst+++ b/Documentation/operate/troubleshoot/troubleshooting.rst@@ -484,19 +484,20 @@ MeaningOK bpf_lsm_activeOK bpffs_supportedOK signed_loader_kernel_abi+ OK kernel_lsm_hooksOK cert_on_diskOK cert_algorithmOK cert_kernel_supportOK kernel_keyringOK embedded_loaderOK tpm2_reachable: TCTI from the config: device:/dev/tpmrm0- 10 of 10 passed+ 11 of 11 passedCausesThe check names the requirement that is missing: root, the BPF LSM- in the kernel's active LSM list, bpffs, the signed-loader ABI, the- certificate and its algorithm, the kernel keyring, the embedded- loader or the TPM.+ in the kernel's active LSM list, bpffs, the signed-loader ABI, an+ LSM hook only the carried kernel patches add, 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 86d64cd7..4a17394e 100644--- a/Documentation/reference/cli/init.rst+++ b/Documentation/reference/cli/init.rst@@ -95,6 +95,8 @@ Pre-flight checks- The kernel supports bpffs.* - ``signed_loader_kernel_abi``- The kernel has the signed-loader interface.+ * - ``kernel_lsm_hooks``+ - The kernel has every LSM hook the embedded programs attach to.* - ``cert_on_disk``- The primary certificate exists: ``/etc/shirudo/keys/cert.der`` inthe initrd, ``/run/shirudo/runtime/keys/cert.der`` with an active@@ -183,13 +185,14 @@ Run the pre-flight checks on a target:OK bpf_lsm_activeOK bpffs_supportedOK signed_loader_kernel_abi+ OK kernel_lsm_hooksOK cert_on_diskOK cert_algorithmOK cert_kernel_supportOK kernel_keyringOK embedded_loaderOK tpm2_reachable: TCTI from the config: device:/dev/tpmrm0- 10 of 10 passed+ 11 of 11 passedRelated-------diff --git a/Documentation/reference/versions.rst b/Documentation/reference/versions.rstindex a2c33f9a..ab535cfb 100644--- a/Documentation/reference/versions.rst+++ b/Documentation/reference/versions.rst@@ -220,10 +220,11 @@ Kernel ABI----------shirudo needs the signed-loader ABI of the bpf-next tree it targets and-the ``tpm_command`` LSM hook that it carries as a kernel patch. A-release number does not identify such a kernel. The pre-flight check-``signed_loader_kernel_abi`` reads the running kernel's BTF, and a load-on a kernel without the ABI fails with ``kernel_abi_unsupported``.+the kernel patches it carries, among them the ``tpm_command`` and+``mm_read_access`` LSM hooks. A release number does not identify such a+kernel. The pre-flight checks ``signed_loader_kernel_abi`` and+``kernel_lsm_hooks`` read the running kernel's BTF, and a load on a+kernel without the ABI fails with ``kernel_abi_unsupported``.:doc:`/operate/plan/system-requirements` lists the kernel requirements and howto identify a kernel, :doc:`/internals/kernel-interfaces` every interfaceshirudo uses, and :doc:`/internals/kernel-patches` the carried patches.diff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rstindex c58e4afc..42f08073 100644--- a/Documentation/security/threat-model.rst+++ b/Documentation/security/threat-model.rst@@ -167,10 +167,12 @@ without the protection.and :src:`kernel/0005-mm-Let-LSMs-check-read-only-remote-mm-access-under-CAP_PERFMON.patch`.- T1 needs the ABI, T5 the hook, T3 the label read, the sleepablehooks, the trusted parent and the read-only mm access hook.- - Partly. The pre-flight check ``signed_loader_kernel_abi`` tests- the ABI. Without the hook the ``bpf2`` object does not load, and- without the sleepable hooks and the trusted parent the ``seal``- object does not load; ``init`` fails either way. Without the+ - Partly. The pre-flight checks ``signed_loader_kernel_abi`` and+ ``kernel_lsm_hooks`` test the ABI and every LSM hook the+ programs attach to, ``tpm_command`` and ``mm_read_access``+ included, before ``init`` loads anything. Without the sleepable+ hooks and the trusted parent the ``seal`` object does not load;+ ``init`` fails either way. Without thelabel read, the seal falls back to its marks where a read failsfor the caller (T3).* - A4diff --git a/testing/unit/kernel_abi.c b/testing/unit/kernel_abi.cindex e978f69f..198e69a7 100644--- a/testing/unit/kernel_abi.c+++ b/testing/unit/kernel_abi.c@@ -1,12 +1,18 @@// SPDX-License-Identifier: BSD-2-Clause/* Copyright (c) 2026 Authors of shirudo */+#include <errno.h>+#include <linux/btf.h>#include <bpf/btf.h>#include <bpf/libbpf.h>+#include <shirudo/misc.h>+#include <shirudo/string.h>+#include "kernel_abi.h"+#include "loader.h"#include "unit.h"enum fixture {@@ -246,6 +252,153 @@ static void needs_verdict_unsigned(void)check(VERDICT_SIGNED, 0, "verdict must have type u8");}+/* A BTF with a bpf_lsm_<hook> function for each hook but skip's. */+static struct btf *lsm_btf(const struct lsm_hooks *hooks, const char *skip)+{+ char function[LSM_HOOK_NAME_MAX + 8];+ 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 < hooks->n; i++) {+ if (skip && !strcmp(hooks->name[i], skip))+ continue;+ if (strsprintf(function, sizeof(function), "bpf_lsm_%s",+ hooks->name[i]) < 0 ||+ btf__add_func(btf, function, BTF_FUNC_GLOBAL, proto) < 0)+ goto err;+ }+ return btf;+err:+ btf__free(btf);+ return NULL;+}++static void hooks_of(struct lsm_hooks *hooks, const char *const *names,+ size_t n)+{+ size_t i;++ hooks->n = 0;+ for (i = 0; i < n; i++)+ ASSERT_TRUE(strscpy(hooks->name[hooks->n++], names[i],+ sizeof(hooks->name[0])) >= 0);+}++static void embedded_hooks(struct lsm_hooks *hooks)+{+ hooks->n = 0;+ ASSERT_EQ(kernel_abi_lsm_hooks(bpf2_blob(), hooks), 0);+ ASSERT_EQ(kernel_abi_lsm_hooks(seal_blob(), hooks), 0);+}++static bool has_hook(const struct lsm_hooks *hooks, const char *hook)+{+ size_t i;++ for (i = 0; i < hooks->n; i++)+ if (!strcmp(hooks->name[i], hook))+ return true;+ return false;+}++static void lsm_hooks_come_from_the_embedded_objects(void)+{+ static struct lsm_hooks hooks;+ size_t i, j;++ embedded_hooks(&hooks);+ EXPECT_TRUE(has_hook(&hooks, "bpf"));+ EXPECT_TRUE(has_hook(&hooks, "inode_unlink"));+ EXPECT_TRUE(has_hook(&hooks, "mm_read_access"));+ EXPECT_FALSE(has_hook(&hooks, "lsm/bpf"));+ for (i = 0; i < hooks.n; i++)+ for (j = i + 1; j < hooks.n; j++)+ EXPECT_NE(strcmp(hooks.name[i], hooks.name[j]), 0);+}++static void lsm_hooks_all_present(void)+{+ static const char *const names[] = { "bpf", "file_open" };+ char detail[KERNEL_ABI_DETAIL_MAX] = {};+ static struct lsm_hooks hooks;+ struct btf *btf;++ hooks_of(&hooks, names, ARRAY_SIZE(names));+ btf = lsm_btf(&hooks, NULL);+ ASSERT_TRUE(btf);+ EXPECT_EQ(kernel_abi_validate_lsm_hooks(btf, &hooks, detail,+ sizeof(detail)), 0);+ EXPECT_STREQ(detail, "");+ btf__free(btf);+}++static void lsm_hooks_missing_are_named(void)+{+ static const char *const names[] = { "bpf", "file_open", "mm_read_access" };+ static const char *const two[] = { "bpf", "x_one", "x_two" };+ char detail[KERNEL_ABI_DETAIL_MAX] = {};+ static struct lsm_hooks hooks;+ struct btf *btf;++ hooks_of(&hooks, names, ARRAY_SIZE(names));+ btf = lsm_btf(&hooks, "mm_read_access");+ ASSERT_TRUE(btf);+ EXPECT_EQ(kernel_abi_validate_lsm_hooks(btf, &hooks, detail,+ sizeof(detail)), -EOPNOTSUPP);+ EXPECT_STREQ(detail, "kernel has no LSM hook mm_read_access; "+ "boot a shirudo-patched kernel");+ btf__free(btf);++ hooks_of(&hooks, names, 1);+ btf = lsm_btf(&hooks, NULL);+ ASSERT_TRUE(btf);+ hooks_of(&hooks, two, ARRAY_SIZE(two));+ EXPECT_EQ(kernel_abi_validate_lsm_hooks(btf, &hooks, detail,+ sizeof(detail)), -EOPNOTSUPP);+ EXPECT_STREQ(detail, "kernel has no LSM hooks x_one, x_two; "+ "boot a shirudo-patched kernel");+ btf__free(btf);+}++static void lsm_hooks_of_the_embedded_objects_checked(void)+{+ char detail[KERNEL_ABI_DETAIL_MAX] = {};+ static struct lsm_hooks hooks;+ struct btf *btf;++ embedded_hooks(&hooks);+ btf = lsm_btf(&hooks, NULL);+ ASSERT_TRUE(btf);+ EXPECT_EQ(kernel_abi_validate_lsm_hooks(btf, &hooks, detail,+ sizeof(detail)), 0);+ btf__free(btf);+ btf = lsm_btf(&hooks, "mm_read_access");+ ASSERT_TRUE(btf);+ EXPECT_EQ(kernel_abi_validate_lsm_hooks(btf, &hooks, detail,+ sizeof(detail)), -EOPNOTSUPP);+ EXPECT_STREQ(detail, "kernel has no LSM hook mm_read_access; "+ "boot a shirudo-patched kernel");+ btf__free(btf);+}++static void lsm_hooks_need_btf(void)+{+ char detail[KERNEL_ABI_DETAIL_MAX] = {};+ static struct lsm_hooks hooks;++ EXPECT_EQ(kernel_abi_validate_lsm_hooks(NULL, &hooks, detail,+ sizeof(detail)), -EOPNOTSUPP);+ EXPECT_TRUE(strstr(detail, "kernel BTF is unavailable"));+}+static const struct unit_case cases[] = {UNIT_CASE(accepts_the_required_prefix),UNIT_CASE(accepts_fields_past_the_prefix),@@ -266,6 +419,11 @@ static const struct unit_case cases[] = {UNIT_CASE(needs_keyring_type_u8),UNIT_CASE(needs_verdict),UNIT_CASE(needs_verdict_unsigned),+ UNIT_CASE(lsm_hooks_come_from_the_embedded_objects),+ UNIT_CASE(lsm_hooks_all_present),+ UNIT_CASE(lsm_hooks_missing_are_named),+ UNIT_CASE(lsm_hooks_of_the_embedded_objects_checked),+ UNIT_CASE(lsm_hooks_need_btf),{}};diff --git a/util/kernel_abi.c b/util/kernel_abi.cindex f2ade000..0c561d54 100644--- a/util/kernel_abi.c+++ b/util/kernel_abi.c@@ -3,8 +3,9 @@* Copyright (c) 2026 Authors of shirudo** The signed loader requires map hashing, exclusive-map binding, and signed- * program metadata. Stock kernels can expose BPF and the BPF LSM while still- * lacking this contract, so reject those kernels before BPF_PROG_LOAD.+ * program metadata, and the embedded programs attach to LSM hooks that only+ * the carried kernel patches add. Stock kernels can expose BPF and the BPF+ * LSM while lacking either, so reject those kernels before BPF_PROG_LOAD.*/#include <errno.h>@@ -24,6 +25,7 @@#include <shirudo/string.h>#include "kernel_abi.h"+#include "loader.h"#define MAP_SHA_SIZE 32U#define BTF_MAX_UNWRAP 32@@ -214,3 +216,105 @@ int kernel_abi_check(char *detail, size_t detail_sz)btf__free(btf);return ret;}++/* The hook an lsm or lsm.s program attaches to, NULL for another kind. */+static const char *lsm_hook(const struct bpf_program *prog)+{+ const char *sec = bpf_program__section_name(prog);++ if (sec && (str_skip_prefix(&sec, "lsm/") ||+ str_skip_prefix(&sec, "lsm.s/")))+ return sec;+ return NULL;+}++static int lsm_hooks_add(struct lsm_hooks *hooks, const char *hook)+{+ size_t i;++ for (i = 0; i < hooks->n; i++)+ if (!strcmp(hooks->name[i], hook))+ return 0;+ if (hooks->n == ARRAY_SIZE(hooks->name) ||+ strscpy(hooks->name[hooks->n], hook, sizeof(hooks->name[0])) < 0)+ return -E2BIG;+ hooks->n++;+ return 0;+}++int kernel_abi_lsm_hooks(const struct embedded_bpf *obj,+ struct lsm_hooks *hooks)+{+ struct bpf_program *prog;+ struct bpf_object *bo;+ const char *hook;+ int ret = 0;+ long err;++ bo = bpf_object__open_mem(obj->data, obj->size, NULL);+ err = libbpf_get_error(bo);+ if (err)+ return (int)err;+ bpf_object__for_each_program(prog, bo) {+ hook = lsm_hook(prog);+ if (hook) {+ ret = lsm_hooks_add(hooks, hook);+ if (ret)+ break;+ }+ }+ bpf_object__close(bo);+ return ret;+}++int kernel_abi_validate_lsm_hooks(const struct btf *btf,+ const struct lsm_hooks *hooks,+ char *detail, size_t detail_sz)+{+ char missing[KERNEL_ABI_DETAIL_MAX] = "";+ char function[LSM_HOOK_NAME_MAX + 8];+ size_t i, n_missing = 0, len = 0;+ int ret;++ if (detail && detail_sz)+ detail[0] = '\0';+ if (!btf)+ return abi_fail(detail, detail_sz,+ "kernel BTF is unavailable; boot a shirudo-patched kernel");+ for (i = 0; i < hooks->n; i++) {+ if (strsprintf(function, sizeof(function), "bpf_lsm_%s",+ hooks->name[i]) >= 0 &&+ btf__find_by_name_kind(btf, function, BTF_KIND_FUNC) > 0)+ continue;+ ret = strsprintf(missing + len, sizeof(missing) - len, "%s%s",+ n_missing ? ", " : "", hooks->name[i]);+ if (ret > 0)+ len += (size_t)ret;+ n_missing++;+ }+ if (!n_missing)+ return 0;+ return abi_fail(detail, detail_sz,+ "kernel has no LSM hook%s %s; boot a shirudo-patched kernel",+ n_missing > 1 ? "s" : "", missing);+}++int kernel_abi_check_lsm_hooks(const struct lsm_hooks *hooks,+ char *detail, size_t detail_sz)+{+ struct btf *btf;+ long err;+ int ret;++ if (detail && detail_sz)+ detail[0] = '\0';+ btf = btf__load_vmlinux_btf();+ err = libbpf_get_error(btf);+ if (err)+ return abi_fail(detail, detail_sz,+ "cannot read target kernel BTF: %s; boot a shirudo-patched kernel",+ strerror((int)-err));+ ret = kernel_abi_validate_lsm_hooks(btf, hooks, detail, detail_sz);+ btf__free(btf);+ return ret;+}diff --git a/util/kernel_abi.h b/util/kernel_abi.hindex 02e39cdc..1204fbff 100644--- a/util/kernel_abi.h+++ b/util/kernel_abi.h@@ -24,4 +24,33 @@ int kernel_abi_validate_btf(const struct btf *btf,int kernel_abi_check(char *detail, size_t detail_sz);+struct embedded_bpf;++#define LSM_HOOKS_MAX 128+#define LSM_HOOK_NAME_MAX 64++struct lsm_hooks {+ char name[LSM_HOOKS_MAX][LSM_HOOK_NAME_MAX];+ size_t n;+};++/*+ * Adds the hooks the lsm and lsm.s programs of obj attach to, each name+ * once: 0, -E2BIG when they do not fit, or the error of opening obj.+ */+int kernel_abi_lsm_hooks(const struct embedded_bpf *obj,+ struct lsm_hooks *hooks);++/*+ * Whether btf has a bpf_lsm_<hook> function for every hook: 0, or+ * -EOPNOTSUPP with the hooks it lacks named in detail.+ */+int kernel_abi_validate_lsm_hooks(const struct btf *btf,+ const struct lsm_hooks *hooks,+ char *detail, size_t detail_sz);++/* kernel_abi_validate_lsm_hooks() on the running kernel's BTF. */+int kernel_abi_check_lsm_hooks(const struct lsm_hooks *hooks,+ char *detail, size_t detail_sz);+#endif /* SHIRUDO_KERNEL_ABI_H */diff --git a/util/preflight.c b/util/preflight.cindex 3d41b5b0..0745dc9e 100644--- a/util/preflight.c+++ b/util/preflight.c@@ -174,6 +174,43 @@ static void check_signed_loader_abi(struct preflight_result *r)}}+/*+ * The hooks come from the embedded objects themselves, so a program+ * added on a hook only a carried kernel patch provides is checked+ * without a list here to keep in step.+ */+static void check_lsm_hooks(struct preflight_result *r)+{+ const struct embedded_bpf *objs[] = { bpf2_blob(), seal_blob() };+ struct preflight_check *c =+ next_slot(r, PRE_LSM_HOOKS, "kernel_lsm_hooks");+ struct lsm_hooks hooks = {};+ size_t i;+ int ret;++ for (i = 0; i < ARRAY_SIZE(objs); i++) {+ if (!objs[i]->size) {+ pass(c);+ strscpy(c->detail, "skipped (embedded objects missing)",+ sizeof(c->detail));+ return;+ }+ ret = kernel_abi_lsm_hooks(objs[i], &hooks);+ if (ret) {+ fail(r, c, "cannot read the LSM hooks of the embedded %s object: %s",+ objs[i]->name, strerror(-ret));+ return;+ }+ }+ if (kernel_abi_check_lsm_hooks(&hooks, c->detail,+ sizeof(c->detail)) == 0) {+ pass(c);+ } else {+ c->ok = false;+ r->n_failed++;+ }+}+static void check_cert(struct preflight_result *r){struct preflight_check *c =@@ -378,6 +415,7 @@ void preflight_run(struct preflight_result *out)check_bpf_lsm(out);check_bpffs(out);check_signed_loader_abi(out);+ check_lsm_hooks(out);check_cert(out);check_cert_algorithm(out);check_cert_kernel_algo(out);diff --git a/util/preflight.h b/util/preflight.hindex 1d001467..bf085faa 100644--- a/util/preflight.h+++ b/util/preflight.h@@ -17,6 +17,7 @@ enum preflight_id {PRE_BPF_LSM,PRE_BPFFS,PRE_SIGNED_LOADER_ABI,+ PRE_LSM_HOOKS,PRE_CERT_ON_DISK,PRE_CERT_ALGORITHM,PRE_CERT_KERNEL_ALGO,