eBPF-based security platform
chain: measure Java upatch loads and unloads as events of their own
John Fastabend250bc5771a11
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rstindex ca911bc4..dd231203 100644--- a/Documentation/contribute/testing/proof-coverage.rst+++ b/Documentation/contribute/testing/proof-coverage.rst@@ -830,8 +830,8 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.- Stores only PCRs 0 to 23 and marks exactly that one, from 64 hexdigits; a refusal marks nothing* - ``chain-events``- - ``chain_upatch_event()``, ``chain_key_event()``,- ``chain_tree_event()``, ``chain_fold()``,+ - ``chain_upatch_tag()``, ``chain_upatch_event()``,+ ``chain_key_event()``, ``chain_tree_event()``, ``chain_fold()``,``chain_history_step()``, ``chain_init_event()`` of an empty set- Names of 0 to 8 and 255 to 257 bytes; every payload, digest,sequence number, register and history root@@ -839,7 +839,7 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.``Shirudo.Events``: the tag and its NUL, then the fixed fields, thename cut at 256 bytes and the sequence number most significant bytefirst; the event, or the next history root, is the digest, or zeros- with the error+ with the error; a BPF or Java load or unload takes the model's tag* - ``measured-fold-model``- ``measured_event()``, ``measured_holds()``- Every sequence of up to six init, load and unload records over two@@ -1084,8 +1084,9 @@ the model follows the C.* - ``Shirudo.Events``, the event encodings of :src:`util/chain.c`and the history root's step of :src:`util/chain_history.c`- ``bytes_injective``: two admitted events hash the same bytes only- when they are the same event; ``listing_injective``: the init- event's listing spells one set while no name holds a newline;+ when they are the same event, a Java load or unload never a BPF+ one; ``listing_injective``: the init event's listing spells one set,+ each member's type with it, while no name holds a newline;``sorts_unique``: ``qsort()`` leaves a set of distinct names in oneorder; ``replay_injective_on``, ``replay_collision``: equalregisters come from the same typed events in the same order, ordiff --git a/Documentation/internals/measurement-register.rst b/Documentation/internals/measurement-register.rstindex e28bf619..270968b0 100644--- a/Documentation/internals/measurement-register.rst+++ b/Documentation/internals/measurement-register.rst@@ -245,6 +245,13 @@ the formulas, ``\0`` is the NUL that ends the tag.* - ``upatch_unload``- ``SHA256("shirudo-unload\0" || payload_sha256 || name)``- ``upatch unload``, ``bpm`` removals, revocation purges+ * - ``java_load``+ - ``SHA256("shirudo-java\0" || payload_sha256 || name)``+ - ``upatch load`` and ``bpm`` installs of a Java upatch+ * - ``java_unload``+ - ``SHA256("shirudo-java-unload\0" || payload_sha256 || name)``+ - ``upatch unload``, ``bpm`` removals and revocation purges of a+ Java upatch* - ``tree_op``- ``SHA256("shirudo-tree\0" || seq || operation_digest)``, ``seq``as eight big-endian bytes@@ -257,7 +264,9 @@ the formulas, ``\0`` is the NUL that ends the tag.the SHA-256 of the certificate's DER when the set has one member, elsethe SHA-256 over the members' DER digests in sorted order.``set_digest`` is the SHA-256 of one line ``<payload_sha256 hex>-<name>\n`` per restored upatch, sorted by name. ``tree_root`` is the+<name>\n`` per restored upatch, sorted by name, a Java upatch's line+prefixed with ``java ``. A payload digest is hex alone, so a Java member+never lists like a BPF one. ``tree_root`` is theupatch tree's projection root at boot, or 32 zero bytes without a tree.Boot restore is one event however many upatches it restores, and the@@ -268,8 +277,8 @@ purges follows as an ``upatch_unload`` with the reason ``key_revoke``.Revocations a config carries at boot are not individual events; the``init`` event's trust set reflects them.-Lean 4 proves that these encodings tell every two events apart, for-upatch names of every length ``upatch_name_valid()`` takes, load and+Lean 4 proves that these encodings tell every two events apart, a Java+event from a BPF one included, for upatch names of every length ``upatch_name_valid()`` takes, load andunload names of 1 to 256 bytes as ``attest predict`` takes them, andevery sequence number, and that the ``init`` listing spells one set ofdistinct names whatever order ``qsort()`` leaves. With the register'sdiff --git a/Documentation/reference/cli/attest-predict.rst b/Documentation/reference/cli/attest-predict.rstindex 451d1ac7..887490a9 100644--- a/Documentation/reference/cli/attest-predict.rst+++ b/Documentation/reference/cli/attest-predict.rst@@ -21,7 +21,7 @@ Usage.. code-block:: noneshirudo attest predict --cert=<certificate>... [--upatch=<file>]...- [--expect=<name>=<digest>]... [--tree-root=<digest>]+ [--expect=[java:]<name>=<digest>]... [--tree-root=<digest>][--event=<event>]...[--bind-pcr=9=<value> --bind-pcr=11=<value>--writer-public=<modulus>] [--json]@@ -65,6 +65,11 @@ Events* - ``unload:<name>=<payload-sha256>``- ``upatch unload`` of ``<name>``, with the digest the directivenames+ * - ``java-load:<name>=<payload-sha256>``+ - ``upatch load`` of the Java upatch ``<name>``+ * - ``java-unload:<name>=<payload-sha256>``+ - ``upatch unload`` of the Java upatch ``<name>``, with the digest+ the directive names* - ``tree:<sequence>=<operation-digest>``- an operation ``bpm tree sync`` applied* - ``revoke:<label>=<certificate-sha256>``@@ -114,10 +119,11 @@ Options- An artifact archived on the target at boot. Repeatable, eachupatch name once across ``--upatch`` and ``--expect``.* - ``--expect``- - ``<name>=<digest>``- - An archived upatch by name and payload digest. Repeatable. The- name is one a upatch can have, see- :doc:`/concepts/upatches-and-packages`, and comes once.+ - ``[java:]<name>=<digest>``+ - An archived upatch by name and payload digest, ``java:`` before+ the name for a Java upatch. Repeatable. The name is one a upatch+ can have, see :doc:`/concepts/upatches-and-packages`, and comes+ once.* - ``--tree-root``- ``<digest>``, 64 hex characters- The upatch tree projection root at boot. Default: all zero.diff --git a/Documentation/reference/cli/attest-verify.rst b/Documentation/reference/cli/attest-verify.rstindex 88312111..b60b4ea8 100644--- a/Documentation/reference/cli/attest-verify.rst+++ b/Documentation/reference/cli/attest-verify.rst@@ -23,7 +23,7 @@ Usage[--ak-name=<name>] [--ek-name=<name>][--expect-credential=<credential>][--cert=<certificate>]... [--upatch=<file>]...- [--expect=<name>=<digest>]... [--tree-root=<digest>]+ [--expect=[java:]<name>=<digest>]... [--tree-root=<digest>][--expect-chain=<register>] [--allow-runtime-events][--expect-config-root=<root> --expect-config-pcr=<pcr>][--expect-pcr=<pcr>=<value>]...@@ -106,11 +106,11 @@ Optionsupatch name once across ``--upatch`` and ``--expect``. Needs``--cert``.* - ``--expect``- - ``<name>=<digest>``- - An archived upatch by name and payload digest. Repeatable. The- name is one a upatch can have, see- :doc:`/concepts/upatches-and-packages`, and comes once. Needs- ``--cert``.+ - ``[java:]<name>=<digest>``+ - An archived upatch by name and payload digest, ``java:`` before+ the name for a Java upatch. Repeatable. The name is one a upatch+ can have, see :doc:`/concepts/upatches-and-packages`, and comes+ once. Needs ``--cert``.* - ``--tree-root``- ``<digest>``, 64 hex characters- The upatch tree projection root at boot. Needs ``--cert``.diff --git a/Documentation/reference/eventlog.rst b/Documentation/reference/eventlog.rstindex 3cfd6374..797fafab 100644--- a/Documentation/reference/eventlog.rst+++ b/Documentation/reference/eventlog.rst@@ -123,6 +123,15 @@ Event kinds- ``upatch``: object with ``name`` and ``payload_sha256``.``reason``: ``key_revoke`` for an unload caused by a keyrevocation, absent otherwise.+ * - ``java_load``+ - ``shirudo upatch load`` of a Java upatch, after the agent is+ installed, before it reports success.+ - As ``upatch_load``.+ * - ``java_unload``+ - ``shirudo upatch unload`` of a Java upatch, before the agent is+ removed, and a live config activation that revokes the key that+ signed it.+ - As ``upatch_unload``.* - ``key_revoke``- ``shirudo image update``, when the activated config revokes acertificate that is in the ``.bpf`` keyring. A revocation@@ -167,7 +176,8 @@ Fields of an init record* - ``set``- array of object- The upatches boot restore applied or found loaded, each with- ``name`` and ``payload_sha256``.+ ``name`` and ``payload_sha256``, and ``type`` ``java`` for a Java+ upatch. A BPF upatch carries no ``type``.* - ``pcr``- integer- ``9``, the PCR that anchors the register.@@ -229,6 +239,10 @@ document.- ``SHA256("shirudo-upatch\0" || payload_sha256 || name)``* - ``upatch_unload``- ``SHA256("shirudo-unload\0" || payload_sha256 || name)``+ * - ``java_load``+ - ``SHA256("shirudo-java\0" || payload_sha256 || name)``+ * - ``java_unload``+ - ``SHA256("shirudo-java-unload\0" || payload_sha256 || name)``* - ``key_revoke``- ``SHA256("shirudo-key-revoke\0" || cert_sha256)``* - ``tree_op``@@ -238,7 +252,8 @@ document.256 bytes.- ``seq`` is the tree sequence number as an 8-byte big-endian integer.- ``set_digest`` is the SHA-256 of one line ``<payload_sha256> <name>\n``- per member of ``set``, sorted by name, concatenated. An empty set+ per member of ``set``, prefixed with ``java `` for a member whose+ ``type`` is ``java``, sorted by name, concatenated. An empty setgives the SHA-256 of the empty string,``e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855``.- The register starts at 32 zero bytes after every TPM reset. Eachdiff --git a/Documentation/security/cryptography.rst b/Documentation/security/cryptography.rstindex 36ab36fc..7f4e4829 100644--- a/Documentation/security/cryptography.rst+++ b/Documentation/security/cryptography.rst@@ -132,15 +132,17 @@ Digests- Input* - Payload digest of a upatch (``payload_sha256``)- SHA-256- - The signed bytes: loader instructions followed by the map data+ - The signed bytes: loader instructions followed by the map data,+ or the data alone for a Java upatch* - ``excl_prog_hash`` of the metadata map- SHA-256- The loader's instructions* - Register events- SHA-256- A domain tag (``shirudo-init``, ``shirudo-upatch``,- ``shirudo-unload``, ``shirudo-key-revoke``, ``shirudo-tree``)- followed by the event's fields+ ``shirudo-unload``, ``shirudo-java``, ``shirudo-java-unload``,+ ``shirudo-key-revoke``, ``shirudo-tree``) followed by the event's+ fields* - Register value- SHA-256- The TPM's NV extend: the old value followed by the eventdiff --git a/builtin-attest.c b/builtin-attest.cindex 0ff76d4c..5c620a02 100644--- a/builtin-attest.c+++ b/builtin-attest.c@@ -24,6 +24,7 @@#include "util/chain.h"#include "util/crypto.h"#include "util/config_measure.h"+#include "util/eventlog.h"#include "util/image_runtime.h"#include "util/help.h"#include "util/hex.h"@@ -97,7 +98,7 @@ static const struct opt_spec verify_opts[] = {"a certificate the target enrols (repeatable)" },[VERIFY_UPATCH] = { "--upatch", 0, OPT_REPEAT, "<file>","an installed artifact (repeatable)" },- [VERIFY_EXPECT] = { "--expect", 0, OPT_REPEAT, "<name>=<hex>",+ [VERIFY_EXPECT] = { "--expect", 0, OPT_REPEAT, "[java:]<name>=<hex>","an installed upatch by name and payload digest" },[VERIFY_TREE_ROOT] = { "--tree-root", 0, 0, "<hex>","the upatch tree projection root at boot" },@@ -165,12 +166,13 @@ static const struct opt_spec predict_opts[] = {"a certificate the target enrols (repeatable)" },[PREDICT_UPATCH] = { "--upatch", 0, OPT_REPEAT, "<file>","an installed artifact (repeatable)" },- [PREDICT_EXPECT] = { "--expect", 0, OPT_REPEAT, "<name>=<hex>",+ [PREDICT_EXPECT] = { "--expect", 0, OPT_REPEAT, "[java:]<name>=<hex>","an installed upatch by name and payload digest" },[PREDICT_TREE_ROOT] = { "--tree-root", 0, 0, "<hex>","the upatch tree projection root at boot" },[PREDICT_EVENT] = { "--event", 0, OPT_REPEAT, "<kind>:<name>=<hex>",- "a load, unload, tree:<seq> or revoke:<fingerprint> event after boot" },+ "a load, unload, java-load, java-unload, tree:<seq> or "+ "revoke:<fingerprint> event after boot" },[PREDICT_BIND_PCR] = { "--bind-pcr", 0, OPT_REPEAT, "<n>=<hex>","PCR 9 and 11 values at binding, for the register name" },[PREDICT_JSON] = { "--json", 0, 0, NULL,@@ -235,7 +237,7 @@ static int member_add_file(struct members *m, const char *path)measured_upatch_of(&e, &u);upatch_close(&u);ret = measured_set_add(m->set, &m->n, ATTEST_MEMBERS_MAX, e.name,- e.payload_hex);+ e.payload_hex, e.type);if (ret)pr_err("attest: --upatch %s: %s", path,ret == -EEXIST ? "a second upatch of that name" :@@ -243,18 +245,23 @@ static int member_add_file(struct members *m, const char *path)return ret;}+/* name=<hex> for a BPF upatch, java:name=<hex> for a Java one. */static int member_add_spec(struct members *m, const char *spec){- char name[UPATCH_NAME_MAX + 1];char hex[2 * UPATCH_PAYLOAD_HASH_LEN + 1];__u8 payload[UPATCH_PAYLOAD_HASH_LEN];+ char name[UPATCH_NAME_MAX + 1];+ const char *rest = spec;+ bool java;int ret;- ret = attest_parse_name_digest(spec, name, payload);+ java = str_skip_prefix(&rest, "java:");+ ret = attest_parse_name_digest(rest, name, payload);if (!ret) {hex_encode(payload, sizeof(payload), hex);ret = measured_set_add(m->set, &m->n, ATTEST_MEMBERS_MAX, name,- hex);+ hex, java ? UPATCH_TYPE_JAVA :+ UPATCH_TYPE_BPF);}if (ret)pr_err("attest: --expect '%s': %s", spec,@@ -287,15 +294,15 @@ static int apply_event(const char *spec, __u8 chain[32])char name[UPATCH_NAME_MAX + 1];__u8 digest[32], event[32];size_t kind_len;+ __u32 type;+ bool load;if (!colon || attest_parse_name_digest(colon + 1, name, digest))return -EINVAL;kind_len = (size_t)(colon - spec);- if (span_equals(spec, kind_len, "load")) {- if (chain_upatch_event("shirudo-upatch", name, digest, event))- return -EIO;- } else if (span_equals(spec, kind_len, "unload")) {- if (chain_upatch_event("shirudo-unload", name, digest, event))+ if (eventlog_upatch_kind_parse(spec, kind_len, &type, &load)) {+ if (chain_upatch_event(chain_upatch_tag(type, load), name,+ digest, event))return -EIO;} else if (span_equals(spec, kind_len, "tree")) {__s64 seq;diff --git a/builtin-init.c b/builtin-init.cindex a9cdef44..7a68b26d 100644--- a/builtin-init.c+++ b/builtin-init.c@@ -1215,6 +1215,8 @@ static int append_init_event(const struct measured_upatch *set,json_kv_string(&e, "name", set[i].name);json_kv_string(&e, "payload_sha256",set[i].payload_hex);+ if (set[i].type == UPATCH_TYPE_JAVA)+ json_kv_string(&e, "type", "java");json_obj_close(&e);}json_arr_close(&e);diff --git a/testing/helper/test-register.c b/testing/helper/test-register.cindex da0b6954..b326fc63 100644--- a/testing/helper/test-register.c+++ b/testing/helper/test-register.c@@ -233,13 +233,16 @@ int main(int argc, char **argv)return 1;return 0;}- if (strcmp(argv[1], "load") == 0 && argc == 4) {+ if ((strcmp(argv[1], "load") == 0 ||+ strcmp(argv[1], "java-load") == 0) && argc == 4) {+ __u32 type = argv[1][0] == 'j' ? UPATCH_TYPE_JAVA :+ UPATCH_TYPE_BPF;__u8 payload[32];if (hex_decode(argv[3], payload, 32))return 2;- if (chain_upatch_event("shirudo-upatch", argv[2], payload,- event))+ if (chain_upatch_event(chain_upatch_tag(type, true), argv[2],+ payload, event))return 1;ret = chain_extend(event, chain);printf("rc=%d\n", ret);@@ -247,8 +250,8 @@ int main(int argc, char **argv)return 1;printf("value=");puthex(chain, 32);- return append_event("upatch_load", argv[2], payload, event,- chain) ? 1 : 0;+ return append_event(eventlog_upatch_record(type, true), argv[2],+ payload, event, chain) ? 1 : 0;}/* A tree operation, with the digest in the form the target records. */if (strcmp(argv[1], "tree") == 0 && argc == 6) {diff --git a/testing/proofs/cbmc/chain-events.c b/testing/proofs/cbmc/chain-events.cindex 477a0e66..17ca919a 100644--- a/testing/proofs/cbmc/chain-events.c+++ b/testing/proofs/cbmc/chain-events.c@@ -23,7 +23,7 @@#pragma CPROVER check pop/* The longest string an event hashes: a upatch of a 256-byte name. */-#define HASHED_MAX (sizeof("shirudo-unload") + 32 + UPATCH_NAME_MAX)+#define HASHED_MAX (sizeof("shirudo-java-unload") + 32 + UPATCH_NAME_MAX)/* What the last hash was given and gave, and the one before it. */static __u8 hashed[HASHED_MAX];@@ -176,6 +176,20 @@ static void upatch_event(const char *tag)check_result(ret, 1, event);}+/* The tag each payload type and direction takes, as the model names it. */+static void upatch_tags(void)+{+ __CPROVER_assert(!strcmp(chain_upatch_tag(UPATCH_TYPE_BPF, true),+ "shirudo-upatch") &&+ !strcmp(chain_upatch_tag(UPATCH_TYPE_BPF, false),+ "shirudo-unload") &&+ !strcmp(chain_upatch_tag(UPATCH_TYPE_JAVA, true),+ "shirudo-java") &&+ !strcmp(chain_upatch_tag(UPATCH_TYPE_JAVA, false),+ "shirudo-java-unload"),+ "tagUpatch, tagUnload, tagJava and tagJavaUnload");+}+/* keyBytes: the tag and its NUL, then the certificate's digest. */static void key_event(void){@@ -291,8 +305,11 @@ static void init_event(void)void harness(void){+ upatch_tags();upatch_event("shirudo-upatch");upatch_event("shirudo-unload");+ upatch_event("shirudo-java");+ upatch_event("shirudo-java-unload");key_event();tree_event();fold();diff --git a/testing/proofs/lean4/Shirudo/Events.lean b/testing/proofs/lean4/Shirudo/Events.leanindex c7323b15..0e047f09 100644--- a/testing/proofs/lean4/Shirudo/Events.lean+++ b/testing/proofs/lean4/Shirudo/Events.lean@@ -10,11 +10,12 @@ hashes them. Every event is SHA-256 of a tag, its NUL and fixed fields:- `chain_init_event()`: `"shirudo-init"`, the certificate set's digest,the digest of the listing of the restored set, one- `"<payload hex> <name>\n"` line per upatch sorted by name, and the- upatch tree's root;+ `"<payload hex> <name>\n"` line per upatch sorted by name, a Java+ upatch's line prefixed with `"java "`, and the upatch tree's root;- `chain_upatch_event()`: `"shirudo-upatch"` for a load or- `"shirudo-unload"` for an unload, the payload digest and the name cut- at `strnlen(name, UPATCH_NAME_MAX)`;+ `"shirudo-unload"` for an unload of a BPF upatch, `"shirudo-java"` or+ `"shirudo-java-unload"` for a Java one, the payload digest and the name+ cut at `strnlen(name, UPATCH_NAME_MAX)`;- `chain_key_event()`: `"shirudo-key-revoke"` and the certificate'sdigest;- `chain_tree_event()`: `"shirudo-tree"`, the operation's sequence@@ -41,7 +42,9 @@ accepts of at most `UPATCH_NAME_MAX` bytes and sequence numbers a`__s64` holds, `Fields.bytes` is injective (`bytes_injective`): the firstNUL ends the tag, no tag holds one and no two are equal, and every fieldafter it has a fixed length but the name, which runs to the end. The-listing is injective while no name holds a newline (`listing_injective`).+listing is injective while no name holds a newline (`listing_injective`):+no hex digit is the `j` of `"java "`, so a Java line cannot pass for a+BPF one.Outside that domain both fail: a name of more than `UPATCH_NAME_MAX`bytes is cut (`upatch_cut`), and a name with a newline spells twomembers (`listing_newline`). `qsort()` leaves a set whose names are@@ -101,6 +104,8 @@ def Digest.zero : Digest := ⟨List.replicate 32 0, by simp⟩def tagInit : List UInt8 := ascii "shirudo-init"def tagUpatch : List UInt8 := ascii "shirudo-upatch"def tagUnload : List UInt8 := ascii "shirudo-unload"+def tagJava : List UInt8 := ascii "shirudo-java"+def tagJavaUnload : List UInt8 := ascii "shirudo-java-unload"def tagKeyRevoke : List UInt8 := ascii "shirudo-key-revoke"def tagTree : List UInt8 := ascii "shirudo-tree"def tagHistory : List UInt8 := ascii "shirudo-tree-history"@@ -118,6 +123,8 @@ inductive Fields where| init (cert setDigest treeRoot : Digest)| upatch (name : List UInt8) (payload : Digest)| unload (name : List UInt8) (payload : Digest)+ | javaLoad (name : List UInt8) (payload : Digest)+ | javaUnload (name : List UInt8) (payload : Digest)| keyRevoke (cert : Digest)| tree (seq : Int) (operation : Digest)@@ -160,6 +167,8 @@ def Fields.bytes : Fields → List UInt8| .init c s r => initBytes c s r| .upatch n p => upatchBytes tagUpatch n p| .unload n p => upatchBytes tagUnload n p+ | .javaLoad n p => upatchBytes tagJava n p+ | .javaUnload n p => upatchBytes tagJavaUnload n p| .keyRevoke c => keyBytes tagKeyRevoke c| .tree s o => treeBytes s o@@ -205,6 +214,8 @@ def S64 (seq : Int) : Prop :=def Fields.Fits : Fields → Prop| .upatch n _ => NameFits n| .unload n _ => NameFits n+ | .javaLoad n _ => NameFits n+ | .javaUnload n _ => NameFits n| .tree s _ => S64 s| _ => True@@ -308,6 +319,8 @@ def Fields.tag : Fields → List UInt8| .init .. => tagInit| .upatch .. => tagUpatch| .unload .. => tagUnload+ | .javaLoad .. => tagJava+ | .javaUnload .. => tagJavaUnload| .keyRevoke .. => tagKeyRevoke| .tree .. => tagTree@@ -316,6 +329,8 @@ def Fields.body : Fields → List UInt8| .init c s r => c.bytes ++ s.bytes ++ r.bytes| .upatch n p => p.bytes ++ n.take (strnlen n nameMax)| .unload n p => p.bytes ++ n.take (strnlen n nameMax)+ | .javaLoad n p => p.bytes ++ n.take (strnlen n nameMax)+ | .javaUnload n p => p.bytes ++ n.take (strnlen n nameMax)| .keyRevoke c => c.bytes| .tree s o => be64 (u64OfS64 s) ++ o.bytes@@ -350,6 +365,14 @@ theorem Fields.body_injective {f g : Fields} (hf : f.Fits) (hg : g.Fits)obtain ⟨hp, hn⟩ := split_digest hrw [strnlen_fits hf, strnlen_fits hg] at hnrw [hn, hp]+ case javaLoad.javaLoad n p n' p' =>+ obtain ⟨hp, hn⟩ := split_digest h+ rw [strnlen_fits hf, strnlen_fits hg] at hn+ rw [hn, hp]+ case javaUnload.javaUnload n p n' p' =>+ obtain ⟨hp, hn⟩ := split_digest h+ rw [strnlen_fits hf, strnlen_fits hg] at hn+ rw [hn, hp]case keyRevoke.keyRevoke c c' =>rw [Digest.ext_bytes h]case tree.tree s o s' o' =>@@ -372,6 +395,8 @@ at most `UPATCH_NAME_MAX` bytes, sequence numbers of a `__s64`. -/def Fields.Admitted : Fields → Prop| .upatch n _ => NameAdmitted n| .unload n _ => NameAdmitted n+ | .javaLoad n _ => NameAdmitted n+ | .javaUnload n _ => NameAdmitted n| .tree s _ => S64 s| _ => True@@ -379,6 +404,8 @@ theorem Fields.Admitted.fits {f : Fields} (h : f.Admitted) : f.Fits := bycases f with| upatch n p => exact NameAdmitted.fits h| unload n p => exact NameAdmitted.fits h+ | javaLoad n p => exact NameAdmitted.fits h+ | javaUnload n p => exact NameAdmitted.fits h| tree s o => exact h| init => trivial| keyRevoke => trivial@@ -408,10 +435,11 @@ theorem upatch_cut (tag n : List UInt8) (x : UInt8) (p : Digest)/-! ### The init event's set -//-- A member of the restored set: `struct measured_upatch`, its payload-already hex-encoded by `hex_encode()`. -/+already hex-encoded by `hex_encode()`, and whether it is a Java upatch. -/structure Member wherename : List UInt8payload : Digest+ java : Bool/-- `"0123456789abcdef"[n]` of `hex_encode()`. -/def hexDigit (n : Nat) : UInt8 := (ascii "0123456789abcdef").getD n 0@@ -421,10 +449,15 @@ high nibble first. -/def hexEncode (bs : List UInt8) : List UInt8 :=bs.flatMap fun b => [hexDigit (b.toNat >>> 4), hexDigit (b.toNat &&& 15)]-/-- One line of the listing: `"%s %s\n"` of the payload's hex and the-name. -/+/-- `member_prefix()`: `"java "` for a Java member, nothing for a BPF+one. -/+def memberPrefix (m : Member) : List UInt8 :=+ if m.java then ascii "java " else []++/-- One line of the listing: `"%s%s %s\n"` of the prefix, the payload's+hex and the name. -/def line (m : Member) : List UInt8 :=- hexEncode m.payload.bytes ++ [32] ++ m.name ++ [10]+ memberPrefix m ++ hexEncode m.payload.bytes ++ [32] ++ m.name ++ [10]/-- The listing `chain_init_event()` hashes into the set's digest: thelines of the set in the order `qsort()` left it. -/@@ -474,6 +507,47 @@ theorem hexEncode_injective :theorem hexEncode_digest (d : Digest) : (hexEncode d.bytes).length = 64 := byrw [hexEncode_length, d.length_eq]+/-- No hex digit is the `j` that starts `"java "`. -/+theorem hexDigit_ne_j (n : Nat) : hexDigit n ≠ 106 := by+ by_cases h : n < 16+ · exact (by decide : ∀ a : Fin 16, hexDigit a.val ≠ 106) ⟨n, h⟩+ · have hl : (ascii "0123456789abcdef").length ≤ n := by+ simp [ascii]; omega+ simp only [hexDigit, List.getD_eq_getElem?_getD, List.getElem?_eq_none hl,+ Option.getD_none]+ decide++/-- A digest's hex starts with a hex digit. -/+theorem hexEncode_head (d : Digest) :+ ∃ x t, hexEncode d.bytes = x :: t ∧ x ≠ 106 := by+ have hl := d.length_eq+ cases hb : d.bytes with+ | nil => rw [hb] at hl; simp at hl+ | cons b bs =>+ exact ⟨hexDigit (b.toNat >>> 4), hexDigit (b.toNat &&& 15) :: hexEncode bs,+ by simp [hexEncode], hexDigit_ne_j _⟩++/-- Two lines that agree have the same prefix: a Java line starts with+`j`, a BPF line with a hex digit. -/+theorem line_prefix {m n : Member} {r s : List UInt8}+ (h : memberPrefix m ++ (hexEncode m.payload.bytes ++ r) =+ memberPrefix n ++ (hexEncode n.payload.bytes ++ s)) :+ m.java = n.java ∧ hexEncode m.payload.bytes ++ r =+ hexEncode n.payload.bytes ++ s := by+ obtain ⟨x, t, hx, hxj⟩ := hexEncode_head m.payload+ obtain ⟨y, u, hy, hyj⟩ := hexEncode_head n.payload+ cases hm : m.java <;> cases hn : n.java <;>+ simp only [memberPrefix, hm, hn, Bool.false_eq_true, ite_true, ite_false,+ List.nil_append] at h+ · exact ⟨rfl, h⟩+ · rw [hx] at h+ simp [ascii] at h+ exact absurd h.1 hxj+ · rw [hy] at h+ simp [ascii] at h+ exact absurd h.1.symm hyj+ · exact ⟨rfl, List.append_cancel_left h⟩+/-- While no name holds a newline, the listing spells one set: each lineis 64 hex digits, a space, and the name up to the newline. -/theorem listing_injective :@@ -483,27 +557,28 @@ theorem listing_injective :| [], m :: _, _, _, h => by simp [listing, line] at h| m :: _, [], _, _, h => by simp [listing, line] at h| m :: ms, n :: ns, hs, ht, h => by- simp only [listing, List.flatMap_cons, line, List.append_assoc,- List.singleton_append] at h+ simp only [listing, List.flatMap_cons, line, List.append_assoc] at h+ obtain ⟨hj, h⟩ := line_prefix h+ simp only [List.singleton_append] at hhave h1 := List.append_inj h(by rw [hexEncode_digest, hexEncode_digest])have hp := Digest.ext_bytes (hexEncode_injective _ _ h1.1)have h1' := h1.2- simp only [List.cons_append, List.cons.injEq, true_and] at h1'+ simp only [List.cons.injEq, true_and] at h1'have h2 := split_at 10 (hs m (by simp)) (ht n (by simp)) h1'have hrest := listing_injective ms ns(fun x hx => hs x (by simp [hx])) (fun x hx => ht x (by simp [hx])) h2.2cases mcases n- simp only at hp h2- rw [hp, h2.1, hrest]+ simp only at hp h2 hj+ rw [hp, h2.1, hj, hrest]/-- Outside the domain: a name holding a newline and the hex of anotherpayload makes one member spell two. -/theorem listing_newline (p q : Digest) (a b : List UInt8) :- listing [⟨a ++ 10 :: hexEncode q.bytes ++ 32 :: b, p⟩] =- listing [⟨a, p⟩, ⟨b, q⟩] := by- simp [listing, line]+ listing [⟨a ++ 10 :: hexEncode q.bytes ++ 32 :: b, p, false⟩] =+ listing [⟨a, p, false⟩, ⟨b, q, false⟩] := by+ simp [listing, line, memberPrefix]/-! ### The order `qsort()` leaves -/@@ -590,17 +665,19 @@ theorem sorts_unique {s t out out' : List Member}members of one name sort, and their listings differ unless the payloadsare equal. -/theorem sorts_tie (n : List UInt8) (p q : Digest) :- Sorts [⟨n, p⟩, ⟨n, q⟩] [⟨n, p⟩, ⟨n, q⟩] ∧- Sorts [⟨n, p⟩, ⟨n, q⟩] [⟨n, q⟩, ⟨n, p⟩] ∧- (listing [⟨n, p⟩, ⟨n, q⟩] = listing [⟨n, q⟩, ⟨n, p⟩] → p = q) := by- have heq : memberCmp ⟨n, p⟩ ⟨n, q⟩ = .eq :=+ Sorts [⟨n, p, false⟩, ⟨n, q, false⟩] [⟨n, p, false⟩, ⟨n, q, false⟩] ∧+ Sorts [⟨n, p, false⟩, ⟨n, q, false⟩] [⟨n, q, false⟩, ⟨n, p, false⟩] ∧+ (listing [⟨n, p, false⟩, ⟨n, q, false⟩] =+ listing [⟨n, q, false⟩, ⟨n, p, false⟩] → p = q) := by+ have heq : memberCmp ⟨n, p, false⟩ ⟨n, q, false⟩ = .eq :=(strcmp_total.eq_iff _ _).mpr rfl- have heq' : memberCmp ⟨n, q⟩ ⟨n, p⟩ = .eq :=+ have heq' : memberCmp ⟨n, q, false⟩ ⟨n, p, false⟩ = .eq :=(strcmp_total.eq_iff _ _).mpr rflrefine ⟨⟨List.Perm.refl _, by simp [heq]⟩,⟨List.Perm.swap _ _ _, by simp [heq']⟩, fun h => ?_⟩- simp only [listing, line, List.flatMap_cons, List.flatMap_nil,- List.append_nil, List.append_assoc] at h+ simp only [listing, line, memberPrefix, List.flatMap_cons, List.flatMap_nil,+ List.append_nil, List.append_assoc, Bool.false_eq_true, ite_false,+ List.nil_append] at hhave := List.append_inj h (by rw [hexEncode_digest, hexEncode_digest])exact Digest.ext_bytes (hexEncode_injective _ _ this.1)@@ -608,12 +685,14 @@ theorem sorts_tie (n : List UInt8) (p q : Digest) :/-- An event the register measures, as the target means it: an init eventwith its certificate set's digest, the restored set as `qsort()` left it-and the tree root; a load or an unload of a upatch; a key revocation; a-tree operation. -/+and the tree root; a load or an unload of a BPF or a Java upatch; a key+revocation; a tree operation. -/inductive Event where| init (cert : Digest) (set : List Member) (treeRoot : Digest)| load (name : List UInt8) (payload : Digest)| unload (name : List UInt8) (payload : Digest)+ | javaLoad (name : List UInt8) (payload : Digest)+ | javaUnload (name : List UInt8) (payload : Digest)| keyRevoke (cert : Digest)| tree (seq : Int) (operation : Digest)@@ -622,6 +701,8 @@ def Event.Admitted : Event → Prop| .init _ s _ => ∀ m ∈ s, NameAdmitted m.name| .load n _ => NameAdmitted n| .unload n _ => NameAdmitted n+ | .javaLoad n _ => NameAdmitted n+ | .javaUnload n _ => NameAdmitted n| .keyRevoke _ => True| .tree s _ => S64 s@@ -654,6 +735,8 @@ def Event.fields : Event → Fields| .init c s r => .init c (sha (listing s)) r| .load n p => .upatch n p| .unload n p => .unload n p+ | .javaLoad n p => .javaLoad n p+ | .javaUnload n p => .javaUnload n p| .keyRevoke c => .keyRevoke c| .tree s o => .tree s o@@ -702,6 +785,8 @@ theorem Event.admitted_fields {e : Event} (h : e.Admitted) :| init => trivial| load n p => exact h| unload n p => exact h+ | javaLoad n p => exact h+ | javaUnload n p => exact h| keyRevoke => trivial| tree s o => exact h@@ -744,6 +829,8 @@ theorem Event.digest_injective_on (a b : Event) (ha : a.Predicted)first| (simp only [Fields.upatch.injEq] at hf; rw [hf.1, hf.2])| (simp only [Fields.unload.injEq] at hf; rw [hf.1, hf.2])+ | (simp only [Fields.javaLoad.injEq] at hf; rw [hf.1, hf.2])+ | (simp only [Fields.javaUnload.injEq] at hf; rw [hf.1, hf.2])| (simp only [Fields.keyRevoke.injEq] at hf; rw [hf])| (simp only [Fields.tree.injEq] at hf; rw [hf.1, hf.2])diff --git a/testing/unit/chain.c b/testing/unit/chain.cindex 1ad9b2ea..062c5e64 100644--- a/testing/unit/chain.c+++ b/testing/unit/chain.c@@ -39,6 +39,42 @@ static void chain_events_hash_their_documented_input(void)EXPECT_MEMEQ(event, want, sizeof(want));}+/* Each payload type and direction has a tag of its own. */+static void upatch_tags_are_distinct(void)+{+ const char *const tags[] = {+ chain_upatch_tag(UPATCH_TYPE_BPF, true),+ chain_upatch_tag(UPATCH_TYPE_BPF, false),+ chain_upatch_tag(UPATCH_TYPE_JAVA, true),+ chain_upatch_tag(UPATCH_TYPE_JAVA, false),+ };++ EXPECT_STREQ(tags[0], "shirudo-upatch");+ EXPECT_STREQ(tags[1], "shirudo-unload");+ EXPECT_STREQ(tags[2], "shirudo-java");+ EXPECT_STREQ(tags[3], "shirudo-java-unload");+ EXPECT_FALSE(chain_upatch_tag(3, true));+}++/* The longest tag, with its NUL, the digest and a whole name. */+static void java_unload_event_hashes_its_documented_input(void)+{+ static const char tag[] = "shirudo-java-unload";+ __u8 payload[32], buf[sizeof(tag) + 32 + UPATCH_NAME_MAX];+ char name[UPATCH_NAME_MAX + 1];+ __u8 want[32], event[32];++ memset(payload, 0x3c, sizeof(payload));+ memset(name, 'n', UPATCH_NAME_MAX);+ name[UPATCH_NAME_MAX] = '\0';+ memcpy(buf, tag, sizeof(tag));+ memcpy(buf + sizeof(tag), payload, sizeof(payload));+ memcpy(buf + sizeof(tag) + 32, name, UPATCH_NAME_MAX);+ ASSERT_EQ(sha256_buffer(buf, sizeof(buf), want), 0);+ ASSERT_EQ(chain_upatch_event(tag, name, payload, event), 0);+ EXPECT_MEMEQ(event, want, sizeof(want));+}+static void chain_fold_extends_like_the_register(void){__u8 chain[32] = {}, event[32], cat[64] = {}, want[32];@@ -54,6 +90,8 @@ static const struct unit_case cases[] = {UNIT_CASE(sha256_2_hashes_the_concatenation),UNIT_CASE(sha256_2_reports_a_failure),UNIT_CASE(chain_events_hash_their_documented_input),+ UNIT_CASE(upatch_tags_are_distinct),+ UNIT_CASE(java_unload_event_hashes_its_documented_input),UNIT_CASE(chain_fold_extends_like_the_register),{}};diff --git a/testing/unit/eventlog.c b/testing/unit/eventlog.cindex 39d1ddf2..9f23a3b0 100644--- a/testing/unit/eventlog.c+++ b/testing/unit/eventlog.c@@ -7,6 +7,7 @@#include <unistd.h>#include <shirudo/string.h>+#include <shirudo/upatch.h>#include "eventlog.h"#include "io.h"@@ -229,11 +230,73 @@ static void set_counts_in_an_init_record_alone(void)drop_log();}+static void upatch_record(char *buf, size_t cap, const char *type,+ const char *name)+{+ struct json_emit e = { .buf = buf, .cap = cap };+ __u8 digest[32];++ memset(digest, 3, sizeof(digest));+ eventlog_record_open(&e, type, digest);+ json_obj_open_key(&e, "upatch");+ json_kv_string(&e, "name", name);+ json_kv_string(&e, "payload_sha256", DIGEST_HEX);+ json_obj_close(&e);+ json_obj_close(&e);+ ASSERT_EQ(e.err, 0);+}++static void upatch_records_name_type_and_direction(void)+{+ __u32 type = 0;+ bool load = false;++ EXPECT_STREQ(eventlog_upatch_record(UPATCH_TYPE_BPF, true),+ "upatch_load");+ EXPECT_STREQ(eventlog_upatch_record(UPATCH_TYPE_JAVA, false),+ "java_unload");+ EXPECT_FALSE(eventlog_upatch_record(3, true));+ EXPECT_TRUE(eventlog_upatch_record_parse("java_load", &type, &load));+ EXPECT_UEQ(type, UPATCH_TYPE_JAVA);+ EXPECT_TRUE(load);+ EXPECT_TRUE(eventlog_upatch_record_parse("upatch_unload", &type,+ &load));+ EXPECT_UEQ(type, UPATCH_TYPE_BPF);+ EXPECT_FALSE(load);+ EXPECT_FALSE(eventlog_upatch_record_parse("java_loa", &type, &load));+ EXPECT_FALSE(eventlog_upatch_record_parse("upatch_load ", &type,+ &load));+}++/* A Java load and unload count as upatch events like BPF ones. */+static void java_records_count_as_upatch_events(void)+{+ char init[1024], load[1024], unload[1024], text[4096];+ struct seen_upatches s = {};+ int len;++ use_log();+ set_record(init, sizeof(init), "init", "fix");+ upatch_record(load, sizeof(load), "java_load", "agent");+ upatch_record(unload, sizeof(unload), "java_unload", "agent");+ len = strsprintf(text, sizeof(text), "%s\n%s\n%s\n", init, load,+ unload);+ ASSERT_TRUE(len > 0);+ write_log(text, (size_t)len);+ ASSERT_EQ(eventlog_boot_upatches(count_upatch, &s), 0);+ EXPECT_EQ(s.inits, 1);+ EXPECT_EQ(s.loads, 2);+ EXPECT_STREQ(s.last, "agent");+ drop_log();+}+static const struct unit_case cases[] = {UNIT_CASE(unfinished_append_is_no_record),UNIT_CASE(nul_in_the_log_is_refused),UNIT_CASE(tree_op_measured_this_boot),UNIT_CASE(set_counts_in_an_init_record_alone),+ UNIT_CASE(upatch_records_name_type_and_direction),+ UNIT_CASE(java_records_count_as_upatch_events),{}};diff --git a/testing/unit/measured.c b/testing/unit/measured.cindex 3fc40a0b..76eed636 100644--- a/testing/unit/measured.c+++ b/testing/unit/measured.c@@ -4,9 +4,12 @@#include <errno.h>#include "chain.h"+#include "crypto.h"#include "measured.h"#include "unit.h"+#define BPF UPATCH_TYPE_BPF+#define JAVA UPATCH_TYPE_JAVA#define PAYLOAD "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"/* An init event lists each upatch once, by a name an upatch can have. */@@ -15,20 +18,59 @@ static void set_takes_each_upatch_once(void)struct measured_upatch set[2];size_t n = 0;- EXPECT_EQ(measured_set_add(set, &n, 2, "a", PAYLOAD), 0);- EXPECT_EQ(measured_set_add(set, &n, 2, "a", PAYLOAD), -EEXIST);- EXPECT_EQ(measured_set_add(set, &n, 2, "a\n" PAYLOAD " b", PAYLOAD),- -EINVAL);- EXPECT_EQ(measured_set_add(set, &n, 2, "b", "00"), -EINVAL);- EXPECT_EQ(measured_set_add(set, &n, 2, "b", PAYLOAD), 0);- EXPECT_EQ(measured_set_add(set, &n, 2, "c", PAYLOAD), -E2BIG);+ EXPECT_EQ(measured_set_add(set, &n, 2, "a", PAYLOAD, BPF), 0);+ EXPECT_EQ(measured_set_add(set, &n, 2, "a", PAYLOAD, BPF), -EEXIST);+ EXPECT_EQ(measured_set_add(set, &n, 2, "a", PAYLOAD, JAVA), -EEXIST);+ EXPECT_EQ(measured_set_add(set, &n, 2, "a\n" PAYLOAD " b", PAYLOAD,+ BPF), -EINVAL);+ EXPECT_EQ(measured_set_add(set, &n, 2, "b", "00", BPF), -EINVAL);+ EXPECT_EQ(measured_set_add(set, &n, 2, "b", PAYLOAD, 3), -EINVAL);+ EXPECT_EQ(measured_set_add(set, &n, 2, "b", PAYLOAD, JAVA), 0);+ EXPECT_EQ(measured_set_add(set, &n, 2, "c", PAYLOAD, BPF), -E2BIG);ASSERT_EQ(n, 2);EXPECT_STREQ(set[0].name, "a");+ EXPECT_UEQ(set[0].type, BPF);EXPECT_STREQ(set[1].payload_hex, PAYLOAD);+ EXPECT_UEQ(set[1].type, JAVA);+}++static void member_type_is_bpf_or_java(void)+{+ __u32 type = 0;++ EXPECT_EQ(measured_member_type("", &type), 0);+ EXPECT_UEQ(type, BPF);+ EXPECT_EQ(measured_member_type("java", &type), 0);+ EXPECT_UEQ(type, JAVA);+ EXPECT_EQ(measured_member_type("bpf", &type), -EINVAL);+ EXPECT_EQ(measured_member_type("Java", &type), -EINVAL);+}++/*+ * A Java member lists with its prefix, so a set that differs only in a+ * member's type gives another init event.+ */+static void init_event_tells_java_from_bpf(void)+{+ __u8 cert[32] = {}, root[32] = {}, digest[32], want[32];+ __u8 bpf_event[32], java_event[32];+ static const char listing[] = "java " PAYLOAD " a\n";+ struct measured_upatch set[1] = {+ { .name = "a", .payload_hex = PAYLOAD, .type = BPF },+ };++ ASSERT_EQ(chain_init_event(cert, set, 1, root, digest, bpf_event), 0);+ set[0].type = JAVA;+ ASSERT_EQ(chain_init_event(cert, set, 1, root, digest, java_event), 0);+ EXPECT_TRUE(memcmp(bpf_event, java_event, sizeof(bpf_event)));+ ASSERT_EQ(sha256_buffer(listing, sizeof(listing) - 1, want), 0);+ EXPECT_MEMEQ(digest, want, sizeof(want));}static const struct unit_case cases[] = {UNIT_CASE(set_takes_each_upatch_once),+ UNIT_CASE(member_type_is_bpf_or_java),+ UNIT_CASE(init_event_tells_java_from_bpf),{}};diff --git a/util/attest.c b/util/attest.cindex de73e013..6d596e21 100644--- a/util/attest.c+++ b/util/attest.c@@ -526,7 +526,9 @@ static bool record_word(const char *s)static int on_record(void *ctx){struct parse *p = ctx;+ __u32 payload_type;struct record *r;+ bool load;if (p->n_recs >= RECORDS_MAX)return -E2BIG;@@ -582,18 +584,16 @@ static int on_record(void *ctx)memset(r, 0, sizeof(*r));strscpy(r->type, "init", sizeof(r->type));memcpy(r->digest, in->digest, 32);- } else if (!strcmp(p->type, "upatch_load") ||- !strcmp(p->type, "upatch_unload")) {- bool load = p->type[7] == 'l';+ } else if (eventlog_upatch_record_parse(p->type, &payload_type,+ &load)) {__u8 payload[32], event[32];if (!upatch_name_valid(p->upatch_name))return -EINVAL;strscpy(r->label, p->upatch_name, sizeof(r->label));if (!hex_decode(p->upatch_payload_hex, payload, 32) &&- !chain_upatch_event(load ? "shirudo-upatch" :- "shirudo-unload", p->upatch_name,- payload, event))+ !chain_upatch_event(chain_upatch_tag(payload_type, load),+ p->upatch_name, payload, event))r->derived = !memneq_explicit(event, r->digest, 32);} else if (!strcmp(p->type, "tree_op")) {__u8 operation[32], event[32];diff --git a/util/chain.c b/util/chain.cindex 0adc79dc..61b9fd67 100644--- a/util/chain.c+++ b/util/chain.c@@ -49,6 +49,15 @@ out:return ret;}+/*+ * A payload digest is hex alone, so the prefix cannot be read as part of+ * one: a Java member and a BPF member never list alike.+ */+static const char *member_prefix(const struct measured_upatch *m)+{+ return m->type == UPATCH_TYPE_JAVA ? "java " : "";+}+static int member_cmp(const void *a, const void *b){return strcmp(((const struct measured_upatch *)a)->name,@@ -65,11 +74,12 @@ int chain_init_event(const __u8 cert_sha[32],char *listing;int ret;- /* set_digest = SHA256(sorted "<payload_hex> <name>\n" listing) */+ /* set_digest = SHA256(sorted "[java ]<payload_hex> <name>\n" listing) */if (n > 1)qsort(set, n, sizeof(*set), member_cmp);for (i = 0; i < n; i++)- listing_len += strlen(set[i].payload_hex) + 1 ++ listing_len += strlen(member_prefix(&set[i])) ++ strlen(set[i].payload_hex) + 1 +strlen(set[i].name) + 1;memset(set_digest, 0, 32);memset(event, 0, 32);@@ -78,7 +88,8 @@ int chain_init_event(const __u8 cert_sha[32],return -ENOMEM;for (i = 0; i < n; i++) {int w = strsprintf(listing + off, listing_len + 1 - off,- "%s %s\n", set[i].payload_hex, set[i].name);+ "%s%s %s\n", member_prefix(&set[i]),+ set[i].payload_hex, set[i].name);if (w < 0) {xfree(listing, listing_len + 1);@@ -98,10 +109,22 @@ int chain_init_event(const __u8 cert_sha[32],return sha256_buffer(tag, taglen + 96, event);}+const char *chain_upatch_tag(__u32 type, bool load)+{+ switch (type) {+ case UPATCH_TYPE_BPF:+ return load ? "shirudo-upatch" : "shirudo-unload";+ case UPATCH_TYPE_JAVA:+ return load ? "shirudo-java" : "shirudo-java-unload";+ default:+ return NULL;+ }+}+int chain_upatch_event(const char *tag, const char *name,const __u8 payload[32], __u8 event[32]){- __u8 buf[sizeof("shirudo-unload") + 32 + UPATCH_NAME_MAX];+ __u8 buf[sizeof("shirudo-java-unload") + 32 + UPATCH_NAME_MAX];size_t taglen = strlen(tag) + 1, off = 0;size_t namelen = strnlen(name, UPATCH_NAME_MAX);diff --git a/util/chain.h b/util/chain.hindex ed8d1c91..370c28f7 100644--- a/util/chain.h+++ b/util/chain.h@@ -41,11 +41,20 @@ int sha256_2(const void *a, size_t a_len,struct measured_upatch {char name[UPATCH_NAME_MAX + 1];char payload_hex[2 * UPATCH_PAYLOAD_HASH_LEN + 1];+ __u32 type; /* enum upatch_type */};+/*+ * The domain tag of an upatch event, by payload type and direction:+ * "shirudo-upatch" and "shirudo-unload" for BPF, "shirudo-java" and+ * "shirudo-java-unload" for Java. NULL for a type with none.+ */+const char *chain_upatch_tag(__u32 type, bool load);+/** The event digests every measuring path and the predictor share.- * The set is sorted by name before hashing.+ * The set is sorted by name before hashing, and lists a Java member+ * as "java <payload_hex> <name>", a BPF one without the prefix.*/int chain_init_event(const __u8 cert_sha[32],struct measured_upatch *set, size_t n,diff --git a/util/eventlog.c b/util/eventlog.cindex 8301e607..8a9471b0 100644--- a/util/eventlog.c+++ b/util/eventlog.c@@ -549,6 +549,62 @@ int eventlog_tree_op_measured(__s64 seq, const char *operation_digest)return t.found;}+static const struct {+ const char *record;+ const char *kind;+ __u32 type;+ bool load;+} upatch_records[] = {+ { "upatch_load", "load", UPATCH_TYPE_BPF, true },+ { "upatch_unload", "unload", UPATCH_TYPE_BPF, false },+ { "java_load", "java-load", UPATCH_TYPE_JAVA, true },+ { "java_unload", "java-unload", UPATCH_TYPE_JAVA, false },+};++bool eventlog_upatch_kind_parse(const char *kind, size_t len, __u32 *type,+ bool *load)+{+ size_t i;++ for (i = 0; i < ARRAY_SIZE(upatch_records); i++) {+ const char *k = upatch_records[i].kind;++ if (strlen(k) == len && !memcmp(kind, k, len)) {+ *type = upatch_records[i].type;+ *load = upatch_records[i].load;+ return true;+ }+ }+ return false;+}++const char *eventlog_upatch_record(__u32 type, bool load)+{+ size_t i;++ for (i = 0; i < ARRAY_SIZE(upatch_records); i++) {+ if (upatch_records[i].type == type &&+ upatch_records[i].load == load)+ return upatch_records[i].record;+ }+ return NULL;+}++bool eventlog_upatch_record_parse(const char *record, __u32 *type,+ bool *load)+{+ size_t i;++ for (i = 0; i < ARRAY_SIZE(upatch_records); i++) {+ if (!strcmp(upatch_records[i].record, record)) {+ *type = upatch_records[i].type;+ *load = upatch_records[i].load;+ return true;+ }+ }+ return false;+}+struct upatches_ctx {int (*cb)(enum eventlog_upatch_event event, const char *name,const char *payload_hex, void *ctx);@@ -581,7 +637,9 @@ static int upatches_line(const char *line, size_t len, bool is_init,sizeof(u->payload_hex), 0, NULL, 0 },};struct json_array set = { upatches_set_entry, u };+ __u32 payload_type;char type[32];+ bool load;/* Only an init record's set lists what boot restore loaded. */struct json_field f[] = {{ "type", JSON_T_STRING, type, sizeof(type), 0, NULL, 0 },@@ -604,12 +662,12 @@ static int upatches_line(const char *line, size_t len, bool is_init,return u->ret ? u->ret : -EINVAL;if (is_init)return 0;- if (strcmp(type, "upatch_load") && strcmp(type, "upatch_unload"))+ if (!eventlog_upatch_record_parse(type, &payload_type, &load))return 0;if (!f[2].seen || !upatch_member_valid(u->name, u->payload_hex))return -EINVAL;- return u->cb(type[7] == 'l' ? EVENTLOG_UPATCH_LOAD :- EVENTLOG_UPATCH_UNLOAD, u->name, u->payload_hex, u->ctx);+ return u->cb(load ? EVENTLOG_UPATCH_LOAD : EVENTLOG_UPATCH_UNLOAD,+ u->name, u->payload_hex, u->ctx);}int eventlog_boot_upatches(int (*cb)(enum eventlog_upatch_event event,diff --git a/util/eventlog.h b/util/eventlog.hindex 376b2768..be373476 100644--- a/util/eventlog.h+++ b/util/eventlog.h@@ -91,6 +91,26 @@ enum eventlog_upatch_event {EVENTLOG_UPATCH_UNLOAD,};+/*+ * The record type of an upatch load or unload of a payload of type:+ * "upatch_load" and "upatch_unload" for BPF, "java_load" and+ * "java_unload" for Java. NULL for a type with none.+ */+const char *eventlog_upatch_record(__u32 type, bool load);+/*+ * Whether record is one eventlog_upatch_record() gives, and if so the+ * payload type and direction it records.+ */+bool eventlog_upatch_record_parse(const char *record, __u32 *type,+ bool *load);+/*+ * As eventlog_upatch_record_parse(), for the len bytes at kind that+ * attest predict --event spells the record with: "load", "unload",+ * "java-load" or "java-unload".+ */+bool eventlog_upatch_kind_parse(const char *kind, size_t len, __u32 *type,+ bool *load);+/** The loads and unloads the records measure, in order: this boot's, or* every boot's since the log began. An init record givesdiff --git a/util/measured.c b/util/measured.cindex 2b5a8d26..a39469d2 100644--- a/util/measured.c+++ b/util/measured.c@@ -96,6 +96,7 @@ struct record_fields {char operation[sizeof("sha256:") + 2 * 32];char name[UPATCH_NAME_MAX + 1];char payload_hex[2 * UPATCH_PAYLOAD_HASH_LEN + 1];+ char member_type[8];__s64 seq;struct measured_upatch *set;size_t n;@@ -105,12 +106,16 @@ struct record_fields {static int record_set_entry(void *arg){struct record_fields *r = arg;+ __u32 type;int ret;- ret = measured_set_add(r->set, &r->n, MEASURED_MAX, r->name,- r->payload_hex);+ ret = measured_member_type(r->member_type, &type);+ if (!ret)+ ret = measured_set_add(r->set, &r->n, MEASURED_MAX, r->name,+ r->payload_hex, type);r->name[0] = '\0';r->payload_hex[0] = '\0';+ r->member_type[0] = '\0';return ret;}@@ -120,6 +125,8 @@ static int record_parse(const char *line, size_t len, struct record_fields *r){ "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 },+ { "type", JSON_T_STRING, r->member_type,+ sizeof(r->member_type), 0, NULL, 0 },};struct json_field upatch[] = {{ "name", JSON_T_STRING, r->name, sizeof(r->name), 0, NULL, 0 },@@ -172,15 +179,16 @@ static int init_event_of(struct record_fields *r, __u8 event[32])event);}-static int upatch_event_of(struct record_fields *r, __u8 event[32])+static int upatch_event_of(struct record_fields *r, __u32 type, bool load,+ __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);+ return chain_upatch_event(chain_upatch_tag(type, load), r->name,+ payload, event);}static int key_event_of(struct record_fields *r, __u8 event[32])@@ -203,11 +211,13 @@ static int tree_event_of(struct record_fields *r, __u8 event[32])static int record_event_of(struct record_fields *r, __u8 event[32]){+ __u32 type;+ bool load;+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 (eventlog_upatch_record_parse(r->type, &type, &load))+ return upatch_event_of(r, type, load, event);if (!strcmp(r->type, "key_revoke"))return key_event_of(r, event);if (!strcmp(r->type, "tree_op"))@@ -339,10 +349,24 @@ void measured_upatch_of(struct measured_upatch *e, const struct upatch *u)upatch_header_name(u, e->name);hex_encode(u->hdr.payload_sha256, UPATCH_PAYLOAD_HASH_LEN,e->payload_hex);+ e->type = u->hdr.type;+}++int measured_member_type(const char *text, __u32 *type)+{+ if (!text[0]) {+ *type = UPATCH_TYPE_BPF;+ return 0;+ }+ if (!strcmp(text, "java")) {+ *type = UPATCH_TYPE_JAVA;+ return 0;+ }+ return -EINVAL;}int measured_set_add(struct measured_upatch *set, size_t *n, size_t max,- const char *name, const char *payload_hex)+ const char *name, const char *payload_hex, __u32 type){struct measured_upatch *e;size_t i;@@ -355,10 +379,13 @@ int measured_set_add(struct measured_upatch *set, size_t *n, size_t max,}if (*n >= max)return -E2BIG;+ if (!chain_upatch_tag(type, true))+ return -EINVAL;e = &set[*n];if (strscpy(e->name, name, sizeof(e->name)) < 0 ||strscpy(e->payload_hex, payload_hex, sizeof(e->payload_hex)) < 0)return -E2BIG;+ e->type = type;(*n)++;return 0;}diff --git a/util/measured.h b/util/measured.hindex 8fadd2b0..c530896c 100644--- a/util/measured.h+++ b/util/measured.h@@ -28,17 +28,23 @@ void measured_set_free(struct measured_set *s);struct measured_upatch;struct upatch;-/* The name and payload an init record lists for the upatch u. */+/* The name, payload and type an init record lists for the upatch u. */void measured_upatch_of(struct measured_upatch *e, const struct upatch *u);+/*+ * The payload type an init record's member names in its "type": BPF+ * where it names none, Java for "java"; -EINVAL for any other.+ */+int measured_member_type(const char *text, __u32 *type);+/** A member added to the *n of set, which holds at most max: 0, -EINVAL- * for one upatch_member_valid() refuses, -EEXIST for a name the set+ * for one upatch_member_valid() refuses or a type with no events, -EEXIST for a name the set* holds, for an init event lists each upatch once and sorts by name, or* -E2BIG when the set is full.*/int measured_set_add(struct measured_upatch *set, size_t *n, size_t max,- const char *name, const char *payload_hex);+ const char *name, const char *payload_hex, __u32 type);/** The upatches pinned in the private bpffs, or archived with the seal