Netfilter/IPVS fixes for net
Subsystems (6)
Pablo Neira Ayuso
maintainerv2 cover Netfilter/IPVS fixes for net
v2: remove kdoc update in nf_conntrack_expect.
anything else, just like v1.
-o-
Hi,
The following batch contains Netfilter/IPVS fixes for net, this batch addresses LLM findings, several of these fixes has been reproduced by syzbot:
1) Add recursion count to nf_reject infrastructure to prevent stack overflow, from Fernando F. Mancera.
2) Address a infinite loop in RCU list traversal with the TWOS IPVS scheduler, from Darong Lu and Julian Anastasov.
3) Use kfree_rcu() to release IPVS parent application to fix UaF, from Chengfeng Ye.
4) Fix UaF in IPVS netns removal path due to releasing per-net protocol data too early, from Chengfeng Ye.
5) Fix unbounded growth of stale, unmatchable IPVS connection entries, from Julian Anastasov.
6) Restrict maximum master conntrack chain depth in ctnetlink, from Florian Westphal.
7) Preserve recursion count on nfqueue reinjection, from Zihan Xi.
8) Restore GRE timeout in the flowtable, otherwise, WARN_ON_ONCE() is reachable, from Michail Andreas Michalopoulos.
9) Check non-null gc transaction in nft_rbtree before queuing it for processing, from Xingyuan Mo.
10) Replace WARN_ON_ONCE() by DEBUG_NET_WARN_ON_ONCE() in nf_conntrack_bridge which is reachable if the protocol of the tag of a frame differs from the vlan_protocol of the bridge. From Laveesh Bansal.
11) Check x_tables target name is non-null before accessing it when building a new table, from Fernando F. Mancera.
12) Fix out-of-bound memory read in nf_conntrack_sip, from Joas Antonio Dos Santos.
13) Fix a memleak in nf_tables hardware offload when double deleting the same rule, in the same batch.
14) Use a mutex in xt_recent netns exit path instead of spinlock, since proc unregistration might sleep. From Cen Zhang.
15) Zero the nf_nat_range2 struct before filling it up, to avoid using stale uninitialized stack data to configure NAT with an esoteric NAT flags combination. From Florian Westphal.
Please, pull these changes from:
git://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf.git nf-26-11-03
Thanks.
----------------------------------------------------------------
The following changes since commit cb284914e749388eef402938bdb3115fe1f6d337:
net: ipa: Fix double free of the interrupt structure on config error (2026-10-30 19:04:37 -0700)
are available in the Git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf.git nf-26-11-03
for you to fetch changes up to 3d234337bbb1e32fab38e4e30da72edc5ddc241f:
netfilter: nat: zero newrange before filling it in (2026-11-03 14:50:15 +0100)
----------------------------------------------------------------
netfilter pull request 26-11-03
----------------------------------------------------------------
Cen Zhang (Microsoft) (1):
netfilter: xt_recent: do not call remove_proc_entry() under recent_lock
Chengfeng Ye (2):
ipvs: Defer application parent freeing until after RCU readers
ipvs: fix protocol data lifetime during namespace teardown
Darong Lu (1):
ipvs: wait grace period while reusing dest from trash
Fernando Fernandez Mancera (2):
netfilter: nft_reject: prevent unbounded recursion in output hooks
netfilter: x_tables: check target name before accessing verdict
Florian Westphal (2):
netfilter: nf_conntrack: limit expectation master chain depth
netfilter: nat: zero newrange before filling it in
Joas Antonio dos Santos (1):
netfilter: nf_conntrack_sip: fix OOB read in ct_sip_parse_transport()
Julian Anastasov (1):
ipvs: do not create conns from packets with port 0
Laveesh Bansal (1):
netfilter: bridge: use DEBUG_NET_WARN_ON_ONCE in nf_ct_bridge_refrag()
Michail Andreas Michalopoulos (1):
netfilter: flowtable: extend ct timeout of offloaded GRE flows
Pablo Neira Ayuso (1):
netfilter: nf_tables: fix memleak when rule deletion fails with hardware offload
Xingyuan Mo (1):
netfilter: nft_set_rbtree: check catchall_sync return before queue_done
Zihan Xi (1):
netfilter: nf_dup: preserve guard across NFQUEUE reinjection
include/linux/netdevice_xmit.h | 4 +- include/net/ip_vs.h | 4 +- include/net/netfilter/nf_conntrack_helper.h | 15 ++++ include/net/netfilter/nf_dup_netdev.h | 35 --------- include/net/netfilter/nf_queue.h | 1 + include/net/netfilter/nf_recursion.h | 42 +++++++++++ net/bridge/netfilter/nf_conntrack_bridge.c | 2 +- net/ipv4/netfilter/arp_tables.c | 5 +- net/ipv4/netfilter/ip_tables.c | 5 +- net/ipv4/netfilter/nf_reject_ipv4.c | 12 +++ net/ipv6/netfilter/ip6_tables.c | 5 +- net/ipv6/netfilter/nf_reject_ipv6.c | 13 ++++ net/netfilter/ipvs/ip_vs_app.c | 2 +- net/netfilter/ipvs/ip_vs_core.c | 15 ++-- net/netfilter/ipvs/ip_vs_ctl.c | 109 ++++++++++++++++++++++------ net/netfilter/ipvs/ip_vs_proto_sctp.c | 5 ++ net/netfilter/ipvs/ip_vs_proto_tcp.c | 5 ++ net/netfilter/ipvs/ip_vs_proto_udp.c | 4 + net/netfilter/nf_conntrack_expect.c | 3 + net/netfilter/nf_conntrack_netlink.c | 36 ++++++--- net/netfilter/nf_conntrack_sip.c | 4 +- net/netfilter/nf_dup_netdev.c | 1 + net/netfilter/nf_flow_table_core.c | 1 + net/netfilter/nf_nat_masquerade.c | 4 +- net/netfilter/nf_queue.c | 1 + net/netfilter/nf_tables_api.c | 21 +++--- net/netfilter/nf_tables_offload.c | 3 + net/netfilter/nfnetlink_queue.c | 17 ++++- net/netfilter/nft_fwd_netdev.c | 1 + net/netfilter/nft_set_rbtree.c | 3 +- net/netfilter/xt_NETMAP.c | 4 +- net/netfilter/xt_recent.c | 7 +- 32 files changed, 279 insertions(+), 110 deletions(-) create mode 100644 include/net/netfilter/nf_recursion.h
Pablo Neira Ayuso
maintainerPATCH 1/1 netfilter: flowtable: do not scrub as crossing netns
IPIP tunneling support does not result in crossing a netns, therefore, adopt the more conservative variant of skb_scrub_packet() by specifying !xnet, keep the existing skb mark in place.
---
@Lorenzo: Do you remember the reason why this is scrubbing as crossing netns? There is a LLM report that is flagging this as an issue when interacting with other subsystem, eg. packets was marked. At least in the tunnel driver I can see this is conditional.
net/netfilter/nf_flow_table_ip.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/netfilter/nf_flow_table_ip.c +2 -2
@@ -630,7 +630,7 @@ static int nf_flow_tunnel_ip_push(struct net *net, struct sk_buff *skb,if (err)return err; -skb_scrub_packet (skb, true); +skb_scrub_packet (skb, false);skb_clear_hash_if_not_l4 (skb);/* Push down and install the IP header. */
@@ -699,7 +699,7 @@ static int nf_flow_tunnel_ip6_push(struct net *net, struct sk_buff *skb,if (err)return err; -skb_scrub_packet (skb, true); +skb_scrub_packet (skb, false); mtu =dst_mtu (dst) -sizeof (*ip6h); mtu =max (mtu,IPV6_MIN_MTU );skb_dst_update_pmtu_no_confirm (skb, mtu);
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH nf-next] netfilter: flowtable: do not scrub as crossing netns
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
Hi!
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
- Whether the issue was actually triggered, or is only theoretical (e.g. found by code inspection). If it was triggered please include the symptoms, like the stack trace or error messages.
Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account. If the series needs another revision for other reasons, please include the information in the commit messages then.
The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.
Pablo Neira Ayuso
maintainerRe: [PATCH nf-next] netfilter: flowtable: do not scrub as crossing netns
Quoted 9 lines · On Tue, Nov 03, 2026 at 02:08:06PM +0000, [email protected] wrote:
Hi!
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
- Whether the issue was actually triggered, or is only theoretical (e.g. found by code inspection). If it was triggered please include the symptoms, like the stack trace or error messages.
Quoted 5 lines
Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account. If the series needs another revision for other reasons, please include the information in the commit messages then.
This came from ... :drum-roll: ... a LLM unrelated report, providing with a patch that was not looking very good, but I could scavenge this subtle aspect from that pile.
Quoted 2 lines
The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.
Pablo Neira Ayuso
maintainerPATCH v2 1/15 netfilter: nft_reject: prevent unbounded recursion in output hooks
In reply to Pablo Neira AyusoFrom: Fernando Fernandez Mancera <[email protected]>
nftables payload-mangling rules can rewrite packet contents (such as TCP flags) before they are evaluated by reject targets. This defeats the content-based loop guards, allowing unbounded hook recursion that leads to kernel stack exhaustion and panics.
Fix this by extracting the per-CPU xmit recursion limit previously used by the netdev duplication/forwarding paths into a shared header, and apply it to the inet and inet6 reject transmit functions. It disables BH while transmitting the skb to address a possible migration to different CPU leading to imbalanced decrementation of the recursion counters.
Note that the dev_queue_xmit() path used when CONFIG_BRIDGE_NETFILTER is enabled does not require this recursion guard. In that scenario, the generated reset packet is transmitted directly at Layer 2. Because the packet egresses from a different hook, it cannot synchronously loop. In addition nf_send_unreach() and nf_send_unreach6() are safe as the ICMP transmit functions are protected against recursion by the icmp_xmit_lock() and icmpv6_xmit_lock() guards.
--- include/linux/netdevice_xmit.h | 4 +-- include/net/netfilter/nf_dup_netdev.h | 35 ---------------------- include/net/netfilter/nf_recursion.h | 42 +++++++++++++++++++++++++++ net/ipv4/netfilter/nf_reject_ipv4.c | 12 ++++++++ net/ipv6/netfilter/nf_reject_ipv6.c | 13 +++++++++ net/netfilter/nf_dup_netdev.c | 1 + net/netfilter/nft_fwd_netdev.c | 1 + 7 files changed, 71 insertions(+), 37 deletions(-) create mode 100644 include/net/netfilter/nf_recursion.h
include/linux/netdevice_xmit.h +2 -2
@@ -18,8 +18,8 @@ struct netdev_xmit {u8 sched_mirred_nest;struct net_device *sched_mirred_dev[MIRRED_NEST_LIMIT ];#endif -#if IS_ENABLED (CONFIG_NF_DUP_NETDEV ) -u8 nf_dup_skb_recursion; +#if IS_ENABLED (CONFIG_NETFILTER ) +u8 nf_xmit_skb_recursion;#endif };
include/net/netfilter/nf_dup_netdev.h +0 -35
@@ -9,41 +9,6 @@void nf_dup_netdev_egress (const struct nft_pktinfo *pkt,int oif);void nf_fwd_netdev_egress (const struct nft_pktinfo *pkt,int oif); -#define NF_RECURSION_LIMIT 2 - -#ifndef CONFIG_PREEMPT_RT -static inline bool nf_dev_xmit_recursion (void ) -{ -return unlikely (__this_cpu_read (softnet_data.xmit.nf_dup_skb_recursion) > -NF_RECURSION_LIMIT ); -} - -static inline void nf_dev_xmit_recursion_inc (void ) -{ -__this_cpu_inc (softnet_data.xmit.nf_dup_skb_recursion); -} - -static inline void nf_dev_xmit_recursion_dec (void ) -{ -__this_cpu_dec (softnet_data.xmit.nf_dup_skb_recursion); -} -#else -static inline bool nf_dev_xmit_recursion (void ) -{ -return unlikely (current->net_xmit.nf_dup_skb_recursion >NF_RECURSION_LIMIT ); -} - -static inline void nf_dev_xmit_recursion_inc (void ) -{ - current->net_xmit.nf_dup_skb_recursion++; -} - -static inline void nf_dev_xmit_recursion_dec (void ) -{ - current->net_xmit.nf_dup_skb_recursion--; -} -#endif -struct nft_offload_ctx ;struct nft_flow_rule ;
include/net/netfilter/nf_recursion.h new file +42 -0
42 lines not on this page · raw message
net/ipv4/netfilter/nf_reject_ipv4.c +12 -0
@@ -10,6 +10,7 @@#include <net/dst.h> #include <net/dst_metadata.h> #include <net/netfilter/ipv4/nf_reject.h> +#include <net/netfilter/nf_recursion.h> #include <linux/netfilter_ipv4.h> #include <linux/netfilter_bridge.h>
@@ -336,7 +337,18 @@ void nf_send_reset(struct net *net, struct sock *sk, struct sk_buff *oldskb,dev_queue_xmit (nskb); } else#endif + { +local_bh_disable (); +if (nf_dev_xmit_recursion ()) { +local_bh_enable (); + goto free_nskb; + } + +nf_dev_xmit_recursion_inc ();ip_local_out (net, nskb->sk, nskb); +nf_dev_xmit_recursion_dec (); +local_bh_enable (); + }return ;
net/ipv6/netfilter/nf_reject_ipv6.c +13 -0
@@ -10,6 +10,7 @@#include <net/ip6_checksum.h> #include <net/dst_metadata.h> #include <net/netfilter/ipv6/nf_reject.h> +#include <net/netfilter/nf_recursion.h> #include <linux/netfilter_ipv6.h> #include <linux/netfilter_bridge.h>
@@ -409,7 +410,19 @@ void nf_send_reset6(struct net *net, struct sock *sk, struct sk_buff *oldskb, dev_queue_xmit(nskb); } else#endif + { +local_bh_disable (); +if (nf_dev_xmit_recursion ()) { +local_bh_enable (); +kfree_skb (nskb); +return ; + } + +nf_dev_xmit_recursion_inc ();ip6_local_out (net, sk, nskb); +nf_dev_xmit_recursion_dec (); +local_bh_enable (); + } }EXPORT_SYMBOL_GPL (nf_send_reset6);
net/netfilter/nf_dup_netdev.c +1 -0
@@ -12,6 +12,7 @@#include <net/netfilter/nf_tables.h> #include <net/netfilter/nf_tables_offload.h> #include <net/netfilter/nf_dup_netdev.h> +#include <net/netfilter/nf_recursion.h> static void nf_do_netdev_egress (struct sk_buff *skb,struct net_device *dev,enum nf_dev_hooks hook)
net/netfilter/nft_fwd_netdev.c +1 -0
@@ -14,6 +14,7 @@#include <net/netfilter/nf_tables.h> #include <net/netfilter/nf_tables_offload.h> #include <net/netfilter/nf_dup_netdev.h> +#include <net/netfilter/nf_recursion.h> #include <net/neighbour.h> #include <net/ip.h>
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH net 01/15] netfilter: nft_reject: prevent unbounded recursion in output hooks
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
Hi!
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.
- Whether the issue was actually triggered, or is only theoretical (e.g. found by code inspection). If it was triggered please include the symptoms, like the stack trace or error messages.
Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account. If the series needs another revision for other reasons, please include the information in the commit messages then.
The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.
Pablo Neira Ayuso
maintainerRe: [PATCH net 01/15] netfilter: nft_reject: prevent unbounded recursion in output hooks
Quoted 5 lines · On Tue, Nov 03, 2026 at 02:08:22PM +0000, [email protected] wrote:
Hi!
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
Quoted 7 lines
- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.
- Whether the issue was actually triggered, or is only theoretical (e.g. found by code inspection). If it was triggered please include the symptoms, like the stack trace or error messages.
Regarding the automated request, this series was primarily identified through LLM-assisted analysis.
A summary of the discovery and triggering status follows:
Issues triggered and verified: Several issues were actively triggered and confirmed via automated testing. This includes KASAN reports for use-after-free and out-of-bounds access (patches 3, 4, 11), syzbot reports for warnings and panics (patches 8 and 10), and a confirmed sleeping function call in an atomic context (patch 14). Patch 12 was identified via LLM and subsequently verified using a userspace ASan harness.
Issues identified via LLM (Preventative):
The remaining patches were identified via LLM-assisted review. These address edge cases to prevent future issues such as unbounded recursion (patch 1), infinite loops (patch 2), stale connection growth (commit 5), chain depth exhaustion (patch 6), guard loss of recursive count during reinjection (patch 7), NULL pointer dereferences (patch 9), memory leaks (patch 13), and uninitialized stack data (patch 15).
This is a LLM answer to this automated message, with a few edits.
Quoted 8 lines
Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account. If the series needs another revision for other reasons, please include the information in the commit messages then.
The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.
Pablo Neira Ayuso
maintainerPATCH v2 2/15 ipvs: wait grace period while reusing dest from trash
In reply to Pablo Neira AyusoFrom: Darong Lu <[email protected]>
Darong Lu and the VEGA team report a problem with the TWOS scheduler that manifests as a loop while traversing the list of destinations. The problem occurs if destinations are removed and re-added through the destination trash before an RCU grace period has elapsed. As destinations are re-added to a new service, CPUs traversing the old destination list may never reach the old list head and fail to terminate.
As the problem is not specific to the TWOS scheduler, use a general solution. Jiayuan Chen provided an initial solution based on get_state_synchronize_rcu() and cond_synchronize_rcu(), where the grace period cookie is saved after a destination is unlinked and moved into the destination trash. cond_synchronize_rcu() then ensures the grace period before the destination is reused.
Use a temporary list for destinations removed together from a service or during a flush. A single get_state_synchronize_rcu() call records the cookie for all destinations before the temporary list is spliced into the public destination trash. This also limits destination-trash locking to one acquisition for bulk removal.
Destination edits that remove and re-add an entry to the real-server hash table use the same grace-period mechanism. Destinations returned to the trash after an estimator failure are put back without recording a new cookie.
--- include/net/ip_vs.h | 4 +- net/netfilter/ipvs/ip_vs_ctl.c | 109 ++++++++++++++++++++++++++------- 2 files changed, 90 insertions(+), 23 deletions(-)
include/net/ip_vs.h +3 -1
@@ -1021,7 +1021,9 @@ struct ip_vs_dest {struct rcu_head rcu_head;struct list_head t_list;/* in dest_trash */ - unsigned int in_rs_table:1; /* we are in rs_table */ +unsigned long rcu_state;/* GP cookie when trashed */ + unsigned int in_rs_table:1, /* we are in rs_table */ + need_gp:1; /* Need GP on rehash */ };/* The scheduler object */
net/netfilter/ipvs/ip_vs_ctl.c +87 -22
230 lines not on this page · raw message
Signature · 1 line
2.47.3
Julian Anastasov
maintainerRe: [PATCH net 02/15] ipvs: wait grace period while reusing dest from trash
Hello,
Quoted 59 lines · Pablo Neira Ayuso
From: Darong Lu <[email protected]>
Darong Lu and the VEGA team report a problem with the TWOS scheduler that manifests as a loop while traversing the list of destinations. The problem occurs if destinations are removed and re-added through the destination trash before an RCU grace period has elapsed. As destinations are re-added to a new service, CPUs traversing the old destination list may never reach the old list head and fail to terminate.
As the problem is not specific to the TWOS scheduler, use a general solution. Jiayuan Chen provided an initial solution based on
get_state_synchronize_rcu()andcond_synchronize_rcu(), where the grace period cookie is saved after a destination is unlinked and moved into the destination trash.cond_synchronize_rcu()then ensures the grace period before the destination is reused.Use a temporary list for destinations removed together from a service or during a flush. A single
get_state_synchronize_rcu()call records the cookie for all destinations before the temporary list is spliced into the public destination trash. This also limits destination-trash locking to one acquisition for bulk removal.Destination edits that remove and re-add an entry to the real-server hash table use the same grace-period mechanism. Destinations returned to the trash after an estimator failure are put back without recording a new cookie.
Fixes ceec4c381681 ("ipvs: convert services to rcu")Reported-by Vega <[email protected]>Reported-by Darong Lu <[email protected]>Co-developed-by Jiayuan Chen <[email protected]>Signed-off-by Jiayuan Chen <[email protected]>Signed-off-by Darong Lu <[email protected]>Signed-off-by Ren Wei <[email protected]>Signed-off-by Julian Anastasov <[email protected]>Signed-off-by Pablo Neira Ayuso <[email protected]>--- include/net/ip_vs.h | 4 +- net/netfilter/ipvs/ip_vs_ctl.c | 109 ++++++++++++++++++++++++++------- 2 files changed, 90 insertions(+), 23 deletions(-)net/netfilter/ipvs/ip_vs_ctl.c +2
-4@@ -1566,11 +1615,18 @@ ip_vs_add_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest) ret =ip_vs_start_estimator (svc->ipvs, &dest->stats);/* On error put back dest into the trash */ -if (ret <0 ) -ip_vs_trash_put_dest (svc->ipvs, dest, dest->idle_start, - false); -else +if (ret <0 ) { +ip_vs_trash_put_back (svc->ipvs, dest);
net/netfilter/ipvs/ip_vs_ctl.c ip_vs_add_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest) · line 1629
Quoted 10 lines
net/netfilter/ipvs/ip_vs_ctl.c +9
-0+ }else { +/* Readers may still follow the n_list/d_list of the +* unlinked dest, wait before linking it again. +*/ +cond_synchronize_rcu (dest->rcu_state); +/* need_gp matters only on dest edit, reinit it */ + dest->need_gp =0 ; +__ip_vs_update_dest (svc, dest, udest,1 ); + }
BTW, this patch needs a little fix above:
https://sashiko.dev/#/patchset/20261102005654.532438-1-pablo%40netfilter.org
I'll post more info soon.
Regards
--
Julian Anastasov <[email protected]>
[email protected]
AI reviewRe: [PATCH net 02/15] ipvs: wait grace period while reusing dest from trash
In reply to Pablo Neira AyusoThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.
Critical: 0 · High: 1 · Medium: 0 · Low: 1
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [High] ipvs: wait for RCU grace period before calling ip_vs_start_estimator()
- [Low] The comment on the new rcu_state field in struct ip_vs_dest (include/net/ip_vs.h:1024) reads /* GP cookie when trashed */, but…
--
ipvs: wait grace period while reusing dest from trash
This patch fixes looping readers of the destination list. The loop happens when a dest is removed and re-added through the destination trash before an RCU grace period has elapsed. A grace-period cookie is saved with get_state_synchronize_rcu() when dests are unlinked, and cond_synchronize_rcu() is called before the dest is relinked. Dest edits that unhash and rehash an entry in rs_table use the same mechanism.
include/net/ip_vs.h struct ip_vs_dest { · line 1024
Quoted 10 lines
include/net/ip_vs.h +1
-1@@ -1021,7 +1021,9 @@ struct ip_vs_dest {struct rcu_head rcu_head;struct list_head t_list;/* in dest_trash */ - unsigned int in_rs_table:1; /* we are in rs_table */ +unsigned long rcu_state;/* GP cookie when trashed */
^^^^
[Severity: Low]
This isn't a bug, but is this comment on struct ip_vs_dest::rcu_state complete?
The patch also writes rcu_state in __ip_vs_update_dest() when a live dest is edited and unhashed from rs_table. That dest stays on svc->destinations and never goes to the trash:
net/netfilter/ipvs/ip_vs_ctl.c:__ip_vs_update_dest() {
...
if (unlinked) {
dest->rcu_state = get_state_synchronize_rcu();
/* Need grace period in this or next dest update */
dest->need_gp = 1;
}
...
}ip_vs_rs_hash() then reads that cookie through need_gp before hlist_add_head_rcu().
So the field holds either the trash-time cookie read by ip_vs_add_dest(), or the edit-time unhash cookie read by ip_vs_rs_hash().
Could the comment say something like "GP cookie when unlinked (trash or rehash)" to cover both uses?
Quoted 5 lines
include/net/ip_vs.h +2
-0+ unsigned int in_rs_table:1, /* we are in rs_table */ + need_gp:1; /* Need GP on rehash */ };/* The scheduler object */
[ ... ]
Quoted 16 lines
net/netfilter/ipvs/ip_vs_ctl.c +5
-4@@ -1566,11 +1615,18 @@ ip_vs_add_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest) ret =ip_vs_start_estimator (svc->ipvs, &dest->stats);/* On error put back dest into the trash */ -if (ret <0 ) -ip_vs_trash_put_dest (svc->ipvs, dest, dest->idle_start, - false); -else +if (ret <0 ) { +ip_vs_trash_put_back (svc->ipvs, dest); + }else { +/* Readers may still follow the n_list/d_list of the +* unlinked dest, wait before linking it again.
net/netfilter/ipvs/ip_vs_ctl.c ip_vs_add_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest) · line 1632
Quoted 10 lines
net/netfilter/ipvs/ip_vs_ctl.c +6
-0+*/ +cond_synchronize_rcu (dest->rcu_state); +/* need_gp matters only on dest edit, reinit it */ + dest->need_gp =0 ; +__ip_vs_update_dest (svc, dest, udest,1 ); + } }else {/* * Allocate and initialize the dest structure
[Severity: High]
Does cond_synchronize_rcu() need to come before ip_vs_start_estimator() here?
The comment covers n_list and d_list, but dest->stats.est.list is a third RCU-linked node that gets reused on this path. When the dest was deleted, __ip_vs_del_dest() called ip_vs_stop_estimator(). For an estimator that kthread 0 had already moved to a tick chain (est->ktid >= 0), that does hlist_del_rcu(&est->list) on td->chains[cid] and does not wait for a grace period.
The estimator kthreads walk that chain without service_mutex. They do it in ip_vs_chain_estimation() under rcu_read_lock() taken in ip_vs_tick_estimation(). So a reader can still be positioned on this est when the dest is taken back out of the trash.
ip_vs_add_dest() calls ip_vs_start_estimator() before the new grace period wait, and that rewrites the node right away:
net/netfilter/ipvs/ip_vs_est.c:ip_vs_start_estimator() {
...
if (ret >= 0)
hlist_add_head(&est->list, &ipvs->est_temp_list);
else
INIT_HLIST_NODE(&est->list);
...
}On success, a reader still on the old chain follows est->list.next onto ipvs->est_temp_list. That list is not RCU protected, and ip_vs_stop_estimator() uses plain hlist_del() for ktid < 0 entries. The reader can therefore end up dereferencing LIST_POISON1.
On failure, next becomes NULL and the walk ends early, skipping the remaining estimators on td->chains[cid]. The failure path also never reaches cond_synchronize_rcu() at all.
This is the same unlink-then-relink-without-a-grace-period problem the patch fixes for n_list and d_list. It is reachable with the same delete/re-add sequence from netlink or sockopt, as long as estimator kthreads are running.
Would it work to move cond_synchronize_rcu(dest->rcu_state) up to just after ip_vs_trash_get_dest() returns a dest? The wait would then cover est->list too, on both the success and error paths. The comment could be updated to mention the estimator list as well.
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261103140308.591690-3-pablo%40netfilter.org
Julian Anastasov
maintainerRe: [PATCH net 02/15] ipvs: wait grace period while reusing dest from trash
Hello,
Adding other authors to CC. In short, the plan is to
create new version.
Quoted 59 lines · On Wed, 4 Nov 2026, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.
Critical: 0 · High: 1 · Medium: 0 · Low: 1
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [High] ipvs: wait for RCU grace period before calling
ip_vs_start_estimator()
- [Low] The comment on the newrcu_statefield instruct ip_vs_dest(include/net/ip_vs.h:1024) reads/* GP cookie when trashed */, but…--
ipvs: wait grace period while reusing dest from trash
This patch fixes looping readers of the destination list. The loop happens when a dest is removed and re-added through the destination trash before an RCU grace period has elapsed. A grace-period cookie is saved with
get_state_synchronize_rcu()when dests are unlinked, andcond_synchronize_rcu()is called before the dest is relinked. Dest edits that unhash and rehash an entry inrs_tableuse the same mechanism.include/net/ip_vs.h struct ip_vs_dest { · line 1024
Quoted 10 lines
include/net/ip_vs.h +1
-1@@ -1021,7 +1021,9 @@ struct ip_vs_dest {struct rcu_head rcu_head;struct list_head t_list;/* in dest_trash */ - unsigned int in_rs_table:1; /* we are in rs_table */ +unsigned long rcu_state;/* GP cookie when trashed */ ^^^^[Severity: Low]
This isn't a bug, but is this comment on structip_vs_dest::rcu_statecomplete?The patch also writes
rcu_statein__ip_vs_update_dest()when a live dest is edited and unhashed fromrs_table. That dest stays onsvc->destinationsand never goes to the trash:net/netfilter/ipvs/ip_vs_ctl.c:__ip_vs_update_dest() { ... if (unlinked) { dest->rcu_state = get_state_synchronize_rcu(); /* Need grace period in this or next dest update */ dest->need_gp = 1; } ... }
Quoted 8 lines
ip_vs_rs_hash()then reads that cookie throughneed_gpbeforehlist_add_head_rcu().So the field holds either the trash-time cookie read by
ip_vs_add_dest(), or the edit-time unhash cookie read byip_vs_rs_hash().Could the comment say something like "GP cookie when unlinked (trash or rehash)" to cover both uses?
Sounds good
Quoted 78 lines
Quoted 6 lines
include/net/ip_vs.h +2
-0+ unsigned int in_rs_table:1, /* we are in rs_table */ + need_gp:1; /* Need GP on rehash */ };/* The scheduler object */ [ ... ]
Quoted 16 lines
net/netfilter/ipvs/ip_vs_ctl.c +5
-4@@ -1566,11 +1615,18 @@ ip_vs_add_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest) ret =ip_vs_start_estimator (svc->ipvs, &dest->stats);/* On error put back dest into the trash */ -if (ret <0 ) -ip_vs_trash_put_dest (svc->ipvs, dest, dest->idle_start, - false); -else +if (ret <0 ) { +ip_vs_trash_put_back (svc->ipvs, dest); + }else { +/* Readers may still follow the n_list/d_list of the +* unlinked dest, wait before linking it again. net/netfilter/ipvs/ip_vs_ctl.c ip_vs_add_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest) · line 1632
Quoted 10 lines
net/netfilter/ipvs/ip_vs_ctl.c +6
-0+*/ +cond_synchronize_rcu (dest->rcu_state); +/* need_gp matters only on dest edit, reinit it */ + dest->need_gp =0 ; +__ip_vs_update_dest (svc, dest, udest,1 ); + } }else {/* * Allocate and initialize the dest structure [Severity: High]
Doescond_synchronize_rcu()need to come beforeip_vs_start_estimator()here?The comment covers
n_listandd_list, butdest->stats.est.listis a third RCU-linked node that gets reused on this path. When the dest was deleted,__ip_vs_del_dest()calledip_vs_stop_estimator(). For an estimator that kthread 0 had already moved to a tick chain (est->ktid>= 0), that doeshlist_del_rcu(&est->list)on td->chains[cid] and does not wait for a grace period.The estimator kthreads walk that chain without
service_mutex. They do it inip_vs_chain_estimation()underrcu_read_lock()taken inip_vs_tick_estimation(). So a reader can still be positioned on this est when the dest is taken back out of the trash.
ip_vs_add_dest()callsip_vs_start_estimator()before the new grace period wait, and that rewrites the node right away:net/netfilter/ipvs/ip_vs_est.c:ip_vs_start_estimator() { ... if (ret >= 0) hlist_add_head(&est->list, &ipvs->est_temp_list); else INIT_HLIST_NODE(&est->list); ... }On success, a reader still on the old chain follows
est->list.nextontoipvs->est_temp_list. That list is not RCU protected, andip_vs_stop_estimator()uses plainhlist_del()for ktid < 0 entries. The reader can therefore end up dereferencingLIST_POISON1.On failure, next becomes NULL and the walk ends early, skipping the remaining estimators on td->chains[cid]. The failure path also never reaches
cond_synchronize_rcu()at all.This is the same unlink-then-relink-without-a-grace-period problem the patch fixes for
n_listandd_list. It is reachable with the same delete/re-add sequence from netlink or sockopt, as long as estimator kthreads are running.
Quoted 4 lines
Would it work to move
cond_synchronize_rcu(dest->rcu_state)up to just afterip_vs_trash_get_dest()returns a dest? The wait would then coverest->listtoo, on both the success and error paths. The comment could be updated to mention the estimator list as well.
Yep, this is the right thing to do
Quoted 2 lines
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261103140308.591690-3-pablo%40netfilter.org
Regards
--
Julian Anastasov <[email protected]>
Pablo Neira Ayuso
maintainerPATCH v2 3/15 ipvs: Defer application parent freeing until after RCU readers
In reply to Pablo Neira AyusoFrom: Chengfeng Ye <[email protected]>
unregister_ip_vs_app() removes each incarnation from its protocol list and uses call_rcu() to defer freeing it, but frees the parent application immediately. An RCU reader that already found an incarnation can still access the parent through inc->app in ip_vs_app_inc_get().
During FTP helper module unload, the following interleaving is possible:
CPU 0 (RCU reader) CPU 1 (module unload)
tcp_app_conn_bind()
find inc in protocol list
unregister_ip_vs_app()
ip_vs_app_inc_release(inc)
list_del_rcu(&inc->p_list)
call_rcu(&inc->rcu_head, ...)
kfree(a)
ip_vs_app_inc_get(inc)
try_module_get(inc->app->module)The helper module is already going away, so try_module_get() would fail, but evaluating its argument first reads the freed parent. The subsequent rcu_barrier() in pernet unregistration cannot protect this earlier free. KASAN reported:
BUG: KASAN: slab-use-after-free in ip_vs_app_inc_get+0x7c/0x90 Call Trace: ip_vs_app_inc_get+0x7c/0x90 tcp_app_conn_bind+0x1bc/0x290 ip_vs_conn_new+0x1915/0x20d0 ip_vs_schedule+0x697/0xea0 tcp_conn_schedule+0x489/0x820 ip_vs_in_hook+0x7bf/0x1f40 Allocated by task 88: kmemdup_noprof+0x20/0x50 register_ip_vs_app+0x12d/0x2c0 __ip_vs_ftp_init+0x56/0x160 [ip_vs_ftp] Freed by task 104: kfree+0x131/0x3c0 unregister_ip_vs_app+0x2f4/0x5b0 unregister_pernet_operations+0x232/0x490 unregister_pernet_subsys+0x1c/0x30 __do_sys_delete_module+0x346/0x510
Use kfree_rcu() with the existing rcu_head to keep the parent alive until these readers finish. Successful helper references already prevent normal module unload, and incarnation RCU callbacks do not access the parent.
--- net/netfilter/ipvs/ip_vs_app.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/ipvs/ip_vs_app.c +1 -1
@@ -242,7 +242,7 @@ void unregister_ip_vs_app(struct netns_ipvs *ipvs, struct ip_vs_app *app) }list_del (&a->a_list); -kfree (a); +kfree_rcu (a, rcu_head);/* decrease the module use count */ ip_vs_use_count_dec ();
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH v2 4/15 ipvs: fix protocol data lifetime during namespace teardown
In reply to Pablo Neira AyusoFrom: Chengfeng Ye <[email protected]>
IPVS frees its per-net protocol data before control cleanup cancels defense work and unregisters the sysctl table. A defense worker can load a protocol data pointer, then namespace cleanup can unlink and free that object before the worker dereferences pd->pp or pd->next. The worker's securetcp_lock does not serialize with protocol cleanup. A concurrent defense-mode sysctl write can reach the same timeout update path.
KASAN reported:
BUG: KASAN: slab-use-after-free in ip_vs_protocol_timeout_change Workqueue: events_long defense_work_handler Call Trace: ip_vs_protocol_timeout_change+0x1b4/0x1e0 update_defense_level+0x63e/0xd80 defense_work_handler+0x1e/0xc0 Allocated by task 125: ip_vs_protocol_net_init+0xda/0x2f0 __ip_vs_init+0x16b/0x240 setup_net+0xfc/0x310 Freed by task 12: ip_vs_protocol_net_cleanup+0x1d6/0x2e0 __ip_vs_cleanup_batch+0x7d/0x100 cleanup_net+0x38c/0x770
Run control cleanup before protocol cleanup so that defense work and active sysctl handlers have finished before their protocol data is freed. Initialize protocols before exposing the control interface, and unwind in reverse order, to enforce the same lifetime on initialization failure. Protocol initialization and exit do not depend on control state.
--- net/netfilter/ipvs/ip_vs_core.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
net/netfilter/ipvs/ip_vs_core.c +6 -6
@@ -2502,12 +2502,12 @@ static int __net_init __ip_vs_init(struct net *net)if (ip_vs_estimator_net_init (ipvs) <0 ) goto estimator_fail; -if (ip_vs_control_net_init (ipvs) <0 ) - goto control_fail; -if (ip_vs_protocol_net_init (ipvs) <0 ) goto protocol_fail; +if (ip_vs_control_net_init (ipvs) <0 ) + goto control_fail; +if (ip_vs_app_net_init (ipvs) <0 ) goto app_fail;
@@ -2527,10 +2527,10 @@ static int __net_init __ip_vs_init(struct net *net) conn_fail:ip_vs_app_net_cleanup (ipvs); app_fail: -ip_vs_protocol_net_cleanup (ipvs); -protocol_fail:ip_vs_control_net_cleanup (ipvs); control_fail: +ip_vs_protocol_net_cleanup (ipvs); +protocol_fail:ip_vs_estimator_net_cleanup (ipvs); estimator_fail: net->ipvs =NULL ;
@@ -2547,8 +2547,8 @@ static void __net_exit __ip_vs_cleanup_batch(struct list_head *net_list) ipvs =net_ipvs (net);ip_vs_conn_net_cleanup (ipvs);ip_vs_app_net_cleanup (ipvs); -ip_vs_protocol_net_cleanup (ipvs);ip_vs_control_net_cleanup (ipvs); +ip_vs_protocol_net_cleanup (ipvs);ip_vs_estimator_net_cleanup (ipvs);IP_VS_DBG (2 ,"ipvs netns %d released\n" , ipvs->gen); net->ipvs =NULL ;
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH v2 5/15 ipvs: do not create conns from packets with port 0
In reply to Pablo Neira AyusoFrom: Julian Anastasov <[email protected]>
We should disallow normal connections with cport 0 to be created from packets by the scheduler (from clients) or by ip_vs_new_conn_out (from real servers) because later such traffic can not hit the connection due to the NO_CPORT requirement. As result, it can lead to duplicate connection for every packet. Do the same for vport 0.
--- net/netfilter/ipvs/ip_vs_core.c | 3 ++- net/netfilter/ipvs/ip_vs_proto_sctp.c | 5 +++++ net/netfilter/ipvs/ip_vs_proto_tcp.c | 5 +++++ net/netfilter/ipvs/ip_vs_proto_udp.c | 4 ++++ 4 files changed, 16 insertions(+), 1 deletion(-)
net/netfilter/ipvs/ip_vs_core.c +2 -1
@@ -1305,6 +1305,7 @@ static inline bool is_new_conn_expected(const struct ip_vs_conn *cp,* In fwmark-VS actual vaddr and vport are unknown to IPVS * 2) Real Server and Virtual Service were NOT configured without port: * This is to allow match of different VS to the same RS ip-addr +* 3) Client port is not 0 */ struct ip_vs_conn *ip_vs_new_conn_out(struct ip_vs_service *svc, struct ip_vs_dest *dest,
@@ -1328,7 +1329,7 @@ struct ip_vs_conn *ip_vs_new_conn_out(struct ip_vs_service *svc,/* check pre-requisites are satisfied */ if (svc->fwmark)return NULL ; -if (!vport || !dport) +if (!vport || !dport || !cport)return NULL ;/* for persistent service first create connection template */
net/netfilter/ipvs/ip_vs_proto_sctp.c +5 -0
@@ -30,6 +30,7 @@ sctp_conn_schedule(struct netns_ipvs *ipvs, int af, struct sk_buff *skb, sch =skb_header_pointer (skb, iph->len +sizeof (_sctph),sizeof (_schunkh), &_schunkh);if (sch) { +/* Accept packet without scheduling? */ if (sch->type ==SCTP_CID_ABORT || !(sysctl_sloppy_sctp (ipvs) || sch->type ==SCTP_CID_INIT ))
@@ -47,6 +48,10 @@ sctp_conn_schedule(struct netns_ipvs *ipvs, int af, struct sk_buff *skb,return 0 ; } +/* Do not create connection for port 0, skip the packet */ +if (!ports[0 ] || !ports[1 ]) +return 1 ; +if (likely (!ip_vs_iph_inverse (iph))) svc =ip_vs_service_find (ipvs, af, skb->mark, iph->protocol, &iph->daddr, ports[1 ]);
net/netfilter/ipvs/ip_vs_proto_tcp.c +5 -0
@@ -48,6 +48,7 @@ tcp_conn_schedule(struct netns_ipvs *ipvs, int af, struct sk_buff *skb,if (likely (!ip_vs_iph_icmp (iph))) { th =skb_header_pointer (skb, iph->len,sizeof (_tcph), &_tcph);if (th) { +/* Accept packet without scheduling? */ if (th->rst || !(sysctl_sloppy_tcp (ipvs) || th->syn))return 1 ; ports = &th->source;
@@ -62,6 +63,10 @@ tcp_conn_schedule(struct netns_ipvs *ipvs, int af, struct sk_buff *skb,return 0 ; } +/* Do not create connection for port 0, skip the packet */ +if (!ports[0 ] || !ports[1 ]) +return 1 ; +/* No !th->ack check to allow scheduling on SYN+ACK for Active FTP */ if (likely (!ip_vs_iph_inverse (iph)))
net/netfilter/ipvs/ip_vs_proto_udp.c +4 -0
@@ -52,6 +52,10 @@ udp_conn_schedule(struct netns_ipvs *ipvs, int af, struct sk_buff *skb,return 0 ; } +/* Do not create connection for port 0, skip the packet */ +if (!ports[0 ] || !ports[1 ]) +return 1 ; +if (likely (!ip_vs_iph_inverse (iph))) svc =ip_vs_service_find (ipvs, af, skb->mark, iph->protocol, &iph->daddr, ports[1 ]);
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH net 05/15] ipvs: do not create conns from packets with port 0
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.
Critical: 0 · High: 0 · Medium: 3 · Low: 0
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [Medium] The subject of this patch says IPVS should not create connections from packets with port 0.
- [Medium] udp_conn_schedule() (net/netfilter/ipvs/ip_vs_proto_udp.c:55-57) now returns 1 for any UDP…
- [Medium] The new port-0 early return 1 in tcp_conn_schedule() (ip_vs_proto_tcp.c:66-68), udp_conn_schedule()…
--
ipvs: do not create conns from packets with port 0
This commit stops the TCP, UDP and SCTP conn_schedule handlers and ip_vs_new_conn_out() from creating normal connections when the client port or virtual port is 0. Such connections can never be matched later because of the NO_CPORT lookup rule, so a new duplicate connection is created for every packet.
Quoted 4 lines
net/netfilter/ipvs/ip_vs_core.c +0
-0
[ ... ]
net/netfilter/ipvs/ip_vs_core.c struct ip_vs_conn *ip_vs_new_conn_out(struct ip_vs_service *svc, · line 1335
Quoted 9 lines
net/netfilter/ipvs/ip_vs_core.c +1
-1@@ -1328,7 +1329,7 @@ struct ip_vs_conn *ip_vs_new_conn_out(struct ip_vs_service *svc,/* check pre-requisites are satisfied */ if (svc->fwmark)return NULL ; -if (!vport || !dport) +if (!vport || !dport || !cport)return NULL ;/* for persistent service first create connection template */
[Severity: Medium]
The FTP application helper also creates connections from packet data. Does it need the same port 0 check?
In ip_vs_ftp_in(), a client on an established FTP control connection can send "PORT a,b,c,d,0,0". The legacy branch of ip_vs_ftp_get_addrport() returns success with port 0:
net/netfilter/ipvs/ip_vs_ftp.c:ip_vs_ftp_get_addrport() {
...
*port = get_unaligned((__be16 *) (p + 4));
return 1;
...
}ip_vs_ftp_in() then uses that value as the cport. It creates a normal connection without IP_VS_CONN_F_NO_CPORT:
net/netfilter/ipvs/ip_vs_ftp.c:ip_vs_ftp_in() {
...
ip_vs_conn_fill_param(cp->ipvs, cp->af,
ipvsh->protocol, &to, port, &cp->vaddr,
htons(ntohs(cp->vport)-1), &p);
n_cp = ip_vs_conn_in_get(&p);
if (!n_cp) {
n_cp = ip_vs_conn_new(&p, cp->af, &cp->daddr,
htons(ntohs(cp->dport)-1),
IP_VS_CONN_F_NFCT, cp->dest,
skb->mark);
...
}__ip_vs_conn_in_get() only matches when this holds:
(!p->cport ^ (!(cp->flags & IP_VS_CONN_F_NO_CPORT)))
So a lookup with cport 0 never finds this entry.
Wouldn't every such PORT command then add another duplicate connection? That is the same problem the commit message describes.
The extended EPRT/EPSV branch of the same function already rejects port 0, so the two formats handle it differently:
if (s == data_limit || !hport || *s != edelim) return -1;
[ ... ]
Quoted 4 lines
net/netfilter/ipvs/ip_vs_proto_tcp.c +0
-0
[ ... ]
net/netfilter/ipvs/ip_vs_proto_tcp.c tcp_conn_schedule(struct netns_ipvs *ipvs, int af, struct sk_buff *skb, · line 69
Quoted 8 lines
net/netfilter/ipvs/ip_vs_proto_tcp.c +4
-0@@ -62,6 +63,10 @@ tcp_conn_schedule(struct netns_ipvs *ipvs, int af, struct sk_buff *skb,return 0 ; } +/* Do not create connection for port 0, skip the packet */ +if (!ports[0 ] || !ports[1 ]) +return 1 ; +
[Severity: Medium]
Does this change where port 0 packets sent to a configured VIP:port end up?
This check runs before ip_vs_service_find(). Returning 1 while *cpp is still NULL makes ip_vs_try_to_schedule() accept the packet:
net/netfilter/ipvs/ip_vs_core.c:ip_vs_try_to_schedule() {
...
if (unlikely(!*cpp)) {
...
*verdict = NF_ACCEPT;
return 0;
}
...
}Before this patch, a SYN with source port 0 to a VIP:port that has a service was always handled by IPVS. It was either scheduled to a real server or passed to ip_vs_leave(). When no destination was available, ip_vs_leave() did this:
icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); return NF_DROP;
Now the packet goes to the director's own TCP stack instead. If the director has a local listener on the same port (for example bound to the wildcard address), can a remote sender using source port 0 now complete a handshake with it through the VIP? IPVS used to shadow that listener. The local TCP receive path does not appear to reject th->source == 0.
udp_conn_schedule() gets the same early return, and so does sctp_conn_schedule() for INIT chunks.
Would dropping port 0 packets that match a service, instead of accepting them, avoid the duplicate connections without changing where this traffic goes?
Quoted 9 lines
net/netfilter/ipvs/ip_vs_proto_tcp.c +0
-0/* No !th->ack check to allow scheduling on SYN+ACK for Active FTP */ if (likely (!ip_vs_iph_inverse (iph)))net/netfilter/ipvs/ip_vs_proto_udp.c +0
-0
net/netfilter/ipvs/ip_vs_proto_udp.c udp_conn_schedule(struct netns_ipvs *ipvs, int af, struct sk_buff *skb, · line 58
Quoted 7 lines
net/netfilter/ipvs/ip_vs_proto_udp.c +4
-0@@ -52,6 +52,10 @@ udp_conn_schedule(struct netns_ipvs *ipvs, int af, struct sk_buff *skb,return 0 ; } +/* Do not create connection for port 0, skip the packet */ +if (!ports[0 ] || !ports[1 ]) +return 1 ; +
[Severity: Medium]
Does this check also need to cover one-packet scheduling (OPS) UDP services?
It runs before the service lookup, so it also applies to services with IP_VS_SVC_F_ONEPACKET. ip_vs_schedule() sets IP_VS_CONN_F_ONE_PACKET on those connections:
flags = (svc->flags & IP_VS_SVC_F_ONEPACKET && iph->protocol == IPPROTO_UDP) ? IP_VS_CONN_F_ONE_PACKET : 0;
ip_vs_conn_hash() never inserts such connections into the table:
net/netfilter/ipvs/ip_vs_conn.c:ip_vs_conn_hash() {
...
if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
return 0;
...
}So they cannot cause the duplicate hashed connections this patch targets.
Before this patch, an OPS service forwarded UDP datagrams with source port 0 to a real server. RFC 768 allows source port 0 when no reply is expected, which is the one-way traffic OPS is meant for.
With this change, ip_vs_try_to_schedule() returns NF_ACCEPT for these datagrams. They go to the director's local stack and are no longer load balanced. Could ONE_PACKET UDP services be exempted from this check?
Quoted 3 lines
net/netfilter/ipvs/ip_vs_proto_udp.c +0
-0if (likely (!ip_vs_iph_inverse (iph))) svc =ip_vs_service_find (ipvs, af, skb->mark, iph->protocol, &iph->daddr, ports[1 ]);
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261103140308.591690-3-pablo%40netfilter.org
Julian Anastasov
maintainerRe: [PATCH net 05/15] ipvs: do not create conns from packets with port 0
Hello,
This patch will need new version.
Quoted 89 lines · On Wed, 4 Nov 2026, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.
Critical: 0 · High: 0 · Medium: 3 · Low: 0
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [Medium] The subject of this patch says IPVS should not create connections from packets with port 0.
- [Medium]udp_conn_schedule()(net/netfilter/ipvs/ip_vs_proto_udp.c:55-57) now returns 1 for any UDP…
- [Medium] The new port-0 earlyreturn 1intcp_conn_schedule()(ip_vs_proto_tcp.c:66-68), udp_conn_schedule()…--
ipvs: do not create conns from packets with port 0
This commit stops the TCP, UDP and SCTP
conn_schedulehandlers andip_vs_new_conn_out()from creating normal connections when the client port or virtual port is 0. Such connections can never be matched later because of theNO_CPORTlookup rule, so a new duplicate connection is created for every packet.Quoted 4 lines
net/netfilter/ipvs/ip_vs_core.c +0
-0[ ... ]
net/netfilter/ipvs/ip_vs_core.c struct ip_vs_conn *ip_vs_new_conn_out(struct ip_vs_service *svc, · line 1335
Quoted 9 lines
net/netfilter/ipvs/ip_vs_core.c +1
-1@@ -1328,7 +1329,7 @@ struct ip_vs_conn *ip_vs_new_conn_out(struct ip_vs_service *svc,/* check pre-requisites are satisfied */ if (svc->fwmark)return NULL ; -if (!vport || !dport) +if (!vport || !dport || !cport)return NULL ;/* for persistent service first create connection template */ [Severity: Medium]
The FTP application helper also creates connections from packet data. Does it need the same port 0 check?In
ip_vs_ftp_in(), a client on an established FTP control connection can send "PORT a,b,c,d,0,0". The legacy branch ofip_vs_ftp_get_addrport()returns success with port 0:net/netfilter/ipvs/ip_vs_ftp.c:ip_vs_ftp_get_addrport() { ... *port = get_unaligned((__be16 *) (p + 4)); return 1; ... }
ip_vs_ftp_in()then uses that value as the cport. It creates a normal connection withoutIP_VS_CONN_F_NO_CPORT:net/netfilter/ipvs/ip_vs_ftp.c:ip_vs_ftp_in() { ... ip_vs_conn_fill_param(cp->ipvs, cp->af, ipvsh->protocol, &to, port, &cp->vaddr, htons(ntohs(cp->vport)-1), &p); n_cp = ip_vs_conn_in_get(&p); if (!n_cp) { n_cp = ip_vs_conn_new(&p, cp->af, &cp->daddr, htons(ntohs(cp->dport)-1), IP_VS_CONN_F_NFCT, cp->dest, skb->mark); ... }
__ip_vs_conn_in_get()only matches when this holds:(!p->cport ^ (!(cp->flags & IP_VS_CONN_F_NO_CPORT)))So a lookup with cport 0 never finds this entry.
Wouldn't every such PORT command then add another duplicate connection? That is the same problem the commit message describes.
Quoted 7 lines
The extended EPRT/EPSV branch of the same function already rejects port 0, so the two formats handle it differently:
if (s == data_limit || !hport || *s != edelim) return -1;[ ... ]
Port 0 in ip_vs_ftp.c is already handled with a pending patch
"ipvs: reject FTP control ports as data ports".
Quoted 50 lines
Quoted 5 lines
net/netfilter/ipvs/ip_vs_proto_tcp.c +0
-0[ ... ]
net/netfilter/ipvs/ip_vs_proto_tcp.c tcp_conn_schedule(struct netns_ipvs *ipvs, int af, struct sk_buff *skb, · line 69
Quoted 8 lines
net/netfilter/ipvs/ip_vs_proto_tcp.c +4
-0@@ -62,6 +63,10 @@ tcp_conn_schedule(struct netns_ipvs *ipvs, int af, struct sk_buff *skb,return 0 ; } +/* Do not create connection for port 0, skip the packet */ +if (!ports[0 ] || !ports[1 ]) +return 1 ; +[Severity: Medium]
Does this change where port 0 packets sent to a configured VIP:port end up?This check runs before
ip_vs_service_find(). Returning 1 while *cpp is still NULL makesip_vs_try_to_schedule()accept the packet:net/netfilter/ipvs/ip_vs_core.c:ip_vs_try_to_schedule() { ... if (unlikely(!*cpp)) { ... *verdict = NF_ACCEPT; return 0; } ... }Before this patch, a SYN with source port 0 to a VIP:port that has a service was always handled by IPVS. It was either scheduled to a real server or passed to
ip_vs_leave(). When no destination was available,ip_vs_leave()did this:icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); return NF_DROP;Now the packet goes to the director's own TCP stack instead. If the director has a local listener on the same port (for example bound to the wildcard address), can a remote sender using source port 0 now complete a handshake with it through the VIP? IPVS used to shadow that listener. The local TCP receive path does not appear to reject
th->source== 0.
Quoted 6 lines
udp_conn_schedule()gets the same early return, and so doessctp_conn_schedule()for INIT chunks.Would dropping port 0 packets that match a service, instead of accepting them, avoid the duplicate connections without changing where this traffic goes?
Ok, we can change it to drop the packet from port 0
Quoted 42 lines
Quoted 10 lines
net/netfilter/ipvs/ip_vs_proto_tcp.c +0
-0/* No !th->ack check to allow scheduling on SYN+ACK for Active FTP */ if (likely (!ip_vs_iph_inverse (iph)))net/netfilter/ipvs/ip_vs_proto_udp.c +0
-0net/netfilter/ipvs/ip_vs_proto_udp.c udp_conn_schedule(struct netns_ipvs *ipvs, int af, struct sk_buff *skb, · line 58
Quoted 7 lines
net/netfilter/ipvs/ip_vs_proto_udp.c +4
-0@@ -52,6 +52,10 @@ udp_conn_schedule(struct netns_ipvs *ipvs, int af, struct sk_buff *skb,return 0 ; } +/* Do not create connection for port 0, skip the packet */ +if (!ports[0 ] || !ports[1 ]) +return 1 ; +[Severity: Medium]
Does this check also need to cover one-packet scheduling (OPS) UDP services?It runs before the service lookup, so it also applies to services with
IP_VS_SVC_F_ONEPACKET.ip_vs_schedule()setsIP_VS_CONN_F_ONE_PACKETon those connections:flags = (svc->flags & IP_VS_SVC_F_ONEPACKET && iph->protocol == IPPROTO_UDP) ? IP_VS_CONN_F_ONE_PACKET : 0;
ip_vs_conn_hash()never inserts such connections into the table:net/netfilter/ipvs/ip_vs_conn.c:ip_vs_conn_hash() { ... if (cp->flags & IP_VS_CONN_F_ONE_PACKET) return 0; ... }So they cannot cause the duplicate hashed connections this patch targets.
Quoted 7 lines
Before this patch, an OPS service forwarded UDP datagrams with source port 0 to a real server. RFC 768 allows source port 0 when no reply is expected, which is the one-way traffic OPS is meant for.
With this change,
ip_vs_try_to_schedule()returnsNF_ACCEPTfor these datagrams. They go to the director's local stack and are no longer load balanced. CouldONE_PACKETUDP services be exempted from this check?
Too complex to do, the ONE_PACKET flag may be set indest->conn_flags, not just in svc->flags, so after matching the service and selecting a destination, i.e. while creating connection.
Quoted 5 lines
net/netfilter/ipvs/ip_vs_proto_udp.c udp_conn_schedule(struct netns_ipvs *ipvs, int af, struct sk_buff *skb, · line 61
Quoted 4 lines
net/netfilter/ipvs/ip_vs_proto_udp.c +0
-0if (likely (!ip_vs_iph_inverse (iph))) svc =ip_vs_service_find (ipvs, af, skb->mark, iph->protocol, &iph->daddr, ports[1 ]);
pw-bot: cr
Quoted 2 lines
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261103140308.591690-3-pablo%40netfilter.org
Regards
--
Julian Anastasov <[email protected]>
Pablo Neira Ayuso
maintainerPATCH v2 6/15 netfilter: nf_conntrack: limit expectation master chain depth
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
A conntrack can point to a master conntrack, which itself may point to another master, and so on. There is currently no bound on how deep this chain can grow.
Force a max depth in nf_ct_expect_alloc(): walk the master chain and reject allocation if it would exceed 2 levels of nesting.
Also add ctnetlink_find_get_master and use it so that ctnetlink create paths return -EMLINK instead of falling through to nf_ct_expect_alloc() and returning a misleading -ENOMEM.
This prevents unbounded ct->master->master->... chains.
--- include/net/netfilter/nf_conntrack_helper.h | 15 +++++++++ net/netfilter/nf_conntrack_expect.c | 3 ++ net/netfilter/nf_conntrack_netlink.c | 36 +++++++++++++++------ 3 files changed, 44 insertions(+), 10 deletions(-)
include/net/netfilter/nf_conntrack_helper.h +15 -0
@@ -29,6 +29,8 @@ enum nf_ct_helper_flags {#define NF_CT_HELPER_NAME_LEN 16 +#define NF_CT_MAX_EXPECT_CHAIN_LEN 2 +/* Must be kept in sync with the classes defined by helpers */ #define NF_CT_MAX_EXPECT_CLASSES 4
@@ -159,6 +161,19 @@ static inline void nf_ct_help_put(const struct nf_conn *ct)kfree_rcu (helper, rcu); } +static inline bool nf_ct_master_acceptable (const struct nf_conn *m) +{ +unsigned int depth =0 ; + +while (m->master) { +if (++depth >=NF_CT_MAX_EXPECT_CHAIN_LEN ) +return false; + m = m->master; + } + +return true; +} +int nf_conntrack_helper_init (void );void nf_conntrack_helper_fini (void );
net/netfilter/nf_conntrack_expect.c +3 -0
@@ -306,6 +306,9 @@ struct nf_conntrack_expect *nf_ct_expect_alloc(struct nf_conn *me) {struct nf_conntrack_expect *new; +if (!nf_ct_master_acceptable (me)) +return NULL ; + new =kmem_cache_zalloc (nf_ct_expect_cachep,GFP_ATOMIC );if (!new)return NULL ;
net/netfilter/nf_conntrack_netlink.c +26 -10
@@ -2231,6 +2231,26 @@ ctnetlink_change_conntrack(struct nf_conn *ct,return 0 ; } +static struct nf_conn * +ctnetlink_find_get_master (struct net *net,const struct nf_conntrack_zone *zone, +const struct nf_conntrack_tuple *tuple) +{ +struct nf_conntrack_tuple_hash *master_h; +struct nf_conn *master_ct; + + master_h =nf_conntrack_find_get (net, zone, tuple); +if (!master_h) +return ERR_PTR (-ENOENT ); + + master_ct =nf_ct_tuplehash_to_ctrack (master_h); +if (!nf_ct_master_acceptable (master_ct)) { +nf_ct_put (master_ct); +return ERR_PTR (-EMLINK ); + } + +return master_ct; +} +static struct nf_conn * ctnetlink_create_conntrack(struct net *net, const struct nf_conntrack_zone *zone,
@@ -2356,7 +2376,6 @@ ctnetlink_create_conntrack(struct net *net,/* setup master conntrack: this is a confirmed expectation */ if (cda[CTA_TUPLE_MASTER ]) {struct nf_conntrack_tuple master; -struct nf_conntrack_tuple_hash *master_h;struct nf_conn *master_ct; err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER,
@@ -2364,12 +2383,11 @@ ctnetlink_create_conntrack(struct net *net,if (err <0 ) goto err2; - master_h =nf_conntrack_find_get (net, zone, &master); -if (master_h ==NULL ) { - err = -ENOENT ; + master_ct =ctnetlink_find_get_master (net, zone, &master); +if (IS_ERR (master_ct)) { + err =PTR_ERR (master_ct); goto err2; } - master_ct =nf_ct_tuplehash_to_ctrack (master_h);__set_bit (IPS_EXPECTED_BIT , &ct->status); ct->master = master_ct; }
@@ -3613,7 +3631,6 @@ ctnetlink_create_expect(struct net *net, u_int8_t u3, u32 portid, int report) { struct nf_conntrack_tuple tuple, mask, master_tuple; -struct nf_conntrack_tuple_hash *h =NULL ;struct nf_conntrack_expect *exp;struct nf_conn *ct;int err;
@@ -3633,10 +3650,9 @@ ctnetlink_create_expect(struct net *net,return err;/* Look for master conntrack of this expectation */ - h =nf_conntrack_find_get (net, zone, &master_tuple); -if (!h) -return -ENOENT ; - ct =nf_ct_tuplehash_to_ctrack (h); + ct =ctnetlink_find_get_master (net, zone, &master_tuple); +if (IS_ERR (ct)) +return PTR_ERR (ct);rcu_read_lock (); exp =ctnetlink_alloc_expect (cda, ct,NULL , &tuple, &mask);
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH net 06/15] netfilter: nf_conntrack: limit expectation master chain depth
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.
Critical: 0 · High: 1 · Medium: 0 · Low: 2
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [High] The new global limit NF_CT_MAX_EXPECT_CHAIN_LEN = 2, enforced by nf_ct_master_acceptable() inside nf_ct_expect_alloc(), is lower than…
- [Low] The commit message says only that the patch 'prevents unbounded ct->master->master->... chains'.
- [Low] The commit message describes ctnetlink_find_get_master() as making 'ctnetlink create paths return -EMLINK instead of falling…
--
netfilter: nf_conntrack: limit expectation master chain depth
This commit adds nf_ct_master_acceptable(), which walks the ct->master chain. It is called from nf_ct_expect_alloc() so that no expectation can be created once the chain reaches NF_CT_MAX_EXPECT_CHAIN_LEN (2). The commit also adds ctnetlink_find_get_master(), so the ctnetlink create paths reject such masters with -EMLINK.
Quoted 3 lines
Also add
ctnetlink_find_get_masterand use it so that ctnetlink create paths return-EMLINKinstead of falling through tonf_ct_expect_alloc()and returning a misleading-ENOMEM.
[Severity: Low]
Is this description accurate for ctnetlink_create_conntrack()? That path never reaches nf_ct_expect_alloc(). For CTA_TUPLE_MASTER it assigns the master directly:
net/netfilter/nf_conntrack_netlink.c:ctnetlink_create_conntrack() {
...
__set_bit(IPS_EXPECTED_BIT, &ct->status);
ct->master = master_ct;
...
}So on that path, the check in ctnetlink_find_get_master() is the only depth enforcement. It does more than fix an errno.
Could the commit message say this, so the hunk isn't read as cosmetic?
The misleading -ENOMEM also remains for the other nf_ct_expect_alloc() callers, because a NULL return now means either OOM or depth rejection:
- ctnetlink_alloc_expect() still maps NULL to ERR_PTR(-ENOMEM). It is also reached from ctnetlink_glue_attach_expect() (nfqueue NFQA_EXP), which has no -EMLINK pre-check. nfqnl_ct_parse() ignores that return value, though.
- The ftp, irc, sane, tftp, amanda and sip helpers log "cannot alloc expectation" and return NF_DROP.
- nft_ct_expect_obj_eval() returns NF_DROP.
Quoted 1 line
This prevents unbounded ct->master->master->... chains.
[Severity: Low]
Should the commit message say what an unbounded chain leads to? nf_ct_destroy() releases the master before it frees the ct, so the release is not a tail call:
net/netfilter/nf_conntrack_core.c:nf_ct_destroy() {
...
if (ct->master)
nf_ct_put(ct->master);
nf_conntrack_free(ct);
}Suppose the masters have already left the hash table and each child holds the last reference to its master. Freeing the leaf then recurses nf_ct_put()->nf_ct_destroy()->nf_ct_put()... once per level.
Before this patch, only nf_conntrack_max bounded the depth. There are two ways to build a long chain:
- ctnetlink CTA_TUPLE_MASTER, which needs CAP_NET_ADMIN (available inside a user namespace).
- Q.931 call forwarding from the network. expect_callforwarding() has no loop guard, unlike process_gcf().
This looks like a fix for kernel stack exhaustion, and it has a Reported-by. Should it also carry a Fixes: tag and Cc: stable? The other netfilter fixes in this batch carry Fixes: tags. Backporters would also want to know about the H.323 behaviour change described below.
Quoted 19 lines
include/net/netfilter/nf_conntrack_helper.h +4
-0@@ -29,6 +29,8 @@ enum nf_ct_helper_flags {#define NF_CT_HELPER_NAME_LEN 16 +#define NF_CT_MAX_EXPECT_CHAIN_LEN 2 +/* Must be kept in sync with the classes defined by helpers */ #define NF_CT_MAX_EXPECT_CLASSES 4@@ -159,6 +161,19 @@ static inline void nf_ct_help_put(const struct nf_conn *ct)kfree_rcu (helper, rcu); } +static inline bool nf_ct_master_acceptable (const struct nf_conn *m) +{
include/net/netfilter/nf_conntrack_helper.h static inline void nf_ct_help_put(const struct nf_conn *ct) · line 175
Quoted 10 lines
include/net/netfilter/nf_conntrack_helper.h +10
-0+unsigned int depth =0 ; + +while (m->master) { +if (++depth >=NF_CT_MAX_EXPECT_CHAIN_LEN ) +return false; + m = m->master; + } + +return true; +}
[Severity: High]
Is a limit of 2 enough for the in-tree H.323 helper? With this check, a ct that has a grandmaster can no longer create expectations. Normal gatekeeper operation builds a deeper chain than that:
RAS ct
expect_q931() / process_acf() / process_lcf() (assign_helper = q931)
Q.931 ct (master = RAS, set by init_conntrack())
expect_h245() (assign_helper = h245)
H.245 ct (master = Q.931, grandmaster = RAS)
Now suppose the H.245 ct sees an OpenLogicalChannel:
h245_help()
process_h245()
process_olc()
process_h245_channel()
expect_rtp_rtcp() / expect_t120()
nf_ct_expect_alloc(H.245 ct)
nf_ct_master_acceptable() -> falseexpect_rtp_rtcp() then fails:
if ((rtp_exp = nf_ct_expect_alloc(ct)) == NULL) return -1;
and h245_help() drops the packet:
drop: spin_unlock_bh(&nf_h323_lock); nf_ct_helper_log(skb, ct, "cannot process H.245 message"); return NF_DROP;
Wouldn't this drop the H.245 OLC packets of gatekeeper-admitted calls? RTP, RTCP and T.120 would then never get expectations.
The GCF case fails one level earlier. In process_gcf(), a discovery RAS ct expects a registration RAS ct. The helper already bounds this recursion itself ("Avoid RAS expectation loops").
If the registration RAS then expects Q.931, that Q.931 ct already has two ancestors. Its expect_h245() and fastStart process_olc() calls fail, and q931_help() returns NF_DROP with "cannot process Q.931 message".
Does the bound need to be at least 3, or 4 for the GCF plus H.245 plus RTP case? Or could the limit apply only to the self-recursive cases, such as Q.931 to Q.931 call forwarding via expect_callforwarding()?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261103140308.591690-3-pablo%40netfilter.org
Pablo Neira Ayuso
maintainerPATCH v2 7/15 netfilter: nf_dup: preserve guard across NFQUEUE reinjection
In reply to Pablo Neira AyusoFrom: Zihan Xi <[email protected]>
NFQUEUE can suspend traversal of a duplicated packet while the task-local duplication guard is set. The output call then returns and clears the guard before a later verdict resumes hook traversal.
Save the guard in the queue entry and restore it while reinjection continues hook traversal. Preserve and restore the verdict task's prior state, including nested synchronous reinjection.
--- include/net/netfilter/nf_queue.h | 1 + net/netfilter/nf_queue.c | 1 + net/netfilter/nfnetlink_queue.c | 17 ++++++++++++++--- 3 files changed, 16 insertions(+), 3 deletions(-)
include/net/netfilter/nf_queue.h +1 -0
@@ -23,6 +23,7 @@ struct nf_queue_entry {struct net_device *physout;#endif struct nf_hook_state state; +bool in_nf_duplicate;bool nf_ct_is_unconfirmed;u16 size;/* sizeof(entry) + saved route keys */
net/netfilter/nf_queue.c +1 -0
@@ -219,6 +219,7 @@ static int __nf_queue(struct sk_buff *skb, const struct nf_hook_state *state, *entry = (struct nf_queue_entry ) { .skb = skb, .skb_dev = skb->dev, + .in_nf_duplicate = current->in_nf_duplicate, .state = *state, .hook_index = index, .size =sizeof (*entry) + route_key_size,
net/netfilter/nfnetlink_queue.c +14 -3
@@ -44,6 +44,7 @@#include <net/sock.h> #include <net/tcp_states.h> #include <net/netfilter/nf_queue.h> +#include <net/net_debug.h> #include <net/netns/generic.h> #include <linux/atomic.h>
@@ -365,21 +366,30 @@ static void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)const struct nf_hook_entry *hook_entry;const struct nf_hook_entries *hooks;struct sk_buff *skb = entry->skb; +bool old_in_nf_duplicate;const struct net *net;unsigned int i;int err;u8 pf; +DEBUG_NET_WARN_ONCE (current->in_nf_duplicate && + !entry->in_nf_duplicate, +"%s called with unexpected duplication guard" , + __func__); +local_bh_disable (); + old_in_nf_duplicate = current->in_nf_duplicate; + net = entry->state.net; pf = entry->state.pf; + current->in_nf_duplicate = entry->in_nf_duplicate; hooks =nf_hook_entries_head (net, pf, entry->state.hook); i = entry->hook_index;if (!hooks || i >= hooks->num_hook_entries) {kfree_skb_reason (skb,SKB_DROP_REASON_NETFILTER_DROP );nf_queue_entry_free (entry); -return ; + goto out; } hook_entry = &hooks->hooks[i];
@@ -402,9 +412,7 @@ static void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)switch (verdict &NF_VERDICT_MASK ) {case NF_ACCEPT :case NF_STOP : -local_bh_disable (); entry->state.okfn (entry->state.net, entry->state.sk, skb); -local_bh_enable ();break ;case NF_QUEUE : err =nf_queue (skb, &entry->state, i, verdict);
@@ -418,6 +426,9 @@ static void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict) }nf_queue_entry_free (entry); +out: + current->in_nf_duplicate = old_in_nf_duplicate; +local_bh_enable (); }/* return true if the entry has an unconfirmed conntrack attached that isn't owned by us
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH net 07/15] netfilter: nf_dup: preserve guard across NFQUEUE reinjection
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] The commit message for "netfilter: nf_dup: preserve guard across NFQUEUE reinjection" leaves out a behaviour change.
- [Low] The patch has Cc: [email protected] and Fixes: cd58bcd9787e (2010, xt_TEE), but it reads and writes current->in_nf_duplicate.
--
netfilter: nf_dup: preserve guard across NFQUEUE reinjection
This patch saves current->in_nf_duplicate in struct nf_queue_entry when a packet is queued. nf_reinject() then restores it while hook traversal resumes after a verdict, and puts back the verdict task's previous value on exit. BH is now disabled for the whole of nf_reinject().
Quoted 2 lines
Fixes cd58bcd9787e ("netfilter: xt_TEE: have cloned packet travel through Xtables too")
[Severity: Low]
Will this apply to the stable trees that this Fixes tag covers?
__nf_queue() and nf_reinject() read and write current->in_nf_duplicate. That task_struct bit was only added by:
a1f1acb9c5db ("netfilter: nf_dup{4, 6}: Move duplication check to task_struct")
Older stable trees covered by the 2010 Fixes tag, such as 6.12, 6.6, 6.1, 5.15 and 5.10, still use the per-CPU nf_skb_duplicated guard. That flag is also cleared after ip_local_out() returns, so the same bug seems to exist there. This patch won't build against those trees, though.
Could a dependency note or backport hint be added? That would make it clear those trees need a version that saves and restores the per-CPU flag instead.
Quoted 4 lines
net/netfilter/nfnetlink_queue.c +0
-0
[ ... ]
Quoted 17 lines
net/netfilter/nfnetlink_queue.c +8
-0@@ -365,21 +366,30 @@ static void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)const struct nf_hook_entry *hook_entry;const struct nf_hook_entries *hooks;struct sk_buff *skb = entry->skb; +bool old_in_nf_duplicate;const struct net *net;unsigned int i;int err;u8 pf; +DEBUG_NET_WARN_ONCE (current->in_nf_duplicate && + !entry->in_nf_duplicate, +"%s called with unexpected duplication guard" , + __func__); +local_bh_disable (); + old_in_nf_duplicate = current->in_nf_duplicate; +
[ ... ]
Quoted 8 lines
net/netfilter/nfnetlink_queue.c +0
-2@@ -402,9 +412,7 @@ static void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)switch (verdict &NF_VERDICT_MASK ) {case NF_ACCEPT :case NF_STOP : -local_bh_disable (); entry->state.okfn (entry->state.net, entry->state.sk, skb); -local_bh_enable ();break ;
[ ... ]
net/netfilter/nfnetlink_queue.c static void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict) · line 432
Quoted 8 lines
net/netfilter/nfnetlink_queue.c +3
-0@@ -418,6 +426,9 @@ static void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict) }nf_queue_entry_free (entry); +out: + current->in_nf_duplicate = old_in_nf_duplicate; +local_bh_enable (); }
[Severity: Low]
This isn't a bug, but the commit message doesn't mention that the BH-disabled region in nf_reinject() is now much wider.
Before this patch, local_bh_disable() and local_bh_enable() only wrapped entry->state.okfn() in the NF_ACCEPT and NF_STOP case. Now every reinjected NFQUEUE packet runs with BH disabled, duplicated or not. That covers the NF_REPEAT hookfn re-run, nf_reroute(), nf_iterate() and the nf_queue() re-queue path.
The change looks needed, because a softirq running on the current task must not see the guard while it is set. Could the commit message say so and explain why?
The message also only says that the guard is cleared:
"The output call then returns and clears the guard before a later verdict resumes hook traversal."
It doesn't describe what users see. nf_dup_ipv4() clears the guard unconditionally after the output call, and nf_dup_ipv6() does the same:
net/ipv4/netfilter/nf_dup_ipv4.c:nf_dup_ipv4() {
...
if (nf_dup_ipv4_route(net, skb, gw, oif)) {
current->in_nf_duplicate = true;
ip_local_out(net, skb->sk, skb);
current->in_nf_duplicate = false;
...
}Say the duplicate is queued in LOCAL_OUT or POSTROUTING and reinjected later. A TEE or nft dup rule further along then duplicates it again. That new copy can be queued again, so each verdict makes another copy.
Could the changelog describe this behaviour?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261103140308.591690-3-pablo%40netfilter.org
Pablo Neira Ayuso
maintainerPATCH v2 8/15 netfilter: flowtable: extend ct timeout of offloaded GRE flows
In reply to Pablo Neira AyusoFrom: Michail Andreas Michalopoulos <[email protected]>
Since commit 03428ca5cee9 ("netfilter: conntrack: rework offload nf_conn timeout extension logic"), the flowtable gc worker extends the timeout of an offloaded conntrack entry from nf_flow_table_extend_ct_timeout() once it falls below 5 minutes. That function only knows about TCP and UDP and hits WARN_ON_ONCE() for any other layer 4 protocol. However, GRE flows can be offloaded too, both via nft_flow_offload and act_ct, since commit 4e8d9584d154 ("netfilter: flowtable: Support GRE").
The timeout of an offloaded GRE entry drops below that threshold as soon as a packet of the flow still goes through conntrack. With ip6gre tunnels using the default encapsulation limit this is ordinary traffic:
the flowtable does not parse the Destination Options header in front of GRE, so every packet of the flow takes the classic forwarding path. It also happens for a packet with ttl <= 1, which the flowtable fast path passes up to the classic forwarding path. nf_conntrack_gre_packet() then refreshes ct->timeout to the GRE stream timeout (180 seconds by default), and the next gc run triggers, as reported by syzbot:
WARNING: net/netfilter/nf_flow_table_core.c:553 at nf_flow_table_extend_ct_timeout+0x1ee/0x2f0 Workqueue: events_power_efficient nf_flow_offload_work_gc Call Trace: nf_flow_offload_gc_step+0x545/0x7b0 nf_flow_table_iterate+0x13c/0x1f0 nf_flow_offload_work_gc+0x22/0x70 process_scheduled_works+0xc3d/0x1630 worker_thread+0xa47/0xfb0 kthread+0x38b/0x480
Besides the splat, which panics the machine with panic_on_warn, the default branch sets the new timeout to 1 jiffy, so the conntrack entry expires right away and the offloaded flow is torn down.
Handle GRE like UDP and extend the timeout to NF_CT_DAY. GRE conntrack has no protocol state to take into account, and NF_CT_DAY is also what flow_offload_add() already sets for every offloaded entry, GRE included. This also restores the behaviour from before commit 03428ca5cee9, when the conntrack gc worker extended offloaded entries of any protocol to NF_CT_DAY.
--- net/netfilter/nf_flow_table_core.c | 1 + 1 file changed, 1 insertion(+)
net/netfilter/nf_flow_table_core.c +1 -0
@@ -544,6 +544,7 @@ static void nf_flow_table_extend_ct_timeout(struct nf_conn *ct)switch (l4proto) {case IPPROTO_UDP : +case IPPROTO_GRE : new_timeout =NF_CT_DAY ;break ;case IPPROTO_TCP :
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH v2 9/15 netfilter: nft_set_rbtree: check catchall_sync return before queue_done
In reply to Pablo Neira AyusoFrom: Xingyuan Mo <[email protected]>
nft_rbtree_gc_queue() passes the result of nft_trans_gc_catchall_sync() straight to nft_trans_gc_queue_sync_done(). When the catchall path flushes a full GC batch and nft_trans_gc_alloc() fails, catchall_sync returns NULL and queue_sync_done dereferences trans->net (NULL+0x10). Guard the call like the pipapo GC path already does.
--- net/netfilter/nft_set_rbtree.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
net/netfilter/nft_set_rbtree.c +2 -1
@@ -953,7 +953,8 @@ static void nft_rbtree_gc_queue(struct nft_set *set) } gc =nft_trans_gc_catchall_sync (gc); -nft_trans_gc_queue_sync_done (gc); +if (gc) +nft_trans_gc_queue_sync_done (gc); } static u64 nft_rbtree_privsize(const struct nlattr * const nla[],
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH v2 10/15 netfilter: bridge: use DEBUG_NET_WARN_ON_ONCE in nf_ct_bridge_refrag()
In reply to Pablo Neira AyusoFrom: Laveesh Bansal <[email protected]>
nf_ct_bridge_refrag() warns when skb->protocol is neither IPv4 nor IPv6. A vlan filtering bridge can get there with a vlan ethertype: if the protocol of the tag of a frame differs from the vlan_protocol of the bridge, br_allowed_ingress() moves the tag back into the packet after NF_BR_PRE_ROUTING, where bridge conntrack has already defragmented it.
Anyone who can send fragments into a port of such a bridge can trigger the warning, which takes the machine down with panic_on_warn. The packet is dropped either way, so only warn on kernels built with CONFIG_DEBUG_NET.
This only avoids the warning. Such fragmented packets are still dropped. syzbot reported the warning. It keeps triggering on CONFIG_DEBUG_NET kernels until the vlan tag is handled, which is left for a follow-up:
https://syzkaller.appspot.com/bug?extid=a8ba738fe2db6b4bb27f
--- net/bridge/netfilter/nf_conntrack_bridge.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/bridge/netfilter/nf_conntrack_bridge.c +1 -1
@@ -358,7 +358,7 @@ nf_ct_bridge_refrag(struct sk_buff *skb, const struct nf_hook_state *state,nf_br_ip6_fragment (state->net, state->sk, skb, &data, output);break ;default : -WARN_ON_ONCE (1 ); +DEBUG_NET_WARN_ON_ONCE (1 );return NF_DROP ; }
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH v2 11/15 netfilter: x_tables: check target name before accessing verdict
In reply to Pablo Neira AyusoFrom: Fernando Fernandez Mancera <[email protected]>
Non-standard targets may contain only struct xt_entry_target. mark_source_chains() nevertheless reads the target as an xt_standard_target before checking its name, potentially reading four bytes beyond a header-only target.
Check that the target is standard before reading its verdict. Non-standard targets remain handled as fallthrough rules.
See the following KASAN trace:
BUG: KASAN: slab-out-of-bounds in translate_table+0x1660/0x1a40 [ip6_tables] Read of size 4 at addr ffff88800ed56ff8 by task rep/432 Call Trace: kasan_report+0xfa/0x120 translate_table+0x1660/0x1a40 [ip6_tables] do_ip6t_set_ctl+0x667/0xa76 [ip6_tables] nf_setsockopt+0x77/0xd0 do_sock_setsockopt+0x118/0x490 __sys_setsockopt+0xe4/0x160 __x64_sys_setsockopt+0xbd/0x190 do_syscall_64+0xde/0x4f0 entry_SYSCALL_64_after_hwframe+0x77/0x7f Allocated by task 432: kasan_save_stack+0x2f/0x50 __kasan_kmalloc+0x7f/0x90 __kvmalloc_node_noprof+0x1b1/0x5c0 xt_alloc_table_info+0x3c/0xa0 do_ip6t_set_ctl+0x5ee/0xa76 [ip6_tables] nf_setsockopt+0x77/0xd0 do_sock_setsockopt+0x118/0x490 __sys_setsockopt+0xe4/0x160 __x64_sys_setsockopt+0xbd/0x190 do_syscall_64+0xde/0x4f0 entry_SYSCALL_64_after_hwframe+0x77/0x7f
--- net/ipv4/netfilter/arp_tables.c | 5 +++-- net/ipv4/netfilter/ip_tables.c | 5 +++-- net/ipv6/netfilter/ip6_tables.c | 5 +++-- 3 files changed, 9 insertions(+), 6 deletions(-)
net/ipv4/netfilter/arp_tables.c +3 -2
@@ -371,11 +371,12 @@ static int mark_source_chains(const struct xt_table_info *newinfo, e->counters.pcnt = pos; pos += size; }else { -int newpos = t->verdict; +int newpos;if (strcmp (t->target.u.user.name,XT_STANDARD_TARGET ) ==0 && - newpos >=0 ) { + t->verdict >=0 ) { + newpos = t->verdict;/* This a jump; chase it. */ if (!xt_find_jump_offset (offsets, newpos, newinfo->number))
net/ipv4/netfilter/ip_tables.c +3 -2
@@ -422,11 +422,12 @@ mark_source_chains(const struct xt_table_info *newinfo, e->counters.pcnt = pos; pos += size; }else { -int newpos = t->verdict; +int newpos;if (strcmp (t->target.u.user.name,XT_STANDARD_TARGET ) ==0 && - newpos >=0 ) { + t->verdict >=0 ) { + newpos = t->verdict;/* This a jump; chase it. */ if (!xt_find_jump_offset (offsets, newpos, newinfo->number))
net/ipv6/netfilter/ip6_tables.c +3 -2
@@ -440,11 +440,12 @@ mark_source_chains(const struct xt_table_info *newinfo, e->counters.pcnt = pos; pos += size; }else { -int newpos = t->verdict; +int newpos;if (strcmp (t->target.u.user.name,XT_STANDARD_TARGET ) ==0 && - newpos >=0 ) { + t->verdict >=0 ) { + newpos = t->verdict;/* This a jump; chase it. */ if (!xt_find_jump_offset (offsets, newpos, newinfo->number))
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH v2 12/15 netfilter: nf_conntrack_sip: fix OOB read in ct_sip_parse_transport()
In reply to Pablo Neira AyusoFrom: Joas Antonio dos Santos <[email protected]>
ct_sip_parse_transport() compares the value of the "transport=" header parameter against "TCP"/"UDP" with strncasecmp(..., 3), but it never checks that the parsed value is at least 3 bytes long.
ct_sip_parse_param() returns matchlen = end - start, where end is the next ';' or, when there is no ';', the end of the payload buffer (limit = dptr + datalen). If the "transport=" value is only 1 or 2 bytes long and runs exactly to the end of the payload (e.g. a REGISTER request whose Contact header ends in ";transport=T" with no trailing CRLF), matchlen is less than 3 while strncasecmp() still reads up to 3 bytes, reading 1-2 bytes past the end of the linearized skb data.
Add a matchlen check before the comparison, mirroring the sdp_media_type() pattern a few lines below, so short values cannot reach strncasecmp().
The bug was found by LLM-assisted manual review of the SIP helper's parsers and confirmed with a userspace ASan harness built from the verbatim parser functions, which reports a heap-buffer-overflow read of size 2 in strncasecmp() called from ct_sip_parse_transport().
--- net/netfilter/nf_conntrack_sip.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/netfilter/nf_conntrack_sip.c +2 -2
@@ -701,9 +701,9 @@ static int ct_sip_parse_transport(struct nf_conn *ct, const char *dptr,if (ct_sip_parse_param (ct, dptr, dataoff, datalen,"transport=" , &matchoff, &matchlen)) { -if (!strncasecmp (dptr + matchoff,"TCP" ,strlen ("TCP" ))) +if (matchlen >=3 && !strncasecmp (dptr + matchoff,"TCP" ,3 )) *proto =IPPROTO_TCP ; -else if (!strncasecmp (dptr + matchoff,"UDP" ,strlen ("UDP" ))) +else if (matchlen >=3 && !strncasecmp (dptr + matchoff,"UDP" ,3 )) *proto =IPPROTO_UDP ;else return 0 ;
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH v2 13/15 netfilter: nf_tables: fix memleak when rule deletion fails with hardware offload
In reply to Pablo Neira AyusoAllocate flow rule object after transaction has been allocation and rule deactivation has been performed, otherwise memleak is possible. Let the transaction unwind the existing rule deactivation from the abort path, this needs to update nft_flow_rule_destroy() to skip release if flow_rule is null.
While at this, remove check for non-null flow_rule in newrule error path now that nft_flow_rule_destroy() is skipped if flow_rule is null.
--- net/netfilter/nf_tables_api.c | 21 +++++++++------------ net/netfilter/nf_tables_offload.c | 3 +++ 2 files changed, 12 insertions(+), 12 deletions(-)
net/netfilter/nf_tables_api.c +9 -12
@@ -760,16 +760,6 @@ static int nft_delrule(struct nft_ctx *ctx, struct nft_rule *rule)if (trans ==NULL )return -ENOMEM ; -if (ctx->chain->flags &NFT_CHAIN_HW_OFFLOAD ) { - flow =nft_flow_rule_create (ctx->net, rule); -if (IS_ERR (flow)) { -nft_trans_destroy (trans); -return PTR_ERR (flow); - } - -nft_trans_flow_rule (trans) = flow; - } - err =nf_tables_delrule_deactivate (ctx, rule);if (err <0 ) {nft_trans_destroy (trans);
@@ -777,6 +767,14 @@ static int nft_delrule(struct nft_ctx *ctx, struct nft_rule *rule) }nft_rule_expr_deactivate (ctx, rule,NFT_TRANS_PREPARE ); +if (ctx->chain->flags &NFT_CHAIN_HW_OFFLOAD ) { + flow =nft_flow_rule_create (ctx->net, rule); +if (IS_ERR (flow)) +return PTR_ERR (flow); + +nft_trans_flow_rule (trans) = flow; + } +return 0 ; }
@@ -4570,8 +4568,7 @@ static int nf_tables_newrule(struct sk_buff *skb, const struct nfnl_info *info, err_destroy_flow_rule:nft_use_dec_restore (&chain->use); err_destroy_flow: -if (flow) -nft_flow_rule_destroy (flow); +nft_flow_rule_destroy (flow); err_release_rule:nft_rule_expr_deactivate (&ctx, rule,NFT_TRANS_PREPARE_ERROR );nf_tables_rule_destroy (&ctx, rule);
net/netfilter/nf_tables_offload.c +3 -0
@@ -148,6 +148,9 @@ void nft_flow_rule_destroy(struct nft_flow_rule *flow)struct flow_action_entry *entry;int i; +if (!flow) +return ; +flow_action_for_each (i, entry, &flow->rule->action) {switch (entry->id) {case FLOW_ACTION_REDIRECT :
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH net 13/15] netfilter: nf_tables: fix memleak when rule deletion fails with hardware offload
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] The commit message calls the bug fixed in nft_delrule() (net/netfilter/nf_tables_api.c) a plain "memleak", but the old error…
--
netfilter: nf_tables: fix memleak when rule deletion fails with hardware offload
The patch moves the flow rule allocation in nft_delrule() so it runs after the rule has been deactivated. This means the -ENOENT error path no longer drops an already built flow rule. nft_flow_rule_destroy() now accepts a NULL flow, and the NULL check in the nf_tables_newrule() error path is removed.
Quoted 2 lines
Allocate flow rule object after transaction has been allocation and rule deactivation has been performed, otherwise memleak is possible. Let the
[Severity: Low]
The code change looks correct. Should the commit message also mention the net_device reference leak?
Before this patch, when nf_tables_delrule_deactivate() returned -ENOENT in nft_delrule(), only the transaction was freed:
err = nf_tables_delrule_deactivate(ctx, rule);
if (err < 0) {
nft_trans_destroy(trans);
return err;
}nft_trans_destroy() only does kfree(trans). The flow rule already stored in nft_trans_flow_rule(trans) never reached nft_flow_rule_destroy().
Take a rule in an NFT_CHAIN_HW_OFFLOAD chain that uses fwd or dup to a device. For that rule, nft_fwd_dup_netdev_offload() in net/netfilter/nf_dup_netdev.c holds a device reference that only nft_flow_rule_destroy() drops:
dev = dev_get_by_index(ctx->net, oif); ... /* nft_flow_rule_destroy() releases the reference on this device. */ entry->dev = dev;
So each failed delete leaked a netdev refcount as well as the memory. A leaked refcount can leave unregister_netdevice() waiting for the device to become free, and that blocks netns teardown.
This path is easy to reach. __nft_rule_lookup() matches only on rule->handle and does no genmask check. Deleting the same handle twice in one batch therefore hits the -ENOENT path in nft_delrule().
Could the changelog mention the netdev reference leak, so stable backporters can judge the impact of this fix?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261103140308.591690-3-pablo%40netfilter.org
Pablo Neira Ayuso
maintainerPATCH v2 14/15 netfilter: xt_recent: do not call remove_proc_entry() under recent_lock
In reply to Pablo Neira AyusoFrom: "Cen Zhang (Microsoft)" <[email protected]>
recent_proc_net_exit() removes the /proc/net/xt_recent/<name> entries of a dying network namespace while holding spin_lock_bh(&recent_lock). remove_proc_entry() may sleep: proc_entry_rundown() waits until every task that is still inside the entry's file operations has left. A reader of such an entry is counted in by proc_reg_read() before recent_seq_start() takes recent_lock. So if a read is in flight when the namespace is torn down, cleanup_net() sleeps in atomic context holding the spinlock that the reader is spinning on, and neither side can ever make progress.
An unprivileged user can trigger this from a user namespace by adding a rule with the "recent" match and reading its proc file while the namespace is being destroyed. As recent_lock is global, every later attempt to take it spins forever too: the spinning can spread to all CPUs, and network namespace teardown stops system-wide.
BUG: sleeping function called from invalid context at
kernel/sched/completion.c:116
#3: (recent_lock){+...}-{3:3}, at: recent_net_exit+0x2b/0x140
Workqueue: netns cleanup_net
Call Trace:
proc_entry_rundown+0x185/0x1b0
remove_proc_entry+0x21a/0x520
recent_net_exit+0x7f/0x140
cleanup_net+0x414/0x8f0
...
rcu: INFO: rcu_preempt self-detected stall on CPU
Call Trace:
queued_spin_lock_slowpath+0x313/0xc20
recent_seq_start+0x87/0x1b0
seq_read_iter+0x265/0x11e0
proc_reg_read+0x1ab/0x270Fix by taking recent_mutex instead of recent_lock. According to commit a0e889bb1bdc ("[NETFILTER]: recent match: fix "sleeping function called from invalid context""), recent_mutex is the lock to use here: it protects recent_net->tables and was introduced so that proc entries are created and removed without recent_lock held, because those calls may sleep. hashlimit_proc_net_exit() does the same for xt_hashlimit.
--- net/netfilter/xt_recent.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
net/netfilter/xt_recent.c +4 -3
@@ -650,14 +650,15 @@ static void __net_exit recent_proc_net_exit(struct net *net)/* recent_net_exit() is called before recent_mt_destroy(). Make sure * that the parent xt_recent proc entry is empty before trying to -* remove it. +* remove it. Use recent_mutex instead of recent_lock since +* remove_proc_entry() may sleep. */ -spin_lock_bh (&recent_lock); +mutex_lock (&recent_mutex);list_for_each_entry (t, &recent_net->tables, list)remove_proc_entry (t->name, recent_net->xt_recent); recent_net->xt_recent =NULL ; -spin_unlock_bh (&recent_lock); +mutex_unlock (&recent_mutex);remove_proc_entry ("xt_recent" , net->proc_net); }
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH v2 15/15 netfilter: nat: zero newrange before filling it in
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
nf_nat_masquerade_ipv4(), nf_nat_masquerade_ipv6(), netmap_tg4() and netmap_tg6() each build a stack-local struct nf_nat_range2 from the caller-supplied range, inheriting range->flags unmodified. Two of them only memset() the min_addr/max_addr members, the other two don't memset() anything at all, so newrange.base_proto is left as stack garbage.
If the inherited flags include NF_NAT_RANGE_PROTO_OFFSET, nf_nat_l4proto_unique_tuple() reads range->base_proto.all to compute the NAT port offset. Neither the NETMAP nor the MASQUERADE targets mask the flags field in their checkentry callback, so this uninitialized stack value can end up feeding the NAT port selection.
The per-member memset()s are not enough: they have to enumerate every field that must be cleared, and base_proto was missed when NF_NAT_RANGE_PROTO_OFFSET/base_proto was added to the struct. memset() the whole struct instead, so every member is covered regardless of which ones get explicitly assigned afterwards.
There are other places that miss this, but those hardcode .flags so nat core won't read from the uninitialized fields.
C99 initializers would be preferrable, but this is a more intrusive change. Lets followup in nf-next and convert others as well.
This is an LLM finding.
--- net/netfilter/nf_nat_masquerade.c | 4 ++-- net/netfilter/xt_NETMAP.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)
net/netfilter/nf_nat_masquerade.c +2 -2
@@ -62,8 +62,7 @@ nf_nat_masquerade_ipv4(struct sk_buff *skb, unsigned int hooknum, nat->masq_index = out->ifindex;/* Transfer from original range. */ -memset (&newrange.min_addr,0 ,sizeof (newrange.min_addr)); -memset (&newrange.max_addr,0 ,sizeof (newrange.max_addr)); +memset (&newrange,0 ,sizeof (newrange)); newrange.flags = range->flags |NF_NAT_RANGE_MAP_IPS ; newrange.min_addr.ip = newsrc; newrange.max_addr.ip = newsrc;
@@ -242,6 +241,7 @@ nf_nat_masquerade_ipv6(struct sk_buff *skb, const struct nf_nat_range2 *range,if (nat) nat->masq_index = out->ifindex; +memset (&newrange,0 ,sizeof (newrange)); newrange.flags = range->flags |NF_NAT_RANGE_MAP_IPS ; newrange.min_addr.in6 = src; newrange.max_addr.in6 = src;
net/netfilter/xt_NETMAP.c +2 -2
@@ -44,6 +44,7 @@ netmap_tg6(struct sk_buff *skb, const struct xt_action_param *par) netmask.ip6[i]; } +memset (&newrange,0 ,sizeof (newrange)); newrange.flags = range->flags |NF_NAT_RANGE_MAP_IPS ; newrange.min_addr = new_addr; newrange.max_addr = new_addr;
@@ -91,8 +92,7 @@ netmap_tg4(struct sk_buff *skb, const struct xt_action_param *par) new_ip =ip_hdr (skb)->saddr & ~netmask; new_ip |= mr->range[0 ].min_ip & netmask; -memset (&newrange.min_addr,0 ,sizeof (newrange.min_addr)); -memset (&newrange.max_addr,0 ,sizeof (newrange.max_addr)); +memset (&newrange,0 ,sizeof (newrange)); newrange.flags = mr->range[0 ].flags |NF_NAT_RANGE_MAP_IPS ; newrange.min_addr.ip = new_ip; newrange.max_addr.ip = new_ip;
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerHi,
Quoted 2 lines · Pablo Neira Ayuso
v2: remove kdoc update in
nf_conntrack_expect.
anything else, just like v1.
There is an issue with patch 6/15 which needs a bit more work.
I will resubmit v3 without 6/15. IPVS patch 2/15 might also need a second spin.
Preparing v3 here...
Quoted 153 lines
-o-
Hi,
The following batch contains Netfilter/IPVS fixes for net, this batch addresses LLM findings, several of these fixes has been reproduced by syzbot:
1) Add recursion count to
nf_rejectinfrastructure to prevent stack overflow, from Fernando F. Mancera.2) Address a infinite loop in RCU list traversal with the TWOS IPVS scheduler, from Darong Lu and Julian Anastasov.
3) Use
kfree_rcu()to release IPVS parent application to fix UaF, from Chengfeng Ye.4) Fix UaF in IPVS netns removal path due to releasing per-net protocol data too early, from Chengfeng Ye.
5) Fix unbounded growth of stale, unmatchable IPVS connection entries, from Julian Anastasov.
6) Restrict maximum master conntrack chain depth in ctnetlink, from Florian Westphal.
7) Preserve recursion count on nfqueue reinjection, from Zihan Xi.
8) Restore GRE timeout in the flowtable, otherwise,
WARN_ON_ONCE()is reachable, from Michail Andreas Michalopoulos.9) Check non-null gc transaction in
nft_rbtreebefore queuing it for processing, from Xingyuan Mo.10) Replace
WARN_ON_ONCE()byDEBUG_NET_WARN_ON_ONCE()innf_conntrack_bridgewhich is reachable if the protocol of the tag of a frame differs from thevlan_protocolof the bridge. From Laveesh Bansal.11) Check
x_tablestarget name is non-null before accessing it when building a new table, from Fernando F. Mancera.12) Fix out-of-bound memory read in
nf_conntrack_sip, from Joas Antonio Dos Santos.13) Fix a memleak in
nf_tableshardware offload when double deleting the same rule, in the same batch.14) Use a mutex in
xt_recentnetns exit path instead of spinlock, since proc unregistration might sleep. From Cen Zhang.15) Zero the
nf_nat_range2struct before filling it up, to avoid using stale uninitialized stack data to configure NAT with an esoteric NAT flags combination. From Florian Westphal.Please, pull these changes from:
git://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf.gitnf-26-11-03Thanks.
----------------------------------------------------------------
The following changes since commit
cb284914e749388eef402938bdb3115fe1f6d337:net: ipa: Fix double free of the interrupt structure on config error (2026-10-30 19:04:37 -0700)
are available in the Git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf.gitnf-26-11-03for you to fetch changes up to
3d234337bbb1e32fab38e4e30da72edc5ddc241f:netfilter: nat: zero newrange before filling it in (2026-11-03 14:50:15 +0100)
----------------------------------------------------------------
netfilter pull request 26-11-03----------------------------------------------------------------
Cen Zhang (Microsoft) (1):
netfilter:xt_recent: do not callremove_proc_entry()underrecent_lockChengfeng Ye (2):
ipvs: Defer application parent freeing until after RCU readers
ipvs: fix protocol data lifetime during namespace teardownDarong Lu (1):
ipvs: wait grace period while reusing dest from trashFernando Fernandez Mancera (2):
netfilter:nft_reject: prevent unbounded recursion in output hooks
netfilter:x_tables: check target name before accessing verdictFlorian Westphal (2):
netfilter:nf_conntrack: limit expectation master chain depth
netfilter: nat: zero newrange before filling it inJoas Antonio dos Santos (1):
netfilter:nf_conntrack_sip: fix OOB read inct_sip_parse_transport()Julian Anastasov (1):
ipvs: do not create conns from packets with port 0Laveesh Bansal (1):
netfilter: bridge: useDEBUG_NET_WARN_ON_ONCEinnf_ct_bridge_refrag()Michail Andreas Michalopoulos (1):
netfilter: flowtable: extend ct timeout of offloaded GRE flowsPablo Neira Ayuso (1):
netfilter:nf_tables: fix memleak when rule deletion fails with hardware offloadXingyuan Mo (1):
netfilter:nft_set_rbtree: checkcatchall_syncreturn beforequeue_doneZihan Xi (1):
netfilter:nf_dup: preserve guard across NFQUEUE reinjectioninclude/linux/netdevice_xmit.h | 4 +- include/net/ip_vs.h | 4 +- include/net/netfilter/nf_conntrack_helper.h | 15 ++++ include/net/netfilter/nf_dup_netdev.h | 35 --------- include/net/netfilter/nf_queue.h | 1 + include/net/netfilter/nf_recursion.h | 42 +++++++++++ net/bridge/netfilter/nf_conntrack_bridge.c | 2 +- net/ipv4/netfilter/arp_tables.c | 5 +- net/ipv4/netfilter/ip_tables.c | 5 +- net/ipv4/netfilter/nf_reject_ipv4.c | 12 +++ net/ipv6/netfilter/ip6_tables.c | 5 +- net/ipv6/netfilter/nf_reject_ipv6.c | 13 ++++ net/netfilter/ipvs/ip_vs_app.c | 2 +- net/netfilter/ipvs/ip_vs_core.c | 15 ++-- net/netfilter/ipvs/ip_vs_ctl.c | 109 ++++++++++++++++++++++------ net/netfilter/ipvs/ip_vs_proto_sctp.c | 5 ++ net/netfilter/ipvs/ip_vs_proto_tcp.c | 5 ++ net/netfilter/ipvs/ip_vs_proto_udp.c | 4 + net/netfilter/nf_conntrack_expect.c | 3 + net/netfilter/nf_conntrack_netlink.c | 36 ++++++--- net/netfilter/nf_conntrack_sip.c | 4 +- net/netfilter/nf_dup_netdev.c | 1 + net/netfilter/nf_flow_table_core.c | 1 + net/netfilter/nf_nat_masquerade.c | 4 +- net/netfilter/nf_queue.c | 1 + net/netfilter/nf_tables_api.c | 21 +++--- net/netfilter/nf_tables_offload.c | 3 + net/netfilter/nfnetlink_queue.c | 17 ++++- net/netfilter/nft_fwd_netdev.c | 1 + net/netfilter/nft_set_rbtree.c | 3 +- net/netfilter/xt_NETMAP.c | 4 +- net/netfilter/xt_recent.c | 7 +- 32 files changed, 279 insertions(+), 110 deletions(-) create mode 100644 include/net/netfilter/nf_recursion.h