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 1/4] xfrm: iptfs: free held packets on SA delete to fix a netns UAF
Date: Mon, 30 Nov 2026 22:21:57 -0500	[thread overview]
Message-ID: <20261201032200.37030-2-juanagusnicco@gmail.com> (raw)
In-Reply-To: <20261201032200.37030-1-juanagusnicco@gmail.com>

IP-TFS keeps received packets beyond the call that delivered them: the
reorder window keeps out-of-order packets until the missing one arrives
or the drop timer fires, and an inner packet that spans several outer
packets is kept in ra_newskb until it is complete. A held packet keeps
its SA alive through the secpath, and IP-TFS frees what it holds only
when the SA is destroyed, which cannot happen while the packets hold
it. So when the network namespace is dismantled, the SA is deleted but
survives, and the drop timer later processes the held packets after the
namespace and its devices have been freed, a use-after-free of both:

  BUG: KASAN: slab-use-after-free in xfrm_input+0xadd/0x2620
  Read of size 8 at addr ff11000011ecbc80 by task swapper/1/0

xfrm_input() reads the freed device, takes the namespace from it, and
counts the dead SA through the freed struct net, a per-CPU increment
through a pointer read from freed memory.

An unprivileged user can trigger this from a user and network
namespace, by holding a packet on an IP-TFS SA and leaving the
namespace before the drop timer fires.

Fix this with a new delete_state mode callback, called from
__xfrm_state_delete(). IP-TFS frees the held packets there and marks
the SA so that a packet still in flight on another CPU is not held
afterwards. Because the callback runs under x->lock, it cancels the
drop timer without waiting for it.

Namespace exit now also flushes all SAs in pre_exit, next to the
policies; the flush in exit stays and finds nothing. The two changes
are made together because freeing the held packets from exit would
release their routes after IPv6 has torn down its dst_ops, and
conntrack teardown would wait on their entries. cleanup_net() waits for
an RCU grace period between pre_exit and exit, which also covers a drop
timer that is still running.

Fixes: 6be02e3e4f37 ("xfrm: iptfs: handle reordering of received packets")
Fixes: 075694765446 ("xfrm: iptfs: handle received fragmented inner packets")
Cc: stable@vger.kernel.org
Signed-off-by: Juan Agustin Niccolini <juanagusnicco@gmail.com>
---
Reproducer: https://github.com/AgusNicco/iptfs-uafs/blob/main/iptfs_netns_uaf.c

 include/net/xfrm.h     |  3 +++
 net/xfrm/xfrm_iptfs.c  | 48 +++++++++++++++++++++++++++++++++++++++++-
 net/xfrm/xfrm_policy.c |  1 +
 net/xfrm/xfrm_state.c  |  3 +++
 4 files changed, 54 insertions(+), 1 deletion(-)

diff --git a/include/net/xfrm.h b/include/net/xfrm.h
index 5fbb73474e32..97f90eba73c2 100644
--- a/include/net/xfrm.h
+++ b/include/net/xfrm.h
@@ -490,6 +490,8 @@ static inline void xfrm_unset_type_offload(struct xfrm_state *x)
  * @owner: module owner or NULL
  * @init_state: Add/init mode specific state in `xfrm_state *x`
  * @clone_state: Copy mode specific values from `orig` to new state `x`
+ * @delete_state: Release mode specific resources when `x` is deleted. Called
+ *	with `x->lock` held.
  * @destroy_state: Cleanup mode specific state from `xfrm_state *x`
  * @user_init: Process mode specific netlink attributes from user
  * @copy_to_user: Add netlink attributes to `attrs` based on state in `x`
@@ -509,6 +511,7 @@ struct xfrm_mode_cbs {
 	struct module	*owner;
 	int	(*init_state)(struct xfrm_state *x);
 	int	(*clone_state)(struct xfrm_state *x, struct xfrm_state *orig);
+	void	(*delete_state)(struct xfrm_state *x);
 	void	(*destroy_state)(struct xfrm_state *x);
 	int	(*user_init)(struct net *net, struct xfrm_state *x,
 			     struct nlattr **attrs,
diff --git a/net/xfrm/xfrm_iptfs.c b/net/xfrm/xfrm_iptfs.c
index 76272b0a1829..0dde380eb16c 100644
--- a/net/xfrm/xfrm_iptfs.c
+++ b/net/xfrm/xfrm_iptfs.c
@@ -141,6 +141,7 @@ struct skb_wseq {
  * @drop_lock: lock to protect reorder queue.
  * @drop_timer: timer for considering next packet lost.
  * @drop_time_ns: timer intervan in nanoseconds.
+ * @dead: SA deleted, do not hold any more packets.
  * @ra_newskb: new pkt being reassembled.
  * @ra_wantseq: expected next sequence for reassembly.
  * @ra_runt: last pkt bytes from very end of last skb.
@@ -168,6 +169,7 @@ struct xfrm_iptfs_data {
 	spinlock_t drop_lock;
 	struct hrtimer drop_timer;
 	u64 drop_time_ns;
+	bool dead;
 
 	/* Tunnel input reassembly */
 	struct sk_buff *ra_newskb; /* new pkt being reassembled */
@@ -1308,6 +1310,13 @@ done:
 		skb_abort_seq_read(&skbseq);
 		kfree_skb(skb);
 	}
+
+	if (unlikely(READ_ONCE(xtfs->dead))) {
+		spin_lock(&xtfs->drop_lock);
+		if (xtfs->ra_newskb)
+			iptfs_reassem_abort(xtfs);
+		spin_unlock(&xtfs->drop_lock);
+	}
 }
 
 /* ------------------------------- */
@@ -1749,7 +1758,10 @@ static int iptfs_input(struct xfrm_state *x, struct sk_buff *skb)
 	INIT_LIST_HEAD(&freelist);
 
 	spin_lock(&xtfs->drop_lock);
-	iptfs_input_reorder(xtfs, skb, &list, &freelist);
+	if (likely(!xtfs->dead))
+		iptfs_input_reorder(xtfs, skb, &list, &freelist);
+	else
+		list_add_tail(&skb->list, &freelist);
 	spin_unlock(&xtfs->drop_lock);
 
 	list_for_each_entry_safe(skb, next, &list, list) {
@@ -2715,6 +2727,7 @@ static int iptfs_clone_state(struct xfrm_state *x, struct xfrm_state *orig)
 	xtfs->w_seq_set = false;
 	xtfs->w_wantseq = 0;
 	xtfs->w_savedlen = 0;
+	xtfs->dead = false;
 	xtfs->ra_newskb = NULL;
 	xtfs->ra_wantseq = 0;
 	xtfs->ra_runtlen = 0;
@@ -2744,6 +2757,38 @@ static int iptfs_init_state(struct xfrm_state *x)
 	return 0;
 }
 
+static void iptfs_delete_state(struct xfrm_state *x)
+{
+	struct xfrm_iptfs_data *xtfs = x->mode_data;
+	struct sk_buff_head list;
+	struct skb_wseq *s, *se;
+	struct sk_buff *skb;
+
+	if (!xtfs)
+		return;
+
+	__skb_queue_head_init(&list);
+
+	spin_lock(&xtfs->drop_lock);
+	WRITE_ONCE(xtfs->dead, true);
+	if (xtfs->ra_newskb) {
+		__skb_queue_tail(&list, xtfs->ra_newskb);
+		xtfs->ra_newskb = NULL;
+	}
+	for (s = xtfs->w_saved, se = s + xtfs->w_savedlen; s < se; s++) {
+		if (s->skb) {
+			__skb_queue_tail(&list, s->skb);
+			s->skb = NULL;
+		}
+	}
+	xtfs->w_savedlen = 0;
+	hrtimer_try_to_cancel(&xtfs->drop_timer);
+	spin_unlock(&xtfs->drop_lock);
+
+	while ((skb = __skb_dequeue(&list)))
+		kfree_skb(skb);
+}
+
 static void iptfs_destroy_state(struct xfrm_state *x)
 {
 	struct xfrm_iptfs_data *xtfs = x->mode_data;
@@ -2784,6 +2829,7 @@ static const struct xfrm_mode_cbs iptfs_mode_cbs = {
 	.owner = THIS_MODULE,
 	.init_state = iptfs_init_state,
 	.clone_state = iptfs_clone_state,
+	.delete_state = iptfs_delete_state,
 	.destroy_state = iptfs_destroy_state,
 	.user_init = iptfs_user_init,
 	.copy_to_user = iptfs_copy_to_user,
diff --git a/net/xfrm/xfrm_policy.c b/net/xfrm/xfrm_policy.c
index d2e938d4db9a..af17985647b4 100644
--- a/net/xfrm/xfrm_policy.c
+++ b/net/xfrm/xfrm_policy.c
@@ -4318,6 +4318,7 @@ static void __net_exit xfrm_net_pre_exit(struct net *net)
 	xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
 #endif
 	xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
+	xfrm_state_flush(net, 0, false);
 }
 
 static void xfrm_policy_fini(struct net *net)
diff --git a/net/xfrm/xfrm_state.c b/net/xfrm/xfrm_state.c
index eb984ed11888..065c1fefb562 100644
--- a/net/xfrm/xfrm_state.c
+++ b/net/xfrm/xfrm_state.c
@@ -846,6 +846,9 @@ int __xfrm_state_delete(struct xfrm_state *x)
 
 		xfrm_state_delete_tunnel(x);
 
+		if (x->mode_cbs && x->mode_cbs->delete_state)
+			x->mode_cbs->delete_state(x);
+
 		/* All xfrm_state objects are created by xfrm_state_alloc.
 		 * The xfrm_state_alloc call gives a reference, and that
 		 * is what we are dropping here.
-- 
2.53.0


  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 ` Juan Agustin Niccolini [this message]
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 ` [PATCH ipsec 3/4] xfrm: iptfs: free queued packets on SA delete to fix a netns UAF Juan Agustin Niccolini
2026-12-01  3:22 ` [PATCH ipsec 4/4] xfrm: iptfs: don't keep the input device of queued packets to fix a UAF 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-2-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