eBPF-based security platform
seal: inherit task policy into socket storage
John Fastabend6434f767f390
diff --git a/Documentation/contribute/testing/testing.rst b/Documentation/contribute/testing/testing.rstindex c0f6e355..04b77f33 100644--- a/Documentation/contribute/testing/testing.rst+++ b/Documentation/contribute/testing/testing.rst@@ -123,8 +123,8 @@ A shell test is named ``t<F><K><NN>-<what>.sh``:* - 6- ``functional/tamperproof/``, ``functional/labels/``- Attacks on the admission and BPF command policy, bpffs, the- seal, task authority, task policy labels, the keyrings and the TPM- path+ seal, task authority, task and socket policy labels, the keyrings+ and the TPM path* - 7- ``functional/bpm/``- Package commands, publishing and the upatch treediff --git a/Documentation/internals/architecture.rst b/Documentation/internals/architecture.rstindex 6861200e..b14179fa 100644--- a/Documentation/internals/architecture.rst+++ b/Documentation/internals/architecture.rst@@ -80,8 +80,9 @@ with ``.incbin`` in :src:`util/loader.c`:device-mapper and perf ioctl guards and the dm-verity device records.``seal``- Task marks (``bprm_check_security``, ``bprm_committed_creds``,- ``task_alloc``), process protection (``ptrace_access_check``,+ Task and socket policy marks (``bprm_check_security``,+ ``bprm_committed_creds``, ``task_alloc``, ``socket_post_create``),+ process protection (``ptrace_access_check``,``perf_event_alloc``, ``mmap_file``, ``file_mprotect``) and the labelchecks on writable state.diff --git a/Documentation/internals/enforcement.rst b/Documentation/internals/enforcement.rstindex 4f25db06..fa54e15c 100644--- a/Documentation/internals/enforcement.rst+++ b/Documentation/internals/enforcement.rst@@ -42,7 +42,7 @@ device records of :src:`bpf/lib/`.``init`` loads both without a signature, from the binary theauthenticated initrd carries.-The objects share six maps, which ``init`` hands from ``bpf2`` to+The objects share seven maps, which ``init`` hands from ``bpf2`` to``seal`` with ``bpf_map__reuse_fd()`` and pins in the private bpffs:.. list-table::@@ -65,6 +65,10 @@ The objects share six maps, which ``init`` hands from ``bpf2`` to* - ``policy_map``- ``policy_map``- Task storage with committed policy bits and the exec candidate.+ * - ``socket_policy_map``+ - ``socket_policy_map``+ - Socket storage for committed policy bits, cloned from listeners+ to accepted sockets.* - ``shirudo_devices``- ``devices``- The dm-verity devices recorded as runtime or config, up to 128.@@ -78,7 +82,7 @@ The objects share six maps, which ``init`` hands from ``bpf2`` to``init`` freezes every one of them against userspace writes except``shirudo_roots``, which an authorized task writes before each config-mapping. ``bpf2`` keeps a seventh map, ``shirudo_mnt_rec``, with the bpffs+mapping. ``bpf2`` also keeps ``shirudo_mnt_rec`` for the bpffsmount it records, and ``seal`` keeps its scratch buffer and the inodestorage of directory marks.@@ -426,6 +430,12 @@ when the record it needs is missing.- ``EACCES``- Uncounted- Failed exec leaves committed policy unchanged.+ * - ``lsm/socket_post_create`` (``seal``)+ - Copies nonzero committed task policy bits into socket storage.+ ``BPF_F_CLONE`` carries listener storage to accepted sockets.+ - ``ENOMEM``+ - None+ - No storage is needed when the task has no policy bits.* - ``lsm/task_alloc`` (``seal``)- Copies a marked parent's mark and any committed policy bits to thechild.diff --git a/Documentation/internals/seal.rst b/Documentation/internals/seal.rstindex 2a48e651..f3051a12 100644--- a/Documentation/internals/seal.rst+++ b/Documentation/internals/seal.rst@@ -85,6 +85,13 @@ authority may set, update or remove it. An absent policy or a file systemwithout xattrs contributes zero bits; invalid lengths and other readerrors refuse the exec.+``socket_post_create`` snapshots a task's committed bits into+``socket_policy_map`` when it creates a socket. A socket with no policy+uses no storage and reads as zero. If nonzero policy storage cannot be+allocated, socket creation fails. The map uses ``BPF_F_CLONE`` so an+accepted socket inherits the listener's snapshot, even when the task+calling ``accept()`` has since accumulated other bits.+The device record that the exec check consults comes from``lsm.s/bdev_setintegrity``: it records a dm-verity device as runtimewhen its root hash equals the runtime root the UKI authenticated, whichdiff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rstindex 5fa9e07a..cfca5379 100644--- a/Documentation/security/threat-model.rst+++ b/Documentation/security/threat-model.rst@@ -304,6 +304,10 @@ Assets- ``policy_map`` task storage, pinned as ``policy_map``- Frozen against userspace writes; exec hooks OR protected inodevalues and commit only after a successful exec.+ * - Socket policy bits+ - ``socket_policy_map`` socket storage, pinned as ``socket_policy_map``+ - Frozen against userspace writes; socket creation snapshots task+ bits and accepted sockets inherit the listener's value.* - Device records and expected roots- ``shirudo_devices`` (frozen) and ``shirudo_roots`` (not frozen,written by authorized tasks for their own config mapping)diff --git a/bpf/lib/lsm.h b/bpf/lib/lsm.hindex 2fdd3b90..d31ff980 100644--- a/bpf/lib/lsm.h+++ b/bpf/lib/lsm.h@@ -60,6 +60,13 @@ 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");+static __always_inline struct sb_id *shirudo_policy(void){__u32 zero = 0;diff --git a/bpf/seal.c b/bpf/seal.cindex 95df8291..d3415262 100644--- a/bpf/seal.c+++ b/bpf/seal.c@@ -473,6 +473,35 @@ int BPF_PROG(shirudo_seal_inode_removexattr, struct mnt_idmap *idmap,char _license[] SEC("license") = "Dual BSD/GPL";+SEC("lsm/socket_post_create")+int BPF_PROG(shirudo_seal_socket_post_create, struct socket *sock,+ int family __attribute__((__unused__)),+ int type __attribute__((__unused__)),+ int protocol __attribute__((__unused__)),+ int kern __attribute__((__unused__)), int ret)+{+ struct task_struct *task = bpf_get_current_task_btf();+ struct shirudo_task_policy *policy;+ struct sock *sk;+ __u64 active;++ if (ret)+ return ret;+ if (!sock)+ return -ENOMEM;+ policy = bpf_task_storage_get(&policy_map, task, NULL, 0);+ if (!policy || !policy->active)+ return 0;+ sk = sock->sk;+ if (!sk)+ return -ENOMEM;+ active = policy->active;+ if (!bpf_sk_storage_get(&socket_policy_map, sk, &active,+ BPF_LOCAL_STORAGE_GET_F_CREATE))+ return -ENOMEM;+ return 0;+}+struct seccomp___shirudo {int mode;} __attribute__((preserve_access_index));diff --git a/testing/functional/labels/t6142-policy-socket-inheritance.sh b/testing/functional/labels/t6142-policy-socket-inheritance.shnew file mode 100644index 00000000..6f2a64b2--- /dev/null+++ b/testing/functional/labels/t6142-policy-socket-inheritance.sh@@ -0,0 +1,28 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# t6142 -- task policy follows a listener into accepted sockets.++cd "$(dirname "$0")/../.."+. ./integration-lib.sh+target_guard++PROBE=/run/shirudo/runtime/usr/bin/shirudo-policy-probe+[ -x "$PROBE" ] || test_skip_all "runtime policy probe is missing"++WORK=$(mktemp -d) || test_skip_all "cannot make a scratch directory"+trap 'rm -rf "$WORK"' EXIT HUP INT TERM++cat >"$WORK/socket-test" <<EOF+#!$PROBE socket-inherit+EOF+chmod +x "$WORK/socket-test"+"$PROBE" set-policy "$WORK/socket-test" 0x1 ||+ test_skip_all "scratch filesystem cannot store policy xattrs"++test_expect_success 'listener policy is cloned to accepted socket' '+ "$WORK/socket-test"+'++test_donediff --git a/testing/helper/policy-probe.c b/testing/helper/policy-probe.cindex 32e5a648..4e6f4942 100644--- a/testing/helper/policy-probe.c+++ b/testing/helper/policy-probe.c@@ -2,12 +2,15 @@/* Copyright (c) 2026 Authors of shirudo */#include <sys/auxv.h>+#include <netinet/in.h>+#include <sys/socket.h>#include "../../include/shirudo/misc.h"#include <errno.h>#include <fcntl.h>#include <linux/bpf.h>+#include <limits.h>#include <stdint.h>#include <stdio.h>#include <stdlib.h>@@ -85,6 +88,145 @@ static int read_policy(const char *pin, __u64 *active)return rc;}+static int read_socket_policy(int socket_fd, __u64 *active)+{+ union bpf_attr attr = {};+ __u64 value = 0;+ int fd, rc;++ attr.pathname = (uintptr_t)"/run/shirudo/bpffs/socket_policy_map";+ attr.file_flags = BPF_F_RDONLY;+ fd = syscall(SYS_bpf, BPF_OBJ_GET, &attr, sizeof(attr));+ if (fd < 0)+ return -errno;+ memset(&attr, 0, sizeof(attr));+ attr.map_fd = fd;+ attr.key = (uintptr_t)&socket_fd;+ attr.value = (uintptr_t)&value;+ rc = syscall(SYS_bpf, BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));+ if (rc && errno != ENOENT)+ rc = -errno;+ else+ rc = 0;+ close(fd);+ if (!rc)+ *active = value;+ return rc;+}++static int check_socket_policy(int socket_fd, __u64 expected)+{+ __u64 actual = 0;++ return read_socket_policy(socket_fd, &actual) || actual != expected;+}++static int parse_policy(const char *text, __u64 *value);++static int accept_child(void)+{+ __u64 active = 0;+ __u64 task_bits, socket_bits;+ const char *listener_text = getenv("SHIRUDO_TEST_LISTENER");+ const char *task_text = getenv("SHIRUDO_TEST_TASK_BITS");+ const char *socket_text = getenv("SHIRUDO_TEST_SOCKET_BITS");+ char *end;+ long listener;+ int accepted;++ if (!listener_text || !task_text || !socket_text ||+ parse_policy(task_text, &task_bits) ||+ parse_policy(socket_text, &socket_bits))+ return 1;+ listener = strtol(listener_text, &end, 10);+ if (*end || listener < 0 ||+ read_policy("/run/shirudo/bpffs/policy_map", &active) ||+ active != task_bits)+ return 1;+ accepted = accept((int)listener, NULL, NULL);+ if (accepted < 0)+ return 1;+ return check_socket_policy(accepted, socket_bits);+}++static int socket_inheritance(const char *script, __u64 extra)+{+ struct sockaddr_in addr = {};+ static const char contents[] =+ "#!/run/shirudo/runtime/usr/bin/shirudo-policy-probe accept-child\n";+ char acceptor[PATH_MAX], number[32];+ __u64 active = 0;+ int listener, client, status, rc, length;+ pid_t child;++ if (read_policy("/run/shirudo/bpffs/policy_map", &active) || !active)+ return 1;+ listener = socket(AF_INET, SOCK_STREAM, 0);+ if (listener < 0 || check_socket_policy(listener, active))+ return 1;+ addr.sin_family = AF_INET;+ addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);+ if (bind(listener, (struct sockaddr *)&addr, sizeof(addr)) ||+ listen(listener, 1))+ return 1;+ rc = getsockname(listener, (struct sockaddr *)&addr,+ (socklen_t[]){ sizeof(addr) });+ if (rc)+ return 1;+ length = snprintf(acceptor, sizeof(acceptor), "%s.acceptor", script);+ if (length < 0 || (size_t)length >= sizeof(acceptor))+ return 1;+ {+ int fd = open(acceptor, O_WRONLY | O_CREAT | O_TRUNC, 0700);++ if (fd < 0)+ return 1;+ if (write(fd, contents, sizeof(contents) - 1) !=+ (ssize_t)(sizeof(contents) - 1)) {+ close(fd);+ return 1;+ }+ close(fd);+ }+ {+ __u64 value = extra;++ if (setxattr(acceptor, "security.bpf.shirudo.policy", &value,+ sizeof(value), 0))+ return 1;+ }+ length = snprintf(number, sizeof(number), "%d", listener);+ if (length < 0 || (size_t)length >= sizeof(number) ||+ setenv("SHIRUDO_TEST_LISTENER", number, 1))+ return 1;+ length = snprintf(number, sizeof(number), "0x%llx", active | extra);+ if (length < 0 || (size_t)length >= sizeof(number) ||+ setenv("SHIRUDO_TEST_TASK_BITS", number, 1))+ return 1;+ length = snprintf(number, sizeof(number), "0x%llx", active);+ if (length < 0 || (size_t)length >= sizeof(number) ||+ setenv("SHIRUDO_TEST_SOCKET_BITS", number, 1))+ return 1;+ child = fork();+ if (child < 0)+ return 1;+ if (!child) {+ char *const args[] = { acceptor, NULL };++ execv(acceptor, args);+ _exit(127);+ }+ client = socket(AF_INET, SOCK_STREAM, 0);+ if (client < 0 || connect(client, (struct sockaddr *)&addr, sizeof(addr)))+ return 1;+ close(client);+ if (waitpid(child, &status, 0) != child)+ return 1;+ close(listener);+ unlink(acceptor);+ return !WIFEXITED(status) || WEXITSTATUS(status);+}+static int parse_policy(const char *text, __u64 *value){char *end;@@ -266,6 +408,14 @@ int main(int argc, char **argv)return check_policy_env();if (argc >= 2 && !strcmp(argv[1], "fork-exec-policy"))return fork_exec_policy_env();+ if (argc >= 3 && !strcmp(argv[1], "socket-inherit")) {+ __u64 extra;++ extra = 2;+ return socket_inheritance(argv[2], extra);+ }+ if (argc >= 2 && !strcmp(argv[1], "accept-child"))+ return accept_child();if (argc < 3)return 2;if (!strcmp(argv[1], "unlink"))diff --git a/util/image_guards.c b/util/image_guards.cindex 66ef4805..22df4a1d 100644--- a/util/image_guards.c+++ b/util/image_guards.c@@ -27,6 +27,7 @@const struct image_guardshirudo_seal_guards[SEAL_HOOK_COUNT] = {+ { "shirudo_seal_socket_post_create", SEAL_SOCKET_POST_CREATE_PIN },{ "shirudo_seal_bprm_creds_for_exec", SEAL_BPRM_CREDS_PIN },{ "shirudo_seal_bprm_check_security",SEAL_BPRM_CHECK_PIN },@@ -149,6 +150,11 @@ const struct guard_map shirudo_guard_maps[GUARD_MAP_COUNT] = {BPF_MAP_TYPE_TASK_STORAGE, sizeof(__u32),sizeof(struct shirudo_task_policy), 0, BPF_F_NO_PREALLOC,},+ [GUARD_SOCKET_POLICY] = {+ "socket_policy_map", SOCKET_POLICY_PIN,+ BPF_MAP_TYPE_SK_STORAGE, sizeof(__u32), sizeof(__u64), 0,+ BPF_F_NO_PREALLOC | BPF_F_CLONE,+ },[GUARD_DEVICES] = {"shirudo_devices", BPFFS_DIR "/devices",BPF_MAP_TYPE_HASH, sizeof(__u64),diff --git a/util/image_guards.h b/util/image_guards.hindex b5900ab6..232d1ebc 100644--- a/util/image_guards.h+++ b/util/image_guards.h@@ -8,7 +8,7 @@#include <stdint.h>-#define SEAL_HOOK_COUNT 21+#define SEAL_HOOK_COUNT 22#define IMAGE_SEAL_HOOK_COUNT 2#define MOUNT_HOOK_COUNT 5#define OBSERVER_HOOK_COUNT 9@@ -37,6 +37,7 @@ enum guard_map_index {GUARD_ROOTS,GUARD_TAMPER,GUARD_POLICY,+ GUARD_SOCKET_POLICY,GUARD_MAP_COUNT,};diff --git a/util/paths.h b/util/paths.hindex c5b9c6b4..286f872a 100644--- a/util/paths.h+++ b/util/paths.h@@ -100,6 +100,9 @@#define IDENTITY_PIN BPFFS_DIR "/identity"#define TAMPER_PIN BPFFS_DIR "/tamper"#define POLICY_PIN BPFFS_DIR "/policy_map"+#define SEAL_SOCKET_POST_CREATE_PIN \+ BPFFS_DIR "/seal_socket_post_create_link"+#define SOCKET_POLICY_PIN BPFFS_DIR "/socket_policy_map"#define RUNTIME_DIR RUN_DIR "/runtime"#define CONFIG_IMAGE_DIR RUN_DIR "/config"