mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Haofeng Li <920484857@qq.com>
To: Quentin Monnet <qmo@kernel.org>
Cc: Alexei Starovoitov <ast@kernel.org>,
	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: [PATCH v2] bpf: fix netfilter link comparison to handle unsigned flags
Date: Mon, 22 Sep 2025 10:11:06 +0800	[thread overview]
Message-ID: <tencent_E0C8EAB7EBECF90582B0CD21490DBCF53A0A@qq.com> (raw)

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>
---
Changes in v2:
- Added conditional checks for all fields (pf, hooknum, flags) to prevent
- overflow while preserving consistent comparison style and original order.
---
 tools/bpf/bpftool/net.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/tools/bpf/bpftool/net.c b/tools/bpf/bpftool/net.c
index cfc6f944f7c3..a8caea07be8f 100644
--- a/tools/bpf/bpftool/net.c
+++ b/tools/bpf/bpftool/net.c
@@ -801,22 +801,27 @@ static int netfilter_link_compar(const void *a, const void *b)
 {
 	const struct bpf_link_info *nfa = a;
 	const struct bpf_link_info *nfb = b;
-	int delta;
 
-	delta = nfa->netfilter.pf - nfb->netfilter.pf;
-	if (delta)
-		return delta;
+	if (nfa->netfilter.pf < nfb->netfilter.pf)
+		return -1;
+	if (nfa->netfilter.pf > nfb->netfilter.pf)
+		return 1;
 
-	delta = nfa->netfilter.hooknum - nfb->netfilter.hooknum;
-	if (delta)
-		return delta;
+	if (nfa->netfilter.hooknum < nfb->netfilter.hooknum)
+		return -1;
+	if (nfa->netfilter.hooknum > nfb->netfilter.hooknum)
+		return 1;
 
 	if (nfa->netfilter.priority < nfb->netfilter.priority)
 		return -1;
 	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;
 }
 
 static void show_link_netfilter(void)
-- 
2.25.1


             reply	other threads:[~2025-09-22  2:16 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-22  2:11 Haofeng Li [this message]
2025-09-22  2:21 ` 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=tencent_E0C8EAB7EBECF90582B0CD21490DBCF53A0A@qq.com \
    --to=920484857@qq.com \
    --cc=13266079573@163.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=lihaofeng@kylinos.cn \
    --cc=linux-kernel@vger.kernel.org \
    --cc=qmo@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

all inboxes | Powered by JetHome®