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

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.")
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Link: https://lore.kernel.org/r/YtZ+oAySqIhFl6/J@kili
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Dan Carpenter and committed by
Alexei Starovoitov
b77ffb30 c5d22f4c

+1 -1
+1 -1
tools/lib/bpf/gen_loader.c
··· 533 533 gen->attach_kind = kind; 534 534 ret = snprintf(gen->attach_target, sizeof(gen->attach_target), "%s%s", 535 535 prefix, attach_name); 536 - if (ret == sizeof(gen->attach_target)) 536 + if (ret >= sizeof(gen->attach_target)) 537 537 gen->error = -ENOSPC; 538 538 } 539 539