[LTP] [PATCH] syscalls/bpf: zero-initialize bpf_attr including padding bits

Cyril Hrubis chrubis@suse.cz
Thu Feb 6 14:36:21 CET 2025


Hi!
> gcc 15 stopped zero-initializing padding bits:
>   https://gcc.gnu.org/gcc-15/changes.html

I would even call this a bug even if it's allowed by the C standard.

> However kernel bpf syscall checks that all unused fields for a command
> are set to zero in CHECK_ATTR() macro, which causes tests to fail with
> EINVAL.
> 
> Signed-off-by: Jan Stancek <jstancek@redhat.com>
> ---
>  testcases/kernel/syscalls/bpf/bpf_common.c | 32 ++++++++++++----------
>  1 file changed, 18 insertions(+), 14 deletions(-)
> 
> diff --git a/testcases/kernel/syscalls/bpf/bpf_common.c b/testcases/kernel/syscalls/bpf/bpf_common.c
> index 95b5bc12eaa4..d765c4e32936 100644
> --- a/testcases/kernel/syscalls/bpf/bpf_common.c
> +++ b/testcases/kernel/syscalls/bpf/bpf_common.c
> @@ -49,13 +49,14 @@ int bpf_map_create(union bpf_attr *const attr)
>  
>  int bpf_map_array_create(const uint32_t max_entries)
>  {
> -	union bpf_attr map_attr = {
> -		.map_type = BPF_MAP_TYPE_ARRAY,
> -		.key_size = 4,
> -		.value_size = 8,
> -		.max_entries = max_entries,
> -		.map_flags = 0
> -	};
> +	/* zero-initialize entire struct including padding bits */
> +	union bpf_attr map_attr = {};
> +
> +	map_attr.map_type = BPF_MAP_TYPE_ARRAY;
> +	map_attr.key_size = 4;
> +	map_attr.value_size = 8;
> +	map_attr.max_entries = max_entries;
> +	map_attr.map_flags = 0;

So struct foo bar = {.bar = foo}; does not work,
but struct foo bar = {}: bar.bar = foo; does? That is insane...

>  	return bpf_map_create(&map_attr);
>  }
> @@ -64,13 +65,16 @@ void bpf_map_array_get(const int map_fd,
>  		       const uint32_t *const array_indx,
>  		       uint64_t *const array_val)
>  {
> -	union bpf_attr elem_attr = {
> -		.map_fd = map_fd,
> -		.key = ptr_to_u64(array_indx),
> -		.value = ptr_to_u64(array_val),
> -		.flags = 0
> -	};
> -	const int ret = bpf(BPF_MAP_LOOKUP_ELEM, &elem_attr, sizeof(elem_attr));
> +	/* zero-initialize entire struct including padding bits */
> +	union bpf_attr elem_attr = {};
> +	int ret;
> +
> +	elem_attr.map_fd = map_fd;
> +	elem_attr.key = ptr_to_u64(array_indx);
> +	elem_attr.value = ptr_to_u64(array_val);
> +	elem_attr.flags = 0;
> +
> +	ret = bpf(BPF_MAP_LOOKUP_ELEM, &elem_attr, sizeof(elem_attr));
>  
>  	if (ret) {
>  		tst_brk(TBROK | TTERRNO,
> -- 
> 2.43.0
> 
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp

-- 
Cyril Hrubis
chrubis@suse.cz


More information about the ltp mailing list