Skip to contentSkip to conversation

bpftool: Use libbpf error code for flow dissector query

PATCH Superseded Woojin Ji 5 participants 9 messages Jun 2 – Jun 3

Subsystems (2)
lore
Share & tools
lore

Woojin Ji

author
#0 lore raw

PATCH 1/1 bpftool: Use libbpf error code for flow dissector query

bpf_prog_query() returns a negative errno on failure. query_flow_dissector() currently closes the namespace fd and then reads errno to decide whether -EINVAL means that the running kernel does not support flow dissector queries.

That errno check controls behavior, not just diagnostics: -EINVAL is handled as a non-fatal old-kernel case, while any other error makes bpftool net fail. Reading errno after close() is fragile, because close() can overwrite errno before the check. Use the libbpf-returned error code instead so the compatibility branch is based on the BPF_PROG_QUERY result itself.

Keep the existing errno reset in the non-fatal path to preserve batch mode behavior. The success path is unchanged.

Fixes 7f0c57fec80f ("bpftool: show flow_dissector attachment status")
Assisted-by ChatGPT:gpt-5.5
---
 tools/bpf/bpftool/net.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/bpf/bpftool/net.c +2 -2
index 974189da8a91..dba28755d284 100644
@@ -603,14 +603,14 @@ static int query_flow_dissector(struct bpf_attach_info *attach_info)
 			     &attach_flags, prog_ids, &prog_cnt);
 	close(fd);
 	if (err) {
-		if (errno == EINVAL) {
+		if (err == -EINVAL) {
 			/* Older kernel's don't support querying
 			 * flow dissector programs.
 			 */
 			errno = 0;
 			return 0;
 		}
-		p_err("can't query prog: %s", strerror(errno));
+		p_err("can't query prog: %s", strerror(-err));
 		return -1;
 	}
 
Signature · 1 line
2.54.0
reviewer
#1 lore raw
Quoted 8 lines · Woojin Ji

bpf_prog_query() returns a negative errno on failure. query_flow_dissector() currently closes the namespace fd and then reads errno to decide whether -EINVAL means that the running kernel does not support flow dissector queries.

That errno check controls behavior, not just diagnostics: -EINVAL is handled as a non-fatal old-kernel case, while any other error makes bpftool net fail. Reading errno after close() is fragile, because close() can overwrite errno before the check. Use the libbpf-returned error code instead so the

Do you have evidence that close() is fragile?

Quoted 33 lines

compatibility branch is based on the BPF_PROG_QUERY result itself.

Keep the existing errno reset in the non-fatal path to preserve batch mode behavior. The success path is unchanged.

Fixes 7f0c57fec80f ("bpftool: show flow_dissector attachment status")
Assisted-by ChatGPT:gpt-5.5
---
  tools/bpf/bpftool/net.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
tools/bpf/bpftool/net.c +2 -2
index 974189da8a91..dba28755d284 100644
@@ -603,14 +603,14 @@ static int query_flow_dissector(struct bpf_attach_info *attach_info)
  			     &attach_flags, prog_ids, &prog_cnt);
  	close(fd);
  	if (err) {
-		if (errno == EINVAL) {
+		if (err == -EINVAL) {
  			/* Older kernel's don't support querying
  			 * flow dissector programs.
  			 */
  			errno = 0;
  			return 0;
  		}
-		p_err("can't query prog: %s", strerror(errno));
+		p_err("can't query prog: %s", strerror(-err));
  		return -1;
  	}
 
reviewer
#2 lore raw
Quoted 8 lines · Yonghong Song
Quoted 5 lines · Woojin Ji

bpf_prog_query() returns a negative errno on failure. query_flow_dissector() currently closes the namespace fd and then reads errno to decide whether -EINVAL means that the running kernel does not support flow dissector queries.

Quoted 6 lines
Quoted 4 lines

That errno check controls behavior, not just diagnostics: -EINVAL is handled as a non-fatal old-kernel case, while any other error makes bpftool net fail. Reading errno after close() is fragile, because close() can overwrite errno before the check. Use the libbpf-returned error code instead so the

Do you have evidence that close() is fragile?

The patch itself looks good to me. But it would be great in commit message to answer why. Note that fd is readonly: fd = open("/proc/self/ns/net", O_RDONLY).

Quoted 36 lines
Quoted 34 lines

compatibility branch is based on the BPF_PROG_QUERY result itself.

Keep the existing errno reset in the non-fatal path to preserve batch mode behavior. The success path is unchanged.

Fixes 7f0c57fec80f ("bpftool: show flow_dissector attachment status")
Assisted-by ChatGPT:gpt-5.5

---
  tools/bpf/bpftool/net.c | 4 ++--

  1 file changed, 2 insertions(+), 2 deletions(-)
tools/bpf/bpftool/net.c +2 -2
index 974189da8a91..dba28755d284 100644
@@ -603,14 +603,14 @@ static int query_flow_dissector(struct 
 bpf_attach_info *attach_info)
                    &attach_flags, prog_ids, &prog_cnt);
       close(fd);
       if (err) {
-        if (errno == EINVAL) {
+        if (err == -EINVAL) {
               /* Older kernel's don't support querying
                * flow dissector programs.
                */
               errno = 0;
               return 0;
           }
-        p_err("can't query prog: %s", strerror(errno));
+        p_err("can't query prog: %s", strerror(-err));
           return -1;
       }
author
#3 lore raw

Thanks for taking a look.

Yes, the fd is opened read-only, so I do not mean to claim that close() on /proc/self/ns/net commonly fails in normal use, or that delayed writeback errors are expected here.

The fragile part is that the BPF_PROG_QUERY error is consumed only after an intervening close(), even though bpf_prog_query() has already returned the negative errno in err. If close() changes errno, the old-kernel -EINVAL compatibility case can be missed.

I reproduced this with an LD_PRELOAD fault injector that forced BPF_PROG_QUERY for BPF_FLOW_DISSECTOR to fail with EINVAL, and then forced close() on the netns fd to fail with EIO. The unpatched bpftool reported:

Error: can't query prog: Input/output error

With this patch, the same injected failure is handled as the intended non-fatal EINVAL compatibility case.

I'll send a v2 with the commit message updated to make this clearer. The code change itself is unchanged.

author
#4 lore raw

PATCH v2 1/1 bpftool: Use libbpf error code for flow dissector query

In reply to Yonghong Song

bpf_prog_query() returns a negative errno on failure. query_flow_dissector() currently closes the namespace fd and then reads errno to decide whether -EINVAL means that the running kernel does not support flow dissector queries.

That errno check controls behavior, not just diagnostics: -EINVAL is handled as a non-fatal old-kernel case, while any other error makes bpftool net fail.

The namespace fd is opened read-only, so close() is not expected to commonly fail in normal use. Still, the BPF_PROG_QUERY error is already available in err, and reading errno after an intervening close() is fragile. If close() does change errno, the compatibility branch may be based on close()'s error instead of the BPF_PROG_QUERY result.

This was reproduced with an LD_PRELOAD fault injector that forced BPF_PROG_QUERY for BPF_FLOW_DISSECTOR to fail with EINVAL and then forced close() on the netns fd to fail with EIO. The unpatched bpftool reported "can't query prog: Input/output error". With this change, the same injected failure is handled as the intended non-fatal EINVAL compatibility case.

Use the libbpf-returned error code instead. Keep the existing errno reset in the non-fatal path to preserve batch mode behavior. The success path is unchanged.

Fixes 7f0c57fec80f ("bpftool: show flow_dissector attachment status")
Assisted-by ChatGPT:gpt-5.5

---
v2:
- Expand the commit message to explain why relying on errno after close() is fragile for this error path.
- Mention that the netns fd is read-only and that close() failure is not expected to be common in normal use.
- Add the LD_PRELOAD reproduction result.
- No code changes.

 tools/bpf/bpftool/net.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/bpf/bpftool/net.c +2 -2
index 974189da8a91..dba28755d284 100644
@@ -603,14 +603,14 @@ static int query_flow_dissector(struct bpf_attach_info *attach_info)
 			     &attach_flags, prog_ids, &prog_cnt);
 	close(fd);
 	if (err) {
-		if (errno == EINVAL) {
+		if (err == -EINVAL) {
 			/* Older kernel's don't support querying
 			 * flow dissector programs.
 			 */
 			errno = 0;
 			return 0;
 		}
-		p_err("can't query prog: %s", strerror(errno));
+		p_err("can't query prog: %s", strerror(-err));
 		return -1;
 	}
 
Signature · 1 line
2.54.0
Acked-by on v2 1/1
#5 lore raw
Quoted 22 lines · Woojin Ji

bpf_prog_query() returns a negative errno on failure. query_flow_dissector() currently closes the namespace fd and then reads errno to decide whether -EINVAL means that the running kernel does not support flow dissector queries.

That errno check controls behavior, not just diagnostics: -EINVAL is handled as a non-fatal old-kernel case, while any other error makes bpftool net fail.

The namespace fd is opened read-only, so close() is not expected to commonly fail in normal use. Still, the BPF_PROG_QUERY error is already available in err, and reading errno after an intervening close() is fragile. If close() does change errno, the compatibility branch may be based on close()'s error instead of the BPF_PROG_QUERY result.

This was reproduced with an LD_PRELOAD fault injector that forced BPF_PROG_QUERY for BPF_FLOW_DISSECTOR to fail with EINVAL and then forced close() on the netns fd to fail with EIO. The unpatched bpftool reported "can't query prog: Input/output error". With this change, the same injected failure is handled as the intended non-fatal EINVAL compatibility case.

Quoted 8 lines

Use the libbpf-returned error code instead. Keep the existing errno reset in the non-fatal path to preserve batch mode behavior. The success path is unchanged.

Fixes 7f0c57fec80f ("bpftool: show flow_dissector attachment status")
Assisted-by ChatGPT:gpt-5.5

---

The commit message seems verbose.

Other than that:

[...]

reviewer Acked-by on v2 1/1
#6 lore raw
In reply to Woojin Ji
Quoted 22 lines · Woojin Ji

bpf_prog_query() returns a negative errno on failure. query_flow_dissector() currently closes the namespace fd and then reads errno to decide whether -EINVAL means that the running kernel does not support flow dissector queries.

That errno check controls behavior, not just diagnostics: -EINVAL is handled as a non-fatal old-kernel case, while any other error makes bpftool net fail.

The namespace fd is opened read-only, so close() is not expected to commonly fail in normal use. Still, the BPF_PROG_QUERY error is already available in err, and reading errno after an intervening close() is fragile. If close() does change errno, the compatibility branch may be based on close()'s error instead of the BPF_PROG_QUERY result.

This was reproduced with an LD_PRELOAD fault injector that forced BPF_PROG_QUERY for BPF_FLOW_DISSECTOR to fail with EINVAL and then forced close() on the netns fd to fail with EIO. The unpatched bpftool reported "can't query prog: Input/output error". With this change, the same injected failure is handled as the intended non-fatal EINVAL compatibility case.

Quoted 7 lines

Use the libbpf-returned error code instead. Keep the existing errno reset in the non-fatal path to preserve batch mode behavior. The success path is unchanged.

Fixes 7f0c57fec80f ("bpftool: show flow_dissector attachment status")
Assisted-by ChatGPT:gpt-5.5
maintainer Acked-by
#7 lore raw

2026-06-02 20:01 UTC-0700 ~ Yonghong Song <[email protected]>

Quoted 30 lines · Woojin Ji
Quoted 27 lines

bpf_prog_query() returns a negative errno on failure. query_flow_dissector() currently closes the namespace fd and then reads errno to decide whether -EINVAL means that the running kernel does not support flow dissector queries.

That errno check controls behavior, not just diagnostics: -EINVAL is handled as a non-fatal old-kernel case, while any other error makes bpftool
net fail.

The namespace fd is opened read-only, so close() is not expected to commonly fail in normal use. Still, the BPF_PROG_QUERY error is already available in err, and reading errno after an intervening close() is fragile. If close() does change errno, the compatibility branch may be based on close()'s error instead of the BPF_PROG_QUERY result.

This was reproduced with an LD_PRELOAD fault injector that forced BPF_PROG_QUERY for BPF_FLOW_DISSECTOR to fail with EINVAL and then forced close() on the netns fd to fail with EIO. The unpatched bpftool reported "can't query prog: Input/output error". With this change, the same injected failure is handled as the intended non-fatal EINVAL compatibility case.

Use the libbpf-returned error code instead. Keep the existing errno reset in the non-fatal path to preserve batch mode behavior. The success path is unchanged.

Quoted 6 lines
Quoted 3 lines
Fixes 7f0c57fec80f ("bpftool: show flow_dissector attachment status")
Assisted-by ChatGPT:gpt-5.5

Thanks for the fix

#8 lore raw
In reply to Woojin Ji
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Hello:

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

Quoted 10 lines · On Wed, 3 Jun 2026 09:33:39 +0900 you wrote:

bpf_prog_query() returns a negative errno on failure. query_flow_dissector() currently closes the namespace fd and then reads errno to decide whether -EINVAL means that the running kernel does not support flow dissector queries.

That errno check controls behavior, not just diagnostics: -EINVAL is handled as a non-fatal old-kernel case, while any other error makes bpftool net fail.

[...]

Here is the summary with links:
- [bpf,v2] bpftool: Use libbpf error code for flow dissector query

    https://git.kernel.org/bpf/bpf-next/c/8a7f2bff2165

You are awesome, thank you!

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