Skip to content

eBPF-based security platform

testing: prove the Beefile reader and the package and index writers

Daniel Borkmann2f9c668b46ca
diff --git a/.agents/skills/proofs/SKILL.md b/.agents/skills/proofs/SKILL.md
index 5ca969e5..4210120c 100644
--- a/.agents/skills/proofs/SKILL.md
+++ b/.agents/skills/proofs/SKILL.md
@@ -95,10 +95,11 @@ gigabytes is one to reshape, not to wait for.
allocation a fixed size the harness bounds. A write through a pointer
into a big struct becomes an update of all of it; index the member in
place, as `sep()` of `util/json.c` does.
-- `proof.h` models `vsnprintf()` for `%s` and `%d`, `%u`, `%x` with a zero
- flag, a width and `ll`, and fails a proof on any other conversion;
- CBMC does not promote variadic arguments, so the model reads each by
- its size.
+- `proof.h` models `vsnprintf()` for `%s` and `%d`, `%u`, `%x`, `%o` with
+ a zero flag, a width, written out or `*`, and `ll`, and fails a proof
+ on any other conversion; CBMC does not promote variadic arguments, so
+ the model reads each by its size. `%o` writes each digit at a place
+ known beforehand, as `bpm-tar-writer` needs of the tar header's fields.
- A struct of tens of kilobytes, such as a tree operation, makes even a
few symbolic copies slow: prove the logic that reads its fields on a
smaller harness, or leave it to the unit cases.
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rst
index a2af5016..6867b065 100644
--- a/Documentation/contribute/testing/proof-coverage.rst
+++ b/Documentation/contribute/testing/proof-coverage.rst
@@ -262,6 +262,108 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.
- Lengths 0 to 18
- What install loads and uninstall unloads goes by the suffix alone;
a pair is terminated, the other half, and pairs back to the name
+ * - ``bpm-tar-writer``
+ - ``tar_entry()``, ``tar_octal()``, ``tar_write()`` of
+ :src:`tools/shirudo-build/bpm-build.c`, against
+ ``bpm_tar_header_ok()`` and ``bpm_tar_octal()``
+ - Names of 1, 99 and 100 bytes, every size and mode, a file or a
+ directory without content; the checksum for names that end in 16
+ free bytes; every value into a field of 8 and 12 bytes;
+ ``gzwrite()`` a stub that takes every write
+ - Refused before a byte is written from 100 bytes of name or 8 GiB of
+ content on; else one 512-byte header the reader takes, with its
+ checksum, the name terminated in its field, no prefix, and name,
+ size and type read back, uid, gid and mtime 0; content of up to
+ 2\ :sup:`31` - 1 bytes padded to where the reader looks for the
+ next header; a value wider than its field is refused
+ * - ``bpm-beefile-targets``
+ - ``build_target_ok()``, ``build_add_target()``, against
+ ``bpm_archive_rel_ok()``
+ - Targets of 0 to 8 bytes; up to 4 bytes into a table that holds one
+ target
+ - Takes exactly the paths the archive reader takes that have no
+ final slash; a target joins the table once, and a refused one is
+ not counted
+ * - ``bpm-beefile-members``
+ - ``build_entry_name()``, against ``package_member_name_ok()`` and
+ ``bpm_archive_rel_ok()``
+ - Targets of 0 to 6 bytes below a directory of 2, every buffer size
+ - Refused exactly when it does not fit, with nothing written; else
+ the directory, a slash and the target, terminated; a target bpm
+ build takes is the member the reader finds, and the directory's
+ own entry a path it takes
+ * - ``bpm-beefile-names``
+ - ``build_name_ok()``, against ``bpm_package_name_ok()`` and
+ ``bpm_name_ok()``
+ - Up to 129 bytes, past the 128-byte limit
+ - Takes exactly the names the target takes as a package's, each one
+ the repository index takes
+ * - ``bpm-beefile-version``
+ - ``build_version_ok()``, against ``bpm_version_ok()`` and
+ ``bpm_archive_rel_ok()``
+ - Versions of 5 to 9 bytes, behind a name of 1
+ - A version bpm build takes is one the target takes, and
+ ``<name>-<version>`` one component to the archive reader
+ * - ``bpm-beefile-string``
+ - ``build_parse_string()``, ``build_parse_value()``
+ - Values of 0 to 12 bytes, every buffer size up to one byte more
+ - Takes exactly a quoted string between blanks that fits, decoded as
+ a reference decoder does, with ``\n``, ``\r``, ``\t``, ``\\`` and
+ ``\"`` in a ``"..."`` string alone; nothing written past its
+ buffer; a value read past is a string or starts as an array
+ * - ``bpm-beefile-array``
+ - ``build_parse_string_array()``
+ - Values up to 10 bytes, a limit of 1 or 2 strings
+ - One string or more up to the limit, each terminated, between
+ brackets; no row past the limit written; a refused array is not
+ counted
+ * - ``bpm-beefile-lines``
+ - ``build_strip_comment()``, ``build_trim()``, ``build_bare_key()``
+ - Lines of 0 to 10 bytes; a string, a ``#`` and 2 bytes behind it
+ - A line is cut at a ``#`` or not at all, at the first one in a line
+ without a quote, and a string the reader takes stays whole before
+ its comment; trimming drops the blanks at both ends alone; a name
+ is bare parts apart by single dots
+ * - ``bpm-beefile-tables``
+ - ``build_parse_spec()``, ``build_add_target()``
+ - One table header after every count of its kind up to the limit;
+ ``[upatches.tree]`` and the sub-tables without an upatch; four
+ fixed targets
+ - ``[[files]]``, ``[[upatches]]``, ``[[upatches.rodata]]`` and
+ ``[[upatches.policy]]`` are counted below 128, 128, 16 and 64 and
+ refused there; a table without its upatch is refused and counted
+ nowhere; ``Beefile``, ``README.md``, ``cert.der`` and
+ ``build-environment.json`` are no targets
+ * - ``bpm-index-writer``
+ - ``push_index()`` of :src:`tools/shirudo-build/bpm-publish.c`,
+ against ``bpm_index_parse()``
+ - One package of fixed short strings into a namespace without an
+ index, of sizes 1, 2\ :sup:`63` - 1, 0 and -1; registry, clock
+ and signer stubs
+ - The document handed to the signer is one the target reads, with
+ the one package and each field as pushed, but for a size of 0 or
+ less, which the target refuses; one document signed, one manifest
+ put
+ * - ``bpm-index-append``
+ - ``push_index()``, ``fetch_index()``, ``index_entry_compare()``,
+ against ``bpm_index_parse()``
+ - An index of one package as the registry serves it, and a push of
+ another package, of the same version again, and of the same
+ version with another digest; registry, signature check, clock and
+ signer stubs
+ - The index signed next is one the target reads, with the pushed
+ package once and before the one listed, which is kept as it was;
+ a version with other content is refused and nothing signed
+ * - ``bpm-tree-writer``
+ - ``tree_emit_publication()``, against ``tree_parse()`` and
+ ``tree_publication_check()``
+ - Five fixed publications without operations: the first of a tree,
+ the last generation and sequence number, one past each, and one
+ without a tree ID
+ - Tree, generation, parent, base and head read back as written;
+ the target reads up to generation and sequence number
+ 2\ :sup:`63` - 2 and refuses the three ``bpm tree append`` does
+ not write
* - ``upatch-header``
- ``parse_header()``, ``validate_bounds()`` of the ``.upatch``
reader
@@ -930,12 +1032,6 @@ a proof covers.
- No proof is written yet. A harness could carry their logic, in
whole or in part: the package state and tree state readers, which
selectors of a tree can meet on one node and the status verdict
- * - :src:`tools/shirudo-build/bpm-build.c`,
- :src:`tools/shirudo-build/bpm-publish.c`
- - No proof is written yet. The operator's Beefile reader and its
- writers of the package, the index and the tree publication: the
- proofs cover what the target's readers accept, not what these
- write
* - :src:`util/harden.c`, :src:`util/identity.c`,
:src:`util/image_cache.c`, :src:`util/image_config.c`,
:src:`util/image_guards.c`, :src:`util/image_keys.c`,
@@ -1007,6 +1103,17 @@ of :src:`util/upatch_maps.c` that puts a page together from the chunks
limit of 64 CBMC ends a run on the code as it is and none on the code
broken, so no proof of the limit is kept.
+Within the operator's package builder and publisher, the proofs cover the
+tar header, the member names, the Beefile's values, lines, names and
+table limits, and the index and the tree publication on fixed documents.
+The Beefile's keys, defaults and the checks behind its last line,
+``build_validate()``, the rodata commands, ``build_archive()`` and what
+it puts into ``build-environment.json``, a publication with operations,
+the manifests, and the generation and sequence numbers ``bpm tree
+append`` counts up have no proof. A value of any length in a JSON
+document moves every byte behind it, and a tree operation takes 13 KiB
+in each of its copies: a proof over either runs for minutes.
+
Within the ``.upatch`` reader, the proofs cover its parts but not their
composition: ``upatch_parse()`` and ``upatch_open_memory()`` join the
header, the signed sections, the decoders and the policy labels the
diff --git a/testing/proofs/cbmc/bpm-beefile-array.c b/testing/proofs/cbmc/bpm-beefile-array.c
new file mode 100644
index 00000000..c5f5c98d
--- /dev/null
+++ b/testing/proofs/cbmc/bpm-beefile-array.c
@@ -0,0 +1,72 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * An array value of a Beefile, on every value of up to VALUE_MAX bytes:
+ * build_parse_string_array() of tools/shirudo-build/bpm-build.c takes one
+ * string or more up to its limit, each terminated in its row, between
+ * brackets, and counts none of an array it refuses.
+ */
+
+/* As proof.h, before the system headers can include <ctype.h>. */
+#define __NO_CTYPE 1
+
+#include <dirent.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <strings.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/utsname.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+#include <zlib.h>
+
+#define PROOF_MEMCPY_BYTEWISE
+#include "proof.h"
+#include "../../../util/string.c"
+#include "../../../tools/shirudo-build/bpm-build.c"
+
+#define VALUE_MAX 10
+#define ROWS 2
+
+static bool blank(char c)
+{
+ return c == ' ' || (c >= '\t' && c <= '\r');
+}
+
+void harness(void)
+{
+ char out[ROWS][TREE_STRING_MAX], was_byte;
+ size_t first = 0, last, j, k;
+ int n, was, max, ret;
+ char *v;
+
+ v = any_bytes(VALUE_MAX + 1);
+ v[VALUE_MAX] = '\0';
+ /* Every caller passes a limit of 16, the rows of its table. */
+ __CPROVER_assume(max >= 1 && max <= ROWS && j < TREE_STRING_MAX);
+ was = n;
+ was_byte = out[ROWS - 1][j];
+ ret = build_parse_string_array(v, out, max, &n);
+ __CPROVER_assert(max == ROWS || out[ROWS - 1][j] == was_byte,
+ "no row past the limit written");
+ if (ret) {
+ __CPROVER_assert(n == was, "a refused array is not counted");
+ return;
+ }
+ __CPROVER_assert(n >= 1 && n <= max, "one string or more, to the limit");
+ __CPROVER_assume(k < (size_t)n);
+ __CPROVER_assert(strnlen(out[k], VALUE_MAX + 1) <= VALUE_MAX,
+ "each string terminated");
+ last = strnlen(v, VALUE_MAX);
+ while (first < last && blank(v[first]))
+ first++;
+ while (last > first && blank(v[last - 1]))
+ last--;
+ __CPROVER_assert(last - first >= 2 && v[first] == '[' &&
+ v[last - 1] == ']', "between brackets");
+}
diff --git a/testing/proofs/cbmc/bpm-beefile-lines.c b/testing/proofs/cbmc/bpm-beefile-lines.c
new file mode 100644
index 00000000..01abf5ba
--- /dev/null
+++ b/testing/proofs/cbmc/bpm-beefile-lines.c
@@ -0,0 +1,136 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * A line of a Beefile as tools/shirudo-build/bpm-build.c cuts it up, on
+ * every line of LEN bytes: a comment is cut at a '#' and never inside a
+ * string the reader takes, trimming drops the blanks at both ends alone,
+ * and a table or key name is bare parts apart by single dots.
+ */
+
+/* As proof.h, before the system headers can include <ctype.h>. */
+#define __NO_CTYPE 1
+
+#include <dirent.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <strings.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/utsname.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+#include <zlib.h>
+
+#include "proof.h"
+#include "../../../util/string.c"
+#include "../../../tools/shirudo-build/bpm-build.c"
+
+/* What follows the '#' behind a string, any bytes. */
+#define TAIL 2
+
+static bool blank(char c)
+{
+ return c == ' ' || (c >= '\t' && c <= '\r');
+}
+
+static bool bare(char c)
+{
+ return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') ||
+ (c >= 'a' && c <= 'z') || c == '_' || c == '-';
+}
+
+/* Any string of len bytes, with room for more behind it. */
+static char *string(size_t len, size_t room)
+{
+ char *s = any_bytes(room + 1);
+
+ __CPROVER_assume(strnlen(s, len) == len);
+ s[len] = '\0';
+ return s;
+}
+
+static void comment_cut(void)
+{
+ char *line = string(LEN, LEN), was[LEN + 1];
+ bool quotes = false;
+ size_t i, n, hash, at;
+
+ for (i = 0; i <= LEN; i++) {
+ was[i] = line[i];
+ quotes |= line[i] == '"' || line[i] == '\'';
+ }
+ for (hash = 0; hash < LEN && was[hash] != '#'; hash++)
+ ;
+ build_strip_comment(line);
+ n = strnlen(line, LEN);
+ __CPROVER_assume(at <= LEN);
+ __CPROVER_assert(at == n || line[at] == was[at],
+ "no byte changed but the one that ends the line");
+ __CPROVER_assert(n == LEN || was[n] == '#', "cut at a '#' or not at all");
+ __CPROVER_assert(quotes || n == hash,
+ "at the first one of a line without a quote");
+}
+
+/* key = "a # b" # comment: the string stays whole and the comment goes. */
+static void string_kept(void)
+{
+ char *line = string(LEN, LEN + 1 + TAIL), out[LEN + 1], was[LEN];
+ size_t i;
+
+ if (build_parse_string(line, out, sizeof(out)))
+ return;
+ for (i = 0; i < LEN; i++)
+ was[i] = line[i];
+ line[LEN] = '#';
+ __CPROVER_assume(strnlen(line + LEN + 1, TAIL) == TAIL);
+ line[LEN + 1 + TAIL] = '\0';
+ build_strip_comment(line);
+ __CPROVER_assert(strnlen(line, LEN + 1) == LEN &&
+ !memcmp(line, was, LEN),
+ "a string the reader takes kept whole, its comment cut");
+}
+
+static void trimmed(void)
+{
+ char *line = string(LEN, LEN), was[LEN + 1];
+ size_t i, from, n, at;
+ char *s;
+
+ for (i = 0; i <= LEN; i++)
+ was[i] = line[i];
+ s = build_trim(line);
+ __CPROVER_assert(s >= line && s <= line + LEN, "inside the line");
+ from = (size_t)(s - line);
+ n = strnlen(s, LEN - from);
+ __CPROVER_assert(!n || (!blank(s[0]) && !blank(s[n - 1])),
+ "no blank at either end");
+ __CPROVER_assume(at < LEN);
+ __CPROVER_assert(at >= from && at < from + n ? s[at - from] == was[at] :
+ blank(was[at]), "nothing but blanks dropped");
+}
+
+static void named(void)
+{
+ char *key = string(LEN, LEN);
+ bool dots, parts = LEN > 0;
+ size_t i;
+
+ for (i = 0; i < LEN; i++)
+ parts &= bare(key[i]) ||
+ (dots && key[i] == '.' && i && i + 1 < LEN &&
+ key[i - 1] != '.' && key[i + 1] != '.');
+ __CPROVER_assert(build_bare_key(key, dots) == parts,
+ "bare parts, apart by single dots where dots may be");
+}
+
+void harness(void)
+{
+ comment_cut();
+ string_kept();
+ trimmed();
+ named();
+}
diff --git a/testing/proofs/cbmc/bpm-beefile-members.c b/testing/proofs/cbmc/bpm-beefile-members.c
new file mode 100644
index 00000000..7b58875f
--- /dev/null
+++ b/testing/proofs/cbmc/bpm-beefile-members.c
@@ -0,0 +1,80 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * build_entry_name() of tools/shirudo-build/bpm-build.c, which names every
+ * member bpm build writes, on every target of LEN bytes: the archive's
+ * directory, a slash and the target, where the readers of
+ * util/bpm_package.c find the member, or nothing when it does not fit.
+ */
+
+/* As proof.h, before the system headers can include <ctype.h>. */
+#define __NO_CTYPE 1
+
+#include <dirent.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <strings.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/utsname.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+#include <zlib.h>
+#include <openssl/bio.h>
+#include <openssl/evp.h>
+#include <openssl/pem.h>
+#include <openssl/x509.h>
+
+#define PROOF_MEMCPY_BYTEWISE
+#include "proof.h"
+#include "../../../util/string.c"
+#include "../../../util/bpm_package.c"
+#include "../../../tools/shirudo-build/bpm-build.c"
+
+/* The archive's directory, <name>-<version>, here of two bytes. */
+#define TOP 2
+#define NAME_MAX_LEN (TOP + 1 + LEN)
+
+static char *string(size_t len)
+{
+ char *s = any_bytes(len + 1);
+
+ __CPROVER_assume(strnlen(s, len) == len);
+ s[len] = '\0';
+ return s;
+}
+
+void harness(void)
+{
+ char name[NAME_MAX_LEN + 2], was;
+ char *top, *target;
+ size_t cap, i;
+ bool taken;
+ int ret;
+
+ top = string(TOP);
+ target = string(LEN);
+ /* One component, as bpm-beefile-names shows of <name>-<version>. */
+ __CPROVER_assume(bpm_archive_rel_ok(top) && !strchr(top, '/'));
+ __CPROVER_assume(cap <= sizeof(name) && i < sizeof(name));
+ was = name[i];
+ ret = build_entry_name(name, cap, top, target);
+ __CPROVER_assert(!ret == (cap > NAME_MAX_LEN),
+ "refused exactly when it does not fit");
+ if (ret) {
+ __CPROVER_assert(name[i] == was, "a refusal writes nothing");
+ return;
+ }
+ __CPROVER_assert(!name[NAME_MAX_LEN] && !memcmp(name, top, TOP) &&
+ name[TOP] == '/' && !memcmp(name + TOP + 1, target, LEN),
+ "the directory, a slash and the target, terminated");
+ taken = !build_target_ok(target);
+ __CPROVER_assert(!taken || package_member_name_ok(name, target),
+ "a target bpm build takes, found under its name");
+ __CPROVER_assert(LEN || (bpm_archive_rel_ok(name) && name[TOP] == '/'),
+ "the directory's own entry a path the reader takes");
+}
diff --git a/testing/proofs/cbmc/bpm-beefile-names.c b/testing/proofs/cbmc/bpm-beefile-names.c
new file mode 100644
index 00000000..fe173fcc
--- /dev/null
+++ b/testing/proofs/cbmc/bpm-beefile-names.c
@@ -0,0 +1,51 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * The package name bpm build takes from a Beefile, on every string of up
+ * to NAME_MAX bytes: build_name_ok() of tools/shirudo-build/bpm-build.c
+ * takes exactly the names util/bpm_oci.c takes as a package's on the
+ * target, which the repository index takes too.
+ */
+
+/* As proof.h, before the system headers can include <ctype.h>. */
+#define __NO_CTYPE 1
+
+#include <dirent.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <strings.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/utsname.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+#include <zlib.h>
+#include <curl/curl.h>
+#include <openssl/bio.h>
+#include <openssl/evp.h>
+#include <openssl/pem.h>
+#include <openssl/x509.h>
+
+#include "proof.h"
+#include "../../../util/string.c"
+#include "../../../util/bpm_oci.c"
+#include "../../../tools/shirudo-build/bpm-build.c"
+
+#define NAME_MAX (BPM_MAX_NAME + 1)
+
+void harness(void)
+{
+ char *s = any_bytes(NAME_MAX + 1);
+ bool taken;
+
+ __CPROVER_assume(!s[NAME_MAX]);
+ taken = build_name_ok(s);
+ __CPROVER_assert(taken == bpm_package_name_ok(s),
+ "exactly the names of a package");
+ __CPROVER_assert(!taken || bpm_name_ok(s),
+ "which the repository index takes too");
+}
diff --git a/testing/proofs/cbmc/bpm-beefile-string.c b/testing/proofs/cbmc/bpm-beefile-string.c
new file mode 100644
index 00000000..34657355
--- /dev/null
+++ b/testing/proofs/cbmc/bpm-beefile-string.c
@@ -0,0 +1,120 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * A string value of a Beefile, on every value of LEN bytes:
+ * build_parse_string() of tools/shirudo-build/bpm-build.c takes exactly a
+ * quoted string that fits its buffer, decoded as the Beefile reference
+ * says, and writes nothing past the buffer.
+ */
+
+/* As proof.h, before the system headers can include <ctype.h>. */
+#define __NO_CTYPE 1
+
+#include <dirent.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <strings.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/utsname.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+#include <zlib.h>
+
+#define PROOF_MEMCPY_BYTEWISE
+#include "proof.h"
+#include "../../../util/string.c"
+#include "../../../tools/shirudo-build/bpm-build.c"
+
+#define OUT_MAX (LEN + 1)
+
+static bool blank(char c)
+{
+ return c == ' ' || (c >= '\t' && c <= '\r');
+}
+
+/*
+ * A string as the Beefile reference has it, between the blanks around
+ * it: a quote, text and the same quote, with \n, \r, \t, \\ and \" in a
+ * "..." string and no escape in a '...' one. The length of the text
+ * decoded into d, or -1.
+ */
+static int quoted(const char *v, char *d)
+{
+ size_t from = 0, to = LEN, i;
+ int n = 0;
+ char c;
+
+ while (from < LEN && blank(v[from]))
+ from++;
+ while (to > from && blank(v[to - 1]))
+ to--;
+ if (to - from < 2 || (v[from] != '"' && v[from] != '\'') ||
+ v[to - 1] != v[from])
+ return -1;
+ for (i = from + 1; i < to - 1; i++) {
+ c = v[i];
+ if (c == v[from])
+ return -1;
+ if (c == '\\' && v[from] == '"') {
+ c = v[++i];
+ if (i == to - 1 || !memchr("nrt\\\"", c, 5))
+ return -1;
+ c = c == 'n' ? '\n' : c == 'r' ? '\r' : c == 't' ? '\t' : c;
+ }
+ d[n++] = c;
+ }
+ return n;
+}
+
+static void string_value(const char *v)
+{
+ char out[OUT_MAX], text[LEN + 1], was;
+ size_t cap, i;
+ int n, ret;
+
+ /* Every caller passes the size of an array, 16 bytes or more. */
+ __CPROVER_assume(cap >= 1 && cap <= OUT_MAX && i < OUT_MAX);
+ was = out[i];
+ ret = build_parse_string(v, out, cap);
+ n = quoted(v, text);
+ __CPROVER_assert(!ret == (n >= 0 && (size_t)n < cap),
+ "exactly a quoted string that fits");
+ __CPROVER_assert(i < cap || out[i] == was, "nothing past its buffer");
+ if (ret) {
+ __CPROVER_assert(ret == -EINVAL || ret == -ENAMETOOLONG,
+ "refused as malformed or too long");
+ return;
+ }
+ __CPROVER_assert(n >= 0 && !out[n] && !memcmp(out, text, (size_t)n),
+ "the text between the quotes, decoded and terminated");
+}
+
+/* authors alone takes an array, which bpm build reads past. */
+static void ignored_value(const char *v)
+{
+ char out[OUT_MAX];
+ size_t first = 0;
+
+ while (first < LEN && blank(v[first]))
+ first++;
+ if (build_parse_value(v, out, sizeof(out), true))
+ return;
+ __CPROVER_assert(quoted(v, out) >= 0 ||
+ (first < LEN && v[first] == '['),
+ "a string, or what starts as an array");
+}
+
+void harness(void)
+{
+ char *v = any_bytes(LEN + 1);
+
+ __CPROVER_assume(strnlen(v, LEN) == LEN);
+ v[LEN] = '\0';
+ string_value(v);
+ ignored_value(v);
+}
diff --git a/testing/proofs/cbmc/bpm-beefile-tables.c b/testing/proofs/cbmc/bpm-beefile-tables.c
new file mode 100644
index 00000000..086a12b2
--- /dev/null
+++ b/testing/proofs/cbmc/bpm-beefile-tables.c
@@ -0,0 +1,162 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * build_parse_spec() of tools/shirudo-build/bpm-build.c from every count
+ * of tables up to its limit: one more [[files]], [[upatches]], rodata or
+ * policy table is counted below the limit and refused at it, and a
+ * sub-table without its [[upatches]] or a target that is one of bpm
+ * build's own members is refused. LEN picks the case.
+ */
+
+/* As proof.h, before the system headers can include <ctype.h>. */
+#define __NO_CTYPE 1
+
+#include <dirent.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <strings.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/utsname.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+#include <zlib.h>
+
+#include "proof.h"
+#include "../../../util/string.c"
+#include "../../../tools/shirudo-build/bpm-build.c"
+
+enum {
+ FILES, UPATCHES, RODATA, POLICY, STRAY_RODATA, STRAY_POLICY,
+ STRAY_TREE, OWN_MEMBERS,
+};
+
+static struct bpm_build_spec spec;
+
+/* strtok_r() for the one delimiter the reader cuts lines at. */
+char *strtok_r(char *s, const char *delim, char **save)
+{
+ char *end;
+
+ __CPROVER_assert(delim[0] && !delim[1], "one delimiter");
+ if (!s)
+ s = *save;
+ while (*s == delim[0])
+ s++;
+ if (!*s) {
+ *save = s;
+ return NULL;
+ }
+ for (end = s; *end && *end != delim[0]; end++)
+ ;
+ if (*end)
+ *end++ = '\0';
+ *save = end;
+ return s;
+}
+
+/* The tables of an upatch, which the reader allocates at their limits. */
+void *xcalloc(size_t nmemb, size_t size)
+{
+ static struct bpm_build_policy policies[BPM_BUILD_MAX_POLICIES];
+ static struct bpm_build_rodata rodata[BPM_BUILD_MAX_RODATA];
+
+ if (nmemb == BPM_BUILD_MAX_RODATA && size == sizeof(rodata[0]))
+ return rodata;
+ __CPROVER_assert(nmemb == BPM_BUILD_MAX_POLICIES &&
+ size == sizeof(policies[0]), "rodata or policies");
+ return policies;
+}
+
+/*
+ * The Beefile has no [package] name, so the reader refuses it once every
+ * line is read, and what it counted until then stays to be looked at.
+ */
+static void counted(char *body, size_t *count, size_t limit)
+{
+ size_t was = *count;
+ int ret;
+
+ __CPROVER_assume(was <= limit);
+ ret = build_parse_spec(&spec, body);
+ __CPROVER_assert(ret == (was == limit ? -E2BIG : -EINVAL),
+ "refused at the limit, as too many");
+ __CPROVER_assert(*count == was + (was < limit),
+ "one more below the limit, none at it");
+}
+
+static void stray(char *body, int error)
+{
+ __CPROVER_assert(build_parse_spec(&spec, body) == error &&
+ !spec.n_upatches && !spec.upatches[0].n_rodata &&
+ !spec.upatches[0].n_policies &&
+ !spec.upatches[0].tree.present,
+ "a sub-table without its upatch refused, nothing counted");
+}
+
+/* The members bpm build writes itself, as the package format names them. */
+static void own_members_refused(void)
+{
+ static const char *const own[] = {
+ "Beefile", "README.md", "cert.der", "build-environment.json",
+ };
+ char seen[1][PATH_MAX];
+ size_t n = 0, i;
+
+ for (i = 0; i < ARRAY_SIZE(own); i++)
+ __CPROVER_assert(build_add_target(seen, &n, own[i]) == -EEXIST &&
+ !n, "no target is one of bpm build's own");
+}
+
+void harness(void)
+{
+ char files[] = "[[files]]", upatches[] = "[[upatches]]";
+ char rodata[] = "[[upatches]]\n[[upatches.rodata]]";
+ char policy[] = "[[upatches]]\n[[upatches.policy]]";
+ char stray_rodata[] = "[[upatches.rodata]]";
+ char stray_policy[] = "[[upatches.policy]]";
+ char stray_tree[] = "[upatches.tree]";
+ struct bpm_build_upatch *u = &spec.upatches[0];
+ size_t n;
+
+ switch (LEN) {
+ case FILES:
+ spec.n_files = n;
+ counted(files, &spec.n_files, BPM_BUILD_MAX_ENTRIES);
+ break;
+ case UPATCHES:
+ spec.n_upatches = n;
+ counted(upatches, &spec.n_upatches, BPM_BUILD_MAX_ENTRIES);
+ break;
+ case RODATA:
+ /* A table the reader allocated holds what it counted. */
+ u->n_rodata = n;
+ if (n)
+ u->rodata = xcalloc(BPM_BUILD_MAX_RODATA,
+ sizeof(*u->rodata));
+ counted(rodata, &u->n_rodata, BPM_BUILD_MAX_RODATA);
+ break;
+ case POLICY:
+ u->n_policies = n;
+ if (n)
+ u->policies = xcalloc(BPM_BUILD_MAX_POLICIES,
+ sizeof(*u->policies));
+ counted(policy, &u->n_policies, BPM_BUILD_MAX_POLICIES);
+ break;
+ case STRAY_RODATA:
+ stray(stray_rodata, -E2BIG);
+ break;
+ case STRAY_POLICY:
+ stray(stray_policy, -E2BIG);
+ break;
+ case STRAY_TREE:
+ stray(stray_tree, -EINVAL);
+ break;
+ default:
+ own_members_refused();
+ }
+}
diff --git a/testing/proofs/cbmc/bpm-beefile-targets.c b/testing/proofs/cbmc/bpm-beefile-targets.c
new file mode 100644
index 00000000..2468590c
--- /dev/null
+++ b/testing/proofs/cbmc/bpm-beefile-targets.c
@@ -0,0 +1,95 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * The member paths bpm build takes from a Beefile, for every target of LEN
+ * bytes: tools/shirudo-build/bpm-build.c takes exactly the file paths the
+ * archive reader of util/bpm_package.c takes, and each of them once;
+ * bpm-beefile-tables has the members bpm build writes itself.
+ */
+
+/* As proof.h, before the system headers can include <ctype.h>. */
+#define __NO_CTYPE 1
+
+#include <dirent.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <strings.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/utsname.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+#include <zlib.h>
+#include <openssl/bio.h>
+#include <openssl/evp.h>
+#include <openssl/pem.h>
+#include <openssl/x509.h>
+
+#define PROOF_MEMCPY_BYTEWISE
+#include "proof.h"
+#include "../../../util/string.c"
+#include "../../../util/bpm_package.c"
+#include "../../../tools/shirudo-build/bpm-build.c"
+
+/* Targets up to this long also join a table; longer ones cost minutes. */
+#define ADD_MAX 4
+
+static char *string(size_t len)
+{
+ char *s = any_bytes(len + 1);
+
+ __CPROVER_assume(strnlen(s, len) == len);
+ s[len] = '\0';
+ return s;
+}
+
+/* A target is a path the reader extracts, and one that names a file. */
+static bool target_taken(const char *target)
+{
+ bool taken = !build_target_ok(target);
+
+ __CPROVER_assert(taken == (bpm_archive_rel_ok(target) &&
+ target[LEN - !!LEN] != '/'),
+ "exactly the reader's paths without a final slash");
+ return taken;
+}
+
+/* A target joins the one seen before it, any string or none. */
+static void target_added(const char *target, bool taken)
+{
+ char seen[2][PATH_MAX] = {};
+ size_t n = 1, i, j;
+ bool twice;
+ int ret;
+
+ for (j = 0; j <= LEN; j++) {
+ char any;
+
+ seen[0][j] = any;
+ }
+ __CPROVER_assume(i <= LEN);
+ twice = !strcmp(seen[0], target);
+ ret = build_add_target(seen, &n, target);
+ __CPROVER_assert(!ret == (taken && !twice), "a path taken once");
+ if (ret) {
+ __CPROVER_assert(n == 1, "a refused target is not counted");
+ return;
+ }
+ __CPROVER_assert(n == 2 && seen[1][i] == target[i],
+ "the target seen from now on");
+}
+
+void harness(void)
+{
+ char *target;
+ bool taken;
+
+ target = string(LEN);
+ taken = target_taken(target);
+ if (LEN <= ADD_MAX)
+ target_added(target, taken);
+}
diff --git a/testing/proofs/cbmc/bpm-beefile-version.c b/testing/proofs/cbmc/bpm-beefile-version.c
new file mode 100644
index 00000000..3f54c17d
--- /dev/null
+++ b/testing/proofs/cbmc/bpm-beefile-version.c
@@ -0,0 +1,66 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * The version bpm build takes, on every string of LEN bytes:
+ * tools/shirudo-build/bpm-build.c takes only versions util/bpm_oci.c lets
+ * through on the target, and <name>-<version> is one directory to the
+ * archive reader of util/bpm_package.c.
+ */
+
+/* As proof.h, before the system headers can include <ctype.h>. */
+#define __NO_CTYPE 1
+
+#include <dirent.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <strings.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/utsname.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+#include <zlib.h>
+#include <curl/curl.h>
+#include <openssl/bio.h>
+#include <openssl/evp.h>
+#include <openssl/pem.h>
+#include <openssl/x509.h>
+
+#include "proof.h"
+#include "../../../util/string.c"
+#include "../../../util/bpm_oci.c"
+#include "../../../util/bpm_package.c"
+#include "../../../tools/shirudo-build/bpm-build.c"
+
+static char *string(size_t len)
+{
+ char *s = any_bytes(len + 1);
+
+ __CPROVER_assume(strnlen(s, len) == len);
+ s[len] = '\0';
+ return s;
+}
+
+void harness(void)
+{
+ char top[1 + 1 + LEN + 1];
+ char *name, *version;
+
+ version = string(LEN);
+ if (!build_version_ok(version))
+ return;
+ __CPROVER_assert(bpm_version_ok(version),
+ "a version the target takes");
+ /* build_archive() names the one directory of the archive so. */
+ name = string(1);
+ if (!build_name_ok(name))
+ return;
+ __CPROVER_assert(strsprintf(top, sizeof(top), "%s-%s", name, version) ==
+ LEN + 2 && bpm_archive_rel_ok(top) &&
+ !strchr(top, '/'),
+ "one component the archive reader takes");
+}
diff --git a/testing/proofs/cbmc/bpm-index-append.c b/testing/proofs/cbmc/bpm-index-append.c
new file mode 100644
index 00000000..e50e37da
--- /dev/null
+++ b/testing/proofs/cbmc/bpm-index-append.c
@@ -0,0 +1,272 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * push_index() of tools/shirudo-build/bpm-publish.c on a namespace whose
+ * index lists one package, which bpm_index_parse() of util/bpm_package.c
+ * reads: the target reads the index signed next with the package kept and
+ * the pushed one in order, a version pushed again once, and a version
+ * with other content is refused unsigned. LEN picks the case.
+ */
+
+/* As proof.h, before the system headers can include <ctype.h>. */
+#define __NO_CTYPE 1
+
+#include <fcntl.h>
+#include <limits.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <strings.h>
+#include <sys/stat.h>
+#include <time.h>
+#include <unistd.h>
+#include <zlib.h>
+#include <curl/curl.h>
+#include <openssl/bio.h>
+#include <openssl/evp.h>
+#include <openssl/pem.h>
+#include <openssl/x509.h>
+
+#define PROOF_MEMCPY_BYTEWISE
+#include "proof.h"
+#include "../../../util/string.c"
+#include "../../../util/hex.c"
+#include "../../../util/json.c"
+/* The registry and the signature check are the stubs below. */
+#include "bpm_oci.h"
+#include "bpm_package.h"
+#define bpm_oci_url bpm_oci_url_unused
+#define bpm_oci_get_memory bpm_oci_get_memory_unused
+#define bpm_oci_get_blob bpm_oci_get_blob_unused
+#define bpm_oci_parse_manifest bpm_oci_parse_manifest_unused
+#define bpm_oci_push_blob bpm_oci_push_blob_unused
+#define bpm_oci_put_manifest bpm_oci_put_manifest_unused
+#define bpm_sha256_hex bpm_sha256_hex_unused
+#define bpm_digest_matches bpm_digest_matches_unused
+#define bpm_buffer_free bpm_buffer_free_unused
+#define bpm_verify_cms bpm_verify_cms_unused
+#include "../../../util/bpm_oci.c"
+#include "../../../util/bpm_package.c"
+#undef bpm_oci_url
+#undef bpm_oci_get_memory
+#undef bpm_oci_get_blob
+#undef bpm_oci_parse_manifest
+#undef bpm_oci_push_blob
+#undef bpm_oci_put_manifest
+#undef bpm_sha256_hex
+#undef bpm_digest_matches
+#undef bpm_buffer_free
+#undef bpm_verify_cms
+#include "../../../tools/shirudo-build/bpm-publish.c"
+
+enum { APPENDED, AGAIN, CHANGED };
+
+/* Room for the document of two short entries, of the 12 KiB asked for. */
+#define DOC_MAX 512
+
+#define ENTRY(name, repository, digest) \
+ "{\"schema\":1,\"generated_at\":\"2026-01-01T00:00:00Z\"," \
+ "\"packages\":[{\"name\":\"" name "\",\"version\":\"1.0.0\"," \
+ "\"repository\":\"" repository "\"," \
+ "\"manifest_digest\":\"sha256:m\"," \
+ "\"package_digest\":\"sha256:" digest "\",\"package_size\":5," \
+ "\"signer_fingerprint\":\"5e\"}]}"
+
+/* The index the registry serves, as an earlier push signed it. */
+static const char *const listed[] = {
+ [APPENDED] = ENTRY("b", "ns/b", "p"),
+ [AGAIN] = ENTRY("a", "ns/a", "p"),
+ [CHANGED] = ENTRY("a", "ns/a", "q"),
+};
+
+/* Which of the cases this run is. */
+static const int pushed = LEN;
+
+static int allocations, documents, manifests;
+
+/* First the index's entries, two of 512, then the manifest's layers. */
+void *xcalloc(size_t nmemb, size_t size)
+{
+ static struct bpm_index_entry entries[2];
+ static struct bpm_descriptor layers[2];
+
+ if (size == sizeof(entries[0]))
+ return entries;
+ __CPROVER_assert(size == sizeof(layers[0]), "entries or layers");
+ return layers;
+}
+
+/* In push_index()'s order: the document, its signature, the manifest. */
+void *xmalloc(size_t size)
+{
+ static char document[DOC_MAX], signature[1], manifest[2 * DOC_MAX];
+
+ switch (allocations++) {
+ case 0:
+ return document;
+ case 1:
+ return signature;
+ default:
+ return manifest;
+ }
+}
+
+void xfree(void *p, size_t size)
+{
+}
+
+void pr_err(const char *fmt, ...)
+{
+}
+
+/* Two entries at most, in the order the index keeps. */
+void qsort(void *base, size_t nmemb, size_t size,
+ int (*compare)(const void *, const void *))
+{
+ struct bpm_index_entry *entry = base, first;
+
+ __CPROVER_assert(nmemb <= 2 && size == sizeof(*entry),
+ "two entries at most");
+ if (nmemb < 2 || compare(&entry[0], &entry[1]) <= 0)
+ return;
+ first = entry[0];
+ entry[0] = entry[1];
+ entry[1] = first;
+}
+
+int iso8601(time_t t, char *out, size_t cap)
+{
+ return strscpy(out, "2026-01-02T00:00:00Z", cap) < 0 ? -EINVAL : 0;
+}
+
+int bpm_oci_url(const struct bpm_oci_client *c, const char *repo,
+ const char *kind, const char *ref, char *out, size_t sz)
+{
+ out[0] = '\0';
+ return 0;
+}
+
+int bpm_oci_get_memory(struct bpm_oci_client *c, const char *url,
+ const char *accept, struct bpm_buffer *out)
+{
+ return 0;
+}
+
+/* The index's manifest: its document and its signature, sizes unsaid. */
+int bpm_oci_parse_manifest(const __u8 *body, size_t len,
+ struct bpm_descriptor *layers, int max_layers,
+ int *n_layers, char *package_name,
+ size_t package_name_sz)
+{
+ bpm_copy_string(layers[0].media, sizeof(layers[0].media),
+ BPM_INDEX_MEDIA);
+ bpm_copy_string(layers[1].media, sizeof(layers[1].media),
+ BPM_INDEX_SIG_MEDIA);
+ layers[0].size = -1;
+ layers[1].size = -1;
+ *n_layers = 2;
+ return 0;
+}
+
+int bpm_oci_get_blob(struct bpm_oci_client *c, const char *url,
+ const char *accept, struct bpm_buffer *out)
+{
+ static __u8 signature[1];
+
+ if (strcmp(accept, BPM_INDEX_MEDIA)) {
+ out->data = signature;
+ out->len = sizeof(signature);
+ return 0;
+ }
+ out->data = (__u8 *)listed[pushed];
+ out->len = strlen(listed[pushed]);
+ return 0;
+}
+
+int bpm_sha256_hex(const void *data, size_t len, char *hex, size_t hex_sz)
+{
+ hex[0] = '\0';
+ return 0;
+}
+
+bool bpm_digest_matches(const char *want, const char *hex)
+{
+ return true;
+}
+
+int bpm_verify_cms(const __u8 *payload, size_t payload_len,
+ const __u8 *sig, size_t sig_len, const char *cert_path)
+{
+ return 0;
+}
+
+void bpm_buffer_free(struct bpm_buffer *b)
+{
+}
+
+int bpm_oci_push_blob(struct bpm_oci_client *c, const char *repository,
+ const void *body, size_t body_len, const char *media,
+ struct bpm_descriptor *out)
+{
+ bpm_copy_string(out->media, sizeof(out->media), media);
+ bpm_copy_string(out->digest, sizeof(out->digest), "sha256:0");
+ out->size = (__s64)body_len;
+ return 0;
+}
+
+int bpm_oci_put_manifest(struct bpm_oci_client *c, const char *repository,
+ const char *reference, const __u8 *body,
+ size_t body_len, char *digest, size_t digest_sz)
+{
+ manifests++;
+ return 0;
+}
+
+static bool is_package(const struct bpm_index_entry *e, const char *name,
+ const char *repository, __s64 size)
+{
+ return !strcmp(e->name, name) && !strcmp(e->version, "1.0.0") &&
+ !strcmp(e->repository, repository) &&
+ !strcmp(e->manifest_digest, "sha256:m") &&
+ !strcmp(e->package_digest, "sha256:p") &&
+ !strcmp(e->signer, "5e") && e->package_size == size;
+}
+
+/* The signer, given the bytes the target will check and then read. */
+int cms_sign(struct sign_io *io, const char *pkey_path, const char *cert_path)
+{
+ struct bpm_index_entry read[2];
+ int n = 0;
+
+ documents++;
+ __CPROVER_assert(!bpm_index_parse(io->insns, io->insns_sz, read, &n),
+ "an index the target reads");
+ __CPROVER_assert(n == (pushed == APPENDED ? 2 : 1) &&
+ is_package(&read[0], "a", "ns/a", 7),
+ "the package pushed, once, before those behind it");
+ __CPROVER_assert(pushed != APPENDED ||
+ is_package(&read[1], "b", "ns/b", 5),
+ "the package listed before kept as it was");
+ io->signature_sz = sizeof(char);
+ return 0;
+}
+
+void harness(void)
+{
+ struct bpm_package_info info = {
+ .name = "a", .version = "1.0.0", .signer = "5e",
+ };
+ struct bpm_descriptor layer = { .digest = "sha256:p", .size = 7 };
+ struct bpm_oci_client c;
+ int ret;
+
+ ret = push_index(&c, "ns/bpm-index", "ns/a", &info, "sha256:m", &layer,
+ "key", "cert", false);
+ if (pushed == CHANGED) {
+ __CPROVER_assert(ret == -EEXIST && !documents && !manifests,
+ "a version with other content refused unsigned");
+ return;
+ }
+ __CPROVER_assert(!ret && documents == 1 && manifests == 1,
+ "one document signed, one manifest put");
+}
diff --git a/testing/proofs/cbmc/bpm-index-writer.c b/testing/proofs/cbmc/bpm-index-writer.c
new file mode 100644
index 00000000..6ddb7eb9
--- /dev/null
+++ b/testing/proofs/cbmc/bpm-index-writer.c
@@ -0,0 +1,172 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * push_index() of tools/shirudo-build/bpm-publish.c, which signs a
+ * namespace's first repository index, against bpm_index_parse() of
+ * util/bpm_package.c: the document it hands the signer the target reads
+ * back as the one package, field by field. Registry and signer are stubs.
+ */
+
+/* As proof.h, before the system headers can include <ctype.h>. */
+#define __NO_CTYPE 1
+
+#include <fcntl.h>
+#include <limits.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <strings.h>
+#include <sys/stat.h>
+#include <time.h>
+#include <unistd.h>
+#include <zlib.h>
+#include <curl/curl.h>
+#include <openssl/bio.h>
+#include <openssl/evp.h>
+#include <openssl/pem.h>
+#include <openssl/x509.h>
+
+#define PROOF_MEMCPY_BYTEWISE
+#include "proof.h"
+#include "../../../util/string.c"
+#include "../../../util/hex.c"
+#include "../../../util/json.c"
+/* The registry calls push_index() makes are the stubs below. */
+#include "bpm_oci.h"
+#define bpm_oci_url bpm_oci_url_unused
+#define bpm_oci_get_memory bpm_oci_get_memory_unused
+#define bpm_oci_push_blob bpm_oci_push_blob_unused
+#define bpm_oci_put_manifest bpm_oci_put_manifest_unused
+#include "../../../util/bpm_oci.c"
+#undef bpm_oci_url
+#undef bpm_oci_get_memory
+#undef bpm_oci_push_blob
+#undef bpm_oci_put_manifest
+#include "../../../util/bpm_package.c"
+#include "../../../tools/shirudo-build/bpm-publish.c"
+
+/* Room for the document of one short entry, of the 8 KiB asked for. */
+#define DOC_MAX 512
+/* The package pushed, and what push_index() did about it. */
+static struct bpm_index_entry pushed;
+static int allocations, documents, manifests;
+
+/* The entries of the index, two of the 512 asked for. */
+void *xcalloc(size_t nmemb, size_t size)
+{
+ static struct bpm_index_entry entries[2];
+
+ __CPROVER_assert(nmemb == BPM_MAX_ENTRIES && size == sizeof(entries[0]),
+ "only the index's entries are allocated so");
+ return entries;
+}
+
+/* In push_index()'s order: the document, its signature, the manifest. */
+void *xmalloc(size_t size)
+{
+ static char document[DOC_MAX], signature[1], manifest[2 * DOC_MAX];
+
+ switch (allocations++) {
+ case 0:
+ return document;
+ case 1:
+ return signature;
+ default:
+ return manifest;
+ }
+}
+
+void xfree(void *p, size_t size)
+{
+}
+
+void qsort(void *base, size_t nmemb, size_t size,
+ int (*compare)(const void *, const void *))
+{
+ __CPROVER_assert(nmemb == 1, "one entry, which needs no order");
+}
+
+int iso8601(time_t t, char *out, size_t cap)
+{
+ return strscpy(out, "2026-01-01T00:00:00Z", cap) < 0 ? -EINVAL : 0;
+}
+
+int bpm_oci_url(const struct bpm_oci_client *c, const char *repo,
+ const char *kind, const char *ref, char *out, size_t sz)
+{
+ out[0] = '\0';
+ return 0;
+}
+
+/* A namespace without an index, which --new-index starts. */
+int bpm_oci_get_memory(struct bpm_oci_client *c, const char *url,
+ const char *accept, struct bpm_buffer *out)
+{
+ return -ENOENT;
+}
+
+int bpm_oci_push_blob(struct bpm_oci_client *c, const char *repository,
+ const void *body, size_t body_len, const char *media,
+ struct bpm_descriptor *out)
+{
+ bpm_copy_string(out->media, sizeof(out->media), media);
+ bpm_copy_string(out->digest, sizeof(out->digest), "sha256:0");
+ out->size = (__s64)body_len;
+ return 0;
+}
+
+int bpm_oci_put_manifest(struct bpm_oci_client *c, const char *repository,
+ const char *reference, const __u8 *body,
+ size_t body_len, char *digest, size_t digest_sz)
+{
+ manifests++;
+ return 0;
+}
+
+/* The signer, given the bytes the target will check and then read. */
+int cms_sign(struct sign_io *io, const char *pkey_path, const char *cert_path)
+{
+ struct bpm_index_entry read[1];
+ int n = 0, ret;
+
+ documents++;
+ ret = bpm_index_parse(io->insns, io->insns_sz, read, &n);
+ __CPROVER_assert(!ret == (pushed.package_size > 0),
+ "an index the target reads, but for a package of no size");
+ if (!ret)
+ __CPROVER_assert(n == 1 && !strcmp(read[0].name, pushed.name) &&
+ !strcmp(read[0].version, pushed.version) &&
+ !strcmp(read[0].repository, pushed.repository) &&
+ !strcmp(read[0].manifest_digest,
+ pushed.manifest_digest) &&
+ !strcmp(read[0].package_digest,
+ pushed.package_digest) &&
+ !strcmp(read[0].signer, pushed.signer) &&
+ read[0].package_size == pushed.package_size,
+ "the one package, each field as it was pushed");
+ io->signature_sz = sizeof(char);
+ return 0;
+}
+
+void harness(void)
+{
+ static const __s64 sizes[] = { 1, INT64_MAX, 0, -1 };
+ struct bpm_package_info info = {
+ .name = "a", .version = "1.0.0", .signer = "5e",
+ };
+ struct bpm_descriptor layer = {
+ .digest = "sha256:p", .size = sizes[LEN],
+ };
+ struct bpm_oci_client c;
+ int ret;
+
+ pushed = (struct bpm_index_entry){
+ .name = "a", .version = "1.0.0", .repository = "ns/a",
+ .manifest_digest = "sha256:m", .package_digest = "sha256:p",
+ .signer = "5e", .package_size = sizes[LEN],
+ };
+ ret = push_index(&c, "ns/bpm-index", "ns/a", &info, "sha256:m", &layer,
+ "key", "cert", true);
+ __CPROVER_assert(!ret && documents == 1 && manifests == 1,
+ "one document signed, one manifest put");
+}
diff --git a/testing/proofs/cbmc/bpm-tar-writer.c b/testing/proofs/cbmc/bpm-tar-writer.c
new file mode 100644
index 00000000..9baec451
--- /dev/null
+++ b/testing/proofs/cbmc/bpm-tar-writer.c
@@ -0,0 +1,178 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * tar_entry() of tools/shirudo-build/bpm-build.c on every name of LEN
+ * bytes, size and mode: a member is refused before a byte is written, or
+ * its header is one block the readers of util/bpm_package.c take, with the
+ * same name, size and type, and the next header lies where they look.
+ */
+
+/* As proof.h, before the system headers can include <ctype.h>. */
+#define __NO_CTYPE 1
+
+#include <dirent.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <strings.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/utsname.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+#include <zlib.h>
+#include <openssl/bio.h>
+#include <openssl/evp.h>
+#include <openssl/pem.h>
+#include <openssl/x509.h>
+
+#define PROOF_MEMCPY_BYTEWISE
+#include "proof.h"
+#include "../../../util/string.c"
+#include "../../../util/bpm_package.c"
+#include "../../../tools/shirudo-build/bpm-build.c"
+
+#define TAR_BLOCK 512
+#define TAR_NAME 100
+#define TAR_SIZE_MAX 077777777777ULL
+/* The last bytes of a name that are free where the checksum is proven. */
+#define FREE 16
+
+/* The member tar_entry() is given, and what reached the archive. */
+static const char *member;
+static size_t member_len;
+static mode_t member_mode;
+static char member_type;
+static __u64 archived;
+static bool headed, summing;
+
+/*
+ * A field as the reader's bpm_tar_octal() takes it, from a copy: its
+ * search for the digits would go through the whole header otherwise.
+ */
+static bool octal_field(const __u8 *h, size_t off, size_t n, __u64 want)
+{
+ char field[12];
+ size_t i;
+ __u64 v;
+
+ for (i = 0; i < n; i++)
+ field[i] = (char)h[off + i];
+ return !bpm_tar_octal(field, n, &v) && v == want;
+}
+
+/* A header as bpm_pkg_extract() and the member readers take it. */
+static void header_reads_back(const __u8 *h)
+{
+ if (summing) {
+ __CPROVER_assert(bpm_tar_header_ok(h),
+ "the checksum the reader computes");
+ return;
+ }
+ __CPROVER_assert(memchr(h, '\0', TAR_NAME) && !h[345],
+ "a name terminated in its field, no prefix");
+ __CPROVER_assert(!strcmp((const char *)h, member),
+ "the name read back");
+ __CPROVER_assert(octal_field(h, 124, 12, member_len),
+ "the size read back");
+ __CPROVER_assert(h[156] == member_type, "the type read back");
+ __CPROVER_assert(octal_field(h, 100, 8, member_mode & 07777) &&
+ octal_field(h, 108, 8, 0) &&
+ octal_field(h, 116, 8, 0) && octal_field(h, 136, 12, 0),
+ "the permission bits, uid, gid and mtime 0");
+ __CPROVER_assert(!memcmp(h + 257, "ustar", 6) &&
+ !memcmp(h + 263, "00", 2), "a ustar header");
+}
+
+/*
+ * zlib's gzwrite(), every write taken whole: the first block is the
+ * header, checked while tar_entry() still holds it, the rest is counted.
+ */
+int gzwrite(gzFile file, voidpc buf, __u32 len)
+{
+ if (!headed) {
+ __CPROVER_assert(len == TAR_BLOCK, "a header of one block first");
+ header_reads_back(buf);
+ headed = true;
+ }
+ archived += len;
+ return (int)len;
+}
+
+static void member_written(const char *name, bool checksum)
+{
+ const size_t name_len = LEN;
+ struct bpm_tar tar;
+ mode_t mode;
+ size_t len;
+ __u64 pad;
+ char type;
+ int ret;
+
+ /* build_archive() writes files, and one directory without content. */
+ __CPROVER_assume(type == '0' || (type == '5' && !len));
+ member = name;
+ member_len = len;
+ member_mode = mode;
+ member_type = type;
+ summing = checksum;
+ headed = false;
+ archived = 0;
+ ret = tar_entry(&tar, name, NULL, len, mode, type);
+ if (checksum)
+ return;
+ if (name_len >= TAR_NAME || len > TAR_SIZE_MAX) {
+ __CPROVER_assert(ret && !headed,
+ "refused before a byte is written");
+ return;
+ }
+ __CPROVER_assert(headed, "the header written");
+ /* One gzwrite() takes INT_MAX bytes; build_read_file() reads 256 MiB. */
+ __CPROVER_assert(!ret == (len <= INT_MAX),
+ "written whole, up to what one write takes");
+ if (ret)
+ return;
+ pad = (TAR_BLOCK - len % TAR_BLOCK) % TAR_BLOCK;
+ __CPROVER_assert(archived == TAR_BLOCK + len + pad,
+ "the next header where the readers look for it");
+}
+
+/* tar_octal() alone: tar_entry() gives it no value wider than its field. */
+static void octal_written(size_t size)
+{
+ __u8 field[12];
+ __u64 value;
+ int ret;
+
+ ret = tar_octal(field, 0, size, value);
+ __CPROVER_assert(!ret == !(value >> (3 * (size - 1))),
+ "refused exactly with more digits than the field");
+ __CPROVER_assert(ret || octal_field(field, 0, size, value),
+ "a value that fits read back");
+}
+
+void harness(void)
+{
+ char *name;
+ size_t i;
+
+ octal_written(8);
+ octal_written(12);
+ name = any_bytes(LEN + 1);
+ name[LEN] = '\0';
+ __CPROVER_assume(strnlen(name, LEN) == LEN);
+ member_written(name, false);
+ if (LEN >= TAR_NAME)
+ return;
+ /*
+ * The checksum apart, for names that end in FREE bytes of any value:
+ * the solver holds the writer's sum against the reader's from the
+ * first free byte on, and through a whole name that takes minutes.
+ */
+ for (i = 0; i + FREE < LEN; i++)
+ name[i] = 'a';
+ member_written(name, true);
+}
diff --git a/testing/proofs/cbmc/bpm-tree-writer.c b/testing/proofs/cbmc/bpm-tree-writer.c
new file mode 100644
index 00000000..51061f7f
--- /dev/null
+++ b/testing/proofs/cbmc/bpm-tree-writer.c
@@ -0,0 +1,99 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * tree_emit_publication() of util/bpm_tree.c, which bpm tree append signs,
+ * against tree_parse(), on publications without operations that LEN
+ * picks: tree, generation, parent, base and head read back as written, up
+ * to the last generation and sequence number the target takes.
+ */
+
+/* util/string.c behind a file that includes its header. */
+#define STRING_INTERNAL
+
+#include <stdint.h>
+#include <stdlib.h>
+
+#define PROOF_MEMCPY_BYTEWISE
+#include "proof.h"
+#include "../../../util/bpm_tree.c"
+#include "../../../util/string.c"
+#include "../../../util/hex.c"
+#include "../../../util/json.c"
+
+#define DOC_MAX 512
+#define PARENT \
+ "sha256:eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"
+
+enum { FIRST, LAST, GENERATION_PAST, HEAD_PAST, UNNAMED };
+
+/*
+ * The allocator hands out objects of their types, so that the bytes
+ * written are the bytes read back: the document in DOC_MAX bytes of the
+ * 4 KiB asked for, and the reader's state.
+ */
+void *xmalloc(size_t size)
+{
+ static char document[DOC_MAX];
+
+ return document;
+}
+
+void *xcalloc(size_t nmemb, size_t size)
+{
+ static struct tree_reader reader;
+
+ __CPROVER_assert(nmemb == 1 && size == sizeof(reader),
+ "only tree_parse() allocates so");
+ return &reader;
+}
+
+void xfree(void *p, size_t size)
+{
+}
+
+void harness(void)
+{
+ static const struct tree_publication cases[] = {
+ [FIRST] = { .schema = 1, .tree_id = "tree", .generation = 1 },
+ [LAST] = {
+ .schema = 1, .tree_id = "tree",
+ .generation = INT64_MAX - 1,
+ .parent_manifest_digest = PARENT,
+ .head_seq = INT64_MAX - 1,
+ },
+ [GENERATION_PAST] = {
+ .schema = 1, .tree_id = "tree", .generation = INT64_MAX,
+ },
+ [HEAD_PAST] = {
+ .schema = 1, .tree_id = "tree", .generation = 1,
+ .head_seq = INT64_MAX,
+ },
+ [UNNAMED] = { .schema = 1, .generation = 1 },
+ };
+ const struct tree_publication *pub = &cases[LEN];
+ struct tree_publication back;
+ const int written = LEN;
+ size_t len = 0;
+ char *body;
+ int ret;
+
+ __CPROVER_assert(!tree_emit_publication(pub, &body, &len),
+ "a publication written");
+ ret = tree_parse(body, len, &back);
+ /* bpm tree append refuses to write the last three. */
+ __CPROVER_assert(ret == (written <= LAST ? 0 : -EINVAL),
+ "read up to the last generation and sequence number");
+ if (ret)
+ return;
+ __CPROVER_assert(back.schema == 1 && !strcmp(back.tree_id, pub->tree_id) &&
+ back.generation == pub->generation &&
+ !strcmp(back.parent_manifest_digest,
+ pub->parent_manifest_digest) &&
+ back.base_seq == pub->base_seq &&
+ back.head_seq == pub->head_seq && !back.n_operations,
+ "tree, generation, parent, base and head as written");
+ __CPROVER_assert(tree_publication_check(&back) ==
+ (written == FIRST ? 0 : -EINVAL),
+ "tree sync goes on with one whose head is its base");
+}
diff --git a/testing/proofs/cbmc/proof.h b/testing/proofs/cbmc/proof.h
index c7a8ff5e..6ea54bc7 100644
--- a/testing/proofs/cbmc/proof.h
+++ b/testing/proofs/cbmc/proof.h
@@ -127,6 +127,53 @@ static size_t format_number(char *buf, size_t size, size_t n, __u64 v,
return n;
}
+/*
+ * A character at n, which is lo or more, lo known before the solver runs:
+ * where lo leaves one place for it or none, the write is to that place,
+ * and the other bytes of the buffer keep the values they had.
+ */
+static size_t format_char_from(char *buf, size_t size, size_t n, size_t lo,
+ char c)
+{
+ __CPROVER_assert(n >= lo, "the vsnprintf() model's lower bound");
+ if (lo + 1 >= size)
+ return n + 1;
+ if (lo + 2 < size)
+ return format_char(buf, size, n, c);
+ if (n == lo)
+ buf[lo] = c;
+ return n + 1;
+}
+
+/*
+ * %o apart from format_number(): a digit is three bits of the value, so a
+ * value within its width has every digit at a place known beforehand.
+ */
+static size_t format_octal(char *buf, size_t size, size_t n, __u64 v,
+ size_t width, bool zero)
+{
+ size_t k = 1, j;
+
+ while (k < 22 && v >> (3 * k))
+ k++;
+ if (k > width) {
+ for (j = 0; j < 22; j++)
+ if (j < k)
+ format_char(buf, size, n + j, (char)('0' +
+ ((v >> (3 * (k - 1 - j))) & 7)));
+ return n + k;
+ }
+ for (j = 0; j < width; j++) {
+ size_t place = width - 1 - j;
+ char c = zero ? '0' : ' ';
+
+ if (place < 22 && (zero || place < k))
+ c = (char)('0' + ((v >> (3 * place)) & 7));
+ format_char(buf, size, n + j, c);
+ }
+ return n + width;
+}
+
/*
* CBMC passes each variadic argument as an object of its own type, a
* char not promoted to int, so an integer is read by its size, a char or
@@ -155,12 +202,13 @@ static __u64 integer_arg(va_list *ap, bool is_signed, bool *neg)
/*
* CBMC has no vsnprintf() either. This one formats what shirudo's proofs
- * reach, %s and the d, u and x conversions with a zero flag, a width and
- * ll, and fails the proof on any other.
+ * reach, %s and the d, u, x and o conversions with a zero flag, a width,
+ * written out or as *, and ll, and fails the proof on any other. lo counts
+ * the characters whose number does not depend on a value.
*/
int vsnprintf(char *buf, size_t size, const char *fmt, va_list ap)
{
- size_t n = 0;
+ size_t n = 0, lo = 0;
for (; *fmt; fmt++) {
bool zero = false, neg = false;
@@ -169,7 +217,7 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list ap)
__u64 v;
if (*fmt != '%' || fmt[1] == '%') {
- n = format_char(buf, size, n, *fmt);
+ n = format_char_from(buf, size, n, lo++, *fmt);
fmt += *fmt == '%';
continue;
}
@@ -179,6 +227,14 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list ap)
}
while (*fmt >= '1' && *fmt <= '9' && width < 10)
width = width * 10 + (size_t)(*fmt++ - '0');
+ if (*fmt == '*') {
+ int star = va_arg(ap, int);
+
+ __CPROVER_assert(star >= 0,
+ "a width the vsnprintf() model has");
+ width = (size_t)star;
+ fmt++;
+ }
if (fmt[0] == 'l' && fmt[1] == 'l')
fmt += 2;
if (*fmt == 's') {
@@ -188,14 +244,20 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list ap)
n = format_char(buf, size, n, *s);
continue;
}
- __CPROVER_assert(*fmt == 'd' || *fmt == 'u' || *fmt == 'x',
+ __CPROVER_assert(*fmt == 'd' || *fmt == 'u' || *fmt == 'x' ||
+ *fmt == 'o',
"a conversion the vsnprintf() model has");
v = integer_arg(&ap, *fmt == 'd', &neg);
+ if (*fmt == 'o') {
+ n = format_octal(buf, size, n, v, width, zero);
+ lo += width ? width : 1;
+ continue;
+ }
n = format_number(buf, size, n, v, neg, *fmt == 'x' ? 16 : 10,
width, zero);
}
if (size)
- buf[n < size ? n : size - 1] = '\0';
+ buf[n < size && lo + 1 < size ? n : size - 1] = '\0';
return (int)n;
}
diff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.list
index 884f0f46..08cf41c3 100644
--- a/testing/proofs/cbmc/proofs.list
+++ b/testing/proofs/cbmc/proofs.list
@@ -108,3 +108,15 @@ upatch-writer-kprobes -I ../../.. -I ../../../vendor/libbpf/include/uapi -I ../.
upatch-writer-xdp -I ../../.. -I ../../../vendor/libbpf/include/uapi -I ../../../vendor/libbpf --refine-arithmetic --memory-leak-check --max-field-sensitivity-array-size 512 --object-bits 12 --unwind 4 --unwindset strlen.0:161,strcmp.0:161,strncmp.0:21,strchr.0:161,strrchr.0:161,strnlen.0:161,memchr.0:129,memcmp.0:129,parse_base.0:21,parse_base.1:21,digits.0:21,default_attach_section.0:8,uprobe_section_target.0:9,text.0:161,decimal.0:5,resolve.1:65 LEN=0-8
upatch-writer-uprobes -I ../../.. -I ../../../vendor/libbpf/include/uapi -I ../../../vendor/libbpf --refine-arithmetic --memory-leak-check --max-field-sensitivity-array-size 512 --object-bits 12 --unwind 4 --unwindset strlen.0:161,strcmp.0:161,strncmp.0:21,strchr.0:161,strrchr.0:161,strnlen.0:161,memchr.0:129,memcmp.0:129,parse_base.0:21,parse_base.1:21,digits.0:21,default_attach_section.0:8,uprobe_section_target.0:9,text.0:161,decimal.0:5,resolve.1:65 LEN=0-21
vouch-payload --malloc-may-fail --malloc-fail-null --unwind 3 --unwindset memcmp.0:33,harness.0:5 LEN=0-2
+bpm-tar-writer --unwind 24 --unwindset tar_entry.0:513,bpm_tar_header_ok.0:513,memchr.0:101,memcpy.0:101,strlen.0:101,strnlen.0:101,strcmp.0:102,harness.0:101 --max-field-sensitivity-array-size 512 LEN=1,99,100
+bpm-beefile-string --unwind 15 LEN=0-12
+bpm-beefile-array --unwind 12 --unwindset build_parse_string_array.4:5
+bpm-beefile-lines --unwind 14 LEN=0-10
+bpm-beefile-tables --unwind 40 LEN=0-7
+bpm-beefile-names --unwind 131
+bpm-beefile-version --unwind 13 LEN=5-9
+bpm-beefile-targets --unwind 11 LEN=0-8
+bpm-beefile-members --unwind 11 LEN=0-6
+bpm-index-writer --unwind 26 --unwindset strlen.0:64,strnlen.0:64,memcpy.0:64,put_string_n.1:64 --max-field-sensitivity-array-size 512 LEN=0-3
+bpm-index-append --unwind 26 --unwindset strlen.0:260,strcmp.0:64,strnlen.0:64,memcpy.0:64,put_string_n.1:64 --max-field-sensitivity-array-size 512 LEN=0-2
+bpm-tree-writer --unwind 80 --max-field-sensitivity-array-size 512 LEN=0-4