Skip to content

eBPF-based security platform

bpf: keep only shared headers, in bpf/lib

Daniel Borkmann0603252c24a4
diff --git a/.github/scripts/check-docs.sh b/.github/scripts/check-docs.sh
index 914da89d..810914e4 100755
--- a/.github/scripts/check-docs.sh
+++ b/.github/scripts/check-docs.sh
@@ -72,7 +72,7 @@ while IFS= read -r counter; do
done <"$work/counters"
# The hooks, counters and tests a threat fact box names exist.
-grep -ohE 'SEC\("lsm(\.s)?/[a-z_]+"\)' bpf/*.c bpf/*.h |
+grep -ohE 'SEC\("lsm(\.s)?/[a-z_]+"\)' bpf/*.c bpf/lib/*.h |
sed -E 's/SEC\("lsm(\.s)?\/([a-z_]+)"\)/\2/' | sort -u >"$work/hooks"
while IFS= read -r page; do
grep -E '^:(Enforced at|Counters|Tests):' "$page" >"$work/facts" || continue
diff --git a/Documentation/contribute/code/developer-guide.rst b/Documentation/contribute/code/developer-guide.rst
index a7f5bbf2..d0fa9a79 100644
--- a/Documentation/contribute/code/developer-guide.rst
+++ b/Documentation/contribute/code/developer-guide.rst
@@ -344,11 +344,13 @@ Project layout
linked into ``shirudo-build`` only.
* - ``bpf/``
- The embedded BPF programs: ``bpf2.c`` (admission, mount, image
- and device guards), ``seal.c`` (the seal), ``unload.c`` (the
- signed unload directive) and their headers.
+ and device guards), ``seal.c`` (the seal) and ``unload.c`` (the
+ signed unload directive).
* - ``bpf/lib/``
- - Headers shared with upatches: decoders, identity, serializers,
- tamper counters and common helpers.
+ - The headers more than one BPF program includes: the LSM helpers
+ and verity device records of ``bpf2.c`` and ``seal.c``, and,
+ shared with upatches and userspace, decoders, identity,
+ serializers, tamper counters and common helpers.
* - ``include/shirudo/``
- Integer types, string and number helpers, the ``.upatch``
container layout, TPM types and the enforcement definitions
diff --git a/Documentation/internals/enforcement.rst b/Documentation/internals/enforcement.rst
index 25b411d2..e02dacaa 100644
--- a/Documentation/internals/enforcement.rst
+++ b/Documentation/internals/enforcement.rst
@@ -34,10 +34,11 @@ and returns an error that the kernel hands back to the caller.
Two objects
-----------
-The binary embeds two BPF objects. ``bpf2`` (``bpf/bpf2.c`` with
-``bpf/admission.h``) holds BPF admission and the guards of shirudo's own
-kernel state. ``seal`` (``bpf/seal.c`` with ``bpf/seal.h``) holds the
-task marks, the process protection and the checks on writable state.
+The binary embeds two BPF objects. ``bpf2`` (``bpf/bpf2.c``) holds BPF
+admission and the guards of shirudo's own kernel state. ``seal``
+(``bpf/seal.c``) holds the task marks, the process protection and the
+checks on writable state. Both share the LSM helpers and the verity
+device records of ``bpf/lib/``.
``init`` loads both without a signature, from the binary the
authenticated initrd carries.
diff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rst
index 62430634..b2ab0389 100644
--- a/Documentation/security/threat-model.rst
+++ b/Documentation/security/threat-model.rst
@@ -631,9 +631,9 @@ neither permissions nor ownership stop root. A changed archive would be
restored at the next boot, a truncated eventlog would make the register
unexplainable, and a planted file would be taken for shirudo's own.
-The seal (``bpf/seal.c`` and ``bpf/seal.h``) marks shirudo's inodes
-with the extended attribute ``security.bpf.shirudo`` and checks every
-operation on a marked inode against the caller's authority. ``init``
+The seal (``bpf/seal.c``) marks shirudo's inodes with the extended
+attribute ``security.bpf.shirudo`` and checks every operation on a
+marked inode against the caller's authority. ``init``
labels the existing tree on the run that brings enforcement up, before
the seal attaches. On every later run a state root without the label is
refused, since it is a tree bound over the path rather than the one
diff --git a/Makefile.shirudo b/Makefile.shirudo
index 283e0a11..7a24ca93 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -365,7 +365,7 @@ build/target/%.o: %.c Makefile.shirudo $(CONFIG_MK_DEP)
# 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) \
- $(wildcard bpf/*.h bpf/lib/*.h) include/shirudo/tpm.h \
+ $(wildcard bpf/lib/*.h) include/shirudo/tpm.h \
include/shirudo/enforcement.h
@printf ' CLANG $@\n'
$(Q)$(CLANG_BPF) -Iinclude -Ibpf/lib -I$(dir $(VMLINUX_H)) -I$(LIBBPF_VENDOR) \
diff --git a/bpf/admission.h b/bpf/admission.h
deleted file mode 100644
index 6364ab80..00000000
--- a/bpf/admission.h
+++ /dev/null
@@ -1,540 +0,0 @@
-/* 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
-
-#include <bpf/bpf_endian.h>
-
-/*
- * BPF mutations require a task marked by successful exec from the runtime
- * image. Verified signed loaders perform their own calls in kernel context.
- */
-static __always_inline int shirudo_self_or_deny(enum tamper_point point)
-{
- struct 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 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 = TAMPER_LOAD_NOT_LOADER;
- else if (BPF_CORE_READ(attr, signature_size) == 0)
- point = 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
- * successful runtime exec. 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(TAMPER_MAP_CREATE);
-
- case BPF_MAP_UPDATE_ELEM:
- case BPF_MAP_FREEZE:
- return shirudo_self_or_deny(TAMPER_MAP_WRITE);
-
- case BPF_PROG_TEST_RUN:
- return shirudo_self_or_deny(TAMPER_PROG_RUN);
-
- case BPF_LINK_CREATE:
- case BPF_RAW_TRACEPOINT_OPEN:
- return shirudo_self_or_deny(TAMPER_ATTACH);
-
- case BPF_OBJ_PIN:
- return shirudo_self_or_deny(TAMPER_OBJ_PIN);
-
- /*
- * A descriptor keeps its object alive: a link held open stays
- * attached after shirudo unpins it for an unload, a revocation or
- * the undo of a refused load, and no LSM hook sees a link descriptor
- * being handed out. A map descriptor reads the state the serializers
- * own. Neither is root's to take, by pin or by id.
- */
- case BPF_OBJ_GET:
- case BPF_MAP_GET_FD_BY_ID:
- case BPF_LINK_GET_FD_BY_ID:
- return shirudo_self_or_deny(TAMPER_OBJ_GET);
-
- /*
- * Read-only / observability cmds. 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_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_BTF_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(TAMPER_BPF_CMD, -EACCES);
- }
-}
-
-/*
- * Writable map fds let userspace alter array contents and writable
- * mmap lets it alter arena pages without another bpf() command; a
- * read-only one reads what the serializers own. Every route to a map
- * descriptor ends here, so every one is shirudo's.
- */
-SEC("lsm/bpf_map")
-int BPF_PROG(shirudo_lsm_bpf_map, struct bpf_map *map, fmode_t fmode,
- int ret)
-{
- if (ret)
- return ret;
- if (fmode & FMODE_WRITE)
- return shirudo_self_or_deny(TAMPER_MAP_FD);
- return shirudo_self_or_deny(TAMPER_OBJ_GET);
-}
-
-/*
- * 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 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(TAMPER_LOAD_UNVERIFIED,
- -EACCES);
- if (ktype != BPF_SIG_KEYRING_BPF)
- return shirudo_tamper_deny(TAMPER_LOAD_KEYRING_KIND,
- -EACCES);
- /* The kernel singleton is recorded without a serial on some loads. */
- if (serial && serial != id->keyring_serial)
- return shirudo_tamper_deny(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 sb_id *id;
- __u32 zero = 0;
-
- (void)arg3;
- (void)arg4;
- (void)arg5;
- if (ret && ret != -ENOSYS)
- return ret;
- if (option == PR_GET_ENFORCEMENT) {
- id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
- if (!id)
- return -EACCES;
- return -PR_ENFORCING_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(TAMPER_EXE_IDENTITY, -EPERM);
- return -ENOSYS;
-}
-
-/* Runtime executables always get secure-exec and refuse tracing at exec. */
-SEC("lsm/bprm_check_security")
-int BPF_PROG(shirudo_lsm_bprm_check_security, struct linux_binprm *bprm,
- int ret)
-{
- struct sb_id *id;
- __u32 zero = 0;
-
- if (ret)
- return ret;
- id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
- if (!id || !shirudo_file_runtime(bprm->file))
- return 0;
- if (BPF_CORE_READ(bprm, unsafe) & LSM_UNSAFE_PTRACE)
- return shirudo_tamper_deny(TAMPER_EXEC_TRACED, -EPERM);
- bpf_bprm_opts_set(bprm, BPF_F_BPRM_SECUREEXEC);
- return 0;
-}
-
-/*
- * Protect bpffs and the filesystem holding image mountpoints. Only a trusted
- * runtime task may change their inodes. Count bpffs refusals at `point`.
- */
-static __always_inline int shirudo_bpffs_guard(__u32 target_dev,
- enum tamper_point point)
-{
- struct 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 != TAMPER_MAX)
- return shirudo_tamper_deny(point, -EPERM);
- return -EPERM;
-}
-
-/*
- * The trust anchors in the kernel's keyrings: the bpf and dm-verity
- * keyrings init froze and the keys they hold. Root owns both with write
- * and setattr permission, so without this hook a single keyctl could
- * unlink, clear, revoke or invalidate the certificates every load and
- * every config root 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 sb_id *id;
- __u32 zero = 0, i;
- struct key *key;
- bool possessed;
- __s32 serial;
-
- if (ret)
- return ret;
- id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
- if (!id)
- return 0;
- key = (struct key *)((unsigned long)key_ref & ~1UL);
- serial = BPF_CORE_READ(key, serial);
- switch (need) {
- case KEY_NEED_WRITE:
- case KEY_NEED_SEARCH:
- case KEY_NEED_SETATTR:
- case KEY_NEED_UNLINK:
- break;
- default:
- return 0;
- }
- if (id->keyring_serial == 0)
- return 0;
- possessed = (unsigned long)key_ref & 1;
- if (serial != id->keyring_serial && serial != id->dm_verity_ring &&
- serial != id->dm_verity_key) {
- for (i = 0; i < IDENTITY_KEYS_MAX; i++)
- if (i < id->n_keys && id->key_serials[i] == serial)
- break;
- if (i == 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(TAMPER_KEYRING, -EACCES);
-}
-
-/*
- * A TPM signature alone cannot change Shirudo's NV register. Restrict
- * extension of its frozen index while leaving unrelated TPM signing alone.
- */
-SEC("lsm/tpm_command")
-int BPF_PROG(shirudo_lsm_tpm_command, struct file *file,
- const __u8 *command, size_t command_len, int ret)
-{
- struct sb_id *id;
- __u32 code, index;
-
- (void)file;
- if (ret)
- return ret;
- if (command_len < 10 ||
- bpf_probe_read_kernel(&code, sizeof(code), command + 6))
- return shirudo_tamper_deny(TAMPER_TPM_EXTEND,
- -EACCES);
- if (bpf_ntohl(code) != TPM_CC_NV_EXTEND)
- return 0;
- if (command_len < 18 ||
- bpf_probe_read_kernel(&index, sizeof(index), command + 14))
- return shirudo_tamper_deny(TAMPER_TPM_EXTEND,
- -EACCES);
- id = shirudo_policy();
- if (!id || (bpf_ntohl(index) == id->nv_index &&
- !shirudo_task_is_self(id)))
- return shirudo_tamper_deny(TAMPER_TPM_EXTEND,
- -EACCES);
- return 0;
-}
-
-/*
- * 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),
- 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),
- TAMPER_MAX);
-}
-
-/*
- * The private bpffs takes no entry shirudo did not make. bpffs lets
- * root make directories, symbolic links and hard links: a symbolic
- * link in place of a pin directory would send the pins of the next
- * load into a bpffs of root's own, where no guard reaches them, and a
- * hard link to a pin would keep the link alive past its unlink.
- */
-SEC("lsm/inode_mkdir")
-int BPF_PROG(shirudo_lsm_inode_mkdir, const struct mnt_idmap *idmap,
- struct inode *dir, struct dentry *dentry, umode_t mode, int ret)
-{
- if (ret)
- return ret;
- return shirudo_bpffs_guard(BPF_CORE_READ(dir, i_sb, s_dev),
- TAMPER_PIN_ENTRY);
-}
-
-SEC("lsm/inode_symlink")
-int BPF_PROG(shirudo_lsm_inode_symlink, const struct mnt_idmap *idmap,
- struct inode *dir, struct dentry *dentry, const char *old_name,
- int ret)
-{
- if (ret)
- return ret;
- return shirudo_bpffs_guard(BPF_CORE_READ(dir, i_sb, s_dev),
- TAMPER_PIN_ENTRY);
-}
-
-SEC("lsm/inode_link")
-int BPF_PROG(shirudo_lsm_inode_link, const struct mnt_idmap *idmap,
- struct dentry *old_dentry, struct inode *dir,
- struct dentry *new_dentry, int ret)
-{
- if (ret)
- return ret;
- return shirudo_bpffs_guard(BPF_CORE_READ(dir, i_sb, s_dev),
- TAMPER_PIN_ENTRY);
-}
-
-/*
- * A kexec hands the machine to a kernel and an initrd of root's
- * choosing with the TPM as it is, the PCRs and this boot's register
- * with it, so the evidence of this boot would vouch for whatever runs
- * after. No task loads a kexec image or initramfs by either system
- * call.
- */
-SEC("lsm/kernel_load_data")
-int BPF_PROG(shirudo_lsm_kernel_load_data, enum kernel_load_data_id id,
- bool contents, int ret)
-{
- if (ret)
- return ret;
- if (id != LOADING_KEXEC_IMAGE && id != LOADING_KEXEC_INITRAMFS)
- return 0;
- return shirudo_tamper_deny(TAMPER_KEXEC, -EPERM);
-}
-
-SEC("lsm/kernel_read_file")
-int BPF_PROG(shirudo_lsm_kernel_read_file, struct file *file,
- enum kernel_read_file_id id, bool contents, int ret)
-{
- if (ret)
- return ret;
- if (id != READING_KEXEC_IMAGE && id != READING_KEXEC_INITRAMFS)
- return 0;
- return shirudo_tamper_deny(TAMPER_KEXEC, -EPERM);
-}
-
-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),
- TAMPER_PIN_RENAME);
- return ret ? ret :
- shirudo_bpffs_guard(BPF_CORE_READ(new_dir, i_sb, s_dev),
- 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),
- 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),
- 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. The VFS calls sb_umount for the
- * requested mount alone, so an unmount of a mount above the bpffs, or
- * of one whose unmount propagates to such a mount, is caught by the walk
- * in bpf2.c from the mount init recorded.
- */
-static __always_inline __u32 shirudo_bpffs_dev(void)
-{
- struct 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 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 d95d095b..fddee370 100644
--- a/bpf/bpf2.c
+++ b/bpf/bpf2.c
@@ -7,7 +7,538 @@
* runtime/config mounts alongside the shared bpffs and BPF admission checks.
*/
#include "verity_policy.h"
-#include "admission.h"
+#include <bpf/bpf_endian.h>
+
+/*
+ * BPF mutations require a task marked by successful exec from the runtime
+ * image. Verified signed loaders perform their own calls in kernel context.
+ */
+static __always_inline int shirudo_self_or_deny(enum tamper_point point)
+{
+ struct 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 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 = TAMPER_LOAD_NOT_LOADER;
+ else if (BPF_CORE_READ(attr, signature_size) == 0)
+ point = 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
+ * successful runtime exec. 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(TAMPER_MAP_CREATE);
+
+ case BPF_MAP_UPDATE_ELEM:
+ case BPF_MAP_FREEZE:
+ return shirudo_self_or_deny(TAMPER_MAP_WRITE);
+
+ case BPF_PROG_TEST_RUN:
+ return shirudo_self_or_deny(TAMPER_PROG_RUN);
+
+ case BPF_LINK_CREATE:
+ case BPF_RAW_TRACEPOINT_OPEN:
+ return shirudo_self_or_deny(TAMPER_ATTACH);
+
+ case BPF_OBJ_PIN:
+ return shirudo_self_or_deny(TAMPER_OBJ_PIN);
+
+ /*
+ * A descriptor keeps its object alive: a link held open stays
+ * attached after shirudo unpins it for an unload, a revocation or
+ * the undo of a refused load, and no LSM hook sees a link descriptor
+ * being handed out. A map descriptor reads the state the serializers
+ * own. Neither is root's to take, by pin or by id.
+ */
+ case BPF_OBJ_GET:
+ case BPF_MAP_GET_FD_BY_ID:
+ case BPF_LINK_GET_FD_BY_ID:
+ return shirudo_self_or_deny(TAMPER_OBJ_GET);
+
+ /*
+ * Read-only / observability cmds. 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_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_BTF_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(TAMPER_BPF_CMD, -EACCES);
+ }
+}
+
+/*
+ * Writable map fds let userspace alter array contents and writable
+ * mmap lets it alter arena pages without another bpf() command; a
+ * read-only one reads what the serializers own. Every route to a map
+ * descriptor ends here, so every one is shirudo's.
+ */
+SEC("lsm/bpf_map")
+int BPF_PROG(shirudo_lsm_bpf_map, struct bpf_map *map, fmode_t fmode,
+ int ret)
+{
+ if (ret)
+ return ret;
+ if (fmode & FMODE_WRITE)
+ return shirudo_self_or_deny(TAMPER_MAP_FD);
+ return shirudo_self_or_deny(TAMPER_OBJ_GET);
+}
+
+/*
+ * 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 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(TAMPER_LOAD_UNVERIFIED,
+ -EACCES);
+ if (ktype != BPF_SIG_KEYRING_BPF)
+ return shirudo_tamper_deny(TAMPER_LOAD_KEYRING_KIND,
+ -EACCES);
+ /* The kernel singleton is recorded without a serial on some loads. */
+ if (serial && serial != id->keyring_serial)
+ return shirudo_tamper_deny(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 sb_id *id;
+ __u32 zero = 0;
+
+ (void)arg3;
+ (void)arg4;
+ (void)arg5;
+ if (ret && ret != -ENOSYS)
+ return ret;
+ if (option == PR_GET_ENFORCEMENT) {
+ id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
+ if (!id)
+ return -EACCES;
+ return -PR_ENFORCING_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(TAMPER_EXE_IDENTITY, -EPERM);
+ return -ENOSYS;
+}
+
+/* Runtime executables always get secure-exec and refuse tracing at exec. */
+SEC("lsm/bprm_check_security")
+int BPF_PROG(shirudo_lsm_bprm_check_security, struct linux_binprm *bprm,
+ int ret)
+{
+ struct sb_id *id;
+ __u32 zero = 0;
+
+ if (ret)
+ return ret;
+ id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
+ if (!id || !shirudo_file_runtime(bprm->file))
+ return 0;
+ if (BPF_CORE_READ(bprm, unsafe) & LSM_UNSAFE_PTRACE)
+ return shirudo_tamper_deny(TAMPER_EXEC_TRACED, -EPERM);
+ bpf_bprm_opts_set(bprm, BPF_F_BPRM_SECUREEXEC);
+ return 0;
+}
+
+/*
+ * Protect bpffs and the filesystem holding image mountpoints. Only a trusted
+ * runtime task may change their inodes. Count bpffs refusals at `point`.
+ */
+static __always_inline int shirudo_bpffs_guard(__u32 target_dev,
+ enum tamper_point point)
+{
+ struct 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 != TAMPER_MAX)
+ return shirudo_tamper_deny(point, -EPERM);
+ return -EPERM;
+}
+
+/*
+ * The trust anchors in the kernel's keyrings: the bpf and dm-verity
+ * keyrings init froze and the keys they hold. Root owns both with write
+ * and setattr permission, so without this hook a single keyctl could
+ * unlink, clear, revoke or invalidate the certificates every load and
+ * every config root 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 sb_id *id;
+ __u32 zero = 0, i;
+ struct key *key;
+ bool possessed;
+ __s32 serial;
+
+ if (ret)
+ return ret;
+ id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
+ if (!id)
+ return 0;
+ key = (struct key *)((unsigned long)key_ref & ~1UL);
+ serial = BPF_CORE_READ(key, serial);
+ switch (need) {
+ case KEY_NEED_WRITE:
+ case KEY_NEED_SEARCH:
+ case KEY_NEED_SETATTR:
+ case KEY_NEED_UNLINK:
+ break;
+ default:
+ return 0;
+ }
+ if (id->keyring_serial == 0)
+ return 0;
+ possessed = (unsigned long)key_ref & 1;
+ if (serial != id->keyring_serial && serial != id->dm_verity_ring &&
+ serial != id->dm_verity_key) {
+ for (i = 0; i < IDENTITY_KEYS_MAX; i++)
+ if (i < id->n_keys && id->key_serials[i] == serial)
+ break;
+ if (i == 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(TAMPER_KEYRING, -EACCES);
+}
+
+/*
+ * A TPM signature alone cannot change Shirudo's NV register. Restrict
+ * extension of its frozen index while leaving unrelated TPM signing alone.
+ */
+SEC("lsm/tpm_command")
+int BPF_PROG(shirudo_lsm_tpm_command, struct file *file,
+ const __u8 *command, size_t command_len, int ret)
+{
+ struct sb_id *id;
+ __u32 code, index;
+
+ (void)file;
+ if (ret)
+ return ret;
+ if (command_len < 10 ||
+ bpf_probe_read_kernel(&code, sizeof(code), command + 6))
+ return shirudo_tamper_deny(TAMPER_TPM_EXTEND,
+ -EACCES);
+ if (bpf_ntohl(code) != TPM_CC_NV_EXTEND)
+ return 0;
+ if (command_len < 18 ||
+ bpf_probe_read_kernel(&index, sizeof(index), command + 14))
+ return shirudo_tamper_deny(TAMPER_TPM_EXTEND,
+ -EACCES);
+ id = shirudo_policy();
+ if (!id || (bpf_ntohl(index) == id->nv_index &&
+ !shirudo_task_is_self(id)))
+ return shirudo_tamper_deny(TAMPER_TPM_EXTEND,
+ -EACCES);
+ return 0;
+}
+
+/*
+ * 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),
+ 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),
+ TAMPER_MAX);
+}
+
+/*
+ * The private bpffs takes no entry shirudo did not make. bpffs lets
+ * root make directories, symbolic links and hard links: a symbolic
+ * link in place of a pin directory would send the pins of the next
+ * load into a bpffs of root's own, where no guard reaches them, and a
+ * hard link to a pin would keep the link alive past its unlink.
+ */
+SEC("lsm/inode_mkdir")
+int BPF_PROG(shirudo_lsm_inode_mkdir, const struct mnt_idmap *idmap,
+ struct inode *dir, struct dentry *dentry, umode_t mode, int ret)
+{
+ if (ret)
+ return ret;
+ return shirudo_bpffs_guard(BPF_CORE_READ(dir, i_sb, s_dev),
+ TAMPER_PIN_ENTRY);
+}
+
+SEC("lsm/inode_symlink")
+int BPF_PROG(shirudo_lsm_inode_symlink, const struct mnt_idmap *idmap,
+ struct inode *dir, struct dentry *dentry, const char *old_name,
+ int ret)
+{
+ if (ret)
+ return ret;
+ return shirudo_bpffs_guard(BPF_CORE_READ(dir, i_sb, s_dev),
+ TAMPER_PIN_ENTRY);
+}
+
+SEC("lsm/inode_link")
+int BPF_PROG(shirudo_lsm_inode_link, const struct mnt_idmap *idmap,
+ struct dentry *old_dentry, struct inode *dir,
+ struct dentry *new_dentry, int ret)
+{
+ if (ret)
+ return ret;
+ return shirudo_bpffs_guard(BPF_CORE_READ(dir, i_sb, s_dev),
+ TAMPER_PIN_ENTRY);
+}
+
+/*
+ * A kexec hands the machine to a kernel and an initrd of root's
+ * choosing with the TPM as it is, the PCRs and this boot's register
+ * with it, so the evidence of this boot would vouch for whatever runs
+ * after. No task loads a kexec image or initramfs by either system
+ * call.
+ */
+SEC("lsm/kernel_load_data")
+int BPF_PROG(shirudo_lsm_kernel_load_data, enum kernel_load_data_id id,
+ bool contents, int ret)
+{
+ if (ret)
+ return ret;
+ if (id != LOADING_KEXEC_IMAGE && id != LOADING_KEXEC_INITRAMFS)
+ return 0;
+ return shirudo_tamper_deny(TAMPER_KEXEC, -EPERM);
+}
+
+SEC("lsm/kernel_read_file")
+int BPF_PROG(shirudo_lsm_kernel_read_file, struct file *file,
+ enum kernel_read_file_id id, bool contents, int ret)
+{
+ if (ret)
+ return ret;
+ if (id != READING_KEXEC_IMAGE && id != READING_KEXEC_INITRAMFS)
+ return 0;
+ return shirudo_tamper_deny(TAMPER_KEXEC, -EPERM);
+}
+
+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),
+ TAMPER_PIN_RENAME);
+ return ret ? ret :
+ shirudo_bpffs_guard(BPF_CORE_READ(new_dir, i_sb, s_dev),
+ 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),
+ 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),
+ 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. The VFS calls sb_umount for the
+ * requested mount alone, so an unmount of a mount above the bpffs, or
+ * of one whose unmount propagates to such a mount, is caught by the walk
+ * in bpf2.c from the mount init recorded.
+ */
+static __always_inline __u32 shirudo_bpffs_dev(void)
+{
+ struct 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 tamper_point point)
+{
+ __u32 bpffs = shirudo_bpffs_dev();
+
+ if (!bpffs || sb_dev != bpffs)
+ return 0;
+ return shirudo_tamper_deny(point, -EPERM);
+}
char _license[] SEC("license") = "Dual BSD/GPL";
diff --git a/bpf/lsm.h b/bpf/lib/lsm.h
similarity index 98%
rename from bpf/lsm.h
rename to bpf/lib/lsm.h
index bd68a761..0f6f2327 100644
--- a/bpf/lsm.h
+++ b/bpf/lib/lsm.h
@@ -37,7 +37,7 @@
#include <shirudo/enforcement.h>
-#include "lib/identity.h"
+#include "identity.h"
#include "tamper.h"
#define TASK_TRUSTED 1
diff --git a/bpf/verity_policy.h b/bpf/lib/verity_policy.h
similarity index 100%
rename from bpf/verity_policy.h
rename to bpf/lib/verity_policy.h
diff --git a/bpf/seal.c b/bpf/seal.c
index 8b402cc4..94b97ee5 100644
--- a/bpf/seal.c
+++ b/bpf/seal.c
@@ -3,7 +3,467 @@
/* Shirudo retains the writable-state label seal and trusts runtime execs. */
#include "verity_policy.h"
-#include "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
+
+#define 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 an inode as the last hook with a handle on it read it: a
+ * directory at every open, a file at an open for writing.
+ */
+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 writable targets. Caller authority comes from
+ * successful runtime exec, never from a label on the caller's binary.
+ */
+static __always_inline int seal_caller(enum tamper_point point)
+{
+ struct 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 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(XATTR) + 1];
+ long n;
+
+ n = bpf_probe_read_kernel_str(buf, sizeof(buf), name);
+ if (n != sizeof(XATTR))
+ return false;
+ return !bpf_strncmp(buf, sizeof(XATTR), 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 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 task_authority *parent_authority;
+ struct 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 -ENOMEM;
+ authority->trusted = parent_authority->trusted;
+ return 0;
+}
+
+/* Protect tasks marked by successful exec from the runtime 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(TAMPER_SEAL_PERF, -EACCES);
+
+ return 0;
+}
+
+/* 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(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;
+ int tlen;
+
+ 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, 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(TAMPER_SEAL_WRITE);
+ bpf_dynptr_from_mem(s->target, LABEL_LEN, 0, &tdp);
+ tlen = bpf_get_file_xattr(file, XATTR, &tdp);
+ if (tlen == LABEL_LEN)
+ seal_mark(inode, s->target, tlen);
+ return seal_verdict(tlen, TAMPER_SEAL_WRITE);
+}
+
+/*
+ * An overlay copies a lower inode up before the first write reaches it,
+ * label and all, since the copy-up runs as the writer: a write shirudo
+ * made through one mounted over sealed state landed in the upper
+ * directory, root's, and the sealed inode stayed as it was. Sealed
+ * state is never copied up. The hook cannot sleep, so the mark a write
+ * open or a directory open leaves stands in for the label.
+ */
+SEC("lsm/inode_copy_up")
+int BPF_PROG(shirudo_seal_inode_copy_up, struct dentry *src,
+ struct cred **new, int ret)
+{
+ struct inode *inode = src->d_inode;
+
+ (void)new;
+ if (ret)
+ return ret;
+ if (!inode || !bpf_inode_storage_get(&seal_dir_marks, inode, NULL, 0))
+ return 0;
+ return shirudo_tamper_deny(TAMPER_SEAL_COPY_UP, -EPERM);
+}
+
+/* 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, 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, 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 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, 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, TAMPER_SEAL_UNLINK);
+}
+
+/*
+ * A sealed directory takes new entries from shirudo alone. The label a
+ * creation inherits stops writes to the new inode, but a rename or a
+ * link brings an inode in with its contents, and a symbolic link sends
+ * whoever follows it elsewhere. The mark stands in for the label these
+ * hooks cannot read.
+ */
+static __always_inline int seal_dir_entry(struct inode *dir)
+{
+ if (!bpf_inode_storage_get(&seal_dir_marks, dir, NULL, 0))
+ return 0;
+ return seal_caller(TAMPER_SEAL_ENTRY);
+}
+
+SEC("lsm/inode_create")
+int BPF_PROG(shirudo_seal_inode_create, const struct mnt_idmap *idmap,
+ struct inode *dir, struct dentry *dentry, umode_t mode, int ret)
+{
+ return ret ? ret : seal_dir_entry(dir);
+}
+
+SEC("lsm/inode_mkdir")
+int BPF_PROG(shirudo_seal_inode_mkdir, const struct mnt_idmap *idmap,
+ struct inode *dir, struct dentry *dentry, umode_t mode, int ret)
+{
+ return ret ? ret : seal_dir_entry(dir);
+}
+
+SEC("lsm/inode_symlink")
+int BPF_PROG(shirudo_seal_inode_symlink, const struct mnt_idmap *idmap,
+ struct inode *dir, struct dentry *dentry, const char *old_name,
+ int ret)
+{
+ return ret ? ret : seal_dir_entry(dir);
+}
+
+SEC("lsm/inode_mknod")
+int BPF_PROG(shirudo_seal_inode_mknod, const struct mnt_idmap *idmap,
+ struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev,
+ int ret)
+{
+ return ret ? ret : seal_dir_entry(dir);
+}
+
+SEC("lsm/inode_link")
+int BPF_PROG(shirudo_seal_inode_link, const struct mnt_idmap *idmap,
+ struct dentry *old_dentry, struct inode *dir,
+ struct dentry *new_dentry, int ret)
+{
+ return ret ? ret : seal_dir_entry(dir);
+}
+
+/* Protect the source, the destination directory and an existing target. */
+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, TAMPER_SEAL_RENAME);
+ if (err)
+ return err;
+ err = seal_dir_entry(new_dir);
+ if (err)
+ return err;
+ if (!target)
+ return 0;
+ return seal_dentry(new_dentry, 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, 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(TAMPER_SEAL_LABEL);
+ return seal_dentry(dentry, 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, TAMPER_SEAL_XATTR);
+}
char _license[] SEC("license") = "Dual BSD/GPL";
diff --git a/bpf/seal.h b/bpf/seal.h
deleted file mode 100644
index 00ebe1ea..00000000
--- a/bpf/seal.h
+++ /dev/null
@@ -1,468 +0,0 @@
-/* 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
-
-#define 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 an inode as the last hook with a handle on it read it: a
- * directory at every open, a file at an open for writing.
- */
-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 writable targets. Caller authority comes from
- * successful runtime exec, never from a label on the caller's binary.
- */
-static __always_inline int seal_caller(enum tamper_point point)
-{
- struct 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 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(XATTR) + 1];
- long n;
-
- n = bpf_probe_read_kernel_str(buf, sizeof(buf), name);
- if (n != sizeof(XATTR))
- return false;
- return !bpf_strncmp(buf, sizeof(XATTR), 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 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 task_authority *parent_authority;
- struct 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 -ENOMEM;
- authority->trusted = parent_authority->trusted;
- return 0;
-}
-
-/* Protect tasks marked by successful exec from the runtime 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(TAMPER_SEAL_PERF, -EACCES);
-
- return 0;
-}
-
-/* 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(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;
- int tlen;
-
- 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, 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(TAMPER_SEAL_WRITE);
- bpf_dynptr_from_mem(s->target, LABEL_LEN, 0, &tdp);
- tlen = bpf_get_file_xattr(file, XATTR, &tdp);
- if (tlen == LABEL_LEN)
- seal_mark(inode, s->target, tlen);
- return seal_verdict(tlen, TAMPER_SEAL_WRITE);
-}
-
-/*
- * An overlay copies a lower inode up before the first write reaches it,
- * label and all, since the copy-up runs as the writer: a write shirudo
- * made through one mounted over sealed state landed in the upper
- * directory, root's, and the sealed inode stayed as it was. Sealed
- * state is never copied up. The hook cannot sleep, so the mark a write
- * open or a directory open leaves stands in for the label.
- */
-SEC("lsm/inode_copy_up")
-int BPF_PROG(shirudo_seal_inode_copy_up, struct dentry *src,
- struct cred **new, int ret)
-{
- struct inode *inode = src->d_inode;
-
- (void)new;
- if (ret)
- return ret;
- if (!inode || !bpf_inode_storage_get(&seal_dir_marks, inode, NULL, 0))
- return 0;
- return shirudo_tamper_deny(TAMPER_SEAL_COPY_UP, -EPERM);
-}
-
-/* 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, 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, 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 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, 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, TAMPER_SEAL_UNLINK);
-}
-
-/*
- * A sealed directory takes new entries from shirudo alone. The label a
- * creation inherits stops writes to the new inode, but a rename or a
- * link brings an inode in with its contents, and a symbolic link sends
- * whoever follows it elsewhere. The mark stands in for the label these
- * hooks cannot read.
- */
-static __always_inline int seal_dir_entry(struct inode *dir)
-{
- if (!bpf_inode_storage_get(&seal_dir_marks, dir, NULL, 0))
- return 0;
- return seal_caller(TAMPER_SEAL_ENTRY);
-}
-
-SEC("lsm/inode_create")
-int BPF_PROG(shirudo_seal_inode_create, const struct mnt_idmap *idmap,
- struct inode *dir, struct dentry *dentry, umode_t mode, int ret)
-{
- return ret ? ret : seal_dir_entry(dir);
-}
-
-SEC("lsm/inode_mkdir")
-int BPF_PROG(shirudo_seal_inode_mkdir, const struct mnt_idmap *idmap,
- struct inode *dir, struct dentry *dentry, umode_t mode, int ret)
-{
- return ret ? ret : seal_dir_entry(dir);
-}
-
-SEC("lsm/inode_symlink")
-int BPF_PROG(shirudo_seal_inode_symlink, const struct mnt_idmap *idmap,
- struct inode *dir, struct dentry *dentry, const char *old_name,
- int ret)
-{
- return ret ? ret : seal_dir_entry(dir);
-}
-
-SEC("lsm/inode_mknod")
-int BPF_PROG(shirudo_seal_inode_mknod, const struct mnt_idmap *idmap,
- struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev,
- int ret)
-{
- return ret ? ret : seal_dir_entry(dir);
-}
-
-SEC("lsm/inode_link")
-int BPF_PROG(shirudo_seal_inode_link, const struct mnt_idmap *idmap,
- struct dentry *old_dentry, struct inode *dir,
- struct dentry *new_dentry, int ret)
-{
- return ret ? ret : seal_dir_entry(dir);
-}
-
-/* Protect the source, the destination directory and an existing target. */
-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, TAMPER_SEAL_RENAME);
- if (err)
- return err;
- err = seal_dir_entry(new_dir);
- if (err)
- return err;
- if (!target)
- return 0;
- return seal_dentry(new_dentry, 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, 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(TAMPER_SEAL_LABEL);
- return seal_dentry(dentry, 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, TAMPER_SEAL_XATTR);
-}
-
-#endif /* SHIRUDO_BPF_SEAL_H */
diff --git a/testing/Makefile b/testing/Makefile
index f0fdaa86..fef80232 100644
--- a/testing/Makefile
+++ b/testing/Makefile
@@ -119,7 +119,7 @@ TEST_VM_BUNDLE_STAMP := $(TEST_VM_BUNDLE_DIR)/.stamp
TEST_VM_BUNDLE_SOURCES := $(sort $(wildcard \
$(ROOT)/*.c $(ROOT)/*.h $(ROOT)/include/shirudo/*.h \
$(ROOT)/util/*.c $(ROOT)/util/*.h \
- $(ROOT)/bpf/*.c $(ROOT)/bpf/*.h $(ROOT)/bpf/lib/*.h \
+ $(ROOT)/bpf/*.c $(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 \