eBPF-based security platform
bpf: guard rename inside the private bpffs
Daniel Borkmann4ccbea8b52e0
diff --git a/Documentation/architecture.rst b/Documentation/architecture.rstindex b6426b7f..ece1ab52 100644--- a/Documentation/architecture.rst+++ b/Documentation/architecture.rst@@ -262,11 +262,12 @@ the serials of the keys the keyring holds) is written and``BPF_MAP_FREEZE``'d before any link is created, so userspace cannotchange it for the rest of the boot. Writable map fds and map updatesare restricted by the same executable-identity gate.-``lsm/inode_unlink``,+``lsm/inode_unlink``, ``lsm/inode_rename``,``lsm/inode_setxattr`` and ``lsm/inode_removexattr`` on shirudo's bpffsallow the operation only when the calling task's ``mm->exe_file`` is-shirudo's binary: root ``rm`` cannot drop a pin, and nobody else can-forge the payload xattr. ``lsm/key_permission`` applies the same test+shirudo's binary: root ``rm`` cannot drop a pin, ``mv`` cannot replace+one (the VFS unlinks a rename's victim without ``inode_unlink``), and+nobody else can forge the payload xattr. ``lsm/key_permission`` applies the same testto the trust anchor in the kernel's keyrings: write, setattr and unlinkon the sealed keyring and on the keys it holds, and the unpossessedsearch ``keyctl invalidate`` uses to reach a key by serial, are denieddiff --git a/Documentation/threat-model.rst b/Documentation/threat-model.rstindex 9113b99d..adbc136f 100644--- a/Documentation/threat-model.rst+++ b/Documentation/threat-model.rst@@ -123,10 +123,13 @@ T2 Detaching or replacing enforcement``lsm/bpf`` denies ``LINK_DETACH``, ``LINK_UPDATE``,``PROG_ATTACH``, ``PROG_DETACH`` and ``BTF_LOAD`` from userspace.The links are kept alive by pins in a private bpffs;- ``inode_unlink`` on that filesystem allows only shirudo's own- executable, and ``sb_umount``, ``move_mount``, ``sb_mount`` and- ``sb_remount`` deny everyone, so neither ``rm`` nor ``umount`` nor- mounting over the directory removes a pin. Infrastructure maps are+ ``inode_unlink`` and ``inode_rename`` on it allow only shirudo's own+ executable (the VFS unlinks the victim of a rename without+ ``inode_unlink``, so a rename over a link's pin would otherwise+ 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 arefrozen at creation, so even with the LSM gone userspace could notrewrite the identity map. ``lsm/bpf_map`` denies writable map fdsto every process except shirudo; map updates and freezes have thediff --git a/bpf/bpf2.c b/bpf/bpf2.cindex e9a240a1..949daf49 100644--- a/bpf/bpf2.c+++ b/bpf/bpf2.c@@ -34,16 +34,22 @@* process except shirudo. Read-only map access* remains available; this also blocks writable* mmap of arena maps.- * lsm/inode_unlink -- denies VFS unlink on shirudo's private+ * lsm/inode_unlink,+ * lsm/inode_rename -- deny VFS unlink and rename on shirudo's private* bpffs superblock unless the caller is shirudo* itself (its exe_file inode matches the* identity recorded at init). Permanently* protects the LSM-link and upatch pins from an- * attacker's `rm` (seal excludes the bpffs- * mount, so bpf2 is the only protector here),- * while letting `shirudo upatch unload` remove- * a pin to detach a program. seal applies the- * same exe-inode check. The task_prctl hook below+ * attacker's `rm` and `mv` (seal excludes the+ * bpffs mount, so bpf2 is the only protector+ * here), while letting `shirudo upatch unload`+ * remove a pin to detach a program. Rename is+ * guarded because the VFS asks only the rename+ * hook of it and the victim of a rename is+ * unlinked without inode_unlink, so renaming any+ * pin over a link's pin would drop that link's+ * only reference. seal applies the same+ * exe-inode check. The task_prctl hook below* prevents non-Shirudo callers from changing* mm->exe_file through PR_SET_MM.* lsm/key_permission -- guards the trust anchor in the kernel's@@ -313,8 +319,9 @@ int BPF_PROG(shirudo_lsm_task_prctl, int option, unsigned long arg2,* Verdict for an operation targeting an inode in superblock target_dev:* allow (0) if it is not shirudo's bpffs, or if the calling task's* exe_file matches the identity recorded at init; deny (-EPERM)- * otherwise. The single guard shared by inode_unlink, inode_setxattr- * and inode_removexattr -- "only shirudo may mutate its own bpffs".+ * otherwise. The single guard shared by inode_unlink, inode_rename,+ * inode_setxattr and inode_removexattr -- "only shirudo may mutate its+ * own bpffs".*/static __always_inline int shirudo_bpffs_guard(__u32 target_dev){@@ -390,6 +397,21 @@ int BPF_PROG(shirudo_lsm_inode_unlink, struct inode *dir,return shirudo_bpffs_guard(BPF_CORE_READ(dir, i_sb, s_dev));}+/*+ * Rename is the other way a pin can go: the VFS asks only this hook of+ * it, and simple_rename() unlinks the victim without inode_unlink, so+ * renaming any pin over a link's pin would drop that link's only+ * reference. Both directories share the superblock, a rename never+ * crossing one, so the source directory names it for both ends.+ */+SEC("lsm/inode_rename")+int BPF_PROG(shirudo_lsm_inode_rename, struct inode *old_dir,+ struct dentry *old_dentry, struct inode *new_dir,+ struct dentry *new_dentry)+{+ return shirudo_bpffs_guard(BPF_CORE_READ(old_dir, i_sb, s_dev));+}+/** Only shirudo may set xattrs on its own bpffs, so the* security.bpf.shirudo.payload value recorded on each pin directorydiff --git a/builtin-init.c b/builtin-init.cindex fc41b152..bd5972de 100644--- a/builtin-init.c+++ b/builtin-init.c@@ -787,10 +787,10 @@ static int init_lsm(bool attach_filesystem_seal)struct bpf_link *mount_links[ARRAY_SIZE(mount_hooks)] = {};const struct shirudo_embedded_bpf *blob = shirudo_bpf2_blob();struct bpf_program *p_bpf, *p_bpf_map, *p_unlink, *p_setx, *p_rmx;- struct bpf_program *p_pload;+ struct bpf_program *p_pload, *p_rename;struct bpf_object *obj = NULL, *seal_obj = NULL;struct bpf_link *link_bpf = NULL, *link_bpf_map = NULL;- struct bpf_link *link_unlink = NULL;+ struct bpf_link *link_unlink = NULL, *link_rename = NULL;struct bpf_link *link_setx = NULL, *link_rmx = NULL, *link_pload = NULL;struct bpf_link *link_keyperm = NULL;struct bpf_link *seal_links[ARRAY_SIZE(seal_hooks)] = {};@@ -941,6 +941,8 @@ static int init_lsm(bool attach_filesystem_seal)p_unlink = bpf_object__find_program_by_name(obj,"shirudo_lsm_inode_unlink");+ p_rename = bpf_object__find_program_by_name(obj,+ "shirudo_lsm_inode_rename");p_setx = bpf_object__find_program_by_name(obj,"shirudo_lsm_inode_setxattr");p_rmx = bpf_object__find_program_by_name(obj,@@ -952,9 +954,8 @@ static int init_lsm(bool attach_filesystem_seal)p_keyperm = bpf_object__find_program_by_name(obj, "shirudo_lsm_key_permission");p_bpf = bpf_object__find_program_by_name(obj, "shirudo_lsm_bpf");- if (!p_unlink || !p_setx || !p_rmx || !p_pload || !p_bpf_map ||- !p_bpf ||- !p_keyperm) {+ if (!p_unlink || !p_rename || !p_setx || !p_rmx || !p_pload ||+ !p_bpf_map || !p_bpf || !p_keyperm) {pr_err("expected programs missing in bpf2 object");err = -ENOENT;goto out;@@ -973,6 +974,11 @@ static int init_lsm(bool attach_filesystem_seal)pr_err("attach lsm/inode_unlink: %s", strerror(-err));goto out;}+ err = attach_pin(p_rename, SHIRUDO_INODE_RENAME_LINK_PIN, &link_rename);+ if (err) {+ pr_err("attach lsm/inode_rename: %s", strerror(-err));+ goto out;+ }err = attach_pin(p_setx, SHIRUDO_INODE_SETXATTR_LINK_PIN, &link_setx);if (err) {pr_err("attach lsm/inode_setxattr: %s", strerror(-err));@@ -1062,6 +1068,7 @@ out:bpf_link__destroy(link_keyperm);bpf_link__destroy(link_rmx);bpf_link__destroy(link_setx);+ bpf_link__destroy(link_rename);bpf_link__destroy(link_unlink);bpf_object__close(obj);return err;diff --git a/testing/run-target-tests.sh b/testing/run-target-tests.shindex c51fe17a..ee9a0831 100755--- a/testing/run-target-tests.sh+++ b/testing/run-target-tests.sh@@ -41,6 +41,7 @@ for t in \tamperproof/t1110-lsm-deny.sh \tamperproof/t1112-prog-load-keyring-gate.sh \tamperproof/t1113-mount-lockdown.sh \+ tamperproof/t1135-pin-rename.sh \tamperproof/t1130-map-lockdown.sh \tamperproof/t1131-pr-set-mm-identity.sh \tamperproof/t1125-key-permission.sh \diff --git a/testing/tamperproof/t1135-pin-rename.sh b/testing/tamperproof/t1135-pin-rename.shnew file mode 100755index 00000000..b15b7737--- /dev/null+++ b/testing/tamperproof/t1135-pin-rename.sh@@ -0,0 +1,60 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# t1135 -- rename inside the private bpffs. The VFS asks only the rename+# hook of a rename and unlinks the victim without inode_unlink, so+# before bpf2's lsm/inode_rename a root `mv` of any pin over a link's+# pin dropped that link's only reference and detached the hook without+# a guard firing. Both the plain rename of a pin and the rename over+# the lsm/bpf link's pin must now be refused with EPERM, the pins must+# still be there and enforcement must survive. Non-destructive when the+# guard works; the trap re-inits if a regression unpinned the link.++cd "$(dirname "$0")/.."+. ./integration-lib.sh+target_guard+command -v python3 >/dev/null 2>&1 || test_skip_all "no python3"++H=helper/test-bpf-load+[ -x "$H" ] || test_skip_all "test-bpf-load helper not built"+B=/run/shirudo/bpffs+[ -e "$B/identity" ] || test_skip_all "no identity pin at $B/identity"++restore() {+ [ -e "$B/lsm_link" ] || "$SHIRUDO_BIN" init >/dev/null 2>&1 || true+}+trap restore EXIT++# rename(2) of $1 to $2: prints the errno, 0 for success.+RENAME='+import errno, os, sys+try:+ os.rename(sys.argv[1], sys.argv[2])+ print(0)+except OSError as e:+ print(e.errno)+'++test_expect_success 'baseline: unsigned userspace load is denied' '+ [ "$(./$H)" = "errno=13" ]+'++test_expect_success 'renaming a pin to another name is refused' '+ [ "$(python3 -c "$RENAME" "$B/identity" "$B/t1135-moved")" = 1 ] &&+ [ -e "$B/identity" ] && [ ! -e "$B/t1135-moved" ]+'++test_expect_success 'renaming a pin over the lsm/bpf link pin is refused' '+ [ "$(python3 -c "$RENAME" "$B/identity" "$B/lsm_link")" = 1 ] &&+ [ -e "$B/identity" ] && [ -e "$B/lsm_link" ] &&+ [ "$(./$H)" = "errno=13" ]+'++test_expect_success 'renaming the lsm/bpf link pin itself is refused' '+ [ "$(python3 -c "$RENAME" "$B/lsm_link" "$B/t1135-link")" = 1 ] &&+ [ -e "$B/lsm_link" ] && [ ! -e "$B/t1135-link" ] &&+ [ "$(./$H)" = "errno=13" ]+'++test_donediff --git a/testing/tamperproof/test.plan.md b/testing/tamperproof/test.plan.mdindex 0d4a620c..35fd1b98 100644--- a/testing/tamperproof/test.plan.md+++ b/testing/tamperproof/test.plan.md@@ -47,6 +47,7 @@ checks that Shirudo can still export the pre-existing Python exec record.| TP-25 | Root creates a file, directory, symlink or O_TMPFILE under a labelled directory | Covered | `t1133-seal-init-xattr.sh` | The inode is born labelled and sealed from that instant; a child of a new directory needs no reopen; the run directory is labelled; a file outside the tree is not. || TP-26 | Root attaches a debugger to a running shirudo task with `PTRACE_ATTACH` | Covered | `t1134-seal-task.sh` | The seal refuses with `EACCES`, Yama's `EPERM` would not do; the task is unharmed and finishes. || TP-27 | Root binds a perf event to a running shirudo task | Covered | `t1134-seal-task.sh` | `perf_event_open` on the task is refused with `EACCES`; the same event on the caller's own task is the positive control. |+| TP-28 | Root renames a pin in the private bpffs, or renames one over the lsm/bpf link's pin | Covered | `t1135-pin-rename.sh` | Both are refused with `EPERM`; the pins stay and enforcement survives. Before `lsm/inode_rename` the rename over a pin dropped the link with no guard firing. |## Review notesdiff --git a/util/paths.h b/util/paths.hindex 8c28783f..b146771c 100644--- a/util/paths.h+++ b/util/paths.h@@ -62,6 +62,7 @@#define SHIRUDO_SB_MOUNT_LINK_PIN SHIRUDO_BPFFS_DIR "/bpf2_sb_mount_link"#define SHIRUDO_SB_REMOUNT_LINK_PIN SHIRUDO_BPFFS_DIR "/bpf2_sb_remount_link"#define SHIRUDO_INODE_UNLINK_LINK_PIN SHIRUDO_BPFFS_DIR "/bpf2_inode_unlink_link"+#define SHIRUDO_INODE_RENAME_LINK_PIN SHIRUDO_BPFFS_DIR "/bpf2_inode_rename_link"#define SHIRUDO_INODE_SETXATTR_LINK_PIN SHIRUDO_BPFFS_DIR "/bpf2_inode_setxattr_link"#define SHIRUDO_INODE_RMXATTR_LINK_PIN SHIRUDO_BPFFS_DIR "/bpf2_inode_removexattr_link"#define SHIRUDO_KEY_PERMISSION_LINK_PIN SHIRUDO_BPFFS_DIR "/bpf2_key_permission_link"