Skip to content

eBPF-based security platform

identity: compare a guard's name with the kernel's in one place

Daniel Borkmanncfa71216fbce
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index 4e483a73..44ac55fa 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -885,12 +885,12 @@ signed config image.
Unit suites and fuzz targets
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-The unit suites in :src:`testing/unit/` cover 37 of the modules in
+The unit suites in :src:`testing/unit/` cover 39 of the modules in
:src:`util/`: ``attest``, ``bpm_oci``, ``bpm_package``, ``bpm_state``,
``bpm_tree``, ``bpm_tree_overlap``, ``bpm_tree_state``, ``chain``,
``chain_history``, ``conditions``, ``config_update``, ``crypto_openssl``,
-``eventlog``, ``format``, ``hex``, ``io``, ``java_agent``, ``json``,
-``kernel_abi``,
+``curl``, ``eventlog``, ``format``, ``hex``, ``identity``, ``io``,
+``java_agent``, ``json``, ``kernel_abi``,
``keepalive``, ``keepalive_quote``, ``kernel_build_id``,
``kernel_prereq``, ``kernel_release``, ``keys``, ``log``, ``measured``,
``options``,
@@ -1372,7 +1372,7 @@ Parsers and units
- The dm-verity superblock check in :src:`util/config_image.c` has no
fuzz target. ``t5001`` covers truncation and unsupported fields.
-- 39 of the 69 modules in :src:`util/` have no unit suite of their own.
+- 37 of the 76 modules in :src:`util/` have no unit suite of their own.
Related
-------
diff --git a/testing/unit/identity.c b/testing/unit/identity.c
new file mode 100644
index 00000000..7dc48ebe
--- /dev/null
+++ b/testing/unit/identity.c
@@ -0,0 +1,51 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+#include <linux/bpf.h>
+
+#include "identity.h"
+#include "unit.h"
+
+static void kernel_name_is_the_start_of_a_long_one(void)
+{
+ char name[BPF_OBJ_NAME_LEN] = "shirudo_bpffs_s";
+
+ EXPECT_TRUE(guard_name_matches(name, "shirudo_bpffs_sb"));
+ EXPECT_TRUE(guard_name_matches(name, "shirudo_bpffs_s"));
+ EXPECT_TRUE(guard_name_matches(name, "shirudo_bpffs_s_and_more"));
+ EXPECT_FALSE(guard_name_matches(name, "shirudo_bpffs_"));
+ EXPECT_FALSE(guard_name_matches(name, "shirudo_bpffs_x"));
+ EXPECT_FALSE(guard_name_matches(name, ""));
+}
+
+static void short_name_is_the_whole_one(void)
+{
+ char name[BPF_OBJ_NAME_LEN] = "shirudo_tasks";
+
+ EXPECT_TRUE(guard_name_matches(name, "shirudo_tasks"));
+ EXPECT_FALSE(guard_name_matches(name, "shirudo_task"));
+ EXPECT_FALSE(guard_name_matches(name, "shirudo_tasks_"));
+ EXPECT_FALSE(guard_name_matches(name, "shirudo_task_policy"));
+ EXPECT_FALSE(guard_name_matches(name, "SHIRUDO_TASKS"));
+}
+
+static void unterminated_name_matches_nothing(void)
+{
+ char name[BPF_OBJ_NAME_LEN];
+
+ memset(name, 'x', sizeof(name));
+ EXPECT_FALSE(guard_name_matches(name, "xxxxxxxxxxxxxxx"));
+ EXPECT_FALSE(guard_name_matches(name, "xxxxxxxxxxxxxxxx"));
+ memset(name, 0, sizeof(name));
+ EXPECT_TRUE(guard_name_matches(name, ""));
+ EXPECT_FALSE(guard_name_matches(name, "x"));
+}
+
+static const struct unit_case cases[] = {
+ UNIT_CASE(kernel_name_is_the_start_of_a_long_one),
+ UNIT_CASE(short_name_is_the_whole_one),
+ UNIT_CASE(unterminated_name_matches_nothing),
+ {}
+};
+
+UNIT_SUITE(identity, cases);
diff --git a/util/identity.c b/util/identity.c
index 39cf6a75..4a2a86cc 100644
--- a/util/identity.c
+++ b/util/identity.c
@@ -80,6 +80,15 @@ int enforcement_active(void)
return errno == EINVAL || errno == ENOSYS ? 0 : -errno;
}
+bool guard_name_matches(const char *name, const char *full)
+{
+ size_t len = strnlen(name, BPF_OBJ_NAME_LEN);
+
+ return len < BPF_OBJ_NAME_LEN &&
+ len == strnlen(full, BPF_OBJ_NAME_LEN - 1) &&
+ !memcmp(name, full, len);
+}
+
int guard_map_check(int fd, const struct guard_map *expected, __u32 *id)
{
struct bpf_map_info info = {};
@@ -92,8 +101,7 @@ int guard_map_check(int fd, const struct guard_map *expected, __u32 *id)
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))
+ !guard_name_matches(info.name, expected->name))
return -EPROTO;
if (id)
*id = info.id;
diff --git a/util/identity.h b/util/identity.h
index 6a5bc264..cd6aa35c 100644
--- a/util/identity.h
+++ b/util/identity.h
@@ -9,6 +9,8 @@
#ifndef SHIRUDO_UTIL_IDENTITY_H
#define SHIRUDO_UTIL_IDENTITY_H
+#include <stdbool.h>
+
#include <shirudo/misc.h>
#include <shirudo/tpm.h>
@@ -108,6 +110,13 @@ struct guard_map {
extern const struct guard_map
shirudo_guard_maps[GUARD_MAP_COUNT];
+/*
+ * Whether name, as the kernel reports an object's, is the one it keeps of
+ * an object created as full, which is its first BPF_OBJ_NAME_LEN - 1
+ * bytes.
+ */
+bool guard_name_matches(const char *name, const char *full);
+
/*
* 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
diff --git a/util/image_guards.c b/util/image_guards.c
index 1fa29d19..b04a2f18 100644
--- a/util/image_guards.c
+++ b/util/image_guards.c
@@ -267,8 +267,8 @@ static int guard_program_maps(const struct bpf_prog_info *program,
if (ret)
return ret;
for (j = 0; j < GUARD_MAP_COUNT; j++) {
- if (strncmp(info.name, shirudo_guard_maps[j].name,
- sizeof(info.name) - 1))
+ if (!guard_name_matches(info.name,
+ shirudo_guard_maps[j].name))
continue;
if (info.id != expected[j])
return -EPROTO;
@@ -349,7 +349,7 @@ static int guard_link(const struct image_guard *guard,
if (program.type != BPF_PROG_TYPE_LSM || !program.attach_btf_id ||
!program.attach_btf_obj_id || program.id != link.prog_id ||
program.nr_map_ids > ARRAY_SIZE(map_ids) ||
- strncmp(program.name, guard->prog, sizeof(program.name) - 1) ||
+ !guard_name_matches(program.name, guard->prog) ||
link.tracing.target_obj_id != program.attach_btf_obj_id ||
link.tracing.target_btf_id != program.attach_btf_id)
return -EPROTO;