Skip to content

eBPF-based security platform

bpf: refuse unmounting any mount above the private bpffs

Daniel Borkmannd2ffd1606fc7
diff --git a/Documentation/architecture.rst b/Documentation/architecture.rst
index ece1ab52..4a3eebae 100644
--- a/Documentation/architecture.rst
+++ b/Documentation/architecture.rst
@@ -278,7 +278,9 @@ unlinking a key is ``shirudo keys revoke`` and nothing else.
``lsm/sb_umount``, ``lsm/move_mount``,
``lsm/sb_mount`` and ``lsm/sb_remount`` deny unconditionally, for every
caller including shirudo, any unmount, relocation, mount-over or
-remount of that superblock. This last set is load-bearing, not
+remount of that superblock, and ``sb_umount`` refuses any mount with
+the bpffs beneath it, which a lazy unmount would take down too. This
+last set is load-bearing, not
defence in depth: shirudo is agentless, the pins are the only thing
keeping the enforcement programs alive, and one ``umount`` would
otherwise detach everything.
diff --git a/Documentation/threat-model.rst b/Documentation/threat-model.rst
index adbc136f..03b84dfc 100644
--- a/Documentation/threat-model.rst
+++ b/Documentation/threat-model.rst
@@ -129,7 +129,10 @@ T2 Detaching or replacing enforcement
drop its only reference), and ``sb_umount``, ``move_mount``,
``sb_mount`` and ``sb_remount`` deny everyone, so neither ``rm``
nor ``mv`` nor ``umount`` nor mounting over the directory removes a
- pin. Infrastructure maps are
+ pin; ``sb_umount`` also refuses any mount with the bpffs beneath
+ it, so a lazy unmount of ``/run`` or of a bind of it, which would
+ take the subtree down with the hook fired for the ancestor alone,
+ is refused as well. Infrastructure maps are
frozen at creation, so even with the LSM gone userspace could not
rewrite the identity map. ``lsm/bpf_map`` denies writable map fds
to every process except shirudo; map updates and freezes have the
@@ -292,10 +295,17 @@ Each of these is known, documented and bounded; none is silent.
``CapabilityBoundingSet=`` and container capability profiles.
3. A lazy (``MNT_DETACH``) unmount of an ancestor of
- ``/run/shirudo/bpffs`` tears the subtree down without firing
- ``sb_umount`` on the child. The ancestor is ``/run`` or the root
- filesystem, a far higher bar than the direct unmount the guards
- close. Mounting over ``/run/shirudo`` hides the pins without
+ ``/run/shirudo/bpffs`` tears the subtree down and fires
+ ``sb_umount`` for the ancestor alone, so ``sb_umount`` walks the
+ mount tree below its target and refuses one that has the bpffs
+ beneath it, in the caller's own namespace, where a copy of the
+ bpffs mount sits wherever a propagated unmount would reach the
+ original. The walk is bounded; a tree it cannot cover is refused.
+ The cost is that a tree holding a bind of the bpffs cannot be
+ unmounted by anyone until the boot ends, which nothing legitimate
+ does. A namespace torn down with its last task frees its copies
+ without any unmount, and the original mount keeps the superblock.
+ Mounting over ``/run/shirudo`` hides the pins without
detaching anything: enforcement stays, and every state-changing
command refuses ``lsm_detached`` until the mount is gone.
diff --git a/bpf/bpf2.c b/bpf/bpf2.c
index 949daf49..61974eb0 100644
--- a/bpf/bpf2.c
+++ b/bpf/bpf2.c
@@ -76,7 +76,9 @@
* lsm/sb_remount -- mount-lifecycle lockdown of the private bpffs
* superblock (identified by s_dev). Unconditional
* deny: no caller, not even shirudo, may unmount,
- * relocate, mount over, or remount it for the boot.
+ * relocate, mount over, or remount it for the boot,
+ * nor unmount any mount with the bpffs beneath it,
+ * which a lazy unmount would take down with it.
* Without this a single `umount` of the bpffs drops
* the pinned links and detaches ALL enforcement,
* since shirudo is agentless and the pins are the
@@ -451,10 +453,11 @@ int BPF_PROG(shirudo_lsm_inode_removexattr, struct mnt_idmap *idmap,
* hooks are self-protecting: their own pins live in the bpffs they keep
* mounted.
*
- * Residual: a lazy unmount (MNT_DETACH) of an *ancestor* mount can tear
- * down the subtree without firing sb_umount on this child; that needs
- * the ancestor (e.g. /var) to be unmountable, a far higher bar than the
- * trivial direct unmount closed here.
+ * A lazy unmount (MNT_DETACH) of a mount above the bpffs, /run or the
+ * root, takes the subtree down with it and fires sb_umount for the
+ * ancestor alone, so sb_umount also refuses any mount that has the
+ * bpffs beneath it. Nothing unmounts /run or the root on a live
+ * system; switch-root moves /run and never unmounts it.
*/
static __always_inline int shirudo_deny_bpffs_mount(__u32 sb_dev)
{
@@ -467,10 +470,80 @@ static __always_inline int shirudo_deny_bpffs_mount(__u32 sb_dev)
return sb_dev == id->sb_dev ? -EPERM : 0;
}
+/* The address a trusted pointer holds, as a plain number. */
+static __always_inline unsigned long shirudo_ptr_addr(const void *ptr)
+{
+ unsigned long addr = 0;
+
+ bpf_probe_read_kernel(&addr, sizeof(addr), &ptr);
+ return addr;
+}
+
+#define shirudo_mount_of(ptr, member) \
+ ((struct mount *)((unsigned long)(ptr) - \
+ bpf_core_field_offset(struct mount, member)))
+
+#define SHIRUDO_MOUNT_WALK_MAX 4096
+#define SHIRUDO_MOUNT_DEPTH_MAX 64
+
+/*
+ * Whether `root` or a mount below it is of the file system `sb_dev`:
+ * the subtree an unmount of `root` takes down. The walk is over the
+ * caller's own mount tree, so a copy of the bpffs mount in another
+ * namespace is found where a propagated unmount would reach it.
+ * Stackless: down to the first child, else across to the next sibling
+ * of the nearest ancestor that has one, until the walk is back at
+ * `root`. A tree the budget cannot cover is taken to hold the bpffs.
+ */
+static __always_inline bool shirudo_mount_tree_has(struct mount *root,
+ __u32 sb_dev)
+{
+ struct mount *cur = root, *parent;
+ struct list_head *next;
+ bool climbed;
+ int i, j;
+
+ bpf_for(i, 0, SHIRUDO_MOUNT_WALK_MAX) {
+ if (BPF_CORE_READ(cur, mnt.mnt_sb, s_dev) == sb_dev)
+ return true;
+ next = BPF_CORE_READ(cur, mnt_mounts.next);
+ if (next != &cur->mnt_mounts) {
+ cur = shirudo_mount_of(next, mnt_child);
+ continue;
+ }
+ climbed = false;
+ bpf_for(j, 0, SHIRUDO_MOUNT_DEPTH_MAX) {
+ if (cur == root)
+ return false;
+ parent = BPF_CORE_READ(cur, mnt_parent);
+ next = BPF_CORE_READ(cur, mnt_child.next);
+ if (next != &parent->mnt_mounts) {
+ cur = shirudo_mount_of(next, mnt_child);
+ climbed = true;
+ break;
+ }
+ cur = parent;
+ }
+ if (!climbed)
+ return true;
+ }
+ return true;
+}
+
SEC("lsm/sb_umount")
int BPF_PROG(shirudo_lsm_sb_umount, struct vfsmount *mnt, int flags)
{
- return shirudo_deny_bpffs_mount(BPF_CORE_READ(mnt, mnt_sb, s_dev));
+ struct shirudo_sb_id *id;
+ struct mount *target;
+ __u32 zero = 0;
+
+ id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
+ if (!id || id->sb_dev == 0)
+ return 0;
+ if (BPF_CORE_READ(mnt, mnt_sb, s_dev) == id->sb_dev)
+ return -EPERM;
+ target = shirudo_mount_of(shirudo_ptr_addr(mnt), mnt);
+ return shirudo_mount_tree_has(target, id->sb_dev) ? -EPERM : 0;
}
SEC("lsm/move_mount")
diff --git a/testing/tamperproof/t1113-mount-lockdown.sh b/testing/tamperproof/t1113-mount-lockdown.sh
index 8ded47fd..0653c937 100755
--- a/testing/tamperproof/t1113-mount-lockdown.sh
+++ b/testing/tamperproof/t1113-mount-lockdown.sh
@@ -9,6 +9,10 @@
# the whole mount lifecycle of that superblock, so a plain (or lazy)
# `umount` of /run/shirudo/bpffs -- which before these hooks silently
# disabled every LSM program -- is refused, and enforcement survives.
+# sb_umount also refuses a mount with the bpffs beneath it, since a lazy
+# unmount of /run or of a bind of it takes the subtree down and fires
+# the hook for the ancestor alone; that is tried in a private mount
+# namespace, where a regression would cost only the namespace's copies.
#
# Non-destructive when the guards work (the operations are denied, so
# nothing changes). If a guard has regressed and the bpffs actually gets
@@ -54,6 +58,22 @@ test_expect_success 'remount of the bpffs is denied' '
[ "$(./$H)" = "errno=13" ]
'
+test_expect_success 'lazy umount of a mount above the bpffs is denied' '
+ unshare -m sh -c "
+ mount -t tmpfs none /mnt && mkdir -p /mnt/b /mnt/plain &&
+ mount --bind $B /mnt/b && mount -t tmpfs none /mnt/plain &&
+ umount -l /mnt/plain &&
+ ! umount -l /mnt 2>/dev/null && mountpoint -q /mnt/b" &&
+ mountpoint -q "$B" &&
+ [ "$(./$H)" = "errno=13" ]
+'
+
+test_expect_success 'lazy umount of /run itself is denied' '
+ unshare -m sh -c "! umount -l /run 2>/dev/null && mountpoint -q $B" &&
+ mountpoint -q "$B" &&
+ [ "$(./$H)" = "errno=13" ]
+'
+
test_expect_success 'mounting over the bpffs is denied' '
mount -t tmpfs none "$B" 2>/dev/null
# A successful shadow-mount would leave a tmpfs (not bpf) on top.
diff --git a/testing/tamperproof/test.plan.md b/testing/tamperproof/test.plan.md
index 35fd1b98..ffc5e8c5 100644
--- a/testing/tamperproof/test.plan.md
+++ b/testing/tamperproof/test.plan.md
@@ -27,7 +27,7 @@ checks that Shirudo can still export the pre-existing Python exec record.
| TP-05 | Add a key to the sealed BPF keyring | Covered | `t1121-bpf-keyring.sh` | Keep sealing denial and enrollment checks. |
| TP-06 | Unlink, clear, revoke, invalidate, re-permission, timeout, or link a key in the anchor | Covered | `t1125-key-permission.sh` | Keep per-operation denial assertions and status check. |
| TP-07 | Root modifies a labelled file under `/var/lib/shirudo` or changes its xattr | Covered | `t1101-seal.sh` | Keep write and xattr denial assertions. |
-| TP-08 | Direct or lazy unmount, remount, or mount over private bpffs | Covered | `t1113-mount-lockdown.sh` | Keep mountpoint and enforcement-survival checks. |
+| TP-08 | Direct or lazy unmount, remount, or mount over private bpffs, or lazy unmount of a mount above it | Covered | `t1113-mount-lockdown.sh` | Keep mountpoint and enforcement-survival checks; the ancestor case runs in a private mount namespace. |
| TP-09 | Obtain writable serializer-array or arena FD by pinned path | Covered | `t1130-map-lockdown.sh` | Current test covers `shirudo_ser` and `obs_i_arena`. |
| TP-10 | Obtain writable map FD by map ID | Covered | `t1130-map-lockdown.sh` | Current test covers both maps. |
| TP-11 | Read-only map FD access as a positive control | Covered | `t1130-map-lockdown.sh` | Preserve both path and map-ID read-only checks. |