Skip to content

eBPF-based security platform

init: require each sealed keyring to hold exactly its certificates

Daniel Borkmann9682513a8a47
diff --git a/Documentation/threat-model.rst b/Documentation/threat-model.rst
index 4559371e..710fd1c9 100644
--- a/Documentation/threat-model.rst
+++ b/Documentation/threat-model.rst
@@ -214,7 +214,10 @@ T3 Tampering with shirudo's files and process
``KEYCTL_RESTRICT_KEYRING`` so no key can be linked into it for the
rest of the boot, ``lsm/key_permission`` leaves writing, unlinking,
revoking, invalidating and re-permissioning it and the keys it
- holds to tasks with runtime authority. The kernel refuses caller-supplied
+ holds to tasks with runtime authority. Once sealed it must hold exactly
+ the trusted certificates: init refuses a keyring holding any other link,
+ including a nested keyring the kernel's signature search would enter or
+ a key it cannot describe, and one restricted by another task. The kernel refuses caller-supplied
keyrings for signature verification. The BPF ring serial and enrolled key
serials are frozen into the pinned identity map and checked by later loads.
@@ -389,7 +392,8 @@ T11 Image-mode config and executable substitution
roots carry a detached ML-DSA signature over their 64 lowercase ASCII
hex bytes, verified by the kernel's dm-verity trust path, including
built-in trusted keys. The initrd enrolls its config certificate and
- seals the dedicated ``.dm-verity`` keyring. A signed config never grants
+ seals the dedicated ``.dm-verity`` keyring, which must then hold exactly
+ that certificate. A signed config never grants
runtime executable authority. Shirudo validates the standard superblock,
fixed format and exact backing-file sizes, creates or validates a strict
read-only mapping and reads every data block before parsing contents or
diff --git a/Makefile.shirudo b/Makefile.shirudo
index 325c4ecf..eee6a60e 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -630,6 +630,7 @@ TEST_HELPERS := $(VERITY_TEST_HELPERS) \
testing/upatch/kernel-obs/exec/exec-paths
TEST_HELPERS += testing/helper/test-nv-writer
TEST_HELPERS += testing/helper/test-nv-writer-cleanup
+TEST_HELPERS += testing/helper/test-keyring-seal
TEST_HELPERS += testing/helper/test-tpm-preopened
testing/helper/test-json-parser: testing/helper/test-json-parser.c util/json.o util/hex.o \
@@ -686,6 +687,16 @@ testing/helper/test-nv-writer-cleanup: testing/helper/test-nv-writer-cleanup.c \
-Wl,--wrap=shirudo_image_runtime_root \
$(STATIC_LIBS) $(DYNAMIC_LIBS)
+testing/helper/test-keyring-seal: testing/helper/test-keyring-seal.c \
+ testing/helper/keyring-seal.h builtin-init.c \
+ $(filter-out $(call obj,builtin-init.o),$(BUILTIN_OBJS)) \
+ $(UTIL_OBJS) $(VERSION_OBJ) $(LIBBPF_A)
+ @printf ' CC $@\n'
+ $(Q)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< \
+ $(filter %.o %.a,$^) \
+ -Wl,--wrap=keyring_bpf_add_cert -Wl,--wrap=keyring_bpf_seal \
+ $(STATIC_LIBS) $(DYNAMIC_LIBS)
+
testing/helper/test-tpm-preopened: testing/helper/test-tpm-preopened.c \
$(REGISTER_OBJS)
@printf ' CC $@\n'
diff --git a/builtin-init.c b/builtin-init.c
index bc824587..883ada73 100644
--- a/builtin-init.c
+++ b/builtin-init.c
@@ -76,27 +76,6 @@ static void print_preflight(const struct shirudo_preflight_result *r)
r->n - r->n_failed, r->n, r->n_failed);
}
-static void keyring_count_asymmetric(key_serial_t serial, const char *type,
- const char *description, void *ud)
-{
- size_t *n = ud;
-
- (void)serial;
- (void)description;
- if (strcmp(type, "asymmetric") == 0)
- (*n)++;
-}
-
-static int keyring_asymmetric_count(key_serial_t ring)
-{
- size_t n = 0;
- int rc = keyring_list(ring, keyring_count_asymmetric, &n);
-
- if (rc)
- return rc;
- return (int)n;
-}
-
static struct shirudo_trusted_set enrolled;
static bool initrd_mode;
@@ -104,40 +83,37 @@ static bool initrd_mode;
static int keyring_holds_set(key_serial_t ring,
const struct shirudo_trusted_set *set)
{
- size_t i, held = 0;
+ struct keyring_cert certs[SHIRUDO_KEYS_MAX];
+ size_t i;
+ int rc;
for (i = 0; i < set->n; i++) {
- const __u8 *der = NULL;
- size_t der_sz = 0;
- int rc;
-
- rc = keys_cert_der(&set->certs[i], &der, &der_sz);
+ rc = keys_cert_der(&set->certs[i], &certs[i].der,
+ &certs[i].len);
if (rc)
return rc;
- rc = keyring_holds_cert(ring, der, der_sz);
- if (rc < 0)
- return rc;
- held += rc;
}
- return (int)held;
+ return keyring_holds_exactly(ring, certs, set->n);
}
/*
* The bpf keyring is the trust anchor a signed loader is verified
* against once it is sealed. Booted with bpf.keyring_unsealed=1 it
* takes keys until the first seal, which is init's job from the initrd:
- * enrol every trusted certificate and seal. Found sealed, it either
- * holds exactly that set (-EEXIST: use it) or was sealed empty by the
- * kernel at boot (-ENOKEY: initialization refuses until reboot with the
- * enrollment parameter); sealed around anything else, it is refused.
+ * enrol every trusted certificate and seal. Found sealed, it was sealed
+ * by init earlier this boot (-EEXIST: use it) or empty by the kernel at
+ * boot (-ENOKEY). Either way it must then hold exactly the trusted set;
+ * a key another task linked before the seal, or a restriction init did
+ * not install, is refused.
*/
static int init_bpf_keyring(key_serial_t ring,
const struct shirudo_trusted_set *set)
{
- int count, held, rc;
+ bool sealed = false;
size_t i;
+ int rc;
- for (i = 0; i < set->n; i++) {
+ for (i = 0; i < set->n && !sealed; i++) {
const __u8 *der = NULL;
size_t der_sz = 0;
key_serial_t key;
@@ -172,36 +148,44 @@ static int init_bpf_keyring(key_serial_t ring,
(int)ring);
return -EKEYREJECTED;
}
- /* Sealed before init: by init earlier this boot, or not. */
- held = keyring_holds_set(ring, set);
- if (held < 0) {
- pr_err("bpf keyring: compare: %s", strerror(-held));
- return held;
- }
- count = keyring_asymmetric_count(ring);
- if (count < 0) {
- pr_err("bpf keyring: list: %s", strerror(-count));
- return count;
+ sealed = true;
+ }
+ if (!sealed) {
+ rc = keyring_bpf_seal();
+ if (rc == -EEXIST) {
+ pr_err("bpf keyring serial=%d carries a restriction "
+ "init did not install (tampered)", (int)ring);
+ return -EKEYREJECTED;
}
- if (count == 0)
- return -ENOKEY;
- if ((size_t)held == set->n && (size_t)count == set->n) {
- pr_info("bpf keyring serial=%d: already holds the %zu "
- "trusted certificate%s, sealed this boot",
- (int)ring, set->n, set->n == 1 ? "" : "s");
- return -EEXIST;
+ if (rc) {
+ pr_err("bpf keyring: certificates enrolled but the "
+ "keyring could not be sealed: %s",
+ strerror(-rc));
+ return rc;
}
- pr_err("bpf keyring serial=%d is sealed around %d key%s, "
- "%d of the %zu trusted certificates (tampered)",
- (int)ring, count, count == 1 ? "" : "s", held, set->n);
- return -EKEYREJECTED;
}
- rc = keyring_bpf_seal();
- if (rc) {
- pr_err("bpf keyring: certificates enrolled but the keyring "
- "could not be sealed: %s", strerror(-rc));
+ rc = keyring_holds_set(ring, set);
+ if (rc < 0) {
+ pr_err("bpf keyring: compare: %s", strerror(-rc));
return rc;
}
+ if (!rc && sealed && keyring_holds_exactly(ring, NULL, 0) == 1) {
+ pr_err("bpf keyring serial=%d was sealed empty at boot; boot "
+ "with bpf.keyring_unsealed=1", (int)ring);
+ return -ENOKEY;
+ }
+ if (!rc) {
+ pr_err("bpf keyring serial=%d is sealed around keys other than "
+ "the %zu trusted certificate%s (tampered)", (int)ring,
+ set->n, set->n == 1 ? "" : "s");
+ return -EKEYREJECTED;
+ }
+ if (sealed) {
+ pr_info("bpf keyring serial=%d: already holds the %zu trusted "
+ "certificate%s, sealed this boot", (int)ring, set->n,
+ set->n == 1 ? "" : "s");
+ return -EEXIST;
+ }
pr_info("bpf keyring serial=%d: %zu certificate%s enrolled and sealed",
(int)ring, set->n, set->n == 1 ? "" : "s");
return 0;
diff --git a/contrib/build-test-vm-bundle.sh b/contrib/build-test-vm-bundle.sh
index 5736adf4..e53f8623 100755
--- a/contrib/build-test-vm-bundle.sh
+++ b/contrib/build-test-vm-bundle.sh
@@ -334,6 +334,7 @@ testing/helper/test-tpm-wire
testing/helper/test-nv-cphash
testing/helper/test-nv-writer
testing/helper/test-nv-writer-cleanup
+testing/helper/test-keyring-seal
testing/helper/test-tpm-preopened
testing/helper/test-config
testing/helper/test-bpf-load
diff --git a/testing/helper/keyring-seal.h b/testing/helper/keyring-seal.h
new file mode 100644
index 00000000..3bf5a0f2
--- /dev/null
+++ b/testing/helper/keyring-seal.h
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+/* Copyright (c) 2026 Authors of shirudo */
+
+#ifndef SHIRUDO_TEST_KEYRING_SEAL_H
+#define SHIRUDO_TEST_KEYRING_SEAL_H
+
+#include <stddef.h>
+
+#include "util/keyring.h"
+
+key_serial_t __wrap_keyring_bpf_add_cert(const __u8 *cert, size_t cert_len);
+int __wrap_keyring_bpf_seal(void);
+
+#endif /* SHIRUDO_TEST_KEYRING_SEAL_H */
diff --git a/testing/helper/test-keyring-seal.c b/testing/helper/test-keyring-seal.c
new file mode 100644
index 00000000..4fa3adc0
--- /dev/null
+++ b/testing/helper/test-keyring-seal.c
@@ -0,0 +1,193 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+#include <linux/keyctl.h>
+
+#include "../../builtin-init.c"
+#include "keyring-seal.h"
+
+/* init's enrollment runs against a private keyring, not the sealed .bpf. */
+static key_serial_t test_ring;
+
+key_serial_t __wrap_keyring_bpf_add_cert(const __u8 *cert, size_t cert_len)
+{
+ long ret = syscall(__NR_add_key, "asymmetric", NULL, cert, cert_len,
+ test_ring);
+
+ return ret < 0 ? -errno : (key_serial_t)ret;
+}
+
+int __wrap_keyring_bpf_seal(void)
+{
+ if (syscall(__NR_keyctl, KEYCTL_RESTRICT_KEYRING, test_ring, 0, 0,
+ 0) < 0)
+ return -errno;
+ return 0;
+}
+
+static key_serial_t add(const char *type, const char *description,
+ const void *payload, size_t len, key_serial_t ring)
+{
+ long ret = syscall(__NR_add_key, type, description, payload, len,
+ ring);
+
+ return ret < 0 ? -errno : (key_serial_t)ret;
+}
+
+static int read_cert(const char *path, struct keyring_cert *cert)
+{
+ __u8 *der = NULL;
+ size_t len = 0;
+ int rc;
+
+ rc = keys_read_der(path, &der, &len);
+ if (rc)
+ return rc;
+ cert->der = der;
+ cert->len = len;
+ return 0;
+}
+
+static int expect(const char *what, int got, int want)
+{
+ if (got == want)
+ return 0;
+ fprintf(stderr, "%s: got %d, want %d\n", what, got, want);
+ return 1;
+}
+
+/* Every link counts, of any type and whether it can be described. */
+static int exact_case(const struct keyring_cert *a,
+ const struct keyring_cert *b)
+{
+ struct keyring_cert both[2] = { *a, *b }, swapped[2] = { *b, *a };
+ struct keyring_cert twice[2] = { *a, *a };
+ key_serial_t ring, key;
+ int fails = 0;
+
+ ring = add("keyring", "shirudo-test-exact", NULL, 0,
+ KEY_SPEC_THREAD_KEYRING);
+ if (ring < 0)
+ return ring;
+ fails += expect("empty, none", keyring_holds_exactly(ring, NULL, 0), 1);
+ fails += expect("empty, one", keyring_holds_exactly(ring, a, 1), 0);
+ if (add("asymmetric", NULL, a->der, a->len, ring) < 0)
+ return -EIO;
+ fails += expect("one, one", keyring_holds_exactly(ring, a, 1), 1);
+ fails += expect("one, none", keyring_holds_exactly(ring, NULL, 0), 0);
+ fails += expect("one, two", keyring_holds_exactly(ring, both, 2), 0);
+ if (add("asymmetric", NULL, b->der, b->len, ring) < 0)
+ return -EIO;
+ fails += expect("two, two", keyring_holds_exactly(ring, both, 2), 1);
+ fails += expect("two, swapped",
+ keyring_holds_exactly(ring, swapped, 2), 1);
+ fails += expect("two, twice", keyring_holds_exactly(ring, twice, 2), 0);
+ fails += expect("two, one", keyring_holds_exactly(ring, a, 1), 0);
+
+ key = add("keyring", "shirudo-test-nested", NULL, 0, ring);
+ fails += expect("nested keyring",
+ key < 0 ? key : keyring_holds_exactly(ring, both, 2), 0);
+ if (key > 0)
+ keyring_unlink_key(ring, key);
+ key = add("user", "shirudo-test-user", "x", 1, ring);
+ fails += expect("user key",
+ key < 0 ? key : keyring_holds_exactly(ring, both, 2), 0);
+ if (key > 0)
+ keyring_unlink_key(ring, key);
+ key = add("user", "shirudo-test-hidden", "x", 1, ring);
+ if (key > 0 && syscall(__NR_keyctl, KEYCTL_SETPERM, key, 0, 0, 0) < 0)
+ key = -errno;
+ fails += expect("key hidden from view",
+ key < 0 ? key : keyring_holds_exactly(ring, both, 2), 0);
+ if (key > 0)
+ keyring_unlink_key(ring, key);
+ fails += expect("two again", keyring_holds_exactly(ring, both, 2), 1);
+ keyring_unlink_key(KEY_SPEC_THREAD_KEYRING, ring);
+ return fails ? -EKEYREJECTED : 0;
+}
+
+/* <set> is the trusted set init enrols; <other> a certificate outside it. */
+static int seal_case(const char *mode,
+ const struct shirudo_trusted_set *set,
+ const struct keyring_cert *own,
+ const struct keyring_cert *other)
+{
+ int rc, fails = 0;
+
+ test_ring = add("keyring", "shirudo-test-seal", NULL, 0,
+ KEY_SPEC_THREAD_KEYRING);
+ if (test_ring < 0)
+ return test_ring;
+ if (!strcmp(mode, "fresh")) {
+ fails += expect("enrol and seal",
+ init_bpf_keyring(test_ring, set), 0);
+ fails += expect("found sealed with the set",
+ init_bpf_keyring(test_ring, set), -EEXIST);
+ } else if (!strcmp(mode, "foreign")) {
+ if (add("asymmetric", NULL, other->der, other->len,
+ test_ring) < 0)
+ return -EIO;
+ fails += expect("foreign key sealed in",
+ init_bpf_keyring(test_ring, set), -EKEYREJECTED);
+ fails += expect("found sealed with a foreign key",
+ init_bpf_keyring(test_ring, set), -EKEYREJECTED);
+ } else if (!strcmp(mode, "nested")) {
+ if (add("keyring", "shirudo-test-nested", NULL, 0,
+ test_ring) < 0)
+ return -EIO;
+ fails += expect("nested keyring sealed in",
+ init_bpf_keyring(test_ring, set), -EKEYREJECTED);
+ } else if (!strcmp(mode, "restricted")) {
+ if (add("asymmetric", NULL, own->der, own->len, test_ring) < 0 ||
+ syscall(__NR_keyctl, KEYCTL_RESTRICT_KEYRING, test_ring,
+ "asymmetric", "key_or_keyring:0:chain", 0) < 0)
+ return -EIO;
+ rc = init_bpf_keyring(test_ring, set);
+ fails += expect("restriction init did not install",
+ rc == 0 || rc == -EEXIST, 0);
+ } else if (!strcmp(mode, "empty")) {
+ if (__wrap_keyring_bpf_seal())
+ return -EIO;
+ fails += expect("sealed empty",
+ init_bpf_keyring(test_ring, set), -ENOKEY);
+ } else {
+ return -EINVAL;
+ }
+ keyring_unlink_key(KEY_SPEC_THREAD_KEYRING, test_ring);
+ return fails ? -EKEYREJECTED : 0;
+}
+
+int main(int argc, char **argv)
+{
+ struct keyring_cert own = {}, other = {};
+ struct shirudo_trusted_set set = {};
+ char keys[PATH_MAX];
+ int rc;
+
+ if (argc != 5) {
+ fprintf(stderr, "usage: %s exact|fresh|foreign|nested|"
+ "restricted|empty <keys-dir> <revoked-dir> <other.der>\n",
+ argv[0]);
+ return 2;
+ }
+ if (strsprintf(keys, sizeof(keys), "%s/%s", argv[2],
+ SHIRUDO_CERT_FILE) < 0)
+ return 2;
+ rc = read_cert(keys, &own);
+ if (!rc)
+ rc = read_cert(argv[4], &other);
+ if (!rc && strcmp(argv[1], "exact"))
+ rc = keys_load(argv[2], argv[3], false, &set);
+ if (!rc)
+ rc = strcmp(argv[1], "exact") ?
+ seal_case(argv[1], &set, &own, &other) :
+ exact_case(&own, &other);
+ if (rc)
+ fprintf(stderr, "keyring seal test %s: %s\n", argv[1],
+ strerror(-rc));
+ if (own.der)
+ xfree((void *)own.der, own.len);
+ if (other.der)
+ xfree((void *)other.der, other.len);
+ return rc ? 1 : 0;
+}
diff --git a/testing/run-target-tests.sh b/testing/run-target-tests.sh
index 86a7498e..4a0da0a8 100755
--- a/testing/run-target-tests.sh
+++ b/testing/run-target-tests.sh
@@ -84,6 +84,7 @@ for t in \
tamperproof/t1166-kexec.sh \
tamperproof/t1168-proc-substitution.sh \
tamperproof/t1169-register-lock.sh \
+ tamperproof/t1170-keyring-seal.sh \
tamperproof/t1150-verity-devices.sh \
tamperproof/t1151-verity-process.sh \
tamperproof/t1153-verity-hidden.sh \
diff --git a/testing/tamperproof/t1170-keyring-seal.sh b/testing/tamperproof/t1170-keyring-seal.sh
new file mode 100755
index 00000000..776a17ef
--- /dev/null
+++ b/testing/tamperproof/t1170-keyring-seal.sh
@@ -0,0 +1,53 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+#
+# t1170 -- a sealed keyring must hold exactly the certificates init
+# enrolled. On a private keyring standing in for the bpf one, init
+# refuses a foreign key, a nested keyring and a restriction it did not
+# install, and the exact check counts every link, including a key that
+# cannot be described.
+
+cd "$(dirname "$0")/.."
+. ./integration-lib.sh
+target_guard
+
+H=./helper/test-keyring-seal
+FIXTURES=${SHIRUDO_TEST_FIXTURES_DIR:?target fixture directory is not set}
+[ -x "$H" ] || test_skip_all "missing $H"
+[ -s "$FIXTURES/cert.der" ] && [ -s "$FIXTURES/own.der" ] ||
+ test_skip_all "missing certificate fixtures"
+W=$(mktemp -d)
+trap 'rm -rf "$W"' EXIT
+mkdir "$W/keys" "$W/revoked" &&
+ cp "$FIXTURES/cert.der" "$W/keys/cert.der" || exit 1
+
+run() {
+ "$H" "$1" "$W/keys" "$W/revoked" "$FIXTURES/own.der"
+}
+
+test_expect_success 'the exact check counts every link of a keyring' '
+ run exact
+'
+
+test_expect_success 'init enrols and seals, then finds the keyring sealed' '
+ run fresh
+'
+
+test_expect_success 'init refuses a key linked before its seal' '
+ run foreign
+'
+
+test_expect_success 'init refuses a nested keyring linked before its seal' '
+ run nested
+'
+
+test_expect_success 'init refuses a restriction it did not install' '
+ run restricted
+'
+
+test_expect_success 'init reports a keyring the kernel sealed empty' '
+ run empty
+'
+
+test_done
diff --git a/util/image_runtime.c b/util/image_runtime.c
index de777f70..5a95c0b8 100644
--- a/util/image_runtime.c
+++ b/util/image_runtime.c
@@ -702,6 +702,42 @@ static int boot_keys_error(const char *step, int rc)
return rc;
}
+/*
+ * The kernel trusts every key in the sealed keyring for config roots, so
+ * whether init sealed it now or earlier this boot, it must hold exactly
+ * the config certificate.
+ */
+static int seal_config_key(key_serial_t ring, const __u8 *der, size_t size)
+{
+ struct keyring_cert cert = { .der = der, .len = size };
+ key_serial_t key;
+ int rc;
+
+ key = shirudo_keyring_dm_verity_add_cert(der, size);
+ if (key < 0 && key != -EPERM && key != -EACCES)
+ return boot_keys_error("add config verification certificate",
+ (int)key);
+ if (key < 0 && keyring_holds_exactly(ring, NULL, 0) == 1)
+ return boot_keys_error("dm-verity keyring sealed empty at boot; "
+ "boot with dm_verity.keyring_unsealed=1",
+ -ENOKEY);
+ if (key >= 0) {
+ rc = shirudo_keyring_dm_verity_seal();
+ if (rc == -EEXIST)
+ return boot_keys_error("seal dm-verity keyring restricted "
+ "by another task (tampered)",
+ -EKEYREJECTED);
+ if (rc)
+ return boot_keys_error("seal dm-verity keyring", rc);
+ }
+ rc = keyring_holds_exactly(ring, &cert, 1);
+ if (rc <= 0)
+ return boot_keys_error("check dm-verity keyring holds only the "
+ "config certificate",
+ rc ? rc : -EKEYREJECTED);
+ return 0;
+}
+
static int enroll_config_key(void)
{
__u8 *der = NULL;
@@ -719,22 +755,9 @@ static int enroll_config_key(void)
xfree(der, size);
return boot_keys_error("lookup dm-verity keyring", rc);
}
- rc = keyring_holds_cert(ring, der, size);
- if (rc < 0) {
- xfree(der, size);
- return boot_keys_error("check config certificate enrollment", rc);
- }
- if (!rc)
- rc = shirudo_keyring_dm_verity_add_cert(der, size);
+ rc = seal_config_key(ring, der, size);
xfree(der, size);
- if (rc < 0)
- return boot_keys_error("add config verification certificate", rc);
- rc = shirudo_keyring_dm_verity_seal();
- if (rc == -EEXIST)
- return 0;
- if (rc)
- return boot_keys_error("seal dm-verity keyring", rc);
- return 0;
+ return rc;
}
/* The authenticated initrd validates the boot set before installing guards. */
diff --git a/util/keyring.c b/util/keyring.c
index 6a686889..dba88515 100644
--- a/util/keyring.c
+++ b/util/keyring.c
@@ -18,6 +18,7 @@
#include <linux/keyctl.h>
#include <shirudo/misc.h>
+#include <shirudo/string.h>
#include "keyring.h"
#include "io.h"
@@ -28,6 +29,8 @@
#define KEYCTL_RESTRICT_KEYRING 29
#endif
+#define KEYRING_DESC_MAX 512
+
static long sys_add_key(const char *type, const char *desc,
const void *payload, size_t plen, key_serial_t ring)
{
@@ -71,13 +74,13 @@ static void split_describe(char *desc, const char **type,
*description = p;
}
-int keyring_list(key_serial_t keyring,
- shirudo_keyring_cb cb, void *ud)
+/* Every link of <keyring>, whatever its type; freed with xfree(, *size). */
+static int read_links(key_serial_t keyring, key_serial_t **out, size_t *n,
+ size_t *size)
{
key_serial_t *serials = NULL;
size_t cap = 64 * sizeof(*serials);
long ret;
- size_t n, i;
serials = xmalloc(cap);
if (!serials)
@@ -106,17 +109,31 @@ int keyring_list(key_serial_t keyring,
/*
* The keyring can grow again between the two reads;
* KEYCTL_READ then returns the full size while filling only
- * cap bytes. Clamp to what the buffer actually holds so the
- * loop below never reads past the allocation.
+ * cap bytes. Clamp to what the buffer actually holds so no
+ * caller reads past the allocation.
*/
if ((size_t)ret > cap)
ret = (long)cap;
}
+ *out = serials;
+ *n = (size_t)ret / sizeof(*serials);
+ *size = cap;
+ return 0;
+}
+
+int keyring_list(key_serial_t keyring,
+ shirudo_keyring_cb cb, void *ud)
+{
+ key_serial_t *serials = NULL;
+ size_t n = 0, cap = 0, i;
+ int rc;
- n = (size_t)ret / sizeof(*serials);
+ rc = read_links(keyring, &serials, &n, &cap);
+ if (rc)
+ return rc;
for (i = 0; i < n; i++) {
- char desc[512];
const char *type, *description;
+ char desc[KEYRING_DESC_MAX];
if (describe_key(serials[i], desc, sizeof(desc)) != 0)
continue;
@@ -127,8 +144,29 @@ int keyring_list(key_serial_t keyring,
return 0;
}
+/* The description the kernel gives the key it parses from <der>. */
+static int cert_description(const __u8 *der, size_t der_len,
+ char out[KEYRING_DESC_MAX])
+{
+ const char *type, *description;
+ char desc[KEYRING_DESC_MAX];
+ long probe;
+ int rc;
+
+ probe = sys_add_key("asymmetric", NULL, der, der_len,
+ KEY_SPEC_THREAD_KEYRING);
+ if (probe < 0)
+ return -errno;
+ rc = describe_key((key_serial_t)probe, desc, sizeof(desc));
+ sys_keyctl(KEYCTL_UNLINK, probe, KEY_SPEC_THREAD_KEYRING, 0, 0);
+ if (rc)
+ return rc;
+ split_describe(desc, &type, &description);
+ return strscpy(out, description, KEYRING_DESC_MAX) < 0 ? -ERANGE : 0;
+}
+
struct holds_cert_ctx {
- char want[512];
+ char want[KEYRING_DESC_MAX];
key_serial_t serial;
bool found;
};
@@ -149,25 +187,11 @@ int keyring_holds_cert(key_serial_t keyring,
const __u8 *der, size_t der_len)
{
struct holds_cert_ctx ctx = {};
- const char *type, *description;
- char desc[512];
- long probe;
int rc;
- probe = sys_add_key("asymmetric", NULL, der, der_len,
- KEY_SPEC_THREAD_KEYRING);
- if (probe < 0)
- return -errno;
- rc = describe_key((key_serial_t)probe, desc, sizeof(desc));
- sys_keyctl(KEYCTL_UNLINK, probe, KEY_SPEC_THREAD_KEYRING, 0, 0);
- if (rc)
- return rc;
- split_describe(desc, &type, &description);
- if (strlen(description) >= sizeof(ctx.want))
- return -ERANGE;
- memcpy(ctx.want, description, strlen(description) + 1);
-
- rc = keyring_list(keyring, holds_cert_cb, &ctx);
+ rc = cert_description(der, der_len, ctx.want);
+ if (!rc)
+ rc = keyring_list(keyring, holds_cert_cb, &ctx);
if (rc)
return rc;
return ctx.found ? 1 : 0;
@@ -178,29 +202,73 @@ key_serial_t keyring_find_cert(key_serial_t keyring,
size_t der_len)
{
struct holds_cert_ctx ctx = {};
- const char *type, *description;
- char desc[512];
- long probe;
int rc;
- probe = sys_add_key("asymmetric", NULL, der, der_len,
- KEY_SPEC_THREAD_KEYRING);
- if (probe < 0)
- return -errno;
- rc = describe_key((key_serial_t)probe, desc, sizeof(desc));
- sys_keyctl(KEYCTL_UNLINK, probe, KEY_SPEC_THREAD_KEYRING, 0, 0);
- if (rc)
- return rc;
- split_describe(desc, &type, &description);
- if (strlen(description) >= sizeof(ctx.want))
- return -ERANGE;
- memcpy(ctx.want, description, strlen(description) + 1);
- rc = keyring_list(keyring, holds_cert_cb, &ctx);
+ rc = cert_description(der, der_len, ctx.want);
+ if (!rc)
+ rc = keyring_list(keyring, holds_cert_cb, &ctx);
if (rc)
return rc;
return ctx.found ? ctx.serial : -ENOKEY;
}
+/*
+ * Every link counts, whatever its type and whether it can be described:
+ * the kernel's signature check searches a nested keyring, which the
+ * seal does not cover, and a key hidden from view still verifies.
+ */
+int keyring_holds_exactly(key_serial_t keyring,
+ const struct keyring_cert *certs, size_t n)
+{
+ char (*want)[KEYRING_DESC_MAX] = NULL;
+ size_t links = 0, cap = 0, i, j;
+ key_serial_t *serials = NULL;
+ bool *matched = NULL;
+ int rc = -ENOMEM;
+
+ if (n) {
+ want = xcalloc(n, sizeof(*want));
+ matched = xcalloc(n, sizeof(*matched));
+ if (!want || !matched)
+ goto out;
+ }
+ for (i = 0; i < n; i++) {
+ rc = cert_description(certs[i].der, certs[i].len, want[i]);
+ if (rc)
+ goto out;
+ }
+ rc = read_links(keyring, &serials, &links, &cap);
+ if (rc)
+ goto out;
+ if (links != n)
+ goto out;
+ for (i = 0; i < links; i++) {
+ const char *type, *description;
+ char desc[KEYRING_DESC_MAX];
+
+ if (describe_key(serials[i], desc, sizeof(desc)))
+ goto out;
+ split_describe(desc, &type, &description);
+ if (strcmp(type, "asymmetric"))
+ goto out;
+ for (j = 0; j < n; j++)
+ if (!matched[j] && !strcmp(description, want[j]))
+ break;
+ if (j == n)
+ goto out;
+ matched[j] = true;
+ }
+ rc = 1;
+out:
+ if (serials)
+ xfree(serials, cap);
+ if (want)
+ xfree(want, n * sizeof(*want));
+ if (matched)
+ xfree(matched, n * sizeof(*matched));
+ return rc;
+}
+
int keyring_unlink_key(key_serial_t keyring, key_serial_t key)
{
if (sys_keyctl(KEYCTL_UNLINK, key, keyring, 0, 0) < 0)
diff --git a/util/keyring.h b/util/keyring.h
index a626ef99..5d4947b6 100644
--- a/util/keyring.h
+++ b/util/keyring.h
@@ -30,6 +30,20 @@ typedef void (*shirudo_keyring_cb)(key_serial_t serial,
int keyring_holds_cert(key_serial_t keyring,
const __u8 *der, size_t der_len);
+struct keyring_cert {
+ const __u8 *der;
+ size_t len;
+};
+
+/*
+ * Whether <keyring> holds exactly the <n> certificates and nothing
+ * else: 1 if so, 0 if not, -errno. Two certificates the kernel
+ * describes alike cannot both be held; the later link replaces the
+ * earlier one.
+ */
+int keyring_holds_exactly(key_serial_t keyring,
+ const struct keyring_cert *certs, size_t n);
+
/* The serial of <keyring>'s key for the certificate, -ENOKEY, -errno. */
key_serial_t keyring_find_cert(key_serial_t keyring,
const __u8 *der,