eBPF-based security platform
testing/tamperproof: reach the pins through a second mount of the bpffs
KP Singh2a560729814a
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex 955c621e..adb148b0 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -695,6 +695,12 @@ signed config image.refused; a legacy move of an unrelated mount is refusedwhere ``move_mount(2)`` is not; the same changes in a privatemount namespace are harmless+ * - ``t6159-bpffs-second-mount``+ - A legacy ``MS_BIND`` gives the private bpffs a second mount point+ in a mount namespace of root's. Through it a pin is not unlinked,+ renamed or opened, and the second mount is not unmounted,+ remounted or mounted over; each refusal is counted, and nothing+ reaches the namespace of ``init````bpm`` (7)~~~~~~~~~~~@@ -1316,7 +1322,7 @@ The threats are those of :doc:`/security/threat-model`.* - T2 Detaching or replacing enforcement- ``t6101``, ``t6105``, ``t6106``, ``t6107``, ``t6108``,``t6109``, ``t6116``, ``t6119``, ``t6120``, ``t6121``,- ``t6138``+ ``t6138``, ``t6159``* - T3 Tampering with shirudo's files and process- ``t6001``, ``t6006``, ``t6007``, ``t6110``, ``t6111``,``t6112``, ``t6114``, ``t6115``, ``t6118``, ``t6123``,@@ -1361,13 +1367,14 @@ tests assert that a refusal moves its counter:* - Counter- Test- * - ``LoadNotLoader``, ``ProgAttach``, ``ObjPin``, ``ObjGet``,- ``BpfCommand``, ``PinUnlink``, ``PinRename``, ``BpffsUmount``+ * - ``LoadNotLoader``, ``ProgAttach``, ``ObjPin``, ``BpfCommand``- ``t6108``+ * - ``ObjGet``, ``PinUnlink``, ``PinRename``, ``BpffsUmount``+ - ``t6108``, ``t6159``* - ``BpffsUmountAbove``- ``t6120``, ``t6155``, ``t6158``* - ``BpffsMount``- - ``t6158``+ - ``t6158``, ``t6159``* - ``PinEntry``- ``t6121``* - ``PerfAttach``diff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rstindex d40d1896..47d0a64e 100644--- a/Documentation/security/threat-model.rst+++ b/Documentation/security/threat-model.rst@@ -648,8 +648,14 @@ The pins live as long as the bpffs superblock, which goes with its lastmount. ``lsm/sb_umount``, ``lsm/move_mount``, ``lsm/sb_mount`` and``lsm/sb_remount`` refuse every caller, shirudo included, an unmount, amove, a mount over or a remount of the private bpffs (``BpffsUmount``,-``BpffsMount``). Figure 2 shows the two ways an operation on a mount far-from the bpffs takes it along.+``BpffsMount``). They go by the superblock, as the guards on the pins+do, and not by the path. A legacy ``mount(2)`` with ``MS_BIND`` names+its source in a string that ``lsm/sb_mount`` cannot resolve, so root can+bind the bpffs at a second path. That is a second name for the same pins+and no other way to them: through it a pin is no more unlinked, renamed+or opened, and the second mount is no more unmounted, remounted or+mounted over than the first (``t6159``, R3). Figure 2 shows the two ways+an operation on a mount far from the bpffs takes it along.Bootstrap in the initrd records the unique mount IDs of its root and thetarget root. In the protected namespace, only host PID 1 can pivot from@@ -1700,6 +1706,10 @@ R3 Pins depend on the mount treereleasing them: enforcement continues, ``shirudo status`` reports theLSM as not attached, and ``upatch load`` and ``upatch unload`` refuse``tampered`` with the message ``lsm_detached`` in their pre-flight.+ A second mount of the bpffs, which root binds with the legacy+ ``mount(2)``, holds the same pins under the same guards, the one on+ its unmount included: an unmount of it is refused as one of the first+ is, so it stays for as long as the mount it sits on.The walk's bounds refuse every unmount of a tree they cannot cover.Mitigations: D7, D11.diff --git a/testing/functional/tamperproof/t6159-bpffs-second-mount.sh b/testing/functional/tamperproof/t6159-bpffs-second-mount.shnew file mode 100755index 00000000..f667a7d0--- /dev/null+++ b/testing/functional/tamperproof/t6159-bpffs-second-mount.sh@@ -0,0 +1,156 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# t6159 -- a second mount of the private bpffs is a second name for the+# same pins and no other way to them. A legacy mount(2) with MS_BIND+# names its source in a string lsm/sb_mount cannot resolve, so root can+# bind the bpffs somewhere else. The guards go by the file system and+# not by the path: through the second mount a pin is no more unlinked,+# renamed or opened than through the first, and the second mount is no+# more unmounted, remounted or mounted over than the first.+#+# Each call is made in a mount namespace of its own, which takes the+# second mount with it when the caller exits: the guards refuse the+# unmount, so nothing else would remove it.++cd "$(dirname "$0")/../.."+. ./integration-lib.sh+target_guard+command -v python3 >/dev/null 2>&1 || test_skip_all "no python3"+C=helper/test-bpf-cmd+[ -x "$C" ] || test_skip_all "test-bpf-cmd helper not built"+B=/run/shirudo/bpffs+mountpoint -q "$B" || test_skip_all "no private bpffs"++W=$(mktemp -d)+M=$W/second.py+COPY=$W/copy+mkdir "$COPY"+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"+}++enforcing() {+ mountpoint -q "$B" && test -e "$B/lsm_link" &&+ [ "$(./$C LINK_CREATE)" = "errno=13" ]+}++# In a new mount namespace, bind the bpffs at the second path with the+# legacy call, make one call there and answer with 0 or the name of its+# errno. A pin is named relative to the second mount.+cat >"$M" <<'PY'+import ctypes as c+import errno+import os+import sys++libc = c.CDLL(None, use_errno=True)+libc.syscall.restype = c.c_long+bpffs, copy, op = (os.fsencode(a) for a in sys.argv[1:4])+names = [os.path.join(copy, os.fsencode(a)) for a in sys.argv[4:]]+++def failed(what):+ sys.exit("%s: %s" % (what, errno.errorcode[c.get_errno()]))+++# CLONE_NEWNS, then MS_REC | MS_PRIVATE on / and MS_BIND.+if libc.unshare(0x20000):+ failed("unshare")+if libc.mount(None, b"/", None, c.c_ulong(1 << 14 | 1 << 18), None):+ failed("private")+if libc.mount(bpffs, copy, None, c.c_ulong(1 << 12), None):+ failed("bind")++if op == b"stat":+ ret = 0 if os.path.lexists(names[0]) else -1+ c.set_errno(errno.ENOENT)+elif op == b"unlink":+ ret = libc.unlink(names[0])+elif op == b"rename":+ ret = libc.rename(names[0], names[1])+elif op == b"obj-get":+ # union bpf_attr for BPF_OBJ_GET: pathname, bpf_fd, file_flags.+ path = c.create_string_buffer(names[0])+ attr = (c.c_uint64 * 2)(c.addressof(path), 0)+ nr = {"x86_64": 321, "aarch64": 280}[os.uname().machine]+ ret = libc.syscall(nr, 7, c.byref(attr), c.c_uint(c.sizeof(attr)))+ ret = 0 if ret >= 0 else -1+elif op == b"umount":+ ret = libc.umount2(copy, 0)+elif op == b"detach":+ # MNT_DETACH+ ret = libc.umount2(copy, 2)+elif op == b"remount":+ # MS_REMOUNT | MS_RDONLY+ ret = libc.mount(None, copy, None, c.c_ulong(1 << 5 | 1), None)+elif op == b"over":+ ret = libc.mount(b"none", copy, b"tmpfs", c.c_ulong(0), None)+else:+ sys.exit(2)+print(0 if ret == 0 else errno.errorcode[c.get_errno()])+PY++second() {+ python3 "$M" "$B" "$COPY" "$@"+}++test_expect_success 'enforcement is live to begin with' '+ enforcing+'++test_expect_success 'a legacy bind gives the bpffs a second mount point' '+ [ "$(second stat lsm_link)" = 0 ] &&+ [ "$(second stat no-such-pin)" = ENOENT ]+'++test_expect_success 'a pin is not unlinked through it, and the attempt is counted' '+ before=$(counter PinUnlink) &&+ [ "$(second unlink lsm_link)" = EPERM ] &&+ [ "$(counter PinUnlink)" -gt "$before" ] &&+ enforcing+'++test_expect_success 'nor renamed' '+ before=$(counter PinRename) &&+ [ "$(second rename lsm_link moved)" = EPERM ] &&+ [ "$(counter PinRename)" -gt "$before" ] &&+ test ! -e "$B/moved" &&+ enforcing+'++test_expect_success 'nor opened' '+ before=$(counter ObjGet) &&+ [ "$(second obj-get lsm_link)" = EACCES ] &&+ [ "$(counter ObjGet)" -gt "$before" ]+'++test_expect_success 'the second mount is not unmounted either' '+ before=$(counter BpffsUmount) &&+ [ "$(second umount)" = EPERM ] &&+ [ "$(second detach)" = EPERM ] &&+ [ "$(counter BpffsUmount)" -ge "$((before + 2))" ] &&+ enforcing+'++test_expect_success 'nor remounted or mounted over' '+ before=$(counter BpffsMount) &&+ [ "$(second remount)" = EPERM ] &&+ [ "$(second over)" = EPERM ] &&+ [ "$(counter BpffsMount)" -gt "$before" ] &&+ enforcing+'++test_expect_success 'none of it reached the namespace of init' '+ ! mountpoint -q "$COPY" &&+ [ "$(findmnt -n -t bpf -o TARGET | grep -c "^$B\$")" = 1 ] &&+ enforcing+'++test_done