BPF List
 help / color / mirror / Atom feed
From: Yonghong Song <yonghong.song@linux.dev>
To: Eric Biggers <ebiggers@kernel.org>,
	bpf@vger.kernel.org, Andrii Nakryiko <andrii@kernel.org>,
	Eduard Zingerman <eddyz87@gmail.com>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Martin KaFai Lau <martin.lau@linux.dev>
Cc: Song Liu <song@kernel.org>,
	John Fastabend <john.fastabend@gmail.com>,
	KP Singh <kpsingh@kernel.org>,
	Stanislav Fomichev <sdf@fomichev.me>, Hao Luo <haoluo@google.com>,
	Jiri Olsa <jolsa@kernel.org>,
	Tony Ambardar <tony.ambardar@gmail.com>
Subject: Re: [PATCH bpf] libbpf: Fix undefined behavior in {get,put}_unaligned_be32()
Date: Mon, 6 Oct 2025 09:12:49 -0700	[thread overview]
Message-ID: <fc03414f-7b9d-4222-a66f-67be4ea32fb7@linux.dev> (raw)
In-Reply-To: <20251006012037.159295-1-ebiggers@kernel.org>



On 10/5/25 6:20 PM, Eric Biggers wrote:
> These violate aliasing rules and may be miscompiled unless
> -fno-strict-aliasing is used.  Replace them with the standard memcpy()
> solution.  Note that compilers know how to optimize this properly.

For clang, -fstrict-aliasing is on by default. For gcc, -fstrict-aliasing
is on by default for >= O2. So indeed, with macro based implementation below,
it is *possible* that the compiler may explore strict-aliasing rule and may
generate incorrect code.

>
> Fixes: 4a1c9e544b8d ("libbpf: remove linux/unaligned.h dependency for libbpf_sha256()")
> Signed-off-by: Eric Biggers <ebiggers@kernel.org>

Ack with a nit below.

Acked-by: Yonghong Song <yonghong.song@linux.dev>

> ---
>   tools/lib/bpf/libbpf_utils.c | 24 ++++++++++++++----------
>   1 file changed, 14 insertions(+), 10 deletions(-)
>
> diff --git a/tools/lib/bpf/libbpf_utils.c b/tools/lib/bpf/libbpf_utils.c
> index 5d66bc6ff0982..ac3beae54cf67 100644
> --- a/tools/lib/bpf/libbpf_utils.c
> +++ b/tools/lib/bpf/libbpf_utils.c
> @@ -146,20 +146,24 @@ const char *libbpf_errstr(int err)
>   		snprintf(buf, sizeof(buf), "%d", err);
>   		return buf;
>   	}
>   }
>   
> -#pragma GCC diagnostic push
> -#pragma GCC diagnostic ignored "-Wpacked"
> -#pragma GCC diagnostic ignored "-Wattributes"
> -struct __packed_u32 { __u32 __val; } __attribute__((packed));
> -#pragma GCC diagnostic pop
> -
> -#define get_unaligned_be32(p) be32_to_cpu((((struct __packed_u32 *)(p))->__val))
> -#define put_unaligned_be32(v, p) do {							\
> -	((struct __packed_u32 *)(p))->__val = cpu_to_be32(v);				\
> -} while (0)
> +static inline __u32 get_unaligned_be32(const void *p)
> +{
> +	__be32 val;

To be consistent with put_unaligned_be32(), rename the above 'val'
to 'be_val'?

> +
> +	memcpy(&val, p, sizeof(val));
> +	return be32_to_cpu(val);
> +}
> +
> +static inline void put_unaligned_be32(__u32 val, void *p)
> +{
> +	__be32 be_val = cpu_to_be32(val);
> +
> +	memcpy(p, &be_val, sizeof(be_val));
> +}
>   
>   #define SHA256_BLOCK_LENGTH 64
>   #define Ch(x, y, z) (((x) & (y)) ^ (~(x) & (z)))
>   #define Maj(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
>   #define Sigma_0(x) (ror32((x), 2) ^ ror32((x), 13) ^ ror32((x), 22))
>
> base-commit: de7342228b7343774d6a9981c2ddbfb5e201044b


  reply	other threads:[~2025-10-06 16:12 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-06  1:20 [PATCH bpf] libbpf: Fix undefined behavior in {get,put}_unaligned_be32() Eric Biggers
2025-10-06 16:12 ` Yonghong Song [this message]
2025-10-06 16:18   ` Andrii Nakryiko

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=fc03414f-7b9d-4222-a66f-67be4ea32fb7@linux.dev \
    --to=yonghong.song@linux.dev \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=ebiggers@kernel.org \
    --cc=eddyz87@gmail.com \
    --cc=haoluo@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=sdf@fomichev.me \
    --cc=song@kernel.org \
    --cc=tony.ambardar@gmail.com \
    /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