Linux kernel -stable discussions
 help / color / mirror / Atom feed
From: Xingyuan Mo <hdthky0@gmail.com>
To: Pablo Neira Ayuso <pablo@netfilter.org>,
	Florian Westphal <fw@strlen.de>, Phil Sutter <phil@nwl.cc>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Simon Horman <horms@kernel.org>
Cc: "open list:NETFILTER" <netfilter-devel@vger.kernel.org>,
	"open list:NETFILTER" <coreteam@netfilter.org>,
	Xingyuan Mo <hdthky0@gmail.com>,
	stable@vger.kernel.org
Subject: [PATCH nf] netfilter: nf_nat: don't kill an unconfirmed conntrack on oif change
Date: Thu, 3 Dec 2026 17:49:09 +0800	[thread overview]
Message-ID: <20261203094859.2263418-1-hdthky0@gmail.com> (raw)

nf_nat_inet_fn() runs nf_ct_kill_acct() on the conntrack when a
connection that has already been set up for masquerade shows up at
POST_ROUTING with an output device other than the one the masquerade
target recorded. Nothing guarantees that this conntrack was confirmed
by then.

ipmr forwards a multicast packet by cloning the skb for every vif and
running each clone through NF_INET_FORWARD and ip_output(), so every
clone reaches POST_ROUTING with its own output device while the
conntrack created at PRE_ROUTING is still unconfirmed, because
ip_mr_input() is a dst.input handler and LOCAL_IN with its nf_confirm
hook is never crossed. If the first clone gets masqueraded and is then
dropped by a postrouting hook that runs before nf_confirm, the second
clone finds NAT already initialised for the connection, takes the
oif_changed path and calls nf_ct_delete() on the unconfirmed
conntrack. clean_from_lists() then unlinks
tuplehash[IP_CT_DIR_ORIGINAL].hnnode, whose pprev is NULL until
confirmation, and writes through the NULL pointer.

An unconfirmed conntrack is not linked into any list, so there is
nothing to delete. Drop the packet and leave the conntrack to be
released by the last skb holding a reference to it.

KASAN report:
  BUG: KASAN: null-ptr-deref in __nf_ct_delete_from_lists+0x1b3/0x290
  Write of size 8 at addr 0000000000000000 by task poc119/577
  Call Trace:
   __nf_ct_delete_from_lists+0x1b3/0x290
   nf_ct_delete.part.0+0x119/0x320
   nf_ct_kill_acct+0xbe/0xd0
   nf_nat_inet_fn+0x2aa/0x450
   nf_nat_ipv4_out+0x96/0x200
   nf_hook_slow+0x69/0x140
   ip_mc_output+0x413/0x500
   ip_mr_output+0x119/0x810
   ipmr_queue_fwd_xmit.constprop.0+0x512/0x520
   ip_mr_forward+0x5d9/0x5e0
   ip_mr_input+0x273/0x730

Fixes: c65ef8dc7b1c ("netfilter: nf_nat: Also handle non-ESTABLISHED routing changes in MASQUERADE")
Cc: stable@vger.kernel.org
Signed-off-by: Xingyuan Mo <hdthky0@gmail.com>
---
 net/netfilter/nf_nat_core.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/net/netfilter/nf_nat_core.c b/net/netfilter/nf_nat_core.c
index 5329881ce2fff..f9e9a6e5b1594 100644
--- a/net/netfilter/nf_nat_core.c
+++ b/net/netfilter/nf_nat_core.c
@@ -959,6 +959,9 @@ nf_nat_inet_fn(void *priv, struct sk_buff *skb,
 	return nf_nat_packet(ct, ctinfo, state->hook, skb);
 
 oif_changed:
+	if (!nf_ct_is_confirmed(ct))
+		return NF_DROP;
+
 	nf_ct_kill_acct(ct, ctinfo, skb);
 	return NF_DROP;
 }
-- 
2.43.0


                 reply	other threads:[~2026-12-03  9:49 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261203094859.2263418-1-hdthky0@gmail.com \
    --to=hdthky0@gmail.com \
    --cc=coreteam@netfilter.org \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=fw@strlen.de \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=pablo@netfilter.org \
    --cc=phil@nwl.cc \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox