Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

libbpf: Fix undefined behavior in {get,put}_unaligned_be32()

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.

Fixes: 4a1c9e544b8d ("libbpf: remove linux/unaligned.h dependency for libbpf_sha256()")
Signed-off-by: Eric Biggers <ebiggers@kernel.org>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Yonghong Song <yonghong.song@linux.dev>
Link: https://lore.kernel.org/bpf/20251006012037.159295-1-ebiggers@kernel.org

authored by

Eric Biggers and committed by
Andrii Nakryiko
4d920ed6 de734222

+13 -9
+13 -9
tools/lib/bpf/libbpf_utils.c
··· 148 148 } 149 149 } 150 150 151 - #pragma GCC diagnostic push 152 - #pragma GCC diagnostic ignored "-Wpacked" 153 - #pragma GCC diagnostic ignored "-Wattributes" 154 - struct __packed_u32 { __u32 __val; } __attribute__((packed)); 155 - #pragma GCC diagnostic pop 151 + static inline __u32 get_unaligned_be32(const void *p) 152 + { 153 + __be32 val; 156 154 157 - #define get_unaligned_be32(p) be32_to_cpu((((struct __packed_u32 *)(p))->__val)) 158 - #define put_unaligned_be32(v, p) do { \ 159 - ((struct __packed_u32 *)(p))->__val = cpu_to_be32(v); \ 160 - } while (0) 155 + memcpy(&val, p, sizeof(val)); 156 + return be32_to_cpu(val); 157 + } 158 + 159 + static inline void put_unaligned_be32(__u32 val, void *p) 160 + { 161 + __be32 be_val = cpu_to_be32(val); 162 + 163 + memcpy(p, &be_val, sizeof(be_val)); 164 + } 161 165 162 166 #define SHA256_BLOCK_LENGTH 64 163 167 #define Ch(x, y, z) (((x) & (y)) ^ (~(x) & (z)))