Skip to content

Commit c35dce6

Browse files
random6-xyzgregkh
authored andcommitted
bpftool: Use libbpf error code for flow dissector query
[ Upstream commit 8a7f2bf ] 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: 7f0c57f ("bpftool: show flow_dissector attachment status") Signed-off-by: Woojin Ji <random6.xyz@gmail.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Acked-by: Leon Hwang <leon.hwang@linux.dev> Acked-by: Yonghong Song <yonghong.song@linux.dev> Acked-by: Quentin Monnet <qmo@kernel.org> Link: https://lore.kernel.org/bpf/20260603003339.33791-1-random6.xyz@gmail.com Assisted-by: ChatGPT:gpt-5.5 Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 7132388 commit c35dce6

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

tools/bpf/bpftool/net.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -594,14 +594,14 @@ static int query_flow_dissector(struct bpf_attach_info *attach_info)
594594
&attach_flags, prog_ids, &prog_cnt);
595595
close(fd);
596596
if (err) {
597-
if (errno == EINVAL) {
597+
if (err == -EINVAL) {
598598
/* Older kernel's don't support querying
599599
* flow dissector programs.
600600
*/
601601
errno = 0;
602602
return 0;
603603
}
604-
p_err("can't query prog: %s", strerror(errno));
604+
p_err("can't query prog: %s", strerror(-err));
605605
return -1;
606606
}
607607

0 commit comments

Comments
 (0)