eBPF-based security platform
bpf: identify shirudo in the seal by its executable, not by a label
Puranjay Mohanf4ec4da91b5d
diff --git a/Documentation/architecture.rst b/Documentation/architecture.rstindex 3523c0d0..ec47414e 100644--- a/Documentation/architecture.rst+++ b/Documentation/architecture.rst@@ -285,18 +285,21 @@ otherwise detach everything.Filesystem seal. ``bpf/seal.c`` relies on every inode under``/var/lib/shirudo/`` and the installed binary carrying the xattr``security.bpf.shirudo`` with one fixed 64-byte value, the same for-every build, so that a newer shirudo keeps its access to the state an-older one labelled. Its ``file_open`` (for write), ``inode_unlink``,+every build. The label says what is protected, not who may touch it:+its value is public, and a binary that carries it proves nothing, since+root can set it on any file it creates and any file system it mounts can+present it. The ``file_open`` (for write), ``inode_unlink``,``inode_rename``, ``inode_setxattr`` and ``inode_removexattr`` hooks-read the label of the target inode and the label of the caller's-``exe_file`` and allow only when both exist and match, compared in-constant time. No ``bpf_d_path()``, no path strings, nothing a bind-mount or a mount namespace can confuse. ``bprm_check_security`` and-``bprm_committed_creds`` mark tasks that exec the labelled binary in+read the label of the target inode and, when there is one, allow only+the task that runs the executable ``init`` recorded in the frozen+identity map, the same ``(dev, ino)`` test the bpffs guards use. No+``bpf_d_path()``, no path strings, nothing a bind mount or a mount+namespace can confuse. ``bprm_check_security`` and+``bprm_committed_creds`` mark tasks that exec a labelled binary intask-local storage, ``task_alloc`` inherits the mark, and``ptrace_access_check`` and ``perf_event_alloc`` deny against markedtasks, so a root debugger or profiler cannot attach to a running-shirudo.+shirudo; a forged label can only mark its forger.Process hardening. ``util/harden.c`` runs at startup on the target:``PR_SET_DUMPABLE`` off, ``PR_SET_NO_NEW_PRIVS``, ``PR_SET_PTRACER`` todiff --git a/Documentation/threat-model.rst b/Documentation/threat-model.rstindex 67d23c80..41d22bcb 100644--- a/Documentation/threat-model.rst+++ b/Documentation/threat-model.rst@@ -144,8 +144,11 @@ T2 Detaching or replacing enforcementT3 Tampering with shirudo's files and processEvery inode under ``/var/lib/shirudo`` carries the``security.bpf.shirudo`` label; the seal program allows write open,- unlink, rename and xattr changes only to a process whose executable- carries the same label, compared by xattr value and never by path.+ unlink, rename and xattr changes only to the process that runs the+ executable ``init`` recorded, by device and inode and never by path.+ The label marks what is protected and is public; it is not what+ identifies the caller, as a binary that carries it, set by root or+ presented by a file system it mounts, would then be shirudo.Marked shirudo tasks cannot be ptraced or perf-sampled, and theprocess drops dumpability, forbids new privileges and lockssensitive buffers. The bpf keyring is sealed withdiff --git a/bpf/bpf2.c b/bpf/bpf2.cindex 542c5d3a..e9a240a1 100644--- a/bpf/bpf2.c+++ b/bpf/bpf2.c@@ -42,9 +42,8 @@* 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. The exe-inode check- * is the interim of the xattr-based identity- * seal will use. The task_prctl hook below+ * a pin to detach a program. 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'sdiff --git a/bpf/seal.c b/bpf/seal.cindex c0170d11..286166e1 100644--- a/bpf/seal.c+++ b/bpf/seal.c@@ -2,35 +2,38 @@/** Copyright (c) 2026 Authors of shirudo*- * Filesystem-sealing LSM: xattr-based identity, never paths.+ * Filesystem-sealing LSM: xattr-based labels, never paths.*- * Every file/directory under /var/lib/shirudo/ (except the private- * bpffs mount, which bpf/bpf2.c guards) is labelled with the inode- * xattr security.bpf.shirudo whose value is one fixed 64-character- * string; the shirudo binary carries the same label. The- * filesystem hooks read the target's label and, if present, require the- * calling task's executable to carry an equal label -- so only shirudo- * may write, unlink, rename, or relabel its own on-disk state. The exec- * hooks cache whether the successfully executed file carries a Shirudo- * label in task storage for later access-control decisions. An- * unlabelled filesystem target is not ours and is allowed through- * untouched. No bpf_d_path(), no path strings: the label rides the- * inode, immune to bind-mount / namespace / TOCTOU games. Reads are not- * gated; only write-mode opens and the mutating inode ops are.+ * Every file/directory under /var/lib/shirudo/ (except the private bpffs+ * mount, which bpf/bpf2.c guards) is labelled with the inode xattr+ * security.bpf.shirudo whose value is one fixed 64-character string; the+ * shirudo binary carries the same label. The filesystem hooks read the+ * target's label and, if present, require the calling task to run the+ * executable shirudo init recorded in the identity map (the test bpf2+ * applies to the bpffs) -- so only shirudo may write, unlink, rename, or+ * relabel its own on-disk state. The label only marks a target: its+ * value is public, so a binary carrying it proves nothing about the+ * caller. The exec hooks cache whether the successfully executed file+ * carries a Shirudo label in task storage for later access-control+ * decisions. An unlabelled filesystem target is not ours and is allowed+ * through untouched. No bpf_d_path(), no path strings: the label rides+ * the inode, immune to bind-mount / namespace / TOCTOU games. Reads are+ * not gated; only write-mode opens and the mutating inode ops are.** The filesystem and exec xattr hooks are sleepable (lsm.s/) because* reading an xattr can sleep. The xattr-reading kfuncs write into a- * bpf_dynptr over map memory- * (the verifier rejects stack), so the two scratch buffers live in- * per-task storage: a task is in only one of these syscalls at a time,- * and its storage survives CPU migration across the sleeping read -- so- * no cross-invocation races, unlike a shared global buffer.+ * bpf_dynptr over map memory (the verifier rejects stack), so the+ * scratch buffer lives in per-task storage: a task is in only one of+ * these syscalls at a time, and its storage survives CPU migration+ * across the sleeping read -- so no cross-invocation races, unlike a+ * shared global buffer.*/#include "vmlinux.h"#include <bpf/bpf_helpers.h>#include <bpf/bpf_tracing.h>#include <bpf/bpf_core_read.h>+#include "shirudo_identity.h"#ifndef EACCES#define EACCES 13@@ -47,7 +50,6 @@ char _license[] SEC("license") = "Dual BSD/GPL";struct seal_scratch {char target[LABEL_LEN];- char exe[LABEL_LEN];__u8 exec_candidate_valid;__u8 exec_candidate_labelled;__u8 exe_is_shirudo;@@ -60,41 +62,29 @@ struct {__type(value, struct seal_scratch);} seal_scratch_map SEC(".maps");-extern struct file *bpf_get_task_exe_file(struct task_struct *task) __ksym;-extern void bpf_put_file(struct file *file) __ksym;extern int bpf_get_dentry_xattr(struct dentry *dentry, const char *name__str,struct bpf_dynptr *value_p) __ksym;extern int bpf_get_file_xattr(struct file *file, const char *name__str,struct bpf_dynptr *value_p) __ksym;/*- * With the target's label already read into s->target (tlen bytes),- * decide the verdict. tlen != LABEL_LEN means the target is not one of- * shirudo's labelled inodes -- allow. Otherwise the calling task's- * exe_file must carry an equal label. 0 = allow, -EACCES = deny.+ * With the length of the target's label already read (tlen), decide the+ * verdict. tlen != LABEL_LEN means the target is not one of shirudo's+ * labelled inodes -- allow. Otherwise the calling task must run the+ * executable recorded at init. 0 = allow, -EACCES = deny.*/-static __always_inline int seal_verdict(struct task_struct *task,- struct seal_scratch *s, int tlen)+static __always_inline int seal_verdict(int tlen){- struct bpf_dynptr edp;- struct file *exe;- int elen, i, diff = 0;+ struct shirudo_sb_id *id;+ __u32 zero = 0;if (tlen != LABEL_LEN)return 0; /* not labelled (absent / wrong size) */- exe = bpf_get_task_exe_file(task);- if (!exe)- return -EACCES; /* no exe (kernel thread): not shirudo */- bpf_dynptr_from_mem(s->exe, LABEL_LEN, 0, &edp);- elen = bpf_get_file_xattr(exe, SHIRUDO_XATTR, &edp);- bpf_put_file(exe);-- if (elen != LABEL_LEN)- return -EACCES; /* caller's exe is not shirudo */- for (i = 0; i < LABEL_LEN; i++)- diff |= s->target[i] ^ s->exe[i];- return diff ? -EACCES : 0;+ id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);+ if (!id)+ return -EACCES;+ return shirudo_task_is_self(id) ? 0 : -EACCES;}static __always_inline struct seal_scratch *seal_scratch_get(@@ -225,8 +215,7 @@ int BPF_PROG(shirudo_seal_file_open, struct file *file)if (!s)return 0; /* no scratch: fail open (see header) */bpf_dynptr_from_mem(s->target, LABEL_LEN, 0, &tdp);- return seal_verdict(task, s,- bpf_get_file_xattr(file, SHIRUDO_XATTR, &tdp));+ return seal_verdict(bpf_get_file_xattr(file, SHIRUDO_XATTR, &tdp));}static __always_inline int seal_dentry(struct dentry *dentry)@@ -238,8 +227,7 @@ static __always_inline int seal_dentry(struct dentry *dentry)if (!s)return 0;bpf_dynptr_from_mem(s->target, LABEL_LEN, 0, &tdp);- return seal_verdict(task, s,- bpf_get_dentry_xattr(dentry, SHIRUDO_XATTR, &tdp));+ return seal_verdict(bpf_get_dentry_xattr(dentry, SHIRUDO_XATTR, &tdp));}SEC("lsm.s/inode_unlink")diff --git a/builtin-init.c b/builtin-init.cindex 1f1756fb..75ee0404 100644--- a/builtin-init.c+++ b/builtin-init.c@@ -638,8 +638,11 @@ static int attach_pin(struct bpf_program *p, const char *pin,* down together). Must run BEFORE lsm/bpf is attached: seal is loaded* unsigned and is not prog_type SYSCALL, so once the bpf() gate is live* it would refuse this very load -- the same bootstrap window bpf2 uses.+ * identity_fd is bpf2's frozen identity map, which the seal reads to+ * recognise shirudo's executable instead of declaring its own copy.*/-static int attach_seal(struct bpf_object **obj_out, struct bpf_link **links)+static int attach_seal(struct bpf_object **obj_out, struct bpf_link **links,+ int identity_fd){static const struct {const char *prog;@@ -658,6 +661,7 @@ static int attach_seal(struct bpf_object **obj_out, struct bpf_link **links)};const struct shirudo_embedded_bpf *blob = shirudo_seal_blob();struct bpf_object *obj;+ struct bpf_map *m;int err, i;*obj_out = NULL;@@ -671,6 +675,18 @@ static int attach_seal(struct bpf_object **obj_out, struct bpf_link **links)pr_err("seal: bpf_object__open_mem: %d", err);return err;}+ m = bpf_object__find_map_by_name(obj, "shirudo_bpffs_sb");+ if (!m) {+ pr_err("seal: shirudo_bpffs_sb map not in embedded seal");+ bpf_object__close(obj);+ return -ENOENT;+ }+ err = bpf_map__reuse_fd(m, identity_fd);+ if (err) {+ pr_err("seal: share the identity map: %s", strerror(-err));+ bpf_object__close(obj);+ return err;+ }err = bpf_object__load(obj);if (err) {pr_err("seal: bpf_object__load: %d", err);@@ -981,7 +997,7 @@ static int init_lsm(bool attach_filesystem_seal)}if (attach_filesystem_seal) {- err = attach_seal(&seal_obj, seal_links);+ err = attach_seal(&seal_obj, seal_links, map_fd);if (err)goto out;}diff --git a/util/seal.c b/util/seal.cindex 9f89b24f..844d1a9b 100644--- a/util/seal.c+++ b/util/seal.c@@ -57,7 +57,7 @@ int shirudo_seal_label_tree(void)struct stat st;ssize_t n;- /* The binary's own inode carries the label too (exe-file identity). */+ /* Label the binary too: the seal protects it and marks its tasks. */n = readlink("/proc/self/exe", self, sizeof(self) - 1);if (n > 0) {self[n] = '\0';diff --git a/util/seal.h b/util/seal.hindex c0694cac..15c72c36 100644--- a/util/seal.h+++ b/util/seal.h@@ -10,11 +10,8 @@/** The label value: SHA-256 of the xattr name, 64 hex characters, the- * same for every build. bpf/seal.c only compares the caller's and the- * target's labels for equality, so a value that changed per release- * would lock a newer binary out of the state an older one labelled- * until something relabelled it while the seal is not attached; from- * the initrd, with the root file system read-only, nothing can.+ * same for every build. bpf/seal.c takes any 64-byte value as marking a+ * protected inode; the identity map says who may touch it.*/#define SHIRUDO_SEAL_LABEL \"94442b45cfcdc59aabc2a91364ed2ae129418e05a5c1985231b9c032e673f469"