libbpf.c memory leaks fixes proposal.
From 381ae513671ea214f0f6a04ca9da75dfe4411683 Mon Sep 17 00:00:00 2001 From: David Carlier <devnexen@xxxxxxxxx> Date: Sat, 18 Jan 2025 10:30:39 +0000 Subject: [PATCH] libbpf.c: bpf_program__attach_uprobe_opts fix possible memory leaks. bpf_program__attach_perf_event_opts() might be not enough to close the file descriptor, bpt_link__destroy() does a more thorough clean up including its inner file descriptor. Applying to bpf_program__attach_kprobe_opts/bpf_program__attach_tracepoints_opts too. Signed-off-by: David Carlier <devnexen@xxxxxxxxx> --- src/libbpf.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libbpf.c b/src/libbpf.c index 194809d..e571675 100644 --- a/src/libbpf.c +++ b/src/libbpf.c @@ -11271,7 +11271,7 @@ bpf_program__attach_kprobe_opts(const struct bpf_program *prog, link = bpf_program__attach_perf_event_opts(prog, pfd, &pe_opts); err = libbpf_get_error(link); if (err) { - close(pfd); + bpf_link__destroy(link); pr_warn("prog '%s': failed to attach to %s '%s+0x%zx': %s\n", prog->name, retprobe ? "kretprobe" : "kprobe", func_name, offset, @@ -12259,7 +12259,7 @@ bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid, link = bpf_program__attach_perf_event_opts(prog, pfd, &pe_opts); err = libbpf_get_error(link); if (err) { - close(pfd); + bpf_link__destroy(link); pr_warn("prog '%s': failed to attach to %s '%s:0x%zx': %s\n", prog->name, retprobe ? "uretprobe" : "uprobe", binary_path, func_offset, @@ -12514,7 +12514,7 @@ struct bpf_link *bpf_program__attach_tracepoint_opts(const struct bpf_program *p link = bpf_program__attach_perf_event_opts(prog, pfd, &pe_opts); err = libbpf_get_error(link); if (err) { - close(pfd); + bpf_link__destroy(link); pr_warn("prog '%s': failed to attach to tracepoint '%s/%s': %s\n", prog->name, tp_category, tp_name, errstr(err)); -- 2.47.2