Netdev List
 help / color / mirror / Atom feed
* [PATCH ipsec 0/4] xfrm: iptfs: fix use-after-free by held and queued packets
@ 2026-12-01  3:21 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
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Juan Agustin Niccolini @ 2026-12-01  3:21 UTC (permalink / raw)
  To: Steffen Klassert, Herbert Xu, David S . Miller
  Cc: netdev, ipsec, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Willem de Bruijn, Simon Horman, Christian Hopps, Antony Antony,
	Sabrina Dubroca, Shaomin Chen, Roshan Kumar

IP-TFS keeps packets for later on both sides: received packets in its
reorder window and reassembly buffer, and outgoing packets on its output
queue until the send timer fires. A kept packet points at its device,
and through its SA at its network namespace, without keeping either
alive. Each side can be left holding a freed object of each kind, which
gives four use-after-frees:

  receive,  namespace  patch 1
  receive,  device     patch 2
  transmit, namespace  patch 3
  transmit, device     patch 4

An unprivileged user can trigger all four from a user and network
namespace on any kernel with CONFIG_XFRM_IPTFS; no IPsec needs to be
configured, as the user creates the SA. The receive device bug gives an
addition of a chosen value at a chosen kernel address, enough to gain
root. On a configured IP-TFS gateway, the packets queued on transmit
are forwarded traffic, which any host can send without credentials.

Packets that arrive through a vti device also keep vti's tunnel pointer
in skb->cb while held; that is a separate bug in vti and is not
addressed here.

2/4 was discovered by LLM-based static analysis. 1/4 was discovered
while testing the patch for 2/4 on a KASAN build. The other two were
discovered by more thorough static analysis after that unexpected
discovery.

Reproducers: https://github.com/AgusNicco/iptfs-uafs
All 4 untrimmed KASAN reports are also in that repo.

Juan Agustin Niccolini (4):
  xfrm: iptfs: free held packets on SA delete to fix a netns UAF
  xfrm: iptfs: look up the device of held packets again to fix a UAF
  xfrm: iptfs: free queued packets on SA delete to fix a netns UAF
  xfrm: iptfs: don't keep the input device of queued packets to fix a
    UAF

 include/net/xfrm.h     |  3 ++
 net/xfrm/xfrm_iptfs.c  | 83 ++++++++++++++++++++++++++++++++++++++++--
 net/xfrm/xfrm_policy.c |  1 +
 net/xfrm/xfrm_state.c  |  3 ++
 4 files changed, 87 insertions(+), 3 deletions(-)


base-commit: 1213df0bc5993e479706c5eba5830243d778f164
-- 
2.53.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH ipsec 1/4] xfrm: iptfs: free held packets on SA delete to fix a netns UAF
  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
  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
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Juan Agustin Niccolini @ 2026-12-01  3:21 UTC (permalink / raw)
  To: Steffen Klassert, Herbert Xu, David S . Miller
  Cc: netdev, ipsec, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Willem de Bruijn, Simon Horman, Christian Hopps, Antony Antony,
	Sabrina Dubroca, Shaomin Chen, Roshan Kumar

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


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH ipsec 2/4] xfrm: iptfs: look up the device of held packets again to fix a UAF
  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 ` 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
  3 siblings, 0 replies; 5+ messages in thread
From: Juan Agustin Niccolini @ 2026-12-01  3:21 UTC (permalink / raw)
  To: Steffen Klassert, Herbert Xu, David S . Miller
  Cc: netdev, ipsec, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Willem de Bruijn, Simon Horman, Christian Hopps, Antony Antony,
	Sabrina Dubroca, Shaomin Chen, Roshan Kumar

A packet held in the IP-TFS reorder window, or as the in-progress
reassembly packet, keeps skb->dev without a reference on the device.
If the device is deleted in the meantime, xfrm_input() reads the freed
net_device when the packet is released, a use-after-free:

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

The freed device's core_stats pointer is then used for a per-CPU
increment. Once the memory is reused for data the user controls, that
is an addition of a chosen value at a chosen kernel address, which is
enough to escalate privileges to root.

An unprivileged user can trigger this from a user and network
namespace, by sending through an IP-TFS SA with a sequence number
skipped and deleting the receiving device before the drop timer fires.

Fix this as ip_expire() does for IPv4 reassembly: record the ifindex
when a packet is saved in the reorder window, clear skb->dev, and look
the device up again when the packet is released, dropping it if the
device is gone. Since the previous patch, a held packet never outlives
its namespace, so the lookup runs on a live one. A completed inner
packet takes the device of the outer packet that completed it, as in
ip_frag_reasm().

I found this bug during an LLM-based audit. A syzbot report
and a previous proposed patch for this bug exist (see below).

Fixes: 6be02e3e4f37 ("xfrm: iptfs: handle reordering of received packets")
Fixes: 075694765446 ("xfrm: iptfs: handle received fragmented inner packets")
Cc: stable@vger.kernel.org
Reported-by: Roshan Kumar <roshaen09@gmail.com>
Link: https://lore.kernel.org/netdev/20260921083602.814584-1-roshaen09@gmail.com/
Reported-by: syzbot+0008ea314a1096dee2b6@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=0008ea314a1096dee2b6
Signed-off-by: Juan Agustin Niccolini <juanagusnicco@gmail.com>
---
Reproducer: https://github.com/AgusNicco/iptfs-uafs/blob/main/iptfs_reorder_uaf.c

I have an exploit that uses this bug to escalate privileges to root from
an unprivileged user. It is available to the maintainers on request.

 net/xfrm/xfrm_iptfs.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

diff --git a/net/xfrm/xfrm_iptfs.c b/net/xfrm/xfrm_iptfs.c
index 0dde380eb16c..da5729306a61 100644
--- a/net/xfrm/xfrm_iptfs.c
+++ b/net/xfrm/xfrm_iptfs.c
@@ -939,6 +939,7 @@ static u32 iptfs_reassem_cont(struct xfrm_iptfs_data *xtfs, u64 seq,
 	} else {
 		/* We are done with packet reassembly! */
 		iptfs_reassem_done(xtfs);
+		newskb->dev = skb->dev;
 		iptfs_complete_inner_skb(xtfs->x, newskb);
 		list_add_tail(&newskb->list, list);
 	}
@@ -1248,6 +1249,15 @@ static void iptfs_input_ordered(struct xfrm_state *x, struct sk_buff *skb)
 	xtfs = x->mode_data;
 	net = xs_net(x);
 
+	if (!skb->dev) {
+		skb->dev = dev_get_by_index_rcu(net, skb->skb_iif);
+		if (!skb->dev) {
+			XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR);
+			kfree_skb(skb);
+			return;
+		}
+	}
+
 	seq = __esp_seq(skb);
 
 	/* Large enough to hold both types of header */
@@ -1446,6 +1456,13 @@ static void iptfs_set_window_drop_times(struct xfrm_iptfs_data *xtfs, int index)
 			      IPTFS_HRTIMER_MODE);
 }
 
+static void __reorder_save(struct skb_wseq *s, struct sk_buff *skb)
+{
+	skb->skb_iif = skb->dev->ifindex;
+	skb->dev = NULL;
+	s->skb = skb;
+}
+
 static void __reorder_future_fits(struct xfrm_iptfs_data *xtfs,
 				  struct sk_buff *inskb,
 				  struct list_head *freelist)
@@ -1491,7 +1508,7 @@ static void __reorder_future_fits(struct xfrm_iptfs_data *xtfs,
 		return;
 	}
 
-	xtfs->w_saved[index].skb = inskb;
+	__reorder_save(&xtfs->w_saved[index], inskb);
 	xtfs->w_savedlen = max(savedlen, index + 1);
 	iptfs_set_window_drop_times(xtfs, index);
 }
@@ -1626,7 +1643,7 @@ static void __reorder_future_shifts(struct xfrm_iptfs_data *xtfs,
 
 	/* We've shifted. plug the packet in at the end. */
 	xtfs->w_savedlen = nslots - 1;
-	xtfs->w_saved[xtfs->w_savedlen - 1].skb = inskb;
+	__reorder_save(&xtfs->w_saved[xtfs->w_savedlen - 1], inskb);
 	iptfs_set_window_drop_times(xtfs, xtfs->w_savedlen - 1);
 
 	/* if we don't have a slot0 then we must wait for it */
@@ -1720,10 +1737,12 @@ static enum hrtimer_restart iptfs_drop_timer(struct hrtimer *me)
 		kfree_skb_reason(skb, SKB_DROP_REASON_FRAG_REASM_TIMEOUT);
 
 	if (count) {
+		rcu_read_lock();
 		list_for_each_entry_safe(skb, next, &list, list) {
 			skb_list_del_init(skb);
 			iptfs_input_ordered(x, skb);
 		}
+		rcu_read_unlock();
 	}
 
 	return HRTIMER_NORESTART;
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH ipsec 3/4] xfrm: iptfs: free queued packets on SA delete to fix a netns UAF
  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
  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
  3 siblings, 0 replies; 5+ messages in thread
From: Juan Agustin Niccolini @ 2026-12-01  3:21 UTC (permalink / raw)
  To: Steffen Klassert, Herbert Xu, David S . Miller
  Cc: netdev, ipsec, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Willem de Bruijn, Simon Horman, Christian Hopps, Antony Antony,
	Sabrina Dubroca, Shaomin Chen, Roshan Kumar

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


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH ipsec 4/4] xfrm: iptfs: don't keep the input device of queued packets to fix a UAF
  2026-12-01  3:21 [PATCH ipsec 0/4] xfrm: iptfs: fix use-after-free by held and queued packets Juan Agustin Niccolini
                   ` (2 preceding siblings ...)
  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 ` Juan Agustin Niccolini
  3 siblings, 0 replies; 5+ messages in thread
From: Juan Agustin Niccolini @ 2026-12-01  3:22 UTC (permalink / raw)
  To: Steffen Klassert, Herbert Xu, David S . Miller
  Cc: netdev, ipsec, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Willem de Bruijn, Simon Horman, Christian Hopps, Antony Antony,
	Sabrina Dubroca, Shaomin Chen, Roshan Kumar

A forwarded packet reaches iptfs_output_collect() with skb->dev still
set to the device it was received on, and waits on xtfs->queue without
a reference on that device. Without the queue, forwarding runs within
the receive path's RCU read-side section, which keeps the device alive.
If the device is deleted before iptfs_timer fires, the packet is still
encrypted and sent, and ip_output() or ip6_output() passes the freed
device to the POSTROUTING hooks as the input device. A rule that
matches on the input device then reads the freed net_device, and an
NFQUEUE verdict takes a reference on it with dev_hold():

  BUG: KASAN: slab-use-after-free in nft_meta_get_eval+0x4b4/0x600
  Read of size 4 at addr ff11000001d120e0 by task swapper/0/0

The rules that read the input device are the namespace owner's own, so
the freed memory is returned to them: "meta iif", "iifname", "iiftype"
and "iifgroup" hand back its bytes, and "iifkind" dereferences a
pointer taken from it, which with the memory refilled reads 16 bytes
from an address of the user's choosing.

An unprivileged user can trigger this from a user and network
namespace, by forwarding a packet into an IP-TFS SA and deleting the
receiving device before the timer fires.

Clear skb->dev when the packet is queued. The outer packet carries
several inner packets, which may have arrived on different devices, so
it has no single input device. POSTROUTING rules no longer see an input
device for IP-TFS packets, as for packets from local sockets, which are
already sent with a NULL skb->dev.

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_dev_uaf.c

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

diff --git a/net/xfrm/xfrm_iptfs.c b/net/xfrm/xfrm_iptfs.c
index 320de30ed8bd..0fd3a295d5a7 100644
--- a/net/xfrm/xfrm_iptfs.c
+++ b/net/xfrm/xfrm_iptfs.c
@@ -1834,6 +1834,7 @@ static bool iptfs_enqueue(struct xfrm_iptfs_data *xtfs, struct sk_buff *skb)
 			IP6_ECN_set_ce(skb, ipv6_hdr(skb));
 	}
 
+	skb->dev = NULL;
 	__skb_queue_tail(&xtfs->queue, skb);
 	xtfs->queue_size += skb->len;
 	return true;
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-12-01  3:22 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox