Skip to content

eBPF-based security platform

bpf: bind policy-label upatches to task policy

John Fastabend2357b33b3dfc
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index c1b14215..d38c8f2c 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -179,7 +179,8 @@ signed config image.
- ``status --json`` with eighty loaded upatches lists all of them
* - ``t3109-policy-labels``
- A loaded upatch labels executable and ordinary files, ORing
- multiple policy bits with a preexisting xattr
+ multiple policy bits with a preexisting xattr; its signed policy
+ section binds its verdict program to the shared task policy map
``tpm`` (4)
~~~~~~~~~~~
diff --git a/Documentation/internals/signed-loader.rst b/Documentation/internals/signed-loader.rst
index 5ef12415..5fea2851 100644
--- a/Documentation/internals/signed-loader.rst
+++ b/Documentation/internals/signed-loader.rst
@@ -116,7 +116,10 @@ loader inside the kernel, which ``lsm/bpf`` sees with ``kernel`` true.
* - userspace
- ``BPF_PROG_TEST_RUN`` of the loader
- ``lsm/bpf``, runtime authority (``ProgRun``). The loader then
- runs in the kernel.
+ runs in the kernel. When the signed ``SHPL`` section is present,
+ Shirudo opens its pinned ``policy_map`` and supplies that map FD
+ at the signed descriptor index. The generated loader reuses it;
+ it neither creates nor pins a private policy map.
* - loader
- ``BPF_MAP_CREATE``, ``BPF_MAP_UPDATE_ELEM``, ``BPF_MAP_FREEZE``,
``BPF_PROG_LOAD`` through ``bpf_sys_bpf()``
diff --git a/Documentation/reference/formats/upatch-format.rst b/Documentation/reference/formats/upatch-format.rst
index 25e1c357..f197a7d1 100644
--- a/Documentation/reference/formats/upatch-format.rst
+++ b/Documentation/reference/formats/upatch-format.rst
@@ -299,7 +299,8 @@ Section tags
- ``0x4c504853``
- variable
- Signed policy labels, each naming a policy ID, one-hot bit and
- absolute file path.
+ absolute file path. Its presence opts into the shared task
+ ``policy_map`` binding.
The writer emits ``NAME``, then ``UNLD`` for an unload directive, or
``UPGM`` and optional ``DCDB`` and ``SHPL`` sections for a load artifact.
@@ -581,6 +582,47 @@ reads each source file, at most 16 MiB. A decoder whose ``language``,
values above fails the build. :doc:`/upatches/write/decoders` covers the decoder
API.
+SHPL section
+~~~~~~~~~~~~
+
+``SHPL`` starts with a 16-byte version 1 header, followed by ``count``
+fixed-size entries. ``policy_map_idx`` is the zero-based index of the
+``policy_map`` in the loader's map descriptor array. The builder accepts
+this section only when the BPF object declares a compatible task-storage
+map named ``policy_map``. At load time, Shirudo supplies the pinned shared
+task-storage map at that descriptor; the loader does not create or pin a
+private copy. Upatches that do not carry ``SHPL`` use their normal maps.
+
+.. list-table::
+ :header-rows: 1
+ :widths: 8 6 24 62
+
+ * - Offset
+ - Size
+ - Field
+ - Value
+ * - +0
+ - 4
+ - ``magic``
+ - ``0x4c504853``, the bytes ``SHPL``.
+ * - +4
+ - 4
+ - ``version``
+ - ``1``.
+ * - +8
+ - 4
+ - ``count``
+ - Number of policy/path entries; non-zero.
+ * - +12
+ - 4
+ - ``policy_map_idx``
+ - Map descriptor index, less than ``nr_maps`` and 64.
+
+Each entry is 4168 bytes: a little-endian one-hot ``u64`` policy bit,
+64 bytes of NUL-terminated policy ID, then 4096 bytes of NUL-terminated
+absolute path. The loader applies each entry by ORing the policy bit into
+the inode's existing policy xattr.
+
Header fields outside the signature
-----------------------------------
@@ -723,6 +765,9 @@ Versioning
* - ``DCDB``
- 1
- Treats the directory as absent.
+ * - ``SHPL``
+ - 1
+ - Treats the directory as absent.
* - Decoder manifest in ``.rodata``
- 1
- The writer skips the candidate.
diff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rst
index cfca5379..52283cb2 100644
--- a/Documentation/security/threat-model.rst
+++ b/Documentation/security/threat-model.rst
@@ -672,6 +672,14 @@ value. Failed exec preserves the previous value, and children inherit
the committed value. The xattr remains on the inode, and only a task
with runtime authority may set, update or remove it (``SealLabel``).
+A signed upatch with an ``SHPL`` path-to-policy section opts into the
+same task-storage map. The builder requires a compatible ``policy_map``
+declaration, and the target supplies its pinned map FD to the generated
+loader. This lets the upatch's signed BPF programs read or update the
+shared task policy values; the signer therefore controls any policy
+effects those programs implement. Upatches without ``SHPL`` receive no
+binding to this map.
+
The seal keeps a mark in inode storage on every labeled directory it
has seen since the inode came into memory. A new inode under a marked
directory receives the label inside the transaction that creates it
diff --git a/Documentation/upatches/publish/beefile.rst b/Documentation/upatches/publish/beefile.rst
index 35840456..906df401 100644
--- a/Documentation/upatches/publish/beefile.rst
+++ b/Documentation/upatches/publish/beefile.rst
@@ -208,7 +208,11 @@ has fails. Entries that share a name must name the same input.
A policy table declares a named bit and the absolute paths whose
``security.bpf.shirudo.policy`` xattrs this upatch labels. The builder
-embeds the entries in the signed upatch metadata.
+embeds the entries in the signed ``SHPL`` section. Its presence opts the
+upatch into Shirudo's shared task ``policy_map``: the generated loader
+reuses the pinned map rather than creating a private map. The BPF source
+must declare the same map ABI, normally by including
+``shirudo_policy_map.h`` after ``vmlinux.h`` and the libbpf headers.
.. code-block:: toml
@@ -222,6 +226,13 @@ other upatches extends that policy; an ID cannot change masks, and two
different IDs cannot use the same mask in one package. Paths may name
non-executable files.
+For an upatch with at least one policy table, include
+``shirudo_policy_map.h`` in its BPF source so it declares the shared map
+with the required ABI. The header is installed beside the Shirudo BPF
+headers. The signed ``SHPL`` section is what opts the upatch into this
+binding; declaring the map without policy tables leaves it as an ordinary
+upatch-owned map.
+
The generator and the updater run on the operator host with the
operator's rights. The target receives only the signed result. Without
``-f``, ``bpm config update`` takes the Beefile and the updater from the
diff --git a/Makefile.shirudo b/Makefile.shirudo
index 89773f09..4d4c0160 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -304,6 +304,7 @@ BPF_LIBBPF_HEADERS := $(addprefix $(LIBBPF_DIR)/,\
$(addsuffix .h,$(BPF_VENDOR_HEADERS)))
BPF_LIB_HEADERS := bpf/lib/misc.h \
bpf/lib/serializer.h \
+ bpf/lib/shirudo_policy_map.h \
bpf/lib/decoder.h
# bpf/*.bpf.c sources compiled with clang -target bpf and embedded via
diff --git a/bpf/lib/lsm.h b/bpf/lib/lsm.h
index d31ff980..9e816082 100644
--- a/bpf/lib/lsm.h
+++ b/bpf/lib/lsm.h
@@ -28,6 +28,7 @@
#include <shirudo/enforcement.h>
#include "identity.h"
+#include "shirudo_policy_map.h"
#include "tamper.h"
#define TASK_TRUSTED 1
@@ -48,18 +49,6 @@ struct {
__type(value, struct task_authority);
} shirudo_tasks SEC(".maps");
-struct shirudo_task_policy {
- __u64 active;
- __u64 candidate;
-};
-
-struct {
- __uint(type, BPF_MAP_TYPE_TASK_STORAGE);
- __uint(map_flags, BPF_F_NO_PREALLOC);
- __type(key, int);
- __type(value, struct shirudo_task_policy);
-} policy_map SEC(".maps");
-
struct {
__uint(type, BPF_MAP_TYPE_SK_STORAGE);
__uint(map_flags, BPF_F_NO_PREALLOC | BPF_F_CLONE);
diff --git a/bpf/lib/shirudo_policy_map.h b/bpf/lib/shirudo_policy_map.h
new file mode 100644
index 00000000..7de9dd09
--- /dev/null
+++ b/bpf/lib/shirudo_policy_map.h
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
+#ifndef SHIRUDO_POLICY_MAP_H
+#define SHIRUDO_POLICY_MAP_H
+
+struct shirudo_task_policy {
+ __u64 active;
+ __u64 candidate;
+};
+
+struct {
+ __uint(type, BPF_MAP_TYPE_TASK_STORAGE);
+ __uint(map_flags, BPF_F_NO_PREALLOC);
+ __type(key, int);
+ __type(value, struct shirudo_task_policy);
+} policy_map SEC(".maps");
+
+#endif
diff --git a/builtin-upatch.c b/builtin-upatch.c
index c12caf45..4d75eed0 100644
--- a/builtin-upatch.c
+++ b/builtin-upatch.c
@@ -1517,7 +1517,8 @@ out:
* in path components, so libbpf's .bss, .data and .rodata are skipped.
*/
static int pin_upatch_maps(const struct loader_ctx *cx,
- __u32 nr_maps, const char *name)
+ __u32 nr_maps, __u32 shared_policy_map_idx,
+ const char *name)
{
char dir[PATH_MAX], maps_dir[PATH_MAX + 8];
char bpffs_name[UPATCH_BPFFS_NAME_MAX + 1];
@@ -1550,7 +1551,7 @@ static int pin_upatch_maps(const struct loader_ctx *cx,
struct stat st;
int err;
- if (md->map_fd <= 0)
+ if (i == shared_policy_map_idx || md->map_fd <= 0)
continue;
err = bpf_map_get_info_by_fd(md->map_fd, &info, &info_len);
if (err) {
@@ -1685,9 +1686,9 @@ static int __upatch_apply(const char *path, bool verify_only,
char upatch_name[UPATCH_NAME_MAX + 1];
char *vlog; /* shared PROG_LOAD / TEST_RUN log_buf */
size_t insn_cnt, ctx_sz = 0, prog_desc_off;
- int prog_fd = -1, map_fd = -1, ret;
+ int prog_fd = -1, map_fd = -1, policy_map_fd = -1, ret;
struct loader_ctx *cx = NULL;
- __u32 retval = 0;
+ __u32 retval = 0, shared_policy_map_idx = SHIRUDO_UPATCH_MAPS_MAX;
vlog = xmalloc(VERIFIER_LOG_MAX);
if (!vlog) {
@@ -1988,6 +1989,62 @@ static int __upatch_apply(const char *path, bool verify_only,
cx->log_level = 1;
cx->log_size = VERIFIER_LOG_MAX;
cx->log_buf = ptr_to_u64(vlog);
+ if (u.signed_policy_labels) {
+ struct bpf_map_info info = {};
+ struct map_desc *md;
+ __u32 info_len = sizeof(info);
+ int map_err;
+
+ shared_policy_map_idx = u.signed_policy_map_idx;
+ policy_map_fd = bpf_obj_get(POLICY_PIN);
+ if (policy_map_fd < 0) {
+ int err = errno;
+
+ pr_err("upatch load: open policy_map: %s", strerror(err));
+ rep_fail(rep, "payload_load", "unknown", err,
+ "policy_map binding failed");
+ ret = 1;
+ goto out;
+ }
+ map_err = bpf_map_get_info_by_fd(policy_map_fd, &info, &info_len);
+ if (map_err)
+ map_err = errno;
+ else if (info.type != BPF_MAP_TYPE_TASK_STORAGE ||
+ info.key_size != sizeof(__s32) ||
+ info.value_size != 2 * sizeof(__u64) ||
+ info.map_flags != BPF_F_NO_PREALLOC)
+ map_err = EINVAL;
+ if (map_err) {
+ pr_err("upatch load: policy_map ABI mismatch");
+ rep_fail(rep, "payload_load", "unknown", map_err,
+ "policy_map binding failed");
+ close(policy_map_fd);
+ policy_map_fd = -1;
+ ret = 1;
+ goto out;
+ }
+ if (policy_map_fd == 0) {
+ int fd = fcntl(policy_map_fd, F_DUPFD_CLOEXEC, 3);
+
+ if (fd < 0) {
+ int err = errno;
+
+ close(policy_map_fd);
+ policy_map_fd = -1;
+ pr_err("upatch load: duplicate policy_map fd: %s",
+ strerror(err));
+ rep_fail(rep, "payload_load", "unknown", err,
+ "policy_map binding failed");
+ ret = 1;
+ goto out;
+ }
+ close(policy_map_fd);
+ policy_map_fd = fd;
+ }
+ md = (struct map_desc *)((__u8 *)cx + sizeof(*cx)) +
+ shared_policy_map_idx;
+ md->map_fd = policy_map_fd;
+ }
ret = prog_test_run(prog_fd, cx, (__u32)ctx_sz, &retval);
if (ret) {
@@ -2003,6 +2060,10 @@ static int __upatch_apply(const char *path, bool verify_only,
goto out;
}
if ((__s32)retval < 0) {
+ if (policy_map_fd >= 0) {
+ close(policy_map_fd);
+ policy_map_fd = -1;
+ }
/*
* The loader closed what it made before it failed; the
* numbers left in the context name nothing of ours now.
@@ -2051,7 +2112,8 @@ static int __upatch_apply(const char *path, bool verify_only,
goto out;
}
- pin_upatch_maps(cx, u.hdr.nr_maps, upatch_name);
+ pin_upatch_maps(cx, u.hdr.nr_maps, shared_policy_map_idx,
+ upatch_name);
/* After attach and pin, so the value records a loaded upatch. */
set_pin_payload(upatch_name, u.hdr.payload_sha256);
diff --git a/include/shirudo/upatch.h b/include/shirudo/upatch.h
index 95edfa15..eaf230d7 100644
--- a/include/shirudo/upatch.h
+++ b/include/shirudo/upatch.h
@@ -92,6 +92,8 @@ enum upatch_section_tag {
#define SHIRUDO_UPATCH_POLICY_LABELS_MAGIC 0x4c504853u /* "SHPL" le */
#define SHIRUDO_UPATCH_POLICY_LABELS_VERSION 1u
+#define SHIRUDO_UPATCH_POLICY_UNLABELS_VERSION 1u
+#define SHIRUDO_UPATCH_MAPS_MAX 64u
#define SHIRUDO_UPATCH_POLICY_NAME_MAX 64u
#define SHIRUDO_UPATCH_POLICY_PATH_MAX 4096u
#define SHIRUDO_POLICY_XATTR "security.bpf.shirudo.policy"
@@ -100,7 +102,7 @@ struct shirudo_upatch_policy_label_header {
__u32 magic;
__u32 version;
__u32 count;
- __u32 reserved;
+ __u32 policy_map_idx;
};
struct shirudo_upatch_policy_label_entry {
diff --git a/testing/fixtures/policy-labels/policy-labels.bpf.c b/testing/fixtures/policy-labels/policy-labels.bpf.c
index 9d005337..a7cc51d9 100644
--- a/testing/fixtures/policy-labels/policy-labels.bpf.c
+++ b/testing/fixtures/policy-labels/policy-labels.bpf.c
@@ -2,6 +2,7 @@
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
+#include "shirudo_policy_map.h"
char _license[] SEC("license") = "Dual BSD/GPL";
@@ -10,3 +11,16 @@ int BPF_PROG(shirudo_test_policy_labels)
{
return 0;
}
+
+SEC("lsm.s/bprm_check_security")
+int BPF_PROG(shirudo_test_policy_verdict,
+ struct linux_binprm *bprm __attribute__((__unused__)), int ret)
+{
+ struct shirudo_task_policy *policy;
+
+ if (ret)
+ return ret;
+ policy = bpf_task_storage_get(&policy_map, bpf_get_current_task_btf(),
+ NULL, 0);
+ return policy && (policy->active & 1) ? -13 : 0;
+}
diff --git a/testing/functional/upatch/t3109-policy-labels.sh b/testing/functional/upatch/t3109-policy-labels.sh
index e4a2beff..5e111a08 100755
--- a/testing/functional/upatch/t3109-policy-labels.sh
+++ b/testing/functional/upatch/t3109-policy-labels.sh
@@ -31,14 +31,15 @@ trap restore EXIT HUP INT TERM
[ -x "$PROBE" ] || test_skip_all "runtime policy probe is missing"
rm -rf "$WORK"
mkdir -p "$WORK"
-printf '#!/bin/sh\nexit 0\n' >"$WORK/executable"
+printf '#!/bin/sh\n/bin/true\n' >"$WORK/executable"
printf 'ordinary file\n' >"$WORK/ordinary"
chmod 0755 "$WORK/executable"
"$PROBE" set-policy "$WORK/ordinary" 0x8 ||
test_skip_all "scratch filesystem cannot store policy xattrs"
-test_expect_success 'upatch labels executable and ordinary files without clearing existing bits' '
+test_expect_success 'policy section labels files and binds the shared task map' '
"$SHIRUDO_BIN" upatch load "$LOAD" --no-precheck >/dev/null 2>&1 &&
+ ! "$WORK/executable" >/dev/null 2>&1 &&
python3 - "$WORK/executable" "$WORK/ordinary" <<'"'"'PY'"'"'
import os
import sys
diff --git a/testing/helper/test-upatch-prog-metadata.c b/testing/helper/test-upatch-prog-metadata.c
index 8a368a95..ee4f02b6 100644
--- a/testing/helper/test-upatch-prog-metadata.c
+++ b/testing/helper/test-upatch-prog-metadata.c
@@ -183,7 +183,8 @@ static int expect_policy_labels(const struct upatch *u)
memcpy(&header, raw, sizeof(header));
if (le32toh(header.magic) != SHIRUDO_UPATCH_POLICY_LABELS_MAGIC ||
le32toh(header.version) != SHIRUDO_UPATCH_POLICY_LABELS_VERSION ||
- le32toh(header.count) != count || header.reserved)
+ le32toh(header.count) != count ||
+ le32toh(header.policy_map_idx) != 0)
return fail("policy label header mismatch");
cursor = raw + sizeof(header);
for (size_t i = 0; i < count; i++) {
diff --git a/tools/shirudo-build/package.c b/tools/shirudo-build/package.c
index cf1effc0..29877285 100644
--- a/tools/shirudo-build/package.c
+++ b/tools/shirudo-build/package.c
@@ -19,6 +19,7 @@
#include <openssl/evp.h>
+#include <linux/bpf.h>
#include <bpf/libbpf.h>
#include <shirudo/misc.h>
@@ -1587,6 +1588,17 @@ static int finalize_object(struct bpf_object *obj, const char *out_path,
}
opts.gen_hash = true;
+ if (policy_labels_len) {
+ const struct shirudo_upatch_policy_label_header *labels =
+ policy_labels;
+ __u32 map_idx = le32toh(labels->policy_map_idx);
+
+ if (map_idx >= nr_maps || map_idx >= SHIRUDO_UPATCH_MAPS_MAX) {
+ err = -EINVAL;
+ goto out;
+ }
+ opts.reuse_map_mask = 1ULL << map_idx;
+ }
err = bpf_object__gen_loader(obj, &opts);
if (err) {
pr_err("bpf_object__gen_loader: %d", err);
@@ -1758,7 +1770,9 @@ int build_package_load(const char *bpf_o, const char *src_path,
struct bpf_object *obj;
struct shirudo_upatch_policy_label_header *label_blob = NULL;
struct shirudo_upatch_policy_label_entry *label_entries;
- __u32 label_len = 0;
+ struct bpf_map *map, *policy_map;
+ __u32 label_len = 0, policy_map_idx = 0;
+ __u32 map_idx = 0;
int handler_ids[2] = {-1, -1};
int err;
@@ -1780,16 +1794,36 @@ int build_package_load(const char *bpf_o, const char *src_path,
return err;
}
if (nr_policies) {
+ policy_map = bpf_object__find_map_by_name(obj, "policy_map");
+ if (!policy_map ||
+ bpf_map__type(policy_map) != BPF_MAP_TYPE_TASK_STORAGE ||
+ bpf_map__key_size(policy_map) != sizeof(__s32) ||
+ bpf_map__value_size(policy_map) != 2 * sizeof(__u64) ||
+ bpf_map__map_flags(policy_map) != BPF_F_NO_PREALLOC) {
+ pr_err("policy labels require compatible policy_map task storage");
+ err = -EINVAL;
+ goto out;
+ }
+ bpf_object__for_each_map(map, obj) {
+ if (map == policy_map)
+ break;
+ map_idx++;
+ }
+ if (map_idx >= SHIRUDO_UPATCH_MAPS_MAX || map != policy_map) {
+ err = -E2BIG;
+ goto out;
+ }
+ policy_map_idx = map_idx;
label_len = sizeof(*label_blob) + nr_policies * sizeof(*label_entries);
label_blob = xcalloc(1, label_len);
if (!label_blob) {
- bpf_object__close(obj);
- unregister_target_attach_types(handler_ids);
- return -ENOMEM;
+ err = -ENOMEM;
+ goto out;
}
label_blob->magic = htole32(SHIRUDO_UPATCH_POLICY_LABELS_MAGIC);
label_blob->version = htole32(SHIRUDO_UPATCH_POLICY_LABELS_VERSION);
label_blob->count = htole32(nr_policies);
+ label_blob->policy_map_idx = htole32(policy_map_idx);
label_entries = (void *)(label_blob + 1);
for (__u32 i = 0; i < nr_policies; i++) {
label_entries[i] = policies[i];
@@ -1798,6 +1832,10 @@ int build_package_load(const char *bpf_o, const char *src_path,
}
err = finalize_object(obj, out_path, upatch_name, 0, NULL, src_path,
label_blob, label_len, pkey_path, cert_path);
+ obj = NULL;
+out:
+ if (obj)
+ bpf_object__close(obj);
if (label_blob)
xfree(label_blob, label_len);
unregister_target_attach_types(handler_ids);
diff --git a/util/upatch.c b/util/upatch.c
index 473c7868..9f0b70e4 100644
--- a/util/upatch.c
+++ b/util/upatch.c
@@ -120,18 +120,25 @@ static bool has_nul(const char *s, size_t n)
return memchr(s, '\0', n);
}
-static bool parse_policy_labels(const __u8 *p, __u32 len)
+static bool parse_policy_labels(const __u8 *p, __u32 len, __u32 nr_maps,
+ bool bind_policy_map, __u32 *map_idx)
{
struct shirudo_upatch_policy_label_header h;
- __u32 count;
+ __u32 count, version, idx;
if (len < sizeof(h))
return false;
memcpy(&h, p, sizeof(h));
count = le32toh(h.count);
+ version = le32toh(h.version);
+ idx = le32toh(h.policy_map_idx);
if (le32toh(h.magic) != SHIRUDO_UPATCH_POLICY_LABELS_MAGIC ||
- le32toh(h.version) != SHIRUDO_UPATCH_POLICY_LABELS_VERSION ||
- le32toh(h.reserved) || !count ||
+ version != (bind_policy_map ?
+ SHIRUDO_UPATCH_POLICY_LABELS_VERSION :
+ SHIRUDO_UPATCH_POLICY_UNLABELS_VERSION) ||
+ (bind_policy_map && (idx >= nr_maps ||
+ idx >= SHIRUDO_UPATCH_MAPS_MAX)) ||
+ (!bind_policy_map && idx) || !count ||
(__u64)sizeof(h) + (__u64)count *
sizeof(struct shirudo_upatch_policy_label_entry) != len)
return false;
@@ -146,6 +153,8 @@ static bool parse_policy_labels(const __u8 *p, __u32 len)
!has_nul(e.path, sizeof(e.path)) || e.path[0] != '/')
return false;
}
+ if (bind_policy_map)
+ *map_idx = idx;
return true;
}
@@ -215,6 +224,7 @@ static void forget_signed_sections(struct upatch *u)
u->signed_unload_payload = NULL;
u->signed_policy_labels = NULL;
u->signed_policy_labels_len = 0;
+ u->signed_policy_map_idx = 0;
u->signed_policy_unlabels = NULL;
u->signed_policy_unlabels_len = 0;
}
@@ -287,14 +297,17 @@ static void parse_signed_sections(struct upatch *u)
break;
case SHIRUDO_UPATCH_SECTION_POLICY_LABELS:
bit = 16;
- if (!parse_policy_labels(data + off, len))
+ if (!parse_policy_labels(data + off, len, u->hdr.nr_maps,
+ true,
+ &u->signed_policy_map_idx))
goto reject;
u->signed_policy_labels = data + off;
u->signed_policy_labels_len = len;
break;
case SHIRUDO_UPATCH_SECTION_POLICY_UNLABELS:
bit = 32;
- if (!parse_policy_labels(data + off, len))
+ if (!parse_policy_labels(data + off, len, u->hdr.nr_maps,
+ false, NULL))
goto reject;
u->signed_policy_unlabels = data + off;
u->signed_policy_unlabels_len = len;
diff --git a/util/upatch.h b/util/upatch.h
index 2d1578c7..3b295892 100644
--- a/util/upatch.h
+++ b/util/upatch.h
@@ -56,6 +56,7 @@ struct upatch {
const __u8 *signed_unload_payload; /* 32 bytes */
const void *signed_policy_labels;
__u32 signed_policy_labels_len;
+ __u32 signed_policy_map_idx;
const void *signed_policy_unlabels;
__u32 signed_policy_unlabels_len;
};
diff --git a/vendor/libbpf/README.md b/vendor/libbpf/README.md
index edf62902..28e2d1a1 100644
--- a/vendor/libbpf/README.md
+++ b/vendor/libbpf/README.md
@@ -30,7 +30,10 @@ of `bpf-deps` fails on the missing symbol; extend the shim.
`bpf/gen_loader.c` carries one local change on top of the recorded
commit: `MAX_USED_PROGS` is 64 instead of 32, as required by the
-kernel-obs package.
+kernel-obs package. In addition `gen_loader.c` also supports reusing a
+signer-selected map FD supplied through the loader context. `bpf-deps`
+overwrites these files, so re-apply those changes after a refresh until
+they are upstream.
`.github/scripts/check-spdx.sh` holds every vendored `.c` and `.h` to
the licences in this table, so a refresh that brings in a file under
diff --git a/vendor/libbpf/bpf/gen_loader.c b/vendor/libbpf/bpf/gen_loader.c
index 74ccf6c2..12c16367 100644
--- a/vendor/libbpf/bpf/gen_loader.c
+++ b/vendor/libbpf/bpf/gen_loader.c
@@ -116,7 +116,7 @@ void bpf_gen__init(struct bpf_gen *gen, int log_level, int nr_progs, int nr_maps
int max_func_ptr_maps)
{
size_t stack_sz = sizeof(struct loader_stack), nr_progs_sz;
- int i;
+ int i, cleanup_maps, nr_cleanup_maps;
gen->fd_array = add_data(gen, NULL, MAX_FD_ARRAY_SZ * sizeof(int));
gen->log_level = log_level;
@@ -127,6 +127,12 @@ void bpf_gen__init(struct bpf_gen *gen, int log_level, int nr_progs, int nr_maps
gen->error = -E2BIG;
return;
}
+ cleanup_maps = nr_maps + max_func_ptr_maps;
+ nr_cleanup_maps = cleanup_maps;
+ for (i = 0; i < nr_maps; i++) {
+ if (gen->opts->reuse_map_mask & (1ULL << i))
+ nr_cleanup_maps--;
+ }
/* their fds are closed like the fds of the maps of the object when loading fails */
nr_maps += max_func_ptr_maps;
/* save ctx pointer into R6 */
@@ -150,7 +156,7 @@ void bpf_gen__init(struct bpf_gen *gen, int log_level, int nr_progs, int nr_maps
/* 6 insns for emit_sys_close_blob,
* 6 insns for debug_regs in emit_sys_close_blob
*/
- nr_maps * (6 + (gen->log_level ? 6 : 0))));
+ nr_cleanup_maps * (6 + (gen->log_level ? 6 : 0))));
/* remember the label where all error branches will jump to */
gen->cleanup_label = gen->insn_cur - gen->insn_start;
@@ -160,8 +166,12 @@ void bpf_gen__init(struct bpf_gen *gen, int log_level, int nr_progs, int nr_maps
emit(gen, BPF_JMP_IMM(BPF_JSLE, BPF_REG_1, 0, 1));
emit(gen, BPF_EMIT_CALL(BPF_FUNC_sys_close));
}
- for (i = 0; i < nr_maps; i++)
+ for (i = 0; i < nr_maps; i++) {
+ if (i < gen->nr_obj_maps &&
+ (gen->opts->reuse_map_mask & (1ULL << i)))
+ continue;
emit_sys_close_blob(gen, blob_fd_array_off(gen, i));
+ }
/* R7 contains the error code from sys_bpf. Copy it into R0 and exit. */
emit(gen, BPF_MOV64_REG(BPF_REG_0, BPF_REG_7));
emit(gen, BPF_EXIT_INSN());
@@ -511,6 +521,25 @@ void bpf_gen__map_create(struct bpf_gen *gen,
union bpf_attr attr;
memset(&attr, 0, attr_size);
+ if (map_idx >= 0 && map_idx < MAX_USED_MAPS &&
+ (gen->opts->reuse_map_mask & (1ULL << map_idx))) {
+ int ctx_off, fd_idx;
+
+ fd_idx = add_map_fd(gen);
+ ctx_off = sizeof(struct bpf_loader_ctx) +
+ sizeof(struct bpf_map_desc) * map_idx +
+ offsetof(struct bpf_map_desc, map_fd);
+ emit(gen, BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6, ctx_off));
+ emit(gen, BPF_JMP_IMM(BPF_JSGT, BPF_REG_7, 0, 2));
+ emit(gen, BPF_MOV64_IMM(BPF_REG_7, -EBADF));
+ emit_check_err(gen);
+ emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_1,
+ BPF_PSEUDO_MAP_IDX_VALUE,
+ 0, 0, 0,
+ blob_fd_array_off(gen, fd_idx)));
+ emit(gen, BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_7, 0));
+ return;
+ }
attr.map_type = tgt_endian(map_type);
attr.key_size = tgt_endian(key_size);
attr.value_size = tgt_endian(value_size);
diff --git a/vendor/libbpf/bpf/libbpf.h b/vendor/libbpf/bpf/libbpf.h
index 22204a54..6ed1a63e 100644
--- a/vendor/libbpf/bpf/libbpf.h
+++ b/vendor/libbpf/bpf/libbpf.h
@@ -1990,9 +1990,10 @@ struct gen_loader_opts {
__u32 data_sz;
__u32 insns_sz;
bool gen_hash;
+ __u64 reuse_map_mask;
};
-#define gen_loader_opts__last_field gen_hash
+#define gen_loader_opts__last_field reuse_map_mask
LIBBPF_API int bpf_object__gen_loader(struct bpf_object *obj,
struct gen_loader_opts *opts);