Skip to content

eBPF-based security platform

bpf: load separate objects for ordinary and image modes

KP Singhcf9b1510f84f
diff --git a/Documentation/architecture.rst b/Documentation/architecture.rst
index e067231e..fa775e33 100644
--- a/Documentation/architecture.rst
+++ b/Documentation/architecture.rst
@@ -213,14 +213,20 @@ below. The opt-in dm-verity mode uses an authenticated executable image
and committed task authority instead; its differences follow at the end of
this section and in `config-images <config-images.rst>`__.
-Two BPF objects are embedded in the binary: ``bpf/bpf2.c`` guards the
-``bpf()`` syscall and the private bpffs; ``bpf/seal.c`` guards shirudo's
-files and processes. ``shirudo init`` opens these embedded objects and loads
-them with ``bpf_object__load()`` before attaching the BPF admission gates.
-They are bootstrap objects, not signed upatch-loader artifacts. In the
+The binary embeds a pair of BPF objects for each mode. Ordinary mode uses
+``bpf/bpf2.c`` for BPF admission and bpffs protection, and ``bpf/seal.c`` for
+file labels and process protection. Image mode uses ``bpf/bpf2_verity.c``
+and ``bpf/seal_verity.c`` instead. Its objects add verified-device tracking,
+DM ioctl restrictions, runtime exec authority and executable-mapping checks.
+``shirudo init`` opens and loads only the selected pair. The common admission
+and seal programs live in ``bpf/admission.h`` and ``bpf/seal.h``; each mode
+supplies its own caller check through its policy header. Writable state keeps
+the same xattr protection in both modes.
+
+These are bootstrap objects, not signed upatch-loader artifacts. In the
production boot flow their provenance comes from the authenticated initrd.
The map-access, ``bpf_prog_load`` and ``bpf`` gates attach last, after the
-bootstrap objects have loaded and their identity maps are frozen. Subsequent
+selected objects have loaded and their identity maps are frozen. Subsequent
upatch code must enter through the signature-gated loader path.
Both objects contain BPF LSM programs. Their verdicts compose with
SELinux, AppArmor, SMACK and Yama to the most restrictive result, and
diff --git a/Documentation/config-images.rst b/Documentation/config-images.rst
index 75c4922b..9ed6e68d 100644
--- a/Documentation/config-images.rst
+++ b/Documentation/config-images.rst
@@ -197,7 +197,9 @@ initrd first verifies the runtime and selected config to determine the
boot-enrolled signer set. Image boot requires
``dm_verity.keyring_unsealed=1``: the trusted initrd enrolls the config
verification certificate in the dedicated ``.dm-verity`` keyring and seals
-it before handing control to the host. Built-in kernel trust remains part
+it before handing control to the host. Enrollment and sealing address the
+kernel ring through ``KEY_SPEC_DM_VERITY_KEYRING``; there is no search of
+``/proc/keys`` or fallback to a user-created keyring. Built-in kernel trust remains part
of config-root verification. The initrd also enrolls and seals the BPF
keyring, attaches the guards, mounts
the runtime under guard protection and completes measured config activation.
@@ -354,6 +356,36 @@ config evidence. This remains subject to the direct-writer limitation.
6) Enforcement and tests
------------------------
+The binary embeds a separate BPF object pair for each mode. Initialization
+loads only the selected pair; ordinary installations do not load image
+metadata hooks, DM ioctl restrictions or executable-mapping restrictions.
+
+.. list-table:: BPF objects selected at initialization
+ :header-rows: 1
+ :widths: 20 35 45
+
+ * - Mode
+ - Objects
+ - Caller authority
+ * - Ordinary
+ - ``bpf/bpf2.c`` and ``bpf/seal.c``
+ - The executable device/inode recorded at init, or its initrd copy.
+ * - dm-verity
+ - ``bpf/bpf2_verity.c`` and ``bpf/seal_verity.c``
+ - A task mark committed after successful exec from the runtime image.
+
+``bpf/admission.h`` supplies the shared BPF command, signature, keyring and
+bpffs checks. ``bpf/seal.h`` supplies writable-state xattr checks, label
+inheritance, and ptrace/perf checks. The selected policy header determines
+caller authority. Both modes share the same checks without testing the mode
+inside each running BPF program.
+
+The seal label marks a protected target. It never grants the caller write
+access. Ordinary mode labels its executable and state; image mode uses
+dm-verity for executable/config files and labels writable state only.
+Ordinary-mode exec marks protect labelled processes from observation but do
+not authorize BPF operations or file writes.
+
Image mode marks tasks after successful exec from the approved runtime
device, inherits the mark on fork and re-evaluates it on every successful
exec. Exec outside the runtime clears authority. Failed exec preserves
@@ -391,7 +423,7 @@ unmount of a parent mount above the runtime and config images. Legacy ``mount(MS
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.
A changed pathname does not change device-bound executable authority.
-No kernel patches are included.
+No additional LSM hooks are required.
Process-memory protection remains necessary: dm-verity protects backing
files, not writable mappings, ptrace or process-memory writes. Trusted
@@ -410,8 +442,10 @@ and shared mappings. The `threat model <threat-model.rst>`__ describes these
current limits; the separate `directory-entry investigation
<https://github.com/shirudo-dev/shirudo/issues/13>`__ records the earlier baseline.
-The dm-verity implementation uses APIs present at bpf-next commit
-``0867b7bfa9de65674c6e599fa21b762b71011b14`` and adds no kernel patches.
+The enforcement hooks are present at bpf-next commit
+``0867b7bfa9de65674c6e599fa21b762b71011b14``. Direct access to the kernel
+``.dm-verity`` keyring additionally requires upstream commit
+``1041fca2ee8a`` (``keys: add KEY_SPEC_DM_VERITY_KEYRING``).
Shirudo also requires the signed-BPF kernel interfaces listed in
`requirements <requirements.rst>`__.
diff --git a/Makefile.shirudo b/Makefile.shirudo
index e0411046..a4e66e6b 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -254,7 +254,9 @@ BPF_LIB_HEADERS := bpf/lib/shirudo_misc.h \
# .incbin into util/loader.o. bpf/lib/ is include-only.
BPF_SRCS := \
bpf/bpf2.c \
+ bpf/bpf2_verity.c \
bpf/seal.c \
+ bpf/seal_verity.c \
bpf/unload.c
# shirudo-build is operator-only and links util/sign.c (CMS signer)
@@ -356,7 +358,8 @@ build/target/%.o: %.c Makefile.shirudo $(CONFIG_MK)
# BPF objects pull vmlinux.h from vendor/linux, <bpf/...> helper headers
# and UAPI kernel headers from vendor/libbpf, and shirudo's own BPF
# library headers from bpf/lib.
-bpf/%.bpf.o: bpf/%.c $(VMLINUX_H) $(BPF_LIBBPF_HEADERS)
+bpf/%.bpf.o: bpf/%.c $(VMLINUX_H) $(BPF_LIBBPF_HEADERS) \
+ $(wildcard bpf/*.h bpf/lib/*.h)
@printf ' CLANG $@\n'
$(Q)$(CLANG_BPF) -Ibpf/lib -I$(dir $(VMLINUX_H)) -I$(LIBBPF_VENDOR) \
-I$(BPF_UAPI_INCLUDE) \
diff --git a/bpf/admission.h b/bpf/admission.h
new file mode 100644
index 00000000..6d169687
--- /dev/null
+++ b/bpf/admission.h
@@ -0,0 +1,446 @@
+/* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
+/* Copyright (c) 2026 Authors of shirudo */
+
+#ifndef SHIRUDO_BPF_ADMISSION_H
+#define SHIRUDO_BPF_ADMISSION_H
+
+/*
+ * Both modes use the same BPF command allowlist. The selected policy authorizes
+ * the frozen executable in ordinary mode, or a committed task in image mode.
+ */
+static __always_inline int shirudo_self_or_deny(enum shirudo_tamper_point point)
+{
+ struct shirudo_sb_id *id;
+ __u32 zero = 0;
+
+ id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
+ if (!id || !shirudo_task_is_self(id))
+ return shirudo_tamper_deny(point, -EACCES);
+ return 0;
+}
+
+SEC("lsm/bpf")
+int BPF_PROG(shirudo_lsm_bpf, int cmd, union bpf_attr *attr,
+ unsigned int size, bool kernel, int ret)
+{
+ enum shirudo_tamper_point point;
+
+ if (ret)
+ return ret;
+ if (kernel)
+ return 0;
+
+ switch (cmd) {
+ case BPF_PROG_LOAD:
+ /*
+ * Signed-loader entry. Require SYSCALL prog_type AND a
+ * non-zero signature; the kernel verifies the CMS blob
+ * against attr->keyring_id immediately after this hook.
+ */
+ if (BPF_CORE_READ(attr, prog_type) != BPF_PROG_TYPE_SYSCALL)
+ point = SHIRUDO_TAMPER_LOAD_NOT_LOADER;
+ else if (BPF_CORE_READ(attr, signature_size) == 0)
+ point = SHIRUDO_TAMPER_LOAD_UNSIGNED;
+ else
+ return 0;
+ return shirudo_tamper_deny(point, -EACCES);
+
+ /*
+ * Vetted mutating cmds the signed-loader data-map + attach/pin
+ * pattern needs from userspace, each left to a caller authorized by
+ * the selected policy. Signed-loader mutations run in kernel context.
+ * A prog_fd is only obtainable via the signature-gated
+ * BPF_PROG_LOAD above or GET_FD_BY_ID of an already-vetted prog.
+ * gen_loader emits no link-create and bpf_sys_bpf() cannot
+ * BPF_OBJ_PIN, so shirudo's one-shot CLI must create + pin the
+ * link itself, and nobody else has a reason to: a vetted kprobe
+ * program re-attached by root at a site of its choosing would run
+ * signed code where its signer never meant it to, and a pin of
+ * root's into the private bpffs is an entry shirudo did not make.
+ */
+ case BPF_MAP_CREATE:
+ return shirudo_self_or_deny(SHIRUDO_TAMPER_MAP_CREATE);
+
+ case BPF_MAP_UPDATE_ELEM:
+ case BPF_MAP_FREEZE:
+ return shirudo_self_or_deny(SHIRUDO_TAMPER_MAP_WRITE);
+
+ case BPF_PROG_TEST_RUN:
+ return shirudo_self_or_deny(SHIRUDO_TAMPER_PROG_RUN);
+
+ case BPF_LINK_CREATE:
+ case BPF_RAW_TRACEPOINT_OPEN:
+ return shirudo_self_or_deny(SHIRUDO_TAMPER_ATTACH);
+
+ case BPF_OBJ_PIN:
+ return shirudo_self_or_deny(SHIRUDO_TAMPER_OBJ_PIN);
+
+ /*
+ * Read-only / observability cmds, so `bpftool prog show`,
+ * `shirudo status`, and `shirudo upatch get/map` keep working.
+ * BPF_OBJ_UNPIN does not exist as a bpf() cmd; VFS unlink removes
+ * pins and shirudo's lsm/inode_unlink hook blocks `rm` on the
+ * private bpffs.
+ */
+ case BPF_MAP_LOOKUP_ELEM:
+ case BPF_MAP_GET_NEXT_KEY:
+ case BPF_MAP_LOOKUP_BATCH:
+ case BPF_OBJ_GET:
+ case BPF_OBJ_GET_INFO_BY_FD:
+ case BPF_PROG_QUERY:
+ case BPF_PROG_GET_NEXT_ID:
+ case BPF_MAP_GET_NEXT_ID:
+ case BPF_BTF_GET_NEXT_ID:
+ case BPF_LINK_GET_NEXT_ID:
+ case BPF_PROG_GET_FD_BY_ID:
+ case BPF_MAP_GET_FD_BY_ID:
+ case BPF_BTF_GET_FD_BY_ID:
+ case BPF_LINK_GET_FD_BY_ID:
+ case BPF_TASK_FD_QUERY:
+ return 0;
+
+ /*
+ * Default-DENY, fail closed. This covers the tear-down / replace /
+ * corruption cmds an attacker would use to detach or swap out
+ * shirudo's own LSM/upatch links or drain map state
+ * (BPF_LINK_DETACH, BPF_LINK_UPDATE, BPF_PROG_ATTACH/DETACH,
+ * BPF_BTF_LOAD, BPF_MAP_DELETE_ELEM and the *_BATCH /
+ * LOOKUP_AND_DELETE variants) and -- critically -- every cmd
+ * shirudo has not explicitly vetted above, including ones a newer
+ * kernel may add (BPF_TOKEN_CREATE, BPF_PROG_BIND_MAP,
+ * BPF_ITER_CREATE, BPF_PROG_ASSOC_STRUCT_OPS, ...). A future
+ * mutating cmd therefore cannot silently bypass policy: it stays
+ * denied until explicitly classified. -EACCES matches the LSM
+ * denial errno used throughout (threat-model.rst T1/T2).
+ */
+ default:
+ return shirudo_tamper_deny(SHIRUDO_TAMPER_BPF_CMD, -EACCES);
+ }
+}
+
+/*
+ * Writable map fds let userspace alter array contents and writable
+ * mmap lets it alter arena pages without another bpf() command. Keep
+ * both paths exclusive to shirudo while leaving read-only map access
+ * available to observers.
+ */
+SEC("lsm/bpf_map")
+int BPF_PROG(shirudo_lsm_bpf_map, struct bpf_map *map, fmode_t fmode,
+ int ret)
+{
+ struct shirudo_sb_id *id = shirudo_policy();
+
+ if (ret)
+ return ret;
+ if (fmode & FMODE_WRITE)
+ return shirudo_self_or_deny(SHIRUDO_TAMPER_MAP_FD);
+ if (!shirudo_private_map(map))
+ return 0;
+ if (!id || !shirudo_task_is_self(id))
+ return -EACCES;
+ return 0;
+}
+
+/*
+ * Admission gate. Fires from a single call site inside the verifier
+ * (bpf_check), after the kernel has verified any PKCS#7 signature and
+ * recorded the verdict on prog->aux->sig -- so the fields read here are
+ * final. A userspace load is admitted only if the kernel verified it
+ * against exactly shirudo's own sealed keyring; every other program is
+ * denied, fail-closed.
+ *
+ * kernel == true means the load originates from inside an already-vetted
+ * signed loader running in kernel context; those are always allowed
+ * (the loader itself passed this gate when it was loaded from userspace).
+ */
+SEC("lsm/bpf_prog_load")
+int BPF_PROG(shirudo_lsm_bpf_prog_load, struct bpf_prog *prog,
+ union bpf_attr *attr, struct bpf_token *token,
+ bool kernel, int ret)
+{
+ struct shirudo_sb_id *id;
+ __u32 zero = 0, verdict, ktype;
+ __s32 serial;
+
+ if (ret)
+ return ret;
+ if (kernel)
+ return 0;
+
+ /*
+ * Fail closed if the identity record is missing or carries no
+ * keyring serial: without the trust anchor there is no way to tell
+ * a shirudo-signed load from an attacker's, so admit nothing.
+ */
+ id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
+ if (!id || id->keyring_serial == 0)
+ return -EACCES;
+
+ verdict = BPF_CORE_READ(prog, aux, sig.verdict);
+ ktype = BPF_CORE_READ(prog, aux, sig.keyring_type);
+ serial = BPF_CORE_READ(prog, aux, sig.keyring_serial);
+
+ /*
+ * Three ways to fail, each counted apart: no verified signature
+ * on the program, a signature verified against a keyring of the
+ * wrong kind, or against a keyring of the right kind that is not
+ * the one init froze.
+ */
+ if (verdict != BPF_SIG_VERIFIED)
+ return shirudo_tamper_deny(SHIRUDO_TAMPER_LOAD_UNVERIFIED,
+ -EACCES);
+ if (ktype != id->keyring_type)
+ return shirudo_tamper_deny(SHIRUDO_TAMPER_LOAD_KEYRING_KIND,
+ -EACCES);
+ /*
+ * The bpf keyring is the kernel's own singleton, resolved by the
+ * kernel and recorded without a serial (0) because it is held
+ * without a reference, so the kind alone names it. A staged user
+ * keyring is named by the serial init froze, which a same-named
+ * substitute cannot match.
+ */
+ if (ktype == BPF_SIG_KEYRING_BPF) {
+ if (serial && serial != id->keyring_serial)
+ return shirudo_tamper_deny(SHIRUDO_TAMPER_LOAD_KEYRING,
+ -EACCES);
+ } else if (serial != id->keyring_serial) {
+ return shirudo_tamper_deny(SHIRUDO_TAMPER_LOAD_KEYRING,
+ -EACCES);
+ }
+
+ return 0;
+}
+
+SEC("lsm/task_prctl")
+int BPF_PROG(shirudo_lsm_task_prctl, int option, unsigned long arg2,
+ unsigned long arg3, unsigned long arg4, unsigned long arg5,
+ int ret)
+{
+ struct shirudo_sb_id *id;
+ __u32 zero = 0;
+
+ (void)arg3;
+ (void)arg4;
+ (void)arg5;
+ if (ret && ret != -ENOSYS)
+ return ret;
+ if (option == SHIRUDO_PR_GET_MODE) {
+ id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
+ if (!id)
+ return -EACCES;
+ return -SHIRUDO_MODE_ERROR;
+ }
+ if (option != PR_SET_MM ||
+ (arg2 != PR_SET_MM_EXE_FILE && arg2 != PR_SET_MM_MAP))
+ return ret ? ret : -ENOSYS;
+ id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
+ if (!id)
+ return -EACCES;
+ if (!shirudo_task_is_self(id))
+ return shirudo_tamper_deny(SHIRUDO_TAMPER_EXE_IDENTITY, -EPERM);
+ return -ENOSYS;
+}
+
+/*
+ * Verdict for an operation targeting an inode in superblock target_dev:
+ * Ordinary mode protects the bpffs; image mode also protects the /run
+ * filesystem holding its mountpoints. Only an authorized caller may mutate
+ * these inodes. Count bpffs refusals at `point`. Unrelated filesystems pass.
+ */
+SEC("lsm/bprm_check_security")
+int BPF_PROG(shirudo_lsm_bprm_check_security, struct linux_binprm *bprm,
+ int ret)
+{
+ struct shirudo_sb_id *id;
+ __u32 zero = 0;
+
+ if (ret)
+ return ret;
+ id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
+ if (!id || !shirudo_file_is_self(id, bprm->file))
+ return 0;
+ if (BPF_CORE_READ(bprm, unsafe) & LSM_UNSAFE_PTRACE)
+ return shirudo_tamper_deny(SHIRUDO_TAMPER_EXEC_TRACED, -EPERM);
+ bpf_bprm_opts_set(bprm, BPF_F_BPRM_SECUREEXEC);
+ return 0;
+}
+
+static __always_inline int shirudo_bpffs_guard(__u32 target_dev,
+ enum shirudo_tamper_point point)
+{
+ struct shirudo_sb_id *id;
+ __u32 zero = 0;
+
+ id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
+ if (!id || id->sb_dev == 0)
+ return 0;
+ if (!shirudo_protected_filesystem(id, target_dev))
+ return 0;
+ if (shirudo_task_is_self(id))
+ return 0;
+ if (target_dev == id->sb_dev && point != SHIRUDO_TAMPER_MAX)
+ return shirudo_tamper_deny(point, -EPERM);
+ return -EPERM;
+}
+
+/*
+ * The trust anchor in the kernel's keyrings: the keyring init froze
+ * and the keys it holds. Root owns the bpf keyring with write and
+ * setattr permission, so without this hook a single keyctl could
+ * unlink, clear, revoke or invalidate the certificates every load is
+ * verified against. Deny those needs to every task but shirudo. A
+ * search is denied only through an unpossessed reference, which is
+ * how keyctl reaches a key by serial to invalidate it; the kernel
+ * verifies a signature by searching the keyring it possesses, and
+ * that stays open to every loader.
+ */
+SEC("lsm/key_permission")
+int BPF_PROG(shirudo_lsm_key_permission, key_ref_t key_ref,
+ const struct cred *cred, enum key_need_perm need, int ret)
+{
+ struct shirudo_sb_id *id;
+ __u32 zero = 0, i;
+ struct key *key;
+ bool possessed;
+ __s32 serial;
+
+ if (ret)
+ return ret;
+ switch (need) {
+ case KEY_NEED_WRITE:
+ case KEY_NEED_SEARCH:
+ case KEY_NEED_SETATTR:
+ case KEY_NEED_UNLINK:
+ break;
+ default:
+ return 0;
+ }
+ id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
+ if (!id || id->keyring_serial == 0)
+ return 0;
+ possessed = (unsigned long)key_ref & 1;
+ key = (struct key *)((unsigned long)key_ref & ~1UL);
+ serial = BPF_CORE_READ(key, serial);
+ if (serial != id->keyring_serial) {
+ for (i = 0; i < SHIRUDO_IDENTITY_KEYS_MAX; i++)
+ if (i < id->n_keys && id->key_serials[i] == serial)
+ break;
+ if (i == SHIRUDO_IDENTITY_KEYS_MAX)
+ return 0; /* not the anchor -- not ours */
+ }
+ if (need == KEY_NEED_SEARCH && possessed)
+ return 0;
+ return shirudo_task_is_self(id) ? 0 :
+ shirudo_tamper_deny(SHIRUDO_TAMPER_KEYRING, -EACCES);
+}
+
+/*
+ * Inside shirudo's bpffs: only an authorized caller may unlink
+ * (the `upatch unload` path). Every other caller -- including root
+ * `rm` -- is denied, so the LSM link cannot be detached by removing
+ * its pin.
+ */
+SEC("lsm/inode_unlink")
+int BPF_PROG(shirudo_lsm_inode_unlink, struct inode *dir,
+ struct dentry *dentry, int ret)
+{
+ if (ret)
+ return ret;
+ return shirudo_bpffs_guard(BPF_CORE_READ(dir, i_sb, s_dev),
+ SHIRUDO_TAMPER_PIN_UNLINK);
+}
+
+SEC("lsm/inode_rmdir")
+int BPF_PROG(shirudo_lsm_inode_rmdir, struct inode *dir,
+ struct dentry *dentry, int ret)
+{
+ if (ret)
+ return ret;
+ return shirudo_bpffs_guard(BPF_CORE_READ(dir, i_sb, s_dev),
+ SHIRUDO_TAMPER_MAX);
+}
+
+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, int ret)
+{
+ if (ret)
+ return ret;
+ ret = shirudo_bpffs_guard(BPF_CORE_READ(old_dir, i_sb, s_dev),
+ SHIRUDO_TAMPER_PIN_RENAME);
+ return ret ? ret :
+ shirudo_bpffs_guard(BPF_CORE_READ(new_dir, i_sb, s_dev),
+ SHIRUDO_TAMPER_PIN_RENAME);
+}
+
+/*
+ * Only shirudo may set xattrs on its own bpffs, so the
+ * security.bpf.shirudo.payload value recorded on each pin directory
+ * cannot be forged by a root attacker. The unload check trusts this value.
+ */
+SEC("lsm/inode_setxattr")
+int BPF_PROG(shirudo_lsm_inode_setxattr, struct mnt_idmap *idmap,
+ struct dentry *dentry, const char *name, const void *value,
+ __u64 size, int flags, int ret)
+{
+ if (ret)
+ return ret;
+ return shirudo_bpffs_guard(BPF_CORE_READ(dentry, d_inode, i_sb, s_dev),
+ SHIRUDO_TAMPER_PIN_XATTR);
+}
+
+/* Symmetric to setxattr: deny removing a pin's xattrs from non-shirudo. */
+SEC("lsm/inode_removexattr")
+int BPF_PROG(shirudo_lsm_inode_removexattr, struct mnt_idmap *idmap,
+ struct dentry *dentry, const char *name, int ret)
+{
+ if (ret)
+ return ret;
+ return shirudo_bpffs_guard(BPF_CORE_READ(dentry, d_inode, i_sb, s_dev),
+ SHIRUDO_TAMPER_PIN_XATTR);
+}
+
+/*
+ * Mount-lifecycle lockdown of shirudo's private bpffs superblock.
+ *
+ * The pinned LSM links live only in this bpffs, and shirudo is agentless
+ * -- once `shirudo init` exits no process holds an fd to them, so the
+ * pins are their only reference. Tearing the superblock down therefore
+ * detaches every hook at once: a plain `umount /run/shirudo/bpffs`
+ * drops the pinned inodes, releases the links, and silently disables all
+ * enforcement (no signature, keyring, or TPM involved). The inode guards
+ * above stop `rm` of a pin but a mount is not an inode op, so it slips
+ * past them. Gate the whole mount lifecycle of this one superblock:
+ * unmount, relocate (move_mount), mounting over the pin tree (sb_mount),
+ * and remount. shirudo never performs any of these on its own bpffs
+ * after init, so the deny is unconditional -- the mount is immutable for
+ * the boot's lifetime, for every caller including shirudo itself. The
+ * 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).
+ */
+static __always_inline __u32 shirudo_bpffs_dev(void)
+{
+ struct shirudo_sb_id *id;
+ __u32 zero = 0;
+
+ id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
+ return id ? id->sb_dev : 0;
+}
+
+/* -EPERM, counted at `point`, when sb_dev is the bpffs; else 0. */
+static __always_inline int
+shirudo_deny_bpffs_mount(__u32 sb_dev, enum shirudo_tamper_point point)
+{
+ __u32 bpffs = shirudo_bpffs_dev();
+
+ if (!bpffs || sb_dev != bpffs)
+ return 0;
+ return shirudo_tamper_deny(point, -EPERM);
+}
+
+#endif /* SHIRUDO_BPF_ADMISSION_H */
diff --git a/bpf/bpf2.c b/bpf/bpf2.c
index 529c5969..4bc39b39 100644
--- a/bpf/bpf2.c
+++ b/bpf/bpf2.c
@@ -1,475 +1,42 @@
// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
-/*
- * Copyright (c) 2026 Authors of shirudo
- *
- * The central "BPF on BPF" LSM enforcement program. Every refusal is counted
- * at its point in the tamper map, which `shirudo status` reads back.
- */
-
-#include "vmlinux.h"
-#include <bpf/bpf_helpers.h>
-#include <bpf/bpf_tracing.h>
-#include <bpf/bpf_core_read.h>
-
-#ifndef EPERM
-#define EPERM 1
-#endif
-#ifndef EACCES
-#define EACCES 13
-#endif
-#ifndef ENOMEM
-#define ENOMEM 12
-#endif
-#ifndef ENOSYS
-#define ENOSYS 38
-#endif
-#ifndef FMODE_WRITE
-#define FMODE_WRITE 0x2
-#endif
-#define LSM_UNSAFE_PTRACE 2
-#define PR_SET_MM 35
-#define PR_SET_MM_EXE_FILE 13
-#define PR_SET_MM_MAP 14
-
-char _license[] SEC("license") = "Dual BSD/GPL";
-
-#include "policy.h"
-#include "../include/shirudo_mode.h"
-#include "shirudo_tamper.h"
-
-/* The mode selects executable identity or committed task authority. */
-static __always_inline int shirudo_self_or_deny(enum shirudo_tamper_point point)
-{
- struct shirudo_sb_id *id;
- __u32 zero = 0;
-
- id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
- if (!id || !shirudo_task_is_self(id))
- return shirudo_tamper_deny(point, -EACCES);
- return 0;
-}
-
-SEC("lsm/bpf")
-int BPF_PROG(shirudo_lsm_bpf, int cmd, union bpf_attr *attr,
- unsigned int size, bool kernel, int ret)
-{
- enum shirudo_tamper_point point;
-
- if (ret)
- return ret;
- if (kernel)
- return 0;
-
- switch (cmd) {
- case BPF_PROG_LOAD:
- if (BPF_CORE_READ(attr, prog_type) != BPF_PROG_TYPE_SYSCALL)
- point = SHIRUDO_TAMPER_LOAD_NOT_LOADER;
- else if (BPF_CORE_READ(attr, signature_size) == 0)
- point = SHIRUDO_TAMPER_LOAD_UNSIGNED;
- else
- return 0;
- return shirudo_tamper_deny(point, -EACCES);
- case BPF_MAP_CREATE:
- return shirudo_self_or_deny(SHIRUDO_TAMPER_MAP_CREATE);
- case BPF_MAP_UPDATE_ELEM:
- case BPF_MAP_FREEZE:
- return shirudo_self_or_deny(SHIRUDO_TAMPER_MAP_WRITE);
- case BPF_PROG_TEST_RUN:
- return shirudo_self_or_deny(SHIRUDO_TAMPER_PROG_RUN);
- case BPF_LINK_CREATE:
- case BPF_RAW_TRACEPOINT_OPEN:
- return shirudo_self_or_deny(SHIRUDO_TAMPER_ATTACH);
- case BPF_OBJ_PIN:
- return shirudo_self_or_deny(SHIRUDO_TAMPER_OBJ_PIN);
- /*
- * Read-only/observability cmds, so `bpftool prog show`, `shirudo
- * status`, and `shirudo upatch get/map` keep working.
- */
- case BPF_MAP_LOOKUP_ELEM:
- case BPF_MAP_GET_NEXT_KEY:
- case BPF_MAP_LOOKUP_BATCH:
- case BPF_OBJ_GET:
- case BPF_OBJ_GET_INFO_BY_FD:
- case BPF_PROG_QUERY:
- case BPF_PROG_GET_NEXT_ID:
- case BPF_MAP_GET_NEXT_ID:
- case BPF_BTF_GET_NEXT_ID:
- case BPF_LINK_GET_NEXT_ID:
- case BPF_PROG_GET_FD_BY_ID:
- case BPF_MAP_GET_FD_BY_ID:
- case BPF_BTF_GET_FD_BY_ID:
- case BPF_LINK_GET_FD_BY_ID:
- case BPF_TASK_FD_QUERY:
- return 0;
- default:
- return shirudo_tamper_deny(SHIRUDO_TAMPER_BPF_CMD, -EACCES);
- }
-}
-
-SEC("lsm/bpf_map")
-int BPF_PROG(shirudo_lsm_bpf_map, struct bpf_map *map, fmode_t fmode,
- int ret)
-{
- struct shirudo_sb_id *id = shirudo_policy();
- char name[16] = {};
- bool private;
-
- if (ret)
- return ret;
- bpf_probe_read_kernel_str(name, sizeof(name), map->name);
- private = !__builtin_memcmp(name, "shirudo_devices", 16);
- if (fmode & FMODE_WRITE)
- return shirudo_self_or_deny(SHIRUDO_TAMPER_MAP_FD);
- if (!private)
- return 0;
- if (!id || !shirudo_task_is_self(id))
- return -EACCES;
- return 0;
-}
-
-SEC("lsm/bpf_prog_load")
-int BPF_PROG(shirudo_lsm_bpf_prog_load, struct bpf_prog *prog,
- union bpf_attr *attr, struct bpf_token *token,
- bool kernel, int ret)
-{
- struct shirudo_sb_id *id;
- __u32 zero = 0, verdict, ktype;
- __s32 serial;
-
- /*
- * kernel == true means the load originates from inside an already-
- * vetted signed loader running in kernel context; those are always
- * allowed (the loader itself passed this gate when it was loaded
- * from userspace).
- */
- if (ret)
- return ret;
- if (kernel)
- return 0;
-
- id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
- if (!id || id->keyring_serial == 0)
- return -EACCES;
-
- verdict = BPF_CORE_READ(prog, aux, sig.verdict);
- ktype = BPF_CORE_READ(prog, aux, sig.keyring_type);
- serial = BPF_CORE_READ(prog, aux, sig.keyring_serial);
-
- if (verdict != BPF_SIG_VERIFIED)
- return shirudo_tamper_deny(SHIRUDO_TAMPER_LOAD_UNVERIFIED,
- -EACCES);
- if (ktype != id->keyring_type)
- return shirudo_tamper_deny(SHIRUDO_TAMPER_LOAD_KEYRING_KIND,
- -EACCES);
- if (ktype == BPF_SIG_KEYRING_BPF) {
- if (serial && serial != id->keyring_serial)
- return shirudo_tamper_deny(SHIRUDO_TAMPER_LOAD_KEYRING,
- -EACCES);
- } else if (serial != id->keyring_serial) {
- return shirudo_tamper_deny(SHIRUDO_TAMPER_LOAD_KEYRING,
- -EACCES);
- }
- return 0;
-}
-
-SEC("lsm/task_prctl")
-int BPF_PROG(shirudo_lsm_task_prctl, int option, unsigned long arg2,
- unsigned long arg3, unsigned long arg4, unsigned long arg5,
- int ret)
-{
- struct shirudo_sb_id *id;
- __u32 zero = 0;
-
- if (ret && ret != -ENOSYS)
- return ret;
- if (option == SHIRUDO_PR_GET_MODE) {
- id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
- if (!id)
- return -EACCES;
- return id->runtime_mode ? -SHIRUDO_PR_MODE_IMAGE_ERR :
- -SHIRUDO_PR_MODE_LEGACY_ERR;
- }
- if (option != PR_SET_MM ||
- (arg2 != PR_SET_MM_EXE_FILE && arg2 != PR_SET_MM_MAP))
- return ret ? ret : -ENOSYS;
- id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
- if (!id)
- return -EACCES;
- if (!shirudo_task_is_self(id))
- return shirudo_tamper_deny(SHIRUDO_TAMPER_EXE_IDENTITY, -EPERM);
- return -ENOSYS;
-}
-
-SEC("lsm/bprm_check_security")
-int BPF_PROG(shirudo_lsm_bprm_check_security, struct linux_binprm *bprm,
- int ret)
-{
- struct shirudo_sb_id *id;
- __u32 zero = 0;
-
- if (ret)
- return ret;
- id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
- if (!id || !shirudo_file_is_self(id, bprm->file))
- return 0;
- if (BPF_CORE_READ(bprm, unsafe) & LSM_UNSAFE_PTRACE)
- return shirudo_tamper_deny(SHIRUDO_TAMPER_EXEC_TRACED, -EPERM);
- bpf_bprm_opts_set(bprm, BPF_F_BPRM_SECUREEXEC);
- return 0;
-}
-
-static __always_inline int shirudo_bpffs_guard(__u32 target_dev,
- enum shirudo_tamper_point point)
-{
- struct shirudo_sb_id *id;
- __u32 zero = 0;
-
- id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
- if (!id || id->sb_dev == 0)
- return 0;
- if (target_dev != id->sb_dev &&
- (!id->runtime_mode || !id->run_dev || target_dev != id->run_dev))
- return 0;
- if (shirudo_task_is_self(id))
- return 0;
- if (target_dev == id->sb_dev && point != SHIRUDO_TAMPER_MAX)
- return shirudo_tamper_deny(point, -EPERM);
- return -EPERM;
-}
+/* Copyright (c) 2026 Authors of shirudo */
/*
- * The trust anchor in the kernel's keyrings: the keyring init froze
- * and the keys it holds. Root owns the bpf keyring with write and
- * setattr permission, so without this hook a single keyctl could
- * unlink, clear, revoke or invalidate the certificates every load is
- * verified against. Deny those needs to every task but shirudo. A
- * search is denied only through an unpossessed reference, which is
- * how keyctl reaches a key by serial to invalidate it; the kernel
- * verifies a signature by searching the keyring it possesses, and
- * that stays open to every loader.
+ * Ordinary mode records executable device/inode identities at init. The common
+ * admission checks also protect the bpffs and its payload xattrs. File labels
+ * and task observation checks live in the matching seal object.
*/
-SEC("lsm/key_permission")
-int BPF_PROG(shirudo_lsm_key_permission, key_ref_t key_ref,
- const struct cred *cred, enum key_need_perm need, int ret)
-{
- struct shirudo_sb_id *id;
- __u32 zero = 0, i;
- struct key *key;
- bool possessed;
- __s32 serial;
-
- if (ret)
- return ret;
- switch (need) {
- case KEY_NEED_WRITE:
- case KEY_NEED_SEARCH:
- case KEY_NEED_SETATTR:
- case KEY_NEED_UNLINK:
- break;
- default:
- return 0;
- }
- id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
- if (!id || id->keyring_serial == 0)
- return 0;
- possessed = (unsigned long)key_ref & 1;
- key = (struct key *)((unsigned long)key_ref & ~1UL);
- serial = BPF_CORE_READ(key, serial);
- if (serial != id->keyring_serial) {
- for (i = 0; i < SHIRUDO_IDENTITY_KEYS_MAX; i++) {
- if (i < id->n_keys && id->key_serials[i] == serial)
- break;
- }
- if (i == SHIRUDO_IDENTITY_KEYS_MAX)
- return 0; /* not the anchor -- not ours */
- }
- if (need == KEY_NEED_SEARCH && possessed)
- return 0;
- return shirudo_task_is_self(id) ? 0 :
- shirudo_tamper_deny(SHIRUDO_TAMPER_KEYRING, -EACCES);
-}
-
-/*
- * Inside shirudo's bpffs: only shirudo's own executable may unlink
- * (the `upatch unload` path). Every other caller -- including root
- * `rm` -- is denied, so the LSM link cannot be detached by removing
- * its pin.
- */
-SEC("lsm/inode_unlink")
-int BPF_PROG(shirudo_lsm_inode_unlink, struct inode *dir,
- struct dentry *dentry, int ret)
-{
- if (ret)
- return ret;
- return shirudo_bpffs_guard(BPF_CORE_READ(dir, i_sb, s_dev),
- SHIRUDO_TAMPER_PIN_UNLINK);
-}
-
-SEC("lsm/inode_rmdir")
-int BPF_PROG(shirudo_lsm_inode_rmdir, struct inode *dir,
- struct dentry *dentry, int ret)
-{
- if (ret)
- return ret;
- return shirudo_bpffs_guard(BPF_CORE_READ(dir, i_sb, s_dev),
- SHIRUDO_TAMPER_MAX);
-}
-
-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, int ret)
-{
- if (ret)
- return ret;
- ret = shirudo_bpffs_guard(BPF_CORE_READ(old_dir, i_sb, s_dev),
- SHIRUDO_TAMPER_PIN_RENAME);
- return ret ? ret :
- shirudo_bpffs_guard(BPF_CORE_READ(new_dir, i_sb, s_dev),
- SHIRUDO_TAMPER_PIN_RENAME);
-}
-
-/*
- * Only shirudo may set xattrs on its own bpffs, so the
- * security.bpf.shirudo.payload value recorded on each pin directory
- * cannot be forged by a root attacker. See the file header.
- */
-SEC("lsm/inode_setxattr")
-int BPF_PROG(shirudo_lsm_inode_setxattr, struct mnt_idmap *idmap,
- struct dentry *dentry, const char *name, const void *value,
- __u64 size, int flags, int ret)
-{
- if (ret)
- return ret;
- return shirudo_bpffs_guard(BPF_CORE_READ(dentry, d_inode, i_sb, s_dev),
- SHIRUDO_TAMPER_PIN_XATTR);
-}
-
-/* Symmetric to setxattr: deny removing a pin's xattrs from non-shirudo. */
-SEC("lsm/inode_removexattr")
-int BPF_PROG(shirudo_lsm_inode_removexattr, struct mnt_idmap *idmap,
- struct dentry *dentry, const char *name, int ret)
-{
- if (ret)
- return ret;
- return shirudo_bpffs_guard(BPF_CORE_READ(dentry, d_inode, i_sb, s_dev),
- SHIRUDO_TAMPER_PIN_XATTR);
-}
-
-/*
- * Mount-lifecycle lockdown of shirudo's private bpffs superblock.
- *
- * The pinned LSM links live only in this bpffs, and shirudo is agentless
- * -- once `shirudo init` exits no process holds an fd to them, so the
- * pins are their only reference. Tearing the superblock down therefore
- * detaches every hook at once: a plain `umount /run/shirudo/bpffs`
- * drops the pinned inodes, releases the links, and silently disables all
- * enforcement (no signature, keyring, or TPM involved). The inode guards
- * above stop `rm` of a pin but a mount is not an inode op, so it slips
- * past them. Gate the whole mount lifecycle of this one superblock:
- * unmount, relocate (move_mount), mounting over the pin tree (sb_mount),
- * and remount. shirudo never performs any of these on its own bpffs
- * after init, so the deny is unconditional -- the mount is immutable for
- * the boot's lifetime, for every caller including shirudo itself. The
- * hooks are self-protecting: their own pins live in the bpffs they keep
- * mounted.
- *
- * A lazy unmount (MNT_DETACH) of a mount above the bpffs, /run or the
- * root, takes the subtree down with it and fires sb_umount for the
- * ancestor alone. The original mount must stay; namespace copies may
- * go when none of the removed mounts can propagate the unmount out.
- * switch-root moves /run and retains the original mount's unique ID.
- */
-static __always_inline __u32 shirudo_bpffs_dev(void)
-{
- struct shirudo_sb_id *id;
- __u32 zero = 0;
-
- id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
- return id ? id->sb_dev : 0;
-}
-
-/* -EPERM, counted at `point`, when sb_dev is the bpffs; else 0. */
-static __always_inline int
-shirudo_deny_bpffs_mount(__u32 sb_dev, enum shirudo_tamper_point point)
-{
- __u32 bpffs = shirudo_bpffs_dev();
-
- if (!bpffs || sb_dev != bpffs)
- return 0;
- return shirudo_tamper_deny(point, -EPERM);
-}
-
-static __always_inline int shirudo_parent_mount(struct super_block *sb,
- bool trusted_target)
-{
- struct shirudo_sb_id *id = shirudo_policy();
-
- if (!sb || !id || !id->runtime_mode || !id->run_dev ||
- BPF_CORE_READ(sb, s_dev) != id->run_dev)
- return 0;
- return trusted_target && shirudo_task_is_self(id) ? 0 : -EPERM;
-}
-
-static __always_inline int shirudo_image_mount(struct super_block *sb,
- bool replace)
-{
- struct shirudo_sb_id *id = shirudo_policy();
- struct shirudo_device *device;
- struct block_device *bdev;
+#include "policy.h"
+#include "admission.h"
- if (!sb || !id || !id->runtime_mode)
- return 0;
- bdev = BPF_CORE_READ(sb, s_bdev);
- if (!bdev)
- return 0;
- device = shirudo_bdev_device(bdev);
- if (!device)
- return 0;
- if (replace && device->kind == SHIRUDO_DEVICE_CONFIG &&
- shirudo_task_is_self(id))
- return 0;
- return -EPERM;
-}
+char _license[] SEC("license") = "Dual BSD/GPL";
SEC("lsm/sb_umount")
int BPF_PROG(shirudo_lsm_sb_umount, struct vfsmount *mnt, int flags,
int ret)
{
- struct super_block *sb = BPF_CORE_READ(mnt, mnt_sb);
__u32 dev;
if (ret)
return ret;
- ret = shirudo_parent_mount(sb, false);
- if (ret)
- return ret;
- if (BPF_CORE_READ_INTO(&dev, sb, s_dev))
+ if (BPF_CORE_READ_INTO(&dev, mnt, mnt_sb, s_dev))
return shirudo_tamper_deny(SHIRUDO_TAMPER_BPFFS_UMOUNT,
-EPERM);
- ret = shirudo_deny_bpffs_mount(dev, SHIRUDO_TAMPER_BPFFS_UMOUNT);
- return ret ? ret : shirudo_image_mount(sb, true);
+ return shirudo_deny_bpffs_mount(dev, SHIRUDO_TAMPER_BPFFS_UMOUNT);
}
SEC("lsm/move_mount")
int BPF_PROG(shirudo_lsm_move_mount, const struct path *from_path,
const struct path *to_path, int ret)
{
- struct super_block *from = BPF_CORE_READ(from_path, mnt, mnt_sb);
- struct super_block *to = BPF_CORE_READ(to_path, mnt, mnt_sb);
+ __u32 from_dev = BPF_CORE_READ(from_path, mnt, mnt_sb, s_dev);
+ __u32 to_dev = BPF_CORE_READ(to_path, mnt, mnt_sb, s_dev);
if (ret)
return ret;
- if (shirudo_parent_mount(from, false) ||
- shirudo_parent_mount(to, true))
- return -EPERM;
- if (shirudo_deny_bpffs_mount(BPF_CORE_READ(from, s_dev),
- SHIRUDO_TAMPER_BPFFS_MOUNT) ||
- shirudo_deny_bpffs_mount(BPF_CORE_READ(to, s_dev),
- SHIRUDO_TAMPER_BPFFS_MOUNT))
+ if (shirudo_deny_bpffs_mount(from_dev, SHIRUDO_TAMPER_BPFFS_MOUNT))
return -EPERM;
- ret = shirudo_image_mount(from, true);
- return ret ? ret : shirudo_image_mount(to, true);
+ return shirudo_deny_bpffs_mount(to_dev, SHIRUDO_TAMPER_BPFFS_MOUNT);
}
SEC("lsm/sb_mount")
@@ -477,19 +44,10 @@ int BPF_PROG(shirudo_lsm_sb_mount, const char *dev_name,
const struct path *path, const char *type,
unsigned long flags, void *data, int ret)
{
- struct super_block *sb = BPF_CORE_READ(path, mnt, mnt_sb);
-
- if (ret)
- return ret;
- if ((flags & ((1UL << 17) | (1UL << 18) | (1UL << 19) |
- (1UL << 20))) && shirudo_parent_mount(sb, false))
- return -EPERM;
- ret = shirudo_parent_mount(sb, true);
if (ret)
return ret;
- ret = shirudo_deny_bpffs_mount(BPF_CORE_READ(sb, s_dev),
- SHIRUDO_TAMPER_BPFFS_MOUNT);
- return ret ? ret : shirudo_image_mount(sb, true);
+ return shirudo_deny_bpffs_mount(BPF_CORE_READ(path, mnt, mnt_sb, s_dev),
+ SHIRUDO_TAMPER_BPFFS_MOUNT);
}
SEC("lsm/sb_remount")
@@ -498,118 +56,6 @@ int BPF_PROG(shirudo_lsm_sb_remount, struct super_block *sb,
{
if (ret)
return ret;
- ret = shirudo_parent_mount(sb, false);
- if (ret)
- return ret;
- ret = shirudo_deny_bpffs_mount(BPF_CORE_READ(sb, s_dev),
- SHIRUDO_TAMPER_BPFFS_MOUNT);
- return ret ? ret : shirudo_image_mount(sb, false);
-}
-
-struct dm_verity_digest___shirudo {
- const char *alg;
- const __u8 *digest;
- size_t digest_len;
-} __attribute__((preserve_access_index));
-
-SEC("lsm.s/bdev_setintegrity")
-int BPF_PROG(shirudo_lsm_bdev_setintegrity, struct block_device *bdev,
- enum lsm_integrity_type type, const void *value,
- size_t size, int ret)
-{
- const struct dm_verity_digest___shirudo *digest = value;
- struct shirudo_sb_id *id = shirudo_policy();
- struct shirudo_device device = {};
- __u64 cookie = (__u64)bdev;
- const unsigned char *root;
- const char *algorithm;
- unsigned int i;
- char name[8];
- int diff;
-
- if (ret)
- return ret;
- if (!id || !id->runtime_mode || type != LSM_INT_DMVERITY_ROOTHASH)
- return 0;
- if (!value) {
- bpf_map_delete_elem(&shirudo_devices, &cookie);
- return 0;
- }
- if (shirudo_bdev_device(bdev))
- return -EPERM;
- if (BPF_CORE_READ(digest, digest_len) != sizeof(device.root))
- return 0;
- algorithm = BPF_CORE_READ(digest, alg);
- if (bpf_probe_read_kernel_str(name, sizeof(name), algorithm) != 7 ||
- name[0] != 's' || name[1] != 'h' || name[2] != 'a' ||
- name[3] != '2' || name[4] != '5' || name[5] != '6')
- return 0;
- root = BPF_CORE_READ(digest, digest);
- if (bpf_probe_read_kernel(device.root, sizeof(device.root), root))
- return -EACCES;
- diff = 0;
- for (i = 0; i < sizeof(device.root); i++)
- diff |= device.root[i] ^ id->runtime_root[i];
- if (!diff)
- device.kind = SHIRUDO_DEVICE_RUNTIME;
- else if (shirudo_task_is_self(id))
- device.kind = SHIRUDO_DEVICE_CONFIG;
- else
- return 0;
- device.dev = BPF_CORE_READ(bdev, bd_dev);
- device.diskseq = BPF_CORE_READ(bdev, bd_disk, diskseq);
- if (!device.diskseq)
- return -EACCES;
- if (bpf_map_update_elem(&shirudo_devices, &cookie, &device,
- BPF_ANY))
- return -ENOMEM;
- return 0;
-}
-
-SEC("lsm/bdev_free_security")
-void BPF_PROG(shirudo_lsm_bdev_free_security, struct block_device *bdev)
-{
- __u64 cookie = (__u64)bdev;
-
- bpf_map_delete_elem(&shirudo_devices, &cookie);
-}
-
-#define SHIRUDO_DM_CONTROL_DEV ((10U << 20) | 236U)
-#define SHIRUDO_DM_IOCTL_TYPE 0xfdU
-
-/* Match the kernel's type/number dispatch. */
-static __always_inline int shirudo_dm_ioctl(struct file *file,
- unsigned int cmd, int ret)
-{
- struct shirudo_sb_id *id = shirudo_policy();
- struct inode *inode;
-
- if (ret || !id || !id->runtime_mode || shirudo_task_is_self(id))
- return ret;
- inode = BPF_CORE_READ(file, f_inode);
- if ((BPF_CORE_READ(inode, i_mode) & 0170000) != 0020000 ||
- BPF_CORE_READ(inode, i_rdev) != SHIRUDO_DM_CONTROL_DEV)
- return 0;
- if (((cmd >> 8) & 0xff) != SHIRUDO_DM_IOCTL_TYPE)
- return -EPERM;
- switch (cmd & 0xff) {
- case 0: /* DM_VERSION */
- case 2: /* DM_LIST_DEVICES */
- case 7: /* DM_DEV_STATUS */
- case 8: /* DM_DEV_WAIT */
- case 11: /* DM_TABLE_DEPS */
- case 12: /* DM_TABLE_STATUS */
- case 13: /* DM_LIST_VERSIONS */
- case 17: /* DM_GET_TARGET_VERSION */
- return 0;
- default:
- return -EPERM;
- }
-}
-
-SEC("lsm/file_ioctl")
-int BPF_PROG(shirudo_lsm_file_ioctl, struct file *file, unsigned int cmd,
- unsigned long arg, int ret)
-{
- return shirudo_dm_ioctl(file, cmd, ret);
+ return shirudo_deny_bpffs_mount(BPF_CORE_READ(sb, s_dev),
+ SHIRUDO_TAMPER_BPFFS_MOUNT);
}
diff --git a/bpf/bpf2_verity.c b/bpf/bpf2_verity.c
new file mode 100644
index 00000000..6f874130
--- /dev/null
+++ b/bpf/bpf2_verity.c
@@ -0,0 +1,226 @@
+// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+/*
+ * Image mode authorizes tasks marked after exec from the authenticated runtime.
+ * These extra hooks track verity devices, restrict DM controls, and protect the
+ * runtime/config mounts alongside the shared bpffs and BPF admission checks.
+ */
+#include "verity_policy.h"
+#include "admission.h"
+
+char _license[] SEC("license") = "Dual BSD/GPL";
+
+static __always_inline int shirudo_parent_mount(struct super_block *sb,
+ bool trusted_target)
+{
+ struct shirudo_sb_id *id = shirudo_policy();
+
+ if (!sb || !id || !id->run_dev ||
+ BPF_CORE_READ(sb, s_dev) != id->run_dev)
+ return 0;
+ return trusted_target && shirudo_task_is_self(id) ? 0 : -EPERM;
+}
+
+static __always_inline int shirudo_image_mount(struct super_block *sb,
+ bool replace)
+{
+ struct shirudo_sb_id *id = shirudo_policy();
+ struct shirudo_device *device;
+ struct block_device *bdev;
+
+ if (!sb || !id)
+ return 0;
+ bdev = BPF_CORE_READ(sb, s_bdev);
+ if (!bdev)
+ return 0;
+ device = shirudo_bdev_device(bdev);
+ if (!device)
+ return 0;
+ if (replace && device->kind == SHIRUDO_DEVICE_CONFIG &&
+ shirudo_task_is_self(id))
+ return 0;
+ return -EPERM;
+}
+
+SEC("lsm/sb_umount")
+int BPF_PROG(shirudo_lsm_sb_umount, struct vfsmount *mnt, int flags,
+ int ret)
+{
+ struct super_block *sb = BPF_CORE_READ(mnt, mnt_sb);
+ __u32 dev;
+
+ if (ret)
+ return ret;
+ ret = shirudo_parent_mount(sb, false);
+ if (ret)
+ return ret;
+ if (BPF_CORE_READ_INTO(&dev, sb, s_dev))
+ return shirudo_tamper_deny(SHIRUDO_TAMPER_BPFFS_UMOUNT,
+ -EPERM);
+ ret = shirudo_deny_bpffs_mount(dev, SHIRUDO_TAMPER_BPFFS_UMOUNT);
+ return ret ? ret : shirudo_image_mount(sb, true);
+}
+
+SEC("lsm/move_mount")
+int BPF_PROG(shirudo_lsm_move_mount, const struct path *from_path,
+ const struct path *to_path, int ret)
+{
+ struct super_block *from = BPF_CORE_READ(from_path, mnt, mnt_sb);
+ struct super_block *to = BPF_CORE_READ(to_path, mnt, mnt_sb);
+
+ if (ret)
+ return ret;
+ if (shirudo_parent_mount(from, false) ||
+ shirudo_parent_mount(to, true))
+ return -EPERM;
+ if (shirudo_deny_bpffs_mount(BPF_CORE_READ(from, s_dev),
+ SHIRUDO_TAMPER_BPFFS_MOUNT) ||
+ shirudo_deny_bpffs_mount(BPF_CORE_READ(to, s_dev),
+ SHIRUDO_TAMPER_BPFFS_MOUNT))
+ return -EPERM;
+ ret = shirudo_image_mount(from, true);
+ return ret ? ret : shirudo_image_mount(to, true);
+}
+
+SEC("lsm/sb_mount")
+int BPF_PROG(shirudo_lsm_sb_mount, const char *dev_name,
+ const struct path *path, const char *type,
+ unsigned long flags, void *data, int ret)
+{
+ struct super_block *sb = BPF_CORE_READ(path, mnt, mnt_sb);
+
+ if (ret)
+ return ret;
+ if ((flags & ((1UL << 17) | (1UL << 18) | (1UL << 19) |
+ (1UL << 20))) && shirudo_parent_mount(sb, false))
+ return -EPERM;
+ ret = shirudo_parent_mount(sb, true);
+ if (ret)
+ return ret;
+ ret = shirudo_deny_bpffs_mount(BPF_CORE_READ(sb, s_dev),
+ SHIRUDO_TAMPER_BPFFS_MOUNT);
+ return ret ? ret : shirudo_image_mount(sb, true);
+}
+
+SEC("lsm/sb_remount")
+int BPF_PROG(shirudo_lsm_sb_remount, struct super_block *sb,
+ void *mnt_opts, int ret)
+{
+ if (ret)
+ return ret;
+ ret = shirudo_parent_mount(sb, false);
+ if (ret)
+ return ret;
+ ret = shirudo_deny_bpffs_mount(BPF_CORE_READ(sb, s_dev),
+ SHIRUDO_TAMPER_BPFFS_MOUNT);
+ return ret ? ret : shirudo_image_mount(sb, false);
+}
+
+struct dm_verity_digest___shirudo {
+ const char *alg;
+ const __u8 *digest;
+ size_t digest_len;
+} __attribute__((preserve_access_index));
+
+SEC("lsm.s/bdev_setintegrity")
+int BPF_PROG(shirudo_lsm_bdev_setintegrity, struct block_device *bdev,
+ enum lsm_integrity_type type, const void *value,
+ size_t size, int ret)
+{
+ const struct dm_verity_digest___shirudo *digest = value;
+ struct shirudo_sb_id *id = shirudo_policy();
+ struct shirudo_device device = {};
+ __u64 cookie = (__u64)bdev;
+ const unsigned char *root;
+ const char *algorithm;
+ unsigned int i;
+ char name[8];
+ int diff;
+
+ if (ret)
+ return ret;
+ if (!id || type != LSM_INT_DMVERITY_ROOTHASH)
+ return 0;
+ if (!value) {
+ bpf_map_delete_elem(&shirudo_devices, &cookie);
+ return 0;
+ }
+ if (shirudo_bdev_device(bdev))
+ return -EPERM;
+ if (BPF_CORE_READ(digest, digest_len) != sizeof(device.root))
+ return 0;
+ algorithm = BPF_CORE_READ(digest, alg);
+ if (bpf_probe_read_kernel_str(name, sizeof(name), algorithm) != 7 ||
+ name[0] != 's' || name[1] != 'h' || name[2] != 'a' ||
+ name[3] != '2' || name[4] != '5' || name[5] != '6')
+ return 0;
+ root = BPF_CORE_READ(digest, digest);
+ if (bpf_probe_read_kernel(device.root, sizeof(device.root), root))
+ return -EACCES;
+ diff = 0;
+ for (i = 0; i < sizeof(device.root); i++)
+ diff |= device.root[i] ^ id->runtime_root[i];
+ if (!diff)
+ device.kind = SHIRUDO_DEVICE_RUNTIME;
+ else if (shirudo_task_is_self(id))
+ device.kind = SHIRUDO_DEVICE_CONFIG;
+ else
+ return 0;
+ device.dev = BPF_CORE_READ(bdev, bd_dev);
+ device.diskseq = BPF_CORE_READ(bdev, bd_disk, diskseq);
+ if (!device.diskseq)
+ return -EACCES;
+ if (bpf_map_update_elem(&shirudo_devices, &cookie, &device,
+ BPF_ANY))
+ return -ENOMEM;
+ return 0;
+}
+
+SEC("lsm/bdev_free_security")
+void BPF_PROG(shirudo_lsm_bdev_free_security, struct block_device *bdev)
+{
+ __u64 cookie = (__u64)bdev;
+
+ bpf_map_delete_elem(&shirudo_devices, &cookie);
+}
+
+#define SHIRUDO_DM_CONTROL_DEV ((10U << 20) | 236U)
+#define SHIRUDO_DM_IOCTL_TYPE 0xfdU
+
+/* Match the kernel's type/number dispatch. */
+static __always_inline int shirudo_dm_ioctl(struct file *file,
+ unsigned int cmd, int ret)
+{
+ struct shirudo_sb_id *id = shirudo_policy();
+ struct inode *inode;
+
+ if (ret || !id || shirudo_task_is_self(id))
+ return ret;
+ inode = BPF_CORE_READ(file, f_inode);
+ if ((BPF_CORE_READ(inode, i_mode) & 0170000) != 0020000 ||
+ BPF_CORE_READ(inode, i_rdev) != SHIRUDO_DM_CONTROL_DEV)
+ return 0;
+ if (((cmd >> 8) & 0xff) != SHIRUDO_DM_IOCTL_TYPE)
+ return -EPERM;
+ switch (cmd & 0xff) {
+ case 0: /* DM_VERSION */
+ case 2: /* DM_LIST_DEVICES */
+ case 7: /* DM_DEV_STATUS */
+ case 8: /* DM_DEV_WAIT */
+ case 11: /* DM_TABLE_DEPS */
+ case 12: /* DM_TABLE_STATUS */
+ case 13: /* DM_LIST_VERSIONS */
+ case 17: /* DM_GET_TARGET_VERSION */
+ return 0;
+ default:
+ return -EPERM;
+ }
+}
+
+SEC("lsm/file_ioctl")
+int BPF_PROG(shirudo_lsm_file_ioctl, struct file *file, unsigned int cmd,
+ unsigned long arg, int ret)
+{
+ return shirudo_dm_ioctl(file, cmd, ret);
+}
diff --git a/bpf/lsm.h b/bpf/lsm.h
new file mode 100644
index 00000000..a6675b7c
--- /dev/null
+++ b/bpf/lsm.h
@@ -0,0 +1,69 @@
+/* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
+/* Copyright (c) 2026 Authors of shirudo */
+
+#ifndef SHIRUDO_LSM_H
+#define SHIRUDO_LSM_H
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include <bpf/bpf_core_read.h>
+
+#ifndef EPERM
+#define EPERM 1
+#endif
+#ifndef EACCES
+#define EACCES 13
+#endif
+#ifndef ENOMEM
+#define ENOMEM 12
+#endif
+#ifndef ENOSYS
+#define ENOSYS 38
+#endif
+#ifndef FMODE_WRITE
+#define FMODE_WRITE 0x2
+#endif
+#define LSM_UNSAFE_PTRACE 2
+#define PR_SET_MM 35
+#define PR_SET_MM_EXE_FILE 13
+#define PR_SET_MM_MAP 14
+
+#include "lib/shirudo_identity.h"
+#include "../include/shirudo_mode.h"
+#include "shirudo_tamper.h"
+
+#define SHIRUDO_TASK_TRUSTED 1
+#define SHIRUDO_TASK_BOOTSTRAP 2
+
+struct shirudo_task_authority {
+ __u8 trusted;
+ __u8 candidate_valid;
+ __u8 candidate_trusted;
+ __u8 pad;
+ __u32 pad2;
+};
+
+struct {
+ __uint(type, BPF_MAP_TYPE_TASK_STORAGE);
+ __uint(map_flags, BPF_F_NO_PREALLOC);
+ __type(key, int);
+ __type(value, struct shirudo_task_authority);
+} shirudo_tasks SEC(".maps");
+
+static __always_inline struct shirudo_sb_id *shirudo_policy(void)
+{
+ __u32 zero = 0;
+
+ return bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
+}
+
+static __always_inline bool shirudo_marked(struct task_struct *task)
+{
+ struct shirudo_task_authority *authority;
+
+ authority = bpf_task_storage_get(&shirudo_tasks, task, NULL, 0);
+ return authority && authority->trusted;
+}
+
+#endif /* SHIRUDO_LSM_H */
diff --git a/bpf/policy.h b/bpf/policy.h
index 56a5918b..36aa4295 100644
--- a/bpf/policy.h
+++ b/bpf/policy.h
@@ -4,118 +4,36 @@
#ifndef SHIRUDO_BPF_POLICY_H
#define SHIRUDO_BPF_POLICY_H
-#include "lib/shirudo_identity.h"
+#include "lsm.h"
-#define SHIRUDO_DEVICE_RUNTIME 1
-#define SHIRUDO_DEVICE_CONFIG 2
-
-#define SHIRUDO_TASK_TRUSTED 1
-#define SHIRUDO_TASK_BOOTSTRAP 2
-
-struct shirudo_task_authority {
- __u8 trusted;
- __u8 candidate_valid;
- __u8 candidate_trusted;
- __u8 pad;
- __u32 pad2;
-};
-
-struct shirudo_device {
- __u32 dev;
- __u32 kind;
- __u8 root[32];
- __u64 diskseq;
-};
-
-struct {
- __uint(type, BPF_MAP_TYPE_TASK_STORAGE);
- __uint(map_flags, BPF_F_NO_PREALLOC);
- __type(key, int);
- __type(value, struct shirudo_task_authority);
-} shirudo_tasks SEC(".maps");
-
-struct {
- __uint(type, BPF_MAP_TYPE_HASH);
- __uint(max_entries, 128);
- __type(key, __u64);
- __type(value, struct shirudo_device);
-} shirudo_devices SEC(".maps");
-
-struct inode *bpf_real_data_inode(struct file *file) __ksym;
-
-static __always_inline struct shirudo_sb_id *shirudo_policy(void)
-{
- __u32 zero = 0;
-
- return bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
-}
-
-static __always_inline bool shirudo_marked(struct task_struct *task)
-{
- struct shirudo_task_authority *authority;
-
- authority = bpf_task_storage_get(&shirudo_tasks, task, NULL, 0);
- return authority && authority->trusted;
-}
-
-static __always_inline bool shirudo_bootstrap_task(void)
-{
- struct task_struct *task = bpf_get_current_task_btf();
- struct shirudo_task_authority *authority;
-
- authority = bpf_task_storage_get(&shirudo_tasks, task, NULL, 0);
- return authority && authority->trusted == SHIRUDO_TASK_BOOTSTRAP;
-}
+#define SHIRUDO_MODE_ERROR SHIRUDO_PR_MODE_LEGACY_ERR
+#define SHIRUDO_TASK_STORAGE_REQUIRED false
+/* A label protects the target; the frozen executable authorizes the caller. */
static __always_inline bool shirudo_task_is_self(const struct shirudo_sb_id *id)
{
- if (id->runtime_mode)
- return shirudo_marked(bpf_get_current_task_btf());
return shirudo_task_matches_exe(id);
}
-/* Device pointers can be reused even when the free hook did not run. */
-static __always_inline struct shirudo_device *
-shirudo_bdev_device(struct block_device *bdev)
+static __always_inline bool
+shirudo_protected_filesystem(const struct shirudo_sb_id *id, __u32 dev)
{
- struct shirudo_device *device;
- __u64 cookie = (__u64)bdev;
-
- if (!bdev)
- return NULL;
- device = bpf_map_lookup_elem(&shirudo_devices, &cookie);
- if (!device || !device->diskseq ||
- device->dev != BPF_CORE_READ(bdev, bd_dev) ||
- device->diskseq != BPF_CORE_READ(bdev, bd_disk, diskseq))
- return NULL;
- return device;
+ return dev == id->sb_dev;
}
-static __always_inline struct shirudo_device *
-shirudo_inode_device(struct inode *inode)
+/* Ordinary mode has no device-authority map to hide from observers. */
+static __always_inline bool shirudo_private_map(struct bpf_map *map)
{
- struct block_device *bdev;
- struct super_block *sb;
-
- if (!inode)
- return NULL;
- sb = BPF_CORE_READ(inode, i_sb);
- if (!sb || !(BPF_CORE_READ(sb, s_flags) & 1))
- return NULL;
- bdev = BPF_CORE_READ(sb, s_bdev);
- if (!bdev)
- return NULL;
- return shirudo_bdev_device(bdev);
+ return false;
}
-static __always_inline bool shirudo_file_runtime(struct file *file)
+/* Replacing the binary for an upgrade does not authorize its replacement. */
+static __always_inline bool shirudo_replaceable_executable(struct inode *inode)
{
- struct shirudo_device *device;
+ struct shirudo_sb_id *id = shirudo_policy();
- if (!file)
- return false;
- device = shirudo_inode_device(BPF_CORE_READ(file, f_inode));
- return device && device->kind == SHIRUDO_DEVICE_RUNTIME;
+ return id && BPF_CORE_READ(inode, i_sb, s_dev) == id->exe_dev &&
+ BPF_CORE_READ(inode, i_ino) == id->exe_ino;
}
#endif /* SHIRUDO_BPF_POLICY_H */
diff --git a/bpf/seal.c b/bpf/seal.c
index 2b2333da..fc0f3bd3 100644
--- a/bpf/seal.c
+++ b/bpf/seal.c
@@ -1,491 +1,41 @@
// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
-/*
- * Copyright (c) 2026 Authors of shirudo
- *
- * Filesystem-sealing LSM: an inode label, never a path, marks what only
- * shirudo may modify.
- */
+/* Copyright (c) 2026 Authors of shirudo */
-#include "vmlinux.h"
-#include <bpf/bpf_helpers.h>
-#include <bpf/bpf_tracing.h>
-#include <bpf/bpf_core_read.h>
+/* Ordinary mode labels assets and recognizes the recorded executable. */
#include "policy.h"
-#include "shirudo_tamper.h"
-
-#ifndef ENOMEM
-#define ENOMEM 12
-#endif
-#ifndef EPERM
-#define EPERM 1
-#endif
-#ifndef EACCES
-#define EACCES 13
-#endif
-#ifndef ENODATA
-#define ENODATA 61
-#endif
-#ifndef EOPNOTSUPP
-#define EOPNOTSUPP 95
-#endif
-#ifndef FMODE_WRITE
-#define FMODE_WRITE 0x2
-#endif
-#ifndef S_IFMT
-#define S_IFMT 00170000
-#endif
-#ifndef S_IFDIR
-#define S_IFDIR 0040000
-#endif
+#include "seal.h"
char _license[] SEC("license") = "Dual BSD/GPL";
-#define SHIRUDO_XATTR "security.bpf.shirudo"
-/* SHA-256 hex, as util/seal.h writes it. */
-#define LABEL_LEN 64
-
-/*
- * The xattr kfuncs refuse a stack buffer, and a task is in one of these
- * hooks at a time.
- */
-struct seal_scratch {
- char target[LABEL_LEN];
-};
-
-struct {
- __uint(type, BPF_MAP_TYPE_TASK_STORAGE);
- __uint(map_flags, BPF_F_NO_PREALLOC);
- __type(key, int);
- __type(value, struct seal_scratch);
-} seal_scratch_map SEC(".maps");
-
-struct seal_dir_mark {
- char label[LABEL_LEN];
-};
-
-struct {
- __uint(type, BPF_MAP_TYPE_INODE_STORAGE);
- __uint(map_flags, BPF_F_NO_PREALLOC);
- __type(key, int);
- __type(value, struct seal_dir_mark);
-} seal_dir_marks SEC(".maps");
-
-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;
-extern int bpf_inode_init_xattr(struct xattr *xattrs,
- int *xattr_count__ctx_out,
- const char *name__str,
- const struct bpf_dynptr *value_p) __ksym;
-
-/* The mode selects frozen executable identity or committed task authority. */
-static __always_inline int seal_caller(enum shirudo_tamper_point point)
-{
- struct shirudo_sb_id *id;
- __u32 zero = 0;
-
- id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
- if (!id)
- return -EACCES;
- return shirudo_task_is_self(id) ? 0 :
- shirudo_tamper_deny(point, -EACCES);
-}
-
/*
- * No label, or a file system without security xattrs, is not shirudo's
- * inode. Neither is one the caller may not read: the kfunc checks read
- * permission, and failing that closed would refuse every write-only
- * open and every unlink of an unreadable file. Anything else needs
- * shirudo.
+ * In ordinary mode the label marks processes for ptrace/perf protection. It
+ * grants no permission to mutate files or BPF objects; those checks compare
+ * the executable device/inode frozen at init. Stage the mark until commit.
*/
-static __always_inline int seal_verdict(int tlen,
- enum shirudo_tamper_point point)
-{
- switch (tlen) {
- case -ENODATA:
- case -EOPNOTSUPP:
- case -EACCES:
- case -EPERM:
- return 0;
- default:
- return seal_caller(point);
- }
-}
-
-static __always_inline bool seal_is_label(const char *name)
-{
- char buf[sizeof(SHIRUDO_XATTR) + 1];
- long n;
-
- n = bpf_probe_read_kernel_str(buf, sizeof(buf), name);
- if (n != sizeof(SHIRUDO_XATTR))
- return false;
- return !bpf_strncmp(buf, sizeof(SHIRUDO_XATTR), SHIRUDO_XATTR);
-}
-
-static __always_inline struct seal_scratch *
-seal_scratch_get(struct task_struct *task)
-{
- return bpf_task_storage_get(&seal_scratch_map, task, NULL,
- BPF_LOCAL_STORAGE_GET_F_CREATE);
-}
-
-static __always_inline bool seal_is_dir(struct inode *inode)
-{
- return (BPF_CORE_READ(inode, i_mode) & S_IFMT) == S_IFDIR;
-}
-
-static __always_inline void seal_mark(struct inode *inode, const char *label,
- int len)
-{
- struct seal_dir_mark *m;
-
- if (len != LABEL_LEN) {
- bpf_inode_storage_delete(&seal_dir_marks, inode);
- return;
- }
- m = bpf_inode_storage_get(&seal_dir_marks, inode, NULL,
- BPF_LOCAL_STORAGE_GET_F_CREATE);
- if (m)
- __builtin_memcpy(m->label, label, LABEL_LEN);
-}
-
-/* Failed exec preserves the committed authority of the still-running image. */
SEC("lsm.s/bprm_check_security")
int BPF_PROG(shirudo_seal_bprm_check_security,
struct linux_binprm *bprm, int ret)
{
struct task_struct *task = bpf_get_current_task_btf();
- struct shirudo_sb_id *id = shirudo_policy();
struct shirudo_task_authority *authority;
struct seal_scratch *scratch;
- struct inode *real_inode;
struct bpf_dynptr dp;
- bool trusted = false;
int len;
if (ret)
return ret;
- if (!id)
- return -EACCES;
- if (id->runtime_mode) {
- real_inode = bpf_real_data_inode(bprm->file);
- trusted = real_inode == BPF_CORE_READ(bprm, file, f_inode) &&
- shirudo_file_runtime(bprm->file);
- } else {
- scratch = seal_scratch_get(task);
- if (!scratch)
- return 0;
- bpf_dynptr_from_mem(scratch->target, LABEL_LEN, 0, &dp);
- len = bpf_get_file_xattr(bprm->file, SHIRUDO_XATTR, &dp);
- trusted = len == LABEL_LEN;
- }
- if (trusted && id->runtime_mode && BPF_CORE_READ(task, ptrace))
- return -EACCES;
- if (trusted && id->runtime_mode && BPF_CORE_READ(task, seccomp.mode))
+ if (!shirudo_policy())
return -EACCES;
+ scratch = seal_scratch_get(task);
+ if (!scratch)
+ return 0;
+ bpf_dynptr_from_mem(scratch->target, LABEL_LEN, 0, &dp);
+ len = bpf_get_file_xattr(bprm->file, SHIRUDO_XATTR, &dp);
authority = bpf_task_storage_get(&shirudo_tasks, task, NULL,
BPF_LOCAL_STORAGE_GET_F_CREATE);
if (!authority)
- return id->runtime_mode ? -ENOMEM : 0;
- authority->candidate_trusted = trusted;
- authority->candidate_valid = 1;
- return 0;
-}
-
-SEC("lsm.s/bprm_committed_creds")
-void BPF_PROG(shirudo_seal_bprm_committed_creds,
- const struct linux_binprm *bprm)
-{
- struct task_struct *task = bpf_get_current_task_btf();
- struct shirudo_task_authority *authority;
-
- (void)bprm;
- authority = bpf_task_storage_get(&shirudo_tasks, task, NULL, 0);
- if (!authority)
- return;
- authority->trusted = authority->candidate_valid &&
- authority->candidate_trusted;
- authority->candidate_valid = 0;
- authority->candidate_trusted = 0;
-}
-
-SEC("lsm/task_alloc")
-int BPF_PROG(shirudo_seal_task_alloc, struct task_struct *child,
- __u64 clone_flags, int ret)
-{
- struct task_struct *parent = bpf_get_current_task_btf();
- struct shirudo_task_authority *parent_authority;
- struct shirudo_task_authority *authority;
-
- (void)clone_flags;
- if (ret)
- return ret;
- parent_authority = bpf_task_storage_get(&shirudo_tasks, parent,
- NULL, 0);
- if (!parent_authority || !parent_authority->trusted)
- return 0;
- authority = bpf_task_storage_get(&shirudo_tasks, child, NULL,
- BPF_LOCAL_STORAGE_GET_F_CREATE);
- if (!authority) {
- struct shirudo_sb_id *id = shirudo_policy();
-
- return id && id->runtime_mode ? -ENOMEM : 0;
- }
- authority->trusted = parent_authority->trusted;
- return 0;
-}
-
-SEC("lsm/perf_event_alloc")
-int BPF_PROG(shirudo_seal_perf_event_alloc,
- struct perf_event *event, int ret)
-{
- struct task_struct *target;
-
- if (ret)
- return ret;
-
- target = event->hw.target;
- if (!target)
- return 0;
-
- if (shirudo_marked(target))
- return shirudo_tamper_deny(SHIRUDO_TAMPER_SEAL_PERF, -EACCES);
-
- return 0;
-}
-
-SEC("lsm/ptrace_access_check")
-int BPF_PROG(shirudo_seal_ptrace_access_check,
- struct task_struct *child, unsigned int mode, int ret)
-{
- (void)mode;
- if (ret)
- return ret;
-
- if (shirudo_marked(child))
- return shirudo_tamper_deny(SHIRUDO_TAMPER_SEAL_PTRACE, -EACCES);
-
- return 0;
-}
-
-SEC("lsm.s/file_open")
-int BPF_PROG(shirudo_seal_file_open, struct file *file, int ret)
-{
- struct inode *inode = file->f_inode;
- struct task_struct *task;
- struct seal_scratch *s;
- struct bpf_dynptr tdp;
-
- if (ret)
- return ret;
- task = bpf_get_current_task_btf();
- if (seal_is_dir(inode)) {
- /* never opened for writing; mark it */
- s = seal_scratch_get(task);
- if (!s)
- return 0;
- bpf_dynptr_from_mem(s->target, LABEL_LEN, 0, &tdp);
- seal_mark(inode, s->target,
- bpf_get_file_xattr(file, SHIRUDO_XATTR, &tdp));
- return 0;
- }
- if (!(BPF_CORE_READ(file, f_mode) & FMODE_WRITE))
- return 0;
- s = seal_scratch_get(task);
- if (!s) /* cannot read the label */
- return seal_caller(SHIRUDO_TAMPER_SEAL_WRITE);
- bpf_dynptr_from_mem(s->target, LABEL_LEN, 0, &tdp);
- return seal_verdict(bpf_get_file_xattr(file, SHIRUDO_XATTR, &tdp),
- SHIRUDO_TAMPER_SEAL_WRITE);
-}
-
-static __always_inline void seal_mark_dentry(struct dentry *dentry)
-{
- struct inode *inode = dentry->d_inode;
- struct seal_scratch *s;
- struct bpf_dynptr tdp;
-
- if (!inode || !seal_is_dir(inode))
- return;
- s = seal_scratch_get(bpf_get_current_task_btf());
- if (!s)
- return;
- bpf_dynptr_from_mem(s->target, LABEL_LEN, 0, &tdp);
- seal_mark(inode, s->target,
- bpf_get_dentry_xattr(dentry, SHIRUDO_XATTR, &tdp));
-}
-
-SEC("lsm.s/inode_post_setxattr")
-void BPF_PROG(shirudo_seal_inode_post_setxattr, struct dentry *dentry,
- const char *name, const void *value, __u64 size, int flags)
-{
- seal_mark_dentry(dentry);
-}
-
-SEC("lsm.s/inode_post_removexattr")
-void BPF_PROG(shirudo_seal_inode_post_removexattr, struct dentry *dentry,
- const char *name)
-{
- seal_mark_dentry(dentry);
-}
-
-/*
- * The hook cannot sleep, so a new inode takes its label from the
- * parent's mark, and a new directory is marked in turn so a subtree
- * created in one go needs no reopen. A failed claim aborts the
- * creation: better no inode than one of shirudo's without its label,
- * and the verifier wants an LSM errno, not the kfunc's. A file system
- * that takes no xattrs at creation gets its label from userspace.
- */
-SEC("lsm/inode_init_security")
-int BPF_PROG(shirudo_seal_inode_init_security, struct inode *inode,
- struct inode *dir, const struct qstr *qstr,
- struct xattr *xattrs, int *xattr_count)
-{
- struct seal_dir_mark *m, *child;
- struct bpf_dynptr dp;
-
- if (!xattrs)
- return 0;
- m = bpf_inode_storage_get(&seal_dir_marks, dir, NULL, 0);
- if (!m)
- return 0;
- bpf_dynptr_from_mem(m->label, LABEL_LEN, 0, &dp);
- if (bpf_inode_init_xattr(xattrs, xattr_count, SHIRUDO_XATTR, &dp))
- return -EACCES;
- if (!seal_is_dir(inode))
return 0;
- child = bpf_inode_storage_get(&seal_dir_marks, inode, NULL,
- BPF_LOCAL_STORAGE_GET_F_CREATE);
- if (child)
- __builtin_memcpy(child->label, m->label, LABEL_LEN);
- return 0;
-}
-
-static __always_inline int seal_dentry(struct dentry *dentry,
- enum shirudo_tamper_point point)
-{
- struct task_struct *task = bpf_get_current_task_btf();
- struct seal_scratch *s = seal_scratch_get(task);
- struct bpf_dynptr tdp;
-
- if (!s)
- return seal_caller(point);
- bpf_dynptr_from_mem(s->target, LABEL_LEN, 0, &tdp);
- return seal_verdict(bpf_get_dentry_xattr(dentry, SHIRUDO_XATTR, &tdp),
- point);
-}
-
-SEC("lsm.s/inode_unlink")
-int BPF_PROG(shirudo_seal_inode_unlink, struct inode *dir,
- struct dentry *dentry, int ret)
-{
- return ret ? ret : seal_dentry(dentry, SHIRUDO_TAMPER_SEAL_UNLINK);
-}
-
-static __always_inline bool seal_is_exe(struct inode *inode)
-{
- struct shirudo_sb_id *id;
- __u32 zero = 0;
-
- id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
- return id && !id->runtime_mode &&
- BPF_CORE_READ(inode, i_sb, s_dev) == id->exe_dev &&
- BPF_CORE_READ(inode, i_ino) == id->exe_ino;
-}
-
-/*
- * The recorded executable may be renamed over, as a package upgrade
- * does; the new binary is not shirudo until init records it.
- */
-SEC("lsm.s/inode_rename")
-int BPF_PROG(shirudo_seal_inode_rename, struct inode *old_dir,
- struct dentry *old_dentry, struct inode *new_dir,
- struct dentry *new_dentry, int ret)
-{
- struct inode *target = BPF_CORE_READ(new_dentry, d_inode);
- int err;
-
- if (ret)
- return ret;
- err = seal_dentry(old_dentry, SHIRUDO_TAMPER_SEAL_RENAME);
- if (err)
- return err;
- if (!target || seal_is_exe(target))
- return 0;
- return seal_dentry(new_dentry, SHIRUDO_TAMPER_SEAL_RENAME);
-}
-
-SEC("lsm.s/inode_setattr")
-int BPF_PROG(shirudo_seal_inode_setattr, struct mnt_idmap *idmap,
- struct dentry *dentry, struct iattr *attr, int ret)
-{
- return ret ? ret : seal_dentry(dentry, SHIRUDO_TAMPER_SEAL_SETATTR);
-}
-
-SEC("lsm.s/inode_setxattr")
-int BPF_PROG(shirudo_seal_inode_setxattr, struct mnt_idmap *idmap,
- struct dentry *dentry, const char *name, const void *value,
- __u64 size, int flags, int ret)
-{
- if (ret)
- return ret;
- if (seal_is_label(name))
- return seal_caller(SHIRUDO_TAMPER_SEAL_LABEL);
- return seal_dentry(dentry, SHIRUDO_TAMPER_SEAL_XATTR);
-}
-
-SEC("lsm.s/inode_removexattr")
-int BPF_PROG(shirudo_seal_inode_removexattr, struct mnt_idmap *idmap,
- struct dentry *dentry, const char *name, int ret)
-{
- return ret ? ret : seal_dentry(dentry, SHIRUDO_TAMPER_SEAL_XATTR);
-}
-
-#define SHIRUDO_PROT_WRITE 2
-#define SHIRUDO_PROT_EXEC 4
-#define SHIRUDO_VM_EXEC 4
-
-/* Executable mappings in a trusted process must come from its runtime image. */
-SEC("lsm/mmap_file")
-int BPF_PROG(shirudo_seal_mmap_file, struct file *file,
- unsigned long reqprot, unsigned long prot,
- unsigned long flags, int ret)
-{
- struct shirudo_sb_id *id = shirudo_policy();
-
- if (ret)
- return ret;
- if (!id || !id->runtime_mode || !shirudo_task_is_self(id) ||
- shirudo_bootstrap_task() || !(prot & SHIRUDO_PROT_EXEC))
- return 0;
- if ((prot & SHIRUDO_PROT_WRITE) || !shirudo_file_runtime(file))
- return -EACCES;
- return 0;
-}
-
-/* Disallow executable promotion of pages that could have been modified. */
-SEC("lsm/file_mprotect")
-int BPF_PROG(shirudo_seal_file_mprotect, struct vm_area_struct *vma,
- unsigned long reqprot, unsigned long prot, int ret)
-{
- struct shirudo_sb_id *id = shirudo_policy();
- unsigned long old_flags;
-
- if (ret)
- return ret;
- old_flags = BPF_CORE_READ(vma, vm_flags);
- if (!id || !id->runtime_mode || !shirudo_task_is_self(id) ||
- shirudo_bootstrap_task())
- return 0;
- if ((old_flags & SHIRUDO_VM_EXEC) && (prot & SHIRUDO_PROT_WRITE))
- return -EACCES;
- if (!(prot & SHIRUDO_PROT_EXEC))
- return 0;
- if (!(old_flags & SHIRUDO_VM_EXEC) ||
- !shirudo_file_runtime(BPF_CORE_READ(vma, vm_file)))
- return -EACCES;
+ authority->candidate_trusted = len == LABEL_LEN;
+ authority->candidate_valid = 1;
return 0;
}
diff --git a/bpf/seal.h b/bpf/seal.h
new file mode 100644
index 00000000..036fd58b
--- /dev/null
+++ b/bpf/seal.h
@@ -0,0 +1,396 @@
+/* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
+/* Copyright (c) 2026 Authors of shirudo */
+
+#ifndef SHIRUDO_BPF_SEAL_H
+#define SHIRUDO_BPF_SEAL_H
+
+#ifndef ENOMEM
+#define ENOMEM 12
+#endif
+#ifndef EPERM
+#define EPERM 1
+#endif
+#ifndef EACCES
+#define EACCES 13
+#endif
+#ifndef ENODATA
+#define ENODATA 61
+#endif
+#ifndef EOPNOTSUPP
+#define EOPNOTSUPP 95
+#endif
+#ifndef FMODE_WRITE
+#define FMODE_WRITE 0x2
+#endif
+#ifndef S_IFMT
+#define S_IFMT 00170000
+#endif
+#ifndef S_IFDIR
+#define S_IFDIR 0040000
+#endif
+
+#define SHIRUDO_XATTR "security.bpf.shirudo"
+/* The label is a SHA-256 hex string (util/seal.h) -- always 64 bytes. */
+#define LABEL_LEN 64
+
+/*
+ * Xattr kfuncs may sleep and need dynptr-backed map memory. Task storage keeps
+ * this scratch private across CPU migration; stack or shared globals do not.
+ */
+struct seal_scratch {
+ char target[LABEL_LEN];
+};
+
+struct {
+ __uint(type, BPF_MAP_TYPE_TASK_STORAGE);
+ __uint(map_flags, BPF_F_NO_PREALLOC);
+ __type(key, int);
+ __type(value, struct seal_scratch);
+} seal_scratch_map SEC(".maps");
+
+/* The label of a directory, as the last hook with a handle on it read it. */
+struct seal_dir_mark {
+ char label[LABEL_LEN];
+};
+
+struct {
+ __uint(type, BPF_MAP_TYPE_INODE_STORAGE);
+ __uint(map_flags, BPF_F_NO_PREALLOC);
+ __type(key, int);
+ __type(value, struct seal_dir_mark);
+} seal_dir_marks SEC(".maps");
+
+int bpf_get_dentry_xattr(struct dentry *dentry, const char *name__str,
+ struct bpf_dynptr *value_p) __ksym;
+int bpf_get_file_xattr(struct file *file, const char *name__str,
+ struct bpf_dynptr *value_p) __ksym;
+int bpf_inode_init_xattr(struct xattr *xattrs, int *xattr_count__ctx_out,
+ const char *name__str,
+ const struct bpf_dynptr *value_p) __ksym;
+
+/*
+ * Labels identify protected targets in both modes. Caller authority comes from
+ * the selected executable policy, never from a label on the caller's binary.
+ */
+static __always_inline int seal_caller(enum shirudo_tamper_point point)
+{
+ struct shirudo_sb_id *id;
+ __u32 zero = 0;
+
+ id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
+ if (!id)
+ return -EACCES;
+ return shirudo_task_is_self(id) ? 0 :
+ shirudo_tamper_deny(point, -EACCES);
+}
+
+/*
+ * Decide on the result of reading the target's label (tlen). No label
+ * (-ENODATA) or no security xattrs on the file system (-EOPNOTSUPP) is
+ * not shirudo's inode. Neither is one the caller may not read (-EACCES,
+ * -EPERM): the kfunc checks read permission for the caller, so failing
+ * those closed would refuse every write-only open and every unlink of an
+ * unreadable file. A label of any size, or any other error, requires the
+ * caller to be shirudo. 0 = allow, -EACCES = deny.
+ */
+static __always_inline int seal_verdict(int tlen,
+ enum shirudo_tamper_point point)
+{
+ switch (tlen) {
+ case -ENODATA:
+ case -EOPNOTSUPP:
+ case -EACCES:
+ case -EPERM:
+ return 0;
+ default:
+ return seal_caller(point);
+ }
+}
+
+/* Whether an xattr name is the label itself, not a longer name under it. */
+static __always_inline bool seal_is_label(const char *name)
+{
+ char buf[sizeof(SHIRUDO_XATTR) + 1];
+ long n;
+
+ n = bpf_probe_read_kernel_str(buf, sizeof(buf), name);
+ if (n != sizeof(SHIRUDO_XATTR))
+ return false;
+ return !bpf_strncmp(buf, sizeof(SHIRUDO_XATTR), SHIRUDO_XATTR);
+}
+
+static __always_inline struct seal_scratch *
+seal_scratch_get(struct task_struct *task)
+{
+ return bpf_task_storage_get(&seal_scratch_map, task, NULL,
+ BPF_LOCAL_STORAGE_GET_F_CREATE);
+}
+
+static __always_inline bool seal_is_dir(struct inode *inode)
+{
+ return (BPF_CORE_READ(inode, i_mode) & S_IFMT) == S_IFDIR;
+}
+
+/* Remember a directory's label as just read (len bytes), or forget it. */
+static __always_inline void seal_mark(struct inode *inode, const char *label,
+ int len)
+{
+ struct seal_dir_mark *m;
+
+ if (len != LABEL_LEN) {
+ bpf_inode_storage_delete(&seal_dir_marks, inode);
+ return;
+ }
+ m = bpf_inode_storage_get(&seal_dir_marks, inode, NULL,
+ BPF_LOCAL_STORAGE_GET_F_CREATE);
+ if (m)
+ __builtin_memcpy(m->label, label, LABEL_LEN);
+}
+
+/* Commit only the last exec candidate; failed exec never reaches this hook. */
+SEC("lsm.s/bprm_committed_creds")
+void BPF_PROG(shirudo_seal_bprm_committed_creds,
+ const struct linux_binprm *bprm)
+{
+ struct task_struct *task = bpf_get_current_task_btf();
+ struct shirudo_task_authority *authority;
+
+ (void)bprm;
+ authority = bpf_task_storage_get(&shirudo_tasks, task, NULL, 0);
+ if (!authority)
+ return;
+ authority->trusted = authority->candidate_valid &&
+ authority->candidate_trusted;
+ authority->candidate_valid = 0;
+ authority->candidate_trusted = 0;
+}
+
+SEC("lsm/task_alloc")
+int BPF_PROG(shirudo_seal_task_alloc, struct task_struct *child,
+ __u64 clone_flags, int ret)
+{
+ struct task_struct *parent = bpf_get_current_task_btf();
+ struct shirudo_task_authority *parent_authority;
+ struct shirudo_task_authority *authority;
+
+ (void)clone_flags;
+ if (ret)
+ return ret;
+ parent_authority = bpf_task_storage_get(&shirudo_tasks, parent,
+ NULL, 0);
+ if (!parent_authority || !parent_authority->trusted)
+ return 0;
+ authority = bpf_task_storage_get(&shirudo_tasks, child, NULL,
+ BPF_LOCAL_STORAGE_GET_F_CREATE);
+ if (!authority)
+ return SHIRUDO_TASK_STORAGE_REQUIRED ? -ENOMEM : 0;
+ authority->trusted = parent_authority->trusted;
+ return 0;
+}
+
+/* Protect tasks marked by labelled exec (ordinary) or runtime exec (image). */
+SEC("lsm/perf_event_alloc")
+int BPF_PROG(shirudo_seal_perf_event_alloc,
+ struct perf_event *event, int ret)
+{
+ struct task_struct *target;
+
+ if (ret)
+ return ret;
+
+ target = event->hw.target;
+ if (!target)
+ return 0; /* CPU-wide events have no task target to inspect. */
+
+ if (shirudo_marked(target))
+ return shirudo_tamper_deny(SHIRUDO_TAMPER_SEAL_PERF, -EACCES);
+
+ return 0;
+}
+
+/* Both modes keep ptrace away from processes with committed exec marks. */
+SEC("lsm/ptrace_access_check")
+int BPF_PROG(shirudo_seal_ptrace_access_check,
+ struct task_struct *child, unsigned int mode, int ret)
+{
+ (void)mode;
+ if (ret)
+ return ret;
+
+ if (shirudo_marked(child))
+ return shirudo_tamper_deny(SHIRUDO_TAMPER_SEAL_PTRACE, -EACCES);
+
+ return 0;
+}
+
+SEC("lsm.s/file_open")
+int BPF_PROG(shirudo_seal_file_open, struct file *file, int ret)
+{
+ struct inode *inode = file->f_inode;
+ struct task_struct *task;
+ struct seal_scratch *s;
+ struct bpf_dynptr tdp;
+
+ if (ret)
+ return ret;
+ task = bpf_get_current_task_btf();
+ if (seal_is_dir(inode)) {
+ /* Only ever opened for reading; its label marks it. */
+ s = seal_scratch_get(task);
+ if (!s)
+ return 0;
+ bpf_dynptr_from_mem(s->target, LABEL_LEN, 0, &tdp);
+ seal_mark(inode, s->target,
+ bpf_get_file_xattr(file, SHIRUDO_XATTR, &tdp));
+ return 0;
+ }
+ if (!(BPF_CORE_READ(file, f_mode) & FMODE_WRITE))
+ return 0;
+ s = seal_scratch_get(task);
+ if (!s) /* cannot read the label */
+ return seal_caller(SHIRUDO_TAMPER_SEAL_WRITE);
+ bpf_dynptr_from_mem(s->target, LABEL_LEN, 0, &tdp);
+ return seal_verdict(bpf_get_file_xattr(file, SHIRUDO_XATTR, &tdp),
+ SHIRUDO_TAMPER_SEAL_WRITE);
+}
+
+/* Refresh a directory's mark from its label after an xattr change. */
+static __always_inline void seal_mark_dentry(struct dentry *dentry)
+{
+ struct inode *inode = dentry->d_inode;
+ struct seal_scratch *s;
+ struct bpf_dynptr tdp;
+
+ if (!inode || !seal_is_dir(inode))
+ return;
+ s = seal_scratch_get(bpf_get_current_task_btf());
+ if (!s)
+ return;
+ bpf_dynptr_from_mem(s->target, LABEL_LEN, 0, &tdp);
+ seal_mark(inode, s->target,
+ bpf_get_dentry_xattr(dentry, SHIRUDO_XATTR, &tdp));
+}
+
+SEC("lsm.s/inode_post_setxattr")
+void BPF_PROG(shirudo_seal_inode_post_setxattr, struct dentry *dentry,
+ const char *name, const void *value, __u64 size, int flags)
+{
+ seal_mark_dentry(dentry);
+}
+
+SEC("lsm.s/inode_post_removexattr")
+void BPF_PROG(shirudo_seal_inode_post_removexattr, struct dentry *dentry,
+ const char *name)
+{
+ seal_mark_dentry(dentry);
+}
+
+/*
+ * Label a new inode inside the transaction that creates it when its
+ * parent directory is marked, with the parent's label, and mark a new
+ * directory in turn so a subtree created in one go needs no reopen.
+ * The hook cannot sleep, so the label comes from the mark, not from
+ * disk. A claim that fails (no slot left, no memory) aborts the
+ * creation: better no inode than one of shirudo's without its label,
+ * and the verifier wants a constant in the LSM's error range rather
+ * than the kfunc's. A filesystem that takes no xattrs at creation
+ * passes none; the label follows from userspace there.
+ */
+SEC("lsm/inode_init_security")
+int BPF_PROG(shirudo_seal_inode_init_security, struct inode *inode,
+ struct inode *dir, const struct qstr *qstr,
+ struct xattr *xattrs, int *xattr_count)
+{
+ struct seal_dir_mark *m, *child;
+ struct bpf_dynptr dp;
+
+ if (!xattrs)
+ return 0;
+ m = bpf_inode_storage_get(&seal_dir_marks, dir, NULL, 0);
+ if (!m)
+ return 0;
+ bpf_dynptr_from_mem(m->label, LABEL_LEN, 0, &dp);
+ if (bpf_inode_init_xattr(xattrs, xattr_count, SHIRUDO_XATTR, &dp))
+ return -EACCES;
+ if (!seal_is_dir(inode))
+ return 0;
+ child = bpf_inode_storage_get(&seal_dir_marks, inode, NULL,
+ BPF_LOCAL_STORAGE_GET_F_CREATE);
+ if (child)
+ __builtin_memcpy(child->label, m->label, LABEL_LEN);
+ return 0;
+}
+
+static __always_inline int seal_dentry(struct dentry *dentry,
+ enum shirudo_tamper_point point)
+{
+ struct task_struct *task = bpf_get_current_task_btf();
+ struct seal_scratch *s = seal_scratch_get(task);
+ struct bpf_dynptr tdp;
+
+ if (!s)
+ return seal_caller(point);
+ bpf_dynptr_from_mem(s->target, LABEL_LEN, 0, &tdp);
+ return seal_verdict(bpf_get_dentry_xattr(dentry, SHIRUDO_XATTR, &tdp),
+ point);
+}
+
+SEC("lsm.s/inode_unlink")
+int BPF_PROG(shirudo_seal_inode_unlink, struct inode *dir,
+ struct dentry *dentry, int ret)
+{
+ return ret ? ret : seal_dentry(dentry, SHIRUDO_TAMPER_SEAL_UNLINK);
+}
+
+/*
+ * Protect both ends of a rename: moving a labelled inode out and
+ * replacing one. Ordinary mode allows replacement of its recorded binary
+ * for upgrades, without giving the replacement authority. Image mode never
+ * makes that exception: executable files are on the read-only runtime.
+ */
+SEC("lsm.s/inode_rename")
+int BPF_PROG(shirudo_seal_inode_rename, struct inode *old_dir,
+ struct dentry *old_dentry, struct inode *new_dir,
+ struct dentry *new_dentry, int ret)
+{
+ struct inode *target = BPF_CORE_READ(new_dentry, d_inode);
+ int err;
+
+ if (ret)
+ return ret;
+ err = seal_dentry(old_dentry, SHIRUDO_TAMPER_SEAL_RENAME);
+ if (err)
+ return err;
+ if (!target || shirudo_replaceable_executable(target))
+ return 0;
+ return seal_dentry(new_dentry, SHIRUDO_TAMPER_SEAL_RENAME);
+}
+
+/* Truncate, chmod, chown and utimes all come through here. */
+SEC("lsm.s/inode_setattr")
+int BPF_PROG(shirudo_seal_inode_setattr, struct mnt_idmap *idmap,
+ struct dentry *dentry, struct iattr *attr, int ret)
+{
+ return ret ? ret : seal_dentry(dentry, SHIRUDO_TAMPER_SEAL_SETATTR);
+}
+
+SEC("lsm.s/inode_setxattr")
+int BPF_PROG(shirudo_seal_inode_setxattr, struct mnt_idmap *idmap,
+ struct dentry *dentry, const char *name, const void *value,
+ __u64 size, int flags, int ret)
+{
+ if (ret)
+ return ret;
+ if (seal_is_label(name))
+ return seal_caller(SHIRUDO_TAMPER_SEAL_LABEL);
+ return seal_dentry(dentry, SHIRUDO_TAMPER_SEAL_XATTR);
+}
+
+SEC("lsm.s/inode_removexattr")
+int BPF_PROG(shirudo_seal_inode_removexattr, struct mnt_idmap *idmap,
+ struct dentry *dentry, const char *name, int ret)
+{
+ return ret ? ret : seal_dentry(dentry, SHIRUDO_TAMPER_SEAL_XATTR);
+}
+
+#endif /* SHIRUDO_BPF_SEAL_H */
diff --git a/bpf/seal_verity.c b/bpf/seal_verity.c
new file mode 100644
index 00000000..763837ff
--- /dev/null
+++ b/bpf/seal_verity.c
@@ -0,0 +1,87 @@
+// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+/* Image mode retains the writable-state label seal and trusts runtime execs. */
+#include "verity_policy.h"
+#include "seal.h"
+
+char _license[] SEC("license") = "Dual BSD/GPL";
+
+/*
+ * Image mode grants authority only to a direct file on the verified runtime.
+ * Overlay copies cannot qualify. Ptrace or seccomp supplied before exec must
+ * not control the new trusted process. A failed exec retains the old mark.
+ */
+SEC("lsm.s/bprm_check_security")
+int BPF_PROG(shirudo_seal_bprm_check_security,
+ struct linux_binprm *bprm, int ret)
+{
+ struct task_struct *task = bpf_get_current_task_btf();
+ struct shirudo_task_authority *authority;
+ struct inode *real_inode;
+ bool trusted;
+
+ if (ret)
+ return ret;
+ if (!shirudo_policy())
+ return -EACCES;
+ real_inode = bpf_real_data_inode(bprm->file);
+ trusted = real_inode == BPF_CORE_READ(bprm, file, f_inode) &&
+ shirudo_file_runtime(bprm->file);
+ if (trusted && (BPF_CORE_READ(task, ptrace) ||
+ BPF_CORE_READ(task, seccomp.mode)))
+ return -EACCES;
+ authority = bpf_task_storage_get(&shirudo_tasks, task, NULL,
+ BPF_LOCAL_STORAGE_GET_F_CREATE);
+ if (!authority)
+ return -ENOMEM;
+ authority->candidate_trusted = trusted;
+ authority->candidate_valid = 1;
+ return 0;
+}
+
+#define SHIRUDO_PROT_WRITE 2
+#define SHIRUDO_PROT_EXEC 4
+#define SHIRUDO_VM_EXEC 4
+
+/* Executable mappings in a trusted process must come from its runtime image. */
+SEC("lsm/mmap_file")
+int BPF_PROG(shirudo_seal_mmap_file, struct file *file,
+ unsigned long reqprot, unsigned long prot,
+ unsigned long flags, int ret)
+{
+ struct shirudo_sb_id *id = shirudo_policy();
+
+ if (ret)
+ return ret;
+ if (!id || !shirudo_task_is_self(id) ||
+ shirudo_bootstrap_task() || !(prot & SHIRUDO_PROT_EXEC))
+ return 0;
+ if ((prot & SHIRUDO_PROT_WRITE) || !shirudo_file_runtime(file))
+ return -EACCES;
+ return 0;
+}
+
+/* Disallow executable promotion of pages that could have been modified. */
+SEC("lsm/file_mprotect")
+int BPF_PROG(shirudo_seal_file_mprotect, struct vm_area_struct *vma,
+ unsigned long reqprot, unsigned long prot, int ret)
+{
+ struct shirudo_sb_id *id = shirudo_policy();
+ unsigned long old_flags;
+
+ if (ret)
+ return ret;
+ old_flags = BPF_CORE_READ(vma, vm_flags);
+ if (!id || !shirudo_task_is_self(id) ||
+ shirudo_bootstrap_task())
+ return 0;
+ if ((old_flags & SHIRUDO_VM_EXEC) && (prot & SHIRUDO_PROT_WRITE))
+ return -EACCES;
+ if (!(prot & SHIRUDO_PROT_EXEC))
+ return 0;
+ if (!(old_flags & SHIRUDO_VM_EXEC) ||
+ !shirudo_file_runtime(BPF_CORE_READ(vma, vm_file)))
+ return -EACCES;
+ return 0;
+}
diff --git a/bpf/verity_policy.h b/bpf/verity_policy.h
new file mode 100644
index 00000000..389174d7
--- /dev/null
+++ b/bpf/verity_policy.h
@@ -0,0 +1,111 @@
+/* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
+/* Copyright (c) 2026 Authors of shirudo */
+
+#ifndef SHIRUDO_BPF_VERITY_POLICY_H
+#define SHIRUDO_BPF_VERITY_POLICY_H
+
+#include "lsm.h"
+
+#define SHIRUDO_MODE_ERROR SHIRUDO_PR_MODE_IMAGE_ERR
+#define SHIRUDO_TASK_STORAGE_REQUIRED true
+
+#define SHIRUDO_DEVICE_RUNTIME 1
+#define SHIRUDO_DEVICE_CONFIG 2
+
+struct shirudo_device {
+ __u32 dev;
+ __u32 kind;
+ __u8 root[32];
+ __u64 diskseq;
+};
+
+struct {
+ __uint(type, BPF_MAP_TYPE_HASH);
+ __uint(max_entries, 128);
+ __type(key, __u64);
+ __type(value, struct shirudo_device);
+} shirudo_devices SEC(".maps");
+
+struct inode *bpf_real_data_inode(struct file *file) __ksym;
+
+static __always_inline bool shirudo_bootstrap_task(void)
+{
+ struct task_struct *task = bpf_get_current_task_btf();
+ struct shirudo_task_authority *authority;
+
+ authority = bpf_task_storage_get(&shirudo_tasks, task, NULL, 0);
+ return authority && authority->trusted == SHIRUDO_TASK_BOOTSTRAP;
+}
+
+/* The mark changes only when exec commits, so failed exec keeps authority. */
+static __always_inline bool shirudo_task_is_self(const struct shirudo_sb_id *id)
+{
+ return shirudo_marked(bpf_get_current_task_btf());
+}
+
+static __always_inline bool
+shirudo_protected_filesystem(const struct shirudo_sb_id *id, __u32 dev)
+{
+ return dev == id->sb_dev || (id->run_dev && dev == id->run_dev);
+}
+
+/* Device records expose pointers and belong only to trusted image tasks. */
+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));
+}
+
+/* Runtime files are immutable; writable state gets no executable exception. */
+static __always_inline bool shirudo_replaceable_executable(struct inode *inode)
+{
+ return false;
+}
+
+/* Device pointers can be reused even when the free hook did not run. */
+static __always_inline struct shirudo_device *
+shirudo_bdev_device(struct block_device *bdev)
+{
+ struct shirudo_device *device;
+ __u64 cookie = (__u64)bdev;
+
+ if (!bdev)
+ return NULL;
+ device = bpf_map_lookup_elem(&shirudo_devices, &cookie);
+ if (!device || !device->diskseq ||
+ device->dev != BPF_CORE_READ(bdev, bd_dev) ||
+ device->diskseq != BPF_CORE_READ(bdev, bd_disk, diskseq))
+ return NULL;
+ return device;
+}
+
+static __always_inline struct shirudo_device *
+shirudo_inode_device(struct inode *inode)
+{
+ struct block_device *bdev;
+ struct super_block *sb;
+
+ if (!inode)
+ return NULL;
+ sb = BPF_CORE_READ(inode, i_sb);
+ if (!sb || !(BPF_CORE_READ(sb, s_flags) & 1))
+ return NULL;
+ bdev = BPF_CORE_READ(sb, s_bdev);
+ if (!bdev)
+ return NULL;
+ return shirudo_bdev_device(bdev);
+}
+
+static __always_inline bool shirudo_file_runtime(struct file *file)
+{
+ struct shirudo_device *device;
+
+ if (!file)
+ return false;
+ device = shirudo_inode_device(BPF_CORE_READ(file, f_inode));
+ return device && device->kind == SHIRUDO_DEVICE_RUNTIME;
+}
+
+#endif /* SHIRUDO_BPF_VERITY_POLICY_H */
diff --git a/builtin-init.c b/builtin-init.c
index a10ccf08..f4430ed5 100644
--- a/builtin-init.c
+++ b/builtin-init.c
@@ -600,7 +600,8 @@ out:
* the running binary is the initrd's copy, which is gone after the
* switch to the root file system; the identity that lasts is the
* root's binary at the same path, and the initrd's copy is recorded
- * beside it only for the rest of this init.
+ * beside it only for the rest of this init. Image mode records no executable
+ * inode: its successful-exec mark is bound to the authenticated runtime root.
*/
static int identity_executables(struct shirudo_identity *id)
{
@@ -682,7 +683,7 @@ static int attach_guard(struct bpf_program *program,
return attach_pin(program, shirudo_gate_guards[guard].pin, link);
}
-/* Both objects share authority, device state and refusal counters. */
+/* The chosen object pair shares identity, task marks and refusal counters. */
static int share_policy_maps(struct bpf_object *source,
struct bpf_object *destination)
{
@@ -691,6 +692,8 @@ static int share_policy_maps(struct bpf_object *source,
int rc;
for (i = 0; i < ARRAY_SIZE(shirudo_guard_maps); i++) {
+ if (!image_mode && i == SHIRUDO_GUARD_DEVICES)
+ continue;
from = bpf_object__find_map_by_name(source,
shirudo_guard_maps[i].name);
to = bpf_object__find_map_by_name(destination,
@@ -704,12 +707,37 @@ static int share_policy_maps(struct bpf_object *source,
return 0;
}
+/* Attach in order. The caller owns each pinned link, including on failure. */
+static int attach_guard_group(struct bpf_object *obj,
+ const struct shirudo_image_guard *guards,
+ size_t count, struct bpf_link **links)
+{
+ struct bpf_program *program;
+ size_t i;
+ int rc;
+
+ for (i = 0; i < count; i++) {
+ program = bpf_object__find_program_by_name(obj, guards[i].prog);
+ if (!program) {
+ pr_err("program %s missing", guards[i].prog);
+ return -ENOENT;
+ }
+ rc = attach_pin(program, guards[i].pin, &links[i]);
+ if (rc) {
+ pr_err("attach %s: %s", guards[i].prog, strerror(-rc));
+ return rc;
+ }
+ }
+ return 0;
+}
+
+/* Both mode objects retain the same writable-state xattr protection. */
static int attach_seal(struct bpf_object *policy, struct bpf_object **obj_out,
- struct bpf_link **links)
+ struct bpf_link **links, struct bpf_link **image_links)
{
- const struct shirudo_embedded_bpf *blob = seal_blob();
+ const struct shirudo_embedded_bpf *blob = seal_blob(image_mode);
struct bpf_object *obj;
- int err, i;
+ int err;
*obj_out = NULL;
if (!blob || !blob->data || !blob->size) {
@@ -736,24 +764,12 @@ static int attach_seal(struct bpf_object *policy, struct bpf_object **obj_out,
bpf_object__close(obj);
return err;
}
- for (i = 0; i < (int)ARRAY_SIZE(shirudo_seal_guards); i++) {
- const struct shirudo_image_guard *hook =
- &shirudo_seal_guards[i];
- struct bpf_program *p =
- bpf_object__find_program_by_name(obj, hook->prog);
-
- if (!p) {
- pr_err("seal: program %s missing", hook->prog);
- err = -ENOENT;
- break;
- }
- err = attach_pin(p, hook->pin, &links[i]);
- if (err) {
- pr_err("seal: attach %s: %s", hook->prog,
- strerror(-err));
- break;
- }
- }
+ err = attach_guard_group(obj, shirudo_seal_guards,
+ ARRAY_SIZE(shirudo_seal_guards), links);
+ if (!err && image_mode)
+ err = attach_guard_group(obj, shirudo_image_seal_guards,
+ ARRAY_SIZE(shirudo_image_seal_guards),
+ image_links);
if (err) {
bpf_object__close(obj);
return err;
@@ -786,13 +802,54 @@ static void collect_asymmetric(key_serial_t serial, const char *type,
k->serials[k->n++] = (__s32)serial;
}
+/* Seed bootstrap before hooks attach, then forbid userspace map writes. */
+static int freeze_task_maps(struct bpf_object *obj)
+{
+ struct shirudo_task_authority authority = {
+ .trusted = SHIRUDO_TASK_BOOTSTRAP,
+ };
+ struct bpf_map *map;
+ int fd, pidfd, rc = 0;
+ __u32 i;
+
+ pidfd = syscall(SYS_pidfd_open, getpid(), 0);
+ if (pidfd < 0)
+ return -errno;
+ for (i = SHIRUDO_GUARD_TASKS; i < SHIRUDO_GUARD_MAP_COUNT; i++) {
+ const struct shirudo_guard_map *expected =
+ &shirudo_guard_maps[i];
+
+ if (!image_mode && i == SHIRUDO_GUARD_DEVICES)
+ continue;
+ map = bpf_object__find_map_by_name(obj, expected->name);
+ if (!map) {
+ rc = -ENOENT;
+ break;
+ }
+ fd = bpf_map__fd(map);
+ if (i == SHIRUDO_GUARD_TASKS &&
+ bpf_map_update_elem(fd, &pidfd, &authority, BPF_ANY)) {
+ rc = -errno;
+ break;
+ }
+ if (bpf_map_freeze(fd) || bpf_obj_pin(fd, expected->pin)) {
+ rc = -errno;
+ break;
+ }
+ }
+ close(pidfd);
+ return rc;
+}
+
static int init_lsm(bool attach_filesystem_seal)
{
+ struct bpf_link *image_links[SHIRUDO_IMAGE_OBSERVER_HOOK_COUNT] = {};
+ struct bpf_link *image_seal[SHIRUDO_IMAGE_SEAL_HOOK_COUNT] = {};
struct bpf_link *link_setx = NULL, *link_rmx = NULL, *link_pload = NULL;
struct bpf_link *identity_links[ARRAY_SIZE(shirudo_observer_guards)] = {};
struct bpf_link *mount_links[ARRAY_SIZE(shirudo_mount_guards)] = {};
struct bpf_program *p_bpf, *p_bpf_map, *p_unlink, *p_setx, *p_rmx;
- const struct shirudo_embedded_bpf *blob = bpf2_blob();
+ const struct shirudo_embedded_bpf *blob = bpf2_blob(image_mode);
struct bpf_link *seal_links[SHIRUDO_SEAL_HOOK_COUNT] = {};
struct bpf_link *link_bpf = NULL, *link_bpf_map = NULL;
struct bpf_object *obj = NULL, *seal_obj = NULL;
@@ -857,7 +914,11 @@ static int init_lsm(bool attach_filesystem_seal)
id.run_dev = kernel_dev_t(st.st_dev);
}
- /* So inode guards can tell `upatch unload` from an attacker's rm. */
+ /*
+ * Ordinary mode records executable device/inode identities. Image mode
+ * instead records the authenticated runtime root for exec task marks.
+ * Both distinguish authorized `upatch unload` from an attacker's `rm`.
+ */
err = identity_executables(&id);
if (err)
goto out;
@@ -922,42 +983,9 @@ static int init_lsm(bool attach_filesystem_seal)
pr_err("freeze sb-identity map: %s", strerror(errno));
goto out;
}
- {
- const struct shirudo_guard_map *maps =
- &shirudo_guard_maps[SHIRUDO_GUARD_TASKS];
- struct shirudo_task_authority authority = {
- .trusted = SHIRUDO_TASK_BOOTSTRAP,
- };
- struct bpf_map *map;
- int fd, pidfd;
-
- pidfd = syscall(SYS_pidfd_open, getpid(), 0);
- if (pidfd < 0) {
- err = -errno;
- goto out;
- }
- for (i = 0; i < SHIRUDO_GUARD_MAP_COUNT - 1; i++) {
- map = bpf_object__find_map_by_name(obj, maps[i].name);
- if (!map) {
- err = -ENOENT;
- break;
- }
- fd = bpf_map__fd(map);
- if (!i && bpf_map_update_elem(fd, &pidfd, &authority,
- BPF_ANY)) {
- err = -errno;
- break;
- }
- if (bpf_map_freeze(fd) ||
- bpf_obj_pin(fd, maps[i].pin)) {
- err = -errno;
- break;
- }
- }
- close(pidfd);
- if (err)
- goto out;
- }
+ err = freeze_task_maps(obj);
+ if (err)
+ goto out;
/*
* Pinned so that every later command reads the anchor init froze
* from the kernel rather than from a file: the serial a load names
@@ -970,27 +998,16 @@ static int init_lsm(bool attach_filesystem_seal)
goto out;
}
- /* Install the identity guards before enabling the bpf() gates. */
- for (i = 0; i < (int)ARRAY_SIZE(shirudo_observer_guards); i++) {
- const struct shirudo_image_guard *hook =
- &shirudo_observer_guards[i];
- struct bpf_program *p;
-
- p = bpf_object__find_program_by_name(obj, hook->prog);
-
- if (!p) {
- pr_err("program %s missing in bpf2 object",
- hook->prog);
- err = -ENOENT;
- goto out;
- }
- err = attach_pin(p, hook->pin, &identity_links[i]);
- if (err) {
- pr_err("attach %s: %s", hook->prog,
- strerror(-err));
- goto out;
- }
- }
+ /* The executable-identity guard precedes every BPF admission gate. */
+ err = attach_guard_group(obj, shirudo_observer_guards,
+ ARRAY_SIZE(shirudo_observer_guards),
+ identity_links);
+ if (!err && image_mode)
+ err = attach_guard_group(obj, shirudo_image_observer_guards,
+ SHIRUDO_IMAGE_OBSERVER_HOOK_COUNT,
+ image_links);
+ if (err)
+ goto out;
p_unlink = guard_program(obj, SHIRUDO_GUARD_UNLINK);
p_setx = guard_program(obj, SHIRUDO_GUARD_SETXATTR);
@@ -1040,29 +1057,13 @@ static int init_lsm(bool attach_filesystem_seal)
* With the other bpffs guards, so the mount is immutable before
* enforcement goes live.
*/
- for (i = 0; i < (int)ARRAY_SIZE(shirudo_mount_guards); i++) {
- const struct shirudo_image_guard *hook =
- &shirudo_mount_guards[i];
- struct bpf_program *p;
-
- p = bpf_object__find_program_by_name(obj, hook->prog);
-
- if (!p) {
- pr_err("program %s missing in bpf2 object",
- hook->prog);
- err = -ENOENT;
- goto out;
- }
- err = attach_pin(p, hook->pin, &mount_links[i]);
- if (err) {
- pr_err("attach %s: %s", hook->prog,
- strerror(-err));
- goto out;
- }
- }
+ err = attach_guard_group(obj, shirudo_mount_guards,
+ ARRAY_SIZE(shirudo_mount_guards), mount_links);
+ if (err)
+ goto out;
if (attach_filesystem_seal) {
- err = attach_seal(obj, &seal_obj, seal_links);
+ err = attach_seal(obj, &seal_obj, seal_links, image_seal);
if (err)
goto out;
}
@@ -1085,12 +1086,23 @@ static int init_lsm(bool attach_filesystem_seal)
goto out;
}
- pr_info("bpf2%s attached: lsm/bpf gate live%s",
+ pr_info("%s%s attached: lsm/bpf gate live%s",
+ blob->name,
attach_filesystem_seal ? " + seal" : "",
attach_filesystem_seal ? ", bpffs and /var/lib/shirudo sealed" :
", filesystem seal unavailable on this kernel");
err = 0;
out:
+ for (i = 0; i < (int)ARRAY_SIZE(image_seal); i++) {
+ if (err && image_seal[i])
+ bpf_link__unpin(image_seal[i]);
+ bpf_link__destroy(image_seal[i]);
+ }
+ for (i = 0; i < (int)ARRAY_SIZE(image_links); i++) {
+ if (err && image_links[i])
+ bpf_link__unpin(image_links[i]);
+ bpf_link__destroy(image_links[i]);
+ }
for (i = 0; i < (int)ARRAY_SIZE(identity_links); i++) {
if (err && identity_links[i])
bpf_link__unpin(identity_links[i]);
diff --git a/testing/Makefile b/testing/Makefile
index 9199379c..f9ee946f 100644
--- a/testing/Makefile
+++ b/testing/Makefile
@@ -110,7 +110,7 @@ KERNEL_BUILD_STAMP := $(KERNEL_BUILD_ARTIFACT_DIR)/$(VM_KERNEL_IMAGE_NAME).sha25
TEST_VM_BUNDLE_STAMP := $(TEST_VM_BUNDLE_DIR)/.stamp
TEST_VM_BUNDLE_SOURCES := $(sort $(wildcard \
$(ROOT)/*.c $(ROOT)/*.h $(ROOT)/include/*.h $(ROOT)/util/*.c $(ROOT)/util/*.h \
- $(ROOT)/bpf/*.c $(ROOT)/bpf/lib/*.h \
+ $(ROOT)/bpf/*.c $(ROOT)/bpf/*.h $(ROOT)/bpf/lib/*.h \
$(ROOT)/tools/shirudo-build/*.c $(ROOT)/tools/shirudo-build/*.h \
$(ROOT)/vendor/libbpf/bpf/*.c $(ROOT)/vendor/libbpf/bpf/*.h \
$(ROOT)/vendor/libbpf/include/linux/*.h \
diff --git a/util/image_guards.c b/util/image_guards.c
index 6f158cb1..b47c6cde 100644
--- a/util/image_guards.c
+++ b/util/image_guards.c
@@ -45,6 +45,10 @@ shirudo_seal_guards[SHIRUDO_SEAL_HOOK_COUNT] = {
SHIRUDO_SEAL_PTRACE_PIN },
{ "shirudo_seal_perf_event_alloc",
SHIRUDO_SEAL_PERF_PIN },
+};
+
+const struct shirudo_image_guard
+shirudo_image_seal_guards[SHIRUDO_IMAGE_SEAL_HOOK_COUNT] = {
{ "shirudo_seal_mmap_file",
SHIRUDO_BPFFS_DIR "/seal_mmap_link" },
{ "shirudo_seal_file_mprotect",
@@ -61,19 +65,23 @@ shirudo_mount_guards[SHIRUDO_MOUNT_HOOK_COUNT] = {
const struct shirudo_image_guard
shirudo_observer_guards[SHIRUDO_OBSERVER_HOOK_COUNT] = {
- { "shirudo_lsm_file_ioctl", SHIRUDO_BPFFS_DIR "/file_ioctl_link" },
{ "shirudo_lsm_task_prctl", SHIRUDO_TASK_PRCTL_LINK_PIN },
{ "shirudo_lsm_bprm_check_security", SHIRUDO_BPRM_CHECK_LINK_PIN },
- { "shirudo_lsm_bdev_setintegrity",
- SHIRUDO_BPFFS_DIR "/bdev_integrity_link" },
- { "shirudo_lsm_bdev_free_security",
- SHIRUDO_BPFFS_DIR "/bdev_free_link" },
{ "shirudo_lsm_inode_rmdir",
SHIRUDO_BPFFS_DIR "/inode_rmdir_link" },
{ "shirudo_lsm_inode_rename",
SHIRUDO_INODE_RENAME_LINK_PIN },
};
+const struct shirudo_image_guard
+shirudo_image_observer_guards[SHIRUDO_IMAGE_OBSERVER_HOOK_COUNT] = {
+ { "shirudo_lsm_file_ioctl", SHIRUDO_BPFFS_DIR "/file_ioctl_link" },
+ { "shirudo_lsm_bdev_setintegrity",
+ SHIRUDO_BPFFS_DIR "/bdev_integrity_link" },
+ { "shirudo_lsm_bdev_free_security",
+ SHIRUDO_BPFFS_DIR "/bdev_free_link" },
+};
+
const struct shirudo_image_guard
shirudo_gate_guards[SHIRUDO_GATE_HOOK_COUNT] = {
[SHIRUDO_GUARD_UNLINK] = {
@@ -311,6 +319,14 @@ int shirudo_image_guards_ready(void)
.guards = shirudo_seal_guards,
.count = ARRAY_SIZE(shirudo_seal_guards),
},
+ {
+ .guards = shirudo_image_seal_guards,
+ .count = ARRAY_SIZE(shirudo_image_seal_guards),
+ },
+ {
+ .guards = shirudo_image_observer_guards,
+ .count = ARRAY_SIZE(shirudo_image_observer_guards),
+ },
};
__u32 map_ids[SHIRUDO_GUARD_MAP_COUNT] = {};
struct btf *kernel = NULL;
diff --git a/util/image_guards.h b/util/image_guards.h
index 9da40c99..4690a1e7 100644
--- a/util/image_guards.h
+++ b/util/image_guards.h
@@ -8,9 +8,11 @@
#include <stdint.h>
-#define SHIRUDO_SEAL_HOOK_COUNT 16
+#define SHIRUDO_SEAL_HOOK_COUNT 14
+#define SHIRUDO_IMAGE_SEAL_HOOK_COUNT 2
#define SHIRUDO_MOUNT_HOOK_COUNT 4
-#define SHIRUDO_OBSERVER_HOOK_COUNT 7
+#define SHIRUDO_OBSERVER_HOOK_COUNT 4
+#define SHIRUDO_IMAGE_OBSERVER_HOOK_COUNT 3
struct shirudo_image_guard {
const char *prog;
@@ -48,6 +50,10 @@ struct shirudo_guard_map {
extern const struct shirudo_image_guard
shirudo_seal_guards[SHIRUDO_SEAL_HOOK_COUNT];
+extern const struct shirudo_image_guard
+ shirudo_image_seal_guards[SHIRUDO_IMAGE_SEAL_HOOK_COUNT];
+extern const struct shirudo_image_guard
+ shirudo_image_observer_guards[SHIRUDO_IMAGE_OBSERVER_HOOK_COUNT];
extern const struct shirudo_image_guard
shirudo_mount_guards[SHIRUDO_MOUNT_HOOK_COUNT];
extern const struct shirudo_image_guard
diff --git a/util/loader.c b/util/loader.c
index eaa41e5f..2f62590d 100644
--- a/util/loader.c
+++ b/util/loader.c
@@ -10,26 +10,35 @@
#include "loader.h"
asm(".section .rodata, \"a\"\n"
- ".global shirudo_bpf2_o; shirudo_bpf2_o:\n"
- ".incbin \"bpf/bpf2.bpf.o\"\n"
- ".global shirudo_bpf2_o_end; shirudo_bpf2_o_end:\n"
- ".previous\n");
+ ".global shirudo_bpf2_o; shirudo_bpf2_o:\n"
+ ".incbin \"bpf/bpf2.bpf.o\"\n"
+ ".global shirudo_bpf2_o_end; shirudo_bpf2_o_end:\n"
+ ".previous\n");
asm(".section .rodata, \"a\"\n"
- ".global shirudo_seal_o; shirudo_seal_o:\n"
- ".incbin \"bpf/seal.bpf.o\"\n"
- ".global shirudo_seal_o_end; shirudo_seal_o_end:\n"
- ".previous\n");
+ ".global shirudo_seal_o; shirudo_seal_o:\n"
+ ".incbin \"bpf/seal.bpf.o\"\n"
+ ".global shirudo_seal_o_end; shirudo_seal_o_end:\n"
+ ".previous\n");
asm(".section .rodata, \"a\"\n"
- ".global shirudo_unload_bpf_o; shirudo_unload_bpf_o:\n"
- ".incbin \"bpf/unload.bpf.o\"\n"
- ".global shirudo_unload_bpf_o_end; shirudo_unload_bpf_o_end:\n"
- ".previous\n");
+ ".global shirudo_bpf2_verity_o; shirudo_bpf2_verity_o:\n"
+ ".incbin \"bpf/bpf2_verity.bpf.o\"\n"
+ ".global shirudo_bpf2_verity_o_end; shirudo_bpf2_verity_o_end:\n"
+ ".previous\n");
+
+asm(".section .rodata, \"a\"\n"
+ ".global shirudo_seal_verity_o; shirudo_seal_verity_o:\n"
+ ".incbin \"bpf/seal_verity.bpf.o\"\n"
+ ".global shirudo_seal_verity_o_end; shirudo_seal_verity_o_end:\n"
+ ".previous\n");
+
+asm(".section .rodata, \"a\"\n"
+ ".global shirudo_unload_bpf_o; shirudo_unload_bpf_o:\n"
+ ".incbin \"bpf/unload.bpf.o\"\n"
+ ".global shirudo_unload_bpf_o_end; shirudo_unload_bpf_o_end:\n"
+ ".previous\n");
-extern const char shirudo_bpf2_o[], shirudo_bpf2_o_end[];
-extern const char shirudo_seal_o[], shirudo_seal_o_end[];
-extern const char shirudo_unload_bpf_o[], shirudo_unload_bpf_o_end[];
static const struct shirudo_embedded_bpf bpf2 = {
.name = "bpf2",
@@ -50,21 +59,35 @@ static const struct shirudo_embedded_bpf unload = {
.data = shirudo_unload_bpf_o,
};
-const struct shirudo_embedded_bpf *bpf2_blob(void)
+const struct shirudo_embedded_bpf *bpf2_blob(bool image_mode)
{
static struct shirudo_embedded_bpf b;
- b = bpf2;
- b.size = (size_t)(shirudo_bpf2_o_end - shirudo_bpf2_o);
+ if (image_mode) {
+ b.name = "bpf2_verity";
+ b.data = shirudo_bpf2_verity_o;
+ b.size = (size_t)(shirudo_bpf2_verity_o_end -
+ shirudo_bpf2_verity_o);
+ } else {
+ b = bpf2;
+ b.size = (size_t)(shirudo_bpf2_o_end - shirudo_bpf2_o);
+ }
return &b;
}
-const struct shirudo_embedded_bpf *seal_blob(void)
+const struct shirudo_embedded_bpf *seal_blob(bool image_mode)
{
static struct shirudo_embedded_bpf b;
- b = seal;
- b.size = (size_t)(shirudo_seal_o_end - shirudo_seal_o);
+ if (image_mode) {
+ b.name = "seal_verity";
+ b.data = shirudo_seal_verity_o;
+ b.size = (size_t)(shirudo_seal_verity_o_end -
+ shirudo_seal_verity_o);
+ } else {
+ b = seal;
+ b.size = (size_t)(shirudo_seal_o_end - shirudo_seal_o);
+ }
return &b;
}
diff --git a/util/loader.h b/util/loader.h
index 50e9559d..9b1fbc3d 100644
--- a/util/loader.h
+++ b/util/loader.h
@@ -8,16 +8,24 @@
#ifndef SHIRUDO_LOADER_H
#define SHIRUDO_LOADER_H
+#include <stdbool.h>
#include <stddef.h>
+/* Symbols provided by the embedded object assembly in loader.c. */
+extern const char shirudo_bpf2_o[], shirudo_bpf2_o_end[];
+extern const char shirudo_seal_o[], shirudo_seal_o_end[];
+extern const char shirudo_bpf2_verity_o[], shirudo_bpf2_verity_o_end[];
+extern const char shirudo_seal_verity_o[], shirudo_seal_verity_o_end[];
+extern const char shirudo_unload_bpf_o[], shirudo_unload_bpf_o_end[];
+
struct shirudo_embedded_bpf {
const char *name;
const void *data;
size_t size;
};
-const struct shirudo_embedded_bpf *bpf2_blob(void);
-const struct shirudo_embedded_bpf *seal_blob(void);
+const struct shirudo_embedded_bpf *bpf2_blob(bool image_mode);
+const struct shirudo_embedded_bpf *seal_blob(bool image_mode);
const struct shirudo_embedded_bpf *unload_blob(void);
#endif /* SHIRUDO_LOADER_H */
diff --git a/util/preflight.c b/util/preflight.c
index 552b0b93..8b63f496 100644
--- a/util/preflight.c
+++ b/util/preflight.c
@@ -363,17 +363,23 @@ static void check_embedded_loader(struct shirudo_preflight_result *r)
{
struct shirudo_preflight_check *c =
next_slot(r, SHIRUDO_PRE_EMBEDDED_LOADER, "embedded_loader");
- const struct shirudo_embedded_bpf *bpf2 = bpf2_blob();
- const struct shirudo_embedded_bpf *seal = seal_blob();
+ const struct shirudo_embedded_bpf *bpf2 = bpf2_blob(false);
+ const struct shirudo_embedded_bpf *seal = seal_blob(false);
size_t bpf2_sz = bpf2 ? bpf2->size : 0;
size_t seal_sz = seal ? seal->size : 0;
- if (bpf2_sz > 0 && seal_sz > 0)
- pass(c);
- else
+ if (!bpf2_sz || !seal_sz) {
fail(r, c,
"embedded BPF objects missing (bpf2=%zu seal=%zu)",
bpf2_sz, seal_sz);
+ return;
+ }
+ bpf2 = bpf2_blob(true);
+ seal = seal_blob(true);
+ if (bpf2 && bpf2->size && seal && seal->size)
+ pass(c);
+ else
+ fail(r, c, "embedded image-mode BPF objects missing");
}
static void check_tpm(struct shirudo_preflight_result *r)