Skip to content

eBPF-based security platform

config: end a TPM boot only at a TPM reset

Daniel Borkmann5c6c3b106d04
diff --git a/Documentation/concepts/background/tpm.rst b/Documentation/concepts/background/tpm.rst
index 364597fe..a17ac7f8 100644
--- a/Documentation/concepts/background/tpm.rst
+++ b/Documentation/concepts/background/tpm.rst
@@ -478,15 +478,18 @@ restores it or starts clear. Table 2 lists the three cases.
- Restart count up
- Both cleared
- Refused from ``init`` on, through ``lsm/locked_down``. Should a
- restart happen anyway, extensions fail with ``tpm_error``,
- commands refuse with ``ESTALE`` and ``init`` refuses to bind the
+ restart happen anyway, the reset count says the TPM boot goes on:
+ extensions fail with ``tpm_error``, commands refuse with
+ ``EUCLEAN`` as the config PCR no longer holds its replay, and
+ ``init`` neither records a config boot event nor binds the
register again, until a reboot.
* - Resume from sleep: TPM resume, ``Startup(STATE)`` after
``Shutdown(STATE)``
- Restart count up
- Both restored
- - Commands refuse with ``ESTALE``, because the config activation
- record names another TPM boot, until a reboot.
+ - The TPM boot goes on: commands run as before, a pending config
+ activation stays pending and ``init`` records nothing, until a
+ reboot. A command the suspend interrupts refuses.
``TPM2_ReadClock`` reads the counts, and every quote and certification
carries them. For keys outside the
@@ -546,8 +549,9 @@ these.
Kernel-internal TPM use and direct access to the TPM's bus are
outside it, and every executable in the :term:`runtime image` runs with
:term:`runtime authority` and can extend the register.
-- A TPM restart or resume without a reboot stops shirudo's commands
- until the next reboot (Table 2).
+- A TPM restart without a reboot stops shirudo's commands until the
+ next reboot, and a resume leaves a pending config activation pending
+ until then (Table 2).
- The algorithms are classical: ECDSA P-256 for the AK, RSA-3072 for
the writer key, RSA 2048 or P-256 for the EK. Version 185 of the
TPM 2.0 library specification adds ML-DSA and ML-KEM, but shirudo
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rst
index 2c9440fb..a30e047e 100644
--- a/Documentation/contribute/testing/proof-coverage.rst
+++ b/Documentation/contribute/testing/proof-coverage.rst
@@ -885,8 +885,8 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.
the first line
- ``event_follows()`` accepts exactly what ``Follows`` of
``Shirudo.ConfigActivation`` does and refuses with ``EUCLEAN``;
- ``same_event()`` is the equality of TPM boot, sequence, PCR, kind,
- initial value and root
+ ``same_event()`` is the equality of TPM boot, the reset count,
+ sequence, PCR, kind, initial value and root
* - ``config-evidence-event``
- ``on_event()``, ``append_event()`` of :src:`util/config_attest.c`
- Every event the JSON parser can hand over, into a history of 0, 1,
@@ -1194,7 +1194,10 @@ the model follows the C.
- ``consistent_or_refusing``, ``refusal_persists``: the PCR replays
from the record with its config mounted, or every command but
``status`` and ``image status`` refuses, a pending or drifted state
- until the TPM boot changes; ``extends_once``,
+ until the TPM boot changes; ``pending_until_reboot``,
+ ``resume_keeps_pending``: on any TPM a pending activation stays
+ pending through every resume and restart, until the reset count
+ changes; ``extends_once``,
``extension_follows_pending``, ``interrupted_never_extends``: one
extension per activation and TPM boot, after the pending record,
none after an interruption; ``extension_follows_save``: on root's
@@ -1217,16 +1220,19 @@ the model follows the C.
writes the config mount during a boot; only shirudo writes the
journal, between boots too, which R14 says root can, for all but
``boot_completion`` and ``completion_by_complete``, which hold
- from any journal; PCRs reset only at a reboot or TPM Restart; PCRs
- compared as their lists of extensions, which ``pcr_value_eq_iff``
- justifies for the idealized hash of ``register_injective`` and
- SHA-256 meets unless it collides among the values compared. All
- but
+ from any journal; PCRs reset only at a reboot, a TPM reset or a
+ TPM Restart; PCRs compared as their lists of extensions, which
+ ``pcr_value_eq_iff`` justifies for the idealized hash of
+ ``register_injective`` and SHA-256 meets unless it collides among
+ the values compared. The TPM boot is the reset count; the restart
+ count, which only refuses more, is left out. All but
``journal_wellformed``, ``mounted_selected``, ``boot_completion``,
- ``completion_by_complete`` and ``extension_follows_save`` also take
- a calm TPM, whose boot changes only between commands and never to
- an identity it had, which root breaks with a suspend or
- ``TPM2_Clear`` (R4 of :doc:`/security/threat-model`)
+ ``completion_by_complete``, ``extension_follows_save`` and
+ ``pending_until_reboot`` also take a calm TPM, whose reset count
+ changes only between commands and never to a value it had, and
+ which restarts without a reboot only by a resume between commands,
+ which root breaks with a suspend, a TPM restart or ``TPM2_Clear``
+ (R4 of :doc:`/security/threat-model`)
- Review; CBMC, ``config-update-follows``, for ``event_follows()`` and
``same_event()``
* - ``Shirudo.UpatchLoad``, the load, unload and purge of
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index e4d1cffd..72c98305 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -361,14 +361,15 @@ signed config image.
- Bootstrap settings in the initrd, ordered config verification keys,
duplicate paths and too many keys
* - ``t5007-config-update``
- - Activation events, the pending latch, interruption, locks,
- symbolic links in state
+ - Activation events, the pending latch across a resume and a TPM
+ restart, interruption, locks, symbolic links in state
* - ``t5008-text-lines``
- Long lines and NUL bytes in settings files
* - ``t5009-config-stage``
- Staging without activation or measurement, selection on the next
- TPM boot, cancellation, stale parents, malformed records and
- process death while replacing a staged selection
+ TPM reset and not after a resume or a restart, cancellation, stale
+ parents, malformed records and process death while replacing a
+ staged selection
* - ``t5100-verity-stage``
- A new signer may be staged without live enrollment, activation or
measurement; same-boot init leaves it staged and cancellation is
@@ -836,10 +837,10 @@ signed config image.
Unit suites and fuzz targets
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-The unit suites in :src:`testing/unit/` cover 34 of the modules in
+The unit suites in :src:`testing/unit/` cover 35 of the modules in
:src:`util/`: ``attest``, ``bpm_oci``, ``bpm_package``, ``bpm_state``,
``bpm_tree``, ``bpm_tree_overlap``, ``bpm_tree_state``, ``chain``,
-``chain_history``, ``conditions``, ``crypto_openssl``,
+``chain_history``, ``conditions``, ``config_update``, ``crypto_openssl``,
``eventlog``, ``format``, ``hex``, ``io``, ``java_agent``, ``json``,
``kernel_abi``,
``keepalive``, ``keepalive_quote``, ``kernel_build_id``,
@@ -1014,7 +1015,7 @@ Coverage by area
* - Config images
- ``t4004``, ``t5001`` to ``t5008``, ``t5101`` to ``t5105``,
``t6112``, ``t6127``, ``t6130``, ``t6132``. Fuzz: ``config``,
- ``config-update``, ``image``
+ ``config-update``, ``image``. Unit: ``config_update``
* - Packages and repositories
- ``t7001``, ``t7002``, ``t7003``, ``t7004``, ``t7005``,
``t7006``. Fuzz:
@@ -1298,7 +1299,7 @@ Parsers and units
- The dm-verity superblock check in :src:`util/config_image.c` has no
fuzz target. ``t5001`` covers truncation and unsupported fields.
-- 40 of the 69 modules in :src:`util/` have no unit suite of their own.
+- 39 of the 69 modules in :src:`util/` have no unit suite of their own.
Related
-------
diff --git a/Documentation/internals/boot-flow.rst b/Documentation/internals/boot-flow.rst
index 93b02087..9b2e2631 100644
--- a/Documentation/internals/boot-flow.rst
+++ b/Documentation/internals/boot-flow.rst
@@ -216,20 +216,28 @@ Hibernation and TPM resets
shirudo refuses hibernation from ``init`` on (A9 in
:doc:`/security/threat-model`). The register and the config record
-are tied to the TPM's reset and restart counts. When the TPM is reset or
-restarted without a reboot, which a resume from hibernation can do, the
-register is cleared and one of the counts changes while the kernel's
-boot ID stays. Register extensions then fail with ``tpm_error``, and every
-target command except ``shirudo status`` and ``shirudo image status``
-refuses because the config record belongs to another TPM boot. Nothing
-re-runs ``init`` automatically. After a TPM reset, whose count the
-``init`` record no longer matches, a manual ``shirudo init`` records a
-new config boot event and binds the register again, from the root file
-system and with the PCR values of that moment; a verifier that requires
-an initrd binding rejects that evidence. After a TPM restart the reset
-count still matches the record, and ``init`` refuses to bind the cleared
-register again (``tampered``) before the next reboot, which leaves the
-config selection it recorded pending.
+are tied to the TPM's reset count: a TPM boot ends only when it changes.
+When the TPM is reset or restarted without a reboot, which a resume from
+hibernation can do, the register is cleared while the kernel's boot ID
+stays, and register extensions fail with ``tpm_error``. Nothing re-runs
+``init`` automatically.
+
+- After a TPM reset the config record belongs to another TPM boot, and
+ every target command except ``shirudo status`` and ``shirudo image
+ status`` refuses. A manual ``shirudo init`` records a new config boot
+ event and binds the register again, from the root file system and
+ with the PCR values of that moment; a verifier that requires an initrd
+ binding rejects that evidence. ``TPM2_Clear`` sets the reset count
+ back to zero and counts as such a reset.
+- After a TPM restart the TPM boot goes on, but the restart cleared the
+ config PCR, so commands refuse with ``EUCLEAN``. ``init`` records no
+ boot event in the same TPM boot and refuses to bind the cleared
+ register again (``tampered``), so the target stays so, and a pending
+ activation stays pending, until the next reboot.
+- A resume from suspend to RAM keeps the PCRs, the register and the TPM
+ boot: commands run as before, and a pending activation stays pending
+ until the next reboot. A command the suspend interrupts refuses, as
+ its read back finds the restart count moved.
Kexec
-----
diff --git a/Documentation/internals/config-images.rst b/Documentation/internals/config-images.rst
index 0231deaa..8533d83a 100644
--- a/Documentation/internals/config-images.rst
+++ b/Documentation/internals/config-images.rst
@@ -261,9 +261,10 @@ init can enroll them during the next planned boot, before sealing
unchanged.
The sealed ``next-boot`` file uses the same selection record format as
-``state``. Its target is the candidate and its sequence and TPM reset and
-restart counts identify the completed activation it was staged from.
-Init selects it only in a later TPM boot with that parent still selected.
+``state``. Its target is the candidate and its sequence and TPM reset
+count identify the completed activation it was staged from.
+Init selects it only in a later TPM boot, after a TPM reset and not a
+resume or a restart, with that parent still selected.
It rechecks the exact root, signature, image contents and certificate
vouches before enrollment. The normal boot event measures its activation.
@@ -293,7 +294,9 @@ never reboots it.
The reboot is the recovery. At the next boot, ``init`` selects the
config the record names, pending or not, maps and checks it again,
records a fresh boot event in the config PCR and completes. It never
-repeats the previous boot's extension.
+repeats the previous boot's extension. A TPM boot is the TPM's reset
+count: a resume from suspend or a TPM restart keeps it, so neither ends a
+pending activation, and ``init`` run by hand after one records nothing.
A pending selection is one of three states in which every target
command except ``shirudo status`` and ``shirudo image status`` refuses,
@@ -312,20 +315,21 @@ warning.
- An activation stops or is refused after its pending record: a
crash, a power loss, a failed extension, append, revocation or
mount, or an ``init`` that fails after its boot event.
- - The next TPM boot's ``init`` records a fresh boot event and
- completes.
+ - The next TPM boot's ``init``, after a reboot, records a fresh boot
+ event and completes. A resume or a TPM restart is no new TPM boot.
* - Drift, ``EUCLEAN``
- The config PCR does not replay from the config log, because
- something else extended it, or does not read back as extended;
- or the log or the mounted config is not the one the record names.
+ something else extended it, a TPM restart cleared it, or it does
+ not read back as extended; or the log or the mounted config is not
+ the one the record names.
- A reboot.
* - Another TPM boot, ``ESTALE``
- - The TPM's reset or restart count differs from the record's, or
- changes while a command reads them.
- - ``init`` records a boot event for the new TPM boot. After a TPM
- reset it completes. After a TPM restart it refuses to bind the
- cleared register (``tampered``), so the selection stays pending
- until a reboot; :doc:`boot-flow` explains why.
+ - The TPM's reset count differs from the record's, after a TPM
+ reset or ``TPM2_Clear`` without a reboot, or it or the restart
+ count changes while a command reads them.
+ - ``init`` records a boot event for the new TPM boot, binds the
+ register from the root file system and completes;
+ :doc:`boot-flow` explains what a verifier sees.
Lean 4 proves this protocol for every sequence of commands, crashes,
failed or lost TPM answers, extensions by other processes and reboots:
@@ -333,9 +337,12 @@ shirudo extends the config PCR at most once per activation and TPM boot
and never after an interruption, a pending or drifted state refuses
every command but ``status`` and ``image status`` until the TPM boot
changes, and after a reboot an uninterrupted ``init`` records the boot
-event and admits commands again. These theorems take a TPM whose boot
-changes only between commands and never back to an identity it had.
-Root can break both, with a suspend in the middle of a command or with
+event and admits commands again. On any TPM, a pending activation stays
+pending through every resume and TPM restart until the reset count
+changes. The other theorems take a TPM whose reset count changes only
+between commands and never back to a value it had, and which restarts
+without a reboot only by a resume between commands. Root can break that
+with a suspend in the middle of a command, a TPM restart or
``TPM2_Clear`` (R4 in :doc:`/security/threat-model`); then one activation
can be extended twice in one TPM boot, and only the journal, the mount,
the save before each extension and ``init``'s completion stay proven.
diff --git a/Documentation/operate/maintain/backup-recovery.rst b/Documentation/operate/maintain/backup-recovery.rst
index e01fbbdd..bf855da8 100644
--- a/Documentation/operate/maintain/backup-recovery.rst
+++ b/Documentation/operate/maintain/backup-recovery.rst
@@ -308,8 +308,8 @@ into it (:doc:`/operate/maintain/upgrade`).
TPM reset or restarted without a reboot
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-The activation journal records the TPM's reset and restart counts. When
-either changes while the system runs, as on a resume from hibernation:
+The activation journal records the TPM's reset count, which only a TPM
+reset or ``TPM2_Clear`` changes. When it changes while the system runs:
- Every command that reads target state, except ``status`` and
``image``, prints ``shirudo: image state: Stale file handle`` and
@@ -320,11 +320,18 @@ either changes while the system runs, as on a resume from hibernation:
reads ``the config activation record does not belong to this TPM boot``.
- The register can be gone, which ``ChainIntact`` reports as a warning.
-If you know the TPM was reset, reboot. Do not run ``init`` again: after a
-TPM reset it binds the register from the root file system, which a
-verifier that pins PCR 11 rejects, and after a restart alone it refuses
-to bind and leaves a config activation pending. If you do not know why
-the counts changed, handle the target as in
+A TPM restart, as a resume from hibernation does, keeps the reset count
+but clears the config PCR and the register: commands print ``shirudo:
+image state: Structure needs cleaning``, and ``ConfigActive`` reads
+``the config PCR, activation records and mounted config disagree``. A
+resume from suspend to RAM keeps both, and the target runs on; a config
+activation it finds pending stays pending until the next reboot.
+
+If you know the TPM was reset or restarted, reboot. Do not run ``init``
+again: after a TPM reset it binds the register from the root file
+system, which a verifier that pins PCR 11 rejects, and after a restart
+it records nothing and refuses to bind. If you do not know why the
+counts changed, handle the target as in
:doc:`/operate/troubleshoot/incident-tampered` before you reboot.
:doc:`/operate/plan/hardening` covers turning hibernation off.
diff --git a/Documentation/operate/plan/interoperability.rst b/Documentation/operate/plan/interoperability.rst
index f6dabe2a..3820240c 100644
--- a/Documentation/operate/plan/interoperability.rst
+++ b/Documentation/operate/plan/interoperability.rst
@@ -338,23 +338,25 @@ Hibernation and suspend
``lsm/file_open`` the write of the ``HibernateLocation`` EFI variable
from which systemd's initrd would resume (A9 in
:doc:`/security/threat-model`). The behavior of the register across
- hibernation is not analyzed. Before a suspend, the kernel's TPM driver sends ``TPM2_Shutdown(STATE)`` to a
- TPM 2.0 unless the TPM stays powered, and the TPM counts that in its
- restart count (:doc:`/concepts/background/tpm`). The config activation
- record holds the TPM's reset and restart counts, and shirudo compares
- them with the TPM's before a command runs.
+ hibernation is not analyzed. Before a suspend, the kernel's TPM
+ driver sends ``TPM2_Shutdown(STATE)`` to a TPM 2.0 unless the TPM
+ stays powered, and the TPM counts that in its restart count
+ (:doc:`/concepts/background/tpm`). The config activation record holds
+ the TPM's reset count, which a resume leaves, so the TPM boot goes on;
+ a command the suspend interrupts sees the restart count move and
+ refuses.
:Symptom: ``/sys/power/state`` lists no ``disk``, and ``systemctl
hibernate`` fails as hibernation is not supported. After a resume
- from suspend, shirudo commands refuse with ``shirudo: image state:
- Stale file handle`` and exit 1, and ``shirudo status`` reports
- ``tampered``.
- :doc:`/operate/maintain/backup-recovery` describes the state. For suspend to
- RAM this is a reading of the code and of the TPM's counters and is not
- tested.
+ from suspend, shirudo commands run as before. A config activation
+ that was pending stays pending, and every state-changing command
+ refuses, until the next reboot; ``shirudo init`` run by hand does not
+ end it. This is a reading of the code and of the TPM's counters and
+ is not tested on a target that suspends.
:What to do: Turn hibernation off as :doc:`/operate/plan/hardening` lists, and
keep targets from sleeping, for example with ``AllowSuspend=no`` and
``AllowHibernation=no`` in ``/etc/systemd/sleep.conf``. After an
- unplanned resume, follow :doc:`/operate/maintain/backup-recovery`.
+ unplanned resume with an activation pending, reboot; see
+ :doc:`/operate/maintain/backup-recovery`.
Containers and Kubernetes
-------------------------
diff --git a/Documentation/operate/run/read-status.rst b/Documentation/operate/run/read-status.rst
index c95f580c..ff49abe0 100644
--- a/Documentation/operate/run/read-status.rst
+++ b/Documentation/operate/run/read-status.rst
@@ -653,10 +653,11 @@ The detail of ``ConfigActive`` names the cause:
- ``the config PCR, activation records and mounted config disagree``: the config PCR does not hold the value the activation
records predict. Something extended the PCR after this boot's config
- measurement. Software that measures into the same PCR causes this
+ measurement, or a TPM restart without a reboot cleared it. Software
+ that measures into the same PCR causes this
too, see :doc:`/operate/plan/hardening` for the PCR choice.
-- ``the config activation record does not belong to this TPM boot``: the TPM was reset since the config was measured, for
- example by a resume from hibernation.
+- ``the config activation record does not belong to this TPM boot``: the TPM's reset count changed since the config was
+ measured, by a TPM reset or ``TPM2_Clear`` without a reboot.
- ``the mounted config does not match the selected root``: the
config image mounted at ``/run/shirudo/config`` is not the one the
records select.
diff --git a/Documentation/operate/troubleshoot/troubleshooting.rst b/Documentation/operate/troubleshoot/troubleshooting.rst
index 06f5a84c..d659825c 100644
--- a/Documentation/operate/troubleshoot/troubleshooting.rst
+++ b/Documentation/operate/troubleshoot/troubleshooting.rst
@@ -162,8 +162,8 @@ Causes
- Something extended the config PCR after this boot's config
measurement, either an attacker or other software that measures
into the same PCR.
- - The TPM was reset without a reboot, for example by a resume from
- hibernation.
+ - The TPM was reset or restarted without a reboot, for example by a
+ resume from hibernation.
- The config mount was replaced.
Action
diff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rst
index e5161f84..e83a8c27 100644
--- a/Documentation/security/threat-model.rst
+++ b/Documentation/security/threat-model.rst
@@ -1409,9 +1409,12 @@ target that runs one config while its records claim another.
``shirudo image update`` makes the candidate and the pending selection
durable before it extends the PCR. A validation failure before that
point leaves the active config in use. After it, including on an
-ambiguous TPM response, the target needs a manual reboot. The target
-keeps running, shirudo never reboots it, refuses further state changes and
-config attestation, and ``shirudo status`` reports ``reboot-pending``.
+ambiguous TPM response, the target needs a manual reboot. A resume from
+suspend or a TPM restart is none: the TPM boot is the TPM's reset count,
+which neither changes, so a hand-run ``init`` after one records nothing.
+The target keeps running, shirudo never reboots it, refuses further
+state changes and config attestation, and ``shirudo status`` reports
+``reboot-pending``.
At the next boot, ``init`` verifies the selected image again, reads its
whole mapping, extends the config PCR with a fresh boot event and
@@ -1620,16 +1623,18 @@ R4 Config PCR writers are not authenticated
value the history starts from out of the evidence rather than
requiring zero. A fresh nonce and a genuine quote prove neither who
extended the PCR nor that an activation completed. Root also decides
- when the TPM's boot changes. A suspend to RAM in the middle of a
- command is a TPM Resume or Restart, and ``TPM2_Clear``, which needs
- only the lockout authorization that shirudo leaves empty, puts the
- reset and restart counts back to zero, so a later boot can carry an
- earlier one's identity. One activation can then be extended twice in
- one TPM boot, and a record can name the TPM boot it is in while the
- PCR does not hold its replay, which refuses commands until a reboot.
- The Lean proofs of the activation that count extensions or explain a
- refusal assume neither (``Shirudo.ConfigActivation``). Detection:
- none reliable. Mitigation: D1.
+ when the TPM's boot changes. ``TPM2_Clear``, which needs only the
+ lockout authorization that shirudo leaves empty, puts the reset count
+ back to zero, so a later TPM boot can carry an earlier one's. One in
+ the middle of an activation lets ``init`` extend the same activation
+ a second time in one PCR lifetime, and a repeated reset count lets a
+ record name the TPM boot it is in while the PCR does not hold its
+ replay, which refuses commands until a reboot. A suspend to RAM, or a
+ TPM restart that clears the PCR, keeps the TPM boot and changes
+ nothing but refusals. The Lean proofs of the activation that count
+ extensions or explain a refusal assume neither
+ (``Shirudo.ConfigActivation``). Detection: none reliable. Mitigation:
+ D1.
R5 The boot chain and process memory stay trusted
dm-verity verifies runtime blocks as they are read. It does not
diff --git a/testing/functional/image/t5007-config-update.sh b/testing/functional/image/t5007-config-update.sh
index 32aeaf0c..a0f346f6 100755
--- a/testing/functional/image/t5007-config-update.sh
+++ b/testing/functional/image/t5007-config-update.sh
@@ -114,16 +114,34 @@ test_expect_success 'PCR drift and a changed PCR allocation refuse live updates'
fresh pcr-change && ! "$H" "$WORK/pcr-change" update B 14 >/dev/null
'
-for action in reset restart; do
- test_expect_success "$action requires a new BOOT event for the same root" '
- fresh epoch-'"$action"' &&
- "$H" "$WORK/epoch-'"$action"'" '"$action"' >/dev/null &&
- ! "$H" "$WORK/epoch-'"$action"'" check >/dev/null &&
- "$H" "$WORK/epoch-'"$action"'" boot A >"$WORK/epoch" &&
- [ "$(field "$WORK/epoch" extends)" = 2 ] &&
- [ "$(field "$WORK/epoch" kind)" = 1 ]
- '
-done
+test_expect_success 'reset requires a new BOOT event for the same root' '
+ fresh epoch-reset &&
+ "$H" "$WORK/epoch-reset" reset >/dev/null &&
+ ! "$H" "$WORK/epoch-reset" check >/dev/null &&
+ "$H" "$WORK/epoch-reset" boot A >"$WORK/epoch" &&
+ [ "$(field "$WORK/epoch" extends)" = 2 ] &&
+ [ "$(field "$WORK/epoch" kind)" = 1 ]
+'
+
+test_expect_success 'resume keeps the TPM boot and init records nothing' '
+ fresh epoch-resume &&
+ "$H" "$WORK/epoch-resume" resume >/dev/null &&
+ "$H" "$WORK/epoch-resume" check >/dev/null &&
+ "$H" "$WORK/epoch-resume" boot A >"$WORK/epoch" &&
+ [ "$(field "$WORK/epoch" extends)" = 1 ] &&
+ [ "$(field "$WORK/epoch" sequence)" = 1 ]
+'
+
+test_expect_success 'restart refuses the cleared PCR until a reset' '
+ fresh epoch-restart &&
+ "$H" "$WORK/epoch-restart" restart >/dev/null &&
+ ! "$H" "$WORK/epoch-restart" check >/dev/null &&
+ ! "$H" "$WORK/epoch-restart" boot A >"$WORK/epoch" &&
+ [ "$(field "$WORK/epoch" extends)" = 1 ] &&
+ "$H" "$WORK/epoch-restart" reset >/dev/null &&
+ "$H" "$WORK/epoch-restart" boot A >"$WORK/epoch" &&
+ [ "$(field "$WORK/epoch" extends)" = 2 ]
+'
test_expect_success 'process death after measurement leaves a persistent pending latch' '
fresh crash && crash_update crash 2 &&
@@ -133,6 +151,20 @@ test_expect_success 'process death after measurement leaves a persistent pending
[ "$(field "$WORK/retry" extends)" = 2 ]
'
+for action in resume restart; do
+ test_expect_success "$action keeps an interrupted activation pending" '
+ fresh pending-'"$action"' && crash_update pending-'"$action"' 2 &&
+ "$H" "$WORK/pending-'"$action"'" '"$action"' >/dev/null &&
+ ! "$H" "$WORK/pending-'"$action"'" boot B >"$WORK/pending" &&
+ [ "$(field "$WORK/pending" extends)" = 2 ] &&
+ [ "$(field "$WORK/pending" pending)" = 1 ] &&
+ "$H" "$WORK/pending-'"$action"'" reset >/dev/null &&
+ "$H" "$WORK/pending-'"$action"'" boot B >"$WORK/pending" &&
+ [ "$(field "$WORK/pending" extends)" = 3 ] &&
+ [ "$(field "$WORK/pending" pending)" = 0 ]
+ '
+done
+
test_expect_success 'death before pending rename leaves the completed selection usable' '
fresh pre-pending && crash_update pre-pending 6 &&
"$H" "$WORK/pre-pending" check >/dev/null
diff --git a/testing/functional/image/t5009-config-stage.sh b/testing/functional/image/t5009-config-stage.sh
index 5d97f1f4..5f2c0f6b 100755
--- a/testing/functional/image/t5009-config-stage.sh
+++ b/testing/functional/image/t5009-config-stage.sh
@@ -38,20 +38,31 @@ test_expect_success 'staging B leaves A, its PCR and its event log unchanged' '
"$H" "$WORK/stage" check >/dev/null
'
-for action in reset restart; do
- test_expect_success "$action selects B only on the next TPM boot and measures it once" '
+test_expect_success 'reset selects B only on the next TPM boot and measures it once' '
+ fresh boot-reset &&
+ "$H" "$WORK/boot-reset" stage B >/dev/null &&
+ "$H" "$WORK/boot-reset" reset >/dev/null &&
+ "$H" "$WORK/boot-reset" staged >"$WORK/eligible" &&
+ grep -qx "eligible=1" "$WORK/eligible" &&
+ grep -qx "candidate=/verified/config-B" "$WORK/eligible" &&
+ "$H" "$WORK/boot-reset" boot B >"$WORK/selected" &&
+ grep -qx "prefix=/verified/config-B" "$WORK/selected" &&
+ grep -qx "extends=2" "$WORK/selected" &&
+ [ ! -e "$WORK/boot-reset/next-boot" ] &&
+ "$H" "$WORK/boot-reset" boot B >"$WORK/repeated" &&
+ grep -qx "extends=2" "$WORK/repeated"
+'
+
+for action in resume restart; do
+ test_expect_success "$action leaves B staged for the next reset" '
fresh boot-'"$action"' &&
"$H" "$WORK/boot-'"$action"'" stage B >/dev/null &&
"$H" "$WORK/boot-'"$action"'" '"$action"' >/dev/null &&
"$H" "$WORK/boot-'"$action"'" staged >"$WORK/eligible" &&
- grep -qx "eligible=1" "$WORK/eligible" &&
- grep -qx "candidate=/verified/config-B" "$WORK/eligible" &&
- "$H" "$WORK/boot-'"$action"'" boot B >"$WORK/selected" &&
- grep -qx "prefix=/verified/config-B" "$WORK/selected" &&
- grep -qx "extends=2" "$WORK/selected" &&
- [ ! -e "$WORK/boot-'"$action"'/next-boot" ] &&
- "$H" "$WORK/boot-'"$action"'" boot B >"$WORK/repeated" &&
- grep -qx "extends=2" "$WORK/repeated"
+ grep -qx "eligible=0" "$WORK/eligible" &&
+ ! "$H" "$WORK/boot-'"$action"'" boot B >"$WORK/selected" &&
+ grep -qx "extends=1" "$WORK/selected" &&
+ [ -e "$WORK/boot-'"$action"'/next-boot" ]
'
done
diff --git a/testing/helper/test-config-update.c b/testing/helper/test-config-update.c
index c10a75fe..42f24bec 100644
--- a/testing/helper/test-config-update.c
+++ b/testing/helper/test-config-update.c
@@ -352,8 +352,7 @@ int main(int argc, char **argv)
ret = config_update_read(&update, &previous);
if (!ret)
ret = config_update_staged(&update, &previous,
- ctx.sim.reset_count,
- ctx.sim.restart_count, &target);
+ ctx.sim.reset_count, &target);
printf("eligible=%d\n", ret);
if (ret == 1) {
printf("candidate=%s\n", target.prefix);
@@ -373,6 +372,9 @@ int main(int argc, char **argv)
ctx.sim.restart_count++;
memset(ctx.sim.value, 0, sizeof(ctx.sim.value));
ret = simulator_save(&ctx);
+ } else if (!strcmp(argv[2], "resume")) {
+ ctx.sim.restart_count++;
+ ret = simulator_save(&ctx);
} else if (!strcmp(argv[2], "drift")) {
memset(ctx.sim.value, 0x55, sizeof(ctx.sim.value));
ret = simulator_save(&ctx);
diff --git a/testing/proofs/cbmc/config-update-follows.c b/testing/proofs/cbmc/config-update-follows.c
index f37a40ab..a719d399 100644
--- a/testing/proofs/cbmc/config-update-follows.c
+++ b/testing/proofs/cbmc/config-update-follows.c
@@ -10,7 +10,9 @@
* its sequence is the next and it is a boot event of another TPM boot or
* an update of the same TPM boot on the same PCR from the same initial
* value, and a refusal is EUCLEAN. Two events are the same exactly when
- * TPM boot, sequence, PCR, kind, initial value and root agree.
+ * TPM boot, sequence, PCR, kind, initial value and root agree. A TPM
+ * boot is the model's B, the reset count: the restart count is no part
+ * of it.
*/
#include "proof.h"
@@ -50,8 +52,7 @@ static void follows(void)
boot = record.kind == CONFIG_EVENT_BOOT;
if (!have)
model = record.sequence == 1 && boot;
- else if (previous.reset_count == record.reset_count &&
- previous.restart_count == record.restart_count)
+ else if (previous.reset_count == record.reset_count)
model = record.sequence == previous.sequence + 1 && !boot &&
previous.pcr == record.pcr &&
bytes_equal(previous.initial_pcr, record.initial_pcr);
@@ -67,7 +68,6 @@ static void same(void)
__CPROVER_assert(same_event(&a, &b) ==
(a.reset_count == b.reset_count &&
- a.restart_count == b.restart_count &&
a.sequence == b.sequence && a.pcr == b.pcr &&
a.kind == b.kind &&
bytes_equal(a.initial_pcr, b.initial_pcr) &&
diff --git a/testing/proofs/lean4/Shirudo/ConfigActivation.lean b/testing/proofs/lean4/Shirudo/ConfigActivation.lean
index 19caf2f3..049d0bf0 100644
--- a/testing/proofs/lean4/Shirudo/ConfigActivation.lean
+++ b/testing/proofs/lean4/Shirudo/ConfigActivation.lean
@@ -21,24 +21,30 @@ takes that root instead of the record's while the stage still copies the
record (`config_update_staged()`), and an update, or a boot completed in
another TPM boot than the stage's, removes it.
-The model is the journal, the TPM's boot identity, its reset and restart
-counts, every PCR as the list of its extensions, the config mounted and
+The model is the journal, the TPM boot, which is the TPM's reset count,
+every PCR as the list of its extensions, the config mounted and
the one command the lock lets run, at the steps a crash, a failure or a
lost TPM answer can fall between. An error return is a crash, as the C
writes nothing on its error paths but `complete()`'s restore, which is a
step. An extension whose answer is lost is `extend` followed by `crash`,
or `extendLost`. Any other process may extend any PCR at any time (R4). A
-reboot, and a TPM Restart or Resume without one, start a TPM boot. Every
-state any sequence of these reaches from a fresh target is `Reachable`.
-
-Root decides when the TPM's boot changes: a suspend to RAM at any point of
-a command is a TPM Resume or Restart, and `TPM2_Clear` sets the reset and
-restart counts back to zero, so a later boot can have an identity an
-earlier one had. `rootTpm` is the model with both. `journal_wellformed`,
-`mounted_selected`, `boot_completion`, `completion_by_complete` and
-`extension_follows_save` hold for it. Every other theorem is over
-`shirudo`, the same code on a calm TPM, whose boot changes only between
-commands, to an identity it never had; `restart_in_flight_extends_twice`
+reboot, and a TPM reset or `TPM2_Clear` without one, start a TPM boot; a
+TPM Restart or Resume does not, as `config_update_boot()` and
+`check_record()` compare the reset count alone. The restart count, which
+the read back of an extension compares, is left out: that check only
+refuses more. Every state any sequence of these reaches from a fresh
+target is `Reachable`.
+
+Root decides when the TPM's boot changes and what a restart does: a
+suspend to RAM at any point of a command is a TPM Resume, or a Restart,
+which clears the PCRs, and `TPM2_Clear` sets the reset count back to
+zero, so a later boot can have one an earlier one had. `rootTpm` is the
+model with all of these. `journal_wellformed`, `mounted_selected`,
+`boot_completion`, `completion_by_complete` and `extension_follows_save`
+hold for it, and `pending_until_reboot` for every TPM. Every other theorem
+is over `shirudo`, the same code on a calm TPM, whose reset count changes
+only between commands, to one it never had, and which restarts without a
+reboot only by a Resume between commands; `clear_in_flight_extends_twice`
breaks `extends_once` on root's TPM.
For every such sequence:
@@ -48,7 +54,9 @@ For every such sequence:
refuses: a selection of this TPM boot pending, the PCR extended by
another process, or no record of this TPM boot. `refusal_persists`: the
first two last, with no command starting and shirudo extending nothing,
- until the TPM boot changes.
+ until the TPM boot changes. `pending_until_reboot`: on any TPM, a
+ pending activation stays pending through every resume and restart until
+ the reset count changes.
- (b) `extends_once`: one extension per activation and TPM boot;
`extension_follows_pending`: only with that activation's pending record
saved; `interrupted_never_extends`: none after an interruption. On
@@ -90,10 +98,9 @@ calm TPM.
The variants `retry` and `recover` each add one step of code shirudo
does not have, and `retry_extends_twice` and `recover_admits_within_boot`
give a sequence for each that breaks the theorem it matches.
-`restart_in_flight_extends_twice` is a sequence on root's TPM.
-`resume_recovers_without_reboot` is a sequence of shirudo's own model in
-which `init`, run after a TPM Resume, recovers an interrupted activation
-without a reboot.
+`clear_in_flight_extends_twice` is a sequence on root's TPM.
+`resume_keeps_pending` is the trace a suspend to RAM gives: an
+interrupted activation stays pending across it until a reboot.
-/
namespace Shirudo.ConfigActivation
@@ -107,7 +114,7 @@ inductive Kind
deriving DecidableEq
/-- A line of `events`: the fields `same_event()` compares. `boot` is the
-TPM's reset and restart counts, `initial` the `initial_pcr` the TPM
+TPM's reset count, `initial` the `initial_pcr` the TPM
boot's boot event extended, as the extensions that value holds. -/
structure Event (B R D : Type) where
seq : Nat
@@ -165,8 +172,8 @@ end Model
/-- What the C keeps across commands, and two ghosts. `state` and
`events` with `torn`, a partial last line, are the journal under
-`/var/lib/shirudo/config-update`; `boot` the TPM's reset and restart
-counts; `pcr` every PCR as its extensions this TPM boot, the newest
+`/var/lib/shirudo/config-update`; `boot` the TPM's reset count; `pcr`
+every PCR as its extensions this TPM boot, the newest
first; `mounted` the root of the config at `/run/shirudo/config`;
`cfgPcr` and `cfgImage` the runtime image's `config.pcr` and
`config.image`; `staged` the `next-boot` file of the journal, the record
@@ -215,25 +222,29 @@ structure Sys (B R D : Type) where
/-- Models the code would be if it retried an extension whose answer was
lost or finished a pending activation within the TPM boot, and the TPM
-it runs on: one that a TPM Restart or Resume can change in the middle of
-a command, and one that can come back to a boot identity it had. -/
+it runs on: one whose resets, restarts and resumes can come in the middle
+of a command, one that can come back to a reset count it had, and one
+that restarts without a reboot, clearing the PCRs. -/
structure Variant where
retry : Bool := false
recover : Bool := false
restartInFlight : Bool := false
repeatBoot : Bool := false
+ restartClears : Bool := false
-/-- shirudo's code on a calm TPM, whose boot changes only between
-commands, to an identity it never had: the assumption every theorem over
+/-- shirudo's code on a calm TPM, whose reset count changes only between
+commands, to one it never had, and which restarts without a reboot only
+by a Resume, between commands: the assumption every theorem over
`shirudo` carries. -/
def shirudo : Variant := {}
/-- shirudo's code on the TPM root drives. A suspend to RAM, at any point
-of a command, is a TPM Resume, or a Restart, and `TPM2_Clear`, which only
-the lockout authorization that shirudo leaves empty guards, sets the reset
-and restart counts back to zero, so a later boot can have an identity an
-earlier one had. -/
-def rootTpm : Variant := { restartInFlight := true, repeatBoot := true }
+of a command, is a TPM Resume, or a Restart, which clears the PCRs, and
+`TPM2_Clear`, which only the lockout authorization that shirudo leaves
+empty guards, sets the reset count back to zero, so a later TPM boot can
+have one an earlier one had. -/
+def rootTpm : Variant :=
+ { restartInFlight := true, repeatBoot := true, restartClears := true }
/-- The result codes of `config_update_check()`. -/
inductive Res
@@ -474,13 +485,14 @@ inductive StepIn (v : Variant) : Sys B R D → Sys B R D → Prop
checkRecord digest w r = .ok →
StepIn v ⟨w, .idle⟩ ⟨{ w with state := some ⟨r, false⟩ }, .idle⟩
-/-- A step: one within the TPM boot, a reboot, or a TPM Restart or Resume
-without one. Each gives the TPM a boot it never had, the reset count
-grown at a reset and the restart count at a restart or a resume, unless
-`repeatBoot` lets a `TPM2_Clear` before it bring back an old one. A reboot
-ends the command, empties `/run` and leaves the PCRs with what the
-firmware extended; it may bring a new runtime image. A Restart clears the
-PCRs, a Resume keeps them (`keep`); the OS and its mounts run on. -/
+/-- A step: one within the TPM boot, a reboot, a TPM reset or
+`TPM2_Clear` without one, or a TPM Restart or Resume. The TPM boot is the
+reset count. A reboot and a reset give it a value it never had, unless
+`repeatBoot` lets a `TPM2_Clear` bring back an old one; a Restart or a
+Resume keeps it. A reboot ends the command, empties `/run` and leaves the
+PCRs with what the firmware extended; it may bring a new runtime image.
+Otherwise the OS and its mounts run on: a reset or a Restart clears the
+PCRs, a `TPM2_Clear` or a Resume keeps them (`keep`). -/
inductive Step (v : Variant) : Sys B R D → Sys B R D → Prop
| inBoot {s s' : Sys B R D} : StepIn digest v s s' → Step v s s'
| reboot (s : Sys B R D) (b : B) (f : Nat → List D) (idx : Nat) (img : R) :
@@ -488,12 +500,25 @@ inductive Step (v : Variant) : Sys B R D → Sys B R D → Prop
Step v s ⟨{ s.w with
boot := b, pcr := f, mounted := none, cfgPcr := idx, cfgImage := img,
seen := b :: s.w.seen }, .idle⟩
- | restart (s : Sys B R D) (b : B) (keep : Bool) (f : Nat → List D) :
+ | reset (s : Sys B R D) (b : B) (keep : Bool) (f : Nat → List D) :
(s.proc = .idle ∨ v.restartInFlight = true) →
(b ∉ s.w.seen ∨ v.repeatBoot = true) →
Step v s ⟨{ s.w with
boot := b, pcr := if keep then s.w.pcr else f,
seen := b :: s.w.seen }, s.proc⟩
+ | restart (s : Sys B R D) (keep : Bool) (f : Nat → List D) :
+ (s.proc = .idle ∨ v.restartInFlight = true) →
+ (keep = true ∨ v.restartClears = true) →
+ Step v s ⟨{ s.w with pcr := if keep then s.w.pcr else f }, s.proc⟩
+
+/-- A step that keeps the TPM's reset count: one within the TPM boot, or a
+TPM Restart or Resume. -/
+inductive SameBoot (v : Variant) : Sys B R D → Sys B R D → Prop
+ | inBoot {s s' : Sys B R D} : StepIn digest v s s' → SameBoot v s s'
+ | restart (s : Sys B R D) (keep : Bool) (f : Nat → List D) :
+ (s.proc = .idle ∨ v.restartInFlight = true) →
+ (keep = true ∨ v.restartClears = true) →
+ SameBoot v s ⟨{ s.w with pcr := if keep then s.w.pcr else f }, s.proc⟩
/-- The reflexive and transitive closure of a relation. -/
inductive Star {α : Type} (r : α → α → Prop) : α → α → Prop
@@ -801,7 +826,12 @@ theorem base_step [DecidableEq R] {s s' : Sys B R D}
cases h with
| inBoot h => exact base_stepIn hb h
| reboot => trivial
- | restart b keep f hidle _ =>
+ | reset b keep f hidle _ =>
+ rcases hidle with hidle | hidle
+ · simp only [hidle]
+ trivial
+ · simp [shirudo] at hidle
+ | restart keep f hidle _ =>
rcases hidle with hidle | hidle
· simp only [hidle]
trivial
@@ -1121,7 +1151,7 @@ theorem inv_step {s s' : Sys B R D} (hi : Inv digest s)
· exact ⟨glob_newBoot f none idx img hi.1 hnew (Or.inl rfl), trivial,
trivial⟩
· simp [shirudo] at hnew
- | restart b keep f hidle hnew =>
+ | reset b keep f hidle hnew =>
have hnew : b ∉ s.w.seen := by
rcases hnew with hnew | hnew
· exact hnew
@@ -1135,6 +1165,10 @@ theorem inv_step {s s' : Sys B R D} (hi : Inv digest s)
· simp only [hidle]
trivial
· simp [shirudo] at hidle
+ | restart keep f hidle hkeep =>
+ rcases hkeep with rfl | hkeep
+ · exact hi
+ · simp [shirudo] at hkeep
omit [DecidableEq R] in
theorem inv_fresh (b : B) (f : Nat → List D) (idx : Nat) (img : R) :
@@ -1267,8 +1301,10 @@ theorem step_root {s s' : Sys B R D} (h : Step digest shirudo s s') :
rcases hnew with hnew | hnew
· exact .reboot _ b f idx img (Or.inl hnew)
· simp [shirudo] at hnew
- | restart b keep f hidle hnew =>
- exact .restart _ b keep f (Or.inr rfl) (Or.inr rfl)
+ | reset b keep f hidle hnew =>
+ exact .reset _ b keep f (Or.inr rfl) (Or.inr rfl)
+ | restart keep f hidle hkeep =>
+ exact .restart _ keep f (Or.inr rfl) (Or.inr rfl)
theorem reachable_root {s : Sys B R D} (h : Reachable digest shirudo s) :
Reachable digest rootTpm s := by
@@ -1365,6 +1401,7 @@ theorem baseW_step {s s' : Sys B R D} (hb : BaseW digest s.w s.proc)
cases h with
| inBoot h => exact baseW_stepIn hb h
| reboot => trivial
+ | reset => exact baseW_world hb rfl rfl rfl rfl
| restart => exact baseW_world hb rfl rfl rfl rfl
theorem baseW_star {s s' : Sys B R D} (hb : BaseW digest s.w s.proc)
@@ -1487,6 +1524,9 @@ theorem invW_step {s s' : Sys B R D} (hi : InvW digest s)
cases h with
| inBoot h => exact invW_stepIn hi h
| reboot => exact ⟨⟨hi.1.journal, fun _ h => by cases h⟩, trivial, trivial⟩
+ | reset =>
+ exact ⟨globW_of hi.1 rfl rfl rfl rfl, baseW_world hi.2.1 rfl rfl rfl rfl,
+ extW_world hi.2.2 rfl⟩
| restart =>
exact ⟨globW_of hi.1 rfl rfl rfl rfl, baseW_world hi.2.1 rfl rfl rfl rfl,
extW_world hi.2.2 rfl⟩
@@ -2009,7 +2049,13 @@ theorem staged_step {s s' : Sys B R D} (hi : Inv digest s)
cases h with
| inBoot h => exact staged_stepIn hi hk hp h
| reboot => exact ⟨hk, trivial⟩
- | restart b keep f hidle _ =>
+ | reset b keep f hidle _ =>
+ rcases hidle with hidle | hidle
+ · refine ⟨hk, ?_⟩
+ simp only [hidle]
+ trivial
+ · simp [shirudo] at hidle
+ | restart keep f hidle _ =>
rcases hidle with hidle | hidle
· refine ⟨hk, ?_⟩
simp only [hidle]
@@ -2417,14 +2463,14 @@ theorem recover_admits_within_boot (b : B) (f : Nat → List D) (idx : Nat)
exact ⟨check_ok.2 ⟨_, rfl, checkRecord_ok.2
⟨_, scan_cons.2 ⟨_, _, hscanA, hfB, rfl⟩, rfl, rfl, hp2⟩⟩, _, rfl, rfl⟩
-/-- Root's TPM: a TPM Restart or Resume, a suspend for one, lands while
-`image update` runs, between its pending record and its extension, and
-`extends_once` fails. The extension lands in the new TPM
-boot while the record names the old one; the update refuses at its read
-back; `init` run by hand then takes the record as an earlier boot's and
-extends the PCR a second time for the same activation in that TPM boot,
-on top of the first, which a Resume kept. -/
-theorem restart_in_flight_extends_twice (b0 b1 : B) (hb : b1 ≠ b0)
+/-- Root's TPM: a `TPM2_Clear`, which sets the reset count to another
+value and keeps the PCRs, lands while `image update` runs, between its
+pending record and its extension, and `extends_once` fails. The extension
+lands in the new TPM boot while the record names the old one; the update
+refuses at its read back; `init` run by hand then takes the record as an
+earlier boot's and extends the PCR a second time for the same activation
+in that TPM boot, on top of the first. -/
+theorem clear_in_flight_extends_twice (b0 b1 : B) (hb : b1 ≠ b0)
(f : Nat → List D) (idx : Nat) (A B' : R) (hAB : B' ≠ A) :
∃ s : Sys B R D, Reachable digest rootTpm s ∧
¬ s.w.sent.Nodup ∧ s.w.boot = b1 ∧
@@ -2450,7 +2496,7 @@ theorem restart_in_flight_extends_twice (b0 b1 : B) (hb : b1 ≠ b0)
((worldA digest b0 f idx A).pcr idx)⟩
⟨w2, .extending (updB digest b0 f idx A B')
((worldA digest b0 f idx A).pcr idx)⟩ :=
- Step.restart _ b1 true f (Or.inr rfl) (Or.inl hseen)
+ Step.reset _ b1 true f (Or.inr rfl) (Or.inl hseen)
let w3 : World B R D :=
{ w2 with pcr := extendAt w2.pcr idx (digest .update B'),
sent := (b1, (updB digest b0 f idx A B').seq) :: w2.sent }
@@ -2475,31 +2521,63 @@ theorem restart_in_flight_extends_twice (b0 b1 : B) (hb : b1 ≠ b0)
fun hnd => (List.nodup_cons.1 hnd).1 List.mem_cons_self, rfl, ?_⟩
simp [w3, w2, w1, worldA, booted, fresh, extendAt]
-/-- Finding, in shirudo's own model: a TPM Resume, as from suspend to
-RAM, keeps the config PCR but starts a TPM boot, and `init` run by hand
-then recovers an interrupted activation without a reboot. The trace: the
-first boot with config `A`; `image update` to `B'` extends the PCR and
-gets no answer, leaving `B'` pending and the PCR holding its update
-event; the TPM resumes; `init` records a boot event for `B'` on top, and
-commands run with `B'`. One PCR lifetime holds both extensions for `B'`,
-and nothing was rebooted: the config mount stayed. -/
-theorem resume_recovers_without_reboot (b0 b1 : B) (hb : b1 ≠ b0)
- (f : Nat → List D) (idx : Nat) (A B' : R) (hAB : B' ≠ A) :
- ∃ s1 s1' s2 : Sys B R D, Reachable digest shirudo s1 ∧ s1.proc = .idle ∧
+/-- (a) A pending activation stays pending until the TPM's reset count
+changes, a reboot's: whatever commands run, crash or extend the PCR, and
+however often the TPM resumes or restarts, at any point, `init` takes
+nothing, the record stays as it is and every command but `status` and
+`image status` refuses. -/
+theorem pending_until_reboot {v : Variant} (hv : v.recover = false)
+ {s s' : Sys B R D} (hidle : s.proc = .idle) (hp : PendingHere s.w)
+ (hrun : Star (SameBoot digest v) s s') :
+ s'.proc = .idle ∧ s'.w.state = s.w.state ∧ s'.w.boot = s.w.boot ∧
+ ¬ Admits digest s'.w := by
+ obtain ⟨r0, hs0, hb0⟩ := hp
+ have hkeep : ∀ {a : Sys B R D}, a.proc = .idle → a.w.state = s.w.state →
+ a.w.boot = s.w.boot → ∀ {c : Sys B R D}, SameBoot digest v a c →
+ c.proc = .idle ∧ c.w.state = s.w.state ∧ c.w.boot = s.w.boot := by
+ intro a hi ha hb c h
+ have hs : a.w.state = some ⟨r0, true⟩ := ha.trans hs0
+ cases h with
+ | restart keep f _ _ => exact ⟨hi, ha, hb⟩
+ | inBoot h =>
+ cases h with
+ | crash => exact ⟨rfl, ha, hb⟩
+ | outsider w π i d => exact ⟨hi, ha, hb⟩
+ | startUpdate _ _ _ h1 => simp only at hs; rw [hs] at h1; cases h1
+ | initSame _ _ h1 => simp only at hs; rw [hs] at h1; cases h1
+ | initFirst _ _ h1 => simp only at hs; rw [hs] at h1; cases h1
+ | initNew w r q last x h1 h2 =>
+ simp only at hs hb
+ rw [hs] at h1
+ cases h1
+ exact absurd (hb0.trans hb.symm) h2
+ | stage _ _ _ h1 => simp only at hs; rw [hs] at h1; cases h1
+ | unstage _ _ h1 => simp only at hs; rw [hs] at h1; cases h1
+ | recover _ _ hr => rw [hv] at hr; cases hr
+ | _ => cases hi
+ have hall : s'.proc = .idle ∧ s'.w.state = s.w.state ∧
+ s'.w.boot = s.w.boot := by
+ induction hrun with
+ | refl => exact ⟨hidle, rfl, rfl⟩
+ | tail _ hstep ih => exact hkeep ih.1 ih.2.1 ih.2.2 hstep
+ refine ⟨hall.1, hall.2.1, hall.2.2, fun ha => ?_⟩
+ obtain ⟨r, hr, -⟩ := check_ok.1 ha.1
+ rw [hall.2.1, hs0] at hr
+ cases hr
+
+/-- A TPM Resume, as from suspend to RAM, keeps the config PCR and the TPM
+boot, so an interrupted activation stays pending until a reboot. The
+trace: the first boot with config `A`; `image update` to `B'` extends the
+PCR and gets no answer, leaving `B'` pending and the PCR holding its
+update event. Then however the TPM resumes or restarts, `init` records
+nothing and no command runs, until the reset count changes. -/
+theorem resume_keeps_pending (b0 : B) (f : Nat → List D) (idx : Nat)
+ (A B' : R) (hAB : B' ≠ A) :
+ ∃ s1 : Sys B R D, Reachable digest shirudo s1 ∧ s1.proc = .idle ∧
PendingHere s1.w ∧
s1.w.pcr idx = [digest .update B', digest .boot A] ++ f idx ∧
- Step digest shirudo s1 s1' ∧ s1'.w.pcr = s1.w.pcr ∧
- s1'.w.mounted = s1.w.mounted ∧
- Star (StepIn digest shirudo) s1' s2 ∧ s2.proc = .idle ∧
- Admits digest s2.w ∧ s2.w.mounted = some B' ∧
- s2.w.pcr idx = digest .boot B' :: s1.w.pcr idx := by
- have hscanA : scan digest (worldA digest b0 f idx A).events =
- some (some (bootA b0 f idx A), fold digest (bootA b0 f idx A) []) :=
- scan_cons.2 ⟨none, [], rfl, ⟨rfl, rfl⟩, rfl⟩
- have hfB : Follows (some (bootA b0 f idx A)) (updB digest b0 f idx A B') :=
- follows_update B' rfl rfl
- have hseen : b1 ∉ (worldA digest b0 f idx A).seen := by
- simp [worldA, booted, fresh, hb]
+ ∀ s2, Star (SameBoot digest rootTpm) s1 s2 →
+ s2.w.state = s1.w.state ∧ ¬ Admits digest s2.w := by
let run := ((boot_then_update_saved (digest := digest) (v := shirudo)
b0 f idx A B' hAB).tail (StepIn.extend _ _ _)).tail (StepIn.crash _ _)
let w1 : World B R D :=
@@ -2508,24 +2586,13 @@ theorem resume_recovers_without_reboot (b0 b1 : B) (hb : b1 ≠ b0)
pcr := extendAt (worldA digest b0 f idx A).pcr idx (digest .update B'),
sent := (b0, (updB digest b0 f idx A B').seq) ::
(worldA digest b0 f idx A).sent }
- let w2 : World B R D :=
- { w1 with boot := b1, pcr := w1.pcr, seen := b1 :: w1.seen }
- have hrestart : Step digest shirudo ⟨w1, .idle⟩ ⟨w2, .idle⟩ :=
- Step.restart _ b1 true f (Or.inl rfl) (Or.inl hseen)
- have hstale : (updB digest b0 f idx A B').boot ≠ w2.boot := fun h => hb h.symm
- have hfB2 : Follows (some (bootA b0 f idx A))
- (newEvent w2 .boot B' (some (bootA b0 f idx A))) :=
- ⟨rfl, fun h => absurd h.symm hb, fun _ => rfl⟩
- have hinit : Star (StepIn digest shirudo) ⟨w2, .idle⟩
- ⟨booted digest w2 B' (some (bootA b0 f idx A)), .idle⟩ :=
- ((Star.refl _).tail
- (StepIn.initNew _ _ true _ _ rfl hstale hscanA (fun _ => rfl)
- (Or.inr ⟨rfl, hfB⟩))).trans (boot_run _ _ _ _ hscanA hfB2)
- refine ⟨⟨w1, .idle⟩, ⟨w2, .idle⟩, _, ⟨b0, f, idx, A, Star.lift run⟩, rfl,
- ⟨_, rfl, rfl⟩,
- ?_, hrestart, rfl, rfl, hinit, rfl, booted_admits _ _ _ _ hscanA hfB2, rfl,
- extendAt_self _ _ _⟩
- simp [w1, worldA, booted, fresh, extendAt]
+ have hpcr : w1.pcr idx = [digest .update B', digest .boot A] ++ f idx := by
+ simp [w1, worldA, booted, fresh, extendAt]
+ refine ⟨⟨w1, .idle⟩, ⟨b0, f, idx, A, Star.lift run⟩, rfl, ⟨_, rfl, rfl⟩,
+ hpcr, fun s2 h => ?_⟩
+ · obtain ⟨-, hs, -, hna⟩ := pending_until_reboot (v := rootTpm) rfl rfl
+ ⟨_, rfl, rfl⟩ h
+ exact ⟨hs, hna⟩
/-- `image stage` and a reboot: `init` activates the staged config. The
trace: the first boot with config `A`, `image stage` of `B'`, a reboot;
diff --git a/testing/unit/config_update.c b/testing/unit/config_update.c
new file mode 100644
index 00000000..098236b5
--- /dev/null
+++ b/testing/unit/config_update.c
@@ -0,0 +1,300 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+#include <errno.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <shirudo/string.h>
+
+#include "chain.h"
+#include "config_update.h"
+#include "unit.h"
+
+#define PCR 12
+
+static struct {
+ __u32 reset;
+ __u32 restart;
+ __u8 pcr[32];
+ __u32 extends;
+ enum config_update_checkpoint fail_at;
+} tpm;
+
+static char dir[4096];
+
+static int fake_identity(void *ctx, __u32 *reset, __u32 *restart)
+{
+ *reset = tpm.reset;
+ *restart = tpm.restart;
+ return 0;
+}
+
+static int fake_pcr_read(void *ctx, __u32 pcr, __u8 out[32])
+{
+ memcpy(out, tpm.pcr, sizeof(tpm.pcr));
+ return 0;
+}
+
+static int fake_pcr_extend(void *ctx, __u32 pcr, const __u8 event[32])
+{
+ tpm.extends++;
+ return chain_fold(tpm.pcr, event);
+}
+
+static int fake_target(void *ctx, const struct config_update_target *target)
+{
+ return 0;
+}
+
+static int fake_seal(void *ctx, int fd)
+{
+ return 0;
+}
+
+static int fake_checkpoint(void *ctx, enum config_update_checkpoint at)
+{
+ return at == tpm.fail_at ? -ECANCELED : 0;
+}
+
+static const struct config_update_ops ops = {
+ .boot_identity = fake_identity,
+ .pcr_read = fake_pcr_read,
+ .pcr_extend = fake_pcr_extend,
+ .prepare = fake_target,
+ .activate = fake_target,
+ .seal = fake_seal,
+ .checkpoint = fake_checkpoint,
+};
+
+static struct config_update_target target(char id)
+{
+ struct config_update_target t = {};
+
+ ASSERT_TRUE(strsprintf(t.prefix, sizeof(t.prefix),
+ "/verified/config-%c", id) > 0);
+ memset(t.root_hash, id, sizeof(t.root_hash));
+ return t;
+}
+
+/* A fresh journal and a TPM in its first boot. */
+static void use_journal(void)
+{
+ const char *tmp = getenv("TMPDIR");
+
+ memset(&tpm, 0, sizeof(tpm));
+ tpm.reset = 1;
+ ASSERT_TRUE(strsprintf(dir, sizeof(dir),
+ "%s/shirudo-unit-config-XXXXXX",
+ tmp && tmp[0] ? tmp : "/tmp") > 0);
+ ASSERT_TRUE(mkdtemp(dir));
+}
+
+static void drop_journal(void)
+{
+ static const char *const names[] = {
+ "state", "events", "lock", "next-boot", ".state.tmp",
+ ".next-boot.tmp",
+ };
+ char path[4200];
+
+ for (size_t i = 0; i < ARRAY_SIZE(names); i++) {
+ if (strsprintf(path, sizeof(path), "%s/%s", dir,
+ names[i]) > 0)
+ unlink(path);
+ }
+ rmdir(dir);
+}
+
+/* One run of init, as image_boot() and image_boot_complete() drive it. */
+static int init(char id)
+{
+ struct config_update_target t = target(id);
+ struct config_update update;
+ int ret;
+
+ ret = config_update_open(dir, &ops, NULL, &update);
+ if (ret)
+ return ret;
+ ret = config_update_boot(&update, &t, PCR);
+ if (!ret)
+ ret = config_update_complete_boot(&update);
+ config_update_close(&update);
+ return ret;
+}
+
+static int update(char id)
+{
+ struct config_update_target t = target(id);
+ struct config_update update;
+ int ret;
+
+ ret = config_update_open(dir, &ops, NULL, &update);
+ if (ret)
+ return ret;
+ ret = config_update_begin(&update, &t, PCR);
+ config_update_close(&update);
+ return ret;
+}
+
+static int stage(char id)
+{
+ struct config_update_target t = target(id);
+ struct config_update update;
+ int ret;
+
+ ret = config_update_open(dir, &ops, NULL, &update);
+ if (ret)
+ return ret;
+ ret = config_update_stage(&update, &t);
+ config_update_close(&update);
+ return ret;
+}
+
+static int check(void)
+{
+ struct config_update update;
+ int ret;
+
+ ret = config_update_open(dir, &ops, NULL, &update);
+ if (ret)
+ return ret;
+ ret = config_update_check(&update, NULL);
+ config_update_close(&update);
+ return ret;
+}
+
+/* boot_target()'s question: does the stage take the next init? */
+static int staged(void)
+{
+ struct config_update_record previous;
+ struct config_update_target t;
+ struct config_update update;
+ int ret;
+
+ ret = config_update_open(dir, &ops, NULL, &update);
+ if (ret)
+ return ret;
+ ret = config_update_read(&update, &previous);
+ if (!ret)
+ ret = config_update_staged(&update, &previous, tpm.reset, &t);
+ config_update_close(&update);
+ return ret;
+}
+
+static __s64 sequence(void)
+{
+ struct config_update_record record;
+ struct config_update update;
+
+ ASSERT_EQ(config_update_open(dir, &ops, NULL, &update), 0);
+ ASSERT_EQ(config_update_read(&update, &record), 0);
+ config_update_close(&update);
+ return record.sequence;
+}
+
+/* A suspend to RAM: the TPM resumes with its PCRs and counts a restart. */
+static void resume(void)
+{
+ tpm.restart++;
+}
+
+/* A TPM restart without a reboot clears the PCRs as well. */
+static void restart(void)
+{
+ tpm.restart++;
+ memset(tpm.pcr, 0, sizeof(tpm.pcr));
+}
+
+static void reboot(void)
+{
+ tpm.reset++;
+ tpm.restart = 0;
+ memset(tpm.pcr, 0, sizeof(tpm.pcr));
+}
+
+/* An activation stopped after its extension stays pending across a resume. */
+static void resume_keeps_pending_activation(void)
+{
+ __u32 extends;
+
+ use_journal();
+ ASSERT_EQ(init('A'), 0);
+ tpm.fail_at = CONFIG_CHECK_EXTEND;
+ ASSERT_EQ(update('B'), -ECANCELED);
+ tpm.fail_at = 0;
+ resume();
+ extends = tpm.extends;
+ EXPECT_EQ(init('B'), -EINPROGRESS);
+ EXPECT_EQ(check(), -EINPROGRESS);
+ EXPECT_EQ(tpm.extends, extends);
+ reboot();
+ EXPECT_EQ(init('B'), 0);
+ EXPECT_EQ(check(), 0);
+ drop_journal();
+}
+
+/* After a resume the record is of this boot, and init re-runs as in it. */
+static void resume_keeps_completed_selection(void)
+{
+ __u32 extends;
+ __s64 seq;
+
+ use_journal();
+ ASSERT_EQ(init('A'), 0);
+ seq = sequence();
+ resume();
+ extends = tpm.extends;
+ EXPECT_EQ(check(), 0);
+ EXPECT_EQ(init('A'), 0);
+ EXPECT_EQ(tpm.extends, extends);
+ EXPECT_EQ(sequence(), seq);
+ EXPECT_EQ(init('B'), -EBUSY);
+ EXPECT_EQ(update('B'), 0);
+ EXPECT_EQ(check(), 0);
+ drop_journal();
+}
+
+/* A TPM restart clears the config PCR: refused until the reboot. */
+static void restart_refuses_until_reboot(void)
+{
+ __u32 extends;
+
+ use_journal();
+ ASSERT_EQ(init('A'), 0);
+ restart();
+ extends = tpm.extends;
+ EXPECT_EQ(check(), -EUCLEAN);
+ EXPECT_EQ(init('A'), -EUCLEAN);
+ EXPECT_EQ(tpm.extends, extends);
+ reboot();
+ EXPECT_EQ(init('A'), 0);
+ EXPECT_EQ(check(), 0);
+ drop_journal();
+}
+
+/* A stage waits for a new reset count; a resume is none. */
+static void stage_waits_for_reboot(void)
+{
+ use_journal();
+ ASSERT_EQ(init('A'), 0);
+ ASSERT_EQ(stage('B'), 0);
+ resume();
+ EXPECT_EQ(staged(), 0);
+ EXPECT_EQ(init('A'), 0);
+ reboot();
+ EXPECT_EQ(staged(), 1);
+ EXPECT_EQ(init('B'), 0);
+ EXPECT_EQ(check(), 0);
+ drop_journal();
+}
+
+static const struct unit_case cases[] = {
+ UNIT_CASE(resume_keeps_pending_activation),
+ UNIT_CASE(resume_keeps_completed_selection),
+ UNIT_CASE(restart_refuses_until_reboot),
+ UNIT_CASE(stage_waits_for_reboot),
+ {}
+};
+
+UNIT_SUITE(config_update, cases);
diff --git a/util/config_update.c b/util/config_update.c
index 04a26187..a3db3d0f 100644
--- a/util/config_update.c
+++ b/util/config_update.c
@@ -334,11 +334,15 @@ static int state_save(struct config_update *update,
return selection_save(update, record, "state", ".state.tmp", inject);
}
+/*
+ * A TPM boot ends only at a TPM reset: a Resume or a Restart keeps it, so
+ * that only a reboot ends a pending activation. The restart count is left
+ * to the checks of one command, which refuse a TPM restart in the middle.
+ */
static bool same_boot(const struct config_update_record *a,
const struct config_update_record *b)
{
- return a->reset_count == b->reset_count &&
- a->restart_count == b->restart_count;
+ return a->reset_count == b->reset_count;
}
static bool same_target(const struct config_update_target *a,
@@ -538,8 +542,7 @@ static int check_record(struct config_update *update,
ret = -EUCLEAN;
if (!ret)
ret = live_read(update, record->pcr, &reset, &restart, value);
- if (!ret && (reset != record->reset_count ||
- restart != record->restart_count))
+ if (!ret && reset != record->reset_count)
ret = -ESTALE;
if (!ret && memcmp(value, expected, 32))
ret = -EUCLEAN;
@@ -683,8 +686,7 @@ int config_update_stage(struct config_update *update,
int config_update_staged(struct config_update *update,
const struct config_update_record *previous,
- __u32 reset, __u32 restart,
- struct config_update_target *target)
+ __u32 reset, struct config_update_target *target)
{
struct config_update_record staged = {};
int ret;
@@ -696,7 +698,7 @@ int config_update_staged(struct config_update *update,
return ret == -ENOENT ? 0 : ret;
if (staged.pending)
return -EBADMSG;
- if (staged.reset_count == reset && staged.restart_count == restart)
+ if (staged.reset_count == reset)
return 0;
if (same_target(&staged.target, &previous->target))
return 0;
@@ -770,8 +772,7 @@ int config_update_boot(struct config_update *update,
ret = update->ops->boot_identity(update->ctx, &reset, &restart);
if (ret)
return ret;
- if (previous.sequence && previous.reset_count == reset &&
- previous.restart_count == restart) {
+ if (previous.sequence && previous.reset_count == reset) {
if (previous.pending)
return -EINPROGRESS;
if (previous.pcr != pcr ||
@@ -780,8 +781,7 @@ int config_update_boot(struct config_update *update,
return config_update_check(update, NULL);
}
if (previous.sequence && !same_target(t, &previous.target)) {
- ret = config_update_staged(update, &previous, reset, restart,
- &staged);
+ ret = config_update_staged(update, &previous, reset, &staged);
if (ret < 0)
return ret;
if (!ret || !same_target(t, &staged))
diff --git a/util/config_update.h b/util/config_update.h
index 10bc67ae..91fa7d9d 100644
--- a/util/config_update.h
+++ b/util/config_update.h
@@ -88,8 +88,7 @@ int config_update_stage_read(struct config_update *update,
/* 1 selects an eligible staged target, 0 leaves the current selection. */
int config_update_staged(struct config_update *update,
const struct config_update_record *previous,
- __u32 reset, __u32 restart,
- struct config_update_target *target);
+ __u32 reset, struct config_update_target *target);
int config_update_clear_staged(struct config_update *update);
int config_update_begin(struct config_update *update,
const struct config_update_target *t,
diff --git a/util/image_runtime.c b/util/image_runtime.c
index cc397591..f47a9fb3 100644
--- a/util/image_runtime.c
+++ b/util/image_runtime.c
@@ -805,7 +805,7 @@ static int boot_target(struct config_update_target *target)
ret = tpm_boot_identity(image.tpm, &reset, &restart);
if (!ret)
ret = config_update_staged(&image.update, &previous,
- reset, restart, &staged);
+ reset, &staged);
if (ret < 0)
return ret;
if (ret > 0) {