Skip to content

eBPF-based security platform

proofs: prove the fixes of a Beefile's tree table

Daniel Borkmanne39ee81a1c63
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rst
index 1ebb3f87..d7cf4669 100644
--- a/Documentation/contribute/testing/proof-coverage.rst
+++ b/Documentation/contribute/testing/proof-coverage.rst
@@ -441,6 +441,15 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.
- 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-fixes``
+ - ``build_parse_fixes()``, ``bpm_fix_id_ok()``,
+ ``bpm_strings_sort()``
+ - Arrays of three IDs of 0 to 2 bytes each, of any bytes but a
+ quote, a backslash and NUL; ``qsort()`` modelled as an insertion
+ sort
+ - Taken exactly when each ID is a nonempty run of
+ ``[A-Za-z0-9._:-]`` and no two are the same, then kept whole and
+ in ascending order; a second ``fixes`` in one table refused
* - ``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
@@ -1341,8 +1350,9 @@ and the readers of ``/proc`` and the root file system, go to the kernel
and have none.
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.
+tar header, the member names, the Beefile's values, lines, names, fixes
+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,
diff --git a/testing/proofs/cbmc/bpm-beefile-fixes.c b/testing/proofs/cbmc/bpm-beefile-fixes.c
new file mode 100644
index 00000000..133f6e95
--- /dev/null
+++ b/testing/proofs/cbmc/bpm-beefile-fixes.c
@@ -0,0 +1,133 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * build_parse_fixes() of tools/shirudo-build/bpm-build.c on an array of
+ * three IDs of up to LEN bytes: taken exactly when each is a nonempty run
+ * of [A-Za-z0-9._:-] and no two are the same, then kept in ascending
+ * order; a table that gives fixes twice is refused.
+ */
+
+/* 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"
+
+#define IDS 3
+#define SLOT (LEN + 3)
+
+static char value[1 + IDS * SLOT + 1];
+
+void pr_err(const char *fmt, ...)
+{
+}
+
+/* CBMC models neither strspn() nor qsort(). */
+size_t strspn(const char *s, const char *accept)
+{
+ size_t n = 0;
+
+ while (s[n] && strchr(accept, s[n]))
+ n++;
+ return n;
+}
+
+void qsort(void *base, size_t nmemb, size_t size,
+ int (*compare)(const void *, const void *))
+{
+ char (*row)[TREE_STRING_MAX] = base, held[TREE_STRING_MAX];
+
+ __CPROVER_assert(size == TREE_STRING_MAX, "rows of strings");
+ for (size_t i = 1; i < nmemb; i++) {
+ for (size_t j = i; j && compare(row[j - 1], row[j]) > 0; j--) {
+ memcpy(held, row[j], size);
+ memcpy(row[j], row[j - 1], size);
+ memcpy(row[j - 1], held, size);
+ }
+ }
+}
+
+static bool fix_char(char c)
+{
+ return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
+ (c >= '0' && c <= '9') || c == '.' || c == '_' || c == ':' ||
+ c == '-';
+}
+
+/*
+ * Blanks pad each ID's slot after its closing quote, so that where an ID
+ * starts does not depend on the lengths of those before it.
+ */
+static void any_array(char ids[IDS][LEN + 1], const size_t lens[IDS])
+{
+ size_t at = 0;
+
+ value[at++] = '[';
+ for (int i = 0; i < IDS; i++) {
+ value[at++] = '"';
+ for (size_t k = 0; k <= LEN; k++) {
+ if (k < lens[i])
+ __CPROVER_assume(ids[i][k] && ids[i][k] != '"' &&
+ ids[i][k] != '\\');
+ else
+ ids[i][k] = '\0';
+ value[at++] = k < lens[i] ? ids[i][k] :
+ k == lens[i] ? '"' : ' ';
+ }
+ value[at++] = i + 1 < IDS ? ',' : ']';
+ }
+ value[at] = '\0';
+}
+
+void harness(void)
+{
+ struct bpm_fixes fixes = {};
+ char ids[IDS][LEN + 1];
+ bool valid = true;
+ size_t lens[IDS];
+ int ret;
+
+ for (int i = 0; i < IDS; i++)
+ __CPROVER_assume(lens[i] <= LEN);
+ any_array(ids, lens);
+ for (int i = 0; i < IDS; i++) {
+ valid = valid && lens[i];
+ for (size_t k = 0; k < lens[i]; k++)
+ valid = valid && fix_char(ids[i][k]);
+ for (int j = 0; j < i; j++)
+ valid = valid && strcmp(ids[i], ids[j]) != 0;
+ }
+ ret = build_parse_fixes(value, &fixes);
+ __CPROVER_assert(!ret == valid,
+ "taken exactly when each ID is a nonempty run of [A-Za-z0-9._:-], none twice");
+ if (ret)
+ return;
+ __CPROVER_assert(fixes.n == IDS, "every ID kept");
+ for (int i = 0; i < IDS; i++) {
+ bool given = false;
+
+ for (int j = 0; j < IDS; j++)
+ given = given || !strcmp(fixes.ids[i], ids[j]);
+ __CPROVER_assert(given, "each one of those given");
+ __CPROVER_assert(!i || strcmp(fixes.ids[i - 1], fixes.ids[i]) < 0,
+ "in ascending order");
+ }
+ __CPROVER_assert(build_parse_fixes(value, &fixes) == -EINVAL &&
+ fixes.n == IDS, "fixes given twice in one table refused");
+}
diff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.list
index 286a3075..f3de23ed 100644
--- a/testing/proofs/cbmc/proofs.list
+++ b/testing/proofs/cbmc/proofs.list
@@ -119,6 +119,7 @@ bpm-beefile-names --unwind 131
bpm-beefile-version --unwind 13 LEN=5-9
bpm-beefile-targets --unwind 11 --unwindset strlen.0:24,strncmp.0:24 LEN=0-8
bpm-beefile-members --unwind 11 LEN=0-6
+bpm-beefile-fixes --unwind 5 --unwindset strchr.0:67 LEN=0-2
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