eBPF-based security platform
testing: prove the TPM structures, config readers and kernel notes
Daniel Borkmann9dbbcb0fd328
diff --git a/.agents/skills/proofs/SKILL.md b/.agents/skills/proofs/SKILL.mdindex 7803dd4c..39c7da2d 100644--- a/.agents/skills/proofs/SKILL.md+++ b/.agents/skills/proofs/SKILL.md@@ -45,8 +45,8 @@ to wait for.## Keep it fast- A length CBMC keeps symbolic makes every access to the buffer costly.- Run the proof once per length instead: `LEN=0-6` or `LEN=16,64` in- `proofs.list`, with the harness using `LEN`.+ Run the proof once per length instead: `LEN=0-6`, `LEN=16,64` or+ `LEN=0-24,28,32` in `proofs.list`, with the harness using `LEN`.- A global `--unwind` unrolls every loop that far, including those whosecount comes from the data. Give each loop its bound with`--unwindset <function>.<n>:<bound>` (`goto-cc --function harness ... -o@@ -61,6 +61,12 @@ to wait for.- `too many addressed objects` asks for `--object-bits 12`.- Division and remainder cost the SAT solver most: `--refine-arithmetic`took one proof from 360 s to 21 s, though it slows others. Time both.+- Code that reads a file goes through the `PROOF_FILE` models of+ `proof.h` (`getline()`, `pread()`, and an `errno` that keeps its value:+ CBMC's own changes at every read, so an `EINTR` retry loop never ends).+ `any_input(len, max)` covers inputs of every length for code that reads+ at most `max` bytes. A `pread()` model that may fail made one proof take+ 18 GB; let it succeed and prove the failure paths apart.- A `memchr()` over thousands of bytes unrolls slowly. Where the calleronly asks whether the byte is there and `n` is a constant, define`PROOF_MEMCHR_QUANTIFIED` before including `proof.h` for a quantifieddiff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rstindex 35b0a8c3..83c26572 100644--- a/Documentation/contribute/testing/proof-coverage.rst+++ b/Documentation/contribute/testing/proof-coverage.rst@@ -188,6 +188,105 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.- Every encoding and every value of up to 8 bytes- The bits stay inside the integer's bytes; the value is zero- orsign-extended from them+ * - ``tpm-reader``+ - The TPM reader primitives of :src:`util/tpm_wire.c`: ``get8()``+ to ``get64()``, ``get2b()``, ``get2b_fixed()``, ``get_name()``+ - Every reader state over an input of any size+ - Every read stays inside what is left, and what is left still ends+ at the input's end; a sized field lies inside the input and its+ bound; a name is at most 34 bytes+ * - ``tpm-signature``+ - ``tpm_ecdsa_signature_parse()``+ - Every input+ - Accepts exactly ECDSA over SHA-256 with 32-byte r and s, in 72+ bytes, and returns those bytes+ * - ``tpm-attest``+ - ``tpm_attest_parse()``+ - Every input; takes about 8 minutes+ - Starts with ``TPM_GENERATED``; names fit 34 bytes; extra data of+ at most 64 bytes inside the input; a quote spans the input with a+ 24-bit PCR mask; an NV certification ends with its data, at most+ 2048 bytes+ * - ``tpm-public``+ - ``tpm_public_parse()``, ``tpm_ecc_public_parse()``+ - Every input; takes about 5 minutes and 7 GB+ - Accepts only the key shapes shirudo uses, P-256 or RSA-2048 with a+ 256-byte modulus, each spanning exactly the input+ * - ``tpm-digest``+ - The policy, name and cpHash digests of :src:`util/tpm_wire.c`,+ ``tpm_writer_auth_hash()``+ - Names of any length, SHA-256 as any result+ - Each reads only whole inputs and writes only its output; an object+ name has a public area of 4 bytes or more; a writer's+ authorization takes a nonce of 1 to 64 bytes+ * - ``tpm-kdf``+ - ``tpm_kdfa()``, ``tpm_kdfe()``, ``tpm_credential_protect()``+ - Labels up to 66 bytes, outputs up to 64, any context+ - Reads and writes only what it is given; an oversized context or+ party is refused+ * - ``attest-fields``+ - ``record_word()``+ - Up to 16 characters+ - Passes exactly ``[a-z_]``+ * - ``attest-pcr``+ - ``on_pcr()`` of the evidence parser+ - Every index and 64-byte value text+ - Stores only PCRs 0 to 23 and marks exactly that one, from 64 hex+ digits; a refusal marks nothing+ * - ``eventlog-records``+ - ``log_records()``+ - Logs of 0 to 32 bytes+ - Refuses exactly a log with a NUL; keeps whole lines unchanged up+ to the last newline, cutting only a torn last line+ * - ``config-image``+ - ``config_image_read_header()``+ - Hash files of 0, 511, 512 and 4096 bytes+ - Accepts exactly the version 1 SHA-256 layout with 4096-byte blocks,+ a 32-byte salt and 1 to 2^28 data blocks; the hash tree's size+ by dm-verity's own formula; nothing changed on refusal+ * - ``config-auth``+ - ``set_owner_auth()``+ - Values of 0 to 16 bytes+ - A passphrase as it stands, ``hex:`` digits as the bytes they spell;+ a refusal keeps the value before+ * - ``config-hosts``+ - ``tls_classical_hosts_valid()``+ - Values of 0 to 7 bytes+ - Empty, or DNS names apart by commas+ * - ``image-bootstrap``+ - ``bootstrap_parse()`` with ``read_text_line()``+ - One settings line of 0 to 8 bytes, every earlier state, failed+ reads and allocations+ - A certificate under the runtime directory; a PCR only one the+ config accepts; nothing changes the runtime outside the initial+ settings+ * - ``image-path``+ - ``image_path_valid()``+ - Up to 8 bytes+ - An accepted path is absolute, not ``/``, with no ``..`` component+ and no empty one but the last; every absolute path of plain+ components is accepted+ * - ``dm-names``+ - ``dm_name_ok()``, ``dm_device()``+ - Up to 7 bytes+ - A name of 1 to 127 of ``[A-Za-z0-9._-]``, not ``.`` or ``..``; a+ device as decimal major and minor+ * - ``kernel-notes``+ - ``kernel_build_id_from_notes()``+ - Notes of 0 to 24, 28, 32, 36 and 40 bytes+ - Reads nothing past the notes; a build ID of 1 to 64 lowercase hex+ digits that is the first GNU build ID note's, or empty+ * - ``kernel-release``+ - ``kernel_release_prefix_matches()``+ - Releases and prefixes up to 8 bytes+ - Matches exactly where the prefix ends with the release or before a+ ``.`` or ``-``+ * - ``log-escape``+ - ``escape()`` of :src:`util/log.c`+ - Messages of 0 to 5 bytes, every buffer size+ - Terminated printable ASCII, every backslash a ``\\`` or three octal+ digits, decoding to the message or to a prefix cut between+ charactersLean 4 proofs-------------@@ -222,9 +321,13 @@ Not covered yetThe fuzz targets in :doc:`test-coverage` cover these parsers; no proofdoes: the JSON parser and emitter, the package archive, index and-manifest readers, the tree publication and log, the eventlog, the TPM-structures of evidence, the configuration files and images, and the-kernel notes and configuration.+manifest readers, the tree publication and log, the eventlog's lines,+the evidence document beyond its PCRs, the config update record, the+site configuration file, the config image's settings and tree, and the+kernel configuration. The ``.upatch`` reader and the TPM structures are+covered part by part, as the table shows; what joins those parts, the+TPM policy that takes a PCR selection and the event encoders of the+register, which need a model of SHA-256, have no proof.Within the ``.upatch`` reader, the proofs cover its parts but not theircomposition: ``upatch_parse()`` and ``upatch_open_memory()`` join thediff --git a/Documentation/contribute/testing/proofs.rst b/Documentation/contribute/testing/proofs.rstindex c0dc59cb..fb68a4c9 100644--- a/Documentation/contribute/testing/proofs.rst+++ b/Documentation/contribute/testing/proofs.rst@@ -85,10 +85,12 @@ Write a CBMC proof1. Write ``testing/proofs/cbmc/<name>.c``. It includes ``proof.h`` andthe source file it proves, so that it reaches static functions too, anddefines ``harness()``, which CBMC runs with every input at once.- ``any_bytes(n)`` gives ``n`` bytes of any contents, and a variable- left uninitialized takes any value; ``__CPROVER_assume()`` narrows the- inputs to the bound, and ``__CPROVER_assert()`` states each property- with a message that says what holds.+ ``any_bytes(n)`` gives ``n`` bytes of any contents, ``any_input(len,+ max)`` an input of any length for code that reads at most ``max``+ bytes of it, and a variable left uninitialized takes any value;+ ``__CPROVER_assume()`` narrows the inputs to the bound, and+ ``__CPROVER_assert()`` states each property with a message that says+ what holds.2. State what the code must do from its contract, not from its code: anaccepted string is a number of the right digits, an accepted file has@@ -102,9 +104,10 @@ Write a CBMC proofwith ``goto-instrument --show-loops`` naming them. The unwindingassertions ``run.sh`` adds fail when an input needs more iterations,so a bound too low fails the proof rather than weakening it.- - ``LEN=<a>-<b>`` runs the proof once for each length, with ``LEN``- defined: CBMC takes far longer over an input whose length it has to- keep symbolic.+ - ``LEN=<a>-<b>``, or a list of lengths and ranges such as+ ``LEN=0-24,28,32``, runs the proof once for each length, with+ ``LEN`` defined: CBMC takes far longer over an input whose length it+ has to keep symbolic.- ``--object-bits <n>`` when CBMC reports too many addressed objects.4. Check that the proof can fail: break the code it proves, for instance@@ -122,7 +125,11 @@ the code meets; a harness that copies from such a pointer defines``PROOF_MEMCPY_BYTEWISE`` before it includes ``proof.h``. A harness whosecode searches thousands of bytes with ``memchr()`` only to learn whethera byte is there defines ``PROOF_MEMCHR_QUANTIFIED`` for a model that-answers without a loop.+answers without a loop. ``PROOF_FILE`` models a file of that many bytes+that ``getline()`` and ``pread()`` read, and an ``errno`` that keeps what+was stored, where CBMC's own takes any value and lets a loop that+retries on ``EINTR`` run for ever; ``PROOF_DEVICE_NUMBERS`` gives+``makedev()``, ``major()`` and ``minor()`` bodies.Write a Lean 4 proof--------------------diff --git a/testing/proofs/cbmc/attest-fields.c b/testing/proofs/cbmc/attest-fields.cnew file mode 100644index 00000000..5c7cd657--- /dev/null+++ b/testing/proofs/cbmc/attest-fields.c@@ -0,0 +1,37 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * record_word() of util/attest.c, for every value of up to LEN characters+ * of a target's evidence: it passes only the [a-z_] words a record's type+ * may hold, which the report prints. The evidence's hex fields go through+ * hex_decode_max(), which the hex proof covers.+ */++#include "proof.h"+#include "../../../util/hex.c"+#include "../../../util/attest.c"++static char *any_string(size_t cap)+{+ char *s = any_bytes(cap);++ s[cap - 1] = '\0';+ return s;+}++static void word(void)+{+ char *s = any_string(LEN + 1);+ bool lower = true;+ size_t i;++ for (i = 0; s[i]; i++)+ lower = lower && ((s[i] >= 'a' && s[i] <= 'z') || s[i] == '_');+ __CPROVER_assert(record_word(s) == lower, "[a-z_] and nothing else");+}++void harness(void)+{+ word();+}diff --git a/testing/proofs/cbmc/attest-pcr.c b/testing/proofs/cbmc/attest-pcr.cnew file mode 100644index 00000000..84cff844--- /dev/null+++ b/testing/proofs/cbmc/attest-pcr.c@@ -0,0 +1,43 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * on_pcr() of util/attest.c, which stores each PCR value of a target's+ * evidence, for every index and value text: it stores only a 64-digit hex+ * value under one of the 24 PCRs and marks that one, and a refusal marks+ * nothing.+ */++#include "proof.h"+#include "../../../util/hex.c"+#include "../../../util/attest.c"++static struct parse p;++static char any_char(void)+{+ char c;++ return c;+}++void harness(void)+{+ __u32 had;+ __s64 index;+ size_t i;++ p.pcr_have = had;+ p.pcr_index = index;+ for (i = 0; i + 1 < sizeof(p.pcr_value_hex); i++)+ p.pcr_value_hex[i] = any_char();+ if (on_pcr(&p)) {+ __CPROVER_assert(p.pcr_have == had, "a refusal marks nothing");+ return;+ }+ __CPROVER_assert(index >= 0 && index < 24 &&+ p.pcr_have == (had | 1u << index),+ "one of the 24 PCRs marked");+ __CPROVER_assert(strlen(p.pcr_value_hex) == 64,+ "a SHA-256 value in hex");+}diff --git a/testing/proofs/cbmc/config-auth.c b/testing/proofs/cbmc/config-auth.cnew file mode 100644index 00000000..db42dc0a--- /dev/null+++ b/testing/proofs/cbmc/config-auth.c@@ -0,0 +1,71 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * set_owner_auth() of util/config.c, the shirudo.conf reader, on every+ * tpm.owner_auth value of up to LEN bytes and any earlier value: it stores+ * the bytes an even run of hex digits after "hex:" spells, or the text+ * itself, and a refusal leaves the earlier value intact.+ */++#define PROOF_MEMCPY_BYTEWISE+#include "proof.h"+#include "../../../util/string.c"+#include "../../../util/hex.c"+#include "../../../util/config.c"++static struct config cfg;++void pr_warn(const char *fmt, ...)+{+}++static char *any_string(void)+{+ char *s = any_bytes(LEN + 1);++ s[LEN] = '\0';+ return s;+}++static bool hex_run(const char *s, size_t n)+{+ size_t i;++ for (i = 0; i < n; i++)+ if (hex_nibble(s[i]) < 0)+ return false;+ return !(n % 2);+}++void harness(void)+{+ char *val = any_string(), before[LEN + 1];+ size_t had, n, i;++ __CPROVER_assume(had <= sizeof(cfg.tpm_owner_auth));+ cfg.tpm_owner_auth_len = had;+ for (i = 0; i < LEN; i++)+ before[i] = cfg.tpm_owner_auth[i];+ n = strlen(val);+ set_owner_auth(&cfg, val);+ if (strncmp(val, "hex:", 4)) {+ __CPROVER_assert(cfg.tpm_owner_auth_len == n &&+ !memneq_explicit(cfg.tpm_owner_auth, val, n),+ "a passphrase as it stands");+ return;+ }+ if (!hex_run(val + 4, n - 4)) {+ __CPROVER_assert(cfg.tpm_owner_auth_len == had &&+ !memneq_explicit(cfg.tpm_owner_auth, before, LEN),+ "a refusal leaves the earlier value");+ return;+ }+ __CPROVER_assert(cfg.tpm_owner_auth_len == (n - 4) / 2,+ "the bytes the hex digits spell");+ for (i = 0; i < (n - 4) / 2; i++)+ __CPROVER_assert((__u8)cfg.tpm_owner_auth[i] ==+ (hex_nibble(val[4 + 2 * i]) << 4 |+ hex_nibble(val[5 + 2 * i])),+ "each one");+}diff --git a/testing/proofs/cbmc/config-hosts.c b/testing/proofs/cbmc/config-hosts.cnew file mode 100644index 00000000..89dea178--- /dev/null+++ b/testing/proofs/cbmc/config-hosts.c@@ -0,0 +1,44 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * tls_classical_hosts_valid() of util/config.c, the shirudo.conf reader, on+ * every tls.classical_hosts value of up to LEN bytes: what it passes is+ * empty or a comma-separated list of names dns_name_valid() takes.+ */++#define PROOF_MEMCPY_BYTEWISE+#include "proof.h"+#include "../../../util/string.c"+#include "../../../util/hex.c"+#include "../../../util/config.c"++void pr_warn(const char *fmt, ...)+{+}++static char *any_string(void)+{+ char *s = any_bytes(LEN + 1);++ s[LEN] = '\0';+ return s;+}++void harness(void)+{+ char *val = any_string();+ size_t start, i;++ if (!tls_classical_hosts_valid(val) || !*val)+ return;+ for (start = 0; ; start = i + 1) {+ char name[LEN + 1] = {};++ for (i = start; val[i] && val[i] != ','; i++)+ name[i - start] = val[i];+ __CPROVER_assert(dns_name_valid(name), "DNS names");+ if (!val[i])+ break;+ }+}diff --git a/testing/proofs/cbmc/config-image.c b/testing/proofs/cbmc/config-image.cnew file mode 100644index 00000000..328668b9--- /dev/null+++ b/testing/proofs/cbmc/config-image.c@@ -0,0 +1,66 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * config_image_read_header() of util/config_image.c, the target's preflight+ * of a config image's dm-verity superblock, on every hash file of LEN bytes:+ * it accepts exactly the version 1 SHA-256 layout of 4096-byte blocks and+ * 32-byte salt over 1 to 2^28 data blocks, and returns the size of the+ * hash tree dm-verity reads for them.+ */++#define PROOF_FILE LEN+#include "proof.h"+#include "../../../util/config_image.c"++static __u32 le32_at(size_t off)+{+ return get_le32(proof_file + off);+}++static bool superblock_valid(void)+{+ static const char sha256[32] = "sha256";++ return !memcmp(proof_file, "verity\0\0", 8) && le32_at(8) == 1 &&+ le32_at(12) == 1 && !memcmp(proof_file + 32, sha256, 32) &&+ le32_at(64) == 4096 && le32_at(68) == 4096 &&+ get_le16(proof_file + 80) == 32;+}++/* The superblock and the levels dm-verity's constructor lays out. */+static __u64 tree_blocks(__u64 n)+{+ __u64 total = 1;+ __u32 levels = 0, i;++ while ((n - 1) >> (7 * levels))+ levels++;+ for (i = 1; i <= levels; i++)+ total += (n + (1ull << (7 * i)) - 1) >> (7 * i);+ return total;+}++void harness(void)+{+ struct config_image image, was;+ __u64 blocks;+ size_t i;++ proof_file_open();+ was = image;+ if (config_image_read_header(3, &image)) {+ __CPROVER_assert(!memcmp(&image, &was, sizeof(image)),+ "a refusal leaves the image as it was");+ return;+ }+ blocks = get_le64(proof_file + 72);+ __CPROVER_assert(superblock_valid(), "the one superblock layout");+ __CPROVER_assert(image.data_blocks == blocks && blocks &&+ blocks <= 1u << 28, "1 to 2^28 data blocks");+ __CPROVER_assert(image.hash_blocks == tree_blocks(blocks),+ "the hash tree dm-verity reads");+ for (i = 0; i < CONFIG_IMAGE_SALT_SIZE; i++)+ __CPROVER_assert(image.salt[i] == proof_file[88 + i],+ "the salt it records");+}diff --git a/testing/proofs/cbmc/dm-names.c b/testing/proofs/cbmc/dm-names.cnew file mode 100644index 00000000..cf152814--- /dev/null+++ b/testing/proofs/cbmc/dm-names.c@@ -0,0 +1,65 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * dm_name_ok() and dm_device() of util/dm_verity.c on every string of up to+ * LEN_MAX bytes: a device-mapper name is 1 to 127 of [A-Za-z0-9._-] and not+ * "." or "..", and a device is two decimal numbers around a ':' that the+ * device number holds unchanged.+ */++#define PROOF_DEVICE_NUMBERS+#include "proof.h"+#include "../../../util/string.c"+#include "../../../util/dm_verity.c"++#define LEN_MAX 7++static char *any_string(void)+{+ char *s = any_bytes(LEN_MAX + 1);+ size_t len;++ __CPROVER_assume(len <= LEN_MAX);+ s[LEN_MAX] = '\0';+ return s + LEN_MAX - len;+}++static bool name_char(char c)+{+ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||+ (c >= '0' && c <= '9') || c == '.' || c == '_' || c == '-';+}++static bool decimal(const char *s, size_t n)+{+ size_t i;++ for (i = 0; i < n; i++)+ if (s[i] < '0' || s[i] > '9')+ return false;+ return n;+}++void harness(void)+{+ char *name = any_string(), *token = any_string(), *was;+ size_t n = strlen(name), i, colon;+ dev_t dev;++ if (dm_name_ok(name)) {+ __CPROVER_assert(n && strcmp(name, ".") && strcmp(name, ".."),+ "a name, not . or ..");+ for (i = 0; i < n; i++)+ __CPROVER_assert(name_char(name[i]), "of [A-Za-z0-9._-]");+ }+ n = strlen(token);+ was = any_bytes(n + 1);+ memcpy(was, token, n + 1);+ if (dm_device(token, &dev))+ return;+ colon = strchr(was, ':') - was;+ __CPROVER_assert(decimal(was, colon) &&+ decimal(was + colon + 1, n - colon - 1),+ "decimal major:minor");+}diff --git a/testing/proofs/cbmc/eventlog-records.c b/testing/proofs/cbmc/eventlog-records.cnew file mode 100644index 00000000..6801bd1e--- /dev/null+++ b/testing/proofs/cbmc/eventlog-records.c@@ -0,0 +1,36 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * log_records() of util/eventlog.c, which every eventlog reader runs on+ * what read_file() gives it, on every log of LEN bytes: it refuses one that+ * holds a NUL, and otherwise leaves the string of its whole lines, every+ * byte as it was, cutting only a last line without its newline.+ */++#include "proof.h"+#include "../../../util/io.c"+#include "../../../util/eventlog.c"++void harness(void)+{+ char *buf, *was;+ size_t i, n;++ buf = any_bytes(LEN + 1);+ buf[LEN] = '\0';+ was = any_bytes(LEN + 1);+ memcpy(was, buf, LEN + 1);+ if (log_records(buf, LEN)) {+ __CPROVER_assert(memchr(was, '\0', LEN),+ "refuses only a log that holds a NUL");+ return;+ }+ n = strnlen(buf, LEN + 1);+ __CPROVER_assert(!memchr(was, '\0', LEN), "a log without a NUL");+ __CPROVER_assert(!n || buf[n - 1] == '\n', "whole lines");+ for (i = 0; i < n; i++)+ __CPROVER_assert(buf[i] == was[i], "kept as it was");+ for (i = n; i < LEN; i++)+ __CPROVER_assert(was[i] != '\n', "cutting only a torn line");+}diff --git a/testing/proofs/cbmc/image-bootstrap.c b/testing/proofs/cbmc/image-bootstrap.cnew file mode 100644index 00000000..4996d1cc--- /dev/null+++ b/testing/proofs/cbmc/image-bootstrap.c@@ -0,0 +1,50 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * bootstrap_parse() of util/image_bootstrap.c on every settings line of LEN+ * bytes, a read failure and whatever earlier lines set: what it accepts+ * holds a certificate under the runtime and a PCR measurements may use,+ * and only the initial read, the initrd's, sets the runtime image or its+ * root.+ */++#define PROOF_FILE LEN+#define PROOF_MEMCPY_BYTEWISE+#include "proof.h"+#include "../../../util/malloc.c"+#include "../../../util/string.c"+#include "../../../util/hex.c"+#include "../../../util/io.c"+#include "../../../util/config_measure.c"+#include "../../../util/image_bootstrap.c"++static struct image_bootstrap boot;++void harness(void)+{+ bool initial, had_root, had_runtime;+ size_t i;+ FILE *f;++ proof_file_open();+ for (i = 0; i + 1 < LEN; i++)+ __CPROVER_assume(proof_file[i] != '\n');+ f = fopen("bootstrap", "r");+ __CPROVER_assume(f);+ boot.have_root = had_root;+ if (had_runtime)+ strscpy(boot.runtime, "/r", sizeof(boot.runtime));+ if (bootstrap_parse(f, &boot, initial))+ return;+ __CPROVER_assert(!boot.certificate[0] ||+ !strncmp(boot.certificate, RUNTIME_DIR "/",+ sizeof(RUNTIME_DIR)),+ "a certificate under the runtime");+ __CPROVER_assert(!boot.have_pcr || !config_measure_pcr_valid(boot.pcr),+ "a PCR measurements may use");+ if (!initial)+ __CPROVER_assert(boot.have_root == had_root &&+ !boot.runtime[0] == !had_runtime,+ "the runtime only from the initial read");+}diff --git a/testing/proofs/cbmc/image-path.c b/testing/proofs/cbmc/image-path.cnew file mode 100644index 00000000..04fb928f--- /dev/null+++ b/testing/proofs/cbmc/image-path.c@@ -0,0 +1,47 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * image_path_valid() of util/image_bootstrap.c, which vets the image paths+ * of the bootstrap settings, on every string of up to LEN_MAX bytes: what it+ * accepts is absolute, not the root and has no empty or ".." component but+ * a last empty one, and it accepts every absolute path of plain components.+ */++#include "proof.h"+#include "../../../util/image_bootstrap.c"++#define LEN_MAX 8++static bool component_is(const char *s, size_t n, const char *what)+{+ return n == strlen(what) && !strncmp(s, what, n);+}++void harness(void)+{+ bool plain = true, safe = true;+ size_t len, start, i, n;+ char *path, *s;++ s = any_bytes(LEN_MAX + 1);+ s[LEN_MAX] = '\0';+ __CPROVER_assume(len <= LEN_MAX);+ path = s + LEN_MAX - len;+ len = strlen(path);+ for (start = 1; start <= len; start = i + 1) {+ for (i = start; i < len && path[i] != '/'; i++)+ ;+ n = i - start;+ plain = plain && n && !component_is(path + start, n, ".") &&+ !component_is(path + start, n, "..");+ safe = safe && !component_is(path + start, n, "..") &&+ (n || i == len);+ }+ if (image_path_valid(path))+ __CPROVER_assert(path[0] == '/' && len > 1 && safe,+ "absolute, not the root, no traversal");+ if (len && path[0] == '/' && plain)+ __CPROVER_assert(image_path_valid(path),+ "every absolute path of plain components");+}diff --git a/testing/proofs/cbmc/kernel-notes.c b/testing/proofs/cbmc/kernel-notes.cnew file mode 100644index 00000000..b2b452a4--- /dev/null+++ b/testing/proofs/cbmc/kernel-notes.c@@ -0,0 +1,68 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * kernel_build_id_from_notes() of util/kernel_build_id.c on every note+ * section of LEN bytes: it reads nothing outside the section, a refusal+ * leaves an empty string, and a build ID is the lowercase hex of at most+ * 64 bytes, that of the first note when it is the GNU build ID.+ */++#include "proof.h"+#include "../../../util/hex.c"+#include "../../../util/kernel_build_id.c"++static __u32 word(const __u8 *p)+{+ __u32 v;++ memcpy(&v, p, sizeof(v));+ return v;+}++static bool lower_hex(char c)+{+ return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f');+}++static void first_note(const __u8 *notes, const char *out)+{+ static const char d[] = "0123456789abcdef";+ __u32 namesz, descsz;+ size_t desc, i;++ if (LEN < 16)+ return;+ namesz = word(notes);+ descsz = word(notes + 4);+ if (word(notes + 8) != 3 || namesz < 3 || namesz > LEN ||+ memcmp(notes + 12, "GNU", 3) || !descsz || descsz > LEN)+ return;+ desc = 12 + align4(namesz);+ if (desc + align4(descsz) > LEN)+ return;+ for (i = 0; i < descsz; i++)+ __CPROVER_assert(out[2 * i] == d[notes[desc + i] >> 4] &&+ out[2 * i + 1] == d[notes[desc + i] & 15],+ "the first note's build ID");+ __CPROVER_assert(!out[2 * descsz], "and nothing more");+}++void harness(void)+{+ char out[KERNEL_BUILD_ID_STRING_MAX];+ __u8 *notes;+ size_t i, n;++ notes = any_bytes(LEN);+ if (kernel_build_id_from_notes(notes, LEN, out)) {+ __CPROVER_assert(!out[0], "a refusal leaves an empty string");+ return;+ }+ n = strnlen(out, sizeof(out));+ __CPROVER_assert(n < sizeof(out) && n >= 2 && !(n % 2),+ "a terminated ID of whole bytes, at most 64");+ for (i = 0; i < n; i++)+ __CPROVER_assert(lower_hex(out[i]), "in lowercase hex");+ first_note(notes, out);+}diff --git a/testing/proofs/cbmc/kernel-release.c b/testing/proofs/cbmc/kernel-release.cnew file mode 100644index 00000000..8452c2fb--- /dev/null+++ b/testing/proofs/cbmc/kernel-release.c@@ -0,0 +1,38 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * kernel_release_prefix_matches() of util/kernel_release.c, which picks a+ * tree's upatches for the running kernel, on every release and prefix of up+ * to LEN_MAX characters: it matches exactly a prefix that ends where the+ * release does or at a '.' or '-', and reads nothing past either string.+ */++#include "proof.h"+#include "../../../util/kernel_release.c"++#define LEN_MAX 8++static char *any_string(void)+{+ char *s = any_bytes(LEN_MAX + 1);+ size_t len;++ __CPROVER_assume(len <= LEN_MAX);+ s[LEN_MAX] = '\0';+ return s + LEN_MAX - len;+}++void harness(void)+{+ char *release = any_string(), *prefix = any_string();+ size_t n = strlen(prefix), i;+ bool starts = strlen(release) >= n;++ for (i = 0; starts && i < n; i++)+ starts = release[i] == prefix[i];+ __CPROVER_assert(kernel_release_prefix_matches(release, prefix) ==+ (!n || (starts && (!release[n] || release[n] == '.' ||+ release[n] == '-'))),+ "a prefix ending at a component boundary");+}diff --git a/testing/proofs/cbmc/log-escape.c b/testing/proofs/cbmc/log-escape.cnew file mode 100644index 00000000..4419b818--- /dev/null+++ b/testing/proofs/cbmc/log-escape.c@@ -0,0 +1,54 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * escape() of util/log.c, which every message passes before syslog and the+ * terminal, on every message of up to LEN bytes and every buffer: it writes+ * a terminated line of printable ASCII inside the buffer that decodes back+ * to the message, or to a start of it cut between two characters.+ */++#include "proof.h"+#include "../../../util/log.c"++static bool octal(char c)+{+ return c >= '0' && c <= '7';+}++void harness(void)+{+ size_t cap, len, i, n, k;+ char *src, *dst;+ __u8 c;++ src = any_bytes(LEN + 1);+ src[LEN] = '\0';+ len = strnlen(src, LEN);+ __CPROVER_assume(cap >= 1 && cap <= 4 * LEN + 2);+ dst = any_bytes(cap);+ escape(dst, cap, src);+ for (i = 0, n = 0; dst[i]; n++) {+ __CPROVER_assert(i + 1 < cap, "terminated inside the buffer");+ __CPROVER_assert(dst[i] >= 0x20 && dst[i] < 0x7f,+ "printable ASCII only");+ if (dst[i] != '\\') {+ c = dst[i++];+ } else if (dst[i + 1] == '\\') {+ c = '\\';+ i += 2;+ } else {+ __CPROVER_assert(dst[i + 1] <= '3' && octal(dst[i + 1]) &&+ octal(dst[i + 2]) && octal(dst[i + 3]),+ "a backslash, then itself or 3 octal digits");+ c = (dst[i + 1] - '0') << 6 | (dst[i + 2] - '0') << 3 |+ (dst[i + 3] - '0');+ i += 4;+ }+ __CPROVER_assert(n < len && c == (__u8)src[n],+ "decodes to the message");+ }+ k = 4 * len + 1;+ if (cap >= k)+ __CPROVER_assert(n == len, "the whole message when it fits");+}diff --git a/testing/proofs/cbmc/proof.h b/testing/proofs/cbmc/proof.hindex 6790c2f1..851ff15e 100644--- a/testing/proofs/cbmc/proof.h+++ b/testing/proofs/cbmc/proof.h@@ -10,12 +10,13 @@#define SHIRUDO_PROOF_H#include <stddef.h>+#include <string.h>#include <shirudo/types.h>/*- * CBMC models neither strnlen() nor memchr(), and a function without a- * body returns anything.+ * CBMC models neither strnlen(), memchr() nor strstr(), and a function+ * without a body returns anything.*/size_t strnlen(const char *s, size_t max){@@ -58,6 +59,18 @@ void *memchr(const void *s, int c, size_t n)}#endif+char *strstr(const char *s, const char *needle)+{+ size_t n = strlen(needle);++ for (;; s++) {+ if (!strncmp(s, needle, n))+ return (char *)s;+ if (!*s)+ return NULL;+ }+}+/** CBMC 5.95's memcpy() reads wrong bytes from a string literal when the* source pointer is one of several literals picked by symbolic data, and@@ -85,4 +98,116 @@ static inline void *any_bytes(size_t size)return p;}+/*+ * An input of len bytes, any len, for code that reads at most max of+ * them: it ends where its object does, so that a read past len fails the+ * proof, and one longer than max is its first max bytes.+ */+static inline void *any_input(size_t len, size_t max)+{+ __u8 *p = any_bytes(max);++ return len <= max ? p + max - len : p;+}++#ifdef PROOF_DEVICE_NUMBERS+#include <sys/sysmacros.h>+#include <sys/types.h>++/* glibc defines these inline only when it optimizes; its encoding. */+dev_t gnu_dev_makedev(__u32 major, __u32 minor)+{+ return ((dev_t)(major & 0x00000fffu) << 8) |+ ((dev_t)(major & 0xfffff000u) << 32) |+ (dev_t)(minor & 0x000000ffu) |+ ((dev_t)(minor & 0xffffff00u) << 12);+}++__u32 gnu_dev_major(dev_t dev)+{+ return ((dev & 0x00000000000fff00ull) >> 8) |+ ((dev & 0xfffff00000000000ull) >> 32);+}++__u32 gnu_dev_minor(dev_t dev)+{+ return (dev & 0x00000000000000ffull) |+ ((dev & 0x00000ffffff00000ull) >> 12);+}+#endif++#ifdef PROOF_FILE+#include <errno.h>+#include <stdbool.h>+#include <stdio.h>+#include <stdlib.h>+#include <sys/types.h>++/*+ * A file of PROOF_FILE bytes of any contents, or of the proof_file_len a+ * harness cuts it to, which getline() reads line by line, into a buffer of+ * a size the solver need not track, or fails to read with an I/O or+ * allocation error, and pread() reads at any offset.+ */+static size_t proof_file_at, proof_file_len;+static __u8 *proof_file;++/*+ * CBMC's own errno takes any value at every read, which lets a loop that+ * retries on EINTR run for ever; this one keeps what was stored.+ */+static int proof_errno;++int *__errno_location(void)+{+ return &proof_errno;+}++static inline void proof_file_open(void)+{+ proof_file = any_bytes(PROOF_FILE);+ proof_file_len = PROOF_FILE;+ proof_file_at = 0;+}++ssize_t getline(char **line, size_t *cap, FILE *f)+{+ size_t n = 0;+ bool fail;+ char *p;++ if (fail) {+ errno = EIO;+ return -1;+ }+ if (proof_file_at == proof_file_len)+ return -1;+ while (proof_file_at + n < proof_file_len &&+ proof_file[proof_file_at + n++] != '\n')+ ;+ p = malloc(PROOF_FILE + 1);+ if (!p) {+ errno = ENOMEM;+ return -1;+ }+ memcpy(p, proof_file + proof_file_at, n);+ p[n] = '\0';+ free(*line);+ *line = p;+ *cap = PROOF_FILE + 1;+ proof_file_at += n;+ return n;+}++ssize_t pread(int fd, void *buf, size_t n, off_t off)+{+ if (off < 0 || (size_t)off >= proof_file_len)+ return 0;+ if (n > proof_file_len - (size_t)off)+ n = proof_file_len - (size_t)off;+ memcpy(buf, proof_file + off, n);+ return n;+}+#endif+#endif /* SHIRUDO_PROOF_H */diff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.listindex 54c66c70..60e2c8fb 100644--- a/testing/proofs/cbmc/proofs.list+++ b/testing/proofs/cbmc/proofs.list@@ -26,3 +26,21 @@ upatch-rodata -I ../../../vendor/libbpf/include/uapi -I ../../../vendor/libbpfstring-number-model --unwind 6bpm-tree-follows --unwind 4bpm-upatch-member --unwind 30 LEN=0-18+tpm-reader --unwind 1+tpm-signature --unwind 1 --unwindset bytes_at.0:33+tpm-attest --unwind 1 --unwindset memcpy.0:35+tpm-public --unwind 1+tpm-digest --unwind 2 --object-bits 12+tpm-kdf --unwind 3 --unwindset strlen.0:68 --object-bits 12 -DLABEL_MAX=66 -DOUT_MAX=64+attest-fields --unwind 18 LEN=0-16+attest-pcr --unwind 1 --unwindset harness.0:65,hex_decode.0:33,strlen.0:66 --no-array-field-sensitivity+eventlog-records --unwind 34 LEN=0-32+config-image --unwind 6 --unwindset memcmp.0:89,harness.0:33 LEN=0,511,512,4096+config-auth --unwind 18 LEN=0-16+config-hosts --unwind 9 LEN=0-7+image-bootstrap --unwind 12 --unwindset bootstrap_parse.0:2,hex_decode.0:1 LEN=0-8+image-path --unwind 11+dm-names --unwind 10+kernel-notes --unwind 1 --unwindset kernel_build_id_from_notes.0:5,hex_encode.0:29,strnlen.0:58,harness.0:58,first_note.0:29,memcmp.0:4 LEN=0-24,28,32,36,40+kernel-release --unwind 10+log-escape --unwind 7 LEN=0-5diff --git a/testing/proofs/cbmc/run.sh b/testing/proofs/cbmc/run.shindex 16396323..873c8fc3 100755--- a/testing/proofs/cbmc/run.sh+++ b/testing/proofs/cbmc/run.sh@@ -6,9 +6,9 @@# properties the harnesses in this directory state, for every input within# the bounds each harness names, and fail when one does not hold. Each# harness defines harness() and includes the source it proves; proofs.list-# gives its CBMC options, and LEN=<a>-<b> or LEN=<x>,<y> there runs it once-# for each length, as CBMC handles a fixed length far faster than one it-# has to keep symbolic. -v prints the whole CBMC output and the trace of a+# gives its CBMC options, and LEN=<a>-<b>,<c> there runs it once for each+# length the list of lengths and ranges names, as CBMC handles a fixed+# length far faster than one it has to keep symbolic. -v prints the whole CBMC output and the trace of a# failed check.## testing/proofs/cbmc/run.sh [-v] [<proof>...]@@ -72,11 +72,11 @@ while read -r name opts; do*) rest="$rest $o" ;;esacdone- case $lens in- -) ;;- *-*) lens=$(seq "${lens%-*}" "${lens#*-}") ;;- *) lens=$(printf '%s' "$lens" | tr ',' ' ') ;;- esac+ [ "$lens" = - ] ||+ lens=$(printf '%s\n' "$lens" | tr ',' '\n' |+ while IFS=- read -r from to; do+ seq "$from" "${to:-$from}"+ done)start=$(date +%s)checked=0bad=diff --git a/testing/proofs/cbmc/tpm-attest.c b/testing/proofs/cbmc/tpm-attest.cnew file mode 100644index 00000000..7f125120--- /dev/null+++ b/testing/proofs/cbmc/tpm-attest.c@@ -0,0 +1,49 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * tpm_attest_parse() of util/tpm_wire.c, which attest verify runs on the+ * quote and certification a target sends, for inputs of every size: it+ * reads nothing outside them, and what it accepts is a quote or an NV+ * certification spanning the whole input, its sized fields inside it.+ */++#define PROOF_MEMCPY_BYTEWISE+#include "proof.h"+#include "../../../util/tpm_wire.c"++/* An NV certification of the longest names, extra data and contents. */+#define ATTEST_MAX (4u + 2 + 2 + 34 + 2 + 64 + 27 + 2 + 34 + 2 + 2 + 2048)++void harness(void)+{+ size_t len, head, extra_at;+ struct tpm_attest a;+ const __u8 *buf;++ buf = any_bytes(min(len, ATTEST_MAX));+ if (tpm_attest_parse(buf, len, &a))+ return;+ __CPROVER_assert(get_be32(buf) == TPM_GENERATED, "TPM-generated");+ __CPROVER_assert(a.signer_len <= TPM_NAME_LEN,+ "a signer's name that fits its buffer");+ extra_at = 8 + a.signer_len + 2;+ __CPROVER_assert(a.extra_len <= 64 && a.extra == buf + extra_at,+ "extra data inside the input, at its place");+ head = extra_at + a.extra_len + 8 + 4 + 4 + 1 + 8;+ if (a.type == TPM_ST_ATTEST_QUOTE) {+ __CPROVER_assert(len == head + 4 + 2 + 1 + TPM_PCR_SELECT_LEN ++ 2 + 32, "a quote spans the whole input");+ __CPROVER_assert(a.pcr_mask < 1u << 24,+ "one SHA-256 bank of 24 PCRs");+ return;+ }+ __CPROVER_assert(a.type == TPM_ST_ATTEST_NV, "a quote or an NV one");+ __CPROVER_assert(a.index_name_len <= TPM_NAME_LEN,+ "an index name that fits its buffer");+ __CPROVER_assert(a.nv_len <= 2048 &&+ len == head + 2 + a.index_name_len + 2 + 2 ++ a.nv_len &&+ a.nv_data == buf + len - a.nv_len,+ "the NV contents end the input");+}diff --git a/testing/proofs/cbmc/tpm-digest.c b/testing/proofs/cbmc/tpm-digest.cnew file mode 100644index 00000000..8e2a61e0--- /dev/null+++ b/testing/proofs/cbmc/tpm-digest.c@@ -0,0 +1,110 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * What util/tpm_wire.c hands SHA-256, modelled as any result, for every+ * length a caller can pass: the policy digests but PolicyPCR's, the object,+ * NV and qualified names, cpHash and the writer's authorization hash only+ * whole inputs into whole outputs, and a nonce of 1 to 64 bytes.+ */++#include "proof.h"+#include "../../../util/tpm_wire.c"++static int any_result(void)+{+ int ret;++ return ret;+}++int sha256_init(struct sha256_ctx *ctx)+{+ return any_result();+}++int sha256_update(struct sha256_ctx *ctx, const void *data, size_t len)+{+ __CPROVER_assert(__CPROVER_r_ok(data, len), "hashes only its input");+ return any_result();+}++int sha256_final(struct sha256_ctx *ctx, __u8 out[32])+{+ __CPROVER_assert(__CPROVER_w_ok(out, 32), "a digest's room");+ __CPROVER_havoc_slice(out, 32);+ return any_result();+}++void sha256_free(struct sha256_ctx *ctx)+{+}++int sha256_buffer(const void *data, size_t len, __u8 out[32])+{+ __CPROVER_assert(__CPROVER_r_ok(data, len), "hashes only its input");+ __CPROVER_assert(__CPROVER_w_ok(out, 32), "a digest's room");+ __CPROVER_havoc_slice(out, 32);+ return any_result();+}++int hmac_sha256(const __u8 *key, size_t key_len, const void *data,+ size_t len, __u8 out[32])+{+ __CPROVER_assert(__CPROVER_r_ok(key, key_len) &&+ __CPROVER_r_ok(data, len), "MACs only its input");+ __CPROVER_assert(__CPROVER_w_ok(out, 32), "a MAC's room");+ __CPROVER_havoc_slice(out, 32);+ return any_result();+}++int aes128_cfb_encrypt(const __u8 key[16], const __u8 *in, size_t len,+ __u8 *out)+{+ __CPROVER_assert(__CPROVER_r_ok(key, 16) && __CPROVER_r_ok(in, len) &&+ __CPROVER_w_ok(out, len), "encrypts only its input");+ __CPROVER_havoc_slice(out, len);+ return any_result();+}++static __u8 *digest(void)+{+ return any_bytes(32);+}++static void policies(void)+{+ __u8 *a = digest(), *b = digest(), *name = any_bytes(TPM_NAME_LEN);+ __u8 *prev = digest(), *out = digest(), *pub = any_bytes(384);+ __u32 cc, index, attrs, hierarchy;+ __u16 data_size;+ bool written;++ if (tpm_policy_nv_written(prev, written, out) ||+ tpm_policy_or(a, b, out) || tpm_policy_signed(prev, pub, out) ||+ tpm_policy_command_code(prev, cc, out) ||+ tpm_register_policy_final(a, b, pub, out) ||+ tpm_nv_name(index, attrs, prev, data_size, name) ||+ tpm_qualified_name(hierarchy, name, any_bytes(TPM_NAME_LEN)) ||+ tpm_nv_extend_cphash(name, prev, out))+ return;+}++static void names(void)+{+ __u8 *name = any_bytes(TPM_NAME_LEN);+ size_t len, nonce_len;++ if (!tpm_object_name(any_bytes(len), len, name))+ __CPROVER_assert(len >= 4, "a public area of type and name hash");+ if (!tpm_writer_auth_hash(any_bytes(64), nonce_len, digest(),+ digest()))+ __CPROVER_assert(nonce_len && nonce_len <= 64,+ "a nonce of 1 to 64 bytes");+}++void harness(void)+{+ policies();+ names();+}diff --git a/testing/proofs/cbmc/tpm-kdf.c b/testing/proofs/cbmc/tpm-kdf.cnew file mode 100644index 00000000..52cbea6b--- /dev/null+++ b/testing/proofs/cbmc/tpm-kdf.c@@ -0,0 +1,102 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The key derivations of util/tpm_wire.c that attest challenge protects a+ * credential with, with SHA-256, HMAC and AES modelled as any result: for+ * every label of up to LABEL_MAX bytes and every length, KDFa, KDFe and+ * credential protection hash only whole inputs and refuse longer ones.+ */++#include "proof.h"+#include "../../../util/tpm_wire.c"++static int any_result(void)+{+ int ret;++ return ret;+}++int sha256_init(struct sha256_ctx *ctx)+{+ return any_result();+}++int sha256_update(struct sha256_ctx *ctx, const void *data, size_t len)+{+ __CPROVER_assert(__CPROVER_r_ok(data, len), "hashes only its input");+ return any_result();+}++int sha256_final(struct sha256_ctx *ctx, __u8 out[32])+{+ __CPROVER_assert(__CPROVER_w_ok(out, 32), "a digest's room");+ __CPROVER_havoc_slice(out, 32);+ return any_result();+}++void sha256_free(struct sha256_ctx *ctx)+{+}++int sha256_buffer(const void *data, size_t len, __u8 out[32])+{+ __CPROVER_assert(__CPROVER_r_ok(data, len), "hashes only its input");+ __CPROVER_assert(__CPROVER_w_ok(out, 32), "a digest's room");+ __CPROVER_havoc_slice(out, 32);+ return any_result();+}++int hmac_sha256(const __u8 *key, size_t key_len, const void *data,+ size_t len, __u8 out[32])+{+ __CPROVER_assert(__CPROVER_r_ok(key, key_len) &&+ __CPROVER_r_ok(data, len), "MACs only its input");+ __CPROVER_assert(__CPROVER_w_ok(out, 32), "a MAC's room");+ __CPROVER_havoc_slice(out, 32);+ return any_result();+}++int aes128_cfb_encrypt(const __u8 key[16], const __u8 *in, size_t len,+ __u8 *out)+{+ __CPROVER_assert(__CPROVER_r_ok(key, 16) && __CPROVER_r_ok(in, len) &&+ __CPROVER_w_ok(out, len), "encrypts only its input");+ __CPROVER_havoc_slice(out, len);+ return any_result();+}++static __u8 *digest(void)+{+ return any_bytes(32);+}++static char *label(void)+{+ char *s = any_bytes(LABEL_MAX + 1);++ s[LABEL_MAX] = '\0';+ return s;+}++void harness(void)+{+ size_t key_len, u_len, v_len, out_len;+ __u8 *out;++ __CPROVER_assume(key_len <= 64 && out_len <= OUT_MAX);+ out = any_bytes(OUT_MAX);+ if (!tpm_kdfa(any_bytes(64), key_len, label(),+ any_bytes(TPM_NAME_LEN), u_len,+ any_bytes(TPM_NAME_LEN), v_len, out, out_len))+ __CPROVER_assert(u_len <= TPM_NAME_LEN && v_len <= TPM_NAME_LEN,+ "KDFa contexts of names");+ if (!tpm_kdfe(digest(), label(), any_bytes(64), u_len,+ any_bytes(64), v_len, digest()))+ __CPROVER_assert(u_len <= 64 && v_len <= 64,+ "KDFe parties of points");+ if (tpm_credential_protect(digest(), any_bytes(TPM_NAME_LEN), digest(),+ any_bytes(TPM_ID_OBJECT_LEN)))+ return;+}diff --git a/testing/proofs/cbmc/tpm-public.c b/testing/proofs/cbmc/tpm-public.cnew file mode 100644index 00000000..780c6ebe--- /dev/null+++ b/testing/proofs/cbmc/tpm-public.c@@ -0,0 +1,56 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * tpm_public_parse() and tpm_ecc_public_parse() of util/tpm_wire.c, on the+ * key public areas of evidence, for inputs of every size: they read nothing+ * outside the input and accept only the P-256 and RSA-2048 shapes they+ * name, spanning the whole input.+ */++#include "proof.h"+#include "../../../util/tpm_wire.c"++/* The longest: a policy, a protector, a scheme and an RSA modulus. */+#define PUBLIC_MAX (14 + 32 + 4 + 2 + 8 + 256)++static bool shape(const struct tpm_public *p)+{+ return (p->policy_len == 0 || p->policy_len == 32) &&+ (p->sym_alg == TPM_ALG_NULL ||+ (p->sym_alg == TPM_ALG_AES && p->sym_bits == 128 &&+ p->sym_mode == TPM_ALG_CFB)) &&+ (p->scheme == TPM_ALG_NULL ||+ (p->scheme == TPM_ALG_ECDSA &&+ p->scheme_hash == TPM_ALG_SHA256)) &&+ ((p->type == TPM_ALG_ECC && p->curve == TPM_ECC_NIST_P256) ||+ (p->type == TPM_ALG_RSA && p->rsa_bits == 2048 &&+ p->n_len == 256 &&+ (!p->rsa_exponent || p->rsa_exponent == 65537)));+}++static size_t wire_len(const struct tpm_public *p)+{+ return 14 + p->policy_len ++ (p->sym_alg != TPM_ALG_NULL ? 4 : 0) ++ (p->scheme != TPM_ALG_NULL ? 2 : 0) ++ (p->type == TPM_ALG_ECC ? 72 : 8 + p->n_len);+}++void harness(void)+{+ struct tpm_ecc_public ecc;+ struct tpm_public p;+ const __u8 *buf;+ size_t len;++ buf = any_input(len, PUBLIC_MAX);+ if (!tpm_public_parse(buf, len, &p))+ __CPROVER_assert(shape(&p) && len == wire_len(&p),+ "a key of the shapes it names, the whole input");+ if (!tpm_ecc_public_parse(buf, len, &ecc))+ __CPROVER_assert(ecc.curve == TPM_ECC_NIST_P256 &&+ ecc.scheme == TPM_ALG_ECDSA &&+ ecc.hash == TPM_ALG_SHA256,+ "a P-256 ECDSA key over SHA-256");+}diff --git a/testing/proofs/cbmc/tpm-reader.c b/testing/proofs/cbmc/tpm-reader.cnew file mode 100644index 00000000..f69e0331--- /dev/null+++ b/testing/proofs/cbmc/tpm-reader.c@@ -0,0 +1,66 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The reader of util/tpm_wire.c, through which its parsers take every field+ * of a TPM structure, for every input of any size and every field size: a+ * read stays inside what is left, keeps the position and what is left+ * summing to the input's end, and a sized field fits its bound.+ */++#include "proof.h"+#include "../../../util/tpm_wire.c"++void harness(void)+{+ size_t left, max, n, name_len;+ const __u8 *start, *data;+ __u8 name[TPM_NAME_LEN];+ struct reader r;+ __u64 v64;+ __u32 v32;+ __u16 v16;+ int which;+ __u8 v8;++ start = any_bytes(left);+ r.p = start;+ r.left = left;+ switch (which) {+ case 0:+ if (get8(&r, &v8))+ return;+ break;+ case 1:+ if (get16(&r, &v16))+ return;+ break;+ case 2:+ if (get32(&r, &v32))+ return;+ break;+ case 3:+ if (get64(&r, &v64))+ return;+ break;+ case 4:+ if (get2b(&r, &data, &n, max))+ return;+ __CPROVER_assert(n <= max && data >= start &&+ data + n == r.p, "a field inside the input");+ break;+ case 5:+ __CPROVER_assume(max <= TPM_NAME_LEN);+ if (get2b_fixed(&r, name, max))+ return;+ break;+ default:+ if (get_name(&r, name, &name_len))+ return;+ __CPROVER_assert(name_len <= TPM_NAME_LEN, "a name that fits");+ break;+ }+ __CPROVER_assert(r.p >= start &&+ (size_t)(r.p - start) + r.left == left,+ "what is left ends with the input");+}diff --git a/testing/proofs/cbmc/tpm-signature.c b/testing/proofs/cbmc/tpm-signature.cnew file mode 100644index 00000000..a8b9d786--- /dev/null+++ b/testing/proofs/cbmc/tpm-signature.c@@ -0,0 +1,49 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * tpm_ecdsa_signature_parse() of util/tpm_wire.c, on the signatures of+ * evidence, for inputs of every size: it reads nothing past its input and+ * accepts exactly a TPMT_SIGNATURE of ECDSA over SHA-256 with 32-byte r and+ * s, which it returns as they stand.+ */++#include "proof.h"+#include "../../../util/tpm_wire.c"++#define SIG_LEN (2u + 2 + 2 + 32 + 2 + 32)++static bool bytes_at(const __u8 *a, const __u8 *b, size_t n)+{+ size_t i;++ for (i = 0; i < n; i++)+ if (a[i] != b[i])+ return false;+ return true;+}++void harness(void)+{+ __u8 r[32], s[32];+ bool well_formed;+ size_t len;+ __u8 *buf;+ int ret;++ buf = any_bytes(min(len, SIG_LEN));+ ret = tpm_ecdsa_signature_parse(buf, len, r, s);+ if (len != SIG_LEN) {+ __CPROVER_assert(ret, "nothing but the signature");+ return;+ }+ well_formed = get_be16(buf) == TPM_ALG_ECDSA &&+ get_be16(buf + 2) == TPM_ALG_SHA256 &&+ get_be16(buf + 4) == 32 && get_be16(buf + 38) == 32;+ __CPROVER_assert(!ret == well_formed,+ "accepts exactly ECDSA, SHA-256, 32-byte r and s");+ if (!ret)+ __CPROVER_assert(bytes_at(r, buf + 6, 32) &&+ bytes_at(s, buf + 40, 32),+ "r and s as the signature holds them");+}