Skip to content

Commit 7fef179

Browse files
borkmannAlexei Starovoitov
authored andcommitted
libbpf: Guard add_data() against size overflow
add_data() computes size8 = roundup(size, 8) and then hands size8 to realloc_data_buf() before doing memcpy(gen->data_cur, data, size) with the original size. A wrapped size8 passes through the realloc_data_buf() INT32_MAX check. Harden this against overflow, though not realistic to happen in practice. Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lore.kernel.org/r/20260602133052.423725-3-daniel@iogearbox.net Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 3c56ee3 commit 7fef179

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

tools/lib/bpf/gen_loader.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,16 @@ void bpf_gen__init(struct bpf_gen *gen, int log_level, int nr_progs, int nr_maps
160160

161161
static int add_data(struct bpf_gen *gen, const void *data, __u32 size)
162162
{
163-
__u32 size8 = roundup(size, 8);
164163
__u64 zero = 0;
164+
__u32 size8;
165165
void *prev;
166166

167+
if (size > INT32_MAX) {
168+
gen->error = -ERANGE;
169+
return 0;
170+
}
171+
size8 = roundup(size, 8);
172+
167173
if (realloc_data_buf(gen, size8))
168174
return 0;
169175
prev = gen->data_cur;

0 commit comments

Comments
 (0)