eBPF-based security platform
seal: refuse CAP_PERFMON's read of a command's memory files
Daniel Borkmann9b4762b32b18
diff --git a/Documentation/contribute/arch-support.rst b/Documentation/contribute/arch-support.rstindex c52d3a58..00ef6215 100644--- a/Documentation/contribute/arch-support.rst+++ b/Documentation/contribute/arch-support.rst@@ -49,9 +49,9 @@ Comparison``-mbranch-protection=standard``- None* - Carried kernel patches- - ``kernel/0001`` to ``kernel/0004``- - ``kernel/0001`` to ``kernel/0004``- - ``kernel/0001`` to ``kernel/0004``+ - ``kernel/0001`` to ``kernel/0005``+ - ``kernel/0001`` to ``kernel/0005``+ - ``kernel/0001`` to ``kernel/0005``* - BPF trampolines (``lsm``, ``fentry``, ``fexit``, ``fmod_ret``)- 36 programs per function- 37 programs per function@@ -152,7 +152,7 @@ shirudoLinux kernel~~~~~~~~~~~~-- Get ``kernel/0001`` to ``kernel/0004`` upstream. None of them is in+- Get ``kernel/0001`` to ``kernel/0005`` upstream. None of them is inbpf-next; :doc:`/internals/kernel-patches` describes each.- riscv64: BPF trampolines on a kernel built with ``CONFIG_CFI``.- riscv64: arenas on CPUs without Zacas.diff --git a/Documentation/internals/enforcement.rst b/Documentation/internals/enforcement.rstindex 8a78bfa6..fb8008d7 100644--- a/Documentation/internals/enforcement.rst+++ b/Documentation/internals/enforcement.rst@@ -513,6 +513,13 @@ when the record it needs is missing.- ``EACCES``- ``SealPtrace``, attach mode only- Closed.+ * - ``lsm/mm_read_access`` (``seal``)+ - A read-only access to a marked task's memory that+ ``mm_access()`` would grant ``CAP_PERFMON`` after the ptrace check+ refused it: ``environ``, ``maps``, ``smaps``, ``pagemap``.+ - ``EACCES``+ - None+ - Closed, with carried patch 0005.* - ``lsm/perf_event_alloc`` (``seal``)- A perf event bound to a marked task. CPU-wide events have no taskand are not checked.diff --git a/Documentation/internals/kernel-interfaces.rst b/Documentation/internals/kernel-interfaces.rstindex 7f440f03..84aa81f6 100644--- a/Documentation/internals/kernel-interfaces.rst+++ b/Documentation/internals/kernel-interfaces.rst@@ -58,7 +58,8 @@ bpf-next contains the signed-loader ABI at merge ``0867b7bfa9de``.later. shirudo defines ``KEY_SPEC_DM_VERITY_KEYRING`` (-10) itself whenthe UAPI headers lack it. The ``tpm_command`` LSM hook, the label readswithout the calling task's permission, the sleepable form of six inode-hooks and the trusted ``d_parent`` are not in bpf-next;+hooks, the trusted ``d_parent`` and the ``mm_read_access`` LSM hook are+not in bpf-next;:doc:`kernel-patches` lists the patches the test kernel carries. Akernel is admitted by the ABI check below, not by its release string.@@ -282,6 +283,9 @@ LSM hooks* - ``tpm_command``- ``bpf2``- Carried patch+ * - ``mm_read_access``+ - ``seal``+ - Carried patch* - ``task_prctl``, ``bprm_check_security``, ``inode_unlink``,``inode_rmdir``, ``inode_mkdir``, ``inode_symlink``,``inode_link``, ``inode_rename``, ``inode_setxattr``,diff --git a/Documentation/internals/kernel-patches.rst b/Documentation/internals/kernel-patches.rstindex f895fd6f..8812fa7d 100644--- a/Documentation/internals/kernel-patches.rst+++ b/Documentation/internals/kernel-patches.rst@@ -38,6 +38,10 @@ Summary- Daniel Borkmann- No- All+ * - ``0005-mm-Let-LSMs-check-read-only-remote-mm-access-under-CAP_PERFMON.patch``+ - Daniel Borkmann+ - No+ - All"In bpf-next" was checked against the bpf-next tree at commit``57fae4059380``: ``include/linux/lsm_hook_defs.h`` there has no@@ -45,8 +49,9 @@ Summaryread permission before it returns a ``security.bpf.`` xattr to an LSMprogram, the sleepable hook set of ``kernel/bpf/bpf_lsm.c`` lacks``inode_mkdir``, ``inode_link``, ``inode_set_acl``, ``inode_remove_acl``,-``inode_file_setattr`` and ``inode_copy_up``, and the verifier trusts-``d_inode`` of a dentry but not ``d_parent``.+``inode_file_setattr`` and ``inode_copy_up``, the verifier trusts+``d_inode`` of a dentry but not ``d_parent``, and ``may_access_mm()``+grants a read-mode access to ``CAP_PERFMON`` without asking an LSM.The x86 ftrace fix the tree used to carry is upstream as``31ac959c915c`` ("x86/ftrace: Mark jmp mode by clearing bit 63 of@@ -202,6 +207,40 @@ Without it``seal`` object fails to load and ``init`` fails at "loadingenforcement", with the result the TPM command hook describes.+Read-only mm access hook+------------------------++Subject+ mm: Let LSMs check read-only remote mm access under CAP_PERFMON++Files+ ``kernel/fork.c``, ``include/linux/lsm_hook_defs.h``,+ ``include/linux/security.h``, ``security/security.c``.++What it changes+ ``may_access_mm()``, which ``mm_access()`` asks before it hands out+ another task's memory, grants a read-mode access to a caller with+ ``CAP_PERFMON`` or ``CAP_SYS_ADMIN`` after ``ptrace_may_access()``+ refused it, since commit ``0c555a3c1bc9`` ("mm,procfs: allow+ read-only remote mm access under CAP_PERFMON"). The patch has it+ call a new hook, ``security_mm_read_access(task, mode)``, before+ it grants the access that way, and refuse it when the hook does.+ An access ``ptrace_may_access()`` allows is not checked again.++Why shirudo needs it+ The seal refuses every ptrace mode against a task with runtime+ authority, and ``ptrace_may_access()`` asks it, but the perfmon path+ comes after that refusal: root opened ``/proc/<pid>/environ``,+ ``maps``, ``smaps``, ``numa_maps``, ``pagemap`` and ``auxv`` of a+ running command, its environment and the layout of its memory. The+ seal attaches ``lsm/mm_read_access`` and refuses a marked task there+ too.++Without it+ The ``seal`` object fails to attach the hook, and ``init`` fails at+ "loading enforcement", with the result the TPM command hook+ describes.+Related-------diff --git a/Documentation/internals/seal.rst b/Documentation/internals/seal.rstindex 841c27a1..25968d22 100644--- a/Documentation/internals/seal.rst+++ b/Documentation/internals/seal.rst@@ -157,7 +157,10 @@ task. It counts one whose mode includes ``PTRACE_MODE_ATTACH``(``SealPtrace``), which covers attach, ``/proc/<pid>/mem``,``pidfd_getfd()`` and ``process_vm_readv()``, and refuses a read-modeone uncounted, because journald reads ``/proc/<pid>/exe`` of every-process it logs for and a counted read would rise on an honest node. ``lsm/perf_event_alloc``+process it logs for and a counted read would rise on an honest node.+``lsm/mm_read_access``, a hook of carried patch 0005, refuses the+read-only access ``mm_access()`` grants ``CAP_PERFMON`` after that+refusal, to ``environ``, ``maps`` and the other memory files. ``lsm/perf_event_alloc``refuses a perf event bound to a marked task (``SealPerf``).``lsm/task_kill`` lets a signal to a marked task through, since rootmay end a command that hangs and systemd stops the runs its timersdiff --git a/Documentation/operate/plan/system-requirements.rst b/Documentation/operate/plan/system-requirements.rstindex d80a14c6..500c7d19 100644--- a/Documentation/operate/plan/system-requirements.rst+++ b/Documentation/operate/plan/system-requirements.rst@@ -96,6 +96,11 @@ Kernel sourceThe seal reads the label of the directory a new entry goes intofrom it; without the patch the verifier refuses the seal's entryhooks.+ * - :src:`kernel/0005-mm-Let-LSMs-check-read-only-remote-mm-access-under-CAP_PERFMON.patch`+ - Adds the ``mm_read_access`` LSM hook, which ``mm_access()`` asks+ before it grants a read-only access to another task's memory to+ ``CAP_PERFMON``. The seal refuses a task with runtime authority+ there, and ``init`` fails without the hook.:doc:`/internals/kernel-patches` gives the upstream status of thecarried patches.diff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rstindex 73d05ba2..f48ce2b3 100644--- a/Documentation/security/threat-model.rst+++ b/Documentation/security/threat-model.rst@@ -157,15 +157,16 @@ without the protection.task's read permission, sleepable LSM programs on``inode_mkdir``, ``inode_link``, ``inode_set_acl``,``inode_remove_acl``, ``inode_file_setattr`` and- ``inode_copy_up``, and a ``dentry->d_parent`` the verifier- trusts. Upstream has none of these, and shirudo carries them as+ ``inode_copy_up``, a ``dentry->d_parent`` the verifier trusts,+ and the ``mm_read_access`` LSM hook. Upstream has none of these,+ and shirudo carries them as:src:`kernel/0001-tpm-Let-LSMs-inspect-userspace-commands.patch`,:src:`kernel/0002-bpf-Read-security.bpf-xattrs-for-LSM-programs-without-the-caller-s-permission.patch`,- :src:`kernel/0003-bpf-Let-sleepable-LSM-programs-attach-to-the-remaining-inode-hooks.patch`- and- :src:`kernel/0004-bpf-Treat-dentry-d_parent-as-a-trusted-pointer.patch`.+ :src:`kernel/0003-bpf-Let-sleepable-LSM-programs-attach-to-the-remaining-inode-hooks.patch`,+ :src:`kernel/0004-bpf-Treat-dentry-d_parent-as-a-trusted-pointer.patch`+ and :src:`kernel/0005-mm-Let-LSMs-check-read-only-remote-mm-access-under-CAP_PERFMON.patch`.- T1 needs the ABI, T5 the hook, T3 the label read, the sleepable- hooks and the trusted parent.+ hooks, the trusted parent and the read-only mm access hook.- Partly. The pre-flight check ``signed_loader_kernel_abi`` teststhe ABI. Without the hook the ``bpf2`` object does not load, andwithout the sleepable hooks and the trusted parent the ``seal``@@ -843,7 +844,9 @@ An authorized process is protected against being taken over:``/proc/<pid>/mem``, ``pidfd_getfd(2)`` and ``process_vm_readv(2)``(``SealPtrace``), and refuses a read-mode one, which covers``/proc/<pid>/exe``, ``environ``, ``maps`` and the ``fd`` links,- without a counter.+ without a counter. ``lsm/mm_read_access`` refuses the read-only+ access ``mm_access()`` would otherwise grant ``CAP_PERFMON`` past that+ refusal (carried patch 0005, A3).- ``lsm/perf_event_alloc`` refuses a perf event bound to a marked task(``SealPerf``). Events on all tasks of a CPU, and events opened on atask before the exec that marks it, are not checked (D9).@@ -899,8 +902,8 @@ An authorized process is protected against being taken over:``lsm/key_permission``,``lsm.s/bprm_check_security``, ``lsm.s/bprm_committed_creds``,``lsm/task_alloc``, ``lsm/bprm_check_security``,- ``lsm/ptrace_access_check``, ``lsm/perf_event_alloc``,- ``lsm/task_setrlimit``,+ ``lsm/ptrace_access_check``, ``lsm/mm_read_access``,+ ``lsm/perf_event_alloc``, ``lsm/task_setrlimit``,``lsm/task_prctl``, ``lsm/mmap_file``, ``lsm/file_mprotect``; insupport, ``lsm.s/bprm_creds_for_exec`` and ``lsm/socket_post_create``carry the policy bits, ``lsm.s/inode_post_setxattr`` anddiff --git a/bpf/seal.c b/bpf/seal.cindex 1b91059c..53856d68 100644--- a/bpf/seal.c+++ b/bpf/seal.c@@ -272,6 +272,21 @@ int BPF_PROG(shirudo_seal_ptrace_access_check,return -EACCES;}+/*+ * mm_access() grants a read-mode access to CAP_PERFMON after the check+ * above refused it, and asks here first (carried patch 0005): the+ * environment, memory map and page map of a marked task stay its own,+ * refused as uncounted as any read mode.+ */+SEC("lsm/mm_read_access")+int BPF_PROG(shirudo_seal_mm_read_access, struct task_struct *task,+ unsigned int mode, int ret)+{+ if (ret)+ return ret;+ return shirudo_marked(task) ? -EACCES : 0;+}+/** A signal from outside is let through: root may end a command that* hangs, and systemd stops the runs its timers start. It is counted, sodiff --git a/kernel/0005-mm-Let-LSMs-check-read-only-remote-mm-access-under-CAP_PERFMON.patch b/kernel/0005-mm-Let-LSMs-check-read-only-remote-mm-access-under-CAP_PERFMON.patchnew file mode 100644index 00000000..850b0a45--- /dev/null+++ b/kernel/0005-mm-Let-LSMs-check-read-only-remote-mm-access-under-CAP_PERFMON.patch@@ -0,0 +1,113 @@+From 94eb18b95c122ad0747d3c4030dce92c65ce9a97 Mon Sep 17 00:00:00 2001+From: Daniel Borkmann <[email protected]>+Date: Tue, 17 Nov 2026 12:20:31 +0000+Subject: [PATCH] mm: Let LSMs check read-only remote mm access under+ CAP_PERFMON++Since commit 0c555a3c1bc9 ("mm,procfs: allow read-only remote mm access+under CAP_PERFMON"), mm_access() grants a read-mode access to a caller+with CAP_PERFMON or CAP_SYS_ADMIN after ptrace_may_access() refused it.+The refusal may have come from an LSM's ptrace_access_check hook, which+then has no say: /proc/<pid>/environ, maps, smaps, numa_maps, pagemap+and auxv of a task the LSM keeps others away from open all the same.++Shirudo marks the processes that run its runtime binary and refuses+every ptrace access to them from tasks without its authority. With the+perfmon path, root reads their environment and their memory layout.++Call a new mm_read_access hook before mm_access() grants a read-mode+access that way. It sees the target task and the PTRACE_MODE flags of+the access, as ptrace_access_check does, and is consulted only on the+CAP_PERFMON path, so an access ptrace_may_access() allows is not+checked twice.++Signed-off-by: Daniel Borkmann <[email protected]>+---+ include/linux/lsm_hook_defs.h | 1 ++ include/linux/security.h | 7 ++++++++ kernel/fork.c | 3 ++-+ security/security.c | 17 ++++++++++++++++++ 4 files changed, 27 insertions(+), 1 deletion(-)++diff --git a/include/linux/lsm_hook_defs.h b/include/linux/lsm_hook_defs.h+index 90209bf5cf6f..c52571d8ee65 100644+--- a/include/linux/lsm_hook_defs.h++++ b/include/linux/lsm_hook_defs.h+@@ -36,6 +36,7 @@ LSM_HOOK(int, 0, binder_transfer_file, const struct cred *from,+ LSM_HOOK(int, 0, ptrace_access_check, struct task_struct *child,+ unsigned int mode)+ LSM_HOOK(int, 0, ptrace_traceme, struct task_struct *parent)++LSM_HOOK(int, 0, mm_read_access, struct task_struct *task, unsigned int mode)+ LSM_HOOK(int, 0, capget, const struct task_struct *target, kernel_cap_t *effective,+ kernel_cap_t *inheritable, kernel_cap_t *permitted)+ LSM_HOOK(int, 0, capset, struct cred *new, const struct cred *old,+diff --git a/include/linux/security.h b/include/linux/security.h+index f017412d5adb..e44a065b1145 100644+--- a/include/linux/security.h++++ b/include/linux/security.h+@@ -340,6 +340,7 @@ int security_binder_transfer_file(const struct cred *from,+ const struct cred *to, const struct file *file);+ int security_ptrace_access_check(struct task_struct *child, unsigned int mode);+ int security_ptrace_traceme(struct task_struct *parent);++int security_mm_read_access(struct task_struct *task, unsigned int mode);+ int security_capget(const struct task_struct *target,+ kernel_cap_t *effective,+ kernel_cap_t *inheritable,+@@ -687,6 +688,12 @@ static inline int security_ptrace_traceme(struct task_struct *parent)+ return cap_ptrace_traceme(parent);+ }+++static inline int security_mm_read_access(struct task_struct *task,++ unsigned int mode)++{++ return 0;++}+++ static inline int security_capget(const struct task_struct *target,+ kernel_cap_t *effective,+ kernel_cap_t *inheritable,+diff --git a/kernel/fork.c b/kernel/fork.c+index 8b108ed37152..f8da387a126e 100644+--- a/kernel/fork.c++++ b/kernel/fork.c+@@ -1402,7 +1402,8 @@ static bool may_access_mm(struct mm_struct *mm, struct task_struct *task, unsign+ return true;+ if (ptrace_may_access(task, mode))+ return true;+- if ((mode & PTRACE_MODE_READ) && perfmon_capable())++ if ((mode & PTRACE_MODE_READ) && perfmon_capable() &&++ !security_mm_read_access(task, mode))+ return true;+ return false;+ }+diff --git a/security/security.c b/security/security.c+index 86636c894ebe..45869f746bcb 100644+--- a/security/security.c++++ b/security/security.c+@@ -609,6 +609,23 @@ int security_ptrace_traceme(struct task_struct *parent)+ return call_int_hook(ptrace_traceme, parent);+ }+++/**++ * security_mm_read_access() - Check a read-only access granted by CAP_PERFMON++ * @task: task whose memory is accessed++ * @mode: PTRACE_MODE flags of the access, read mode++ *++ * mm_access() grants a read-only access to the memory of @task to a caller++ * with CAP_PERFMON even when ptrace_may_access(), and so the++ * ptrace_access_check hook, refused it. Check that access before it is++ * granted that way.++ *++ * Return: Returns 0 if permission is granted.++ */++int security_mm_read_access(struct task_struct *task, unsigned int mode)++{++ return call_int_hook(mm_read_access, task, mode);++}+++ /**+ * security_capget() - Get the capability sets for a process+ * @target: target process+--+2.43.0+diff --git a/testing/functional/tamperproof/t6134-seal-task.sh b/testing/functional/tamperproof/t6134-seal-task.shindex 19d70fbd..6072025b 100755--- a/testing/functional/tamperproof/t6134-seal-task.sh+++ b/testing/functional/tamperproof/t6134-seal-task.sh@@ -98,8 +98,10 @@ if fd >= 0:sys.exit(1 if ctypes.get_errno() == 13 else 3)'-test_expect_success 'root cannot read the exe link or environ of a running shirudo' '- for kind in exe environ; do+# mm_access() grants CAP_PERFMON a read of environ and maps past the+# ptrace check unless mm_read_access refuses it (carried patch 0005).+test_expect_success 'root cannot read the exe link, environ or maps of a running shirudo' '+ for kind in exe environ maps; dopython3 -c "$PROC" "$kind" "$PID"; rc=$?[ "$rc" = 1 ] || { echo "$kind: $rc" >&2; return 1; }donediff --git a/util/image_guards.c b/util/image_guards.cindex 545ea769..b7a3b215 100644--- a/util/image_guards.c+++ b/util/image_guards.c@@ -59,6 +59,8 @@ shirudo_seal_guards[SEAL_HOOK_COUNT] = {{ "shirudo_seal_inode_copy_up", SEAL_COPY_UP_PIN },{ "shirudo_seal_ptrace_access_check",SEAL_PTRACE_PIN },+ { "shirudo_seal_mm_read_access",+ BPFFS_DIR "/seal_mm_read_access_link" },{ "shirudo_seal_perf_event_alloc",SEAL_PERF_PIN },{ "shirudo_seal_task_kill", SEAL_TASK_KILL_PIN },diff --git a/util/image_guards.h b/util/image_guards.hindex af59062a..6b19a20c 100644--- a/util/image_guards.h+++ b/util/image_guards.h@@ -8,7 +8,7 @@#include <stdint.h>-#define SEAL_HOOK_COUNT 28+#define SEAL_HOOK_COUNT 29#define IMAGE_SEAL_HOOK_COUNT 2#define MOUNT_HOOK_COUNT 5#define OBSERVER_HOOK_COUNT 10