Skip to content

eBPF-based security platform

bpf: trust tasks that execute from the runtime image

KP Singh44c910e449ba
diff --git a/Makefile.shirudo b/Makefile.shirudo
index bd972ac9..d085141a 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -193,6 +193,7 @@ UTIL_SRCS := \
util/kernel_build_id.c \
util/kernel_release.c \
util/config.c \
+ util/image_guards.c \
util/config_image.c \
util/dm_verity.c \
util/bpm_source_config.c \
diff --git a/bpf/bpf2.c b/bpf/bpf2.c
index 08645a4f..529c5969 100644
--- a/bpf/bpf2.c
+++ b/bpf/bpf2.c
@@ -33,9 +33,11 @@
char _license[] SEC("license") = "Dual BSD/GPL";
-#include "shirudo_identity.h"
+#include "policy.h"
+#include "../include/shirudo_mode.h"
#include "shirudo_tamper.h"
+/* The mode selects executable identity or committed task authority. */
static __always_inline int shirudo_self_or_deny(enum shirudo_tamper_point point)
{
struct shirudo_sb_id *id;
@@ -49,10 +51,12 @@ static __always_inline int shirudo_self_or_deny(enum shirudo_tamper_point point)
SEC("lsm/bpf")
int BPF_PROG(shirudo_lsm_bpf, int cmd, union bpf_attr *attr,
- unsigned int size, bool kernel)
+ unsigned int size, bool kernel, int ret)
{
enum shirudo_tamper_point point;
+ if (ret)
+ return ret;
if (kernel)
return 0;
@@ -103,16 +107,30 @@ int BPF_PROG(shirudo_lsm_bpf, int cmd, union bpf_attr *attr,
}
SEC("lsm/bpf_map")
-int BPF_PROG(shirudo_lsm_bpf_map, struct bpf_map *map, fmode_t fmode)
+int BPF_PROG(shirudo_lsm_bpf_map, struct bpf_map *map, fmode_t fmode,
+ int ret)
{
- if (!(fmode & FMODE_WRITE))
+ struct shirudo_sb_id *id = shirudo_policy();
+ char name[16] = {};
+ bool private;
+
+ if (ret)
+ return ret;
+ bpf_probe_read_kernel_str(name, sizeof(name), map->name);
+ private = !__builtin_memcmp(name, "shirudo_devices", 16);
+ if (fmode & FMODE_WRITE)
+ return shirudo_self_or_deny(SHIRUDO_TAMPER_MAP_FD);
+ if (!private)
return 0;
- return shirudo_self_or_deny(SHIRUDO_TAMPER_MAP_FD);
+ if (!id || !shirudo_task_is_self(id))
+ return -EACCES;
+ return 0;
}
SEC("lsm/bpf_prog_load")
int BPF_PROG(shirudo_lsm_bpf_prog_load, struct bpf_prog *prog,
- union bpf_attr *attr, struct bpf_token *token, bool kernel)
+ union bpf_attr *attr, struct bpf_token *token,
+ bool kernel, int ret)
{
struct shirudo_sb_id *id;
__u32 zero = 0, verdict, ktype;
@@ -124,6 +142,8 @@ int BPF_PROG(shirudo_lsm_bpf_prog_load, struct bpf_prog *prog,
* allowed (the loader itself passed this gate when it was loaded
* from userspace).
*/
+ if (ret)
+ return ret;
if (kernel)
return 0;
@@ -160,7 +180,16 @@ int BPF_PROG(shirudo_lsm_task_prctl, int option, unsigned long arg2,
struct shirudo_sb_id *id;
__u32 zero = 0;
- if ((ret && ret != -ENOSYS) || option != PR_SET_MM ||
+ if (ret && ret != -ENOSYS)
+ return ret;
+ if (option == SHIRUDO_PR_GET_MODE) {
+ id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
+ if (!id)
+ return -EACCES;
+ return id->runtime_mode ? -SHIRUDO_PR_MODE_IMAGE_ERR :
+ -SHIRUDO_PR_MODE_LEGACY_ERR;
+ }
+ if (option != PR_SET_MM ||
(arg2 != PR_SET_MM_EXE_FILE && arg2 != PR_SET_MM_MAP))
return ret ? ret : -ENOSYS;
id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
@@ -198,10 +227,14 @@ static __always_inline int shirudo_bpffs_guard(__u32 target_dev,
id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
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 shirudo_task_is_self(id) ? 0 :
- shirudo_tamper_deny(point, -EPERM);
+ if (target_dev != id->sb_dev &&
+ (!id->runtime_mode || !id->run_dev || target_dev != id->run_dev))
+ return 0;
+ if (shirudo_task_is_self(id))
+ return 0;
+ if (target_dev == id->sb_dev && point != SHIRUDO_TAMPER_MAX)
+ return shirudo_tamper_deny(point, -EPERM);
+ return -EPERM;
}
/*
@@ -217,7 +250,7 @@ static __always_inline int shirudo_bpffs_guard(__u32 target_dev,
*/
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)
+ const struct cred *cred, enum key_need_perm need, int ret)
{
struct shirudo_sb_id *id;
__u32 zero = 0, i;
@@ -225,6 +258,8 @@ int BPF_PROG(shirudo_lsm_key_permission, key_ref_t key_ref,
bool possessed;
__s32 serial;
+ if (ret)
+ return ret;
switch (need) {
case KEY_NEED_WRITE:
case KEY_NEED_SEARCH:
@@ -262,25 +297,35 @@ int BPF_PROG(shirudo_lsm_key_permission, key_ref_t key_ref,
*/
SEC("lsm/inode_unlink")
int BPF_PROG(shirudo_lsm_inode_unlink, struct inode *dir,
- struct dentry *dentry)
+ struct dentry *dentry, int ret)
{
+ if (ret)
+ return ret;
return shirudo_bpffs_guard(BPF_CORE_READ(dir, i_sb, s_dev),
SHIRUDO_TAMPER_PIN_UNLINK);
}
-/*
- * Rename is the other way a pin can go: the VFS asks only this hook of
- * it, and simple_rename() unlinks the victim without inode_unlink, so
- * renaming any pin over a link's pin would drop that link's only
- * reference. Both directories share the superblock, a rename never
- * crossing one, so the source directory names it for both ends.
- */
+SEC("lsm/inode_rmdir")
+int BPF_PROG(shirudo_lsm_inode_rmdir, struct inode *dir,
+ struct dentry *dentry, int ret)
+{
+ if (ret)
+ return ret;
+ return shirudo_bpffs_guard(BPF_CORE_READ(dir, i_sb, s_dev),
+ SHIRUDO_TAMPER_MAX);
+}
+
SEC("lsm/inode_rename")
int BPF_PROG(shirudo_lsm_inode_rename, struct inode *old_dir,
struct dentry *old_dentry, struct inode *new_dir,
- struct dentry *new_dentry)
+ struct dentry *new_dentry, int ret)
{
- return shirudo_bpffs_guard(BPF_CORE_READ(old_dir, i_sb, s_dev),
+ if (ret)
+ return ret;
+ ret = shirudo_bpffs_guard(BPF_CORE_READ(old_dir, i_sb, s_dev),
+ SHIRUDO_TAMPER_PIN_RENAME);
+ return ret ? ret :
+ shirudo_bpffs_guard(BPF_CORE_READ(new_dir, i_sb, s_dev),
SHIRUDO_TAMPER_PIN_RENAME);
}
@@ -292,8 +337,10 @@ int BPF_PROG(shirudo_lsm_inode_rename, struct inode *old_dir,
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)
+ __u64 size, int flags, int ret)
{
+ if (ret)
+ return ret;
return shirudo_bpffs_guard(BPF_CORE_READ(dentry, d_inode, i_sb, s_dev),
SHIRUDO_TAMPER_PIN_XATTR);
}
@@ -301,8 +348,10 @@ int BPF_PROG(shirudo_lsm_inode_setxattr, struct mnt_idmap *idmap,
/* 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)
+ struct dentry *dentry, const char *name, int ret)
{
+ if (ret)
+ return ret;
return shirudo_bpffs_guard(BPF_CORE_READ(dentry, d_inode, i_sb, s_dev),
SHIRUDO_TAMPER_PIN_XATTR);
}
@@ -325,8 +374,11 @@ int BPF_PROG(shirudo_lsm_inode_removexattr, struct mnt_idmap *idmap,
* hooks are self-protecting: their own pins live in the bpffs they keep
* mounted.
*
- * A lazy unmount of a parent such as /run can still detach this bpffs
- * without calling sb_umount for it.
+ * A lazy unmount (MNT_DETACH) of a mount above the bpffs, /run or the
+ * root, takes the subtree down with it and fires sb_umount for the
+ * ancestor alone. The original mount must stay; namespace copies may
+ * go when none of the removed mounts can propagate the unmount out.
+ * switch-root moves /run and retains the original mount's unique ID.
*/
static __always_inline __u32 shirudo_bpffs_dev(void)
{
@@ -348,44 +400,216 @@ shirudo_deny_bpffs_mount(__u32 sb_dev, enum shirudo_tamper_point point)
return shirudo_tamper_deny(point, -EPERM);
}
+static __always_inline int shirudo_parent_mount(struct super_block *sb,
+ bool trusted_target)
+{
+ struct shirudo_sb_id *id = shirudo_policy();
+
+ if (!sb || !id || !id->runtime_mode || !id->run_dev ||
+ BPF_CORE_READ(sb, s_dev) != id->run_dev)
+ return 0;
+ return trusted_target && shirudo_task_is_self(id) ? 0 : -EPERM;
+}
+
+static __always_inline int shirudo_image_mount(struct super_block *sb,
+ bool replace)
+{
+ struct shirudo_sb_id *id = shirudo_policy();
+ struct shirudo_device *device;
+ struct block_device *bdev;
+
+ if (!sb || !id || !id->runtime_mode)
+ return 0;
+ bdev = BPF_CORE_READ(sb, s_bdev);
+ if (!bdev)
+ return 0;
+ device = shirudo_bdev_device(bdev);
+ if (!device)
+ return 0;
+ if (replace && device->kind == SHIRUDO_DEVICE_CONFIG &&
+ shirudo_task_is_self(id))
+ return 0;
+ return -EPERM;
+}
+
SEC("lsm/sb_umount")
-int BPF_PROG(shirudo_lsm_sb_umount, struct vfsmount *mnt, int flags)
+int BPF_PROG(shirudo_lsm_sb_umount, struct vfsmount *mnt, int flags,
+ int ret)
{
- return shirudo_deny_bpffs_mount(BPF_CORE_READ(mnt, mnt_sb, s_dev),
- SHIRUDO_TAMPER_BPFFS_UMOUNT);
+ struct super_block *sb = BPF_CORE_READ(mnt, mnt_sb);
+ __u32 dev;
+
+ if (ret)
+ return ret;
+ ret = shirudo_parent_mount(sb, false);
+ if (ret)
+ return ret;
+ if (BPF_CORE_READ_INTO(&dev, sb, s_dev))
+ return shirudo_tamper_deny(SHIRUDO_TAMPER_BPFFS_UMOUNT,
+ -EPERM);
+ ret = shirudo_deny_bpffs_mount(dev, SHIRUDO_TAMPER_BPFFS_UMOUNT);
+ return ret ? ret : shirudo_image_mount(sb, true);
}
SEC("lsm/move_mount")
int BPF_PROG(shirudo_lsm_move_mount, const struct path *from_path,
- const struct path *to_path)
+ const struct path *to_path, int ret)
{
- __u32 from = BPF_CORE_READ(from_path, mnt, mnt_sb, s_dev);
- __u32 to = BPF_CORE_READ(to_path, mnt, mnt_sb, s_dev);
+ struct super_block *from = BPF_CORE_READ(from_path, mnt, mnt_sb);
+ struct super_block *to = BPF_CORE_READ(to_path, mnt, mnt_sb);
- if (shirudo_deny_bpffs_mount(from, SHIRUDO_TAMPER_BPFFS_MOUNT))
+ if (ret)
+ return ret;
+ if (shirudo_parent_mount(from, false) ||
+ shirudo_parent_mount(to, true))
+ return -EPERM;
+ if (shirudo_deny_bpffs_mount(BPF_CORE_READ(from, s_dev),
+ SHIRUDO_TAMPER_BPFFS_MOUNT) ||
+ shirudo_deny_bpffs_mount(BPF_CORE_READ(to, s_dev),
+ SHIRUDO_TAMPER_BPFFS_MOUNT))
return -EPERM;
- return shirudo_deny_bpffs_mount(to, SHIRUDO_TAMPER_BPFFS_MOUNT);
+ ret = shirudo_image_mount(from, true);
+ return ret ? ret : shirudo_image_mount(to, true);
}
-/*
- * Deny mounting anything whose target path resolves onto shirudo's
- * bpffs -- i.e. mounting over the pin tree to shadow it. init's own
- * mount is unaffected: it runs before this hook attaches, and at that
- * point the mountpoint still resolves to the underlying fs, not the
- * (not-yet-mounted) bpffs.
- */
SEC("lsm/sb_mount")
int BPF_PROG(shirudo_lsm_sb_mount, const char *dev_name,
const struct path *path, const char *type,
- unsigned long flags, void *data)
+ unsigned long flags, void *data, int ret)
{
- return shirudo_deny_bpffs_mount(BPF_CORE_READ(path, mnt, mnt_sb, s_dev),
- SHIRUDO_TAMPER_BPFFS_MOUNT);
+ struct super_block *sb = BPF_CORE_READ(path, mnt, mnt_sb);
+
+ if (ret)
+ return ret;
+ if ((flags & ((1UL << 17) | (1UL << 18) | (1UL << 19) |
+ (1UL << 20))) && shirudo_parent_mount(sb, false))
+ return -EPERM;
+ ret = shirudo_parent_mount(sb, true);
+ if (ret)
+ return ret;
+ ret = shirudo_deny_bpffs_mount(BPF_CORE_READ(sb, s_dev),
+ SHIRUDO_TAMPER_BPFFS_MOUNT);
+ return ret ? ret : shirudo_image_mount(sb, true);
}
SEC("lsm/sb_remount")
-int BPF_PROG(shirudo_lsm_sb_remount, struct super_block *sb, void *mnt_opts)
+int BPF_PROG(shirudo_lsm_sb_remount, struct super_block *sb,
+ void *mnt_opts, int ret)
+{
+ if (ret)
+ return ret;
+ ret = shirudo_parent_mount(sb, false);
+ if (ret)
+ return ret;
+ ret = shirudo_deny_bpffs_mount(BPF_CORE_READ(sb, s_dev),
+ SHIRUDO_TAMPER_BPFFS_MOUNT);
+ return ret ? ret : shirudo_image_mount(sb, false);
+}
+
+struct dm_verity_digest___shirudo {
+ const char *alg;
+ const __u8 *digest;
+ size_t digest_len;
+} __attribute__((preserve_access_index));
+
+SEC("lsm.s/bdev_setintegrity")
+int BPF_PROG(shirudo_lsm_bdev_setintegrity, struct block_device *bdev,
+ enum lsm_integrity_type type, const void *value,
+ size_t size, int ret)
+{
+ const struct dm_verity_digest___shirudo *digest = value;
+ struct shirudo_sb_id *id = shirudo_policy();
+ struct shirudo_device device = {};
+ __u64 cookie = (__u64)bdev;
+ const unsigned char *root;
+ const char *algorithm;
+ unsigned int i;
+ char name[8];
+ int diff;
+
+ if (ret)
+ return ret;
+ if (!id || !id->runtime_mode || type != LSM_INT_DMVERITY_ROOTHASH)
+ return 0;
+ if (!value) {
+ bpf_map_delete_elem(&shirudo_devices, &cookie);
+ return 0;
+ }
+ if (shirudo_bdev_device(bdev))
+ return -EPERM;
+ if (BPF_CORE_READ(digest, digest_len) != sizeof(device.root))
+ return 0;
+ algorithm = BPF_CORE_READ(digest, alg);
+ if (bpf_probe_read_kernel_str(name, sizeof(name), algorithm) != 7 ||
+ name[0] != 's' || name[1] != 'h' || name[2] != 'a' ||
+ name[3] != '2' || name[4] != '5' || name[5] != '6')
+ return 0;
+ root = BPF_CORE_READ(digest, digest);
+ if (bpf_probe_read_kernel(device.root, sizeof(device.root), root))
+ return -EACCES;
+ diff = 0;
+ for (i = 0; i < sizeof(device.root); i++)
+ diff |= device.root[i] ^ id->runtime_root[i];
+ if (!diff)
+ device.kind = SHIRUDO_DEVICE_RUNTIME;
+ else if (shirudo_task_is_self(id))
+ device.kind = SHIRUDO_DEVICE_CONFIG;
+ else
+ return 0;
+ device.dev = BPF_CORE_READ(bdev, bd_dev);
+ device.diskseq = BPF_CORE_READ(bdev, bd_disk, diskseq);
+ if (!device.diskseq)
+ return -EACCES;
+ if (bpf_map_update_elem(&shirudo_devices, &cookie, &device,
+ BPF_ANY))
+ return -ENOMEM;
+ return 0;
+}
+
+SEC("lsm/bdev_free_security")
+void BPF_PROG(shirudo_lsm_bdev_free_security, struct block_device *bdev)
+{
+ __u64 cookie = (__u64)bdev;
+
+ bpf_map_delete_elem(&shirudo_devices, &cookie);
+}
+
+#define SHIRUDO_DM_CONTROL_DEV ((10U << 20) | 236U)
+#define SHIRUDO_DM_IOCTL_TYPE 0xfdU
+
+/* Match the kernel's type/number dispatch. */
+static __always_inline int shirudo_dm_ioctl(struct file *file,
+ unsigned int cmd, int ret)
+{
+ struct shirudo_sb_id *id = shirudo_policy();
+ struct inode *inode;
+
+ if (ret || !id || !id->runtime_mode || shirudo_task_is_self(id))
+ return ret;
+ inode = BPF_CORE_READ(file, f_inode);
+ if ((BPF_CORE_READ(inode, i_mode) & 0170000) != 0020000 ||
+ BPF_CORE_READ(inode, i_rdev) != SHIRUDO_DM_CONTROL_DEV)
+ return 0;
+ if (((cmd >> 8) & 0xff) != SHIRUDO_DM_IOCTL_TYPE)
+ return -EPERM;
+ switch (cmd & 0xff) {
+ case 0: /* DM_VERSION */
+ case 2: /* DM_LIST_DEVICES */
+ case 7: /* DM_DEV_STATUS */
+ case 8: /* DM_DEV_WAIT */
+ case 11: /* DM_TABLE_DEPS */
+ case 12: /* DM_TABLE_STATUS */
+ case 13: /* DM_LIST_VERSIONS */
+ case 17: /* DM_GET_TARGET_VERSION */
+ return 0;
+ default:
+ return -EPERM;
+ }
+}
+
+SEC("lsm/file_ioctl")
+int BPF_PROG(shirudo_lsm_file_ioctl, struct file *file, unsigned int cmd,
+ unsigned long arg, int ret)
{
- return shirudo_deny_bpffs_mount(BPF_CORE_READ(sb, s_dev),
- SHIRUDO_TAMPER_BPFFS_MOUNT);
+ return shirudo_dm_ioctl(file, cmd, ret);
}
diff --git a/bpf/lib/shirudo_identity.h b/bpf/lib/shirudo_identity.h
index 61cfff26..6d70d250 100644
--- a/bpf/lib/shirudo_identity.h
+++ b/bpf/lib/shirudo_identity.h
@@ -1,18 +1,10 @@
/* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
-/*
- * Copyright (c) 2026 Authors of shirudo
- *
- * The identity record shirudo init freezes, shared by the bpf2 and seal
- * objects. Include it after vmlinux.h and the libbpf BPF headers.
- */
+/* Copyright (c) 2026 Authors of shirudo */
#ifndef SHIRUDO_BPF_IDENTITY_H
#define SHIRUDO_BPF_IDENTITY_H
-/*
- * Populated by init before the programs attach. Must match struct
- * shirudo_identity in util/identity.h byte for byte.
- */
+/* Frozen ABI shared with struct shirudo_identity in util/identity.h. */
#define SHIRUDO_IDENTITY_KEYS_MAX 16
struct shirudo_sb_id {
@@ -27,6 +19,9 @@ struct shirudo_sb_id {
__u32 n_keys;
__u32 pad2;
__s32 key_serials[SHIRUDO_IDENTITY_KEYS_MAX];
+ __u32 runtime_mode;
+ __u32 run_dev;
+ __u8 runtime_root[32];
};
struct {
@@ -52,7 +47,7 @@ static __always_inline bool shirudo_file_is_self(const struct shirudo_sb_id *id,
ino == id->alt_exe_ino;
}
-static __always_inline bool shirudo_task_is_self(const struct shirudo_sb_id *id)
+static __always_inline bool shirudo_task_matches_exe(const struct shirudo_sb_id *id)
{
struct task_struct *task = bpf_get_current_task_btf();
diff --git a/bpf/policy.h b/bpf/policy.h
new file mode 100644
index 00000000..56a5918b
--- /dev/null
+++ b/bpf/policy.h
@@ -0,0 +1,121 @@
+/* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
+/* Copyright (c) 2026 Authors of shirudo */
+
+#ifndef SHIRUDO_BPF_POLICY_H
+#define SHIRUDO_BPF_POLICY_H
+
+#include "lib/shirudo_identity.h"
+
+#define SHIRUDO_DEVICE_RUNTIME 1
+#define SHIRUDO_DEVICE_CONFIG 2
+
+#define SHIRUDO_TASK_TRUSTED 1
+#define SHIRUDO_TASK_BOOTSTRAP 2
+
+struct shirudo_task_authority {
+ __u8 trusted;
+ __u8 candidate_valid;
+ __u8 candidate_trusted;
+ __u8 pad;
+ __u32 pad2;
+};
+
+struct shirudo_device {
+ __u32 dev;
+ __u32 kind;
+ __u8 root[32];
+ __u64 diskseq;
+};
+
+struct {
+ __uint(type, BPF_MAP_TYPE_TASK_STORAGE);
+ __uint(map_flags, BPF_F_NO_PREALLOC);
+ __type(key, int);
+ __type(value, struct shirudo_task_authority);
+} shirudo_tasks SEC(".maps");
+
+struct {
+ __uint(type, BPF_MAP_TYPE_HASH);
+ __uint(max_entries, 128);
+ __type(key, __u64);
+ __type(value, struct shirudo_device);
+} shirudo_devices SEC(".maps");
+
+struct inode *bpf_real_data_inode(struct file *file) __ksym;
+
+static __always_inline struct shirudo_sb_id *shirudo_policy(void)
+{
+ __u32 zero = 0;
+
+ return bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
+}
+
+static __always_inline bool shirudo_marked(struct task_struct *task)
+{
+ struct shirudo_task_authority *authority;
+
+ authority = bpf_task_storage_get(&shirudo_tasks, task, NULL, 0);
+ return authority && authority->trusted;
+}
+
+static __always_inline bool shirudo_bootstrap_task(void)
+{
+ struct task_struct *task = bpf_get_current_task_btf();
+ struct shirudo_task_authority *authority;
+
+ authority = bpf_task_storage_get(&shirudo_tasks, task, NULL, 0);
+ return authority && authority->trusted == SHIRUDO_TASK_BOOTSTRAP;
+}
+
+static __always_inline bool shirudo_task_is_self(const struct shirudo_sb_id *id)
+{
+ if (id->runtime_mode)
+ return shirudo_marked(bpf_get_current_task_btf());
+ return shirudo_task_matches_exe(id);
+}
+
+/* Device pointers can be reused even when the free hook did not run. */
+static __always_inline struct shirudo_device *
+shirudo_bdev_device(struct block_device *bdev)
+{
+ struct shirudo_device *device;
+ __u64 cookie = (__u64)bdev;
+
+ if (!bdev)
+ return NULL;
+ device = bpf_map_lookup_elem(&shirudo_devices, &cookie);
+ if (!device || !device->diskseq ||
+ device->dev != BPF_CORE_READ(bdev, bd_dev) ||
+ device->diskseq != BPF_CORE_READ(bdev, bd_disk, diskseq))
+ return NULL;
+ return device;
+}
+
+static __always_inline struct shirudo_device *
+shirudo_inode_device(struct inode *inode)
+{
+ struct block_device *bdev;
+ struct super_block *sb;
+
+ if (!inode)
+ return NULL;
+ sb = BPF_CORE_READ(inode, i_sb);
+ if (!sb || !(BPF_CORE_READ(sb, s_flags) & 1))
+ return NULL;
+ bdev = BPF_CORE_READ(sb, s_bdev);
+ if (!bdev)
+ return NULL;
+ return shirudo_bdev_device(bdev);
+}
+
+static __always_inline bool shirudo_file_runtime(struct file *file)
+{
+ struct shirudo_device *device;
+
+ if (!file)
+ return false;
+ device = shirudo_inode_device(BPF_CORE_READ(file, f_inode));
+ return device && device->kind == SHIRUDO_DEVICE_RUNTIME;
+}
+
+#endif /* SHIRUDO_BPF_POLICY_H */
diff --git a/bpf/seal.c b/bpf/seal.c
index 58c32602..2b2333da 100644
--- a/bpf/seal.c
+++ b/bpf/seal.c
@@ -10,9 +10,12 @@
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>
-#include "shirudo_identity.h"
+#include "policy.h"
#include "shirudo_tamper.h"
+#ifndef ENOMEM
+#define ENOMEM 12
+#endif
#ifndef EPERM
#define EPERM 1
#endif
@@ -47,9 +50,6 @@ char _license[] SEC("license") = "Dual BSD/GPL";
*/
struct seal_scratch {
char target[LABEL_LEN];
- __u8 exec_candidate_valid;
- __u8 exec_candidate_labelled;
- __u8 exe_is_shirudo;
};
struct {
@@ -79,6 +79,7 @@ extern int bpf_inode_init_xattr(struct xattr *xattrs,
const char *name__str,
const struct bpf_dynptr *value_p) __ksym;
+/* The mode selects frozen executable identity or committed task authority. */
static __always_inline int seal_caller(enum shirudo_tamper_point point)
{
struct shirudo_sb_id *id;
@@ -150,28 +151,46 @@ static __always_inline void seal_mark(struct inode *inode, const char *label,
__builtin_memcpy(m->label, label, LABEL_LEN);
}
-/*
- * 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.
- */
+/* Failed exec preserves the committed authority of the still-running image. */
SEC("lsm.s/bprm_check_security")
int BPF_PROG(shirudo_seal_bprm_check_security,
- struct linux_binprm *bprm)
+ struct linux_binprm *bprm, int ret)
{
struct task_struct *task = bpf_get_current_task_btf();
- struct seal_scratch *s = seal_scratch_get(task);
- struct file *file = bprm->file;
+ struct shirudo_sb_id *id = shirudo_policy();
+ struct shirudo_task_authority *authority;
+ struct seal_scratch *scratch;
+ struct inode *real_inode;
struct bpf_dynptr dp;
- int len = -1;
+ bool trusted = false;
+ int len;
- if (!s)
- return 0;
- if (file) {
- bpf_dynptr_from_mem(s->target, LABEL_LEN, 0, &dp);
- len = bpf_get_file_xattr(file, SHIRUDO_XATTR, &dp);
+ if (ret)
+ return ret;
+ if (!id)
+ return -EACCES;
+ if (id->runtime_mode) {
+ real_inode = bpf_real_data_inode(bprm->file);
+ trusted = real_inode == BPF_CORE_READ(bprm, file, f_inode) &&
+ shirudo_file_runtime(bprm->file);
+ } else {
+ scratch = seal_scratch_get(task);
+ if (!scratch)
+ return 0;
+ bpf_dynptr_from_mem(scratch->target, LABEL_LEN, 0, &dp);
+ len = bpf_get_file_xattr(bprm->file, SHIRUDO_XATTR, &dp);
+ trusted = len == LABEL_LEN;
}
- s->exec_candidate_labelled = len == LABEL_LEN;
- s->exec_candidate_valid = 1;
+ if (trusted && id->runtime_mode && BPF_CORE_READ(task, ptrace))
+ return -EACCES;
+ if (trusted && id->runtime_mode && BPF_CORE_READ(task, seccomp.mode))
+ return -EACCES;
+ authority = bpf_task_storage_get(&shirudo_tasks, task, NULL,
+ BPF_LOCAL_STORAGE_GET_F_CREATE);
+ if (!authority)
+ return id->runtime_mode ? -ENOMEM : 0;
+ authority->candidate_trusted = trusted;
+ authority->candidate_valid = 1;
return 0;
}
@@ -180,36 +199,41 @@ void BPF_PROG(shirudo_seal_bprm_committed_creds,
const struct linux_binprm *bprm)
{
struct task_struct *task = bpf_get_current_task_btf();
- struct seal_scratch *s =
- bpf_task_storage_get(&seal_scratch_map, task, NULL, 0);
+ struct shirudo_task_authority *authority;
(void)bprm;
- if (!s || !s->exec_candidate_valid)
+ authority = bpf_task_storage_get(&shirudo_tasks, task, NULL, 0);
+ if (!authority)
return;
- if (!s->exec_candidate_labelled) {
- bpf_task_storage_delete(&seal_scratch_map, task);
- return;
- }
- s->exe_is_shirudo = 1;
- s->exec_candidate_valid = 0;
- s->exec_candidate_labelled = 0;
+ 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)
+ __u64 clone_flags, int ret)
{
struct task_struct *parent = bpf_get_current_task_btf();
- struct seal_scratch *parent_s;
- struct seal_scratch *child_s;
+ struct shirudo_task_authority *parent_authority;
+ struct shirudo_task_authority *authority;
(void)clone_flags;
- parent_s = bpf_task_storage_get(&seal_scratch_map, parent, NULL, 0);
- if (!parent_s || !parent_s->exe_is_shirudo)
+ if (ret)
+ return ret;
+ parent_authority = bpf_task_storage_get(&shirudo_tasks, parent,
+ NULL, 0);
+ if (!parent_authority || !parent_authority->trusted)
return 0;
- child_s = seal_scratch_get(child);
- if (child_s)
- child_s->exe_is_shirudo = 1;
+ authority = bpf_task_storage_get(&shirudo_tasks, child, NULL,
+ BPF_LOCAL_STORAGE_GET_F_CREATE);
+ if (!authority) {
+ struct shirudo_sb_id *id = shirudo_policy();
+
+ return id && id->runtime_mode ? -ENOMEM : 0;
+ }
+ authority->trusted = parent_authority->trusted;
return 0;
}
@@ -218,7 +242,6 @@ int BPF_PROG(shirudo_seal_perf_event_alloc,
struct perf_event *event, int ret)
{
struct task_struct *target;
- struct seal_scratch *s;
if (ret)
return ret;
@@ -227,8 +250,7 @@ int BPF_PROG(shirudo_seal_perf_event_alloc,
if (!target)
return 0;
- s = bpf_task_storage_get(&seal_scratch_map, target, NULL, 0);
- if (s && s->exe_is_shirudo)
+ if (shirudo_marked(target))
return shirudo_tamper_deny(SHIRUDO_TAMPER_SEAL_PERF, -EACCES);
return 0;
@@ -238,27 +260,26 @@ SEC("lsm/ptrace_access_check")
int BPF_PROG(shirudo_seal_ptrace_access_check,
struct task_struct *child, unsigned int mode, int ret)
{
- struct seal_scratch *s;
-
(void)mode;
if (ret)
return ret;
- s = bpf_task_storage_get(&seal_scratch_map, child, NULL, 0);
- if (s && s->exe_is_shirudo)
+ if (shirudo_marked(child))
return shirudo_tamper_deny(SHIRUDO_TAMPER_SEAL_PTRACE, -EACCES);
return 0;
}
SEC("lsm.s/file_open")
-int BPF_PROG(shirudo_seal_file_open, struct file *file)
+int BPF_PROG(shirudo_seal_file_open, struct file *file, int ret)
{
struct inode *inode = file->f_inode;
struct task_struct *task;
struct seal_scratch *s;
struct bpf_dynptr tdp;
+ if (ret)
+ return ret;
task = bpf_get_current_task_btf();
if (seal_is_dir(inode)) {
/* never opened for writing; mark it */
@@ -359,9 +380,9 @@ static __always_inline int seal_dentry(struct dentry *dentry,
SEC("lsm.s/inode_unlink")
int BPF_PROG(shirudo_seal_inode_unlink, struct inode *dir,
- struct dentry *dentry)
+ struct dentry *dentry, int ret)
{
- return seal_dentry(dentry, SHIRUDO_TAMPER_SEAL_UNLINK);
+ return ret ? ret : seal_dentry(dentry, SHIRUDO_TAMPER_SEAL_UNLINK);
}
static __always_inline bool seal_is_exe(struct inode *inode)
@@ -370,7 +391,8 @@ static __always_inline bool seal_is_exe(struct inode *inode)
__u32 zero = 0;
id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);
- return id && BPF_CORE_READ(inode, i_sb, s_dev) == id->exe_dev &&
+ return id && !id->runtime_mode &&
+ BPF_CORE_READ(inode, i_sb, s_dev) == id->exe_dev &&
BPF_CORE_READ(inode, i_ino) == id->exe_ino;
}
@@ -381,11 +403,13 @@ static __always_inline bool seal_is_exe(struct inode *inode)
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)
+ struct dentry *new_dentry, int ret)
{
struct inode *target = BPF_CORE_READ(new_dentry, d_inode);
int err;
+ if (ret)
+ return ret;
err = seal_dentry(old_dentry, SHIRUDO_TAMPER_SEAL_RENAME);
if (err)
return err;
@@ -396,16 +420,18 @@ int BPF_PROG(shirudo_seal_inode_rename, struct inode *old_dir,
SEC("lsm.s/inode_setattr")
int BPF_PROG(shirudo_seal_inode_setattr, struct mnt_idmap *idmap,
- struct dentry *dentry, struct iattr *attr)
+ struct dentry *dentry, struct iattr *attr, int ret)
{
- return seal_dentry(dentry, SHIRUDO_TAMPER_SEAL_SETATTR);
+ return ret ? ret : seal_dentry(dentry, SHIRUDO_TAMPER_SEAL_SETATTR);
}
SEC("lsm.s/inode_setxattr")
int BPF_PROG(shirudo_seal_inode_setxattr, struct mnt_idmap *idmap,
struct dentry *dentry, const char *name, const void *value,
- __u64 size, int flags)
+ __u64 size, int flags, int ret)
{
+ if (ret)
+ return ret;
if (seal_is_label(name))
return seal_caller(SHIRUDO_TAMPER_SEAL_LABEL);
return seal_dentry(dentry, SHIRUDO_TAMPER_SEAL_XATTR);
@@ -413,7 +439,53 @@ int BPF_PROG(shirudo_seal_inode_setxattr, struct mnt_idmap *idmap,
SEC("lsm.s/inode_removexattr")
int BPF_PROG(shirudo_seal_inode_removexattr, struct mnt_idmap *idmap,
- struct dentry *dentry, const char *name)
+ struct dentry *dentry, const char *name, int ret)
{
- return seal_dentry(dentry, SHIRUDO_TAMPER_SEAL_XATTR);
+ return ret ? ret : seal_dentry(dentry, SHIRUDO_TAMPER_SEAL_XATTR);
+}
+
+#define SHIRUDO_PROT_WRITE 2
+#define SHIRUDO_PROT_EXEC 4
+#define SHIRUDO_VM_EXEC 4
+
+/* Executable mappings in a trusted process must come from its runtime image. */
+SEC("lsm/mmap_file")
+int BPF_PROG(shirudo_seal_mmap_file, struct file *file,
+ unsigned long reqprot, unsigned long prot,
+ unsigned long flags, int ret)
+{
+ struct shirudo_sb_id *id = shirudo_policy();
+
+ if (ret)
+ return ret;
+ if (!id || !id->runtime_mode || !shirudo_task_is_self(id) ||
+ shirudo_bootstrap_task() || !(prot & SHIRUDO_PROT_EXEC))
+ return 0;
+ if ((prot & SHIRUDO_PROT_WRITE) || !shirudo_file_runtime(file))
+ return -EACCES;
+ return 0;
+}
+
+/* Disallow executable promotion of pages that could have been modified. */
+SEC("lsm/file_mprotect")
+int BPF_PROG(shirudo_seal_file_mprotect, struct vm_area_struct *vma,
+ unsigned long reqprot, unsigned long prot, int ret)
+{
+ struct shirudo_sb_id *id = shirudo_policy();
+ unsigned long old_flags;
+
+ if (ret)
+ return ret;
+ old_flags = BPF_CORE_READ(vma, vm_flags);
+ if (!id || !id->runtime_mode || !shirudo_task_is_self(id) ||
+ shirudo_bootstrap_task())
+ return 0;
+ if ((old_flags & SHIRUDO_VM_EXEC) && (prot & SHIRUDO_PROT_WRITE))
+ return -EACCES;
+ if (!(prot & SHIRUDO_PROT_EXEC))
+ return 0;
+ if (!(old_flags & SHIRUDO_VM_EXEC) ||
+ !shirudo_file_runtime(BPF_CORE_READ(vma, vm_file)))
+ return -EACCES;
+ return 0;
}
diff --git a/include/shirudo_mode.h b/include/shirudo_mode.h
new file mode 100644
index 00000000..03efc610
--- /dev/null
+++ b/include/shirudo_mode.h
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
+/* Copyright (c) 2026 Authors of shirudo */
+
+#ifndef SHIRUDO_MODE_H
+#define SHIRUDO_MODE_H
+
+#define SHIRUDO_PR_GET_MODE 0x53520001
+#define SHIRUDO_PR_MODE_LEGACY_ERR 1001
+#define SHIRUDO_PR_MODE_IMAGE_ERR 1002
+
+#define SHIRUDO_MODE_NONE 0
+#define SHIRUDO_MODE_LEGACY 1
+#define SHIRUDO_MODE_IMAGE 2
+
+#endif /* SHIRUDO_MODE_H */
diff --git a/util/identity.c b/util/identity.c
index b3b2ebe0..ba748072 100644
--- a/util/identity.c
+++ b/util/identity.c
@@ -7,6 +7,7 @@
#include <errno.h>
#include <string.h>
+#include <sys/prctl.h>
#include <unistd.h>
#include <linux/bpf.h>
@@ -16,8 +17,22 @@
#include <shirudo/misc.h>
#include "identity.h"
+#include "shirudo_mode.h"
#include "paths.h"
+int shirudo_identity_mode(void)
+{
+ int mode = prctl(SHIRUDO_PR_GET_MODE, 0, 0, 0, 0);
+
+ if (mode >= 0)
+ return -EPROTO;
+ if (errno == SHIRUDO_PR_MODE_LEGACY_ERR)
+ return SHIRUDO_MODE_LEGACY;
+ if (errno == SHIRUDO_PR_MODE_IMAGE_ERR)
+ return SHIRUDO_MODE_IMAGE;
+ return errno == EINVAL || errno == ENOSYS ? SHIRUDO_MODE_NONE : -errno;
+}
+
int identity_read(struct shirudo_identity *out)
{
struct bpf_map_info info = {};
diff --git a/util/identity.h b/util/identity.h
index cf0fe4f9..4fe86a55 100644
--- a/util/identity.h
+++ b/util/identity.h
@@ -17,31 +17,57 @@
/* The most keys the anchor keyring holds; util/keys.h bounds the set. */
#define SHIRUDO_IDENTITY_KEYS_MAX 16
-/*
- * Must match struct shirudo_sb_id in bpf/lib/shirudo_identity.h byte for
- * byte. exe is the binary that keeps running the target; alt_exe, when
- * set, the initrd's copy that ran init and is gone after the switch to
- * the root. key_serials are the anchor keyring's keys, which
- * lsm/key_permission guards with the keyring itself.
- */
+/* The frozen ABI shared with bpf/lib/shirudo_identity.h. */
struct shirudo_identity {
- __u32 sb_dev;
- __u32 exe_dev;
- __u64 exe_ino;
+ __u32 sb_dev;
+ __u32 exe_dev;
+ __u64 exe_ino;
__s32 keyring_serial;
- __u32 keyring_type;
- __u32 alt_exe_dev;
- __u32 pad;
- __u64 alt_exe_ino;
- __u32 n_keys;
- __u32 pad2;
+ __u32 keyring_type;
+ __u32 alt_exe_dev;
+ __u32 pad;
+ __u64 alt_exe_ino;
+ __u32 n_keys;
+ __u32 pad2;
__s32 key_serials[SHIRUDO_IDENTITY_KEYS_MAX];
+ __u32 runtime_mode;
+ __u32 run_dev;
+ __u8 runtime_root[32];
};
+#define SHIRUDO_TASK_TRUSTED 1
+#define SHIRUDO_TASK_BOOTSTRAP 2
+
+#define SHIRUDO_DEVICE_RUNTIME 1
+#define SHIRUDO_DEVICE_CONFIG 2
+
+struct shirudo_device_authority {
+ __u32 dev;
+ __u32 kind;
+ __u8 root[32];
+ __u64 diskseq;
+};
+
+struct shirudo_task_authority {
+ __u8 trusted;
+ __u8 candidate_valid;
+ __u8 candidate_trusted;
+ __u8 pad;
+ __u32 pad2;
+};
+
+_Static_assert(sizeof(struct shirudo_device_authority) == 48,
+ "device authority ABI");
+_Static_assert(sizeof(struct shirudo_task_authority) == 8,
+ "task authority ABI");
+_Static_assert(sizeof(struct shirudo_identity) == 152,
+ "frozen identity ABI");
+
/*
* The identity map init populated, froze and pinned this boot. -ENOENT
* when init has not run, another -errno when the pin cannot be read.
*/
+int shirudo_identity_mode(void);
int identity_read(struct shirudo_identity *out);
#endif /* SHIRUDO_UTIL_IDENTITY_H */
diff --git a/util/image_guards.c b/util/image_guards.c
new file mode 100644
index 00000000..8182c98e
--- /dev/null
+++ b/util/image_guards.c
@@ -0,0 +1,339 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+#include <shirudo/misc.h>
+
+#include <errno.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <linux/bpf.h>
+#include <linux/btf.h>
+#include <bpf/bpf.h>
+#include <bpf/btf.h>
+#include <bpf/libbpf.h>
+
+#include "identity.h"
+#include "image_guards.h"
+#include "log.h"
+#include "macros.h"
+#include "paths.h"
+#include "tamper.h"
+
+const struct shirudo_image_guard
+shirudo_seal_guards[SHIRUDO_SEAL_HOOK_COUNT] = {
+ { "shirudo_seal_bprm_check_security",
+ SHIRUDO_SEAL_BPRM_CHECK_PIN },
+ { "shirudo_seal_bprm_committed_creds",
+ SHIRUDO_SEAL_BPRM_COMMITTED_PIN },
+ { "shirudo_seal_task_alloc", SHIRUDO_SEAL_TASK_ALLOC_PIN },
+ { "shirudo_seal_file_open", SHIRUDO_SEAL_FILE_OPEN_PIN },
+ { "shirudo_seal_inode_unlink", SHIRUDO_SEAL_UNLINK_PIN },
+ { "shirudo_seal_inode_rename", SHIRUDO_SEAL_RENAME_PIN },
+ { "shirudo_seal_inode_setattr", SHIRUDO_SEAL_SETATTR_PIN },
+ { "shirudo_seal_inode_setxattr", SHIRUDO_SEAL_SETXATTR_PIN },
+ { "shirudo_seal_inode_removexattr", SHIRUDO_SEAL_RMXATTR_PIN },
+ { "shirudo_seal_inode_post_setxattr",
+ SHIRUDO_SEAL_POST_SETXATTR_PIN },
+ { "shirudo_seal_inode_post_removexattr",
+ SHIRUDO_SEAL_POST_RMXATTR_PIN },
+ { "shirudo_seal_inode_init_security",
+ SHIRUDO_SEAL_INIT_SECURITY_PIN },
+ { "shirudo_seal_ptrace_access_check",
+ SHIRUDO_SEAL_PTRACE_PIN },
+ { "shirudo_seal_perf_event_alloc",
+ SHIRUDO_SEAL_PERF_PIN },
+ { "shirudo_seal_mmap_file",
+ SHIRUDO_BPFFS_DIR "/seal_mmap_link" },
+ { "shirudo_seal_file_mprotect",
+ SHIRUDO_BPFFS_DIR "/seal_mprotect_link" },
+};
+
+const struct shirudo_image_guard
+shirudo_mount_guards[SHIRUDO_MOUNT_HOOK_COUNT] = {
+ { "shirudo_lsm_sb_umount", SHIRUDO_SB_UMOUNT_LINK_PIN },
+ { "shirudo_lsm_move_mount", SHIRUDO_MOVE_MOUNT_LINK_PIN },
+ { "shirudo_lsm_sb_mount", SHIRUDO_SB_MOUNT_LINK_PIN },
+ { "shirudo_lsm_sb_remount", SHIRUDO_SB_REMOUNT_LINK_PIN },
+};
+
+const struct shirudo_image_guard
+shirudo_observer_guards[SHIRUDO_OBSERVER_HOOK_COUNT] = {
+ { "shirudo_lsm_file_ioctl", SHIRUDO_BPFFS_DIR "/file_ioctl_link" },
+ { "shirudo_lsm_task_prctl", SHIRUDO_TASK_PRCTL_LINK_PIN },
+ { "shirudo_lsm_bdev_setintegrity",
+ SHIRUDO_BPFFS_DIR "/bdev_integrity_link" },
+ { "shirudo_lsm_bdev_free_security",
+ SHIRUDO_BPFFS_DIR "/bdev_free_link" },
+ { "shirudo_lsm_inode_rmdir",
+ SHIRUDO_BPFFS_DIR "/inode_rmdir_link" },
+ { "shirudo_lsm_inode_rename",
+ SHIRUDO_INODE_RENAME_LINK_PIN },
+};
+
+const struct shirudo_image_guard
+shirudo_gate_guards[SHIRUDO_GATE_HOOK_COUNT] = {
+ [SHIRUDO_GUARD_UNLINK] = {
+ "shirudo_lsm_inode_unlink", SHIRUDO_INODE_UNLINK_LINK_PIN,
+ },
+ [SHIRUDO_GUARD_SETXATTR] = {
+ "shirudo_lsm_inode_setxattr", SHIRUDO_INODE_SETXATTR_LINK_PIN,
+ },
+ [SHIRUDO_GUARD_REMOVEXATTR] = {
+ "shirudo_lsm_inode_removexattr", SHIRUDO_INODE_RMXATTR_LINK_PIN,
+ },
+ [SHIRUDO_GUARD_KEY_PERMISSION] = {
+ "shirudo_lsm_key_permission", SHIRUDO_KEY_PERMISSION_LINK_PIN,
+ },
+ [SHIRUDO_GUARD_BPF_MAP] = {
+ "shirudo_lsm_bpf_map", SHIRUDO_LSM_BPF_MAP_LINK_PIN,
+ },
+ [SHIRUDO_GUARD_PROG_LOAD] = {
+ "shirudo_lsm_bpf_prog_load", SHIRUDO_LSM_PROG_LOAD_LINK_PIN,
+ },
+ [SHIRUDO_GUARD_BPF] = {
+ "shirudo_lsm_bpf", SHIRUDO_LSM_LINK_PIN,
+ },
+};
+
+const struct shirudo_guard_map shirudo_guard_maps[SHIRUDO_GUARD_MAP_COUNT] = {
+ [SHIRUDO_GUARD_IDENTITY] = {
+ "shirudo_bpffs_sb", SHIRUDO_IDENTITY_PIN,
+ BPF_MAP_TYPE_ARRAY, sizeof(__u32),
+ sizeof(struct shirudo_identity), 1, 0,
+ },
+ [SHIRUDO_GUARD_TASKS] = {
+ "shirudo_tasks", SHIRUDO_BPFFS_DIR "/task_authority",
+ BPF_MAP_TYPE_TASK_STORAGE, sizeof(__u32),
+ sizeof(struct shirudo_task_authority), 0, BPF_F_NO_PREALLOC,
+ },
+ [SHIRUDO_GUARD_DEVICES] = {
+ "shirudo_devices", SHIRUDO_BPFFS_DIR "/devices",
+ BPF_MAP_TYPE_HASH, sizeof(__u64),
+ sizeof(struct shirudo_device_authority), 128, 0,
+ },
+ [SHIRUDO_GUARD_TAMPER] = {
+ "shirudo_tamper", SHIRUDO_TAMPER_PIN,
+ BPF_MAP_TYPE_ARRAY, sizeof(__u32),
+ sizeof(__u64), SHIRUDO_TAMPER_MAX, 0,
+ },
+};
+
+static int guard_map_ids(__u32 ids[SHIRUDO_GUARD_MAP_COUNT])
+{
+ struct bpf_map_info info;
+ __u32 length;
+ __u32 i;
+ int fd, rc;
+
+ for (i = 0; i < SHIRUDO_GUARD_MAP_COUNT; i++) {
+ const struct shirudo_guard_map *expected =
+ &shirudo_guard_maps[i];
+
+ fd = bpf_obj_get(expected->pin);
+ if (fd < 0)
+ return -errno;
+ memset(&info, 0, sizeof(info));
+ length = sizeof(info);
+ rc = bpf_map_get_info_by_fd(fd, &info, &length);
+ if (rc)
+ rc = -errno;
+ else if (!info.id || info.type != expected->type ||
+ info.key_size != expected->key_size ||
+ info.value_size != expected->value_size ||
+ info.max_entries != expected->max_entries ||
+ info.map_flags != expected->flags ||
+ info.name[sizeof(info.name) - 1] ||
+ strncmp(info.name, expected->name,
+ sizeof(info.name) - 1))
+ rc = -EPROTO;
+ close(fd);
+ if (rc) {
+ pr_err("image: invalid guard map %s: %s", expected->pin,
+ strerror(-rc));
+ return rc;
+ }
+ ids[i] = info.id;
+ }
+ return 0;
+}
+
+static int guard_program_maps(const struct bpf_prog_info *program,
+ const __u32 *map_ids,
+ const __u32 expected[SHIRUDO_GUARD_MAP_COUNT])
+{
+ struct bpf_map_info info;
+ __u32 i, j;
+ __u32 length;
+ int fd, rc;
+
+ for (i = 0; i < program->nr_map_ids; i++) {
+ fd = bpf_map_get_fd_by_id(map_ids[i]);
+ if (fd < 0)
+ return -errno;
+ memset(&info, 0, sizeof(info));
+ length = sizeof(info);
+ rc = bpf_map_get_info_by_fd(fd, &info, &length);
+ if (rc)
+ rc = -errno;
+ close(fd);
+ if (rc)
+ return rc;
+ for (j = 0; j < SHIRUDO_GUARD_MAP_COUNT; j++) {
+ if (strncmp(info.name, shirudo_guard_maps[j].name,
+ sizeof(info.name) - 1))
+ continue;
+ if (info.id != expected[j])
+ return -EPROTO;
+ }
+ }
+ return 0;
+}
+
+static int guard_kernel_btf(__u32 id, struct btf **result)
+{
+ struct bpf_btf_info info = {};
+ __u32 length = sizeof(info);
+ char name[16] = {};
+ struct btf *btf;
+ int fd, rc;
+
+ fd = bpf_btf_get_fd_by_id(id);
+ if (fd < 0)
+ return -errno;
+ info.name = (uintptr_t)name;
+ info.name_len = sizeof(name);
+ rc = bpf_btf_get_info_by_fd(fd, &info, &length);
+ if (rc)
+ rc = -errno;
+ else if (!info.kernel_btf ||
+ !memchr(name, '\0', sizeof(name)) ||
+ strcmp(name, "vmlinux"))
+ rc = -EPROTO;
+ close(fd);
+ if (rc)
+ return rc;
+ btf = btf__load_from_kernel_by_id(id);
+ rc = libbpf_get_error(btf);
+ if (rc)
+ return rc;
+ *result = btf;
+ return 0;
+}
+
+static int guard_link(const struct shirudo_image_guard *guard,
+ const __u32 maps[SHIRUDO_GUARD_MAP_COUNT],
+ struct btf **kernel, __u32 *kernel_id)
+{
+ struct bpf_link_info link = {};
+ struct bpf_prog_info program = {};
+ __u32 map_ids[16] = {};
+ char function[96];
+ __u32 length;
+ const char *suffix;
+ int fd, prog, rc, type_id;
+
+ fd = bpf_obj_get(guard->pin);
+ if (fd < 0)
+ return -errno;
+ length = sizeof(link);
+ rc = bpf_link_get_info_by_fd(fd, &link, &length);
+ if (rc)
+ rc = -errno;
+ else if (link.type != BPF_LINK_TYPE_TRACING ||
+ !link.id || !link.prog_id ||
+ link.tracing.attach_type != BPF_LSM_MAC)
+ rc = -EPROTO;
+ close(fd);
+ if (rc)
+ return rc;
+ prog = bpf_prog_get_fd_by_id(link.prog_id);
+ if (prog < 0)
+ return -errno;
+ program.map_ids = (uintptr_t)map_ids;
+ program.nr_map_ids = ARRAY_SIZE(map_ids);
+ length = sizeof(program);
+ rc = bpf_prog_get_info_by_fd(prog, &program, &length);
+ if (rc)
+ rc = -errno;
+ close(prog);
+ if (rc)
+ return rc;
+ if (program.type != BPF_PROG_TYPE_LSM || !program.attach_btf_id ||
+ !program.attach_btf_obj_id || program.id != link.prog_id ||
+ program.nr_map_ids > ARRAY_SIZE(map_ids) ||
+ strncmp(program.name, guard->prog, sizeof(program.name) - 1) ||
+ link.tracing.target_obj_id != program.attach_btf_obj_id ||
+ link.tracing.target_btf_id != program.attach_btf_id)
+ return -EPROTO;
+ if (!*kernel) {
+ rc = guard_kernel_btf(program.attach_btf_obj_id, kernel);
+ if (rc)
+ return rc;
+ *kernel_id = program.attach_btf_obj_id;
+ }
+ if (*kernel_id != program.attach_btf_obj_id)
+ return -EPROTO;
+ suffix = !strncmp(guard->prog, "shirudo_lsm_", 12) ?
+ guard->prog + 12 : guard->prog + 13;
+ if (snprintf(function, sizeof(function), "bpf_lsm_%s", suffix) >=
+ (int)sizeof(function))
+ return -ENAMETOOLONG;
+ type_id = btf__find_by_name_kind(*kernel, function, BTF_KIND_FUNC);
+ if (type_id <= 0 || (__u32)type_id != program.attach_btf_id)
+ return -EPROTO;
+ return guard_program_maps(&program, map_ids, maps);
+}
+
+/* Authenticated initrd bootstrap precedes every hostile task. */
+int shirudo_image_guards_ready(void)
+{
+ const struct {
+ const struct shirudo_image_guard *guards;
+ size_t count;
+ } groups[] = {
+ {
+ .guards = shirudo_gate_guards,
+ .count = ARRAY_SIZE(shirudo_gate_guards),
+ },
+ {
+ .guards = shirudo_observer_guards,
+ .count = ARRAY_SIZE(shirudo_observer_guards),
+ },
+ {
+ .guards = shirudo_mount_guards,
+ .count = ARRAY_SIZE(shirudo_mount_guards),
+ },
+ {
+ .guards = shirudo_seal_guards,
+ .count = ARRAY_SIZE(shirudo_seal_guards),
+ },
+ };
+ __u32 map_ids[SHIRUDO_GUARD_MAP_COUNT] = {};
+ struct btf *kernel = NULL;
+ __u32 kernel_id = 0;
+ __u32 i, j;
+ int rc;
+
+ rc = guard_map_ids(map_ids);
+ if (rc)
+ return rc;
+ for (i = 0; i < ARRAY_SIZE(groups); i++) {
+ for (j = 0; j < groups[i].count; j++) {
+ const struct shirudo_image_guard *guard =
+ &groups[i].guards[j];
+
+ rc = guard_link(guard, map_ids, &kernel, &kernel_id);
+ if (rc) {
+ pr_err("image: missing or invalid guard %s: %s",
+ guard->pin, strerror(-rc));
+ goto out;
+ }
+ }
+ }
+out:
+ btf__free(kernel);
+ return rc;
+}
diff --git a/util/image_guards.h b/util/image_guards.h
new file mode 100644
index 00000000..17e8d550
--- /dev/null
+++ b/util/image_guards.h
@@ -0,0 +1,62 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+/* Copyright (c) 2026 Authors of shirudo */
+
+#ifndef SHIRUDO_IMAGE_GUARDS_H
+#define SHIRUDO_IMAGE_GUARDS_H
+
+#include <shirudo/misc.h>
+
+#include <stdint.h>
+
+#define SHIRUDO_SEAL_HOOK_COUNT 16
+#define SHIRUDO_MOUNT_HOOK_COUNT 4
+#define SHIRUDO_OBSERVER_HOOK_COUNT 6
+
+struct shirudo_image_guard {
+ const char *prog;
+ const char *pin;
+};
+
+enum shirudo_gate_guard {
+ SHIRUDO_GUARD_UNLINK,
+ SHIRUDO_GUARD_SETXATTR,
+ SHIRUDO_GUARD_REMOVEXATTR,
+ SHIRUDO_GUARD_KEY_PERMISSION,
+ SHIRUDO_GUARD_BPF_MAP,
+ SHIRUDO_GUARD_PROG_LOAD,
+ SHIRUDO_GUARD_BPF,
+ SHIRUDO_GATE_HOOK_COUNT,
+};
+
+enum shirudo_guard_map_index {
+ SHIRUDO_GUARD_IDENTITY,
+ SHIRUDO_GUARD_TASKS,
+ SHIRUDO_GUARD_DEVICES,
+ SHIRUDO_GUARD_TAMPER,
+ SHIRUDO_GUARD_MAP_COUNT,
+};
+
+struct shirudo_guard_map {
+ const char *name;
+ const char *pin;
+ __u32 type;
+ __u32 key_size;
+ __u32 value_size;
+ __u32 max_entries;
+ __u32 flags;
+};
+
+extern const struct shirudo_image_guard
+ shirudo_seal_guards[SHIRUDO_SEAL_HOOK_COUNT];
+extern const struct shirudo_image_guard
+ shirudo_mount_guards[SHIRUDO_MOUNT_HOOK_COUNT];
+extern const struct shirudo_image_guard
+ shirudo_observer_guards[SHIRUDO_OBSERVER_HOOK_COUNT];
+extern const struct shirudo_image_guard
+ shirudo_gate_guards[SHIRUDO_GATE_HOOK_COUNT];
+extern const struct shirudo_guard_map
+ shirudo_guard_maps[SHIRUDO_GUARD_MAP_COUNT];
+
+int shirudo_image_guards_ready(void);
+
+#endif /* SHIRUDO_IMAGE_GUARDS_H */
diff --git a/util/paths.h b/util/paths.h
index 9115ef81..4e59ab50 100644
--- a/util/paths.h
+++ b/util/paths.h
@@ -88,6 +88,15 @@
#define SHIRUDO_IDENTITY_PIN SHIRUDO_BPFFS_DIR "/identity"
#define SHIRUDO_TAMPER_PIN SHIRUDO_BPFFS_DIR "/tamper"
+#define SHIRUDO_RUNTIME_DIR SHIRUDO_RUN_DIR "/runtime"
+#define SHIRUDO_CONFIG_IMAGE_DIR SHIRUDO_RUN_DIR "/config"
+#define SHIRUDO_CONFIG_CANDIDATE_DIR SHIRUDO_RUN_DIR "/config-candidate"
+#define SHIRUDO_CONFIG_UPDATE_DIR SHIRUDO_VAR_LIB "/config-update"
+#define SHIRUDO_IMAGE_CACHE_DIR SHIRUDO_VAR_LIB "/images"
+#define SHIRUDO_RUNTIME_CONF_PATH \
+ SHIRUDO_RUNTIME_DIR "/etc/shirudo/shirudo.conf"
+#define SHIRUDO_RUNTIME_CERT_PATH SHIRUDO_RUNTIME_DIR "/keys/cert.der"
+
/* Operator-side. */
#define SHIRUDO_SIGNING_KEY_FILE "signing_key.pem"
#define SHIRUDO_SIGNING_KEY_PATH SHIRUDO_KEYS_DIR "/" SHIRUDO_SIGNING_KEY_FILE