Skip to content

eBPF-based security platform

proofs: grow a tree publication's operations in bpm-tree-reserve

Daniel Borkmann01e5fafbca3b
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rst
index 516c28d5..0e7e8b62 100644
--- a/Documentation/contribute/testing/proof-coverage.rst
+++ b/Documentation/contribute/testing/proof-coverage.rst
@@ -246,6 +246,13 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.
base of 0 without the segment keys or a later one with an
operation, a ``sha256:`` digest and a root of 64 hex digits, and
operations that run from the base to the head
+ * - ``bpm-tree-reserve``
+ - ``tree_operations_reserve()``, the allocator a stub that may fail
+ - Every count and capacity up to ``TREE_MAX_OPS``
+ - Room kept as it is; a full array grown to 16 operations or twice
+ its size, never past ``TREE_MAX_OPS``, with one more slot free;
+ ``-E2BIG`` at ``TREE_MAX_OPS`` and ``-ENOMEM`` without memory,
+ nothing changed then
* - ``bpm-tree-follows``
- ``tree_publication_follows()``, ``tree_segment_precedes()``,
``tree_walk_within()``
diff --git a/testing/proofs/cbmc/bpm-tree-reserve.c b/testing/proofs/cbmc/bpm-tree-reserve.c
new file mode 100644
index 00000000..683e4624
--- /dev/null
+++ b/testing/proofs/cbmc/bpm-tree-reserve.c
@@ -0,0 +1,75 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * tree_operations_reserve() of util/bpm_tree.c, which grows the operations
+ * tree_parse() reads from a registry, from every count and capacity: the
+ * array kept while it has room, a full one grown to 16 operations or twice
+ * its size and never past TREE_MAX_OPS, and a refusal at TREE_MAX_OPS or
+ * a failed allocation that changes nothing. The allocator is its contract:
+ * an array of operations of 13 KiB each is past what CBMC can copy.
+ */
+
+#include "proof.h"
+
+#define xreallocarray reserve_realloc
+#include "../../../util/bpm_tree.c"
+#undef xreallocarray
+
+static char before, after;
+static bool alloc_fails;
+static int calls;
+static void *got;
+static size_t got_old, got_new, got_size;
+
+void *reserve_realloc(void *ptr, size_t old_nmemb, size_t new_nmemb,
+ size_t size)
+{
+ calls++;
+ got = ptr;
+ got_old = old_nmemb;
+ got_new = new_nmemb;
+ got_size = size;
+ return alloc_fails ? NULL : &after;
+}
+
+void harness(void)
+{
+ struct tree_publication pub = {};
+ int n, cap, want, ret;
+ bool fails, kept;
+ void *was;
+
+ __CPROVER_assume(cap >= 0 && cap <= TREE_MAX_OPS && n >= 0 && n <= cap);
+ alloc_fails = fails;
+ was = cap ? &before : NULL;
+ pub.n_operations = n;
+ pub.operations_capacity = cap;
+ pub.operations = was;
+ ret = tree_operations_reserve(&pub);
+ kept = pub.operations == was && pub.operations_capacity == cap &&
+ pub.n_operations == n;
+ if (n >= TREE_MAX_OPS) {
+ __CPROVER_assert(ret == -E2BIG && !calls && kept,
+ "no operation past TREE_MAX_OPS");
+ return;
+ }
+ if (n < cap) {
+ __CPROVER_assert(!ret && !calls && kept,
+ "an array with room kept as it is");
+ return;
+ }
+ want = cap ? min(2 * cap, TREE_MAX_OPS) : 16;
+ __CPROVER_assert(calls == 1 && got == was && got_old == (size_t)cap &&
+ got_new == (size_t)want &&
+ got_size == sizeof(struct tree_operation),
+ "a full array grown to 16 or twice, at most TREE_MAX_OPS");
+ if (fails) {
+ __CPROVER_assert(ret == -ENOMEM && kept,
+ "a failed allocation changes nothing");
+ return;
+ }
+ __CPROVER_assert(!ret && pub.operations == (void *)&after &&
+ pub.operations_capacity == want && n < want &&
+ pub.n_operations == n, "room for one more operation");
+}
diff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.list
index 0ba603e5..2e552ed3 100644
--- a/testing/proofs/cbmc/proofs.list
+++ b/testing/proofs/cbmc/proofs.list
@@ -82,6 +82,7 @@ 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 --unwindset hex_digits.0:65,hex_decode.0:33,strlen.0:129,strncmp.0:8 --malloc-may-fail --malloc-fail-null LEN=0,1
+bpm-tree-reserve --unwind 1
bpm-source-config --unwind 24
bpm-oci-strings --unwind 9
bpm-semver-model --slice-formula --unwind 1 --unwindset strlen.0:10,strcmp.0:10,strcspn.0:2,strcspn.1:4,digits.0:10,parse_base.0:10,parse_base.1:10,semver_parse.0:4,semver_identifiers.0:4,semver_identifiers.1:3,prerelease_compare.0:3,memchr.0:4,memcmp.0:4,bpm_semver_compare.0:4,strnlen.0:10,any_string.0:10,break_at.0:10,split_on.0:10,next_ident.0:4,numeral.0:8,numeric.0:8,ident_ok.0:4,identifiers.0:3,lex_bytes.0:10,memcmp_model.0:4,lex_idents.0:3,semver_model_compare.0:4 LEN=9