diff mbox series

[bpf-next,1/1]

Message ID CA+XhMqyt7LGkitBrNE1goRMQdsP23=BwLsCor0pY+mM6zO2+zg@mail.gmail.com (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series [bpf-next,1/1] | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for bpf-next
netdev/apply success Patch already applied to bpf-next-0

Commit Message

David CARLIER Jan. 21, 2025, 4:50 p.m. UTC
libbpf.c memory leaks fixes proposal.

Comments

Ihor Solodrai Jan. 21, 2025, 7:41 p.m. UTC | #1
Hi David,

Thanks for the patch.

Please take a look at "submitting patches" page on kernel docs and resubmit.
https://docs.kernel.org/process/submitting-patches.html


On Tuesday, January 21st, 2025 at 8:50 AM, David CARLIER <devnexen@gmail.com> wrote:

> libbpf.c memory leaks fixes proposal.
diff mbox series

Patch

From 381ae513671ea214f0f6a04ca9da75dfe4411683 Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen@gmail.com>
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@gmail.com>
---
 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