Article View: linux.kernel.netdev
Article #21785[PATCH net-next v1 2/6] ipv4: avoid a stack canary in ip_finish_output2()
From: Xuanqiang Luo
Date: Tue, 24 Nov 2026 13:06
Date: Tue, 24 Nov 2026 13:06
160 lines
14918 bytes
14918 bytes
From: Xuanqiang Luo <luoxuanqiang@kylinos.cn>
ip_neigh_gw4() passes the address of its by-value @daddr argument to
__neigh_create() when the lockless lookup finds no neighbour.
Once inlined, this forces a stack canary (CONFIG_STACKPROTECTOR_STRONG=y)
into its callers, ip_finish_output2() being the one in the transmit path
of every IPv4 packet, although creating a neighbour is rare compared to
finding one.
Change ip_neigh_gw4() to take a pointer to the address, like
ip_neigh_gw6(). All callers have the address in the route, in the packet
or in another object that is not on their stack.
Call it only once in ip_neigh_for_gw() and ipv4_dst_neigh_lookup(), so
that the lookup and the creation are no longer inlined twice there.
Change the type of ipv4_nh in struct bpf_nh_params to __be32, since its
address is now given to ip_neigh_gw4().
Note that skb_do_redirect() and nf_flow_offload_ip_hook() still need a
canary for other reasons.
$ scripts/bloat-o-meter -t vmlinux.old vmlinux.new
add/remove: 0/0 grow/shrink: 1/5 up/down: 3/-846 (-843)
Function old new delta
nf_flow_offload_ip_hook 2065 2068 +3
ip_do_fragment 1441 1425 -16
ipv4_dst_neigh_lookup 684 587 -97
skb_do_redirect 4382 4222 -160
vrf_finish_output 1614 1345 -269
ip_finish_output2 1775 1471 -304
Total: Before=24068916, After=24068073, chg -0.00%
Signed-off-by: Xuanqiang Luo <luoxuanqiang@kylinos.cn>
---
include/linux/filter.h | 2 +-
include/net/route.h | 16 ++++++++--------
net/core/filter.c | 2 +-
net/ipv4/route.c | 15 +++++++--------
net/netfilter/nf_flow_table_ip.c | 4 +++-
5 files changed, 20 insertions(+), 19 deletions(-)
diff --git a/include/linux/filter.h b/include/linux/filter.h
index 4f0cd67caccb..e258ba4741af 100644
--- a/include/linux/filter.h
+++ b/include/linux/filter.h
@@ -871,7 +871,7 @@ struct bpf_skb_data_end {
struct bpf_nh_params {
u32 nh_family;
union {
- u32 ipv4_nh;
+ __be32 ipv4_nh;
struct in6_addr ipv6_nh;
};
};
diff --git a/include/net/route.h b/include/net/route.h
index 3fccd31eb74d..1571e1c6082c 100644
--- a/include/net/route.h
+++ b/include/net/route.h
@@ -406,15 +406,15 @@ static inline int ip4_dst_hoplimit(const struct dst_entry *dst)
}
static inline struct neighbour *ip_neigh_gw4(struct net_device *dev,
- __be32 daddr)
+ const __be32 *daddr)
{
struct neighbour *neigh;
- neigh = __ipv4_neigh_lookup_noref(dev, (__force u32)daddr);
+ neigh = __ipv4_neigh_lookup_noref(dev, (__force u32)*daddr);
if (unlikely(!neigh)) {
struct neigh_table *tbl = arp_table(dev_net(dev));
- neigh = __neigh_create(tbl, &daddr, dev, false);
+ neigh = __neigh_create(tbl, daddr, dev, false);
}
return neigh;
@@ -425,17 +425,17 @@ static inline struct neighbour *ip_neigh_for_gw(struct rtable *rt,
bool *is_v6gw)
{
struct net_device *dev = rt->dst.dev;
- struct neighbour *neigh;
+ const __be32 *daddr;
if (likely(rt->rt_gw_family == AF_INET)) {
- neigh = ip_neigh_gw4(dev, rt->rt_gw4);
+ daddr = &rt->rt_gw4;
} else if (rt->rt_gw_family == AF_INET6) {
- neigh = ip_neigh_gw6(dev, &rt->rt_gw6);
*is_v6gw = true;
+ return ip_neigh_gw6(dev, &rt->rt_gw6);
} else {
- neigh = ip_neigh_gw4(dev, ip_hdr(skb)->daddr);
+ daddr = &ip_hdr(skb)->daddr;
}
- return neigh;
+ return ip_neigh_gw4(dev, daddr);
}
#endif /* _ROUTE_H */
diff --git a/net/core/filter.c b/net/core/filter.c
index f42ecfb5841f..8ee023c87c24 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -2376,7 +2376,7 @@ static int bpf_out_neigh_v4(struct net *net, struct sk_buff *skb,
neigh = ip_neigh_gw6(dev, &nh->ipv6_nh);
is_v6gw = true;
} else if (nh->nh_family == AF_INET) {
- neigh = ip_neigh_gw4(dev, nh->ipv4_nh);
+ neigh = ip_neigh_gw4(dev, &nh->ipv4_nh);
} else {
rcu_read_unlock();
goto out_drop;
diff --git a/net/ipv4/route.c b/net/ipv4/route.c
index d0930ae4c5ae..585c10743e1b 100644
--- a/net/ipv4/route.c
+++ b/net/ipv4/route.c
@@ -419,15 +419,14 @@ static struct neighbour *ipv4_dst_neigh_lookup(const struct dst_entry *dst,
rcu_read_lock();
dev = dst_dev_rcu(dst);
- if (likely(rt->rt_gw_family == AF_INET)) {
- n = ip_neigh_gw4(dev, rt->rt_gw4);
- } else if (rt->rt_gw_family == AF_INET6) {
+ if (unlikely(rt->rt_gw_family == AF_INET6)) {
n = ip_neigh_gw6(dev, &rt->rt_gw6);
- } else {
- __be32 pkey;
-
- pkey = skb ? ip_hdr(skb)->daddr : *((__be32 *) daddr);
- n = ip_neigh_gw4(dev, pkey);
+ } else {
+ if (likely(rt->rt_gw_family == AF_INET))
+ daddr = &rt->rt_gw4;
+ else if (skb)
+ daddr = &ip_hdr(skb)->daddr;
+ n = ip_neigh_gw4(dev, daddr);
}
if (!IS_ERR(n) && !refcount_inc_not_zero(&n->refcnt))
diff --git a/net/netfilter/nf_flow_table_ip.c b/net/netfilter/nf_flow_table_ip.c
index 78bba416dd5b..19f62005b517 100644
--- a/net/netfilter/nf_flow_table_ip.c
+++ b/net/netfilter/nf_flow_table_ip.c
@@ -872,7 +872,9 @@ static int nf_flow_queue_xmit4(struct sk_buff *skb,
flow_offload_teardown(flow);
return NF_DROP;
}
- neigh = ip_neigh_gw4(rt->dst.dev, rt_nexthop(rt, ip_addr->ip));
+ neigh = ip_neigh_gw4(rt->dst.dev,
+ rt->rt_gw_family == AF_INET ?
+ &rt->rt_gw4 : &ip_addr->ip);
if (IS_ERR(neigh)) {
flow_offload_teardown(flow);
return NF_DROP;
--
2.43.0
Technical Details
Message-ID:
<20261124130631.1886963-3-xuanqiang.luo@linux.dev>Path:
mbox-mo1.pugleaf.net!srv.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-370369-linux-kernel-netdev=dewy.ruReferences:
<20261124130631.1886963-1-xuanqiang.luo@linux.dev>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-370369-linux-kernel-netdev=dewy.ru From: xuanqiang.luo@linux.dev (Xuanqiang Luo) Newsgroups: linux.kernel.netdev Subject: [PATCH net-next v1 2/6] ipv4: avoid a stack canary in ip_finish_output2() Date: Tue, 24 Nov 2026 22:06:27 +0900 Organization: Usenet Network Message-ID: <20261124130631.1886963-3-xuanqiang.luo@linux.dev> References: <20261124130631.1886963-1-xuanqiang.luo@linux.dev> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Injection-Info: usenet.network; logging-data="4140482"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:uFaWl8MP3+r94Y3jZPL35aCMGN4= sha256:dFSdcPwNtgR9vC4TlyP42YsGDmWW/7B/fmMzFoAfSR4= sha1:sIW/GVqWbRsDmemxMRuXBRN5JQQ= sha256:k4gq3Tc8EVReKL++S/B8JcNuR5wxdvBEktzKHh7zNeY= List-ID: <netdev.vger.kernel.org> X-Mailpost-Empty-Hdrs: X-Spam-Level X-Received: from localhost (localhost [127.0.0.1]) by mail.dewy.ru (Postfix) with ESMTP id EAE06125E15; Tue, 24 Nov 2026 16:08:00 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -3.05 X-Spam-Status: No, score=-3.05 tagged_above=-9999 required=6.31 tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (1024-bit key) header.d=linux.dev X-Received: from mail.dewy.ru ([127.0.0.1]) by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id KXcS1I93XPjJ; Tue, 24 Nov 2026 16:07:57 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-370369-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=linux.dev X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10]) by mail.dewy.ru (Postfix) with ESMTPS id 64402125E13; Tue, 24 Nov 2026 16:07:49 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by sea.lore.kernel.org (Postfix) with ESMTP id 2DA193B46F; Tue, 24 Nov 2026 13:07:46 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795525665; b=Ggoq7i8g/NqIbs15ib4lXHFoxIAlY8UY97EAlGqK6h/HhRn3VTcwbhzoXl/7GRmoWaN0 E6rUgIWu9BMmD+UrpA0dRYD3cFYJ6nGGUB3Yu0wb4Mr4oDAvbRpoHlAnYawVU7E8+LQly jz6QKrV2OBw4KF2AIL9Pp7/tEp3/IVbzlneijZx5Dkb1qYz6u9nCXP2z3T652TorGYNmA a3aPKKEi5vS9pYGSsTwQ4Iic12pZxcemEILZPxUghaL6uVVX3FTqkDcBb3De33cSPwsuY V4SA8YCi5N0wT8IuwiZdkC3eomnBi+BSFZUtk/VLldpjgZ0gtX8ZvifpSte7qrr7mYw== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795525665; h=X-Original-To:Received:DMARC-Filter:X-Envelope-To:DKIM-Signature: X-Envelope-To:Received:X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc: Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe: List-Unsubscribe:List-Owner:Precedence:MIME-Version: Content-Transfer-Encoding; bh=fEuuayYjW4XK27k8r55+0VUrC6Hs/dGSwkUdlWW+LZE=; b=fLYf80AXVS3C5vN56EAb0+7aP9ZPwK5dlxGAq1XskLXM9AvvwNu/TeB0aUGabzHziq9i K7ICVBGkiURJW80reHA6JJgrx+SZqbzaOmPTSx3oJFfeY0AD6rfD8MecMB/U04C2HKuD2 eAbIXlMYs1weuj1VXkmOEu5lrtK26Tl9YZV5OUiy0NCv/hp8EHYA32qZzJc+Au73mjQQs 0/zI8QiPk5LZ2woIxyphNe2UMouRPjQr3fMmt1NREpz88bqRn7Wl1hn5rIOGPuT6CDgsp pC1hcR/nVXk1m2fG2oroAlCUFffySGKRDrXL7GLqD03jhAxl95VjjURO3Is8yTVmyAw== X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=Xi68m6lQ; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4EB04606A33A; Tue, 24 Nov 2026 13:07:45 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=Xi68m6lQ X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.83 X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795525664; b=zT///nTZG4+iecAqNoFLmunGB6ezKA5Qb0yJX+Gub//e6LZLjPx7m3vL2MtzMvri2KRW rUX3NMRrAKgoo9adMqz1/BFsJQjXEu7e8DmI5Z+Tnd4rgfM3p0mX6H9fgCXXjUpSXkgd6 nWpV1fKx4+DHDb8zKcTWgbO9Sq8/YfwFb0kyiD97JvLUY/mO08Lkm+slLJGadgbJ60Xqe ARWDDDFpOZ3AcTJKqBBnnAb+b7nG7KBkxXjw7420m9HfRnYfS84FqMNBgPK2qvNTJFep2 dMZdlXpQRWpm/f5W/ftT+aUVJLeR2Q6LOr2y1eE01xhBlb9ZQJv8tyGhYVnF+VY5Eqg== X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795525664; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=fEuuayYjW4XK27k8r55+0VUrC6Hs/dGSwkUdlWW+LZE=; b=g5sW75UruCo3/pWmEl63R69xxUafiFteVspTCnsVBevgM2UhNtZM/UTrjjFjUMS3ez6Q xFI4qpAbTRpP4QLBYFfCh7we+jA/26L1G5nQzSG7X/KDBYjs6+jvA8mDWX62R4kq2uSER qPgOiHvQNHI0J1bsZVAvY/7eAUDKmBxgsLePrKuualZdt3sz+wZpU2u/sE1S3QAply4/0 Bp5ADNqllfuutPc6FB/kJ/dQ8aVBfyogYjBaImNNm7xEoyjrWRRfafrYXk8+e5NHkcARZ XDuy/y3HnXiVfZsZDq68tgNdZ0B5bFc1aBDeQpqIbC8n7GJM7BkWrZOveCHMsVMPArQ== X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=Xi68m6lQ; arc=none smtp.remote-ip=91.218.175.83 X-Received: from mta0.migadu.com (out-83.mta0.migadu.com [91.218.175.83]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D1583606A326 for <netdev@vger.kernel.org>; Tue, 24 Nov 2026 13:07:43 +0000 (UTC) X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D1583606A326 X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev X-Envelope-To: netdev@vger.kernel.org X-DKIM-Signature: a=rsa-sha256; bh=lVCqhItCiIoTTyh8Q0RjFysA+d8zHM9N0SL1I0gqsaw=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795525661; v=1; x=1796130461; b=Xi68m6lQUSHdNMDO+P4VgY83OvfQHbLGw2USCjdd7r6VMF0//ep1QADwsKDPGt3kVlj7Gyo9 UGogOBo9kpRojrZEzt7njcQeSjsqCzasELJDjz3/zyupd/HFXpzc7w4pEVdtRux/JYFGS1jU+Tv gcWU0yPYYKB7IQ1YOznremUA= X-Envelope-To: netdev@vger.kernel.org X-Received: by smtp.migadu.com with ESMTPS id a0f3c9bae6bbb747; Tue, 24 Nov 2026 13:07:41 +0000 X-Mizu-Trace-ID: a0f3c9bae6bbb747 X-Migadu-Flow: FLOW_OUT X-Cc: "David S . Miller" <davem@davemloft.net>, Eric Dumazet <edumazet@kernel.org>, Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>, Simon Horman <horms@kernel.org>, Alexei Starovoitov <ast@kernel.org>, Daniel Borkmann <daniel@iogearbox.net>, John Fastabend <john.fastabend@gmail.com>, Andrii Nakryiko <andrii@kernel.org>, Eduard Zingerman <eddyz87@gmail.com>, Kumar Kartikeya Dwivedi <memxor@gmail.com>, Martin KaFai Lau <martin.lau@linux.dev>, Song Liu <song@kernel.org>, Yonghong Song <yonghong.song@linux.dev>, Jiri Olsa <jolsa@kernel.org>, Emil Tsalapatis <emil@etsalapatis.com>, Ihor Solodrai <ihor.solodrai@linux.dev>, Matthieu Baerts <matttbe@kernel.org>, Mat Martineau <martineau@kernel.org>, Geliang Tang <geliang@kernel.org>, David Ahern <dsahern@kernel.org>, Ido Schimmel <idosch@nvidia.com>, Stanislav Fomichev <sdf@fomichev.me>, Neal Cardwell <ncardwell@google.com>, Kuniyuki Iwashima <kuniyu@google.com>, Pablo Neira Ayuso <pablo@netfilter.org>, Florian Westphal <fw@strlen.de>, Phil Sutter <phil@nwl.cc>, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, mptcp@lists.linux.dev, netfilter-devel@vger.kernel.org, coreteam@netfilter.org, Xuanqiang Luo <luoxuanqiang@kylinos.cn> X-Mailer: git-send-email 2.43.0 X-In-Reply-To: <20261124130631.1886963-1-xuanqiang.luo@linux.dev> X-Mailing-List: netdev@vger.kernel.org X-List-Post: <mailto:netdev@vger.kernel.org> X-List-Help: <mailto:netdev+help@vger.kernel.org> X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org> X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org> X-List-Owner: <mailto:netdev+owner@vger.kernel.org> X-Precedence: list