From: Nan Li 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") Cc: stable@kernel.org Reported-by: Yuan Tan Reported-by: Yifan Wu Reported-by: Juefei Pu Reported-by: Xin Liu Signed-off-by: Nan Li Signed-off-by: Ren Wei --- net/ipv4/netfilter/nf_dup_ipv4.c | 30 ++++++++++++++++++++++++++++++ net/ipv6/netfilter/nf_dup_ipv6.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/net/ipv4/netfilter/nf_dup_ipv4.c b/net/ipv4/netfilter/nf_dup_ipv4.c index 9a773502f10a..fd469f47bbf7 100644 --- a/net/ipv4/netfilter/nf_dup_ipv4.c +++ b/net/ipv4/netfilter/nf_dup_ipv4.c @@ -16,6 +16,8 @@ #include #include #include +#include +#include #include #if IS_ENABLED(CONFIG_NF_CONNTRACK) #include @@ -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; diff --git a/net/ipv6/netfilter/nf_dup_ipv6.c b/net/ipv6/netfilter/nf_dup_ipv6.c index 6da3102b7c1b..973c860cf387 100644 --- a/net/ipv6/netfilter/nf_dup_ipv6.c +++ b/net/ipv6/netfilter/nf_dup_ipv6.c @@ -9,6 +9,8 @@ #include #include #include +#include +#include #include #include #include @@ -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; -- 2.43.0