Skip to content

eBPF-based security platform

bpf2: take an unmount for one that propagates to a mount, not to its peers

Daniel Borkmann77ad58af28a8
diff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rst
index 4a207c69..cece1b59 100644
--- a/Documentation/security/threat-model.rst
+++ b/Documentation/security/threat-model.rst
@@ -591,9 +591,10 @@ mount. ``lsm/sb_umount``, ``lsm/move_mount``, ``lsm/sb_mount`` and
``lsm/sb_remount`` refuse every caller, shirudo included, an unmount, a
move, a mount over or a remount of the private bpffs (``BpffsUmount``,
``BpffsMount``). The kernel asks ``sb_umount`` only about the mount it
-was given, so the hook also walks what the unmount would take, the
-subtree and its propagation peers and slaves, from the bpffs mount
-``init`` recorded, and refuses an unmount that reaches it
+was given, so the hook also walks what the unmount would take: the
+subtree, and for each mount in it the copies that propagation made at
+its mountpoint on the peers and slaves of its parent, from the bpffs
+mount ``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
@@ -1480,9 +1481,7 @@ R22 Labeled inodes are interchangeable
package or tree state read from the wrong file. This is a reading of
the code and is not reproduced. A bind mount over the state root in
``init``'s namespace, an idmapped one among them, was reproduced:
- every command refuses the state as foreign from then on, and the
- mount cannot be removed while the unmount guard refuses every
- unmount of a mount in the root's propagation group (R3).
+ every command refuses the state as foreign from then on.
R23 Policy labels follow the inode
A policy label is an xattr of the executable's inode, applied at
diff --git a/bpf/bpf2.c b/bpf/bpf2.c
index 570f82d2..2550bdb8 100644
--- a/bpf/bpf2.c
+++ b/bpf/bpf2.c
@@ -568,24 +568,36 @@ static __always_inline int shirudo_mount_group(struct mount *m)
}
/*
- * Whether unmounting m unmounts a: a is m, a peer of m, or a slave of
- * m's peer group, directly or through masters of its own.
+ * Whether unmounting m unmounts a: a is m, or a copy of m that
+ * propagation made, at m's mountpoint on a peer of m's parent or on a
+ * slave of that peer group, directly or through masters of its own. A
+ * peer of m itself at another mountpoint stays: a bind of a shared
+ * mount is a peer of its source, and the root's binds are peers of the
+ * root.
*/
-static __always_inline bool shirudo_umount_reaches(struct mount *m, int group,
+static __always_inline bool shirudo_umount_reaches(struct mount *m,
struct mount *a)
{
- int i;
+ struct mount *parent;
+ int group, i;
if (a == m)
return true;
+ if (BPF_CORE_READ(a, mnt_mp) != BPF_CORE_READ(m, mnt_mp))
+ return false;
+ parent = BPF_CORE_READ(m, mnt_parent);
+ if (!parent)
+ return true;
+ group = shirudo_mount_group(parent);
if (!group)
return false;
+ parent = BPF_CORE_READ(a, mnt_parent);
bpf_for(i, 0, MOUNT_MASTERS_MAX) {
- if (shirudo_mount_group(a) == group)
- return true;
- a = BPF_CORE_READ(a, mnt_master);
- if (!a)
+ if (!parent)
return false;
+ if (shirudo_mount_group(parent) == group)
+ return true;
+ parent = BPF_CORE_READ(parent, mnt_master);
}
return true;
}
@@ -598,14 +610,13 @@ static __always_inline bool shirudo_umount_takes_bpffs(struct mount *m,
struct mount *bpffs)
{
struct mount *a = bpffs, *parent;
- int group = shirudo_mount_group(m);
int i;
bpf_for(i, 0, MOUNT_DEPTH_MAX) {
parent = BPF_CORE_READ(a, mnt_parent);
if (!parent || parent == a)
return false;
- if (shirudo_umount_reaches(m, group, a))
+ if (shirudo_umount_reaches(m, a))
return true;
a = parent;
}
diff --git a/testing/functional/tamperproof/t6120-umount-above.sh b/testing/functional/tamperproof/t6120-umount-above.sh
index 6efe0ce0..c7168d34 100755
--- a/testing/functional/tamperproof/t6120-umount-above.sh
+++ b/testing/functional/tamperproof/t6120-umount-above.sh
@@ -46,6 +46,17 @@ test_expect_success 'enforcement is live to begin with' '
enforcing
'
+# A bind of a shared mount is a peer of its source: the root's binds are
+# peers of the root, under which the bpffs sits, and unmounting one must
+# not count as unmounting the root.
+test_expect_success 'a bind mount of a root file system path is any process'"'"'s to unmount' '
+ mkdir -p "$W/bind" &&
+ mount --bind /var/lib "$W/bind" &&
+ umount "$W/bind" &&
+ ! mountpoint -q "$W/bind" &&
+ enforcing
+'
+
test_expect_success 'a lazy unmount of /run is refused and counted' '
before=$(counter BpffsUmountAbove) &&
! umount -l /run 2>/dev/null &&