eBPF-based security platform
testing: prove that the target reads what the upatch writer lays out
Daniel Borkmann81a7e6b43951
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rstindex 4d4e6869..a2af5016 100644--- a/Documentation/contribute/testing/proof-coverage.rst+++ b/Documentation/contribute/testing/proof-coverage.rst@@ -350,6 +350,87 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.- Every encoding and every value of up to 8 bytes- The bits stay inside the integer's bytes; the value is zero- orsign-extended from them+ * - ``upatch-writer-header``+ - ``pack_header()`` of :src:`tools/shirudo-build/package.c`, read+ back by ``parse_header()``+ - Every value of every field and every name shorter than 256 bytes+ - The reader takes the header from a file of any size and reads back+ each value; the name is padded with zeros to its 256 bytes+ * - ``upatch-writer-sections``+ - ``compose_signed_data()``, read back by+ ``parse_signed_sections()``+ - Loader data of 0, 1, 7 and 8 bytes; a name with an unload digest,+ or with attach metadata of 1 to 8 bytes+ - The reader finds each section where the writer put it, at an+ 8-byte boundary, with its bytes and length, the loader's data in+ front and zeros between; a count of 0 or above 8 and data past what+ a length field holds are refused, with nothing written+ * - ``upatch-writer-load``+ - The four sections of a load, through the same two functions+ - 1 byte of loader data, 3 of attach metadata, any 217 bytes as+ decoders and any 4188 as policy labels+ - Data of the length the format gives; the reader keeps the sections+ exactly when ``parse_decoders()`` and ``parse_policy_labels()``+ take those bytes, each at its offset behind its padding, with the+ maps the labels name+ * - ``upatch-writer-decoders``+ - ``parse_decoder_manifest()`` and ``build_decoder_blob()``, read+ back by ``parse_decoders()`` and ``upatch_decoder_view()``+ - 0 to 2 decoders with sources of 3 and 1 bytes; any manifest the+ writer takes for one decoder, names of 2 bytes for two+ - A section of the length the format gives, its reserved field zero;+ each view shows the manifest's values, its names padded with zeros+ and the source's bytes at its offset; no section without a decoder+ and none past what a length field holds+ * - ``upatch-writer-prog-specs``+ - ``build_prog_metadata()`` and the section parsers it calls, read+ back by ``prog_specs_parse()``; libbpf's object and the ELF+ resolver modelled+ - 22 objects of 0 to 2 programs from a table: the sections attached+ by BTF target, cgroup and netfilter, a test run, serializers of+ one and two maps, one with any 3 bytes behind its map's first,+ a map name at its limit, and the sections and programs the writer+ must refuse; any map count, any two map names and any output map,+ or none+ - A table of the length the format gives; the reader takes it and+ reads back each role, kind, target, offset and map; a refusal+ leaves nothing written and nothing allocated+ * - ``upatch-writer-kprobes``+ - The kprobe entries of ``build_prog_metadata()``+ - 16 objects from a table: a symbol of 1 to 127 bytes, alone or with+ a decimal or hexadecimal offset of up to 64 bits, and the sections+ the writer must refuse, a symbol of 128 bytes and an offset behind+ ``kretprobe`` among them+ - The symbol and offset read back are the section's; what the reader+ refuses, or would cut short, is refused+ * - ``upatch-writer-xdp``+ - The XDP entries of ``build_prog_metadata()``+ - ``xdp/`` with a first digit of 0 to 4, 8 or 9 and any 3 bytes+ behind it; one program, or two that start with 1 and 2 or both+ with 1+ - The device classes read back are the decimal mask of the section;+ a mask the reader refuses, and a class two programs select, are+ refused+ * - ``upatch-writer-uprobes``+ - The uprobe target block of ``build_prog_metadata()``+ - 22 objects from a table: each uprobe section form at the path+ ``/x``, alone, before and behind another program, with any build+ ID of 1, 2, 3, 20 or 64 bytes and any one offset, or two rising+ ones for a glob; sections the writer must refuse; a resolver that+ fails+ - The reader takes the block and reads back each record's path,+ build ID and offsets for the program it names, a function's offset+ below 4 GiB; a refusal leaves nothing written and nothing allocated+ * - ``vouch-payload``+ - ``keys_vouch_payload()``, ``keys_verify_vouch()`` and+ ``keys_verify_signature()`` of :src:`util/keys.c`, the signature+ check a stub that takes the signed bytes from their signer alone+ - Certificates of 0 to 2 bytes, a set of two members, every+ allocation allowed to fail+ - What ``shirudo-build keys vouch`` signs is the prefix, its NUL and+ the certificate; the vouch admits that certificate under the+ member that signed it, and no other certificate of its length or+ longer; without memory nothing is written* - ``json-string``- ``parse_string()`` of :src:`util/json.c`- Lengths 0 to 6, every room up to one byte more@@ -850,13 +931,11 @@ a proof covers.whole or in part: the package state and tree state readers, whichselectors of a tree can meet on one node and the status verdict* - :src:`tools/shirudo-build/bpm-build.c`,- :src:`tools/shirudo-build/bpm-publish.c`,- :src:`tools/shirudo-build/package.c`,- :src:`tools/shirudo-build/vouch.c`+ :src:`tools/shirudo-build/bpm-publish.c`- No proof is written yet. The operator's Beefile reader and its- writers of the loader, the ``.upatch`` container, the package,- the index, the tree publication and the vouch: the proofs cover- what the target's readers accept, not what these write+ writers of the package, the index and the tree publication: the+ proofs cover what the target's readers accept, not what these+ write* - :src:`util/harden.c`, :src:`util/identity.c`,:src:`util/image_cache.c`, :src:`util/image_config.c`,:src:`util/image_guards.c`, :src:`util/image_keys.c`,@@ -871,7 +950,7 @@ a proof covers.``bpf()``, netlink, ``/proc`` and the process's own limits* - :src:`util/crypto_openssl.c`, :src:`util/curl.c`,:src:`util/elf_target.c`, :src:`util/kernel_abi.c`,- :src:`util/keys.c`, :src:`util/nv_writer.c`, :src:`util/sign.c`,+ :src:`util/nv_writer.c`, :src:`util/sign.c`,:src:`util/tls_openssl.c`, :src:`util/tpm.c`,:src:`util/upatch_decode.c`- Mostly calls into a library a harness would have to model whole:@@ -893,12 +972,14 @@ a proof covers.:src:`shirudo.c`, :src:`tools/shirudo-build/compile.c`,:src:`tools/shirudo-build/image.c`,:src:`tools/shirudo-build/shirudo-build.c`,- :src:`tools/shirudo-build/vmlinux.c`+ :src:`tools/shirudo-build/vmlinux.c`,+ :src:`tools/shirudo-build/vouch.c`- Commands: options, the order of calls into :src:`util/` andoutput. ``Shirudo.UpatchLoad`` models the protocols of ``init``,``upatch load`` and ``upatch unload``, by review, and the treesync harnesses read ``builtin-bpm.c``; no proof reads the other- commands+ commands. ``vouch.c`` has OpenSSL sign the bytes+ ``keys_vouch_payload()`` gives it, which ``vouch-payload`` coversNot covered yet---------------@@ -935,3 +1016,14 @@ that two XDP programs select disjoint device classes, which the``upatch`` unit cases check, ``find_rodata_data()``on loaders of other shapes, ``btf_dump_cost()``, the ``.rodata`` printersand the libbpf calls behind ``btf_rodata_btf()`` have no proof.++Within the ``.upatch`` writer, the proofs cover the functions that lay+out bytes, each with the reader of its part, but not what joins them:+``finalize_object()`` and ``package_resign_symbols()`` compute the+header's offsets around the loader libbpf generates and the signature+OpenSSL makes, ``build_package_load()`` writes the header of the policy+labels between its libbpf calls, and ``collect_decoder_manifests()``+picks the manifests out of ``.rodata``. The attach metadata is proven+for the sections of a table, not for every section name: a kprobe or+uprobe target of any bytes makes the reader's side of the proof too+large, and no proof reaches a uprobe path at its 4095-byte limit.diff --git a/Documentation/reference/formats/upatch-format.rst b/Documentation/reference/formats/upatch-format.rstindex 38a35135..97c40915 100644--- a/Documentation/reference/formats/upatch-format.rst+++ b/Documentation/reference/formats/upatch-format.rst@@ -757,7 +757,10 @@ CBMC proves these checks of the reader for every header, of a file ofany size, and for the signed sections, decoders, policy labels andprogram table within the bounds:doc:`/contribute/testing/proof-coverage` lists: what the reader accepts-lies inside the file, and what it refuses leaves nothing behind.+lies inside the file, and what it refuses leaves nothing behind. Within+the bounds listed there it also proves that the reader takes what the+writer lays out, and reads back the header, the signed sections, the+decoders and the program table as they were written.Target admission----------------diff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.listindex c126369f..884f0f46 100644--- a/testing/proofs/cbmc/proofs.list+++ b/testing/proofs/cbmc/proofs.list@@ -99,3 +99,12 @@ config-evidence-parse --unwind 1 --unwindset config_evidence_parse.0:7,on_event.config-evidence-roundtrip --unwind 1 --unwindset config_evidence_emit.0:4,hex_encode.0:33,config_evidence_parse.0:7,on_event.0:4,replay.0:4,hex_decode.0:33,strlen.0:66,strcmp.0:15,named.0:7,json_parse.0:7,json_parse.1:4,give_event.0:4,give_text.0:66,json_kv_string.0:66,sequence_overflows.0:4,replays.0:4,same.0:33,blank.0:33,check_read_back.0:4 --memory-leak-check --slice-formula LEN=0-2config-evidence-verify --unwind 1 --unwindset replay.0:4,memcmp.0:33,in_order.0:4,check_event.0:24,same.0:33 --slice-formula LEN=0-3config-evidence-collect --unwind 1 --unwindset config_update_events.0:4,replay.0:4,memcmp.0:33,history.0:4,check_exported.0:4,same.0:33,blank.0:33 --memory-leak-check --slice-formula LEN=0-2+upatch-writer-header -I ../../.. -I ../../../vendor/libbpf/include/uapi -I ../../../vendor/libbpf --unwind 2 --unwindset strnlen.0:257,memchr.0:257,memcmp.0:33+upatch-writer-sections -I ../../.. -I ../../../vendor/libbpf/include/uapi -I ../../../vendor/libbpf --memory-leak-check --max-field-sensitivity-array-size 512 --object-bits 10 --unwind 2 --unwindset harness.0:9,compose_signed_data.0:3,compose_signed_data.1:3,parse_signed_sections.0:3,parse_decoders.0:0,parse_policy_labels.0:0 LEN=0,1,7,8+upatch-writer-load -I ../../.. -I ../../../vendor/libbpf/include/uapi -I ../../../vendor/libbpf --max-field-sensitivity-array-size 8192 --unwind 2 --unwindset compose_signed_data.0:5,compose_signed_data.1:5,parse_signed_sections.0:5,parse_decoders.0:2,parse_policy_labels.0:2 LEN=1+upatch-writer-decoders -I ../../.. -I ../../../vendor/libbpf/include/uapi -I ../../../vendor/libbpf --memory-leak-check --max-field-sensitivity-array-size 512 --unwind 3 --unwindset strnlen.0:65,memchr.0:65,copy_bytes.0:65,viewed.0:4 LEN=0-2+upatch-writer-prog-specs -I ../../.. -I ../../../vendor/libbpf/include/uapi -I ../../../vendor/libbpf --refine-arithmetic --memory-leak-check --max-field-sensitivity-array-size 512 --object-bits 12 --unwind 4 --unwindset strlen.0:161,strcmp.0:161,strncmp.0:21,strchr.0:161,strrchr.0:161,strnlen.0:161,memchr.0:129,memcmp.0:129,parse_base.0:21,parse_base.1:21,digits.0:21,default_attach_section.0:8,uprobe_section_target.0:9,text.0:161,decimal.0:5,resolve.1:65 LEN=0-21+upatch-writer-kprobes -I ../../.. -I ../../../vendor/libbpf/include/uapi -I ../../../vendor/libbpf --refine-arithmetic --memory-leak-check --max-field-sensitivity-array-size 512 --object-bits 12 --unwind 4 --unwindset strlen.0:161,strcmp.0:161,strncmp.0:21,strchr.0:161,strrchr.0:161,strnlen.0:161,memchr.0:129,memcmp.0:129,parse_base.0:21,parse_base.1:21,digits.0:21,default_attach_section.0:8,uprobe_section_target.0:9,text.0:161,decimal.0:5,resolve.1:65 LEN=0-15+upatch-writer-xdp -I ../../.. -I ../../../vendor/libbpf/include/uapi -I ../../../vendor/libbpf --refine-arithmetic --memory-leak-check --max-field-sensitivity-array-size 512 --object-bits 12 --unwind 4 --unwindset strlen.0:161,strcmp.0:161,strncmp.0:21,strchr.0:161,strrchr.0:161,strnlen.0:161,memchr.0:129,memcmp.0:129,parse_base.0:21,parse_base.1:21,digits.0:21,default_attach_section.0:8,uprobe_section_target.0:9,text.0:161,decimal.0:5,resolve.1:65 LEN=0-8+upatch-writer-uprobes -I ../../.. -I ../../../vendor/libbpf/include/uapi -I ../../../vendor/libbpf --refine-arithmetic --memory-leak-check --max-field-sensitivity-array-size 512 --object-bits 12 --unwind 4 --unwindset strlen.0:161,strcmp.0:161,strncmp.0:21,strchr.0:161,strrchr.0:161,strnlen.0:161,memchr.0:129,memcmp.0:129,parse_base.0:21,parse_base.1:21,digits.0:21,default_attach_section.0:8,uprobe_section_target.0:9,text.0:161,decimal.0:5,resolve.1:65 LEN=0-21+vouch-payload --malloc-may-fail --malloc-fail-null --unwind 3 --unwindset memcmp.0:33,harness.0:5 LEN=0-2diff --git a/testing/proofs/cbmc/upatch-writer-decoders.c b/testing/proofs/cbmc/upatch-writer-decoders.cnew file mode 100644index 00000000..86c5f849--- /dev/null+++ b/testing/proofs/cbmc/upatch-writer-decoders.c@@ -0,0 +1,171 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The decoders build_decoder_blob() of package.c writes for LEN decoders,+ * each of any manifest parse_decoder_manifest() takes from .rodata and a+ * short source: the section has the length the format gives,+ * parse_decoders() of util/upatch.c takes it, and upatch_decoder_view()+ * shows each manifest's values and names and each source.+ */++/* As proof.h, before the system headers can include <ctype.h>. */+#define __NO_CTYPE 1++#include <openssl/evp.h>+#include <linux/bpf.h>+#include <bpf/libbpf.h>++#include "proof.h"++/*+ * CBMC's memcpy() makes all of a heap object symbolic when the length is,+ * and a loop makes an integer it fills so. A length the compiler knows+ * takes the first and any other the loop, which leaves the counts and+ * offsets the reader branches on the constants they are.+ */+static void *copy_bytes(void *dst, const void *src, size_t n, bool known)+{+ const __u8 *s = src;+ __u8 *d = dst;+ size_t i;++ if (known)+ return memcpy(dst, src, n);+ for (i = 0; i < n; i++)+ d[i] = s[i];+ return dst;+}++#define memcpy(dst, src, n) \+ copy_bytes(dst, src, n, __builtin_constant_p(n))++#include "../../../util/malloc.c"+#include "../../../util/string.c"+#include "../../../util/upatch.c"+#include "../../../tools/shirudo-build/package.c"++#define NR_MAX 2+#define ENTRIES_END (16 + 200 * LEN)++static const __u32 source_len[NR_MAX] = { 3, 1 };++static void short_name(char *name, char first, __u32 idx)+{+ name[0] = first;+ name[1] = (char)('0' + idx);+ name[2] = '\0';+}++/*+ * A manifest of any bytes, which for more than one decoder spells short+ * names, so that the lengths the writer copies are constants to CBMC.+ */+static const struct decoder_manifest *any_manifest(__u32 idx)+{+ struct decoder_manifest *m = any_bytes(sizeof(*m));++ if (LEN > 1) {+ short_name(m->upatch, 'u', idx);+ short_name(m->map, 'm', idx);+ short_name(m->source, 's', idx);+ short_name(m->entrypoint, 'e', idx);+ }+ return m;+}++/* The name the manifest's size bytes spell, padded with zeros. */+static bool same_name(const char *got, const char *manifest, size_t size)+{+ size_t n = strnlen(manifest, size), i;++ __CPROVER_assume(i < size);+ return got[i] == (i < n ? manifest[i] : '\0');+}++static void viewed(const struct upatch *u, __u32 idx,+ const struct decoder_manifest *m, const char *source,+ __u32 source_off)+{+ struct upatch_decoder_view d;+ __u32 i;++ __CPROVER_assert(!upatch_decoder_view(u, idx, &d), "viewed");+ __CPROVER_assert(d.language == get_le16(&m->language) &&+ d.api_version == get_le16(&m->api_version) &&+ d.input_format == get_le16(&m->input_format) &&+ d.export_signal == get_le16(&m->export_signal),+ "the language, API, input and signal of the manifest");+ __CPROVER_assert(same_name(d.upatch, m->upatch, sizeof(m->upatch)) &&+ same_name(d.map, m->map, sizeof(m->map)) &&+ same_name(d.source_name, m->source,+ sizeof(m->source)) &&+ same_name(d.entrypoint, m->entrypoint,+ sizeof(m->entrypoint)),+ "the names of the manifest");+ __CPROVER_assert((const __u8 *)d.source == u->map + source_off &&+ d.source_len == source_len[idx],+ "a source behind the entries and the sources before");+ for (i = 0; i < source_len[idx]; i++)+ __CPROVER_assert(u->map[source_off + i] == (__u8)source[i],+ "the bytes of the source");+}++/* The shortest source with which a section passes what its length holds. */+static void refused(void)+{+ struct decoder_file big = { .source_len = UINT32_MAX - 215 };+ struct decoder_set set = { &big, 1 };+ __u32 len = 0xdeadbeef;+ void *out = (void *)1;++ __CPROVER_assert(build_decoder_blob(&set, &out, &len) == -EOVERFLOW &&+ !out && !len,+ "no section past what a length field holds");+}++void harness(void)+{+ const struct decoder_manifest *manifest[NR_MAX];+ __u32 len = 0xdeadbeef, off = ENTRIES_END, i;+ struct decoder_file items[NR_MAX];+ struct decoder_set set = { items, LEN };+ void *blob = (void *)1;+ struct upatch u = {};+ size_t n;++ for (i = 0; i < LEN; i++) {+ manifest[i] = any_manifest(i);+ /* collect_decoder_manifests() skips what this does not take. */+ __CPROVER_assume(parse_decoder_manifest((const void *)manifest[i],+ n, &items[i].manifest));+ /* decoder_set_add() refuses an empty source. */+ items[i].source = any_bytes(source_len[i]);+ items[i].source_len = source_len[i];+ off += source_len[i];+ }+ __CPROVER_assert(!build_decoder_blob(&set, &blob, &len), "built");+ if (!LEN) {+ __CPROVER_assert(!blob && !len, "no section without a decoder");+ refused();+ return;+ }+ __CPROVER_assert(blob && len == off,+ "a section of the length the format gives");+ /* As just shown, so that CBMC takes both for the constants they are. */+ __CPROVER_assume(blob && len == off);+ u.map = blob;+ u.map_size = len;+ __CPROVER_assert(parse_decoders(&u, blob, len) &&+ u.nr_decoders == LEN,+ "the reader takes what the writer wrote");+ if (!u.decoder_blob)+ return;+ __CPROVER_assert(!get_le32(u.map + 12), "a zero reserved field");+ off = ENTRIES_END;+ for (i = 0; i < LEN; i++) {+ viewed(&u, i, manifest[i], items[i].source, off);+ off += source_len[i];+ }+ xfree(blob, len);+}diff --git a/testing/proofs/cbmc/upatch-writer-header.c b/testing/proofs/cbmc/upatch-writer-header.cnew file mode 100644index 00000000..c69d24d4--- /dev/null+++ b/testing/proofs/cbmc/upatch-writer-header.c@@ -0,0 +1,65 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The header pack_header() of tools/shirudo-build/package.c writes, for+ * every value of every field and every name shorter than its 256 bytes:+ * util/upatch.c's parse_header() takes it from a file of any size and+ * reads back each value, the name padded with zeros as the signed one is.+ */++/* As proof.h, before the system headers can include <ctype.h>. */+#define __NO_CTYPE 1++#include <openssl/evp.h>+#include <linux/bpf.h>+#include <bpf/libbpf.h>++#include "proof.h"+#include "../../../util/string.c"+#include "../../../util/upatch.c"+#include "../../../tools/shirudo-build/package.c"++void harness(void)+{+ __u32 loader_off, loader_len, sig_off, sig_len, meta_off, meta_len;+ __u32 data_off, data_len, log_buf_sz, rodata_data_off;+ __u32 type, flags, nr_maps, nr_progs;+ __u8 payload[UPATCH_PAYLOAD_HASH_LEN];+ struct upatch_host_header out;+ char name[UPATCH_NAME_MAX];+ struct upatch_header h;+ size_t len, n, i;++ /*+ * finalize_object() packs a name its strscpy() fit into 256 bytes,+ * package_resign_symbols() one upatch_header_name() took from a+ * header in which it found a NUL.+ */+ __CPROVER_assume(len < sizeof(name) && !name[len] &&+ strnlen(name, len) == len);+ pack_header(&h, name, type, flags, payload, loader_off, loader_len,+ sig_off, sig_len, meta_off, meta_len, data_off, data_len,+ log_buf_sz, rodata_data_off, nr_maps, nr_progs);+ __CPROVER_assume(n >= sizeof(h));+ __CPROVER_assert(!parse_header((const __u8 *)&h, n, &out),+ "the reader takes the header");+ __CPROVER_assert(out.type == type && out.flags == flags &&+ out.nr_maps == nr_maps && out.nr_progs == nr_progs &&+ out.log_buf_sz == log_buf_sz &&+ out.rodata_data_off == rodata_data_off,+ "the type, flags, counts, log size and .rodata offset");+ __CPROVER_assert(out.loader_insns_off == loader_off &&+ out.loader_insns_len == loader_len &&+ out.signature_off == sig_off &&+ out.signature_len == sig_len &&+ out.metadata_off == meta_off &&+ out.metadata_len == meta_len &&+ out.data_off == data_off && out.data_len == data_len,+ "the offset and length of every section");+ __CPROVER_assume(i < sizeof(name));+ __CPROVER_assert(out.name[i] == (i < len ? name[i] : '\0'),+ "the name, padded with zeros");+ __CPROVER_assert(!memcmp(out.payload_sha256, payload, sizeof(payload)),+ "the payload digest");+}diff --git a/testing/proofs/cbmc/upatch-writer-kprobes.c b/testing/proofs/cbmc/upatch-writer-kprobes.cnew file mode 100644index 00000000..ded59a14--- /dev/null+++ b/testing/proofs/cbmc/upatch-writer-kprobes.c@@ -0,0 +1,11 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The attach metadata proof for kprobe and kretprobe sections: a symbol of+ * up to 127 bytes and an offset of up to 64 bits, decimal or hexadecimal,+ * read back as written, and the sections the writer must refuse.+ */++#define KPROBE_OBJECTS+#include "upatch-writer-prog-specs.c"diff --git a/testing/proofs/cbmc/upatch-writer-load.c b/testing/proofs/cbmc/upatch-writer-load.cnew file mode 100644index 00000000..bbcb2c4f--- /dev/null+++ b/testing/proofs/cbmc/upatch-writer-load.c@@ -0,0 +1,86 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The four signed sections of a load, as compose_signed_data() lays them+ * behind loader data of LEN bytes: the name, attach metadata that ends off+ * a boundary, and any bytes for one decoder and one policy label. The data+ * has the length the format gives, and the reader keeps the sections, each+ * where it was written, exactly when it takes those decoders and labels.+ */++#define LOAD_SECTIONS+#include "upatch-writer-sections.c"++#define ATTACH_LEN 3+#define DECODERS_LEN (sizeof(struct upatch_decoder_blob) + \+ sizeof(struct upatch_decoder_entry) + 1)+#define LABELS_LEN (sizeof(struct upatch_policy_label_header) + \+ sizeof(struct upatch_policy_label_entry))+#define ALIGNED(len) (((len) + 7) & ~7u)+#define NAME_OFF ALIGNED(LEN)+#define ATTACH_OFF (NAME_OFF + UPATCH_NAME_MAX)+#define DECODERS_OFF ALIGNED(ATTACH_OFF + ATTACH_LEN)+#define LABELS_OFF ALIGNED(DECODERS_OFF + DECODERS_LEN)+#define DIR_OFF ALIGNED(LABELS_OFF + LABELS_LEN)+#define DATA_LEN (DIR_OFF + 4 * sizeof(struct upatch_signed_section) + \+ sizeof(struct upatch_signed_trailer))++static bool written(const struct upatch *u, const void *got, __u32 off,+ const void *p, __u32 len)+{+ return got == u->map + off && read_back(u, got, p, len);+}++void harness(void)+{+ __u32 nr_maps, map_idx, socket_map_idx, len = 0xdeadbeef, k;+ const __u8 *decoders = any_bytes(DECODERS_LEN);+ const __u8 *labels = any_bytes(LABELS_LEN);+ const __u8 *attach = any_bytes(ATTACH_LEN);+ const __u8 *data = any_bytes(LEN);+ struct upatch u = {}, alone = {};+ void *out = (void *)1;+ bool taken;++ /* finalize_object() signs a name its strscpy() fit into 256 bytes. */+ name = any_bytes(UPATCH_NAME_MAX);+ __CPROVER_assume(k < UPATCH_NAME_MAX && !name[k]);+ u.hdr.nr_maps = nr_maps;+ taken = parse_decoders(&alone, decoders, DECODERS_LEN) &&+ parse_policy_labels(labels, LABELS_LEN, nr_maps, &map_idx,+ &socket_map_idx);+ add_section(UPATCH_SECTION_NAME, name, UPATCH_NAME_MAX);+ add_section(UPATCH_SECTION_ATTACH, attach, ATTACH_LEN);+ add_section(UPATCH_SECTION_DECODERS, decoders, DECODERS_LEN);+ add_section(UPATCH_SECTION_POLICY_LABELS, labels, LABELS_LEN);+ __CPROVER_assert(!compose_signed_data(data, LEN, sections, nr_sections,+ &out, &len) && len == DATA_LEN,+ "data of the length the format gives");+ u.map = out;+ u.map_size = len;+ u.hdr.data_len = len;+ parse_signed_sections(&u);+ __CPROVER_assert(!!u.signed_name == taken,+ "kept exactly with decoders and labels the reader takes");+ if (!taken)+ return;+ __CPROVER_assert(written(&u, u.signed_name, NAME_OFF, name,+ UPATCH_NAME_MAX) &&+ written(&u, u.signed_attach, ATTACH_OFF, attach,+ ATTACH_LEN) &&+ u.signed_attach_len == ATTACH_LEN,+ "the name and the attach metadata");+ __CPROVER_assert(written(&u, u.decoder_blob, DECODERS_OFF, decoders,+ DECODERS_LEN) &&+ u.decoder_blob_len == DECODERS_LEN &&+ u.nr_decoders == alone.nr_decoders,+ "the decoders behind the padding");+ __CPROVER_assert(written(&u, u.signed_policy_labels, LABELS_OFF,+ labels, LABELS_LEN) &&+ u.signed_policy_labels_len == LABELS_LEN &&+ u.signed_policy_map_idx == map_idx &&+ u.signed_socket_policy_map_idx == socket_map_idx,+ "the policy labels and the maps they name");+ __CPROVER_assert(!u.signed_unload_payload, "no unload digest");+}diff --git a/testing/proofs/cbmc/upatch-writer-prog-specs.c b/testing/proofs/cbmc/upatch-writer-prog-specs.cnew file mode 100644index 00000000..a941cc12--- /dev/null+++ b/testing/proofs/cbmc/upatch-writer-prog-specs.c@@ -0,0 +1,470 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The attach metadata build_prog_metadata() of package.c writes for the+ * programs LEN picks from a table, with libbpf's object and the ELF+ * resolver modelled: it has the length the format gives, prog_specs_parse()+ * of util/upatch.c takes it and reads back what each section spells, and+ * what the reader refuses is refused with nothing written.+ */++/* As proof.h, before the system headers can include <ctype.h>. */+#define __NO_CTYPE 1++#include <openssl/evp.h>+#include <linux/bpf.h>+#include <bpf/libbpf.h>++#include "proof.h"+#include "../../../util/malloc.c"+#include "../../../util/string.c"+#include "../../../util/upatch.c"+#include "../../../tools/shirudo-build/package.c"++#define TAIL 3+#define TEXT_MAX 160+#define NR_PROGS_MAX 2+#define NR_OFFSETS 2+#define UPROBE_PATH "/x"+#define A15 "aaaaaaaaaaaaaaa"+#define A127 A15 "a" A15 "a" A15 "a" A15 "a" A15 "a" A15 "a" \+ A15 "a" A15++enum { TAIL_NONE, TAIL_SEC, TAIL_NAME };+enum { TAKEN, EITHER, REFUSED };++/*+ * A program: its section and name, any TAIL bytes behind the one or the+ * other where tail says so, whether the writer takes it, and what the+ * reader then gives. A uprobe's binary has a build ID of build_id_len+ * bytes, and the resolver finds it unless no_binary is set.+ */+struct shape {+ const char *sec;+ const char *name;+ __u32 tail;+ __u32 verdict;+ __u32 role;+ __u32 kind;+ const char *target;+ __u64 offset;+ __u32 build_id_len;+ bool no_binary;+};++#define PROG(sec, verdict, kind, ...) \+ { sec, "p", TAIL_NONE, verdict, UPATCH_PROG_ROLE_ATTACH, \+ UPATCH_ATTACH_##kind, __VA_ARGS__ }+#define PLAIN(sec, kind) \+ PROG(sec, TAKEN, kind, "")+#define PROBE(sec, kind, target, offset) \+ PROG(sec, TAKEN, kind, target, offset)+#define REFUSE(sec) \+ { sec, "p", TAIL_NONE, REFUSED }+#define XDP(digit, verdict) \+ { "xdp/" digit, "p", TAIL_SEC, verdict, UPATCH_PROG_ROLE_ATTACH, \+ UPATCH_ATTACH_XDP }+#define SERIALIZER(map, tail, verdict) \+ { "syscall", SERIALIZER_PREFIX map, tail, verdict, \+ UPATCH_PROG_ROLE_SERIALIZER }+#define TEST_RUN(id, verdict) \+ { "syscall", TEST_RUN_PREFIX id, TAIL_NONE, verdict, \+ UPATCH_PROG_ROLE_TEST_RUN, 0, "" }+#define AT(sec, at) \+ sec "/" UPROBE_PATH ":" at+/* The writer refuses a function the resolver finds beyond 4 GiB. */+#define UPROBE(sec, kind, target, offset, build_id_len) \+ PROG(sec, EITHER, kind, target, offset, build_id_len)+#define GLOB(sec, kind, target, build_id_len) \+ PROG(sec, TAKEN, kind, target, 0, build_id_len)+#define NO_BINARY(sec, kind) \+ PROG(sec, REFUSED, kind, "f", 0, 20, true)++static const struct {+ __u32 nr_progs;+ struct shape progs[NR_PROGS_MAX];+} objects[] = {+#if defined(KPROBE_OBJECTS)+ { 1, { PROBE("kprobe/f", KPROBE, "f", 0) } },+ { 1, { PROBE("kprobe/fn+7", KPROBE, "fn", 7) } },+ { 1, { PROBE("kprobe/a+b+0x1122334455667788", KPROBE, "a+b",+ 0x1122334455667788) } },+ { 1, { PROBE("kprobe/" A127, KPROBE, A127, 0) } },+ { 1, { PROBE("kretprobe/f", KRETPROBE, "f", 0) } },+ { 2, { PROBE("kretprobe/g+0", KRETPROBE, "g", 0),+ PROBE("kprobe/f+18446744073709551615", KPROBE, "f",+ UINT64_MAX) } },+ { 1, { REFUSE("kprobe/" A127 "a") } },+ { 1, { REFUSE("kretprobe/f+1") } },+ { 1, { REFUSE("kprobe/+1") } },+ { 1, { REFUSE("kprobe/f+") } },+ { 1, { REFUSE("kprobe/f+07") } },+ { 1, { REFUSE("kprobe.multi/f") } },+ { 1, { REFUSE("kprobe") } },+ { 1, { REFUSE("kprobe/") } },+ { 1, { REFUSE("kretprobe") } },+ { 1, { REFUSE("kretprobe/") } },+#elif defined(XDP_OBJECTS)+ { 1, { XDP("0", EITHER) } },+ { 1, { XDP("1", EITHER) } },+ { 1, { XDP("2", EITHER) } },+ { 1, { XDP("3", EITHER) } },+ { 1, { XDP("4", REFUSED) } },+ { 1, { XDP("8", EITHER) } },+ { 1, { XDP("9", EITHER) } },+ { 2, { XDP("1", EITHER), XDP("2", EITHER) } },+ { 2, { XDP("1", EITHER), XDP("1", EITHER) } },+#elif defined(UPROBE_OBJECTS)+ { 1, { UPROBE(AT("uprobe", "f"), UPROBE, "f", 0, 20) } },+ { 1, { UPROBE(AT("uprobe.s", "fn+0x1f"), UPROBE, "fn", 31, 1) } },+ { 1, { UPROBE(AT("uprobe", "0x1f"), UPROBE, "0x1f", 0,+ UPATCH_BUILD_ID_MAX) } },+ { 1, { UPROBE(AT("uretprobe.s", "f"), URETPROBE, "f", 0, 20) } },+ { 1, { UPROBE(AT("uretprobe", "0X2"), URETPROBE, "0X2", 0, 20) } },+ { 1, { GLOB(AT("uprobe.multi.s", "f*"), UPROBE_MULTI, "f*",+ UPATCH_BUILD_ID_MAX) } },+ { 1, { GLOB(AT("uretprobe.multi", "?"), URETPROBE_MULTI, "?", 1) } },+ { 1, { GLOB(AT("uprobe.multi", A127), UPROBE_MULTI, A127, 20) } },+ { 2, { GLOB(AT("uprobe.multi", "f*"), UPROBE_MULTI, "f*", 20),+ UPROBE(AT("uprobe", "g+9"), UPROBE, "g", 9, 3) } },+ { 2, { PLAIN("fentry/f", RAW_TRACEPOINT),+ UPROBE(AT("uretprobe", "g"), URETPROBE, "g", 0, 20) } },+ { 2, { UPROBE(AT("uprobe.s", "0x8"), UPROBE, "0x8", 0, 2),+ PLAIN("tp_btf/f", RAW_TRACEPOINT) } },+ { 1, { REFUSE(AT("uretprobe", "f+1")) } },+ { 1, { REFUSE(AT("uprobe", "0xg")) } },+ { 1, { REFUSE(AT("uprobe.multi", "f,g")) } },+ { 1, { REFUSE(AT("uretprobe.multi.s", "f|g")) } },+ { 1, { REFUSE(AT("uprobe.multi", A127 "a")) } },+ { 1, { REFUSE(AT("uprobe.multi", "")) } },+ { 1, { REFUSE("uprobe/x:f") } },+ { 1, { REFUSE("uretprobe/" UPROBE_PATH) } },+ { 1, { REFUSE("uprobe") } },+ { 1, { NO_BINARY(AT("uprobe.multi", "f"), UPROBE_MULTI) } },+ { 1, { NO_BINARY(AT("uprobe", "f"), UPROBE) } },+#else+ { 0 },+ { 1, { PLAIN("fentry/f", RAW_TRACEPOINT) } },+ { 1, { PLAIN("?lsm.s/f", RAW_TRACEPOINT) } },+ { 1, { PLAIN("fmod_ret/f", FMOD_RET) } },+ { 1, { PLAIN("fmod_ret.s/f", FMOD_RET) } },+ { 1, { PLAIN("netfilter", NETFILTER_IPV6_PRE_ROUTING) } },+ { 2, { PLAIN("cgroup_skb/ingress", CGROUP_SKB_INGRESS),+ PLAIN("cgroup_skb/egress", CGROUP_SKB_EGRESS) } },+ { 1, { TEST_RUN("t", TAKEN) } },+ { 2, { TEST_RUN("t", TAKEN), TEST_RUN("u", REFUSED) } },+ { 1, { { "syscall", "p", TAIL_NONE, REFUSED } } },+ { 1, { SERIALIZER("m", TAIL_NAME, EITHER) } },+ { 1, { SERIALIZER(A15, TAIL_NONE, EITHER) } },+ { 2, { SERIALIZER("a", TAIL_NONE, EITHER),+ SERIALIZER("b", TAIL_NONE, EITHER) } },+ { 2, { SERIALIZER("a", TAIL_NONE, EITHER),+ SERIALIZER("a", TAIL_NONE, REFUSED) } },+ { 1, { SERIALIZER(A15 "a", TAIL_NONE, REFUSED) } },+ { 1, { SERIALIZER(SERIAL_MAP_NAME, TAIL_NONE, REFUSED) } },+ { 1, { { "fentry/f", SERIALIZER_PREFIX "m", TAIL_NONE, REFUSED } } },+ { 1, { REFUSE("fmod_ret") } },+ { 1, { REFUSE("cgroup/skb") } },+ { 1, { REFUSE("sk_skb") } },+ { 1, { REFUSE("socket") } },+ { 1, { REFUSE("tracepoint/f") } },+#endif+};++#define NR_PROGS (objects[LEN].nr_progs)++struct bpf_program {+ __u32 idx;+ char name[TEXT_MAX];+ char sec[TEXT_MAX];+};++struct bpf_map {+ __u32 idx;+ char name[TEXT_MAX];+ __u32 type;+ __u32 key_size;+ __u32 value_size;+ __u32 max_entries;+};++/* The programs, two maps of any name and the serializers' output map. */+struct bpf_object {+ struct bpf_program progs[NR_PROGS_MAX];+ struct bpf_map maps[3];+};++/* What the ELF resolver found for each uprobe, in the order it was asked. */+static struct elf_target resolved[NR_PROGS_MAX];+static __u64 resolved_offsets[NR_PROGS_MAX][NR_OFFSETS];+static __u32 nr_resolved;++struct bpf_program *bpf_object__next_program(const struct bpf_object *obj,+ struct bpf_program *prev)+{+ __u32 idx = prev ? prev->idx + 1 : 0;++ return idx < NR_PROGS ? (struct bpf_program *)&obj->progs[idx] : NULL;+}++const char *bpf_program__name(const struct bpf_program *prog)+{+ return prog->name;+}++const char *bpf_program__section_name(const struct bpf_program *prog)+{+ return prog->sec;+}++struct bpf_map *bpf_object__next_map(const struct bpf_object *obj,+ const struct bpf_map *prev)+{+ __u32 idx = prev ? prev->idx + 1 : 0;++ return idx < ARRAY_SIZE(obj->maps) ?+ (struct bpf_map *)&obj->maps[idx] : NULL;+}++const char *bpf_map__name(const struct bpf_map *map)+{+ return map->name;+}++enum bpf_map_type bpf_map__type(const struct bpf_map *map)+{+ return map->type;+}++__u32 bpf_map__key_size(const struct bpf_map *map)+{+ return map->key_size;+}++__u32 bpf_map__value_size(const struct bpf_map *map)+{+ return map->value_size;+}++__u32 bpf_map__max_entries(const struct bpf_map *map)+{+ return map->max_entries;+}++void pr_err(const char *fmt, ...)+{+}++char *strerror(int err)+{+ return "error";+}++static const struct shape *uprobe_shape(__u32 nth)+{+ __u32 i;++ for (i = 0; i < NR_PROGS; i++)+ if (kind_has_uprobe_target(objects[LEN].progs[i].kind) &&+ !nth--)+ return &objects[LEN].progs[i];+ return NULL;+}++/*+ * What util/elf_target.c gives: an errno, or a build ID of 1 to 64 bytes,+ * here of the length the shape names, with one file offset for a function+ * and strictly rising ones for a glob, here NR_OFFSETS of them.+ */+static int resolve(struct elf_target *out, __u32 nr_offsets)+{+ const struct shape *shape = uprobe_shape(nr_resolved);+ __u8 build_id[UPATCH_BUILD_ID_MAX];+ __u64 offsets[NR_OFFSETS];+ __u32 i;+ int err;++ __CPROVER_assert(shape, "resolved for a uprobe only");+ memset(out, 0, sizeof(*out));+ if (shape->no_binary) {+ __CPROVER_assume(err < 0 && err >= -4095);+ return err;+ }+ out->offsets = xcalloc(nr_offsets, sizeof(*out->offsets));+ if (!out->offsets)+ return -ENOMEM;+ for (i = 0; i < nr_offsets; i++) {+ __CPROVER_assume(!i || offsets[i - 1] < offsets[i]);+ out->offsets[i] = offsets[i];+ resolved_offsets[nr_resolved][i] = offsets[i];+ }+ out->offsets_cap = nr_offsets;+ out->nr_offsets = nr_offsets;+ for (i = 0; i < shape->build_id_len; i++)+ out->build_id[i] = build_id[i];+ out->build_id_len = shape->build_id_len;+ resolved[nr_resolved++] = *out;+ return 0;+}++int elf_target_resolve(const char *path, const char *pattern,+ struct elf_target *out)+{+ if (!path || path[0] != '/' || !pattern || !pattern[0] || !out)+ return -EINVAL;+ return resolve(out, NR_OFFSETS);+}++int elf_target_resolve_func(const char *path, const char *func, __u64 offset,+ struct elf_target *out)+{+ if (!path || path[0] != '/' || (func && !func[0]) || !out)+ return -EINVAL;+ return resolve(out, 1);+}++void elf_target_free(struct elf_target *target)+{+ xfree(target->offsets,+ (size_t)target->offsets_cap * sizeof(*target->offsets));+ memset(target, 0, sizeof(*target));+}++/* prefix and then, where it goes there, any TAIL bytes and a NUL. */+static void text(char *dst, const char *prefix, bool tail)+{+ size_t n = strlen(prefix), i;+ char behind[TAIL];++ for (i = 0; i < n; i++)+ dst[i] = prefix[i];+ for (i = 0; tail && i < TAIL; i++)+ dst[n++] = behind[i];+ dst[n] = '\0';+}++/* The table of the format: a header, the entries, the uprobe targets. */+static __u32 table_len(void)+{+ __u32 len = 16 + 160 * NR_PROGS, nr_uprobes = 0, i;++ for (i = 0; i < NR_PROGS; i++) {+ const struct shape *shape = &objects[LEN].progs[i];++ if (!kind_has_uprobe_target(shape->kind))+ continue;+ len += 16 + strlen(UPROBE_PATH) + shape->build_id_len ++ 8 * (kind_is_uprobe_multi(shape->kind) ? NR_OFFSETS : 1);+ nr_uprobes++;+ }+ return len + (nr_uprobes ? 16 : 0);+}++static void uprobe_target(const struct prog_spec *s,+ const struct shape *shape, __u32 nth)+{+ __u32 i;++ __CPROVER_assert(s->uprobe_path_len == strlen(UPROBE_PATH) &&+ !strcmp(s->uprobe_path, UPROBE_PATH),+ "the path the section names");+ __CPROVER_assert(s->build_id_len == shape->build_id_len &&+ !memcmp(s->build_id, resolved[nth].build_id,+ UPATCH_BUILD_ID_MAX),+ "the build ID of the binary");+ __CPROVER_assert(s->nr_uprobe_offsets == resolved[nth].nr_offsets,+ "as many offsets as resolved");+ for (i = 0; i < s->nr_uprobe_offsets; i++)+ __CPROVER_assert(s->uprobe_offsets[i] ==+ resolved_offsets[nth][i],+ "the offsets resolved");+ __CPROVER_assert(kind_is_uprobe_multi(s->kind) ||+ s->uprobe_offsets[0] <= UINT32_MAX,+ "a function below 4 GiB");+}++static __u32 decimal(const char *digits)+{+ __u32 value = 0;++ for (; *digits; digits++)+ value = value * 10 + (__u32)(*digits - '0');+ return value;+}++static void read_back(const struct prog_spec *s, const char *sec,+ const char *name, const struct shape *shape,+ __u32 nth_uprobe)+{+ __CPROVER_assert(s->role == shape->role && s->kind == shape->kind,+ "the role and kind of the section");+ if (s->role == UPATCH_PROG_ROLE_SERIALIZER)+ __CPROVER_assert(!strcmp(s->map, name ++ strlen(SERIALIZER_PREFIX)),+ "the map the serializer names");+ else if (s->kind == UPATCH_ATTACH_XDP)+ __CPROVER_assert(s->device_classes ==+ decimal(sec + strlen("xdp/")),+ "the device classes the section names");+ else+ __CPROVER_assert(!strcmp(s->target, shape->target) &&+ s->offset == shape->offset,+ "the target and offset the section names");+ if (kind_has_uprobe_target(s->kind))+ uprobe_target(s, shape, nth_uprobe);+}++void harness(void)+{+ __u32 nr_maps, len = 0xdeadbeef, classes = 0, nr_uprobes = 0, i;+ const struct shape *shape = objects[LEN].progs;+ struct prog_spec *specs;+ __u32 verdict = TAKEN;+ struct bpf_object obj;+ void *md = (void *)1;+ bool has_output;+ int ret;++ for (i = 0; i < NR_PROGS; i++) {+ obj.progs[i].idx = i;+ text(obj.progs[i].sec, shape[i].sec,+ shape[i].tail == TAIL_SEC);+ text(obj.progs[i].name, shape[i].name,+ shape[i].tail == TAIL_NAME);+ verdict = max(verdict, shape[i].verdict);+ }+ for (i = 0; i < ARRAY_SIZE(obj.maps); i++) {+ obj.maps[i].idx = i;+ __CPROVER_assume(!obj.maps[i].name[UPATCH_PROG_MAP_MAX]);+ }+ text(obj.maps[2].name, SERIAL_MAP_NAME, false);+ if (!has_output)+ obj.maps[2].name[0] = '\0';+ ret = build_prog_metadata(&obj, nr_maps, NR_PROGS, &md, &len);+ __CPROVER_assert(verdict != TAKEN || !ret, "taken");+ __CPROVER_assert(verdict != REFUSED || ret, "refused");+ if (ret) {+ __CPROVER_assert(ret < 0 && !md && !len,+ "an error and no metadata");+ return;+ }+ __CPROVER_assert(md && len == table_len(),+ "a table of the length the format gives");+ /* As just shown, so that CBMC takes both for the constants they are. */+ __CPROVER_assume(md && len == table_len());+ ret = prog_specs_parse(md, len, nr_maps, NR_PROGS, &specs);+ __CPROVER_assert(!ret, "the reader takes what the writer wrote");+ if (ret)+ return;+ for (i = 0; i < NR_PROGS; i++) {+ read_back(&specs[i], obj.progs[i].sec, obj.progs[i].name,+ &shape[i], nr_uprobes);+ nr_uprobes += kind_has_uprobe_target(shape[i].kind);+ __CPROVER_assert(!(classes & specs[i].device_classes),+ "no device class for two programs");+ classes |= specs[i].device_classes;+ }+ prog_specs_free(specs, NR_PROGS);+ xfree(md, len);+}diff --git a/testing/proofs/cbmc/upatch-writer-sections.c b/testing/proofs/cbmc/upatch-writer-sections.cnew file mode 100644index 00000000..2a5e5af2--- /dev/null+++ b/testing/proofs/cbmc/upatch-writer-sections.c@@ -0,0 +1,157 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The signed sections compose_signed_data() of package.c lays behind+ * loader data of LEN bytes, for the name with an unload digest or with+ * attach metadata of 1 to ATTACH_MAX bytes: parse_signed_sections() of+ * util/upatch.c finds each where it was written, aligned, with its bytes+ * and length, the loader's data in front and zeros between.+ */++#define PROOF_MEMCHR_QUANTIFIED+/* As proof.h, before the system headers can include <ctype.h>. */+#define __NO_CTYPE 1++#include <openssl/evp.h>+#include <linux/bpf.h>+#include <bpf/libbpf.h>++#include "proof.h"+#include "../../../util/malloc.c"+#include "../../../util/string.c"+#include "../../../util/upatch.c"+#include "../../../tools/shirudo-build/package.c"++#define ATTACH_MAX 8++static struct signed_section sections[UPATCH_SIGNED_SECTIONS_MAX];+static __u32 nr_sections;+static const char *name;++static void add_section(__u32 tag, const void *data, __u32 len)+{+ sections[nr_sections++] = (struct signed_section){ tag, data, len };+}++static __u32 offset(const struct upatch *u, const void *section)+{+ return (__u32)((const __u8 *)section - u->map);+}++/* Whether the reader kept the len bytes at p, aligned behind the data. */+static bool read_back(const struct upatch *u, const void *got, const void *p,+ __u32 len)+{+ const __u8 *at = got, *bytes = p;+ __u32 i;++ if (!at || offset(u, at) & 7 || offset(u, at) < LEN)+ return false;+ __CPROVER_assume(i < len);+ return at[i] == bytes[i];+}++static bool inside(const struct upatch *u, const void *section, __u32 len,+ __u32 at)+{+ return section && at >= offset(u, section) &&+ at - offset(u, section) < len;+}++/* The name and one more section of len bytes, which the reader kept at got. */+static void composed(const __u8 *data, struct upatch *u, const void **got,+ __u32 len)+{+ __u32 out_len = 0xdeadbeef, dir_off, i, j;+ void *out = (void *)1;++ __CPROVER_assert(!compose_signed_data(data, LEN, sections, nr_sections,+ &out, &out_len), "composed");+ u->map = out;+ u->map_size = out_len;+ u->hdr.data_len = out_len;+ parse_signed_sections(u);+ __CPROVER_assert(read_back(u, u->signed_name, name, UPATCH_NAME_MAX),+ "the name where the reader looks");+ if (LEN) {+ __CPROVER_assume(i < LEN);+ __CPROVER_assert(u->map[i] == data[i],+ "the loader's data in front");+ }+ dir_off = get_le32(u->map + out_len - 8);+ __CPROVER_assert(!(dir_off & 7), "an aligned directory");+ __CPROVER_assume(j < dir_off);+ __CPROVER_assert(j < LEN || !u->map[j] ||+ inside(u, u->signed_name, UPATCH_NAME_MAX, j) ||+ inside(u, *got, len, j),+ "zeros between the data and the sections");+ __CPROVER_assert(!u->decoder_blob && !u->signed_policy_labels,+ "no section that was not written");+}++static void unload_directive(const __u8 *data)+{+ __u8 digest[UPATCH_PAYLOAD_HASH_LEN];+ struct upatch u = {};++ add_section(UPATCH_SECTION_UNLOAD, digest, sizeof(digest));+ composed(data, &u, (const void **)&u.signed_unload_payload,+ sizeof(digest));+ __CPROVER_assert(read_back(&u, u.signed_unload_payload, digest,+ sizeof(digest)),+ "the digest where the reader looks");+ __CPROVER_assert(!u.signed_attach, "no attach metadata");+ xfree((void *)u.map, u.map_size);+}++static void load(const __u8 *data, __u32 attach_len)+{+ const __u8 *attach = any_bytes(attach_len);+ struct upatch u = {};++ add_section(UPATCH_SECTION_ATTACH, attach, attach_len);+ composed(data, &u, (const void **)&u.signed_attach, attach_len);+ __CPROVER_assert(u.signed_attach_len == attach_len &&+ read_back(&u, u.signed_attach, attach, attach_len),+ "the attach metadata where the reader looks");+ __CPROVER_assert(!u.signed_unload_payload, "no unload digest");+ xfree((void *)u.map, u.map_size);+}++/* A count the reader refuses, and a length that passes what a field holds. */+static void refused(const __u8 *data)+{+ struct signed_section big[2] = { { .len = UINT32_MAX } };+ __u32 nr, len = 0xdeadbeef;+ void *out = (void *)1;++ __CPROVER_assume(!nr || nr > UPATCH_SIGNED_SECTIONS_MAX);+ __CPROVER_assert(compose_signed_data(data, LEN, sections, nr, &out,+ &len) == -EINVAL && !out && !len,+ "no directory of a size the reader refuses");+ out = (void *)1;+ len = 0xdeadbeef;+ __CPROVER_assert(compose_signed_data(data, LEN, big, 2, &out,+ &len) == -EOVERFLOW && !out && !len,+ "no data past what a length field holds");+}++#ifndef LOAD_SECTIONS+void harness(void)+{+ const __u8 *data = any_bytes(LEN);+ __u32 attach_len, k;++ /* finalize_object() signs a name its strscpy() fit into 256 bytes. */+ name = any_bytes(UPATCH_NAME_MAX);+ __CPROVER_assume(k < UPATCH_NAME_MAX && !name[k]);+ add_section(UPATCH_SECTION_NAME, name, UPATCH_NAME_MAX);+ unload_directive(data);+ for (attach_len = 1; attach_len <= ATTACH_MAX; attach_len++) {+ nr_sections = 1;+ load(data, attach_len);+ }+ refused(data);+}+#endifdiff --git a/testing/proofs/cbmc/upatch-writer-uprobes.c b/testing/proofs/cbmc/upatch-writer-uprobes.cnew file mode 100644index 00000000..6bf7c58c--- /dev/null+++ b/testing/proofs/cbmc/upatch-writer-uprobes.c@@ -0,0 +1,12 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The attach metadata proof for uprobe sections, with any build ID and+ * offsets the ELF resolver finds, or none: the reader takes the target+ * block the writer appends, and reads back each record's path, build ID+ * and offsets for the program the record names.+ */++#define UPROBE_OBJECTS+#include "upatch-writer-prog-specs.c"diff --git a/testing/proofs/cbmc/upatch-writer-xdp.c b/testing/proofs/cbmc/upatch-writer-xdp.cnew file mode 100644index 00000000..7ae0f37e--- /dev/null+++ b/testing/proofs/cbmc/upatch-writer-xdp.c@@ -0,0 +1,11 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The attach metadata proof for XDP sections, a first digit and any TAIL+ * bytes behind it, one program or two: the writer takes a mask only of the+ * device classes the reader knows, and no class for two programs.+ */++#define XDP_OBJECTS+#include "upatch-writer-prog-specs.c"diff --git a/testing/proofs/cbmc/vouch-payload.c b/testing/proofs/cbmc/vouch-payload.cnew file mode 100644index 00000000..60efeb97--- /dev/null+++ b/testing/proofs/cbmc/vouch-payload.c@@ -0,0 +1,112 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The bytes a vouch signs, for certificates of LEN bytes and a set of two+ * members, with OpenSSL's check a stub that takes the signed bytes from+ * their signer alone: `shirudo-build keys vouch` signs the prefix, its NUL+ * and the certificate, and keys_verify_vouch() of util/keys.c admits that+ * certificate under that member, and no other or longer one.+ */++/* As proof.h, before the system headers can include <ctype.h>. */+#define __NO_CTYPE 1++#include <curl/curl.h>++#include "proof.h"+#include "../../../util/malloc.c"+#include "../../../util/keys.c"++#define NR_MEMBERS 2++static const __u8 *vouched;+static size_t vouched_len;+static __u8 signer;++/* A member's certificate is one byte here, its place in the set. */+int cert_read_der(const __u8 *der, size_t len, struct cert *cert)+{+ cert->impl = (void *)(size_t)(der[0] + 1);+ return 0;+}++void cert_free(struct cert *cert)+{+}++int verify_detached(const __u8 *payload, size_t payload_len,+ const __u8 *signature, size_t signature_len,+ const struct cert *cert)+{+ return cert->impl == (void *)(size_t)(signer + 1) &&+ payload_len == vouched_len &&+ !memcmp(payload, vouched, payload_len) ? 0 : -EKEYREJECTED;+}++/* As build_keys_vouch() of tools/shirudo-build/vouch.c has OpenSSL sign. */+static bool vouch(const __u8 *der)+{+ static const char prefix[] = KEYS_VOUCH_PREFIX;+ __u8 *payload = NULL;+ size_t size = 0, i;+ int ret;++ ret = keys_vouch_payload(der, LEN, &payload, &size);+ if (ret) {+ __CPROVER_assert(ret == -ENOMEM && !payload && !size,+ "only memory fails, and leaves nothing");+ return false;+ }+ __CPROVER_assert(size == sizeof(prefix) + LEN,+ "a payload as long as the prefix and the certificate");+ __CPROVER_assume(i < size);+ __CPROVER_assert(payload[i] == (i < sizeof(prefix) ? (__u8)prefix[i] :+ der[i - sizeof(prefix)]),+ "the prefix, its NUL and then the certificate");+ vouched = payload;+ vouched_len = size;+ return true;+}++static bool members(struct trusted_set *set)+{+ __u8 i;++ for (i = 0; i < NR_MEMBERS; i++) {+ set->certs[i].sha256[0] = i;+ set->certs[i].fingerprint[0] = i;+ if (der_keep(set->certs[i].sha256, &i, sizeof(i)))+ return false;+ }+ set->n = NR_MEMBERS;+ return true;+}++void harness(void)+{+ const __u8 *der = any_bytes(LEN), *other = any_bytes(LEN + 1);+ struct trusted_set set = {};+ __u8 voucher[32], sig[1];+ bool same = true;+ size_t i;+ int ret;++ __CPROVER_assume(signer < NR_MEMBERS);+ if (!members(&set) || !vouch(der))+ return;+ ret = keys_verify_vouch(&set, der, LEN, sig, sizeof(sig), voucher);+ __CPROVER_assert(ret == -ENOMEM ||+ (!ret && voucher[0] == signer),+ "the vouch admits its certificate under its signer");+ for (i = 0; i < LEN; i++)+ same &= other[i] == der[i];+ ret = keys_verify_vouch(&set, other, LEN, sig, sizeof(sig), voucher);+ __CPROVER_assert(ret == -ENOMEM ||+ ret == (same ? 0 : -EKEYREJECTED),+ "and no other certificate of its length");+ ret = keys_verify_vouch(&set, other, LEN + 1, sig, sizeof(sig),+ voucher);+ __CPROVER_assert(ret == -ENOMEM || ret == -EKEYREJECTED,+ "nor a longer one");+}