Skip to content

eBPF-based security platform

bpf: refuse the mount moves that take the bpffs along

John Fastabend60b42c62f912
diff --git a/Documentation/internals/enforcement.rst b/Documentation/internals/enforcement.rst
index a2a9a311..a08567de 100644
--- a/Documentation/internals/enforcement.rst
+++ b/Documentation/internals/enforcement.rst
@@ -412,18 +412,22 @@ Hooks in bpf2
* - ``lsm/move_mount``
- To every caller: moving the bpffs, the ``/run/shirudo`` mount or
the runtime image, or moving a mount onto the bpffs or the runtime
- image. Moving a mount onto a path in ``/run/shirudo``, and moving
- a config image or onto one, to a task without authority.
+ image. Moving a mount above the bpffs, which takes the bpffs
+ along, to every caller but host PID 1 on the initrd root. Moving
+ a mount onto a path in ``/run/shirudo``, and moving a config
+ image or onto one, to a task without authority.
- ``EPERM``: ``BpffsMount``; others uncounted
- Closed.
* - ``lsm/sb_mount``
- A mount onto the bpffs or runtime image. A legacy propagation
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 a
+ initrd handoff. A legacy ``MS_MOVE`` onto any mount of the
+ recorded bpffs's mount namespace, which reaches the hook without
+ its source, with the same PID 1 exception; ``move_mount(2)``
+ moves a mount there. A mount onto a
path in ``/run/shirudo`` or on a config image to a task without
- authority. The source of a bind or legacy move is not checked.
+ authority. The source of a bind is not checked.
- ``EPERM``: ``BpffsMount``; others uncounted
- Closed.
* - ``lsm/sb_remount``
@@ -650,7 +654,7 @@ The hooks decide only what reaches them. These paths do not:
program descriptor are not hooked here.
- On the pinned kernel, propagation changes through ``mount_setattr(2)``
have no LSM hook. The source of a bind or legacy move is absent from
- ``lsm/sb_mount``.
+ ``lsm/sb_mount``, which refuses the legacy move for that reason.
- A perf event bound to a task before that task execs the runtime image
survives the exec, and CPU-wide events and probes on the runtime
binary are not checked.
diff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rst
index 5083451a..03951150 100644
--- a/Documentation/security/threat-model.rst
+++ b/Documentation/security/threat-model.rst
@@ -657,7 +657,17 @@ the first to the second. It may change propagation on the bpffs ancestry
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 checks
+``ip netns add`` asks for it on ``/run/netns``. That refusal does not
+hold on its own, since ``mount_setattr(2)`` changes propagation without
+a hook on the pinned kernel (R3), and a legacy ``MS_MOVE`` reaches
+``lsm/sb_mount`` with its target alone. The hook therefore refuses
+every legacy move onto a mount of the protected namespace
+(``BpffsMount``), whatever it moves, to every caller but host PID 1 on
+the initrd root. A move there goes through ``move_mount(2)``, where
+``lsm/move_mount`` sees both paths and refuses, with the same exception,
+a move of the bpffs or of a mount above it, which would take the bpffs
+along. A program that knows only the legacy call no longer moves a
+mount in that namespace. Before allowing the old root to detach, the unmount guard checks
that the new root has a copy of the same bpffs subtree outside the reach
of the unmount and transfers its protection to that copy.
@@ -1680,17 +1690,18 @@ R3 Pins depend on the mount tree
The pins hold the links through the bpffs superblock. The guards of
T2 cover unmounts of the bpffs and of mounts above it in ``init``'s
namespace. A private copy of the tree in another namespace is any
- process's to drop, which releases nothing. Legacy ``mount(2)`` with
- ``MS_MOVE`` reaches ``lsm/sb_mount`` with the target only, so the
- source mount is not checked (D8). On the pinned kernel,
+ process's to drop, which releases nothing. On the pinned kernel,
``mount_setattr(2)`` has no propagation hook, so root can change
- mount attributes and propagation, making the bpffs read-only for
- instance. A mount over ``/run`` or an ancestor hides the pins without
+ mount attributes and propagation, making the bpffs read-only or the
+ mounts above it private for instance. A private parent no longer
+ lets the bpffs move: ``lsm/sb_mount`` refuses every legacy move in
+ that namespace and ``lsm/move_mount`` a move of the bpffs or of a
+ mount above it. A mount over ``/run`` or an ancestor hides the pins without
releasing them: enforcement continues, ``shirudo status`` reports the
LSM as not attached, and ``upatch load`` and ``upatch unload`` refuse
``tampered`` with the message ``lsm_detached`` in their pre-flight.
The walk's bounds refuse every unmount of a tree they cannot cover.
- Mitigations: D7, D8, D11.
+ Mitigations: D7, D11.
R4 Config PCR writers are not authenticated
The config PCR, one of 0 to 15 other than 9 and 11 chosen in the
@@ -2251,10 +2262,6 @@ D7 Pins without the mount tree
mount (R3), tracked in `issue 22
<https://github.com/shirudo-dev/shirudo/issues/22>`__.
-D8 Legacy mount moves
- Check the source of a legacy ``MS_MOVE`` (R3), tracked in `issue 12
- <https://github.com/shirudo-dev/shirudo/issues/12>`__.
-
D9 Perf events across exec
Refuse task-bound perf events that a task opened before the exec
that marks it (T3), tracked in `issue 11
diff --git a/bpf/bpf2.c b/bpf/bpf2.c
index 428cc341..37c2d24c 100644
--- a/bpf/bpf2.c
+++ b/bpf/bpf2.c
@@ -721,6 +721,7 @@ char _license[] SEC("license") = "Dual BSD/GPL";
#define MOUNT_CHILDREN_MAX 64
#define MOUNT_CHAIN_MAX 8
#define SHIRUDO_MNT_DETACH 2
+#define SHIRUDO_MS_MOVE (1UL << 13)
#define SHIRUDO_MS_REC (1UL << 14)
#define SHIRUDO_MS_SILENT (1UL << 15)
#define SHIRUDO_MS_SHARED (1UL << 20)
@@ -1127,9 +1128,12 @@ static __always_inline bool shirudo_initrd_pid1(const struct mount_record *rec)
/*
* Refuse a change to the recorded bpffs mount or to a mount above it,
* in the namespace bootstrap recorded, to every caller but PID 1 on the
- * initrd root.
+ * initrd root. A legacy move names only the mount it moves onto, so
+ * whole_ns refuses it on any mount of that namespace: the kernel moves a
+ * mount of a namespace only onto a mount of the same one.
*/
-static __always_inline int shirudo_bpffs_tree_change(struct mount *target)
+static __always_inline int shirudo_bpffs_tree_change(struct mount *target,
+ bool whole_ns)
{
struct mnt_namespace *ns;
struct mount_record *rec;
@@ -1149,6 +1153,8 @@ static __always_inline int shirudo_bpffs_tree_change(struct mount *target)
return 0;
if (shirudo_initrd_pid1(rec))
return 0;
+ if (whole_ns)
+ goto deny;
bpf_for(i, 0, MOUNT_DEPTH_MAX) {
if (m == target)
break;
@@ -1173,7 +1179,7 @@ static __always_inline int shirudo_propagation(struct mount *target,
*/
if (type == SHIRUDO_MS_SHARED)
return 0;
- return shirudo_bpffs_tree_change(target);
+ return shirudo_bpffs_tree_change(target, false);
}
SEC("lsm/move_mount")
@@ -1182,6 +1188,7 @@ int BPF_PROG(shirudo_lsm_move_mount, const struct path *from_path,
{
struct super_block *from = BPF_CORE_READ(from_path, mnt, mnt_sb);
struct super_block *to = BPF_CORE_READ(to_path, mnt, mnt_sb);
+ struct vfsmount *vfsmnt;
if (ret)
return ret;
@@ -1193,6 +1200,11 @@ int BPF_PROG(shirudo_lsm_move_mount, const struct path *from_path,
shirudo_deny_bpffs_mount(BPF_CORE_READ(to, s_dev),
TAMPER_BPFFS_MOUNT))
return -EPERM;
+ /* A mount above the bpffs takes it along wherever it goes. */
+ vfsmnt = BPF_CORE_READ(from_path, mnt);
+ ret = shirudo_bpffs_tree_change(shirudo_mount_of(vfsmnt), false);
+ if (ret)
+ return ret;
ret = shirudo_image_mount(from, true);
return ret ? ret : shirudo_image_mount(to, true);
}
@@ -1234,6 +1246,13 @@ int BPF_PROG(shirudo_lsm_sb_mount, const char *dev_name,
if (ret)
return ret;
+ if (flags & SHIRUDO_MS_MOVE) {
+ struct vfsmount *vfsmnt = BPF_CORE_READ(path, mnt);
+
+ ret = shirudo_bpffs_tree_change(shirudo_mount_of(vfsmnt), true);
+ if (ret)
+ return ret;
+ }
if ((flags & PROPAGATION_TYPES) &&
!(flags & ~(PROPAGATION_TYPES | SHIRUDO_MS_REC | SHIRUDO_MS_SILENT))) {
struct vfsmount *vfsmnt = BPF_CORE_READ(path, mnt);