Skip to content

Commit

Permalink
libbpf: Fix memory leak when emitting final btf_ext
Browse files Browse the repository at this point in the history
Free temporary allocated memory used to construct finalized .BTF.ext data.
Found by Coverity static analysis on libbpf's Github repo.

Fixes: 8fd27bf ("libbpf: Add BPF static linker BTF and BTF.ext support")
Signed-off-by: Andrii Nakryiko <[email protected]>
Signed-off-by: Alexei Starovoitov <[email protected]>
Acked-by: Song Liu <[email protected]>
Link: https://lore.kernel.org/bpf/[email protected]
  • Loading branch information
anakryiko authored and Alexei Starovoitov committed Mar 30, 2021
1 parent b83fd19 commit 05d8170
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions tools/lib/bpf/linker.c
Original file line number Diff line number Diff line change
Expand Up @@ -1906,8 +1906,10 @@ static int finalize_btf_ext(struct bpf_linker *linker)
struct dst_sec *sec = &linker->secs[i];

sz = emit_btf_ext_data(linker, cur, sec->sec_name, &sec->func_info);
if (sz < 0)
return sz;
if (sz < 0) {
err = sz;
goto out;
}

cur += sz;
}
Expand All @@ -1921,8 +1923,10 @@ static int finalize_btf_ext(struct bpf_linker *linker)
struct dst_sec *sec = &linker->secs[i];

sz = emit_btf_ext_data(linker, cur, sec->sec_name, &sec->line_info);
if (sz < 0)
return sz;
if (sz < 0) {
err = sz;
goto out;
}

cur += sz;
}
Expand All @@ -1936,8 +1940,10 @@ static int finalize_btf_ext(struct bpf_linker *linker)
struct dst_sec *sec = &linker->secs[i];

sz = emit_btf_ext_data(linker, cur, sec->sec_name, &sec->core_relo_info);
if (sz < 0)
return sz;
if (sz < 0) {
err = sz;
goto out;
}

cur += sz;
}
Expand All @@ -1948,8 +1954,10 @@ static int finalize_btf_ext(struct bpf_linker *linker)
if (err) {
linker->btf_ext = NULL;
pr_warn("failed to parse final .BTF.ext data: %d\n", err);
return err;
goto out;
}

return 0;
out:
free(data);
return err;
}

0 comments on commit 05d8170

Please sign in to comment.