Skip to contentSkip to conversation

libbpf: fix an snprintf() overflow check

PATCH Applied Dan Carpenter 3 participants 3 messages Jul 19, 2022

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

PATCH 1/1 libbpf: fix an snprintf() overflow check

The snprintf() function returns the number of bytes it *would* have copied if there were enough space. So it can return > the sizeof(gen->attach_target).

Fixes 67234743736a ("libbpf: Generate loader program out of BPF ELF file.")
---
 tools/lib/bpf/gen_loader.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/gen_loader.c +1 -1
index 927745b08014..23f5c46708f8 100644
@@ -533,7 +533,7 @@ void bpf_gen__record_attach_target(struct bpf_gen *gen, const char *attach_name,
 	gen->attach_kind = kind;
 	ret = snprintf(gen->attach_target, sizeof(gen->attach_target), "%s%s",
 		       prefix, attach_name);
-	if (ret == sizeof(gen->attach_target))
+	if (ret >= sizeof(gen->attach_target))
 		gen->error = -ENOSPC;
 }
 
Signature · 1 line
2.35.1
#2 lore raw
In reply to Dan Carpenter
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 8 lines · On Tue, 19 Jul 2022 12:51:28 +0300 you wrote:

The snprintf() function returns the number of bytes it *would* have copied if there were enough space. So it can return > the sizeof(gen->attach_target).

Fixes 67234743736a ("libbpf: Generate loader program out of BPF ELF file.")

[...]

Here is the summary with links:
- libbpf: fix an snprintf() overflow check

    https://git.kernel.org/bpf/bpf-next/c/b77ffb30cfc5

You are awesome, thank you!

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