Skip to content

eBPF-based security platform

keys: use the kernel ID for the dm-verity keyring

KP Singhbf98d20776d5
diff --git a/Documentation/requirements.rst b/Documentation/requirements.rst
index 9c3598e9..cbfbf7d3 100644
--- a/Documentation/requirements.rst
+++ b/Documentation/requirements.rst
@@ -121,14 +121,16 @@ The opt-in dm-verity mode additionally requires ``CONFIG_BLK_DEV_DM``,
and config mappings use verity format 1, SHA-256, 4096-byte blocks and a
standard verity superblock. ``CONFIG_DM_VERITY_VERIFY_ROOTHASH_SIG`` is
required for the config root signature. The kernel must provide the
-``.dm-verity`` keyring enrollment and sealing API. Image boot uses
+``.dm-verity`` keyring enrollment and sealing API, including
+``KEY_SPEC_DM_VERITY_KEYRING``. The special keyring ID was added by upstream
+commit ``1041fca2ee8a`` (``keys: add KEY_SPEC_DM_VERITY_KEYRING``). Apply that
+commit if the selected kernel does not yet contain it. Image boot uses
``dm_verity.keyring_unsealed=1`` so the trusted initrd can enroll its config
certificate and seal that keyring. Built-in trusted certificates remain
part of the kernel's dm-verity trust path. The runtime must match the exact
UKI-authenticated root; config-signing trust does not authorize executables.
-Image mode uses existing upstream kernel APIs and carries no kernel
-patches. Existing hooks do not establish every desired protection against
+Image enforcement uses existing upstream LSM hooks. Existing hooks do not establish every desired protection against
hostile device-mapper controls or mount-tree changes; see the limitations in
`config-images <config-images.rst>`__. BPF signing keys are enrolled at boot
before reject-all keyring sealing. Ordinary installations do not enable
@@ -399,7 +401,8 @@ binary, as described in `operations <operations.rst>`__.
-------------------------------
Use the upstream development-tree ABI and options described above. Image
-mode uses existing LSM hooks and adds no kernel patches. The
+mode uses existing LSM hooks. A kernel predating
+``KEY_SPEC_DM_VERITY_KEYRING`` needs the upstream keyring-ID commit above. The
`config-images <config-images.rst>`__ guide describes the two policy modes,
test commands and remaining protection gaps. Run both complete policy-mode
suites when validating a deployment.
diff --git a/util/keyring.c b/util/keyring.c
index acd646ee..726658ee 100644
--- a/util/keyring.c
+++ b/util/keyring.c
@@ -6,7 +6,6 @@
*/
#include <errno.h>
-#include <limits.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stddef.h>
@@ -336,60 +335,29 @@ int keyring_bpf_seal(void)
return 0;
}
-/* The kernel ring is unpossessed; discover it only during trusted boot. */
+/* Resolve the kernel-owned ring directly, without a name-based search. */
key_serial_t shirudo_keyring_dm_verity_serial(void)
{
- static const char expected[] = "keyring;0;0;082f0000;.dm-verity";
- char line[1024], flags[8], expiry[32], type[32], name[64];
- __u32 serial, usage, permissions, uid, gid;
- key_serial_t found = -ENOKEY;
- char description[128];
- FILE *file;
- int rc;
+ long ret = sys_keyctl(KEYCTL_GET_KEYRING_ID,
+ KEY_SPEC_DM_VERITY_KEYRING, 0, 0, 0);
- file = fopen("/proc/keys", "re");
- if (!file)
- return -errno;
- while (fgets(line, sizeof(line), file)) {
- if (sscanf(line, "%x %7s %u %31s %x %u %u %31s %63s",
- &serial, flags, &usage, expiry, &permissions,
- &uid, &gid, type, name) != 9)
- continue;
- if (!serial || serial > INT_MAX || uid || gid ||
- permissions != 0x082f0000 || strchr(flags, 'Q') ||
- strcmp(type, "keyring") || strcmp(name, ".dm-verity:"))
- continue;
- rc = describe_key(serial, description, sizeof(description));
- if (rc || strcmp(description, expected))
- continue;
- if (found >= 0) {
- found = -EEXIST;
- break;
- }
- found = serial;
- }
- if (ferror(file))
- found = -EIO;
- fclose(file);
- return found;
+ return ret < 0 ? -errno : (key_serial_t)ret;
}
key_serial_t shirudo_keyring_dm_verity_add_cert(const __u8 *cert,
size_t cert_len)
{
- key_serial_t ring = shirudo_keyring_dm_verity_serial();
+ long ret = sys_add_key("asymmetric", NULL, cert, cert_len,
+ KEY_SPEC_DM_VERITY_KEYRING);
- return ring < 0 ? ring : keyring_add_cert(ring, cert, cert_len);
+ return ret < 0 ? -errno : (key_serial_t)ret;
}
int shirudo_keyring_dm_verity_seal(void)
{
- key_serial_t ring = shirudo_keyring_dm_verity_serial();
-
- if (ring < 0)
- return ring;
/* No restriction type: reject every subsequent addition. */
- if (sys_keyctl(KEYCTL_RESTRICT_KEYRING, ring, 0, 0, 0) < 0)
+ if (sys_keyctl(KEYCTL_RESTRICT_KEYRING, KEY_SPEC_DM_VERITY_KEYRING,
+ 0, 0, 0) < 0)
return -errno;
return 0;
}
diff --git a/util/keyring.h b/util/keyring.h
index 319ecc8b..b016cf2a 100644
--- a/util/keyring.h
+++ b/util/keyring.h
@@ -85,7 +85,12 @@ key_serial_t keyring_bpf_add_cert(const __u8 *cert,
/* Seal it for the rest of the boot: no key can be linked afterwards. */
int keyring_bpf_seal(void);
-/* Trusted initrd only: discover the kernel-created .dm-verity keyring. */
+/* The kernel-owned config trust anchor, populated only by trusted initrd. */
+#ifndef KEY_SPEC_DM_VERITY_KEYRING
+#define KEY_SPEC_DM_VERITY_KEYRING (-10)
+#endif
+
+/* Resolve the special ID; never fall back to a same-named user keyring. */
key_serial_t shirudo_keyring_dm_verity_serial(void);
key_serial_t shirudo_keyring_dm_verity_add_cert(const __u8 *cert,
size_t cert_len);