eBPF-based security platform
upatch: bind the map count in the signed program metadata
Daniel Borkmann64c4057976af
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex 5214aaa0..23043ce3 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -299,7 +299,7 @@ signed config image.- ``attach_scope_mismatch`` in a foreign network or cgroupnamespace, and through a substituted PMU* - ``t6117-signed-metadata``- - Edited attach metadata, name and unload target+ - Edited attach metadata, name, map count and unload target* - ``t6118-foreign-entries``- Entries root brings into sealed directories* - ``t6119-perf-attach``@@ -436,12 +436,12 @@ signed config image.Unit suites and fuzz targets~~~~~~~~~~~~~~~~~~~~~~~~~~~~-The unit suites in :src:`testing/unit/` cover seven of the modules in+The unit suites in :src:`testing/unit/` cover eight of the modules in:src:`util/`: ``chain``, ``hex``, ``json``, ``kernel_abi``,-``kernel_build_id``, ``kernel_release`` and ``string``, and ``overflow``-the arithmetic of :src:`include/shirudo/overflow.h`. The other modules-are reached through the test helpers, the fuzz targets and the binaries-the functional tests run.+``kernel_build_id``, ``kernel_release``, ``string`` and ``upatch``, and+``overflow`` the arithmetic of :src:`include/shirudo/overflow.h`. The+other modules are reached through the test helpers, the fuzz targets and+the binaries the functional tests run... list-table:::header-rows: 1diff --git a/Documentation/reference/formats/upatch-format.rst b/Documentation/reference/formats/upatch-format.rstindex 5f462350..c5ba8838 100644--- a/Documentation/reference/formats/upatch-format.rst+++ b/Documentation/reference/formats/upatch-format.rst@@ -328,8 +328,8 @@ descriptors.- Number of program entries. Equal to the header ``nr_progs``.* - +12- 4- - ``reserved``- - 0.+ - ``nr_maps``+ - Number of maps in the BPF object. Equal to the header ``nr_maps``.Each program entry is 160 bytes:@@ -603,9 +603,10 @@ signed bytes and trusts others as they are.* - ``nr_progs``- Must equal the ``nr_progs`` of the signed ``UPGM`` section.* - ``nr_maps``- - Not checked against signed content. The target uses it to find the- program descriptors behind the map descriptors in the loader- context, and to pin and close the map descriptors.+ - Must equal the ``nr_maps`` of the signed ``UPGM`` section. The+ target uses it to find the program descriptors behind the map+ descriptors in the loader context, and to pin and close the map+ descriptors.* - ``rodata_data_off``- Bounds-checked only: the ``.rodata`` size from the signed BTF mustfit between it and the end of ``data``. ``bpm info data`` reads@@ -675,7 +676,7 @@ of checks 1 to 6 is reported as ``parser_rejected``, see#. A ``NAME`` section is present and equals the header ``name``.#. ``payload_sha256`` equals SHA-256 of ``loader_insns || data``.#. A ``UPGM`` section is present and parses: version 1 (any other version- is ``EOPNOTSUPP``), ``reserved`` 0, ``nr_progs`` equal to the header's,+ is ``EOPNOTSUPP``), ``nr_maps`` and ``nr_progs`` equal to the header's,every entry valid for its role and kind, and a ``UPUT`` block that ispresent exactly when a kind 7 entry exists and ends the section.#. Each ``uprobe.multi`` target file is a regular file whose GNU build ID@@ -725,7 +726,7 @@ A section tag the reader does not know makes it treat the directory asabsent. A new section therefore needs readers that know its tag beforewriters emit it. Reserved fields are 0 in every file a writer emits. Thereader checks ``reserved1``, the directory entries' ``reserved`` and the-``UPGM`` and ``UPUT`` ``reserved`` fields. It does not check ``flags``+``UPUT`` ``reserved`` field. It does not check ``flags``bits 1 to 31 or the ``DCDB`` ``reserved`` field.Limitsdiff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rstindex 69ec450a..a2fa1120 100644--- a/Documentation/security/threat-model.rst+++ b/Documentation/security/threat-model.rst@@ -762,7 +762,9 @@ SHA-256 of the loader's instructions. The name, the attach metadata, andfor an unload directive the target name and the expected payload digestride in signed sections of that data. A load takes its attach sitesfrom nowhere else, and refuses an artifact whose attach metadata the-signature does not cover (``parser_rejected``).+signature does not cover or whose header program and map counts, which+place each program's descriptor in the loader context, differ from the+signed ones (``parser_rejected``).An unload directive is a signed loader of its own and goes through thesame ``BPF_PROG_LOAD``. Before it removes anything, shirudo compares thediff --git a/Documentation/upatches/learn/concepts.rst b/Documentation/upatches/learn/concepts.rstindex fc5e19d2..690e5e9f 100644--- a/Documentation/upatches/learn/concepts.rst+++ b/Documentation/upatches/learn/concepts.rst@@ -106,9 +106,11 @@ where the loader never reads them but the signature covers them:The upatch name. The target refuses a file whose header name differsfrom the signed one.``UPGM``- The attach metadata of a load: for each program its role, its attach- kind and, where the kind needs one, the target symbol, offset or- uprobe binary. The builder derives it from the section names.+ The attach metadata of a load: the number of programs and maps and,+ for each program, its role, its attach kind and, where the kind needs+ one, the target symbol, offset or uprobe binary. The builder derives+ it from the section names. The target refuses a file whose header+ counts differ from the signed ones.``UNLD``The payload digest an unload directive expects to find on the upatchit removes.diff --git a/builtin-upatch.c b/builtin-upatch.cindex e84c656f..49d7ff9d 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -1822,8 +1822,8 @@ static int __upatch_apply(const char *path, bool verify_only,ret = 1;goto out;}- ret = prog_specs_parse(attach, attach_len, u.hdr.nr_progs,- &prog_specs);+ ret = prog_specs_parse(attach, attach_len, u.hdr.nr_maps,+ u.hdr.nr_progs, &prog_specs);if (ret) {pr_err("upatch program metadata rejected: %s",strerror(-ret));diff --git a/include/shirudo/upatch.h b/include/shirudo/upatch.hindex a3537ae3..d897bb0d 100644--- a/include/shirudo/upatch.h+++ b/include/shirudo/upatch.h@@ -195,7 +195,7 @@ struct upatch_prog_metadata {__le32 magic; /* UPATCH_PROG_MAGIC */__le32 version; /* UPATCH_PROG_VERSION */__le32 nr_progs;- __le32 reserved;+ __le32 nr_maps;/** Then: nr_progs struct upatch_prog_entry entries in* libbpf/gen_loader program iteration order.diff --git a/testing/functional/tamperproof/t6117-signed-metadata.sh b/testing/functional/tamperproof/t6117-signed-metadata.shindex e94318ee..435bb0d5 100755--- a/testing/functional/tamperproof/t6117-signed-metadata.sh+++ b/testing/functional/tamperproof/t6117-signed-metadata.sh@@ -27,9 +27,9 @@ trap 'rm -rf "$WORK"' EXITrequire_loaded shirudo-test-basic || test_skip_all "cannot load the basic fixture"[ ! -e "$B/shirudo-test-scope-kprobe" ] || test_skip_all "scope kprobe fixture is loaded"-# Rewrite the header name, or a string inside one signed section named-# by its tag, keeping payload_sha256 consistent so only the signature or-# the signed name can refuse the result.+# Rewrite the header name or map count, or a string inside one signed+# section named by its tag, keeping payload_sha256 consistent so only the+# signature or the signed sections can refuse the result.mutate() {python3 - "$@" <<'PY'import hashlib, pathlib, struct, sys@@ -40,6 +40,8 @@ data_off, data_len = struct.unpack_from("<II", data, 328)if what == "header-name":name = sys.argv[4].encode()data[16:272] = name + bytes(256 - len(name))+elif what == "header-maps":+ struct.pack_into("<I", data, 340, struct.unpack_from("<I", data, 340)[0] + 1)elif what == "section":tag, (old, new) = sys.argv[4].encode(), sys.argv[5].split("=")magic, version, dir_off, nr = struct.unpack_from(@@ -79,6 +81,12 @@ test_expect_success 'a load whose header name differs from the signed name is re[ ! -e "$B/shirudo-test-scope-kprobe" ]'+test_expect_success 'a load whose header map count differs from the signed count is refused' '+ mutate "$KP" "$WORK/remapped.upatch" header-maps &&+ [ "$(category upatch load "$WORK/remapped.upatch")" = parser_rejected ] &&+ [ ! -e "$B/shirudo-test-scope-kprobe" ]+'+test_expect_success 'an unload directive cannot be pointed at another upatch' 'mutate "$KP_UNLOAD" "$WORK/repointed.unload.upatch" header-name shirudo-test-basic &&[ "$(category upatch unload "$WORK/repointed.unload.upatch")" = parser_rejected ] &&diff --git a/testing/fuzz/fuzz-prog-metadata.c b/testing/fuzz/fuzz-prog-metadata.cindex 0a8aad9c..d5bc1b0a 100644--- a/testing/fuzz/fuzz-prog-metadata.c+++ b/testing/fuzz/fuzz-prog-metadata.c@@ -16,16 +16,17 @@ int LLVMFuzzerTestOneInput(const __u8 *data, size_t size){struct upatch_prog_metadata md;struct prog_spec *specs;- __u32 nr_progs = 0;+ __u32 nr_maps = 0, nr_progs = 0;if (size > UINT32_MAX)return 0;- /* The header claims the count the container's header must match. */+ /* The header claims the counts the container's header must match. */if (size >= sizeof(md)) {memcpy(&md, data, sizeof(md));+ nr_maps = le32_to_cpu(md.nr_maps);nr_progs = le32_to_cpu(md.nr_progs);}- if (!prog_specs_parse(data, (__u32)size, nr_progs, &specs))+ if (!prog_specs_parse(data, (__u32)size, nr_maps, nr_progs, &specs))prog_specs_free(specs, nr_progs);return 0;}diff --git a/testing/fuzz/fuzz-upatch.c b/testing/fuzz/fuzz-upatch.cindex ae024b1e..0fe28c51 100644--- a/testing/fuzz/fuzz-upatch.c+++ b/testing/fuzz/fuzz-upatch.c@@ -23,8 +23,8 @@ int LLVMFuzzerTestOneInput(const __u8 *data, size_t size)if (!upatch_open(path, &u)) {attach = upatch_signed_attach(&u, &attach_len);if (attach &&- !prog_specs_parse(attach, attach_len, u.hdr.nr_progs,- &specs))+ !prog_specs_parse(attach, attach_len, u.hdr.nr_maps,+ u.hdr.nr_progs, &specs))prog_specs_free(specs, u.hdr.nr_progs);upatch_signed_name(&u);upatch_signed_unload_payload(&u);diff --git a/testing/helper/test-upatch-prog-metadata.c b/testing/helper/test-upatch-prog-metadata.cindex b313e97d..f6aa00b9 100644--- a/testing/helper/test-upatch-prog-metadata.c+++ b/testing/helper/test-upatch-prog-metadata.c@@ -81,7 +81,7 @@ static int expect_serializer(const struct upatch *u)memcpy(progs, raw + sizeof(md), sizeof(progs));if (le32_to_cpu(md.magic) != UPATCH_PROG_MAGIC ||le32_to_cpu(md.version) != UPATCH_PROG_VERSION ||- le32_to_cpu(md.nr_progs) != 2)+ le32_to_cpu(md.nr_progs) != 2 || le32_to_cpu(md.nr_maps) != 4)return fail("unexpected metadata header");for (i = 0; i < 2; i++) {if (le32_to_cpu(progs[i].role) != UPATCH_PROG_ROLE_SERIALIZER ||diff --git a/testing/unit/upatch.c b/testing/unit/upatch.cnew file mode 100644index 00000000..5b58ac4d--- /dev/null+++ b/testing/unit/upatch.c@@ -0,0 +1,61 @@+// SPDX-License-Identifier: BSD-2-Clause+/* Copyright (c) 2026 Authors of shirudo */++#include <errno.h>++#include "unit.h"+#include "upatch.h"++#define ONE_PROG_LEN \+ (sizeof(struct upatch_prog_metadata) + sizeof(struct upatch_prog_entry))++static void one_raw_tracepoint(__u8 *buf, __u32 nr_maps)+{+ struct upatch_prog_metadata md = {};+ struct upatch_prog_entry prog = {};++ md.magic = cpu_to_le32(UPATCH_PROG_MAGIC);+ md.version = cpu_to_le32(UPATCH_PROG_VERSION);+ md.nr_progs = cpu_to_le32(1);+ md.nr_maps = cpu_to_le32(nr_maps);+ prog.role = cpu_to_le32(UPATCH_PROG_ROLE_ATTACH);+ prog.kind = cpu_to_le32(UPATCH_ATTACH_RAW_TRACEPOINT);+ memcpy(buf, &md, sizeof(md));+ memcpy(buf + sizeof(md), &prog, sizeof(prog));+}++static void prog_specs_take_the_signed_counts(void)+{+ struct prog_spec *specs = NULL;+ __u8 buf[ONE_PROG_LEN];++ one_raw_tracepoint(buf, 3);+ ASSERT_EQ(prog_specs_parse(buf, sizeof(buf), 3, 1, &specs), 0);+ ASSERT_TRUE(specs);+ EXPECT_UEQ(specs[0].role, UPATCH_PROG_ROLE_ATTACH);+ EXPECT_UEQ(specs[0].kind, UPATCH_ATTACH_RAW_TRACEPOINT);+ EXPECT_EQ(specs[0].target_fd, -1);+ prog_specs_free(specs, 1);+}++static void prog_specs_refuse_another_header_count(void)+{+ struct prog_spec *specs = NULL;+ __u8 buf[ONE_PROG_LEN];++ one_raw_tracepoint(buf, 3);+ EXPECT_EQ(prog_specs_parse(buf, sizeof(buf), 4, 1, &specs), -EINVAL);+ EXPECT_FALSE(specs);+ EXPECT_EQ(prog_specs_parse(buf, sizeof(buf), 2, 1, &specs), -EINVAL);+ EXPECT_EQ(prog_specs_parse(buf, sizeof(buf), 3, 2, &specs), -EINVAL);+ EXPECT_EQ(prog_specs_parse(buf, sizeof(buf), 3, 0, &specs), -EINVAL);+ EXPECT_FALSE(specs);+}++static const struct unit_case cases[] = {+ UNIT_CASE(prog_specs_take_the_signed_counts),+ UNIT_CASE(prog_specs_refuse_another_header_count),+ {}+};++UNIT_SUITE(upatch, cases);diff --git a/tools/shirudo-build/package.c b/tools/shirudo-build/package.cindex 7388ae55..6c0d0d19 100644--- a/tools/shirudo-build/package.c+++ b/tools/shirudo-build/package.c@@ -1140,8 +1140,8 @@ static int validate_serializer_output_map(struct bpf_object *obj)return -EINVAL;}-static int build_prog_metadata(struct bpf_object *obj, __u32 nr_progs,- void **out, __u32 *out_len)+static int build_prog_metadata(struct bpf_object *obj, __u32 nr_maps,+ __u32 nr_progs, void **out, __u32 *out_len){struct build_uprobe_target {__u32 prog_idx;@@ -1183,6 +1183,7 @@ static int build_prog_metadata(struct bpf_object *obj, __u32 nr_progs,md->magic = cpu_to_le32(UPATCH_PROG_MAGIC);md->version = cpu_to_le32(UPATCH_PROG_VERSION);md->nr_progs = cpu_to_le32(nr_progs);+ md->nr_maps = cpu_to_le32(nr_maps);seen_maps = xcalloc(nr_progs, sizeof(*seen_maps));if (!seen_maps) {err = -ENOMEM;@@ -1567,7 +1568,7 @@ static int finalize_object(struct bpf_object *obj, const char *out_path,if (err)goto out;- err = build_prog_metadata(obj, nr_progs,+ err = build_prog_metadata(obj, nr_maps, nr_progs,&attach_meta, &attach_meta_len);if (err)goto out;diff --git a/util/upatch.c b/util/upatch.cindex b47d95db..5bc64cab 100644--- a/util/upatch.c+++ b/util/upatch.c@@ -476,7 +476,7 @@ void prog_specs_free(struct prog_spec *specs, __u32 nr_progs)}int prog_specs_parse(const void *metadata, __u32 metadata_len,- __u32 nr_progs, struct prog_spec **out)+ __u32 nr_maps, __u32 nr_progs, struct prog_spec **out){struct upatch_prog_metadata md;struct prog_spec *specs = NULL;@@ -498,7 +498,7 @@ int prog_specs_parse(const void *metadata, __u32 metadata_len,count = le32_to_cpu(md.nr_progs);if (version != UPATCH_PROG_VERSION)return -EOPNOTSUPP;- if (count != nr_progs || le32_to_cpu(md.reserved) != 0)+ if (count != nr_progs || le32_to_cpu(md.nr_maps) != nr_maps)return -EINVAL;base_len = struct_size(&md, progs, count);if (base_len > UINT32_MAX)diff --git a/util/upatch.h b/util/upatch.hindex 7e13ec6e..4dae49a7 100644--- a/util/upatch.h+++ b/util/upatch.h@@ -138,7 +138,7 @@ struct prog_spec {* per program, target_fd -1. 0 or -errno; free with prog_specs_free().*/int prog_specs_parse(const void *metadata, __u32 metadata_len,- __u32 nr_progs, struct prog_spec **out);+ __u32 nr_maps, __u32 nr_progs, struct prog_spec **out);void prog_specs_free(struct prog_spec *specs, __u32 nr_progs);/*