Skip to content

eBPF-based security platform

init: boot Shirudo from verified runtime and config images

KP Singhbc62e9c53fce
diff --git a/Makefile.shirudo b/Makefile.shirudo
index b09e57ef..c7878d6e 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -193,9 +193,14 @@ UTIL_SRCS := \
util/kernel_build_id.c \
util/kernel_release.c \
util/config.c \
+ util/image_config.c \
+ util/image_runtime.c \
util/image_guards.c \
+ util/image_keys.c \
util/config_image.c \
util/dm_verity.c \
+ util/config_update.c \
+ util/config_measure.c \
util/bpm_source_config.c \
util/eventlog.c \
util/json.c \
@@ -418,7 +423,7 @@ build/target/util/loader.o: $(BPF_OBJS)
tools/shirudo-build/shirudo-build: $(BUILD_OBJS) util/sign.o util/config_image.o \
util/log.o util/json.o util/loader.o \
util/io.o util/malloc.o util/paths.o util/config.o \
- util/upatch.o util/timefmt.o \
+ util/image_config.o util/upatch.o util/timefmt.o \
util/elf_target.o util/hex.o util/keys.o \
util/seal.o util/harden.o util/string.o \
util/crypto_openssl.o util/tls_openssl.o \
@@ -597,10 +602,12 @@ testing/helper/test-conditions: testing/helper/test-conditions.c util/conditions
# util/config.o resolves config_load(), called by tpm.o.
# The register code pulls in the eventlog, which pulls in the seal
# label writer and the JSON codec.
-REGISTER_OBJS := util/config_image.o util/chain.o util/tpm.o util/tpm_wire.o util/eventlog.o \
+REGISTER_OBJS := util/config_update.o util/config_measure.o util/config_image.o \
+ util/chain.o util/tpm.o util/tpm_wire.o util/eventlog.o \
util/timefmt.o \
util/hex.o util/crypto_openssl.o util/json.o util/string.o \
- util/io.o util/seal.o util/config.o util/log.o util/malloc.o \
+ util/io.o util/seal.o util/config.o util/image_config.o \
+ util/log.o util/malloc.o \
util/initrd.o $(VERSION_OBJ)
REGISTER_LIBS := $(LIBCRYPTO_LIBS) $(LIBSSL_LIBS) $(TSS2_LIBS)
@@ -610,7 +617,8 @@ testing/helper/test-keys: testing/helper/test-keys.c util/keys.o \
$(Q)$(CC) $(CFLAGS) -o $@ $^ $(REGISTER_LIBS)
testing/helper/test-seal: testing/helper/test-seal.c util/seal.o util/io.o \
- util/log.o util/string.o util/malloc.o
+ util/log.o util/string.o util/malloc.o \
+ util/config.o util/image_config.o util/hex.o
@printf ' CC $@\n'
$(Q)$(CC) $(CFLAGS) -o $@ $^
@@ -638,7 +646,7 @@ testing/helper/tpmsim-cmd: testing/helper/tpmsim-cmd.c
$(Q)$(CC) $(CFLAGS_BASE) -Iinclude $(TPMSIM_CFLAGS) -o $@ $< $(TPMSIM_LIBS) -lcrypto
testing/helper/test-config: testing/helper/test-config.c util/config.o util/io.o util/log.o util/hex.o \
- util/string.o util/malloc.o
+ util/image_config.o util/string.o util/malloc.o
@printf ' CC $@\n'
$(Q)$(CC) $(CFLAGS) -o $@ $^
@@ -682,6 +690,7 @@ testing/helper/test-kernel-abi: testing/helper/test-kernel-abi.c util/kernel_abi
testing/helper/test-otel-map: testing/helper/test-otel-map.c util/upatch_maps.o \
util/upatch_maps_otel.o util/crypto_openssl.o \
util/tls_openssl.o util/serializer.o util/seal.o \
+ util/config.o util/image_config.o \
util/upatch_decode.o util/upatch_resolve.o \
util/upatch.o util/json.o util/string.o util/log.o \
util/io.o util/malloc.o $(LIBBPF_A) util/hex.o util/timefmt.o
@@ -701,6 +710,7 @@ testing/helper/test-upatch-serializer-pages: testing/helper/test-upatch-serializ
util/upatch_maps.o util/upatch_resolve.o \
util/serializer.o util/seal.o \
util/json.o util/string.o \
+ util/config.o util/image_config.o \
util/malloc.o util/io.o \
util/log.o util/hex.o util/timefmt.o
@printf ' CC $@\n'
diff --git a/builtin-bpm.c b/builtin-bpm.c
index 492fa28d..195797d5 100644
--- a/builtin-bpm.c
+++ b/builtin-bpm.c
@@ -42,6 +42,7 @@
#include "util/help.h"
#include "util/hex.h"
#include "util/io.h"
+#include "util/initrd.h"
#include "util/json.h"
#include "util/log.h"
#include "util/malloc.h"
@@ -299,6 +300,18 @@ static int load_source(const char *name, struct bpm_source *s)
fclose(fp);
if (!s->endpoint[0] || !s->key_file[0])
return -EINVAL;
+ if (shirudo_image_config_active()) {
+ const char *relative = s->key_file;
+ char resolved[BPM_MAX_KEY];
+
+ if (!strncmp(relative, "/etc/shirudo/bpm/keys/", 22))
+ relative++;
+ rc = shirudo_image_path_relative(resolved, sizeof(resolved),
+ relative);
+ if (rc)
+ return rc;
+ bpm_copy_string(s->key_file, sizeof(s->key_file), resolved);
+ }
return 0;
}
@@ -1211,6 +1224,13 @@ static int cmd_bpm_config(int argc, const char **argv)
closedir(d);
return 0;
}
+ if (shirudo_image_config_active() &&
+ (!strcmp(argv[2], "add") || !strcmp(argv[2], "remove") ||
+ !strcmp(argv[2], "rm"))) {
+ pr_err("image mode: change repository definitions and keys in "
+ "a signed config image, then run `shirudo image update`");
+ return 1;
+ }
if ((!strcmp(argv[2], "remove") || !strcmp(argv[2], "rm")) && argc == 4) {
char p[PATH_MAX], key[PATH_MAX];
struct bpm_source s;
@@ -1323,7 +1343,7 @@ static int tree_fetch_publication(const struct bpm_tree_config *cfg,
if (rc)
return rc;
return bpm_tree_fetch_publication(&c, repository, cfg->reference,
- SHIRUDO_CERT_PATH, pub,
+ shirudo_cert_path(), pub,
manifest_digest, digest_sz);
}
@@ -2191,6 +2211,11 @@ static int cmd_bpm_key(int argc, const char **argv)
print_help(&bpm_key_help);
return 2;
}
+ if (!strcmp(sub, "add") && shirudo_image_config_active()) {
+ pr_err("image mode: add vouched BPF signers to the signed "
+ "config selected for boot and reboot to enroll them");
+ return 1;
+ }
if (!strcmp(sub, "list")) {
rc = list_keys();
return rc ? bpm_fail("key list", rc) : 0;
diff --git a/builtin-config.c b/builtin-config.c
index df19429c..c474f861 100644
--- a/builtin-config.c
+++ b/builtin-config.c
@@ -37,6 +37,7 @@ static void usage(void)
int cmd_config(int argc, const char **argv)
{
+ bool image_mode = shirudo_image_config_active();
struct shirudo_config cfg;
bool file_present;
int rc;
@@ -60,7 +61,10 @@ int cmd_config(int argc, const char **argv)
return 1;
}
- if (file_present)
+ if (image_mode)
+ printf("# shirudo config (authenticated config image; "
+ "TPM settings from runtime image)\n\n");
+ else if (file_present)
printf("# shirudo config (effective, loaded from %s)\n\n",
SHIRUDO_CONF_PATH);
else
@@ -78,12 +82,14 @@ int cmd_config(int argc, const char **argv)
"# binary auth value. May also be supplied via the\n"
"# SHIRUDO_TPM_OWNER_AUTH env var; that override wins.\n");
if (cfg.tpm_owner_auth_len > 0) {
- const char *env = getenv("SHIRUDO_TPM_OWNER_AUTH");
+ const char *env = image_mode ? NULL :
+ getenv("SHIRUDO_TPM_OWNER_AUTH");
printf("# tpm.owner_auth = (set, %zu bytes; redacted%s)\n\n",
cfg.tpm_owner_auth_len,
- env ? "; from $SHIRUDO_TPM_OWNER_AUTH"
- : "; from " SHIRUDO_CONF_PATH);
+ image_mode ? "; from runtime image" :
+ env ? "; from $SHIRUDO_TPM_OWNER_AUTH" :
+ "; from " SHIRUDO_CONF_PATH);
} else {
printf("# tpm.owner_auth = (unset)\n\n");
}
@@ -97,11 +103,12 @@ int cmd_config(int argc, const char **argv)
"# when this is set. The SHIRUDO_TPM_TCTI env var overrides\n"
"# this in turn.\n");
if (cfg.tpm_tcti[0]) {
- const char *env = getenv("SHIRUDO_TPM_TCTI");
+ const char *env = image_mode ? NULL :
+ getenv("SHIRUDO_TPM_TCTI");
printf("tpm.tcti = %s # from %s\n\n", cfg.tpm_tcti,
- env && *env ? "$SHIRUDO_TPM_TCTI"
- : SHIRUDO_CONF_PATH);
+ image_mode ? "runtime image" :
+ env && *env ? "$SHIRUDO_TPM_TCTI" : SHIRUDO_CONF_PATH);
} else {
printf("# tpm.tcti = (unset -- using device:/dev/tpmrm0)\n\n");
}
diff --git a/builtin-image.c b/builtin-image.c
index d48a6270..cb31c5b3 100644
--- a/builtin-image.c
+++ b/builtin-image.c
@@ -6,6 +6,8 @@
#include <string.h>
#include "builtin.h"
+#include "util/image_config.h"
+#include "util/image_runtime.h"
#include "util/log.h"
#include "util/malloc.h"
@@ -13,7 +15,8 @@ static void image_help(void)
{
fprintf(stderr,
"usage: shirudo image build [image builder options]\n"
- " shirudo image verify <prefix> --cert <certificate>\n");
+ " shirudo image verify <prefix> --cert <certificate>\n"
+ " shirudo image status\n");
}
int cmd_image(int argc, const char **argv)
@@ -21,7 +24,7 @@ int cmd_image(int argc, const char **argv)
int rc;
if (argc == 2 && !strcmp(argv[1], "--list-cmds")) {
- puts("build verify");
+ puts("build verify status");
return 0;
}
if (argc < 2 || !strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")) {
@@ -45,6 +48,15 @@ int cmd_image(int argc, const char **argv)
xfree(helper, bytes);
return rc;
}
- image_help();
- return 2;
+ if (!strcmp(argv[1], "status") && argc == 2) {
+ rc = shirudo_image_status();
+ } else {
+ image_help();
+ return 2;
+ }
+ if (rc == -EINPROGRESS)
+ pr_err("image: activation is pending; further changes are blocked");
+ else if (rc)
+ pr_err("image %s: %s", argv[1], strerror(-rc));
+ return rc ? 1 : 0;
}
diff --git a/builtin-init.c b/builtin-init.c
index ef94ce9b..a10ccf08 100644
--- a/builtin-init.c
+++ b/builtin-init.c
@@ -16,6 +16,8 @@
#include <string.h>
#include <sys/mount.h>
#include <sys/stat.h>
+#include <sys/statvfs.h>
+#include <sys/syscall.h>
#include <sys/sysmacros.h>
#include <sys/types.h>
#include <unistd.h>
@@ -38,6 +40,9 @@
#include "util/macros.h"
#include "util/identity.h"
#include "util/initrd.h"
+#include "util/image_config.h"
+#include "util/image_guards.h"
+#include "util/image_runtime.h"
#include "util/keyring.h"
#include "util/keys.h"
#include "util/loader.h"
@@ -130,6 +135,7 @@ 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
@@ -284,18 +290,31 @@ static int init_keyring(void)
bool frozen;
int rc;
- rc = keys_load(shirudo_keys_dir(), SHIRUDO_KEYS_REVOKED_DIR,
- true, &set);
+ 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 ||
+ 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));
return rc;
}
- if (set.n_revoked)
+ 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)
+ if (initrd_mode && !image_mode)
warn_root_keys_differ(&set);
frozen = identity_read(&id) == 0;
@@ -316,13 +335,15 @@ static int init_keyring(void)
enrolled = set;
return 0;
}
- if (rc != -ENOKEY)
+ 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));
@@ -467,10 +488,36 @@ 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;
+ 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 -errno;
+ }
/* So that the init record left here is sealed from its creation. */
rc = seal_label(SHIRUDO_RUN_DIR);
- if (rc)
+ if (rc) {
+ if (image_mode)
+ return rc;
pr_warn("seal: label %s: %s", SHIRUDO_RUN_DIR, strerror(-rc));
+ }
rc = seal_mkdir_p(SHIRUDO_BPFFS_DIR, 0700);
if (rc) {
pr_err("mkdir %s: %s",
@@ -561,6 +608,11 @@ static int identity_executables(struct shirudo_identity *id)
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;
@@ -616,42 +668,47 @@ static int attach_pin(struct bpf_program *p, const char *pin,
return 0;
}
-/* The marking hooks come before inode_init_security, which relies on them. */
-static const struct {
- const char *prog;
- const char *pin;
-} seal_hooks[] = {
- { "shirudo_seal_bprm_check_security", SHIRUDO_SEAL_BPRM_CHECK_PIN },
- { "shirudo_seal_bprm_committed_creds",
- SHIRUDO_SEAL_BPRM_COMMITTED_PIN },
- { "shirudo_seal_task_alloc", SHIRUDO_SEAL_TASK_ALLOC_PIN },
- { "shirudo_seal_ptrace_access_check", SHIRUDO_SEAL_PTRACE_PIN },
- { "shirudo_seal_perf_event_alloc", SHIRUDO_SEAL_PERF_PIN },
- { "shirudo_seal_file_open", SHIRUDO_SEAL_FILE_OPEN_PIN },
- { "shirudo_seal_inode_post_setxattr",
- SHIRUDO_SEAL_POST_SETXATTR_PIN },
- { "shirudo_seal_inode_post_removexattr",
- SHIRUDO_SEAL_POST_RMXATTR_PIN },
- { "shirudo_seal_inode_init_security",
- SHIRUDO_SEAL_INIT_SECURITY_PIN },
- { "shirudo_seal_inode_unlink", SHIRUDO_SEAL_UNLINK_PIN },
- { "shirudo_seal_inode_rename", SHIRUDO_SEAL_RENAME_PIN },
- { "shirudo_seal_inode_setattr", SHIRUDO_SEAL_SETATTR_PIN },
- { "shirudo_seal_inode_setxattr", SHIRUDO_SEAL_SETXATTR_PIN },
- { "shirudo_seal_inode_removexattr", SHIRUDO_SEAL_RMXATTR_PIN },
-};
+static struct bpf_program *guard_program(struct bpf_object *object,
+ enum shirudo_gate_guard guard)
+{
+ const struct shirudo_image_guard *entry = &shirudo_gate_guards[guard];
-/*
- * Before lsm/bpf attaches: the seal is loaded unsigned and would be
- * refused once the gate is live. It reads bpf2's identity map and
- * counts into its tamper map, so one set covers both.
- */
-static int attach_seal(struct bpf_object **obj_out, struct bpf_link **links,
- int identity_fd, int tamper_fd)
+ return bpf_object__find_program_by_name(object, entry->prog);
+}
+
+static int attach_guard(struct bpf_program *program,
+ enum shirudo_gate_guard guard, struct bpf_link **link)
+{
+ return attach_pin(program, shirudo_gate_guards[guard].pin, link);
+}
+
+/* Both objects share authority, device state and refusal counters. */
+static int share_policy_maps(struct bpf_object *source,
+ struct bpf_object *destination)
+{
+ struct bpf_map *from, *to;
+ size_t i;
+ int rc;
+
+ for (i = 0; i < ARRAY_SIZE(shirudo_guard_maps); i++) {
+ from = bpf_object__find_map_by_name(source,
+ shirudo_guard_maps[i].name);
+ to = bpf_object__find_map_by_name(destination,
+ shirudo_guard_maps[i].name);
+ if (!from || !to)
+ return -ENOENT;
+ rc = bpf_map__reuse_fd(to, bpf_map__fd(from));
+ if (rc)
+ return rc;
+ }
+ return 0;
+}
+
+static int attach_seal(struct bpf_object *policy, struct bpf_object **obj_out,
+ struct bpf_link **links)
{
const struct shirudo_embedded_bpf *blob = seal_blob();
struct bpf_object *obj;
- struct bpf_map *m;
int err, i;
*obj_out = NULL;
@@ -665,49 +722,34 @@ static int attach_seal(struct bpf_object **obj_out, struct bpf_link **links,
pr_err("seal: bpf_object__open_mem: %d", err);
return err;
}
- m = bpf_object__find_map_by_name(obj, "shirudo_bpffs_sb");
- if (!m) {
- pr_err("seal: shirudo_bpffs_sb map not in embedded seal");
- bpf_object__close(obj);
- return -ENOENT;
- }
- err = bpf_map__reuse_fd(m, identity_fd);
- if (err) {
- pr_err("seal: share the identity map: %s", strerror(-err));
- bpf_object__close(obj);
- return err;
- }
- m = bpf_object__find_map_by_name(obj, "shirudo_tamper");
- if (!m) {
- pr_err("seal: shirudo_tamper map not in embedded seal");
- bpf_object__close(obj);
- return -ENOENT;
- }
- err = bpf_map__reuse_fd(m, tamper_fd);
- if (err) {
- pr_err("seal: share the tamper map: %s", strerror(-err));
- bpf_object__close(obj);
- return err;
+ err = share_policy_maps(policy, obj);
+ if (!err)
+ err = bpf_object__load(obj);
+ if (!err) {
+ struct bpf_map *scratch;
+
+ scratch = bpf_object__find_map_by_name(obj, "seal_scratch_map");
+ err = scratch ? bpf_map_freeze(bpf_map__fd(scratch)) : -ENOENT;
}
- err = bpf_object__load(obj);
if (err) {
pr_err("seal: bpf_object__load: %d", err);
bpf_object__close(obj);
return err;
}
- for (i = 0; i < (int)ARRAY_SIZE(seal_hooks); i++) {
+ for (i = 0; i < (int)ARRAY_SIZE(shirudo_seal_guards); i++) {
+ const struct shirudo_image_guard *hook =
+ &shirudo_seal_guards[i];
struct bpf_program *p =
- bpf_object__find_program_by_name(obj,
- seal_hooks[i].prog);
+ bpf_object__find_program_by_name(obj, hook->prog);
if (!p) {
- pr_err("seal: program %s missing", seal_hooks[i].prog);
+ pr_err("seal: program %s missing", hook->prog);
err = -ENOENT;
break;
}
- err = attach_pin(p, seal_hooks[i].pin, &links[i]);
+ err = attach_pin(p, hook->pin, &links[i]);
if (err) {
- pr_err("seal: attach %s: %s", seal_hooks[i].prog,
+ pr_err("seal: attach %s: %s", hook->prog,
strerror(-err));
break;
}
@@ -746,46 +788,29 @@ static void collect_asymmetric(key_serial_t serial, const char *type,
static int init_lsm(bool attach_filesystem_seal)
{
- struct shirudo_identity id = {};
- static const struct {
- const char *prog;
- const char *pin;
- } mount_hooks[] = {
- { "shirudo_lsm_sb_umount", SHIRUDO_SB_UMOUNT_LINK_PIN },
- { "shirudo_lsm_move_mount", SHIRUDO_MOVE_MOUNT_LINK_PIN },
- { "shirudo_lsm_sb_mount", SHIRUDO_SB_MOUNT_LINK_PIN },
- { "shirudo_lsm_sb_remount", SHIRUDO_SB_REMOUNT_LINK_PIN },
- };
- static const struct {
- const char *prog;
- const char *pin;
- } identity_hooks[] = {
- { "shirudo_lsm_task_prctl", SHIRUDO_TASK_PRCTL_LINK_PIN },
- { "shirudo_lsm_bprm_check_security",
- SHIRUDO_BPRM_CHECK_LINK_PIN },
- };
- struct bpf_link *identity_links[ARRAY_SIZE(identity_hooks)] = {};
- struct bpf_link *mount_links[ARRAY_SIZE(mount_hooks)] = {};
- const struct shirudo_embedded_bpf *blob = bpf2_blob();
+ struct bpf_link *link_setx = NULL, *link_rmx = NULL, *link_pload = NULL;
+ struct bpf_link *identity_links[ARRAY_SIZE(shirudo_observer_guards)] = {};
+ struct bpf_link *mount_links[ARRAY_SIZE(shirudo_mount_guards)] = {};
struct bpf_program *p_bpf, *p_bpf_map, *p_unlink, *p_setx, *p_rmx;
- struct bpf_program *p_pload, *p_rename;
- struct bpf_object *obj = NULL, *seal_obj = NULL;
+ 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_link *link_unlink = NULL, *link_rename = NULL;
- struct bpf_link *link_setx = NULL, *link_rmx = NULL, *link_pload = NULL;
+ struct bpf_object *obj = NULL, *seal_obj = NULL;
struct bpf_link *link_keyperm = NULL;
- struct bpf_link *seal_links[ARRAY_SIZE(seal_hooks)] = {};
- struct bpf_program *p_keyperm;
+ struct bpf_link *link_unlink = NULL;
+ struct shirudo_identity id = {};
struct anchor_keys keys = {};
+ struct bpf_program *p_pload;
+ struct bpf_program *p_key;
key_serial_t keyring;
- struct bpf_map *m_sb, *m_tamper;
- struct stat st;
+ struct bpf_map *m_sb;
+ int err, map_fd, i;
__u32 zero = 0;
- int err, map_fd, tamper_fd, i;
+ struct stat st;
- if (access(SHIRUDO_LSM_LINK_PIN, F_OK) == 0) {
+ if (access(shirudo_gate_guards[SHIRUDO_GUARD_BPF].pin, F_OK) == 0) {
pr_info("bpf2 already attached (%s exists)",
- SHIRUDO_LSM_LINK_PIN);
+ shirudo_gate_guards[SHIRUDO_GUARD_BPF].pin);
return 0;
}
/* A pin without the links behind it is what a failed attempt leaves. */
@@ -824,6 +849,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);
+ }
/* So inode guards can tell `upatch unload` from an attacker's rm. */
err = identity_executables(&id);
@@ -890,6 +922,42 @@ static int init_lsm(bool attach_filesystem_seal)
pr_err("freeze sb-identity map: %s", strerror(errno));
goto out;
}
+ {
+ const struct shirudo_guard_map *maps =
+ &shirudo_guard_maps[SHIRUDO_GUARD_TASKS];
+ struct shirudo_task_authority authority = {
+ .trusted = SHIRUDO_TASK_BOOTSTRAP,
+ };
+ struct bpf_map *map;
+ int fd, pidfd;
+
+ pidfd = syscall(SYS_pidfd_open, getpid(), 0);
+ if (pidfd < 0) {
+ err = -errno;
+ goto out;
+ }
+ for (i = 0; i < SHIRUDO_GUARD_MAP_COUNT - 1; i++) {
+ map = bpf_object__find_map_by_name(obj, maps[i].name);
+ if (!map) {
+ err = -ENOENT;
+ break;
+ }
+ fd = bpf_map__fd(map);
+ if (!i && bpf_map_update_elem(fd, &pidfd, &authority,
+ BPF_ANY)) {
+ err = -errno;
+ break;
+ }
+ if (bpf_map_freeze(fd) ||
+ bpf_obj_pin(fd, maps[i].pin)) {
+ err = -errno;
+ break;
+ }
+ }
+ close(pidfd);
+ if (err)
+ goto out;
+ }
/*
* Pinned so that every later command reads the anchor init froze
* from the kernel rather than from a file: the serial a load names
@@ -902,67 +970,38 @@ static int init_lsm(bool attach_filesystem_seal)
goto out;
}
- /*
- * The tamper counters, pinned for status to read and frozen so
- * that no task, shirudo included, can reset them from userspace;
- * the hooks write them.
- */
- m_tamper = bpf_object__find_map_by_name(obj, "shirudo_tamper");
- if (!m_tamper) {
- pr_err("shirudo_tamper map not in embedded bpf2");
- err = -ENOENT;
- goto out;
- }
- tamper_fd = bpf_map__fd(m_tamper);
- if (bpf_map_freeze(tamper_fd) != 0) {
- err = -errno;
- pr_err("freeze tamper map: %s", strerror(errno));
- goto out;
- }
- if (bpf_obj_pin(tamper_fd, SHIRUDO_TAMPER_PIN) != 0) {
- err = -errno;
- pr_err("pin tamper map at %s: %s", SHIRUDO_TAMPER_PIN,
- strerror(errno));
- goto out;
- }
-
/* Install the identity guards before enabling the bpf() gates. */
- for (i = 0; i < (int)ARRAY_SIZE(identity_hooks); i++) {
+ for (i = 0; i < (int)ARRAY_SIZE(shirudo_observer_guards); i++) {
+ const struct shirudo_image_guard *hook =
+ &shirudo_observer_guards[i];
struct bpf_program *p;
- p = bpf_object__find_program_by_name(obj,
- identity_hooks[i].prog);
+ p = bpf_object__find_program_by_name(obj, hook->prog);
+
if (!p) {
pr_err("program %s missing in bpf2 object",
- identity_hooks[i].prog);
+ hook->prog);
err = -ENOENT;
goto out;
}
- err = attach_pin(p, identity_hooks[i].pin, &identity_links[i]);
+ err = attach_pin(p, hook->pin, &identity_links[i]);
if (err) {
- pr_err("attach %s: %s", identity_hooks[i].prog,
+ pr_err("attach %s: %s", hook->prog,
strerror(-err));
goto out;
}
}
- p_unlink = bpf_object__find_program_by_name(obj,
- "shirudo_lsm_inode_unlink");
- p_rename = bpf_object__find_program_by_name(obj,
- "shirudo_lsm_inode_rename");
- p_setx = bpf_object__find_program_by_name(obj,
- "shirudo_lsm_inode_setxattr");
- p_rmx = bpf_object__find_program_by_name(obj,
- "shirudo_lsm_inode_removexattr");
- p_pload = bpf_object__find_program_by_name(obj,
- "shirudo_lsm_bpf_prog_load");
- p_bpf_map = bpf_object__find_program_by_name(obj,
- "shirudo_lsm_bpf_map");
- p_keyperm = bpf_object__find_program_by_name(obj,
- "shirudo_lsm_key_permission");
- p_bpf = bpf_object__find_program_by_name(obj, "shirudo_lsm_bpf");
- if (!p_unlink || !p_rename || !p_setx || !p_rmx || !p_pload ||
- !p_bpf_map || !p_bpf || !p_keyperm) {
+ p_unlink = guard_program(obj, SHIRUDO_GUARD_UNLINK);
+ p_setx = guard_program(obj, SHIRUDO_GUARD_SETXATTR);
+ p_rmx = guard_program(obj, SHIRUDO_GUARD_REMOVEXATTR);
+ p_pload = guard_program(obj, SHIRUDO_GUARD_PROG_LOAD);
+ p_bpf_map = guard_program(obj, SHIRUDO_GUARD_BPF_MAP);
+ p_key = guard_program(obj, SHIRUDO_GUARD_KEY_PERMISSION);
+ p_bpf = guard_program(obj, SHIRUDO_GUARD_BPF);
+ if (!p_unlink || !p_setx || !p_rmx || !p_pload || !p_bpf_map ||
+ !p_bpf ||
+ !p_key) {
pr_err("expected programs missing in bpf2 object");
err = -ENOENT;
goto out;
@@ -976,28 +1015,22 @@ static int init_lsm(bool attach_filesystem_seal)
* Pinning a link creates a bpffs inode, gated by neither guard set;
* seal's unsigned load and the identity-map freeze precede the gates.
*/
- err = attach_pin(p_unlink, SHIRUDO_INODE_UNLINK_LINK_PIN, &link_unlink);
+ err = attach_guard(p_unlink, SHIRUDO_GUARD_UNLINK, &link_unlink);
if (err) {
pr_err("attach lsm/inode_unlink: %s", strerror(-err));
goto out;
}
- err = attach_pin(p_rename, SHIRUDO_INODE_RENAME_LINK_PIN, &link_rename);
- if (err) {
- pr_err("attach lsm/inode_rename: %s", strerror(-err));
- goto out;
- }
- err = attach_pin(p_setx, SHIRUDO_INODE_SETXATTR_LINK_PIN, &link_setx);
+ err = attach_guard(p_setx, SHIRUDO_GUARD_SETXATTR, &link_setx);
if (err) {
pr_err("attach lsm/inode_setxattr: %s", strerror(-err));
goto out;
}
- err = attach_pin(p_rmx, SHIRUDO_INODE_RMXATTR_LINK_PIN, &link_rmx);
+ err = attach_guard(p_rmx, SHIRUDO_GUARD_REMOVEXATTR, &link_rmx);
if (err) {
pr_err("attach lsm/inode_removexattr: %s", strerror(-err));
goto out;
}
- err = attach_pin(p_keyperm, SHIRUDO_KEY_PERMISSION_LINK_PIN,
- &link_keyperm);
+ err = attach_guard(p_key, SHIRUDO_GUARD_KEY_PERMISSION, &link_keyperm);
if (err) {
pr_err("attach lsm/key_permission: %s", strerror(-err));
goto out;
@@ -1007,44 +1040,46 @@ static int init_lsm(bool attach_filesystem_seal)
* With the other bpffs guards, so the mount is immutable before
* enforcement goes live.
*/
- for (i = 0; i < (int)ARRAY_SIZE(mount_hooks); i++) {
+ for (i = 0; i < (int)ARRAY_SIZE(shirudo_mount_guards); i++) {
+ const struct shirudo_image_guard *hook =
+ &shirudo_mount_guards[i];
struct bpf_program *p;
- p = bpf_object__find_program_by_name(obj, mount_hooks[i].prog);
+ p = bpf_object__find_program_by_name(obj, hook->prog);
+
if (!p) {
pr_err("program %s missing in bpf2 object",
- mount_hooks[i].prog);
+ hook->prog);
err = -ENOENT;
goto out;
}
- err = attach_pin(p, mount_hooks[i].pin, &mount_links[i]);
+ err = attach_pin(p, hook->pin, &mount_links[i]);
if (err) {
- pr_err("attach %s: %s", mount_hooks[i].prog,
+ pr_err("attach %s: %s", hook->prog,
strerror(-err));
goto out;
}
}
if (attach_filesystem_seal) {
- err = attach_seal(&seal_obj, seal_links, map_fd, tamper_fd);
+ err = attach_seal(obj, &seal_obj, seal_links);
if (err)
goto out;
}
- err = attach_pin(p_bpf_map, SHIRUDO_LSM_BPF_MAP_LINK_PIN,
- &link_bpf_map);
+ err = attach_guard(p_bpf_map, SHIRUDO_GUARD_BPF_MAP, &link_bpf_map);
if (err) {
pr_err("attach lsm/bpf_map: %s", strerror(-err));
goto out;
}
- err = attach_pin(p_pload, SHIRUDO_LSM_PROG_LOAD_LINK_PIN, &link_pload);
+ err = attach_guard(p_pload, SHIRUDO_GUARD_PROG_LOAD, &link_pload);
if (err) {
pr_err("attach lsm/bpf_prog_load: %s", strerror(-err));
goto out;
}
- err = attach_pin(p_bpf, SHIRUDO_LSM_LINK_PIN, &link_bpf);
+ err = attach_guard(p_bpf, SHIRUDO_GUARD_BPF, &link_bpf);
if (err) {
pr_err("attach lsm/bpf: %s", strerror(-err));
goto out;
@@ -1061,10 +1096,30 @@ out:
bpf_link__unpin(identity_links[i]);
bpf_link__destroy(identity_links[i]);
}
- for (i = 0; i < (int)ARRAY_SIZE(seal_links); i++)
+ for (i = 0; i < (int)ARRAY_SIZE(seal_links); i++) {
+ if (err && seal_links[i])
+ bpf_link__unpin(seal_links[i]);
bpf_link__destroy(seal_links[i]);
- for (i = 0; i < (int)ARRAY_SIZE(mount_links); i++)
+ }
+ for (i = 0; i < (int)ARRAY_SIZE(mount_links); i++) {
+ if (err && mount_links[i])
+ bpf_link__unpin(mount_links[i]);
bpf_link__destroy(mount_links[i]);
+ }
+ if (err) {
+ struct bpf_link *links[] = {
+ link_bpf, link_bpf_map, link_pload, link_keyperm,
+ link_rmx, link_setx, link_unlink,
+ };
+
+ for (i = 0; i < (int)ARRAY_SIZE(links); i++)
+ if (links[i])
+ bpf_link__unpin(links[i]);
+ (void)unlink(SHIRUDO_IDENTITY_PIN);
+ (void)unlink(SHIRUDO_TAMPER_PIN);
+ (void)unlink(SHIRUDO_BPFFS_DIR "/task_authority");
+ (void)unlink(SHIRUDO_BPFFS_DIR "/devices");
+ }
bpf_object__close(seal_obj);
bpf_link__destroy(link_bpf);
bpf_link__destroy(link_bpf_map);
@@ -1072,7 +1127,6 @@ out:
bpf_link__destroy(link_keyperm);
bpf_link__destroy(link_rmx);
bpf_link__destroy(link_setx);
- bpf_link__destroy(link_rename);
bpf_link__destroy(link_unlink);
bpf_object__close(obj);
return err;
@@ -1241,7 +1295,8 @@ static int append_init_event(const struct shirudo_measured_upatch *set,
for (i = 0; i < n; i++) {
json_obj_open(&e);
json_kv_string(&e, "name", set[i].name);
- json_kv_string(&e, "payload_sha256", set[i].payload_hex);
+ json_kv_string(&e, "payload_sha256",
+ set[i].payload_hex);
json_obj_close(&e);
}
json_arr_close(&e);
@@ -1249,7 +1304,8 @@ static int append_init_event(const struct shirudo_measured_upatch *set,
json_kv_string(&e, "pcr_before", pcr_hex);
json_kv_int(&e, "phase_pcr", SHIRUDO_CHAIN_PHASE_PCR);
json_kv_string(&e, "phase_before", phase_hex);
- json_kv_string(&e, "bound_in", info->initrd ? "initrd" : "root");
+ json_kv_string(&e, "bound_in",
+ info->initrd ? "initrd" : "root");
json_kv_string(&e, "nv_name", name_hex);
json_kv_string(&e, "anchor", info->anchor);
json_kv_string(&e, "anchor_digest", anchor_hex);
@@ -1315,7 +1371,7 @@ static int chain_extend_init(struct shirudo_measured_upatch *set, size_t n)
config_load(&cfg);
max_bytes = cfg.eventlog_max_bytes;
full = eventlog_would_exceed(max_bytes);
- explicit_bzero(&cfg, sizeof(cfg)); /* scrub tpm_owner_auth */
+ explicit_bzero(&cfg, sizeof(cfg));
if (full) {
pr_err("eventlog %s would exceed eventlog.max_bytes=%llu; "
"refusing chain extend (eventlog_full)",
@@ -1396,7 +1452,8 @@ 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" },
+ { "--no-seal",
+ "skip filesystem sealing when target LSM hooks are not sleepable" },
};
static const struct shirudo_help_section init_help_sections[] = {
@@ -1452,6 +1509,19 @@ 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;
@@ -1482,6 +1552,8 @@ static int run_init(bool no_seal)
return rc;
rc = init_keyring();
+ if (!rc && image_mode)
+ rc = shirudo_image_boot_keys_close();
if (rc)
return rc;
@@ -1492,14 +1564,17 @@ static int run_init(bool no_seal)
* 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) {
+ 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)
+ 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 "
@@ -1513,11 +1588,29 @@ static int run_init(bool no_seal)
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) {
- pr_err("init: incomplete -- LSM active, but boot-restore "
- "failed; re-run `shirudo init` after fixing the "
- "upatch");
+ 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");
xfree(set, n * sizeof(*set));
return rc;
}
@@ -1530,6 +1623,12 @@ static int run_init(bool no_seal)
return rc;
}
+ if (image_mode) {
+ 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",
@@ -1549,19 +1648,32 @@ int cmd_init(int argc, const char **argv)
if (strcmp(a, "--check-only") == 0) {
check_only = true;
- } else if (strcmp(a, "--no-seal") == 0) {
+ continue;
+ }
+ if (strcmp(a, "--no-seal") == 0) {
no_seal = true;
- } else if (is_help(a)) {
+ continue;
+ }
+ if (is_help(a)) {
print_help(&init_help);
return 0;
- } else {
- pr_err("init: unknown option '%s'", a);
- usage();
- return 2;
}
+ pr_err("init: unknown option '%s'", a);
+ usage();
+ return 2;
}
initrd_mode = in_initrd();
+ rc = shirudo_image_bootstrap_load();
+ if (rc < 0) {
+ 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);
@@ -1577,6 +1689,7 @@ int cmd_init(int argc, const char **argv)
if (initrd_mode && initrd_enter())
return 1;
rc = run_init(no_seal);
+ shirudo_image_leave();
if (initrd_mode)
initrd_leave();
return rc ? 1 : 0;
diff --git a/builtin-keys.c b/builtin-keys.c
index aa5d0b11..b37e3f58 100644
--- a/builtin-keys.c
+++ b/builtin-keys.c
@@ -36,6 +36,7 @@
#include "util/hex.h"
#include "util/identity.h"
#include "util/io.h"
+#include "util/image_keys.h"
#include "util/json.h"
#include "util/keyring.h"
#include "util/keys.h"
@@ -577,6 +578,11 @@ static int keys_add(int argc, const char **argv)
print_help(&keys_add_help);
return 2;
}
+ if (shirudo_image_config_active()) {
+ pr_err("image mode: add vouched BPF signers to the signed "
+ "config selected for boot and reboot to enroll them");
+ return 1;
+ }
keys_dir = keys_dir_resolve(keys_dir_arg);
if (strsprintf(revoked, sizeof(revoked), "%s/revoked", keys_dir) < 0) {
pr_err("keys add: keys directory path too long");
@@ -818,6 +824,30 @@ static int purge_signed_by(const __u8 *der, size_t der_sz,
return rc ? rc : purged;
}
+int shirudo_keys_revoke_image(const __u8 *der, size_t size, bool boot)
+{
+ key_serial_t ring, key;
+ size_t unloaded;
+ int rc;
+
+ if (!boot)
+ return -EOPNOTSUPP;
+ ring = keyring_bpf_serial();
+ if (ring < 0)
+ return ring;
+ key = keyring_find_cert(ring, der, size);
+ if (key != -ENOKEY && key < 0)
+ return key;
+ if (key >= 0) {
+ rc = keyring_unlink_key(ring, key);
+ if (rc)
+ return rc;
+ }
+ rc = purge_signed_by(der, size, &unloaded);
+ 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];
@@ -973,6 +1003,11 @@ static int keys_revoke(int argc, const char **argv)
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);
/*
@@ -1202,7 +1237,8 @@ static int keys_show(int argc, const char **argv)
}
}
- keys_dir = keys_dir_resolve(keys_dir_arg);
+ keys_dir = shirudo_image_config_active() ? SHIRUDO_RUNTIME_DIR "/keys" :
+ keys_dir_resolve(keys_dir_arg);
rc = key_paths(keys_dir, pkey_path, sizeof(pkey_path),
cert_path, sizeof(cert_path));
diff --git a/builtin-status.c b/builtin-status.c
index 7cfefbc6..7b5e1384 100644
--- a/builtin-status.c
+++ b/builtin-status.c
@@ -46,6 +46,7 @@
#include "util/log.h"
#include "util/malloc.h"
#include "util/paths.h"
+#include "util/initrd.h"
#include "util/kmodules.h"
#include "util/kmsg.h"
#include "util/format.h"
@@ -149,7 +150,7 @@ static void gather_assets(struct asset_view *a)
struct stat st;
*a = (struct asset_view){};
- a->cert = access(SHIRUDO_CERT_PATH, F_OK) == 0;
+ a->cert = access(shirudo_cert_path(), F_OK) == 0;
a->signing_key = access(SHIRUDO_SIGNING_KEY_PATH, F_OK) == 0;
if (stat(eventlog_path(), &st) == 0) {
a->eventlog = true;
@@ -660,7 +661,7 @@ int cmd_status(int argc, const char **argv)
mode = !lsm_attached ? "inactive" :
!strcmp(keyring_mode, "bpf") ? "production" : "staging";
is_operator = access(SHIRUDO_SIGNING_KEY_PATH, F_OK) == 0;
- cert_present = access(SHIRUDO_CERT_PATH, F_OK) == 0;
+ cert_present = access(shirudo_cert_path(), F_OK) == 0;
is_target = cert_present || lsm_attached || keyring_present;
n_ups = scandir(SHIRUDO_BPFFS_UPATCHES_DIR, &ups, notdot, alphasort);
diff --git a/builtin-upatch.c b/builtin-upatch.c
index 761ace98..b9984bb9 100644
--- a/builtin-upatch.c
+++ b/builtin-upatch.c
@@ -47,6 +47,7 @@
#include "util/macros.h"
#include "util/identity.h"
#include "util/keyring.h"
+#include "util/tpm.h"
#include "util/log.h"
#include "util/hex.h"
#include "util/malloc.h"
@@ -1990,12 +1991,13 @@ static int version_gate(const char *expect_version_id, bool no_version_check,
return 0;
}
-static int append_upatch_event(const char *type, const char *name,
+/* Eventlog entry ("upatch_load" / "upatch_unload") mirroring an extend. */
+static int format_upatch_event(const char *type, const char *name,
const __u8 payload[32],
- const __u8 event[32], const char *reason)
+ const __u8 event[32], const char *reason,
+ char *buf, size_t size)
{
- char buf[4096];
- struct shirudo_json_emit e = { .buf = buf, .cap = sizeof(buf) };
+ struct shirudo_json_emit e = { .buf = buf, .cap = size };
char ts[32], boot_id[64], nv_index[16], payload_hex[65], digest_hex[65];
eventlog_timestamp(ts, sizeof(ts));
@@ -2025,7 +2027,19 @@ static int append_upatch_event(const char *type, const char *name,
strerror(-e.err));
return e.err;
}
- return eventlog_append(buf);
+ return 0;
+}
+
+static int append_upatch_event(const char *type, const char *name,
+ const __u8 payload[32],
+ const __u8 event[32], const char *reason)
+{
+ char buf[4096];
+ int rc;
+
+ rc = format_upatch_event(type, name, payload, event, reason,
+ buf, sizeof(buf));
+ return rc ? rc : eventlog_append(buf);
}
/*
diff --git a/shirudo.c b/shirudo.c
index dcd65063..443f82ad 100644
--- a/shirudo.c
+++ b/shirudo.c
@@ -13,6 +13,7 @@
#include "builtin.h"
#include "util/harden.h"
+#include "util/image_runtime.h"
#include "util/help.h"
#include "util/log.h"
#include "util/macros.h"
@@ -43,7 +44,7 @@ static const struct cmd commands[] = {
{ "build", cmd_build, GROUP_OPERATOR, "compile + sign a .upatch" },
{ "image", cmd_image, GROUP_LOCAL,
- "build and verify dm-verity images" },
+ "build, verify and activate dm-verity config images" },
{ "init", cmd_init, GROUP_TARGET,
"bootstrap shirudo enforcement on this target" },
{ "upatch", cmd_upatch, GROUP_TARGET,
@@ -107,6 +108,7 @@ int main(int argc, const char **argv)
const char *names[ARRAY_SIZE(commands)];
size_t matches[ARRAY_SIZE(commands)];
size_t i, n_match;
+ int rc;
harden_stdio();
log_init();
@@ -168,5 +170,14 @@ int main(int argc, const char **argv)
return 1;
}
- return c->fn(argc - 1, argv + 1);
+ rc = shirudo_image_enter(c->name, argc - 1, argv + 1);
+
+ if (rc) {
+ fprintf(stderr, "shirudo: image state: %s\n", strerror(-rc));
+ shirudo_image_leave();
+ return 1;
+ }
+ rc = c->fn(argc - 1, argv + 1);
+ shirudo_image_leave();
+ return rc;
}
diff --git a/systemd/shirudo-initrd.service.in b/systemd/shirudo-initrd.service.in
index 3238ec20..2044c91b 100644
--- a/systemd/shirudo-initrd.service.in
+++ b/systemd/shirudo-initrd.service.in
@@ -15,7 +15,9 @@
# 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.
+# 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.
[Unit]
Description=shirudo: bring up the BPF LSM and bind the register from the initrd
diff --git a/tools/gen-initrd/module-setup.sh b/tools/gen-initrd/module-setup.sh
index 9ff9e05e..b152c1a9 100755
--- a/tools/gen-initrd/module-setup.sh
+++ b/tools/gen-initrd/module-setup.sh
@@ -65,6 +65,11 @@ 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
}
install() {
diff --git a/tools/gen-initrd/shirudo-gen-initrd b/tools/gen-initrd/shirudo-gen-initrd
index 3caf724d..f0164f70 100755
--- a/tools/gen-initrd/shirudo-gen-initrd
+++ b/tools/gen-initrd/shirudo-gen-initrd
@@ -19,12 +19,13 @@ Regenerate the initrd of a kernel with shirudo inside, through dracut.
--kver <release> kernel release (default: the running kernel)
--output <file> initrd to write (default: dracut's for the release)
--check [<file>] only check an initrd, do not build one
- --print-cmdline print the kernel parameter the initrd needs
+ --print-cmdline print the kernel parameters the initrd needs
--cert <file> primary certificate to embed (default: the installed one)
--trusted-dir <dir> further trusted certificates (default: trusted/ beside it)
--binary <file> shirudo binary to embed (default: the installed one)
--binary-path <path> where that binary lives on the root file system
--unit <file> shirudo-initrd.service to embed (default: installed)
+ --bootstrap-conf <file> authenticated bootstrap shirudo.conf to embed
--kmoddir <dir> kernel modules to take from <dir>/<release>
--no-kernel embed no kernel modules (all-built-in kernels)
--module-dir <dir> dracut module to install from (default: this
@@ -60,6 +61,7 @@ trusted_dir=
binary=
binary_path=
unit=
+bootstrap_conf=
kmoddir=
no_kernel=0
module_dir=
@@ -81,6 +83,7 @@ while [ $# -gt 0 ]; do
--binary) binary=${2:?}; shift 2 ;;
--binary-path) binary_path=${2:?}; shift 2 ;;
--unit) unit=${2:?}; shift 2 ;;
+ --bootstrap-conf) bootstrap_conf=${2:?}; shift 2 ;;
--kmoddir) kmoddir=${2:?}; shift 2 ;;
--no-kernel) no_kernel=1; shift ;;
--module-dir) module_dir=${2:?}; shift 2 ;;
@@ -90,6 +93,12 @@ while [ $# -gt 0 ]; do
esac
done
+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 ]; then
printf '%s\n' "$CMDLINE_PARAM"
exit 0
@@ -181,6 +190,7 @@ fi
[ -z "$cert" ] || [ -s "$cert" ] || die "no certificate at $cert"
[ -z "$trusted_dir" ] || [ -d "$trusted_dir" ] || die "no directory at $trusted_dir"
[ -z "$binary" ] || [ -x "$binary" ] || die "no executable at $binary"
+[ -z "$bootstrap_conf" ] || [ -s "$bootstrap_conf" ] || die "no bootstrap config at $bootstrap_conf"
if [ -n "$binary" ] && [ -z "$binary_path" ]; then
die "--binary needs --binary-path, the path of that binary on the root file system"
fi
@@ -192,7 +202,7 @@ trap 'rm -rf "$tmp"' EXIT
# dracut.conf(5) file of their own, which also shuts out the host's
# drop-ins so that a test initrd depends on nothing but what was asked.
conf_args=()
-if [ -n "$cert$trusted_dir$binary$unit" ]; then
+if [ -n "$cert$trusted_dir$binary$unit$bootstrap_conf" ]; then
{
printf 'add_dracutmodules+=" shirudo "\n'
[ -z "$cert" ] || printf 'shirudo_cert=%q\n' "$cert"
@@ -200,6 +210,7 @@ if [ -n "$cert$trusted_dir$binary$unit" ]; then
[ -z "$binary" ] || printf 'shirudo_binary=%q\n' "$binary"
[ -z "$binary_path" ] || printf 'shirudo_binary_path=%q\n' "$binary_path"
[ -z "$unit" ] || printf 'shirudo_unit=%q\n' "$unit"
+ [ -z "$bootstrap_conf" ] || printf 'shirudo_conf=%q\n' "$bootstrap_conf"
} >"$tmp/shirudo.conf"
conf_args=(--conf "$tmp/shirudo.conf" --confdir "$tmp")
fi
@@ -218,6 +229,8 @@ say "running dracut for $kver"
dracut "${args[@]}"
check_initrd "$out"
-if ! grep -q -w -- "$CMDLINE_PARAM" /proc/cmdline 2>/dev/null; then
- say "the kernel command line must carry $CMDLINE_PARAM for the initrd to enrol the certificate; this boot does not"
-fi
+for parameter in $CMDLINE_PARAM; do
+ if ! grep -q -w -- "$parameter" /proc/cmdline 2>/dev/null; then
+ say "the kernel command line must carry $parameter for initrd key enrollment; this boot does not"
+ fi
+done
diff --git a/util/bpm_tree_state.c b/util/bpm_tree_state.c
index 9c96c007..3dfa532c 100644
--- a/util/bpm_tree_state.c
+++ b/util/bpm_tree_state.c
@@ -390,8 +390,11 @@ int tree_selector_matches(const struct shirudo_tree_selector *s)
}
}
if (s->n_labels) {
- rc = read_config_file("/etc/shirudo/node.labels", labels, &nl,
- ARRAY_SIZE(labels), false);
+ const char *nodes =
+ shirudo_image_path(SHIRUDO_IMAGE_NODE_LABELS);
+
+ rc = read_config_file(nodes ?: "/etc/shirudo/node.labels",
+ labels, &nl, ARRAY_SIZE(labels), false);
if (rc == -ENOENT)
return 0;
if (rc)
diff --git a/util/chain.c b/util/chain.c
index 88352d33..7adb0065 100644
--- a/util/chain.c
+++ b/util/chain.c
@@ -137,13 +137,19 @@ void chain_tree_event(__s64 seq, const __u8 operation_digest[32],
shirudo_sha256(buf, taglen + 40, event);
}
-void chain_fold(__u8 chain[32], const __u8 event[32])
+int shirudo_chain_fold_checked(__u8 chain[32], const __u8 event[32])
{
__u8 cat[64];
memcpy(cat, chain, 32);
memcpy(cat + 32, event, 32);
- shirudo_sha256(cat, sizeof(cat), chain);
+ return sha256_buffer(cat, sizeof(cat), chain);
+}
+
+void chain_fold(__u8 chain[32], const __u8 event[32])
+{
+ if (shirudo_chain_fold_checked(chain, event))
+ memset(chain, 0, 32);
}
void chain_anchor(const __u8 name[SHIRUDO_TPM_NAME_LEN],
diff --git a/util/chain.h b/util/chain.h
index d952daed..f3b7167c 100644
--- a/util/chain.h
+++ b/util/chain.h
@@ -64,6 +64,7 @@ void chain_upatch_event(const char *tag, const char *name,
const __u8 payload[32], __u8 event[32]);
void chain_tree_event(__s64 seq, const __u8 operation_digest[32],
__u8 event[32]);
+int shirudo_chain_fold_checked(__u8 chain[32], const __u8 event[32]);
void chain_fold(__u8 chain[32], const __u8 event[32]);
/* The PCR record anchoring a register name: nvpcr-init:shirudo:0x..:<name>. */
diff --git a/util/config.c b/util/config.c
index 361afe76..e87d8372 100644
--- a/util/config.c
+++ b/util/config.c
@@ -20,6 +20,7 @@
#include "hex.h"
#include "io.h"
#include "json.h"
+#include "image_config.h"
#include "log.h"
#include "paths.h"
@@ -221,6 +222,9 @@ int config_load_path(struct shirudo_config *cfg, const char *path)
cfg->map_export_log_record_max_bytes = (__u32)v;
else
cfg->map_export_otlp_max_bytes = (__u32)v;
+ } else if (!strncmp(key, "config.", 7) ||
+ !strncmp(key, "runtime.", 8)) {
+ continue;
} else {
if (strncmp(key, "map_export.", 11) == 0)
map_export_bad(cfg, key, val, "unknown key");
@@ -256,6 +260,8 @@ int config_load(struct shirudo_config *cfg)
const char *env;
int rc;
+ if (shirudo_image_config_get(cfg))
+ return 0;
rc = config_load_path(cfg, SHIRUDO_CONF_PATH);
if (rc && rc != -ENOENT)
return rc;
diff --git a/util/config_measure.c b/util/config_measure.c
new file mode 100644
index 00000000..08e5807b
--- /dev/null
+++ b/util/config_measure.c
@@ -0,0 +1,34 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+#include <shirudo/misc.h>
+
+#include <errno.h>
+#include <string.h>
+
+#include "config_measure.h"
+#include "crypto.h"
+
+int shirudo_config_measure_pcr_valid(__u32 pcr)
+{
+ return pcr < 16 && pcr != 9 && pcr != 11 ? 0 : -EINVAL;
+}
+
+int shirudo_config_measure_event(enum shirudo_config_event_kind kind,
+ const __u8 root_hash[32], __u8 out[32])
+{
+ static const char domain[] = "shirudo-config-root-v1";
+ __u8 data[sizeof(domain) + 4 + 32];
+
+ if (!root_hash || !out ||
+ (kind != SHIRUDO_CONFIG_EVENT_BOOT &&
+ kind != SHIRUDO_CONFIG_EVENT_UPDATE))
+ return -EINVAL;
+ memcpy(data, domain, sizeof(domain));
+ data[sizeof(domain)] = 0;
+ data[sizeof(domain) + 1] = 0;
+ data[sizeof(domain) + 2] = 0;
+ data[sizeof(domain) + 3] = kind;
+ memcpy(data + sizeof(domain) + 4, root_hash, 32);
+ return sha256_buffer(data, sizeof(data), out);
+}
diff --git a/util/config_measure.h b/util/config_measure.h
new file mode 100644
index 00000000..ba6f24b2
--- /dev/null
+++ b/util/config_measure.h
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+/* Copyright (c) 2026 Authors of shirudo */
+
+#ifndef SHIRUDO_CONFIG_MEASURE_H
+#define SHIRUDO_CONFIG_MEASURE_H
+
+#include <shirudo/misc.h>
+
+#include <stdint.h>
+
+#include "config_update.h"
+
+int shirudo_config_measure_pcr_valid(__u32 pcr);
+int shirudo_config_measure_event(enum shirudo_config_event_kind kind,
+ const __u8 root_hash[32], __u8 out[32]);
+
+#endif /* SHIRUDO_CONFIG_MEASURE_H */
diff --git a/util/config_update.c b/util/config_update.c
new file mode 100644
index 00000000..dfe7f909
--- /dev/null
+++ b/util/config_update.c
@@ -0,0 +1,657 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+#include <shirudo/misc.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/file.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "chain.h"
+#include "config_measure.h"
+#include "config_update.h"
+#include "hex.h"
+#include "io.h"
+#include "json.h"
+#include "log.h"
+#include "macros.h"
+
+#define RECORD_MAX (2 * SHIRUDO_CONFIG_UPDATE_PREFIX_MAX + 1024)
+
+static int target_valid(const struct shirudo_config_update_target *target)
+{
+ size_t len, i;
+
+ if (!target)
+ return -EINVAL;
+ len = strnlen(target->prefix, sizeof(target->prefix));
+ if (!len || len == sizeof(target->prefix) || target->prefix[0] != '/')
+ return -EINVAL;
+ for (i = 0; i < len; i++)
+ if ((__u8)target->prefix[i] < 0x20 ||
+ (__u8)target->prefix[i] == 0x7f)
+ return -EINVAL;
+ return 0;
+}
+
+static int record_encode(const struct shirudo_config_update_record *record,
+ bool state, char *out, size_t cap, size_t *length)
+{
+ struct shirudo_json_emit emit = { .buf = out, .cap = cap - 1 };
+ char root[65], initial[65];
+
+ hex_encode(record->target.root_hash, 32, root);
+ hex_encode(record->initial_pcr, 32, initial);
+ json_obj_open(&emit);
+ json_kv_int(&emit, "schema", 1);
+ json_kv_int(&emit, "sequence", record->sequence);
+ json_kv_int(&emit, "reset_count", record->reset_count);
+ json_kv_int(&emit, "restart_count", record->restart_count);
+ json_kv_int(&emit, "pcr", record->pcr);
+ json_kv_int(&emit, "kind", record->kind);
+ json_kv_string(&emit, "root_hash", root);
+ json_kv_string(&emit, "initial_pcr", initial);
+ if (state) {
+ json_kv_string(&emit, "prefix", record->target.prefix);
+ json_kv_bool(&emit, "pending", record->pending);
+ }
+ json_obj_close(&emit);
+ if (emit.err)
+ return emit.err;
+ out[emit.len++] = '\n';
+ *length = emit.len;
+ return 0;
+}
+
+static int record_decode(const char *data, size_t length, bool state,
+ struct shirudo_config_update_record *record)
+{
+ __s64 schema = 0, sequence = 0, reset = 0, restart = 0;
+ char root[65] = {}, initial[65] = {};
+ __s64 pcr = 0, kind = 0;
+ int pending = 0;
+ size_t i;
+ int rc;
+ struct shirudo_json_field fields[] = {
+ { "schema", SHIRUDO_JSON_T_INT, &schema, 0, 0, NULL, 0 },
+ { "sequence", SHIRUDO_JSON_T_INT, &sequence, 0, 0, NULL, 0 },
+ { "reset_count", SHIRUDO_JSON_T_INT, &reset, 0, 0, NULL, 0 },
+ { "restart_count", SHIRUDO_JSON_T_INT, &restart, 0, 0,
+ NULL, 0 },
+ { "pcr", SHIRUDO_JSON_T_INT, &pcr, 0, 0, NULL, 0 },
+ { "kind", SHIRUDO_JSON_T_INT, &kind, 0, 0, NULL, 0 },
+ { "root_hash", SHIRUDO_JSON_T_STRING, root, sizeof(root), 0,
+ NULL, 0 },
+ { "initial_pcr", SHIRUDO_JSON_T_STRING, initial,
+ sizeof(initial), 0, NULL, 0 },
+ { "prefix", SHIRUDO_JSON_T_STRING, record->target.prefix,
+ sizeof(record->target.prefix), 0, NULL, 0 },
+ { "pending", SHIRUDO_JSON_T_BOOL, &pending, 0, 0, NULL, 0 },
+ };
+
+ memset(record, 0, sizeof(*record));
+ rc = json_parse(data, length, fields, ARRAY_SIZE(fields));
+ if (rc)
+ return rc;
+ for (i = 0; i < ARRAY_SIZE(fields) - (state ? 0 : 2); i++)
+ if (!fields[i].seen)
+ return -EBADMSG;
+ if (schema != 1 || sequence <= 0 || reset < 0 ||
+ reset > UINT32_MAX || restart < 0 || restart > UINT32_MAX ||
+ pcr < 0 || pcr > 15 || shirudo_config_measure_pcr_valid(pcr) ||
+ (kind != SHIRUDO_CONFIG_EVENT_BOOT &&
+ kind != SHIRUDO_CONFIG_EVENT_UPDATE) ||
+ hex_decode(root, record->target.root_hash, 32) ||
+ hex_decode(initial, record->initial_pcr, 32) ||
+ (state && target_valid(&record->target)))
+ return -EBADMSG;
+ record->sequence = sequence;
+ record->reset_count = reset;
+ record->restart_count = restart;
+ record->pcr = pcr;
+ record->kind = kind;
+ record->pending = pending;
+ return 0;
+}
+
+static int regular_file(int fd)
+{
+ struct stat st;
+
+ if (fstat(fd, &st))
+ return -errno;
+ if (!S_ISREG(st.st_mode) || st.st_nlink != 1 ||
+ st.st_uid != geteuid() || (st.st_mode & 077))
+ return -EPERM;
+ return 0;
+}
+
+static int checkpoint(struct shirudo_config_update *update,
+ enum shirudo_config_update_checkpoint point)
+{
+ return update->ops->checkpoint ?
+ update->ops->checkpoint(update->ctx, point) :
+ 0;
+}
+
+int shirudo_config_update_open(const char *directory,
+ const struct shirudo_config_update_ops *ops,
+ void *ctx, struct shirudo_config_update *out)
+{
+ struct stat st;
+ int rc;
+
+ if (!out)
+ return -EINVAL;
+ memset(out, 0, sizeof(*out));
+ out->dir_fd = -1;
+ out->lock_fd = -1;
+ if (!directory || !ops || !ops->boot_identity ||
+ !ops->pcr_read || !ops->pcr_extend || !ops->prepare ||
+ !ops->activate || !ops->seal)
+ return -EINVAL;
+ out->ops = ops;
+ out->ctx = ctx;
+ out->dir_fd = open(directory,
+ O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
+ if (out->dir_fd < 0)
+ return -errno;
+ if (fstat(out->dir_fd, &st)) {
+ rc = -errno;
+ goto fail;
+ }
+ if (st.st_uid != geteuid() || (st.st_mode & 022)) {
+ rc = -EPERM;
+ goto fail;
+ }
+ rc = ops->seal(ctx, out->dir_fd);
+ if (rc)
+ goto fail;
+ out->lock_fd = openat(out->dir_fd,
+ "lock",
+ O_RDWR | O_CREAT | O_CLOEXEC |
+ O_NOFOLLOW | O_NONBLOCK, 0600);
+ if (out->lock_fd < 0) {
+ rc = -errno;
+ goto fail;
+ }
+ rc = regular_file(out->lock_fd);
+ if (rc)
+ goto fail;
+ if (flock(out->lock_fd, LOCK_EX | LOCK_NB)) {
+ rc = -errno;
+ goto fail;
+ }
+ rc = ops->seal(ctx, out->lock_fd);
+ if (rc)
+ goto fail;
+ if (fsync(out->lock_fd) || fsync(out->dir_fd)) {
+ rc = -errno;
+ goto fail;
+ }
+ return 0;
+fail:
+ shirudo_config_update_close(out);
+ return rc;
+}
+
+void shirudo_config_update_close(struct shirudo_config_update *update)
+{
+ if (!update)
+ return;
+ if (update->lock_fd >= 0)
+ close(update->lock_fd);
+ if (update->dir_fd >= 0)
+ close(update->dir_fd);
+ update->dir_fd = -1;
+ update->lock_fd = -1;
+}
+
+int shirudo_config_update_read(struct shirudo_config_update *update,
+ struct shirudo_config_update_record *out)
+{
+ char wire[RECORD_MAX];
+ struct stat st;
+ size_t done = 0;
+ ssize_t got;
+ int fd, rc;
+
+ if (!update || update->dir_fd < 0 || !out)
+ return -EINVAL;
+ fd = openat(update->dir_fd, "state",
+ O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK);
+ if (fd < 0)
+ return -errno;
+ rc = regular_file(fd);
+ if (!rc && fstat(fd, &st))
+ rc = -errno;
+ if (!rc && (st.st_size <= 0 || (__u64)st.st_size >= sizeof(wire)))
+ rc = -EBADMSG;
+ while (!rc && done < (size_t)st.st_size) {
+ got = read(fd, wire + done, st.st_size - done);
+ if (got < 0) {
+ if (errno == EINTR)
+ continue;
+ rc = -errno;
+ } else if (!got) {
+ rc = -EIO;
+ } else {
+ done += got;
+ }
+ }
+ if (!rc)
+ rc = record_decode(wire, done, true, out);
+ close(fd);
+ return rc;
+}
+
+static int state_save(struct shirudo_config_update *update,
+ const struct shirudo_config_update_record *record,
+ bool inject)
+{
+ char wire[RECORD_MAX];
+ size_t length;
+ int fd, rc;
+
+ rc = record_encode(record, true, wire, sizeof(wire), &length);
+ if (rc)
+ return rc;
+ if (unlinkat(update->dir_fd, ".state.tmp", 0) && errno != ENOENT)
+ return -errno;
+ fd = openat(update->dir_fd, ".state.tmp",
+ O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC | O_NOFOLLOW, 0600);
+ if (fd < 0)
+ return -errno;
+ rc = update->ops->seal(update->ctx, fd);
+ if (!rc)
+ rc = write_complete(fd, wire, length);
+ if (!rc && inject)
+ rc = checkpoint(update, SHIRUDO_CONFIG_CHECK_STATE_WRITE);
+ if (!rc && fsync(fd))
+ rc = -errno;
+ if (close(fd) && !rc)
+ rc = -errno;
+ if (!rc &&
+ renameat(update->dir_fd, ".state.tmp", update->dir_fd, "state"))
+ rc = -errno;
+ if (!rc && inject)
+ rc = checkpoint(update, SHIRUDO_CONFIG_CHECK_STATE_RENAME);
+ if (!rc && fsync(update->dir_fd))
+ rc = -errno;
+ if (rc)
+ unlinkat(update->dir_fd, ".state.tmp", 0);
+ return rc;
+}
+
+static bool same_boot(const struct shirudo_config_update_record *a,
+ const struct shirudo_config_update_record *b)
+{
+ return a->reset_count == b->reset_count &&
+ a->restart_count == b->restart_count;
+}
+
+static bool same_event(const struct shirudo_config_update_record *a,
+ const struct shirudo_config_update_record *b)
+{
+ return same_boot(a, b) && a->sequence == b->sequence &&
+ a->pcr == b->pcr && a->kind == b->kind &&
+ !memcmp(a->initial_pcr, b->initial_pcr, 32) &&
+ !memcmp(a->target.root_hash, b->target.root_hash, 32);
+}
+
+static int event_follows(const struct shirudo_config_update_record *previous,
+ const struct shirudo_config_update_record *record)
+{
+ if (record->sequence != previous->sequence + 1)
+ return -EUCLEAN;
+ if (!previous->sequence || !same_boot(previous, record))
+ return record->kind == SHIRUDO_CONFIG_EVENT_BOOT ? 0 :
+ -EUCLEAN;
+ if (record->kind != SHIRUDO_CONFIG_EVENT_UPDATE ||
+ previous->pcr != record->pcr ||
+ memcmp(previous->initial_pcr, record->initial_pcr, 32))
+ return -EUCLEAN;
+ return 0;
+}
+
+/* A trailing partial append can be discarded only on a later trusted boot. */
+static int read_line(FILE *file, char *line, size_t cap, size_t *length)
+{
+ int c;
+
+ *length = 0;
+ while ((c = fgetc(file)) != EOF) {
+ if (!c || *length == cap)
+ return -EBADMSG;
+ line[(*length)++] = c;
+ if (c == '\n')
+ return 1;
+ }
+ if (ferror(file))
+ return -EIO;
+ return *length ? -ENODATA : 0;
+}
+
+static int events_scan(struct shirudo_config_update *update,
+ struct shirudo_config_update_record *last,
+ __u8 value[32], off_t *end, bool partial,
+ shirudo_config_cb consume, void *ctx)
+{
+ struct shirudo_config_update_record previous = {}, record;
+ const __u8 *root = record.target.root_hash;
+ __u8 running[32] = {}, digest[32];
+ char wire[RECORD_MAX];
+ size_t length;
+ int fd, rc;
+ FILE *file;
+
+ memset(last, 0, sizeof(*last));
+ if (end)
+ *end = 0;
+ fd = openat(update->dir_fd, "events",
+ O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK);
+ if (fd < 0)
+ return errno == ENOENT ? 0 : -errno;
+ rc = regular_file(fd);
+ if (rc) {
+ close(fd);
+ return rc;
+ }
+ file = fdopen(fd, "r");
+ if (!file) {
+ rc = -errno;
+ close(fd);
+ return rc;
+ }
+ while ((rc = read_line(file, wire, sizeof(wire), &length)) > 0) {
+ rc = record_decode(wire, length, false, &record);
+ if (!rc)
+ rc = event_follows(&previous, &record);
+ if (!rc && record.kind == SHIRUDO_CONFIG_EVENT_BOOT)
+ memcpy(running, record.initial_pcr, 32);
+ if (!rc)
+ rc = shirudo_config_measure_event(record.kind,
+ root, digest);
+ if (!rc)
+ rc = shirudo_chain_fold_checked(running, digest);
+ if (!rc && consume)
+ rc = consume(&record, ctx);
+ if (rc)
+ break;
+ previous = record;
+ if (end) {
+ *end = ftello(file);
+ if (*end < 0) {
+ rc = -errno;
+ break;
+ }
+ }
+ }
+ if (rc == -ENODATA && partial)
+ rc = 0;
+ if (!rc) {
+ *last = previous;
+ if (value)
+ memcpy(value, running, 32);
+ }
+ fclose(file);
+ return rc;
+}
+
+int shirudo_config_update_events(struct shirudo_config_update *update,
+ shirudo_config_cb consume, void *ctx)
+{
+ struct shirudo_config_update_record last;
+
+ if (!update || update->dir_fd < 0 || !consume)
+ return -EINVAL;
+ return events_scan(update, &last, NULL, NULL, false, consume, ctx);
+}
+
+static int event_append(struct shirudo_config_update *update,
+ const struct shirudo_config_update_record *record)
+{
+ struct shirudo_config_update_record last;
+ char wire[RECORD_MAX];
+ size_t length;
+ int fd, rc;
+ off_t end;
+
+ rc = events_scan(update, &last, NULL, &end,
+ record->kind == SHIRUDO_CONFIG_EVENT_BOOT, NULL, NULL);
+ if (!rc)
+ rc = event_follows(&last, record);
+ if (!rc)
+ rc = record_encode(record, false, wire, sizeof(wire), &length);
+ if (rc)
+ return rc;
+ fd = openat(update->dir_fd, "events",
+ O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC | O_NOFOLLOW |
+ O_NONBLOCK, 0600);
+ if (fd < 0)
+ return -errno;
+ rc = regular_file(fd);
+ if (!rc)
+ rc = update->ops->seal(update->ctx, fd);
+ if (!rc && ftruncate(fd, end))
+ rc = -errno;
+ if (!rc)
+ rc = write_complete(fd, wire, length);
+ if (!rc)
+ rc = checkpoint(update, SHIRUDO_CONFIG_CHECK_EVENT_WRITE);
+ if (!rc && fsync(fd))
+ rc = -errno;
+ if (close(fd) && !rc)
+ rc = -errno;
+ if (!rc && fsync(update->dir_fd))
+ rc = -errno;
+ return rc;
+}
+
+static int live_read(struct shirudo_config_update *update, __u32 pcr,
+ __u32 *reset, __u32 *restart, __u8 value[32])
+{
+ __u32 reset_after, restart_after;
+ int rc;
+
+ rc = update->ops->boot_identity(update->ctx, reset, restart);
+ if (!rc)
+ rc = update->ops->pcr_read(update->ctx, pcr, value);
+ if (!rc)
+ rc = update->ops->boot_identity(update->ctx, &reset_after,
+ &restart_after);
+ if (!rc && (*reset != reset_after || *restart != restart_after))
+ rc = -ESTALE;
+ return rc;
+}
+
+static int check_record(struct shirudo_config_update *update,
+ const struct shirudo_config_update_record *record)
+{
+ struct shirudo_config_update_record last;
+ __u8 expected[32], value[32];
+ __u32 reset, restart;
+ int rc;
+
+ rc = events_scan(update, &last, expected, NULL, false, NULL, NULL);
+ if (!rc && !same_event(record, &last))
+ rc = -EUCLEAN;
+ if (!rc)
+ rc = live_read(update, record->pcr, &reset, &restart, value);
+ if (!rc && (reset != record->reset_count ||
+ restart != record->restart_count))
+ rc = -ESTALE;
+ if (!rc && memcmp(value, expected, 32))
+ rc = -EUCLEAN;
+ return rc;
+}
+
+int shirudo_config_update_check(struct shirudo_config_update *update,
+ struct shirudo_config_update_record *out)
+{
+ struct shirudo_config_update_record record;
+ int rc;
+
+ rc = shirudo_config_update_read(update, &record);
+ if (!rc && record.pending)
+ rc = -EINPROGRESS;
+ if (!rc)
+ rc = check_record(update, &record);
+ if (!rc && out)
+ *out = record;
+ return rc;
+}
+
+static int complete(struct shirudo_config_update *update)
+{
+ struct shirudo_config_update_record record;
+ int rc, restore;
+
+ rc = shirudo_config_update_read(update, &record);
+ if (!rc)
+ rc = check_record(update, &record);
+ if (rc)
+ return rc;
+ record.pending = false;
+ rc = state_save(update, &record, true);
+ if (!rc)
+ rc = checkpoint(update, SHIRUDO_CONFIG_CHECK_COMPLETE_SYNC);
+ if (rc) {
+ record.pending = true;
+ restore = state_save(update, &record, false);
+ if (restore)
+ pr_err("image: cannot restore pending selection: %s",
+ strerror(-restore));
+ }
+ return rc;
+}
+
+static int begin_event(struct shirudo_config_update *update,
+ const struct shirudo_config_update_target *target,
+ const struct shirudo_config_update_record *previous,
+ __u32 pcr, enum shirudo_config_event_kind kind)
+{
+ struct shirudo_config_update_record record = {};
+ __u8 before[32], expected[32], value[32], event[32];
+ __u32 reset, restart;
+ int rc;
+
+ if (previous->sequence == INT64_MAX)
+ return -EOVERFLOW;
+ rc = update->ops->prepare(update->ctx, target);
+ if (rc)
+ return rc;
+ record.target = *target;
+ record.sequence = previous->sequence + 1;
+ record.pcr = pcr;
+ record.kind = kind;
+ record.pending = true;
+ rc = live_read(update, pcr, &record.reset_count, &record.restart_count,
+ before);
+ if (rc)
+ return rc;
+ memcpy(record.initial_pcr, kind == SHIRUDO_CONFIG_EVENT_BOOT ?
+ before : previous->initial_pcr, 32);
+ if (!rc)
+ rc = event_follows(previous, &record);
+ if (!rc)
+ rc = shirudo_config_measure_event(kind, target->root_hash,
+ event);
+ memcpy(expected, before, 32);
+ if (!rc)
+ rc = shirudo_chain_fold_checked(expected, event);
+ if (!rc)
+ rc = state_save(update, &record, true);
+ if (!rc)
+ rc = checkpoint(update, SHIRUDO_CONFIG_CHECK_PENDING_SYNC);
+ if (!rc)
+ rc = update->ops->pcr_extend(update->ctx, pcr, event);
+ if (!rc)
+ rc = checkpoint(update, SHIRUDO_CONFIG_CHECK_EXTEND);
+ if (!rc)
+ rc = live_read(update, pcr, &reset, &restart, value);
+ if (!rc && (reset != record.reset_count ||
+ restart != record.restart_count ||
+ memcmp(value, expected, 32)))
+ rc = -EUCLEAN;
+ if (!rc)
+ rc = event_append(update, &record);
+ if (!rc)
+ rc = checkpoint(update, SHIRUDO_CONFIG_CHECK_EVENT_SYNC);
+ if (!rc)
+ rc = update->ops->activate(update->ctx, target);
+ if (!rc)
+ rc = checkpoint(update, SHIRUDO_CONFIG_CHECK_ACTIVATE);
+ if (rc)
+ return rc;
+ if (kind == SHIRUDO_CONFIG_EVENT_BOOT) {
+ update->boot_pending = true;
+ return 0;
+ }
+ return complete(update);
+}
+
+int shirudo_config_update_boot(struct shirudo_config_update *update,
+ const struct shirudo_config_update_target *t,
+ __u32 pcr)
+{
+ struct shirudo_config_update_record previous = {}, last;
+ __u32 reset, restart;
+ int rc;
+
+ if (!update || update->dir_fd < 0 || target_valid(t) ||
+ shirudo_config_measure_pcr_valid(pcr))
+ return -EINVAL;
+ update->boot_pending = false;
+ rc = shirudo_config_update_read(update, &previous);
+ if (rc && rc != -ENOENT)
+ return rc;
+ rc = update->ops->boot_identity(update->ctx, &reset, &restart);
+ if (rc)
+ return rc;
+ if (previous.sequence && previous.reset_count == reset &&
+ previous.restart_count == restart) {
+ if (previous.pending)
+ return -EINPROGRESS;
+ if (previous.pcr != pcr ||
+ memcmp(t->root_hash, previous.target.root_hash, 32))
+ return -EBUSY;
+ return shirudo_config_update_check(update, NULL);
+ }
+ if (previous.sequence &&
+ (strcmp(t->prefix, previous.target.prefix) ||
+ memcmp(t->root_hash, previous.target.root_hash, 32)))
+ return -EUCLEAN;
+ rc = events_scan(update, &last, NULL, NULL, previous.pending,
+ NULL, NULL);
+ if (rc)
+ return rc;
+ if (!previous.sequence && last.sequence)
+ return -EUCLEAN;
+ if (previous.sequence &&
+ !(same_event(&previous, &last) ||
+ (previous.pending && !event_follows(&last, &previous))))
+ return -EUCLEAN;
+ return begin_event(update, t, &last, pcr,
+ SHIRUDO_CONFIG_EVENT_BOOT);
+}
+
+int shirudo_config_update_complete_boot(struct shirudo_config_update *update)
+{
+ int rc;
+
+ if (!update || update->dir_fd < 0)
+ return -EINVAL;
+ if (!update->boot_pending)
+ return shirudo_config_update_check(update, NULL);
+ update->boot_pending = false;
+ rc = complete(update);
+ return rc;
+}
diff --git a/util/config_update.h b/util/config_update.h
new file mode 100644
index 00000000..8ef6003f
--- /dev/null
+++ b/util/config_update.h
@@ -0,0 +1,90 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+/* Copyright (c) 2026 Authors of shirudo */
+
+#ifndef SHIRUDO_CONFIG_UPDATE_H
+#define SHIRUDO_CONFIG_UPDATE_H
+
+#include <shirudo/misc.h>
+
+#include <stdbool.h>
+#include <stdint.h>
+
+#define SHIRUDO_CONFIG_UPDATE_PREFIX_MAX 4096u
+
+enum shirudo_config_event_kind {
+ SHIRUDO_CONFIG_EVENT_BOOT = 1,
+ SHIRUDO_CONFIG_EVENT_UPDATE = 2,
+};
+
+enum shirudo_config_update_checkpoint {
+ SHIRUDO_CONFIG_CHECK_PENDING_SYNC = 1,
+ SHIRUDO_CONFIG_CHECK_EXTEND,
+ SHIRUDO_CONFIG_CHECK_EVENT_SYNC,
+ SHIRUDO_CONFIG_CHECK_ACTIVATE,
+ SHIRUDO_CONFIG_CHECK_COMPLETE_SYNC,
+ SHIRUDO_CONFIG_CHECK_STATE_WRITE,
+ SHIRUDO_CONFIG_CHECK_STATE_RENAME,
+ SHIRUDO_CONFIG_CHECK_EVENT_WRITE,
+};
+
+struct shirudo_config_update_target {
+ char prefix[SHIRUDO_CONFIG_UPDATE_PREFIX_MAX];
+ __u8 root_hash[32];
+};
+
+struct shirudo_config_update_record {
+ struct shirudo_config_update_target target;
+ __u8 initial_pcr[32];
+ __u64 sequence;
+ __u32 reset_count;
+ __u32 restart_count;
+ __u32 pcr;
+ enum shirudo_config_event_kind kind;
+ bool pending;
+};
+
+/* prepare verifies the signed mapping and contents before saving intent. */
+struct shirudo_config_update_ops {
+ int (*boot_identity)(void *ctx, __u32 *reset, __u32 *restart);
+ int (*pcr_read)(void *ctx, __u32 pcr, __u8 out[32]);
+ int (*pcr_extend)(void *ctx, __u32 pcr, const __u8 event[32]);
+ int (*prepare)(void *ctx,
+ const struct shirudo_config_update_target *target);
+ int (*activate)(void *ctx,
+ const struct shirudo_config_update_target *target);
+ int (*seal)(void *ctx, int fd);
+ int (*checkpoint)(void *ctx,
+ enum shirudo_config_update_checkpoint checkpoint);
+};
+
+typedef int (*shirudo_config_cb)(const struct shirudo_config_update_record *r,
+ void *ctx);
+
+struct shirudo_config_update {
+ const struct shirudo_config_update_ops *ops;
+ void *ctx;
+ int dir_fd;
+ int lock_fd;
+ bool boot_pending;
+};
+
+/* Holds the update lock until close. The directory must already exist. */
+int shirudo_config_update_open(const char *directory,
+ const struct shirudo_config_update_ops *ops,
+ void *ctx, struct shirudo_config_update *out);
+void shirudo_config_update_close(struct shirudo_config_update *update);
+int shirudo_config_update_read(struct shirudo_config_update *update,
+ struct shirudo_config_update_record *out);
+/* Boot leaves intent pending until upatch restoration and NV init succeed. */
+int shirudo_config_update_boot(struct shirudo_config_update *update,
+ const struct shirudo_config_update_target *t,
+ __u32 pcr);
+int shirudo_config_update_complete_boot(struct shirudo_config_update *update);
+/* Refuses pending updates and PCR drift; callers also validate live mounts. */
+int shirudo_config_update_check(struct shirudo_config_update *update,
+ struct shirudo_config_update_record *out);
+/* Events describe selections, not a proof that activation completed. */
+int shirudo_config_update_events(struct shirudo_config_update *update,
+ shirudo_config_cb consume, void *ctx);
+
+#endif /* SHIRUDO_CONFIG_UPDATE_H */
diff --git a/util/eventlog.c b/util/eventlog.c
index 82c31e05..d95408e6 100644
--- a/util/eventlog.c
+++ b/util/eventlog.c
@@ -30,14 +30,16 @@
const char *eventlog_path(void)
{
- const char *env = getenv("SHIRUDO_EVENTLOG_PATH");
+ const char *env = shirudo_image_config_active() ? NULL :
+ getenv("SHIRUDO_EVENTLOG_PATH");
return env && *env ? env : SHIRUDO_EVENTLOG_PATH;
}
const char *eventlog_pending_path(void)
{
- const char *env = getenv("SHIRUDO_EVENTLOG_PENDING_PATH");
+ const char *env = shirudo_image_config_active() ? NULL :
+ getenv("SHIRUDO_EVENTLOG_PENDING_PATH");
return env && *env ? env : SHIRUDO_EVENTLOG_PENDING_PATH;
}
diff --git a/util/image_config.c b/util/image_config.c
new file mode 100644
index 00000000..5b6c374c
--- /dev/null
+++ b/util/image_config.c
@@ -0,0 +1,268 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+#include <shirudo/misc.h>
+#include <shirudo/string.h>
+
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "config.h"
+#include "image_config.h"
+#include "malloc.h"
+
+static const char *const relative[SHIRUDO_IMAGE_PATH_COUNT] = {
+ [SHIRUDO_IMAGE_ETC] = "etc/shirudo/bpm",
+ [SHIRUDO_IMAGE_REGISTRIES] = "etc/shirudo/bpm/registries.d",
+ [SHIRUDO_IMAGE_REPOSITORY_KEYS] = "etc/shirudo/bpm/keys",
+ [SHIRUDO_IMAGE_TREE_CONF] = "etc/shirudo/bpm/upatch-tree.conf",
+ [SHIRUDO_IMAGE_NODE_LABELS] = "etc/shirudo/node.labels",
+ [SHIRUDO_IMAGE_TRUSTED_KEYS] = "keys/trusted",
+ [SHIRUDO_IMAGE_REVOKED_KEYS] = "keys/revoked",
+};
+
+static char paths[SHIRUDO_IMAGE_PATH_COUNT][PATH_MAX];
+static struct shirudo_config effective;
+static bool active;
+static int config_fd = -1;
+
+bool shirudo_image_config_active(void)
+{
+ return active;
+}
+
+int shirudo_image_config_get(struct shirudo_config *out)
+{
+ if (!active)
+ return 0;
+ *out = effective;
+ return 1;
+}
+
+int shirudo_image_config_root_fd(void)
+{
+ return config_fd;
+}
+
+const char *shirudo_image_path(enum shirudo_image_path which)
+{
+ if (which >= SHIRUDO_IMAGE_PATH_COUNT || config_fd < 0)
+ return NULL;
+ return paths[which];
+}
+
+int shirudo_image_path_relative(char *out, __u64 cap, const char *path)
+{
+ int n;
+
+ if (config_fd < 0 || !path || path[0] == '/' || strstr(path, ".."))
+ return -EINVAL;
+ n = snprintf(out, cap, "/proc/self/fd/%d/%s", config_fd, path);
+ return n < 0 || (__u64)n >= cap ? -ENAMETOOLONG : 0;
+}
+
+static int validate_tree(int root_fd, dev_t device, __u32 depth,
+ __u32 *entries)
+{
+ struct dirent *entry;
+ struct stat st;
+ DIR *dir;
+ int fd, rc = 0;
+
+ if (depth > 32)
+ return -ELOOP;
+ fd = openat(root_fd, ".", O_RDONLY | O_DIRECTORY | O_CLOEXEC);
+ if (fd < 0)
+ return -errno;
+ dir = fdopendir(fd);
+ if (!dir) {
+ rc = -errno;
+ close(fd);
+ return rc;
+ }
+ errno = 0;
+ while ((entry = readdir(dir)) != NULL) {
+ if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
+ continue;
+ if (++*entries > 65536) {
+ rc = -E2BIG;
+ break;
+ }
+ if (fstatat(fd, entry->d_name, &st, AT_SYMLINK_NOFOLLOW)) {
+ rc = -errno;
+ break;
+ }
+ if (st.st_dev != device ||
+ (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))) {
+ rc = -EPERM;
+ break;
+ }
+ if (S_ISDIR(st.st_mode)) {
+ int child = openat(fd, entry->d_name,
+ O_RDONLY | O_DIRECTORY | O_CLOEXEC |
+ O_NOFOLLOW);
+
+ if (child < 0) {
+ rc = -errno;
+ break;
+ }
+ rc = validate_tree(child, device, depth + 1, entries);
+ close(child);
+ if (rc)
+ break;
+ }
+ errno = 0;
+ }
+ if (!entry && errno && !rc)
+ rc = -errno;
+ closedir(dir);
+ return rc;
+}
+
+static int validate_settings(int root_fd)
+{
+ char *line = NULL;
+ size_t capacity = 0;
+ FILE *file;
+ ssize_t length;
+ int fd, rc = 0;
+
+ fd = openat(root_fd, "etc/shirudo/shirudo.conf",
+ O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
+ if (fd < 0)
+ return -errno;
+ file = fdopen(fd, "r");
+ if (!file) {
+ rc = -errno;
+ close(fd);
+ return rc;
+ }
+ while ((length = getline(&line, &capacity, file)) >= 0) {
+ char *p = line, *end, *value;
+
+ if (length > 1023 || strlen(line) != (size_t)length) {
+ rc = -E2BIG;
+ break;
+ }
+ while (*p == ' ' || *p == '\t')
+ p++;
+ if (*p == '#' || *p == '\n' || !*p)
+ continue;
+ value = strchr(p, '=');
+ if (!value) {
+ rc = -EINVAL;
+ break;
+ }
+ *value++ = 0;
+ end = p + strlen(p);
+ while (end > p && (end[-1] == ' ' || end[-1] == '\t'))
+ end--;
+ *end = 0;
+ if (!strcmp(p, "eventlog.max_bytes")) {
+ __u64 bytes;
+
+ while (*value == ' ' || *value == '\t')
+ value++;
+ end = value + strlen(value);
+ while (end > value && (end[-1] == ' ' ||
+ end[-1] == '\t' || end[-1] == '\r' ||
+ end[-1] == '\n'))
+ end--;
+ if (parse_u64(value, end - value, ~0ULL, &bytes)) {
+ rc = -EINVAL;
+ break;
+ }
+ } else if (strncmp(p, "map_export.", 11)) {
+ rc = -EPERM;
+ break;
+ }
+ }
+ if (ferror(file) && !rc)
+ rc = -EIO;
+ libc_free(line);
+ fclose(file);
+ return rc;
+}
+
+int shirudo_image_config_validate(int root_fd)
+{
+ struct shirudo_config candidate;
+ __u32 entries = 0;
+ char path[64];
+ struct stat st;
+ int rc;
+
+ if (fstat(root_fd, &st))
+ return -errno;
+ if (!S_ISDIR(st.st_mode))
+ return -ENOTDIR;
+ rc = validate_tree(root_fd, st.st_dev, 0, &entries);
+ if (!rc)
+ rc = validate_settings(root_fd);
+ if (!rc) {
+ snprintf(path, sizeof(path),
+ "/proc/self/fd/%d/etc/shirudo/shirudo.conf", root_fd);
+ rc = config_load_path(&candidate, path);
+ if (!rc && !candidate.map_export_valid)
+ rc = -EINVAL;
+ explicit_bzero(&candidate, sizeof(candidate));
+ }
+ return rc;
+}
+
+int shirudo_image_config_set(const struct shirudo_config *bootstrap,
+ int root_fd)
+{
+ struct shirudo_config candidate;
+ char path[PATH_MAX];
+ int fd = -1, rc;
+ __u32 i;
+
+ candidate = *bootstrap;
+ if (root_fd >= 0) {
+ rc = shirudo_image_config_validate(root_fd);
+ if (rc)
+ return rc;
+ fd = fcntl(root_fd, F_DUPFD_CLOEXEC, 3);
+ if (fd < 0)
+ return -errno;
+ snprintf(path, sizeof(path),
+ "/proc/self/fd/%d/etc/shirudo/shirudo.conf", fd);
+ rc = config_load_path(&candidate, path);
+ if (rc || !candidate.map_export_valid) {
+ close(fd);
+ return rc ? rc : -EINVAL;
+ }
+ memcpy(candidate.tpm_owner_auth, bootstrap->tpm_owner_auth,
+ sizeof(candidate.tpm_owner_auth));
+ candidate.tpm_owner_auth_len = bootstrap->tpm_owner_auth_len;
+ memcpy(candidate.tpm_tcti, bootstrap->tpm_tcti,
+ sizeof(candidate.tpm_tcti));
+ }
+ if (config_fd >= 0)
+ close(config_fd);
+ config_fd = fd;
+ effective = candidate;
+ active = true;
+ for (i = 0; i < SHIRUDO_IMAGE_PATH_COUNT; i++)
+ snprintf(paths[i], sizeof(paths[i]), "/proc/self/fd/%d/%s",
+ config_fd, relative[i]);
+ return 0;
+}
+
+void shirudo_image_config_clear(void)
+{
+ if (config_fd >= 0)
+ close(config_fd);
+ config_fd = -1;
+ active = false;
+ explicit_bzero(&effective, sizeof(effective));
+ memset(paths, 0, sizeof(paths));
+}
diff --git a/util/image_config.h b/util/image_config.h
new file mode 100644
index 00000000..85ac3047
--- /dev/null
+++ b/util/image_config.h
@@ -0,0 +1,34 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+/* Copyright (c) 2026 Authors of shirudo */
+
+#ifndef SHIRUDO_IMAGE_CONFIG_H
+#define SHIRUDO_IMAGE_CONFIG_H
+
+#include <shirudo/misc.h>
+
+#include <stdbool.h>
+
+struct shirudo_config;
+
+enum shirudo_image_path {
+ SHIRUDO_IMAGE_ETC,
+ SHIRUDO_IMAGE_REGISTRIES,
+ SHIRUDO_IMAGE_REPOSITORY_KEYS,
+ SHIRUDO_IMAGE_TREE_CONF,
+ SHIRUDO_IMAGE_NODE_LABELS,
+ SHIRUDO_IMAGE_TRUSTED_KEYS,
+ SHIRUDO_IMAGE_REVOKED_KEYS,
+ SHIRUDO_IMAGE_PATH_COUNT,
+};
+
+bool shirudo_image_config_active(void);
+int shirudo_image_config_get(struct shirudo_config *out);
+int shirudo_image_config_set(const struct shirudo_config *bootstrap,
+ int root_fd);
+int shirudo_image_config_validate(int root_fd);
+void shirudo_image_config_clear(void);
+const char *shirudo_image_path(enum shirudo_image_path which);
+int shirudo_image_path_relative(char *out, __u64 cap, const char *path);
+int shirudo_image_config_root_fd(void);
+
+#endif /* SHIRUDO_IMAGE_CONFIG_H */
diff --git a/util/image_guards.c b/util/image_guards.c
index 8182c98e..6f158cb1 100644
--- a/util/image_guards.c
+++ b/util/image_guards.c
@@ -63,6 +63,7 @@ const struct shirudo_image_guard
shirudo_observer_guards[SHIRUDO_OBSERVER_HOOK_COUNT] = {
{ "shirudo_lsm_file_ioctl", SHIRUDO_BPFFS_DIR "/file_ioctl_link" },
{ "shirudo_lsm_task_prctl", SHIRUDO_TASK_PRCTL_LINK_PIN },
+ { "shirudo_lsm_bprm_check_security", SHIRUDO_BPRM_CHECK_LINK_PIN },
{ "shirudo_lsm_bdev_setintegrity",
SHIRUDO_BPFFS_DIR "/bdev_integrity_link" },
{ "shirudo_lsm_bdev_free_security",
diff --git a/util/image_guards.h b/util/image_guards.h
index 17e8d550..9da40c99 100644
--- a/util/image_guards.h
+++ b/util/image_guards.h
@@ -10,7 +10,7 @@
#define SHIRUDO_SEAL_HOOK_COUNT 16
#define SHIRUDO_MOUNT_HOOK_COUNT 4
-#define SHIRUDO_OBSERVER_HOOK_COUNT 6
+#define SHIRUDO_OBSERVER_HOOK_COUNT 7
struct shirudo_image_guard {
const char *prog;
diff --git a/util/image_keys.c b/util/image_keys.c
new file mode 100644
index 00000000..7244670c
--- /dev/null
+++ b/util/image_keys.c
@@ -0,0 +1,165 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+#include <shirudo/misc.h>
+
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "hex.h"
+#include "image_keys.h"
+#include "image_config.h"
+#include "keyring.h"
+#include "keys.h"
+#include "malloc.h"
+#include "paths.h"
+
+static int revocations(int root_fd, bool apply, bool boot)
+{
+ struct dirent *entry;
+ struct stat root, st;
+ int keys_fd, dir_fd, rc = 0;
+ DIR *dir;
+
+ if (fstat(root_fd, &root))
+ return -errno;
+ keys_fd = openat(root_fd, "keys",
+ O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
+ if (keys_fd < 0)
+ return errno == ENOENT ? 0 : -errno;
+ dir_fd = openat(keys_fd, "revoked",
+ O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
+ close(keys_fd);
+ if (dir_fd < 0)
+ return errno == ENOENT ? 0 : -errno;
+ dir = fdopendir(dir_fd);
+ if (!dir) {
+ rc = -errno;
+ close(dir_fd);
+ return rc;
+ }
+ errno = 0;
+ while ((entry = readdir(dir)) != NULL) {
+ __u8 *der = NULL;
+ __u8 digest[32], fingerprint[32], expected[32];
+ char canonical[69], path[64], name[65];
+ size_t size = 0;
+ int fd;
+
+ if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
+ continue;
+ if (strlen(entry->d_name) != 68 ||
+ strcmp(entry->d_name + 64, ".der")) {
+ rc = -EINVAL;
+ break;
+ }
+ memcpy(name, entry->d_name, 64);
+ name[64] = 0;
+ rc = hex_decode(name, expected, 32);
+ if (rc)
+ break;
+ hex_encode(expected, 32, canonical);
+ memcpy(canonical + 64, ".der", 5);
+ if (strcmp(canonical, entry->d_name)) {
+ rc = -EINVAL;
+ break;
+ }
+ fd = openat(dir_fd, entry->d_name,
+ O_RDONLY | O_NONBLOCK | O_CLOEXEC | O_NOFOLLOW);
+ if (fd < 0) {
+ rc = -errno;
+ break;
+ }
+ rc = fstat(fd, &st) ? -errno : 0;
+ if (!rc && (!S_ISREG(st.st_mode) || st.st_dev != root.st_dev ||
+ st.st_size <= 0 ||
+ st.st_size > SHIRUDO_KEYS_DER_MAX))
+ rc = -EINVAL;
+ snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);
+ if (!rc)
+ rc = keys_read_der(path, &der, &size);
+ close(fd);
+ if (!rc)
+ rc = keys_digest_der(der, size, digest,
+ fingerprint);
+ if (!rc && memcmp(expected, fingerprint, 32))
+ rc = -EKEYREJECTED;
+ if (!rc && apply)
+ rc = shirudo_keys_revoke_image(der, size, boot);
+ xfree(der, size);
+ if (rc)
+ break;
+ errno = 0;
+ }
+ if (!entry && errno && !rc)
+ rc = -errno;
+ closedir(dir);
+ return rc;
+}
+
+int shirudo_keys_validate_image_set(const char *primary, int root_fd,
+ struct shirudo_trusted_set *set)
+{
+ char keys[64], revoked[80];
+ int rc;
+
+ rc = shirudo_image_config_validate(root_fd);
+ if (!rc)
+ rc = revocations(root_fd, false, false);
+ if (rc)
+ return rc;
+ snprintf(keys, sizeof(keys), "/proc/self/fd/%d/keys", root_fd);
+ snprintf(revoked, sizeof(revoked), "%s/revoked", keys);
+ rc = shirudo_keys_load_image(primary, keys, revoked, true, set);
+ if (!rc && (!set->n || set->n_unvouched))
+ rc = -EKEYREJECTED;
+ return rc;
+}
+
+int shirudo_keys_check_image_set(const struct shirudo_trusted_set *set)
+{
+ key_serial_t keyring;
+ size_t i;
+ int rc;
+
+ keyring = keyring_bpf_serial();
+ if (keyring < 0)
+ return keyring;
+ for (i = 0; i < set->n; i++) {
+ __u8 *der = NULL;
+ size_t size = 0;
+
+ rc = keys_read_der(set->certs[i].path, &der, &size);
+ if (rc)
+ return rc;
+ rc = keyring_holds_cert(keyring, der, size);
+ xfree(der, size);
+ if (rc != 1)
+ return rc < 0 ? rc : -ENOKEY;
+ }
+ return 0;
+}
+
+int shirudo_keys_validate_image(int root_fd)
+{
+ struct shirudo_trusted_set set;
+ int rc;
+
+ rc = shirudo_keys_validate_image_set(SHIRUDO_RUNTIME_CERT_PATH,
+ root_fd, &set);
+ return rc ? rc : shirudo_keys_check_image_set(&set);
+}
+
+int shirudo_keys_apply_image(int root_fd, bool boot)
+{
+ int rc;
+
+ rc = shirudo_keys_validate_image(root_fd);
+ return rc ? rc : revocations(root_fd, true, boot);
+}
diff --git a/util/image_keys.h b/util/image_keys.h
new file mode 100644
index 00000000..fa1dc2ad
--- /dev/null
+++ b/util/image_keys.h
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+/* Copyright (c) 2026 Authors of shirudo */
+
+#ifndef SHIRUDO_IMAGE_KEYS_H
+#define SHIRUDO_IMAGE_KEYS_H
+
+#include <shirudo/misc.h>
+
+#include <stdbool.h>
+#include <stddef.h>
+
+struct shirudo_trusted_set;
+
+int shirudo_keys_validate_image_set(const char *primary, int root_fd,
+ struct shirudo_trusted_set *set);
+int shirudo_keys_check_image_set(const struct shirudo_trusted_set *set);
+int shirudo_keys_validate_image(int root_fd);
+
+int shirudo_keys_apply_image(int root_fd, bool boot);
+int shirudo_keys_revoke_image(const __u8 *der, size_t size, bool boot);
+
+#endif /* SHIRUDO_IMAGE_KEYS_H */
diff --git a/util/image_runtime.c b/util/image_runtime.c
new file mode 100644
index 00000000..f9504d9f
--- /dev/null
+++ b/util/image_runtime.c
@@ -0,0 +1,1017 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+#include <shirudo/misc.h>
+#include <shirudo/string.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <linux/fs.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+#include <sys/statvfs.h>
+#include <sys/sysmacros.h>
+#include <sys/xattr.h>
+#include <unistd.h>
+
+#include <bpf/bpf.h>
+
+#include "../builtin.h"
+#include "config.h"
+#include "config_image.h"
+#include "config_measure.h"
+#include "config_update.h"
+#include "crypto.h"
+#include "dm_verity.h"
+#include "hex.h"
+#include "identity.h"
+#include "image_config.h"
+#include "image_guards.h"
+#include "image_runtime.h"
+#include "io.h"
+#include "json.h"
+#include "keys.h"
+#include "keyring.h"
+#include "malloc.h"
+#include "log.h"
+#include "macros.h"
+#include "paths.h"
+#include "seal.h"
+#include "shirudo_mode.h"
+#include "tpm.h"
+
+struct image_bootstrap {
+ char runtime[PATH_MAX];
+ char config[PATH_MAX];
+ char certificate[PATH_MAX];
+ __u8 runtime_root[32];
+ __u32 pcr;
+ bool have_root;
+ bool have_pcr;
+};
+
+struct image_context {
+ struct shirudo_config_update_target boot_target;
+ struct shirudo_dm_verity boot_runtime;
+ struct shirudo_config_update update;
+ struct shirudo_config_image_open candidate;
+ struct shirudo_dm_verity candidate_device;
+ struct shirudo_config bootstrap_config;
+ struct image_bootstrap bootstrap;
+ struct shirudo_tpm *tpm;
+ int candidate_fd;
+ int certificate_fd;
+ bool loaded;
+ bool locked;
+ bool preparing_boot;
+ bool boot_prepared;
+ bool boot_activation;
+};
+
+static struct image_context image = {
+ .candidate_fd = -1,
+ .certificate_fd = -1,
+ .candidate = { .data_fd = -1, .hash_fd = -1, .signature_fd = -1 },
+ .candidate_device = { .data_loop_fd = -1, .hash_loop_fd = -1 },
+};
+
+#include "image_keys.h"
+
+static int load_runtime_config(void);
+static int current_config(void);
+
+static bool absolute_path(const char *path)
+{
+ return path && path[0] == '/' && !strstr(path, "/../") &&
+ !strstr(path, "/./") && !strstr(path, "//") &&
+ strcmp(path, "/") &&
+ (strlen(path) < 3 || strcmp(path + strlen(path) - 3, "/.."));
+}
+
+static char *trim(char *text)
+{
+ char *end;
+
+ while (*text == ' ' || *text == '\t')
+ text++;
+ end = text + strlen(text);
+ while (end > text && (end[-1] == '\n' || end[-1] == '\r' ||
+ end[-1] == ' ' || end[-1] == '\t'))
+ *--end = 0;
+ return text;
+}
+
+static int load_bootstrap(const char *path, bool initial)
+{
+ struct image_bootstrap *boot = &image.bootstrap;
+ char line[PATH_MAX + 128];
+ FILE *file;
+ int rc = 0;
+
+ file = fopen(path, "re");
+ if (!file)
+ return -errno;
+ while (fgets(line, sizeof(line), file)) {
+ char *key, *value;
+
+ if (!strchr(line, '\n') && !feof(file)) {
+ rc = -E2BIG;
+ break;
+ }
+ key = trim(line);
+ if (!*key || *key == '#')
+ continue;
+ value = strchr(key, '=');
+ if (!value) {
+ rc = -EINVAL;
+ break;
+ }
+ *value++ = 0;
+ key = trim(key);
+ value = trim(value);
+ if (!strcmp(key, "runtime.root_hash")) {
+ if (!initial || boot->have_root ||
+ hex_decode(value, boot->runtime_root, 32)) {
+ rc = -EINVAL;
+ break;
+ }
+ boot->have_root = true;
+ } else if (!strcmp(key, "runtime.image") && initial) {
+ if (!absolute_path(value) || boot->runtime[0])
+ rc = -EINVAL;
+ else
+ strscpy(boot->runtime, value, sizeof(boot->runtime));
+ } else if (!strcmp(key, "config.image")) {
+ if (!absolute_path(value))
+ rc = -EINVAL;
+ else
+ strscpy(boot->config, value, sizeof(boot->config));
+ } else if (!strcmp(key, "config.verification_key")) {
+ if (strncmp(value, SHIRUDO_RUNTIME_DIR "/",
+ sizeof(SHIRUDO_RUNTIME_DIR)) ||
+ !absolute_path(value))
+ rc = -EPERM;
+ else
+ strscpy(boot->certificate, value, sizeof(boot->certificate));
+ } else if (!strcmp(key, "config.pcr")) {
+ __u64 pcr;
+
+ if (parse_u64(value, strlen(value), 23, &pcr) ||
+ shirudo_config_measure_pcr_valid(pcr)) {
+ rc = -EINVAL;
+ } else {
+ boot->pcr = pcr;
+ boot->have_pcr = true;
+ }
+ } else if (!strncmp(key, "config.", 7) ||
+ !strncmp(key, "runtime.", 8)) {
+ rc = -EINVAL;
+ }
+ if (rc)
+ break;
+ }
+ if (ferror(file) && !rc)
+ rc = -EIO;
+ fclose(file);
+ return rc;
+}
+
+static int reject_overrides(void)
+{
+ static const char *const names[] = {
+ "SHIRUDO_KEYS_DIR",
+ "SHIRUDO_TPM_TCTI",
+ "SHIRUDO_TPM_OWNER_AUTH",
+ "SHIRUDO_EVENTLOG_PATH",
+ "SHIRUDO_EVENTLOG_PENDING_PATH",
+ "SHIRUDO_INITRD_RELEASE",
+ "SHIRUDO_SYSROOT",
+ };
+ size_t i;
+
+ for (i = 0; i < ARRAY_SIZE(names); i++) {
+ if (getenv(names[i])) {
+ pr_err("image mode: bootstrap override %s is forbidden",
+ names[i]);
+ return -EPERM;
+ }
+ }
+ return 0;
+}
+
+int shirudo_image_bootstrap_load(void)
+{
+ struct shirudo_identity identity;
+ int mode, rc;
+
+ if (image.loaded)
+ return 1;
+ mode = shirudo_identity_mode();
+ if (mode < 0)
+ return mode;
+ rc = identity_read(&identity);
+ if (mode != SHIRUDO_MODE_NONE && rc)
+ return rc;
+ if (!rc && mode != (identity.runtime_mode ?
+ SHIRUDO_MODE_IMAGE : SHIRUDO_MODE_LEGACY))
+ return -EPROTO;
+ if (!rc && identity.runtime_mode) {
+ memcpy(image.bootstrap.runtime_root, identity.runtime_root, 32);
+ image.bootstrap.have_root = true;
+ rc = load_runtime_config();
+ } else {
+ rc = load_bootstrap(SHIRUDO_CONF_PATH, 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;
+ if (!rc)
+ rc = config_load_path(&image.bootstrap_config,
+ SHIRUDO_CONF_PATH);
+ }
+ if (rc)
+ return rc;
+ rc = reject_overrides();
+ if (rc)
+ return rc;
+ rc = shirudo_image_config_set(&image.bootstrap_config, -1);
+ if (!rc)
+ image.loaded = true;
+ return rc ? rc : 1;
+}
+
+const __u8 *shirudo_image_runtime_root(void)
+{
+ return image.loaded ? image.bootstrap.runtime_root : NULL;
+}
+
+static int mkdir_owned(const char *path)
+{
+ struct stat st;
+ bool created;
+
+ created = mkdir(path, 0700) == 0;
+ if (!created && errno != EEXIST)
+ return -errno;
+ if (lstat(path, &st))
+ return -errno;
+ if (!S_ISDIR(st.st_mode) || st.st_uid || (st.st_mode & 0022))
+ return -EPERM;
+ return created ? seal_label(path) : 0;
+}
+
+static void mapping_name(const __u8 root[32], char out[128])
+{
+ char hex[65];
+
+ hex_encode(root, 32, hex);
+ snprintf(out, 128, "shirudo-config-%s", hex);
+}
+
+static int device_observed(dev_t device, const __u8 root[32], __u32 kind)
+{
+ struct shirudo_device_authority entry;
+ __u32 dev = (major(device) << 20) | minor(device);
+ __u64 key, next, diskseq;
+ struct stat st;
+ char path[64];
+ int fd, block, rc = -EACCES;
+
+ snprintf(path, sizeof(path), "/dev/dm-%u", minor(device));
+ block = open(path, O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
+ if (block < 0)
+ return -errno;
+ if (fstat(block, &st) || !S_ISBLK(st.st_mode) || st.st_rdev != device ||
+ ioctl(block, BLKGETDISKSEQ, &diskseq)) {
+ rc = errno ? -errno : -EPERM;
+ close(block);
+ return rc;
+ }
+ fd = bpf_obj_get(SHIRUDO_BPFFS_DIR "/devices");
+ if (fd < 0) {
+ rc = -errno;
+ goto out_block;
+ }
+ if (bpf_map_get_next_key(fd, NULL, &key))
+ goto out;
+ do {
+ if (!bpf_map_lookup_elem(fd, &key, &entry) &&
+ entry.dev == dev && entry.kind == kind &&
+ entry.diskseq == diskseq &&
+ (!root || !memcmp(entry.root, root, 32))) {
+ rc = 0;
+ break;
+ }
+ if (bpf_map_get_next_key(fd, &key, &next))
+ break;
+ key = next;
+ } while (1);
+out:
+ close(fd);
+out_block:
+ close(block);
+ return rc;
+}
+
+static int runtime_observed(dev_t device)
+{
+ return device_observed(device, image.bootstrap.runtime_root,
+ SHIRUDO_DEVICE_RUNTIME);
+}
+
+static int mount_flags(int fd, bool executable)
+{
+ __u64 required = ST_RDONLY | ST_NOSUID | ST_NODEV;
+ struct statvfs st;
+
+ if (!executable)
+ required |= ST_NOEXEC;
+ if (fstatvfs(fd, &st))
+ return -errno;
+ return (st.f_flag & required) == required ? 0 : -EPERM;
+}
+
+static int load_runtime_config(void)
+{
+ struct stat root, cert;
+ char certificate[64];
+ int rc;
+
+ if (!image.preparing_boot) {
+ rc = shirudo_image_guards_ready();
+ if (rc)
+ return rc;
+ }
+ if (stat(SHIRUDO_RUNTIME_DIR, &root))
+ return -errno;
+ if (!image.preparing_boot) {
+ rc = runtime_observed(root.st_dev);
+ if (rc)
+ return rc;
+ }
+ {
+ struct stat metadata;
+ char path[64];
+ int fd;
+
+ fd = open(SHIRUDO_RUNTIME_CONF_PATH,
+ O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
+ if (fd < 0)
+ return -errno;
+ rc = fstat(fd, &metadata) ? -errno : 0;
+ if (!rc && (!S_ISREG(metadata.st_mode) ||
+ metadata.st_dev != root.st_dev))
+ rc = -EPERM;
+ snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);
+ if (!rc)
+ rc = load_bootstrap(path, false);
+ if (!rc)
+ rc = config_load_path(&image.bootstrap_config,
+ path);
+ close(fd);
+ }
+ if (rc)
+ return rc;
+ if (!image.bootstrap.config[0] || !image.bootstrap.certificate[0] ||
+ !image.bootstrap.have_pcr)
+ return -EINVAL;
+ rc = shirudo_image_config_set(&image.bootstrap_config, -1);
+ if (rc)
+ return rc;
+ if (image.certificate_fd >= 0)
+ close(image.certificate_fd);
+ image.certificate_fd = open(image.bootstrap.certificate,
+ O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
+ if (image.certificate_fd < 0)
+ return -errno;
+ if (fstat(image.certificate_fd, &cert))
+ return -errno;
+ if (!S_ISREG(cert.st_mode) || cert.st_dev != root.st_dev)
+ return -EPERM;
+ {
+ int fd = open(SHIRUDO_RUNTIME_CERT_PATH,
+ O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
+
+ if (fd < 0)
+ return -errno;
+ rc = fstat(fd, &cert) ? -errno : 0;
+ if (!rc &&
+ (!S_ISREG(cert.st_mode) || cert.st_dev != root.st_dev))
+ rc = -EPERM;
+ if (!rc)
+ rc = mount_flags(fd, true);
+ close(fd);
+ if (rc)
+ return rc;
+ }
+ snprintf(certificate, sizeof(certificate), "/proc/self/fd/%d",
+ image.certificate_fd);
+ strscpy(image.bootstrap.certificate, certificate, sizeof(image.bootstrap.certificate));
+ return 0;
+}
+
+int shirudo_image_runtime_mount(void)
+{
+ struct shirudo_config_image_open opened = {
+ .data_fd = -1,
+ .hash_fd = -1,
+ .signature_fd = -1,
+ };
+ struct shirudo_dm_verity device = {
+ .data_loop_fd = -1,
+ .hash_loop_fd = -1,
+ };
+ bool created = false;
+ int rc, cleanup;
+ struct stat st;
+
+ if (!image.loaded)
+ return -EINVAL;
+ if (!image.preparing_boot && !stat(SHIRUDO_RUNTIME_DIR, &st) &&
+ !runtime_observed(st.st_dev))
+ return load_runtime_config();
+ rc = shirudo_config_image_open_root(image.bootstrap.runtime,
+ image.bootstrap.runtime_root,
+ &opened);
+ if (rc)
+ return rc;
+ rc = mkdir_owned(SHIRUDO_RUNTIME_DIR);
+ if (!rc) {
+ rc = shirudo_dm_verity_create(&opened, "shirudo-runtime",
+ &device);
+ created = !rc;
+ }
+ if (rc == -EEXIST)
+ rc = shirudo_dm_verity_inspect("shirudo-runtime", &opened.image,
+ &device);
+ if (!rc)
+ rc = shirudo_dm_verity_verify(&device, &opened);
+ if (!rc)
+ rc = shirudo_dm_verity_mount(&device, SHIRUDO_RUNTIME_DIR,
+ true);
+ if (rc && created) {
+ cleanup = shirudo_dm_verity_remove(&device, false);
+ if (cleanup)
+ pr_warn("image: cannot remove runtime mapping: %s",
+ strerror(-cleanup));
+ }
+ shirudo_dm_verity_close(&device);
+ if (!rc && image.preparing_boot)
+ image.boot_runtime = device;
+ shirudo_config_image_close(&opened);
+ if (!rc)
+ rc = load_runtime_config();
+ return rc;
+}
+
+static int seal_fd(void *ctx, int fd)
+{
+ (void)ctx;
+ return fsetxattr(fd, SHIRUDO_XATTR_NAME, SHIRUDO_SEAL_LABEL,
+ strlen(SHIRUDO_SEAL_LABEL), 0) ?
+ -errno :
+ 0;
+}
+
+static int boot_identity(void *ctx, __u32 *reset, __u32 *restart)
+{
+ struct image_context *current = ctx;
+
+ return shirudo_tpm_boot_identity(current->tpm, reset, restart);
+}
+
+static int pcr_read(void *ctx, __u32 pcr, __u8 out[32])
+{
+ struct image_context *current = ctx;
+
+ return tpm_pcr_read(current->tpm, pcr, out);
+}
+
+static int pcr_extend(void *ctx, __u32 pcr, const __u8 event[32])
+{
+ struct image_context *current = ctx;
+
+ return tpm_pcr_extend(current->tpm, pcr, event);
+}
+
+static int collect_mapping(const char *name, dev_t device, void *ctx)
+{
+ struct shirudo_dm_verity retired = {
+ .data_loop_fd = -1,
+ .hash_loop_fd = -1,
+ };
+ const dev_t *active = ctx;
+ int rc;
+
+ if (device == *active ||
+ device_observed(device, NULL, SHIRUDO_DEVICE_CONFIG))
+ return 0;
+ if (strlen(name) >= sizeof(retired.name))
+ return -ENAMETOOLONG;
+ strscpy(retired.name, name, sizeof(retired.name));
+ retired.device = device;
+ rc = shirudo_dm_verity_remove(&retired, false);
+ if (rc && rc != -EBUSY && rc != -ENXIO && rc != -ENODEV)
+ pr_warn("image: cannot remove retired mapping %s: %s", name,
+ strerror(-rc));
+ return 0;
+}
+
+static void collect_mappings(void)
+{
+ struct stat st;
+ dev_t active = 0;
+ int rc;
+
+ if (!image.locked)
+ return;
+ if (!stat(SHIRUDO_CONFIG_IMAGE_DIR, &st))
+ active = st.st_dev;
+ rc = shirudo_dm_verity_list(collect_mapping, &active);
+ if (rc)
+ pr_warn("image: cannot collect retired mappings: %s",
+ strerror(-rc));
+}
+
+static void close_candidate(void)
+{
+ if (image.candidate_fd >= 0)
+ close(image.candidate_fd);
+ image.candidate_fd = -1;
+ if (image.locked && umount2(SHIRUDO_CONFIG_CANDIDATE_DIR, MNT_DETACH) &&
+ errno != EINVAL && errno != ENOENT)
+ pr_warn("image: cannot release candidate mount: %s",
+ strerror(errno));
+ shirudo_dm_verity_close(&image.candidate_device);
+ shirudo_config_image_close(&image.candidate);
+}
+
+static int candidate_keys(int fd)
+{
+ int rc;
+
+ rc = shirudo_keys_validate_image(fd);
+ if (rc == -ENOKEY)
+ pr_err("image: BPF signer was not enrolled at boot; "
+ "reboot with the new trusted key set required");
+ return rc;
+}
+
+static int prepare(void *ctx, const struct shirudo_config_update_target *target)
+{
+ const char *mountpoint = SHIRUDO_CONFIG_CANDIDATE_DIR;
+ struct image_context *current = ctx;
+ struct shirudo_dm_verity *device = &current->candidate_device;
+ bool created = false;
+ int rc, cleanup;
+ char name[128];
+ struct stat st;
+
+ close_candidate();
+ if (!current->preparing_boot)
+ collect_mappings();
+ if (strncmp(target->prefix, SHIRUDO_IMAGE_CACHE_DIR "/",
+ sizeof(SHIRUDO_IMAGE_CACHE_DIR)) ||
+ !absolute_path(target->prefix))
+ return -EPERM;
+ rc = shirudo_config_image_open(target->prefix, &current->candidate);
+ if (rc)
+ return rc;
+ if (memcmp(target->root_hash, current->candidate.image.root_hash, 32))
+ return -EKEYREJECTED;
+ mapping_name(target->root_hash, name);
+ rc = shirudo_dm_verity_inspect(name, &current->candidate.image,
+ device);
+ if (rc == -ENXIO || rc == -ENODEV || rc == -ENOENT) {
+ rc = shirudo_dm_verity_create(&current->candidate, name,
+ device);
+ created = !rc;
+ }
+ if (!rc)
+ rc = shirudo_dm_verity_verify(device,
+ &current->candidate);
+ if (!rc && !current->preparing_boot)
+ rc = device_observed(current->candidate_device.device,
+ current->candidate.image.root_hash,
+ SHIRUDO_DEVICE_CONFIG);
+ if (rc) {
+ if (created) {
+ cleanup = shirudo_dm_verity_remove(device, false);
+ if (cleanup)
+ pr_warn("image: cannot remove candidate mapping: %s",
+ strerror(-cleanup));
+ }
+ return rc;
+ }
+ if (!stat(SHIRUDO_CONFIG_IMAGE_DIR, &st) &&
+ st.st_dev == current->candidate_device.device) {
+ current->candidate_fd = open(SHIRUDO_CONFIG_IMAGE_DIR,
+ O_PATH | O_DIRECTORY | O_CLOEXEC);
+ } else {
+ if (umount2(mountpoint, MNT_DETACH) &&
+ errno != EINVAL && errno != ENOENT)
+ return -errno;
+ rc = mkdir_owned(mountpoint);
+ if (!rc)
+ rc = shirudo_dm_verity_mount(device,
+ mountpoint,
+ false);
+ if (rc)
+ return rc;
+ current->candidate_fd = open(mountpoint,
+ O_PATH | O_DIRECTORY | O_CLOEXEC);
+ }
+ if (current->candidate_fd < 0)
+ return -errno;
+ rc = mount_flags(current->candidate_fd, false);
+ if (!rc)
+ rc = shirudo_image_config_validate(current->candidate_fd);
+ if (!rc && !current->preparing_boot)
+ rc = candidate_keys(current->candidate_fd);
+ return rc;
+}
+
+static int activate(void *ctx,
+ const struct shirudo_config_update_target *target)
+{
+ struct image_context *current = ctx;
+ struct stat st;
+ int fd, rc;
+
+ (void)target;
+ rc = shirudo_keys_apply_image(current->candidate_fd,
+ current->boot_activation);
+ if (rc)
+ return rc;
+ rc = mkdir_owned(SHIRUDO_CONFIG_IMAGE_DIR);
+ if (rc)
+ return rc;
+ if (stat(SHIRUDO_CONFIG_IMAGE_DIR, &st))
+ return -errno;
+ if (st.st_dev != current->candidate_device.device) {
+ if (umount2(SHIRUDO_CONFIG_IMAGE_DIR, MNT_DETACH) &&
+ errno != EINVAL)
+ return -errno;
+ rc = shirudo_dm_verity_mount(&current->candidate_device,
+ SHIRUDO_CONFIG_IMAGE_DIR, false);
+ if (rc)
+ return rc;
+ }
+ fd = open(SHIRUDO_CONFIG_IMAGE_DIR, O_PATH | O_DIRECTORY | O_CLOEXEC);
+ if (fd < 0)
+ return -errno;
+ if (fstat(fd, &st))
+ rc = -errno;
+ else if (st.st_dev != current->candidate_device.device)
+ rc = -ESTALE;
+ else
+ rc = mount_flags(fd, false);
+ if (!rc)
+ rc = shirudo_image_config_set(&current->bootstrap_config, fd);
+ close(fd);
+ return rc;
+}
+
+static const struct shirudo_config_update_ops update_ops = {
+ .boot_identity = boot_identity,
+ .pcr_read = pcr_read,
+ .pcr_extend = pcr_extend,
+ .prepare = prepare,
+ .activate = activate,
+ .seal = seal_fd,
+};
+
+static int open_update(void)
+{
+ int rc;
+
+ if (image.locked)
+ return 0;
+ rc = mkdir_owned(SHIRUDO_CONFIG_UPDATE_DIR);
+ if (!rc)
+ rc = tpm_open(&image.tpm);
+ if (!rc)
+ rc = shirudo_config_update_open(SHIRUDO_CONFIG_UPDATE_DIR,
+ &update_ops, &image,
+ &image.update);
+ if (!rc)
+ image.locked = true;
+ return rc;
+}
+
+static int boot_target(struct shirudo_config_update_target *target)
+{
+ struct shirudo_config_update_record previous;
+ struct shirudo_config_image_open opened;
+ const char *prefix;
+ bool selected;
+ int rc;
+
+ rc = shirudo_config_update_read(&image.update, &previous);
+ selected = !rc;
+ prefix = selected ? previous.target.prefix : image.bootstrap.config;
+ if (rc && rc != -ENOENT)
+ return rc;
+ rc = shirudo_config_image_open(prefix, &opened);
+ if (rc)
+ return rc;
+ if (selected && memcmp(previous.target.root_hash,
+ opened.image.root_hash, 32)) {
+ rc = -EKEYREJECTED;
+ } else {
+ strscpy(target->prefix, prefix, sizeof(target->prefix));
+ memcpy(target->root_hash, opened.image.root_hash, 32);
+ }
+ shirudo_config_image_close(&opened);
+ return rc;
+}
+
+static int enroll_config_key(void)
+{
+ __u8 *der = NULL;
+ key_serial_t ring;
+ size_t size = 0;
+ int rc;
+
+ rc = keys_read_der(image.bootstrap.certificate, &der, &size);
+ if (rc)
+ return rc;
+ ring = shirudo_keyring_dm_verity_serial();
+ rc = ring < 0 ? ring : keyring_holds_cert(ring, der, size);
+ if (!rc)
+ rc = shirudo_keyring_dm_verity_add_cert(der, size);
+ xfree(der, size);
+ if (rc < 0)
+ return rc;
+ rc = shirudo_keyring_dm_verity_seal();
+ return rc == -EEXIST ? 0 : rc;
+}
+
+/* The authenticated initrd validates the boot set before installing guards. */
+int shirudo_image_boot_keys(struct shirudo_trusted_set *set)
+{
+ struct shirudo_trusted_cert primary = { .primary = true };
+ size_t i;
+ int rc;
+
+ image.preparing_boot = true;
+ rc = shirudo_image_runtime_mount();
+ if (!rc)
+ rc = enroll_config_key();
+ if (!rc)
+ rc = open_update();
+ if (!rc)
+ rc = boot_target(&image.boot_target);
+ if (!rc)
+ rc = prepare(&image, &image.boot_target);
+ if (!rc)
+ rc = shirudo_keys_validate_image_set(SHIRUDO_RUNTIME_CERT_PATH,
+ image.candidate_fd, set);
+ if (rc)
+ return rc;
+ /* Guard loaders need the initial signer until activation. */
+ strscpy(primary.path, SHIRUDO_RUNTIME_CERT_PATH, sizeof(primary.path));
+ rc = keys_digest_file(primary.path, primary.sha256,
+ primary.fingerprint);
+ if (rc)
+ return rc;
+ for (i = 0; i < set->n; i++)
+ if (!memcmp(set->certs[i].sha256, primary.sha256, 32))
+ break;
+ if (i == set->n) {
+ if (set->n == SHIRUDO_KEYS_MAX)
+ return -E2BIG;
+ set->certs[set->n++] = primary;
+ }
+ image.boot_prepared = true;
+ return 0;
+}
+
+int shirudo_image_boot_keys_close(void)
+{
+ int rc = 0, err;
+
+ if (!image.preparing_boot)
+ return 0;
+ shirudo_image_config_clear();
+ if (image.candidate_fd >= 0)
+ close(image.candidate_fd);
+ image.candidate_fd = -1;
+ if (image.certificate_fd >= 0)
+ close(image.certificate_fd);
+ image.certificate_fd = -1;
+ if (umount(SHIRUDO_CONFIG_CANDIDATE_DIR) && errno != EINVAL &&
+ errno != ENOENT)
+ rc = -errno;
+ if (image.candidate_device.name[0]) {
+ err = shirudo_dm_verity_remove(&image.candidate_device, false);
+ if (!rc && err && err != -ENXIO && err != -ENODEV)
+ rc = err;
+ }
+ shirudo_config_image_close(&image.candidate);
+ if (umount(SHIRUDO_RUNTIME_DIR) && errno != EINVAL && errno != ENOENT &&
+ !rc)
+ rc = -errno;
+ if (image.boot_runtime.name[0]) {
+ err = shirudo_dm_verity_remove(&image.boot_runtime, false);
+ if (!rc && err && err != -ENXIO && err != -ENODEV)
+ rc = err;
+ }
+ image.preparing_boot = false;
+ return rc;
+}
+
+int shirudo_image_boot(void)
+{
+ struct shirudo_config_update_target target = {};
+ int rc;
+
+ rc = open_update();
+ if (rc)
+ return rc;
+ if (image.boot_prepared) {
+ target = image.boot_target;
+ } else {
+ rc = boot_target(&target);
+ if (rc)
+ return rc;
+ }
+ image.boot_activation = true;
+ rc = shirudo_config_update_boot(&image.update, &target,
+ image.bootstrap.pcr);
+ image.boot_activation = false;
+ if (rc == -EINPROGRESS)
+ pr_err("image: activation is pending; further changes are blocked");
+ return rc;
+}
+
+static int config_mount_check(const struct shirudo_config_update_record *record)
+{
+ struct shirudo_config_image_open opened;
+ struct shirudo_dm_verity device;
+ struct stat st;
+ char name[128];
+ int fd, rc;
+
+ rc = shirudo_config_image_open(record->target.prefix, &opened);
+ if (rc)
+ return rc;
+ if (memcmp(record->target.root_hash, opened.image.root_hash, 32))
+ rc = -EKEYREJECTED;
+ mapping_name(record->target.root_hash, name);
+ if (!rc)
+ rc = shirudo_dm_verity_inspect(name, &opened.image, &device);
+ shirudo_config_image_close(&opened);
+ if (rc)
+ return rc;
+ if (stat(SHIRUDO_CONFIG_IMAGE_DIR, &st) || st.st_dev != device.device) {
+ rc = -EUCLEAN;
+ goto out;
+ }
+ rc = device_observed(device.device, record->target.root_hash,
+ SHIRUDO_DEVICE_CONFIG);
+ if (rc)
+ goto out;
+ fd = open(SHIRUDO_CONFIG_IMAGE_DIR, O_PATH | O_DIRECTORY | O_CLOEXEC);
+ if (fd < 0) {
+ rc = -errno;
+ goto out;
+ }
+ rc = mount_flags(fd, false);
+ if (!rc)
+ rc = shirudo_image_config_set(&image.bootstrap_config, fd);
+ close(fd);
+out:
+ shirudo_dm_verity_close(&device);
+ return rc;
+}
+
+int shirudo_image_boot_complete(void)
+{
+ struct shirudo_config_update_record record;
+ int rc;
+
+ rc = shirudo_config_update_read(&image.update, &record);
+ if (!rc)
+ rc = config_mount_check(&record);
+ if (!rc)
+ rc = shirudo_config_update_complete_boot(&image.update);
+ return rc;
+}
+
+static int current_config(void)
+{
+ struct shirudo_config_update_record record;
+ int rc;
+
+ rc = open_update();
+ if (!rc)
+ rc = shirudo_config_update_check(&image.update, &record);
+ return rc ? rc : config_mount_check(&record);
+}
+
+int shirudo_image_enter(const char *command, int argc, const char **argv)
+{
+ struct shirudo_identity identity;
+ int mode, rc;
+
+ if (!strcmp(command, "image") && argc > 1 &&
+ (!strcmp(argv[1], "build") || !strcmp(argv[1], "verify") ||
+ !strcmp(argv[1], "--help") || !strcmp(argv[1], "-h") ||
+ !strcmp(argv[1], "--list-cmds")))
+ return 0;
+ if (!strcmp(command, "init") || !strcmp(command, "genkey") ||
+ !strcmp(command, "build") || !strcmp(command, "help") ||
+ !strcmp(command, "version"))
+ return 0;
+ mode = shirudo_identity_mode();
+ if (mode < 0)
+ return mode;
+ rc = identity_read(&identity);
+ if (mode != SHIRUDO_MODE_NONE && 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))
+ return -EPROTO;
+ if (!identity.runtime_mode)
+ return 0;
+ if (!strcmp(command, "attest"))
+ return -EOPNOTSUPP;
+ rc = shirudo_image_bootstrap_load();
+ if (rc < 0)
+ return rc;
+ rc = load_runtime_config();
+ if (rc)
+ return rc;
+ for (int i = 1; i < argc; i++) {
+ if (!strncmp(argv[i], "--keys-dir", 10) ||
+ !strncmp(argv[i], "--cert", 6) ||
+ !strncmp(argv[i], "--tcti", 6))
+ return -EPERM;
+ }
+ if (!strcmp(command, "image"))
+ return open_update();
+ return current_config();
+}
+
+int shirudo_image_status(void)
+{
+ struct shirudo_config_update_record record;
+ char buffer[512], root[65];
+ struct shirudo_json_emit emit = {
+ .buf = buffer,
+ .cap = sizeof(buffer),
+ };
+ int rc;
+
+ if (!image.locked)
+ return -ENODEV;
+ rc = shirudo_config_update_read(&image.update, &record);
+ if (rc)
+ return rc;
+ hex_encode(record.target.root_hash, 32, root);
+ rc = record.pending ? -EINPROGRESS : current_config();
+ json_obj_open(&emit);
+ json_kv_string(&emit, "root_hash", root);
+ json_kv_int(&emit, "pcr", record.pcr);
+ json_kv_int(&emit, "sequence", record.sequence);
+ json_kv_int(&emit, "reset_count", record.reset_count);
+ json_kv_int(&emit, "restart_count", record.restart_count);
+ json_kv_bool(&emit, "pending", record.pending);
+ json_kv_bool(&emit, "consistent", !rc);
+ json_obj_close(&emit);
+ if (emit.err)
+ return emit.err;
+ puts(buffer);
+ return rc;
+}
+
+void shirudo_image_leave(void)
+{
+ (void)shirudo_image_boot_keys_close();
+ shirudo_image_config_clear();
+ close_candidate();
+ collect_mappings();
+ if (image.locked)
+ shirudo_config_update_close(&image.update);
+ image.locked = false;
+ tpm_close(image.tpm);
+ image.tpm = NULL;
+ if (image.certificate_fd >= 0)
+ close(image.certificate_fd);
+ image.certificate_fd = -1;
+ explicit_bzero(&image.bootstrap_config, sizeof(image.bootstrap_config));
+}
diff --git a/util/image_runtime.h b/util/image_runtime.h
new file mode 100644
index 00000000..220fdd6c
--- /dev/null
+++ b/util/image_runtime.h
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+/* Copyright (c) 2026 Authors of shirudo */
+
+#ifndef SHIRUDO_IMAGE_RUNTIME_H
+#define SHIRUDO_IMAGE_RUNTIME_H
+
+#include <shirudo/misc.h>
+
+#include <stdint.h>
+
+struct shirudo_config_update;
+struct shirudo_trusted_set;
+
+int shirudo_image_bootstrap_load(void);
+const __u8 *shirudo_image_runtime_root(void);
+int shirudo_image_runtime_mount(void);
+int shirudo_image_boot_keys(struct shirudo_trusted_set *set);
+int shirudo_image_boot_keys_close(void);
+int shirudo_image_boot(void);
+int shirudo_image_boot_complete(void);
+int shirudo_image_enter(const char *command, int argc, const char **argv);
+void shirudo_image_leave(void);
+int shirudo_image_status(void);
+
+#endif /* SHIRUDO_IMAGE_RUNTIME_H */
diff --git a/util/initrd.c b/util/initrd.c
index 985747dc..4357f764 100644
--- a/util/initrd.c
+++ b/util/initrd.c
@@ -14,26 +14,32 @@
bool in_initrd(void)
{
- const char *env = getenv("SHIRUDO_INITRD_RELEASE");
+ const char *env = shirudo_image_config_active() ? NULL :
+ getenv("SHIRUDO_INITRD_RELEASE");
return access(env && *env ? env : "/etc/initrd-release", F_OK) == 0;
}
const char *sysroot(void)
{
- const char *env = getenv("SHIRUDO_SYSROOT");
+ const char *env = shirudo_image_config_active() ? NULL :
+ getenv("SHIRUDO_SYSROOT");
return env && *env ? env : "/sysroot";
}
const char *shirudo_keys_dir(void)
{
+ if (shirudo_image_config_root_fd() >= 0)
+ return SHIRUDO_RUNTIME_DIR "/keys";
return in_initrd() ? SHIRUDO_INITRD_KEYS_DIR :
SHIRUDO_KEYS_DIR;
}
const char *shirudo_cert_path(void)
{
+ if (shirudo_image_config_root_fd() >= 0)
+ return SHIRUDO_RUNTIME_CERT_PATH;
return in_initrd() ? SHIRUDO_INITRD_CERT_PATH :
SHIRUDO_CERT_PATH;
}
diff --git a/util/keys.c b/util/keys.c
index 9332164c..a570c02b 100644
--- a/util/keys.c
+++ b/util/keys.c
@@ -96,8 +96,8 @@ int keys_digest_der(const __u8 *der, size_t der_sz,
return rc;
}
-static int keys_digest_file(const char *path, __u8 sha256[32],
- __u8 fingerprint[32])
+int keys_digest_file(const char *path, __u8 sha256[32],
+ __u8 fingerprint[32])
{
size_t der_sz = 0;
__u8 *der = NULL;
@@ -127,7 +127,8 @@ bool keys_is_revoked(const char *revoked_dir,
{
char path[PATH_MAX];
- if (keys_path_for(revoked_dir, fingerprint, ".der", path,
+ if (!revoked_dir ||
+ keys_path_for(revoked_dir, fingerprint, ".der", path,
sizeof(path)))
return false;
return access(path, F_OK) == 0;
@@ -360,13 +361,13 @@ static int scan_trusted(const char *keys_dir, const char *revoked_dir,
return rc;
}
-int keys_load(const char *keys_dir, const char *revoked_dir,
- bool with_trusted, struct shirudo_trusted_set *set)
+static int load_set(const char *primary, const char *keys_dir,
+ const char *revoked_dir, bool with_trusted, bool image,
+ struct shirudo_trusted_set *set)
{
struct keys_candidate *cand = NULL;
__u8 sha256[32], fingerprint[32];
char revoked[PATH_MAX];
- char path[PATH_MAX];
size_t n_cand = 0;
int rc;
@@ -377,32 +378,81 @@ int keys_load(const char *keys_dir, const char *revoked_dir,
return -ENAMETOOLONG;
revoked_dir = revoked;
}
- if (strsprintf(path, sizeof(path), "%s/%s", keys_dir,
- SHIRUDO_CERT_FILE) < 0)
- return -ENAMETOOLONG;
- rc = keys_digest_file(path, sha256, fingerprint);
+ rc = keys_digest_file(primary, sha256, fingerprint);
if (rc)
return rc;
- if (!keys_is_revoked(revoked_dir, fingerprint))
- set_add(set, path, sha256, fingerprint, NULL);
+ if (image || !keys_is_revoked(revoked_dir, fingerprint)) {
+ rc = set_add(set, primary, sha256, fingerprint, NULL);
+ if (rc)
+ return rc;
+ }
set->n_revoked = count_der(revoked_dir);
if (!with_trusted)
- return 0;
+ goto filter;
cand = xcalloc(KEYS_SCAN_MAX, sizeof(*cand));
if (!cand)
return -ENOMEM;
- rc = scan_trusted(keys_dir, revoked_dir, set, cand, &n_cand);
+ rc = scan_trusted(keys_dir, image ? NULL : revoked_dir, set, cand,
+ &n_cand);
if (!rc)
rc = admit_vouched(set, cand, n_cand);
candidates_free(cand, n_cand);
if (rc)
return rc;
+filter:
+ if (image) {
+ size_t i, kept = 0;
+
+ for (i = 0; i < set->n; i++)
+ if (!keys_is_revoked(revoked_dir,
+ set->certs[i].fingerprint))
+ set->certs[kept++] = set->certs[i];
+ set->n = kept;
+ }
if (set->n > 1)
qsort(set->certs, set->n, sizeof(set->certs[0]), cert_cmp);
return 0;
}
+int shirudo_keys_load_split(const char *primary, const char *keys_dir,
+ const char *revoked_dir, bool with_trusted,
+ struct shirudo_trusted_set *set)
+{
+ return load_set(primary, keys_dir, revoked_dir, with_trusted, false,
+ set);
+}
+
+int shirudo_keys_load_image(const char *primary, const char *keys_dir,
+ const char *revoked_dir, bool with_trusted,
+ struct shirudo_trusted_set *set)
+{
+ return load_set(primary, keys_dir, revoked_dir, with_trusted, true,
+ set);
+}
+
+int keys_load(const char *keys_dir, const char *revoked_dir,
+ bool with_trusted, struct shirudo_trusted_set *set)
+{
+ char keys[PATH_MAX], primary[PATH_MAX];
+ int rc;
+
+ if (shirudo_image_config_root_fd() >= 0 &&
+ (!strcmp(keys_dir, SHIRUDO_KEYS_DIR) ||
+ !strcmp(keys_dir, SHIRUDO_RUNTIME_DIR "/keys"))) {
+ rc = shirudo_image_path_relative(keys, sizeof(keys), "keys");
+ if (rc)
+ return rc;
+ return shirudo_keys_load_image(SHIRUDO_RUNTIME_CERT_PATH, keys,
+ SHIRUDO_KEYS_REVOKED_DIR, with_trusted, set);
+ }
+ if (snprintf(primary, sizeof(primary), "%s/%s", keys_dir,
+ SHIRUDO_CERT_FILE) >= (int)sizeof(primary))
+ return -ENAMETOOLONG;
+ return shirudo_keys_load_split(primary, keys_dir, revoked_dir,
+ with_trusted, set);
+}
+
static int digest_cmp(const void *a, const void *b)
{
return memcmp(a, b, 32);
diff --git a/util/keys.h b/util/keys.h
index 2877ab43..57850430 100644
--- a/util/keys.h
+++ b/util/keys.h
@@ -55,12 +55,23 @@ struct shirudo_trusted_set {
int keys_load(const char *keys_dir, const char *revoked_dir,
bool with_trusted, struct shirudo_trusted_set *set);
+int shirudo_keys_load_split(const char *primary, const char *keys_dir,
+ const char *revoked_dir, bool with_trusted,
+ struct shirudo_trusted_set *set);
+
+/* Validate historical vouches before excluding revoked BPF signing keys. */
+int shirudo_keys_load_image(const char *primary, const char *keys_dir,
+ const char *revoked_dir, bool with_trusted,
+ struct shirudo_trusted_set *set);
+
/* A whole file of at most SHIRUDO_KEYS_DER_MAX bytes, xmalloc'd. */
int keys_read_der(const char *path, __u8 **out,
size_t *out_sz);
int keys_digest_der(const __u8 *der, size_t der_sz,
__u8 sha256[32], __u8 fingerprint[32]);
+int keys_digest_file(const char *path, __u8 sha256[32],
+ __u8 fingerprint[32]);
/*
* The digest the init event covers: the one certificate's digest, or
diff --git a/util/paths.h b/util/paths.h
index 4e59ab50..e52b0ea2 100644
--- a/util/paths.h
+++ b/util/paths.h
@@ -14,6 +14,8 @@
#include <stddef.h>
#include <string.h>
+#include "image_config.h"
+
/* Target-side. */
#define SHIRUDO_ETC_DIR "/etc/shirudo"
#define SHIRUDO_VAR_LIB "/var/lib/shirudo"
@@ -22,7 +24,12 @@
#define SHIRUDO_CERT_FILE "cert.der"
#define SHIRUDO_CERT_PATH SHIRUDO_KEYS_DIR "/" SHIRUDO_CERT_FILE
/* Further certificates enrolled beside cert.der, and the revoked ones. */
-#define SHIRUDO_KEYS_REVOKED_DIR SHIRUDO_KEYS_DIR "/revoked"
+#define SHIRUDO_KEYS_TRUSTED_DIR \
+ (shirudo_image_path(SHIRUDO_IMAGE_TRUSTED_KEYS) ?: \
+ SHIRUDO_KEYS_DIR "/trusted")
+#define SHIRUDO_KEYS_REVOKED_DIR \
+ (shirudo_image_path(SHIRUDO_IMAGE_REVOKED_KEYS) ?: \
+ SHIRUDO_KEYS_DIR "/revoked")
/* The initrd's own copy of the keys, measured with the initrd. */
#define SHIRUDO_INITRD_KEYS_DIR "/etc/shirudo/keys"
#define SHIRUDO_INITRD_CERT_PATH SHIRUDO_INITRD_KEYS_DIR "/" SHIRUDO_CERT_FILE
@@ -40,15 +47,22 @@
#define SHIRUDO_BPFFS_DIR SHIRUDO_RUN_DIR "/bpffs"
/* The init record written from the initrd, until the root adopts it. */
#define SHIRUDO_EVENTLOG_PENDING_PATH SHIRUDO_RUN_DIR "/eventlog.pending"
-#define SHIRUDO_BPM_ETC_DIR "/etc/shirudo/bpm"
-#define SHIRUDO_BPM_REGISTRIES_DIR SHIRUDO_BPM_ETC_DIR "/registries.d"
-#define SHIRUDO_BPM_KEYS_DIR SHIRUDO_BPM_ETC_DIR "/keys"
+#define SHIRUDO_BPM_ETC_DIR \
+ (shirudo_image_path(SHIRUDO_IMAGE_ETC) ?: "/etc/shirudo/bpm")
+#define SHIRUDO_BPM_REGISTRIES_DIR \
+ (shirudo_image_path(SHIRUDO_IMAGE_REGISTRIES) ?: \
+ "/etc/shirudo/bpm/registries.d")
+#define SHIRUDO_BPM_KEYS_DIR \
+ (shirudo_image_path(SHIRUDO_IMAGE_REPOSITORY_KEYS) ?: \
+ "/etc/shirudo/bpm/keys")
#define SHIRUDO_BPM_VAR_DIR SHIRUDO_VAR_LIB "/bpm"
#define SHIRUDO_BPM_REPOS_DIR SHIRUDO_BPM_VAR_DIR "/repos"
#define SHIRUDO_BPM_PACKAGES_DIR SHIRUDO_BPM_VAR_DIR "/packages"
#define SHIRUDO_BPM_STAGING_DIR SHIRUDO_BPM_VAR_DIR "/staging"
#define SHIRUDO_BPM_STATE_PATH SHIRUDO_BPM_VAR_DIR "/state.json"
-#define SHIRUDO_BPM_TREE_ETC_PATH SHIRUDO_BPM_ETC_DIR "/upatch-tree.conf"
+#define SHIRUDO_BPM_TREE_ETC_PATH \
+ (shirudo_image_path(SHIRUDO_IMAGE_TREE_CONF) ?: \
+ "/etc/shirudo/bpm/upatch-tree.conf")
#define SHIRUDO_BPM_TREE_VAR_DIR SHIRUDO_BPM_VAR_DIR "/upatch-tree"
#define SHIRUDO_BPM_TREE_STATE_PATH SHIRUDO_BPM_TREE_VAR_DIR "/state.json"
#define SHIRUDO_BPM_TREE_EVENTLOG_PATH SHIRUDO_BPM_TREE_VAR_DIR "/tree-eventlog"
@@ -78,7 +92,8 @@
#define SHIRUDO_SEAL_PERF_PIN SHIRUDO_BPFFS_DIR "/seal_perf_event_alloc_link"
#define SHIRUDO_SEAL_UNLINK_PIN SHIRUDO_BPFFS_DIR "/seal_inode_unlink_link"
#define SHIRUDO_SEAL_RENAME_PIN SHIRUDO_BPFFS_DIR "/seal_inode_rename_link"
-#define SHIRUDO_SEAL_SETATTR_PIN SHIRUDO_BPFFS_DIR "/seal_inode_setattr_link"
+#define SHIRUDO_SEAL_SETATTR_PIN \
+ SHIRUDO_BPFFS_DIR "/seal_inode_setattr_link"
#define SHIRUDO_SEAL_SETXATTR_PIN SHIRUDO_BPFFS_DIR "/seal_inode_setxattr_link"
#define SHIRUDO_SEAL_RMXATTR_PIN SHIRUDO_BPFFS_DIR "/seal_inode_removexattr_link"
#define SHIRUDO_SEAL_POST_SETXATTR_PIN SHIRUDO_BPFFS_DIR "/seal_inode_post_setxattr_link"
diff --git a/util/seal.c b/util/seal.c
index c6912b88..4dfeb456 100644
--- a/util/seal.c
+++ b/util/seal.c
@@ -19,6 +19,7 @@
#include <shirudo/string.h>
+#include "image_config.h"
#include "io.h"
#include "log.h"
#include "paths.h"
@@ -40,10 +41,13 @@ int seal_label(const char *path)
* and never walks two trees at once.
*/
static dev_t seal_root_dev;
+static int seal_walk_error;
static int seal_label_cb(const char *path, const struct stat *sb,
int typeflag, struct FTW *ftwbuf)
{
+ int rc;
+
(void)typeflag;
(void)ftwbuf;
/*
@@ -54,9 +58,19 @@ static int seal_label_cb(const char *path, const struct stat *sb,
*/
if (sb->st_dev != seal_root_dev)
return 0;
- if (seal_label(path) != 0)
- pr_warn("seal: label %s: %s", path, strerror(errno));
- return 0; /* best-effort: keep walking */
+ if (shirudo_image_config_active() &&
+ !S_ISREG(sb->st_mode) && !S_ISDIR(sb->st_mode))
+ rc = -EINVAL;
+ else
+ rc = 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;
+ }
+ }
+ return 0;
}
int seal_label_tree(void)
@@ -65,8 +79,9 @@ int seal_label_tree(void)
struct stat st;
ssize_t n;
- /* Label the binary too: the seal protects it and marks its tasks. */
- n = readlink("/proc/self/exe", self, sizeof(self) - 1);
+ /* 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)
@@ -76,8 +91,9 @@ int seal_label_tree(void)
if (stat(SHIRUDO_VAR_LIB, &st) != 0)
return -errno;
seal_root_dev = st.st_dev;
+ seal_walk_error = 0;
if (nftw(SHIRUDO_VAR_LIB, seal_label_cb, 16, FTW_PHYS | FTW_MOUNT) != 0)
- return -errno;
+ return seal_walk_error ? seal_walk_error : -errno;
return 0;
}
diff --git a/util/tpm.c b/util/tpm.c
index aeff1c68..0d3fc0d5 100644
--- a/util/tpm.c
+++ b/util/tpm.c
@@ -17,6 +17,7 @@
#include <shirudo/string.h>
#include "config.h"
+#include "image_config.h"
#include "log.h"
#include "malloc.h"
#include "paths.h"
@@ -42,7 +43,8 @@ int tpm_tcti(char *buf, size_t size)
const char *tcti;
ssize_t n;
- tcti = getenv("SHIRUDO_TPM_TCTI");
+ tcti = shirudo_image_config_active() ? NULL :
+ getenv("SHIRUDO_TPM_TCTI");
if ((!tcti || !*tcti) && config_load(&cfg) == 0 &&
cfg.tpm_tcti[0])
tcti = cfg.tpm_tcti;
@@ -193,6 +195,26 @@ int tpm_pcr_extend(struct shirudo_tpm *t, __u32 pcr,
return 0;
}
+int shirudo_tpm_boot_identity(struct shirudo_tpm *t, __u32 *reset,
+ __u32 *restart)
+{
+ TPMS_TIME_INFO *info = NULL;
+ TSS2_RC rc;
+
+ if (!t || !reset || !restart)
+ return -EINVAL;
+ rc = Esys_ReadClock(t->esys, ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
+ &info);
+ if (rc != TSS2_RC_SUCCESS) {
+ pr_err("tpm: ReadClock: rc=0x%08x", (__u32)rc);
+ return -EIO;
+ }
+ *reset = info->clockInfo.resetCount;
+ *restart = info->clockInfo.restartCount;
+ Esys_Free(info);
+ return 0;
+}
+
int tpm_reset_count(struct shirudo_tpm *t, __u32 *out)
{
TPMS_TIME_INFO *info = NULL;
diff --git a/util/tpm.h b/util/tpm.h
index e6436cf1..98c4177d 100644
--- a/util/tpm.h
+++ b/util/tpm.h
@@ -37,6 +37,8 @@ int tpm_pcr_extend(struct shirudo_tpm *t, __u32 pcr,
* of the period a clear-on-reset index lives through.
*/
int tpm_reset_count(struct shirudo_tpm *t, __u32 *out);
+int shirudo_tpm_boot_identity(struct shirudo_tpm *t, __u32 *reset,
+ __u32 *restart);
struct tpm_nv_info {
bool defined;