Skip to content

eBPF-based security platform

proofs: prove how bpm_buffer_append() grows a registry body

Daniel Borkmanndd03f198bdd1
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rst
index b8cf1d8b..2eb3f390 100644
--- a/Documentation/contribute/testing/proof-coverage.rst
+++ b/Documentation/contribute/testing/proof-coverage.rst
@@ -203,6 +203,15 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.
all; values terminated, without a leading blank, a media type
without parameters, a location without CR or LF; a challenge
that fits kept whole, a longer one dropped rather than cut
+ * - ``bpm-oci-buffer``
+ - ``bpm_buffer_append()``
+ - A buffer of 0, 3, 8, 12 and 15 bytes holding any shorter body,
+ every chunk of up to 8 bytes, allocations allowed to fail, with
+ ``BPM_MAX_HTTP_BODY`` set to 16 for its 16 MiB
+ - ``-E2BIG`` exactly when the body would reach the limit and
+ ``-ENOMEM`` without memory, nothing changed then; else the chunk
+ behind the body, which is kept, inside the buffer and terminated,
+ a buffer with room kept
* - ``bpm-oci-descriptor``
- ``on_descriptor()``, ``on_index_entry()``
- Any descriptor or index entry, into an empty or a full table
diff --git a/testing/proofs/cbmc/bpm-oci-buffer.c b/testing/proofs/cbmc/bpm-oci-buffer.c
new file mode 100644
index 00000000..083d5e4e
--- /dev/null
+++ b/testing/proofs/cbmc/bpm-oci-buffer.c
@@ -0,0 +1,62 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * bpm_buffer_append() of util/bpm_oci.c, which keeps every registry body
+ * read into memory, from a buffer of LEN bytes holding any body shorter,
+ * for every chunk of up to CHUNK_MAX bytes and an allocation that may
+ * fail: refused exactly when the body would reach BPM_MAX_HTTP_BODY, and
+ * then or without memory nothing changed; else the chunk behind the body,
+ * inside the buffer and terminated. The limit is 16 bytes here for its
+ * 16 MiB: the checks against it are the same expressions.
+ */
+
+#define BPM_MAX_HTTP_BODY 16u
+#define CHUNK_MAX 8
+
+/* util/malloc.c behind a file that includes its header. */
+#define MALLOC_INTERNAL
+#define STRING_INTERNAL
+
+#include "proof.h"
+#include "../../../util/bpm_oci.c"
+#include "../../../util/malloc.c"
+
+void harness(void)
+{
+ struct bpm_buffer b = { .cap = LEN };
+ char was[LEN ? LEN : 1], *chunk;
+ size_t len, n, i;
+ __u8 *data;
+ int ret;
+
+ __CPROVER_assume(n <= CHUNK_MAX &&
+ (LEN ? len < LEN && len < BPM_MAX_HTTP_BODY : !len));
+ data = LEN ? any_bytes(LEN) : NULL;
+ for (i = 0; i < LEN; i++)
+ was[i] = data[i];
+ b.data = data;
+ b.len = len;
+ chunk = any_bytes(CHUNK_MAX);
+ ret = bpm_buffer_append(&b, chunk, n);
+ if (n >= BPM_MAX_HTTP_BODY - len || ret == -ENOMEM) {
+ __CPROVER_assert(ret == (n >= BPM_MAX_HTTP_BODY - len ? -E2BIG :
+ -ENOMEM) &&
+ b.data == data && b.len == len && b.cap == LEN,
+ "refused at the limit or without memory, nothing "
+ "changed");
+ return;
+ }
+ __CPROVER_assert(!ret && b.len == len + n && b.len < b.cap &&
+ !b.data[b.len], "the body grown, inside and terminated");
+ __CPROVER_assert(len + n + 1 <= LEN ? b.data == data && b.cap == LEN :
+ b.cap > len + n, "a buffer with room kept");
+ for (i = 0; i < BPM_MAX_HTTP_BODY; i++) {
+ if (i < len)
+ __CPROVER_assert(b.data[i] == (__u8)was[i],
+ "the body kept");
+ else if (i < len + n)
+ __CPROVER_assert(b.data[i] == (__u8)chunk[i - len],
+ "the chunk behind it");
+ }
+}
diff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.list
index 3670aae5..82e80fd3 100644
--- a/testing/proofs/cbmc/proofs.list
+++ b/testing/proofs/cbmc/proofs.list
@@ -79,6 +79,7 @@ bpm-semver-prerelease --unwind 7
bpm-oci-challenge --unwind 18
bpm-oci-token --unwind 18
bpm-oci-header --unwind 28
+bpm-oci-buffer --malloc-may-fail --malloc-fail-null --unwind 18 LEN=0,3,8,12,15
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
diff --git a/util/bpm_oci.h b/util/bpm_oci.h
index d9c3272a..abf6bdf8 100644
--- a/util/bpm_oci.h
+++ b/util/bpm_oci.h
@@ -27,7 +27,9 @@ struct json_emit;
#define BPM_MAX_ENTRIES 512
#define BPM_MAX_STATE 256
/* The largest response read into memory, an upatch artifact. */
+#ifndef BPM_MAX_HTTP_BODY
#define BPM_MAX_HTTP_BODY (16u * 1024u * 1024u)
+#endif
#define BPM_MAX_PACKAGE_BYTES (256u * 1024u * 1024u)
#define BPM_MAX_PACKAGE_ENTRIES 4096u
#define BPM_OCI_USERNAME_SIZE 256