Skip to content

eBPF-based security platform

upatch: undo the pins a stopped load leaves and report them in status

Daniel Borkmann3768ce503549
diff --git a/AGENTS.md b/AGENTS.md
index cf85301a..39548e10 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -259,7 +259,11 @@ one usually still compiles and passes tests.
- **upatch state changes extend the TPM measurement register**
with a domain-separated tag, in deterministic order. Commands refuse
on drift (`tampered`) rather than retrying. Boot restore is exactly one
- register event and a re-run of `init` adds none. The register follows
+ register event and a re-run of `init` adds none. A load stopped
+ between its pins and its extend leaves pins without a record: `status`
+ reports them (`UpatchesMeasured`) and the next upatch command undoes
+ them under the register lock, which a load or an unload holds from
+ before it touches the pins to its append. The register follows
systemd's NvPCR construction (`util/chain.c`): an NV extend index that
clears at TPM reset, a first write only possible while PCRs 9 and 11
hold the values the policy was computed from, which is why `init`
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index 7a5e9c61..4d351fa9 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -274,7 +274,8 @@ signed config image.
* - ``t6003-tamper-points``
- Every tamper counter has a name and a meaning
* - ``t6004-conditions``
- - Status conditions: both keyrings, register, config activation
+ - Status conditions: both keyrings, register, pins, config
+ activation
* - ``t6005-mode-objects``
- The embedded admission and seal objects include image
enforcement
@@ -373,6 +374,9 @@ signed config image.
interpreter handlers
* - ``t6142-policy-socket-inheritance``
- Task policy is inherited by a listener and its accepted socket
+ * - ``t6143-unmeasured-load``
+ - A load stopped before its extend is reported by status and undone
+ by the next upatch command
``bpm`` (7)
~~~~~~~~~~~
@@ -588,7 +592,7 @@ Coverage by area
``t5003``
* - TPM register
- ``t4001``, ``t4003``, ``t4101``, ``t4102``, ``t4103``,
- ``t6125``, ``t6128``. Unit: ``chain``
+ ``t6125``, ``t6128``, ``t6143``. Unit: ``chain``
* - Attestation
- ``t2003``, ``t2005``, ``t4003``, ``t4004``, ``t4102``,
``t9105``. Fuzz: ``attest``, ``tpm-wire``
diff --git a/Documentation/internals/measurement-register.rst b/Documentation/internals/measurement-register.rst
index 12f4ad4d..e64ce6b1 100644
--- a/Documentation/internals/measurement-register.rst
+++ b/Documentation/internals/measurement-register.rst
@@ -247,13 +247,17 @@ of their own could interleave extend, extend, append, append, and leave a
log that replays the events in the other order. Every measuring command,
an upatch load or unload, a live revocation and a tree operation,
therefore holds ``/var/lib/shirudo/eventlog.lock`` from its extension to
-its append.
+its append; a load or an unload holds it from before it touches the pins.
The order of extension and state change is chosen per operation. A load
loads, archives and then extends; a failed archive copy or a refused
extension undoes the load, so no upatch runs unmeasured. An unload
extends first and detaches after, so a stopped process leaves a register
-that records more than happened, never less. An append that fails after
+that records more than happened, never less. A load stopped between its
+pins and its extension leaves pins the register does not cover:
+``status`` reports them and the next load or unload undoes them under
+the same lock before it acts, so the register stays behind what runs
+for one command at most. An append that fails after
a successful extension is reported as a desync for the operator to
reconcile.
diff --git a/Documentation/operate/run/read-status.rst b/Documentation/operate/run/read-status.rst
index 8220b02e..e756458d 100644
--- a/Documentation/operate/run/read-status.rst
+++ b/Documentation/operate/run/read-status.rst
@@ -67,6 +67,7 @@ loaded, captured from the test VM:
LsmAttached OK lsm/bpf link present and pinned
KeyringSealed OK bpf serial=130847582, dm-verity serial=962472513, pins match
ChainIntact OK live register == eventlog replay
+ UpatchesMeasured OK every pinned upatch is in this boot's records
TpmMeasured OK NV index 0x01818E00 readable
ConfigActive OK config dc90bacc356f.. is active
@@ -178,6 +179,15 @@ verdict is derived from them.
``ALERT`` ``live register != eventlog replay (tamper?)``. ``WARN``
``no register bound this boot (init incomplete)`` or ``no TPM
answered, no register to replay``.
+ * - ``UpatchesMeasured``
+ - Every upatch pinned in the private bpffs is in this boot's
+ records: loaded with the payload its pin records and not unloaded
+ since. ``ALERT`` ``<n> pinned without a record of this boot:
+ <names>`` is what a load stopped between its pins and its extend
+ leaves, which the next ``upatch load`` or ``upatch unload``
+ undoes before it acts. ``WARN`` ``no record of this boot to check
+ the pins against`` before ``init``, or ``pins not checked against
+ the records: <error>``.
* - ``TpmMeasured``
- The NV index is defined, written and readable. ``WARN`` ``NV
index 0x01818E00 not bound this boot`` or ``no TPM answered``.
@@ -388,7 +398,7 @@ precedence: ``tampered``, ``lsm-off``, ``init-pending``,
``tampered``
~~~~~~~~~~~~
-A ``tampered`` verdict has one of four reasons. Each shows as an
+A ``tampered`` verdict has one of five reasons. Each shows as an
``ALERT`` condition. The lines below are what ``status`` prints in each
state, cut to the verdict and the condition that raised it. ``<pinned>``,
``<live>`` and ``<root>`` stand for the values of your target.
@@ -406,6 +416,21 @@ record, the eventlog of this boot was altered or hidden, or the index is
not the one ``init`` bound. The ``TPM Measurement`` section shows which
lines disagree.
+A upatch is pinned without a record:
+
+.. code-block:: text
+
+ Status: tampered (a upatch is pinned without a record of this boot)
+ ...
+ UpatchesMeasured ALERT 1 pinned without a record of this boot: <name>
+
+A load was stopped between its pins and its extend, so the upatch runs
+while the register does not cover it, or an unload was stopped after
+its extend. The next ``upatch load`` or ``upatch unload``, and every
+``bpm`` command that loads or unloads, undoes those pins before it acts
+and records the undo in the journal. What stopped the load is the
+question for the incident.
+
The live keyring is not the one ``init`` sealed:
.. code-block:: text
@@ -541,6 +566,7 @@ its own.
Status: degraded (no register bound this boot)
...
ChainIntact WARN no register bound this boot (init incomplete)
+ UpatchesMeasured WARN no record of this boot to check the pins against
TpmMeasured WARN NV index 0x01818E00 not bound this boot
Enforcement is up, but the TPM holds no written measurement register, or
diff --git a/Documentation/reference/cli/status.rst b/Documentation/reference/cli/status.rst
index 6dc804e8..93b67f0d 100644
--- a/Documentation/reference/cli/status.rst
+++ b/Documentation/reference/cli/status.rst
@@ -40,9 +40,9 @@ report. The roles come from what is present:
otherwise. It says nothing about the keyring. The ``KeyringSealed``
condition does.
-The report shows five conditions, each ``OK``, ``WARN`` or ``ALERT``:
-``LsmAttached``, ``KeyringSealed``, ``ChainIntact``, ``TpmMeasured`` and
-``ConfigActive``. The verdict is the first of these that applies:
+The report shows six conditions, each ``OK``, ``WARN`` or ``ALERT``:
+``LsmAttached``, ``KeyringSealed``, ``ChainIntact``,
+``UpatchesMeasured``, ``TpmMeasured`` and ``ConfigActive``. The verdict is the first of these that applies:
.. list-table::
:header-rows: 1
@@ -54,8 +54,9 @@ The report shows five conditions, each ``OK``, ``WARN`` or ``ALERT``:
- The live register differs from the eventlog replay, the ``.bpf``
or ``.dm-verity`` keyring is not the one ``init`` froze or is
gone, a keyring holds a link ``init`` did not freeze, the config
- key is gone from ``.dm-verity``, or the selected config cannot be
- verified against its records.
+ key is gone from ``.dm-verity``, a upatch is pinned without a
+ record of this boot, or the selected config cannot be verified
+ against its records.
* - ``lsm-off``
- The LSM link is not pinned.
* - ``init-pending``
@@ -180,6 +181,7 @@ Show the state of a target:
LsmAttached OK lsm/bpf link present and pinned
KeyringSealed OK bpf serial=130847582, dm-verity serial=962472513, pins match
ChainIntact OK live register == eventlog replay
+ UpatchesMeasured OK every pinned upatch is in this boot's records
TpmMeasured OK NV index 0x01818E00 readable
ConfigActive OK config dc90bacc356f.. is active
diff --git a/Documentation/reference/cli/upatch-load.rst b/Documentation/reference/cli/upatch-load.rst
index 73179b92..18646e1f 100644
--- a/Documentation/reference/cli/upatch-load.rst
+++ b/Documentation/reference/cli/upatch-load.rst
@@ -45,6 +45,11 @@ Description
message ``chain_drift``).
#. It refuses when the eventlog would exceed ``eventlog.max_bytes``
(``eventlog_full``).
+#. It takes the register lock, ``/var/lib/shirudo/eventlog.lock``, which
+ it holds to the end, and undoes the pins of a load that was stopped
+ before its extend or of an unload stopped after it, the pins
+ ``status`` reports under ``UpatchesMeasured``. A upatch of the same
+ name that is loaded is refused (``already_loaded``).
#. It parses the artifact: a LOAD artifact, a safe and signed name, a
payload digest that matches the payload, valid attach metadata
(``parser_rejected``). For a uprobe it checks each target binary
@@ -68,7 +73,10 @@ Description
When the copy to the archive or the register extend fails, ``load``
removes the pins and the archive again, so that no upatch runs
unmeasured. When the extend succeeded and only the eventlog append failed,
-the upatch stays loaded and the report says ``tpm_error``.
+the upatch stays loaded and the report says ``tpm_error``. A ``load``
+stopped between its pins and its extend leaves the pins: ``status``
+reports them as ``tampered`` and the next ``load`` or ``unload`` undoes
+them before it acts.
``--no-precheck`` skips step 3 only. The kernel signature check and the
LSM still apply. ``bpm`` passes it when it loads the upatches of a
diff --git a/Documentation/reference/cli/upatch-unload.rst b/Documentation/reference/cli/upatch-unload.rst
index 5d31a1af..0911e488 100644
--- a/Documentation/reference/cli/upatch-unload.rst
+++ b/Documentation/reference/cli/upatch-unload.rst
@@ -40,6 +40,9 @@ both in signed sections (see :doc:`build`). ``unload``:
#. Runs the pre-flight checks of ``load``, unless ``--no-precheck``.
#. Refuses when the eventlog would exceed ``eventlog.max_bytes``
(``eventlog_full``).
+#. Takes the register lock, which it holds to the end, and undoes the
+ pins of a load or an unload that was stopped before the register
+ covered them, as ``load`` does.
#. Loads the directive's loader with ``BPF_PROG_LOAD`` against the
``.bpf`` keyring, so that the kernel verifies its signature before
anything is removed.
diff --git a/Documentation/reference/failure-categories.rst b/Documentation/reference/failure-categories.rst
index c95a679d..a06e21be 100644
--- a/Documentation/reference/failure-categories.rst
+++ b/Documentation/reference/failure-categories.rst
@@ -198,8 +198,9 @@ Categories
* - ``tpm_error``
- ``tpm_extend``
- errno of the call
- - The register lock, the TPM or the extend failed. A load is undone
- when the register did not move. When the extend succeeded and the
+ - The register lock could not be taken, in which case nothing was
+ loaded or detached, or the TPM or the extend failed. A load is
+ undone when the register did not move. When the extend succeeded and the
eventlog append failed, the message reads ``eventlog append failed
after the extend``.
- Check ``shirudo status``. A register ahead of the eventlog shows as
diff --git a/Documentation/reference/files.rst b/Documentation/reference/files.rst
index 72818d5c..3744447d 100644
--- a/Documentation/reference/files.rst
+++ b/Documentation/reference/files.rst
@@ -130,7 +130,8 @@ the label as foreign. In the initrd, ``init`` binds
* - ``eventlog.lock``
- measuring commands
- ``0600``
- - Lock held from each register extend to its eventlog append.
+ - Lock held from each register extend to its eventlog append, and
+ by a load or an unload from before it touches the pins.
* - ``locks/bpm.lock``
- ``bpm``
- ``0600``
diff --git a/Documentation/reference/formats/status-json.rst b/Documentation/reference/formats/status-json.rst
index 5b25145b..eec3c77a 100644
--- a/Documentation/reference/formats/status-json.rst
+++ b/Documentation/reference/formats/status-json.rst
@@ -158,8 +158,9 @@ Verdicts
``.bpf`` or ``.dm-verity`` keyring's serial differs from the
pinned serial, a keyring ``init`` pinned is gone, a keyring holds
a link ``init`` did not freeze, the config key ``init`` froze is
- gone from ``.dm-verity``, or the selected config cannot be
- verified against its activation records.
+ gone from ``.dm-verity``, a upatch is pinned without a record of
+ this boot, or the selected config cannot be verified against its
+ activation records.
* - ``lsm-off``
- The LSM link is not pinned. Enforcement is off.
* - ``init-pending``
@@ -175,7 +176,7 @@ Verdicts
Conditions
----------
-``conditions`` always holds these five types, in this order. ``status``
+``conditions`` always holds these six types, in this order. ``status``
is ``OK``, ``WARN`` or ``ALERT``. ``<serial>``, ``<n>`` and ``<root>``
stand for a keyring serial, a number of links and the first 12 hex
digits of the config root hash. ``KeyringSealed`` reports the first of
@@ -235,6 +236,18 @@ its details that applies, in the order listed.
* -
- ``ALERT``
- ``live register != eventlog replay (tamper?)``
+ * - ``UpatchesMeasured``
+ - ``OK``
+ - ``every pinned upatch is in this boot's records``
+ * -
+ - ``WARN``
+ - ``no record of this boot to check the pins against``
+ * -
+ - ``WARN``
+ - ``pins not checked against the records: <error>``
+ * -
+ - ``ALERT``
+ - ``<n> pinned without a record of this boot: <names>``
* - ``TpmMeasured``
- ``OK``
- ``NV index 0x01818E00 readable``
@@ -702,6 +715,11 @@ indented here:
"status": "OK",
"detail": "live register == eventlog replay"
},
+ {
+ "type": "UpatchesMeasured",
+ "status": "OK",
+ "detail": "every pinned upatch is in this boot's records"
+ },
{
"type": "TpmMeasured",
"status": "OK",
diff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rst
index 237d6b92..7e219ea3 100644
--- a/Documentation/security/threat-model.rst
+++ b/Documentation/security/threat-model.rst
@@ -890,9 +890,15 @@ The order of the steps decides what a crash or a stopped process
leaves. A load whose archive copy or extension fails is undone, pins
and archive. An unload or a purge is measured before it detaches, so an
interruption leaves a register that claims more than happened, never a
-detach it does not know. A measuring command holds a lock beside the
-eventlog from its extend to its append, so the register and the log
-advance in one order. An append that fails after a successful extend
+detach it does not know. A load that root stops between its pins and
+its extend, with ``SIGKILL`` say, leaves pins the register does not
+cover and the upatch attached: ``shirudo status`` reports them
+(``UpatchesMeasured``, verdict ``tampered``) and the next upatch
+command, ``bpm``'s included, undoes them before it acts. A measuring
+command holds a lock beside the eventlog from its extend to its append,
+a load or an unload from that undo to its append, so the register and
+the log advance in one order and the undo never takes a load in flight
+for a stopped one. An append that fails after a successful extend
leaves the register ahead of the log; shirudo reports the desync and
``shirudo status`` reports a register its replay cannot explain.
@@ -916,7 +922,7 @@ tree operations run without it.
``tpm_error`` and ``eventlog_full`` from shirudo
:Counters: ``TpmExtend``
:Tests: ``t4002``, ``t4003``, ``t4102``, ``t4103``, ``t6115``,
- ``t6122``, ``t6128``
+ ``t6122``, ``t6128``, ``t6143``
:Residual: R4, R12, R20, U6, U8, U9
T6 Boot window
@@ -1275,7 +1281,8 @@ R2 The command interface of the runtime binary is the boundary
serializer with ``shirudo upatch map`` and send the output where it
chooses, and hold the lock files shirudo serializes on. No hook
covers signals, so root can also stop a running command, which
- keeps open whatever descriptors it holds (U17).
+ keeps open whatever descriptors it holds (U17); a load stopped
+ before its extend is reported and undone (T5).
``CAP_SYS_RESOURCE`` can rewrite a process's executable link, which
``lsm/task_prctl`` refuses and the mark does not depend on. The
initrd's bootstrap binary and the memory of authorized tasks stay
diff --git a/Documentation/start/how-it-works.rst b/Documentation/start/how-it-works.rst
index b1b5c554..ca120e6d 100644
--- a/Documentation/start/how-it-works.rst
+++ b/Documentation/start/how-it-works.rst
@@ -136,7 +136,9 @@ Measure
a matching record to the eventlog. When the archive copy or the
extension fails, shirudo undoes the load rather than leave it
unmeasured. If the undo fails as well, the command says so and leaves
- the target for the operator to reconcile.
+ the target for the operator to reconcile. A load stopped before the
+ extension leaves pins the register does not cover; ``status`` reports
+ them and the next load or unload undoes them.
The register starts from zero at every boot. Its value depends only on
the trusted certificates, the set restored at boot, the upatch tree's
diff --git a/Documentation/upatches/learn/concepts.rst b/Documentation/upatches/learn/concepts.rst
index de3fd96a..fcb07f80 100644
--- a/Documentation/upatches/learn/concepts.rst
+++ b/Documentation/upatches/learn/concepts.rst
@@ -265,8 +265,9 @@ link keeps a program attached after the command exits and lets the unload path
detach it.
It cannot stay loaded unmeasured. A load whose archive copy or register
-extend fails is undone, and ``init`` refuses to restore an archived upatch
-the register does not cover.
+extend fails is undone, a load stopped before its extend is undone by the
+next load or unload and reported by ``status`` until then, and ``init``
+refuses to restore an archived upatch the register does not cover.
It cannot be removed without the signer. A loaded upatch goes away through
a signed unload directive bound to its payload, or through a key
diff --git a/Makefile.shirudo b/Makefile.shirudo
index fc488c3b..4e0dc3b4 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -221,6 +221,7 @@ UTIL_SRCS := \
$(TLS_SRC) \
util/loader.c \
util/upatch.c \
+ util/measured.c \
util/keyring.c \
util/keys.c \
util/identity.c \
diff --git a/builtin-status.c b/builtin-status.c
index 2eb54a6c..102b9d4a 100644
--- a/builtin-status.c
+++ b/builtin-status.c
@@ -32,6 +32,7 @@
#include "util/chain.h"
#include "util/bpm_tree.h"
#include "util/conditions.h"
+#include "util/measured.h"
#include "util/config.h"
#include "util/crypto.h"
#include "util/eventlog.h"
@@ -62,6 +63,21 @@
#include "util/upatch_table.h"
#include "util/version.h"
+#define UNMEASURED_NAMES 256
+
+static int note_unmeasured(const char *name, void *ctx)
+{
+ char *names = ctx;
+ size_t used = strlen(names);
+
+ if (used >= 3 && !strcmp(names + used - 3, "..."))
+ return 0;
+ if (strsprintf(names + used, UNMEASURED_NAMES - used, "%s%s",
+ used ? ", " : "", name) < 0 && used)
+ strscpy(names + used, ", ...", UNMEASURED_NAMES - used);
+ return 0;
+}
+
static int notdot(const struct dirent *e)
{
return strcmp(e->d_name, ".") != 0 && strcmp(e->d_name, "..") != 0;
@@ -882,8 +898,10 @@ int cmd_status(int argc, const char **argv)
int n_ups, i, fd, ret = 0;
const char *otel_status = NULL;
bool otel_endpoint_configured = false;
+ char unmeasured_names[UNMEASURED_NAMES] = "";
char config_root_hex[65] = "";
__u8 config_root[32];
+ int unmeasured, lock;
int config_state;
for (i = 1; i < argc; i++) {
@@ -998,6 +1016,10 @@ int cmd_status(int argc, const char **argv)
n_ups = scandir(BPFFS_UPATCHES_DIR, &ups, notdot, alphasort);
if (n_ups < 0)
n_ups = 0;
+ lock = eventlog_lock();
+ unmeasured = lock < 0 ? lock :
+ measured_unmeasured_pins(note_unmeasured, unmeasured_names);
+ eventlog_unlock(lock);
gather_assets(&assets);
tamper_rc = tamper_read(&tamper);
@@ -1057,6 +1079,9 @@ int cmd_status(int argc, const char **argv)
cin.config_seen = config_state != -ENODEV;
cin.config_state = config_state;
cin.config_root = config_root_hex;
+ cin.pins_rc = min(unmeasured, 0);
+ cin.pins_unmeasured = max(unmeasured, 0);
+ cin.pins_detail = unmeasured_names;
keyring_cond_input(&cin, &bpf, &dmv);
if (json) {
diff --git a/builtin-upatch.c b/builtin-upatch.c
index 26b7db1f..caa4937b 100644
--- a/builtin-upatch.c
+++ b/builtin-upatch.c
@@ -50,6 +50,7 @@
#include "util/keyring.h"
#include "util/tpm.h"
#include "util/log.h"
+#include "util/measured.h"
#include "util/hex.h"
#include "util/malloc.h"
#include "util/paths.h"
@@ -2773,7 +2774,7 @@ static int stage_upatch(const char *src, char *staged, size_t sz)
}
/* Take a load the register did not record back out: pins, then archive. */
-static void unload_unmeasured(const char *name)
+static int undo_load(const char *name)
{
char persisted[PATH_MAX];
int ret;
@@ -2783,6 +2784,13 @@ static void unload_unmeasured(const char *name)
UPATCHES_DIR, name) >= 0 &&
unlink(persisted) != 0 && errno != ENOENT)
ret = -errno;
+ return ret;
+}
+
+static void unload_unmeasured(const char *name)
+{
+ int ret = undo_load(name);
+
if (ret)
pr_err("upatch load: %s is loaded but not measured and could "
"not be undone: %s; operator must reconcile", name,
@@ -2792,6 +2800,36 @@ static void unload_unmeasured(const char *name)
name);
}
+static int undo_unmeasured_pin(const char *name, void *ctx)
+{
+ int ret = undo_load(name);
+
+ if (ret)
+ pr_err("upatch: %s is pinned without a record of this boot and could not be undone: %s; operator must reconcile",
+ name, strerror(-ret));
+ else
+ pr_audit("upatch: %s was pinned without a record of this boot; the load was undone",
+ name);
+ return 0;
+}
+
+/*
+ * A load that a signal stopped between its pins and its extend, or an
+ * unload stopped between its append and its detach, left pins the
+ * register does not cover, which status reports meanwhile. Every upatch
+ * command undoes them before it acts, under the register lock a load
+ * holds from before its pins to its append, so that the scan never
+ * takes a load in flight for one that was stopped.
+ */
+static void undo_unmeasured_pins(void)
+{
+ int ret = measured_unmeasured_pins(undo_unmeasured_pin, NULL);
+
+ if (ret < 0 && ret != -ENOENT)
+ pr_warn("upatch: cannot check the pins against this boot's records: %s",
+ strerror(-ret));
+}
+
static int cmd_upatch_load(int argc, const char **argv)
{
const char *file, *expect_chain, *expect_version_id, *report;
@@ -2801,7 +2839,7 @@ static int cmd_upatch_load(int argc, const char **argv)
struct upatch u;
struct op_report rep = { .op = "upatch_load", .phase = "done" };
bool no_precheck, no_version_check, have_id = false, measured;
- int err, ret = 1;
+ int err, lock = -1, ret = 1;
for (int i = 1; i < argc; i++) {
if (is_help(argv[i])) {
@@ -2861,6 +2899,16 @@ static int cmd_upatch_load(int argc, const char **argv)
"unsafe upatch name");
goto done;
}
+ lock = eventlog_lock();
+ if (lock < 0) {
+ pr_err("upatch load: cannot take the register lock: %s "
+ "(category: tpm_error) -- nothing loaded",
+ strerror(-lock));
+ rep_fail(&rep, "tpm_extend", "tpm_error", -lock,
+ "cannot take the register lock");
+ goto done;
+ }
+ undo_unmeasured_pins();
/*
* Only a completed load records the payload and only an unload
* removes it. A load over a live upatch rebinds its serializer pins
@@ -2889,8 +2937,9 @@ static int cmd_upatch_load(int argc, const char **argv)
* would drift from it. A load whose archive copy fails, a file system
* root filled say, or one the register then refuses to take is
* undone, pins and archive, so that no upatch runs unmeasured; a
- * caller who stops this process first gets the same at the next
- * init, which refuses to restore what the register does not cover.
+ * caller who stops this process first gets the same from the next
+ * upatch command, and from the next init, which refuses to restore
+ * what the register does not cover.
*/
err = persist_upatch(file, name);
if (err) {
@@ -2902,9 +2951,9 @@ static int cmd_upatch_load(int argc, const char **argv)
goto done;
}
- err = upatch_chain_event("shirudo-upatch", "upatch_load", "load",
- name, payload, NULL, rep.chain_after,
- &measured);
+ err = upatch_chain_event_locked("shirudo-upatch", "upatch_load",
+ "load", name, payload, NULL,
+ rep.chain_after, &measured);
if (err) {
if (!measured)
unload_unmeasured(name);
@@ -2919,6 +2968,7 @@ static int cmd_upatch_load(int argc, const char **argv)
rep.tpm_present = rep.chain_after[0] != '\0';
ret = 0;
done:
+ eventlog_unlock(lock);
if (ret == 1 && rep.category &&
!strcmp(rep.category, "attach_target_missing"))
ret = UPATCH_INSTALL_TARGET_MISSING;
@@ -2940,7 +2990,7 @@ static int cmd_upatch_unload(int argc, const char **argv)
struct upatch u;
struct op_report rep = { .op = "upatch_unload", .phase = "done" };
bool no_precheck, no_version_check;
- int err, ret = 1;
+ int err, lock = -1, ret = 1;
for (int i = 1; i < argc; i++) {
if (is_help(argv[i])) {
@@ -3033,6 +3083,16 @@ static int cmd_upatch_unload(int argc, const char **argv)
if (eventlog_room(&rep, "unload") != 0)
goto done;
+ lock = eventlog_lock();
+ if (lock < 0) {
+ pr_err("upatch unload: cannot take the register lock: %s "
+ "(category: tpm_error) -- nothing detached",
+ strerror(-lock));
+ rep_fail(&rep, "tpm_extend", "tpm_error", -lock,
+ "cannot take the register lock");
+ goto done;
+ }
+ undo_unmeasured_pins();
/* A bad signature fails here, before anything is removed. */
if (__upatch_apply(file, true, &rep) != 0) {
pr_err("upatch unload: signature verification failed; refusing");
@@ -3091,8 +3151,9 @@ static int cmd_upatch_unload(int argc, const char **argv)
* fails after the event leaves a shield the register says is gone,
* which the next status reports and the operator can repeat.
*/
- err = upatch_chain_event("shirudo-unload", "upatch_unload", "unload",
- name, payload, NULL, rep.chain_after, NULL);
+ err = upatch_chain_event_locked("shirudo-unload", "upatch_unload",
+ "unload", name, payload, NULL,
+ rep.chain_after, NULL);
if (err) {
rep_fail(&rep, "tpm_extend", chain_event_category(err, false),
err, "chain extend failed; nothing detached");
@@ -3117,6 +3178,7 @@ static int cmd_upatch_unload(int argc, const char **argv)
rep.tpm_present = rep.chain_after[0] != '\0';
ret = 0;
done:
+ eventlog_unlock(lock);
if (ret != 0)
pr_err("upatch unload failed: name=%s phase=%s category=%s "
"err=%d (%s) message=%s",
diff --git a/testing/functional/tamperproof/t6004-conditions.sh b/testing/functional/tamperproof/t6004-conditions.sh
index c500d539..62be9b6a 100755
--- a/testing/functional/tamperproof/t6004-conditions.sh
+++ b/testing/functional/tamperproof/t6004-conditions.sh
@@ -8,7 +8,8 @@
# key that is gone makes the status tampered, and the register
# conditions tell a TPM that does not answer from one with no register.
# A pending config activation is reboot-pending, and a config its records
-# cannot vouch for is tampered.
+# cannot vouch for is tampered. A pin without a record of this boot is
+# tampered, and pins that cannot be checked are a warning.
cd "$(dirname "$0")/../.."
. ./test-lib.sh
@@ -106,6 +107,32 @@ test_expect_success 'a bound register is measured' '
grep -qx "TpmMeasured OK NV index 0x01818E00 readable"
'
+test_expect_success 'every pinned upatch in the records is OK' '
+ ./$H 1 bpf 100 100 match |
+ grep -qx "UpatchesMeasured OK every pinned upatch is in this boot.s records"
+'
+
+test_expect_success 'a pin without a record of this boot is tampered' '
+ out=$(SHIRUDO_TEST_PINS=unmeasured:shirudo-test-basic,kernel-obs ./$H 1 bpf 100 100 match) &&
+ echo "$out" | grep -qx "status=tampered" &&
+ echo "$out" | grep -qx "reason=a upatch is pinned without a record of this boot" &&
+ echo "$out" | grep -qx "UpatchesMeasured ALERT 2 pinned without a record of this boot: shirudo-test-basic,kernel-obs"
+'
+
+test_expect_success 'pins that cannot be checked are a warning, not a verdict' '
+ out=$(SHIRUDO_TEST_PINS=norecord ./$H 1 bpf 100 100 match) &&
+ echo "$out" | grep -qx "status=ok" &&
+ echo "$out" | grep -qx "UpatchesMeasured WARN no record of this boot to check the pins against" &&
+ out=$(SHIRUDO_TEST_PINS=error ./$H 1 bpf 100 100 match) &&
+ echo "$out" | grep -qx "status=ok" &&
+ echo "$out" | grep -qx "UpatchesMeasured WARN pins not checked against the records: Permission denied"
+'
+
+test_expect_success 'a register mismatch is the reason given before the pins' '
+ SHIRUDO_TEST_PINS=unmeasured:x ./$H 1 bpf 100 100 mismatch |
+ grep -qx "reason=live register != eventlog replay"
+'
+
test_expect_success 'an active config is OK' '
./$H 1 bpf 100 100 match |
grep -qx "ConfigActive OK config 0123456789ab.. is active"
diff --git a/testing/functional/tamperproof/t6143-unmeasured-load.sh b/testing/functional/tamperproof/t6143-unmeasured-load.sh
new file mode 100755
index 00000000..bfa29a1c
--- /dev/null
+++ b/testing/functional/tamperproof/t6143-unmeasured-load.sh
@@ -0,0 +1,128 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+#
+# t6143 -- a load stopped between its pins and its extend cannot keep a
+# upatch running unmeasured: status reports the pins the register does
+# not cover as tampered, and the next upatch command, a load or an
+# unload of another upatch, undoes them before it acts.
+
+cd "$(dirname "$0")/../.."
+. ./integration-lib.sh
+target_guard
+command -v python3 >/dev/null 2>&1 || test_skip_all "no python3"
+FIXTURES=$SHIRUDO_TEST_FIXTURES_DIR
+NAME=shirudo-test-basic
+OTHER=shirudo-test-marker
+LOAD="$FIXTURES/$NAME.upatch"
+UNLOAD="$FIXTURES/$NAME.unload.upatch"
+PINDIR="/run/shirudo/bpffs/upatches/$NAME"
+OTHERDIR="/run/shirudo/bpffs/upatches/$OTHER"
+ARCHIVE="/var/lib/shirudo/upatches/$NAME.upatch"
+EVENTLOG=/var/lib/shirudo/eventlog
+W=$(mktemp -d)
+cleanup() {
+ [ ! -e "$OTHERDIR" ] ||
+ "$SHIRUDO_BIN" upatch unload "$FIXTURES/$OTHER.unload.upatch" \
+ >/dev/null 2>&1 || :
+ [ ! -e "$PINDIR" ] ||
+ "$SHIRUDO_BIN" upatch unload "$UNLOAD" >/dev/null 2>&1 || :
+ rm -rf "$W"
+}
+trap cleanup EXIT
+for f in "$LOAD" "$UNLOAD" "$FIXTURES/$OTHER.upatch" \
+ "$FIXTURES/$OTHER.unload.upatch"; do
+ [ -s "$f" ] || test_skip_all "missing fixture $f"
+done
+[ -s "$EVENTLOG" ] || test_skip_all "no eventlog"
+# An earlier test of the family may have left either loaded.
+[ ! -e "$PINDIR" ] || "$SHIRUDO_BIN" upatch unload "$UNLOAD" >/dev/null 2>&1
+[ ! -e "$OTHERDIR" ] ||
+ "$SHIRUDO_BIN" upatch unload "$FIXTURES/$OTHER.unload.upatch" \
+ >/dev/null 2>&1
+for d in "$PINDIR" "$OTHERDIR"; do
+ [ ! -e "$d" ] || test_skip_all "$(basename "$d") cannot be unloaded"
+done
+
+# Start a load and stop it once its link pin appears, before the
+# register moves. A load that got as far as its record, which the
+# eventlog shows as one more line, is unloaded and tried again. The
+# poll is builtins only, so that it runs faster than the extend, and
+# bounded, as a load that fails stays a zombie until it is waited for.
+stopped_load() {
+ for try in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20; do
+ lines=$(wc -l <"$EVENTLOG")
+ "$SHIRUDO_BIN" upatch load "$LOAD" >/dev/null 2>&1 &
+ pid=$!
+ i=0
+ while kill -0 "$pid" 2>/dev/null && [ ! -e "$PINDIR/link" ] &&
+ [ "$i" -lt 2000000 ]; do
+ i=$((i + 1))
+ done
+ kill -9 "$pid" 2>/dev/null || :
+ wait "$pid" 2>/dev/null || :
+ if [ -e "$PINDIR/link" ] &&
+ [ "$(wc -l <"$EVENTLOG")" = "$lines" ]; then
+ return 0
+ fi
+ [ ! -e "$PINDIR" ] ||
+ "$SHIRUDO_BIN" upatch unload "$UNLOAD" >/dev/null 2>&1 || :
+ done
+ return 1
+}
+
+# The verdict and the UpatchesMeasured condition of a status document.
+measured_condition() {
+ python3 - "$1" <<'PY'
+import json, sys
+d = json.load(open(sys.argv[1], encoding="utf-8"))
+c = [c for c in d["conditions"] if c["type"] == "UpatchesMeasured"]
+print(d["status"], c[0]["status"] if c else "-", c[0]["detail"] if c else "-")
+PY
+}
+
+test_expect_success 'a load stopped before its extend leaves pins without a record' '
+ stopped_load &&
+ [ -e "$PINDIR/link" ] &&
+ [ ! -e "$ARCHIVE" ]
+'
+test_expect_success 'status reports the unmeasured pins as tampered' '
+ "$SHIRUDO_BIN" status --json >"$W/status.json" 2>/dev/null &&
+ measured_condition "$W/status.json" >"$W/cond" &&
+ read -r verdict cond detail <"$W/cond" &&
+ [ "$verdict" = tampered ] &&
+ [ "$cond" = ALERT ] &&
+ case "$detail" in *"$NAME"*) true ;; *) false ;; esac &&
+ "$SHIRUDO_BIN" status 2>/dev/null | grep -q "^Status: *tampered" &&
+ "$SHIRUDO_BIN" status 2>/dev/null | grep -q "UpatchesMeasured *ALERT"
+'
+test_expect_success 'the next load undoes the unmeasured pins before it acts' '
+ "$SHIRUDO_BIN" upatch load "$FIXTURES/$OTHER.upatch" >/dev/null 2>&1 &&
+ [ ! -e "$PINDIR" ] &&
+ [ -e "$OTHERDIR/link" ] &&
+ "$SHIRUDO_BIN" status --json >"$W/status.json" 2>/dev/null &&
+ measured_condition "$W/status.json" >"$W/cond" &&
+ read -r verdict cond detail <"$W/cond" &&
+ [ "$verdict" != tampered ] &&
+ [ "$cond" = OK ]
+'
+test_expect_success 'the next unload undoes the unmeasured pins before it acts' '
+ stopped_load &&
+ "$SHIRUDO_BIN" upatch unload "$FIXTURES/$OTHER.unload.upatch" \
+ >/dev/null 2>&1 &&
+ [ ! -e "$PINDIR" ] &&
+ [ ! -e "$OTHERDIR" ] &&
+ "$SHIRUDO_BIN" status --json >"$W/status.json" 2>/dev/null &&
+ measured_condition "$W/status.json" >"$W/cond" &&
+ read -r verdict cond detail <"$W/cond" &&
+ [ "$verdict" != tampered ] &&
+ [ "$cond" = OK ]
+'
+test_expect_success 'a load after the undo is measured again' '
+ "$SHIRUDO_BIN" upatch load "$LOAD" >/dev/null 2>&1 &&
+ [ -e "$PINDIR/link" ] &&
+ [ -s "$ARCHIVE" ] &&
+ "$SHIRUDO_BIN" upatch unload "$UNLOAD" >/dev/null 2>&1 &&
+ [ ! -e "$PINDIR" ]
+'
+test_done
diff --git a/testing/helper/test-conditions.c b/testing/helper/test-conditions.c
index 77437a4a..0244b529 100644
--- a/testing/helper/test-conditions.c
+++ b/testing/helper/test-conditions.c
@@ -48,7 +48,7 @@ int main(int argc, char **argv)
struct condition conds[CONDITIONS_MAX];
struct conditions_input in = {};
__s64 lsm, serial, pinned;
- const char *mode, *config;
+ const char *mode, *config, *pins;
char reason[160];
size_t n, i;
@@ -96,6 +96,17 @@ int main(int argc, char **argv)
!strcmp(config, "untrusted") ? -EUCLEAN :
!strcmp(config, "stale") ? -ESTALE : 0;
in.config_root = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
+ pins = getenv("SHIRUDO_TEST_PINS");
+ if (pins && !strcmp(pins, "norecord")) {
+ in.pins_rc = -ENOENT;
+ } else if (pins && !strcmp(pins, "error")) {
+ in.pins_rc = -EACCES;
+ } else if (pins && !strncmp(pins, "unmeasured:", 11)) {
+ in.pins_detail = pins + 11;
+ in.pins_unmeasured = 1;
+ for (pins = in.pins_detail; *pins; pins++)
+ in.pins_unmeasured += *pins == ',';
+ }
status_reason(&in, reason, sizeof(reason));
printf("status=%s\n", status_verdict(&in));
diff --git a/util/conditions.c b/util/conditions.c
index eeee33b3..5c931c7c 100644
--- a/util/conditions.c
+++ b/util/conditions.c
@@ -138,6 +138,25 @@ size_t conditions(const struct conditions_input *in,
}
n++;
}
+ if (n < cap) {
+ c[n].type = "UpatchesMeasured";
+ if (in->pins_rc == -ENOENT)
+ set_detail(&c[n], "WARN",
+ "no record of this boot to check the pins against");
+ else if (in->pins_rc)
+ set_detail(&c[n], "WARN",
+ "pins not checked against the records: %s",
+ strerror(-in->pins_rc));
+ else if (in->pins_unmeasured)
+ set_detail(&c[n], "ALERT",
+ "%d pinned without a record of this boot: %s",
+ in->pins_unmeasured,
+ in->pins_detail ? in->pins_detail : "?");
+ else
+ set_detail(&c[n], "OK",
+ "every pinned upatch is in this boot's records");
+ n++;
+ }
if (n < cap) {
c[n].type = "TpmMeasured";
c[n].status = in->chain_seen ? "OK" : "WARN";
@@ -202,8 +221,8 @@ const char *status_verdict(const struct conditions_input *in)
return "unreachable";
if (!in->parsed)
return "unparseable";
- if ((in->cm_seen && !in->chain_matches) || keyring_tampered(in) ||
- config_untrusted(in))
+ if ((in->cm_seen && !in->chain_matches) || in->pins_unmeasured ||
+ keyring_tampered(in) || config_untrusted(in))
return "tampered";
if (!in->lsm_attached)
return "lsm-off";
@@ -230,6 +249,9 @@ void status_reason(const struct conditions_input *in,
} else if (strcmp(v, "tampered") == 0) {
if (in->cm_seen && !in->chain_matches)
strscpy(buf, "live register != eventlog replay", cap);
+ else if (in->pins_unmeasured)
+ strscpy(buf, "a upatch is pinned without a record of this boot",
+ cap);
else if (config_untrusted(in))
strscpy(buf, "the selected config cannot be verified against its records",
cap);
diff --git a/util/conditions.h b/util/conditions.h
index c3f91cb7..ce04ab39 100644
--- a/util/conditions.h
+++ b/util/conditions.h
@@ -46,6 +46,9 @@ struct conditions_input {
int config_seen;
int config_state; /* image_config_state() */
const char *config_root; /* hex, when config_seen */
+ int pins_rc; /* 0 when the pins were checked, else -errno */
+ int pins_unmeasured; /* pinned without a record this boot */
+ const char *pins_detail; /* their names, when any */
};
struct condition {
@@ -59,7 +62,7 @@ struct condition {
* KeyringSealed, ChainIntact, TpmMeasured, ConfigActive); callers can
* size their stack array off this.
*/
-#define CONDITIONS_MAX 5
+#define CONDITIONS_MAX 6
size_t conditions(const struct conditions_input *in,
struct condition *out, size_t cap);
diff --git a/util/eventlog.c b/util/eventlog.c
index 335f53e3..dc185b10 100644
--- a/util/eventlog.c
+++ b/util/eventlog.c
@@ -19,6 +19,7 @@
#include <shirudo/misc.h>
#include <shirudo/string.h>
+#include <shirudo/upatch.h>
#include "chain.h"
#include "eventlog.h"
@@ -471,6 +472,78 @@ int eventlog_boot_lines(int (*cb)(const char *line, size_t len,
return walk_boot(lines_line, &l);
}
+struct upatches_ctx {
+ int (*cb)(enum eventlog_upatch_event event, const char *name,
+ const char *payload_hex, void *ctx);
+ void *ctx;
+ char name[UPATCH_NAME_MAX + 1];
+ char payload_hex[2 * UPATCH_PAYLOAD_HASH_LEN + 1];
+ int ret;
+};
+
+static int upatches_set_entry(void *arg)
+{
+ struct upatches_ctx *u = arg;
+
+ if (!u->name[0] || strlen(u->payload_hex) != 2 * UPATCH_PAYLOAD_HASH_LEN)
+ return -EINVAL;
+ u->ret = u->cb(EVENTLOG_UPATCH_LOAD, u->name, u->payload_hex, u->ctx);
+ u->name[0] = '\0';
+ u->payload_hex[0] = '\0';
+ return u->ret;
+}
+
+static int upatches_line(const char *line, size_t len, bool is_init,
+ void *ctx)
+{
+ struct upatches_ctx *u = ctx;
+ struct json_field entry[] = {
+ { "name", JSON_T_STRING, u->name, sizeof(u->name), 0,
+ NULL, 0 },
+ { "payload_sha256", JSON_T_STRING, u->payload_hex,
+ sizeof(u->payload_hex), 0, NULL, 0 },
+ };
+ struct json_array set = { upatches_set_entry, u };
+ char type[32];
+ struct json_field f[] = {
+ { "type", JSON_T_STRING, type, sizeof(type), 0, NULL, 0 },
+ { "set", JSON_T_ARRAY, &set, 0, 0, entry, ARRAY_SIZE(entry) },
+ { "upatch", JSON_T_OBJECT, NULL, 0, 0, entry,
+ ARRAY_SIZE(entry) },
+ };
+ int ret;
+
+ u->name[0] = '\0';
+ u->payload_hex[0] = '\0';
+ type[0] = '\0';
+ if (is_init) {
+ ret = u->cb(EVENTLOG_UPATCH_INIT, NULL, NULL, u->ctx);
+ if (ret)
+ return ret;
+ }
+ if (json_parse(line, len, f, ARRAY_SIZE(f)))
+ return u->ret ? u->ret : -EINVAL;
+ if (is_init)
+ return 0;
+ if (strcmp(type, "upatch_load") && strcmp(type, "upatch_unload"))
+ return 0;
+ if (!f[2].seen || !u->name[0] ||
+ strlen(u->payload_hex) != 2 * UPATCH_PAYLOAD_HASH_LEN)
+ return -EINVAL;
+ return u->cb(type[7] == 'l' ? EVENTLOG_UPATCH_LOAD :
+ EVENTLOG_UPATCH_UNLOAD, u->name, u->payload_hex, u->ctx);
+}
+
+int eventlog_boot_upatches(int (*cb)(enum eventlog_upatch_event event,
+ const char *name,
+ const char *payload_hex, void *ctx),
+ void *ctx)
+{
+ struct upatches_ctx u = { .cb = cb, .ctx = ctx };
+
+ return walk_boot(upatches_line, &u);
+}
+
void eventlog_timestamp(char *out, size_t cap)
{
iso8601(time(NULL), out, cap);
diff --git a/util/eventlog.h b/util/eventlog.h
index 30c1c7a9..1d78408b 100644
--- a/util/eventlog.h
+++ b/util/eventlog.h
@@ -74,6 +74,22 @@ int eventlog_boot_init(struct eventlog_init *out);
int eventlog_boot_lines(int (*cb)(const char *line, size_t len,
void *ctx), void *ctx);
+enum eventlog_upatch_event {
+ EVENTLOG_UPATCH_INIT,
+ EVENTLOG_UPATCH_LOAD,
+ EVENTLOG_UPATCH_UNLOAD,
+};
+
+/*
+ * The loads and unloads this boot's records measure, in order. An init
+ * record gives EVENTLOG_UPATCH_INIT with no name, which starts the set
+ * over, then each upatch of its set as a load.
+ */
+int eventlog_boot_upatches(int (*cb)(enum eventlog_upatch_event event,
+ const char *name,
+ const char *payload_hex, void *ctx),
+ void *ctx);
+
/*
* 1 if appending one more event would push the eventlog past max_bytes;
* 0 if it fits or max_bytes is 0 (unlimited). The register and the log
diff --git a/util/measured.c b/util/measured.c
new file mode 100644
index 00000000..85079509
--- /dev/null
+++ b/util/measured.c
@@ -0,0 +1,126 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+#include <dirent.h>
+#include <errno.h>
+#include <limits.h>
+#include <string.h>
+#include <sys/xattr.h>
+
+#include <shirudo/misc.h>
+#include <shirudo/string.h>
+#include <shirudo/upatch.h>
+
+#include "eventlog.h"
+#include "hex.h"
+#include "malloc.h"
+#include "measured.h"
+#include "paths.h"
+
+#define MEASURED_MAX 4096
+
+struct measured_entry {
+ char name[UPATCH_NAME_MAX + 1];
+ char payload_hex[2 * UPATCH_PAYLOAD_HASH_LEN + 1];
+};
+
+struct measured_set {
+ struct measured_entry *entries;
+ size_t n;
+};
+
+static int measured_event(enum eventlog_upatch_event event, const char *name,
+ const char *payload_hex, void *ctx)
+{
+ struct measured_set *s = ctx;
+ size_t i;
+
+ if (event == EVENTLOG_UPATCH_INIT) {
+ s->n = 0;
+ return 0;
+ }
+ for (i = 0; i < s->n; i++) {
+ if (!strcmp(s->entries[i].name, name))
+ break;
+ }
+ if (event == EVENTLOG_UPATCH_UNLOAD) {
+ if (i < s->n)
+ s->entries[i] = s->entries[--s->n];
+ return 0;
+ }
+ if (i == s->n) {
+ if (s->n == MEASURED_MAX)
+ return -ENOSPC;
+ s->n++;
+ }
+ if (strscpy(s->entries[i].name, name,
+ sizeof(s->entries[i].name)) < 0 ||
+ strscpy(s->entries[i].payload_hex, payload_hex,
+ sizeof(s->entries[i].payload_hex)) < 0)
+ return -EINVAL;
+ return 0;
+}
+
+static bool measured_holds(const struct measured_set *s, const char *name,
+ const char *payload_hex)
+{
+ size_t i;
+
+ for (i = 0; i < s->n; i++) {
+ if (!strcmp(s->entries[i].name, name) &&
+ !strcmp(s->entries[i].payload_hex, payload_hex))
+ return true;
+ }
+ return false;
+}
+
+int measured_unmeasured_pins(int (*cb)(const char *name, void *ctx),
+ void *ctx)
+{
+ struct measured_set s = {};
+ struct dirent **pins = NULL;
+ int n, i, ret, unmeasured = 0;
+
+ s.entries = xcalloc(MEASURED_MAX, sizeof(*s.entries));
+ if (!s.entries)
+ return -ENOMEM;
+ ret = eventlog_boot_upatches(measured_event, &s);
+ if (ret)
+ goto out;
+ n = scandir(BPFFS_UPATCHES_DIR, &pins, NULL, alphasort);
+ if (n < 0) {
+ ret = errno == ENOENT ? 0 : -errno;
+ goto out;
+ }
+ for (i = 0; i < n; i++) {
+ char payload_hex[2 * UPATCH_PAYLOAD_HASH_LEN + 1] = "";
+ __u8 payload[UPATCH_PAYLOAD_HASH_LEN];
+ char name[UPATCH_NAME_MAX + 1];
+ char dir[PATH_MAX];
+
+ if (!strcmp(pins[i]->d_name, ".") ||
+ !strcmp(pins[i]->d_name, "..") ||
+ upatch_name_from_bpffs(name, sizeof(name),
+ pins[i]->d_name) ||
+ strsprintf(dir, sizeof(dir), "%s/%s", BPFFS_UPATCHES_DIR,
+ pins[i]->d_name) < 0)
+ continue;
+ if (getxattr(dir, XATTR_PAYLOAD, payload, sizeof(payload)) ==
+ (ssize_t)sizeof(payload))
+ hex_encode(payload, sizeof(payload), payload_hex);
+ if (measured_holds(&s, name, payload_hex))
+ continue;
+ unmeasured++;
+ if (cb) {
+ ret = cb(name, ctx);
+ if (ret)
+ break;
+ }
+ }
+ for (i = 0; i < n; i++)
+ libc_free(pins[i]);
+ libc_free(pins);
+out:
+ xfree(s.entries, MEASURED_MAX * sizeof(*s.entries));
+ return ret ? ret : unmeasured;
+}
diff --git a/util/measured.h b/util/measured.h
new file mode 100644
index 00000000..16fd00de
--- /dev/null
+++ b/util/measured.h
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+/* Copyright (c) 2026 Authors of shirudo */
+
+#ifndef SHIRUDO_MEASURED_H
+#define SHIRUDO_MEASURED_H
+
+/*
+ * The upatches pinned in the private bpffs that this boot's records do
+ * not measure as loaded with the payload their pin records: what a load
+ * stopped between its attach and its extend leaves, or an unload stopped
+ * after its extend. cb runs for each with its name, and a non-zero
+ * return ends the walk with that value. The number found, or -errno
+ * when the pins or the records cannot be read; -ENOENT when this boot
+ * has no record.
+ */
+int measured_unmeasured_pins(int (*cb)(const char *name, void *ctx),
+ void *ctx);
+
+#endif /* SHIRUDO_MEASURED_H */