Commit ebbd7f64 authored by Quentin Monnet's avatar Quentin Monnet Committed by Andrii Nakryiko

bpftool: Fix memory leak in prog_dump()

Following the extraction of prog_dump() from do_dump(), the struct btf
allocated in prog_dump() is no longer freed on error; the struct
bpf_prog_linfo is not freed at all. Make sure we release them before
exiting the function.

Fixes: ec202509 ("bpftool: Match several programs with same tag")
Signed-off-by: default avatarQuentin Monnet <quentin@isovalent.com>
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20211110114632.24537-2-quentin@isovalent.com
parent 325d956d
...@@ -709,8 +709,8 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode, ...@@ -709,8 +709,8 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode,
char func_sig[1024]; char func_sig[1024];
unsigned char *buf; unsigned char *buf;
__u32 member_len; __u32 member_len;
int fd, err = -1;
ssize_t n; ssize_t n;
int fd;
if (mode == DUMP_JITED) { if (mode == DUMP_JITED) {
if (info->jited_prog_len == 0 || !info->jited_prog_insns) { if (info->jited_prog_len == 0 || !info->jited_prog_insns) {
...@@ -749,7 +749,7 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode, ...@@ -749,7 +749,7 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode,
if (fd < 0) { if (fd < 0) {
p_err("can't open file %s: %s", filepath, p_err("can't open file %s: %s", filepath,
strerror(errno)); strerror(errno));
return -1; goto exit_free;
} }
n = write(fd, buf, member_len); n = write(fd, buf, member_len);
...@@ -757,7 +757,7 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode, ...@@ -757,7 +757,7 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode,
if (n != (ssize_t)member_len) { if (n != (ssize_t)member_len) {
p_err("error writing output file: %s", p_err("error writing output file: %s",
n < 0 ? strerror(errno) : "short write"); n < 0 ? strerror(errno) : "short write");
return -1; goto exit_free;
} }
if (json_output) if (json_output)
...@@ -771,7 +771,7 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode, ...@@ -771,7 +771,7 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode,
info->netns_ino, info->netns_ino,
&disasm_opt); &disasm_opt);
if (!name) if (!name)
return -1; goto exit_free;
} }
if (info->nr_jited_func_lens && info->jited_func_lens) { if (info->nr_jited_func_lens && info->jited_func_lens) {
...@@ -866,9 +866,12 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode, ...@@ -866,9 +866,12 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode,
kernel_syms_destroy(&dd); kernel_syms_destroy(&dd);
} }
btf__free(btf); err = 0;
return 0; exit_free:
btf__free(btf);
bpf_prog_linfo__free(prog_linfo);
return err;
} }
static int do_dump(int argc, char **argv) static int do_dump(int argc, char **argv)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment