Skip to contentSkip to conversation

bpf: fix uninitialized values in BPF_{CORE,PROBE}_READ

PATCH Superseded Anton Protopopov 2 participants 2 messages Apr 30, 2025 – May 1, 2025

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 bpf: fix uninitialized values in BPF_{CORE,PROBE}_READ

With the latest LLVM bpf selftests build will fail with the following error message:

    progs/profiler.inc.h:710:31: error: default initialization of an object of type 'typeof ((parent_task)->real_cred->uid.val)' (aka 'const unsigned int') leaves the object uninitialized and is incompatible with C++ [-Werror,-Wdefault-const-init-unsafe]
      710 |         proc_exec_data->parent_uid = BPF_CORE_READ(parent_task, real_cred, uid.val);
          |                                      ^
    tools/testing/selftests/bpf/tools/include/bpf/bpf_core_read.h:520:35: note: expanded from macro 'BPF_CORE_READ'
      520 |         ___type((src), a, ##__VA_ARGS__) __r;                               \
          |                                          ^

This happens because BPF_CORE_READ (and other macro) declare the variable __r using the ___type macro which can inherit const modifier from intermediate types.

Fix this by using __typeof_unqual__, when supported. (And when it is not supported, the problem shouldn't appear, as older compilers haven't complained.)

Fixes 792001f4f7aa ("libbpf: Add user-space variants of BPF_CORE_READ() family of macros")
Fixes a4b09a9ef945 ("libbpf: Add non-CO-RE variants of BPF_CORE_READ() macro family")
---
 tools/lib/bpf/bpf_core_read.h | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
tools/lib/bpf/bpf_core_read.h +12 -4
index c0e13cdf9660..a371213b7f3e 100644
@@ -390,6 +390,14 @@ extern void *bpf_rdonly_cast(const void *obj, __u32 btf_id) __ksym __weak;
 
 #define ___type(...) typeof(___arrow(__VA_ARGS__))
 
+#if defined(__clang__) && (__clang_major__ >= 19)
+#define ___type_unqual(...) __typeof_unqual__(___arrow(__VA_ARGS__))
+#elif defined(__GNUC__) && (__GNUC__ >= 14)
+#define ___type_unqual(...) __typeof_unqual__(___arrow(__VA_ARGS__))
+#else
+#define ___type_unqual(...) ___type(__VA_ARGS__)
+#endif
+
 #define ___read(read_fn, dst, src_type, src, accessor)			    \
 	read_fn((void *)(dst), sizeof(*(dst)), &((src_type)(src))->accessor)
 
@@ -517,7 +525,7 @@ extern void *bpf_rdonly_cast(const void *obj, __u32 btf_id) __ksym __weak;
  * than enough for any practical purpose.
  */
 #define BPF_CORE_READ(src, a, ...) ({					    \
-	___type((src), a, ##__VA_ARGS__) __r;				    \
+	___type_unqual((src), a, ##__VA_ARGS__) __r;			    \
 	BPF_CORE_READ_INTO(&__r, (src), a, ##__VA_ARGS__);		    \
 	__r;								    \
 })
@@ -533,14 +541,14 @@ extern void *bpf_rdonly_cast(const void *obj, __u32 btf_id) __ksym __weak;
  * input argument.
  */
 #define BPF_CORE_READ_USER(src, a, ...) ({				    \
-	___type((src), a, ##__VA_ARGS__) __r;				    \
+	___type_unqual((src), a, ##__VA_ARGS__) __r;			    \
 	BPF_CORE_READ_USER_INTO(&__r, (src), a, ##__VA_ARGS__);		    \
 	__r;								    \
 })
 
 /* Non-CO-RE variant of BPF_CORE_READ() */
 #define BPF_PROBE_READ(src, a, ...) ({					    \
-	___type((src), a, ##__VA_ARGS__) __r;				    \
+	___type_unqual((src), a, ##__VA_ARGS__) __r;			    \
 	BPF_PROBE_READ_INTO(&__r, (src), a, ##__VA_ARGS__);		    \
 	__r;								    \
 })
@@ -552,7 +560,7 @@ extern void *bpf_rdonly_cast(const void *obj, __u32 btf_id) __ksym __weak;
  * not restricted to kernel types only.
  */
 #define BPF_PROBE_READ_USER(src, a, ...) ({				    \
-	___type((src), a, ##__VA_ARGS__) __r;				    \
+	___type_unqual((src), a, ##__VA_ARGS__) __r;			    \
 	BPF_PROBE_READ_USER_INTO(&__r, (src), a, ##__VA_ARGS__);	    \
 	__r;								    \
 })
Signature · 1 line
2.34.1
#1 lore raw
Quoted 31 lines · Anton Protopopov

With the latest LLVM bpf selftests build will fail with the following error message:

    progs/profiler.inc.h:710:31: error: default initialization of an object of type 'typeof ((parent_task)->real_cred->uid.val)' (aka 'const unsigned int') leaves the object uninitialized and is incompatible with C++ [-Werror,-Wdefault-const-init-unsafe]
      710 |         proc_exec_data->parent_uid = BPF_CORE_READ(parent_task, real_cred, uid.val);
          |                                      ^
    tools/testing/selftests/bpf/tools/include/bpf/bpf_core_read.h:520:35: note: expanded from macro 'BPF_CORE_READ'
      520 |         ___type((src), a, ##__VA_ARGS__) __r;                               \
          |                                          ^

This happens because BPF_CORE_READ (and other macro) declare the variable __r using the ___type macro which can inherit const modifier from intermediate types.

Fix this by using __typeof_unqual__, when supported. (And when it is not supported, the problem shouldn't appear, as older compilers haven't complained.)

Fixes 792001f4f7aa ("libbpf: Add user-space variants of BPF_CORE_READ() family of macros")
Fixes a4b09a9ef945 ("libbpf: Add non-CO-RE variants of BPF_CORE_READ() macro family")
---
 tools/lib/bpf/bpf_core_read.h | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
tools/lib/bpf/bpf_core_read.h +0 -0
index c0e13cdf9660..a371213b7f3e 100644

tools/lib/bpf/bpf_core_read.h extern void *bpf_rdonly_cast(const void *obj, __u32 btf_id) __ksym __weak; · line 399

Quoted 10 lines
tools/lib/bpf/bpf_core_read.h +7 -0
@@ -390,6 +390,14 @@ extern void *bpf_rdonly_cast(const void *obj, __u32 btf_id) __ksym __weak;
 
 #define ___type(...) typeof(___arrow(__VA_ARGS__))
 
+#if defined(__clang__) && (__clang_major__ >= 19)
+#define ___type_unqual(...) __typeof_unqual__(___arrow(__VA_ARGS__))
+#elif defined(__GNUC__) && (__GNUC__ >= 14)
+#define ___type_unqual(...) __typeof_unqual__(___arrow(__VA_ARGS__))
+#else
+#define ___type_unqual(...) ___type(__VA_ARGS__)
+#endif

instead of defining both ___type_unqual and ___type, let's just define ___type as either typeof or typeof_unqual and use it uniformly? I don't think we ever care about const/volatile/restrict stuff in ___type usage

pw-bot: cr

Quoted 43 lines
tools/lib/bpf/bpf_core_read.h +5 -5
+
 #define ___read(read_fn, dst, src_type, src, accessor)                     \
        read_fn((void *)(dst), sizeof(*(dst)), &((src_type)(src))->accessor)
 
@@ -517,7 +525,7 @@ extern void *bpf_rdonly_cast(const void *obj, __u32 btf_id) __ksym __weak;
  * than enough for any practical purpose.
  */
 #define BPF_CORE_READ(src, a, ...) ({                                      \
-       ___type((src), a, ##__VA_ARGS__) __r;                               \
+       ___type_unqual((src), a, ##__VA_ARGS__) __r;                        \
        BPF_CORE_READ_INTO(&__r, (src), a, ##__VA_ARGS__);                  \
        __r;                                                                \
 })
@@ -533,14 +541,14 @@ extern void *bpf_rdonly_cast(const void *obj, __u32 btf_id) __ksym __weak;
  * input argument.
  */
 #define BPF_CORE_READ_USER(src, a, ...) ({                                 \
-       ___type((src), a, ##__VA_ARGS__) __r;                               \
+       ___type_unqual((src), a, ##__VA_ARGS__) __r;                        \
        BPF_CORE_READ_USER_INTO(&__r, (src), a, ##__VA_ARGS__);             \
        __r;                                                                \
 })
 
 /* Non-CO-RE variant of BPF_CORE_READ() */
 #define BPF_PROBE_READ(src, a, ...) ({                                     \
-       ___type((src), a, ##__VA_ARGS__) __r;                               \
+       ___type_unqual((src), a, ##__VA_ARGS__) __r;                        \
        BPF_PROBE_READ_INTO(&__r, (src), a, ##__VA_ARGS__);                 \
        __r;                                                                \
 })
@@ -552,7 +560,7 @@ extern void *bpf_rdonly_cast(const void *obj, __u32 btf_id) __ksym __weak;
  * not restricted to kernel types only.
  */
 #define BPF_PROBE_READ_USER(src, a, ...) ({                                \
-       ___type((src), a, ##__VA_ARGS__) __r;                               \
+       ___type_unqual((src), a, ##__VA_ARGS__) __r;                        \
        BPF_PROBE_READ_USER_INTO(&__r, (src), a, ##__VA_ARGS__);            \
        __r;                                                                \
 })
--

2.34.1