eBPF-based security platform
testing: prove the .upatch sections, programs and .rodata view
Daniel Borkmann0ed424020c2c
diff --git a/.agents/skills/proofs/SKILL.md b/.agents/skills/proofs/SKILL.mdindex 7b04450c..7803dd4c 100644--- a/.agents/skills/proofs/SKILL.md+++ b/.agents/skills/proofs/SKILL.md@@ -54,8 +54,24 @@ to wait for.global one small; the unwinding assertions fail the proof if an inputneeds more, so a tight bound is safe.- A loop the input cannot reach at the harness's sizes takes bound 0, as- the policy labels do in data too short to hold one.+ the policy labels do in data too short to hold one. CBMC still runs+ its body once to the unwinding assertion, so a costly body bounded 0+ is better kept out of reach by the harness, as `upatch-signed-sections`+ does with no maps.- `too many addressed objects` asks for `--object-bits 12`.+- Division and remainder cost the SAT solver most: `--refine-arithmetic`+ took one proof from 360 s to 21 s, though it slows others. Time both.+- A `memchr()` over thousands of bytes unrolls slowly. Where the caller+ only asks whether the byte is there and `n` is a constant, define+ `PROOF_MEMCHR_QUANTIFIED` before including `proof.h` for a quantified+ model; it returns any match, not the first.+- A cast the code means, such as `(__u32)insn.imm`, fails+ `--conversion-check`. Include the file between `#pragma CPROVER check+ push`, `#pragma CPROVER check disable "conversion"` and `#pragma CPROVER+ check pop`, and keep the check on everywhere else.+- A struct of tens of kilobytes, such as a tree operation, makes even a+ few symbolic copies slow: prove the logic that reads its fields on a+ smaller harness, or leave it to the unit cases.- A proof over a few hundred symbolic bytes with long string scans cantake minutes: split it, proving the parts the caller composes, as the`.upatch` header, sections and decoders are proven apart.diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rstindex cbe49fb3..7720f365 100644--- a/Documentation/contribute/testing/proof-coverage.rst+++ b/Documentation/contribute/testing/proof-coverage.rst@@ -113,6 +113,79 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.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+ * - ``upatch-signed-sections``+ - ``parse_signed_sections()``+ - Data of 15, 16, 40 and 64 bytes+ - Sections kept only behind a valid trailer, the directory below+ the trailer, each section inside the data below the directory;+ nothing kept on refusal+ * - ``upatch-signed-name``+ - The ``NAME`` and ``DECODERS`` sections of+ ``parse_signed_sections()``+ - A one-section directory in 288 bytes+ - A kept name is terminated within 256 bytes; a refusal keeps+ neither+ * - ``upatch-decoders``+ - ``parse_decoders()``, ``upatch_decoder_view()``,+ ``upatch_has_decoders()``+ - Sections of every length declaring up to 2 decoders+ - Names terminated, each source non-empty and inside the section+ behind the entries, every kind set, nothing kept on refusal,+ ``-ENOENT`` past the last decoder+ * - ``upatch-policy-labels``+ - ``parse_policy_labels()``+ - Sections of 0, 1 and 2 labels and the lengths around them+ - An accepted section fills its length exactly, with its magic and+ version, two distinct map indexes below the map count, one bit+ per label and a terminated name and absolute path; a refusal+ writes nothing+ * - ``upatch-name``+ - ``upatch_name_valid()``, ``upatch_select()``+ - Names up to 24 bytes, archive names up to 31+ - Valid exactly for ``[A-Za-z0-9._-]+`` not starting with a dot;+ an archive entry exactly for a name and ``.upatch``+ * - ``upatch-prog-specs``+ - ``prog_specs_parse()``, ``prog_specs_free()``+ - Tables of 3, 15, 16 and 176 bytes, as many programs as fit, every+ allocation allowed to fail+ - A refusal leaves nothing allocated and ``*out`` NULL; an accepted+ program has a known role and kind, a terminated target and map,+ and uprobe fields only on a uprobe+ * - ``upatch-prog-uprobes``+ - The uprobe target records of ``prog_specs_parse()``+ - One program and one record in 226 bytes, path and build ID up to+ 9 bytes, up to 2 offsets+ - An absolute path of its stated length without a NUL, a target+ without ``,`` or ``|``, a build ID of 1 to 64 bytes, offsets+ non-empty and strictly rising, everything freed+ * - ``upatch-loader``+ - ``loader_lddw()``, ``sys_bpf_call()``, ``map_create_slot()``,+ ``map_update_slot()``, ``loader_store_source()``+ - Loaders of 4, 5, 25 and 49 instructions+ - Reads only the loader, and only instructions before the call it+ looks back from; reports exactly what it matched; every slot+ below the table's size+ * - ``upatch-rodata-btf``+ - ``loader_btf()``+ - Loaders of 4, 10 and 15 instructions and 24 bytes of data+ - The BTF it locates lies inside the data+ * - ``upatch-rodata``+ - ``find_rodata_data()``, ``map_name_rodata()``+ - Any loader of 6 or 8 instructions; at 24, the shape the loader+ generator emits with every other field free; 48 bytes of data+ - The value it locates lies inside the data, with the size and type+ its section declares+ * - ``upatch-btf``+ - ``btf_name_printable()``, ``btf_decl_printable()``,+ ``btf_unwrap()``+ - BTF of 4 types, names up to 6 bytes, libbpf modelled+ - A printed name is printable ASCII, an accepted declaration chain+ has no loop, an unwrapped type is 0 or has no qualifier+ * - ``upatch-btf-int``+ - ``btf_integer()``+ - Every encoding and every value of up to 8 bytes+ - The bits stay inside the integer's bytes; the value is zero- or+ sign-extended from themLean 4 proofs-------------@@ -146,8 +219,15 @@ Not covered yet---------------The fuzz targets in :doc:`test-coverage` cover these parsers; no proof-does: the JSON parser and emitter, the ``.upatch`` signed sections,-decoders and policy labels, the ``.rodata`` view, the package archive,-index and manifest readers, the tree publication and log, the eventlog,-the TPM structures of evidence, the configuration files and images, and-the kernel notes and configuration.+does: the JSON parser and emitter, the package archive, index and+manifest readers, the tree publication and log, the eventlog, the TPM+structures of evidence, the configuration files and images, and the+kernel notes and configuration.++Within the ``.upatch`` reader, the proofs cover its parts but not their+composition: ``upatch_parse()`` and ``upatch_open_memory()`` join the+header, the signed sections, the decoders and the policy labels the+proofs cover one by one, and a proof of the whole file runs out of+memory. A program table of two or more programs, ``find_rodata_data()``+on loaders of other shapes, ``btf_dumpable()``, the ``.rodata`` printers+and the libbpf calls behind ``btf_rodata_btf()`` have no proof.diff --git a/Documentation/contribute/testing/proofs.rst b/Documentation/contribute/testing/proofs.rstindex b847dbc7..c0dc59cb 100644--- a/Documentation/contribute/testing/proofs.rst+++ b/Documentation/contribute/testing/proofs.rst@@ -119,7 +119,10 @@ one. Give the function a model in ``proof.h``, or keep the proof away fromthe code that calls it. CBMC 5.95's own ``memcpy()`` reads wrong bytes froma string literal that symbolic data picks among several, and fails a proofthe code meets; a harness that copies from such a pointer defines-``PROOF_MEMCPY_BYTEWISE`` before it includes ``proof.h``.+``PROOF_MEMCPY_BYTEWISE`` before it includes ``proof.h``. A harness whose+code searches thousands of bytes with ``memchr()`` only to learn whether+a byte is there defines ``PROOF_MEMCHR_QUANTIFIED`` for a model that+answers without a loop.Write a Lean 4 proof--------------------diff --git a/testing/proofs/cbmc/proof.h b/testing/proofs/cbmc/proof.hindex b61a5c8d..6790c2f1 100644--- a/testing/proofs/cbmc/proof.h+++ b/testing/proofs/cbmc/proof.h@@ -26,6 +26,26 @@ size_t strnlen(const char *s, size_t max)return n;}+#ifdef PROOF_MEMCHR_QUANTIFIED+/*+ * For ranges of thousands of bytes, which a loop unrolls too slowly, where+ * the caller asks whether a byte is there and not where: NULL exactly when+ * none of the n bytes matches, n a constant, and otherwise any match. CBMC+ * ignores a quantifier over a range it cannot bound ("warning: ignoring+ * forall"), and then this may also return NULL where a byte matches.+ */+void *memchr(const void *s, int c, size_t n)+{+ const __u8 *p = s;+ size_t i;++ __CPROVER_assert(__CPROVER_r_ok(s, n), "memchr() reads its range");+ if (__CPROVER_forall { size_t j; j >= n || p[j] != (__u8)c })+ return NULL;+ __CPROVER_assume(i < n && p[i] == (__u8)c);+ return (void *)(p + i);+}+#elsevoid *memchr(const void *s, int c, size_t n){const __u8 *p = s;@@ -36,6 +56,7 @@ void *memchr(const void *s, int c, size_t n)return (void *)(p + i);return NULL;}+#endif/** CBMC 5.95's memcpy() reads wrong bytes from a string literal when thediff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.listindex 4a4ce135..54c66c70 100644--- a/testing/proofs/cbmc/proofs.list+++ b/testing/proofs/cbmc/proofs.list@@ -11,6 +11,18 @@ attest-pcrs --unwind 9attest-name-digest --unwind 72 LEN=64-70paths-bpffs-name --unwind 18upatch-header --unwind 2 --unwindset memchr.0:257+upatch-decoders --unwind 2 --unwindset memchr.0:65,parse_decoders.0:3,harness.0:3+upatch-policy-labels --unwind 3 LEN=0,19,20,4187,4188,4189,8356+upatch-name --unwind 33+upatch-signed-sections --refine-arithmetic --unwind 2 --unwindset parse_signed_sections.0:4,parse_decoders.0:0,parse_policy_labels.0:0 LEN=15,16,40,64+upatch-signed-name --refine-arithmetic --unwind 2 --unwindset parse_signed_sections.0:2,parse_decoders.0:2,parse_policy_labels.0:0 LEN=288+upatch-prog-specs --refine-arithmetic --memory-leak-check --malloc-may-fail --malloc-fail-null --unwind 3 --unwindset memchr.0:129,strchr.0:129,strnlen.0:1,strlen.0:1,prog_specs_parse.2:0,prog_specs_parse.3:0,uprobe_valid.0:1 LEN=3,15,16,176+upatch-prog-uprobes --memory-leak-check --malloc-may-fail --malloc-fail-null --unwind 3 --unwindset memchr.0:129,strchr.0:129,strnlen.0:12,strlen.0:12,prog_specs_parse.2:3,prog_specs_parse.3:2,uprobe_valid.0:3 LEN=226+upatch-loader -I ../../../vendor/libbpf/include/uapi -I ../../../vendor/libbpf --unwind 50 LEN=4,5,25,49+upatch-rodata-btf -I ../../../vendor/libbpf/include/uapi -I ../../../vendor/libbpf --refine-arithmetic --object-bits 10 --unwind 16 LEN=4,10,15+upatch-btf -I ../../../vendor/libbpf/include/uapi -I ../../../vendor/libbpf --unwind 6 --unwindset btf_decl_printable.0:33,btf_name_printable.0:7,btf_unwrap.0:9+upatch-btf-int -I ../../../vendor/libbpf/include/uapi -I ../../../vendor/libbpf --unwind 2 --unwindset btf_integer.0:65+upatch-rodata -I ../../../vendor/libbpf/include/uapi -I ../../../vendor/libbpf --refine-arithmetic --object-bits 12 --max-field-sensitivity-array-size 32 --unwind 25 --unwindset memcpy.0:49,strlen.0:18,strcmp.0:18,strnlen.0:18 LEN=6,8,24string-number-model --unwind 6bpm-tree-follows --unwind 4bpm-upatch-member --unwind 30 LEN=0-18diff --git a/testing/proofs/cbmc/upatch-btf-int.c b/testing/proofs/cbmc/upatch-btf-int.cnew file mode 100644index 00000000..dbb3c37f--- /dev/null+++ b/testing/proofs/cbmc/upatch-btf-int.c@@ -0,0 +1,41 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The integers util/upatch_rodata.c reads from the .rodata of a .upatch by+ * their BTF encoding, for every encoding and value of up to 8 bytes: it+ * reads only the integer's bytes and returns a value of its bits.+ */++#include "proof.h"+#include "../../../util/upatch_rodata.c"++void harness(void)+{+ struct {+ struct btf_type t;+ __u32 enc;+ } it;+ bool is_signed, is_bool;+ __u32 bits, size;+ __u64 value;+ __u8 *bytes;++ __CPROVER_assume(size <= sizeof(value));+ bytes = any_bytes(size);+ if (btf_integer(&it.t, bytes, size, &value, &is_signed, &is_bool))+ return;+ bits = BTF_INT_BITS(it.enc);+ __CPROVER_assert(it.t.size && it.t.size <= size && bits &&+ BTF_INT_OFFSET(it.enc) + bits <= it.t.size * 8,+ "bits inside the integer's bytes");+ if (bits == 64)+ return;+ if (!is_signed)+ __CPROVER_assert(!(value >> bits),+ "an unsigned value of its bits");+ else+ __CPROVER_assert(!(value >> (bits - 1)) ||+ !~(value | ((1ULL << (bits - 1)) - 1)),+ "a signed value of its bits");+}diff --git a/testing/proofs/cbmc/upatch-btf.c b/testing/proofs/cbmc/upatch-btf.cnew file mode 100644index 00000000..4fa2da98--- /dev/null+++ b/testing/proofs/cbmc/upatch-btf.c@@ -0,0 +1,102 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * What util/upatch_rodata.c prints of the BTF of a .upatch, which may come+ * unsigned, for BTF of NR_TYPES types and names of up to NAME_LEN bytes in+ * libbpf's stead: a name it prints is printable ASCII, a type whose+ * declaration it prints ends without a loop, and a type it unwraps is none+ * or one without a qualifier.+ */++#include "proof.h"+#include "../../../util/upatch_rodata.c"++#define NR_TYPES 4+#define NAME_LEN 6++struct model_type {+ struct btf_type t;+ struct btf_array a;+};++static struct model_type types[NR_TYPES];+static char names[NR_TYPES][NAME_LEN];++const struct btf_type *btf__type_by_id(const struct btf *btf, __u32 id)+{+ return id < NR_TYPES ? &types[id].t : NULL;+}++const char *btf__name_by_offset(const struct btf *btf, __u32 off)+{+ return off < NR_TYPES ? names[off] : NULL;+}++static bool printable(const char *s)+{+ size_t i;++ for (i = 0; i < NAME_LEN && s[i]; i++)+ if ((__u8)s[i] < 0x20 || (__u8)s[i] > 0x7e)+ return false;+ return true;+}++static bool qualifier(__u32 kind)+{+ return kind == BTF_KIND_TYPEDEF || kind == BTF_KIND_VOLATILE ||+ kind == BTF_KIND_CONST || kind == BTF_KIND_RESTRICT ||+ kind == BTF_KIND_TYPE_TAG;+}++static bool followed(__u32 kind)+{+ return kind == BTF_KIND_PTR || kind == BTF_KIND_VOLATILE ||+ kind == BTF_KIND_CONST || kind == BTF_KIND_RESTRICT ||+ kind == BTF_KIND_TYPE_TAG || kind == BTF_KIND_ARRAY;+}++/* Without a loop, a walk through NR_TYPES types ends in as many steps. */+static bool walk_ends(__u32 id)+{+ const struct model_type *m;+ __u32 step;++ for (step = 0; step <= NR_TYPES; step++) {+ if (!id)+ return true;+ if (id >= NR_TYPES)+ return false;+ m = &types[id];+ if (!followed(BTF_INFO_KIND(m->t.info)))+ return true;+ id = BTF_INFO_KIND(m->t.info) == BTF_KIND_ARRAY ? m->a.type :+ m->t.type;+ }+ return false;+}++void harness(void)+{+ __u32 i, id, off, unwrapped;++ for (i = 0; i < NR_TYPES; i++) {+ struct model_type any_type;+ char any_name[NAME_LEN];++ types[i] = any_type;+ memcpy(names[i], any_name, NAME_LEN);+ __CPROVER_assume(!names[i][NAME_LEN - 1]);+ }+ if (btf_name_printable(NULL, off))+ __CPROVER_assert(off < NR_TYPES && printable(names[off]),+ "a printable name");+ if (btf_decl_printable(NULL, id))+ __CPROVER_assert(walk_ends(id), "a declaration without a loop");+ unwrapped = btf_unwrap(NULL, id);+ __CPROVER_assert(!unwrapped ||+ (unwrapped < NR_TYPES &&+ !qualifier(BTF_INFO_KIND(types[unwrapped].t.info))),+ "a type without a qualifier");+}diff --git a/testing/proofs/cbmc/upatch-decoders.c b/testing/proofs/cbmc/upatch-decoders.cnew file mode 100644index 00000000..962a13bb--- /dev/null+++ b/testing/proofs/cbmc/upatch-decoders.c@@ -0,0 +1,71 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The DECODERS section of a .upatch, for sections of every length that+ * declare up to NR_MAX decoders: util/upatch.c reads only the entries the+ * section holds, and every decoder it views has terminated names and a+ * source inside the section, behind the entries.+ */++#include "proof.h"+#include "../../../util/upatch.c"++#define NR_MAX 2++static bool terminated(const char *s, size_t size)+{+ return memchr(s, '\0', size);+}++static void viewed(const struct upatch *u, __u32 idx)+{+ const __u8 *entries_end, *end, *source;+ struct upatch_decoder_view d;++ entries_end = u->map + sizeof(struct upatch_decoder_blob) ++ u->nr_decoders * sizeof(struct upatch_decoder_entry);+ end = u->map + u->map_size;+ __CPROVER_assert(!upatch_decoder_view(u, idx, &d), "viewed");+ source = (const __u8 *)d.source;+ __CPROVER_assert(d.source_len && source >= entries_end &&+ source <= end &&+ d.source_len <= (size_t)(end - source),+ "a source inside the section, behind the entries");+ __CPROVER_assert(terminated(d.upatch, sizeof(d.upatch)) &&+ terminated(d.map, sizeof(d.map)) &&+ terminated(d.source_name, sizeof(d.source_name)) &&+ terminated(d.entrypoint, sizeof(d.entrypoint)),+ "terminated names");+ __CPROVER_assert(d.language && d.api_version && d.input_format &&+ d.export_signal, "a language, API, input and signal");+}++void harness(void)+{+ struct upatch_decoder_view d;+ struct upatch u = {};+ __u32 len, i;+ __u8 *bp;++ bp = any_bytes(len);+ __CPROVER_assume(len < 12 || get_le32(bp + 8) <= NR_MAX);+ u.map = bp;+ u.map_size = len;+ if (!parse_decoders(&u, bp, len)) {+ __CPROVER_assert(!u.decoder_blob && !u.nr_decoders,+ "nothing kept of a refused section");+ __CPROVER_assert(upatch_decoder_view(&u, 0, &d) == -ENOENT &&+ !d.source, "no decoder to view");+ return;+ }+ __CPROVER_assert((const __u8 *)u.decoder_blob == bp &&+ u.decoder_blob_len == len &&+ u.nr_decoders == get_le32(bp + 8) && u.nr_decoders,+ "the whole section and its count");+ for (i = 0; i < NR_MAX; i++)+ if (i < u.nr_decoders)+ viewed(&u, i);+ __CPROVER_assert(upatch_decoder_view(&u, u.nr_decoders, &d) ==+ -ENOENT && !d.source, "none past the last");+}diff --git a/testing/proofs/cbmc/upatch-loader.c b/testing/proofs/cbmc/upatch-loader.cnew file mode 100644index 00000000..858e04c0--- /dev/null+++ b/testing/proofs/cbmc/upatch-loader.c@@ -0,0 +1,106 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The loader patterns util/upatch_rodata.c matches in a .upatch, for+ * loaders of LEN instructions, each length proofs.list names: every+ * matcher reads only the loader, those that look back from a call only+ * the instructions before it, each reports exactly what it matched, and+ * every map slot it returns indexes the table of maps.+ */++#include "proof.h"++/* The loader reads instruction immediates as unsigned on purpose. */+#pragma CPROVER check push+#pragma CPROVER check disable "conversion"+#include "../../../util/upatch_rodata.c"+#pragma CPROVER check pop++#define UNSET 0xdeadbeefu++static __u32 imm(const struct bpf_insn *insn)+{+ __u32 v;++ memcpy(&v, &insn->imm, sizeof(v));+ return v;+}++static bool lddw(const struct bpf_insn *insn, __u8 reg)+{+ return insn->code == (BPF_LD | BPF_DW | BPF_IMM) &&+ insn->dst_reg == reg &&+ insn->src_reg == BPF_PSEUDO_MAP_IDX_VALUE;+}++static void lddw_matched(const struct bpf_insn *insns)+{+ __u32 value = UNSET;+ size_t i;+ __u8 reg;++ __CPROVER_assume(i <= LEN);+ if (!loader_lddw(insns, LEN, i, reg, &value)) {+ __CPROVER_assert(value == UNSET, "nothing written");+ return;+ }+ __CPROVER_assert(i + 1 < LEN && lddw(&insns[i], reg) &&+ !insns[i].imm && !insns[i + 1].code &&+ value == imm(&insns[i + 1]),+ "a whole load of the register and its value");+}++static void call_matched(const struct bpf_insn *insns)+{+ __u32 cmd = UNSET, attr = UNSET, attr_size = UNSET;+ size_t i;++ __CPROVER_assume(i <= LEN);+ if (!sys_bpf_call(insns, LEN, i, &cmd, &attr, &attr_size)) {+ __CPROVER_assert(cmd == UNSET && attr_size == UNSET,+ "no command or size written");+ return;+ }+ __CPROVER_assert(i >= 4 && i < LEN &&+ insns[i].code == (BPF_JMP | BPF_CALL) &&+ insns[i].imm == BPF_FUNC_sys_bpf,+ "a call of sys_bpf()");+ __CPROVER_assert(insns[i - 4].dst_reg == BPF_REG_1 &&+ cmd == imm(&insns[i - 4]) &&+ lddw(&insns[i - 3], BPF_REG_2) &&+ attr == imm(&insns[i - 2]) &&+ insns[i - 1].dst_reg == BPF_REG_3 &&+ attr_size == imm(&insns[i - 1]),+ "the command, attr and size it passes");+}++/*+ * The call at the end of the loader, so that a read past it is one past+ * the loader too.+ */+static void slots_matched(const struct bpf_insn *insns)+{+ __u32 slot = UNSET, source = UNSET, attr, field;++ if (map_create_slot(insns, LEN, 0, LEN, &slot))+ __CPROVER_assert(slot < RODATA_MAPS_MAX, "a slot of the table");+ else+ __CPROVER_assert(slot == UNSET, "nothing written");+ slot = UNSET;+ if (map_update_slot(insns, LEN, LEN, attr, &slot))+ __CPROVER_assert(slot < RODATA_MAPS_MAX, "a slot of the table");+ else+ __CPROVER_assert(slot == UNSET, "nothing written");+ if (!loader_store_source(insns, LEN, LEN, field, &source))+ __CPROVER_assert(source == UNSET, "nothing written");+}++void harness(void)+{+ const struct bpf_insn *insns = any_bytes(LEN * sizeof(*insns));++ lddw_matched(insns);+ call_matched(insns);+ slots_matched(insns);+}diff --git a/testing/proofs/cbmc/upatch-name.c b/testing/proofs/cbmc/upatch-name.cnew file mode 100644index 00000000..b3429dfb--- /dev/null+++ b/testing/proofs/cbmc/upatch-name.c@@ -0,0 +1,63 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The upatch names util/upatch.c takes from outside, for every name of up+ * to LEN_MAX bytes: upatch_name_valid() accepts exactly [A-Za-z0-9._-]++ * not starting with a dot, so no path separator, "." or "..", and+ * upatch_select() exactly the archive names, something then ".upatch".+ */++#include "proof.h"+#include "../../../util/string.c"+#include "../../../util/upatch.c"++#define LEN_MAX 24++static bool name_char(char c)+{+ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||+ (c >= '0' && c <= '9') || c == '.' || c == '_' || c == '-';+}++static void name_valid(void)+{+ bool valid, chars = true;+ size_t n, i;+ char *name;++ __CPROVER_assume(n <= LEN_MAX);+ name = any_bytes(n + 1);+ __CPROVER_assume(!name[n] && strnlen(name, n) == n);+ valid = upatch_name_valid(name);+ for (i = 0; i < n; i++)+ chars &= name_char(name[i]);+ __CPROVER_assert(valid == (n > 0 && name[0] != '.' && chars),+ "exactly the names of the pattern");+ for (i = 0; i < n; i++)+ __CPROVER_assert(!valid || name[i] != '/', "no separator");+ __CPROVER_assert(!upatch_name_valid(NULL), "no name is invalid");+}++static void archive_name(void)+{+ static const char suffix[] = ".upatch";+ const size_t suffix_len = sizeof(suffix) - 1;+ struct dirent e;+ size_t n;+ int ret;++ __CPROVER_assume(n <= LEN_MAX + suffix_len &&+ !e.d_name[n] && strnlen(e.d_name, n) == n);+ ret = upatch_select(&e);+ __CPROVER_assert(ret == (n > suffix_len &&+ !memcmp(e.d_name + n - suffix_len, suffix,+ suffix_len)),+ "exactly a name before .upatch");+}++void harness(void)+{+ name_valid();+ archive_name();+}diff --git a/testing/proofs/cbmc/upatch-policy-labels.c b/testing/proofs/cbmc/upatch-policy-labels.cnew file mode 100644index 00000000..de21e3b3--- /dev/null+++ b/testing/proofs/cbmc/upatch-policy-labels.c@@ -0,0 +1,69 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The policy labels section of a .upatch, for sections of LEN bytes, each+ * length proofs.list names: util/upatch.c accepts one only when its labels+ * fill it, it names two distinct maps of the upatch, and every label has+ * one bit, a name and a terminated absolute path; a refusal writes nothing.+ */++#define PROOF_MEMCHR_QUANTIFIED+#include "proof.h"+#include "../../../util/upatch.c"++#define HEADER_LEN sizeof(struct upatch_policy_label_header)+#define ENTRY_LEN sizeof(struct upatch_policy_label_entry)++static bool terminated(const char *s, size_t size)+{+ return memchr(s, '\0', size);+}++static void label_valid(const __u8 *p, __u32 i)+{+ struct upatch_policy_label_entry e;+ __u64 bit;++ memcpy(&e, p + HEADER_LEN + i * ENTRY_LEN, sizeof(e));+ bit = le64_to_cpu(e.bit);+ __CPROVER_assert(bit && !(bit & (bit - 1)), "one bit");+ __CPROVER_assert(e.policy[0] && terminated(e.policy, sizeof(e.policy)),+ "a terminated policy name");+ __CPROVER_assert(e.path[0] == '/' && terminated(e.path, sizeof(e.path)),+ "a terminated absolute path");+}++void harness(void)+{+ __u32 nr_maps, idx, socket_idx, i;+ const __u32 unset = 0xdeadbeef;+ const size_t len = LEN;+ const size_t nr_fit = len < HEADER_LEN ? 0 :+ (len - HEADER_LEN) / ENTRY_LEN;+ __u8 *p;++ p = any_bytes(len);+ idx = unset;+ socket_idx = unset;+ if (!parse_policy_labels(p, len, nr_maps, &idx, &socket_idx)) {+ __CPROVER_assert(idx == unset && socket_idx == unset,+ "a refusal writes nothing");+ return;+ }+ __CPROVER_assert(get_le32(p) == UPATCH_POLICY_LABELS_MAGIC &&+ get_le32(p + 4) == UPATCH_POLICY_LABELS_VERSION,+ "its magic and version");+ __CPROVER_assert(nr_fit && get_le32(p + 8) == nr_fit &&+ len == HEADER_LEN + nr_fit * ENTRY_LEN,+ "labels that fill the section");+ __CPROVER_assert(idx == get_le32(p + 12) &&+ socket_idx == get_le32(p + 16),+ "the maps it names");+ __CPROVER_assert(idx < nr_maps && socket_idx < nr_maps &&+ idx < UPATCH_MAPS_MAX &&+ socket_idx < UPATCH_MAPS_MAX && idx != socket_idx,+ "two distinct maps of the upatch");+ for (i = 0; i < nr_fit; i++)+ label_valid(p, i);+}diff --git a/testing/proofs/cbmc/upatch-prog-specs.c b/testing/proofs/cbmc/upatch-prog-specs.cnew file mode 100644index 00000000..c4ab1e6b--- /dev/null+++ b/testing/proofs/cbmc/upatch-prog-specs.c@@ -0,0 +1,116 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The signed attach metadata of a .upatch, for metadata of LEN bytes, each+ * length proofs.list names, with as many programs as it has room for:+ * prog_specs_parse() reads nothing outside it, gives every program a known+ * role and kind with terminated names, every uprobe an absolute path and+ * rising offsets, and frees all it took on refusal, as prog_specs_free() does.+ */++#include "proof.h"+#include "../../../util/malloc.c"+#include "../../../util/upatch.c"++#define ENTRIES_LEN (LEN - (__s64)sizeof(struct upatch_prog_metadata))+#define NR_PROGS (ENTRIES_LEN < 0 ? 0 : \+ ENTRIES_LEN / sizeof(struct upatch_prog_entry))++static bool terminated(const char *s, size_t size)+{+ return memchr(s, '\0', size);+}++static bool needs_target(__u32 kind)+{+ return kind == UPATCH_ATTACH_KPROBE ||+ kind == UPATCH_ATTACH_KRETPROBE ||+ kind == UPATCH_ATTACH_UPROBE_MULTI;+}++static void uprobe_valid(const struct prog_spec *s)+{+ __u32 j;++ __CPROVER_assert(s->uprobe_path && s->uprobe_path[0] == '/' &&+ strnlen(s->uprobe_path, s->uprobe_path_len + 1) ==+ s->uprobe_path_len, "an absolute path of its length");+ __CPROVER_assert(!strchr(s->target, ',') && !strchr(s->target, '|'),+ "a target without separators");+ __CPROVER_assert(s->build_id_len && s->build_id_len <=+ UPATCH_BUILD_ID_MAX, "a build ID");+ __CPROVER_assert(s->nr_uprobe_offsets && s->uprobe_offsets,+ "an offset");+ for (j = 1; j < s->nr_uprobe_offsets; j++)+ __CPROVER_assert(s->uprobe_offsets[j - 1] <+ s->uprobe_offsets[j], "rising offsets");+}++static void attach_valid(const struct prog_spec *s)+{+ __CPROVER_assert(!s->map[0], "no map");+ __CPROVER_assert(s->kind <= UPATCH_ATTACH_UPROBE_MULTI,+ "a known attach kind");+ if (!needs_target(s->kind)) {+ __CPROVER_assert(!s->target[0] && !s->offset,+ "no target or offset");+ return;+ }+ __CPROVER_assert(s->target[0], "a target");+ __CPROVER_assert(s->kind == UPATCH_ATTACH_KPROBE || !s->offset,+ "an offset for a kprobe alone");+ if (s->kind == UPATCH_ATTACH_UPROBE_MULTI)+ uprobe_valid(s);+}++static void spec_valid(const struct prog_spec *s)+{+ __CPROVER_assert(s->target_fd == -1, "no descriptor yet");+ __CPROVER_assert(terminated(s->target, sizeof(s->target)) &&+ terminated(s->map, sizeof(s->map)),+ "terminated target and map");+ if (s->kind != UPATCH_ATTACH_UPROBE_MULTI)+ __CPROVER_assert(!s->uprobe_path && !s->uprobe_offsets,+ "uprobe targets for uprobes alone");+ switch (s->role) {+ case UPATCH_PROG_ROLE_ATTACH:+ attach_valid(s);+ break;+ case UPATCH_PROG_ROLE_SERIALIZER:+ __CPROVER_assert(s->map[0] && !s->target[0] && !s->kind &&+ !s->offset, "a map and nothing else");+ break;+ case UPATCH_PROG_ROLE_TEST_RUN:+ __CPROVER_assert(!s->map[0] && !s->target[0] && !s->kind &&+ !s->offset, "nothing");+ break;+ default:+ __CPROVER_assert(false, "a known role");+ }+}++void harness(void)+{+ struct prog_spec *specs = (void *)1;+ const __u32 nr_progs = NR_PROGS;+ __u32 nr_maps, i;+ __u8 *metadata;+ int ret;++ metadata = any_bytes(LEN);+ ret = prog_specs_parse(metadata, LEN, nr_maps, nr_progs, &specs);+ if (ret) {+ __CPROVER_assert(ret < 0 && !specs, "an error and no specs");+ return;+ }+ __CPROVER_assert(specs, "the specs");+ __CPROVER_assert(get_le32(metadata) == UPATCH_PROG_MAGIC &&+ get_le32(metadata + 4) == UPATCH_PROG_VERSION &&+ get_le32(metadata + 8) == nr_progs &&+ get_le32(metadata + 12) == nr_maps,+ "the magic, version and counts of the upatch");+ for (i = 0; i < nr_progs; i++)+ spec_valid(&specs[i]);+ prog_specs_free(specs, nr_progs);+}diff --git a/testing/proofs/cbmc/upatch-prog-uprobes.c b/testing/proofs/cbmc/upatch-prog-uprobes.cnew file mode 100644index 00000000..f2c88888--- /dev/null+++ b/testing/proofs/cbmc/upatch-prog-uprobes.c@@ -0,0 +1,9 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The attach metadata proof for metadata long enough to hold a uprobe+ * target block, which proofs.list runs with the bounds its records need.+ */++#include "upatch-prog-specs.c"diff --git a/testing/proofs/cbmc/upatch-rodata-btf.c b/testing/proofs/cbmc/upatch-rodata-btf.cnew file mode 100644index 00000000..fe9a6772--- /dev/null+++ b/testing/proofs/cbmc/upatch-rodata-btf.c@@ -0,0 +1,41 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * Where util/upatch_rodata.c finds the BTF of a .upatch, for loaders of+ * LEN instructions, each length proofs.list names, and data of DATA_LEN+ * bytes: it reads only the loader and the data, and the BTF it returns+ * lies inside the data.+ */++#include "proof.h"+#include "../../../util/string.c"+#include "../../../util/upatch.c"++/* The loader reads instruction immediates as unsigned on purpose. */+#pragma CPROVER check push+#pragma CPROVER check disable "conversion"+#include "../../../util/upatch_rodata.c"+#pragma CPROVER check pop++#define DATA_LEN 24++void harness(void)+{+ struct {+ struct bpf_insn insns[LEN];+ __u8 data[DATA_LEN];+ } loader;+ struct upatch u = {};+ __u32 off, size;++ u.map = (const __u8 *)&loader;+ u.map_size = sizeof(loader);+ u.hdr.loader_insns_len = sizeof(loader.insns);+ u.hdr.data_off = sizeof(loader.insns);+ u.hdr.data_len = DATA_LEN;+ if (loader_btf(&u, &off, &size))+ return;+ __CPROVER_assert(off <= DATA_LEN && size <= DATA_LEN - off,+ "BTF inside the data");+}diff --git a/testing/proofs/cbmc/upatch-rodata.c b/testing/proofs/cbmc/upatch-rodata.cnew file mode 100644index 00000000..f1f11b40--- /dev/null+++ b/testing/proofs/cbmc/upatch-rodata.c@@ -0,0 +1,106 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * Where util/upatch_rodata.c finds the .rodata value of a .upatch, with+ * data of DATA_LEN bytes, for loaders of any LEN instructions up to 8 and,+ * at LEN 24, for the gen_loader map create and update with any operands:+ * it reads only the loader and the data, and the value lies in the data.+ */++#define PROOF_MEMCPY_BYTEWISE+#include "proof.h"+#include "../../../util/string.c"+#include "../../../util/upatch.c"++/* The loader reads instruction immediates as unsigned on purpose. */+#pragma CPROVER check push+#pragma CPROVER check disable "conversion"+#include "../../../util/upatch_rodata.c"+#pragma CPROVER check pop++#define DATA_LEN 48+#define SHAPED_LEN 24++static struct btf_type datasec;++const struct btf_type *btf__type_by_id(const struct btf *btf, __u32 id)+{+ bool found;++ return found ? &datasec : NULL;+}++static void op(struct bpf_insn *insn, __u8 code, __u8 dst, __u8 src)+{+ insn->code = code;+ insn->dst_reg = dst;+ insn->src_reg = src;+ insn->off = 0;+}++static void lddw(struct bpf_insn *insn, __u8 dst)+{+ op(insn, BPF_LD | BPF_DW | BPF_IMM, dst, BPF_PSEUDO_MAP_IDX_VALUE);+ insn->imm = 0;+ op(insn + 1, 0, 0, 0);+}++static void sys_bpf(struct bpf_insn *insn, __s32 cmd)+{+ op(insn, BPF_ALU64 | BPF_MOV | BPF_K, BPF_REG_1, 0);+ insn->imm = cmd;+ lddw(insn + 1, BPF_REG_2);+ op(insn + 3, BPF_ALU64 | BPF_MOV | BPF_K, BPF_REG_3, 0);+ op(insn + 4, BPF_JMP | BPF_CALL, 0, 0);+ insn[4].imm = BPF_FUNC_sys_bpf;+}++/*+ * The fd copy and value store a map update needs, a map create, the slot+ * it stores its fd in, and the update; every other immediate stays free.+ */+static void gen_loader_shape(struct bpf_insn *insns)+{+ lddw(&insns[0], BPF_REG_2);+ op(&insns[2], BPF_LDX | BPF_W | BPF_MEM, BPF_REG_0, BPF_REG_2);+ lddw(&insns[3], BPF_REG_1);+ op(&insns[5], BPF_STX | BPF_W | BPF_MEM, BPF_REG_1, BPF_REG_0);+ lddw(&insns[6], BPF_REG_0);+ lddw(&insns[8], BPF_REG_1);+ op(&insns[10], BPF_STX | BPF_DW | BPF_MEM, BPF_REG_1, BPF_REG_0);+ sys_bpf(&insns[11], BPF_MAP_CREATE);+ lddw(&insns[16], BPF_REG_1);+ op(&insns[18], BPF_STX | BPF_W | BPF_MEM, BPF_REG_1, BPF_REG_7);+ sys_bpf(&insns[19], BPF_MAP_UPDATE_ELEM);+}++void harness(void)+{+ struct {+ struct bpf_insn insns[LEN];+ __u8 data[DATA_LEN];+ } loader;+ struct rodata_view view = {};+ struct upatch u = {};+ struct btf_type t;+ __u32 id;++ if (LEN == SHAPED_LEN)+ gen_loader_shape(loader.insns);+ u.map = (const __u8 *)&loader;+ u.map_size = sizeof(loader);+ u.hdr.loader_insns_len = sizeof(loader.insns);+ u.hdr.data_off = sizeof(loader.insns);+ u.hdr.data_len = DATA_LEN;+ datasec = t;+ if (find_rodata_data(&u, (const struct btf *)&u, id, &view))+ return;+ __CPROVER_assert(BTF_INFO_KIND(datasec.info) == BTF_KIND_DATASEC &&+ view.type_id == id && view.size == datasec.size,+ "the value of the section");+ __CPROVER_assert(view.data_offset <= DATA_LEN &&+ view.size <= DATA_LEN - view.data_offset &&+ view.bytes == loader.data + view.data_offset,+ "inside the data");+}diff --git a/testing/proofs/cbmc/upatch-signed-name.c b/testing/proofs/cbmc/upatch-signed-name.cnew file mode 100644index 00000000..391dc175--- /dev/null+++ b/testing/proofs/cbmc/upatch-signed-name.c@@ -0,0 +1,9 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The signed sections proof for data long enough to hold a name or a+ * decoder blob, where it takes a directory of one section to stay small.+ */++#include "upatch-signed-sections.c"diff --git a/testing/proofs/cbmc/upatch-signed-sections.c b/testing/proofs/cbmc/upatch-signed-sections.cnew file mode 100644index 00000000..e1980168--- /dev/null+++ b/testing/proofs/cbmc/upatch-signed-sections.c@@ -0,0 +1,99 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The signed sections of a .upatch, read from its data before any+ * signature is checked, for data of LEN bytes, each length proofs.list+ * names, with one section where it can hold a name: util/upatch.c keeps+ * sections only behind a valid trailer, each inside the data and below the+ * directory, the name terminated, and no length or count without them.+ */++#define PROOF_MEMCHR_QUANTIFIED+#include "proof.h"+#include "../../../util/upatch.c"++#define NAMED_LEN (UPATCH_NAME_MAX + \+ sizeof(struct upatch_signed_section) + \+ sizeof(struct upatch_signed_trailer))++static bool below(const struct upatch *u, const void *p, size_t len,+ __u32 dir_off)+{+ const __u8 *b = p;++ return b >= u->map && b <= u->map + dir_off &&+ len <= (size_t)(u->map + dir_off - b);+}++static bool kept_any(const struct upatch *u)+{+ return u->signed_name || u->signed_attach ||+ u->signed_unload_payload || u->signed_policy_labels ||+ u->decoder_blob;+}++static void forgotten(const struct upatch *u)+{+ __CPROVER_assert(!u->signed_attach_len &&+ !u->signed_policy_labels_len &&+ !u->decoder_blob_len && !u->nr_decoders &&+ !u->signed_policy_map_idx &&+ !u->signed_socket_policy_map_idx,+ "no length, count or map without a section");+}++void harness(void)+{+ const struct upatch_signed_trailer *tr;+ const size_t len = LEN;+ struct upatch u = {};+ __u32 dir_off, nr;++ u.map = any_bytes(len);+ tr = len < sizeof(*tr) ? NULL :+ (const void *)(u.map + len - sizeof(*tr));+ if (len >= NAMED_LEN)+ __CPROVER_assume(get_le32(&tr->nr_sections) <= 1);+ u.map_size = len;+ u.hdr.data_len = len;+ /*+ * No length here holds a policy labels section, which+ * upatch-policy-labels.c proves; without maps, CBMC sees that the+ * section is refused before its labels are read.+ */+ u.hdr.nr_maps = 0;+ parse_signed_sections(&u);+ if (!kept_any(&u)) {+ forgotten(&u);+ return;+ }+ dir_off = get_le32(&tr->dir_off);+ nr = get_le32(&tr->nr_sections);+ __CPROVER_assert(get_le32(&tr->magic) == UPATCH_SIGNED_TRAILER_MAGIC &&+ get_le32(&tr->version) == UPATCH_SIGNED_VERSION &&+ nr && nr <= UPATCH_SIGNED_SECTIONS_MAX,+ "a valid trailer");+ __CPROVER_assert((__u64)dir_off + nr *+ sizeof(struct upatch_signed_section) <=+ len - sizeof(*tr), "the directory below the trailer");+ __CPROVER_assert(!u.signed_name ||+ (below(&u, u.signed_name, UPATCH_NAME_MAX, dir_off) &&+ memchr(u.signed_name, '\0', UPATCH_NAME_MAX)),+ "a terminated name below the directory");+ __CPROVER_assert(!u.signed_attach ||+ (u.signed_attach_len &&+ below(&u, u.signed_attach, u.signed_attach_len,+ dir_off)),+ "attach metadata below the directory");+ __CPROVER_assert(!u.signed_unload_payload ||+ below(&u, u.signed_unload_payload,+ UPATCH_PAYLOAD_HASH_LEN, dir_off),+ "an unload digest below the directory");+ __CPROVER_assert(!u.signed_policy_labels, "no policy labels");+ __CPROVER_assert(!u.decoder_blob ||+ (u.nr_decoders &&+ below(&u, u.decoder_blob, u.decoder_blob_len,+ dir_off)),+ "decoders below the directory");+}