Skip to contentSkip to conversation

bpftool: Skip prog/map that disappears while looking it up by name

PATCH Applied Jiayuan Chen 3 participants 4 messages Jul 20

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpftool: Skip prog/map that disappears while looking it up by name

Looking up a prog or map by name walks the whole id space. There is a window between bpf_prog_get_next_id()/bpf_map_get_next_id() and getting an fd for that id in which an unrelated object can be freed, and the lookup then fails with ENOENT and aborts the whole command.

Skip such ids and keep walking, the same way do_show() already does.

---
 tools/bpf/bpftool/common.c | 4 ++++
 1 file changed, 4 insertions(+)
tools/bpf/bpftool/common.c +4 -0
index 8bfcff9e2f63..ef366ccc9650 100644
@@ -832,6 +832,8 @@ static int prog_fd_by_nametag(void *nametag, int **fds, bool tag)
 
 		fd = bpf_prog_get_fd_by_id(id);
 		if (fd < 0) {
+			if (errno == ENOENT)
+				continue;
 			p_err("can't get prog by id (%u): %s",
 			      id, strerror(errno));
 			goto err_close_fds;
@@ -996,6 +998,8 @@ static int map_fd_by_name(char *name, int **fds,
 		opts_ro.open_flags = BPF_F_RDONLY;
 		fd = bpf_map_get_fd_by_id_opts(id, &opts_ro);
 		if (fd < 0) {
+			if (errno == ENOENT)
+				continue;
 			p_err("can't get map by id (%u): %s",
 			      id, strerror(errno));
 			goto err_close_fds;
Signature · 1 line
2.43.0
#1 lore raw
This looks like a bug fix for the prog/map lookup functions. Should this include:
Quoted 5 lines

commit 084a91fc9c087ee9f5a2163da8368590cad6f121
Author: Jiayuan Chen <[email protected]>

bpftool: Skip prog/map that disappears while looking it up by name

Quoted 8 lines

Looking up a prog or map by name walks the whole id space. There is a window between bpf_prog_get_next_id()/bpf_map_get_next_id() and getting an fd for that id in which an unrelated object can be freed, and the lookup then fails with ENOENT and aborts the whole command.

Skip such ids and keep walking, the same way do_show() already does.

This looks like a bug fix for the prog/map lookup functions. Should this include:

Fixes a479b8ce4ed1 ("tools/bpftool: Move map/prog parsing logic into common")

---
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/29725108152

#2 lore raw
Quoted 10 lines · On 7/20/26 4:02 PM, [email protected] wrote:
Quoted 10 lines

commit 084a91fc9c087ee9f5a2163da8368590cad6f121
Author: Jiayuan Chen <[email protected]>

bpftool: Skip prog/map that disappears while looking it up by name

Looking up a prog or map by name walks the whole id space. There is a window between bpf_prog_get_next_id()/bpf_map_get_next_id() and getting an fd for that id in which an unrelated object can be freed, and the lookup then fails with ENOENT and aborts the whole command.

Quoted 7 lines
Quoted 3 lines

Skip such ids and keep walking, the same way do_show() already does.

This looks like a bug fix for the prog/map lookup functions. Should this include:

Fixes a479b8ce4ed1 ("tools/bpftool: Move map/prog parsing logic into common")

It's not worth backporting it.

People can always upgrade bpftool to latest version.

#3 lore raw
In reply to Jiayuan Chen
This patch was applied to bpf/bpf-next.git (master) by Kumar Kartikeya Dwivedi <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Kumar Kartikeya Dwivedi <[email protected]>:

Quoted 8 lines · On Mon, 20 Jul 2026 15:15:18 +0800 you wrote:

Looking up a prog or map by name walks the whole id space. There is a window between bpf_prog_get_next_id()/bpf_map_get_next_id() and getting an fd for that id in which an unrelated object can be freed, and the lookup then fails with ENOENT and aborts the whole command.

Skip such ids and keep walking, the same way do_show() already does.

[...]

Here is the summary with links:
- [bpf-next,v1] bpftool: Skip prog/map that disappears while looking it up by name

    https://git.kernel.org/bpf/bpf-next/c/cfce77b63375

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html