xfrm: iptfs: fix use-after-free by held and queued packets
Subsystems (3)
cover xfrm: iptfs: fix use-after-free by held and queued packets
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
Signature · 1 line
2.53.0
PATCH 1/4 xfrm: iptfs: free held packets on SA delete to fix a netns UAF
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.
---
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(-)
include/net/xfrm.h +3 -0
@@ -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,
net/xfrm/xfrm_iptfs.c +47 -1
90 lines not on this page · raw message
net/xfrm/xfrm_policy.c +1 -0
@@ -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)
net/xfrm/xfrm_state.c +3 -0
@@ -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.
Signature · 1 line
2.53.0
PATCH 2/4 xfrm: iptfs: look up the device of held packets again to fix a UAF
In reply to Juan Agustin NiccoliniA 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).
---
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(-)
net/xfrm/xfrm_iptfs.c +21 -2
@@ -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 ;
Signature · 1 line
2.53.0
PATCH 3/4 xfrm: iptfs: free queued packets on SA delete to fix a netns UAF
In reply to Juan Agustin NiccoliniIP-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.
---
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(+)
net/xfrm/xfrm_iptfs.c +11 -0
@@ -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) {
Signature · 1 line
2.53.0
PATCH 4/4 xfrm: iptfs: don't keep the input device of queued packets to fix a UAF
In reply to Juan Agustin NiccoliniA 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.
---
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(+)
net/xfrm/xfrm_iptfs.c +1 -0
@@ -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;
Signature · 1 line
2.53.0