Skip to contentSkip to conversation

libbpf: fix libbp_sha256() for Github compatibility

PATCH Superseded Andrii Nakryiko 3 participants 9 messages Sep 30, 2025 – Oct 1, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Andrii Nakryiko

author maintainer
#0 lore raw

cover libbpf: fix libbp_sha256() for Github compatibility

Recent reimplementation of libbpf_sha256() introduced issues for libbpf's Github mirror due to reliance on linux/unaligned.h header. This patch set fixes those issues to make libbpf source code compatible with Github mirror setup.

This patch set starts with a bit of organization: we introduce libbpf_utils.c as a place for generic internal helpers like libbpf_errstr() and libbpf_sha256(), and move a few existing helpers there. We also clean up libbpf_strerror_r(), which seems to be a leftover of some previous refactorings.

And finally, we move libbpf_sha256() from huge libbpf.c into libbpf_utils.c, following up with fix ups to make its code more Github-friendly.

Andrii Nakryiko (5):
libbpf: make libbpf_errno.c into more generic libbpf_utils.c
libbpf: remove unused libbpf_strerror_r and STRERR_BUFSIZE
libbpf: move libbpf_errstr() into libbpf_utils.c
libbpf: move libbpf_sha256() implementation into libbpf_utils.c
libbpf: remove linux/unaligned.h dependency for libbpf_sha256()

 tools/lib/bpf/Build             |   2 +-
 tools/lib/bpf/btf.c             |   1 -
 tools/lib/bpf/btf_dump.c        |   1 -
 tools/lib/bpf/elf.c             |   1 -
 tools/lib/bpf/features.c        |   1 -
 tools/lib/bpf/gen_loader.c      |   3 +-
 tools/lib/bpf/libbpf.c          | 101 -------------
 tools/lib/bpf/libbpf_errno.c    |  75 ----------
 tools/lib/bpf/libbpf_internal.h |  15 ++
 tools/lib/bpf/libbpf_utils.c    | 249 ++++++++++++++++++++++++++++++++
 tools/lib/bpf/linker.c          |   1 -
 tools/lib/bpf/relo_core.c       |   1 -
 tools/lib/bpf/ringbuf.c         |   1 -
 tools/lib/bpf/str_error.c       | 104 -------------
 tools/lib/bpf/str_error.h       |  19 ---
 tools/lib/bpf/usdt.c            |   1 -
 16 files changed, 266 insertions(+), 310 deletions(-)
 delete mode 100644 tools/lib/bpf/libbpf_errno.c
 create mode 100644 tools/lib/bpf/libbpf_utils.c
 delete mode 100644 tools/lib/bpf/str_error.c
 delete mode 100644 tools/lib/bpf/str_error.h
Signature · 1 line
2.47.3
author maintainer
#1 lore raw

PATCH 1/5 libbpf: make libbpf_errno.c into more generic libbpf_utils.c

Libbpf is missing one convenient place to put common "utils"-like code that is generic and usable from multiple places. Use libbpf_errno.c as the base for more generic libbpf_utils.c.

---
 tools/lib/bpf/Build                              | 2 +-
 tools/lib/bpf/{libbpf_errno.c => libbpf_utils.c} | 0
 2 files changed, 1 insertion(+), 1 deletion(-)
 rename tools/lib/bpf/{libbpf_errno.c => libbpf_utils.c} (100%)
tools/lib/bpf/Build +1 -1
index e2cd558ca0b4..c30927135fd6 100644
@@ -1,4 +1,4 @@
-libbpf-y := libbpf.o bpf.o nlattr.o btf.o libbpf_errno.o str_error.o \
+libbpf-y := libbpf.o bpf.o nlattr.o btf.o libbpf_utils.o str_error.o \
 	    netlink.o bpf_prog_linfo.o libbpf_probes.o hashmap.o \
 	    btf_dump.o ringbuf.o strset.o linker.o gen_loader.o relo_core.o \
 	    usdt.o zip.o elf.o features.o btf_iter.o btf_relocate.o
tools/lib/bpf/libbpf_utils.c renamed from tools/lib/bpf/libbpf_errno.c +0 -0
similarity index 100%
rename from tools/lib/bpf/libbpf_errno.c
rename to tools/lib/bpf/libbpf_utils.c
Signature · 1 line
2.47.3
author maintainer
#2 lore raw

PATCH 2/5 libbpf: remove unused libbpf_strerror_r and STRERR_BUFSIZE

In reply to Andrii Nakryiko

libbpf_strerror_r() is not exposed as public API and neither is it used inside libbpf itself. Remove it altogether.

Same for STRERR_BUFSIZE, it's just an orphaned leftover constant which we missed to clean up some time earlier.

---
 tools/lib/bpf/libbpf.c    |  2 --
 tools/lib/bpf/str_error.c | 24 ------------------------
 tools/lib/bpf/str_error.h |  4 ----
 3 files changed, 30 deletions(-)
tools/lib/bpf/libbpf.c +0 -2
index f92083f51bdb..c21bc61f5ff4 100644
@@ -319,8 +319,6 @@ static void pr_perm_msg(int err)
 		buf);
 }
 
-#define STRERR_BUFSIZE  128
-
 /* Copied from tools/perf/util/util.h */
 #ifndef zfree
 # define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
tools/lib/bpf/str_error.c +0 -24
index 9a541762f54c..92dbd801102f 100644
@@ -12,30 +12,6 @@
 /* make sure libbpf doesn't use kernel-only integer typedefs */
 #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
 
-/*
- * Wrapper to allow for building in non-GNU systems such as Alpine Linux's musl
- * libc, while checking strerror_r() return to avoid having to check this in
- * all places calling it.
- */
-char *libbpf_strerror_r(int err, char *dst, int len)
-{
-	int ret = strerror_r(err < 0 ? -err : err, dst, len);
-	/* on glibc <2.13, ret == -1 and errno is set, if strerror_r() can't
-	 * handle the error, on glibc >=2.13 *positive* (errno-like) error
-	 * code is returned directly
-	 */
-	if (ret == -1)
-		ret = errno;
-	if (ret) {
-		if (ret == EINVAL)
-			/* strerror_r() doesn't recognize this specific error */
-			snprintf(dst, len, "unknown error (%d)", err < 0 ? err : -err);
-		else
-			snprintf(dst, len, "ERROR: strerror_r(%d)=%d", err, ret);
-	}
-	return dst;
-}
-
 const char *libbpf_errstr(int err)
 {
 	static __thread char buf[12];
tools/lib/bpf/str_error.h +0 -4
index 53e7fbffc13e..d4c82eec034d 100644
@@ -2,10 +2,6 @@
 #ifndef __LIBBPF_STR_ERROR_H
 #define __LIBBPF_STR_ERROR_H
 
-#define STRERR_BUFSIZE  128
-
-char *libbpf_strerror_r(int err, char *dst, int len);
-
 /**
  * @brief **libbpf_errstr()** returns string corresponding to numeric errno
  * @param err negative numeric errno
Signature · 1 line
2.47.3
author maintainer
#3 lore raw

PATCH 3/5 libbpf: move libbpf_errstr() into libbpf_utils.c

In reply to Andrii Nakryiko

Get rid of str_err.{c,h} by moving implementation of libbpf_errstr() into libbpf_utils.c and declarations into libbpf_internal.h.

---
 tools/lib/bpf/Build             |  2 +-
 tools/lib/bpf/btf.c             |  1 -
 tools/lib/bpf/btf_dump.c        |  1 -
 tools/lib/bpf/elf.c             |  1 -
 tools/lib/bpf/features.c        |  1 -
 tools/lib/bpf/gen_loader.c      |  3 +-
 tools/lib/bpf/libbpf.c          |  1 -
 tools/lib/bpf/libbpf_internal.h | 10 +++++
 tools/lib/bpf/libbpf_utils.c    | 72 +++++++++++++++++++++++++++++
 tools/lib/bpf/linker.c          |  1 -
 tools/lib/bpf/relo_core.c       |  1 -
 tools/lib/bpf/ringbuf.c         |  1 -
 tools/lib/bpf/str_error.c       | 80 ---------------------------------
 tools/lib/bpf/str_error.h       | 15 -------
 tools/lib/bpf/usdt.c            |  1 -
 15 files changed, 84 insertions(+), 107 deletions(-)
 delete mode 100644 tools/lib/bpf/str_error.c
 delete mode 100644 tools/lib/bpf/str_error.h
tools/lib/bpf/Build +1 -1
index c30927135fd6..c80204bb72a2 100644
@@ -1,4 +1,4 @@
-libbpf-y := libbpf.o bpf.o nlattr.o btf.o libbpf_utils.o str_error.o \
+libbpf-y := libbpf.o bpf.o nlattr.o btf.o libbpf_utils.o \
 	    netlink.o bpf_prog_linfo.o libbpf_probes.o hashmap.o \
 	    btf_dump.o ringbuf.o strset.o linker.o gen_loader.o relo_core.o \
 	    usdt.o zip.o elf.o features.o btf_iter.o btf_relocate.o
tools/lib/bpf/btf.c +0 -1
index 37682908cb0f..18907f0fcf9f 100644
@@ -23,7 +23,6 @@
 #include "libbpf_internal.h"
 #include "hashmap.h"
 #include "strset.h"
-#include "str_error.h"
 
 #define BTF_MAX_NR_TYPES 0x7fffffffU
 #define BTF_MAX_STR_OFFSET 0x7fffffffU
tools/lib/bpf/btf_dump.c +0 -1
index f09f25eccf3c..6388392f49a0 100644
@@ -21,7 +21,6 @@
 #include "hashmap.h"
 #include "libbpf.h"
 #include "libbpf_internal.h"
-#include "str_error.h"
 
 static const char PREFIXES[] = "\t\t\t\t\t\t\t\t\t\t\t\t\t";
 static const size_t PREFIX_CNT = sizeof(PREFIXES) - 1;
tools/lib/bpf/elf.c +0 -1
index 823f83ad819c..295dbda24580 100644
@@ -9,7 +9,6 @@
 #include <linux/kernel.h>
 
 #include "libbpf_internal.h"
-#include "str_error.h"
 
 /* A SHT_GNU_versym section holds 16-bit words. This bit is set if
  * the symbol is hidden and can only be seen when referenced using an
tools/lib/bpf/features.c +0 -1
index 760657f5224c..b842b83e2480 100644
@@ -6,7 +6,6 @@
 #include "libbpf.h"
 #include "libbpf_common.h"
 #include "libbpf_internal.h"
-#include "str_error.h"
 
 static inline __u64 ptr_to_u64(const void *ptr)
 {
tools/lib/bpf/gen_loader.c +1 -2
index 6945dd99a846..cd5c2543f54d 100644
@@ -4,6 +4,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
+#include <asm/byteorder.h>
 #include <linux/filter.h>
 #include <sys/param.h>
 #include "btf.h"
@@ -13,8 +14,6 @@
 #include "hashmap.h"
 #include "bpf_gen_internal.h"
 #include "skel_internal.h"
-#include <asm/byteorder.h>
-#include "str_error.h"
 
 #define MAX_USED_MAPS	64
 #define MAX_USED_PROGS	32
tools/lib/bpf/libbpf.c +0 -1
index c21bc61f5ff4..6d19e0db492c 100644
@@ -51,7 +51,6 @@
 #include "libbpf.h"
 #include "bpf.h"
 #include "btf.h"
-#include "str_error.h"
 #include "libbpf_internal.h"
 #include "hashmap.h"
 #include "bpf_gen_internal.h"
tools/lib/bpf/libbpf_internal.h +10 -0
index c93797dcaf5b..a8f204139371 100644
@@ -172,6 +172,16 @@ do {				\
 #define pr_info(fmt, ...)	__pr(LIBBPF_INFO, fmt, ##__VA_ARGS__)
 #define pr_debug(fmt, ...)	__pr(LIBBPF_DEBUG, fmt, ##__VA_ARGS__)
 
+/**
+ * @brief **libbpf_errstr()** returns string corresponding to numeric errno
+ * @param err negative numeric errno
+ * @return pointer to string representation of the errno, that is invalidated
+ * upon the next call.
+ */
+const char *libbpf_errstr(int err);
+
+#define errstr(err) libbpf_errstr(err)
+
 #ifndef __has_builtin
 #define __has_builtin(x) 0
 #endif
tools/lib/bpf/libbpf_utils.c +72 -0

85 lines not on this page · raw message

tools/lib/bpf/linker.c +0 -1
index a469e5d4fee7..56ae77047bc3 100644
@@ -25,7 +25,6 @@
 #include "btf.h"
 #include "libbpf_internal.h"
 #include "strset.h"
-#include "str_error.h"
 
 #define BTF_EXTERN_SEC ".extern"
 
tools/lib/bpf/relo_core.c +0 -1
index 2b83c98a1137..6eea5edba58a 100644
@@ -64,7 +64,6 @@ enum libbpf_print_level {
 #include "libbpf.h"
 #include "bpf.h"
 #include "btf.h"
-#include "str_error.h"
 #include "libbpf_internal.h"
 #endif
 
tools/lib/bpf/ringbuf.c +0 -1
index 9702b70da444..00ec4837a06d 100644
@@ -21,7 +21,6 @@
 #include "libbpf.h"
 #include "libbpf_internal.h"
 #include "bpf.h"
-#include "str_error.h"
 
 struct ring {
 	ring_buffer_sample_fn sample_cb;
tools/lib/bpf/str_error.c deleted +0 -80

80 lines not on this page · raw message

tools/lib/bpf/str_error.h deleted +0 -15
deleted file mode 100644
index d4c82eec034d..000000000000
@@ -1,15 +0,0 @@
-/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
-#ifndef __LIBBPF_STR_ERROR_H
-#define __LIBBPF_STR_ERROR_H
-
-/**
- * @brief **libbpf_errstr()** returns string corresponding to numeric errno
- * @param err negative numeric errno
- * @return pointer to string representation of the errno, that is invalidated
- * upon the next call.
- */
-const char *libbpf_errstr(int err);
-
-#define errstr(err) libbpf_errstr(err)
-
-#endif /* __LIBBPF_STR_ERROR_H */
tools/lib/bpf/usdt.c +0 -1
index fc2785eecc17..c174b4086673 100644
@@ -20,7 +20,6 @@
 #include "libbpf_common.h"
 #include "libbpf_internal.h"
 #include "hashmap.h"
-#include "str_error.h"
 
 /* libbpf's USDT support consists of BPF-side state/code and user-space
  * state/code working together in concert. BPF-side parts are defined in
Signature · 1 line
2.47.3
author maintainer
#4 lore raw

PATCH 4/5 libbpf: move libbpf_sha256() implementation into libbpf_utils.c

In reply to Andrii Nakryiko

Move sha256 implementation out of already large and unwieldy libbpf.c into libbpf_utils.c where we'll keep reusable helpers.

---
 tools/lib/bpf/libbpf.c          | 98 ---------------------------------
 tools/lib/bpf/libbpf_internal.h |  5 ++
 tools/lib/bpf/libbpf_utils.c    | 95 ++++++++++++++++++++++++++++++++
 3 files changed, 100 insertions(+), 98 deletions(-)
tools/lib/bpf/libbpf.c +0 -98
index 6d19e0db492c..dd3b2f57082d 100644
@@ -35,7 +35,6 @@
 #include <linux/perf_event.h>
 #include <linux/bpf_perf_event.h>
 #include <linux/ring_buffer.h>
-#include <linux/unaligned.h>
 #include <sys/epoll.h>
 #include <sys/ioctl.h>
 #include <sys/mman.h>
@@ -14282,100 +14281,3 @@ void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s)
 	free(s->progs);
 	free(s);
 }
-
-static inline __u32 ror32(__u32 v, int bits)
-{
-	return (v >> bits) | (v << (32 - bits));
-}
-
-#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))
-#define Sigma_1(x) (ror32((x), 6) ^ ror32((x), 11) ^ ror32((x), 25))
-#define sigma_0(x) (ror32((x), 7) ^ ror32((x), 18) ^ ((x) >> 3))
-#define sigma_1(x) (ror32((x), 17) ^ ror32((x), 19) ^ ((x) >> 10))
-
-static const __u32 sha256_K[64] = {
-	0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
-	0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
-	0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
-	0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
-	0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
-	0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
-	0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
-	0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
-	0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
-	0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
-	0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
-};
-
-#define SHA256_ROUND(i, a, b, c, d, e, f, g, h)                                \
-	{                                                                      \
-		__u32 tmp = h + Sigma_1(e) + Ch(e, f, g) + sha256_K[i] + w[i]; \
-		d += tmp;                                                      \
-		h = tmp + Sigma_0(a) + Maj(a, b, c);                           \
-	}
-
-static void sha256_blocks(__u32 state[8], const __u8 *data, size_t nblocks)
-{
-	while (nblocks--) {
-		__u32 a = state[0];
-		__u32 b = state[1];
-		__u32 c = state[2];
-		__u32 d = state[3];
-		__u32 e = state[4];
-		__u32 f = state[5];
-		__u32 g = state[6];
-		__u32 h = state[7];
-		__u32 w[64];
-		int i;
-
-		for (i = 0; i < 16; i++)
-			w[i] = get_unaligned_be32(&data[4 * i]);
-		for (; i < ARRAY_SIZE(w); i++)
-			w[i] = sigma_1(w[i - 2]) + w[i - 7] +
-			       sigma_0(w[i - 15]) + w[i - 16];
-		for (i = 0; i < ARRAY_SIZE(w); i += 8) {
-			SHA256_ROUND(i + 0, a, b, c, d, e, f, g, h);
-			SHA256_ROUND(i + 1, h, a, b, c, d, e, f, g);
-			SHA256_ROUND(i + 2, g, h, a, b, c, d, e, f);
-			SHA256_ROUND(i + 3, f, g, h, a, b, c, d, e);
-			SHA256_ROUND(i + 4, e, f, g, h, a, b, c, d);
-			SHA256_ROUND(i + 5, d, e, f, g, h, a, b, c);
-			SHA256_ROUND(i + 6, c, d, e, f, g, h, a, b);
-			SHA256_ROUND(i + 7, b, c, d, e, f, g, h, a);
-		}
-		state[0] += a;
-		state[1] += b;
-		state[2] += c;
-		state[3] += d;
-		state[4] += e;
-		state[5] += f;
-		state[6] += g;
-		state[7] += h;
-		data += SHA256_BLOCK_LENGTH;
-	}
-}
-
-void libbpf_sha256(const void *data, size_t len, __u8 out[SHA256_DIGEST_LENGTH])
-{
-	__u32 state[8] = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
-			   0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 };
-	const __be64 bitcount = cpu_to_be64((__u64)len * 8);
-	__u8 final_data[2 * SHA256_BLOCK_LENGTH] = { 0 };
-	size_t final_len = len % SHA256_BLOCK_LENGTH;
-	int i;
-
-	sha256_blocks(state, data, len / SHA256_BLOCK_LENGTH);
-
-	memcpy(final_data, data + len - final_len, final_len);
-	final_data[final_len] = 0x80;
-	final_len = round_up(final_len + 9, SHA256_BLOCK_LENGTH);
-	memcpy(&final_data[final_len - 8], &bitcount, 8);
-
-	sha256_blocks(state, final_data, final_len / SHA256_BLOCK_LENGTH);
-
-	for (i = 0; i < ARRAY_SIZE(state); i++)
-		put_unaligned_be32(state[i], &out[4 * i]);
-}
tools/lib/bpf/libbpf_internal.h +5 -0
index a8f204139371..35b2527bedec 100644
@@ -722,6 +722,11 @@ static inline bool is_pow_of_2(size_t x)
 	return x && (x & (x - 1)) == 0;
 }
 
+static inline __u32 ror32(__u32 v, int bits)
+{
+	return (v >> bits) | (v << (32 - bits));
+}
+
 #define PROG_LOAD_ATTEMPTS 5
 int sys_bpf_prog_load(union bpf_attr *attr, unsigned int size, int attempts);
 
tools/lib/bpf/libbpf_utils.c +95 -0

104 lines not on this page · raw message

Signature · 1 line
2.47.3
author maintainer
#5 lore raw

PATCH 5/5 libbpf: remove linux/unaligned.h dependency for libbpf_sha256()

In reply to Andrii Nakryiko

linux/unaligned.h include dependency is causing issues for libbpf's Github mirror due to {get,put}_unaligned_be32() usage.

So get rid of it by implementing custom variants of those macros that will work both in kernel repo and in Github mirror repo.

Also fix switch from round_up() to roundup(), as the former is not available in Github mirror (and is just a subtle more specific variant of roundup() anyways).

---
 tools/lib/bpf/libbpf_utils.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
tools/lib/bpf/libbpf_utils.c +9 -2
index f8290a0b3aaf..4189504fae75 100644
@@ -13,7 +13,6 @@
 #include <errno.h>
 #include <inttypes.h>
 #include <linux/kernel.h>
-#include <linux/unaligned.h>
 
 #include "libbpf.h"
 #include "libbpf_internal.h"
@@ -149,6 +148,14 @@ const char *libbpf_errstr(int err)
 	}
 }
 
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wpacked"
+struct __packed_u32 { __u32 __val; } __attribute__((packed));
+#pragma GCC diagnostic pop
+
+#define get_unaligned_be32(p) (((struct __packed_u32 *)(p))->__val)
+#define put_unaligned_be32(v, p) do { ((struct __packed_u32 *)(p))->__val = (v); } while (0)
+
 #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)))
@@ -232,7 +239,7 @@ void libbpf_sha256(const void *data, size_t len, __u8 out[SHA256_DIGEST_LENGTH])
 
 	memcpy(final_data, data + len - final_len, final_len);
 	final_data[final_len] = 0x80;
-	final_len = round_up(final_len + 9, SHA256_BLOCK_LENGTH);
+	final_len = roundup(final_len + 9, SHA256_BLOCK_LENGTH);
 	memcpy(&final_data[final_len - 8], &bitcount, 8);
 
 	sha256_blocks(state, final_data, final_len / SHA256_BLOCK_LENGTH);
Signature · 1 line
2.47.3
#6 lore raw

Re: [PATCH bpf-next 5/5] libbpf: remove linux/unaligned.h dependency for libbpf_sha256()

Quoted 29 lines · Andrii Nakryiko

linux/unaligned.h include dependency is causing issues for libbpf's Github mirror due to {get,put}_unaligned_be32() usage.

So get rid of it by implementing custom variants of those macros that will work both in kernel repo and in Github mirror repo.

Also fix switch from round_up() to roundup(), as the former is not available in Github mirror (and is just a subtle more specific variant of roundup() anyways).

---
 tools/lib/bpf/libbpf_utils.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
tools/lib/bpf/libbpf_utils.c +0 -1
index f8290a0b3aaf..4189504fae75 100644
@@ -13,7 +13,6 @@
 #include <errno.h>
 #include <inttypes.h>
 #include <linux/kernel.h>
-#include <linux/unaligned.h>
 
 #include "libbpf.h"
 #include "libbpf_internal.h"

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

Quoted 10 lines
tools/lib/bpf/libbpf_utils.c +7 -0
@@ -149,6 +148,14 @@ const char *libbpf_errstr(int err)
        }
 }
 
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wpacked"
+struct __packed_u32 { __u32 __val; } __attribute__((packed));
+#pragma GCC diagnostic pop
+
+#define get_unaligned_be32(p) (((struct __packed_u32 *)(p))->__val)
+#define put_unaligned_be32(v, p) do { ((struct __packed_u32 *)(p))->__val = (v); } while (0)

doh, obviously these miss be32 conversions, will fix in v2. But let's see if AI catches this.

Quoted 16 lines
tools/lib/bpf/libbpf_utils.c +2 -2
+
 #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)))
@@ -232,7 +239,7 @@ void libbpf_sha256(const void *data, size_t len, __u8 out[SHA256_DIGEST_LENGTH])
 
        memcpy(final_data, data + len - final_len, final_len);
        final_data[final_len] = 0x80;
-       final_len = round_up(final_len + 9, SHA256_BLOCK_LENGTH);
+       final_len = roundup(final_len + 9, SHA256_BLOCK_LENGTH);
        memcpy(&final_data[final_len - 8], &bitcount, 8);
 
        sha256_blocks(state, final_data, final_len / SHA256_BLOCK_LENGTH);
--

2.47.3

#7 lore raw

Re: [PATCH bpf-next 5/5] libbpf: remove linux/unaligned.h dependency for libbpf_sha256()

Quoted 34 lines · Andrii Nakryiko
Quoted 32 lines · Andrii Nakryiko

linux/unaligned.h include dependency is causing issues for libbpf's Github mirror due to {get,put}_unaligned_be32() usage.

So get rid of it by implementing custom variants of those macros that will work both in kernel repo and in Github mirror repo.

Also fix switch from round_up() to roundup(), as the former is not available in Github mirror (and is just a subtle more specific variant of roundup() anyways).

---
 tools/lib/bpf/libbpf_utils.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
tools/lib/bpf/libbpf_utils.c +0 -1
index f8290a0b3aaf..4189504fae75 100644
@@ -13,7 +13,6 @@
 #include <errno.h>
 #include <inttypes.h>
 #include <linux/kernel.h>
-#include <linux/unaligned.h>
 
 #include "libbpf.h"
 #include "libbpf_internal.h"
@@ -149,6 +148,14 @@ const char *libbpf_errstr(int err)
        }
 }
 
Quoted 10 lines

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

Quoted 7 lines
tools/lib/bpf/libbpf_utils.c +7 -0
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wpacked"
+struct __packed_u32 { __u32 __val; } __attribute__((packed));
+#pragma GCC diagnostic pop
+
+#define get_unaligned_be32(p) (((struct __packed_u32 *)(p))->__val)
+#define put_unaligned_be32(v, p) do { ((struct __packed_u32 *)(p))->__val = (v); } while (0)

doh, obviously these miss be32 conversions, will fix in v2. But let's see if AI catches this.

It did, very nice ([0]):

Can these custom macros produce incorrect results on little-endian systems? The original kernel get_unaligned_be32() uses be32_to_cpu() to convert from big-endian to CPU byte order, but this implementation appears to skip the endianness conversion entirely.

Since libbpf_sha256() processes data in big-endian format per the SHA256 specification, won't this cause incorrect hash computation on little-endian architectures where most libbpf users run?

[0] https://github.com/kernel-patches/bpf/pull/9901#issuecomment-3353896300

Quoted 17 lines
Quoted 17 lines
tools/lib/bpf/libbpf_utils.c +2 -2
+
 #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)))
@@ -232,7 +239,7 @@ void libbpf_sha256(const void *data, size_t len, __u8 out[SHA256_DIGEST_LENGTH])
 
        memcpy(final_data, data + len - final_len, final_len);
        final_data[final_len] = 0x80;
-       final_len = round_up(final_len + 9, SHA256_BLOCK_LENGTH);
+       final_len = roundup(final_len + 9, SHA256_BLOCK_LENGTH);
        memcpy(&final_data[final_len - 8], &bitcount, 8);
 
        sha256_blocks(state, final_data, final_len / SHA256_BLOCK_LENGTH);
--

2.47.3

#8 lore raw
In reply to Andrii Nakryiko
Quoted 6 lines · Andrii Nakryiko

Recent reimplementation of libbpf_sha256() introduced issues for libbpf's Github mirror due to reliance on linux/unaligned.h header. This patch set fixes those issues to make libbpf source code compatible with Github mirror setup.

Quoted 8 lines

This patch set starts with a bit of organization: we introduce libbpf_utils.c as a place for generic internal helpers like libbpf_errstr() and libbpf_sha256(), and move a few existing helpers there. We also clean up libbpf_strerror_r(), which seems to be a leftover of some previous refactorings.

And finally, we move libbpf_sha256() from huge libbpf.c into libbpf_utils.c, following up with fix ups to make its code more Github-friendly.

Though only patch 5 is a fix, let's get the whole thing into bpf tree, since we're still in the merge window.