Skip to content

eBPF-based security platform

include: add offsetofend() and sizeof_field()

Daniel Borkmann16243de0873b
diff --git a/include/shirudo/misc.h b/include/shirudo/misc.h
index f706f83f..aba44f3d 100644
--- a/include/shirudo/misc.h
+++ b/include/shirudo/misc.h
@@ -2,13 +2,15 @@
/*
* Copyright (c) 2026 Authors of shirudo
*
- * ARRAY_SIZE(), min() and max(), ptr_to_u64() and shirudo_trunc() for
- * userspace, with the kernel's types from <shirudo/types.h>.
+ * ARRAY_SIZE(), offsetofend(), min() and max(), ptr_to_u64() and
+ * shirudo_trunc() for userspace, with the kernel's types from
+ * <shirudo/types.h>.
*/
#ifndef SHIRUDO_MISC_H
#define SHIRUDO_MISC_H
+#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
@@ -21,6 +23,14 @@
typeof(&(a)[0])))
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]) + __must_be_array(a))
+/*
+ * The size of a member, and the offset just past it, without an object;
+ * the member may be a path such as a.b, as offsetof() takes it.
+ */
+#define sizeof_field(type, ...) sizeof(((type *)0)->__VA_ARGS__)
+#define offsetofend(type, ...) \
+ (offsetof(type, __VA_ARGS__) + sizeof_field(type, __VA_ARGS__))
+
/*
* Each argument is evaluated once, and arguments of two types refuse to
* compile rather than compare after a silent conversion: min_t() and
diff --git a/util/upatch_rodata.c b/util/upatch_rodata.c
index 51b031ef..fd03f7b0 100644
--- a/util/upatch_rodata.c
+++ b/util/upatch_rodata.c
@@ -181,8 +181,7 @@ static int find_rodata_data(const struct shirudo_upatch *u,
char name[BPF_OBJ_NAME_LEN + 1];
if (attr_size > sizeof(attr) ||
- attr_size < offsetof(union bpf_attr, map_name) +
- BPF_OBJ_NAME_LEN ||
+ attr_size < offsetofend(union bpf_attr, map_name) ||
attr_off > u->hdr.data_len ||
attr_size > u->hdr.data_len - attr_off)
return -EINVAL;