eBPF-based security platform
image: check a pinned map's shape before a lookup copies its value
Daniel Borkmann72b193d418df
diff --git a/testing/helper/test-register.c b/testing/helper/test-register.cindex b326fc63..d3495375 100644--- a/testing/helper/test-register.c+++ b/testing/helper/test-register.c@@ -34,7 +34,11 @@/* Record the TPM writer's public identity before the VM attaches its LSM. */static int writer_create(void){- struct bpf_map_create_opts opts = { .sz = sizeof(opts) };+ const struct guard_map *map = &shirudo_guard_maps[GUARD_IDENTITY];+ struct bpf_map_create_opts opts = {+ .sz = sizeof(opts),+ .map_flags = map->flags,+ };struct identity id = {};int fd, ret;__u32 zero = 0;@@ -42,8 +46,8 @@ static int writer_create(void)ret = nv_writer_create(id.nv_writer_seed, id.nv_writer_public);if (ret)goto out;- fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "shirudo_test_id", sizeof(zero),- sizeof(id), 1, &opts);+ fd = bpf_map_create(map->type, map->name, map->key_size,+ map->value_size, map->max_entries, &opts);if (fd < 0) {ret = -errno;goto out;diff --git a/util/identity.c b/util/identity.cindex e78f0c1d..39cf6a75 100644--- a/util/identity.c+++ b/util/identity.c@@ -2,7 +2,8 @@/** Copyright (c) 2026 Authors of shirudo*- * Read back the identity map init pinned in the private bpffs.+ * Read back the identity map init pinned in the private bpffs, and check+ * the shape of every map init pins there.*/#include <errno.h>@@ -20,8 +21,53 @@#include <shirudo/misc.h>#include "identity.h"+#include "integrity.h"#include "io.h"#include "paths.h"+#include "tamper.h"++const struct guard_map shirudo_guard_maps[GUARD_MAP_COUNT] = {+ [GUARD_IDENTITY] = {+ "shirudo_bpffs_sb", IDENTITY_PIN,+ BPF_MAP_TYPE_ARRAY, sizeof(__u32),+ sizeof(struct identity), 1, BPF_F_RDONLY_PROG,+ },+ [GUARD_TASKS] = {+ "shirudo_tasks", BPFFS_DIR "/task_authority",+ BPF_MAP_TYPE_TASK_STORAGE, sizeof(__u32),+ sizeof(struct task_authority), 0, BPF_F_NO_PREALLOC,+ },+ [GUARD_POLICY] = {+ "shirudo_task_policy", POLICY_PIN,+ BPF_MAP_TYPE_TASK_STORAGE, sizeof(__u32),+ sizeof(struct task_policy), 0, BPF_F_NO_PREALLOC,+ },+ [GUARD_SOCKET_POLICY] = {+ "shirudo_sock_policy", SOCKET_POLICY_PIN,+ BPF_MAP_TYPE_SK_STORAGE, sizeof(__u32), sizeof(__u64), 0,+ BPF_F_NO_PREALLOC | BPF_F_CLONE,+ },+ [GUARD_DEVICES] = {+ "shirudo_devices", BPFFS_DIR "/devices",+ BPF_MAP_TYPE_HASH, sizeof(__u64),+ sizeof(struct device_authority), 128, 0,+ },+ [GUARD_ROOTS] = {+ "shirudo_roots", BPFFS_DIR "/config_roots",+ BPF_MAP_TYPE_TASK_STORAGE, sizeof(__u32),+ sizeof(struct expected_root), 0, BPF_F_NO_PREALLOC,+ },+ [GUARD_TAMPER] = {+ "shirudo_tamper", TAMPER_PIN,+ BPF_MAP_TYPE_ARRAY, sizeof(__u32),+ sizeof(__u64), TAMPER_MAX, 0,+ },+ [GUARD_INTEGRITY] = {+ "shirudo_integrity", INTEGRITY_PIN,+ BPF_MAP_TYPE_ARRAY, sizeof(__u32),+ sizeof(__u64), INTEGRITY_MAX, 0,+ },+};int enforcement_active(void){@@ -34,16 +80,34 @@ int enforcement_active(void)return errno == EINVAL || errno == ENOSYS ? 0 : -errno;}-static int identity_map_read(int fd, struct identity *out)+int guard_map_check(int fd, const struct guard_map *expected, __u32 *id){struct bpf_map_info info = {};- __u32 len = sizeof(info);- __u32 zero = 0;+ __u32 length = sizeof(info);- if (bpf_map_get_info_by_fd(fd, &info, &len))+ if (bpf_map_get_info_by_fd(fd, &info, &length))return -errno;- if (info.value_size != sizeof(*out))+ if (!info.id || info.type != expected->type ||+ info.key_size != expected->key_size ||+ info.value_size != expected->value_size ||+ info.max_entries != expected->max_entries ||+ info.map_flags != expected->flags ||+ info.name[sizeof(info.name) - 1] ||+ strncmp(info.name, expected->name, sizeof(info.name) - 1))return -EPROTO;+ if (id)+ *id = info.id;+ return 0;+}++static int identity_map_read(int fd, struct identity *out)+{+ __u32 zero = 0;+ int ret;++ ret = guard_map_check(fd, &shirudo_guard_maps[GUARD_IDENTITY], NULL);+ if (ret)+ return ret;if (bpf_map_lookup_elem(fd, &zero, out))return -errno;return 0;diff --git a/util/identity.h b/util/identity.hindex 07eed494..549f489e 100644--- a/util/identity.h+++ b/util/identity.h@@ -2,7 +2,8 @@/** Copyright (c) 2026 Authors of shirudo*- * The identity init froze into the kernel, read back from its bpffs pin.+ * The identity init froze into the kernel, read back from its bpffs pin,+ * and the shape of every map init pins beside it.*/#ifndef SHIRUDO_UTIL_IDENTITY_H@@ -81,6 +82,38 @@ struct mount_record {BUILD_BUG_ON(sizeof(struct mount_record) != 8);+enum guard_map_index {+ GUARD_IDENTITY,+ GUARD_TASKS,+ GUARD_DEVICES,+ GUARD_ROOTS,+ GUARD_TAMPER,+ GUARD_POLICY,+ GUARD_SOCKET_POLICY,+ GUARD_INTEGRITY,+ GUARD_MAP_COUNT,+};++struct guard_map {+ const char *name;+ const char *pin;+ __u32 type;+ __u32 key_size;+ __u32 value_size;+ __u32 max_entries;+ __u32 flags;+};++extern const struct guard_map+ shirudo_guard_maps[GUARD_MAP_COUNT];++/*+ * 0 when the map behind fd has the type, sizes, flags and name expected+ * gives it, -EPROTO when it has not, or the kernel's error; *id, unless+ * NULL, then holds the map's id.+ */+int guard_map_check(int fd, const struct guard_map *expected, __u32 *id);+/* 1 when enforcement is active, 0 before init, otherwise -errno. */int enforcement_active(void);diff --git a/util/image_guards.c b/util/image_guards.cindex 9f88e40d..1fa29d19 100644--- a/util/image_guards.c+++ b/util/image_guards.c@@ -150,49 +150,6 @@ shirudo_gate_guards[GATE_HOOK_COUNT] = {},};-const struct guard_map shirudo_guard_maps[GUARD_MAP_COUNT] = {- [GUARD_IDENTITY] = {- "shirudo_bpffs_sb", IDENTITY_PIN,- BPF_MAP_TYPE_ARRAY, sizeof(__u32),- sizeof(struct identity), 1, BPF_F_RDONLY_PROG,- },- [GUARD_TASKS] = {- "shirudo_tasks", BPFFS_DIR "/task_authority",- BPF_MAP_TYPE_TASK_STORAGE, sizeof(__u32),- sizeof(struct task_authority), 0, BPF_F_NO_PREALLOC,- },- [GUARD_POLICY] = {- "shirudo_task_policy", POLICY_PIN,- BPF_MAP_TYPE_TASK_STORAGE, sizeof(__u32),- sizeof(struct task_policy), 0, BPF_F_NO_PREALLOC,- },- [GUARD_SOCKET_POLICY] = {- "shirudo_sock_policy", SOCKET_POLICY_PIN,- BPF_MAP_TYPE_SK_STORAGE, sizeof(__u32), sizeof(__u64), 0,- BPF_F_NO_PREALLOC | BPF_F_CLONE,- },- [GUARD_DEVICES] = {- "shirudo_devices", BPFFS_DIR "/devices",- BPF_MAP_TYPE_HASH, sizeof(__u64),- sizeof(struct device_authority), 128, 0,- },- [GUARD_ROOTS] = {- "shirudo_roots", BPFFS_DIR "/config_roots",- BPF_MAP_TYPE_TASK_STORAGE, sizeof(__u32),- sizeof(struct expected_root), 0, BPF_F_NO_PREALLOC,- },- [GUARD_TAMPER] = {- "shirudo_tamper", TAMPER_PIN,- BPF_MAP_TYPE_ARRAY, sizeof(__u32),- sizeof(__u64), TAMPER_MAX, 0,- },- [GUARD_INTEGRITY] = {- "shirudo_integrity", INTEGRITY_PIN,- BPF_MAP_TYPE_ARRAY, sizeof(__u32),- sizeof(__u64), INTEGRITY_MAX, 0,- },-};-/* Initial authenticated boot creates mappings before these guards exist. */int config_root_begin(const __u8 root[32],struct config_root_guard *guard)@@ -214,6 +171,9 @@ int config_root_begin(const __u8 root[32],guard->map_fd = bpf_obj_get(map->pin);if (guard->map_fd < 0)return -errno;+ ret = guard_map_check(guard->map_fd, map, NULL);+ if (ret)+ goto fail;guard->task_fd = syscall(SYS_pidfd_open, getpid(), 0);if (guard->task_fd < 0) {ret = -errno;@@ -262,26 +222,6 @@ int config_root_end(struct config_root_guard *guard)return ret;}-int guard_map_check(int fd, const struct guard_map *expected, __u32 *id)-{- struct bpf_map_info info = {};- __u32 length = sizeof(info);-- if (bpf_map_get_info_by_fd(fd, &info, &length))- return -errno;- if (!info.id || info.type != expected->type ||- info.key_size != expected->key_size ||- info.value_size != expected->value_size ||- info.max_entries != expected->max_entries ||- info.map_flags != expected->flags ||- info.name[sizeof(info.name) - 1] ||- strncmp(info.name, expected->name, sizeof(info.name) - 1))- return -EPROTO;- if (id)- *id = info.id;- return 0;-}-static int guard_map_ids(__u32 ids[GUARD_MAP_COUNT]){__u32 i;diff --git a/util/image_guards.h b/util/image_guards.hindex 5eecbbd8..ef484adb 100644--- a/util/image_guards.h+++ b/util/image_guards.h@@ -32,28 +32,6 @@ enum gate_guard {GATE_HOOK_COUNT,};-enum guard_map_index {- GUARD_IDENTITY,- GUARD_TASKS,- GUARD_DEVICES,- GUARD_ROOTS,- GUARD_TAMPER,- GUARD_POLICY,- GUARD_SOCKET_POLICY,- GUARD_INTEGRITY,- GUARD_MAP_COUNT,-};--struct guard_map {- const char *name;- const char *pin;- __u32 type;- __u32 key_size;- __u32 value_size;- __u32 max_entries;- __u32 flags;-};-extern const struct image_guardshirudo_seal_guards[SEAL_HOOK_COUNT];extern const struct image_guard@@ -66,15 +44,6 @@ extern const struct image_guardshirudo_observer_guards[OBSERVER_HOOK_COUNT];extern const struct image_guardshirudo_gate_guards[GATE_HOOK_COUNT];-extern const struct guard_map- shirudo_guard_maps[GUARD_MAP_COUNT];--/*- * 0 when the map behind fd has the type, sizes, flags and name expected- * gives it, -EPROTO when it has not, or the kernel's error; *id, unless- * NULL, then holds the map's id.- */-int guard_map_check(int fd, const struct guard_map *expected, __u32 *id);struct config_root_guard {int map_fd;diff --git a/util/image_runtime.c b/util/image_runtime.cindex f47a9fb3..f8991d8f 100644--- a/util/image_runtime.c+++ b/util/image_runtime.c@@ -189,6 +189,7 @@ static int mapping_name(const __u8 root[32], char out[128])static int device_observed(dev_t device, const __u8 root[32], __u32 kind){+ const struct guard_map *devices = &shirudo_guard_maps[GUARD_DEVICES];struct device_authority entry;__u32 dev = kernel_dev(device);__u64 key, next, diskseq;@@ -213,11 +214,14 @@ static int device_observed(dev_t device, const __u8 root[32], __u32 kind)close(block);return ret;}- fd = bpf_obj_get(BPFFS_DIR "/devices");+ fd = bpf_obj_get(devices->pin);if (fd < 0) {ret = -errno;goto out_block;}+ ret = guard_map_check(fd, devices, NULL);+ if (ret)+ goto out;ret = -EACCES;if (bpf_map_get_next_key(fd, NULL, &key))goto out;diff --git a/util/integrity.c b/util/integrity.cindex 7015eb57..66a46894 100644--- a/util/integrity.c+++ b/util/integrity.c@@ -6,8 +6,8 @@*/#include <shirudo/misc.h>+#include "identity.h"#include "integrity.h"-#include "paths.h"#include "tamper.h"static const struct {@@ -76,6 +76,6 @@ const char *integrity_meaning(enum integrity_point point)int integrity_read(struct integrity_counts *out){- return counters_read(INTEGRITY_PIN, INTEGRITY_MAX, out->count,- &out->total);+ return counters_read(&shirudo_guard_maps[GUARD_INTEGRITY],+ INTEGRITY_MAX, out->count, &out->total);}diff --git a/util/tamper.c b/util/tamper.cindex a77f1d23..2742d03c 100644--- a/util/tamper.c+++ b/util/tamper.c@@ -13,7 +13,7 @@#include <shirudo/misc.h>-#include "paths.h"+#include "identity.h"#include "tamper.h"static const struct {@@ -120,32 +120,26 @@ const char *tamper_meaning(enum tamper_point point)return tamper_points[point].meaning;}-int counters_read(const char *pin, __u32 n, __u64 *count, __u64 *total)+int counters_read(const struct guard_map *map, __u32 n, __u64 *count,+ __u64 *total){- struct bpf_map_info info = {};- __u32 len = sizeof(info);int fd, ret;__u32 key;memset(count, 0, n * sizeof(*count));*total = 0;- fd = bpf_obj_get(pin);+ fd = bpf_obj_get(map->pin);if (fd < 0)return -errno;- ret = bpf_map_get_info_by_fd(fd, &info, &len);- if (ret) {- ret = -errno;- } else if (info.max_entries != n || info.value_size != sizeof(*count)) {+ ret = guard_map_check(fd, map, NULL);+ if (!ret && (map->max_entries != n ||+ map->value_size != sizeof(*count)))ret = -EPROTO;- } else {- for (key = 0; key < n; key++) {- ret = bpf_map_lookup_elem(fd, &key, &count[key]);- if (ret) {- ret = -errno;- break;- }+ for (key = 0; !ret && key < n; key++) {+ if (bpf_map_lookup_elem(fd, &key, &count[key]))+ ret = -errno;+ else*total += count[key];- }}close(fd);return ret;@@ -153,5 +147,6 @@ int counters_read(const char *pin, __u32 n, __u64 *count, __u64 *total)int tamper_read(struct tamper_counts *out){- return counters_read(TAMPER_PIN, TAMPER_MAX, out->count, &out->total);+ return counters_read(&shirudo_guard_maps[GUARD_TAMPER], TAMPER_MAX,+ out->count, &out->total);}diff --git a/util/tamper.h b/util/tamper.hindex 2c9e4f09..3e50c70e 100644--- a/util/tamper.h+++ b/util/tamper.h@@ -26,7 +26,10 @@ const char *tamper_meaning(enum tamper_point point);*/int tamper_read(struct tamper_counts *out);-/* tamper_read() of the n counters of any such map, pinned at pin. */-int counters_read(const char *pin, __u32 n, __u64 *count, __u64 *total);+struct guard_map;++/* tamper_read() of the n counters of the guard map, at its pin. */+int counters_read(const struct guard_map *map, __u32 n, __u64 *count,+ __u64 *total);#endif /* SHIRUDO_UTIL_TAMPER_H */