Skip to contentSkip to conversation

netfilter: nf_dup: preserve socket ownership on egress duplicates

PATCH New Ren Wei 2 participants 2 messages May 17 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Ren Wei

author
#1 lore raw

PATCH 1/1 netfilter: nf_dup: preserve socket ownership on egress duplicates

From: Nan Li <[email protected]>

pskb_copy() does not preserve skb socket ownership on the duplicate. As a result, nf_dup_ipv4() and nf_dup_ipv6() can reinject locally generated egress packets without the original socket association.

Some later transmit-side paths still rely on skb->sk and on the associated skb destructor for socket write-memory accounting. Preserve the socket on egress duplicates, and restore the original destructor/accounting only when the source skb was already owner charged.

Keep the ingress-side behavior unchanged, since there is no local transmit socket to carry over for PRE_ROUTING or LOCAL_IN traffic.

The issue predates the nf_dup split and was already present in the original xt_TEE implementation.

Fixes e281b19897dc ("netfilter: xtables: inclusion of xt_TEE")
---
 net/ipv4/netfilter/nf_dup_ipv4.c | 30 ++++++++++++++++++++++++++++++
 net/ipv6/netfilter/nf_dup_ipv6.c | 31 +++++++++++++++++++++++++++++++
 2 files changed, 61 insertions(+)
net/ipv4/netfilter/nf_dup_ipv4.c +30 -0
index 9a773502f10a..fd469f47bbf7 100644
@@ -16,6 +16,8 @@
 #include <net/icmp.h>
 #include <net/ip.h>
 #include <net/route.h>
+#include <net/sock.h>
+#include <net/tcp.h>
 #include <net/netfilter/ipv4/nf_dup_ipv4.h>
 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
 #include <net/netfilter/nf_conntrack.h>
@@ -48,9 +50,25 @@ static bool nf_dup_ipv4_route(struct net *net, struct sk_buff *skb,
 	return true;
 }
 
+static bool nf_dup_ipv4_tx_owner(struct sock *sk,
+				 void (*destructor)(struct sk_buff *))
+{
+	return sk && (destructor == sock_wfree ||
+		      destructor == __sock_wfree ||
+		      destructor == tcp_wfree);
+}
+
+static bool nf_dup_ipv4_egress(unsigned int hooknum)
+{
+	return hooknum == NF_INET_LOCAL_OUT ||
+	       hooknum == NF_INET_POST_ROUTING;
+}
+
 void nf_dup_ipv4(struct net *net, struct sk_buff *skb, unsigned int hooknum,
 		 const struct in_addr *gw, int oif)
 {
+	void (*destructor)(struct sk_buff *) = skb->destructor;
+	struct sock *sk = skb->sk;
 	struct iphdr *iph;
 
 	local_bh_disable();
@@ -86,6 +104,18 @@ void nf_dup_ipv4(struct net *net, struct sk_buff *skb, unsigned int hooknum,
 		--iph->ttl;
 
 	if (nf_dup_ipv4_route(net, skb, gw, oif)) {
+		/* pskb_copy() drops skb ownership. Preserve the originating
+		 * socket for egress duplicates, and restore write-memory
+		 * accounting when the original skb was owner charged.
+		 */
+		if (nf_dup_ipv4_egress(hooknum) && sk && sk_fullsock(sk)) {
+			skb->sk = sk;
+			if (nf_dup_ipv4_tx_owner(sk, destructor)) {
+				skb->destructor = destructor;
+				refcount_add(skb->truesize,
+					     &sk->sk_wmem_alloc);
+			}
+		}
 		current->in_nf_duplicate = true;
 		ip_local_out(net, skb->sk, skb);
 		current->in_nf_duplicate = false;
net/ipv6/netfilter/nf_dup_ipv6.c +31 -0
index 6da3102b7c1b..973c860cf387 100644
@@ -9,6 +9,8 @@
 #include <linux/percpu.h>
 #include <linux/skbuff.h>
 #include <linux/netfilter.h>
+#include <net/sock.h>
+#include <net/tcp.h>
 #include <net/ipv6.h>
 #include <net/ip6_route.h>
 #include <net/netfilter/ipv6/nf_dup_ipv6.h>
@@ -44,9 +46,26 @@ static bool nf_dup_ipv6_route(struct net *net, struct sk_buff *skb,
 	return true;
 }
 
+static bool nf_dup_ipv6_tx_owner(struct sock *sk,
+				 void (*destructor)(struct sk_buff *))
+{
+	return sk && (destructor == sock_wfree ||
+		      destructor == __sock_wfree ||
+		      destructor == tcp_wfree);
+}
+
+static bool nf_dup_ipv6_egress(unsigned int hooknum)
+{
+	return hooknum == NF_INET_LOCAL_OUT ||
+	       hooknum == NF_INET_POST_ROUTING;
+}
+
 void nf_dup_ipv6(struct net *net, struct sk_buff *skb, unsigned int hooknum,
 		 const struct in6_addr *gw, int oif)
 {
+	void (*destructor)(struct sk_buff *) = skb->destructor;
+	struct sock *sk = skb->sk;
+
 	local_bh_disable();
 	if (current->in_nf_duplicate)
 		goto out;
@@ -64,6 +83,18 @@ void nf_dup_ipv6(struct net *net, struct sk_buff *skb, unsigned int hooknum,
 		--iph->hop_limit;
 	}
 	if (nf_dup_ipv6_route(net, skb, gw, oif)) {
+		/* pskb_copy() drops skb ownership. Preserve the originating
+		 * socket for egress duplicates, and restore write-memory
+		 * accounting when the original skb was owner charged.
+		 */
+		if (nf_dup_ipv6_egress(hooknum) && sk && sk_fullsock(sk)) {
+			skb->sk = sk;
+			if (nf_dup_ipv6_tx_owner(sk, destructor)) {
+				skb->destructor = destructor;
+				refcount_add(skb->truesize,
+					     &sk->sk_wmem_alloc);
+			}
+		}
 		current->in_nf_duplicate = true;
 		ip6_local_out(net, skb->sk, skb);
 		current->in_nf_duplicate = false;
Signature · 1 line
2.43.0
#2 lore raw
kernel test robot noticed the following build errors:

Hi Ren,

kernel test robot noticed the following build errors:

[auto build test ERROR on netfilter-nf/main]

url:    https://github.com/intel-lab-lkp/linux/commits/Ren-Wei/netfilter-nf_dup-preserve-socket-ownership-on-egress-duplicates/20260517-203809
base:   https://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf.git main
patch link:    https://lore.kernel.org/r/7b2643cdbf92aab1eb0ce00f2c6e7151839cbe40.1778945319.git.tonanli66%40gmail.com
patch subject: [PATCH nf 1/1] netfilter: nf_dup: preserve socket ownership on egress duplicates
config: x86_64-rhel-9.4-ltp (https://download.01.org/0day-ci/archive/20260518/[email protected]/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260518/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>, old ones prefixed by <<):

>> ERROR: modpost: "__sock_wfree" [net/ipv4/netfilter/nf_dup_ipv4.ko] undefined!
>> ERROR: modpost: "tcp_wfree" [net/ipv4/netfilter/nf_dup_ipv4.ko] undefined!
>> ERROR: modpost: "__sock_wfree" [net/ipv6/netfilter/nf_dup_ipv6.ko] undefined!
>> ERROR: modpost: "tcp_wfree" [net/ipv6/netfilter/nf_dup_ipv6.ko] undefined!

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki