已开启
nulllibbpf: fix offsetof() and container_of() to work with CO-RE #204
hthreec创建于 6月3日
nulllibbpf: fix offsetof() and container_of() to work with CO-RE #204
已开启
hthreec创建于 6月3日
共 2 个文件变更+63-1
@@ -0,0 +1,58 @@
1+From ac4279012921e04d4d9151e302b4077b2f0cd314 Mon Sep 17 00:00:00 2001
2+From: Andrii Nakryiko <andrii@kernel.org>
3+Date: Mon, 8 May 2023 23:55:02 -0700
4+Subject: [PATCH] libbpf: fix offsetof() and container_of() to work with CO-RE
5+MIME-Version: 1.0
6+Content-Type: text/plain; charset=UTF-8
7+Content-Transfer-Encoding: 8bit
8+ 
9+It seems like __builtin_offset() doesn't preserve CO-RE field
10+relocations properly. So if offsetof() macro is defined through
11+__builtin_offset(), CO-RE-enabled BPF code using container_of() will be
12+subtly and silently broken.
13+ 
14+To avoid this problem, redefine offsetof() and container_of() in the
15+form that works with CO-RE relocations more reliably.
16+ 
17+Fixes: 5fbc220862fc ("tools/libpf: Add offsetof/container_of macro in bpf_helpers.h")
18+Reported-by: Lennart Poettering <lennart@poettering.net>
19+Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
20+Acked-by: Yonghong Song <yhs@fb.com>
21+Link: https://lore.kernel.org/r/20230509065502.2306180-1-andrii@kernel.org
22+Signed-off-by: Alexei Starovoitov <ast@kernel.org>
23+Signed-off-by: Daniel Müller <deso@posteo.net>
24+---
25+ src/bpf_helpers.h | 15 ++++++++++-----
26+ 1 file changed, 10 insertions(+), 5 deletions(-)
27+ 
28+diff --git a/src/bpf_helpers.h b/src/bpf_helpers.h
29+index 929a3baca..bbab9ad9d 100644
30+--- a/src/bpf_helpers.h
31++++ b/src/bpf_helpers.h
32+@@ -77,16 +77,21 @@
33+ /*
34+ * Helper macros to manipulate data structures
35+ */
36+-#ifndef offsetof
37+-#define offsetof(TYPE, MEMBER) ((unsigned long)&((TYPE *)0)->MEMBER)
38+-#endif
39+-#ifndef container_of
40++
41++/* offsetof() definition that uses __builtin_offset() might not preserve field
42++ * offset CO-RE relocation properly, so force-redefine offsetof() using
43++ * old-school approach which works with CO-RE correctly
44++ */
45++#undef offsetof
46++#define offsetof(type, member) ((unsigned long)&((type *)0)->member)
47++
48++/* redefined container_of() to ensure we use the above offsetof() macro */
49++#undef container_of
50+ #define container_of(ptr, type, member) \
51+ ({ \
52+ void *__mptr = (void *)(ptr); \
53+ ((type *)(__mptr - offsetof(type, member))); \
54+ })
55+-#endif
56+
57+ /*
58+ * Compiler (optimization) barrier.
@@ -4,7 +4,7 @@
4 4 
5Name: %{githubname}5Name: %{githubname}
6Version: %{githubver}6Version: %{githubver}
7-Release: 127+Release: 13
8Summary: Libbpf library8Summary: Libbpf library
9 9 
10License: LGPLv2 or BSD10License: LGPLv2 or BSD
@@ -35,6 +35,7 @@ Patch0018: backport-libbpf-fix-LDX_STX_ST-CO-RE-relocation-size-adjustment-
35Patch0019: backport-libbpf-Fix-hypothetical-STT_SECTION-extern-NULL-deref-case.patch35Patch0019: backport-libbpf-Fix-hypothetical-STT_SECTION-extern-NULL-deref-case.patch
36Patch0020: backport-libbpf-Adaption-for-sched-type-eBPF-program.patch36Patch0020: backport-libbpf-Adaption-for-sched-type-eBPF-program.patch
37Patch0021: backport-libbpf-Fix-buffer-overflow-in-bpf_object__init_prog.patch37Patch0021: backport-libbpf-Fix-buffer-overflow-in-bpf_object__init_prog.patch
38+Patch0022: backport-libbpf-fix-offsetof-and-container_of-to-work-with-CO-RE.patch
38 39 
39# This package supersedes libbpf from kernel-tools,40# This package supersedes libbpf from kernel-tools,
40# which has default Epoch: 0. By having Epoch: 141# which has default Epoch: 0. By having Epoch: 1
@@ -87,6 +88,9 @@ developing applications that use %{name}
87%{_libdir}/libbpf.a88%{_libdir}/libbpf.a
88 89 
89%changelog90%changelog
91+* Wed Jun 03 2026 zengjia <zengjia1@xfusion.com> 2:1.2.2-13
92+- backport-libbpf-fix-offsetof-and-container_of-to-work-with-CO-RE.patch
93+ 
90* Sat May 30 2026 admirexiao <xiaodianpeng@xfusion.com> 2:1.2.2-1294* Sat May 30 2026 admirexiao <xiaodianpeng@xfusion.com> 2:1.2.2-12
91- libbpf: Fix buffer overflow in bpf_object__init_prog95- libbpf: Fix buffer overflow in bpf_object__init_prog
92 96