Skip to content

eBPF-based security platform

testing: prove the .upatch header parser for files of every size

Daniel Borkmann797d83a0eccf
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rst
index 7b72e10a..6997f090 100644
--- a/Documentation/contribute/testing/proof-coverage.rst
+++ b/Documentation/contribute/testing/proof-coverage.rst
@@ -60,6 +60,16 @@ Proofs
- ``lines_len()``
- Up to 8 bytes
- Ends at a newline or is 0, with no newline behind it
+ * - ``upatch-header``
+ - ``parse_header()``, ``validate_bounds()`` of the ``.upatch``
+ reader
+ - Every file: the header is read from a buffer of exactly its size
+ for files of any size
+ - Reads nothing past the header; an accepted header has its magic,
+ version, zero reserved field and a terminated name, a BPF
+ payload, a loader and a signature, whole aligned instructions,
+ every section inside the file and behind the header, and a
+ ``.rodata`` offset inside the data
Not covered yet
---------------
diff --git a/testing/proofs/proofs.list b/testing/proofs/proofs.list
index f6852ad8..efcfbd8a 100644
--- a/testing/proofs/proofs.list
+++ b/testing/proofs/proofs.list
@@ -6,3 +6,4 @@ string-copy --unwind 10
string-sanitize --unwind 8 LEN=0-6
hex --unwind 12
io-lines --unwind 10
+upatch-header --unwind 2 --unwindset memchr.0:257
diff --git a/testing/proofs/upatch-header.c b/testing/proofs/upatch-header.c
new file mode 100644
index 00000000..f5aec63e
--- /dev/null
+++ b/testing/proofs/upatch-header.c
@@ -0,0 +1,50 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * The header of a .upatch, the target's first look at a file from disk or
+ * a registry, for files of every size: util/upatch.c reads nothing past
+ * the header, and the sections of a header it accepts lie inside the file
+ * and behind the header.
+ */
+
+#include "proof.h"
+#include "../../util/upatch.c"
+
+static bool inside(__u32 off, __u32 len, size_t n)
+{
+ return (__u64)off + len <= n &&
+ (!len || off >= UPATCH_HEADER_SIZE);
+}
+
+void harness(void)
+{
+ struct upatch_host_header h;
+ size_t n;
+ __u8 *m;
+
+ m = any_bytes(UPATCH_HEADER_SIZE);
+ if (parse_header(m, n, &h))
+ return;
+ __CPROVER_assert(n >= UPATCH_HEADER_SIZE, "a whole header");
+ __CPROVER_assert(h.magic == UPATCH_MAGIC &&
+ h.version == UPATCH_VERSION && !h.reserved1,
+ "its magic, version and zero reserved field");
+ __CPROVER_assert(memchr(h.name, '\0', sizeof(h.name)),
+ "a terminated name");
+ if (validate_bounds(&h, n))
+ return;
+ __CPROVER_assert(h.type == UPATCH_TYPE_BPF, "a BPF payload");
+ __CPROVER_assert(inside(h.loader_insns_off, h.loader_insns_len, n) &&
+ inside(h.signature_off, h.signature_len, n) &&
+ inside(h.metadata_off, h.metadata_len, n) &&
+ inside(h.data_off, h.data_len, n),
+ "every section inside the file, behind the header");
+ __CPROVER_assert(h.loader_insns_len && h.signature_len,
+ "a loader and a signature");
+ __CPROVER_assert(!(h.loader_insns_off % 8) &&
+ !(h.loader_insns_len % 8),
+ "whole, aligned instructions");
+ __CPROVER_assert(h.rodata_data_off <= h.data_len,
+ ".rodata offset inside the data");
+}