eBPF-based security platform
testing/tamperproof: replay attempted bpffs move-and-detach bypass
John Fastabendeb42e0889cd8
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex b99b260c..4e625aa6 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -673,6 +673,15 @@ signed config image.* - ``t6157-kernel-options``- The test kernel, which signs its modules, passes the pre-flightcheck of module signatures+ * - ``t6158-bpffs-move-detach``+ - The bpffs cannot be moved away and detached: a legacy ``MS_MOVE``+ and a ``move_mount(2)`` of the bpffs, and of ``/run`` above it,+ are refused and counted after ``mount_setattr(2)`` was asked to+ make the parent private; a legacy propagation change on the+ bpffs ancestry and a ``pivot_root`` in its namespace are+ refused; a legacy move of an unrelated mount is refused+ where ``move_mount(2)`` is not; the same changes in a private+ mount namespace are harmless``bpm`` (7)~~~~~~~~~~~@@ -1307,7 +1316,7 @@ The threats are those of :doc:`/security/threat-model`.- ``t4001``, ``t4003``, ``t4004``, ``t4102``, ``t9105``, ``t9112``* - T10 An attempt that leaves no trace- ``t6003``, ``t6108``, ``t6118``, ``t6119``, ``t6120``,- ``t6121``, ``t6124``, ``t6126``, ``t6135``, ``t6137``+ ``t6121``, ``t6124``, ``t6126``, ``t6135``, ``t6137``, ``t6158``* - T11 Image-mode config and executable substitution- ``t5001``, ``t5002``, ``t5003``, ``t5006``, ``t5008``,``t5101``, ``t5102``, ``t5103``, ``t5104``, ``t6112``,@@ -1332,7 +1341,9 @@ tests assert that a refusal moves its counter:``BpfCommand``, ``PinUnlink``, ``PinRename``, ``BpffsUmount``- ``t6108``* - ``BpffsUmountAbove``- - ``t6120``, ``t6155``+ - ``t6120``, ``t6155``, ``t6158``+ * - ``BpffsMount``+ - ``t6158``* - ``PinEntry``- ``t6121``* - ``PerfAttach``@@ -1471,7 +1482,7 @@ Tamper countersNo test asserts that a refusal moves ``LoadUnsigned``,``LoadUnverified``, ``LoadKeyringKind``, ``LoadKeyring``, ``MapCreate``,``MapWrite``, ``MapFdWrite``, ``ProgRun``, ``ExeIdentity``,-``KeyringWrite``, ``PinXattr``, ``BpffsMount``, ``SealWrite``,+``KeyringWrite``, ``PinXattr``, ``SealWrite``,``SealUnlink``, ``SealRename``, ``SealSetattr``, ``SealXattr``,``SealLabel``, ``SealPerf`` or ``TpmExtend``. The refusals behindseveral of them are tested, the counter is not.diff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rstindex 03951150..f6f8cb43 100644--- a/Documentation/security/threat-model.rst+++ b/Documentation/security/threat-model.rst@@ -738,7 +738,7 @@ and the returned link's ifindex around attachment.``PinXattr``, ``BpffsUmount``, ``BpffsUmountAbove``, ``BpffsMount``:Tests: ``t6105``, ``t6106``, ``t6107``, ``t6108``, ``t6109``,``t6116``, ``t6119``, ``t6120``, ``t6121``, ``t6138``, ``t6155``,- ``t3111``+ ``t6158``, ``t3111``:Residual: R1, R2, R3, U17.. _tm-t3:diff --git a/testing/functional/tamperproof/t6158-bpffs-move-detach.sh b/testing/functional/tamperproof/t6158-bpffs-move-detach.shnew file mode 100755index 00000000..b852a987--- /dev/null+++ b/testing/functional/tamperproof/t6158-bpffs-move-detach.sh@@ -0,0 +1,195 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# t6158 -- root detached the private bpffs with no hook and no counter by+# first making the mount tree private, so the kernel would let a legacy+# MS_MOVE relocate the bpffs (do_move_mount refuses a move whose parent+# is shared), then moving it onto a procfs dentry the kernel later+# invalidates through the hookless __detach_mounts path. The LSM cannot+# see a legacy MS_MOVE's source, and mount_setattr(2) changes propagation+# with no hook, so the guards refuse the move itself: every legacy+# MS_MOVE in the bpffs's mount namespace, and a move_mount(2) of the+# bpffs or of a mount above it (BpffsMount). A propagation change on the+# bpffs or a mount above it through mount(2), which lsm/sb_mount sees, is+# refused as well (BpffsMount), and a pivot_root in that namespace+# (BpffsUmountAbove). A mount namespace of its own is any process's to+# change and enforcement stays.+#+# Each move is tried with the parent made private first, through+# mount_setattr(2), which is the state the bypass needs, and the move+# has to be refused all the same. The calls are made with the syscalls+# themselves, so that no util-linux picks another one.++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"+command -v pivot_root >/dev/null 2>&1 || test_skip_all "no pivot_root"+C=helper/test-bpf-cmd+[ -x "$C" ] || test_skip_all "test-bpf-cmd helper not built"+B=/run/shirudo/bpffs+W=/tmp/t6158+M=/tmp/t6158-mnt.py+HOLDER=++mountpoint -q "$B" || test_skip_all "no private bpffs"++# The guarded changes must all be refused, so the real mount tree is+# never altered. Restore shared propagation anyway, in case a regression+# let a partial change through before the hook refused the rest.+cleanup() {+ [ -n "$HOLDER" ] && kill "$HOLDER" 2>/dev/null+ umount -l "$W" 2>/dev/null || true+ rm -rf "$W" "$M" 2>/dev/null || true+ mount --make-rshared / 2>/dev/null || true+}+trap cleanup 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" && [ "$(./$C LINK_CREATE)" = "errno=13" ]+}++# One mount call, answered with 0 or the name of its errno.+cat >"$M" <<'PY'+import ctypes as c+import errno+import os+import sys++libc = c.CDLL(None, use_errno=True)+op, args = sys.argv[1], [os.fsencode(a) for a in sys.argv[2:]]+if op == "legacy-move":+ # MS_MOVE+ ret = libc.mount(args[0], args[1], None, c.c_ulong(1 << 13), None)+elif op == "move-mount":+ # AT_FDCWD on both sides, no flags.+ ret = libc.move_mount(-100, args[0], -100, args[1], 0)+elif op == "setattr-private":+ # struct mount_attr: attr_set, attr_clr, propagation, userns_fd.+ attr = (c.c_uint64 * 4)(0, 0, 1 << 18, 0)+ ret = libc.mount_setattr(-100, args[0], 0, c.byref(attr),+ c.c_size_t(c.sizeof(attr)))+else:+ sys.exit(2)+print(0 if ret == 0 else errno.errorcode[c.get_errno()])+PY++mnt_try() {+ python3 "$M" "$@"+}++test_expect_success 'enforcement is live to begin with' '+ enforcing+'++# The reproduction needs the bpffs parent non-shared. A recursive+# make-private from / is one propagation change on the root mount. Force+# the legacy mount(2) path so the assertion is deterministic regardless+# of the host util-linux; it reaches lsm/sb_mount, which the old guard+# let through because the hook-path superblock was not the bpffs tmpfs.+test_expect_success 'a forced legacy recursive make-private of / is refused and counted' '+ before=$(counter BpffsMount) &&+ ! LIBMOUNT_FORCE_MOUNT2=always mount --make-rprivate / 2>/dev/null &&+ enforcing &&+ [ "$(counter BpffsMount)" -gt "$before" ]+'++# Making the bpffs parent itself private is the same precondition by a+# shorter path; also refused, also counted, over the forced legacy path.+test_expect_success 'a forced legacy make-private of /run is refused and counted' '+ before=$(counter BpffsMount) &&+ ! LIBMOUNT_FORCE_MOUNT2=always mount --make-private /run 2>/dev/null &&+ enforcing &&+ [ "$(counter BpffsMount)" -gt "$before" ]+'++# The bypass itself: /run private, then the bpffs moved onto a procfs+# entry that goes away with its task. The kernel alone answers EINVAL+# while /run is shared, so EPERM is the guard's, on either kernel. /run+# is shared again before anything is asserted.+test_expect_success 'the bpffs does not move from a private /run, by either call' '+ sleep 300 &+ HOLDER=$!+ before=$(counter BpffsMount)+ mnt_try setattr-private /run >/dev/null+ legacy=$(mnt_try legacy-move "$B" /proc/$HOLDER/task)+ new=$(mnt_try move-mount "$B" /proc/$HOLDER/task)+ mount --make-shared /run+ kill "$HOLDER" 2>/dev/null+ HOLDER=+ [ "$legacy" = EPERM ] && [ "$new" = EPERM ] &&+ mountpoint -q "$B" &&+ enforcing &&+ [ "$(counter BpffsMount)" -ge "$((before + 2))" ]+'++# One level up: with / private, /run would move with the bpffs in it.+test_expect_success '/run does not move from a private /, by either call' '+ sleep 300 &+ HOLDER=$!+ before=$(counter BpffsMount)+ mnt_try setattr-private / >/dev/null+ legacy=$(mnt_try legacy-move /run /proc/$HOLDER/task)+ new=$(mnt_try move-mount /run /proc/$HOLDER/task)+ mount --make-shared /+ kill "$HOLDER" 2>/dev/null+ HOLDER=+ [ "$legacy" = EPERM ] && [ "$new" = EPERM ] &&+ mountpoint -q "$B" &&+ enforcing &&+ [ "$(counter BpffsMount)" -ge "$((before + 2))" ]+'++# The legacy call names no source to the hook, so it is refused for+# every mount of this namespace; move_mount(2) is not, and gets the+# kernel's own answer for a mount that has nothing to do with the bpffs.+test_expect_success 'a legacy MS_MOVE of an unrelated mount is refused, move_mount is not' '+ mkdir -p "$W/a" "$W/b" &&+ mount --bind "$W/a" "$W/a" &&+ legacy=$(mnt_try legacy-move "$W/a" "$W/b") &&+ new=$(mnt_try move-mount "$W/a" "$W/b")+ umount -l "$W/b" 2>/dev/null+ umount -l "$W/a" 2>/dev/null+ rm -rf "$W"+ [ "$legacy" = EPERM ] && [ -n "$new" ] && [ "$new" != EPERM ]+'++# pivot_root relocates the namespace root and so could strand the bpffs;+# its old root is not passed to the hook, so a pivot by a task without+# authority in the bpffs namespace is refused.+test_expect_success 'pivot_root in the bpffs mount namespace is refused and counted' '+ before=$(counter BpffsUmountAbove) &&+ mkdir -p "$W/put" &&+ mount --bind "$W" "$W" &&+ ! pivot_root "$W" "$W/put" 2>/dev/null &&+ enforcing &&+ [ "$(counter BpffsUmountAbove)" -gt "$before" ]+ pivot=$?+ umount -l "$W" 2>/dev/null+ rm -rf "$W"+ [ "$pivot" = 0 ]+'++# The guards are scoped to init's own mount objects. A mount namespace of+# its own holds copies, so the same propagation change and a legacy move+# are any process's to make there and enforcement is untouched.+test_expect_success 'the same changes in a private mount namespace are harmless' '+ mkdir -p "$W/a" "$W/b" &&+ unshare -m --propagation private sh -c "+ mount --make-rprivate / &&+ mount --bind $W/a $W/a &&+ [ \"\$(python3 $M legacy-move $W/a $W/b)\" = 0 ]+ " &&+ enforcing+'++test_done