Skip to contentSkip to conversation

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

PATCH In review Eric Biggers 3 participants 3 messages Oct 6, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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()")
---
 tools/lib/bpf/libbpf_utils.c | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)
tools/lib/bpf/libbpf_utils.c +14 -10
index 5d66bc6ff0982..ac3beae54cf67 100644
@@ -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;
+
+	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

Signature · 1 line
2.51.0
reviewer Acked-by on v1 1/1
#1 lore raw
Quoted 3 lines · Eric Biggers

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.

Quoted 3 lines
Fixes 4a1c9e544b8d ("libbpf: remove linux/unaligned.h dependency for libbpf_sha256()")

Ack with a nit below.

Quoted 17 lines
---
  tools/lib/bpf/libbpf_utils.c | 24 ++++++++++++++----------
  1 file changed, 14 insertions(+), 10 deletions(-)
tools/lib/bpf/libbpf_utils.c +0 -3
index 5d66bc6ff0982..ac3beae54cf67 100644
@@ -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"

tools/lib/bpf/libbpf_utils.c const char *libbpf_errstr(int err) · line 153

Quoted 10 lines
tools/lib/bpf/libbpf_utils.c +3 -7
-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'?

Quoted 18 lines
tools/lib/bpf/libbpf_utils.c +11 -0
+
+	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

#2 lore raw
Quoted 14 lines · Yonghong Song
Quoted 3 lines · Eric Biggers

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.

Quoted 1 line
Quoted 7 lines
Quoted 2 lines
Fixes 4a1c9e544b8d ("libbpf: remove linux/unaligned.h dependency for libbpf_sha256()")

Ack with a nit below.

we raced a bit, I just applied this patch a few minutes before your ack. I added your ack and force-pushed

Quoted 14 lines
Quoted 14 lines
---
  tools/lib/bpf/libbpf_utils.c | 24 ++++++++++++++----------
  1 file changed, 14 insertions(+), 10 deletions(-)
tools/lib/bpf/libbpf_utils.c +0 -0
index 5d66bc6ff0982..ac3beae54cf67 100644
@@ -146,20 +146,24 @@ const char *libbpf_errstr(int err)
              snprintf(buf, sizeof(buf), "%d", err);
              return buf;
      }
  }
 
Quoted 16 lines

tools/lib/bpf/libbpf_utils.c const char *libbpf_errstr(int err) · line 153

Quoted 13 lines
tools/lib/bpf/libbpf_utils.c +3 -10
-#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'?

I left this as is, it's fine, IMO.

Quoted 21 lines
Quoted 19 lines
tools/lib/bpf/libbpf_utils.c +11 -0
+
+     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