Skip to content

Commit 3c5e2f1

Browse files
borkmannAlexei Starovoitov
authored andcommitted
libbpf: Skip hash computation when loader generation failed
bpf_gen__finish() calls compute_sha_update_offsets() gated only on the gen_hash option, without first consulting gen->error. On a failed generation this is buggy: a failed realloc_data_buf() sets gen->data_start to NULL (leaving gen->data_cur dangling), so compute_sha_update_offsets() runs libbpf_sha256() over a NULL buffer with a bogus length; a failed realloc_insn_buf() likewise sets gen->insn_start to NULL and the hash immediates get patched through that NULL base. The computed program is discarded in either case, since the following "if (!gen->error)" block does not publish opts->insns once an error is set. Thus, skip the hash pass when generation has already failed. Fixes: ea92308 ("libbpf: Embed and verify the metadata hash in the loader") Reported-by: sashiko <sashiko@sashiko.dev> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lore.kernel.org/r/20260529094119.307264-2-daniel@iogearbox.net Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent e2c8826 commit 3c5e2f1

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

tools/lib/bpf/gen_loader.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,13 +397,12 @@ int bpf_gen__finish(struct bpf_gen *gen, int nr_progs, int nr_maps)
397397
blob_fd_array_off(gen, i));
398398
emit(gen, BPF_MOV64_IMM(BPF_REG_0, 0));
399399
emit(gen, BPF_EXIT_INSN());
400-
if (OPTS_GET(gen->opts, gen_hash, false))
401-
compute_sha_update_offsets(gen);
402-
403-
pr_debug("gen: finish %s\n", errstr(gen->error));
404400
if (!gen->error) {
405401
struct gen_loader_opts *opts = gen->opts;
406402

403+
if (OPTS_GET(opts, gen_hash, false))
404+
compute_sha_update_offsets(gen);
405+
407406
opts->insns = gen->insn_start;
408407
opts->insns_sz = gen->insn_cur - gen->insn_start;
409408
opts->data = gen->data_start;
@@ -418,6 +417,7 @@ int bpf_gen__finish(struct bpf_gen *gen, int nr_progs, int nr_maps)
418417
bpf_insn_bswap(insn++);
419418
}
420419
}
420+
pr_debug("gen: finish %s\n", errstr(gen->error));
421421
return gen->error;
422422
}
423423

0 commit comments

Comments
 (0)