bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf-next 1/2] libbpf: fix memory leak in legacy kprobe attach logic
@ 2021-09-20 18:19 Andrii Nakryiko
  2021-09-20 18:19 ` [PATCH bpf-next 2/2] libbpf: fix legacy kprobe event creation FD error handling Andrii Nakryiko
  0 siblings, 1 reply; 3+ messages in thread
From: Andrii Nakryiko @ 2021-09-20 18:19 UTC (permalink / raw)
  To: bpf, ast, daniel; +Cc: andrii, kernel-team

In some error scenarios legacy_probe string won't be free()'d. Fix this.
This was reported by Coverity static analysis.

Fixes: ca304b40c20d ("libbpf: Introduce legacy kprobe events support")
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
---
 tools/lib/bpf/libbpf.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index da65a1666a5e..6d2f12db6034 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -9365,10 +9365,11 @@ bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
 						    offset, -1 /* pid */);
 	}
 	if (pfd < 0) {
+		err = pfd;
 		pr_warn("prog '%s': failed to create %s '%s' perf event: %s\n",
 			prog->name, retprobe ? "kretprobe" : "kprobe", func_name,
-			libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
-		return libbpf_err_ptr(pfd);
+			libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
+		goto err_out;
 	}
 	link = bpf_program__attach_perf_event_opts(prog, pfd, &pe_opts);
 	err = libbpf_get_error(link);
@@ -9377,7 +9378,7 @@ bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
 		pr_warn("prog '%s': failed to attach to %s '%s': %s\n",
 			prog->name, retprobe ? "kretprobe" : "kprobe", func_name,
 			libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
-		return libbpf_err_ptr(err);
+		goto err_out;
 	}
 	if (legacy) {
 		struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link);
@@ -9387,6 +9388,9 @@ bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
 	}
 
 	return link;
+err_out:
+	free(legacy_probe);
+	return libbpf_err_ptr(err);
 }
 
 struct bpf_link *bpf_program__attach_kprobe(const struct bpf_program *prog,
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH bpf-next 2/2] libbpf: fix legacy kprobe event creation FD error handling
  2021-09-20 18:19 [PATCH bpf-next 1/2] libbpf: fix memory leak in legacy kprobe attach logic Andrii Nakryiko
@ 2021-09-20 18:19 ` Andrii Nakryiko
  2021-09-20 23:23   ` Andrii Nakryiko
  0 siblings, 1 reply; 3+ messages in thread
From: Andrii Nakryiko @ 2021-09-20 18:19 UTC (permalink / raw)
  To: bpf, ast, daniel; +Cc: andrii, kernel-team

open() returns -1 on error, not zero FD. Fix the error handling logic.
Reported by Coverity statis analysis.

Fixes: ca304b40c20d ("libbpf: Introduce legacy kprobe events support")
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
---
 tools/lib/bpf/libbpf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 6d2f12db6034..761497be6ffc 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -9036,7 +9036,7 @@ static int poke_kprobe_events(bool add, const char *name, bool retprobe, uint64_
 	}
 
 	fd = open(file, O_WRONLY | O_APPEND, 0);
-	if (!fd)
+	if (fd < 0)
 		return -errno;
 	ret = write(fd, cmd, strlen(cmd));
 	if (ret < 0)
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH bpf-next 2/2] libbpf: fix legacy kprobe event creation FD error handling
  2021-09-20 18:19 ` [PATCH bpf-next 2/2] libbpf: fix legacy kprobe event creation FD error handling Andrii Nakryiko
@ 2021-09-20 23:23   ` Andrii Nakryiko
  0 siblings, 0 replies; 3+ messages in thread
From: Andrii Nakryiko @ 2021-09-20 23:23 UTC (permalink / raw)
  To: Andrii Nakryiko; +Cc: bpf, Alexei Starovoitov, Daniel Borkmann, Kernel Team

On Mon, Sep 20, 2021 at 11:20 AM Andrii Nakryiko <andrii@kernel.org> wrote:
>
> open() returns -1 on error, not zero FD. Fix the error handling logic.
> Reported by Coverity statis analysis.
>
> Fixes: ca304b40c20d ("libbpf: Introduce legacy kprobe events support")
> Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
> ---

This patch is superseded by a new patch series ([0]), please don't apply.

  [0] https://patchwork.kernel.org/project/netdevbpf/list/?series=550083&state=*

>  tools/lib/bpf/libbpf.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 6d2f12db6034..761497be6ffc 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -9036,7 +9036,7 @@ static int poke_kprobe_events(bool add, const char *name, bool retprobe, uint64_
>         }
>
>         fd = open(file, O_WRONLY | O_APPEND, 0);
> -       if (!fd)
> +       if (fd < 0)
>                 return -errno;
>         ret = write(fd, cmd, strlen(cmd));
>         if (ret < 0)
> --
> 2.30.2
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-09-21  1:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-20 18:19 [PATCH bpf-next 1/2] libbpf: fix memory leak in legacy kprobe attach logic Andrii Nakryiko
2021-09-20 18:19 ` [PATCH bpf-next 2/2] libbpf: fix legacy kprobe event creation FD error handling Andrii Nakryiko
2021-09-20 23:23   ` Andrii Nakryiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).