Skip to content

eBPF-based security platform

image: check one guard map's shape in a function of its own

Daniel Borkmann0eb2967b83ba
diff --git a/util/image_guards.c b/util/image_guards.c
index 2ea34ed9..9f88e40d 100644
--- a/util/image_guards.c
+++ b/util/image_guards.c
@@ -262,10 +262,28 @@ 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])
{
- struct bpf_map_info info;
- __u32 length;
__u32 i;
int fd, ret;
@@ -276,27 +294,13 @@ static int guard_map_ids(__u32 ids[GUARD_MAP_COUNT])
fd = bpf_obj_get(expected->pin);
if (fd < 0)
return -errno;
- memset(&info, 0, sizeof(info));
- length = sizeof(info);
- ret = bpf_map_get_info_by_fd(fd, &info, &length);
- if (ret)
- ret = -errno;
- else if (!info.id || info.type != expected->type ||
- info.key_size != expected->key_size ||
- info.value_size != expected->value_size ||
- info.max_entries != expected->max_entries ||
- info.map_flags != expected->flags ||
- info.name[sizeof(info.name) - 1] ||
- strncmp(info.name, expected->name,
- sizeof(info.name) - 1))
- ret = -EPROTO;
+ ret = guard_map_check(fd, expected, &ids[i]);
close(fd);
if (ret) {
pr_err("image: invalid guard map %s: %s", expected->pin,
strerror(-ret));
return ret;
}
- ids[i] = info.id;
}
return 0;
}
diff --git a/util/image_guards.h b/util/image_guards.h
index 96c12793..5eecbbd8 100644
--- a/util/image_guards.h
+++ b/util/image_guards.h
@@ -69,6 +69,13 @@ extern const struct image_guard
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;
int task_fd;