eBPF-based security platform
kernel: carry a patch that lets LSM programs read security.bpf xattrs
Daniel Borkmann29b112c5c920
diff --git a/Documentation/concepts/background/bpf-lsm.rst b/Documentation/concepts/background/bpf-lsm.rstindex b26c6374..1de079bc 100644--- a/Documentation/concepts/background/bpf-lsm.rst+++ b/Documentation/concepts/background/bpf-lsm.rst@@ -52,9 +52,9 @@ For a shirudo target, the most minimal and ideal setting is ``lsm=bpf``,which enables the BPF LSM alone; the capability checks still run first,and IMA and EVM, when built in, still run last. No other LSM thenrefuses an operation before shirudo's programs see and count it, and-none can hide a seal label by refusing the read of it, the gap the-threat model lists as U12 (see :doc:`/security/threat-model`). The-security code the target runs shrinks to what shirudo uses. One more LSM+on an upstream kernel without the carried xattr patch none can hide a+seal label by refusing the read of it (see :doc:`/security/threat-model`).+The security code the target runs shrinks to what shirudo uses. One more LSMmatters: the threat model assumes ``lockdown=integrity``, and lockdown isan LSM as well. A kernel built with``CONFIG_SECURITY_LOCKDOWN_LSM_EARLY`` starts it before it reads thediff --git a/Documentation/internals/kernel-patches.rst b/Documentation/internals/kernel-patches.rstindex 1aeef241..9709519f 100644--- a/Documentation/internals/kernel-patches.rst+++ b/Documentation/internals/kernel-patches.rst@@ -30,11 +30,17 @@ Summary- KP Singh- No- All+ * - ``0003-bpf-Read-security.bpf-xattrs-for-LSM-programs-without-the-caller-s-permission.patch``+ - Daniel Borkmann+ - No+ - All"In bpf-next" was checked against the bpf-next tree at commit-``4169c9c90532``: ``include/linux/ftrace.h`` there still marks jmp mode-with bit 0, and ``include/linux/lsm_hook_defs.h`` has no ``tpm_command``-hook.+``cd5bc55288d4``: ``include/linux/ftrace.h`` there still marks jmp mode+with bit 0, ``include/linux/lsm_hook_defs.h`` has no ``tpm_command``+hook, and ``fs/bpf_fs_kfuncs.c`` checks the calling task's read+permission before it returns a ``security.bpf.`` xattr to an LSM+program.How the test kernel applies them--------------------------------diff --git a/Documentation/internals/seal.rst b/Documentation/internals/seal.rstindex cfb9fe5f..ff8107fb 100644--- a/Documentation/internals/seal.rst+++ b/Documentation/internals/seal.rst@@ -183,20 +183,22 @@ Reading the label~~~~~~~~~~~~~~~~~The seal reads the label with the ``bpf_get_file_xattr()`` and-``bpf_get_dentry_xattr()`` kfuncs. Both first check that the calling-task may read the inode, against its raw ids. If the seal refused every-operation whose label read failed, it would refuse every write-only-open and every unlink of a file the caller cannot read, on every file-system. So no label (``ENODATA``) and no security extended attributes-on the file system (``EOPNOTSUPP``) count as unlabeled and the operation-goes ahead. A label the caller may not read (``EACCES``, ``EPERM``) is-also what root makes of any label by granting a write without a read,-through an idmapped bind mount or an ACL, and then acting at a file-system uid that cannot read. There the mark of the inode, or of the-directory the operation changes, decides, and a marked one requires-runtime authority. A label of any length, any other error, and a-failure to allocate the scratch buffer the kfunc writes into require-runtime authority.+``bpf_get_dentry_xattr()`` kfuncs. With the carried kernel patch they+return a ``security.bpf.`` label as the in-kernel LSMs read theirs,+without the calling task's permission. Upstream they first check that+the calling task may read the inode, against its raw ids. If the seal+refused every operation whose label read failed, it would refuse every+write-only open and every unlink of a file the caller cannot read, on+every file system. So no label (``ENODATA``) and no security extended+attributes on the file system (``EOPNOTSUPP``) count as unlabeled and+the operation goes ahead. A label the caller may not read (``EACCES``,+``EPERM``) is, on an upstream kernel, also what root makes of any label+by granting a write without a read, through an idmapped bind mount or+an ACL, and then acting at a file system uid that cannot read. There+the mark of the inode, or of the directory the operation changes,+decides, and a marked one requires runtime authority. A label of any+length, any other error, and a failure to allocate the scratch buffer+the kfunc writes into require runtime authority.Labeling at creation---------------------@@ -255,12 +257,13 @@ Hard links to labeled inodesa link can be made inside a sealed directory, where it carries avalid label.-Label reads under another LSM- The kfuncs' read permission check runs every LSM's- ``inode_permission`` hook for the calling task. A policy of another- LSM that lets a task write or unlink a file but not read it makes a- labeled inode without a mark look unlabeled to the seal; a marked- one is refused, and the same failure on an open leaves the mark.+Label reads on an upstream kernel+ Without the carried xattr patch, the kfuncs check that the calling+ task may read the inode, and every LSM's ``inode_permission`` hook+ takes part. A read that fails for the caller, through an idmapped+ mount, an ACL or another LSM's policy, makes a labeled inode+ without a mark look unlabeled to the seal; a marked one is refused,+ and the same failure on an open leaves the mark.ACLs and inode flags``inode_set_acl``, ``inode_remove_acl`` and ``inode_file_setattr``diff --git a/Documentation/operate/plan/interoperability.rst b/Documentation/operate/plan/interoperability.rstindex 00ee7bce..995b6190 100644--- a/Documentation/operate/plan/interoperability.rst+++ b/Documentation/operate/plan/interoperability.rst@@ -306,12 +306,13 @@ Other LSMsshirudo's programs and moves no counter. With ``bpf`` before them,shirudo's refusal ends the walk and the other LSM does not see or logthe attempt.-:Seal labels: The seal reads a file's label through a kfunc that checks- the caller's read permission, and every LSM that implements the- ``inode_permission`` hook takes part. In the kernel tree, SELinux and- Smack implement it; AppArmor, Landlock and Yama do not. A policy that- grants write or unlink but refuses read makes a sealed file look- unlabeled, and the operation goes through without a counter (U12 in+:Seal labels: With the carried kernel patch, the seal reads a file's+ label as the in-kernel LSMs read theirs, without the caller's read+ permission, and no other LSM's ``inode_permission`` policy can hide+ it. On an upstream kernel the kfunc checks that permission, every+ LSM that implements the hook takes part, SELinux and Smack among+ them, and a policy that grants write or unlink but refuses read+ makes a sealed file without a mark look unlabeled (see the seal in:doc:`/security/threat-model`).:Policies: shirudo ships no SELinux, AppArmor or Smack policy. A policythat confines shirudo's processes must allow what they use, among itdiff --git a/Documentation/operate/plan/system-requirements.rst b/Documentation/operate/plan/system-requirements.rstindex e3c1e8ee..6eb22ca2 100644--- a/Documentation/operate/plan/system-requirements.rst+++ b/Documentation/operate/plan/system-requirements.rst@@ -60,7 +60,7 @@ Kernel source``KEY_SPEC_BPF_KEYRING`` with the ``bpf.keyring_unsealed``parameter, and the kfuncs ``bpf_inode_init_xattr`` and``bpf_real_data_inode``. The test suite runs bpf-next commit- ``4169c9c90532``.+ ``cd5bc55288d4``.* - Upstream commit ``1041fca2ee8a``- ``keys: add KEY_SPEC_DM_VERITY_KEYRING``. ``shirudo init``enrolls and seals the config certificate in the ``.dm-verity``@@ -77,6 +77,12 @@ Kernel source- Adds the ``tpm_command`` LSM hook. ``shirudo init`` attaches``lsm/tpm_command`` to refuse an untrusted ``TPM2_NV_Extend`` ofits register and fails without the hook.+ * - :src:`kernel/0003-bpf-Read-security.bpf-xattrs-for-LSM-programs-without-the-caller-s-permission.patch`+ - Lets an LSM program read a ``security.bpf.`` xattr without the+ calling task's read permission on the inode, as the in-kernel+ LSMs read their labels. Without it the seal takes a label its+ caller may not read for a missing one unless the inode carries a+ mark, which root can arrange with an idmapped mount or an ACL.:doc:`/internals/kernel-patches` gives the upstream status of thecarried patches.diff --git a/Documentation/security/security-model.rst b/Documentation/security/security-model.rstindex 57ffd9c1..dffbf48a 100644--- a/Documentation/security/security-model.rst+++ b/Documentation/security/security-model.rst@@ -42,9 +42,8 @@ What shirudo provides* - Only shirudo changes its state- The seal labels ``/var/lib/shirudo`` and checks every change toa labeled inode; keyrings and authorized processes are guarded.- - Marks can be evicted; another LSM can refuse the label read;- offline writes bypass it.- - :ref:`T3 <tm-t3>`, R15, R19, U12+ - Marks can be evicted; offline writes bypass it.+ - :ref:`T3 <tm-t3>`, R15, R19* - Every change is measured- Loads, unloads, revocations and tree operations extend a TPM NVregister whose writes ``lsm/tpm_command`` keeps to shirudo.diff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rstindex cece1b59..237d6b92 100644--- a/Documentation/security/threat-model.rst+++ b/Documentation/security/threat-model.rst@@ -147,13 +147,18 @@ without the protection.checked.* - A3- The kernel has the signed-loader ABI of the bpf-next tree shirudo- targets and the ``tpm_command`` LSM hook. Upstream has no such- hook, and shirudo carries it as- :src:`kernel/0002-tpm-Let-LSMs-inspect-userspace-commands.patch`.- - T1 needs the ABI, T5 the hook.- - Yes. The pre-flight check ``signed_loader_kernel_abi`` tests the- ABI. Without the hook the ``bpf2`` object does not load and- ``init`` fails.+ targets, the ``tpm_command`` LSM hook, and xattr kfuncs that+ return a ``security.bpf.`` label to an LSM program without the+ calling task's read permission. Upstream has neither the hook nor+ the latter, and shirudo carries them as+ :src:`kernel/0002-tpm-Let-LSMs-inspect-userspace-commands.patch`+ and+ :src:`kernel/0003-bpf-Read-security.bpf-xattrs-for-LSM-programs-without-the-caller-s-permission.patch`.+ - T1 needs the ABI, T5 the hook, T3 the label read.+ - Partly. The pre-flight check ``signed_loader_kernel_abi`` tests+ the ABI. Without the hook the ``bpf2`` object does not load and+ ``init`` fails. Without the label read, the seal falls back to+ its marks where a read fails for the caller (T3).* - A4- The kernel refuses unsigned modules (``CONFIG_MODULE_SIG`` withenforcement) and runs with lockdown in integrity mode@@ -661,9 +666,12 @@ target and never grants authority: a file system root mounts can carryit, and a binary that carries it gains nothing. A label read that failswith an error other than ``ENODATA``, ``EOPNOTSUPP``, ``EACCES`` or``EPERM`` requires authority, and so does a failure to allocate the-seal's scratch memory. The kernel checks read permission for the caller-before it returns the label, against the raw ids of the inode (U12), so-the two permission errors mean either a caller that may do nothing or+seal's scratch memory. With the carried xattr patch (A3) the kernel+returns the label to the seal without the caller's read permission, as+it returns their labels to the in-kernel LSMs, and the two permission+errors do not occur. Upstream, the kernel checks read permission for+the caller before it returns the label, against the raw ids of the+inode, so the two errors mean either a caller that may do nothing orone that root gave a write without a read: through an idmapped bindmount, which shows uid 0's files as another id's to the permissioncheck, or through an ACL, and then at a file system uid that cannot@@ -671,8 +679,9 @@ read. Where the read fails that way, the mark of the inode, or of thedirectory the operation changes, decides, and the operation needsauthority when either is marked (``t6111``). The seal marks every inodeit labels at creation, every file shirudo opens, every file anyone-opens for writing and every directory anyone opens; a file shirudo has-not touched since it came into memory has no mark (R15). The labels+opens for writing and every directory anyone opens; on a kernel+without the patch, a file shirudo has not touched since it came into+memory has no mark (R15). The labelsthemselves are refused to such a caller whatever the read said: roottakes a file system uid that cannot read the inode with ``setfsuid()``and keeps the ``CAP_SYS_ADMIN`` that a ``security.*`` xattr change@@ -796,7 +805,7 @@ An authorized process is protected against being taken over:``t6112``, ``t6113``, ``t6114``, ``t6115``, ``t6118``, ``t6123``,``t6124``, ``t6127``, ``t6129``, ``t6131``, ``t6133``, ``t6134``,``t6135``, ``t6136``, ``t6137``-:Residual: R2, R15, R19, R22, U12+:Residual: R2, R15, R19, R22T4 Substituting artifacts~~~~~~~~~~~~~~~~~~~~~~~~~@@ -1415,8 +1424,9 @@ R15 Marks can be evictedsystem and bring them back. An unlabeled entry stays inert: shirudoneither follows nor restores it. A returned labeled entry is takenfor shirudo's own. While a file's mark is absent, root can change- its ACL and inode flags, and a label read it made fail counts as- unlabeled (T3). Detection: only signatures and measurement; an+ its ACL and inode flags, and on a kernel without the carried xattr+ patch (A3) a label read it made fail counts as unlabeled (T3).+ Detection: only signatures and measurement; anarchived upatch restored at the next boot appears in the init event'sset. Mitigation: D6.@@ -1647,19 +1657,6 @@ U11 The boot continues when init failsprefers a halted boot can add an ``OnFailure=`` drop-in to``shirudo-initrd.service``; this is not tested.-U12 Another LSM can switch the seal off- :Affects: The seal (T3).- :Consequence: The kernel's label read checks the caller's read- permission, and every LSM's ``inode_permission`` hook takes part.- A policy that grants write or unlink but refuses read makes a- labeled inode without a mark look unlabeled, and the operation- goes through without a counter; a marked one needs authority- (T3), and the same refusal on an open leaves the mark as it was.- :Status: Confirmed in code and in the kernel; the attack is a- code-path reading, not reproduced.- :Meanwhile: Run no other LSM whose policy root can change at- runtime.-U13 Revocation does not reach everything:Affects: Tree publications and vouched keys (R13).:Consequence: Tree publications verify against the runtime image'sdiff --git a/kernel/0003-bpf-Read-security.bpf-xattrs-for-LSM-programs-without-the-caller-s-permission.patch b/kernel/0003-bpf-Read-security.bpf-xattrs-for-LSM-programs-without-the-caller-s-permission.patchnew file mode 100644index 00000000..9994cee0--- /dev/null+++ b/kernel/0003-bpf-Read-security.bpf-xattrs-for-LSM-programs-without-the-caller-s-permission.patch@@ -0,0 +1,64 @@+From 4d30e9a670741ab7ab778f28bebf445624416059 Mon Sep 17 00:00:00 2001+From: Daniel Borkmann <[email protected]>+Date: Tue, 3 Nov 2026 10:49:48 +0000+Subject: [PATCH] bpf: Read security.bpf xattrs for LSM programs without the+ caller's permission++bpf_get_dentry_xattr() and bpf_get_file_xattr() check that the current+task may read the inode before they return an xattr, for user. and+security.bpf. names alike. For a BPF LSM program the labels it keeps+under security.bpf. are its own, and the task whose operation it decides+is the one the program may refuse: its read permission on the inode says+nothing about the label. The in-kernel LSMs read their labels with+__vfs_getxattr() in kernel context for that reason.++The check also lets that task choose what the program sees. A read that+fails with -EACCES or -EPERM looks to the program like a missing label+unless it fails every such operation closed, which it cannot do without+refusing every write-only open and every unlink of an unreadable file on+the system. Root arranges the failure for any labeled inode: an idmapped+bind mount shows uid 0's files as another id's to the permission check,+an ACL grants another id a write without a read, and a task at that id+through setfsuid() keeps CAP_SYS_ADMIN but not the DAC capabilities, so+the kernel lets it write what the program then takes for unlabeled.+Another LSM's inode_permission policy does the same.++Skip the permission check for security.bpf. names. The kfuncs are+registered for LSM and struct_ops programs, which run in kernel context+with the labels they maintain; user. names keep the check.++Signed-off-by: Daniel Borkmann <[email protected]>+---+ fs/bpf_fs_kfuncs.c | 16 +++++++++++++---+ 1 file changed, 13 insertions(+), 3 deletions(-)++diff --git a/fs/bpf_fs_kfuncs.c b/fs/bpf_fs_kfuncs.c+index 35ccd98a4de6..3447159eb919 100644+--- a/fs/bpf_fs_kfuncs.c++++ b/fs/bpf_fs_kfuncs.c+@@ -106,9 +106,19 @@ static int bpf_xattr_read_permission(const char *name, struct inode *inode)+ if (!inode)+ return -EINVAL;++- /* Allow reading xattr with user. and security.bpf. prefix */+- if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) &&+- !match_security_bpf_prefix(name))++ /*++ * The labels a BPF LSM keeps under security.bpf. are its own, read++ * as SELinux, Smack and EVM read theirs with __vfs_getxattr(): in++ * kernel context, not with the permission of the task whose++ * operation the program decides. That task's read permission says++ * nothing about the label, and a read that fails for it, through an++ * idmapped mount, an ACL or another LSM's policy, would hide the++ * label from the program that enforces it. user. xattrs stay the++ * task's to read.++ */++ if (match_security_bpf_prefix(name))++ return 0;++ if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))+ return -EPERM;++ return inode_permission(&nop_mnt_idmap, inode, MAY_READ);+--+2.43.0+