Skip to content

eBPF-based security platform

bpf: expose socket policy to enforcement upatches

John Fastabendd945c6e8e626
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index d38c8f2c..3fbc2cd0 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -180,7 +180,8 @@ signed config image.
* - ``t3109-policy-labels``
- A loaded upatch labels executable and ordinary files, ORing
multiple policy bits with a preexisting xattr; its signed policy
- section binds its verdict program to the shared task policy map
+ section binds verdict programs to shared task and socket policy
+ maps
``tpm`` (4)
~~~~~~~~~~~
diff --git a/Documentation/internals/signed-loader.rst b/Documentation/internals/signed-loader.rst
index 5fea2851..3b382df5 100644
--- a/Documentation/internals/signed-loader.rst
+++ b/Documentation/internals/signed-loader.rst
@@ -117,9 +117,10 @@ loader inside the kernel, which ``lsm/bpf`` sees with ``kernel`` true.
- ``BPF_PROG_TEST_RUN`` of the loader
- ``lsm/bpf``, runtime authority (``ProgRun``). The loader then
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.
+ Shirudo opens its pinned ``policy_map`` and
+ ``socket_policy_map`` and supplies both FDs at their signed
+ descriptor indices. The generated loader reuses them; it neither
+ creates nor pins private copies.
* - 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 f197a7d1..4fb2322c 100644
--- a/Documentation/reference/formats/upatch-format.rst
+++ b/Documentation/reference/formats/upatch-format.rst
@@ -300,7 +300,7 @@ Section tags
- variable
- Signed policy labels, each naming a policy ID, one-hot bit and
absolute file path. Its presence opts into the shared task
- ``policy_map`` binding.
+ ``policy_map`` and socket ``socket_policy_map`` bindings.
The writer emits ``NAME``, then ``UNLD`` for an unload directive, or
``UPGM`` and optional ``DCDB`` and ``SHPL`` sections for a load artifact.
@@ -585,13 +585,14 @@ 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.
+``SHPL`` starts with a 20-byte version 1 header, followed by ``count``
+fixed-size entries. ``policy_map_idx`` and ``socket_policy_map_idx`` are
+the zero-based indices of the shared maps in the loader's map descriptor
+array. The builder accepts this section only when the BPF object declares
+compatible maps named ``policy_map`` and ``socket_policy_map``. At load
+time, Shirudo supplies the pinned task and socket storage maps at those
+descriptors; the loader does not create or pin private copies. Upatches
+without ``SHPL`` use their normal maps.
.. list-table::
:header-rows: 1
@@ -617,6 +618,10 @@ private copy. Upatches that do not carry ``SHPL`` use their normal maps.
- 4
- ``policy_map_idx``
- Map descriptor index, less than ``nr_maps`` and 64.
+ * - +16
+ - 4
+ - ``socket_policy_map_idx``
+ - Distinct 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
diff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rst
index 52283cb2..2084b86c 100644
--- a/Documentation/security/threat-model.rst
+++ b/Documentation/security/threat-model.rst
@@ -673,12 +673,13 @@ 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.
+shared task and socket policy maps. The builder requires compatible map
+declarations, and the target supplies both pinned map FDs to the
+generated loader. The upatch's signed BPF programs can inspect task and
+socket policy values at any supported LSM hook. Authors are expected to
+read these values; the verifier does not prevent signed programs from
+changing them. Upatches without ``SHPL`` receive no binding to these
+maps.
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
diff --git a/Documentation/upatches/publish/beefile.rst b/Documentation/upatches/publish/beefile.rst
index 906df401..8c0aff36 100644
--- a/Documentation/upatches/publish/beefile.rst
+++ b/Documentation/upatches/publish/beefile.rst
@@ -209,10 +209,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 ``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.
+upatch into Shirudo's shared task ``policy_map`` and socket
+``socket_policy_map``. The generated loader reuses both pinned maps
+rather than creating private copies. The BPF source must declare the map
+ABI, normally by including ``shirudo_policy_map.h`` after ``vmlinux.h``
+and the libbpf headers.
.. code-block:: toml
@@ -227,11 +228,12 @@ 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
+``shirudo_policy_map.h`` in its BPF source so it declares both shared maps
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.
+headers. The signed ``SHPL`` section opts the upatch into these bindings;
+declaring the maps without policy tables leaves them as ordinary
+upatch-owned maps. Signed BPF programs can write the maps, so enforcement
+programs must treat them as read-only by contract.
The generator and the updater run on the operator host with the
operator's rights. The target receives only the signed result. Without
diff --git a/bpf/lib/lsm.h b/bpf/lib/lsm.h
index 9e816082..f0b7acb0 100644
--- a/bpf/lib/lsm.h
+++ b/bpf/lib/lsm.h
@@ -49,13 +49,6 @@ struct {
__type(value, struct task_authority);
} shirudo_tasks SEC(".maps");
-struct {
- __uint(type, BPF_MAP_TYPE_SK_STORAGE);
- __uint(map_flags, BPF_F_NO_PREALLOC | BPF_F_CLONE);
- __type(key, int);
- __type(value, __u64);
-} socket_policy_map SEC(".maps");
-
static __always_inline struct sb_id *shirudo_policy(void)
{
__u32 zero = 0;
diff --git a/bpf/lib/shirudo_policy_map.h b/bpf/lib/shirudo_policy_map.h
index 7de9dd09..6e61a431 100644
--- a/bpf/lib/shirudo_policy_map.h
+++ b/bpf/lib/shirudo_policy_map.h
@@ -14,4 +14,11 @@ struct {
__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);
+ __type(key, int);
+ __type(value, __u64);
+} socket_policy_map SEC(".maps");
+
#endif
diff --git a/builtin-upatch.c b/builtin-upatch.c
index 4d75eed0..d20a7147 100644
--- a/builtin-upatch.c
+++ b/builtin-upatch.c
@@ -1518,6 +1518,7 @@ out:
*/
static int pin_upatch_maps(const struct loader_ctx *cx,
__u32 nr_maps, __u32 shared_policy_map_idx,
+ __u32 shared_socket_policy_map_idx,
const char *name)
{
char dir[PATH_MAX], maps_dir[PATH_MAX + 8];
@@ -1551,7 +1552,8 @@ static int pin_upatch_maps(const struct loader_ctx *cx,
struct stat st;
int err;
- if (i == shared_policy_map_idx || md->map_fd <= 0)
+ if (i == shared_policy_map_idx ||
+ i == shared_socket_policy_map_idx || md->map_fd <= 0)
continue;
err = bpf_map_get_info_by_fd(md->map_fd, &info, &info_len);
if (err) {
@@ -1582,6 +1584,42 @@ static int pin_upatch_maps(const struct loader_ctx *cx,
return 0;
}
+static int open_shared_policy_map(const char *pin, __u32 type,
+ __u32 value_size, __u32 map_flags)
+{
+ struct bpf_map_info info = {};
+ __u32 info_len = sizeof(info);
+ int fd, rc;
+
+ fd = bpf_obj_get(pin);
+ if (fd < 0)
+ return -errno;
+ rc = bpf_map_get_info_by_fd(fd, &info, &info_len);
+ if (rc) {
+ rc = -errno;
+ goto err;
+ }
+ if (info.type != type || info.key_size != sizeof(__s32) ||
+ info.value_size != value_size || info.map_flags != map_flags) {
+ rc = -EINVAL;
+ goto err;
+ }
+ if (fd == 0) {
+ int copy_fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
+
+ if (copy_fd < 0) {
+ rc = -errno;
+ goto err;
+ }
+ close(fd);
+ fd = copy_fd;
+ }
+ return fd;
+err:
+ close(fd);
+ return rc;
+}
+
/*
* The value `upatch unload` checks, unforgeable behind bpf2's xattr
* guard. The load stands, so a failure is not fatal, but it leaves the
@@ -1686,9 +1724,11 @@ 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, policy_map_fd = -1, ret;
+ int prog_fd = -1, map_fd = -1, policy_map_fd = -1;
+ int socket_policy_map_fd = -1, ret;
struct loader_ctx *cx = NULL;
__u32 retval = 0, shared_policy_map_idx = SHIRUDO_UPATCH_MAPS_MAX;
+ __u32 shared_socket_policy_map_idx = SHIRUDO_UPATCH_MAPS_MAX;
vlog = xmalloc(VERIFIER_LOG_MAX);
if (!vlog) {
@@ -1990,60 +2030,46 @@ static int __upatch_apply(const char *path, bool verify_only,
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);
+ shared_socket_policy_map_idx =
+ u.signed_socket_policy_map_idx;
+ policy_map_fd =
+ open_shared_policy_map(POLICY_PIN,
+ BPF_MAP_TYPE_TASK_STORAGE,
+ 2 * sizeof(__u64),
+ BPF_F_NO_PREALLOC);
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,
+ pr_err("upatch load: open policy_map: %s",
+ strerror(-policy_map_fd));
+ rep_fail(rep, "payload_load", "unknown", -policy_map_fd,
"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");
+ socket_policy_map_fd =
+ open_shared_policy_map(SOCKET_POLICY_PIN,
+ BPF_MAP_TYPE_SK_STORAGE, sizeof(__u64),
+ BPF_F_NO_PREALLOC | BPF_F_CLONE);
+ if (socket_policy_map_fd < 0) {
+ int err = -socket_policy_map_fd;
+
+ pr_err("upatch load: open socket_policy_map: %s",
+ strerror(err));
+ rep_fail(rep, "payload_load", "unknown", err,
+ "socket_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;
+ md = (struct map_desc *)((__u8 *)cx + sizeof(*cx)) +
+ shared_socket_policy_map_idx;
+ md->map_fd = socket_policy_map_fd;
}
ret = prog_test_run(prog_fd, cx, (__u32)ctx_sz, &retval);
@@ -2064,6 +2090,10 @@ static int __upatch_apply(const char *path, bool verify_only,
close(policy_map_fd);
policy_map_fd = -1;
}
+ if (socket_policy_map_fd >= 0) {
+ close(socket_policy_map_fd);
+ socket_policy_map_fd = -1;
+ }
/*
* The loader closed what it made before it failed; the
* numbers left in the context name nothing of ours now.
@@ -2113,7 +2143,7 @@ static int __upatch_apply(const char *path, bool verify_only,
}
pin_upatch_maps(cx, u.hdr.nr_maps, shared_policy_map_idx,
- upatch_name);
+ shared_socket_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 eaf230d7..b3731501 100644
--- a/include/shirudo/upatch.h
+++ b/include/shirudo/upatch.h
@@ -103,6 +103,7 @@ struct shirudo_upatch_policy_label_header {
__u32 version;
__u32 count;
__u32 policy_map_idx;
+ __u32 socket_policy_map_idx;
};
struct shirudo_upatch_policy_label_entry {
diff --git a/testing/fixtures/policy-labels/Beefile b/testing/fixtures/policy-labels/Beefile
index 2189f39f..da8a2349 100644
--- a/testing/fixtures/policy-labels/Beefile
+++ b/testing/fixtures/policy-labels/Beefile
@@ -20,3 +20,8 @@ paths = ["/tmp/shirudo-policy-label-test/ordinary"]
id = "ordinary-b"
bit = 0x4
paths = ["/tmp/shirudo-policy-label-test/ordinary"]
+
+[[upatches.policy]]
+id = "socket-check"
+bit = 0x10
+paths = ["/tmp/shirudo-policy-label-test/socket-executable"]
diff --git a/testing/fixtures/policy-labels/policy-labels.bpf.c b/testing/fixtures/policy-labels/policy-labels.bpf.c
index a7cc51d9..01293f39 100644
--- a/testing/fixtures/policy-labels/policy-labels.bpf.c
+++ b/testing/fixtures/policy-labels/policy-labels.bpf.c
@@ -22,5 +22,28 @@ int BPF_PROG(shirudo_test_policy_verdict,
return ret;
policy = bpf_task_storage_get(&policy_map, bpf_get_current_task_btf(),
NULL, 0);
- return policy && (policy->active & 1) ? -13 : 0;
+ return policy && (policy->candidate & 1) ? -13 : 0;
+}
+
+SEC("lsm.s/socket_connect")
+int BPF_PROG(shirudo_test_socket_policy_verdict,
+ struct socket *sock,
+ struct sockaddr *address __attribute__((__unused__)),
+ int addrlen __attribute__((__unused__)),
+ int ret)
+{
+ struct shirudo_task_policy *policy;
+ struct sock *sk;
+ __u64 *socket_policy;
+
+ if (ret || !sock)
+ return ret;
+ sk = sock->sk;
+ if (!sk)
+ return 0;
+ socket_policy = bpf_sk_storage_get(&socket_policy_map, sk, NULL, 0);
+ policy = bpf_task_storage_get(&policy_map, bpf_get_current_task_btf(),
+ NULL, 0);
+ return (socket_policy && (*socket_policy & 16)) ||
+ (policy && (policy->active & 16)) ? -13 : 0;
}
diff --git a/testing/functional/upatch/t3109-policy-labels.sh b/testing/functional/upatch/t3109-policy-labels.sh
index 5e111a08..6ac93437 100755
--- a/testing/functional/upatch/t3109-policy-labels.sh
+++ b/testing/functional/upatch/t3109-policy-labels.sh
@@ -32,14 +32,21 @@ trap restore EXIT HUP INT TERM
rm -rf "$WORK"
mkdir -p "$WORK"
printf '#!/bin/sh\n/bin/true\n' >"$WORK/executable"
+printf '#!/bin/sh\npython3 -c '\''import socket; s=socket.socket();\ntry: s.connect(("127.0.0.1", 9))\nexcept PermissionError: raise SystemExit(0)\nexcept OSError: raise SystemExit(1)\nraise SystemExit(2)'\''\n' \
+ >"$WORK/socket-executable"
printf 'ordinary file\n' >"$WORK/ordinary"
-chmod 0755 "$WORK/executable"
+chmod 0755 "$WORK/executable" "$WORK/socket-executable"
"$PROBE" set-policy "$WORK/ordinary" 0x8 ||
test_skip_all "scratch filesystem cannot store policy xattrs"
-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 &&
+test_expect_success 'policy section binds task and socket maps and labels files' '
+ "$SHIRUDO_BIN" upatch load "$LOAD" --no-precheck &&
+ { ! "$WORK/executable" >/dev/null 2>&1 || {
+ echo "policy executable was allowed" >&2; false;
+ }; } &&
+ { "$WORK/socket-executable" >/dev/null 2>&1 || {
+ echo "socket policy executable failed" >&2; false;
+ }; } &&
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 ee4f02b6..7c218980 100644
--- a/testing/helper/test-upatch-prog-metadata.c
+++ b/testing/helper/test-upatch-prog-metadata.c
@@ -163,14 +163,15 @@ static int expect_uprobe_multi(const struct upatch *u,
static int expect_policy_labels(const struct upatch *u)
{
static const char *const policies[] = {
- "executable", "ordinary-a", "ordinary-b",
+ "executable", "ordinary-a", "ordinary-b", "socket-check",
};
static const char *const paths[] = {
"/tmp/shirudo-policy-label-test/executable",
"/tmp/shirudo-policy-label-test/ordinary",
"/tmp/shirudo-policy-label-test/ordinary",
+ "/tmp/shirudo-policy-label-test/socket-executable",
};
- static const __u64 bits[] = { 1, 2, 4 };
+ static const __u64 bits[] = { 1, 2, 4, 16 };
struct shirudo_upatch_policy_label_entry entry;
struct shirudo_upatch_policy_label_header header;
const __u8 *raw, *cursor;
@@ -184,7 +185,8 @@ static int expect_policy_labels(const struct upatch *u)
if (le32toh(header.magic) != SHIRUDO_UPATCH_POLICY_LABELS_MAGIC ||
le32toh(header.version) != SHIRUDO_UPATCH_POLICY_LABELS_VERSION ||
le32toh(header.count) != count ||
- le32toh(header.policy_map_idx) != 0)
+ le32toh(header.policy_map_idx) != 0 ||
+ le32toh(header.socket_policy_map_idx) != 1)
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 29877285..4ec2e327 100644
--- a/tools/shirudo-build/package.c
+++ b/tools/shirudo-build/package.c
@@ -1592,12 +1592,18 @@ static int finalize_object(struct bpf_object *obj, const char *out_path,
const struct shirudo_upatch_policy_label_header *labels =
policy_labels;
__u32 map_idx = le32toh(labels->policy_map_idx);
+ __u32 socket_map_idx =
+ le32toh(labels->socket_policy_map_idx);
- if (map_idx >= nr_maps || map_idx >= SHIRUDO_UPATCH_MAPS_MAX) {
+ if (map_idx >= nr_maps || map_idx >= SHIRUDO_UPATCH_MAPS_MAX ||
+ socket_map_idx >= nr_maps ||
+ socket_map_idx >= SHIRUDO_UPATCH_MAPS_MAX ||
+ map_idx == socket_map_idx) {
err = -EINVAL;
goto out;
}
- opts.reuse_map_mask = 1ULL << map_idx;
+ opts.reuse_map_mask = (1ULL << map_idx) |
+ (1ULL << socket_map_idx);
}
err = bpf_object__gen_loader(obj, &opts);
if (err) {
@@ -1770,8 +1776,8 @@ 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;
- struct bpf_map *map, *policy_map;
- __u32 label_len = 0, policy_map_idx = 0;
+ struct bpf_map *map, *policy_map, *socket_policy_map;
+ __u32 label_len = 0, policy_map_idx = 0, socket_policy_map_idx = 0;
__u32 map_idx = 0;
int handler_ids[2] = {-1, -1};
int err;
@@ -1795,6 +1801,8 @@ int build_package_load(const char *bpf_o, const char *src_path,
}
if (nr_policies) {
policy_map = bpf_object__find_map_by_name(obj, "policy_map");
+ socket_policy_map =
+ bpf_object__find_map_by_name(obj, "socket_policy_map");
if (!policy_map ||
bpf_map__type(policy_map) != BPF_MAP_TYPE_TASK_STORAGE ||
bpf_map__key_size(policy_map) != sizeof(__s32) ||
@@ -1804,6 +1812,16 @@ int build_package_load(const char *bpf_o, const char *src_path,
err = -EINVAL;
goto out;
}
+ if (!socket_policy_map ||
+ bpf_map__type(socket_policy_map) != BPF_MAP_TYPE_SK_STORAGE ||
+ bpf_map__key_size(socket_policy_map) != sizeof(__s32) ||
+ bpf_map__value_size(socket_policy_map) != sizeof(__u64) ||
+ bpf_map__map_flags(socket_policy_map) !=
+ (BPF_F_NO_PREALLOC | BPF_F_CLONE)) {
+ pr_err("policy labels require compatible socket_policy_map storage");
+ err = -EINVAL;
+ goto out;
+ }
bpf_object__for_each_map(map, obj) {
if (map == policy_map)
break;
@@ -1814,6 +1832,18 @@ int build_package_load(const char *bpf_o, const char *src_path,
goto out;
}
policy_map_idx = map_idx;
+ map_idx = 0;
+ bpf_object__for_each_map(map, obj) {
+ if (map == socket_policy_map)
+ break;
+ map_idx++;
+ }
+ if (map_idx >= SHIRUDO_UPATCH_MAPS_MAX ||
+ map != socket_policy_map || map_idx == policy_map_idx) {
+ err = -E2BIG;
+ goto out;
+ }
+ socket_policy_map_idx = map_idx;
label_len = sizeof(*label_blob) + nr_policies * sizeof(*label_entries);
label_blob = xcalloc(1, label_len);
if (!label_blob) {
@@ -1824,6 +1854,8 @@ int build_package_load(const char *bpf_o, const char *src_path,
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_blob->socket_policy_map_idx =
+ htole32(socket_policy_map_idx);
label_entries = (void *)(label_blob + 1);
for (__u32 i = 0; i < nr_policies; i++) {
label_entries[i] = policies[i];
diff --git a/util/upatch.c b/util/upatch.c
index 9f0b70e4..9fecb648 100644
--- a/util/upatch.c
+++ b/util/upatch.c
@@ -121,40 +121,50 @@ static bool has_nul(const char *s, size_t n)
}
static bool parse_policy_labels(const __u8 *p, __u32 len, __u32 nr_maps,
- bool bind_policy_map, __u32 *map_idx)
+ bool bind_policy_maps, __u32 *map_idx,
+ __u32 *socket_map_idx)
{
struct shirudo_upatch_policy_label_header h;
- __u32 count, version, idx;
+ __u32 count, version, idx, socket_idx;
+ __u32 header_len = bind_policy_maps ? sizeof(h) :
+ offsetof(struct shirudo_upatch_policy_label_header,
+ socket_policy_map_idx);
- if (len < sizeof(h))
+ if (len < header_len)
return false;
- memcpy(&h, p, sizeof(h));
+ memset(&h, 0, sizeof(h));
+ memcpy(&h, p, header_len);
count = le32toh(h.count);
version = le32toh(h.version);
idx = le32toh(h.policy_map_idx);
+ socket_idx = le32toh(h.socket_policy_map_idx);
if (le32toh(h.magic) != SHIRUDO_UPATCH_POLICY_LABELS_MAGIC ||
- version != (bind_policy_map ?
+ version != (bind_policy_maps ?
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 *
+ (bind_policy_maps &&
+ (idx >= nr_maps || idx >= SHIRUDO_UPATCH_MAPS_MAX ||
+ socket_idx >= nr_maps ||
+ socket_idx >= SHIRUDO_UPATCH_MAPS_MAX || idx == socket_idx)) ||
+ (!bind_policy_maps && (idx || socket_idx)) || !count ||
+ (__u64)header_len + (__u64)count *
sizeof(struct shirudo_upatch_policy_label_entry) != len)
return false;
for (__u32 i = 0; i < count; i++) {
struct shirudo_upatch_policy_label_entry e;
__u64 bit;
- memcpy(&e, p + sizeof(h) + i * sizeof(e), sizeof(e));
+ memcpy(&e, p + header_len + i * sizeof(e), sizeof(e));
bit = le64toh(e.bit);
if (!bit || (bit & (bit - 1)) ||
!has_nul(e.policy, sizeof(e.policy)) || !e.policy[0] ||
!has_nul(e.path, sizeof(e.path)) || e.path[0] != '/')
return false;
}
- if (bind_policy_map)
+ if (bind_policy_maps) {
*map_idx = idx;
+ *socket_map_idx = socket_idx;
+ }
return true;
}
@@ -225,6 +235,7 @@ static void forget_signed_sections(struct upatch *u)
u->signed_policy_labels = NULL;
u->signed_policy_labels_len = 0;
u->signed_policy_map_idx = 0;
+ u->signed_socket_policy_map_idx = 0;
u->signed_policy_unlabels = NULL;
u->signed_policy_unlabels_len = 0;
}
@@ -299,7 +310,8 @@ static void parse_signed_sections(struct upatch *u)
bit = 16;
if (!parse_policy_labels(data + off, len, u->hdr.nr_maps,
true,
- &u->signed_policy_map_idx))
+ &u->signed_policy_map_idx,
+ &u->signed_socket_policy_map_idx))
goto reject;
u->signed_policy_labels = data + off;
u->signed_policy_labels_len = len;
@@ -307,7 +319,7 @@ static void parse_signed_sections(struct upatch *u)
case SHIRUDO_UPATCH_SECTION_POLICY_UNLABELS:
bit = 32;
if (!parse_policy_labels(data + off, len, u->hdr.nr_maps,
- false, NULL))
+ false, NULL, 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 3b295892..5b429be3 100644
--- a/util/upatch.h
+++ b/util/upatch.h
@@ -57,6 +57,7 @@ struct upatch {
const void *signed_policy_labels;
__u32 signed_policy_labels_len;
__u32 signed_policy_map_idx;
+ __u32 signed_socket_policy_map_idx;
const void *signed_policy_unlabels;
__u32 signed_policy_unlabels_len;
};