Skip to contentSkip to conversation

libbpf: Skip endianness swap when loader generation failed

PATCH Applied Daniel Borkmann 3 participants 3 messages May 29 – Jun 1

Subsystems (2)
lore
Share & tools
lore

Daniel Borkmann

author maintainer
#0 lore raw

PATCH 1/1 libbpf: Skip endianness swap when loader generation failed

bpf_gen__prog_load() byte-swaps the program insns and the {func,line}_info and CO-RE relo blobs in place for cross-endian targets. The blob offsets come from add_data(), which returns 0 on failure: realloc_data_buf() either frees and NULLs gen->data_start (realloc OOM) or returns early on an already-latched gen->error, leaving a stale, possibly too-small buffer.

Neither bswap site checked for this. With gen->swapped_endian set and a failed generation, "gen->data_start + off" becomes NULL + 0. Guard the same way via !gen->error so they are skipped once generation has failed.

Fixes 8ca3323dce43 ("libbpf: Support creating light skeleton of either endianness")

---
[ Addressing https://lore.kernel.org/bpf/[email protected]/ ]

 tools/lib/bpf/gen_loader.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/lib/bpf/gen_loader.c +2 -2
index bcea21c3b7bb..fb4425cbffa3 100644
@@ -1054,7 +1054,7 @@ void bpf_gen__prog_load(struct bpf_gen *gen,
 		 prog_idx, prog_type, insns_off, insn_cnt, license_off);
 
 	/* convert blob insns to target endianness */
-	if (gen->swapped_endian) {
+	if (gen->swapped_endian && !gen->error) {
 		struct bpf_insn *insn = gen->data_start + insns_off;
 		int i;
 
@@ -1092,7 +1092,7 @@ void bpf_gen__prog_load(struct bpf_gen *gen,
 		 sizeof(struct bpf_core_relo));
 
 	/* convert all info blobs to target endianness */
-	if (gen->swapped_endian)
+	if (gen->swapped_endian && !gen->error)
 		info_blob_bswap(gen, func_info, line_info, core_relos, load_attr);
 
 	libbpf_strlcpy(attr.prog_name, prog_name, sizeof(attr.prog_name));
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Pre-existing issues:
- [Critical] The calculation of total instruction size insn_cnt * sizeof(struct bpf_insn) is evaluated as a size_t but implicitly truncated when passed to add_data(), which expects a 32-bit __u32 size, leading to an undersized allocation and a critical heap buffer overflow.
- [Critical] Calculations for total blob sizes such as func_info_tot_sz = load_attr->func_info_cnt * load_attr->func_info_rec_size suffer from 32-bit integer wrapping, leading to undersized allocations and heap buffer overflows.
- [Critical] The roundup(size, 8) macro call inside add_data() overflows for large sizes close to UINT32_MAX, leading to a bypass of the size validation and a massive out-of-bounds memcpy.
--

commit 3d03d412315397f92ae3ab0965da7090253b5cf8
Author: Daniel Borkmann <[email protected]>

libbpf: Skip endianness swap when loader generation failed

The commit fixes a bug in bpf_gen__prog_load() where program instructions and info blobs are byte-swapped for cross-endian targets. It prevents byte-swapping if loader generation has already failed and gen->error is set, which otherwise causes NULL pointer dereferences when calculating blob offsets on a stale buffer.

Quoted 5 lines
tools/lib/bpf/gen_loader.c +0 -0
index 9478b8f78f260..d9b38b9077eb6 100644

tools/lib/bpf/gen_loader.c static void info_blob_bswap(struct bpf_gen *gen, int func_info, int line_info,

Quoted 10 lines
tools/lib/bpf/gen_loader.c +0 -0
@@ -1026,99 +1026,99 @@ static void info_blob_bswap(struct bpf_gen *gen, int func_info, int line_info,
 
[ ... ]
 
 void bpf_gen__prog_load(struct bpf_gen *gen,
 			enum bpf_prog_type prog_type, const char *prog_name,
 			const char *license, struct bpf_insn *insns, size_t insn_cnt,
 			struct bpf_prog_load_opts *load_attr, int prog_idx)
 {
 	int func_info_tot_sz = load_attr->func_info_cnt *
 			       load_attr->func_info_rec_size;

[Severity: Critical]
This is a pre-existing issue, but does this calculation suffer from 32-bit integer wrapping?

Values like func_info_cnt and func_info_rec_size are 32-bit unsigned integers. If they are controlled by a user, their product could exceed 32 bits and wrap to a small value. Later code iterates over the unabridged func_info_cnt which could lead to an undersized allocation and a massive out-of-bounds read and write.

tools/lib/bpf/gen_loader.c

Quoted 11 lines
tools/lib/bpf/gen_loader.c +0 -0
 	int line_info_tot_sz = load_attr->line_info_cnt *
 			       load_attr->line_info_rec_size;
 	int core_relo_tot_sz = gen->core_relo_cnt *
 			       sizeof(struct bpf_core_relo);
 
[ ... ]
 
 	/* add license string to blob of bytes */
 	license_off = add_data(gen, license, strlen(license) + 1);
 	/* add insns to blob of bytes */
 	insns_off = add_data(gen, insns, insn_cnt * sizeof(struct bpf_insn));

[Severity: Critical]
This is a pre-existing issue, but can the insn_cnt * sizeof(struct bpf_insn) evaluation be implicitly truncated?

The result is evaluated as a size_t, but add_data() expects a 32-bit __u32 size parameter. If insn_cnt is maliciously large, the value could exceed the 32-bit maximum and be implicitly truncated. add_data() would allocate an undersized buffer, while later code like bpf_insn_bswap() operates using the original 64-bit insn_cnt, leading to a heap buffer overflow.

Quoted 4 lines
tools/lib/bpf/gen_loader.c +0 -0
 	pr_debug("gen: prog_load: prog_idx %d type %d insn off %d insns_cnt %zd license off %d\n",
 		 prog_idx, prog_type, insns_off, insn_cnt, license_off);
 
 	/* convert blob insns to target endianness */

tools/lib/bpf/gen_loader.c void bpf_gen__prog_load(struct bpf_gen *gen,

Quoted 10 lines
tools/lib/bpf/gen_loader.c +1 -1
-	if (gen->swapped_endian) {
+	if (gen->swapped_endian && !gen->error) {
 		struct bpf_insn *insn = gen->data_start + insns_off;
 		int i;
 
[ ... ]
 
 	attr.func_info_rec_size = tgt_endian(load_attr->func_info_rec_size);
 	attr.func_info_cnt = tgt_endian(load_attr->func_info_cnt);
 	func_info = add_data(gen, load_attr->func_info, func_info_tot_sz);

[Severity: Critical]
This is a pre-existing issue, but does add_data() bypass size validation for sizes close to UINT32_MAX?

Looking at add_data() in this file:

static int add_data(struct bpf_gen *gen, const void *data, __u32 size)

{
	__u32 size8 = roundup(size, 8);
...
	if (realloc_data_buf(gen, size8))
		return 0;
...
	if (data) {
		memcpy(gen->data_cur, data, size);

If a user provides a size near UINT32_MAX (for example via the func_info_tot_sz calculation), the roundup(size, 8) macro adds 7 and overflows the 32-bit boundary, resulting in size8 becoming 0. The internal realloc_data_buf() bypasses its size > INT32_MAX validation, and the subsequent memcpy() uses the unrounded huge size, resulting in a massive out-of-bounds copy.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Daniel Borkmann
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Quoted 11 lines · On Fri, 29 May 2026 18:28:29 +0200 you wrote:

bpf_gen__prog_load() byte-swaps the program insns and the {func,line}_info and CO-RE relo blobs in place for cross-endian targets. The blob offsets come from add_data(), which returns 0 on failure: realloc_data_buf() either frees and NULLs gen->data_start (realloc OOM) or returns early on an already-latched gen->error, leaving a stale, possibly too-small buffer.

Neither bswap site checked for this. With gen->swapped_endian set and a failed generation, "gen->data_start + off" becomes NULL + 0. Guard the same way via !gen->error so they are skipped once generation has failed.

[...]

Here is the summary with links:
- [bpf-next] libbpf: Skip endianness swap when loader generation failed

    https://git.kernel.org/bpf/bpf-next/c/41300d032a1b

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html