From: Quentin Monnet <qmo@kernel.org>
To: Haofeng Li <920484857@qq.com>, Alexei Starovoitov <ast@kernel.org>
Cc: Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
linux-kernel@vger.kernel.org, Haofeng Li <13266079573@163.com>,
lihaofeng <lihaofeng@kylinos.cn>
Subject: Re: [PATCH] bpf: fix netfilter link comparison to handle unsigned flags
Date: Sat, 20 Sep 2025 01:22:11 +0100 [thread overview]
Message-ID: <72f0108c-ce6c-4f89-b04d-2398d7c808a5@kernel.org> (raw)
In-Reply-To: <tencent_9196B487ED0904A6B2F921A50EECF4D6CB0A@qq.com>
2025-09-19 17:04 UTC+0800 ~ Haofeng Li <920484857@qq.com>
> From: lihaofeng <lihaofeng@kylinos.cn>
>
> The original implementation of netfilter_link_compar() used subtraction
> to compare the netfilter.flags field, which is an unsigned type.
> This could result in incorrect comparison results when the unsigned
> value wrapped around due to underflow.
>
> Changed the comparison logic for flags to use explicit conditional
> checks (similar to how priority is handled) instead of subtraction,
> ensuring correct negative/zero/positive return values regardless of
> the underlying data type.
>
> This fixes potential sorting issues when using this comparison function
> with algorithms like qsort() or bsearch().
>
> Signed-off-by: lihaofeng <lihaofeng@kylinos.cn>
> ---
> tools/bpf/bpftool/net.c | 6 +++++-
> 1 file changed, 5 insertions(+), 1 deletion(-)
>
> diff --git a/tools/bpf/bpftool/net.c b/tools/bpf/bpftool/net.c
> index cfc6f944f7c3..9f840821beda 100644
> --- a/tools/bpf/bpftool/net.c
> +++ b/tools/bpf/bpftool/net.c
> @@ -816,7 +816,11 @@ static int netfilter_link_compar(const void *a, const void *b)
> if (nfa->netfilter.priority > nfb->netfilter.priority)
> return 1;
>
> - return nfa->netfilter.flags - nfb->netfilter.flags;
> + if (nfa->netfilter.flags < nfb->netfilter.flags)
> + return -1;
> + if (nfa->netfilter.flags > nfb->netfilter.flags)
> + return 1;
> + return 0;
> }
>
Thanks! Did you actually observe an overflow producing an error when
sorting, here? Or did you run into some compiler warning? If I'm not
mistaken you'd need the difference between flags for nfa and nfb to be
bigger than 1 << 31 for the sign to reverse. As far as I can tell, the
netfilter.flags can be 0 or 1, so this is unlikely to happen. I note
that fields netfilter.pf and netfilter.hooknum are processed in a
similar way earlier in the function, with the types allowing overflows
but possible values preventing the comparison to reverse in practice.
This being said, I don't mind making the code cleaner for these
comparisons, but we should probably treat all three attributes the same,
and update the rest of the function as well?
Quentin
next prev parent reply other threads:[~2025-09-20 0:22 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-19 9:04 [PATCH] bpf: fix netfilter link comparison to handle unsigned flags Haofeng Li
2025-09-20 0:22 ` Quentin Monnet [this message]
2025-09-22 1:45 ` Haofeng Li
2025-09-22 2:20 ` Alexei Starovoitov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=72f0108c-ce6c-4f89-b04d-2398d7c808a5@kernel.org \
--to=qmo@kernel.org \
--cc=13266079573@163.com \
--cc=920484857@qq.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=daniel@iogearbox.net \
--cc=lihaofeng@kylinos.cn \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox