eBPF-based security platform
testing: prove the status verdict and the package state entry
Daniel Borkmann65585130d22b
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rstindex 6867b065..157f53bc 100644--- a/Documentation/contribute/testing/proof-coverage.rst+++ b/Documentation/contribute/testing/proof-coverage.rst@@ -201,6 +201,22 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.- Lines up to 12 bytes- A setting written only for its exact key and terminated, withoutCR or LF+ * - ``bpm-state-entry``+ - ``on_state_entry()`` of :src:`util/bpm_state.c`+ - Any entry with a name and version of up to 6 bytes, into an empty+ or a full table; a path buffer of 40 bytes, which some of those+ paths outgrow, in place of ``PATH_MAX``, which none does+ - Kept exactly when name, version and source are valid, the path is+ ``BPM_PACKAGES_DIR/<name>/<version>`` and the table has room; kept+ as read, with the next read into a cleared entry; a refusal not+ counted, ``-E2BIG`` from a full table+ * - ``bpm-state-emit``+ - ``bpm_state_emit()`` with the writer of :src:`util/json.c`+ - None, one or both of two fixed entries, into buffers of 0 bytes+ and of each document's size with and without its NUL+ - 0 exactly when the document fits with its NUL, then the state+ document byte for byte and its length; nothing written past the+ buffer, nor to the length on ``-E2BIG``* - ``bpm-tree-selector``- ``tree_selector_kv_add()``, ``tree_selector_copy()``,``tree_selector_free()``, ``tree_text()``@@ -812,6 +828,30 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.- The first command that is the word, else the commands the wordbegins, else those that begin it, in table order, as many as thecapacity takes and none written past it+ * - ``conditions-verdict``+ - ``status_verdict()``, ``status_reason()``+ - Every input; reasons into 1, 8, 32 and 160 bytes+ - The first verdict of its order that applies; ``tampered`` exactly+ when a cause holds on a host that answered, whatever the other+ fields say; the reason of the first cause in the order of the+ reasons, terminated and cut to the buffer+ * - ``conditions-rows``+ - ``conditions()``, ``keyring_condition()``, ``conditions_print()``+ - Every input ``status`` can build: status codes of 0 or a negated+ errno, a replay result only with a register, pins counted or not+ checked; room for 0 to 8 rows; ``vsnprintf()`` modelled to keep+ its arguments+ - As many rows as there is room for, in the report's order; with+ room for all, each the first state of its row that applies, the+ worst first but for ``KeyringSealed``, formatted from that+ state's numbers and names; an ``ALERT`` only under ``tampered``,+ that of ``LsmAttached`` also under ``lsm-off``+ * - ``conditions-detail``+ - ``set_detail()``+ - Every text of up to 193 bytes; texts of 832 to 1024 bytes that+ start with a fixed 832+ - The whole text up to 191 bytes, else its first 188 and ``...``;+ ``?`` for a text of 1024 bytesLean 4 proofs-------------@@ -1027,11 +1067,10 @@ a proof covers.* - Files- Why no proof reads them- * - :src:`util/bpm_state.c`, :src:`util/bpm_tree_overlap.c`,- :src:`util/bpm_tree_state.c`, :src:`util/conditions.c`+ * - :src:`util/bpm_tree_overlap.c`, :src:`util/bpm_tree_state.c`- No proof is written yet. A harness could carry their logic, in- whole or in part: the package state and tree state readers, which- selectors of a tree can meet on one node and the status verdict+ whole or in part: the tree state reader and which selectors of a+ tree can meet on one node* - :src:`util/harden.c`, :src:`util/identity.c`,:src:`util/image_cache.c`, :src:`util/image_config.c`,:src:`util/image_guards.c`, :src:`util/image_keys.c`,@@ -1090,7 +1129,12 @@ the config update record, the site configuration file and the configimage's settings and tree.The ``.upatch`` reader, the TPM structures and the package, index andtree readers are covered part by part, as the table shows; what joins-those parts has no proof.+those parts has no proof. So it is with the package state:+``bpm_state_parse()`` hands ``on_state_entry()`` to ``json_parse()``,+and neither that nor reading back what ``bpm_state_emit()`` writes has a+proof, as a string of any contents moves all that follows it in the+document, which costs CBMC more memory than it has. The ``bpm_state``+unit cases check both on fixed entries.Of :src:`util/paths.c` and :src:`util/serializer.c` the proofs cover thefunctions the table names. ``key_paths()``, which callsdiff --git a/testing/proofs/cbmc/bpm-state-emit.c b/testing/proofs/cbmc/bpm-state-emit.cnew file mode 100644index 00000000..790dbd0b--- /dev/null+++ b/testing/proofs/cbmc/bpm-state-emit.c@@ -0,0 +1,58 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * bpm_state_emit() of util/bpm_state.c with the JSON writer of util/json.c,+ * for none, one or both of two fixed entries, into LEN bytes: 0 exactly+ * when the state document fits with its NUL, the document then byte for+ * byte, and nothing written past LEN bytes or to the length on a refusal.+ */++#include "proof.h"+#include "../../../util/malloc.c"+#include "../../../util/string.c"+#include "../../../util/hex.c"+#include "../../../util/json.c"+#include "../../../util/bpm_state.c"++#define FIX BPM_PACKAGES_DIR "/fix/1.0.0"+#define OBS BPM_PACKAGES_DIR "/kernel-obs/2.1.0-rc.1"++static const struct bpm_state_entry entries[] = {+ { "fix", "1.0.0", "main", FIX },+ { "kernel-obs", "2.1.0-rc.1", "direct", OBS },+};++#define ENTRY_FIX \+ "{\"name\":\"fix\",\"version\":\"1.0.0\",\"source\":\"main\"," \+ "\"path\":\"" FIX "\"}"+#define ENTRY_OBS \+ "{\"name\":\"kernel-obs\",\"version\":\"2.1.0-rc.1\"," \+ "\"source\":\"direct\",\"path\":\"" OBS "\"}"++/* The state document of the first n entries. */+static const char *const document[] = {+ "{\"schema\":1,\"packages\":[]}",+ "{\"schema\":1,\"packages\":[" ENTRY_FIX "]}",+ "{\"schema\":1,\"packages\":[" ENTRY_FIX "," ENTRY_OBS "]}",+};++void harness(void)+{+ char *buf = any_bytes(LEN);+ size_t len, was, size;+ int n, ret;++ __CPROVER_assume(n >= 0 && n < (int)ARRAY_SIZE(document));+ size = strlen(document[n]);+ was = len;+ ret = bpm_state_emit(entries, n, buf, LEN, &len);+ __CPROVER_assert(ret == (size < LEN ? 0 : -E2BIG),+ "0 exactly when the document fits with its NUL");+ if (ret) {+ __CPROVER_assert(len == was, "no length of a refused document");+ return;+ }+ __CPROVER_assert(len == size && !strcmp(buf, document[n]),+ "the state document, byte for byte");+}diff --git a/testing/proofs/cbmc/bpm-state-entry.c b/testing/proofs/cbmc/bpm-state-entry.cnew file mode 100644index 00000000..6feec51f--- /dev/null+++ b/testing/proofs/cbmc/bpm-state-entry.c@@ -0,0 +1,85 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * on_state_entry() of util/bpm_state.c on any entry with a name and+ * version of up to SHORT_MAX bytes, read into an empty table (LEN 0) or a+ * full one (LEN 1): kept exactly when name, version and source are valid+ * and the path is theirs under the packages directory and fits its buffer,+ * and the next read into a cleared one.+ */++/* util/string.c is included behind a file that includes its header. */+#define STRING_INTERNAL++/*+ * A table entry of PATH_MAX bytes costs CBMC minutes and gigabytes at+ * every copy. The path buffer here is one that some of the paths outgrow;+ * none outgrows the real one, which the last line of this file checks.+ */+#include <limits.h>+enum { SYSTEM_PATH_MAX = PATH_MAX };+#undef PATH_MAX+#define PATH_MAX 40++#include "proof.h"+#include "../../../util/bpm_state.c"+#include "../../../util/bpm_oci.c"+#include "../../../util/string.c"++#define SHORT_MAX 6++static void terminate(char *s, size_t size)+{+ __CPROVER_assume(!s[size - 1]);+}++/* BPM_PACKAGES_DIR, the name and the version, a '/' between them. */+static bool path_of(const struct bpm_state_entry *e)+{+ size_t dir = strlen(BPM_PACKAGES_DIR "/"), name = strlen(e->name);++ return dir + name + 1 + strlen(e->version) < sizeof(e->path) &&+ !memcmp(e->path, BPM_PACKAGES_DIR "/", dir) &&+ !memcmp(e->path + dir, e->name, name) &&+ e->path[dir + name] == '/' &&+ !strcmp(e->path + dir + name + 1, e->version);+}++void harness(void)+{+ struct bpm_state_entry items[1], was;+ struct state_sink s;+ size_t i;+ int ret;++ s.items = items;+ s.n = LEN ? BPM_MAX_STATE : 0;+ terminate(s.scratch.name, SHORT_MAX + 1);+ terminate(s.scratch.version, SHORT_MAX + 1);+ terminate(s.scratch.source, sizeof(s.scratch.source));+ terminate(s.scratch.path, sizeof(s.scratch.path));+ was = s.scratch;+ ret = on_state_entry(&s);+ __CPROVER_assert(!ret == (!LEN && bpm_name_ok(was.name) &&+ bpm_version_ok(was.version) &&+ bpm_name_ok(was.source) && path_of(&was)),+ "a valid entry at its own path, while room");+ if (ret) {+ __CPROVER_assert(ret == (LEN ? -E2BIG : -EINVAL) &&+ s.n == (LEN ? BPM_MAX_STATE : 0),+ "a refused entry is not counted");+ return;+ }+ __CPROVER_assume(i < BPM_MAX_NAME);+ __CPROVER_assert(s.n == 1 && items[0].name[i] == was.name[i] &&+ items[0].version[i] == was.version[i] &&+ items[0].source[i] == was.source[i] &&+ items[0].path[i % PATH_MAX] == was.path[i % PATH_MAX],+ "the entry kept as read");+ __CPROVER_assert(!s.scratch.name[i] && !s.scratch.version[i] &&+ !s.scratch.source[i] && !s.scratch.path[i % PATH_MAX],+ "the next entry read into a cleared one");+}++BUILD_BUG_ON(sizeof(BPM_PACKAGES_DIR) + 2 * BPM_MAX_NAME > SYSTEM_PATH_MAX);diff --git a/testing/proofs/cbmc/conditions-detail.c b/testing/proofs/cbmc/conditions-detail.cnew file mode 100644index 00000000..db2188be--- /dev/null+++ b/testing/proofs/cbmc/conditions-detail.c@@ -0,0 +1,58 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * set_detail() of util/conditions.c, which formats the details that list+ * names, on every text of up to LEN bytes, the long ones behind a filler:+ * the whole text where the detail holds it, its start and "..." where+ * not, and "?" once the text outgrows the buffer it is formatted in.+ */++/* util/string.c is included behind a file that includes its header. */+#define STRING_INTERNAL++#include "proof.h"+#include "../../../util/conditions.c"+#include "../../../util/string.c"++/* The buffer set_detail() formats in. */+#define TEXT_MAX 1024++/*+ * A thousand bytes of any contents cost CBMC minutes. A text that long+ * starts with 832 bytes the format itself holds, which is as good a start+ * to cut as any, and ends in up to 192 of any contents.+ */+#define X16 "xxxxxxxxxxxxxxxx"+#define X64 X16 X16 X16 X16+#define X256 X64 X64 X64 X64+#if LEN > 832+#define FILLER X256 X256 X256 X64+#else+#define FILLER ""+#endif+#define FILLED (sizeof(FILLER) - 1)++void harness(void)+{+ char *names = any_bytes(LEN - FILLED + 1);+ struct condition c;+ size_t len, room;++ names[LEN - FILLED] = '\0';+ len = FILLED + strlen(names);+ room = sizeof(c.detail) - 1;+ set_detail(&c, "ALERT", FILLER "%s", names);+ __CPROVER_assert(!strcmp(c.status, "ALERT"), "the status given");+ if (len <= room)+ __CPROVER_assert(!strcmp(c.detail, names),+ "the whole text where the detail holds it");+ else if (len < TEXT_MAX)+ __CPROVER_assert(!strncmp(c.detail, FILLED ? FILLER : names,+ room - 3) &&+ !strcmp(c.detail + room - 3, "..."),+ "the start of a longer text and three dots");+ else+ __CPROVER_assert(!strcmp(c.detail, "?"),+ "no text that outgrows the formatter");+}diff --git a/testing/proofs/cbmc/conditions-rows.c b/testing/proofs/cbmc/conditions-rows.cnew file mode 100644index 00000000..0019722c--- /dev/null+++ b/testing/proofs/cbmc/conditions-rows.c@@ -0,0 +1,360 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * conditions() of util/conditions.c on every input, into room for LEN rows:+ * the rows of the report in order, each the first state of the reference+ * that applies, the worst first but for the keyring, formatted from that+ * state's numbers and names, and none an ALERT under a verdict that does+ * not say so.+ */++/* util/string.c is included behind a file that includes its header. */+#define STRING_INTERNAL+/* A detail is copied from the literal the input picks. */+#define PROOF_MEMCPY_BYTEWISE++/*+ * The digits and names proof.h's vsnprintf() writes put what follows them+ * at an offset CBMC keeps symbolic, which two rows already run out of+ * memory on. The one below keeps each argument for the proof to compare+ * and writes one byte in its place; conditions-detail has the other.+ */+#include <stdio.h>+#define vsnprintf vsnprintf_in_full+#include "proof.h"+#undef vsnprintf++#include "../../../util/conditions.c"+#include "../../../util/string.c"++/* The largest errno the kernel has: a status code is 0 or one, negated. */+#define ERRNO_MAX 4095++/* What a conversion took: a string and its precision, or a number. */+struct taken {+ const char *string;+ size_t cut;+ __s64 number;+};++/* Two to a text: no detail has more, and a text then knows its own. */+static struct taken taken[CONDITIONS_MAX][2];+static size_t n_texts;++/* What the '#' and the '*' of a detail stand for, in their order. */+struct args {+ __s64 number[2];+ const char *string;+ size_t cut;+};++static const char *const row_type[CONDITIONS_MAX] = {+ "LsmAttached", "KeyringSealed", "ChainIntact", "UpatchesMeasured",+ "TpmMeasured", "ConfigActive", "XdpCovered",+};++static size_t put(char *buf, size_t size, size_t n, char c)+{+ if (n + 1 < size)+ buf[n] = c;+ return n + 1;+}++/*+ * The text of a format, each conversion one byte in it that says where+ * what it took is kept.+ */+int vsnprintf(char *buf, size_t size, const char *fmt, va_list ap)+{+ size_t n = 0, text = n_texts++, i = 0;++ __CPROVER_assert(text < ARRAY_SIZE(taken), "a text for each row");+ for (; *fmt; fmt++) {+ struct taken t = {};++ if (*fmt != '%') {+ n = put(buf, size, n, *fmt);+ continue;+ }+ if (*++fmt == '.')+ for (fmt++; *fmt >= '0' && *fmt <= '9'; fmt++)+ t.cut = t.cut * 10 + (size_t)(*fmt - '0');+ if (*fmt == 's') {+ t.string = va_arg(ap, const char *);+ __CPROVER_assert(t.string, "a string for %s");+ } else if (fmt[0] == 'l' && fmt[1] == 'l') {+ t.number = va_arg(ap, __s64);+ fmt += 2;+ } else {+ t.number = va_arg(ap, int);+ }+ __CPROVER_assert((t.string || (*fmt == 'd' && !t.cut)) &&+ i < ARRAY_SIZE(taken[text]),+ "a conversion this vsnprintf() has");+ taken[text][i++] = t;+ n = put(buf, size, n, (char)(2 * text + i));+ }+ if (size)+ buf[min(n, size - 1)] = '\0';+ return (int)n;+}++/* A text of its own for every error, where CBMC's has one for all. */+char *strerror(int errnum)+{+ static char text[ERRNO_MAX + 1];++ __CPROVER_assert(errnum > 0 && errnum <= ERRNO_MAX, "an errno");+ return text + errnum;+}++/*+ * Whether s is the detail, its '#' and '*' what took the args given. A+ * string is told by its address: every name and error has its own, and+ * CBMC gives equal literals one, without which this would fail, not hold.+ */+static bool spells(const char *s, const char *detail, const struct args *args)+{+ size_t numbers = 0;++ for (; *detail; detail++, s++) {+ const struct taken *t;++ if (*detail != '#' && *detail != '*') {+ if (*s != *detail)+ return false;+ continue;+ }+ if (!*s || *s > 2 * n_texts)+ return false;+ t = &taken[(*s - 1) / 2][(*s - 1) % 2];+ if (*detail == '#') {+ if (t->string || t->number != args->number[numbers++])+ return false;+ } else if (t->string != args->string || t->cut != args->cut) {+ return false;+ }+ }+ return !*s;+}++static bool row_is(const struct condition *c, const char *status,+ const char *detail, struct args args)+{+ return !strcmp(c->status, status) && spells(c->detail, detail, &args);+}++#define OK(detail, ...) row_is(c, "OK", detail, (struct args){ __VA_ARGS__ })+#define WARN(detail, ...) row_is(c, "WARN", detail, (struct args){ __VA_ARGS__ })+#define ALERT(detail, ...) row_is(c, "ALERT", detail, (struct args){ __VA_ARGS__ })++static const char *or_unknown(const char *names)+{+ return names ? names : "?";+}++static bool lsm_row(const struct conditions_input *in,+ const struct condition *c)+{+ if (!in->lsm_attached)+ return ALERT("lsm/bpf link absent, enforcement off");+ return OK("lsm/bpf link present and pinned");+}++static bool keyring_row(const struct conditions_input *in,+ const struct condition *c)+{+ if (in->ksp_seen && !in->keyring_present)+ return ALERT("pinned serial #, the .bpf keyring is gone",+ .number = { in->keyring_serial_pinned });+ if (!in->keyring_present)+ return WARN(".bpf keyring absent before init");+ if (in->ksm_seen && !in->keyring_serial_matches)+ return ALERT("pinned serial #, live serial #: swapped",+ .number = { in->keyring_serial_pinned,+ in->keyring_serial });+ if (in->bpf_unfrozen)+ return ALERT("the BPF keyring holds # link* init did not freeze",+ .number = { in->bpf_unfrozen },+ .string = in->bpf_unfrozen == 1 ? "" : "s");+ if (in->dmvp_seen && !in->dmv_present)+ return ALERT("dm-verity pinned serial #, the dm-verity keyring is gone",+ .number = { in->dmv_serial_pinned });+ if (in->dmvm_seen && !in->dmv_serial_matches)+ return ALERT("dm-verity pinned serial #, live serial #: swapped",+ .number = { in->dmv_serial_pinned, in->dmv_serial });+ if (in->dmv_unfrozen)+ return ALERT("the dm-verity keyring holds # link* init did not freeze",+ .number = { in->dmv_unfrozen },+ .string = in->dmv_unfrozen == 1 ? "" : "s");+ if (in->dmv_key_missing)+ return ALERT("a config key init froze is gone from the dm-verity keyring");+ if (in->ksp_seen && in->dmvp_seen)+ return OK("bpf serial #, dm-verity serial #, pins match",+ .number = { in->keyring_serial, in->dmv_serial });+ return OK("bpf serial #, *", .number = { in->keyring_serial },+ .string = in->ksp_seen ? "pin matches" : "no pin recorded");+}++static bool chain_row(const struct conditions_input *in,+ const struct condition *c)+{+ if (in->cm_seen && !in->chain_matches)+ return ALERT("live register differs from the eventlog replay");+ if (!in->chain_seen && !in->tpm_reachable)+ return WARN("no TPM answered, no register to replay");+ if (!in->chain_seen)+ return WARN("no register bound this boot (init incomplete)");+ if (!in->cm_seen)+ return WARN("chain present, replay not evaluated");+ return OK("live register equals the eventlog replay");+}++static bool upatches_row(const struct conditions_input *in,+ const struct condition *c)+{+ if (in->xdp_tampered)+ return ALERT("# XDP archives differ from the loaded payload: *",+ .number = { in->xdp_tampered },+ .string = or_unknown(in->xdp_tampered_detail));+ if (in->links_lost)+ return ALERT("# upatches are missing a link or their archive: *",+ .number = { in->links_lost },+ .string = or_unknown(in->links_lost_detail));+ if (in->pins_unmeasured)+ return ALERT("# pinned or archived without a record of this boot: *",+ .number = { in->pins_unmeasured },+ .string = or_unknown(in->pins_detail));+ if (in->pins_rc == -ENOENT)+ return WARN("no record of this boot to check the pins against");+ if (in->pins_rc)+ return WARN("pins not checked against the records: *",+ .string = strerror(-in->pins_rc));+ if (in->checks_rc)+ return WARN("links or archives not checked: *",+ .string = strerror(-in->checks_rc));+ return OK("every pinned or archived upatch is in this boot's records");+}++static bool tpm_row(const struct conditions_input *in,+ const struct condition *c)+{+ if (in->chain_seen)+ return OK("NV index * readable",+ .string = or_unknown(in->nv_index));+ if (in->tpm_reachable)+ return WARN("NV index * not bound this boot",+ .string = or_unknown(in->nv_index));+ return WARN("no TPM answered");+}++static bool config_row(const struct conditions_input *in,+ const struct condition *c)+{+ if (!in->config_seen)+ return WARN("no config activation state before init");+ if (in->config_state == -EINPROGRESS)+ return WARN("the activation of config *.. is pending until the next reboot",+ .string = or_unknown(in->config_root), .cut = 12);+ if (in->config_state == -ESTALE)+ return ALERT("the config activation record does not belong to this TPM boot");+ if (in->config_state == -EKEYREJECTED)+ return ALERT("the mounted config does not match the selected root");+ if (in->config_state)+ return ALERT("the config PCR, activation records and mounted config disagree");+ return OK("config *.. is active",+ .string = or_unknown(in->config_root), .cut = 12);+}++static bool xdp_row(const struct conditions_input *in,+ const struct condition *c)+{+ if (in->xdp_uncovered)+ return WARN("# selected devices run without their XDP upatch: *",+ .number = { in->xdp_uncovered },+ .string = or_unknown(in->xdp_uncovered_detail));+ if (in->xdp_coverage_rc == -EXDEV)+ return WARN("XDP coverage not checked: *",+ .string = "not in the initial network namespace");+ if (in->xdp_coverage_rc)+ return WARN("XDP coverage not checked: *",+ .string = strerror(-in->xdp_coverage_rc));+ return OK("every device an XDP upatch selects runs it");+}++static bool (*const row_holds[CONDITIONS_MAX])(const struct conditions_input *,+ const struct condition *) = {+ lsm_row, keyring_row, chain_row, upatches_row, tpm_row, config_row,+ xdp_row,+};++/* conditions() hands a name on and reads none: its address is all of it. */+static const char *name_or_none(void)+{+ bool named;++ return named ? any_bytes(1) : NULL;+}++static bool status_code(int rc)+{+ return rc <= 0 && rc >= -ERRNO_MAX;+}++/* The LSM's row alerts under lsm-off too. */+static bool verdict_says(const char *verdict, size_t row)+{+ return !strcmp(verdict, "unreachable") ||+ !strcmp(verdict, "unparseable") ||+ !strcmp(verdict, "tampered") ||+ (!row && !strcmp(verdict, "lsm-off"));+}++void harness(void)+{+ struct condition c[LEN ? LEN : 1], spare;+ size_t rows = min(LEN, CONDITIONS_MAX);+ struct conditions_input in;+ const char *verdict;+ size_t n, i, k;++ in.nv_index = name_or_none();+ in.config_root = name_or_none();+ in.pins_detail = name_or_none();+ in.xdp_tampered_detail = name_or_none();+ in.links_lost_detail = name_or_none();+ in.xdp_uncovered_detail = name_or_none();+ __CPROVER_assume(status_code(in.pins_rc) && status_code(in.checks_rc) &&+ status_code(in.xdp_coverage_rc));+ __CPROVER_assume(in.chain_seen || !in.cm_seen);+ __CPROVER_assume(!in.pins_rc || !in.pins_unmeasured);+ verdict = status_verdict(&in);++ if (rows < ARRAY_SIZE(c))+ spare = c[rows];+ n = conditions(&in, c, LEN);+ __CPROVER_assert(n == rows,+ "as many rows as there is room for, seven at most");+ for (i = 0; i < n; i++)+ __CPROVER_assert(!strcmp(c[i].type, row_type[i]),+ "the rows in the order of the report");+ __CPROVER_assume(k < sizeof(spare.detail));+ if (rows < ARRAY_SIZE(c))+ __CPROVER_assert(c[rows].type == spare.type &&+ c[rows].status == spare.status &&+ c[rows].detail[k] == spare.detail[k],+ "nothing written behind the last row");+ conditions_print(c, n);++ /* Less room changes which rows are written, not what one says. */+ if (n < CONDITIONS_MAX)+ return;+ for (i = 0; i < n; i++) {+ __CPROVER_assert(row_holds[i](&in, &c[i]),+ "the first state of the row that applies");+ __CPROVER_assert(strcmp(c[i].status, "ALERT") ||+ verdict_says(verdict, i),+ "no ALERT under a verdict that does not say so");+ }+}diff --git a/testing/proofs/cbmc/conditions-verdict.c b/testing/proofs/cbmc/conditions-verdict.cnew file mode 100644index 00000000..39ce4f61--- /dev/null+++ b/testing/proofs/cbmc/conditions-verdict.c@@ -0,0 +1,200 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * status_verdict() and status_reason() of util/conditions.c on every input:+ * the verdict is the first of its order that applies, tampered exactly when+ * a cause holds, whatever else the input says, and the reason is that of+ * the first cause in the documented order, cut to a buffer of LEN bytes.+ */++/* util/string.c is included behind a file that includes its header. */+#define STRING_INTERNAL+/* A reason is copied from the literal the input picks. */+#define PROOF_MEMCPY_BYTEWISE++#include "proof.h"+#include "../../../util/conditions.c"+#include "../../../util/string.c"++#define UNREACH_MAX 9++enum verdict {+ UNREACHABLE,+ UNPARSEABLE,+ TAMPERED,+ LSM_OFF,+ INIT_PENDING,+ REBOOT_PENDING,+ DEGRADED,+ OK,+};++static const char *const verdict_word[] = {+ [UNREACHABLE] = "unreachable",+ [UNPARSEABLE] = "unparseable",+ [TAMPERED] = "tampered",+ [LSM_OFF] = "lsm-off",+ [INIT_PENDING] = "init-pending",+ [REBOOT_PENDING] = "reboot-pending",+ [DEGRADED] = "degraded",+ [OK] = "ok",+};++static const char *const verdict_reason[] = {+ [UNPARSEABLE] = "status JSON unparseable",+ [LSM_OFF] = "LSM not attached, enforcement off",+ [INIT_PENDING] = ".bpf keyring absent before init",+ [REBOOT_PENDING] = "a config activation is pending until the next reboot",+ [OK] = "",+};++/* What makes a host tampered, in the order its reason is taken from. */+enum cause {+ REGISTER_DIFFERS,+ XDP_ARCHIVE_CHANGED,+ LINK_LOST,+ PIN_UNMEASURED,+ CONFIG_UNVERIFIED,+ BPF_KEYRING_GONE,+ BPF_KEYRING_SWAPPED,+ DMV_KEYRING_GONE,+ DMV_KEYRING_SWAPPED,+ LINK_UNFROZEN,+ CONFIG_KEY_GONE,+ NR_CAUSES,+};++static const char *const cause_reason[NR_CAUSES] = {+ [REGISTER_DIFFERS] = "live register differs from the eventlog replay",+ [XDP_ARCHIVE_CHANGED] = "the archive of a loaded XDP upatch differs from what was loaded",+ [LINK_LOST] = "a loaded upatch is missing a link or its archive",+ [PIN_UNMEASURED] = "a upatch is pinned or archived without a record of this boot",+ [CONFIG_UNVERIFIED] = "the selected config cannot be verified against its records",+ [BPF_KEYRING_GONE] = "the keyring init froze is gone",+ [BPF_KEYRING_SWAPPED] = "keyring serial swap detected",+ [DMV_KEYRING_GONE] = "the dm-verity keyring init froze is gone",+ [DMV_KEYRING_SWAPPED] = "dm-verity keyring serial swap detected",+ [LINK_UNFROZEN] = "a keyring holds a link init did not freeze",+ [CONFIG_KEY_GONE] = "the config key init froze is gone",+};++static bool cause_holds(const struct conditions_input *in, enum cause cause)+{+ switch (cause) {+ case REGISTER_DIFFERS:+ return in->cm_seen && !in->chain_matches;+ case XDP_ARCHIVE_CHANGED:+ return in->xdp_tampered;+ case LINK_LOST:+ return in->links_lost;+ case PIN_UNMEASURED:+ return in->pins_unmeasured;+ case CONFIG_UNVERIFIED:+ return in->config_seen && in->config_state &&+ in->config_state != -EINPROGRESS;+ case BPF_KEYRING_GONE:+ return in->ksp_seen && !in->keyring_present;+ case BPF_KEYRING_SWAPPED:+ return in->ksm_seen && !in->keyring_serial_matches;+ case DMV_KEYRING_GONE:+ return in->dmvp_seen && !in->dmv_present;+ case DMV_KEYRING_SWAPPED:+ return in->dmvm_seen && !in->dmv_serial_matches;+ case LINK_UNFROZEN:+ return in->bpf_unfrozen || in->dmv_unfrozen;+ case CONFIG_KEY_GONE:+ return in->dmv_key_missing;+ default:+ return false;+ }+}++/* NR_CAUSES when none holds. */+static enum cause first_cause(const struct conditions_input *in)+{+ enum cause cause;++ for (cause = 0; cause < NR_CAUSES; cause++)+ if (cause_holds(in, cause))+ break;+ return cause;+}++static bool verdict_applies(const struct conditions_input *in, enum verdict v)+{+ switch (v) {+ case UNREACHABLE:+ return !in->reachable;+ case UNPARSEABLE:+ return !in->parsed;+ case TAMPERED:+ return first_cause(in) != NR_CAUSES;+ case LSM_OFF:+ return !in->lsm_attached;+ case INIT_PENDING:+ return !in->keyring_present;+ case REBOOT_PENDING:+ return in->config_seen && in->config_state == -EINPROGRESS;+ case DEGRADED:+ return !in->chain_seen || in->xdp_uncovered;+ default:+ return true;+ }+}++static const char *reason_of(const struct conditions_input *in, enum verdict v)+{+ switch (v) {+ case UNREACHABLE:+ return in->unreach ? in->unreach : "";+ case TAMPERED:+ return cause_reason[first_cause(in)];+ case DEGRADED:+ if (in->chain_seen)+ return "a device an XDP upatch selects does not run it";+ return in->tpm_reachable ? "no register bound this boot" :+ "no TPM answered";+ default:+ return verdict_reason[v];+ }+}++/* The text, or as much of it as size bytes hold with their NUL. */+static bool cut_to(const char *buf, const char *text, size_t size)+{+ size_t n = min(strlen(text), size - 1);++ return !strncmp(buf, text, n) && !buf[n];+}++void harness(void)+{+ struct conditions_input in;+ const char *verdict;+ enum verdict v = 0;+ char *reason;+ bool said;++ if (said) {+ char *unreach = any_bytes(UNREACH_MAX + 1);++ __CPROVER_assume(!unreach[UNREACH_MAX]);+ in.unreach = unreach;+ } else {+ in.unreach = NULL;+ }+ verdict = status_verdict(&in);+ while (!verdict_applies(&in, v))+ v++;+ __CPROVER_assert(!strcmp(verdict, verdict_word[v]),+ "the first verdict of the order that applies");+ __CPROVER_assert(!strcmp(verdict, "tampered") ==+ (in.reachable && in.parsed &&+ first_cause(&in) != NR_CAUSES),+ "tampered exactly when the host answered and a cause holds");+ reason = any_bytes(LEN);+ status_reason(&in, reason, LEN);+ __CPROVER_assert(cut_to(reason, reason_of(&in, v), LEN),+ "the reason of the verdict and of the first cause");+}diff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.listindex 08cf41c3..2d561e80 100644--- a/testing/proofs/cbmc/proofs.list+++ b/testing/proofs/cbmc/proofs.list@@ -120,3 +120,8 @@ bpm-beefile-members --unwind 11 LEN=0-6bpm-index-writer --unwind 26 --unwindset strlen.0:64,strnlen.0:64,memcpy.0:64,put_string_n.1:64 --max-field-sensitivity-array-size 512 LEN=0-3bpm-index-append --unwind 26 --unwindset strlen.0:260,strcmp.0:64,strnlen.0:64,memcpy.0:64,put_string_n.1:64 --max-field-sensitivity-array-size 512 LEN=0-2bpm-tree-writer --unwind 80 --max-field-sensitivity-array-size 512 LEN=0-4+conditions-verdict --unwind 66 LEN=1,8,32,160+conditions-rows --object-bits 12 --max-field-sensitivity-array-size 192 --unwind 1 --unwindset vsnprintf.0:3,vsnprintf.1:67,memcpy.0:65,strnlen.0:65,strcmp.0:18,spells.0:65,harness.0:8,harness.1:8,conditions_print.0:8 LEN=0-8+conditions-detail --object-bits 12 --unwind 195 --unwindset vsnprintf.2:836 LEN=193,1024+bpm-state-entry --unwind 1 --unwindset vsnprintf.1:31,vsnprintf.2:7,strcmp.0:42,memcmp.0:32,strlen.0:130,bpm_component_ok.0:130 LEN=0,1+bpm-state-emit --object-bits 12 --unwind 1 --unwindset bpm_state_emit.0:3,put_string_n.1:54,put_raw.0:3,vsnprintf.2:3,format_number.0:3,format_number.3:3,strlen.0:250,strcmp.0:250 LEN=0,26,27,123,124,247,248