Netdev List
 help / color / mirror / Atom feed
From: Juan Agustin Niccolini <juanagusnicco@gmail.com>
To: Steffen Klassert <steffen.klassert@secunet.com>,
	Herbert Xu <herbert@gondor.apana.org.au>,
	"David S . Miller" <davem@davemloft.net>
Cc: netdev@vger.kernel.org, ipsec@lists.linux.dev,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Willem de Bruijn <willemb@google.com>,
	Simon Horman <horms@kernel.org>,
	Christian Hopps <chopps@labn.net>,
	Antony Antony <antony.antony@secunet.com>,
	Sabrina Dubroca <sd@queasysnail.net>,
	Shaomin Chen <eeesssooo020@gmail.com>,
	Roshan Kumar <roshaen09@gmail.com>
Subject: [PATCH ipsec 3/4] xfrm: iptfs: free queued packets on SA delete to fix a netns UAF
Date: Mon, 30 Nov 2026 22:21:59 -0500	[thread overview]
Message-ID: <20261201032200.37030-4-juanagusnicco@gmail.com> (raw)
In-Reply-To: <20261201032200.37030-1-juanagusnicco@gmail.com>

IP-TFS sends outgoing packets from a timer: iptfs_output_collect()
queues them on xtfs->queue, and iptfs_timer sends them after the
configured delay. A queued packet keeps its SA alive through its xfrm
route, and IP-TFS frees the queue and stops iptfs_timer only when the
SA is destroyed, which cannot happen while the packets hold it. A
forwarded packet has no socket that would keep the namespace alive. So
when the network namespace is dismantled with a packet queued, the SA
is deleted but survives, and iptfs_timer later sends the packet after
the namespace has been freed, a use-after-free of struct net:

  BUG: KASAN: slab-use-after-free in xfrm_output_resume+0xdd2/0x1bf0
  Read of size 8 at addr ff1100000d3c3ec0 by task swapper/0/0

Releasing the packet's route then reads the route counter from the same
freed struct net. Namespace exit had already destroyed it, so the
per-CPU access faults in timer context and the machine panics.

An unprivileged user can trigger this from a user and network
namespace, by forwarding a packet into an IP-TFS SA with a long send
delay and leaving the namespace before the timer fires.

Fix this in the delete_state callback added earlier in this series:
free the queued packets and cancel iptfs_timer without waiting for it,
and drop packets that reach iptfs_output_collect() once the SA is no
longer valid, as xfrm_output_one() does. Since namespace exit now
deletes the SAs in pre_exit, the queued packets release their routes
before the xfrm dst_ops are torn down.

Fixes: 0e4fbf013fa5 ("xfrm: iptfs: add user packet (tunnel ingress) handling")
Cc: stable@vger.kernel.org
Signed-off-by: Juan Agustin Niccolini <juanagusnicco@gmail.com>
---
Reproducer: https://github.com/AgusNicco/iptfs-uafs/blob/main/iptfs_output_netns_uaf.c

 net/xfrm/xfrm_iptfs.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/net/xfrm/xfrm_iptfs.c b/net/xfrm/xfrm_iptfs.c
index da5729306a61..320de30ed8bd 100644
--- a/net/xfrm/xfrm_iptfs.c
+++ b/net/xfrm/xfrm_iptfs.c
@@ -1930,6 +1930,13 @@ static int iptfs_output_collect(struct net *net, struct sock *sk, struct sk_buff
 	 */
 	spin_lock_bh(&x->lock);
 
+	if (unlikely(x->km.state != XFRM_STATE_VALID)) {
+		spin_unlock_bh(&x->lock);
+		XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEINVALID);
+		kfree_skb_list(segs);
+		return -EINVAL;
+	}
+
 	skb_list_walk_safe(segs, skb, nskb) {
 		skb_mark_not_on_list(skb);
 
@@ -2788,6 +2795,10 @@ static void iptfs_delete_state(struct xfrm_state *x)
 
 	__skb_queue_head_init(&list);
 
+	skb_queue_splice_init(&xtfs->queue, &list);
+	xtfs->queue_size = 0;
+	hrtimer_try_to_cancel(&xtfs->iptfs_timer);
+
 	spin_lock(&xtfs->drop_lock);
 	WRITE_ONCE(xtfs->dead, true);
 	if (xtfs->ra_newskb) {
-- 
2.53.0


  parent reply	other threads:[~2026-12-01  3:22 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01  3:21 [PATCH ipsec 0/4] xfrm: iptfs: fix use-after-free by held and queued packets Juan Agustin Niccolini
2026-12-01  3:21 ` [PATCH ipsec 1/4] xfrm: iptfs: free held packets on SA delete to fix a netns UAF Juan Agustin Niccolini
2026-12-01  3:21 ` [PATCH ipsec 2/4] xfrm: iptfs: look up the device of held packets again to fix a UAF Juan Agustin Niccolini
2026-12-01  3:21 ` Juan Agustin Niccolini [this message]
2026-12-01  3:22 ` [PATCH ipsec 4/4] xfrm: iptfs: don't keep the input device of queued packets " Juan Agustin Niccolini

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=20261201032200.37030-4-juanagusnicco@gmail.com \
    --to=juanagusnicco@gmail.com \
    --cc=antony.antony@secunet.com \
    --cc=chopps@labn.net \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=eeesssooo020@gmail.com \
    --cc=herbert@gondor.apana.org.au \
    --cc=horms@kernel.org \
    --cc=ipsec@lists.linux.dev \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=roshaen09@gmail.com \
    --cc=sd@queasysnail.net \
    --cc=steffen.klassert@secunet.com \
    --cc=willemb@google.com \
    /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