Skip to content

eBPF-based security platform

bpf: remove clutter

Daniel Borkmanncfa4feddec2d
diff --git a/Makefile.shirudo b/Makefile.shirudo
index 4bc13b99..0fd2417f 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -201,7 +201,7 @@ BPF_VENDOR_HEADERS := bpf_helpers bpf_helper_defs bpf_tracing \
bpf_core_read bpf_endian
BPF_LIBBPF_HEADERS := $(addprefix $(LIBBPF_DIR)/,\
$(addsuffix .h,$(BPF_VENDOR_HEADERS)))
-BPF_LIB_HEADERS := bpf/lib/shirudo_bpf_common.h \
+BPF_LIB_HEADERS := bpf/lib/shirudo_misc.h \
bpf/lib/shirudo_serializer.h \
bpf/lib/shirudo_decoder.h
diff --git a/bpf/bpf2.c b/bpf/bpf2.c
index bd530b0c..7757d778 100644
--- a/bpf/bpf2.c
+++ b/bpf/bpf2.c
@@ -2,95 +2,8 @@
/*
* Copyright (c) 2026 Authors of shirudo
*
- * The central "BPF on BPF" LSM enforcement program.
- *
- * Hooks:
- * lsm/bpf -- gates the bpf() syscall. Kernel-context (signed
- * loader) calls return immediately; userspace
- * BPF_PROG_LOAD must be prog_type=SYSCALL and have
- * a non-zero signature in this cheap pre-filter.
- * The authoritative verdict is enforced by
- * lsm/bpf_prog_load below, after the kernel has
- * verified the signature. Map creation, updates
- * and freezes, test runs, link creation, raw
- * tracepoint opens and pins are shirudo's own
- * executable's alone.
- * lsm/bpf_prog_load -- the admission gate that runs INSIDE the
- * verifier, after signature verification, so the
- * kernel-recorded verdict is final here. A
- * userspace load is admitted only when the kernel
- * recorded BPF_SIG_VERIFIED against exactly the
- * keyring init sealed: sig.verdict must be
- * VERIFIED and sig.keyring_type the kind init
- * froze into the identity map, the bpf keyring
- * (the kernel's singleton, recorded without a
- * serial) or a staged user keyring, which
- * sig.keyring_serial must then name by the
- * serial init froze. Everything else -- unsigned,
- * signed by any other keyring, or signed against
- * a substituted same-named keyring -- is denied.
- * This is what makes "only shirudo-signed BPF
- * loads from userspace" a kernel-enforced
- * invariant rather than a best-effort keyring_id
- * choice by the caller.
- * lsm/bpf_map -- denies writable map file descriptors to every
- * process except shirudo. Read-only map access
- * remains available; this also blocks writable
- * mmap of arena maps.
- * lsm/inode_unlink,
- * lsm/inode_rename -- deny VFS unlink and rename on shirudo's private
- * bpffs superblock unless the caller is shirudo
- * itself (its exe_file inode matches the
- * identity recorded at init). Permanently
- * protects the LSM-link and upatch pins from an
- * attacker's `rm` and `mv` (seal excludes the
- * bpffs mount, so bpf2 is the only protector
- * here), while letting `shirudo upatch unload`
- * remove a pin to detach a program. Rename is
- * guarded because the VFS asks only the rename
- * hook of it and the victim of a rename is
- * unlinked without inode_unlink, so renaming any
- * pin over a link's pin would drop that link's
- * only reference. seal applies the same
- * exe-inode check. The task_prctl hook below
- * prevents non-Shirudo callers from changing
- * mm->exe_file through PR_SET_MM.
- * lsm/key_permission -- guards the trust anchor in the kernel's
- * keyrings: the sealed keyring init froze and the
- * keys it holds may be viewed and searched by
- * anyone, but written, unlinked, revoked, given
- * attributes or looked up for invalidation only
- * by shirudo's own executable. Sealing stops keys
- * from being added; this stops root from taking
- * them away, which is what makes unlinking a key
- * the revocation primitive and nothing else.
- * lsm/inode_setxattr,
- * lsm/inode_removexattr -- same exe-identity gate over the bpffs
- * superblock, so only shirudo may set or remove
- * extended attributes on its own pins. shirudo
- * records each loaded upatch's payload_sha256 as
- * the security.bpf.shirudo.payload xattr on the
- * pin directory; this gate makes that value
- * unforgeable by a root attacker (the unload
- * payload_mismatch check trusts it).
- * lsm/sb_umount,
- * lsm/move_mount,
- * lsm/sb_mount,
- * lsm/sb_remount -- mount-lifecycle lockdown of the private bpffs
- * superblock (identified by s_dev). Unconditional
- * deny: no caller, not even shirudo, may unmount,
- * relocate, mount over, or remount it for the boot,
- * nor unmount any mount with the bpffs beneath it,
- * which a lazy unmount would take down with it.
- * Without this a single `umount` of the bpffs drops
- * the pinned links and detaches ALL enforcement,
- * since shirudo is agentless and the pins are the
- * links' only reference. See the hook block below.
- * lsm/task_prctl -- deny PR_SET_MM executable changes to callers
- * outside the frozen Shirudo executable identity.
- *
- * Every refusal is counted at its point in the tamper map
- * (shirudo_tamper.h), which `shirudo status` reads back.
+ * 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"
@@ -122,7 +35,6 @@ char _license[] SEC("license") = "Dual BSD/GPL";
#include "shirudo_identity.h"
#include "shirudo_tamper.h"
-/* 0 for shirudo's own executable, else a counted -EACCES. */
static __always_inline int shirudo_self_or_deny(enum shirudo_tamper_point point)
{
struct shirudo_sb_id *id;
@@ -145,11 +57,6 @@ int BPF_PROG(shirudo_lsm_bpf, int cmd, union bpf_attr *attr,
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)
@@ -157,43 +64,21 @@ int BPF_PROG(shirudo_lsm_bpf, int cmd, union bpf_attr *attr,
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 shirudo's own
- * executable. Signed-loader map 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.
+ * 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:
@@ -211,32 +96,11 @@ int BPF_PROG(shirudo_lsm_bpf, int cmd, union bpf_attr *attr,
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)
{
@@ -245,18 +109,6 @@ int BPF_PROG(shirudo_lsm_bpf_map, struct bpf_map *map, fmode_t fmode)
return shirudo_self_or_deny(SHIRUDO_TAMPER_MAP_FD);
}
-/*
- * 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)
@@ -265,14 +117,15 @@ int BPF_PROG(shirudo_lsm_bpf_prog_load, struct bpf_prog *prog,
__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 (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;
@@ -281,25 +134,12 @@ int BPF_PROG(shirudo_lsm_bpf_prog_load, struct bpf_prog *prog,
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,
@@ -308,7 +148,6 @@ int BPF_PROG(shirudo_lsm_bpf_prog_load, struct bpf_prog *prog,
return shirudo_tamper_deny(SHIRUDO_TAMPER_LOAD_KEYRING,
-EACCES);
}
-
return 0;
}
@@ -320,9 +159,6 @@ int BPF_PROG(shirudo_lsm_task_prctl, int option, unsigned long arg2,
struct shirudo_sb_id *id;
__u32 zero = 0;
- (void)arg3;
- (void)arg4;
- (void)arg5;
if ((ret && ret != -ENOSYS) || option != PR_SET_MM ||
(arg2 != PR_SET_MM_EXE_FILE && arg2 != PR_SET_MM_MAP))
return ret ? ret : -ENOSYS;
@@ -334,14 +170,6 @@ int BPF_PROG(shirudo_lsm_task_prctl, int option, unsigned long arg2,
return -ENOSYS;
}
-/*
- * Verdict for an operation targeting an inode in superblock target_dev:
- * allow (0) if it is not shirudo's bpffs, or if the calling task's
- * exe_file matches the identity recorded at init; deny (-EPERM),
- * counted at `point`, otherwise. The single guard shared by
- * inode_unlink, inode_rename, inode_setxattr and inode_removexattr --
- * "only shirudo may mutate its own bpffs".
- */
static __always_inline int shirudo_bpffs_guard(__u32 target_dev,
enum shirudo_tamper_point point)
{
@@ -352,7 +180,7 @@ static __always_inline int shirudo_bpffs_guard(__u32 target_dev,
if (!id || id->sb_dev == 0)
return 0;
if (target_dev != id->sb_dev)
- return 0; /* not shirudo's bpffs -- not ours to guard */
+ return 0; /* not shirudo's bpffs -- not ours to guard */
return shirudo_task_is_self(id) ? 0 :
shirudo_tamper_deny(point, -EPERM);
}
@@ -398,7 +226,7 @@ int BPF_PROG(shirudo_lsm_key_permission, key_ref_t key_ref,
if (i < id->n_keys && id->key_serials[i] == serial)
break;
if (i == SHIRUDO_IDENTITY_KEYS_MAX)
- return 0; /* not the anchor -- not ours */
+ return 0; /* not the anchor -- not ours */
}
if (need == KEY_NEED_SEARCH && possessed)
return 0;
diff --git a/bpf/lib/shirudo_bpf_common.h b/bpf/lib/shirudo_bpf_common.h
deleted file mode 100644
index 101608a5..00000000
--- a/bpf/lib/shirudo_bpf_common.h
+++ /dev/null
@@ -1,13 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
-/*
- * Copyright (c) 2026 Authors of shirudo
- *
- * Include-only helpers shared by the bpf/ programs.
- */
-
-#ifndef SHIRUDO_BPF_COMMON_H
-#define SHIRUDO_BPF_COMMON_H
-
-/* TODO: cmd classification table, xattr helpers, etc. */
-
-#endif /* SHIRUDO_BPF_COMMON_H */
diff --git a/bpf/lib/shirudo_identity.h b/bpf/lib/shirudo_identity.h
index b896eced..3dada950 100644
--- a/bpf/lib/shirudo_identity.h
+++ b/bpf/lib/shirudo_identity.h
@@ -39,11 +39,6 @@ struct {
__type(value, struct shirudo_sb_id);
} shirudo_bpffs_sb SEC(".maps");
-/*
- * Whether the calling task runs shirudo's own executable, the one
- * recorded at init, or the initrd's copy of it while that finishes
- * init. A task without an executable is not shirudo.
- */
static __always_inline bool shirudo_task_is_self(const struct shirudo_sb_id *id)
{
struct task_struct *task = bpf_get_current_task_btf();
diff --git a/bpf/lib/shirudo_misc.h b/bpf/lib/shirudo_misc.h
new file mode 100644
index 00000000..e35f33e2
--- /dev/null
+++ b/bpf/lib/shirudo_misc.h
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * Helpers shared by every BPF program.
+ */
+
+#ifndef SHIRUDO_MISC_H
+#define SHIRUDO_MISC_H
+
+#define BUILD_BUG_ON_MSG(cond, msg) _Static_assert(!(cond), msg)
+#define BUILD_BUG_ON(cond) \
+ BUILD_BUG_ON_MSG(cond, "BUILD_BUG_ON failed: " #cond)
+
+#endif /* SHIRUDO_MISC_H */
diff --git a/bpf/lib/shirudo_serializer.h b/bpf/lib/shirudo_serializer.h
index 000e9db3..c07cfa57 100644
--- a/bpf/lib/shirudo_serializer.h
+++ b/bpf/lib/shirudo_serializer.h
@@ -9,6 +9,8 @@
#ifndef SHIRUDO_SERIALIZER_H
#define SHIRUDO_SERIALIZER_H
+#include "shirudo_misc.h"
+
#define SHIRUDO_SERIAL_MAP_NAME "shirudo_ser"
#define SHIRUDO_SERIAL_MAGIC 0x5a4c5253u /* "SRLZ" le */
#define SHIRUDO_SERIAL_VERSION 3u
@@ -60,10 +62,8 @@ struct shirudo_serial_chunk {
unsigned char data[SHIRUDO_SERIAL_CHUNK_PAYLOAD];
};
-_Static_assert(sizeof(struct shirudo_serialize_ctx) ==
- SHIRUDO_SERIAL_CTX_MAX_BYTES,
- "serializer context ABI size changed");
-_Static_assert(sizeof(struct shirudo_serial_chunk) == 4096,
- "serializer chunk ABI must remain one page");
+BUILD_BUG_ON(sizeof(struct shirudo_serialize_ctx) !=
+ SHIRUDO_SERIAL_CTX_MAX_BYTES);
+BUILD_BUG_ON(sizeof(struct shirudo_serial_chunk) != 4096);
#endif /* SHIRUDO_SERIALIZER_H */
diff --git a/bpf/lib/shirudo_tamper.h b/bpf/lib/shirudo_tamper.h
index ae4707ac..955d8408 100644
--- a/bpf/lib/shirudo_tamper.h
+++ b/bpf/lib/shirudo_tamper.h
@@ -4,16 +4,12 @@
*
* The tamper counters: one per point at which a guard refuses a caller,
* counted where the refusal happens and read back by `shirudo status`.
- * Include after vmlinux.h and the libbpf BPF headers.
+ * On the BPF side, include after vmlinux.h and the libbpf headers.
*/
-#ifndef SHIRUDO_BPF_TAMPER_H
-#define SHIRUDO_BPF_TAMPER_H
+#ifndef SHIRUDO_TAMPER_H
+#define SHIRUDO_TAMPER_H
-/*
- * Must match SHIRUDO_TAMPER_POINTS in util/tamper.h in order and
- * number; the reader refuses a map of another size.
- */
enum shirudo_tamper_point {
SHIRUDO_TAMPER_LOAD_NOT_LOADER,
SHIRUDO_TAMPER_LOAD_UNSIGNED,
@@ -46,6 +42,7 @@ enum shirudo_tamper_point {
SHIRUDO_TAMPER_MAX
};
+#ifdef __BPF__
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__uint(max_entries, SHIRUDO_TAMPER_MAX);
@@ -53,12 +50,6 @@ struct {
__type(value, __u64);
} shirudo_tamper SEC(".maps");
-/*
- * Count a refusal at `point` and hand back `err`, so that a deny reads
- * `return shirudo_tamper_deny(SHIRUDO_TAMPER_X, -EACCES);`. Atomic,
- * since refusals on two CPUs may race, and cheap enough for a path
- * that is the exception.
- */
static __always_inline int shirudo_tamper_deny(enum shirudo_tamper_point point,
int err)
{
@@ -70,5 +61,5 @@ static __always_inline int shirudo_tamper_deny(enum shirudo_tamper_point point,
__sync_fetch_and_add(count, 1);
return err;
}
-
-#endif /* SHIRUDO_BPF_TAMPER_H */
+#endif /* __BPF__ */
+#endif /* SHIRUDO_TAMPER_H */
diff --git a/bpf/seal.c b/bpf/seal.c
index 1afdc242..af4191d4 100644
--- a/bpf/seal.c
+++ b/bpf/seal.c
@@ -2,44 +2,8 @@
/*
* Copyright (c) 2026 Authors of shirudo
*
- * Filesystem-sealing LSM: xattr-based labels, never paths.
- *
- * Every file/directory under /var/lib/shirudo/ (except the private bpffs
- * mount, which bpf/bpf2.c guards) is labelled with the inode xattr
- * security.bpf.shirudo whose value is one fixed 64-character string; the
- * shirudo binary carries the same label. The filesystem hooks read the
- * target's label and, if present, require the calling task to run the
- * executable shirudo init recorded in the identity map (the test bpf2
- * applies to the bpffs) -- so only shirudo may modify, unlink, rename,
- * or relabel its own on-disk state. The label only marks a target, and
- * only shirudo may set it; its value is public and a mounted file system
- * can present it, so a binary carrying it proves nothing about the
- * caller. The exec hooks cache whether the successfully executed file
- * carries a Shirudo label in task storage for later access-control
- * decisions. An unlabelled filesystem target is not ours and is allowed
- * through untouched; a label that cannot be read, unless the caller may
- * not read the file, is taken to be there. No bpf_d_path(), no path
- * strings: the label rides the inode, immune to bind-mount / namespace /
- * TOCTOU games. Reads are not gated; only write-mode opens and the
- * mutating inode ops are. Every refusal is counted in the tamper map
- * bpf2 shares with the seal (shirudo_tamper.h).
- *
- * An inode created under a labelled directory is born with the label:
- * lsm/inode_init_security hands it to the filesystem inside the
- * transaction that creates the inode, so there is no instant at which
- * one of shirudo's files exists unlabelled. That hook gets the parent
- * as a bare inode and cannot sleep to read its label, so a labelled
- * directory is remembered in inode storage by the hooks that hold a
- * trusted handle on it: file_open of the directory itself, a setxattr
- * or removexattr on it, and its own creation under a marked parent.
- *
- * The filesystem and exec xattr hooks are sleepable (lsm.s/) because
- * reading an xattr can sleep. The xattr-reading kfuncs write into a
- * bpf_dynptr over map memory (the verifier rejects stack), so the
- * scratch buffer lives in per-task storage: a task is in only one of
- * these syscalls at a time, and its storage survives CPU migration
- * across the sleeping read -- so no cross-invocation races, unlike a
- * shared global buffer.
+ * Filesystem-sealing LSM: an inode label, never a path, marks what only
+ * shirudo may modify.
*/
#include "vmlinux.h"
@@ -74,9 +38,13 @@
char _license[] SEC("license") = "Dual BSD/GPL";
#define SHIRUDO_XATTR "security.bpf.shirudo"
-/* The label is a SHA-256 hex string (util/seal.h) -- always 64 bytes. */
+/* 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];
__u8 exec_candidate_valid;
@@ -91,7 +59,6 @@ struct {
__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];
};
@@ -112,10 +79,6 @@ extern int bpf_inode_init_xattr(struct xattr *xattrs,
const char *name__str,
const struct bpf_dynptr *value_p) __ksym;
-/*
- * 0 if the caller runs the executable recorded at init, else -EACCES,
- * counted at `point`.
- */
static __always_inline int seal_caller(enum shirudo_tamper_point point)
{
struct shirudo_sb_id *id;
@@ -129,13 +92,11 @@ static __always_inline int seal_caller(enum shirudo_tamper_point point)
}
/*
- * 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.
+ * 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.
*/
static __always_inline int seal_verdict(int tlen,
enum shirudo_tamper_point point)
@@ -151,7 +112,6 @@ static __always_inline int seal_verdict(int tlen,
}
}
-/* 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];
@@ -175,7 +135,6 @@ 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)
{
@@ -192,11 +151,8 @@ static __always_inline void seal_mark(struct inode *inode, const char *label,
}
/*
- * Stage the candidate executable's seal label during exec. A later LSM
- * hook may still reject the exec, so the persistent task marker is only
- * updated by bprm_committed_creds after a successful exec. This is because
- * the task struct storage would be reused on the error case for some
- * other task.
+ * A later hook may still reject the exec, so the label is only staged
+ * here and becomes the task's mark in bprm_committed_creds.
*/
SEC("lsm.s/bprm_check_security")
int BPF_PROG(shirudo_seal_bprm_check_security,
@@ -231,7 +187,6 @@ void BPF_PROG(shirudo_seal_bprm_committed_creds,
if (!s || !s->exec_candidate_valid)
return;
if (!s->exec_candidate_labelled) {
- /* Scratch is transient; unlabelled tasks need no persistent entry. */
bpf_task_storage_delete(&seal_scratch_map, task);
return;
}
@@ -258,7 +213,6 @@ int BPF_PROG(shirudo_seal_task_alloc, struct task_struct *child,
return 0;
}
-/* Do not create task-bound perf events for a committed Shirudo task. */
SEC("lsm/perf_event_alloc")
int BPF_PROG(shirudo_seal_perf_event_alloc,
struct perf_event *event, int ret)
@@ -271,7 +225,7 @@ int BPF_PROG(shirudo_seal_perf_event_alloc,
target = event->hw.target;
if (!target)
- return 0; /* CPU-wide events have no task target to inspect. */
+ return 0;
s = bpf_task_storage_get(&seal_scratch_map, target, NULL, 0);
if (s && s->exe_is_shirudo)
@@ -280,7 +234,6 @@ int BPF_PROG(shirudo_seal_perf_event_alloc,
return 0;
}
-/* Do not allow ptrace access to tasks whose committed executable is Shirudo. */
SEC("lsm/ptrace_access_check")
int BPF_PROG(shirudo_seal_ptrace_access_check,
struct task_struct *child, unsigned int mode, int ret)
@@ -308,7 +261,7 @@ int BPF_PROG(shirudo_seal_file_open, struct file *file)
task = bpf_get_current_task_btf();
if (seal_is_dir(inode)) {
- /* Only ever opened for reading; its label marks it. */
+ /* never opened for writing; mark it */
s = seal_scratch_get(task);
if (!s)
return 0;
@@ -318,16 +271,15 @@ int BPF_PROG(shirudo_seal_file_open, struct file *file)
return 0;
}
if (!(BPF_CORE_READ(file, f_mode) & FMODE_WRITE))
- return 0; /* reads are fine */
+ return 0;
s = seal_scratch_get(task);
- if (!s) /* cannot read the label */
+ 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;
@@ -359,15 +311,12 @@ void BPF_PROG(shirudo_seal_inode_post_removexattr, struct 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
+ * 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 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.
+ * 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,
@@ -415,7 +364,6 @@ int BPF_PROG(shirudo_seal_inode_unlink, struct inode *dir,
return seal_dentry(dentry, SHIRUDO_TAMPER_SEAL_UNLINK);
}
-/* Whether an inode is the executable recorded at init. */
static __always_inline bool seal_is_exe(struct inode *inode)
{
struct shirudo_sb_id *id;
@@ -427,9 +375,8 @@ static __always_inline bool seal_is_exe(struct inode *inode)
}
/*
- * Protect both ends of a rename: moving a labelled inode out and
- * replacing one. The recorded executable may be replaced, as a package
- * upgrade does; the new binary is not shirudo until init records it.
+ * 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,
@@ -447,7 +394,6 @@ int BPF_PROG(shirudo_seal_inode_rename, struct inode *old_dir,
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)
diff --git a/testing/t0048-tamper-points.sh b/testing/t0048-tamper-points.sh
index a10f2059..b485585f 100755
--- a/testing/t0048-tamper-points.sh
+++ b/testing/t0048-tamper-points.sh
@@ -2,13 +2,12 @@
# SPDX-License-Identifier: BSD-2-Clause
# Copyright (c) 2026 Authors of shirudo
#
-# t0048 -- the tamper points are declared twice, as the enum the LSM
-# counts with (bpf/lib/shirudo_tamper.h) and as the enum status names
-# them from (util/tamper.h), and a counter is only as good as the two
-# agreeing; this keeps them in the same order with the same members,
-# each with a name and a meaning in the table util/tamper.c holds.
-# Also checks that status reports the counters as absent, not as an
-# error, where init has not run.
+# t0048 -- the tamper points are the one enum the LSM counts with and
+# status names them from (bpf/lib/shirudo_tamper.h), and a counter is
+# only as good as its name; this keeps every member in the table of
+# names and meanings util/tamper.c holds. Also checks that status
+# reports the counters as absent, not as an error, where init has not
+# run.
cd "$(dirname "$0")"
. ./test-lib.sh
@@ -17,19 +16,13 @@ S=../shirudo
W=$(mktemp -d)
trap 'rm -rf "$W"' EXIT
-test_expect_success 'the BPF enum and the userspace enum agree' '
- sed -n "s/^\tSHIRUDO_TAMPER_\([A-Z_]*\),$/\1/p" \
- ../bpf/lib/shirudo_tamper.h >"$W/bpf" &&
- sed -n "s/^\tSHIRUDO_TAMPER_\([A-Z_]*\),$/\1/p" \
- ../util/tamper.h >"$W/user" &&
- [ -s "$W/bpf" ] && [ -s "$W/user" ] &&
- cmp "$W/bpf" "$W/user"
-'
-
test_expect_success 'every point has a name and a meaning' '
+ sed -n "s/^\tSHIRUDO_TAMPER_\([A-Z_]*\),$/\1/p" \
+ ../bpf/lib/shirudo_tamper.h >"$W/points" &&
sed -n "s/^\t\[SHIRUDO_TAMPER_\([A-Z_]*\)\] = { \"[A-Za-z]*\",$/\1/p" \
../util/tamper.c >"$W/table" &&
- cmp "$W/user" "$W/table"
+ [ -s "$W/points" ] &&
+ cmp "$W/points" "$W/table"
'
test_expect_success 'status --json reports the counters as null without init' '
diff --git a/upatches/kernel-obs/obs_common.h b/upatches/kernel-obs/obs_common.h
index a8312083..2e496d73 100644
--- a/upatches/kernel-obs/obs_common.h
+++ b/upatches/kernel-obs/obs_common.h
@@ -5,6 +5,7 @@
#include <bpf/bpf_core_read.h>
#include <bpf/bpf_endian.h>
+#include "shirudo_misc.h"
#if !defined(__BPF_FEATURE_ADDR_SPACE_CAST)
#error "BPF arena access requires clang address-space cast support"
@@ -356,8 +357,7 @@ struct obs_serial_header {
__u32 reserved;
};
-_Static_assert(sizeof(struct obs_serial_header) == 64,
- "kernel-obs serializer header must stay 64 bytes");
+BUILD_BUG_ON(sizeof(struct obs_serial_header) != 64);
#define OBS_SERIAL_VERSION 1
#define OBS_SERIAL_PAGE_CHUNKS 4
diff --git a/upatches/kernel-obs/obs_dns.bpf.c b/upatches/kernel-obs/obs_dns.bpf.c
index a100ae35..3283c097 100644
--- a/upatches/kernel-obs/obs_dns.bpf.c
+++ b/upatches/kernel-obs/obs_dns.bpf.c
@@ -131,10 +131,10 @@ struct dns_hdr {
__u16 arcount;
};
-_Static_assert(sizeof(struct dns_request_key) == 48,
- "dns request key ABI must remain verifier-friendly");
-_Static_assert(sizeof(struct dns_request) == 288,
- "dns request value ABI is shared with the test helper");
+BUILD_BUG_ON_MSG(sizeof(struct dns_request_key) != 48,
+ "dns request key ABI must remain verifier-friendly");
+BUILD_BUG_ON_MSG(sizeof(struct dns_request) != 288,
+ "dns request value ABI is shared with the test helper");
struct {
__uint(type, BPF_MAP_TYPE_LRU_HASH);
diff --git a/upatches/kernel-obs/obs_table.h b/upatches/kernel-obs/obs_table.h
index c30da343..dd087faa 100644
--- a/upatches/kernel-obs/obs_table.h
+++ b/upatches/kernel-obs/obs_table.h
@@ -23,15 +23,14 @@
#define OBS_T_PER_CHUNK (SHIRUDO_SERIAL_CHUNK_PAYLOAD / OBS_T_RECORD)
#define OBS_T_PER_PAGE (OBS_T_PER_CHUNK * OBS_SERIAL_PAGE_CHUNKS)
-_Static_assert(sizeof(OBS_T_KEY) % 8 == 0, "table key must be 8-byte words");
-_Static_assert(sizeof(OBS_T_VAL) % 8 == 0, "table value must be 8-byte words");
-_Static_assert((OBS_T_BUCKETS & (OBS_T_BUCKETS - 1)) == 0,
- "table buckets must be a power of two");
-_Static_assert(OBS_T_RECORD >= sizeof(struct obs_serial_header),
- "table record must hold the page header");
-_Static_assert(OBS_T_RECORD % 8 == 0, "table record must be 8-byte aligned");
-_Static_assert(OBS_T_PER_CHUNK >= 1, "table record must fit a chunk");
-_Static_assert(OBS_T_CAPACITY <= 0xffff, "table capacity must fit the header");
+BUILD_BUG_ON(sizeof(OBS_T_KEY) % 8);
+BUILD_BUG_ON(sizeof(OBS_T_VAL) % 8);
+BUILD_BUG_ON(OBS_T_BUCKETS & (OBS_T_BUCKETS - 1));
+BUILD_BUG_ON(sizeof(struct obs_serial_header) > OBS_T_RECORD);
+BUILD_BUG_ON(OBS_T_RECORD % 8);
+BUILD_BUG_ON(OBS_T_PER_CHUNK < 1);
+BUILD_BUG_ON_MSG(OBS_T_CAPACITY > 0xffff,
+ "table capacity must fit the header");
struct OBS_FN(entry) {
__u32 next;
diff --git a/util/tamper.h b/util/tamper.h
index f1ce6ad4..846a60a8 100644
--- a/util/tamper.h
+++ b/util/tamper.h
@@ -9,43 +9,7 @@
#include <stdint.h>
-/*
- * One counter per point at which a guard refuses a caller. Must match
- * enum shirudo_tamper_point in bpf/lib/shirudo_tamper.h in order and
- * number; the reader refuses a map of another size, and the names and
- * meanings status reports are the table in tamper.c.
- */
-enum shirudo_tamper_point {
- SHIRUDO_TAMPER_LOAD_NOT_LOADER,
- SHIRUDO_TAMPER_LOAD_UNSIGNED,
- SHIRUDO_TAMPER_LOAD_UNVERIFIED,
- SHIRUDO_TAMPER_LOAD_KEYRING_KIND,
- SHIRUDO_TAMPER_LOAD_KEYRING,
- SHIRUDO_TAMPER_MAP_CREATE,
- SHIRUDO_TAMPER_MAP_WRITE,
- SHIRUDO_TAMPER_MAP_FD,
- SHIRUDO_TAMPER_PROG_RUN,
- SHIRUDO_TAMPER_ATTACH,
- SHIRUDO_TAMPER_OBJ_PIN,
- SHIRUDO_TAMPER_BPF_CMD,
- SHIRUDO_TAMPER_EXE_IDENTITY,
- SHIRUDO_TAMPER_KEYRING,
- SHIRUDO_TAMPER_PIN_UNLINK,
- SHIRUDO_TAMPER_PIN_RENAME,
- SHIRUDO_TAMPER_PIN_XATTR,
- SHIRUDO_TAMPER_BPFFS_UMOUNT,
- SHIRUDO_TAMPER_BPFFS_UMOUNT_ABOVE,
- SHIRUDO_TAMPER_BPFFS_MOUNT,
- SHIRUDO_TAMPER_SEAL_WRITE,
- SHIRUDO_TAMPER_SEAL_UNLINK,
- SHIRUDO_TAMPER_SEAL_RENAME,
- SHIRUDO_TAMPER_SEAL_SETATTR,
- SHIRUDO_TAMPER_SEAL_XATTR,
- SHIRUDO_TAMPER_SEAL_LABEL,
- SHIRUDO_TAMPER_SEAL_PTRACE,
- SHIRUDO_TAMPER_SEAL_PERF,
- SHIRUDO_TAMPER_MAX
-};
+#include "bpf/lib/shirudo_tamper.h"
struct shirudo_tamper_counts {
uint64_t count[SHIRUDO_TAMPER_MAX];