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: stab…@vger.kernel.org
Signed-off-by: Juan Agustin Niccolini <juan…@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