eBPF-based security platform
testing: prove the bpm package, registry and tree readers
Daniel Borkmann2cd30a5d68f7
diff --git a/.agents/skills/proofs/SKILL.md b/.agents/skills/proofs/SKILL.mdindex 57fdd9cc..3c3f96e5 100644--- a/.agents/skills/proofs/SKILL.md+++ b/.agents/skills/proofs/SKILL.md@@ -132,6 +132,10 @@ determining its events.5.95's `memcpy()` model, not the code: rerun the input as a constantto confirm, then define `PROOF_MEMCPY_BYTEWISE` before including`proof.h` (as `bpm-upatch-member.c` does).+- CBMC 5.95 also reads wrong bytes at a symbolic offset from a heap+ object of struct type that a `memcpy()` filled, `xreallocarray()`'s+ for one; a wrong value can make a check pass as well as fail. Read+ such an object by its members, never cast to bytes.- `run.sh` passes `--unsigned-char`, as the build passes`-funsigned-char`; a conversion failure on `char` elsewhere means theproof is not built as the code is.diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rstindex e11f6fa3..d8f81fce 100644--- a/Documentation/contribute/testing/proof-coverage.rst+++ b/Documentation/contribute/testing/proof-coverage.rst@@ -91,6 +91,88 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.- A name without ``~`` maps to a pin name without a dot and back toitself; a pin name without a dot that decodes is the encoding ofwhat it decodes to+ * - ``bpm-tar-octal``+ - ``bpm_tar_octal()``+ - Every field of 8 and 12 bytes+ - Accepted exactly as octal digits with blanks or NULs around them,+ at their value; nothing written on refusal+ * - ``bpm-tar-header``+ - ``bpm_tar_header_ok()``+ - Every header without an octal checksum; the checksum against+ headers with 64 free bytes at three places+ - Holds exactly when the field is the byte sum with its own bytes as+ blanks+ * - ``bpm-archive-path``+ - ``bpm_archive_rel_ok()``, ``package_member_name_ok()``+ - Names of 0 to 10 bytes+ - A name passes exactly when it cannot leave its directory; a member+ exactly when it is the path below the one top directory+ * - ``bpm-names``+ - ``bpm_name_ok()``, ``bpm_version_ok()``, ``bpm_package_name_ok()``+ - Up to 129 bytes, past the 128-byte limit+ - Exactly the character, length and first-character rules+ * - ``bpm-semver-valid``+ - ``bpm_semver_valid()``+ - Up to 11 bytes+ - Valid exactly by the SemVer 2.0.0 grammar; the pre-release parsed+ is the text between the core and ``+``+ * - ``bpm-semver-order``+ - ``bpm_semver_compare()``+ - Every pair of strings up to 7 bytes+ - Antisymmetric; equal exactly up to build metadata; every version+ above every invalid string; the core by number; a pre-release+ below its release+ * - ``bpm-semver-transitive``+ - ``bpm_semver_compare()``+ - Every three strings up to 7 bytes+ - Transitive, valid versions or not+ * - ``bpm-semver-prerelease``+ - ``prerelease_compare()``+ - Identifiers up to 3 bytes, lists up to 5+ - Numeric by value and below alphanumeric, alphanumeric by bytes;+ the first difference decides, else the longer list+ * - ``bpm-oci-challenge``+ - ``parse_challenge()``+ - ``WWW-Authenticate`` values up to 17 bytes+ - Outputs terminated in their buffers; only ``Bearer`` with a realm;+ each value a quoted value of the header+ * - ``bpm-oci-token``+ - ``token_from_body()``+ - Answers up to 17 bytes+ - Nothing written on refusal; a token without controls, quotes or+ backslashes, quoted in the answer under ``token`` or+ ``access_token``+ * - ``bpm-oci-header``+ - ``bpm_header()``+ - Header lines up to 20 bytes, NULs included+ - The whole line taken; values terminated, without a leading blank,+ a media type without parameters, a location without CR or LF+ * - ``bpm-oci-descriptor``+ - ``on_descriptor()``, ``on_index_entry()``+ - Any descriptor or index entry, into an empty or a full table+ - Kept exactly when whole, with a ``sha256:`` digest of 64 digits,+ and with room; copied as read; a refusal not counted+ * - ``bpm-oci-strings``+ - ``bpm_trim()``, ``bpm_copy_string()``, ``bpm_digest_matches()``+ - Strings up to 6 bytes, buffers up to 4+ - Terminated, what fits; a digest matches exactly ``sha256:`` and its+ hex in either case+ * - ``bpm-source-config``+ - ``bpm_parse_source_line()``, ``bpm_parse_tree_line()``+ - Lines up to 12 bytes+ - A setting written only for its exact key and terminated, without+ CR or LF+ * - ``bpm-tree-selector``+ - ``tree_selector_kv_add()``, ``tree_selector_copy()``,+ ``tree_selector_free()``, ``tree_text()``+ - 0 or 1 pairs, every limit, allocations allowed to fail+ - ``-E2BIG`` exactly at the limit with nothing changed; a copy owns+ its arrays, a failed one holds nothing; text without controls+ * - ``bpm-tree-reader``+ - ``tree_map_pair()``, ``tree_take_operation()``+ - Every pair and operation, into an empty or a full publication+ - A key taken once, with a value, below its limit; an operation+ taken exactly when whole and with room* - ``bpm-tree-follows``- ``tree_publication_follows()``- Every sequence number and generation; strings up to 2 bytes@@ -383,14 +465,15 @@ Not covered yetThe fuzz targets in :doc:`test-coverage` cover these parsers; no proofdoes: how ``json_parse()`` dispatches objects, arrays and maps to their-schemas, the OTLP headers a configuration adds, the package archive,-index and 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 and-the event encoders of the register, which need a model of SHA-256, have-no proof.+schemas, the OTLP headers a configuration adds, the walk over a+package's tar members, the registry references and URLs, the kernel+configuration the tree selectors read, the tree log, the eventlog's+lines, the evidence document beyond its PCRs, the config update record,+the site configuration file and the config image's settings and tree.+The ``.upatch`` reader, the TPM structures and the package, index and+tree readers are covered part by part, as the table shows; what joins+those parts, 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/testing/proofs/cbmc/bpm-archive-path.c b/testing/proofs/cbmc/bpm-archive-path.cnew file mode 100644index 00000000..2cc77313--- /dev/null+++ b/testing/proofs/cbmc/bpm-archive-path.c@@ -0,0 +1,71 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The member names util/bpm_package.c takes from a .bee, for every name of+ * LEN bytes: a path passes when it cannot leave the directory it is put+ * under, and a member is matched only in the archive's top directory.+ */++#include "proof.h"+#include "../../../util/bpm_package.c"++#define MEMBER_MAX 3++/* A component of s that is exactly "." or "..", starting at i. */+static bool dot_component(const char *s, size_t len, size_t i)+{+ size_t n = 0;++ if (i && s[i - 1] != '/')+ return false;+ while (n < 2 && i + n < len && s[i + n] == '.')+ n++;+ return n && (i + n == len || s[i + n] == '/');+}++static bool contained(const char *s, size_t len)+{+ size_t i;++ if (!len || s[0] == '/')+ return false;+ for (i = 0; i < len; i++) {+ if (s[i] == '/' && i + 1 < len && s[i + 1] == '/')+ return false;+ if (dot_component(s, len, i))+ return false;+ }+ return true;+}++static char *string(size_t len, size_t room)+{+ char *s = any_bytes(room + 1);++ __CPROVER_assume(!s[len] && strnlen(s, len) == len);+ return s;+}++void harness(void)+{+ size_t member_len, dir, i;+ char *name, *member;+ bool same;++ name = string(LEN, LEN);+ __CPROVER_assert(bpm_archive_rel_ok(name) == contained(name, LEN),+ "a name passes exactly when it stays inside");++ __CPROVER_assume(member_len <= MEMBER_MAX);+ member = string(member_len, MEMBER_MAX);+ for (dir = 0; dir < LEN && name[dir] != '/'; dir++)+ ;+ same = dir < LEN && LEN - dir - 1 == member_len;+ for (i = 0; same && i < member_len; i++)+ same = name[dir + 1 + i] == member[i];+ __CPROVER_assert(package_member_name_ok(name, member) ==+ (contained(name, LEN) && contained(member, member_len) &&+ same),+ "a member only as the whole path below the top directory");+}diff --git a/testing/proofs/cbmc/bpm-names.c b/testing/proofs/cbmc/bpm-names.cnew file mode 100644index 00000000..6888206a--- /dev/null+++ b/testing/proofs/cbmc/bpm-names.c@@ -0,0 +1,51 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The package, repository and version names util/bpm_oci.c accepts, for+ * every string of up to NAME_MAX bytes: a name or version is of letters,+ * digits, '.', '_' and '-', a version also '+', not empty, shorter than+ * BPM_MAX_NAME and not led by a dot, a package name led by a letter or digit.+ */++#include "proof.h"+#include "../../../util/bpm_oci.c"++#define NAME_MAX (BPM_MAX_NAME + 1)++static bool alnum(char c)+{+ return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') ||+ (c >= 'a' && c <= 'z');+}++static bool name_chars(const char *s, size_t len, bool plus)+{+ size_t i;++ for (i = 0; i < len; i++)+ if (!alnum(s[i]) && s[i] != '.' && s[i] != '_' && s[i] != '-' &&+ !(plus && s[i] == '+'))+ return false;+ return true;+}++void harness(void)+{+ char *s = any_bytes(NAME_MAX + 1);+ bool sized;+ size_t len;++ __CPROVER_assume(!s[NAME_MAX]);+ len = strnlen(s, NAME_MAX);+ sized = len && len < BPM_MAX_NAME;+ __CPROVER_assert(bpm_name_ok(s) ==+ (sized && s[0] != '.' && name_chars(s, len, false)),+ "a name");+ __CPROVER_assert(bpm_version_ok(s) ==+ (sized && s[0] != '.' && name_chars(s, len, true)),+ "a version");+ __CPROVER_assert(bpm_package_name_ok(s) ==+ (sized && alnum(s[0]) && name_chars(s, len, false)),+ "a package name");+}diff --git a/testing/proofs/cbmc/bpm-oci-challenge.c b/testing/proofs/cbmc/bpm-oci-challenge.cnew file mode 100644index 00000000..89a70a1d--- /dev/null+++ b/testing/proofs/cbmc/bpm-oci-challenge.c@@ -0,0 +1,75 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * parse_challenge() of util/bpm_oci.c, on a registry's WWW-Authenticate+ * value of up to TEXT_MAX bytes: it takes only a Bearer challenge with a+ * realm, each value it takes is a quoted one of the challenge, and every+ * value is terminated inside its buffer, as long as fits it or one more.+ */++#include "proof.h"+#include "../../../util/bpm_oci.c"++#define TEXT_MAX 17+#define REALM_MAX 2+#define OUT_MAX 3++static char *text(void)+{+ char *s = any_bytes(TEXT_MAX + 1);++ __CPROVER_assume(!s[TEXT_MAX]);+ return s;+}++static bool terminated(const char *s, size_t size)+{+ return memchr(s, '\0', size);+}++/* key="value" in s, value its whole quoted text. */+static bool quoted_after(const char *s, size_t len, const char *key,+ const char *value)+{+ size_t k = strlen(key), v = strlen(value), i, j;+ bool match;++ for (i = 0; i + k + v + 2 <= len; i++) {+ match = s[i + k] == '=' && s[i + k + 1] == '"' &&+ s[i + k + 2 + v] == '"';+ for (j = 0; match && j < k; j++)+ match = s[i + j] == key[j];+ for (j = 0; match && j < v; j++)+ match = s[i + k + 2 + j] == value[j];+ if (match)+ return true;+ }+ return false;+}++void harness(void)+{+ char realm[REALM_MAX] = {}, service[OUT_MAX] = {}, scope[1] = {};+ char *header = text();+ size_t len = strlen(header);+ int ret;++ ret = parse_challenge(header, realm, sizeof(realm), service,+ sizeof(service), scope, sizeof(scope));+ __CPROVER_assert(terminated(realm, sizeof(realm)) &&+ terminated(service, sizeof(service)) &&+ terminated(scope, sizeof(scope)),+ "every value terminated inside its buffer");+ if (ret)+ return;+ __CPROVER_assert(!strncasecmp(header, "Bearer", 6),+ "only a Bearer challenge");+ __CPROVER_assert(realm[0] && quoted_after(header, len, "realm", realm),+ "a realm, quoted in the challenge");+ __CPROVER_assert(!service[0] ||+ quoted_after(header, len, "service", service),+ "a service quoted in the challenge, if any");+ __CPROVER_assert(!scope[0] || quoted_after(header, len, "scope", scope),+ "a scope quoted in the challenge, if any");+}diff --git a/testing/proofs/cbmc/bpm-oci-descriptor.c b/testing/proofs/cbmc/bpm-oci-descriptor.cnew file mode 100644index 00000000..9e282495--- /dev/null+++ b/testing/proofs/cbmc/bpm-oci-descriptor.c@@ -0,0 +1,109 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The layer descriptors and index entries util/bpm_oci.c and+ * util/bpm_package.c take from a registry, into an empty table (LEN 0) or a+ * full one (LEN 1): one is kept exactly when whole and while room, a layer's+ * digest "sha256:" and 64 hex digits, and the next read into a cleared one.+ */++#include "proof.h"+#include "../../../util/bpm_package.c"+#include "../../../util/bpm_oci.c"+#include "../../../util/hex.c"++static bool hex_digit(char c)+{+ return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') ||+ (c >= 'A' && c <= 'F');+}++static bool sha256_digest(const char *d)+{+ size_t i;++ if (memcmp(d, "sha256:", 7) || d[71])+ return false;+ for (i = 7; i < 71; i++)+ if (!hex_digit(d[i]))+ return false;+ return true;+}++static void terminate(char *s, size_t size)+{+ __CPROVER_assume(!s[size - 1]);+}++static void descriptor(void)+{+ struct bpm_descriptor items[1], was;+ struct desc_sink s;+ size_t i;+ int ret;++ s.items = items;+ s.max = 1;+ s.n = !!LEN;+ terminate(s.scratch.media, sizeof(s.scratch.media));+ terminate(s.scratch.digest, sizeof(s.scratch.digest));+ terminate(s.scratch.title, sizeof(s.scratch.title));+ was = s.scratch;+ ret = on_descriptor(&s);+ __CPROVER_assert(!ret == (!LEN && was.media[0] && was.size >= 0 &&+ sha256_digest(was.digest)),+ "a whole layer with a SHA-256 digest, while room");+ if (ret) {+ __CPROVER_assert(s.n == !!LEN, "a refused layer is not counted");+ return;+ }+ __CPROVER_assume(i < sizeof(was.digest));+ __CPROVER_assert(s.n == 1 && items[0].size == was.size &&+ items[0].digest[i] == was.digest[i] &&+ items[0].media[i] == was.media[i],+ "the layer kept as read");+ __CPROVER_assert(!s.scratch.media[0] && !s.scratch.digest[0] &&+ !s.scratch.title[0] && s.scratch.size == -1,+ "the next layer read into a cleared one");+}++static void index_entry(void)+{+ struct bpm_index_entry *items, was;+ struct index_sink s;+ size_t i;+ int ret;++ items = any_bytes(sizeof(*items));+ s.items = items;+ s.n = LEN ? BPM_MAX_ENTRIES : 0;+ terminate(s.scratch.name, sizeof(s.scratch.name));+ terminate(s.scratch.version, sizeof(s.scratch.version));+ was = s.scratch;+ ret = on_index_entry(&s);+ __CPROVER_assert(!ret == (!LEN && bpm_name_ok(was.name) &&+ bpm_version_ok(was.version) &&+ was.repository[0] && was.manifest_digest[0] &&+ was.package_digest[0] && was.package_size > 0),+ "a whole entry with a valid name, while room");+ if (ret) {+ __CPROVER_assert(s.n == (LEN ? BPM_MAX_ENTRIES : 0),+ "a refused entry is not counted");+ return;+ }+ __CPROVER_assume(i < sizeof(was.name));+ __CPROVER_assert(s.n == 1 && items[0].package_size == was.package_size &&+ items[0].name[i] == was.name[i] &&+ items[0].package_digest[i] == was.package_digest[i],+ "the entry kept as read");+ __CPROVER_assert(!s.scratch.name[0] && !s.scratch.repository[0] &&+ s.scratch.package_size == -1,+ "the next entry read into a cleared one");+}++void harness(void)+{+ descriptor();+ index_entry();+}diff --git a/testing/proofs/cbmc/bpm-oci-header.c b/testing/proofs/cbmc/bpm-oci-header.cnew file mode 100644index 00000000..b39c381d--- /dev/null+++ b/testing/proofs/cbmc/bpm-oci-header.c@@ -0,0 +1,60 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * bpm_header() of util/bpm_oci.c, libcurl's header callback, for every+ * header line of up to LINE_MAX bytes, NULs included: it takes the whole+ * line, and the challenge, media type and location it keeps are terminated+ * inside their fields, the media type without parameters, neither with a+ * line break.+ */++/* util/string.c is included behind a file that includes its header. */+#define STRING_INTERNAL+#define PROOF_MEMCPY_BYTEWISE++#include "proof.h"+#include "../../../util/bpm_oci.c"+#include "../../../util/string.c"++#define LINE_MAX 20++/* What the line leaves in a field ends inside the line's length. */+static bool short_string(const char *s)+{+ return memchr(s, '\0', LINE_MAX + 1);+}++static bool has_any(const char *s, const char *set)+{+ size_t i, j;++ for (i = 0; s[i]; i++)+ for (j = 0; set[j]; j++)+ if (s[i] == set[j])+ return true;+ return false;+}++void harness(void)+{+ char *line = any_bytes(LINE_MAX);+ struct bpm_http_response r = {};+ size_t n;++ __CPROVER_assume(n <= LINE_MAX);+ __CPROVER_assert(bpm_header(line, 1, n, &r) == n,+ "the whole line taken");+ __CPROVER_assert(short_string(r.www_auth) &&+ short_string(r.content_type) &&+ short_string(r.location),+ "every value terminated inside its field");+ __CPROVER_assert(!has_any(r.content_type, " ;\r\n"),+ "a media type alone");+ __CPROVER_assert(!has_any(r.location, "\r\n"),+ "a location without its line break");+ __CPROVER_assert(!isspace((__u8)r.www_auth[0]) &&+ !isspace((__u8)r.content_type[0]) &&+ !isspace((__u8)r.location[0]),+ "no value starts with a blank");+}diff --git a/testing/proofs/cbmc/bpm-oci-strings.c b/testing/proofs/cbmc/bpm-oci-strings.cnew file mode 100644index 00000000..80a26eba--- /dev/null+++ b/testing/proofs/cbmc/bpm-oci-strings.c@@ -0,0 +1,94 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The string helpers of util/bpm_oci.c, for every string of up to STR_MAX+ * bytes and buffer of up to OUT_MAX: bpm_trim() and bpm_copy_string() leave+ * the string, trimmed for the first, or what fits of it, terminated, and+ * bpm_digest_matches() takes "sha256:" and the hex in either case.+ */++#include "proof.h"+#include "../../../util/bpm_oci.c"++#define STR_MAX 6+#define OUT_MAX 4++static bool blank(char c)+{+ return c == ' ' || (c >= '\t' && c <= '\r');+}++static char *any_string(void)+{+ char *s = any_bytes(STR_MAX + 1);++ __CPROVER_assume(!s[STR_MAX]);+ return s;+}++static void trim(void)+{+ char *in = any_string(), *out = any_bytes(OUT_MAX);+ size_t size, len, first, last;+ int ret;++ __CPROVER_assume(size <= OUT_MAX);+ len = strlen(in);+ for (first = 0; first < len && blank(in[first]); first++)+ ;+ for (last = len; last > first && blank(in[last - 1]); last--)+ ;+ ret = bpm_trim(in, out, size);+ if (!size) {+ __CPROVER_assert(ret == -E2BIG, "no room is -E2BIG");+ return;+ }+ if (last - first < size) {+ __CPROVER_assert(!ret && !memcmp(out, in + first, last - first) &&+ !out[last - first],+ "the string without the blanks around it");+ return;+ }+ __CPROVER_assert(ret == -E2BIG &&+ !memcmp(out, in + first, size - 1) && !out[size - 1],+ "what fits of it, and -E2BIG");+}++static void copy_string(void)+{+ char *in = any_string(), *out = any_bytes(OUT_MAX);+ size_t size, n;++ __CPROVER_assume(size >= 1 && size <= OUT_MAX);+ bpm_copy_string(out, size, in);+ n = min(strlen(in), size - 1);+ __CPROVER_assert(!memcmp(out, in, n) && !out[n],+ "what fits of the string, terminated");+}++static char lower(char c)+{+ return c >= 'A' && c <= 'Z' ? c - 'A' + 'a' : c;+}++static void digest_matches(void)+{+ char *want = any_bytes(STR_MAX + 8), *hex = any_string();+ bool same = !memcmp(want, "sha256:", 7);+ size_t i;++ __CPROVER_assume(!want[STR_MAX + 7]);+ for (i = 0; same && (want[7 + i] || hex[i]); i++)+ same = lower(want[7 + i]) == lower(hex[i]);+ __CPROVER_assert(bpm_digest_matches(want, hex) == same,+ "sha256: and the hex in either case");+ __CPROVER_assert(!bpm_digest_matches(NULL, hex), "no digest, no match");+}++void harness(void)+{+ trim();+ copy_string();+ digest_matches();+}diff --git a/testing/proofs/cbmc/bpm-oci-token.c b/testing/proofs/cbmc/bpm-oci-token.cnew file mode 100644index 00000000..eec928d7--- /dev/null+++ b/testing/proofs/cbmc/bpm-oci-token.c@@ -0,0 +1,73 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * token_from_body() of util/bpm_oci.c, on a token server's answer of up to+ * TEXT_MAX bytes: the token it takes is a quoted value of an answer that+ * names one, terminated and fit for a header line, and a refused answer+ * writes no token.+ */++#include "proof.h"+#include "../../../util/bpm_oci.c"++#define TEXT_MAX 17+#define OUT_MAX 3++static char *text(void)+{+ char *s = any_bytes(TEXT_MAX + 1);++ __CPROVER_assume(!s[TEXT_MAX]);+ return s;+}++static bool terminated(const char *s, size_t size)+{+ return memchr(s, '\0', size);+}++static bool header_safe(const char *s)+{+ for (; *s; s++)+ if ((__u8)*s < 0x21 || *s == '\\' || *s == '"' ||+ (__u8)*s == 0x7f)+ return false;+ return true;+}++static bool quoted_in(const char *s, size_t len, const char *value)+{+ size_t v = strlen(value), i, j;+ bool match;++ for (i = 0; i + v + 2 <= len; i++) {+ match = s[i] == '"' && s[i + v + 1] == '"';+ for (j = 0; match && j < v; j++)+ match = s[i + 1 + j] == value[j];+ if (match)+ return true;+ }+ return false;+}++void harness(void)+{+ char out[OUT_MAX], was[OUT_MAX];+ char *body = text();+ size_t len = strlen(body);++ memcpy(was, out, sizeof(out));+ if (token_from_body(body, out, sizeof(out))) {+ __CPROVER_assert(!memcmp(out, was, sizeof(out)),+ "a refused answer writes no token");+ return;+ }+ __CPROVER_assert(terminated(out, sizeof(out)), "a terminated token");+ __CPROVER_assert(header_safe(out),+ "no blank, control, quote or backslash in the token");+ __CPROVER_assert(quoted_in(body, len, out) &&+ (strstr(body, "\"token\"") ||+ strstr(body, "\"access_token\"")),+ "a quoted value of an answer naming a token");+}diff --git a/testing/proofs/cbmc/bpm-semver-order.c b/testing/proofs/cbmc/bpm-semver-order.cnew file mode 100644index 00000000..21566dc2--- /dev/null+++ b/testing/proofs/cbmc/bpm-semver-order.c@@ -0,0 +1,136 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * bpm_semver_compare() of util/bpm_package.c on every pair of strings of up+ * to STR_MAX bytes: antisymmetric, a tie only for versions apart in build+ * metadata, every SemVer version above any other string, and major, minor+ * and patch deciding by number before a pre-release ranks below its+ * release.+ */++/* util/string.c is included behind a file that includes its header. */+#define STRING_INTERNAL++#include "proof.h"+#include "../../../util/bpm_package.c"+#include "../../../util/string.c"++#define STR_MAX 7++static char *any_string(void)+{+ char *s = any_bytes(STR_MAX + 1);++ __CPROVER_assume(!s[STR_MAX]);+ return s;+}++/* The part precedence reads: up to a build's '+', or all of it. */+static size_t precedence_len(const char *s)+{+ size_t i;++ for (i = 0; s[i] && s[i] != '+'; i++)+ ;+ return i;+}++static size_t core_len(const char *s)+{+ size_t i;++ for (i = 0; s[i] && s[i] != '+' && s[i] != '-'; i++)+ ;+ return i;+}++static bool same_prefix(const char *a, size_t la, const char *b, size_t lb)+{+ size_t i;++ if (la != lb)+ return false;+ for (i = 0; i < la; i++)+ if (a[i] != b[i])+ return false;+ return true;+}++static size_t field_len(const char *s, size_t len)+{+ size_t i;++ for (i = 0; i < len && s[i] != '.'; i++)+ ;+ return i;+}++/*+ * Numbers without leading zeros: the longer is larger, and of two as long+ * the first digit that differs decides.+ */+static int number_order(const char *a, size_t la, const char *b, size_t lb)+{+ size_t i;++ if (la != lb)+ return la < lb ? -1 : 1;+ for (i = 0; i < la; i++)+ if (a[i] != b[i])+ return a[i] < b[i] ? -1 : 1;+ return 0;+}++/* Major, minor and patch of two valid cores, in that order. */+static int core_order(const char *a, const char *b)+{+ size_t la = core_len(a), lb = core_len(b), na, nb;+ int part, c;++ for (part = 0; part < 3; part++) {+ na = field_len(a, la);+ nb = field_len(b, lb);+ c = number_order(a, na, b, nb);+ if (c || part == 2)+ return c;+ a += na + 1;+ la -= na + 1;+ b += nb + 1;+ lb -= nb + 1;+ }+ return 0;+}++static void pair(void)+{+ char *a = any_string(), *b = any_string();+ bool va = bpm_semver_valid(a), vb = bpm_semver_valid(b);+ int ab = bpm_semver_compare(a, b), ba = bpm_semver_compare(b, a);+ size_t ca = core_len(a), cb = core_len(b);+ bool tie;++ __CPROVER_assert(ab >= -1 && ab <= 1, "below, equal or above");+ __CPROVER_assert(ab == -ba, "antisymmetric");+ tie = va && vb ?+ same_prefix(a, precedence_len(a), b, precedence_len(b)) :+ !strcmp(a, b);+ __CPROVER_assert(!ab == tie,+ "a tie only for versions apart in build metadata");+ if (va != vb)+ __CPROVER_assert(ab == (va ? 1 : -1),+ "a SemVer version above any other string");+ if (!va || !vb)+ return;+ if (!same_prefix(a, ca, b, cb))+ __CPROVER_assert(ab == core_order(a, b),+ "major, minor and patch decide by number");+ else if (a[ca] == '-' && b[cb] != '-')+ __CPROVER_assert(ab == -1,+ "a pre-release below the release of its core");+}++void harness(void)+{+ pair();+}diff --git a/testing/proofs/cbmc/bpm-semver-prerelease.c b/testing/proofs/cbmc/bpm-semver-prerelease.cnew file mode 100644index 00000000..a1b23919--- /dev/null+++ b/testing/proofs/cbmc/bpm-semver-prerelease.c@@ -0,0 +1,154 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The pre-release order of util/bpm_package.c, SemVer 2.0.0 11.4, on every+ * pair of valid pre-releases of up to LIST_MAX bytes: identifiers compare+ * left to right, numeric ones by value and below alphanumeric ones, those+ * by their ASCII bytes, and a list after a shorter one it starts with.+ */++/* util/string.c is included behind a file that includes its header. */+#define STRING_INTERNAL++#include "proof.h"+#include "../../../util/bpm_package.c"+#include "../../../util/string.c"++#define IDENT_MAX 3+#define LIST_MAX 5++static bool digit(char c)+{+ return c >= '0' && c <= '9';+}++static bool ident_char(char c)+{+ return digit(c) || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||+ c == '-';+}++static bool numeric(const char *s, size_t len)+{+ size_t i;++ for (i = 0; i < len; i++)+ if (!digit(s[i]))+ return false;+ return true;+}++/* Dot-separated identifiers; a numeric one has no leading 0. */+static bool valid(const char *s, size_t len)+{+ size_t start = 0, i;++ for (i = 0; i <= len; i++) {+ if (i < len && s[i] != '.') {+ if (!ident_char(s[i]))+ return false;+ continue;+ }+ if (i == start || (numeric(s + start, i - start) &&+ i - start > 1 && s[start] == '0'))+ return false;+ start = i + 1;+ }+ return true;+}++static __u32 value(const char *s, size_t len)+{+ __u32 v = 0;+ size_t i;++ for (i = 0; i < len; i++)+ v = v * 10 + (__u32)(s[i] - '0');+ return v;+}++static int ascii_order(const char *a, size_t la, const char *b, size_t lb)+{+ size_t i;++ for (i = 0; i < la && i < lb; i++)+ if (a[i] != b[i])+ return (__u8)a[i] < (__u8)b[i] ? -1 : 1;+ return la == lb ? 0 : la < lb ? -1 : 1;+}++static size_t first_len(const char *s, size_t len)+{+ size_t i;++ for (i = 0; i < len && s[i] != '.'; i++)+ ;+ return i;+}++static char *list(size_t *len, size_t max)+{+ char *s = any_bytes(max);++ __CPROVER_assume(*len <= max && valid(s, *len));+ return s;+}++/* 11.4.1 to 11.4.3: one identifier against another. */+static void identifier(void)+{+ size_t la, lb;+ char *a = list(&la, IDENT_MAX), *b = list(&lb, IDENT_MAX);+ bool na = numeric(a, la), nb = numeric(b, lb);+ __u32 va, vb;+ int c;++ __CPROVER_assume(first_len(a, la) == la && first_len(b, lb) == lb);+ c = prerelease_compare(a, la, b, lb);+ if (na && nb) {+ va = value(a, la);+ vb = value(b, lb);+ __CPROVER_assert(c == (va == vb ? 0 : va < vb ? -1 : 1),+ "numeric identifiers by value");+ } else if (na != nb) {+ __CPROVER_assert(c == (na ? -1 : 1),+ "a numeric identifier below an alphanumeric one");+ } else {+ __CPROVER_assert(c == ascii_order(a, la, b, lb),+ "alphanumeric identifiers by their ASCII bytes");+ }+}++/* 11.4: the first identifiers that differ decide, else the longer list. */+static void lists(void)+{+ size_t la, lb, fa, fb;+ char *a = list(&la, LIST_MAX), *b = list(&lb, LIST_MAX);+ int c;++ __CPROVER_assume(la && lb);+ c = prerelease_compare(a, la, b, lb);+ __CPROVER_assert(c == -prerelease_compare(b, lb, a, la),+ "antisymmetric");+ fa = first_len(a, la);+ fb = first_len(b, lb);+ if (fa != fb || memcmp(a, b, fa))+ __CPROVER_assert(c == prerelease_compare(a, fa, b, fb),+ "the first identifiers decide when they differ");+ else if (fa < la && fb < lb)+ __CPROVER_assert(c == prerelease_compare(a + fa + 1,+ la - fa - 1,+ b + fb + 1,+ lb - fb - 1),+ "else the rest of the lists");+ else+ __CPROVER_assert(c == (la == lb ? 0 : la < lb ? -1 : 1),+ "else the longer list, with more identifiers");+}++void harness(void)+{+ identifier();+ lists();+}diff --git a/testing/proofs/cbmc/bpm-semver-transitive.c b/testing/proofs/cbmc/bpm-semver-transitive.cnew file mode 100644index 00000000..d66f9500--- /dev/null+++ b/testing/proofs/cbmc/bpm-semver-transitive.c@@ -0,0 +1,33 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * bpm_semver_compare() of util/bpm_package.c is transitive over every three+ * strings of up to STR_MAX bytes, SemVer versions or not, so that with its+ * antisymmetry it orders every version a repository can name.+ */++/* util/string.c is included behind a file that includes its header. */+#define STRING_INTERNAL++#include "proof.h"+#include "../../../util/bpm_package.c"+#include "../../../util/string.c"++#define STR_MAX 7++static char *any_string(void)+{+ char *s = any_bytes(STR_MAX + 1);++ __CPROVER_assume(!s[STR_MAX]);+ return s;+}++void harness(void)+{+ char *a = any_string(), *b = any_string(), *c = any_string();++ if (bpm_semver_compare(a, b) <= 0 && bpm_semver_compare(b, c) <= 0)+ __CPROVER_assert(bpm_semver_compare(a, c) <= 0, "transitive");+}diff --git a/testing/proofs/cbmc/bpm-semver-valid.c b/testing/proofs/cbmc/bpm-semver-valid.cnew file mode 100644index 00000000..2476020e--- /dev/null+++ b/testing/proofs/cbmc/bpm-semver-valid.c@@ -0,0 +1,117 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * bpm_semver_valid() of util/bpm_package.c on every string of up to+ * STR_MAX bytes: it takes exactly the versions of the SemVer 2.0.0+ * grammar, and the pre-release it finds in one is the part between the+ * core and any build.+ */++/* util/string.c is included behind a file that includes its header. */+#define STRING_INTERNAL++#include "proof.h"+#include "../../../util/bpm_package.c"+#include "../../../util/string.c"++#define STR_MAX 11++static bool digit(char c)+{+ return c >= '0' && c <= '9';+}++static bool ident_char(char c)+{+ return digit(c) || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||+ c == '-';+}++static bool numeric(const char *s, size_t from, size_t to)+{+ size_t i;++ for (i = from; i < to; i++)+ if (!digit(s[i]))+ return false;+ return true;+}++/* <numeric identifier>: "0", or digits that do not start with 0. */+static bool numeric_identifier(const char *s, size_t from, size_t to)+{+ return from < to && numeric(s, from, to) &&+ (to - from == 1 || s[from] != '0');+}++/* Dot-separated identifiers; a numeric pre-release one has no leading 0. */+static bool identifiers(const char *s, size_t from, size_t to, bool pre)+{+ size_t start = from, i;++ for (i = from; i <= to; i++) {+ if (i < to && s[i] != '.') {+ if (!ident_char(s[i]))+ return false;+ continue;+ }+ if (i == start)+ return false;+ if (pre && numeric(s, start, i) &&+ !numeric_identifier(s, start, i))+ return false;+ start = i + 1;+ }+ return true;+}++/* <version core>: three numeric identifiers and two dots. */+static bool core(const char *s, size_t to)+{+ size_t start = 0, parts = 0, i;++ for (i = 0; i <= to; i++) {+ if (i < to && s[i] != '.')+ continue;+ if (!numeric_identifier(s, start, i))+ return false;+ parts++;+ start = i + 1;+ }+ return parts == 3;+}++static size_t first(const char *s, size_t len, char c)+{+ size_t i;++ for (i = 0; i < len && s[i] != c; i++)+ ;+ return i;+}++void harness(void)+{+ size_t len, plus, minus;+ struct semver v;+ bool grammar;+ char *s;++ s = any_bytes(STR_MAX + 1);+ __CPROVER_assume(!s[STR_MAX]);+ len = strnlen(s, STR_MAX);+ plus = first(s, len, '+');+ minus = first(s, plus, '-');+ grammar = core(s, minus) &&+ (minus == plus || identifiers(s, minus + 1, plus, true)) &&+ (plus == len || identifiers(s, plus + 1, len, false));+ __CPROVER_assert(bpm_semver_valid(s) == grammar,+ "exactly the SemVer 2.0.0 versions are valid");+ if (!semver_parse(s, &v))+ return;+ __CPROVER_assert(minus == plus ?+ !v.pre_len :+ v.pre == s + minus + 1 && v.pre_len == plus - minus - 1,+ "the pre-release between the core and the build");+}diff --git a/testing/proofs/cbmc/bpm-source-config.c b/testing/proofs/cbmc/bpm-source-config.cnew file mode 100644index 00000000..f938c129--- /dev/null+++ b/testing/proofs/cbmc/bpm-source-config.c@@ -0,0 +1,122 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The bpm source and tree config lines util/bpm_source_config.c reads, for+ * every line of up to LINE_MAX bytes: a setting is written only for its own+ * key and terminated inside its buffer, a source one led by no blank and+ * without a line break, a tree one unquoted without blanks around it.+ */++/* util/string.c is included behind a file that includes its header. */+#define STRING_INTERNAL++#include "proof.h"+#include "../../../util/bpm_source_config.c"+#include "../../../util/string.c"++#define LINE_MAX 12+#define OUT 4++static bool blank(char c)+{+ return c == ' ' || (c >= '\t' && c <= '\r');+}++static bool has(const char *s, char c)+{+ return memchr(s, c, strnlen(s, OUT));+}++/* Written for its key alone, and then a terminated value. */+static bool setting(const char *out, const char *was, bool mine, bool lead,+ bool trail)+{+ size_t n;++ if (!mine)+ return !memcmp(out, was, OUT);+ n = strnlen(out, OUT);+ return n < OUT && (!n || ((!lead || !blank(out[0])) &&+ (!trail || !blank(out[n - 1]))));+}++/* A value that starts with a quote, which keeps the blanks inside it. */+static bool quoted(const char *eq)+{+ if (!eq)+ return false;+ for (eq++; blank(*eq); eq++)+ ;+ return *eq == '"';+}++/* The key a parsed line leaves at its start, the blanks around it gone. */+static bool key_is(const char *line, const char *eq, const char *name)+{+ while (blank(*line))+ line++;+ return eq && !strcmp(line, name);+}++static char *any_line(void)+{+ char *line = any_bytes(LINE_MAX + 1);++ __CPROVER_assume(!line[LINE_MAX]);+ return line;+}++static void source_line(void)+{+ char endpoint[OUT], repo[OUT], key[OUT], e[OUT], r[OUT], k[OUT];+ char *line = any_line(), *eq = strchr(line, '=');+ bool is_e, is_r, is_k;++ memcpy(e, endpoint, OUT);+ memcpy(r, repo, OUT);+ memcpy(k, key, OUT);+ if (bpm_parse_source_line(line, endpoint, OUT, repo, OUT, key, OUT))+ return;+ is_e = key_is(line, eq, "endpoint");+ is_r = key_is(line, eq, "index_repository");+ is_k = key_is(line, eq, "key_file");+ __CPROVER_assert(setting(endpoint, e, is_e, true, false) &&+ setting(repo, r, is_r, true, false) &&+ setting(key, k, is_k, true, false),+ "a setting for its key, led by no blank");+ __CPROVER_assert((!is_e || (!has(endpoint, '\r') &&+ !has(endpoint, '\n'))) &&+ (!is_r || (!has(repo, '\r') && !has(repo, '\n'))) &&+ (!is_k || (!has(key, '\r') && !has(key, '\n'))),+ "no line break in a setting");+}++static void tree_line(void)+{+ char endpoint[OUT], reference[OUT], e[OUT], r[OUT];+ char *line = any_line(), *eq = strchr(line, '=');+ bool is_e, is_r, bare = !quoted(eq);+ int ret;++ memcpy(e, endpoint, OUT);+ memcpy(r, reference, OUT);+ ret = bpm_parse_tree_line(line, endpoint, OUT, reference, OUT);+ if (ret == -EINVAL) {+ __CPROVER_assert(eq, "a refused line has a value");+ return;+ }+ if (ret)+ return;+ is_e = key_is(line, eq, "endpoint");+ is_r = key_is(line, eq, "reference");+ __CPROVER_assert(setting(endpoint, e, is_e, bare, bare) &&+ setting(reference, r, is_r, bare, bare),+ "a setting for its key, unquoted without blanks around");+}++void harness(void)+{+ source_line();+ tree_line();+}diff --git a/testing/proofs/cbmc/bpm-tar-header.c b/testing/proofs/cbmc/bpm-tar-header.cnew file mode 100644index 00000000..4a73a046--- /dev/null+++ b/testing/proofs/cbmc/bpm-tar-header.c@@ -0,0 +1,60 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * bpm_tar_header_ok() of util/bpm_package.c: every 512-byte header with a+ * checksum field that is not octal fails, and a header of any bytes in a+ * WINDOW at LEN and in its checksum field, zero elsewhere, passes exactly+ * when the field holds its byte sum, the field's own bytes as blanks.+ */++#include "proof.h"+#include "../../../util/bpm_package.c"++#define TAR_HEADER 512+#define CHKSUM_OFF 148+#define CHKSUM_LEN 8+#define WINDOW 64++static bool in_field(size_t i)+{+ return i >= CHKSUM_OFF && i < CHKSUM_OFF + CHKSUM_LEN;+}++static void any_header(void)+{+ __u64 want;+ __u8 *h;++ h = any_bytes(TAR_HEADER);+ if (bpm_tar_octal((const char *)h + CHKSUM_OFF, CHKSUM_LEN, &want))+ __CPROVER_assert(!bpm_tar_header_ok(h),+ "a checksum field not octal fails");+}++static void windowed_header(void)+{+ __u64 want, sum = CHKSUM_LEN * ' ';+ size_t i;+ __u8 *h;++ h = any_bytes(TAR_HEADER);+ for (i = 0; i < TAR_HEADER; i++) {+ if (in_field(i))+ continue;+ if (i >= LEN && i < LEN + WINDOW)+ sum += h[i];+ else+ __CPROVER_assume(!h[i]);+ }+ if (bpm_tar_octal((const char *)h + CHKSUM_OFF, CHKSUM_LEN, &want))+ return;+ __CPROVER_assert(bpm_tar_header_ok(h) == (want == sum),+ "a header passes exactly when its checksum matches");+}++void harness(void)+{+ any_header();+ windowed_header();+}diff --git a/testing/proofs/cbmc/bpm-tar-octal.c b/testing/proofs/cbmc/bpm-tar-octal.cnew file mode 100644index 00000000..5976b2e4--- /dev/null+++ b/testing/proofs/cbmc/bpm-tar-octal.c@@ -0,0 +1,71 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * bpm_tar_octal() of util/bpm_package.c on every field of LEN bytes, the+ * size and checksum fields of a ustar header: it reads exactly octal+ * digits with nothing but blanks or NULs around them, and the value it+ * returns reads back as those digits.+ */++#include "proof.h"+#include "../../../util/bpm_package.c"++static bool blank(char c)+{+ return c == ' ' || c == '\0';+}++static bool octal(char c)+{+ return c >= '0' && c <= '7';+}++/* The one run of digits in p, from first to last, blanks around it. */+static bool octal_shape(const char *p, size_t n, size_t *first, size_t *last)+{+ bool in_run = false, after = false;+ size_t i;++ *first = n;+ *last = n;+ for (i = 0; i < n; i++) {+ if (octal(p[i]) && !after) {+ if (!in_run)+ *first = i;+ in_run = true;+ } else if (blank(p[i])) {+ if (in_run && !after)+ *last = i;+ after = in_run;+ } else {+ return false;+ }+ }+ return in_run;+}++void harness(void)+{+ size_t first, last, i;+ __u64 v, was;+ bool shaped;+ char *p;+ int ret;++ p = any_bytes(LEN);+ v = was;+ ret = bpm_tar_octal(p, LEN, &v);+ shaped = octal_shape(p, LEN, &first, &last);+ __CPROVER_assert(!ret == shaped, "exactly the octal fields are read");+ if (ret) {+ __CPROVER_assert(v == was, "a refusal leaves the value alone");+ return;+ }+ for (i = first; i < last; i++)+ __CPROVER_assert(((v >> (3 * (last - 1 - i))) & 7) ==+ (__u64)(p[i] - '0'),+ "each digit read back from the value");+ __CPROVER_assert(!(v >> (3 * (last - first))),+ "no bits beyond the digits");+}diff --git a/testing/proofs/cbmc/bpm-tree-reader.c b/testing/proofs/cbmc/bpm-tree-reader.cnew file mode 100644index 00000000..4f35c459--- /dev/null+++ b/testing/proofs/cbmc/bpm-tree-reader.c@@ -0,0 +1,117 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * What tree_parse() of util/bpm_tree.c keeps of a publication from a+ * registry, as its parser hands it over: a kconfig or label key only once,+ * with a value and below its limit, and an operation only whole, with a+ * positive seq, no empty selector list and room in the publication.+ */++/* util/malloc.c and util/string.c behind a file that includes their headers. */+#define MALLOC_INTERNAL+#define STRING_INTERNAL++#include "proof.h"+#include "../../../util/bpm_tree.c"+#include "../../../util/malloc.c"+#include "../../../util/string.c"++#define TEXT_MAX 3++static void map_pair(void)+{+ struct tree_reader reader, *r = &reader;+ struct tree_selector_kv *kvs = NULL;+ struct tree_map m = { r, &kvs, NULL, 0 };+ int n = LEN, n_pair = r->n_pair, max;+ char *key = any_bytes(TEXT_MAX + 1);+ bool known = false;+ size_t i, j;+ int ret;++ m.n = &n;+ m.max = max;+ __CPROVER_assume(max >= 0 && n_pair >= 0 &&+ n_pair <= TREE_MAX_SELECTOR_VALUES && !key[TEXT_MAX] &&+ i < TREE_STRING_MAX && j < TREE_MAX_SELECTOR_VALUES);+ if (LEN) {+ kvs = __CPROVER_allocate(sizeof(*kvs), 0);+ __CPROVER_assume(kvs && !kvs[0].key[TEXT_MAX]);+ known = !strcmp(kvs[0].key, key);+ }+ ret = tree_map_pair(&m, key);+ if (ret) {+ __CPROVER_assert(!n_pair || known || max <= LEN ||+ ret == -ENOMEM,+ "refused without a value, twice or at the limit");+ __CPROVER_assert(n == LEN, "a refused key is not counted");+ return;+ }+ __CPROVER_assert(n_pair && !known && max > LEN,+ "a new key with a value, below the limit");+ __CPROVER_assert(n == LEN + 1 && !strcmp(kvs[LEN].key, key) &&+ kvs[LEN].n_values == n_pair &&+ kvs[LEN].values[j][i] == r->pair[j][i] && !r->n_pair,+ "the key with its values, the next pair empty");+}++static void take_operation(void)+{+ struct tree_reader reader, *r = &reader;+ struct tree_operation *o = &r->op;+ const struct tree_selector *s = &o->selector;+ struct tree_publication pub = {};+ struct json_field lists[3];+ bool whole, lists_ok;+ __s64 seq = o->seq;+ size_t i;+ char id;+ int ret;++ r->pub = &pub;+ r->lists = lists;+ o->selector.kconfig = NULL;+ o->selector.n_kconfig = 0;+ o->selector.labels = NULL;+ o->selector.n_labels = 0;+ pub.n_operations = LEN ? TREE_MAX_OPS : 0;+ pub.operations_capacity = LEN ? TREE_MAX_OPS : 1;+ if (!LEN) {+ pub.operations = __CPROVER_allocate(sizeof(*pub.operations), 0);+ __CPROVER_assume(pub.operations);+ }+ __CPROVER_assume(s->n_kernel_release_prefix >= 0 &&+ s->n_kernel_build_id >= 0 && s->n_architecture >= 0 &&+ i < sizeof(o->id));+ id = o->id[i];+ whole = o->id[0] && o->op[0] && seq > 0 && o->operation_digest[0] &&+ o->artifact.repository[0] && o->artifact.manifest_digest[0] &&+ o->artifact.layer_digest[0] && o->artifact.payload_sha256[0];+ lists_ok = (!lists[0].seen || s->n_kernel_release_prefix) &&+ (!lists[1].seen || s->n_kernel_build_id) &&+ (!lists[2].seen || s->n_architecture);+ ret = tree_take_operation(r);+ if (ret) {+ __CPROVER_assert(!whole || !lists_ok || LEN || ret == -ENOMEM,+ "only a broken operation or a full tree refused");+ __CPROVER_assert(pub.n_operations == (LEN ? TREE_MAX_OPS : 0),+ "a refused operation is not counted");+ return;+ }+ __CPROVER_assert(whole && lists_ok && !LEN,+ "a whole operation kept, while room");+ __CPROVER_assert(pub.n_operations == 1 &&+ pub.operations[0].seq == seq &&+ pub.operations[0].id[i] == id,+ "the operation kept");+ __CPROVER_assert(!o->seq && !o->id[0] && !o->selector.kconfig &&+ !o->selector.labels,+ "the next operation read into a cleared one");+}++void harness(void)+{+ map_pair();+ take_operation();+}diff --git a/testing/proofs/cbmc/bpm-tree-selector.c b/testing/proofs/cbmc/bpm-tree-selector.cnew file mode 100644index 00000000..c524184d--- /dev/null+++ b/testing/proofs/cbmc/bpm-tree-selector.c@@ -0,0 +1,147 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The selector pairs of util/bpm_tree.c, which a tree publication fills+ * from a registry, LEN of them: a pair is added only below its limit, as a+ * new zeroed one after those it keeps, a copy owns its pairs and a freed+ * selector holds none, and a tree string has no control byte.+ */++/* util/malloc.c and util/string.c behind a file that includes their headers. */+#define MALLOC_INTERNAL+#define STRING_INTERNAL++#include "proof.h"+#include "../../../util/bpm_tree.c"+#include "../../../util/malloc.c"+#include "../../../util/string.c"++#define KEY_MAX 3+#define TEXT_MAX 8++static void text(void)+{+ char *s = any_bytes(TEXT_MAX + 1);+ bool control = false;+ size_t i;++ __CPROVER_assume(!s[TEXT_MAX]);+ for (i = 0; s[i]; i++)+ control |= (__u8)s[i] < 0x20;+ __CPROVER_assert(tree_text(s) == !control,+ "a tree string has no control byte");+}++static struct tree_selector_kv *pairs(int n)+{+ struct tree_selector_kv *kvs;+ int i;++ if (!n)+ return NULL;+ kvs = __CPROVER_allocate((size_t)n * sizeof(*kvs), 0);+ __CPROVER_assume(kvs);+ for (i = 0; i < n; i++)+ __CPROVER_assume(!kvs[i].key[KEY_MAX]);+ return kvs;+}++/* A byte of every part of a pair, at any place. */+struct probe {+ size_t i, j;+};++static struct probe any_probe(void)+{+ struct probe p;++ __CPROVER_assume(p.i < TREE_STRING_MAX &&+ p.j < TREE_MAX_SELECTOR_VALUES);+ return p;+}++/* The probed bytes of a pair, to hold against another. */+struct sample {+ char key, value;+ int n_values;+};++static struct sample sample(const struct tree_selector_kv *kv,+ struct probe p)+{+ struct sample s = {+ kv->key[p.i], kv->values[p.j][p.i], kv->n_values,+ };++ return s;+}++static bool same(const struct tree_selector_kv *kv, struct sample s,+ struct probe p)+{+ return kv->key[p.i] == s.key && kv->values[p.j][p.i] == s.value &&+ kv->n_values == s.n_values;+}++static bool zeroed(const struct tree_selector_kv *kv, struct probe p)+{+ return !kv->key[p.i] && !kv->values[p.j][p.i] && !kv->n_values;+}++static void add(void)+{+ struct tree_selector_kv *kvs, *was, *kv = NULL;+ struct probe p = any_probe();+ int n = LEN, max, ret, e;+ struct sample kept;++ __CPROVER_assume(max >= 0 && e >= 0 && e < LEN + !LEN);+ kvs = pairs(n);+ was = kvs;+ if (n)+ kept = sample(&kvs[e], p);+ ret = tree_selector_kv_add(&kvs, &n, max, &kv);+ if (ret) {+ __CPROVER_assert(kvs == was && n == LEN && !kv,+ "a refusal changes nothing");+ __CPROVER_assert((ret == -E2BIG) == (max <= LEN),+ "-E2BIG exactly at the limit");+ return;+ }+ __CPROVER_assert(max > LEN && n == LEN + 1 && kv == kvs + LEN,+ "one more pair, the new one handed back");+ __CPROVER_assert(zeroed(kv, p), "the new pair zeroed");+ __CPROVER_assert(!LEN || same(&kvs[e], kept, p),+ "the pairs before it kept");+}++static void copy(void)+{+ struct tree_selector src, dst;++ src.n_kconfig = LEN;+ src.n_labels = LEN;+ src.kconfig = pairs(LEN);+ src.labels = pairs(LEN);+ if (tree_selector_copy(&dst, &src)) {+ __CPROVER_assert(!dst.kconfig && !dst.labels && !dst.n_kconfig &&+ !dst.n_labels, "a failed copy holds nothing");+ return;+ }+ __CPROVER_assert(dst.n_kconfig == LEN && dst.n_labels == LEN,+ "as many pairs");+ __CPROVER_assert(!LEN || (dst.kconfig != src.kconfig &&+ dst.labels != src.labels),+ "pairs of its own");+ tree_selector_free(&dst);+ __CPROVER_assert(!dst.kconfig && !dst.labels && !dst.n_kconfig &&+ !dst.n_labels, "a freed selector holds nothing");+}++void harness(void)+{+ text();+ add();+ copy();+}diff --git a/testing/proofs/cbmc/proof.h b/testing/proofs/cbmc/proof.hindex ecda1d4d..3e9ed38b 100644--- a/testing/proofs/cbmc/proof.h+++ b/testing/proofs/cbmc/proof.h@@ -9,6 +9,13 @@#ifndef SHIRUDO_PROOF_H#define SHIRUDO_PROOF_H+/*+ * glibc's <ctype.h> macros index a locale table CBMC has no body for; the+ * functions they stand for CBMC models in the C locale, the one shirudo+ * runs in.+ */+#define __NO_CTYPE 1+#include <stdarg.h>#include <stdbool.h>#include <stddef.h>@@ -18,8 +25,8 @@#include <shirudo/types.h>/*- * CBMC models neither strnlen(), memchr(), strstr() nor vsnprintf(), and a- * function without a body returns anything.+ * CBMC models neither strnlen(), memchr(), strcspn(), strstr() nor+ * vsnprintf(), and a function without a body returns anything.*/size_t strnlen(const char *s, size_t max){@@ -62,6 +69,17 @@ void *memchr(const void *s, int c, size_t n)}#endif+size_t strcspn(const char *s, const char *reject)+{+ size_t n, i;++ for (n = 0; s[n]; n++)+ for (i = 0; reject[i]; i++)+ if (s[n] == reject[i])+ return n;+ return n;+}+char *strstr(const char *s, const char *needle){size_t n = strlen(needle);diff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.listindex 550e69fd..cd736767 100644--- a/testing/proofs/cbmc/proofs.list+++ b/testing/proofs/cbmc/proofs.list@@ -55,3 +55,19 @@ otel-retry-after -I ../../../vendor/libbpf -I ../../../vendor/libbpf/include/uapjson-walk -DJSON_MAX_DEPTH=2 --unwind 3 --unwindset string_end.0:2,code_unit.0:0,hex4.0:0,parse_string.0:1,parse_string.1:2,literal_end.0:4,json_text.0:3,json_text.1:3,utf8_width.0:1,parse_array.0:1,parse_object.0:1,parse_object.1:1,digits_end.0:3,digits.0:3,skip_ws.0:3,strlen.0:6,memcmp.0:1,memchr.0:3,strnlen.0:4 LEN=0-2json-limits -DJSON_MAX_DEPTH=2 -DJSON_MAX_BYTES=24 --unwind 8json-field --unwind 1 --unwindset digits_end.0:7,decimal.0:7,harness.0:7,skip_ws.0:7,parse_string.1:7,parse_string.0:4,hex4.0:0,utf8_width.0:4,parse_base.0:7,parse_base.1:7,digits.0:7,strnlen.0:4,strlen.0:6,memcmp.0:6 LEN=0-6+bpm-tar-octal --unwind 13 LEN=8,12+bpm-tar-header --unwind 9 --unwindset bpm_tar_header_ok.0:513,windowed_header.0:513 --max-field-sensitivity-array-size 512 LEN=0,116,448+bpm-archive-path --unwind 12 LEN=0-10+bpm-semver-valid --unwind 13+bpm-semver-order --unwind 9 --unwindset prerelease_compare.0:2,memchr.0:2,memcmp.0:2,strcspn.1:2+bpm-semver-transitive --unwind 9 --unwindset prerelease_compare.0:2,memchr.0:2,memcmp.0:2,strcspn.1:2+bpm-semver-prerelease --unwind 7+bpm-oci-challenge --unwind 18+bpm-oci-token --unwind 18+bpm-oci-header --unwind 22+bpm-oci-descriptor --unwind 130 LEN=0,1+bpm-names --unwind 131+bpm-tree-selector --unwind 10 --malloc-may-fail --malloc-fail-null LEN=0,1+bpm-tree-reader --unwind 6 --malloc-may-fail --malloc-fail-null LEN=0,1+bpm-source-config --unwind 14+bpm-oci-strings --unwind 9