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: stab…@vger.kernel.org
Reported-by: Roshan Kumar <rosh…@gmail.com>
Link: https://lore.kernel.org/netdev/2026…@gmail.com/
Reported-by: syzb…@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=0008ea314a1096dee2b6
Signed-off-by: Juan Agustin Niccolini <juan…@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