Skip to content

eBPF-based security platform

image: match config devices to the root being prepared

KP Singhf0f0a554ff3c
diff --git a/Documentation/config-images.rst b/Documentation/config-images.rst
index 9ed6e68d..77be5295 100644
--- a/Documentation/config-images.rst
+++ b/Documentation/config-images.rst
@@ -402,7 +402,21 @@ mode query used to keep commands inside image policy.
The LSM observes dm-verity metadata through the existing
``bdev_setintegrity`` hook. Lookup checks the block-device pointer, device
-number and disk sequence. The free hook attempts to delete the record,
+number and disk sequence. The runtime root must match the authenticated
+bootstrap value. A config root must match the expected root staged by the
+trusted task creating that mapping. An unrelated mapping made by a trusted
+task is not a config device.
+
+Expected roots live in image-only task storage. The hook reads the preparing
+task's entry and consumes it when the kernel reports the matching root.
+Creation clears it on every exit and reports an error if cleanup fails. A
+remaining valid entry blocks another preparation until cleared or task exit.
+The same helper covers boot activation, temporary download verification and
+live config activation. Initial trusted boot validates the key set before
+installing the guards, then recreates those mappings under enforcement.
+Untrusted tasks cannot modify the expectation map through BPF or a writable
+FD.
+The free hook attempts to delete the device record,
but upstream dispatch can skip that notification. Device identity checks
and cleanup are distinct concerns.
diff --git a/bpf/bpf2_verity.c b/bpf/bpf2_verity.c
index 6f874130..aee4e672 100644
--- a/bpf/bpf2_verity.c
+++ b/bpf/bpf2_verity.c
@@ -123,6 +123,10 @@ struct dm_verity_digest___shirudo {
size_t digest_len;
} __attribute__((preserve_access_index));
+/*
+ * Runtime roots match the frozen UKI value. Config roots must match the current
+ * trusted task's one-use expectation; unrelated mappings gain no status.
+ */
SEC("lsm.s/bdev_setintegrity")
int BPF_PROG(shirudo_lsm_bdev_setintegrity, struct block_device *bdev,
enum lsm_integrity_type type, const void *value,
@@ -130,6 +134,7 @@ int BPF_PROG(shirudo_lsm_bdev_setintegrity, struct block_device *bdev,
{
const struct dm_verity_digest___shirudo *digest = value;
struct shirudo_sb_id *id = shirudo_policy();
+ struct shirudo_expected_root *expected;
struct shirudo_device device = {};
__u64 cookie = (__u64)bdev;
const unsigned char *root;
@@ -161,12 +166,24 @@ int BPF_PROG(shirudo_lsm_bdev_setintegrity, struct block_device *bdev,
diff = 0;
for (i = 0; i < sizeof(device.root); i++)
diff |= device.root[i] ^ id->runtime_root[i];
- if (!diff)
+ if (!diff) {
device.kind = SHIRUDO_DEVICE_RUNTIME;
- else if (shirudo_task_is_self(id))
+ } else {
+ if (!shirudo_task_is_self(id))
+ return 0;
+ expected = bpf_task_storage_get(&shirudo_roots,
+ bpf_get_current_task_btf(),
+ NULL, 0);
+ if (!expected || !expected->valid)
+ return 0;
+ diff = 0;
+ for (i = 0; i < sizeof(device.root); i++)
+ diff |= device.root[i] ^ expected->root[i];
+ if (diff)
+ return 0;
+ expected->valid = 0;
device.kind = SHIRUDO_DEVICE_CONFIG;
- else
- return 0;
+ }
device.dev = BPF_CORE_READ(bdev, bd_dev);
device.diskseq = BPF_CORE_READ(bdev, bd_disk, diskseq);
if (!device.diskseq)
diff --git a/bpf/verity_policy.h b/bpf/verity_policy.h
index 389174d7..4311f982 100644
--- a/bpf/verity_policy.h
+++ b/bpf/verity_policy.h
@@ -26,6 +26,19 @@ struct {
__type(value, struct shirudo_device);
} shirudo_devices SEC(".maps");
+/* Trusted userspace records the expected root for the preparing task. */
+struct shirudo_expected_root {
+ __u8 root[32];
+ __u32 valid;
+};
+
+struct {
+ __uint(type, BPF_MAP_TYPE_TASK_STORAGE);
+ __uint(map_flags, BPF_F_NO_PREALLOC);
+ __type(key, int);
+ __type(value, struct shirudo_expected_root);
+} shirudo_roots SEC(".maps");
+
struct inode *bpf_real_data_inode(struct file *file) __ksym;
static __always_inline bool shirudo_bootstrap_task(void)
diff --git a/builtin-init.c b/builtin-init.c
index f4430ed5..df5253d0 100644
--- a/builtin-init.c
+++ b/builtin-init.c
@@ -692,7 +692,8 @@ static int share_policy_maps(struct bpf_object *source,
int rc;
for (i = 0; i < ARRAY_SIZE(shirudo_guard_maps); i++) {
- if (!image_mode && i == SHIRUDO_GUARD_DEVICES)
+ if (!image_mode && (i == SHIRUDO_GUARD_DEVICES ||
+ i == SHIRUDO_GUARD_ROOTS))
continue;
from = bpf_object__find_map_by_name(source,
shirudo_guard_maps[i].name);
@@ -802,7 +803,7 @@ static void collect_asymmetric(key_serial_t serial, const char *type,
k->serials[k->n++] = (__s32)serial;
}
-/* Seed bootstrap before hooks attach, then forbid userspace map writes. */
+/* Seed bootstrap and freeze shared state except the expected config root. */
static int freeze_task_maps(struct bpf_object *obj)
{
struct shirudo_task_authority authority = {
@@ -819,7 +820,8 @@ static int freeze_task_maps(struct bpf_object *obj)
const struct shirudo_guard_map *expected =
&shirudo_guard_maps[i];
- if (!image_mode && i == SHIRUDO_GUARD_DEVICES)
+ if (!image_mode && (i == SHIRUDO_GUARD_DEVICES ||
+ i == SHIRUDO_GUARD_ROOTS))
continue;
map = bpf_object__find_map_by_name(obj, expected->name);
if (!map) {
@@ -832,7 +834,9 @@ static int freeze_task_maps(struct bpf_object *obj)
rc = -errno;
break;
}
- if (bpf_map_freeze(fd) || bpf_obj_pin(fd, expected->pin)) {
+ /* Expected roots change for each mapping. */
+ if ((i != SHIRUDO_GUARD_ROOTS && bpf_map_freeze(fd)) ||
+ bpf_obj_pin(fd, expected->pin)) {
rc = -errno;
break;
}
@@ -1131,6 +1135,7 @@ out:
(void)unlink(SHIRUDO_TAMPER_PIN);
(void)unlink(SHIRUDO_BPFFS_DIR "/task_authority");
(void)unlink(SHIRUDO_BPFFS_DIR "/devices");
+ (void)unlink(SHIRUDO_BPFFS_DIR "/config_roots");
}
bpf_object__close(seal_obj);
bpf_link__destroy(link_bpf);
diff --git a/util/dm_verity.c b/util/dm_verity.c
index fcbd5dad..7b675beb 100644
--- a/util/dm_verity.c
+++ b/util/dm_verity.c
@@ -27,6 +27,7 @@
#include "dm_verity.h"
#include "hex.h"
+#include "image_guards.h"
#include "keyring.h"
#include "malloc.h"
@@ -426,12 +427,16 @@ int shirudo_dm_verity_create(const struct shirudo_config_image_open *image,
struct shirudo_dm_verity device = {.data_loop_fd = -1,
.hash_loop_fd = -1};
char root[65], salt[65], params[512], signature[64];
+ struct shirudo_config_root_guard guard = {
+ .map_fd = -1,
+ .task_fd = -1,
+ };
struct stat data_st, hash_st;
struct dm_target_spec *target;
union dm_buffer buf;
bool created = false;
key_serial_t key = -1;
- int control = -1, rc, n;
+ int control = -1, rc, cleanup, n;
if (!image || !out || !dm_name_ok(name))
return -EINVAL;
@@ -506,6 +511,11 @@ int shirudo_dm_verity_create(const struct shirudo_config_image_open *image,
memcpy(target + 1, params, n + 1);
target->next = (sizeof(*target) + n + 1 + 7) & ~7u;
buf.io.data_size = buf.io.data_start + target->next;
+ if (image->image.signed_root) {
+ rc = shirudo_config_root_begin(image->image.root_hash, &guard);
+ if (rc)
+ goto out;
+ }
rc = dm_command(control, DM_TABLE_LOAD, &buf);
if (rc)
goto out;
@@ -522,6 +532,9 @@ int shirudo_dm_verity_create(const struct shirudo_config_image_open *image,
if (!rc)
rc = device_path(&device, buf.io.dev);
out:
+ cleanup = shirudo_config_root_end(&guard);
+ if (!rc)
+ rc = cleanup;
if (key >= 0)
(void)keyring_unlink_key(KEY_SPEC_THREAD_KEYRING, key);
if (rc) {
diff --git a/util/identity.h b/util/identity.h
index 4fe86a55..007b87b6 100644
--- a/util/identity.h
+++ b/util/identity.h
@@ -48,6 +48,15 @@ struct shirudo_device_authority {
__u64 diskseq;
};
+/* Image-only task storage for one config mapping preparation. */
+struct shirudo_expected_root {
+ __u8 root[32];
+ __u32 valid;
+};
+
+_Static_assert(sizeof(struct shirudo_expected_root) == 36,
+ "expected config root ABI");
+
struct shirudo_task_authority {
__u8 trusted;
__u8 candidate_valid;
diff --git a/util/image_guards.c b/util/image_guards.c
index b47c6cde..709a8a30 100644
--- a/util/image_guards.c
+++ b/util/image_guards.c
@@ -7,6 +7,7 @@
#include <stdint.h>
#include <stdio.h>
#include <string.h>
+#include <sys/syscall.h>
#include <unistd.h>
#include <linux/bpf.h>
@@ -20,6 +21,7 @@
#include "log.h"
#include "macros.h"
#include "paths.h"
+#include "shirudo_mode.h"
#include "tamper.h"
const struct shirudo_image_guard
@@ -123,6 +125,11 @@ const struct shirudo_guard_map shirudo_guard_maps[SHIRUDO_GUARD_MAP_COUNT] = {
BPF_MAP_TYPE_HASH, sizeof(__u64),
sizeof(struct shirudo_device_authority), 128, 0,
},
+ [SHIRUDO_GUARD_ROOTS] = {
+ "shirudo_roots", SHIRUDO_BPFFS_DIR "/config_roots",
+ BPF_MAP_TYPE_TASK_STORAGE, sizeof(__u32),
+ sizeof(struct shirudo_expected_root), 0, BPF_F_NO_PREALLOC,
+ },
[SHIRUDO_GUARD_TAMPER] = {
"shirudo_tamper", SHIRUDO_TAMPER_PIN,
BPF_MAP_TYPE_ARRAY, sizeof(__u32),
@@ -130,6 +137,75 @@ const struct shirudo_guard_map shirudo_guard_maps[SHIRUDO_GUARD_MAP_COUNT] = {
},
};
+/* Initial authenticated boot creates mappings before these guards exist. */
+int shirudo_config_root_begin(const __u8 root[32],
+ struct shirudo_config_root_guard *guard)
+{
+ struct shirudo_expected_root previous = {}, expected = { .valid = 1 };
+ const struct shirudo_guard_map *map =
+ &shirudo_guard_maps[SHIRUDO_GUARD_ROOTS];
+ int mode, rc;
+
+ *guard = (struct shirudo_config_root_guard) {
+ .map_fd = -1,
+ .task_fd = -1,
+ };
+ mode = shirudo_identity_mode();
+ if (mode == SHIRUDO_MODE_NONE)
+ return 0;
+ if (mode != SHIRUDO_MODE_IMAGE)
+ return mode < 0 ? mode : -EPERM;
+ guard->map_fd = bpf_obj_get(map->pin);
+ if (guard->map_fd < 0)
+ return -errno;
+ guard->task_fd = syscall(SYS_pidfd_open, getpid(), 0);
+ if (guard->task_fd < 0) {
+ rc = -errno;
+ goto fail;
+ }
+ if (bpf_map_lookup_elem(guard->map_fd, &guard->task_fd, &previous)) {
+ if (errno != ENOENT) {
+ rc = -errno;
+ goto fail;
+ }
+ } else if (previous.valid) {
+ rc = -EBUSY;
+ goto fail;
+ }
+ memcpy(expected.root, root, sizeof(expected.root));
+ if (bpf_map_update_elem(guard->map_fd, &guard->task_fd, &expected,
+ BPF_ANY)) {
+ rc = -errno;
+ goto fail;
+ }
+ return 0;
+fail:
+ if (guard->task_fd >= 0)
+ close(guard->task_fd);
+ close(guard->map_fd);
+ guard->map_fd = -1;
+ guard->task_fd = -1;
+ return rc;
+}
+
+/* Clear through retained FDs on every exit; report failure to clear. */
+int shirudo_config_root_end(struct shirudo_config_root_guard *guard)
+{
+ struct shirudo_expected_root empty = {};
+ int rc = 0;
+
+ if (guard->map_fd < 0)
+ return 0;
+ if (bpf_map_update_elem(guard->map_fd, &guard->task_fd, &empty,
+ BPF_ANY))
+ rc = -errno;
+ close(guard->task_fd);
+ close(guard->map_fd);
+ guard->map_fd = -1;
+ guard->task_fd = -1;
+ return rc;
+}
+
static int guard_map_ids(__u32 ids[SHIRUDO_GUARD_MAP_COUNT])
{
struct bpf_map_info info;
diff --git a/util/image_guards.h b/util/image_guards.h
index 4690a1e7..b441e349 100644
--- a/util/image_guards.h
+++ b/util/image_guards.h
@@ -34,6 +34,7 @@ enum shirudo_guard_map_index {
SHIRUDO_GUARD_IDENTITY,
SHIRUDO_GUARD_TASKS,
SHIRUDO_GUARD_DEVICES,
+ SHIRUDO_GUARD_ROOTS,
SHIRUDO_GUARD_TAMPER,
SHIRUDO_GUARD_MAP_COUNT,
};
@@ -63,6 +64,14 @@ extern const struct shirudo_image_guard
extern const struct shirudo_guard_map
shirudo_guard_maps[SHIRUDO_GUARD_MAP_COUNT];
+struct shirudo_config_root_guard {
+ int map_fd;
+ int task_fd;
+};
+
+int shirudo_config_root_begin(const __u8 root[32],
+ struct shirudo_config_root_guard *guard);
+int shirudo_config_root_end(struct shirudo_config_root_guard *guard);
int shirudo_image_guards_ready(void);
#endif /* SHIRUDO_IMAGE_GUARDS_H */