eBPF-based security platform
bpf2: keep the mount points above the private bpffs in place
Daniel Borkmann95bfe4faa35a
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex afb254f9..8def7256 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -511,6 +511,10 @@ signed config image.- A PCI config write, ``/proc/kcore`` and ``/dev/mem``, where thekernel has them, are refused, the first two counted as``Lockdown``, and a read of ``/sys/power/state`` counts nothing+ * - ``t6155-mountpoint-removal``+ - From a namespace without ``/run``, an ``rmdir`` and a rename of+ the directory init's ``/run`` covers are refused and counted, and+ the private bpffs stays mounted``bpm`` (7)~~~~~~~~~~~@@ -1017,7 +1021,7 @@ tests assert that a refusal moves its counter:``BpfCommand``, ``PinUnlink``, ``PinRename``, ``BpffsUmount``- ``t6108``* - ``BpffsUmountAbove``- - ``t6120``+ - ``t6120``, ``t6155``* - ``PinEntry``- ``t6121``* - ``PerfAttach``diff --git a/Documentation/internals/enforcement.rst b/Documentation/internals/enforcement.rstindex ea8b11bf..680baae5 100644--- a/Documentation/internals/enforcement.rst+++ b/Documentation/internals/enforcement.rst@@ -367,11 +367,13 @@ when the record it needs is missing.``lsm/inode_setxattr``, ``lsm/inode_removexattr`` (``bpf2``)- The operation in the private bpffs or the ``/run/shirudo`` tmpfsto a task without authority. Rename checks both directories.- Other file systems are not checked.+ On any file system, unlink, rmdir and rename refuse such a task+ the mount point of the recorded bpffs mount or of a mount above+ it, which the kernel would detach or move in every namespace.- ``EPERM``- In the bpffs: ``PinUnlink``, ``PinEntry`` (mkdir, symlink, link),- ``PinRename``, ``PinXattr``. Rmdir and ``/run/shirudo`` refusals- uncounted.+ ``PinRename``, ``PinXattr``. A mount point: ``BpffsUmountAbove``.+ Rmdir and ``/run/shirudo`` refusals uncounted.- Closed for those two file systems.* - ``lsm/sb_umount`` (``bpf2``)- To every caller: unmounting the bpffs, an unmount whose subtree ordiff --git a/Documentation/security/tamper-counters.rst b/Documentation/security/tamper-counters.rstindex 1e44b204..55588506 100644--- a/Documentation/security/tamper-counters.rst+++ b/Documentation/security/tamper-counters.rst@@ -120,7 +120,11 @@ counter.- unmount of a mount above the private bpffs- ``lsm/sb_umount``: an unmount whose subtree or propagationreaches the recorded bpffs mount, or a tree the walk cannot- finish, every caller+ finish, every caller; ``lsm/inode_rmdir``,+ ``lsm/inode_unlink``, ``lsm/inode_rename``: the removal or rename+ of the mount point of that mount or of one above it, which+ detaches or moves the mounts on it in every namespace, to a task+ without authority- ``EPERM``* - ``BpffsMount``- move, mount over or remount of the private bpffsdiff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rstindex babf6b0e..1eed97f4 100644--- a/Documentation/security/threat-model.rst+++ b/Documentation/security/threat-model.rst@@ -629,9 +629,16 @@ its mountpoint on the peers and slaves of its parent, from the bpffsmount ``init`` recorded, and refuses an unmount that reaches it(``BpffsUmountAbove``). The walk runs without the namespace lock,within fixed bounds of 1024 mounts, 32 levels and 4 masters, and-refuses a tree it cannot finish. The tmpfs at ``/run/shirudo`` gets the-same treatment without counters: no caller unmounts, moves or remounts-it, and only authorized tasks mount onto it.+refuses a tree it cannot finish. Removing a dentry detaches what is+mounted on it in every namespace, and renaming it moves those mounts,+both without ``sb_umount``: from a namespace where ``/run`` is not+mounted, an ``rmdir`` of the directory init's ``/run`` covers would take+the bpffs along. ``lsm/inode_rmdir``, ``lsm/inode_unlink`` and+``lsm/inode_rename`` refuse a task without authority the removal or+rename of the mount point of the recorded bpffs mount or of any mount+above it (``BpffsUmountAbove``, ``t6155``). The tmpfs at+``/run/shirudo`` gets the same treatment without counters: no caller+unmounts, moves or remounts it, and only authorized tasks mount onto it.Root also controls part of the site a load attaches at, since rootruns ``shirudo upatch load``. A ``cgroup_skb`` program attaches at the@@ -661,7 +668,8 @@ and the returned link's ifindex around attachment.``PerfAttach``, ``PinUnlink``, ``PinRename``, ``PinEntry``,``PinXattr``, ``BpffsUmount``, ``BpffsUmountAbove``, ``BpffsMount``:Tests: ``t6105``, ``t6106``, ``t6107``, ``t6108``, ``t6109``,- ``t6116``, ``t6119``, ``t6120``, ``t6121``, ``t6138``, ``t3111``+ ``t6116``, ``t6119``, ``t6120``, ``t6121``, ``t6138``, ``t6155``,+ ``t3111``:Residual: R1, R2, R3, U17.. _tm-t3:diff --git a/bpf/bpf2.c b/bpf/bpf2.cindex 21b1ed82..6fc89698 100644--- a/bpf/bpf2.c+++ b/bpf/bpf2.c@@ -380,14 +380,54 @@ int BPF_PROG(shirudo_lsm_tpm_command, struct file *file,* `rm` -- is denied, so the LSM link cannot be detached by removing* its pin.*/+#define MOUNTPOINT_DEPTH_MAX 32++/*+ * Removing or renaming a dentry detaches or moves what is mounted on it+ * in every namespace, without sb_umount: from a namespace where /run is+ * not mounted, an rmdir of the directory init's /run covers takes the+ * private bpffs with it. The mount points from that bpffs up to the+ * root of init's namespace are shirudo's alone to remove or rename.+ */+static __always_inline int shirudo_mountpoint_guard(struct dentry *dentry)+{+ struct mount_record *rec;+ struct mount *m, *parent;+ struct sb_id *id;+ __u32 zero = 0;+ int i;++ if (!dentry || !(BPF_CORE_READ(dentry, d_flags) &+ bpf_core_enum_value(enum dentry_flags, DCACHE_MOUNTED)))+ return 0;+ rec = bpf_map_lookup_elem(&shirudo_mnt_rec, &zero);+ if (!rec || !rec->mount)+ return 0;+ m = (struct mount *)rec->mount;+ bpf_for(i, 0, MOUNTPOINT_DEPTH_MAX) {+ if (BPF_CORE_READ(m, mnt_mountpoint) == dentry)+ goto deny;+ parent = BPF_CORE_READ(m, mnt_parent);+ if (!parent || parent == m)+ return 0;+ m = parent;+ }+deny:+ id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);+ if (id && shirudo_task_is_self(id))+ return 0;+ return shirudo_tamper_deny(TAMPER_BPFFS_UMOUNT_ABOVE, -EPERM);+}+SEC("lsm/inode_unlink")int BPF_PROG(shirudo_lsm_inode_unlink, struct inode *dir,struct dentry *dentry, int ret){if (ret)return ret;- return shirudo_bpffs_guard(BPF_CORE_READ(dir, i_sb, s_dev),- TAMPER_PIN_UNLINK);+ ret = shirudo_bpffs_guard(BPF_CORE_READ(dir, i_sb, s_dev),+ TAMPER_PIN_UNLINK);+ return ret ? ret : shirudo_mountpoint_guard(dentry);}SEC("lsm/inode_rmdir")@@ -396,8 +436,9 @@ int BPF_PROG(shirudo_lsm_inode_rmdir, struct inode *dir,{if (ret)return ret;- return shirudo_bpffs_guard(BPF_CORE_READ(dir, i_sb, s_dev),- TAMPER_MAX);+ ret = shirudo_bpffs_guard(BPF_CORE_READ(dir, i_sb, s_dev),+ TAMPER_MAX);+ return ret ? ret : shirudo_mountpoint_guard(dentry);}/*@@ -505,9 +546,12 @@ int BPF_PROG(shirudo_lsm_inode_rename, struct inode *old_dir,return ret;ret = shirudo_bpffs_guard(BPF_CORE_READ(old_dir, i_sb, s_dev),TAMPER_PIN_RENAME);- return ret ? ret :- shirudo_bpffs_guard(BPF_CORE_READ(new_dir, i_sb, s_dev),- TAMPER_PIN_RENAME);+ if (!ret)+ ret = shirudo_bpffs_guard(BPF_CORE_READ(new_dir, i_sb, s_dev),+ TAMPER_PIN_RENAME);+ if (!ret)+ ret = shirudo_mountpoint_guard(old_dentry);+ return ret ? ret : shirudo_mountpoint_guard(new_dentry);}/*diff --git a/testing/functional/tamperproof/t6155-mountpoint-removal.sh b/testing/functional/tamperproof/t6155-mountpoint-removal.shnew file mode 100755index 00000000..801ac6f7--- /dev/null+++ b/testing/functional/tamperproof/t6155-mountpoint-removal.sh@@ -0,0 +1,55 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# t6155 -- removing a dentry detaches what is mounted on it in every mount+# namespace, without an unmount hook: from a namespace where /run is not+# mounted, root cannot remove or rename the directory init's /run covers,+# which would take the private bpffs, and with it the enforcement links,+# along. Both are refused and counted as BpffsUmountAbove.++cd "$(dirname "$0")/../.."+. ./integration-lib.sh+target_guard+command -v python3 >/dev/null 2>&1 || test_skip_all "no python3"+command -v unshare >/dev/null 2>&1 || test_skip_all "no unshare"+mountpoint -q /run/shirudo/bpffs || test_skip_all "no private bpffs"+W=$(mktemp -d)+trap 'rm -rf "$W"' EXIT+counter() {+ "$SHIRUDO_BIN" status --json | python3 -c '+import json, sys+print(json.load(sys.stdin)["tamper_attempts"]["counters"][sys.argv[1]])+' "$1"+}+# In a namespace of its own, drops /run there and runs python $1 on the+# directory below it; prints the errno name, or done.+beneath() {+ unshare -m sh -c '+ mount --make-rprivate / || exit 3+ while mountpoint -q /run; do umount -l /run || exit 3; done+ python3 -c "+import errno, os, sys+try:+ $1+ print(\"done\")+except OSError as e:+ print(errno.errorcode[e.errno])+"' sh "$1"+}+before=$(counter BpffsUmountAbove) || test_skip_all "no tamper counters"++test_expect_success 'rmdir of the directory under /run is refused and counted' '+ beneath "os.rmdir(\"/run\")" >"$W/out" &&+ { grep -qx EPERM "$W/out" || { cat "$W/out" >&2; false; }; } &&+ [ "$(counter BpffsUmountAbove)" -gt "$before" ]+'+test_expect_success 'and so is a rename of it' '+ beneath "os.rename(\"/run\", \"/run.t6155\")" >"$W/out" &&+ { grep -qx EPERM "$W/out" || { cat "$W/out" >&2; false; }; }+'+test_expect_success 'the private bpffs is still mounted and status reads it' '+ mountpoint -q /run/shirudo/bpffs &&+ "$SHIRUDO_BIN" status >/dev/null+'+test_done