eBPF-based security platform
upatch: undo only what the register shows was never measured
Daniel Borkmannd02dc346268b
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rstindex e7cf99f7..4a9ee916 100644--- a/Documentation/contribute/testing/proof-coverage.rst+++ b/Documentation/contribute/testing/proof-coverage.rst@@ -1177,7 +1177,10 @@ the model follows the C.files; pin directory removals, archive renames and appends arewhole; a reboot clears pins and register and ``init`` runs as onestep; root changes only unlabeled archive files and brings files- back; the hash of ``Shirudo.Chain`` for ``replay_differs``+ back; the hash of ``Shirudo.Chain`` for ``replay_differs``, and+ for the undo pass, which reads the set only when this boot's+ records fold to the register's value, taken as the records being+ the register's events- CBMC, ``measured-fold-model``, for ``measured_event()`` and``measured_holds()`` up to six records; review for the rest* - ``Shirudo.TreeSync``, ``cmd_bpm_tree_sync()`` anddiff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex 5c23dedc..76729e00 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -480,6 +480,11 @@ signed config image.- A uprobe set through tracefs on the runtime binary leaves thecommands it reaches without authority, counted as``SealUprobe``, until it is removed+ * - ``t6147-forged-records``+ - A load in a namespace with a ``/run`` of root's making and a+ forged pending record, or another boot ID bound over ``/proc``,+ detaches nothing: the undo pass takes only the records the+ register vouches for``bpm`` (7)~~~~~~~~~~~diff --git a/Documentation/internals/measurement-register.rst b/Documentation/internals/measurement-register.rstindex 6ecfefd0..5c852594 100644--- a/Documentation/internals/measurement-register.rst+++ b/Documentation/internals/measurement-register.rst@@ -297,6 +297,18 @@ append that fails aftera successful extension is reported as a desync for the operator toreconcile.+The undo removes what the records do not cover, so the records it reads+must be the register's and nobody else's. The boot ID and the eventlog+are paths, which root binds over in a mount namespace of its own. The+undo therefore takes the boot from the TPM, the last ``init`` record+whose ``nv_name`` and ``tpm_reset_count`` are the live index's name and+the TPM's reset count, derives every record's digest from its fields,+and folds them to the value the TPM reports for the register. Records+that do not fold make it refuse, ``tampered``, and so does the append+failure above until the next reboot. The pending record counts only+from a ``/run/shirudo`` that carries the seal label, which a directory+of root's making cannot.+``init`` binds the register and writes its record without taking thislock, from the initrd, before any upatch command is admitted. Its runfrom the root file system, which upatch commands can run beside, holdsdiff --git a/Documentation/operate/run/read-status.rst b/Documentation/operate/run/read-status.rstindex 9e397040..50c11851 100644--- a/Documentation/operate/run/read-status.rst+++ b/Documentation/operate/run/read-status.rst@@ -242,7 +242,14 @@ verdict is derived from them.upatch has fewer link pins than its signed metadata implies, so partof it is detached, or its sealed archive is gone or unusable. For an upatch with XDP that means an ``xdp_prog_<i>`` pinor the link of another attach program; a device going away is not- a missing link. Handle it the same way. ``WARN`` ``no+ a missing link, but a loaded upatch whose whole pin directory is+ gone is. Handle it the same way. ``ALERT`` ``this boot's records+ do not fold to the measurement register``: the eventlog this+ command read, from the init record that names the live register+ and the TPM's reset count on, does not give the value the TPM+ reports, or a record's digest does not follow from its fields.+ The pins are then not checked, and every ``upatch load`` and+ ``upatch unload`` refuses; handle it as an incident. ``WARN`` ``norecord of this boot to check the pins against`` before ``init``, or``pins not checked against the records: <error>``. ``WARN`` ``linksor archives not checked: <error>`` means one of those checks coulddiff --git a/Documentation/reference/cli/upatch-load.rst b/Documentation/reference/cli/upatch-load.rstindex 14fb44b9..b492b3f4 100644--- a/Documentation/reference/cli/upatch-load.rst+++ b/Documentation/reference/cli/upatch-load.rst@@ -50,9 +50,11 @@ Descriptionit holds to the end, and undoes the pins and archives this boot'srecords do not cover: those of a load stopped before its extend, ofan unload stopped after it, and an archive brought back after its- unload, which ``status`` reports under ``UpatchesMeasured``. It- refuses (``tpm_error``) when it cannot read this boot's records or- cannot undo one of them. A upatch of the same name that is loaded is+ unload, which ``status`` reports under ``UpatchesMeasured``. It takes+ only the records the register vouches for, which fold to the value+ the TPM reports, and refuses (``tampered``) when they do not, and+ (``tpm_error``) when it cannot read this boot's records or cannot+ undo one of them. A upatch of the same name that is loaded isrefused (``already_loaded``).#. It parses the artifact: a LOAD artifact, a safe and signed name, apayload digest that matches the payload, valid attach metadatadiff --git a/Documentation/reference/failure-categories.rst b/Documentation/reference/failure-categories.rstindex 6db302be..ec36e8c9 100644--- a/Documentation/reference/failure-categories.rst+++ b/Documentation/reference/failure-categories.rst@@ -103,11 +103,13 @@ Categoriesconfig, see :doc:`/operate/maintain/keys`.* - ``tampered``- ``pre_flight``, ``tpm_extend``- - none, ``EPERM``, ``EACCES``, ``ENOENT``, ``ESTALE``+ - none, ``EPERM``, ``EACCES``, ``ENOENT``, ``ESTALE``, ``EBADMSG``- The ``.bpf`` keyring is not the frozen anchor (no errno), the``lsm_link`` pin is missing (``ENOENT``), the register differs from- ``--expect-chain`` or cannot be read (``EPERM``), or the register- is not the one ``init`` bound (``EACCES``, the load is undone). From+ ``--expect-chain`` or cannot be read (``EPERM``), the register+ is not the one ``init`` bound (``EACCES``, the load is undone), or+ this boot's eventlog records do not fold to the register+ (``EBADMSG``, nothing changed). From``upatch reconcile``: the archive of a loaded XDP upatch differsfrom what the records say was loaded, or its program pin is missing(``ENOENT``) or holds no XDP program (``ESTALE``).diff --git a/Documentation/reference/formats/eventlog.rst b/Documentation/reference/formats/eventlog.rstindex 11e672f3..05f76d0e 100644--- a/Documentation/reference/formats/eventlog.rst+++ b/Documentation/reference/formats/eventlog.rst@@ -261,9 +261,25 @@ this boot's records the same way:last ``init`` record and the records after it.- A boot without an ``init`` record has no replay.-The upatches this boot measured are folded from the same records: an-``init`` record starts the set over with its ``set``, and an-``upatch_load`` or ``upatch_unload`` record adds or removes its upatch.+The upatches this boot measured are folded from the records the register+vouches for, which the undo pass of ``upatch load`` and ``upatch+unload``, ``upatch reconcile`` and the ``UpatchesMeasured`` condition of+``status`` read. A boot ID read by path is root's to replace in a mount+namespace of its own, so they select the boot by the TPM instead:++- They start at the last ``init`` record whose ``nv_name`` is the name+ of the live index and whose ``tpm_reset_count`` is the TPM's reset+ count, both read over the session ``init`` pinned.+- They derive each record's digest from its fields, as the extend that+ wrote it computed it (see `Event digests`_), and refuse a record whose+ ``digest`` differs and a type no writer uses.+- They fold the records from zero and refuse unless the result is the+ value the TPM reports for the register.++A refusal is ``tampered`` for a load or an unload and an ``ALERT`` of+``UpatchesMeasured`` in ``status``. An ``init`` record starts the set+over with its ``set``, and an ``upatch_load`` or ``upatch_unload``+record adds or removes its upatch.Only an ``init`` record's ``set`` counts. Lean 4 proves the fold holds anupatch exactly when the last record that bears on it, an ``init`` recordor a load or unload of it, leaves it loaded: its load, or an ``init``@@ -286,7 +302,10 @@ Pending recordroot file system. The ``init`` run of ``shirudo.service`` on the rootfile system adopts it: it appends each pending line that this boot'spart of the eventlog does not already hold, then removes the pending-file. Any other append adopts a pending record first.+file. Any other append adopts a pending record first. Every reader+takes the pending record only from a ``/run/shirudo`` that carries the+seal label, and only when the file carries it too: a ``/run`` of root's+making, in a mount namespace of its own, cannot carry it.Size limits-----------diff --git a/Documentation/reference/formats/status-json.rst b/Documentation/reference/formats/status-json.rstindex 4e5993a2..fa90d30e 100644--- a/Documentation/reference/formats/status-json.rst+++ b/Documentation/reference/formats/status-json.rst@@ -200,6 +200,8 @@ else ``a device an XDP upatch selects does not run it``.- ``a loaded upatch is missing a link or its archive``* -- ``a upatch is pinned or archived without a record of this boot``+ * -+ - ``this boot's records do not fold to the measurement register``* -- ``the selected config cannot be verified against its records``* -@@ -323,6 +325,9 @@ bytes: a longer one, such as a long list of names, is cut and ends with* -- ``ALERT``- ``<n> pinned or archived without a record of this boot: <names>``+ * -+ - ``ALERT``+ - ``this boot's records do not fold to the measurement register``* - ``TpmMeasured``- ``OK``- ``NV index 0x01818E00 readable``diff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rstindex c114ce20..03b04a4d 100644--- a/Documentation/security/threat-model.rst+++ b/Documentation/security/threat-model.rst@@ -978,13 +978,20 @@ acts, and refuses when it cannot read the records or undo them. Thesame scan finds the pins an unload stopped after its extend leaves, andan archive without a record of its load; ``init`` restoresonly an archive whose load the eventlog records without a later unload-(``t6144``). A measuring+(``t6144``). The scan takes only the records the register vouches for:+it starts at the init record that names the live index and the TPM's+reset count, derives every record's digest from its fields and folds+them to the value the TPM reports, and refuses ``tampered`` when they+do not fold. A boot ID that root binds over ``/proc``, or a pending+record in a ``/run`` of root's making, which no reader takes since it+carries no seal label, therefore moves nothing (``t6147``). A measuringcommand 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 andthe log advance in one order and the undo never takes a load in flightfor 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.+leaves the register ahead of the log; shirudo reports the desync,+``shirudo status`` reports a register its replay cannot explain, and+every later load and unload refuses until the next reboot.``init`` binds one register per TPM reset. On a re-run with this boot'srecord it reuses the register and adds no event; when the register it@@ -1006,7 +1013,7 @@ tree operations run without it.``tpm_error`` and ``eventlog_full`` from shirudo:Counters: ``TpmExtend``:Tests: ``t4002``, ``t4003``, ``t4102``, ``t4103``, ``t6115``,- ``t6122``, ``t6128``, ``t6143``, ``t6144``+ ``t6122``, ``t6128``, ``t6143``, ``t6144``, ``t6147``:Proofs: ``Shirudo.Chain``, ``Shirudo.Events``, ``Shirudo.Measured``,``Shirudo.UpatchLoad``, ``chain-events``, ``measured-fold-model``,``eventlog-records``, ``tpm-digest``diff --git a/Makefile.shirudo b/Makefile.shirudoindex a2fbd1d2..27e11883 100644--- a/Makefile.shirudo+++ b/Makefile.shirudo@@ -809,10 +809,10 @@ testing/helper/test-harden: testing/helper/test-harden.c util/harden.o util/log.# util/malloc.o brings in x*/libc_free that chain.o/tpm.o use.# util/config.o resolves config_load(), called by tpm.o.# The register code pulls in the eventlog, which pulls in the seal-# label writer and the JSON codec.+# label writer, the JSON codec and the measured set it folds.REGISTER_OBJS := util/config_attest.o util/config_update.o util/config_measure.o util/config_image.o \util/chain.o util/tpm.o util/tpm_wire.o util/eventlog.o \- util/timefmt.o util/upatch.o \+ util/measured.o util/paths.o util/timefmt.o util/upatch.o \util/hex.o util/crypto_openssl.o util/json.o util/string.o \util/io.o util/seal.o util/config.o util/image_config.o \util/log.o util/malloc.o \diff --git a/builtin-status.c b/builtin-status.cindex 786d3cc4..812834af 100644--- a/builtin-status.c+++ b/builtin-status.c@@ -1264,7 +1264,10 @@ int cmd_status(int argc, const char **argv)}upatch_checks_run(mset, &checks);xdp_tampered = checks.nr_changed;- links_lost = checks.nr_lost;+ links_lost = measured_unpinned(mset, note_unmeasured,+ links_lost_names);+ links_lost = links_lost < 0 ? links_lost :+ links_lost + checks.nr_lost;xdp_uncovered = checks.nr_uncovered;checks_rc = checks.check_err;xdp_coverage_rc = checks.coverage_err;diff --git a/builtin-upatch.c b/builtin-upatch.cindex 8f4c5741..aed71c0f 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -2943,7 +2943,9 @@ static int undo_unmeasured_archive(const char *name, const char *path,* the scan never takes a load in flight for one that was stopped. A* command whose pass cannot read the records or take out what they do* not cover refuses: a load beside such pins could share their directory- * under one name, and its own record would not tell the two apart.+ * under one name, and its own record would not tell the two apart. The+ * records count only as the register vouches for them: the eventlog and+ * the boot ID are paths root can bind over in a namespace of its own.*/static int undo_unmeasured(struct op_report *rep, const char *op){@@ -2951,6 +2953,13 @@ static int undo_unmeasured(struct op_report *rep, const char *op)int failed = 0, ret;ret = measured_set_boot(&set);+ if (ret == -EBADMSG) {+ pr_err("upatch %s: this boot's eventlog records do not fold to the measurement register; refused, nothing changed",+ op);+ rep_fail(rep, "tpm_extend", "tampered", -ret,+ "this boot's records do not fold to the register");+ return ret;+ }if (ret) {pr_err("upatch %s: cannot read this boot's records: %s; refused, nothing changed",op, strerror(-ret));diff --git a/testing/functional/tamperproof/t6147-forged-records.sh b/testing/functional/tamperproof/t6147-forged-records.shnew file mode 100755index 00000000..39d507f0--- /dev/null+++ b/testing/functional/tamperproof/t6147-forged-records.sh@@ -0,0 +1,81 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# t6147 -- the undo pass takes only the records the register vouches for:+# a /run of root's making with a pending record that unloads a loaded+# upatch, or another boot's ID bound over /proc in a namespace of root's,+# makes a load there detach nothing, and the measured upatch stays loaded.++cd "$(dirname "$0")/../.."+. ./integration-lib.sh+target_guard+command -v unshare >/dev/null 2>&1 || test_skip_all "no unshare"+FIXTURES=$SHIRUDO_TEST_FIXTURES_DIR+NAME=shirudo-test-basic+PINDIR="/run/shirudo/bpffs/upatches/$NAME"+ARCHIVE="/var/lib/shirudo/upatches/$NAME.upatch"+EVENTLOG=/var/lib/shirudo/eventlog+RUNTIME=$(runtime_binary)+W=$(mktemp -d)+cleanup() {+ [ ! -e "$PINDIR" ] ||+ "$SHIRUDO_BIN" upatch unload "$FIXTURES/$NAME.unload.upatch" \+ >/dev/null 2>&1 || :+ rm -rf "$W"+}+trap cleanup EXIT+for f in "$FIXTURES/$NAME.upatch" "$FIXTURES/$NAME.unload.upatch" \+ "$FIXTURES/shirudo-test-marker.upatch"; do+ [ -s "$f" ] || test_skip_all "missing fixture $f"+done+require_loaded "$NAME" || test_skip_all "$NAME does not load"+BOOT=$(cat /proc/sys/kernel/random/boot_id)+PAYLOAD=$(grep "\"name\":\"$NAME\"" "$EVENTLOG" | tail -1 |+ sed 's/.*"payload_sha256":"\([0-9a-f]*\)".*/\1/')+[ ${#PAYLOAD} = 64 ] || test_skip_all "no record of the load"+# An artifact the load refuses once it has run its undo pass.+cp "$FIXTURES/shirudo-test-marker.upatch" "$W/bad.upatch"+printf '\001' | dd of="$W/bad.upatch" bs=1 conv=notrunc \+ seek=$(($(stat -c %s "$W/bad.upatch") - 1)) 2>/dev/null+lines=$(wc -l <"$EVENTLOG")++# The forged record unloads the upatch in this boot, by its own boot ID.+printf '{"schema":1,"digest":"%064d","type":"upatch_unload","boot_id":"%s","upatch":{"name":"%s","payload_sha256":"%s"}}\n' \+ 0 "$BOOT" "$NAME" "$PAYLOAD" >"$W/pending"+cat >"$W/run.sh" <<EOF+set -e+mount -t tmpfs none "$W/run"+mkdir -p "$W/run/shirudo/bpffs" "$W/run/shirudo/runtime" "$W/run/shirudo/config"+mount --bind /run/shirudo/bpffs "$W/run/shirudo/bpffs"+mount --bind /run/shirudo/runtime "$W/run/shirudo/runtime"+mount --bind /run/shirudo/config "$W/run/shirudo/config"+cp "$W/pending" "$W/run/shirudo/eventlog.pending"+mount --rbind "$W/run" /run+"$RUNTIME" upatch load --no-precheck "$W/bad.upatch" >"$W/out" 2>&1 || :+EOF+mkdir -p "$W/run"++test_expect_success 'a load under a /run of root'"'"'s making detaches nothing' '+ unshare -m sh "$W/run.sh" &&+ ! grep -q "was pinned without a record" "$W/out" &&+ [ -e "$PINDIR/link" ] &&+ [ -s "$ARCHIVE" ]+'+test_expect_success 'and its record never reaches the eventlog' '+ [ "$(wc -l <"$EVENTLOG")" = "$lines" ] &&+ ! grep -q "\"digest\":\"0000000000000000" "$EVENTLOG"+'+test_expect_success 'another boot ID bound over /proc changes nothing either' '+ echo 00000000-0000-0000-0000-000000000000 >"$W/boot_id" &&+ unshare -m sh -c "mount --bind \"$W/boot_id\" /proc/sys/kernel/random/boot_id &&+ \"$RUNTIME\" upatch load --no-precheck \"$W/bad.upatch\"" \+ >"$W/out" 2>&1 || :+ ! grep -q "was pinned without a record" "$W/out" &&+ [ -e "$PINDIR/link" ] &&+ [ -s "$ARCHIVE" ]+'+test_expect_success 'status still finds every pin in this boot'"'"'s records' '+ "$SHIRUDO_BIN" status 2>/dev/null | grep -q "UpatchesMeasured *OK"+'+test_donediff --git a/testing/proofs/cbmc/conditions-rows.c b/testing/proofs/cbmc/conditions-rows.cindex 0019722c..c677a588 100644--- a/testing/proofs/cbmc/conditions-rows.c+++ b/testing/proofs/cbmc/conditions-rows.c@@ -228,6 +228,8 @@ static bool upatches_row(const struct conditions_input *in,.string = or_unknown(in->pins_detail));if (in->pins_rc == -ENOENT)return WARN("no record of this boot to check the pins against");+ if (in->pins_rc == -EBADMSG)+ return ALERT("this boot's records do not fold to the measurement register");if (in->pins_rc)return WARN("pins not checked against the records: *",.string = strerror(-in->pins_rc));diff --git a/testing/proofs/cbmc/conditions-verdict.c b/testing/proofs/cbmc/conditions-verdict.cindex 39ce4f61..846c70e9 100644--- a/testing/proofs/cbmc/conditions-verdict.c+++ b/testing/proofs/cbmc/conditions-verdict.c@@ -55,6 +55,7 @@ enum cause {XDP_ARCHIVE_CHANGED,LINK_LOST,PIN_UNMEASURED,+ RECORDS_DIFFER,CONFIG_UNVERIFIED,BPF_KEYRING_GONE,BPF_KEYRING_SWAPPED,@@ -70,6 +71,7 @@ static const char *const cause_reason[NR_CAUSES] = {[XDP_ARCHIVE_CHANGED] = "the archive of a loaded XDP upatch differs from what was loaded",[LINK_LOST] = "a loaded upatch is missing a link or its archive",[PIN_UNMEASURED] = "a upatch is pinned or archived without a record of this boot",+ [RECORDS_DIFFER] = "this boot's records do not fold to the measurement register",[CONFIG_UNVERIFIED] = "the selected config cannot be verified against its records",[BPF_KEYRING_GONE] = "the keyring init froze is gone",[BPF_KEYRING_SWAPPED] = "keyring serial swap detected",@@ -90,6 +92,8 @@ static bool cause_holds(const struct conditions_input *in, enum cause cause)return in->links_lost;case PIN_UNMEASURED:return in->pins_unmeasured;+ case RECORDS_DIFFER:+ return in->pins_rc == -EBADMSG;case CONFIG_UNVERIFIED:return in->config_seen && in->config_state &&in->config_state != -EINPROGRESS;diff --git a/testing/proofs/lean4/Shirudo/UpatchLoad.lean b/testing/proofs/lean4/Shirudo/UpatchLoad.leanindex e16ce646..fbac4d1b 100644--- a/testing/proofs/lean4/Shirudo/UpatchLoad.lean+++ b/testing/proofs/lean4/Shirudo/UpatchLoad.lean@@ -12,7 +12,8 @@ activation is complete, which `image_boot_complete()` does after `init`restored the archive and wrote its record. A load (`cmd_upatch_load()`)takes the register lock (`eventlog_lock()`) and runs the undo pass(`undo_unmeasured()`). The pass folds this boot's records into the-measured set (`measured_set_boot()`), removes every pin directory whose+measured set (`measured_set_boot()`), only when they fold to the+register's value, and removes every pin directory whosexattr the set does not hold, with the archive of the same name(`measured_unmeasured_pins()`, `undo_load()`), then every labeled archivefile the set does not hold (`measured_unmeasured_archives()`), and refuses@@ -67,7 +68,9 @@ every move. The assumptions, each a step or the absence of one:eventlog and the private bpffs: root cannot write, rename onto orunlink a labeled file, nor touch a pin.- The register's hash and the event encoding are collision resistant,- as `Shirudo.Chain` assumes, for `replay_differs` only.+ as `Shirudo.Chain` assumes, for `replay_differs` and for the undo+ pass's check of the records against the register, which the step+ takes as their being the register's events.For every sequence of steps from a fresh install, in the C's protocol(`C`):@@ -413,10 +416,13 @@ inductive Step (V : Variant) :Step V s { s.exit i withlost := s.lost + if (inflightEv c).isSome then 1 else 0 }/-- `eventlog_lock()`, then `measured_set_boot()` and `scandir()` of- the pin directories. -/+ the pin directories. The set is read only when this boot's records+ fold to the register's value, which with the hash collision resistant+ is when they are the register's events. -/| lockUndo (s) (i c m todo) : s.procs i = some ⟨c, .start⟩ →c.isPurge = false → canLock V s → V.undo = true →measuredBoot s = some m → (∀ n, s.pins n ≠ none → n ∈ todo) →+ bootEvs s = s.reg →Step V s { s.go i c (.undoPins m todo false) with lock := some i }/-- The lock and no undo pass: the variant without it, or a pass thatcannot read the records and goes on regardless. -/@@ -2411,9 +2417,9 @@ theorem undo_pass_needs_lock :have r2 := r1.step (.spawn _ 0 (.load 1 5) rfl (fun _ => rfl))have r3 := r2.step (.spawn _ 1 (.load 2 5) rfl (fun _ => rfl))have r4 := r3.step (.lockUndo _ 0 (.load 1 5) _ []- rfl rfl (Or.inl rfl) rfl rfl (fun _ h => absurd rfl h))+ rfl rfl (Or.inl rfl) rfl rfl (fun _ h => absurd rfl h) rfl)have r5 := r4.step (.lockUndo _ 1 (.load 2 5) _ []- rfl rfl (Or.inl rfl) rfl rfl (fun _ h => absurd rfl h))+ rfl rfl (Or.inl rfl) rfl rfl (fun _ h => absurd rfl h) rfl)have r6 := r5.step (.undoPinsDone _ 1 _ _ _ [] rfl (fun _ h => absurd rfl h))have r7 := r6.step (.undoArchDone _ 1 _ _ _ rfl (Or.inl rfl))have r8 := r7.step (.loadMkdir _ 1 2 5 rfl (fun _ h => by cases h))@@ -2434,9 +2440,9 @@ theorem log_needs_lock :have r2 := r1.step (.spawn _ 0 (.load 1 5) rfl (fun _ => rfl))have r3 := r2.step (.spawn _ 1 (.load 2 5) rfl (fun _ => rfl))have r4 := r3.step (.lockUndo _ 0 (.load 1 5) _ []- rfl rfl (Or.inl rfl) rfl rfl (fun _ h => absurd rfl h))+ rfl rfl (Or.inl rfl) rfl rfl (fun _ h => absurd rfl h) rfl)have r5 := r4.step (.lockUndo _ 1 (.load 2 5) _ []- rfl rfl (Or.inl rfl) rfl rfl (fun _ h => absurd rfl h))+ rfl rfl (Or.inl rfl) rfl rfl (fun _ h => absurd rfl h) rfl)have r6 := r5.step (.undoPinsDone _ 0 _ _ _ [] rfl (fun _ h => absurd rfl h))have r7 := r6.step (.undoArchDone _ 0 _ _ _ rfl (Or.inl rfl))have r8 := r7.step (.undoPinsDone _ 1 _ _ _ [] rfl (fun _ h => absurd rfl h))@@ -2489,7 +2495,7 @@ theorem undo_needs_refusal :have r1 := reach_boot1 { refuse := false }have r2 := r1.step (.spawn _ 0 (.load 1 5) rfl (fun _ => rfl))have r3 := r2.step (.lockUndo _ 0 (.load 1 5) _ []- rfl rfl (Or.inr rfl) rfl rfl (fun _ h => absurd rfl h))+ rfl rfl (Or.inr rfl) rfl rfl (fun _ h => absurd rfl h) rfl)have r4 := r3.step (.undoPinsDone _ 0 _ _ _ [] rfl (fun _ h => absurd rfl h))have r5 := r4.step (.undoArchDone _ 0 _ _ _ rfl (Or.inl rfl))have r6 := r5.step (.loadMkdir _ 0 1 5 rfl (fun _ h => by cases h))@@ -2543,7 +2549,7 @@ theorem xattr_needs_undo :have r1 := reach_boot1 { xattrUndo := false }have r2 := r1.step (.spawn _ 0 (.load 1 5) rfl (fun _ => rfl))have r3 := r2.step (.lockUndo _ 0 (.load 1 5) _ []- rfl rfl (Or.inr rfl) rfl rfl (fun _ h => absurd rfl h))+ rfl rfl (Or.inr rfl) rfl rfl (fun _ h => absurd rfl h) rfl)have r4 := r3.step (.undoPinsDone _ 0 _ _ _ [] rfl (fun _ h => absurd rfl h))have r5 := r4.step (.undoArchDone _ 0 _ _ _ rfl (Or.inl rfl))have r6 := r5.step (.loadMkdir _ 0 1 5 rfl (fun _ h => by cases h))@@ -2557,7 +2563,7 @@ theorem xattr_needs_undo :rfl rfl (Or.inr rfl) rfl rfl (fun n h => byby_cases hn : n = 1· simp [hn]- · exact absurd (by simp [State.go, State.exit, booted, upd, hn]) h))+ · exact absurd (by simp [State.go, State.exit, booted, upd, hn]) h) rfl)have r14 := r13.step (.undoRemove _ 1 _ _ 1 [] _ .ok rfl rfl)have r15 := r14.step (.undoPinsDone _ 1 _ _ _ [] rfl (fun k h => byby_cases hk : k = 1@@ -2591,7 +2597,7 @@ theorem purge_needs_lock :have r1 := reach_boot1 { purgeLock := false }have r2 := r1.step (.spawn _ 0 (.load 1 5) rfl (fun _ => rfl))have r3 := r2.step (.lockUndo _ 0 (.load 1 5) _ []- rfl rfl (Or.inr rfl) rfl rfl (fun _ h => absurd rfl h))+ rfl rfl (Or.inr rfl) rfl rfl (fun _ h => absurd rfl h) rfl)have r4 := r3.step (.undoPinsDone _ 0 _ _ _ [] rfl (fun _ h => absurd rfl h))have r5 := r4.step (.undoArchDone _ 0 _ _ _ rfl (Or.inl rfl))have r6 := r5.step (.loadMkdir _ 0 1 5 rfl (fun _ h => by cases h))@@ -2611,7 +2617,7 @@ theorem purge_needs_lock :rfl rfl (Or.inr rfl) rfl rfl (fun n h => byby_cases hn : n = 1· simp [hn]- · exact absurd (by simp [State.go, State.exit, booted, upd, hn]) h))+ · exact absurd (by simp [State.go, State.exit, booted, upd, hn]) h) rfl)have r18 := r17.step (.undoRemove _ 1 _ _ 1 [] _ .ok rfl rfl)have r19 := r18.step (.undoPinsDone _ 1 _ _ _ [] rfl (fun k h => byby_cases hk : k = 1@@ -2650,7 +2656,7 @@ theorem init_needs_filter :have r1 := reach_boot1 { filter := false }have r2 := r1.step (.spawn _ 0 (.load 1 5) rfl (fun _ => rfl))have r3 := r2.step (.lockUndo _ 0 (.load 1 5) _ []- rfl rfl (Or.inr rfl) rfl rfl (fun _ h => absurd rfl h))+ rfl rfl (Or.inr rfl) rfl rfl (fun _ h => absurd rfl h) rfl)have r4 := r3.step (.undoPinsDone _ 0 _ _ _ [] rfl (fun _ h => absurd rfl h))have r5 := r4.step (.undoArchDone _ 0 _ _ _ rfl (Or.inl rfl))have r6 := r5.step (.loadMkdir _ 0 1 5 rfl (fun _ h => by cases h))diff --git a/testing/unit/conditions.c b/testing/unit/conditions.cindex 9dce2912..d2cde623 100644--- a/testing/unit/conditions.c+++ b/testing/unit/conditions.c@@ -79,6 +79,27 @@ static void a_lost_link_is_tampered(void)EXPECT_STREQ(status_verdict(&in), "tampered");}+static void records_the_register_does_not_vouch_for_are_tampered(void)+{+ struct conditions_input in = {+ .reachable = true,+ .parsed = true,+ .lsm_attached = 1,+ .pins_rc = -EBADMSG,+ };+ struct condition c[CONDITIONS_MAX];+ const struct condition *row;+ char reason[128];++ row = upatches_measured(&in, c);+ ASSERT_TRUE(row);+ EXPECT_STREQ(row->status, "ALERT");+ EXPECT_TRUE(strstr(row->detail, "do not fold to the measurement register"));+ EXPECT_STREQ(status_verdict(&in), "tampered");+ status_reason(&in, reason, sizeof(reason));+ EXPECT_TRUE(strstr(reason, "do not fold to the measurement register"));+}+static void a_check_that_could_not_run_warns(void){struct conditions_input in = {@@ -211,6 +232,7 @@ static const struct unit_case cases[] = {UNIT_CASE(changed_xdp_archive_is_tampered),UNIT_CASE(a_changed_archive_outranks_a_pin_warning),UNIT_CASE(a_lost_link_is_tampered),+ UNIT_CASE(records_the_register_does_not_vouch_for_are_tampered),UNIT_CASE(a_check_that_could_not_run_warns),UNIT_CASE(no_change_leaves_the_condition_alone),UNIT_CASE(an_uncovered_device_degrades),diff --git a/util/chain.c b/util/chain.cindex 91a5310c..77fb4aa8 100644--- a/util/chain.c+++ b/util/chain.c@@ -479,6 +479,28 @@ int chain_read(__u8 out[32])return ret ? -ENODEV : 0;}+int chain_live(struct chain_live *out)+{+ struct tpm *tpm = NULL;+ struct tpm_nv_info nv;+ int ret;++ memset(out, 0, sizeof(*out));+ if (tpm_open(&tpm))+ return -ENODEV;+ ret = tpm_reset_count(tpm, &out->reset_count) ||+ tpm_nv_info(tpm, NV_INDEX, &nv) ? -EIO : 0;+ if (!ret && (!nv.defined || !nv.written ||+ nv.name_len != TPM_NAME_LEN))+ ret = -ENODEV;+ if (!ret) {+ memcpy(out->name, nv.name, TPM_NAME_LEN);+ ret = tpm_nv_read(tpm, NV_INDEX, out->value) ? -EIO : 0;+ }+ tpm_close(tpm);+ return ret;+}+bool chain_bound(void){struct tpm *tpm = NULL;diff --git a/util/chain.h b/util/chain.hindex 4616ae8b..abaa93f5 100644--- a/util/chain.h+++ b/util/chain.h@@ -107,6 +107,20 @@ int chain_extend(const __u8 event[CHAIN_DIGEST_LEN],/* The live value; -ENODEV when no register is written this boot. */int chain_read(__u8 out[CHAIN_DIGEST_LEN]);+/* The register as the TPM reports it, over the session init pinned. */+struct chain_live {+ __u8 name[TPM_NAME_LEN];+ __u32 reset_count;+ __u8 value[CHAIN_DIGEST_LEN];+};++/*+ * The live register's name, value and TPM reset count. -ENODEV when no+ * TPM answers or the index is not written this reset, -EIO when a read+ * fails.+ */+int chain_live(struct chain_live *out);+/* For a probe: a TPM that does not answer is not logged. */bool chain_bound(void);diff --git a/util/conditions.c b/util/conditions.cindex ddd13555..767f6d0b 100644--- a/util/conditions.c+++ b/util/conditions.c@@ -161,6 +161,9 @@ size_t conditions(const struct conditions_input *in,else 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 == -EBADMSG)+ set_detail(&c[n], "ALERT",+ "this boot's records do not fold to the measurement register");else if (in->pins_rc)set_detail(&c[n], "WARN","pins not checked against the records: %s",@@ -263,8 +266,8 @@ const char *status_verdict(const struct conditions_input *in)if (!in->parsed)return "unparseable";if ((in->cm_seen && !in->chain_matches) || in->pins_unmeasured ||- in->xdp_tampered || in->links_lost || keyring_tampered(in) ||- config_untrusted(in))+ in->pins_rc == -EBADMSG || in->xdp_tampered || in->links_lost ||+ keyring_tampered(in) || config_untrusted(in))return "tampered";if (!in->lsm_attached)return "lsm-off";@@ -300,6 +303,9 @@ void status_reason(const struct conditions_input *in,else if (in->pins_unmeasured)strscpy(buf, "a upatch is pinned or archived without a record of this boot",cap);+ else if (in->pins_rc == -EBADMSG)+ strscpy(buf, "this boot's records do not fold to the measurement register",+ cap);else if (config_untrusted(in))strscpy(buf, "the selected config cannot be verified against its records",cap);diff --git a/util/eventlog.c b/util/eventlog.cindex 432f6734..3a7b89b9 100644--- a/util/eventlog.c+++ b/util/eventlog.c@@ -105,6 +105,29 @@ static int log_records(char *buf, size_t len)return 0;}+/*+ * The record the initrd left in /run/shirudo, as sealed state where the+ * log is: a /run of root's making, in a mount namespace of its own,+ * would otherwise hand each reader records the register never saw, and+ * have the next append adopt them into the sealed log. A host without+ * the seal, as the register tests run on, labels neither.+ */+static int read_pending(char **out, size_t *len)+{+ const char *path = eventlog_pending_path();+ int fd, ret;++ if (strcmp(path, EVENTLOG_PENDING_PATH))+ return read_file(path, JSON_MAX_BYTES, out, len);+ fd = seal_parent_labeled(eventlog_path()) ? seal_open_sealed(path) :+ seal_open_read(path);+ if (fd < 0)+ return fd;+ ret = read_fd(fd, path, JSON_MAX_BYTES, out, len);+ close(fd);+ return ret;+}+static int read_log(char **out, size_t *out_len){size_t base_len = 0, pending_len = 0, off;@@ -115,9 +138,7 @@ static int read_log(char **out, size_t *out_len)&base, &base_len);if (ret && ret != -ENOENT)return ret;- rc_pending = read_file(eventlog_pending_path(),- JSON_MAX_BYTES, &pending,- &pending_len);+ rc_pending = read_pending(&pending, &pending_len);if (rc_pending && rc_pending != -ENOENT) {xfree(base, base_len + 1);return rc_pending;@@ -271,7 +292,7 @@ static int adopt_pending(void)size_t len = 0;int ret;- ret = read_file(pending, JSON_MAX_BYTES, &buf, &len);+ ret = read_pending(&buf, &len);if (ret == -ENOENT)return 0;if (ret)@@ -589,6 +610,22 @@ int eventlog_upatches(int (*cb)(enum eventlog_upatch_event event,return walk_log(true, upatches_line, &u);}+int eventlog_read(char **out, size_t *len)+{+ return read_log(out, len);+}++int eventlog_upatches_record(const char *line, size_t len, bool is_init,+ 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 upatches_line(line, len, is_init, &u);+}+void eventlog_timestamp(char *out, size_t cap){iso8601(time(NULL), out, cap);diff --git a/util/eventlog.h b/util/eventlog.hindex 416ebf80..376b2768 100644--- a/util/eventlog.h+++ b/util/eventlog.h@@ -106,6 +106,18 @@ int eventlog_upatches(int (*cb)(enum eventlog_upatch_event event,void *ctx),void *ctx);+/*+ * The sealed log and the pending record after it, xmalloc'd to *len + 1+ * bytes, for a reader that checks each record against the register.+ */+int eventlog_read(char **out, size_t *len);+/* One record of that log to cb, as eventlog_upatches() would give it. */+int eventlog_upatches_record(const char *line, size_t len, bool is_init,+ 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 logdiff --git a/util/measured.c b/util/measured.cindex 3993df37..2b5a8d26 100644--- a/util/measured.c+++ b/util/measured.c@@ -5,6 +5,7 @@#include <errno.h>#include <limits.h>#include <string.h>+#include <sys/stat.h>#include <sys/xattr.h>#include <shirudo/misc.h>@@ -14,14 +15,13 @@#include "chain.h"#include "eventlog.h"#include "hex.h"+#include "json.h"#include "malloc.h"#include "measured.h"#include "paths.h"#include "seal.h"#include "upatch.h"-#define MEASURED_MAX 4096-struct measured_entry {char name[UPATCH_NAME_MAX + 1];char payload_hex[2 * UPATCH_PAYLOAD_HASH_LEN + 1];@@ -64,6 +64,239 @@ static int measured_event(enum eventlog_upatch_event event, const char *name,return 0;}+/* Whether line is an init record of the register the TPM reports now. */+static bool init_of_live(const char *line, size_t len,+ const struct chain_live *live)+{+ char type[8], name_hex[2 * TPM_NAME_LEN + 1];+ __u8 name[TPM_NAME_LEN];+ __s64 reset = -1;+ struct json_field f[] = {+ { "type", JSON_T_STRING, type, sizeof(type), 0, NULL, 0 },+ { "nv_name", JSON_T_STRING, name_hex, sizeof(name_hex), 0,+ NULL, 0 },+ { "tpm_reset_count", JSON_T_INT, &reset, 0, 0, NULL, 0 },+ };++ type[0] = '\0';+ if (json_parse(line, len, f, ARRAY_SIZE(f)) || strcmp(type, "init") ||+ !f[1].seen || !f[2].seen || reset != live->reset_count ||+ strlen(name_hex) != 2 * TPM_NAME_LEN ||+ hex_decode(name_hex, name, sizeof(name)))+ return false;+ return !memcmp(name, live->name, sizeof(name));+}++struct record_fields {+ char type[32];+ char digest_hex[2 * 32 + 1];+ char cert_hex[2 * 32 + 1];+ char tree_root_hex[2 * 32 + 1];+ char key_hex[2 * 32 + 1];+ char operation[sizeof("sha256:") + 2 * 32];+ char name[UPATCH_NAME_MAX + 1];+ char payload_hex[2 * UPATCH_PAYLOAD_HASH_LEN + 1];+ __s64 seq;+ struct measured_upatch *set;+ size_t n;+ bool init, upatch, key, tree;+};++static int record_set_entry(void *arg)+{+ struct record_fields *r = arg;+ int ret;++ ret = measured_set_add(r->set, &r->n, MEASURED_MAX, r->name,+ r->payload_hex);+ r->name[0] = '\0';+ r->payload_hex[0] = '\0';+ return ret;+}++static int record_parse(const char *line, size_t len, struct record_fields *r)+{+ struct json_field entry[] = {+ { "name", JSON_T_STRING, r->name, sizeof(r->name), 0, NULL, 0 },+ { "payload_sha256", JSON_T_STRING, r->payload_hex,+ sizeof(r->payload_hex), 0, NULL, 0 },+ };+ struct json_field upatch[] = {+ { "name", JSON_T_STRING, r->name, sizeof(r->name), 0, NULL, 0 },+ { "payload_sha256", JSON_T_STRING, r->payload_hex,+ sizeof(r->payload_hex), 0, NULL, 0 },+ };+ struct json_field key[] = {+ { "cert_sha256", JSON_T_STRING, r->key_hex, sizeof(r->key_hex),+ 0, NULL, 0 },+ };+ struct json_field tree[] = {+ { "seq", JSON_T_INT, &r->seq, 0, 0, NULL, 0 },+ { "operation_digest", JSON_T_STRING, r->operation,+ sizeof(r->operation), 0, NULL, 0 },+ };+ struct json_array set = { record_set_entry, r };+ struct json_field f[] = {+ { "type", JSON_T_STRING, r->type, sizeof(r->type), 0, NULL, 0 },+ { "digest", JSON_T_STRING, r->digest_hex, sizeof(r->digest_hex),+ 0, NULL, 0 },+ { "cert_sha256", JSON_T_STRING, r->cert_hex, sizeof(r->cert_hex),+ 0, NULL, 0 },+ { "tree_root", JSON_T_STRING, r->tree_root_hex,+ sizeof(r->tree_root_hex), 0, NULL, 0 },+ { "set", JSON_T_ARRAY, &set, 0, 0, entry, ARRAY_SIZE(entry) },+ { "upatch", JSON_T_OBJECT, NULL, 0, 0, upatch,+ ARRAY_SIZE(upatch) },+ { "key", JSON_T_OBJECT, NULL, 0, 0, key, ARRAY_SIZE(key) },+ { "tree", JSON_T_OBJECT, NULL, 0, 0, tree, ARRAY_SIZE(tree) },+ };++ if (json_parse(line, len, f, ARRAY_SIZE(f)) || !f[0].seen ||+ !f[1].seen)+ return -EINVAL;+ r->init = f[2].seen && f[3].seen && f[4].seen;+ r->upatch = f[5].seen;+ r->key = key[0].seen;+ r->tree = tree[0].seen && tree[1].seen && r->seq > 0;+ return 0;+}++static int init_event_of(struct record_fields *r, __u8 event[32])+{+ __u8 cert[32], tree_root[32], set_digest[32];++ if (!r->init || hex_decode(r->cert_hex, cert, sizeof(cert)) ||+ hex_decode(r->tree_root_hex, tree_root, sizeof(tree_root)))+ return -EINVAL;+ return chain_init_event(cert, r->set, r->n, tree_root, set_digest,+ event);+}++static int upatch_event_of(struct record_fields *r, __u8 event[32])+{+ __u8 payload[UPATCH_PAYLOAD_HASH_LEN];++ if (!r->upatch || !upatch_member_valid(r->name, r->payload_hex) ||+ hex_decode(r->payload_hex, payload, sizeof(payload)))+ return -EINVAL;+ return chain_upatch_event(r->type[7] == 'l' ? "shirudo-upatch" :+ "shirudo-unload", r->name, payload, event);+}++static int key_event_of(struct record_fields *r, __u8 event[32])+{+ __u8 cert[32];++ if (!r->key || hex_decode(r->key_hex, cert, sizeof(cert)))+ return -EINVAL;+ return chain_key_event("shirudo-key-revoke", cert, event);+}++static int tree_event_of(struct record_fields *r, __u8 event[32])+{+ __u8 operation[32];++ if (!r->tree || sha256_digest_decode(r->operation, operation))+ return -EINVAL;+ return chain_tree_event(r->seq, operation, event);+}++static int record_event_of(struct record_fields *r, __u8 event[32])+{+ if (!strcmp(r->type, "init"))+ return init_event_of(r, event);+ if (!strcmp(r->type, "upatch_load") ||+ !strcmp(r->type, "upatch_unload"))+ return upatch_event_of(r, event);+ if (!strcmp(r->type, "key_revoke"))+ return key_event_of(r, event);+ if (!strcmp(r->type, "tree_op"))+ return tree_event_of(r, event);+ return -EINVAL;+}++/*+ * The event the fields of a record give, as the extend that wrote it+ * made it, and the digest the record claims for it: a record counts for+ * what it says only where the two agree. A type no writer uses is+ * refused.+ */+static int record_event(const char *line, size_t len, __u8 event[32],+ __u8 digest[32])+{+ struct record_fields *r;+ int ret;++ r = xcalloc(1, sizeof(*r));+ if (!r)+ return -ENOMEM;+ r->set = xcalloc(MEASURED_MAX, sizeof(*r->set));+ if (!r->set) {+ xfree(r, sizeof(*r));+ return -ENOMEM;+ }+ ret = record_parse(line, len, r);+ if (!ret && hex_decode(r->digest_hex, digest, 32))+ ret = -EINVAL;+ if (!ret)+ ret = record_event_of(r, event);+ xfree(r->set, MEASURED_MAX * sizeof(*r->set));+ xfree(r, sizeof(*r));+ return ret;+}++/*+ * eventlog_boot_upatches() for the records the TPM vouches for: the boot+ * starts at the last init record that names the live index and the+ * TPM's reset count, every record's digest is derived from its fields,+ * and the records fold to the value the TPM reports. cb sees the events+ * as the walk goes; only a return of 0 makes them this boot's. -ENOENT+ * without such an init record, -EBADMSG when a record or the fold does+ * not match, -ENODEV without a register this reset.+ */+static int verified_upatches(int (*cb)(enum eventlog_upatch_event event,+ const char *name,+ const char *payload_hex, void *ctx),+ void *ctx)+{+ __u8 chain[32] = {}, event[32], digest[32];+ const char *anchor = NULL, *line, *end;+ struct chain_live live;+ char *buf = NULL;+ size_t len = 0;+ int ret;++ ret = chain_live(&live);+ if (ret)+ return ret;+ ret = eventlog_read(&buf, &len);+ if (ret)+ return ret;+ for (line = buf; (end = strchr(line, '\n')); line = end + 1) {+ if (init_of_live(line, (size_t)(end - line), &live))+ anchor = line;+ }+ if (!anchor)+ ret = -ENOENT;+ for (line = anchor; !ret && (end = strchr(line, '\n'));+ line = end + 1) {+ ret = record_event(line, (size_t)(end - line), event, digest);+ if (!ret && memneq_explicit(event, digest, sizeof(event)))+ ret = -EBADMSG;+ if (!ret)+ ret = chain_fold(chain, event);+ if (!ret)+ ret = eventlog_upatches_record(line,+ (size_t)(end - line),+ line == anchor, cb,+ ctx);+ }+ if (!ret && memneq_explicit(chain, live.value, sizeof(chain)))+ ret = -EBADMSG;+ xfree(buf, len + 1);+ return ret;+}+static int measured_set_fold(struct measured_set **out, bool all){struct measured_set *s;@@ -80,7 +313,7 @@ static int measured_set_fold(struct measured_set **out, bool all)return -ENOMEM;}ret = all ? eventlog_upatches(measured_event, s) :- eventlog_boot_upatches(measured_event, s);+ verified_upatches(measured_event, s);if (ret == -ENOENT && all)ret = 0;if (ret) {@@ -199,6 +432,29 @@ int measured_unmeasured_pins(const struct measured_set *s,return ret ? ret : unmeasured;}+int measured_unpinned(const struct measured_set *s,+ int (*cb)(const char *name, void *ctx), void *ctx)+{+ char dir[PATH_MAX];+ int ret = 0, n = 0;+ struct stat st;+ size_t i;++ for (i = 0; i < s->n && !ret; i++) {+ if (upatch_pin_path(dir, sizeof(dir), s->entries[i].name,+ NULL) < 0)+ return -ENAMETOOLONG;+ if (!lstat(dir, &st))+ continue;+ if (errno != ENOENT)+ return -errno;+ n++;+ if (cb)+ ret = cb(s->entries[i].name, ctx);+ }+ return ret ? ret : n;+}+int measured_unmeasured_archives(const struct measured_set *s,int (*cb)(const char *name,const char *path, void *ctx),diff --git a/util/measured.h b/util/measured.hindex 4499df78..8fadd2b0 100644--- a/util/measured.h+++ b/util/measured.h@@ -11,12 +11,14 @@* The set of upatches the eventlog records as loaded, each with the* payload of its load: an init record's set, with every later load* added and every unload removed, in order. measured_set_boot() folds- * this boot's records and is -ENOENT when there are none;- * measured_set_log() folds every boot's, an empty set when there is no- * log yet.+ * this boot's records as eventlog_verified_upatches() checks them+ * against the register, and fails as it does; measured_set_log() folds+ * every boot's, an empty set when there is no log yet.*/struct measured_set;+#define MEASURED_MAX 4096+int measured_set_boot(struct measured_set **out);int measured_set_log(struct measured_set **out);bool measured_holds(const struct measured_set *s, const char *name,@@ -56,4 +58,13 @@ int measured_unmeasured_archives(const struct measured_set *s,const char *path, void *ctx),void *ctx);+/*+ * The upatches the set holds without a pin directory: detached with no+ * unload of record, which only a task with runtime authority does. cb+ * runs for each; the number found, or -errno when a pin cannot be+ * looked up.+ */+int measured_unpinned(const struct measured_set *s,+ int (*cb)(const char *name, void *ctx), void *ctx);+#endif /* SHIRUDO_MEASURED_H */diff --git a/util/seal.c b/util/seal.cindex 3c31a360..2d7e2b9a 100644--- a/util/seal.c+++ b/util/seal.c@@ -472,14 +472,21 @@ int seal_open(const char *path, int flags, mode_t mode)return fd;}-int seal_open_read(const char *path)+static int seal_open_file(const char *path, bool sealed){+ char label[SEAL_LABEL_LEN];const char *name;int dfd, fd, ret;dfd = seal_open_parent(path, &name);if (dfd < 0)return dfd;+ if (sealed && seal_fget(dfd, label) != SEAL_LABEL_LEN) {+ close(dfd);+ pr_err("%s: its directory is not sealed state; refusing to read it",+ path);+ return -EPERM;+ }fd = openat(dfd, name, O_RDONLY | O_NOFOLLOW | O_NONBLOCK | O_CLOEXEC);ret = fd < 0 ? -errno : seal_regular(fd);if (!ret)@@ -496,6 +503,31 @@ int seal_open_read(const char *path)return fd;}+bool seal_parent_labeled(const char *path)+{+ char label[SEAL_LABEL_LEN];+ const char *name;+ bool labeled;+ int dfd;++ dfd = seal_open_parent(path, &name);+ if (dfd < 0)+ return false;+ labeled = seal_fget(dfd, label) == SEAL_LABEL_LEN;+ close(dfd);+ return labeled;+}++int seal_open_read(const char *path)+{+ return seal_open_file(path, false);+}++int seal_open_sealed(const char *path)+{+ return seal_open_file(path, true);+}+bool seal_owns(const char *path){int fd = seal_open_read(path);diff --git a/util/seal.h b/util/seal.hindex a3ab189f..992f5f18 100644--- a/util/seal.h+++ b/util/seal.h@@ -56,9 +56,19 @@ int seal_mkdir_p(const char *path, mode_t mode);*/int seal_open_read(const char *path);+/*+ * seal_open_read() of an entry whose directory must carry the label:+ * one of root's making, which no task without authority can label,+ * holds nothing shirudo takes for its own. -EPERM when it does not.+ */+int seal_open_sealed(const char *path);+/* Whether seal_open_read() would succeed. */bool seal_owns(const char *path);+/* Whether the directory path names an entry of carries the seal label. */+bool seal_parent_labeled(const char *path);+int seal_write_file(const char *path, const void *data, size_t len,mode_t mode);