Skip to content

eBPF-based security platform

status: name the keyring the anchor is in

Daniel Borkmann1f6bb334a5a6
diff --git a/Documentation/operations.rst b/Documentation/operations.rst
index 2e749677..0430495d 100644
--- a/Documentation/operations.rst
+++ b/Documentation/operations.rst
@@ -96,7 +96,8 @@ Conditions
- ``WARN`` while staging or before ``init``; ``ALERT`` when
the live keyring is not the one ``init`` froze into the
identity map (a swapped keyring: every load refuses
- ``tampered``).
+ ``tampered``). The detail names the kind of keyring,
+ ``bpf`` or ``user``.
* - ``ChainIntact``
- ``ALERT`` when replaying this boot's eventlog does not
reproduce the live register: something extended it that
diff --git a/Makefile.shirudo b/Makefile.shirudo
index 2d2b6740..ed587911 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -526,6 +526,7 @@ TPMSIM_HELPER := $(if $(TPMSIM_LIBS),testing/helper/tpmsim-cmd,)
# Compiled test helpers (also usable as fuzz entry points).
TEST_HELPERS := testing/helper/test-json-parser \
testing/helper/test-string \
+ testing/helper/test-conditions \
testing/helper/test-chain \
testing/helper/test-tpm-wire \
testing/helper/test-register \
@@ -557,6 +558,10 @@ testing/helper/test-json-parser: testing/helper/test-json-parser.c util/json.o u
testing/helper/test-string: testing/helper/test-string.c util/string.o util/malloc.o
@printf ' CC $@\n'
$(Q)$(CC) $(CFLAGS) -o $@ $^
+testing/helper/test-conditions: testing/helper/test-conditions.c util/conditions.o \
+ util/string.o util/malloc.o
+ @printf ' CC $@\n'
+ $(Q)$(CC) $(CFLAGS) -o $@ $^
# chain.o's read/extend paths reference util/tpm (libtss2); the tests
# only exercise the pure helpers, but the symbols must still resolve.
diff --git a/builtin-status.c b/builtin-status.c
index 503cc5bf..50fd678e 100644
--- a/builtin-status.c
+++ b/builtin-status.c
@@ -430,7 +430,8 @@ static size_t build_upatches_table(struct dirent **ups, int n_ups,
/* Status runs on the host, so reachable and parsed are trivially true. */
static void local_cond_input(struct shirudo_conditions_input *in,
- int lsm_attached, int keyring_present, int staging,
+ int lsm_attached, int keyring_present,
+ const char *keyring_mode, int staging,
__s64 keyring_serial,
__s64 keyring_serial_pinned,
int ks_seen, int ksp_seen, int ksm_seen,
@@ -443,6 +444,7 @@ static void local_cond_input(struct shirudo_conditions_input *in,
.parsed = true,
.lsm_attached = lsm_attached,
.keyring_present = keyring_present,
+ .keyring_mode = keyring_mode,
.staging = staging,
.keyring_serial = keyring_serial,
.keyring_serial_pinned = keyring_serial_pinned,
@@ -632,7 +634,8 @@ int cmd_status(int argc, const char **argv)
* The anchor init froze is the kernel's record; the live keyring
* of that kind is what a load would name, and the two must agree.
*/
- if (identity_read(&ident) == 0) {
+ keyring_pinned = identity_read(&ident);
+ if (keyring_pinned == 0) {
keyring_pinned = ident.keyring_serial;
if (ident.keyring_type == SHIRUDO_SIG_KEYRING_BPF) {
keyring_serial = keyring_bpf_serial();
@@ -643,7 +646,6 @@ int cmd_status(int argc, const char **argv)
keyring_mode = "user";
}
} else {
- keyring_pinned = -ENOENT;
keyring_serial = keyring_find(SHIRUDO_KEYRING_NAME);
keyring_mode = keyring_serial >= 0 ? "user" : "none";
}
@@ -755,6 +757,7 @@ int cmd_status(int argc, const char **argv)
size_t nc, ci;
local_cond_input(&cin, lsm_attached, keyring_present,
+ keyring_mode,
lsm_attached && strcmp(keyring_mode, "bpf") != 0,
keyring_serial,
keyring_pinned >= 1 ? keyring_pinned : 0,
@@ -1043,6 +1046,7 @@ int cmd_status(int argc, const char **argv)
const char *verdict;
local_cond_input(&cin, lsm_attached, keyring_present,
+ keyring_mode,
lsm_attached && strcmp(keyring_mode, "bpf") != 0,
keyring_serial,
keyring_pinned >= 1 ? keyring_pinned : 0,
@@ -1095,20 +1099,30 @@ int cmd_status(int argc, const char **argv)
tamper_meaning(i));
}
- printf("\nKeyring (%s):\n", SHIRUDO_KEYRING_NAME);
+ printf("\nKeyring (%s):\n", !strcmp(keyring_mode, "bpf") ?
+ ".bpf" : SHIRUDO_KEYRING_NAME);
if (keyring_present)
printf(" %-22s %lld\n", "Serial:",
(__s64)keyring_serial);
- else
+ else if (keyring_pinned == -ENOENT)
printf(" %-22s %s\n", "Serial:",
"absent (operator host, or `shirudo init` "
"not yet run)");
- if (keyring_pinned < 0)
+ else if (keyring_serial == -ENOKEY)
+ printf(" %-22s %s\n", "Serial:", "absent");
+ else
+ printf(" %-22s lookup error: %s\n", "Serial:",
+ strerror(-keyring_serial));
+ if (keyring_pinned == -ENOENT)
printf(" %-22s %s\n", "Frozen serial:",
"(none this boot)");
+ else if (keyring_pinned < 0)
+ printf(" %-22s unreadable: %s\n", "Frozen serial:",
+ strerror(-keyring_pinned));
else
printf(" %-22s %lld%s\n", "Frozen serial:",
- (__s64)keyring_pinned, !keyring_present ? "" :
+ (__s64)keyring_pinned, !keyring_present ?
+ " (GONE -- removed?)" :
keyring_serial == keyring_pinned ?
" (matches)" :
" (MISMATCH -- swapped?)");
diff --git a/testing/helper/test-conditions.c b/testing/helper/test-conditions.c
new file mode 100644
index 00000000..adfbea4c
--- /dev/null
+++ b/testing/helper/test-conditions.c
@@ -0,0 +1,71 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+/*
+ * Drives util/conditions.c for t0050 with the input `shirudo status`
+ * derives from what it reads off the host.
+ *
+ * test-conditions <lsm 0|1> <bpf|user|none> <serial> <pinned> \
+ * <none|match|mismatch>
+ *
+ * serial is the live keyring's, -1 when it is absent; pinned the one init
+ * froze, 0 when it froze none; the last word the register's replay.
+ */
+
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <shirudo/string.h>
+
+#include "util/conditions.h"
+#include "util/macros.h"
+
+static int arg_s64(const char *s, __s64 *out)
+{
+ return parse_s64(s, strlen(s), -1, INT32_MAX, out);
+}
+
+int main(int argc, char **argv)
+{
+ struct shirudo_condition conds[SHIRUDO_CONDITIONS_MAX];
+ struct shirudo_conditions_input in = {};
+ __s64 lsm, serial, pinned;
+ char reason[160];
+ const char *mode;
+ size_t n, i;
+
+ if (argc != 6 || arg_s64(argv[1], &lsm) || arg_s64(argv[3], &serial) ||
+ arg_s64(argv[4], &pinned)) {
+ fprintf(stderr, "usage: test-conditions <lsm> <mode> <serial> "
+ "<pinned> <none|match|mismatch>\n");
+ return 2;
+ }
+ mode = argv[2];
+
+ in.reachable = true;
+ in.parsed = true;
+ in.lsm_attached = lsm != 0;
+ in.keyring_present = serial >= 0;
+ in.keyring_mode = mode;
+ in.staging = in.lsm_attached && strcmp(mode, "bpf") != 0;
+ in.keyring_serial = serial;
+ in.keyring_serial_pinned = pinned;
+ in.ks_seen = in.keyring_present;
+ in.ksp_seen = pinned >= 1;
+ in.ksm_seen = in.keyring_present && pinned >= 1;
+ in.keyring_serial_matches = in.ksm_seen && serial == pinned;
+ in.chain_seen = strcmp(argv[5], "none") != 0;
+ in.cm_seen = in.chain_seen;
+ in.chain_matches = !strcmp(argv[5], "match");
+ in.nv_index = "0x01818E00";
+
+ status_reason(&in, reason, sizeof(reason));
+ printf("status=%s\n", status_verdict(&in));
+ printf("reason=%s\n", reason);
+ n = conditions(&in, conds, ARRAY_SIZE(conds));
+ for (i = 0; i < n; i++)
+ printf("%s %s %s\n", conds[i].type, conds[i].status,
+ conds[i].detail);
+ return 0;
+}
diff --git a/testing/t0050-conditions.sh b/testing/t0050-conditions.sh
new file mode 100755
index 00000000..19befd55
--- /dev/null
+++ b/testing/t0050-conditions.sh
@@ -0,0 +1,44 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+#
+# t0050 -- util/conditions.c: the KeyringSealed condition names the kind
+# of keyring the anchor is in, and a keyring other than the one init
+# froze makes the status tampered.
+
+cd "$(dirname "$0")"
+. ./test-lib.sh
+
+H=helper/test-conditions
+
+test_expect_success 'the bpf keyring init froze is the production anchor' '
+ out=$(./$H 1 bpf 100 100 match) &&
+ echo "$out" | grep -qx "status=ok" &&
+ echo "$out" | grep -qx "KeyringSealed OK serial=100 in the bpf keyring, pin matches"
+'
+
+test_expect_success 'a staged user keyring warns' '
+ out=$(./$H 1 user 200 200 match) &&
+ echo "$out" | grep -qx "status=ok" &&
+ echo "$out" | grep -q "^KeyringSealed WARN staging mode: serial=200 in the user keyring;"
+'
+
+test_expect_success 'a user keyring without the LSM is not called the bpf keyring' '
+ ./$H 0 user 300 0 none |
+ grep -qx "KeyringSealed OK serial=300 in the user keyring, no pin recorded"
+'
+
+test_expect_success 'before init the keyring is pending, not tampered' '
+ out=$(./$H 0 none -1 0 none) &&
+ echo "$out" | grep -qx "status=lsm-off" &&
+ echo "$out" | grep -qx "KeyringSealed WARN shirudo keyring absent (not init'"'"'d?)"
+'
+
+test_expect_success 'a swapped keyring is tampered' '
+ out=$(./$H 1 bpf 101 100 match) &&
+ echo "$out" | grep -qx "status=tampered" &&
+ echo "$out" | grep -qx "reason=keyring serial swap detected" &&
+ echo "$out" | grep -qx "KeyringSealed ALERT pin serial=100 != live serial=101 (swapped?)"
+'
+
+test_done
diff --git a/util/conditions.c b/util/conditions.c
index 1e04a879..3a9e5fba 100644
--- a/util/conditions.c
+++ b/util/conditions.c
@@ -28,6 +28,10 @@ size_t conditions(const struct shirudo_conditions_input *in,
n++;
}
if (n < cap) {
+ const char *kind = in->keyring_mode &&
+ !strcmp(in->keyring_mode, "bpf") ?
+ "bpf" : "user";
+
c[n].type = "KeyringSealed";
if (!in->keyring_present) {
c[n].status = "WARN";
@@ -49,8 +53,8 @@ size_t conditions(const struct shirudo_conditions_input *in,
} else {
c[n].status = "OK";
snprintf(c[n].detail, sizeof(c[n].detail),
- "serial=%lld in the bpf keyring, %s",
- (__s64)in->keyring_serial,
+ "serial=%lld in the %s keyring, %s",
+ (__s64)in->keyring_serial, kind,
in->ksp_seen ? "pin matches"
: "no pin recorded");
}
diff --git a/util/conditions.h b/util/conditions.h
index a0d42501..62a2b923 100644
--- a/util/conditions.h
+++ b/util/conditions.h
@@ -25,6 +25,7 @@ struct shirudo_conditions_input {
int lsm_attached; /* 0/1 */
int keyring_present;
+ const char *keyring_mode; /* "bpf", "user" or "none" */
int staging; /* enforcing with the cert in the user keyring */
__s64 keyring_serial;
__s64 keyring_serial_pinned;