eBPF-based security platform
bpf: restrict PR_SET_MM to Shirudo callers
John Fastabend90b4f144e8f7
diff --git a/Documentation/architecture.rst b/Documentation/architecture.rstindex 8a63ce45..3523c0d0 100644--- a/Documentation/architecture.rst+++ b/Documentation/architecture.rst@@ -240,14 +240,13 @@ Everything else, including``lsm/bpf_map`` denies writable map file descriptors when the caller'sexecutable identity is not shirudo. Root can retrieve read-only map fdsfor observation, but direct map updates and writable mappings of arena-maps are denied. The ``task_prctl`` and ``file_permission`` hooks scope-the kernel's executable-file check during ``PR_SET_MM_EXE_FILE`` and-``PR_SET_MM_MAP``. They deny only when the resolved file matches the-device and inode of Shirudo's frozen executable identity, including the-initrd copy. Other ``PR_SET_MM`` targets and ordinary executable-file-checks are unaffected. A process that directly executes the recorded-Shirudo binary is still identified as Shirudo; its command interface is-the authorization boundary for those operations.+maps are denied. The ``task_prctl`` hook denies+``PR_SET_MM_EXE_FILE`` and ``PR_SET_MM_MAP`` to callers whose executable+identity is not the frozen Shirudo executable or its initrd copy. This+also denies these two operations to checkpoint and restore tools. A+process that directly executes the recorded Shirudo binary is still+identified as Shirudo; its command interface is the authorization+boundary for those operations.``lsm/bpf_prog_load`` runs after the kernel's signature check and admitsa program only when the verdict is ``BPF_SIG_VERIFIED`` and thediff --git a/Documentation/requirements.rst b/Documentation/requirements.rstindex b2402dbd..85ef27a5 100644--- a/Documentation/requirements.rst+++ b/Documentation/requirements.rst@@ -56,11 +56,6 @@ fixture.``ENOTSUPP``. Either way ``shirudo init`` cannot attach itsenforcement. The same trampolines later carry ``fentry``,``fexit``, ``fmod_ret`` and ``lsm`` upatches at no extra cost.- * - ``CONFIG_FTRACE_SYSCALLS``- - Provides the ``syscalls/sys_exit_prctl`` tracepoint used by- shirudo's ``PR_SET_MM`` identity guard. Without it,- ``shirudo init`` cannot attach the cleanup hook and does not- complete.* - ``CONFIG_SECURITYFS``- ``/sys/kernel/security/lsm``, which the pre-flight reads toconfirm the BPF LSM is active (``bpf_lsm_active``).diff --git a/Documentation/threat-model.rst b/Documentation/threat-model.rstindex 6b7c9906..67d23c80 100644--- a/Documentation/threat-model.rst+++ b/Documentation/threat-model.rst@@ -266,12 +266,13 @@ Each of these is known, documented and bounded; none is silent.the compare runs against signed ``.rodata``.2. ``CAP_SYS_RESOURCE`` can rewrite ``mm->exe_file``. Shirudo's- ``task_prctl`` and ``file_permission`` LSM hooks deny- ``PR_SET_MM_EXE_FILE`` and ``PR_SET_MM_MAP`` when the resolved file- is either frozen Shirudo executable inode, while leaving other- targets alone. A process that directly executes the recorded- Shirudo binary is still identified as Shirudo, so its command- interface remains the authorization boundary. Bound+ ``task_prctl`` LSM hook denies ``PR_SET_MM_EXE_FILE`` and+ ``PR_SET_MM_MAP`` to every caller outside the frozen Shirudo+ executable identity, preventing a root process from impersonating+ Shirudo through these operations. This also denies those two+ operations to checkpoint and restore tools. A process that directly+ executes the recorded Shirudo binary is still identified as Shirudo,+ so its command interface remains the authorization boundary. Bound``CAP_SYS_RESOURCE`` out of untrusted contexts with``CapabilityBoundingSet=`` and container capability profiles.diff --git a/bpf/bpf2.c b/bpf/bpf2.cindex 7041ca8f..0c5d896d 100644--- a/bpf/bpf2.c+++ b/bpf/bpf2.c@@ -44,9 +44,9 @@* while letting `shirudo upatch unload` remove* a pin to detach a program. The exe-inode check* is the interim of the xattr-based identity- * seal will use. The task_prctl and- * file_permission hooks below prevent changing- * mm->exe_file to either recorded Shirudo inode.+ * seal will use. The task_prctl hook below+ * prevents non-Shirudo callers from changing+ * mm->exe_file through PR_SET_MM.* lsm/key_permission -- guards the trust anchor in the kernel's* keyrings: the sealed keyring init froze and the* keys it holds may be viewed and searched by@@ -76,12 +76,8 @@* the pinned links and detaches ALL enforcement,* since shirudo is agentless and the pins are the* links' only reference. See the hook block below.- * lsm/task_prctl,- * lsm/file_permission,- * tracepoint/sys_exit_prctl -- scope PR_SET_MM executable changes and deny- * only when the resolved file is one of the frozen- * Shirudo executable inodes. Task storage is cleared- * at syscall exit when an earlier kernel check fails.+ * lsm/task_prctl -- deny PR_SET_MM executable changes to callers+ * outside the frozen Shirudo executable identity.*/#include "vmlinux.h"@@ -104,10 +100,6 @@#ifndef FMODE_WRITE#define FMODE_WRITE 0x2#endif-#ifndef MAY_EXEC-#define MAY_EXEC 0x1-#endif-#define PR_SET_MM 35#define PR_SET_MM_EXE_FILE 13#define PR_SET_MM_MAP 14@@ -145,17 +137,6 @@ struct {__type(value, struct shirudo_sb_id);} shirudo_bpffs_sb SEC(".maps");-struct shirudo_prctl_state {- __u32 pending;-};--struct {- __uint(type, BPF_MAP_TYPE_TASK_STORAGE);- __uint(map_flags, BPF_F_NO_PREALLOC);- __type(key, int);- __type(value, struct shirudo_prctl_state);-} shirudo_prctl_state_map SEC(".maps");-static __always_inline boolshirudo_task_is_self(const struct shirudo_sb_id *id);@@ -361,34 +342,13 @@ static __always_inline bool shirudo_task_is_self(const struct shirudo_sb_id *id)exe_ino == id->alt_exe_ino;}-static __always_inline bool shirudo_file_is_self(- const struct shirudo_sb_id *id,- struct file *file)-{- struct inode *inode;- __u32 dev;- __u64 ino;-- if (!file)- return false;- inode = BPF_CORE_READ(file, f_inode);- if (!inode)- return false;- dev = BPF_CORE_READ(inode, i_sb, s_dev);- ino = BPF_CORE_READ(inode, i_ino);- if (dev == id->exe_dev && ino == id->exe_ino)- return true;- return id->alt_exe_ino && dev == id->alt_exe_dev &&- ino == id->alt_exe_ino;-}-SEC("lsm/task_prctl")int BPF_PROG(shirudo_lsm_task_prctl, int option, unsigned long arg2,unsigned long arg3, unsigned long arg4, unsigned long arg5,int ret){- struct task_struct *task = bpf_get_current_task_btf();- struct shirudo_prctl_state *state;+ struct shirudo_sb_id *id;+ __u32 zero = 0;(void)arg3;(void)arg4;@@ -396,47 +356,12 @@ int BPF_PROG(shirudo_lsm_task_prctl, int option, unsigned long arg2,if ((ret && ret != -ENOSYS) || option != PR_SET_MM ||(arg2 != PR_SET_MM_EXE_FILE && arg2 != PR_SET_MM_MAP))return ret ? ret : -ENOSYS;- state = bpf_task_storage_get(&shirudo_prctl_state_map, task, NULL,- BPF_LOCAL_STORAGE_GET_F_CREATE);- if (!state)- return -ENOMEM;- state->pending = 1;- return -ENOSYS;-}--SEC("lsm/file_permission")-int BPF_PROG(shirudo_lsm_file_permission, struct file *file, int mask,- int ret)-{- struct task_struct *task = bpf_get_current_task_btf();- struct shirudo_prctl_state *state;- struct shirudo_sb_id *id;- __u32 zero = 0;-- if (ret || !(mask & MAY_EXEC))- return ret;- state = bpf_task_storage_get(&shirudo_prctl_state_map, task, NULL, 0);- if (!state || !state->pending)- return ret;- state->pending = 0;- bpf_task_storage_delete(&shirudo_prctl_state_map, task);id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);if (!id)return -EACCES;- return shirudo_file_is_self(id, file) ? -EPERM : 0;-}--SEC("tracepoint/syscalls/sys_exit_prctl")-int shirudo_trace_sys_exit_prctl(struct trace_event_raw_sys_exit *ctx)-{- struct task_struct *task = bpf_get_current_task_btf();- struct shirudo_prctl_state *state;-- (void)ctx;- state = bpf_task_storage_get(&shirudo_prctl_state_map, task, NULL, 0);- if (state)- bpf_task_storage_delete(&shirudo_prctl_state_map, task);- return 0;+ if (!shirudo_task_is_self(id))+ return -EPERM;+ return -ENOSYS;}/*diff --git a/builtin-init.c b/builtin-init.cindex 5c6ac753..1f1756fb 100644--- a/builtin-init.c+++ b/builtin-init.c@@ -746,10 +746,6 @@ static int init_lsm(bool attach_filesystem_seal)const char *prog;const char *pin;} prctl_hooks[] = {- { "shirudo_lsm_file_permission",- SHIRUDO_FILE_PERMISSION_LINK_PIN },- { "shirudo_trace_sys_exit_prctl",- SHIRUDO_PRCTL_EXIT_LINK_PIN },{ "shirudo_lsm_task_prctl", SHIRUDO_TASK_PRCTL_LINK_PIN },};struct bpf_link *prctl_links[ARRAY_SIZE(prctl_hooks)] = {};diff --git a/contrib/build-kernel-package.sh b/contrib/build-kernel-package.shindex 9c301434..0c36b11b 100755--- a/contrib/build-kernel-package.sh+++ b/contrib/build-kernel-package.sh@@ -496,7 +496,6 @@ require_config CONFIG_BPF_LSM yrequire_config CONFIG_BPF_SYSCALL yrequire_config CONFIG_BPF_JIT yrequire_config CONFIG_CGROUP_BPF y-require_config CONFIG_FTRACE_SYSCALLS yrequire_config CONFIG_FUNCTION_TRACER yrequire_config CONFIG_DYNAMIC_FTRACE yrequire_config CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS ydiff --git a/testing/tamperproof/t1131-pr-set-mm-identity.py b/testing/tamperproof/t1131-pr-set-mm-identity.pyindex 57e01aeb..fcb853e5 100644--- a/testing/tamperproof/t1131-pr-set-mm-identity.py+++ b/testing/tamperproof/t1131-pr-set-mm-identity.py@@ -143,7 +143,7 @@ def child_succeeds(fn):return os.WIFEXITED(status) and os.WEXITSTATUS(status) == 0-def exe_file_result(path, expect_denied):+def exe_file_result(path):before = file_identity("/proc/self/exe")fd = os.open(path, os.O_RDONLY | os.O_CLOEXEC)try:@@ -151,16 +151,11 @@ def exe_file_result(path, expect_denied):finally:os.close(fd)after = file_identity("/proc/self/exe")- if expect_denied:- assert result == -1 and err in (errno.EPERM, errno.EBUSY), \- (result, err)- assert after == before, (before, after)- else:- assert result == 0, (result, err)- assert after == file_identity(path), (after, file_identity(path))+ assert result == -1 and err == errno.EPERM, (result, err)+ assert after == before, (before, after)-def mm_map_result(path, expect_denied):+def mm_map_result(path):before = file_identity("/proc/self/exe")fd = os.open(path, os.O_RDONLY | os.O_CLOEXEC)try:@@ -171,29 +166,25 @@ def mm_map_result(path, expect_denied):finally:os.close(fd)after = file_identity("/proc/self/exe")- if expect_denied:- assert result == -1 and err == errno.EPERM, (result, err)- assert after == before, (before, after)- else:- assert result == 0, (result, err)- assert after == file_identity(path), (after, file_identity(path))+ assert result == -1 and err == errno.EPERM, (result, err)+ assert after == before, (before, after)assert_frozen_executable_identity()-assert child_succeeds(lambda: exe_file_result(SHIRUDO, True)), \- "PR_SET_MM_EXE_FILE did not reject Shirudo's executable"-assert child_succeeds(lambda: exe_file_result(OTHER, False)), \- "PR_SET_MM_EXE_FILE rejected an unrelated executable"-assert child_succeeds(lambda: mm_map_result(SHIRUDO, True)), \- "PR_SET_MM_MAP did not reject Shirudo's executable"-assert child_succeeds(lambda: mm_map_result(OTHER, False)), \- "PR_SET_MM_MAP rejected an unrelated executable"+assert child_succeeds(lambda: exe_file_result(SHIRUDO)), \+ "PR_SET_MM_EXE_FILE was allowed for a non-Shirudo caller"+assert child_succeeds(lambda: exe_file_result(OTHER)), \+ "PR_SET_MM_EXE_FILE accepted an unrelated executable"+assert child_succeeds(lambda: mm_map_result(SHIRUDO)), \+ "PR_SET_MM_MAP was allowed for a non-Shirudo caller"+assert child_succeeds(lambda: mm_map_result(OTHER)), \+ "PR_SET_MM_MAP accepted an unrelated executable"def invalid_fd_then_exec():result, err = call_prctl(PR_SET_MM, PR_SET_MM_EXE_FILE, 1 << 30)- assert result == -1 and err == errno.EBADF, (result, err)+ assert result == -1 and err == errno.EPERM, (result, err)os.execve(SHIRUDO, [SHIRUDO, "status", "--json"], os.environ.copy())diff --git a/testing/tamperproof/t1131-pr-set-mm-identity.sh b/testing/tamperproof/t1131-pr-set-mm-identity.shindex 11d6e1a3..0e3e1bca 100644--- a/testing/tamperproof/t1131-pr-set-mm-identity.sh+++ b/testing/tamperproof/t1131-pr-set-mm-identity.sh@@ -2,8 +2,8 @@# SPDX-License-Identifier: BSD-2-Clause# Copyright (c) 2026 Authors of shirudo#-# t1131 -- PR_SET_MM cannot assign Shirudo's frozen executable identity,-# while unrelated executable-file changes retain normal kernel behavior.+# t1131 -- non-Shirudo callers cannot change executable identity with+# PR_SET_MM.cd "$(dirname "$0")/..". ./integration-lib.sh@@ -17,7 +17,7 @@ caps = next(line.split()[1] for line in status if line.startswith("CapEff:"))sys.exit(0 if int(caps, 16) & (1 << 24) else 1)' || test_skip_all "CAP_SYS_RESOURCE unavailable"-test_expect_success 'PR_SET_MM guards only the recorded Shirudo executable' '+test_expect_success 'PR_SET_MM executable changes require Shirudo identity' 'python3 tamperproof/t1131-pr-set-mm-identity.py "$SHIRUDO_BIN"'diff --git a/testing/tamperproof/test.plan.md b/testing/tamperproof/test.plan.mdindex 643e2fcb..482f16e3 100644--- a/testing/tamperproof/test.plan.md+++ b/testing/tamperproof/test.plan.md@@ -38,7 +38,7 @@ checks that Shirudo can still export the pre-existing Python exec record.| TP-16 | Create a BPF map from a non-Shirudo executable | Not covered | None in tamperproof | Add direct negative test for `BPF_MAP_CREATE`; cover authorized Shirudo loader creation as positive control. || TP-17 | Create links or pin existing objects from root | Not covered | None | `BPF_LINK_CREATE` and `BPF_OBJ_PIN` are allowed. Test whether root can create an unwanted attachment or pin and whether the resulting object affects enforcement/state. || TP-18 | Detach/update links or attach/detach programs from root | Not covered | None | Add negative raw-syscall cases for `BPF_LINK_DETACH`, `BPF_LINK_UPDATE`, `BPF_PROG_ATTACH`, and `BPF_PROG_DETACH`. |-| TP-19 | Root assigns Shirudo's executable identity using `PR_SET_MM` | Covered | `t1131-pr-set-mm-identity.sh` and `t1131-pr-set-mm-identity.py` | Requires `CAP_SYS_RESOURCE`; preserve allowed unrelated-executable control. |+| TP-19 | Root assigns Shirudo's executable identity using `PR_SET_MM` | Covered | `t1131-pr-set-mm-identity.sh` and `t1131-pr-set-mm-identity.py` | Requires `CAP_SYS_RESOURCE`; verify both `PR_SET_MM_EXE_FILE` and `PR_SET_MM_MAP` are denied to non-Shirudo callers. || TP-20 | Obtain or transfer Shirudo's open map FDs via `/proc/<pid>/fd`, `pidfd_getfd`, or `SCM_RIGHTS` | Not covered | None | Test each applicable acquisition/transfer route and verify a received FD cannot be used to mutate protected maps. || TP-21 | Concurrent or repeated serializer invocation changes/loses records | Not covered | None | Add after TP-14 policy fix; exercise repeated and concurrent export attempts and verify record integrity. |diff --git a/util/paths.h b/util/paths.hindex 2054ad6a..cbc4f3fa 100644--- a/util/paths.h+++ b/util/paths.h@@ -66,9 +66,6 @@#define SHIRUDO_INODE_RMXATTR_LINK_PIN SHIRUDO_BPFFS_DIR "/bpf2_inode_removexattr_link"#define SHIRUDO_KEY_PERMISSION_LINK_PIN SHIRUDO_BPFFS_DIR "/bpf2_key_permission_link"#define SHIRUDO_TASK_PRCTL_LINK_PIN SHIRUDO_BPFFS_DIR "/bpf2_task_prctl_link"-#define SHIRUDO_FILE_PERMISSION_LINK_PIN \- SHIRUDO_BPFFS_DIR "/bpf2_file_permission_link"-#define SHIRUDO_PRCTL_EXIT_LINK_PIN SHIRUDO_BPFFS_DIR "/bpf2_prctl_exit_link"#define SHIRUDO_SEAL_FILE_OPEN_PIN SHIRUDO_BPFFS_DIR "/seal_file_open_link"#define SHIRUDO_SEAL_BPRM_CHECK_PIN SHIRUDO_BPFFS_DIR "/seal_bprm_check_security_link"#define SHIRUDO_SEAL_BPRM_COMMITTED_PIN SHIRUDO_BPFFS_DIR "/seal_bprm_committed_creds_link"