BPF List
 help / color / mirror / Atom feed
* [PATCH v1 bpf-next] bpf: fix uninitialized values in BPF_{CORE,PROBE}_READ
@ 2025-04-29 13:08 Anton Protopopov
  2025-04-29 14:21 ` Anton Protopopov
  0 siblings, 1 reply; 2+ messages in thread
From: Anton Protopopov @ 2025-04-29 13:08 UTC (permalink / raw)
  To: bpf; +Cc: Anton Protopopov

With the latest LLVM bpf selftests build will fail:

    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;                               \
          |                                          ^

Fix this by declaring __r to be an array of __u8 of a proper size.

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")
Signed-off-by: Anton Protopopov <a.s.protopopov@gmail.com>
---
 tools/lib/bpf/bpf_core_read.h | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/tools/lib/bpf/bpf_core_read.h b/tools/lib/bpf/bpf_core_read.h
index c0e13cdf9660..be556ccdc002 100644
--- a/tools/lib/bpf/bpf_core_read.h
+++ b/tools/lib/bpf/bpf_core_read.h
@@ -517,9 +517,9 @@ 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;				    \
-	BPF_CORE_READ_INTO(&__r, (src), a, ##__VA_ARGS__);		    \
-	__r;								    \
+	__u8 __r[sizeof(___type((src), a, ##__VA_ARGS__))];		    \
+	BPF_CORE_READ_INTO(__r, (src), a, ##__VA_ARGS__);		    \
+	*(___type((src), a, ##__VA_ARGS__) *)__r;			    \
 })
 
 /*
@@ -533,16 +533,16 @@ 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;				    \
-	BPF_CORE_READ_USER_INTO(&__r, (src), a, ##__VA_ARGS__);		    \
-	__r;								    \
+	__u8 __r[sizeof(___type((src), a, ##__VA_ARGS__))];		    \
+	BPF_CORE_READ_USER_INTO(__r, (src), a, ##__VA_ARGS__);		    \
+	*(___type((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;				    \
-	BPF_PROBE_READ_INTO(&__r, (src), a, ##__VA_ARGS__);		    \
-	__r;								    \
+	__u8 __r[sizeof(___type((src), a, ##__VA_ARGS__))];		    \
+	BPF_PROBE_READ_INTO(__r, (src), a, ##__VA_ARGS__);		    \
+	*(___type((src), a, ##__VA_ARGS__) *)__r;			    \
 })
 
 /*
@@ -552,9 +552,9 @@ 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;				    \
-	BPF_PROBE_READ_USER_INTO(&__r, (src), a, ##__VA_ARGS__);	    \
-	__r;								    \
+	__u8 __r[sizeof(___type((src), a, ##__VA_ARGS__))];		    \
+	BPF_PROBE_READ_USER_INTO(__r, (src), a, ##__VA_ARGS__);		    \
+	*(___type((src), a, ##__VA_ARGS__) *)__r;			    \
 })
 
 #endif
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v1 bpf-next] bpf: fix uninitialized values in BPF_{CORE,PROBE}_READ
  2025-04-29 13:08 [PATCH v1 bpf-next] bpf: fix uninitialized values in BPF_{CORE,PROBE}_READ Anton Protopopov
@ 2025-04-29 14:21 ` Anton Protopopov
  0 siblings, 0 replies; 2+ messages in thread
From: Anton Protopopov @ 2025-04-29 14:21 UTC (permalink / raw)
  To: bpf

On 25/04/29 01:08PM, Anton Protopopov wrote:
> With the latest LLVM bpf selftests build will fail:
> 
>     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;                               \
>           |                                          ^
> 
> Fix this by declaring __r to be an array of __u8 of a proper size.
> 
> 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")
> Signed-off-by: Anton Protopopov <a.s.protopopov@gmail.com>
> ---
>  tools/lib/bpf/bpf_core_read.h | 24 ++++++++++++------------
>  1 file changed, 12 insertions(+), 12 deletions(-)
> 
> diff --git a/tools/lib/bpf/bpf_core_read.h b/tools/lib/bpf/bpf_core_read.h
> index c0e13cdf9660..be556ccdc002 100644
> --- a/tools/lib/bpf/bpf_core_read.h
> +++ b/tools/lib/bpf/bpf_core_read.h
> @@ -517,9 +517,9 @@ 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;				    \
> -	BPF_CORE_READ_INTO(&__r, (src), a, ##__VA_ARGS__);		    \
> -	__r;								    \
> +	__u8 __r[sizeof(___type((src), a, ##__VA_ARGS__))];		    \
> +	BPF_CORE_READ_INTO(__r, (src), a, ##__VA_ARGS__);		    \

And, of course, this breaks things. The line above should have stayed
with &. I will send v2.

> +	*(___type((src), a, ##__VA_ARGS__) *)__r;			    \
>  })
>  
>  /*
> @@ -533,16 +533,16 @@ 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;				    \
> -	BPF_CORE_READ_USER_INTO(&__r, (src), a, ##__VA_ARGS__);		    \
> -	__r;								    \
> +	__u8 __r[sizeof(___type((src), a, ##__VA_ARGS__))];		    \
> +	BPF_CORE_READ_USER_INTO(__r, (src), a, ##__VA_ARGS__);		    \
> +	*(___type((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;				    \
> -	BPF_PROBE_READ_INTO(&__r, (src), a, ##__VA_ARGS__);		    \
> -	__r;								    \
> +	__u8 __r[sizeof(___type((src), a, ##__VA_ARGS__))];		    \
> +	BPF_PROBE_READ_INTO(__r, (src), a, ##__VA_ARGS__);		    \
> +	*(___type((src), a, ##__VA_ARGS__) *)__r;			    \
>  })
>  
>  /*
> @@ -552,9 +552,9 @@ 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;				    \
> -	BPF_PROBE_READ_USER_INTO(&__r, (src), a, ##__VA_ARGS__);	    \
> -	__r;								    \
> +	__u8 __r[sizeof(___type((src), a, ##__VA_ARGS__))];		    \
> +	BPF_PROBE_READ_USER_INTO(__r, (src), a, ##__VA_ARGS__);		    \
> +	*(___type((src), a, ##__VA_ARGS__) *)__r;			    \
>  })
>  
>  #endif
> -- 
> 2.34.1
> 

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-04-29 14:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-29 13:08 [PATCH v1 bpf-next] bpf: fix uninitialized values in BPF_{CORE,PROBE}_READ Anton Protopopov
2025-04-29 14:21 ` Anton Protopopov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox