eBPF-based security platform
bpf: guard the dm-verity keyring like the bpf keyring
Daniel Borkmann3de2ee3a65df
diff --git a/Documentation/architecture.rst b/Documentation/architecture.rstindex f4a063f0..1e67c267 100644--- a/Documentation/architecture.rst+++ b/Documentation/architecture.rst@@ -315,8 +315,8 @@ nobody else can forge the payload xattr. ``lsm/inode_mkdir``,``lsm/inode_symlink`` and ``lsm/inode_link`` there are shirudo's too,so that no symbolic link of root's stands where a load would make itspin directory and no hard link of root's keeps a pin past its unlink. ``lsm/key_permission`` applies the same test-to the trust anchor in the kernel's keyrings: write, setattr and unlink-on the sealed keyring and on the keys it holds, and the unpossessed+to the trust anchors in the kernel's keyrings: write, setattr and unlink+on the sealed bpf and dm-verity keyrings and on the keys they hold, and the unpossessedsearch ``keyctl invalidate`` uses to reach a key by serial, are deniedto every task but shirudo, while viewing, listing and the possessedsearch the kernel verifies signatures with stay open. Sealing stopsdiff --git a/Documentation/threat-model.rst b/Documentation/threat-model.rstindex 710fd1c9..25fc75ad 100644--- a/Documentation/threat-model.rst+++ b/Documentation/threat-model.rst@@ -393,7 +393,8 @@ T11 Image-mode config and executable substitutionhex bytes, verified by the kernel's dm-verity trust path, includingbuilt-in trusted keys. The initrd enrolls its config certificate andseals the dedicated ``.dm-verity`` keyring, which must then hold exactly- that certificate. A signed config never grants+ that certificate, and ``lsm/key_permission`` guards the keyring and its+ key as it guards the bpf keyring. A signed config never grantsruntime executable authority. Shirudo validates the standard superblock,fixed format and exact backing-file sizes, creates or validates a strictread-only mapping and reads every data block before parsing contents ordiff --git a/bpf/admission.h b/bpf/admission.hindex aa811a3a..b03e4574 100644--- a/bpf/admission.h+++ b/bpf/admission.h@@ -296,11 +296,12 @@ static __always_inline int handle_nv_keyring(const struct shirudo_sb_id *id,}/*- * The trust anchor in the kernel's keyrings: the keyring init froze- * and the keys it holds. Root owns the bpf keyring with write and- * setattr permission, so without this hook a single keyctl could- * unlink, clear, revoke or invalidate the certificates every load is- * verified against. Deny those needs to every task but shirudo. A+ * The trust anchors in the kernel's keyrings: the bpf and dm-verity+ * keyrings init froze and the keys they hold. Root owns both with write+ * and setattr permission, so without this hook a single keyctl could+ * unlink, clear, revoke or invalidate the certificates every load and+ * every config root is verified against. Deny those needs to every+ * task but shirudo. A* search is denied only through an unpossessed reference, which is* how keyctl reaches a key by serial to invalidate it; the kernel* verifies a signature by searching the keyring it possesses, and@@ -340,7 +341,8 @@ int BPF_PROG(shirudo_lsm_key_permission, key_ref_t key_ref,if (id->keyring_serial == 0)return 0;possessed = (unsigned long)key_ref & 1;- if (serial != id->keyring_serial) {+ if (serial != id->keyring_serial && serial != id->dm_verity_ring &&+ serial != id->dm_verity_key) {for (i = 0; i < SHIRUDO_IDENTITY_KEYS_MAX; i++)if (i < id->n_keys && id->key_serials[i] == serial)break;diff --git a/bpf/lib/shirudo_identity.h b/bpf/lib/shirudo_identity.hindex 3479fed3..943f1f7f 100644--- a/bpf/lib/shirudo_identity.h+++ b/bpf/lib/shirudo_identity.h@@ -22,6 +22,8 @@ struct shirudo_sb_id {__u8 nv_writer_public[SHIRUDO_TPM_NV_RSA_BYTES];__u8 ak_name[SHIRUDO_TPM_NAME_LEN];__u32 ak_name_len;+ __s32 dm_verity_ring;+ __s32 dm_verity_key;};struct {diff --git a/builtin-init.c b/builtin-init.cindex 883ada73..ad141bab 100644--- a/builtin-init.c+++ b/builtin-init.c@@ -495,6 +495,36 @@ static void collect_asymmetric(key_serial_t serial, const char *type,k->serials[k->n++] = (__s32)serial;}+/* Boot keys left the sealed keyring holding the config certificate alone. */+static int dm_verity_anchor(struct shirudo_identity *id)+{+ struct anchor_keys keys = {};+ key_serial_t ring;+ int err;++ ring = shirudo_keyring_dm_verity_serial();+ if (ring <= 0) {+ err = ring ? ring : -ENOKEY;+ pr_err("resolve the dm-verity keyring serial: %s",+ strerror(-err));+ return err;+ }+ err = keyring_list(ring, collect_asymmetric, &keys);+ if (err) {+ pr_err("list the dm-verity keyring %d: %s", (int)ring,+ strerror(-err));+ return err;+ }+ if (keys.n != 1) {+ pr_err("dm-verity keyring %d holds %zu keys, not the config "+ "certificate alone", (int)ring, keys.n);+ return -EKEYREJECTED;+ }+ id->dm_verity_ring = (__s32)ring;+ id->dm_verity_key = keys.serials[0];+ return 0;+}+/* Seed bootstrap and freeze shared state except the expected config root. */static int freeze_task_maps(struct bpf_object *obj){@@ -675,6 +705,9 @@ static int init_lsm(void)}id.n_keys = (__u32)keys.n;memcpy(id.key_serials, keys.serials, keys.n * sizeof(keys.serials[0]));+ err = dm_verity_anchor(&id);+ if (err)+ goto out;err = nv_writer_create(&id.nv_writer_ring, &id.nv_writer_key, id.nv_writer_public);if (err) {pr_err("init: create RSA-3072 NV writer: %s "diff --git a/testing/helper/test-nv-writer-cleanup.c b/testing/helper/test-nv-writer-cleanup.cindex 9c061c35..2d6ac9d2 100644--- a/testing/helper/test-nv-writer-cleanup.c+++ b/testing/helper/test-nv-writer-cleanup.c@@ -85,8 +85,15 @@ static int enroll(const char *path)if (rc)return rc;rc = keyring_bpf_add_cert(der, len);+ if (rc >= 0)+ rc = keyring_bpf_seal();+ /* init_lsm records the one config key boot keys leave there. */+ if (!rc)+ rc = shirudo_keyring_dm_verity_add_cert(der, len);+ if (rc >= 0)+ rc = shirudo_keyring_dm_verity_seal();xfree(der, len);- return rc < 0 ? rc : keyring_bpf_seal();+ return rc;}static int exercise(const char *mode, const char *object)diff --git a/testing/tamperproof/t1125-key-permission.sh b/testing/tamperproof/t1125-key-permission.shindex 6e95acab..17d4fe67 100755--- a/testing/tamperproof/t1125-key-permission.sh+++ b/testing/tamperproof/t1125-key-permission.sh@@ -2,11 +2,11 @@# SPDX-License-Identifier: BSD-2-Clause# Copyright (c) 2026 Authors of shirudo#-# t1125 -- the trust anchor in the kernel's keyrings is root's to look+# t1125 -- the trust anchors in the kernel's keyrings are root's to look# at and nobody's but shirudo's to change: keyctl cannot unlink, clear,-# revoke, invalidate or re-permission the sealed keyring init froze or-# the certificate keys it holds, while viewing and listing them and-# loading against them keep working.+# revoke, invalidate or re-permission the sealed bpf and dm-verity+# keyrings init froze or the certificate keys they hold, while viewing+# and listing them and loading against them keep working.cd "$(dirname "$0")/..". ./integration-lib.sh@@ -67,6 +67,44 @@ test_expect_success 'root cannot link a key into the anchor keyring' '[ "$(keys_now)" = "$N_BEFORE" ]'+DM_RING=$(awk '$8 == "keyring" && $9 == ".dm-verity:" { print $1 }' /proc/keys)+[ -n "$DM_RING" ] && DM_RING=$((0x$DM_RING))+DM_KEY=$([ -n "$DM_RING" ] && keyctl rlist "$DM_RING" 2>/dev/null)+dm_keys_now() { set -- $(keyctl rlist "$DM_RING" 2>/dev/null); echo $#; }++test_expect_success 'the dm-verity keyring holds the config key alone' '+ [ -n "$DM_RING" ] && [ -n "$DM_KEY" ] &&+ [ "$(dm_keys_now)" = 1 ] &&+ keyctl rdescribe "$DM_KEY" | grep -q "^asymmetric;"+'++test_expect_success 'root cannot unlink, clear, revoke or invalidate it' '+ [ -n "$DM_RING" ] && [ -n "$DM_KEY" ] &&+ ! keyctl unlink "$DM_KEY" "$DM_RING" 2>/dev/null &&+ ! keyctl clear "$DM_RING" 2>/dev/null &&+ ! keyctl revoke "$DM_KEY" 2>/dev/null &&+ ! keyctl revoke "$DM_RING" 2>/dev/null &&+ ! keyctl invalidate "$DM_KEY" 2>/dev/null &&+ ! keyctl invalidate "$DM_RING" 2>/dev/null &&+ keyctl describe "$DM_KEY" >/dev/null &&+ [ "$(dm_keys_now)" = 1 ]+'++test_expect_success 'root cannot re-permission it or link into it' '+ [ -n "$DM_RING" ] && [ -n "$DM_KEY" ] &&+ ! keyctl setperm "$DM_KEY" 0x3f3f3f3f 2>/dev/null &&+ ! keyctl setperm "$DM_RING" 0x3f3f3f3f 2>/dev/null &&+ ! keyctl timeout "$DM_KEY" 1 2>/dev/null &&+ ! keyctl timeout "$DM_RING" 1 2>/dev/null &&+ ! keyctl chown "$DM_RING" 1 2>/dev/null &&+ k=$(keyctl add user shirudo-test-t1125 x @s) &&+ ! keyctl link "$k" "$DM_RING" 2>/dev/null &&+ keyctl unlink "$k" @s &&+ sleep 2 &&+ keyctl describe "$DM_RING" >/dev/null &&+ [ "$(dm_keys_now)" = 1 ]+'+test_expect_success 'status still finds the anchor it froze' 'm=$("$SHIRUDO_BIN" status --json 2>/dev/null | report_field keyring_serial_matches) &&[ "$m" = "True" ]diff --git a/testing/tamperproof/t1131-pr-set-mm-identity.py b/testing/tamperproof/t1131-pr-set-mm-identity.pyindex 9298839c..374517de 100644--- a/testing/tamperproof/t1131-pr-set-mm-identity.py+++ b/testing/tamperproof/t1131-pr-set-mm-identity.py@@ -79,7 +79,9 @@ class ShirudoIdentity(ctypes.Structure):("nv_writer_key", ctypes.c_int32),("nv_writer_public", ctypes.c_uint8 * 384),("ak_name", ctypes.c_uint8 * 34),- ("ak_name_len", ctypes.c_uint32)]+ ("ak_name_len", ctypes.c_uint32),+ ("dm_verity_ring", ctypes.c_int32),+ ("dm_verity_key", ctypes.c_int32)]def assert_frozen_runtime():diff --git a/testing/tamperproof/t1155-verity-boot-signers.py b/testing/tamperproof/t1155-verity-boot-signers.pyindex e0398ee3..0378de53 100644--- a/testing/tamperproof/t1155-verity-boot-signers.py+++ b/testing/tamperproof/t1155-verity-boot-signers.py@@ -87,10 +87,19 @@ def expect_error(expected, number, *arguments):raise AssertionError(f"syscall {number} unexpectedly succeeded")+def proc_keys_serial(name):+ serials = [int(line.split()[0], 16)+ for line in Path("/proc/keys").read_text().splitlines()+ if line.split()[7:9] == ["keyring", name + ":"]]+ require(len(serials) == 1, f"expected one {name} keyring: {serials}")+ return serials[0]++def config_keyring():special = -10 # KEY_SPEC_DM_VERITY_KEYRING- serial = syscall(KEYCTL, 0, special, 0, 0, 0)- require(serial > 0, "dm-verity special ID did not resolve")+ # GET_KEYRING_ID requests unpossessed SEARCH, which the guard denies.+ expect_error(errno.EACCES, KEYCTL, 0, special, 0, 0, 0)+ serial = proc_keys_serial(".dm-verity")identity = describe(special)require(identity[0] == "keyring" and identity[4] == ".dm-verity",identity)@@ -109,13 +118,14 @@ def config_keyring():name = ctypes.create_string_buffer(b".dm-verity")expect_error(errno.EPERM, ADD_KEY, ctypes.addressof(key_type),ctypes.addressof(name), 0, 0, -1)- expect_error(errno.EEXIST, KEYCTL, 29, special, 0, 0, 0)+ # The guard denies setattr and write before the restriction is reached.+ expect_error(errno.EACCES, KEYCTL, 29, special, 0, 0, 0)key_type = ctypes.create_string_buffer(b"asymmetric")der = (FIXTURES / "live.der").read_bytes()payload = ctypes.create_string_buffer(der)- expect_error(errno.EPERM, ADD_KEY, ctypes.addressof(key_type), 0,+ expect_error(errno.EACCES, ADD_KEY, ctypes.addressof(key_type), 0,ctypes.addressof(payload), len(der), special)- require(syscall(KEYCTL, 0, special, 0, 0, 0) == serial,+ require(proc_keys_serial(".dm-verity") == serial,"dm-verity serial changed")after_length = syscall(KEYCTL, 11, special, ctypes.addressof(buffer),len(buffer), 0)diff --git a/util/identity.h b/util/identity.hindex be092101..b0ea97f7 100644--- a/util/identity.h+++ b/util/identity.h@@ -31,6 +31,8 @@ struct shirudo_identity {__u8 nv_writer_public[SHIRUDO_TPM_NV_RSA_BYTES];__u8 ak_name[SHIRUDO_TPM_NAME_LEN];__u32 ak_name_len;+ __s32 dm_verity_ring;+ __s32 dm_verity_key;};#define SHIRUDO_TASK_TRUSTED 1@@ -67,7 +69,7 @@ _Static_assert(sizeof(struct shirudo_device_authority) == 48,"device authority ABI");_Static_assert(sizeof(struct shirudo_task_authority) == 8,"task authority ABI");-_Static_assert(sizeof(struct shirudo_identity) == 548,+_Static_assert(sizeof(struct shirudo_identity) == 556,"frozen identity ABI");/* The bpffs mount init made, which the unmount guard keeps mounted. */