eBPF-based security platform
bpf: let any caller make the bpffs ancestry shared
KP Singhea338568e992
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex 8b3dcfb7..b99b260c 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -415,7 +415,11 @@ signed config image.- The production configuration: ``init`` from the initrd, itsmessages in the journal once, adoption, PCR 11 after the binding,pins under ``/run``, a successful PID 1 pivot in the TPM guest,- shared propagation and unchanged mount tamper counts+ shared propagation and unchanged boot mount tamper counts. A+ task other than PID 1 may share the bpffs again and may not make+ it private, through ``mount(2)``. Sharing a plain directory+ under ``/run``, as ``ip netns add`` does, gets the kernel's+ ``EINVAL`` and no ``BpffsMount`` count* - ``t4102-tpm-chain``- Replay, measured load and unload, evidence from the VM's TPM,challenges with ECC and RSA endorsement keysdiff --git a/Documentation/internals/enforcement.rst b/Documentation/internals/enforcement.rstindex 0414a970..a2a9a311 100644--- a/Documentation/internals/enforcement.rst+++ b/Documentation/internals/enforcement.rst@@ -418,8 +418,9 @@ Hooks in bpf2- Closed.* - ``lsm/sb_mount``- A mount onto the bpffs or runtime image. A legacy propagation- change on the bpffs or its ancestry, with the host PID 1- exceptions for the initrd handoff and restoring shared mounts.+ change on the bpffs or its ancestry other than to shared, which+ every caller may request, with the host PID 1 exception for the+ initrd handoff.A mount onto apath in ``/run/shirudo`` or on a config image to a task withoutauthority. The source of a bind or legacy move is not checked.diff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rstindex f5b5f9c7..cb201ef7 100644--- a/Documentation/security/threat-model.rst+++ b/Documentation/security/threat-model.rst@@ -654,8 +654,10 @@ 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 fromthe first to the second. It may change propagation on the bpffs ancestry-while it is on the initrd root and restore shared propagation after the-switch. Before allowing the old root to detach, the unmount guard checks+while it is on the initrd root. Any caller may make a mount shared at any+time. The kernel refuses to move a mount whose parent is shared, so shared+is the state these guards keep: systemd asks for it after the switch and+``ip netns add`` asks for it on ``/run/netns``. Before allowing the old root to detach, the unmount guard checksthat the new root has a copy of the same bpffs subtree outside the reachof the unmount and transfers its protection to that copy.diff --git a/bpf/bpf2.c b/bpf/bpf2.cindex 5849932b..fd5c5ba3 100644--- a/bpf/bpf2.c+++ b/bpf/bpf2.c@@ -1154,6 +1154,13 @@ static __always_inline int shirudo_propagation(struct mount *target,__u32 zero = 0;int i;+ /*+ * The kernel refuses to move a mount whose parent is shared, so a+ * request to share only restores what this guard keeps. Tools ask+ * for it on directories below the ancestry: ip netns on /run/netns.+ */+ if (type == SHIRUDO_MS_SHARED)+ return 0;rec = bpf_map_lookup_elem(&shirudo_mnt_rec, &zero);if (!rec)return 0;@@ -1164,8 +1171,7 @@ static __always_inline int shirudo_propagation(struct mount *target,goto deny;if ((__u64)ns != rec->mount_ns)return 0;- if ((__u32)bpf_get_current_pid_tgid() == 1 &&- (type == SHIRUDO_MS_SHARED || shirudo_initrd_pid1(rec)))+ if (shirudo_initrd_pid1(rec))return 0;bpf_for(i, 0, MOUNT_DEPTH_MAX) {if (m == target)diff --git a/testing/functional/tpm/t4101-initrd.sh b/testing/functional/tpm/t4101-initrd.shindex 6ccee5bb..3c550b52 100755--- a/testing/functional/tpm/t4101-initrd.sh+++ b/testing/functional/tpm/t4101-initrd.sh@@ -78,6 +78,59 @@ test_expect_success 'the runtime command can check initialization after switch-r[ "$(status_field tpm.bound_in)" = initrd ]'+# Any caller may ask for the shared state the guard keeps, and none but+# PID 1 in the initrd for another. A change the guard let through is+# undone before the case fails.+check_propagation_requests()+{+ python3 - <<'PY'+import ctypes as c+import errno++libc = c.CDLL(None, use_errno=True)+target = b"/run/shirudo/bpffs"+private = 1 << 18+shared = 1 << 20++def legacy(kind):+ return libc.mount(None, target, None, c.c_ulong(kind), None)++c.set_errno(0)+result = legacy(shared)+assert result == 0, (result, c.get_errno())+c.set_errno(0)+result = legacy(private)+err = c.get_errno()+if result == 0:+ legacy(shared)+assert result == -1 and err == errno.EPERM, (result, err)+PY+}++# ip netns add shares /run/netns before that is a mount. The request has+# to reach the kernel, which refuses a path that is no mount root.+share_plain_directory()+{+ python3 - "$1" <<'PY'+import ctypes as c+import errno+import os+import sys++libc = c.CDLL(None, use_errno=True)+# MS_SHARED | MS_REC+result = libc.mount(None, os.fsencode(sys.argv[1]), None, c.c_ulong(1 << 20 | 1 << 14), None)+assert result == -1 and c.get_errno() == errno.EINVAL, (result, c.get_errno())+PY+}++bpffs_mount_refusals()+{+ "$SHIRUDO_BIN" status --json 2>/dev/null | python3 -c '+import json, sys+print(json.load(sys.stdin)["tamper_attempts"]["counters"]["BpffsMount"])'+}+# The QEMU TPM suite enables just this syscall exit at boot. A successful# service alone could also mean systemd used its move-mount fallback.if grep -qw 'trace_event=syscalls:sys_exit_pivot_root' /proc/cmdline; then@@ -93,6 +146,21 @@ if grep -qw 'trace_event=syscalls:sys_exit_pivot_root' /proc/cmdline; thenfindmnt -n -o PROPAGATION -T "$path" | grep -qw shared || return 1done'++ test_expect_success 'a non-init caller can share the bpffs again and nothing else through mount(2)' '+ before=$(bpffs_mount_refusals) &&+ check_propagation_requests &&+ [ "$(bpffs_mount_refusals)" -gt "$before" ] &&+ findmnt -n -o PROPAGATION -T /run/shirudo/bpffs | grep -qw shared+ '++ test_expect_success 'sharing a plain directory under /run reaches the kernel with no refusal counted' '+ before=$(bpffs_mount_refusals) &&+ mkdir /run/t4101-plain &&+ share_plain_directory /run/t4101-plain &&+ rmdir /run/t4101-plain &&+ [ "$(bpffs_mount_refusals)" = "$before" ]+ 'fitest_done