From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 86D4545562D for ; Tue, 22 Sep 2026 09:25:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.130 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790069124; cv=none; b=F11DUWPvB3x93zlMDRbtQf6/rfiaXcStseSXeW8aiJa3IPOvc8sOjxF1+Z77BTbJ79+yQ0eEAz0SAu4zjaxweqrtgddU9wqivtbi+CI1BkOTGrf5qVVM7U4SwCehRgZBaOgLgbbk28/pkNOZugIpRlkG76XwwO6QVoxs//pYf8w= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790069124; c=relaxed/simple; bh=nBgQ1L6cCic/atqsTIZBeuabS1NPMUY9kCa/mWS8Dyc=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=NocjqQkIipv8ymil5MbhP8zpX1sPfAurSHdOwiRZBWltUWFDEPfa9kSNeOAjRtWdiqQ9R6ai1zVZCwEvkzpU2yJWB4ffBrwiQDKwc0RMmvgc11Ddnpe5DQzwNxR4OTOLlGV3iq9v7Obt1PLB7qguHphgwQxCR/AS7gpDKmwN63o= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de; spf=pass smtp.mailfrom=suse.de; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=V4b+62WE; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=V+/q7LZO; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=07NA/LRx; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=BGWKiLun; arc=none smtp.client-ip=195.135.223.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="V4b+62WE"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="V+/q7LZO"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="07NA/LRx"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="BGWKiLun" Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 0674821DBA; Tue, 22 Sep 2026 09:25:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1790069116; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=RbC765AAllBtBdWTPPClBgTFYI0/kZ+1hfv76wZ9Axo=; b=V4b+62WE1GECRH+ezmvl//eXXt3P/hM5n7R5AFm40JQqrsF4Bc5Dfpb9Rdx7obRGxxZ8Y6 u2tkkED2zWhd1JefDZnKssW7/mxnUnZUv/FDbds3hisDirLRgE5XrSOd2yZeKhq9X6xUIO weHz01sZtPny/P1kkoOjzU9FwXFvwog= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1790069116; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=RbC765AAllBtBdWTPPClBgTFYI0/kZ+1hfv76wZ9Axo=; b=V+/q7LZO6Vr2fuGmEtCbwFvKa3t/nXY0opTAv6f7yGDDapwCTxSl6kny4fUe3r6uV9r5o3 Lctfe6leLwjNHFDw== Authentication-Results: smtp-out1.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1790069112; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=RbC765AAllBtBdWTPPClBgTFYI0/kZ+1hfv76wZ9Axo=; b=07NA/LRx23qBU2O3IKwpBtEQ5e0fOqptKXPLk9sFSJaUSnbq6OzrXNo6K1YEdXT62/lYMk BmeDVM0pJRH3guCQHxJMnioD3r7YbyNfJvPEzvvMl6LPzfypAdH6eaikO+230fvoSWmkPn /aoRHJiBs5S0Z9HOfV3oOKpuiAg8Ugg= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1790069112; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=RbC765AAllBtBdWTPPClBgTFYI0/kZ+1hfv76wZ9Axo=; b=BGWKiLun5WvjJKfj8uoU4JSOoGiMHYNsISXTuuDoctodaZHsd+XZ9oBtU1k7WSoGtOFC9K 87vZhredidNlLzDQ== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 575CC136D9; Tue, 22 Sep 2026 09:25:11 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id LwpTBXdJsmptYgAAD6G6ig (envelope-from ); Tue, 22 Sep 2026 09:25:11 +0000 From: Fernando Fernandez Mancera To: netfilter-devel@vger.kernel.org Cc: coreteam@netfilter.org, pablo@netfilter.org, fw@strlen.de, phil@nwl.cc, eric@regit.org, Fernando Fernandez Mancera , Yu Junzhe Subject: [PATCH nf v2] netfilter: nft_reject: prevent unbounded recursion in output hooks Date: Tue, 22 Sep 2026 11:25:03 +0200 Message-ID: <20260922092503.3238-1-fmancera@suse.de> X-Mailer: git-send-email 2.51.0 Precedence: bulk X-Mailing-List: netfilter-devel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Level: X-Spam-Score: -2.80 X-Spam-Flag: NO X-Spamd-Result: default: False [-2.80 / 50.00]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; MID_CONTAINS_FROM(1.00)[]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-0.999]; MIME_GOOD(-0.10)[text/plain]; RCVD_TLS_ALL(0.00)[]; RCPT_COUNT_SEVEN(0.00)[8]; MIME_TRACE(0.00)[0:+]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; TO_MATCH_ENVRCPT_ALL(0.00)[]; FROM_HAS_DN(0.00)[]; FREEMAIL_CC(0.00)[netfilter.org,strlen.de,nwl.cc,regit.org,suse.de,gmail.com]; TO_DN_SOME(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.de:email,suse.de:mid,imap1.dmz-prg2.suse.org:helo]; RCVD_COUNT_TWO(0.00)[2]; FREEMAIL_ENVRCPT(0.00)[gmail.com] 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. Reported-by: Yu Junzhe Closes: https://lore.kernel.org/netfilter-devel/20260921061320.543-1-junzheyu1@gmail.com/ Fixes: bee11dc78fc8 ("netfilter: nft_reject: support for IPv6 and TCP reset") Signed-off-by: Fernando Fernandez Mancera --- 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 + 6 files changed, 69 insertions(+), 35 deletions(-) create mode 100644 include/net/netfilter/nf_recursion.h diff --git a/include/net/netfilter/nf_dup_netdev.h b/include/net/netfilter/nf_dup_netdev.h index f6b05bd80c3f..a39411264614 100644 --- a/include/net/netfilter/nf_dup_netdev.h +++ b/include/net/netfilter/nf_dup_netdev.h @@ -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; diff --git a/include/net/netfilter/nf_recursion.h b/include/net/netfilter/nf_recursion.h new file mode 100644 index 000000000000..3e91b78e21b3 --- /dev/null +++ b/include/net/netfilter/nf_recursion.h @@ -0,0 +1,42 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _NF_RECURSION_H_ +#define _NF_RECURSION_H_ + +#include + +#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 + +#endif /* _NF_RECURSION_H_ */ diff --git a/net/ipv4/netfilter/nf_reject_ipv4.c b/net/ipv4/netfilter/nf_reject_ipv4.c index 59ec465a9df9..3f227a094312 100644 --- a/net/ipv4/netfilter/nf_reject_ipv4.c +++ b/net/ipv4/netfilter/nf_reject_ipv4.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -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; diff --git a/net/ipv6/netfilter/nf_reject_ipv6.c b/net/ipv6/netfilter/nf_reject_ipv6.c index 07cdaa10da0d..f69ddfe72030 100644 --- a/net/ipv6/netfilter/nf_reject_ipv6.c +++ b/net/ipv6/netfilter/nf_reject_ipv6.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -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); diff --git a/net/netfilter/nf_dup_netdev.c b/net/netfilter/nf_dup_netdev.c index c6bd5c29bed6..918716629c15 100644 --- a/net/netfilter/nf_dup_netdev.c +++ b/net/netfilter/nf_dup_netdev.c @@ -12,6 +12,7 @@ #include #include #include +#include static void nf_do_netdev_egress(struct sk_buff *skb, struct net_device *dev, enum nf_dev_hooks hook) diff --git a/net/netfilter/nft_fwd_netdev.c b/net/netfilter/nft_fwd_netdev.c index a48c2f765bba..7447da931a95 100644 --- a/net/netfilter/nft_fwd_netdev.c +++ b/net/netfilter/nft_fwd_netdev.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include -- 2.55.0