Skip to content

eBPF-based security platform

bpf2: refuse an unmount that takes the bpffs mount with it

Daniel Borkmanndbb91dcd097e
diff --git a/Documentation/architecture.rst b/Documentation/architecture.rst
index 22e2108e..4ef8c323 100644
--- a/Documentation/architecture.rst
+++ b/Documentation/architecture.rst
@@ -313,9 +313,12 @@ signer removal happens through signed config activation.
``lsm/sb_umount``, ``lsm/move_mount``,
``lsm/sb_mount`` and ``lsm/sb_remount`` deny unconditionally, for every
caller including shirudo, direct unmount, relocation, mount-over and
-remount of that superblock. A lazy unmount of a parent mount is not covered;
-see residual risk 3 in the threat model and
-`issue 22 <https://github.com/shirudo-dev/shirudo/issues/22>`__.
+remount of that superblock. ``lsm/sb_umount`` also walks what an unmount
+would take down, the subtree of the mount and what propagation carries to
+its peers and slaves, and refuses one that reaches the bpffs mount init
+recorded when it opened the bpffs root with the guards live; a copy in
+another mount namespace is any process's to drop. See residual risk 3 in
+the threat model.
The pins are the only thing
keeping the enforcement programs alive, and one ``umount`` would
otherwise detach everything. Named refusal points increment counters in
diff --git a/Documentation/config-images.rst b/Documentation/config-images.rst
index c38869d2..82aff48b 100644
--- a/Documentation/config-images.rst
+++ b/Documentation/config-images.rst
@@ -409,12 +409,10 @@ ordinary untrusted volume-management tools cannot mutate unrelated DM
mappings either. Read-only control queries remain available, and trusted
image tasks retain control access. Shirudo creates strict mappings, but the
upstream metadata hook does not expose every verification option. Existing
-mount hooks reject direct operations on the bpffs. The mount-tree check
-was reverted because it blocked systemd service startup. A lazy unmount of
-a parent can still detach the bpffs and release its pins when the last
-references disappear; `issue 22
-<https://github.com/shirudo-dev/shirudo/issues/22>`__ tracks this gap.
-The image mount hooks also do not cover every mount attribute change or
+mount hooks reject direct operations on the bpffs, and ``sb_umount``
+refuses an unmount that would take the bpffs mount init made with it, by
+subtree or by propagation (residual risk 3 in the threat model).
+The image mount hooks do not cover every mount attribute change or
unmount of a parent mount above the runtime and config images. Legacy ``mount(MS_MOVE)`` can relocate an image;
the separate `mount-move investigation <https://github.com/shirudo-dev/shirudo/issues/12>`__ records
the VM result and the same source-check gap in the baseline bpffs policy.
diff --git a/Documentation/threat-model.rst b/Documentation/threat-model.rst
index ac20c263..92a8541c 100644
--- a/Documentation/threat-model.rst
+++ b/Documentation/threat-model.rst
@@ -140,9 +140,10 @@ T2 Detaching or replacing enforcement
``inode_unlink`` and ``inode_rename`` there require that authority.
The VFS unlinks a rename's victim without ``inode_unlink``, so both
hooks are needed. ``sb_umount``, ``move_mount``, ``sb_mount`` and
- ``sb_remount`` deny direct operations against that superblock.
- Unmounting a parent mount and legacy mount moves remain gaps,
- described in residual risk 3. Infrastructure maps are
+ ``sb_remount`` deny direct operations against that superblock, and
+ ``sb_umount`` refuses an unmount that would take the bpffs mount
+ init made with it, by subtree or by propagation. Legacy mount moves
+ remain a gap, described in residual risk 3. 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
@@ -418,16 +419,25 @@ Each of these is known, documented and bounded; none is silent.
authorization boundary. The authenticated initrd bootstrap binary and
trusted runtime process memory remain in the TCB.
-3. A lazy unmount of a mount above ``/run/shirudo/bpffs``, such as
- ``umount -l /run``, can detach the bpffs with it. The kernel calls
- ``sb_umount`` for the requested parent mount alone, so the direct
- bpffs check does not stop this. Once its last references disappear,
- the pins can be released and enforcement can detach. The mount-tree
- check was reverted because it blocked systemd service startup.
- `Issue 22 <https://github.com/shirudo-dev/shirudo/issues/22>`__ tracks
- keeping the pins alive without depending on a visible mount tree.
- Mounting over ``/run/shirudo`` only hides the pins; it does not release
- them. Enforcement continues, but state-changing commands refuse
+3. The pins hold the enforcement links through the bpffs superblock,
+ which goes with its last mount. The kernel calls ``sb_umount`` for
+ the requested mount alone, so a lazy unmount of a mount above
+ ``/run/shirudo/bpffs``, ``umount -l /run`` say, took the bpffs with
+ it, and so did one of a copy of ``/run`` in a mount namespace whose
+ mounts still propagated to init's, since systemd makes the whole
+ tree shared again after the switch to the root file system. The
+ guard now walks what an unmount would take, the subtree and what
+ propagation carries to peers and slaves, from the bpffs mount init
+ recorded in its own namespace, and refuses one that reaches it
+ (``BpffsUmountAbove``); a private copy in a namespace of its own is
+ any process's to drop. The walk runs without the namespace lock and
+ within fixed bounds, and refuses a tree it cannot finish, so an
+ unmount of a very large subtree is refused rather than taken for
+ safe. The pins still depend on the mount tree;
+ `issue 22 <https://github.com/shirudo-dev/shirudo/issues/22>`__
+ tracks keeping them alive without it. Mounting over
+ ``/run/shirudo`` only hides the pins; it does not release them.
+ Enforcement continues, but state-changing commands refuse
``lsm_detached`` while their pins are hidden.
Legacy ``mount(MS_MOVE)`` bypasses the source check used by
diff --git a/bpf/admission.h b/bpf/admission.h
index 373c9bdd..030a2923 100644
--- a/bpf/admission.h
+++ b/bpf/admission.h
@@ -438,9 +438,10 @@ 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.
*
- * These checks cover the bpffs itself. A lazy unmount of a parent is not
- * covered: the VFS calls sb_umount for that parent alone. Protecting pins
- * against that case is a separate problem (issue 22).
+ * These checks cover the bpffs itself. The VFS calls sb_umount for the
+ * requested mount alone, so an unmount of a mount above the bpffs, or
+ * of one whose unmount propagates to such a mount, is caught by the walk
+ * in bpf2.c from the mount init recorded.
*/
static __always_inline __u32 shirudo_bpffs_dev(void)
{
diff --git a/bpf/bpf2.c b/bpf/bpf2.c
index 0e13e5d4..8d6242ce 100644
--- a/bpf/bpf2.c
+++ b/bpf/bpf2.c
@@ -11,6 +11,163 @@
char _license[] SEC("license") = "Dual BSD/GPL";
+#define SHIRUDO_MOUNT_WALK_MAX 1024
+#define SHIRUDO_MOUNT_DEPTH_MAX 32
+#define SHIRUDO_MOUNT_MASTERS_MAX 4
+
+static __always_inline struct mount *shirudo_mount_of(struct vfsmount *mnt)
+{
+ return (struct mount *)((__u64)mnt -
+ bpf_core_field_offset(struct mount, mnt));
+}
+
+static __always_inline struct mount *shirudo_mount_child(__u64 entry)
+{
+ return (struct mount *)(entry -
+ bpf_core_field_offset(struct mount, mnt_child));
+}
+
+static __always_inline __u64 shirudo_mount_children(struct mount *m)
+{
+ return (__u64)m + bpf_core_field_offset(struct mount, mnt_mounts);
+}
+
+/* The peer group a mount propagates to, 0 for one that propagates to none. */
+static __always_inline int shirudo_mount_group(struct mount *m)
+{
+ if (!(BPF_CORE_READ(m, mnt_t_flags) & T_SHARED))
+ return 0;
+ return BPF_CORE_READ(m, mnt_group_id);
+}
+
+/*
+ * 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.
+ */
+static __always_inline bool shirudo_umount_reaches(struct mount *m, int group,
+ struct mount *a)
+{
+ int i;
+
+ if (a == m)
+ return true;
+ if (!group)
+ return false;
+ bpf_for(i, 0, SHIRUDO_MOUNT_MASTERS_MAX) {
+ if (shirudo_mount_group(a) == group)
+ return true;
+ a = BPF_CORE_READ(a, mnt_master);
+ if (!a)
+ return false;
+ }
+ return true;
+}
+
+/*
+ * Whether unmounting m unmounts the bpffs mount init made or one above
+ * it. The root of the namespace has no parent to be unmounted from.
+ */
+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, SHIRUDO_MOUNT_DEPTH_MAX) {
+ parent = BPF_CORE_READ(a, mnt_parent);
+ if (!parent || parent == a)
+ return false;
+ if (shirudo_umount_reaches(m, group, a))
+ return true;
+ a = parent;
+ }
+ return true;
+}
+
+/*
+ * The pins hold the enforcement links through the bpffs superblock,
+ * which goes with the last mount of it: a lazy unmount of /run took the
+ * private bpffs with it, and so did one of a copy of /run in another
+ * namespace whose mounts still propagated to init's. Walk what the
+ * unmount of t takes down, its subtree in first-child, next-sibling
+ * order without a stack and through each mount what propagation takes
+ * along, and refuse one that reaches the recorded mount or one above
+ * it. The namespace lock is not held here, so a tree that cannot be
+ * walked within the bounds is refused rather than taken for safe.
+ */
+static __always_inline int shirudo_umount_above(struct vfsmount *mnt)
+{
+ struct shirudo_mount_record *rec;
+ struct mount *t, *m, *bpffs, *parent;
+ __u32 zero = 0;
+ bool sibling;
+ __u64 next;
+ int i, j;
+
+ rec = bpf_map_lookup_elem(&shirudo_mnt_rec, &zero);
+ if (!rec || !rec->mount)
+ return 0;
+ bpffs = (struct mount *)rec->mount;
+ t = shirudo_mount_of(mnt);
+ m = t;
+ bpf_for(i, 0, SHIRUDO_MOUNT_WALK_MAX) {
+ if (shirudo_umount_takes_bpffs(m, bpffs))
+ goto deny;
+ next = (__u64)BPF_CORE_READ(m, mnt_mounts.next);
+ if (!next)
+ goto deny;
+ if (next != shirudo_mount_children(m)) {
+ m = shirudo_mount_child(next);
+ continue;
+ }
+ sibling = false;
+ bpf_for(j, 0, SHIRUDO_MOUNT_DEPTH_MAX) {
+ if (m == t)
+ return 0;
+ parent = BPF_CORE_READ(m, mnt_parent);
+ next = (__u64)BPF_CORE_READ(m, mnt_child.next);
+ if (!parent || !next)
+ goto deny;
+ if (next != shirudo_mount_children(parent)) {
+ m = shirudo_mount_child(next);
+ sibling = true;
+ break;
+ }
+ m = parent;
+ }
+ if (!sibling)
+ goto deny;
+ }
+deny:
+ return shirudo_tamper_deny(SHIRUDO_TAMPER_BPFFS_UMOUNT_ABOVE, -EPERM);
+}
+
+/*
+ * The bpffs mount of init's namespace, recorded once when the bootstrap
+ * task opens its root with the mount guards live: the one the unmount
+ * guard keeps, where a copy in another namespace is any process's to
+ * drop.
+ */
+SEC("lsm/file_open")
+int BPF_PROG(shirudo_lsm_file_open, struct file *file, int ret)
+{
+ struct shirudo_sb_id *id = shirudo_policy();
+ struct shirudo_mount_record *rec;
+ struct vfsmount *mnt;
+ __u32 zero = 0;
+
+ if (ret || !id || !shirudo_bootstrap_task())
+ return ret;
+ mnt = BPF_CORE_READ(file, f_path.mnt);
+ if (!mnt || BPF_CORE_READ(mnt, mnt_sb, s_dev) != id->sb_dev)
+ return 0;
+ rec = bpf_map_lookup_elem(&shirudo_mnt_rec, &zero);
+ if (rec && !rec->mount)
+ rec->mount = (__u64)shirudo_mount_of(mnt);
+ return 0;
+}
+
static __always_inline int shirudo_parent_mount(struct super_block *sb,
bool trusted_target)
{
@@ -59,6 +216,8 @@ int BPF_PROG(shirudo_lsm_sb_umount, struct vfsmount *mnt, int flags,
return shirudo_tamper_deny(SHIRUDO_TAMPER_BPFFS_UMOUNT,
-EPERM);
ret = shirudo_deny_bpffs_mount(dev, SHIRUDO_TAMPER_BPFFS_UMOUNT);
+ if (!ret)
+ ret = shirudo_umount_above(mnt);
return ret ? ret : shirudo_image_mount(sb, true);
}
diff --git a/bpf/lib/shirudo_identity.h b/bpf/lib/shirudo_identity.h
index fb1e6ca6..44a37974 100644
--- a/bpf/lib/shirudo_identity.h
+++ b/bpf/lib/shirudo_identity.h
@@ -29,4 +29,16 @@ struct {
__type(value, struct shirudo_sb_id);
} shirudo_bpffs_sb SEC(".maps");
+/* The bpffs mount of init's namespace, as the unmount guard keeps it. */
+struct shirudo_mount_record {
+ __u64 mount;
+};
+
+struct {
+ __uint(type, BPF_MAP_TYPE_ARRAY);
+ __uint(max_entries, 1);
+ __type(key, __u32);
+ __type(value, struct shirudo_mount_record);
+} shirudo_mnt_rec SEC(".maps");
+
#endif /* SHIRUDO_BPF_IDENTITY_H */
diff --git a/bpf/verity_policy.h b/bpf/verity_policy.h
index c4dd7a1d..5b77d47c 100644
--- a/bpf/verity_policy.h
+++ b/bpf/verity_policy.h
@@ -65,7 +65,8 @@ static __always_inline bool shirudo_private_map(struct bpf_map *map)
char name[16] = {};
bpf_probe_read_kernel_str(name, sizeof(name), map->name);
- return !__builtin_memcmp(name, "shirudo_devices", sizeof(name));
+ return !__builtin_memcmp(name, "shirudo_devices", sizeof(name)) ||
+ !__builtin_memcmp(name, "shirudo_mnt_rec", sizeof(name));
}
/* Device pointers can be reused even when the free hook did not run. */
diff --git a/builtin-init.c b/builtin-init.c
index 0e10678f..c4236e01 100644
--- a/builtin-init.c
+++ b/builtin-init.c
@@ -452,6 +452,32 @@ static int attach_guard_group(struct bpf_object *obj,
return 0;
}
+/*
+ * The recorder in the mount guards sees the bootstrap task open the
+ * bpffs root; the record is read back, since a guard with none to keep
+ * would let an unmount above the bpffs through.
+ */
+static int record_bpffs_mount(struct bpf_object *obj)
+{
+ struct shirudo_mount_record rec = {};
+ struct bpf_map *map;
+ __u32 zero = 0;
+ int fd;
+
+ fd = open(SHIRUDO_BPFFS_DIR, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
+ if (fd < 0)
+ return -errno;
+ close(fd);
+ map = bpf_object__find_map_by_name(obj, "shirudo_mnt_rec");
+ if (!map)
+ return -ENOENT;
+ if (bpf_map_lookup_elem(bpf_map__fd(map), &zero, &rec))
+ return -errno;
+ if (!rec.mount)
+ return -ENOENT;
+ return bpf_map_freeze(bpf_map__fd(map)) ? -errno : 0;
+}
+
/* Writable state and runtime processes always get the full seal. */
static int attach_seal(struct bpf_object *policy, struct bpf_object **obj_out,
struct bpf_link **links, struct bpf_link **image_links)
@@ -780,6 +806,11 @@ static int init_lsm(void)
ARRAY_SIZE(shirudo_mount_guards), mount_links);
if (err)
goto out;
+ err = record_bpffs_mount(obj);
+ if (err) {
+ pr_err("record the bpffs mount: %s", strerror(-err));
+ goto out;
+ }
err = attach_seal(obj, &seal_obj, seal_links, image_seal);
if (err)
diff --git a/util/identity.h b/util/identity.h
index bdd1d7e5..5669ad5b 100644
--- a/util/identity.h
+++ b/util/identity.h
@@ -68,6 +68,14 @@ _Static_assert(sizeof(struct shirudo_task_authority) == 8,
_Static_assert(sizeof(struct shirudo_identity) == 508,
"frozen identity ABI");
+/* The bpffs mount init made, which the unmount guard keeps mounted. */
+struct shirudo_mount_record {
+ __u64 mount;
+};
+
+_Static_assert(sizeof(struct shirudo_mount_record) == 8,
+ "mount record ABI");
+
/*
* The identity map init populated, froze and pinned this boot. -ENOENT
* when init has not run, another -errno when the pin cannot be read.
diff --git a/util/image_guards.c b/util/image_guards.c
index f88c5110..71d07c30 100644
--- a/util/image_guards.c
+++ b/util/image_guards.c
@@ -73,6 +73,7 @@ shirudo_mount_guards[SHIRUDO_MOUNT_HOOK_COUNT] = {
{ "shirudo_lsm_move_mount", SHIRUDO_MOVE_MOUNT_LINK_PIN },
{ "shirudo_lsm_sb_mount", SHIRUDO_SB_MOUNT_LINK_PIN },
{ "shirudo_lsm_sb_remount", SHIRUDO_SB_REMOUNT_LINK_PIN },
+ { "shirudo_lsm_file_open", SHIRUDO_BPFFS_DIR "/bpf2_file_open_link" },
};
const struct shirudo_image_guard
diff --git a/util/image_guards.h b/util/image_guards.h
index df9d5e21..cb75e8d3 100644
--- a/util/image_guards.h
+++ b/util/image_guards.h
@@ -10,7 +10,7 @@
#define SHIRUDO_SEAL_HOOK_COUNT 19
#define SHIRUDO_IMAGE_SEAL_HOOK_COUNT 2
-#define SHIRUDO_MOUNT_HOOK_COUNT 4
+#define SHIRUDO_MOUNT_HOOK_COUNT 5
#define SHIRUDO_OBSERVER_HOOK_COUNT 4
#define SHIRUDO_IMAGE_OBSERVER_HOOK_COUNT 4