eBPF-based security platform
init: require dm-verity for target enforcement
KP Singh28530b8bacf2
diff --git a/Makefile.shirudo b/Makefile.shirudoindex 63a5d9d5..19ae0f32 100644--- a/Makefile.shirudo+++ b/Makefile.shirudo@@ -256,9 +256,7 @@ BPF_LIB_HEADERS := bpf/lib/shirudo_misc.h \# .incbin into util/loader.o. bpf/lib/ is include-only.BPF_SRCS := \bpf/bpf2.c \- bpf/bpf2_verity.c \bpf/seal.c \- bpf/seal_verity.c \bpf/unload.c# shirudo-build is operator-only and links util/sign.c (CMS signer)diff --git a/bpf/admission.h b/bpf/admission.hindex 6d169687..5e2c24d1 100644--- a/bpf/admission.h+++ b/bpf/admission.h@@ -5,8 +5,8 @@#define SHIRUDO_BPF_ADMISSION_H/*- * Both modes use the same BPF command allowlist. The selected policy authorizes- * the frozen executable in ordinary mode, or a committed task in image mode.+ * 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 shirudo_tamper_point point){@@ -48,7 +48,7 @@ int BPF_PROG(shirudo_lsm_bpf, int cmd, union bpf_attr *attr,/** Vetted mutating cmds the signed-loader data-map + attach/pin* pattern needs from userspace, each left to a caller authorized by- * the selected policy. Signed-loader mutations run in kernel context.+ * 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@@ -189,24 +189,12 @@ int BPF_PROG(shirudo_lsm_bpf_prog_load, struct bpf_prog *prog,if (verdict != BPF_SIG_VERIFIED)return shirudo_tamper_deny(SHIRUDO_TAMPER_LOAD_UNVERIFIED,-EACCES);- if (ktype != id->keyring_type)+ if (ktype != BPF_SIG_KEYRING_BPF)return shirudo_tamper_deny(SHIRUDO_TAMPER_LOAD_KEYRING_KIND,-EACCES);- /*- * The bpf keyring is the kernel's own singleton, resolved by the- * kernel and recorded without a serial (0) because it is held- * without a reference, so the kind alone names it. A staged user- * keyring is named by the serial init froze, which a same-named- * substitute cannot match.- */- if (ktype == BPF_SIG_KEYRING_BPF) {- if (serial && serial != id->keyring_serial)- return shirudo_tamper_deny(SHIRUDO_TAMPER_LOAD_KEYRING,- -EACCES);- } else if (serial != id->keyring_serial) {- return shirudo_tamper_deny(SHIRUDO_TAMPER_LOAD_KEYRING,- -EACCES);- }+ /* The kernel singleton is recorded without a serial on some loads. */+ if (serial && serial != id->keyring_serial)+ return shirudo_tamper_deny(SHIRUDO_TAMPER_LOAD_KEYRING, -EACCES);return 0;}@@ -224,11 +212,11 @@ int BPF_PROG(shirudo_lsm_task_prctl, int option, unsigned long arg2,(void)arg5;if (ret && ret != -ENOSYS)return ret;- if (option == SHIRUDO_PR_GET_MODE) {+ if (option == SHIRUDO_PR_GET_ENFORCEMENT) {id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);if (!id)return -EACCES;- return -SHIRUDO_MODE_ERROR;+ return -SHIRUDO_PR_ENFORCING_ERR;}if (option != PR_SET_MM ||(arg2 != PR_SET_MM_EXE_FILE && arg2 != PR_SET_MM_MAP))@@ -241,12 +229,7 @@ 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:- * Ordinary mode protects the bpffs; image mode also protects the /run- * filesystem holding its mountpoints. Only an authorized caller may mutate- * these inodes. Count bpffs refusals at `point`. Unrelated filesystems pass.- */+/* 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)@@ -257,7 +240,7 @@ int BPF_PROG(shirudo_lsm_bprm_check_security, struct linux_binprm *bprm,if (ret)return ret;id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);- if (!id || !shirudo_file_is_self(id, bprm->file))+ if (!id || !shirudo_file_runtime(bprm->file))return 0;if (BPF_CORE_READ(bprm, unsafe) & LSM_UNSAFE_PTRACE)return shirudo_tamper_deny(SHIRUDO_TAMPER_EXEC_TRACED, -EPERM);@@ -265,6 +248,10 @@ int BPF_PROG(shirudo_lsm_bprm_check_security, struct linux_binprm *bprm,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 shirudo_tamper_point point){diff --git a/bpf/bpf2.c b/bpf/bpf2.cindex 4bc39b39..b1839369 100644--- a/bpf/bpf2.c+++ b/bpf/bpf2.c@@ -2,41 +2,85 @@/* Copyright (c) 2026 Authors of shirudo *//*- * Ordinary mode records executable device/inode identities at init. The common- * admission checks also protect the bpffs and its payload xattrs. File labels- * and task observation checks live in the matching seal object.+ * Shirudo authorizes tasks marked after exec from the authenticated runtime.+ * These hooks track verity devices, restrict DM controls, and protect the+ * runtime/config mounts alongside the shared bpffs and BPF admission checks.*/-#include "policy.h"+#include "verity_policy.h"#include "admission.h"char _license[] SEC("license") = "Dual BSD/GPL";+static __always_inline int shirudo_parent_mount(struct super_block *sb,+ bool trusted_target)+{+ struct shirudo_sb_id *id = shirudo_policy();++ if (!sb || !id || !id->run_dev ||+ BPF_CORE_READ(sb, s_dev) != id->run_dev)+ return 0;+ return trusted_target && shirudo_task_is_self(id) ? 0 : -EPERM;+}++static __always_inline int shirudo_image_mount(struct super_block *sb,+ bool replace)+{+ struct shirudo_sb_id *id = shirudo_policy();+ struct shirudo_device *device;+ struct block_device *bdev;++ if (!sb || !id)+ return 0;+ bdev = BPF_CORE_READ(sb, s_bdev);+ if (!bdev)+ return 0;+ device = shirudo_bdev_device(bdev);+ if (!device)+ return 0;+ if (replace && device->kind == SHIRUDO_DEVICE_CONFIG &&+ shirudo_task_is_self(id))+ return 0;+ return -EPERM;+}+SEC("lsm/sb_umount")int BPF_PROG(shirudo_lsm_sb_umount, struct vfsmount *mnt, int flags,int ret){+ struct super_block *sb = BPF_CORE_READ(mnt, mnt_sb);__u32 dev;if (ret)return ret;- if (BPF_CORE_READ_INTO(&dev, mnt, mnt_sb, s_dev))+ 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);- return shirudo_deny_bpffs_mount(dev, SHIRUDO_TAMPER_BPFFS_UMOUNT);+ ret = shirudo_deny_bpffs_mount(dev, SHIRUDO_TAMPER_BPFFS_UMOUNT);+ return ret ? ret : shirudo_image_mount(sb, true);}SEC("lsm/move_mount")int BPF_PROG(shirudo_lsm_move_mount, const struct path *from_path,const struct path *to_path, int ret){- __u32 from_dev = BPF_CORE_READ(from_path, mnt, mnt_sb, s_dev);- __u32 to_dev = BPF_CORE_READ(to_path, mnt, mnt_sb, s_dev);+ struct super_block *from = BPF_CORE_READ(from_path, mnt, mnt_sb);+ struct super_block *to = BPF_CORE_READ(to_path, mnt, mnt_sb);if (ret)return ret;- if (shirudo_deny_bpffs_mount(from_dev, SHIRUDO_TAMPER_BPFFS_MOUNT))+ 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_dev, SHIRUDO_TAMPER_BPFFS_MOUNT);+ ret = shirudo_image_mount(from, true);+ return ret ? ret : shirudo_image_mount(to, true);}SEC("lsm/sb_mount")@@ -44,10 +88,19 @@ int BPF_PROG(shirudo_lsm_sb_mount, const char *dev_name,const struct path *path, const char *type,unsigned long flags, void *data, int ret){+ struct super_block *sb = BPF_CORE_READ(path, mnt, mnt_sb);+if (ret)return ret;- return shirudo_deny_bpffs_mount(BPF_CORE_READ(path, mnt, mnt_sb, s_dev),- SHIRUDO_TAMPER_BPFFS_MOUNT);+ 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")@@ -56,6 +109,135 @@ int BPF_PROG(shirudo_lsm_sb_remount, struct super_block *sb,{if (ret)return ret;- return shirudo_deny_bpffs_mount(BPF_CORE_READ(sb, s_dev),- SHIRUDO_TAMPER_BPFFS_MOUNT);+ 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));++/*+ * Runtime roots match the frozen UKI value. Config roots must match the current+ * trusted task's one-use expectation; unrelated mappings gain no status.+ */+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_expected_root *expected;+ struct shirudo_device device = {};+ __u64 cookie = (__u64)bdev;+ const unsigned char *root;+ const char *algorithm;+ unsigned int i;+ char name[8];+ int diff;++ if (ret)+ return ret;+ if (!id || type != LSM_INT_DMVERITY_ROOTHASH)+ return 0;+ if (!value) {+ bpf_map_delete_elem(&shirudo_devices, &cookie);+ return 0;+ }+ if (shirudo_bdev_device(bdev))+ return -EPERM;+ if (BPF_CORE_READ(digest, digest_len) != sizeof(device.root))+ return 0;+ algorithm = BPF_CORE_READ(digest, alg);+ if (bpf_probe_read_kernel_str(name, sizeof(name), algorithm) != 7 ||+ name[0] != 's' || name[1] != 'h' || name[2] != 'a' ||+ name[3] != '2' || name[4] != '5' || name[5] != '6')+ return 0;+ root = BPF_CORE_READ(digest, digest);+ if (bpf_probe_read_kernel(device.root, sizeof(device.root), root))+ return -EACCES;+ diff = 0;+ for (i = 0; i < sizeof(device.root); i++)+ diff |= device.root[i] ^ id->runtime_root[i];+ if (!diff) {+ device.kind = SHIRUDO_DEVICE_RUNTIME;+ } else {+ if (!shirudo_task_is_self(id))+ return 0;+ expected = bpf_task_storage_get(&shirudo_roots,+ bpf_get_current_task_btf(),+ NULL, 0);+ if (!expected || !expected->valid)+ return 0;+ diff = 0;+ for (i = 0; i < sizeof(device.root); i++)+ diff |= device.root[i] ^ expected->root[i];+ if (diff)+ return 0;+ expected->valid = 0;+ device.kind = SHIRUDO_DEVICE_CONFIG;+ }+ device.dev = BPF_CORE_READ(bdev, bd_dev);+ device.diskseq = BPF_CORE_READ(bdev, bd_disk, diskseq);+ if (!device.diskseq)+ return -EACCES;+ if (bpf_map_update_elem(&shirudo_devices, &cookie, &device,+ BPF_ANY))+ return -ENOMEM;+ return 0;+}++SEC("lsm/bdev_free_security")+void BPF_PROG(shirudo_lsm_bdev_free_security, struct block_device *bdev)+{+ __u64 cookie = (__u64)bdev;++ bpf_map_delete_elem(&shirudo_devices, &cookie);+}++#define SHIRUDO_DM_CONTROL_DEV ((10U << 20) | 236U)+#define SHIRUDO_DM_IOCTL_TYPE 0xfdU++/* Match the kernel's type/number dispatch. */+static __always_inline int shirudo_dm_ioctl(struct file *file,+ unsigned int cmd, int ret)+{+ struct shirudo_sb_id *id = shirudo_policy();+ struct inode *inode;++ if (ret || !id || shirudo_task_is_self(id))+ return ret;+ inode = BPF_CORE_READ(file, f_inode);+ if ((BPF_CORE_READ(inode, i_mode) & 0170000) != 0020000 ||+ BPF_CORE_READ(inode, i_rdev) != SHIRUDO_DM_CONTROL_DEV)+ return 0;+ if (((cmd >> 8) & 0xff) != SHIRUDO_DM_IOCTL_TYPE)+ return -EPERM;+ switch (cmd & 0xff) {+ case 0: /* DM_VERSION */+ case 2: /* DM_LIST_DEVICES */+ case 7: /* DM_DEV_STATUS */+ case 8: /* DM_DEV_WAIT */+ case 11: /* DM_TABLE_DEPS */+ case 12: /* DM_TABLE_STATUS */+ case 13: /* DM_LIST_VERSIONS */+ case 17: /* DM_GET_TARGET_VERSION */+ return 0;+ default:+ return -EPERM;+ }+}++SEC("lsm/file_ioctl")+int BPF_PROG(shirudo_lsm_file_ioctl, struct file *file, unsigned int cmd,+ unsigned long arg, int ret)+{+ return shirudo_dm_ioctl(file, cmd, ret);}diff --git a/bpf/bpf2_verity.c b/bpf/bpf2_verity.cdeleted file mode 100644index aee4e672..00000000--- a/bpf/bpf2_verity.c+++ /dev/null@@ -1,243 +0,0 @@-// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause-/* Copyright (c) 2026 Authors of shirudo */--/*- * Image mode authorizes tasks marked after exec from the authenticated runtime.- * These extra hooks track verity devices, restrict DM controls, and protect the- * runtime/config mounts alongside the shared bpffs and BPF admission checks.- */-#include "verity_policy.h"-#include "admission.h"--char _license[] SEC("license") = "Dual BSD/GPL";--static __always_inline int shirudo_parent_mount(struct super_block *sb,- bool trusted_target)-{- struct shirudo_sb_id *id = shirudo_policy();-- if (!sb || !id || !id->run_dev ||- BPF_CORE_READ(sb, s_dev) != id->run_dev)- return 0;- return trusted_target && shirudo_task_is_self(id) ? 0 : -EPERM;-}--static __always_inline int shirudo_image_mount(struct super_block *sb,- bool replace)-{- struct shirudo_sb_id *id = shirudo_policy();- struct shirudo_device *device;- struct block_device *bdev;-- if (!sb || !id)- return 0;- bdev = BPF_CORE_READ(sb, s_bdev);- if (!bdev)- return 0;- device = shirudo_bdev_device(bdev);- if (!device)- return 0;- if (replace && device->kind == SHIRUDO_DEVICE_CONFIG &&- shirudo_task_is_self(id))- return 0;- return -EPERM;-}--SEC("lsm/sb_umount")-int BPF_PROG(shirudo_lsm_sb_umount, struct vfsmount *mnt, int flags,- int ret)-{- struct super_block *sb = BPF_CORE_READ(mnt, mnt_sb);- __u32 dev;-- if (ret)- return ret;- ret = shirudo_parent_mount(sb, false);- if (ret)- return ret;- if (BPF_CORE_READ_INTO(&dev, sb, s_dev))- return shirudo_tamper_deny(SHIRUDO_TAMPER_BPFFS_UMOUNT,- -EPERM);- ret = shirudo_deny_bpffs_mount(dev, SHIRUDO_TAMPER_BPFFS_UMOUNT);- return ret ? ret : shirudo_image_mount(sb, true);-}--SEC("lsm/move_mount")-int BPF_PROG(shirudo_lsm_move_mount, const struct path *from_path,- const struct path *to_path, int ret)-{- struct super_block *from = BPF_CORE_READ(from_path, mnt, mnt_sb);- struct super_block *to = BPF_CORE_READ(to_path, mnt, mnt_sb);-- if (ret)- return ret;- if (shirudo_parent_mount(from, false) ||- shirudo_parent_mount(to, true))- return -EPERM;- if (shirudo_deny_bpffs_mount(BPF_CORE_READ(from, s_dev),- SHIRUDO_TAMPER_BPFFS_MOUNT) ||- shirudo_deny_bpffs_mount(BPF_CORE_READ(to, s_dev),- SHIRUDO_TAMPER_BPFFS_MOUNT))- return -EPERM;- ret = shirudo_image_mount(from, true);- return ret ? ret : shirudo_image_mount(to, true);-}--SEC("lsm/sb_mount")-int BPF_PROG(shirudo_lsm_sb_mount, const char *dev_name,- const struct path *path, const char *type,- unsigned long flags, void *data, int ret)-{- struct super_block *sb = BPF_CORE_READ(path, mnt, mnt_sb);-- if (ret)- return ret;- if ((flags & ((1UL << 17) | (1UL << 18) | (1UL << 19) |- (1UL << 20))) && shirudo_parent_mount(sb, false))- return -EPERM;- ret = shirudo_parent_mount(sb, true);- if (ret)- return ret;- ret = shirudo_deny_bpffs_mount(BPF_CORE_READ(sb, s_dev),- SHIRUDO_TAMPER_BPFFS_MOUNT);- return ret ? ret : shirudo_image_mount(sb, true);-}--SEC("lsm/sb_remount")-int BPF_PROG(shirudo_lsm_sb_remount, struct super_block *sb,- void *mnt_opts, int ret)-{- if (ret)- return ret;- ret = shirudo_parent_mount(sb, false);- if (ret)- return ret;- ret = shirudo_deny_bpffs_mount(BPF_CORE_READ(sb, s_dev),- SHIRUDO_TAMPER_BPFFS_MOUNT);- return ret ? ret : shirudo_image_mount(sb, false);-}--struct dm_verity_digest___shirudo {- const char *alg;- const __u8 *digest;- size_t digest_len;-} __attribute__((preserve_access_index));--/*- * Runtime roots match the frozen UKI value. Config roots must match the current- * trusted task's one-use expectation; unrelated mappings gain no status.- */-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_expected_root *expected;- struct shirudo_device device = {};- __u64 cookie = (__u64)bdev;- const unsigned char *root;- const char *algorithm;- unsigned int i;- char name[8];- int diff;-- if (ret)- return ret;- if (!id || type != LSM_INT_DMVERITY_ROOTHASH)- return 0;- if (!value) {- bpf_map_delete_elem(&shirudo_devices, &cookie);- return 0;- }- if (shirudo_bdev_device(bdev))- return -EPERM;- if (BPF_CORE_READ(digest, digest_len) != sizeof(device.root))- return 0;- algorithm = BPF_CORE_READ(digest, alg);- if (bpf_probe_read_kernel_str(name, sizeof(name), algorithm) != 7 ||- name[0] != 's' || name[1] != 'h' || name[2] != 'a' ||- name[3] != '2' || name[4] != '5' || name[5] != '6')- return 0;- root = BPF_CORE_READ(digest, digest);- if (bpf_probe_read_kernel(device.root, sizeof(device.root), root))- return -EACCES;- diff = 0;- for (i = 0; i < sizeof(device.root); i++)- diff |= device.root[i] ^ id->runtime_root[i];- if (!diff) {- device.kind = SHIRUDO_DEVICE_RUNTIME;- } else {- if (!shirudo_task_is_self(id))- return 0;- expected = bpf_task_storage_get(&shirudo_roots,- bpf_get_current_task_btf(),- NULL, 0);- if (!expected || !expected->valid)- return 0;- diff = 0;- for (i = 0; i < sizeof(device.root); i++)- diff |= device.root[i] ^ expected->root[i];- if (diff)- return 0;- expected->valid = 0;- device.kind = SHIRUDO_DEVICE_CONFIG;- }- device.dev = BPF_CORE_READ(bdev, bd_dev);- device.diskseq = BPF_CORE_READ(bdev, bd_disk, diskseq);- if (!device.diskseq)- return -EACCES;- if (bpf_map_update_elem(&shirudo_devices, &cookie, &device,- BPF_ANY))- return -ENOMEM;- return 0;-}--SEC("lsm/bdev_free_security")-void BPF_PROG(shirudo_lsm_bdev_free_security, struct block_device *bdev)-{- __u64 cookie = (__u64)bdev;-- bpf_map_delete_elem(&shirudo_devices, &cookie);-}--#define SHIRUDO_DM_CONTROL_DEV ((10U << 20) | 236U)-#define SHIRUDO_DM_IOCTL_TYPE 0xfdU--/* Match the kernel's type/number dispatch. */-static __always_inline int shirudo_dm_ioctl(struct file *file,- unsigned int cmd, int ret)-{- struct shirudo_sb_id *id = shirudo_policy();- struct inode *inode;-- if (ret || !id || shirudo_task_is_self(id))- return ret;- inode = BPF_CORE_READ(file, f_inode);- if ((BPF_CORE_READ(inode, i_mode) & 0170000) != 0020000 ||- BPF_CORE_READ(inode, i_rdev) != SHIRUDO_DM_CONTROL_DEV)- return 0;- if (((cmd >> 8) & 0xff) != SHIRUDO_DM_IOCTL_TYPE)- return -EPERM;- switch (cmd & 0xff) {- case 0: /* DM_VERSION */- case 2: /* DM_LIST_DEVICES */- case 7: /* DM_DEV_STATUS */- case 8: /* DM_DEV_WAIT */- case 11: /* DM_TABLE_DEPS */- case 12: /* DM_TABLE_STATUS */- case 13: /* DM_LIST_VERSIONS */- case 17: /* DM_GET_TARGET_VERSION */- return 0;- default:- return -EPERM;- }-}--SEC("lsm/file_ioctl")-int BPF_PROG(shirudo_lsm_file_ioctl, struct file *file, unsigned int cmd,- unsigned long arg, int ret)-{- return shirudo_dm_ioctl(file, cmd, ret);-}diff --git a/bpf/lib/shirudo_identity.h b/bpf/lib/shirudo_identity.hindex 6d70d250..cb256455 100644--- a/bpf/lib/shirudo_identity.h+++ b/bpf/lib/shirudo_identity.h@@ -9,18 +9,11 @@struct shirudo_sb_id {__u32 sb_dev;- __u32 exe_dev;- __u64 exe_ino;+ __u32 run_dev;__s32 keyring_serial;__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];};@@ -31,27 +24,4 @@ struct {__type(value, struct shirudo_sb_id);} shirudo_bpffs_sb SEC(".maps");-static __always_inline bool shirudo_file_is_self(const struct shirudo_sb_id *id,- struct file *file)-{- __u32 dev;- __u64 ino;-- if (!file)- return false;- dev = BPF_CORE_READ(file, f_inode, i_sb, s_dev);- ino = BPF_CORE_READ(file, f_inode, i_ino);- if (dev == id->exe_dev && ino == id->exe_ino)- return true;- return id->alt_exe_ino && dev == id->alt_exe_dev &&- ino == id->alt_exe_ino;-}--static __always_inline bool shirudo_task_matches_exe(const struct shirudo_sb_id *id)-{- struct task_struct *task = bpf_get_current_task_btf();-- return shirudo_file_is_self(id, BPF_CORE_READ(task, mm, exe_file));-}-#endif /* SHIRUDO_BPF_IDENTITY_H */diff --git a/bpf/lsm.h b/bpf/lsm.hindex a6675b7c..59f7e558 100644--- a/bpf/lsm.h+++ b/bpf/lsm.h@@ -30,7 +30,7 @@#define PR_SET_MM_MAP 14#include "lib/shirudo_identity.h"-#include "../include/shirudo_mode.h"+#include "../include/shirudo_enforcement.h"#include "shirudo_tamper.h"#define SHIRUDO_TASK_TRUSTED 1diff --git a/bpf/policy.h b/bpf/policy.hdeleted file mode 100644index 36aa4295..00000000--- a/bpf/policy.h+++ /dev/null@@ -1,39 +0,0 @@-/* 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 "lsm.h"--#define SHIRUDO_MODE_ERROR SHIRUDO_PR_MODE_LEGACY_ERR-#define SHIRUDO_TASK_STORAGE_REQUIRED false--/* A label protects the target; the frozen executable authorizes the caller. */-static __always_inline bool shirudo_task_is_self(const struct shirudo_sb_id *id)-{- return shirudo_task_matches_exe(id);-}--static __always_inline bool-shirudo_protected_filesystem(const struct shirudo_sb_id *id, __u32 dev)-{- return dev == id->sb_dev;-}--/* Ordinary mode has no device-authority map to hide from observers. */-static __always_inline bool shirudo_private_map(struct bpf_map *map)-{- return false;-}--/* Replacing the binary for an upgrade does not authorize its replacement. */-static __always_inline bool shirudo_replaceable_executable(struct inode *inode)-{- struct shirudo_sb_id *id = shirudo_policy();-- return id && BPF_CORE_READ(inode, i_sb, s_dev) == id->exe_dev &&- BPF_CORE_READ(inode, i_ino) == id->exe_ino;-}--#endif /* SHIRUDO_BPF_POLICY_H */diff --git a/bpf/seal.c b/bpf/seal.cindex fc0f3bd3..3bf7810d 100644--- a/bpf/seal.c+++ b/bpf/seal.c@@ -1,16 +1,29 @@// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause/* Copyright (c) 2026 Authors of shirudo */-/* Ordinary mode labels assets and recognizes the recorded executable. */-#include "policy.h"+/* Shirudo retains the writable-state label seal and trusts runtime execs. */+#include "verity_policy.h"#include "seal.h"char _license[] SEC("license") = "Dual BSD/GPL";+struct seccomp___shirudo {+ int mode;+} __attribute__((preserve_access_index));++/* CONFIG_SECCOMP=n leaves struct seccomp empty in the kernel BTF. */+static __always_inline bool shirudo_task_has_seccomp(struct task_struct *task)+{+ struct seccomp___shirudo *seccomp = (void *)&task->seccomp;++ return bpf_core_field_exists(seccomp->mode) &&+ BPF_CORE_READ(seccomp, mode);+}+/*- * In ordinary mode the label marks processes for ptrace/perf protection. It- * grants no permission to mutate files or BPF objects; those checks compare- * the executable device/inode frozen at init. Stage the mark until commit.+ * Shirudo grants authority only to a direct file on the verified runtime.+ * Overlay copies cannot qualify. Ptrace or seccomp supplied before exec must+ * not control the new trusted process. A failed exec retains the old mark.*/SEC("lsm.s/bprm_check_security")int BPF_PROG(shirudo_seal_bprm_check_security,@@ -18,24 +31,70 @@ int BPF_PROG(shirudo_seal_bprm_check_security,{struct task_struct *task = bpf_get_current_task_btf();struct shirudo_task_authority *authority;- struct seal_scratch *scratch;- struct bpf_dynptr dp;- int len;+ struct inode *real_inode;+ bool trusted;if (ret)return ret;if (!shirudo_policy())return -EACCES;- scratch = seal_scratch_get(task);- if (!scratch)- return 0;- bpf_dynptr_from_mem(scratch->target, LABEL_LEN, 0, &dp);- len = bpf_get_file_xattr(bprm->file, SHIRUDO_XATTR, &dp);+ real_inode = bpf_real_data_inode(bprm->file);+ trusted = real_inode == BPF_CORE_READ(bprm, file, f_inode) &&+ shirudo_file_runtime(bprm->file);+ if (trusted && (BPF_CORE_READ(task, ptrace) ||+ shirudo_task_has_seccomp(task)))+ return -EACCES;authority = bpf_task_storage_get(&shirudo_tasks, task, NULL,BPF_LOCAL_STORAGE_GET_F_CREATE);if (!authority)- return 0;- authority->candidate_trusted = len == LABEL_LEN;+ return -ENOMEM;+ authority->candidate_trusted = trusted;authority->candidate_valid = 1;return 0;}++#define SHIRUDO_PROT_WRITE 2+#define SHIRUDO_PROT_EXEC 4+#define SHIRUDO_VM_EXEC 4++/* Executable mappings in a trusted process must come from its runtime image. */+SEC("lsm/mmap_file")+int BPF_PROG(shirudo_seal_mmap_file, struct file *file,+ unsigned long reqprot, unsigned long prot,+ unsigned long flags, int ret)+{+ struct shirudo_sb_id *id = shirudo_policy();++ if (ret)+ return ret;+ if (!id || !shirudo_task_is_self(id) ||+ shirudo_bootstrap_task() || !(prot & SHIRUDO_PROT_EXEC))+ return 0;+ if ((prot & SHIRUDO_PROT_WRITE) || !shirudo_file_runtime(file))+ return -EACCES;+ return 0;+}++/* Disallow executable promotion of pages that could have been modified. */+SEC("lsm/file_mprotect")+int BPF_PROG(shirudo_seal_file_mprotect, struct vm_area_struct *vma,+ unsigned long reqprot, unsigned long prot, int ret)+{+ struct shirudo_sb_id *id = shirudo_policy();+ unsigned long old_flags;++ if (ret)+ return ret;+ old_flags = BPF_CORE_READ(vma, vm_flags);+ if (!id || !shirudo_task_is_self(id) ||+ shirudo_bootstrap_task())+ return 0;+ if ((old_flags & SHIRUDO_VM_EXEC) && (prot & SHIRUDO_PROT_WRITE))+ return -EACCES;+ if (!(prot & SHIRUDO_PROT_EXEC))+ return 0;+ if (!(old_flags & SHIRUDO_VM_EXEC) ||+ !shirudo_file_runtime(BPF_CORE_READ(vma, vm_file)))+ return -EACCES;+ return 0;+}diff --git a/bpf/seal.h b/bpf/seal.hindex 036fd58b..a7df2462 100644--- a/bpf/seal.h+++ b/bpf/seal.h@@ -183,12 +183,12 @@ int BPF_PROG(shirudo_seal_task_alloc, struct task_struct *child,authority = bpf_task_storage_get(&shirudo_tasks, child, NULL,BPF_LOCAL_STORAGE_GET_F_CREATE);if (!authority)- return SHIRUDO_TASK_STORAGE_REQUIRED ? -ENOMEM : 0;+ return -ENOMEM;authority->trusted = parent_authority->trusted;return 0;}-/* Protect tasks marked by labelled exec (ordinary) or runtime exec (image). */+/* 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)@@ -208,7 +208,7 @@ int BPF_PROG(shirudo_seal_perf_event_alloc,return 0;}-/* Both modes keep ptrace away from processes with committed exec marks. */+/* 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)@@ -342,12 +342,7 @@ int BPF_PROG(shirudo_seal_inode_unlink, struct inode *dir,return ret ? ret : seal_dentry(dentry, SHIRUDO_TAMPER_SEAL_UNLINK);}-/*- * Protect both ends of a rename: moving a labelled inode out and- * replacing one. Ordinary mode allows replacement of its recorded binary- * for upgrades, without giving the replacement authority. Image mode never- * makes that exception: executable files are on the read-only runtime.- */+/* Protect both the source and an existing destination of a rename. */SEC("lsm.s/inode_rename")int BPF_PROG(shirudo_seal_inode_rename, struct inode *old_dir,struct dentry *old_dentry, struct inode *new_dir,@@ -361,7 +356,7 @@ int BPF_PROG(shirudo_seal_inode_rename, struct inode *old_dir,err = seal_dentry(old_dentry, SHIRUDO_TAMPER_SEAL_RENAME);if (err)return err;- if (!target || shirudo_replaceable_executable(target))+ if (!target)return 0;return seal_dentry(new_dentry, SHIRUDO_TAMPER_SEAL_RENAME);}diff --git a/bpf/seal_verity.c b/bpf/seal_verity.cdeleted file mode 100644index f77a0162..00000000--- a/bpf/seal_verity.c+++ /dev/null@@ -1,100 +0,0 @@-// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause-/* Copyright (c) 2026 Authors of shirudo */--/* Image mode retains the writable-state label seal and trusts runtime execs. */-#include "verity_policy.h"-#include "seal.h"--char _license[] SEC("license") = "Dual BSD/GPL";--struct seccomp___shirudo {- int mode;-} __attribute__((preserve_access_index));--/* CONFIG_SECCOMP=n leaves struct seccomp empty in the kernel BTF. */-static __always_inline bool shirudo_task_has_seccomp(struct task_struct *task)-{- struct seccomp___shirudo *seccomp = (void *)&task->seccomp;-- return bpf_core_field_exists(seccomp->mode) &&- BPF_CORE_READ(seccomp, mode);-}--/*- * Image mode grants authority only to a direct file on the verified runtime.- * Overlay copies cannot qualify. Ptrace or seccomp supplied before exec must- * not control the new trusted process. A failed exec retains the old mark.- */-SEC("lsm.s/bprm_check_security")-int BPF_PROG(shirudo_seal_bprm_check_security,- struct linux_binprm *bprm, int ret)-{- struct task_struct *task = bpf_get_current_task_btf();- struct shirudo_task_authority *authority;- struct inode *real_inode;- bool trusted;-- if (ret)- return ret;- if (!shirudo_policy())- return -EACCES;- real_inode = bpf_real_data_inode(bprm->file);- trusted = real_inode == BPF_CORE_READ(bprm, file, f_inode) &&- shirudo_file_runtime(bprm->file);- if (trusted && (BPF_CORE_READ(task, ptrace) ||- shirudo_task_has_seccomp(task)))- return -EACCES;- authority = bpf_task_storage_get(&shirudo_tasks, task, NULL,- BPF_LOCAL_STORAGE_GET_F_CREATE);- if (!authority)- return -ENOMEM;- authority->candidate_trusted = trusted;- authority->candidate_valid = 1;- return 0;-}--#define SHIRUDO_PROT_WRITE 2-#define SHIRUDO_PROT_EXEC 4-#define SHIRUDO_VM_EXEC 4--/* Executable mappings in a trusted process must come from its runtime image. */-SEC("lsm/mmap_file")-int BPF_PROG(shirudo_seal_mmap_file, struct file *file,- unsigned long reqprot, unsigned long prot,- unsigned long flags, int ret)-{- struct shirudo_sb_id *id = shirudo_policy();-- if (ret)- return ret;- if (!id || !shirudo_task_is_self(id) ||- shirudo_bootstrap_task() || !(prot & SHIRUDO_PROT_EXEC))- return 0;- if ((prot & SHIRUDO_PROT_WRITE) || !shirudo_file_runtime(file))- return -EACCES;- return 0;-}--/* Disallow executable promotion of pages that could have been modified. */-SEC("lsm/file_mprotect")-int BPF_PROG(shirudo_seal_file_mprotect, struct vm_area_struct *vma,- unsigned long reqprot, unsigned long prot, int ret)-{- struct shirudo_sb_id *id = shirudo_policy();- unsigned long old_flags;-- if (ret)- return ret;- old_flags = BPF_CORE_READ(vma, vm_flags);- if (!id || !shirudo_task_is_self(id) ||- shirudo_bootstrap_task())- return 0;- if ((old_flags & SHIRUDO_VM_EXEC) && (prot & SHIRUDO_PROT_WRITE))- return -EACCES;- if (!(prot & SHIRUDO_PROT_EXEC))- return 0;- if (!(old_flags & SHIRUDO_VM_EXEC) ||- !shirudo_file_runtime(BPF_CORE_READ(vma, vm_file)))- return -EACCES;- return 0;-}diff --git a/bpf/verity_policy.h b/bpf/verity_policy.hindex 4311f982..c4dd7a1d 100644--- a/bpf/verity_policy.h+++ b/bpf/verity_policy.h@@ -6,9 +6,6 @@#include "lsm.h"-#define SHIRUDO_MODE_ERROR SHIRUDO_PR_MODE_IMAGE_ERR-#define SHIRUDO_TASK_STORAGE_REQUIRED true-#define SHIRUDO_DEVICE_RUNTIME 1#define SHIRUDO_DEVICE_CONFIG 2@@ -71,12 +68,6 @@ static __always_inline bool shirudo_private_map(struct bpf_map *map)return !__builtin_memcmp(name, "shirudo_devices", sizeof(name));}-/* Runtime files are immutable; writable state gets no executable exception. */-static __always_inline bool shirudo_replaceable_executable(struct inode *inode)-{- return false;-}-/* Device pointers can be reused even when the free hook did not run. */static __always_inline struct shirudo_device *shirudo_bdev_device(struct block_device *bdev)diff --git a/builtin-init.c b/builtin-init.cindex df5253d0..8b6207fe 100644--- a/builtin-init.c+++ b/builtin-init.c@@ -32,7 +32,6 @@#include "builtin.h"#include "util/bpm_tree.h"#include "util/chain.h"-#include "util/elf_target.h"#include "util/config.h"#include "util/eventlog.h"#include "util/help.h"@@ -130,45 +129,9 @@ static int keyring_asymmetric_count(key_serial_t ring)return (int)n;}-static bool bpf_keyring_mode;-static struct shirudo_trusted_set enrolled;static bool initrd_mode;-static bool image_mode;--/*- * The initrd enrols its own, measured copies of the keys. The root's- * copies are what every later command reads, so the two differing- * means an initrd that was not regenerated after the keys changed:- * worth a warning, not a refusal, since the measured copies are the- * ones a verifier pins through PCR 11.- */-static void warn_root_keys_differ(const struct shirudo_trusted_set *set)-{- struct shirudo_trusted_set root;- bool differ;- size_t i;- int rc;-- rc = keys_load(SHIRUDO_KEYS_DIR, NULL, true, &root);- if (rc) {- pr_warn("initrd: the root file system has no certificate "- "under %s (%s); the copies under %s are the anchor",- SHIRUDO_KEYS_DIR, strerror(-rc),- SHIRUDO_INITRD_KEYS_DIR);- return;- }- differ = root.n != set->n;- for (i = 0; !differ && i < set->n; i++)- differ = memcmp(root.certs[i].sha256, set->certs[i].sha256,- 32) != 0;- if (differ)- pr_warn("initrd: the keys under %s differ from the root "- "file system's under %s; the initrd's copies are the "- "anchor, regenerate the initrd after changing the keys",- SHIRUDO_INITRD_KEYS_DIR, SHIRUDO_KEYS_DIR);-}static int keyring_holds_set(key_serial_t ring,const struct shirudo_trusted_set *set)@@ -198,9 +161,8 @@ static int keyring_holds_set(key_serial_t ring,* takes keys until the first seal, which is init's job from the initrd:* enrol every trusted certificate and seal. Found sealed, it either* holds exactly that set (-EEXIST: use it) or was sealed empty by the- * kernel at boot (-ENOKEY: the user keyring stages the primary- * certificate until the target reboots into the production- * configuration); sealed around anything else, it is refused.+ * kernel at boot (-ENOKEY: initialization refuses until reboot with the+ * enrollment parameter); sealed around anything else, it is refused.*/static int init_bpf_keyring(key_serial_t ring,const struct shirudo_trusted_set *set)@@ -281,173 +243,28 @@ static int init_bpf_keyring(key_serial_t ring,static int init_keyring(void){- const struct shirudo_trusted_cert *primary = NULL;struct shirudo_trusted_set set;struct shirudo_identity id;- key_serial_t ring, key;- size_t der_sz = 0, i;- __u8 *der = NULL;- bool frozen;+ key_serial_t ring;int rc;- if (image_mode && identity_read(&id) == 0) {- ring = keyring_bpf_serial();- if (!id.runtime_mode ||- id.keyring_type != SHIRUDO_SIG_KEYRING_BPF ||- ring <= 0 || id.keyring_serial != ring ||+ ring = keyring_bpf_serial();+ if (ring <= 0)+ return ring ? ring : -ENOKEY;+ if (!identity_read(&id)) {+ if (id.keyring_type != SHIRUDO_SIG_KEYRING_BPF ||+ id.keyring_serial != ring ||memcmp(id.runtime_root, shirudo_image_runtime_root(), 32))return -EKEYREJECTED;- bpf_keyring_mode = true;return 0;}- if (image_mode)- rc = shirudo_image_boot_keys(&set);- else- rc = keys_load(shirudo_keys_dir(),- SHIRUDO_KEYS_REVOKED_DIR, true, &set);- if (rc) {- pr_err("read the keys under %s: %s", shirudo_keys_dir(),- strerror(-rc));+ rc = shirudo_image_boot_keys(&set);+ if (rc)return rc;- }- if (set.n_revoked && !image_mode)- pr_warn("%zu revoked certificate%s under %s left out; "- "regenerate the initrd", set.n_revoked,- set.n_revoked == 1 ? "" : "s", shirudo_keys_dir());- if (initrd_mode && !image_mode)- warn_root_keys_differ(&set);- frozen = identity_read(&id) == 0;-- ring = keyring_bpf_serial();- if (ring > 0) {- rc = init_bpf_keyring(ring, &set);- if (rc == 0 || rc == -EEXIST) {- if (frozen &&- (id.keyring_type != SHIRUDO_SIG_KEYRING_BPF ||- id.keyring_serial != ring)) {- pr_err("bpf keyring serial=%d holds the "- "certificates, but the anchor frozen "- "this boot is another keyring "- "(tampered)", (int)ring);- return -EKEYREJECTED;- }- bpf_keyring_mode = true;- enrolled = set;- return 0;- }- if (rc != -ENOKEY || image_mode)- return rc;- pr_warn("bpf keyring serial=%d is sealed empty; staging the "- "primary certificate in the user keyring. Boot with "- "bpf.keyring_unsealed=1 and run init from the initrd "- "for the production trust anchor", (int)ring);- } else {- if (image_mode)- return ring;- pr_warn("kernel has no bpf keyring (%s); staging the "- "primary certificate in the user keyring",- strerror(-ring));- }-- /*- * Staging holds the primary certificate alone: the user keyring- * is a stopgap until the reboot, not a place for a set. The- * keyring found by name must be the one whose serial init froze- * this boot, if it froze one: a substitute or a missing keyring- * cannot be repaired before the next boot, because the frozen- * identity is what lsm/bpf_prog_load enforces.- */- for (i = 0; i < set.n; i++) {- if (set.certs[i].primary)- primary = &set.certs[i];- }- if (!primary) {- pr_err("no primary certificate at %s", shirudo_cert_path());- return -ENOENT;- }- rc = read_der(primary->path, &der, &der_sz);- if (rc) {- pr_err("read %s: %s", primary->path, strerror(-rc));+ rc = init_bpf_keyring(ring, &set);+ if (rc && rc != -EEXIST)return rc;- }- memset(&enrolled, 0, sizeof(enrolled));- enrolled.certs[0] = *primary;- enrolled.n = 1;-- ring = keyring_find(SHIRUDO_KEYRING_NAME);- if (frozen && id.keyring_type == SHIRUDO_SIG_KEYRING_USER &&- ring != id.keyring_serial) {- pr_err("keyring \"%s\" is %s; the anchor frozen this boot is "- "serial %d, which only a reboot can replace (tampered)",- SHIRUDO_KEYRING_NAME, ring < 0 ? "gone" :- "not the one init sealed", (int)id.keyring_serial);- xfree(der, der_sz);- return -EKEYREJECTED;- }- if (ring >= 0) {- rc = keyring_holds_cert(ring, der, der_sz);- if (rc > 0) {- xfree(der, der_sz);- rc = keyring_seal(ring);- if (rc && rc != -EACCES)- pr_warn("keyring \"%s\": could not seal (mutable): %s",- SHIRUDO_KEYRING_NAME, strerror(-rc));- pr_info("keyring \"%s\" already present (serial=%d), sealed",- SHIRUDO_KEYRING_NAME, (int)ring);- return 0;- }- pr_warn("keyring \"%s\" serial=%d %s; recreating",- SHIRUDO_KEYRING_NAME, (int)ring,- rc == 0 ? "does not hold the certificate" :- strerror(-rc));- rc = keyring_unlink_from_user(ring);- if (rc) {- pr_err("unlink stale keyring \"%s\": %s",- SHIRUDO_KEYRING_NAME, strerror(-rc));- xfree(der, der_sz);- return rc;- }- } else if (ring != -ENOKEY) {- pr_err("lookup keyring \"%s\": %s",- SHIRUDO_KEYRING_NAME, strerror(-ring));- xfree(der, der_sz);- return ring;- }-- ring = keyring_open_or_create(SHIRUDO_KEYRING_NAME);- if (ring < 0) {- pr_err("open/create keyring \"%s\": %s",- SHIRUDO_KEYRING_NAME, strerror(-ring));- xfree(der, der_sz);- return ring;- }-- key = keyring_add_cert(ring, der, der_sz);- xfree(der, der_sz);- if (key < 0) {- pr_err("add cert to keyring: %s", strerror(-key));- if (key == -ENOENT || key == -ENOPKG)- pr_err("kernel cannot verify this cert's public-key "- "algorithm (ML-DSA needs CONFIG_CRYPTO_MLDSA=y "- "and CONFIG_CRYPTO_SHA512=y; see "- "Documentation/requirements.rst)");- return key;- }-- /*- * Freeze the keyring so no further key can be linked into it this- * boot -- a root attacker cannot drop in a second cert to make the- * kernel accept BPF they signed. Not fatal if it fails: the LSM- * still enforces; the keyring is just left mutable.- */- rc = keyring_seal(ring);- if (rc)- pr_warn("keyring \"%s\": could not seal (mutable): %s",- SHIRUDO_KEYRING_NAME, strerror(-rc));-- pr_info("keyring \"%s\" serial=%d%s, cert key serial=%d",- SHIRUDO_KEYRING_NAME, (int)ring, rc ? "" : " sealed",- (int)key);+ enrolled = set;return 0;}@@ -476,6 +293,7 @@ static int is_bpffs_mounted(const char *path)static int init_bpffs(void){+ struct stat parent, run;int rc;rc = seal_mkdir_p(SHIRUDO_VAR_LIB, 0700);@@ -488,36 +306,30 @@ static int init_bpffs(void)pr_err("mkdir %s: %s", SHIRUDO_RUN_DIR, strerror(-rc));return rc;}- if (image_mode) {- struct stat parent, run;-- rc = is_bpffs_mounted(SHIRUDO_BPFFS_DIR);- if (rc < 0)- return rc;+ rc = is_bpffs_mounted(SHIRUDO_BPFFS_DIR);+ if (rc < 0)+ return rc;+ if (rc)+ return 0;+ if (stat(SHIRUDO_RUN_DIR, &parent) || stat("/run", &run))+ return -errno;+ if (parent.st_dev == run.st_dev) {+ rc = seal_label(SHIRUDO_RUN_DIR);if (rc)- return 0;- if (stat(SHIRUDO_RUN_DIR, &parent) || stat("/run", &run))- return -errno;- if (parent.st_dev == run.st_dev) {- rc = seal_label(SHIRUDO_RUN_DIR);- if (rc)- return rc;- if (mount("tmpfs", SHIRUDO_RUN_DIR, "tmpfs",- MS_NOSUID | MS_NODEV | MS_NOEXEC,- "mode=0700,size=16M"))- return -errno;- }- if (mount(NULL, SHIRUDO_RUN_DIR, NULL,- MS_PRIVATE | MS_REC, NULL))+ return rc;+ if (mount("tmpfs", SHIRUDO_RUN_DIR, "tmpfs",+ MS_NOSUID | MS_NODEV | MS_NOEXEC,+ "mode=0700,size=16M"))return -errno;}+ if (mount(NULL, SHIRUDO_RUN_DIR, NULL,+ MS_PRIVATE | MS_REC, NULL))+ return -errno;+/* So that the init record left here is sealed from its creation. */rc = seal_label(SHIRUDO_RUN_DIR);- if (rc) {- if (image_mode)- return rc;- pr_warn("seal: label %s: %s", SHIRUDO_RUN_DIR, strerror(-rc));- }+ if (rc)+ return rc;rc = seal_mkdir_p(SHIRUDO_BPFFS_DIR, 0700);if (rc) {pr_err("mkdir %s: %s",@@ -553,102 +365,6 @@ static __u32 kernel_dev_t(dev_t st_dev)return ((__u32)major(st_dev) << 20) | (minor(st_dev) & 0xfffffu);}-/*- * Whether two shirudo binaries come from different builds: by GNU- * build ID, which survives the stripping an initrd generator applies,- * and by content only when one of them carries no ID.- */-static bool binaries_differ(const char *a, const char *b)-{- __u8 ida[SHIRUDO_UPATCH_BUILD_ID_MAX], idb[SHIRUDO_UPATCH_BUILD_ID_MAX];- __u8 ba[65536], bb[65536];- __u32 lena = 0, lenb = 0;- bool differ = true;- ssize_t na, nb;- int fa, fb;-- fa = open(a, O_RDONLY | O_CLOEXEC);- fb = open(b, O_RDONLY | O_CLOEXEC);- if (fa < 0 || fb < 0)- goto out;- if (elf_build_id_fd(fa, ida, &lena) == 0 && lena &&- elf_build_id_fd(fb, idb, &lenb) == 0 && lenb) {- differ = lena != lenb || memcmp(ida, idb, lena) != 0;- goto out;- }- if (lseek(fa, 0, SEEK_SET) < 0 || lseek(fb, 0, SEEK_SET) < 0)- goto out;- do {- na = read(fa, ba, sizeof(ba));- nb = read(fb, bb, sizeof(bb));- if (na < 0 || nb < 0 || na != nb || memcmp(ba, bb, (size_t)na))- goto out;- } while (na > 0);- differ = false;-out:- if (fa >= 0)- close(fa);- if (fb >= 0)- close(fb);- return differ;-}--/*- * The executables the bpffs guards admit. Normally the running binary- * alone: /proc/self/exe resolves to it, and it is what later runs- * `upatch unload` and sets the payload xattr on a pin. From the initrd- * the running binary is the initrd's copy, which is gone after the- * switch to the root file system; the identity that lasts is the- * root's binary at the same path, and the initrd's copy is recorded- * beside it only for the rest of this init. Image mode records no executable- * inode: its successful-exec mark is bound to the authenticated runtime root.- */-static int identity_executables(struct shirudo_identity *id)-{- char self[PATH_MAX], root_exe[PATH_MAX];- struct stat st;- ssize_t n;-- if (image_mode) {- id->runtime_mode = 1;- memcpy(id->runtime_root, shirudo_image_runtime_root(), 32);- return 0;- }- if (stat("/proc/self/exe", &st) != 0) {- pr_err("stat /proc/self/exe: %s", strerror(errno));- return -errno;- }- if (!initrd_mode) {- id->exe_dev = kernel_dev_t(st.st_dev);- id->exe_ino = (__u64)st.st_ino;- return 0;- }- id->alt_exe_dev = kernel_dev_t(st.st_dev);- id->alt_exe_ino = (__u64)st.st_ino;-- n = readlink("/proc/self/exe", self, sizeof(self) - 1);- if (n < 0) {- pr_err("readlink /proc/self/exe: %s", strerror(errno));- return -errno;- }- self[n] = '\0';- if (strsprintf(root_exe, sizeof(root_exe), "%s%s", sysroot(),- self) < 0)- return -ENAMETOOLONG;- if (stat(root_exe, &st) != 0) {- pr_err("initrd: %s: %s; the root file system needs shirudo "- "installed at the path the initrd runs it from",- root_exe, strerror(errno));- return -errno;- }- id->exe_dev = kernel_dev_t(st.st_dev);- id->exe_ino = (__u64)st.st_ino;- if (binaries_differ("/proc/self/exe", root_exe))- pr_warn("initrd: %s differs from the initrd's copy of shirudo; "- "regenerate the initrd after upgrading", root_exe);- return 0;-}-static int attach_pin(struct bpf_program *p, const char *pin,struct bpf_link **out){@@ -683,7 +399,7 @@ static int attach_guard(struct bpf_program *program,return attach_pin(program, shirudo_gate_guards[guard].pin, link);}-/* The chosen object pair shares identity, task marks and refusal counters. */+/* The admission and seal objects share bootstrap state and task marks. */static int share_policy_maps(struct bpf_object *source,struct bpf_object *destination){@@ -692,9 +408,6 @@ static int share_policy_maps(struct bpf_object *source,int rc;for (i = 0; i < ARRAY_SIZE(shirudo_guard_maps); i++) {- if (!image_mode && (i == SHIRUDO_GUARD_DEVICES ||- i == SHIRUDO_GUARD_ROOTS))- continue;from = bpf_object__find_map_by_name(source,shirudo_guard_maps[i].name);to = bpf_object__find_map_by_name(destination,@@ -732,11 +445,11 @@ static int attach_guard_group(struct bpf_object *obj,return 0;}-/* Both mode objects retain the same writable-state xattr protection. */+/* Writable state and runtime processes always get the full seal. */static int attach_seal(struct bpf_object *policy, struct bpf_object **obj_out,struct bpf_link **links, struct bpf_link **image_links){- const struct shirudo_embedded_bpf *blob = seal_blob(image_mode);+ const struct shirudo_embedded_bpf *blob = seal_blob();struct bpf_object *obj;int err;@@ -767,7 +480,7 @@ static int attach_seal(struct bpf_object *policy, struct bpf_object **obj_out,}err = attach_guard_group(obj, shirudo_seal_guards,ARRAY_SIZE(shirudo_seal_guards), links);- if (!err && image_mode)+ if (!err)err = attach_guard_group(obj, shirudo_image_seal_guards,ARRAY_SIZE(shirudo_image_seal_guards),image_links);@@ -820,9 +533,6 @@ static int freeze_task_maps(struct bpf_object *obj)const struct shirudo_guard_map *expected =&shirudo_guard_maps[i];- if (!image_mode && (i == SHIRUDO_GUARD_DEVICES ||- i == SHIRUDO_GUARD_ROOTS))- continue;map = bpf_object__find_map_by_name(obj, expected->name);if (!map) {rc = -ENOENT;@@ -845,7 +555,7 @@ static int freeze_task_maps(struct bpf_object *obj)return rc;}-static int init_lsm(bool attach_filesystem_seal)+static int init_lsm(void){struct bpf_link *image_links[SHIRUDO_IMAGE_OBSERVER_HOOK_COUNT] = {};struct bpf_link *image_seal[SHIRUDO_IMAGE_SEAL_HOOK_COUNT] = {};@@ -853,7 +563,7 @@ static int init_lsm(bool attach_filesystem_seal)struct bpf_link *identity_links[ARRAY_SIZE(shirudo_observer_guards)] = {};struct bpf_link *mount_links[ARRAY_SIZE(shirudo_mount_guards)] = {};struct bpf_program *p_bpf, *p_bpf_map, *p_unlink, *p_setx, *p_rmx;- const struct shirudo_embedded_bpf *blob = bpf2_blob(image_mode);+ const struct shirudo_embedded_bpf *blob = bpf2_blob();struct bpf_link *seal_links[SHIRUDO_SEAL_HOOK_COUNT] = {};struct bpf_link *link_bpf = NULL, *link_bpf_map = NULL;struct bpf_object *obj = NULL, *seal_obj = NULL;@@ -910,22 +620,13 @@ static int init_lsm(bool attach_filesystem_seal)goto out;}id.sb_dev = kernel_dev_t(st.st_dev);- if (image_mode) {- if (stat(SHIRUDO_RUN_DIR, &st)) {- err = -errno;- goto out;- }- id.run_dev = kernel_dev_t(st.st_dev);+ if (stat(SHIRUDO_RUN_DIR, &st)) {+ err = -errno;+ goto out;}+ id.run_dev = kernel_dev_t(st.st_dev);- /*- * Ordinary mode records executable device/inode identities. Image mode- * instead records the authenticated runtime root for exec task marks.- * Both distinguish authorized `upatch unload` from an attacker's `rm`.- */- err = identity_executables(&id);- if (err)- goto out;+ memcpy(id.runtime_root, shirudo_image_runtime_root(), 32);/** The trust anchor lsm/bpf_prog_load enforces: only a load the@@ -934,18 +635,11 @@ static int init_lsm(bool attach_filesystem_seal)* enrolled the cert and sealed the keyring, so this resolves the* serial the kernel records for a load that names it as keyring_id.*/- if (bpf_keyring_mode) {- keyring = keyring_bpf_serial();- id.keyring_type = SHIRUDO_SIG_KEYRING_BPF;- } else {- keyring = keyring_find(SHIRUDO_KEYRING_NAME);- id.keyring_type = SHIRUDO_SIG_KEYRING_USER;- }- if (keyring < 0) {- err = keyring;- pr_err("resolve the %s keyring serial: %s",- bpf_keyring_mode ? "bpf" : "\"shirudo\"",- strerror(-err));+ keyring = keyring_bpf_serial();+ id.keyring_type = SHIRUDO_SIG_KEYRING_BPF;+ if (keyring <= 0) {+ err = keyring ? keyring : -ENOKEY;+ pr_err("resolve the bpf keyring serial: %s", strerror(-err));goto out;}id.keyring_serial = (__s32)keyring;@@ -1006,7 +700,7 @@ static int init_lsm(bool attach_filesystem_seal)err = attach_guard_group(obj, shirudo_observer_guards,ARRAY_SIZE(shirudo_observer_guards),identity_links);- if (!err && image_mode)+ if (!err)err = attach_guard_group(obj, shirudo_image_observer_guards,SHIRUDO_IMAGE_OBSERVER_HOOK_COUNT,image_links);@@ -1066,11 +760,9 @@ static int init_lsm(bool attach_filesystem_seal)if (err)goto out;- if (attach_filesystem_seal) {- err = attach_seal(obj, &seal_obj, seal_links, image_seal);- if (err)- goto out;- }+ err = attach_seal(obj, &seal_obj, seal_links, image_seal);+ if (err)+ goto out;err = attach_guard(p_bpf_map, SHIRUDO_GUARD_BPF_MAP, &link_bpf_map);if (err) {@@ -1090,11 +782,8 @@ static int init_lsm(bool attach_filesystem_seal)goto out;}- pr_info("%s%s attached: lsm/bpf gate live%s",- blob->name,- attach_filesystem_seal ? " + seal" : "",- attach_filesystem_seal ? ", bpffs and /var/lib/shirudo sealed" :- ", filesystem seal unavailable on this kernel");+ pr_info("%s + seal attached: lsm/bpf gate live, writable state sealed",+ blob->name);err = 0;out:for (i = 0; i < (int)ARRAY_SIZE(image_seal); i++) {@@ -1469,8 +1158,6 @@ static int chain_extend_init(struct shirudo_measured_upatch *set, size_t n)static const struct shirudo_help_item init_options[] = {{ "--check-only", "run the pre-flight only; never touch kernel state" },- { "--no-seal",- "skip filesystem sealing when target LSM hooks are not sleepable" },};static const struct shirudo_help_section init_help_sections[] = {@@ -1479,7 +1166,7 @@ static const struct shirudo_help_section init_help_sections[] = {};static const struct shirudo_help init_help = {- .usage = "shirudo init [--check-only] [--no-seal]",+ .usage = "shirudo init [--check-only]",.summary = "Bootstrap enforcement on this target. Idempotent; safe ""to re-run.\nThe pinned link survives the process exit.",.sections = init_help_sections,@@ -1495,14 +1182,14 @@ static void usage(void)/** The initrd phase reads the root's state tree: the archived upatches* to restore, the eventlog to continue, the keys to compare. A- * recursive bind of <sysroot>/var/lib/shirudo at its usual path keeps- * every path the same; it is read-only, so nothing is written there- * before the root file system is up, and it is detached when init- * leaves. The private bpffs is under /run, which systemd carries+ * recursive writable bind of <sysroot>/var/lib/shirudo keeps every path+ * the same and lets config recovery record its selection durably. The+ * bind is detached when init leaves. The private bpffs is under /run, which systemd carries* across the switch.*/static int initrd_enter(void){+ struct statvfs state;char src[PATH_MAX];struct stat st;int rc;@@ -1526,29 +1213,16 @@ static int initrd_enter(void)strerror(errno));return -errno;}- if (image_mode) {- struct statvfs state;-- rc = statvfs(SHIRUDO_VAR_LIB, &state) ? -errno :- (state.f_flag & ST_RDONLY ? -EROFS : 0);- if (rc) {- umount2(SHIRUDO_VAR_LIB, MNT_DETACH);- return rc;- }- pr_info("initrd: %s bound writable at %s for config recovery",- src, SHIRUDO_VAR_LIB);- return 0;- }- if (mount(NULL, SHIRUDO_VAR_LIB, NULL,- MS_BIND | MS_REMOUNT | MS_RDONLY, NULL) != 0) {- rc = -errno;- pr_err("initrd: make %s read-only: %s", SHIRUDO_VAR_LIB,- strerror(errno));+ rc = statvfs(SHIRUDO_VAR_LIB, &state) ? -errno :+ (state.f_flag & ST_RDONLY ? -EROFS : 0);+ if (rc) {umount2(SHIRUDO_VAR_LIB, MNT_DETACH);return rc;}- pr_info("initrd: %s bound read-only at %s", src, SHIRUDO_VAR_LIB);+ pr_info("initrd: %s bound writable at %s for config recovery",+ src, SHIRUDO_VAR_LIB);return 0;+}static void initrd_leave(void)@@ -1558,7 +1232,7 @@ static void initrd_leave(void)strerror(errno));}-static int run_init(bool no_seal)+static int run_init(void){struct shirudo_measured_upatch *set = NULL;size_t n = 0;@@ -1569,65 +1243,39 @@ static int run_init(bool no_seal)return rc;rc = init_keyring();- if (!rc && image_mode)+ if (!rc)rc = shirudo_image_boot_keys_close();if (rc)return rc;- /*- * Label the binary + the /var/lib/shirudo tree BEFORE init_lsm- * attaches seal, so the bulk setxattr is not gated by seal's own- * inode_setxattr hook, and fold in the record an initrd phase left- * for this boot. The initrd itself writes nothing to the tree: its- * root is read-only, and the root's init does both on its re-run.- */- if (initrd_mode && !image_mode) {- pr_info("initrd: labels and the eventlog are left to the root "- "file system's init");- } else {- rc = seal_label_tree();- if (rc) {- pr_warn("init: seal label-walk incomplete: %s "- "(some files may be unsealed)", strerror(-rc));- if (image_mode)- return rc;- }- rc = eventlog_adopt_pending();- if (rc) {- pr_err("init: the initrd's record in %s could not be "- "adopted: %s", eventlog_pending_path(),- strerror(-rc));- return rc;- }- }+ /* Seal writable state before the hooks attach, then adopt the initrd log. */+ rc = seal_label_tree();+ if (rc)+ return rc;+ rc = eventlog_adopt_pending();+ if (rc)+ return rc;- rc = init_lsm(!no_seal);+ rc = init_lsm();+ if (rc)+ return rc;++ rc = seal_label_tree();+ if (rc)+ return rc;+ rc = shirudo_image_runtime_mount();+ if (!rc)+ rc = shirudo_image_boot();+ if (!rc)+ rc = keys_load(SHIRUDO_RUNTIME_DIR "/keys",+ SHIRUDO_KEYS_REVOKED_DIR, true, &enrolled);if (rc)return rc;- if (image_mode) {- rc = seal_label_tree();- if (rc)- return rc;- rc = shirudo_image_runtime_mount();- if (!rc)- rc = shirudo_image_boot();- if (!rc)- rc = keys_load(SHIRUDO_RUNTIME_DIR "/keys",- SHIRUDO_KEYS_REVOKED_DIR,- true, &enrolled);- if (rc)- return rc;- }rc = boot_restore(&set, &n);if (rc) {- if (image_mode)- pr_err("init: boot-restore failed; further changes "- "and attestation remain blocked");- else- pr_err("init: incomplete -- LSM active, but boot-restore "- "failed; re-run `shirudo init` after fixing the "- "upatch");+ pr_err("init: boot-restore failed; further changes "+ "and attestation remain blocked");xfree(set, n * sizeof(*set));return rc;}@@ -1640,24 +1288,18 @@ static int run_init(bool no_seal)return rc;}- if (image_mode) {- rc = shirudo_image_boot_complete();- if (rc)- return rc;- }+ rc = shirudo_image_boot_complete();+ if (rc)+ return rc;- pr_info("init: complete (LSM active%s%s)",- no_seal ? ", filesystem seal skipped" :- ", bpffs sealed against VFS unlink",- initrd_mode ? "; initrd phase, the root file system's init "- "adopts the record" : "");+ pr_info("init: complete (LSM active, writable state sealed)");return 0;}int cmd_init(int argc, const char **argv){struct shirudo_preflight_result res;- bool check_only = false, no_seal = false;+ bool check_only = false;int i, rc;for (i = 1; i < argc; i++) {@@ -1667,10 +1309,6 @@ int cmd_init(int argc, const char **argv)check_only = true;continue;}- if (strcmp(a, "--no-seal") == 0) {- no_seal = true;- continue;- }if (is_help(a)) {print_help(&init_help);return 0;@@ -1686,11 +1324,6 @@ int cmd_init(int argc, const char **argv)pr_err("init: authenticated image settings: %s", strerror(-rc));return 1;}- image_mode = rc > 0;- if (image_mode && no_seal) {- pr_err("init: image mode requires all filesystem and exec guards");- return 1;- }preflight_run(&res);print_preflight(&res);@@ -1705,7 +1338,7 @@ int cmd_init(int argc, const char **argv)if (initrd_mode && initrd_enter())return 1;- rc = run_init(no_seal);+ rc = run_init();shirudo_image_leave();if (initrd_mode)initrd_leave();diff --git a/builtin-keys.c b/builtin-keys.cindex c18c1e74..5d6d3456 100644--- a/builtin-keys.c+++ b/builtin-keys.c@@ -579,7 +579,7 @@ static int keys_add(int argc, const char **argv)return 2;}if (shirudo_image_config_active()) {- pr_err("image mode: add vouched BPF signers to the signed "+ pr_err("target: add vouched BPF signers to the signed ""config selected for boot and reboot to enroll them");return 1;}@@ -672,26 +672,10 @@ out:return rc ? 1 : 0;}-static const struct shirudo_help_item keys_revoke_options[] = {- { "--promote <fingerprint>",- "the member that becomes the primary when the primary goes" },- { "--force",- "revoke the last certificate; nothing verifies until a reboot with a new one" },-};--static const struct shirudo_help_section keys_revoke_sections[] = {- { "options:", keys_revoke_options, ARRAY_SIZE(keys_revoke_options) },-};-static const struct shirudo_help keys_revoke_help = {- .usage = "shirudo keys revoke <fingerprint> [--promote <fingerprint>] [--force]",- .summary = "Stop trusting a certificate: unlink its key from the bpf "- "keyring so the kernel\naccepts nothing it signed from now "- "on, unload and purge every archived upatch\nit signed, "- "and keep it out of the set for good. Measured into the "- "register.",- .sections = keys_revoke_sections,- .n_sections = ARRAY_SIZE(keys_revoke_sections),+ .usage = "shirudo keys revoke",+ .summary = "Local revocation is disabled. Publish revocation records "+ "in a signed config image and activate it with image update.",};static int append_key_event(const char *type, const char *fp_hex,@@ -857,321 +841,17 @@ int shirudo_keys_revoke_image(const __u8 *der, size_t size, bool boot)return rc < 0 ? rc : 0;}-/* Replace the primary with a member: its file becomes cert.der. */-static int promote(const struct shirudo_trusted_cert *c)-{- char vouch[PATH_MAX];- size_t der_sz = 0;- __u8 *der = NULL;- int rc;-- rc = keys_read_der(c->path, &der, &der_sz);- if (rc)- return rc;- rc = keys_write_file(SHIRUDO_CERT_PATH, der, der_sz);- xfree(der, der_sz);- if (rc)- return rc;- if (strsprintf(vouch, sizeof(vouch), "%.*s.vouch",- (int)(strlen(c->path) - 4), c->path) >= 0)- unlink(vouch);- unlink(c->path);- return 0;-}--static const struct shirudo_trusted_cert *-set_find(const struct shirudo_trusted_set *set, const __u8 fp[32])-{- size_t i;-- for (i = 0; i < set->n; i++) {- if (memcmp(set->certs[i].fingerprint, fp, 32) == 0)- return &set->certs[i];- }- return NULL;-}--struct revoke_seen {- const char *fp_hex;- bool found;-};--static int revoke_logged(const char *line, size_t len, void *ctx)-{- struct revoke_seen *r = ctx;- char type[32] = {}, fp[65] = {};- struct shirudo_json_field key[] = {- { "fingerprint", SHIRUDO_JSON_T_STRING, fp, sizeof(fp), 0,- NULL, 0 },- };- struct shirudo_json_field f[] = {- { "type", SHIRUDO_JSON_T_STRING, type, sizeof(type), 0, NULL,- 0 },- { "key", SHIRUDO_JSON_T_OBJECT, NULL, 0, 0, key,- ARRAY_SIZE(key) },- };-- if (!json_parse(line, len, f, ARRAY_SIZE(f)) &&- !strcmp(type, "key_revoke") && !strcmp(fp, r->fp_hex))- r->found = true;- return 0;-}--/*- * A revocation left unmeasured: its marker is from this boot and this- * boot's log does not hold it. One from an earlier boot never had its- * key enrolled in this one, so there is nothing to measure.- */-static bool revoke_unmeasured(const char *marker, const char *fp_hex)-{- struct revoke_seen r = { .fp_hex = fp_hex };- struct timespec up;- struct stat st;-- if (stat(marker, &st) || clock_gettime(CLOCK_BOOTTIME, &up))- return true;- if (st.st_mtime < time(NULL) - up.tv_sec)- return false;- return eventlog_boot_lines(revoke_logged, &r) || !r.found;-}--/* The certificate a revoked marker holds, as a member it once was. */-static int revoked_cert(const __u8 fp[32], const char *marker,- struct shirudo_trusted_cert *c, __u8 **der,- size_t *der_sz)-{- char trusted[PATH_MAX];- int rc;-- rc = keys_read_der(marker, der, der_sz);- if (rc)- return rc;- memset(c, 0, sizeof(*c));- rc = keys_digest_der(*der, *der_sz, c->sha256, c->fingerprint);- if (!rc && memcmp(c->fingerprint, fp, 32))- rc = -EKEYREJECTED;- if (!rc)- rc = keys_path_for(SHIRUDO_KEYS_DIR "/trusted", fp,- ".der", trusted, sizeof(trusted));- if (rc) {- xfree(*der, *der_sz);- *der = NULL;- return rc;- }- strscpy(c->path, trusted, sizeof(c->path));- return 0;-}-static int keys_revoke(int argc, const char **argv){- const struct shirudo_trusted_cert *victim, *heir = NULL;- const char *fp_arg = NULL, *promote_arg = NULL;- struct shirudo_trusted_cert gone;- __u8 fp[32], promote_fp[32];- struct shirudo_trusted_set set;- __u8 *der = NULL;- struct shirudo_identity id;- char revoked[PATH_MAX];- key_serial_t ring, key;- size_t der_sz = 0, n_unloaded = 0;- bool force = false, resume = false;- char fp_hex[65];- int rc, purged;-- for (int a = 1; a < argc; a++) {- if (!strcmp(argv[a], "--force")) {- force = true;- } else if (!strcmp(argv[a], "--promote") && a + 1 < argc) {- promote_arg = argv[++a];- } else if (!strncmp(argv[a], "--promote=", 10)) {- promote_arg = argv[a] + 10;- } else if (is_help(argv[a])) {- print_help(&keys_revoke_help);- return 0;- } else if (argv[a][0] == '-') {- pr_err("keys revoke: unknown option '%s'", argv[a]);- return 2;- } else if (!fp_arg) {- fp_arg = argv[a];- } else {- pr_err("keys revoke: unexpected argument '%s'",- argv[a]);- return 2;- }- }- if (!fp_arg) {+ if (argc == 2 && is_help(argv[1])) {print_help(&keys_revoke_help);- return 2;- }- if (strlen(fp_arg) != 64 || hex_decode(fp_arg, fp, 32)) {- pr_err("keys revoke: a fingerprint is 64 hex digits (see "- "`shirudo keys`)");- return 2;- }- if (promote_arg && (strlen(promote_arg) != 64 ||- hex_decode(promote_arg, promote_fp, 32))) {- pr_err("keys revoke: --promote takes a fingerprint");- return 2;- }- if (shirudo_image_config_active()) {- pr_err("image mode: put revocation records in a signed config "- "image, then run `shirudo image update`");- return 1;- }- hex_encode(fp, 32, fp_hex);-- /*- * Only the bpf keyring can lose one key and keep the others; the- * staging keyring holds the primary alone and the rotation guide- * is the way out of it.- */- if (identity_read(&id) ||- id.keyring_type != SHIRUDO_SIG_KEYRING_BPF) {- pr_err("keys revoke: the certificates are not enrolled in the "- "bpf keyring this boot; rotate the key instead");- return 1;- }- ring = keyring_bpf_serial();- if (ring <= 0) {- pr_err("keys revoke: no bpf keyring: %s", strerror(-ring));- return 1;- }- rc = keys_load(SHIRUDO_KEYS_DIR, NULL, true, &set);- if (rc) {- pr_err("keys revoke: read the trusted set: %s", strerror(-rc));- return 1;- }- victim = set_find(&set, fp);- if (!victim && !keys_is_revoked(SHIRUDO_KEYS_REVOKED_DIR, fp)) {- pr_err("keys revoke: %s is not a trusted certificate", fp_hex);- return 1;- }- if (!victim) {- /*- * Revoked before: a run that failed after the marker left- * the unlink, the measurement or the purge to this one.- */- rc = keys_path_for(SHIRUDO_KEYS_REVOKED_DIR, fp,- ".der", revoked, sizeof(revoked));- if (!rc)- rc = revoked_cert(fp, revoked, &gone, &der, &der_sz);- if (rc) {- pr_err("keys revoke: read the revoked certificate: %s",- strerror(-rc));- return 1;- }- pr_info("keys revoke: %s is already revoked; finishing it",- fp_hex);- victim = &gone;- resume = true;- goto finish;- }- if (set.n == 1 && !force) {- pr_err("keys revoke: %s is the only certificate; revoking it "- "leaves nothing to verify against until a reboot with a "- "new one (--force to do it anyway)", fp_hex);- return 1;- }- if (victim->primary && set.n > 1) {- heir = promote_arg ? set_find(&set, promote_fp) : NULL;- if (!heir || heir == victim) {- pr_err("keys revoke: %s is the primary certificate; "- "name another member to take its place with "- "--promote <fingerprint>", fp_hex);- return 1;- }- }- rc = keys_read_der(victim->path, &der, &der_sz);- if (rc) {- pr_err("keys revoke: read %s: %s", victim->path, strerror(-rc));- return 1;- }-- /* The marker first: whatever happens next, no boot enrols it again. */- rc = seal_mkdir_p(SHIRUDO_KEYS_REVOKED_DIR, 0700);- if (rc) {- pr_err("keys revoke: mkdir %s: %s", SHIRUDO_KEYS_REVOKED_DIR,- strerror(-rc));- goto out;- }- rc = keys_path_for(SHIRUDO_KEYS_REVOKED_DIR, fp, ".der",- revoked, sizeof(revoked));- if (!rc)- rc = keys_write_file(revoked, der, der_sz);- if (rc) {- pr_err("keys revoke: write %s: %s", revoked, strerror(-rc));- goto out;- }- if (heir) {- rc = promote(heir);- if (rc) {- pr_err("keys revoke: promote: %s", strerror(-rc));- goto out;- }- }--finish:- /* The kernel: from this moment nothing it signed verifies. */- key = keyring_find_cert(ring, der, der_sz);- if (key == -ENOKEY) {- pr_warn("keys revoke: %s is not in the bpf keyring this boot",- fp_hex);- } else if (key < 0) {- pr_err("keys revoke: find the key in the bpf keyring: %s",- strerror(-key));- rc = key;- goto out;- } else {- rc = keyring_unlink_key(ring, key);- if (rc) {- pr_err("keys revoke: unlink key %d from the bpf "- "keyring %d: %s; the key is marked revoked and "- "leaves at the next boot", (int)key, (int)ring,- strerror(-rc));- goto out;- }- pr_info("keys revoke: key %d unlinked from the bpf keyring",- (int)key);- }-- if (!resume || revoke_unmeasured(revoked, fp_hex)) {- rc = measure_revoke(victim, fp_hex);- if (rc)- goto out;- }-- /* The archive, then the files the set was read from. */- purged = purge_signed_by(der, der_sz, &n_unloaded);- if (purged < 0) {- rc = purged;- pr_err("keys revoke: not every upatch it signed could be "- "purged; run `shirudo upatch list`");- }- if (!victim->primary) {- char vouch[PATH_MAX];-- if (strsprintf(vouch, sizeof(vouch), "%.*s.vouch",- (int)(strlen(victim->path) - 4), victim->path) >= 0)- unlink(vouch);- unlink(victim->path);+ return 0;}- pr_audit("keys revoke: fingerprint=%s unloaded=%zu purged=%d "- "promoted=%s", fp_hex, n_unloaded, purged < 0 ? -1 : purged,- heir ? promote_arg : "-");- printf("revoked %s\n", fp_hex);- if (purged > 0)- printf("purged %d upatch%s it signed, %zu of them loaded\n",- purged, purged == 1 ? "" : "es", n_unloaded);- if (heir)- printf("primary is now %s\n", promote_arg);- pr_info("keys revoke: regenerate the initrd (shirudo-gen-initrd) so "- "the next boot enrols the set without it");-out:- xfree(der, der_sz);- return rc ? 1 : 0;+ pr_err("keys revoke: put revocation records in a signed config image, "+ "then run `shirudo image update`");+ return 1;}-/* Signing needs the private key, so the operator helper does it. */static int keys_vouch(int argc, const char **argv){const char **helper_argv;@@ -1293,13 +973,8 @@ static int keys_show(int argc, const char **argv)}id_rc = identity_read(&id);- if (id_rc == 0 && id.keyring_type == SHIRUDO_SIG_KEYRING_BPF) {- label = ".bpf";- ring = keyring_bpf_serial();- } else {- label = SHIRUDO_KEYRING_NAME;- ring = keyring_find(SHIRUDO_KEYRING_NAME);- }+ label = ".bpf";+ ring = id_rc ? -ENOKEY : keyring_bpf_serial();if (ring >= 0) {if (shown)printf("\n");@@ -1323,7 +998,7 @@ static int keys_show(int argc, const char **argv)pr_err("no shirudo key material on this host: no %s and no ""\"%s\" kernel keyring -- run `shirudo genkey` ""(operator) or `shirudo init` (target)",- cert_path, SHIRUDO_KEYRING_NAME);+ cert_path, ".bpf");rc = 1;}return rc;diff --git a/builtin-status.c b/builtin-status.cindex 7b5e1384..e1735b24 100644--- a/builtin-status.c+++ b/builtin-status.c@@ -433,7 +433,7 @@ static size_t build_upatches_table(struct dirent **ups, int n_ups,/* Status runs on the host, so reachable and parsed are trivially true. */static void local_cond_input(struct shirudo_conditions_input *in,int lsm_attached, int keyring_present,- const char *keyring_mode, int staging,+ const char *keyring_mode,__s64 keyring_serial,__s64 keyring_serial_pinned,int ks_seen, int ksp_seen, int ksm_seen,@@ -447,7 +447,6 @@ static void local_cond_input(struct shirudo_conditions_input *in,.lsm_attached = lsm_attached,.keyring_present = keyring_present,.keyring_mode = keyring_mode,- .staging = staging,.keyring_serial = keyring_serial,.keyring_serial_pinned = keyring_serial_pinned,.keyring_serial_matches = ks_matches,@@ -624,20 +623,10 @@ int cmd_status(int argc, const char **argv)* of that kind is what a load would name, and the two must agree.*/keyring_pinned = identity_read(&ident);- if (keyring_pinned == 0) {+ if (!keyring_pinned)keyring_pinned = ident.keyring_serial;- if (ident.keyring_type == SHIRUDO_SIG_KEYRING_BPF) {- keyring_serial = keyring_bpf_serial();- keyring_mode = "bpf";- } else {- keyring_serial =- keyring_find(SHIRUDO_KEYRING_NAME);- keyring_mode = "user";- }- } else {- keyring_serial = keyring_find(SHIRUDO_KEYRING_NAME);- keyring_mode = keyring_serial >= 0 ? "user" : "none";- }+ keyring_serial = keyring_pinned >= 1 ? keyring_bpf_serial() : -ENOKEY;+ keyring_mode = keyring_serial >= 0 ? "bpf" : "none";keyring_present = keyring_serial >= 0;/** The certificates the keyring of that kind would have been given,@@ -653,13 +642,7 @@ int cmd_status(int argc, const char **argv)&n) == 0)certs_enrolled = (int)n;}- /*- * Enforcing with the certificate in the user keyring is staging:- * the kernel's bpf keyring was sealed empty at boot, so the trust- * anchor is what init could seal from the root file system.- */- mode = !lsm_attached ? "inactive" :- !strcmp(keyring_mode, "bpf") ? "production" : "staging";+ mode = lsm_attached ? "production" : "inactive";is_operator = access(SHIRUDO_SIGNING_KEY_PATH, F_OK) == 0;cert_present = access(shirudo_cert_path(), F_OK) == 0;is_target = cert_present || lsm_attached || keyring_present;@@ -749,7 +732,6 @@ int cmd_status(int argc, const char **argv)local_cond_input(&cin, lsm_attached, keyring_present,keyring_mode,- lsm_attached && strcmp(keyring_mode, "bpf") != 0,keyring_serial,keyring_pinned >= 1 ? keyring_pinned : 0,keyring_present, keyring_pinned >= 1,@@ -1040,7 +1022,6 @@ int cmd_status(int argc, const char **argv)local_cond_input(&cin, lsm_attached, keyring_present,keyring_mode,- lsm_attached && strcmp(keyring_mode, "bpf") != 0,keyring_serial,keyring_pinned >= 1 ? keyring_pinned : 0,keyring_present, keyring_pinned >= 1,@@ -1060,12 +1041,7 @@ int cmd_status(int argc, const char **argv)printf("Host: %s\n", hostname);printf("Roles: %s\n",roles_str(is_target, is_operator));- if (!strcmp(mode, "staging"))- printf("Mode: STAGING (certificate in the "- "user keyring; boot with bpf.keyring_unsealed=1 "- "and run init from the initrd)\n");- else- printf("Mode: %s\n", mode);+ printf("Mode: %s\n", mode);if (reason[0])printf("Status: %s (%s)\n", verdict,reason);@@ -1092,8 +1068,7 @@ int cmd_status(int argc, const char **argv)tamper_meaning(i));}- printf("\nKeyring (%s):\n", !strcmp(keyring_mode, "bpf") ?- ".bpf" : SHIRUDO_KEYRING_NAME);+ printf("\nKeyring (.bpf):\n");if (keyring_present)printf(" %-22s %lld\n", "Serial:",(__s64)keyring_serial);diff --git a/builtin-upatch.c b/builtin-upatch.cindex b9984bb9..a3640192 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -1457,14 +1457,7 @@ static int get_pin_payload(const char *name, __u8 out[32])return 0;}-/*- * The anchor init froze into the identity map: the kernel's bpf keyring- * by its spec, or a staged user keyring by its own serial rather than- * KEY_SPEC_USER_KEYRING, whose search recurses through all of _uid.0,- * where root can link a certificate of their own. The frozen serial- * also catches a same-named keyring substituted for the sealed one;- * *serial is the keyring's own serial either way, for the logs.- */+/* Resolve the kernel BPF keyring and compare it with the frozen record. */static int trust_anchor_keyring(key_serial_t *keyring, key_serial_t *serial,struct shirudo_op_report *rep){@@ -1480,38 +1473,15 @@ static int trust_anchor_keyring(key_serial_t *keyring, key_serial_t *serial,"no trust anchor frozen this boot");return -1;}- if (id.keyring_type == SHIRUDO_SIG_KEYRING_BPF) {- ring = keyring_bpf_serial();- if (ring != id.keyring_serial) {- pr_err("the kernel's bpf keyring (%d) is not the one "- "init sealed (%d)", (int)ring,- (int)id.keyring_serial);- rep_fail(rep, "pre_flight", "tampered", 0,- "bpf keyring is not the frozen anchor");- return -1;- }- *keyring = KEY_SPEC_BPF_KEYRING;- *serial = ring;- return 0;- }- ring = keyring_find(SHIRUDO_KEYRING_NAME);- if (ring < 0) {- pr_err("keyring \"%s\" not found: %s; the anchor frozen this "- "boot was serial %d", SHIRUDO_KEYRING_NAME,- strerror(-ring), (int)id.keyring_serial);- rep_fail(rep, "pre_flight", "tampered", -ring,- "the sealed keyring is gone");- return -1;- }- if (ring != id.keyring_serial) {- pr_err("keyring \"%s\" serial %d != frozen %d -- a same-named "- "keyring was substituted", SHIRUDO_KEYRING_NAME,- (int)ring, (int)id.keyring_serial);+ ring = keyring_bpf_serial();+ if (id.keyring_type != SHIRUDO_SIG_KEYRING_BPF ||+ ring <= 0 || ring != id.keyring_serial) {+ pr_err("the kernel BPF keyring differs from the frozen anchor");rep_fail(rep, "pre_flight", "tampered", 0,- "keyring serial mismatch (substituted keyring)");+ "BPF keyring is not the frozen anchor");return -1;}- *keyring = ring;+ *keyring = KEY_SPEC_BPF_KEYRING;*serial = ring;return 0;}diff --git a/completion/shirudo.bash b/completion/shirudo.bashindex 84d39c76..67a9aa46 100644--- a/completion/shirudo.bash+++ b/completion/shirudo.bash@@ -126,7 +126,7 @@ _shirudo()fi;;init)- COMPREPLY=( $(compgen -W "--check-only --no-seal" -- "$cur") )+ COMPREPLY=( $(compgen -W "--check-only" -- "$cur") );;status)COMPREPLY=( $(compgen -W "--json \diff --git a/include/shirudo_enforcement.h b/include/shirudo_enforcement.hnew file mode 100644index 00000000..48a5e408--- /dev/null+++ b/include/shirudo_enforcement.h@@ -0,0 +1,10 @@+/* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */+/* Copyright (c) 2026 Authors of shirudo */++#ifndef SHIRUDO_ENFORCEMENT_H+#define SHIRUDO_ENFORCEMENT_H++#define SHIRUDO_PR_GET_ENFORCEMENT 0x53520001+#define SHIRUDO_PR_ENFORCING_ERR 1002++#endif /* SHIRUDO_ENFORCEMENT_H */diff --git a/include/shirudo_mode.h b/include/shirudo_mode.hdeleted file mode 100644index 03efc610..00000000--- a/include/shirudo_mode.h+++ /dev/null@@ -1,15 +0,0 @@-/* 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/systemd/shirudo-initrd.service.in b/systemd/shirudo-initrd.service.inindex 2044c91b..154b770f 100644--- a/systemd/shirudo-initrd.service.in+++ b/systemd/shirudo-initrd.service.in@@ -15,9 +15,8 @@# file system's shirudo.service re-runs init and adopts it.## dracut's shirudo module installs and enables this unit; it is never-# started in the root file system. Image mode requires writable state-# under /sysroot/var/lib/shirudo for durable config recovery; legacy mode-# reads it through a read-only bind.+# started in the root file system. It requires writable state under+# /sysroot/var/lib/shirudo for durable config recovery.[Unit]Description=shirudo: bring up the BPF LSM and bind the register from the initrddiff --git a/systemd/shirudo.service.in b/systemd/shirudo.service.inindex 6964eda9..6e56c708 100644--- a/systemd/shirudo.service.in+++ b/systemd/shirudo.service.in@@ -8,20 +8,10 @@# runs `shirudo init` as early as a filesystem-dependent service can,# inside the sysinit phase.#-# After an initrd that ran shirudo-initrd.service the keyring is sealed,-# the LSM is attached with its pins under /run and the register is-# bound; this run finds all of that, labels the state tree, adopts the-# record the initrd left under /run into the eventlog and stops. That-# is the production configuration. Without such an initrd this run does-# everything itself: the bpf keyring was sealed empty at boot, so the-# certificate is staged in the user keyring until the next reboot, and-# the register binds after the initrd, which `status` and the evidence-# both say.-#-# Install: `make install` drops this file; enable with-# systemctl enable shirudo.service-# Extra init options go in /etc/shirudo/init.env, for example:-# SHIRUDO_INIT_ARGS=--no-seal+# The initrd loads the authenticated runtime and signed config before+# switch-root. Install the runtime-service.conf drop-in generated with the+# runtime image; it starts the trusted loader and executable for this phase.+# This run adopts the initrd eventlog record and checks the existing state.[Unit]Description=shirudo: bring up the BPF LSM and re-apply signed upatches@@ -31,15 +21,11 @@ After=systemd-pcrnvdone.serviceBefore=sysinit.target shutdown.targetConflicts=shutdown.targetRequiresMountsFor=/var/lib/shirudo+ConditionPathIsMountPoint=/run/shirudo/runtime[Service]Type=oneshotRemainAfterExit=yes-# Use the normal per-uid kernel keyring, not a service-private one: when-# the cert is staged in the user keyring, `shirudo init` links it under-# KEY_SPEC_USER_KEYRING and later target-side commands search it from-# other processes. systemd's default KeyringMode=private isolates that-# keyring and the cert add fails with EACCES (no possession).KeyringMode=inheritEnvironmentFile=-/etc/shirudo/init.envExecStart=@bindir@/shirudo init $SHIRUDO_INIT_ARGSdiff --git a/tools/gen-initrd/install.sh b/tools/gen-initrd/install.shindex 25af561d..d1df9a92 100755--- a/tools/gen-initrd/install.sh+++ b/tools/gen-initrd/install.sh@@ -6,7 +6,7 @@# place the operator's certificate, put the kernel parameter the bpf# keyring needs on the boot command line and rebuild the initrd with# shirudo inside, so that the next reboot comes up in the production-# configuration (Documentation/initrd.rst). With --now, enforcement+# configuration (Documentation/initrd.rst). Runtime and signed config# starts right away in the staging configuration until that reboot.## curl -fsSL https://raw.githubusercontent.com/shirudo-dev/shirudo/main/tools/gen-initrd/install.sh |@@ -23,7 +23,6 @@ usage: install.sh [options]--version <tag> release to install (default: latest); also SHIRUDO_VERSION--package <file> install this package instead of downloading one;also SHIRUDO_PACKAGE- --now start enforcement now, staged until the reboot--no-cmdline leave the boot command line alone--no-initrd do not rebuild the initrd--switch-to-dracut on a system using initramfs-tools, install dracut@@ -34,13 +33,12 @@ EOF}REPO=${SHIRUDO_REPO:-shirudo-dev/shirudo}-CMDLINE_PARAM=bpf.keyring_unsealed=1+CMDLINE_PARAM="bpf.keyring_unsealed=1 dm_verity.keyring_unsealed=1"CERT_DST=/var/lib/shirudo/keys/cert.dercert=${SHIRUDO_CERT:-}version=${SHIRUDO_VERSION:-}package=${SHIRUDO_PACKAGE:-}-now=0do_cmdline=1do_initrd=1switch_to_dracut=0@@ -51,7 +49,6 @@ while [ $# -gt 0 ]; do--cert) cert=${2:?}; shift 2 ;;--version) version=${2:?}; shift 2 ;;--package) package=${2:?}; shift 2 ;;- --now) now=1; shift ;;--no-cmdline) do_cmdline=0; shift ;;--no-initrd) do_initrd=0; shift ;;--switch-to-dracut) switch_to_dracut=1; shift ;;@@ -205,8 +202,7 @@ if [ -n "$cert" ]; thenelif [ ! -s "$CERT_DST" ]; thensay "no certificate given and none at $CERT_DST; the initrd cannot be built without one"do_initrd=0- now=0-fi+ fi# ---- the boot command line -----------------------------------------------@@ -287,17 +283,10 @@ if [ "$do_initrd" = 1 ]; thenbuild_initrd || initrd_ok=0fi-# ---- now -------------------------------------------------------------------if [ "$now" = 1 ]; then- say "starting enforcement now; the certificate is staged in the user keyring until the reboot"- run systemctl start shirudo.service-fi-# ---- summary -------------------------------------------------------------if [ "$initrd_ok" = 1 ] && [ "$do_initrd" = 1 ] && [ "$do_cmdline" = 1 ]; then- say "done; reboot to bring shirudo up in the production configuration"+ say "done; provision runtime and signed config images before rebuilding the UKI and rebooting"elsesay "done; the production configuration needs the initrd rebuilt and $CMDLINE_PARAM on the command line"fidiff --git a/tools/gen-initrd/module-setup.sh b/tools/gen-initrd/module-setup.shindex b152c1a9..7a07f8f2 100755--- a/tools/gen-initrd/module-setup.sh+++ b/tools/gen-initrd/module-setup.sh@@ -65,11 +65,7 @@ depends() {installkernel() {hostonly='' instmods tpm tpm_tis_core tpm_tis tpm_crb- local conf=${shirudo_conf:-$dracutsysrootdir/etc/shirudo/shirudo.conf}-- if [[ -r $conf ]] && grep -Eq '^[[:space:]]*runtime[.]image[[:space:]]*=' "$conf"; then- hostonly='' instmods dm_mod dm_verity loop ext4- fi+ hostonly='' instmods dm_mod dm_verity loop ext4}install() {@@ -81,8 +77,8 @@ install() {conf=${shirudo_conf:-$dracutsysrootdir/etc/shirudo/shirudo.conf}unit=${shirudo_unit:-$dracutsysrootdir$systemdsystemunitdir/shirudo-initrd.service}- # The root's copy of this binary, at this path, is the identity init- # records for the bpffs guards; the initrd must run it from there.+ # This authenticated initrd binary bootstraps the runtime image. Later+ # invocations use only executables and libraries from that image.inst_binary "$bin" "$path" || return 1# bpm key keeps cert.der as a symlink into keys/; the initrd gets# the file, not a link into a tree it does not have.@@ -99,9 +95,8 @@ install() {"/etc/shirudo/keys/trusted/${f##*/}" || return 1donefi- if [[ -r $conf ]]; then- inst_simple "$conf" /etc/shirudo/shirudo.conf || return 1- fi+ [[ -r $conf ]] || return 1+ inst_simple "$conf" /etc/shirudo/shirudo.conf || return 1# dracut keeps the owner of what it copies, and init refuses keys# and configuration another user could change.chown -R 0:0 "$initdir/etc/shirudo" || return 1diff --git a/tools/gen-initrd/shirudo-gen-initrd b/tools/gen-initrd/shirudo-gen-initrdindex f0164f70..a392d4d9 100755--- a/tools/gen-initrd/shirudo-gen-initrd+++ b/tools/gen-initrd/shirudo-gen-initrd@@ -47,7 +47,7 @@ say()printf 'shirudo-gen-initrd: %s\n' "$*"}-CMDLINE_PARAM=bpf.keyring_unsealed=1+CMDLINE_PARAM="bpf.keyring_unsealed=1 dm_verity.keyring_unsealed=1"MODULE_DIR_INSTALLED=/usr/lib/dracut/modules.d/90shirudoUNIT_INSTALLED=/usr/lib/systemd/system/shirudo-initrd.service@@ -93,12 +93,6 @@ while [ $# -gt 0 ]; doesacdone-bootstrap_settings=${bootstrap_conf:-/etc/shirudo/shirudo.conf}-if [ -r "$bootstrap_settings" ] &&- grep -Eq '^[[:space:]]*runtime\.(image|root_hash)[[:space:]]*=' "$bootstrap_settings"; then- CMDLINE_PARAM+=" dm_verity.keyring_unsealed=1"-fi-if [ "$print_cmdline" = 1 ]; thenprintf '%s\n' "$CMDLINE_PARAM"exit 0@@ -126,6 +120,7 @@ check_initrd()[ -s "$img" ] || die "no initrd at $img"listing=$(lsinitrd "$img" 2>/dev/null) || die "lsinitrd cannot read $img"for want in "etc/shirudo/keys/cert.der" \+ "etc/shirudo/shirudo.conf" \"usr/lib/systemd/system/shirudo-initrd.service" \"initrd.target.wants/shirudo-initrd.service" \"systemd-pcrphase-initrd.service" \diff --git a/util/conditions.c b/util/conditions.cindex aa52c4f7..0f14714f 100644--- a/util/conditions.c+++ b/util/conditions.c@@ -28,17 +28,13 @@ size_t conditions(const struct shirudo_conditions_input *in,n++;}if (n < cap) {- const char *kind = in->keyring_mode &&- !strcmp(in->keyring_mode, "bpf") ?- "bpf" : "user";-c[n].type = "KeyringSealed";if (!in->keyring_present && in->ksp_seen) {c[n].status = "ALERT";snprintf(c[n].detail, sizeof(c[n].detail),- "pin serial=%lld, the %s keyring is gone "+ "pin serial=%lld, the BPF keyring is gone ""(removed?)",- (__s64)in->keyring_serial_pinned, kind);+ (__s64)in->keyring_serial_pinned);} else if (!in->keyring_present) {c[n].status = "WARN";snprintf(c[n].detail, sizeof(c[n].detail),@@ -49,18 +45,11 @@ size_t conditions(const struct shirudo_conditions_input *in,"pin serial=%lld != live serial=%lld (swapped?)",(__s64)in->keyring_serial_pinned,(__s64)in->keyring_serial);- } else if (in->staging) {- c[n].status = "WARN";- snprintf(c[n].detail, sizeof(c[n].detail),- "staging mode: serial=%lld in the user "- "keyring; boot with bpf.keyring_unsealed=1 "- "and run init from the initrd",- (__s64)in->keyring_serial);} else {c[n].status = "OK";snprintf(c[n].detail, sizeof(c[n].detail),- "serial=%lld in the %s keyring, %s",- (__s64)in->keyring_serial, kind,+ "serial=%lld in the BPF keyring, %s",+ (__s64)in->keyring_serial,in->ksp_seen ? "pin matches": "no pin recorded");}diff --git a/util/conditions.h b/util/conditions.hindex a182c8de..86a83686 100644--- a/util/conditions.h+++ b/util/conditions.h@@ -25,8 +25,7 @@ struct shirudo_conditions_input {int lsm_attached; /* 0/1 */int keyring_present;- const char *keyring_mode; /* "bpf", "user" or "none" */- int staging; /* enforcing with the cert in the user keyring */+ const char *keyring_mode; /* "bpf" or "none" */__s64 keyring_serial;__s64 keyring_serial_pinned;int keyring_serial_matches; /* 0/1 if ksm_seen */diff --git a/util/identity.c b/util/identity.cindex ba748072..ba6863ce 100644--- a/util/identity.c+++ b/util/identity.c@@ -17,20 +17,18 @@#include <shirudo/misc.h>#include "identity.h"-#include "shirudo_mode.h"+#include "shirudo_enforcement.h"#include "paths.h"-int shirudo_identity_mode(void)+int shirudo_enforcement_active(void){- int mode = prctl(SHIRUDO_PR_GET_MODE, 0, 0, 0, 0);+ int rc = prctl(SHIRUDO_PR_GET_ENFORCEMENT, 0, 0, 0, 0);- if (mode >= 0)+ if (rc >= 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;+ if (errno == SHIRUDO_PR_ENFORCING_ERR)+ return 1;+ return errno == EINVAL || errno == ENOSYS ? 0 : -errno;}int identity_read(struct shirudo_identity *out)diff --git a/util/identity.h b/util/identity.hindex 007b87b6..4b60791a 100644--- a/util/identity.h+++ b/util/identity.h@@ -11,7 +11,6 @@#include <shirudo/misc.h>/* The keyring kinds the kernel names in a verdict (enum bpf_sig_keyring). */-#define SHIRUDO_SIG_KEYRING_USER 4#define SHIRUDO_SIG_KEYRING_BPF 5/* The most keys the anchor keyring holds; util/keys.h bounds the set. */@@ -20,19 +19,12 @@/* The frozen ABI shared with bpf/lib/shirudo_identity.h. */struct shirudo_identity {__u32 sb_dev;- __u32 exe_dev;- __u64 exe_ino;- __s32 keyring_serial;+ __u32 run_dev;+ __s32 keyring_serial;__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];+ __s32 key_serials[SHIRUDO_IDENTITY_KEYS_MAX];+ __u8 runtime_root[32];};#define SHIRUDO_TASK_TRUSTED 1@@ -69,14 +61,15 @@ _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,+_Static_assert(sizeof(struct shirudo_identity) == 116,"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);+/* 1 when enforcement is active, 0 before init, otherwise -errno. */+int shirudo_enforcement_active(void);int identity_read(struct shirudo_identity *out);#endif /* SHIRUDO_UTIL_IDENTITY_H */diff --git a/util/image_guards.c b/util/image_guards.cindex 709a8a30..70b9d849 100644--- a/util/image_guards.c+++ b/util/image_guards.c@@ -21,7 +21,7 @@#include "log.h"#include "macros.h"#include "paths.h"-#include "shirudo_mode.h"+#include "shirudo_enforcement.h"#include "tamper.h"const struct shirudo_image_guard@@ -150,11 +150,11 @@ int shirudo_config_root_begin(const __u8 root[32],.map_fd = -1,.task_fd = -1,};- mode = shirudo_identity_mode();- if (mode == SHIRUDO_MODE_NONE)+ mode = shirudo_enforcement_active();+ if (!mode)return 0;- if (mode != SHIRUDO_MODE_IMAGE)- return mode < 0 ? mode : -EPERM;+ if (mode < 0)+ return mode;guard->map_fd = bpf_obj_get(map->pin);if (guard->map_fd < 0)return -errno;diff --git a/util/image_runtime.c b/util/image_runtime.cindex b292293d..09cddd84 100644--- a/util/image_runtime.c+++ b/util/image_runtime.c@@ -44,7 +44,7 @@#include "macros.h"#include "paths.h"#include "seal.h"-#include "shirudo_mode.h"+#include "shirudo_enforcement.h"#include "tpm.h"struct image_context {@@ -94,7 +94,7 @@ static int reject_overrides(void)for (i = 0; i < ARRAY_SIZE(names); i++) {if (getenv(names[i])) {- pr_err("image mode: bootstrap override %s is forbidden",+ pr_err("bootstrap override %s is forbidden",names[i]);return -EPERM;}@@ -109,27 +109,21 @@ int shirudo_image_bootstrap_load(void)if (image.loaded)return 1;- mode = shirudo_identity_mode();+ mode = shirudo_enforcement_active();if (mode < 0)return mode;rc = identity_read(&identity);- if (mode != SHIRUDO_MODE_NONE && rc)+ if (mode && rc)return rc;- if (!rc && mode != (identity.runtime_mode ?- SHIRUDO_MODE_IMAGE : SHIRUDO_MODE_LEGACY))+ if (!rc && !mode)return -EPROTO;- if (!rc && identity.runtime_mode) {+ if (!rc) {memcpy(image.bootstrap.runtime_root, identity.runtime_root, 32);image.bootstrap.have_root = true;rc = load_runtime_config(false);} else {rc = shirudo_image_bootstrap_read(SHIRUDO_CONF_PATH,&image.bootstrap, true);- if (rc == -ENOENT)- return 0;- if (!rc && !image.bootstrap.runtime[0] &&- !image.bootstrap.have_root)- return 0;if (!rc &&(!image.bootstrap.runtime[0] || !image.bootstrap.have_root))rc = -EINVAL;@@ -884,19 +878,16 @@ int shirudo_image_enter(const char *command, int argc, const char **argv)!strcmp(command, "build") || !strcmp(command, "help") ||!strcmp(command, "version"))return 0;- mode = shirudo_identity_mode();+ mode = shirudo_enforcement_active();if (mode < 0)return mode;rc = identity_read(&identity);- if (mode != SHIRUDO_MODE_NONE && rc)+ if (mode && rc)return rc;if (rc)return rc == -ENOENT || rc == -EPERM || rc == -EACCES ? 0 : rc;- if (mode != (identity.runtime_mode ?- SHIRUDO_MODE_IMAGE : SHIRUDO_MODE_LEGACY))+ if (!mode)return -EPROTO;- if (!identity.runtime_mode)- return 0;rc = shirudo_image_bootstrap_load();if (rc < 0)return rc;diff --git a/util/keyring.c b/util/keyring.cindex 726658ee..6a686889 100644--- a/util/keyring.c+++ b/util/keyring.c@@ -24,25 +24,6 @@#include "malloc.h"#include "paths.h"-#ifndef KEY_SPEC_USER_KEYRING-#define KEY_SPEC_USER_KEYRING (-4)-#endif--#define KEY_POS_VIEW 0x01000000-#define KEY_POS_READ 0x02000000-#define KEY_POS_WRITE 0x04000000-#define KEY_POS_SEARCH 0x08000000-#define KEY_POS_LINK 0x10000000-#define KEY_POS_SETATTR 0x20000000-#define KEY_USR_VIEW 0x00010000-#define KEY_USR_READ 0x00020000-#define KEY_USR_WRITE 0x00040000-#define KEY_USR_SEARCH 0x00080000-#define KEY_USR_SETATTR 0x00200000--#ifndef KEY_SPEC_SESSION_KEYRING-#define KEY_SPEC_SESSION_KEYRING (-3)-#endif#ifndef KEYCTL_RESTRICT_KEYRING#define KEYCTL_RESTRICT_KEYRING 29#endif@@ -58,88 +39,6 @@ static long sys_keyctl(int cmd, long a, long b, long c, long d)return syscall(__NR_keyctl, cmd, a, b, c, d);}-key_serial_t keyring_find(const char *name)-{- long ret = sys_keyctl(KEYCTL_SEARCH, KEY_SPEC_USER_KEYRING,- (long)"keyring", (long)name, 0);- if (ret < 0)- return (key_serial_t)-errno;- return (key_serial_t)ret;-}--static int keyring_make_mutable(key_serial_t keyring)-{- /*- * A named keyring linked only from @u is not necessarily possessed by- * a systemd service process. Give root enough owner permission to- * populate it; init drops write/setattr again when sealing succeeds.- */- long perm = KEY_POS_VIEW | KEY_POS_READ | KEY_POS_WRITE |- KEY_POS_SEARCH | KEY_POS_LINK | KEY_POS_SETATTR |- KEY_USR_VIEW | KEY_USR_READ | KEY_USR_WRITE |- KEY_USR_SEARCH | KEY_USR_SETATTR;-- if (sys_keyctl(KEYCTL_SETPERM, keyring, perm, 0, 0) < 0)- return -errno;- return 0;-}--key_serial_t keyring_open_or_create(const char *name)-{- key_serial_t s = keyring_find(name);- long ret;- int rc;-- if (s != -ENOKEY)- return s; /* success, or some other error */-- ret = sys_add_key("keyring", name, NULL, 0,- KEY_SPEC_SESSION_KEYRING);- if (ret < 0)- return (key_serial_t)-errno;-- rc = keyring_make_mutable((key_serial_t)ret);- if (rc)- return (key_serial_t)rc;-- if (sys_keyctl(KEYCTL_LINK, ret, KEY_SPEC_USER_KEYRING, 0, 0) < 0)- return (key_serial_t)-errno;-- return (key_serial_t)ret;-}--int keyring_unlink_from_user(key_serial_t keyring)-{- if (sys_keyctl(KEYCTL_UNLINK, keyring, KEY_SPEC_USER_KEYRING, 0, 0) < 0)- return -errno;- return 0;-}--key_serial_t keyring_add_cert(key_serial_t keyring,- const void *cert, size_t cert_len)-{- long ret = sys_add_key("asymmetric", NULL, cert, cert_len, keyring);-- if (ret < 0)- return (key_serial_t)-errno;- return (key_serial_t)ret;-}--int keyring_seal(key_serial_t keyring)-{- /*- * Possessor + user keep view/read/search (so any root process can- * still search the cert at load); nobody keeps write/link/setattr.- * Once SETATTR is gone the perms are locked for the boot.- */- long perm = KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH |- KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH;-- if (sys_keyctl(KEYCTL_SETPERM, keyring, perm, 0, 0) < 0)- return -errno;- return 0;-}-static int describe_key(key_serial_t serial,char *out, size_t out_sz){diff --git a/util/keyring.h b/util/keyring.hindex b016cf2a..a626ef99 100644--- a/util/keyring.h+++ b/util/keyring.h@@ -19,31 +19,6 @@ typedef void (*shirudo_keyring_cb)(key_serial_t serial,const char *description,void *ud);-/* -ENOKEY if absent. */-key_serial_t keyring_find(const char *name);--/*- * As above, but adds the keyring to @u if absent.- */-key_serial_t keyring_open_or_create(const char *name);--/*- * Unlink a keyring from @u so init can replace an incomplete stale keyring.- */-int keyring_unlink_from_user(key_serial_t keyring);--key_serial_t keyring_add_cert(key_serial_t keyring,- const void *cert, size_t cert_len);--/*- * Freeze a keyring for the rest of the boot: drop write/link/setattr- * (keeping view/read/search) so no key can be added or unlinked and the- * permissions cannot be relaxed again -- dropping SETATTR makes it a- * one-way change. The kernel can still find the cert at BPF_PROG_LOAD.- * Returns 0 or -errno.- */-int keyring_seal(key_serial_t keyring);-/** Whether <keyring> holds the certificate <der>: 1 when one of its* keys is that certificate, 0 when none is, -errno otherwise. Thediff --git a/util/loader.c b/util/loader.cindex 2f62590d..11792794 100644--- a/util/loader.c+++ b/util/loader.c@@ -21,18 +21,6 @@ asm(".section .rodata, \"a\"\n"".global shirudo_seal_o_end; shirudo_seal_o_end:\n"".previous\n");-asm(".section .rodata, \"a\"\n"- ".global shirudo_bpf2_verity_o; shirudo_bpf2_verity_o:\n"- ".incbin \"bpf/bpf2_verity.bpf.o\"\n"- ".global shirudo_bpf2_verity_o_end; shirudo_bpf2_verity_o_end:\n"- ".previous\n");--asm(".section .rodata, \"a\"\n"- ".global shirudo_seal_verity_o; shirudo_seal_verity_o:\n"- ".incbin \"bpf/seal_verity.bpf.o\"\n"- ".global shirudo_seal_verity_o_end; shirudo_seal_verity_o_end:\n"- ".previous\n");-asm(".section .rodata, \"a\"\n"".global shirudo_unload_bpf_o; shirudo_unload_bpf_o:\n"".incbin \"bpf/unload.bpf.o\"\n"@@ -59,35 +47,21 @@ static const struct shirudo_embedded_bpf unload = {.data = shirudo_unload_bpf_o,};-const struct shirudo_embedded_bpf *bpf2_blob(bool image_mode)+const struct shirudo_embedded_bpf *bpf2_blob(void){static struct shirudo_embedded_bpf b;- if (image_mode) {- b.name = "bpf2_verity";- b.data = shirudo_bpf2_verity_o;- b.size = (size_t)(shirudo_bpf2_verity_o_end -- shirudo_bpf2_verity_o);- } else {- b = bpf2;- b.size = (size_t)(shirudo_bpf2_o_end - shirudo_bpf2_o);- }+ b = bpf2;+ b.size = (size_t)(shirudo_bpf2_o_end - shirudo_bpf2_o);return &b;}-const struct shirudo_embedded_bpf *seal_blob(bool image_mode)+const struct shirudo_embedded_bpf *seal_blob(void){static struct shirudo_embedded_bpf b;- if (image_mode) {- b.name = "seal_verity";- b.data = shirudo_seal_verity_o;- b.size = (size_t)(shirudo_seal_verity_o_end -- shirudo_seal_verity_o);- } else {- b = seal;- b.size = (size_t)(shirudo_seal_o_end - shirudo_seal_o);- }+ b = seal;+ b.size = (size_t)(shirudo_seal_o_end - shirudo_seal_o);return &b;}diff --git a/util/loader.h b/util/loader.hindex 9b1fbc3d..ad02256d 100644--- a/util/loader.h+++ b/util/loader.h@@ -8,14 +8,11 @@#ifndef SHIRUDO_LOADER_H#define SHIRUDO_LOADER_H-#include <stdbool.h>#include <stddef.h>/* Symbols provided by the embedded object assembly in loader.c. */extern const char shirudo_bpf2_o[], shirudo_bpf2_o_end[];extern const char shirudo_seal_o[], shirudo_seal_o_end[];-extern const char shirudo_bpf2_verity_o[], shirudo_bpf2_verity_o_end[];-extern const char shirudo_seal_verity_o[], shirudo_seal_verity_o_end[];extern const char shirudo_unload_bpf_o[], shirudo_unload_bpf_o_end[];struct shirudo_embedded_bpf {@@ -24,8 +21,8 @@ struct shirudo_embedded_bpf {size_t size;};-const struct shirudo_embedded_bpf *bpf2_blob(bool image_mode);-const struct shirudo_embedded_bpf *seal_blob(bool image_mode);+const struct shirudo_embedded_bpf *bpf2_blob(void);+const struct shirudo_embedded_bpf *seal_blob(void);const struct shirudo_embedded_bpf *unload_blob(void);#endif /* SHIRUDO_LOADER_H */diff --git a/util/paths.h b/util/paths.hindex e52b0ea2..0820529f 100644--- a/util/paths.h+++ b/util/paths.h@@ -125,8 +125,6 @@*/#define SHIRUDO_XATTR_PAYLOAD "security.bpf.shirudo.payload"-#define SHIRUDO_KEYRING_NAME "shirudo"-/** TPM2 NV index of the measurement register (owner-defined range).* Override at build time if it collides with site policy. An NVdiff --git a/util/preflight.c b/util/preflight.cindex 8b63f496..3a44f027 100644--- a/util/preflight.c+++ b/util/preflight.c@@ -363,23 +363,13 @@ static void check_embedded_loader(struct shirudo_preflight_result *r){struct shirudo_preflight_check *c =next_slot(r, SHIRUDO_PRE_EMBEDDED_LOADER, "embedded_loader");- const struct shirudo_embedded_bpf *bpf2 = bpf2_blob(false);- const struct shirudo_embedded_bpf *seal = seal_blob(false);- size_t bpf2_sz = bpf2 ? bpf2->size : 0;- size_t seal_sz = seal ? seal->size : 0;+ const struct shirudo_embedded_bpf *bpf2 = bpf2_blob();+ const struct shirudo_embedded_bpf *seal = seal_blob();- if (!bpf2_sz || !seal_sz) {- fail(r, c,- "embedded BPF objects missing (bpf2=%zu seal=%zu)",- bpf2_sz, seal_sz);- return;- }- bpf2 = bpf2_blob(true);- seal = seal_blob(true);if (bpf2 && bpf2->size && seal && seal->size)pass(c);else- fail(r, c, "embedded image-mode BPF objects missing");+ fail(r, c, "embedded BPF objects missing");}static void check_tpm(struct shirudo_preflight_result *r)diff --git a/util/seal.c b/util/seal.cindex 4dfeb456..32b9b661 100644--- a/util/seal.c+++ b/util/seal.c@@ -19,7 +19,6 @@#include <shirudo/string.h>-#include "image_config.h"#include "io.h"#include "log.h"#include "paths.h"@@ -58,35 +57,21 @@ static int seal_label_cb(const char *path, const struct stat *sb,*/if (sb->st_dev != seal_root_dev)return 0;- if (shirudo_image_config_active() &&- !S_ISREG(sb->st_mode) && !S_ISDIR(sb->st_mode))+ if (!S_ISREG(sb->st_mode) && !S_ISDIR(sb->st_mode))rc = -EINVAL;elserc = seal_label(path);if (rc) {- pr_warn("seal: label %s: %s", path, strerror(-rc));- if (shirudo_image_config_active()) {- seal_walk_error = rc;- return 1;- }+ pr_err("seal: label %s: %s", path, strerror(-rc));+ seal_walk_error = rc;+ return 1;}return 0;}int seal_label_tree(void){- char self[PATH_MAX];struct stat st;- ssize_t n;-- /* The ordinary-mode label protects the binary and marks its tasks. */- n = shirudo_image_config_active() ? -1 :- readlink("/proc/self/exe", self, sizeof(self) - 1);- if (n > 0) {- self[n] = '\0';- if (seal_label(self) != 0)- pr_warn("seal: label %s: %s", self, strerror(errno));- }if (stat(SHIRUDO_VAR_LIB, &st) != 0)return -errno;