Skip to contentSkip to conversation

libbpf: Fix btf object leak in load_module_btfs

PATCH In review Jiri Olsa 3 participants 6 messages Apr 14 – Apr 15

Subsystems (2)
lore
Share & tools
lore

Jiri Olsa

author reviewer
#0 lore raw

PATCH 1/2 libbpf: Fix btf object leak in load_module_btfs

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

---
 tools/lib/bpf/libbpf.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +3 -1
index 0be7017800fe..b60ac8094a9e 100644
@@ -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;
 	}
Signature · 1 line
2.53.0
author reviewer
#1 lore raw

PATCH 2/2 libbpf: Prevent double close of btf objects

Sashiko found possible double close of btf object fd [1], which happens when strdup in load_module_btfs fails at which point the obj->btf_module_cnt is already incremented.

The error path close btf fd and so does later cleanup code in bpf_object_post_load_cleanup function.

[1] https://sashiko.dev/#/patchset/20260324081846.2334094-1-jolsa%40kernel.org

---
 tools/lib/bpf/libbpf.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +2 -1
index b60ac8094a9e..33e7a09915dd 100644
@@ -5825,7 +5825,7 @@ static int load_module_btfs(struct bpf_object *obj)
 		if (err)
 			goto err_out;
 
-		mod_btf = &obj->btf_modules[obj->btf_module_cnt++];
+		mod_btf = &obj->btf_modules[obj->btf_module_cnt];
 
 		mod_btf->btf = btf;
 		mod_btf->id = id;
@@ -5835,6 +5835,7 @@ static int load_module_btfs(struct bpf_object *obj)
 			err = -ENOMEM;
 			goto err_out;
 		}
+		obj->btf_module_cnt++;
 		continue;
 
 err_out:
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH bpf 2/2] libbpf: Prevent double close of btf objects

The fix looks correct. Would it make sense to add a Fixes: tag? The double close was introduced when fd tracking and the err_out error path with close(fd) were added to load_module_btfs():
Quoted 10 lines
tools/lib/bpf/libbpf.c +1 -1
index b60ac8094a9e..33e7a09915dd 100644
@@ -5825,7 +5825,7 @@ static int load_module_btfs(struct bpf_object *obj)
 		if (err)
 			goto err_out;
 
-		mod_btf = &obj->btf_modules[obj->btf_module_cnt++];
+		mod_btf = &obj->btf_modules[obj->btf_module_cnt];

[ ... ]

tools/lib/bpf/libbpf.c static int load_module_btfs(struct bpf_object *obj) · line 5839

Quoted 6 lines
tools/lib/bpf/libbpf.c +1 -0
@@ -5835,6 +5835,7 @@ static int load_module_btfs(struct bpf_object *obj)
 			err = -ENOMEM;
 			goto err_out;
 		}
+		obj->btf_module_cnt++;
 		continue;

The fix looks correct. Would it make sense to add a Fixes: tag? The double close was introduced when fd tracking and the err_out error path with close(fd) were added to load_module_btfs():

Fixes 91abb4a6d79d ("libbpf: Support attachment of BPF tracing programs to kernel modules")

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/24420241619

#3 lore raw

Re: [PATCH bpf 2/2] libbpf: Prevent double close of btf objects

Quoted 24 lines · On Tue, Apr 14, 2026 at 08:29:01PM +0000, [email protected] wrote:
Quoted 10 lines
tools/lib/bpf/libbpf.c +1 -1
index b60ac8094a9e..33e7a09915dd 100644
@@ -5825,7 +5825,7 @@ static int load_module_btfs(struct bpf_object *obj)
 		if (err)
 			goto err_out;
 
-		mod_btf = &obj->btf_modules[obj->btf_module_cnt++];
+		mod_btf = &obj->btf_modules[obj->btf_module_cnt];

[ ... ]

tools/lib/bpf/libbpf.c static int load_module_btfs(struct bpf_object *obj) · line 5839

Quoted 6 lines
tools/lib/bpf/libbpf.c +1 -0
@@ -5835,6 +5835,7 @@ static int load_module_btfs(struct bpf_object *obj)
 			err = -ENOMEM;
 			goto err_out;
 		}
+		obj->btf_module_cnt++;
 		continue;

The fix looks correct. Would it make sense to add a Fixes: tag? The double close was introduced when fd tracking and the err_out error path with close(fd) were added to load_module_btfs():

Quoted 1 line
Fixes 91abb4a6d79d ("libbpf: Support attachment of BPF tracing programs to kernel modules")

right, will add

jirka

#4 lore raw
In reply to Jiri Olsa
When strdup(name) fails a few lines above this, btf_module_cnt has already been incremented and mod_btf->btf already holds the btf pointer:
Quoted 3 lines
tools/lib/bpf/libbpf.c +0 -0

[ ... ]

Quoted 7 lines
tools/lib/bpf/libbpf.c +2 -0
@@ -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);

[ ... ]

tools/lib/bpf/libbpf.c line 5844

Quoted 8 lines
tools/lib/bpf/libbpf.c +1 -0
@@ -5837,6 +5838,7 @@
 		continue;
 
 err_out:
+		btf__free(btf);
 		close(fd);
 		return err;
 	}

When strdup(name) fails a few lines above this, btf_module_cnt has already been incremented and mod_btf->btf already holds the btf pointer:

    mod_btf = &obj->btf_modules[obj->btf_module_cnt++];
    mod_btf->btf = btf;
    ...
    mod_btf->name = strdup(name);
    if (!mod_btf->name) {
        err = -ENOMEM;
        goto err_out;
    }

Can this double-free btf? The new btf__free(btf) here frees the object, but bpf_object_post_load_cleanup() will iterate btf_module_cnt entries and call btf__free(obj->btf_modules[i].btf) on the same pointer. Similarly, close(fd) here and close(obj->btf_modules[i].fd) in cleanup would double-close the fd.

This is addressed by the next patch in the series (36358f0b8f1b "libbpf: Prevent double close of btf objects") which defers the btf_module_cnt increment until after strdup succeeds.

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/24420241619

#5 lore raw
Quoted 44 lines · On Tue, Apr 14, 2026 at 08:28:59PM +0000, [email protected] wrote:
Quoted 3 lines
tools/lib/bpf/libbpf.c +0 -0

[ ... ]

Quoted 7 lines
tools/lib/bpf/libbpf.c +2 -0
@@ -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);

[ ... ]

tools/lib/bpf/libbpf.c line 5844

Quoted 8 lines
tools/lib/bpf/libbpf.c +1 -0
@@ -5837,6 +5838,7 @@
 		continue;
 
 err_out:
+		btf__free(btf);
 		close(fd);
 		return err;
 	}

When strdup(name) fails a few lines above this, btf_module_cnt has already been incremented and mod_btf->btf already holds the btf pointer:

    mod_btf = &obj->btf_modules[obj->btf_module_cnt++];
    mod_btf->btf = btf;
    ...
    mod_btf->name = strdup(name);
    if (!mod_btf->name) {
        err = -ENOMEM;
        goto err_out;
    }

Can this double-free btf? The new btf__free(btf) here frees the object, but bpf_object_post_load_cleanup() will iterate btf_module_cnt entries and call btf__free(obj->btf_modules[i].btf) on the same pointer. Similarly, close(fd) here and close(obj->btf_modules[i].fd) in cleanup would double-close the fd.

Quoted 3 lines

This is addressed by the next patch in the series (36358f0b8f1b "libbpf: Prevent double close of btf objects") which defers the btf_module_cnt increment until after strdup succeeds.

ok, I'll squash both changes in v2

jirka

Quoted 7 lines

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/24420241619