Linux Test Project
 help / color / mirror / Atom feed
From: Cyril Hrubis <chrubis@suse.cz>
To: Jan Stancek <jstancek@redhat.com>
Cc: ltp@lists.linux.it
Subject: Re: [LTP] [PATCH] syscalls/bpf: zero-initialize bpf_attr including padding bits
Date: Thu, 6 Feb 2025 15:08:25 +0100	[thread overview]
Message-ID: <Z6TCWQIediqqUUVW@yuki.lan> (raw)
In-Reply-To: <89d0fb724ea534419942c2647e1ef7ca98f9e72d.1738846569.git.jstancek@redhat.com>

Hi!
> 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;

I had a closer look here, the map_attr is an union with anonymous
structures and I suppose that the problem here is that the padding after
the union is no longer cleared and that there have been some new fields
added, at least compared to the lapi fallback structures we have and we
possibly pass random mess in flags.

Maybe slightly better version would be:

diff --git a/testcases/kernel/syscalls/bpf/bpf_common.c b/testcases/kernel/syscalls/bpf/bpf_common.c
index 95b5bc12e..a8289e106 100644
--- a/testcases/kernel/syscalls/bpf/bpf_common.c
+++ b/testcases/kernel/syscalls/bpf/bpf_common.c
@@ -49,7 +49,9 @@ int bpf_map_create(union bpf_attr *const attr)

 int bpf_map_array_create(const uint32_t max_entries)
 {
-       union bpf_attr map_attr = {
+       union bpf_attr map_attr = {};
+
+       map_attr = (union bpf_attr) {
                .map_type = BPF_MAP_TYPE_ARRAY,
                .key_size = 4,
                .value_size = 8,


-- 
Cyril Hrubis
chrubis@suse.cz

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

  parent reply	other threads:[~2025-02-06 14:08 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-06 12:56 [LTP] [PATCH] syscalls/bpf: zero-initialize bpf_attr including padding bits Jan Stancek
2025-02-06 13:36 ` Cyril Hrubis
2025-02-06 14:08 ` Cyril Hrubis [this message]
2025-02-06 14:34   ` Jan Stancek
2025-02-06 15:04     ` Cyril Hrubis
2025-02-06 21:23   ` [LTP] [PATCH v2] " Jan Stancek
2025-02-07  8:35     ` Cyril Hrubis
2025-02-07  9:47       ` Jan Stancek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Z6TCWQIediqqUUVW@yuki.lan \
    --to=chrubis@suse.cz \
    --cc=jstancek@redhat.com \
    --cc=ltp@lists.linux.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox