public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf 1/2] libbpf: Fix btf object leak in load_module_btfs
@ 2026-04-14 19:48 Jiri Olsa
  2026-04-14 19:48 ` [PATCH bpf 2/2] libbpf: Prevent double close of btf objects Jiri Olsa
  2026-04-14 20:28 ` [PATCH bpf 1/2] libbpf: Fix btf object leak in load_module_btfs bot+bpf-ci
  0 siblings, 2 replies; 6+ messages in thread
From: Jiri Olsa @ 2026-04-14 19:48 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko
  Cc: bpf, Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song

Sashiko found leak in the load_module_btfs error path [1],
which happens when libbpf_ensure_mem fails, then the btf
object is leaked.

Adding missing btf__free object and making sure each iteration
starts with NULL-ed btf pointer.

[1] https://sashiko.dev/#/patchset/20260324081846.2334094-1-jolsa%40kernel.org
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
 tools/lib/bpf/libbpf.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 0be7017800fe..b60ac8094a9e 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -5752,7 +5752,6 @@ static int load_module_btfs(struct bpf_object *obj)
 {
 	struct bpf_btf_info info;
 	struct module_btf *mod_btf;
-	struct btf *btf;
 	char name[64];
 	__u32 id = 0, len;
 	int err, fd;
@@ -5771,6 +5770,8 @@ static int load_module_btfs(struct bpf_object *obj)
 		return 0;
 
 	while (true) {
+		struct btf *btf = NULL;
+
 		err = bpf_btf_get_next_id(id, &id);
 		if (err && errno == ENOENT)
 			return 0;
@@ -5837,6 +5838,7 @@ static int load_module_btfs(struct bpf_object *obj)
 		continue;
 
 err_out:
+		btf__free(btf);
 		close(fd);
 		return err;
 	}
-- 
2.53.0


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

end of thread, other threads:[~2026-04-15  8:23 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-14 19:48 [PATCH bpf 1/2] libbpf: Fix btf object leak in load_module_btfs Jiri Olsa
2026-04-14 19:48 ` [PATCH bpf 2/2] libbpf: Prevent double close of btf objects Jiri Olsa
2026-04-14 20:29   ` bot+bpf-ci
2026-04-15  8:23     ` Jiri Olsa
2026-04-14 20:28 ` [PATCH bpf 1/2] libbpf: Fix btf object leak in load_module_btfs bot+bpf-ci
2026-04-15  8:22   ` Jiri Olsa

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox