eBPF-based security platform
testing: prove the load and unload names of a package's upatches
Daniel Borkmann8cbebd54e814
diff --git a/.agents/skills/proofs/SKILL.md b/.agents/skills/proofs/SKILL.mdindex b157bd3b..7b04450c 100644--- a/.agents/skills/proofs/SKILL.md+++ b/.agents/skills/proofs/SKILL.md@@ -92,6 +92,11 @@ determining its events.`-v`. Rebuild the input it names as a unit test or a small C programbefore calling it a bug: a harness that assumed too little or statedthe property wrongly fails too.+- A trace in which a copy yields bytes that are in neither its source+ nor its destination, from a literal chosen by symbolic data, is CBMC+ 5.95's `memcpy()` model, not the code: rerun the input as a constant+ to confirm, then define `PROOF_MEMCPY_BYTEWISE` before including+ `proof.h` (as `bpm-upatch-member.c` does).- `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 263aad40..195469a6 100644--- a/Documentation/contribute/testing/proof-coverage.rst+++ b/Documentation/contribute/testing/proof-coverage.rst@@ -66,6 +66,11 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.- ``lines_len()``- Up to 8 bytes- Ends at a newline or is 0, with no newline behind it+ * - ``bpm-upatch-member``+ - ``bpm_upatch_member()``, ``bpm_upatch_pair()``+ - 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* - ``upatch-header``- ``parse_header()``, ``validate_bounds()`` of the ``.upatch``readerdiff --git a/Documentation/contribute/testing/proofs.rst b/Documentation/contribute/testing/proofs.rstindex c7080d5b..b847dbc7 100644--- a/Documentation/contribute/testing/proofs.rst+++ b/Documentation/contribute/testing/proofs.rst@@ -116,7 +116,10 @@ Write a CBMC proofA function without a body in the proof, a libc one CBMC does not model ora system call, returns any value, and ``run.sh`` fails a proof that callsone. Give the function a model in ``proof.h``, or keep the proof away from-the code that calls it.+the code that calls it. CBMC 5.95's own ``memcpy()`` reads wrong bytes from+a string literal that symbolic data picks among several, and fails a proof+the code meets; a harness that copies from such a pointer defines+``PROOF_MEMCPY_BYTEWISE`` before it includes ``proof.h``.Write a Lean 4 proof--------------------diff --git a/testing/proofs/cbmc/bpm-upatch-member.c b/testing/proofs/cbmc/bpm-upatch-member.cnew file mode 100644index 00000000..a505744e--- /dev/null+++ b/testing/proofs/cbmc/bpm-upatch-member.c@@ -0,0 +1,55 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * bpm_upatch_member() and bpm_upatch_pair() of util/bpm_package.c, which+ * decide what install loads and uninstall unloads, on every name of LEN+ * bytes: the suffix alone decides, and a pair turns back into its name.+ */++#include <stdbool.h>++#define PROOF_MEMCPY_BYTEWISE+#include "proof.h"+#include "../../../util/bpm_package.c"++static bool ends_with(const char *s, size_t len, const char *suffix)+{+ size_t n = strlen(suffix);++ return len > n && !memcmp(s + len - n, suffix, n);+}++void harness(void)+{+ enum bpm_upatch_member kind, back;+ char out[LEN + 8], again[LEN + 8];+ bool plain, unload, upatch;+ size_t cap, n;+ char *name;++ name = any_bytes(LEN);+ kind = bpm_upatch_member(name, LEN);+ plain = LEN && name[0] != '.' && !memchr(name, '\0', LEN);+ unload = plain && ends_with(name, LEN, ".unload.upatch");+ upatch = plain && ends_with(name, LEN, ".upatch");+ __CPROVER_assert(kind == (unload ? BPM_UPATCH_UNLOAD :+ upatch ? BPM_UPATCH_LOAD : BPM_UPATCH_OTHER),+ "the suffix alone decides");++ __CPROVER_assume(cap <= sizeof(out));+ if (bpm_upatch_pair(name, LEN, out, cap)) {+ __CPROVER_assert(kind == BPM_UPATCH_OTHER ||+ cap <= LEN + 7, "refused only when it cannot fit");+ return;+ }+ n = strnlen(out, cap);+ __CPROVER_assert(n < cap, "terminated");+ back = bpm_upatch_member(out, n);+ __CPROVER_assert(back != BPM_UPATCH_OTHER && back != kind,+ "the other half of the pair");+ __CPROVER_assert(!bpm_upatch_pair(out, n, again, sizeof(again)) &&+ strnlen(again, sizeof(again)) == LEN &&+ !memcmp(again, name, LEN),+ "which pairs back to the name");+}diff --git a/testing/proofs/cbmc/proof.h b/testing/proofs/cbmc/proof.hindex 56e49782..b61a5c8d 100644--- a/testing/proofs/cbmc/proof.h+++ b/testing/proofs/cbmc/proof.h@@ -37,6 +37,25 @@ void *memchr(const void *s, int c, size_t n)return NULL;}+/*+ * CBMC 5.95's memcpy() reads wrong bytes from a string literal when the+ * source pointer is one of several literals picked by symbolic data, and+ * so fails a proof the code meets. A harness that copies from such a+ * pointer defines PROOF_MEMCPY_BYTEWISE before including this header.+ */+#ifdef PROOF_MEMCPY_BYTEWISE+void *memcpy(void *dst, const void *src, size_t n)+{+ const __u8 *s = src;+ __u8 *d = dst;+ size_t i;++ for (i = 0; i < n; i++)+ d[i] = s[i];+ return dst;+}+#endif+static inline void *any_bytes(size_t size){void *p = __CPROVER_allocate(size, 0);diff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.listindex b2507eb7..14fdf49d 100644--- a/testing/proofs/cbmc/proofs.list+++ b/testing/proofs/cbmc/proofs.list@@ -8,3 +8,4 @@ hex --unwind 12io-lines --unwind 10upatch-header --unwind 2 --unwindset memchr.0:257string-number-model --unwind 6+bpm-upatch-member --unwind 30 LEN=0-18