xfrm_state_construct() runs the mode's ->user_init() after
xfrm_dev_state_add() has handed the driver a raw pointer to the state
(netdevsim's ipsec->sa[i].xs, mlx5e's sadb entry). When ->user_init()
fails, the error: label just drops the last reference and the gc worker
frees the state via xfrm_dev_state_free(), which never calls
xdo_dev_state_delete. The driver keeps a dangling pointer and the
hardware SA is never retired. iptfs_user_init() returns -EINVAL for a
non-zero XFRMA_IPTFS_PKT_SIZE no larger than header_len, so a single
XFRM_MSG_NEWSA with XFRMA_OFFLOAD_DEV and XFRMA_IPTFS_PKT_SIZE=1 is
enough; netdevsim's ipsec debugfs file then reads the freed state:
BUG: KASAN: slab-use-after-free in nsim_dbg_netdev_ops_read (drivers/net/netdevsim/ipsec.c:42)
Read of size 2 at addr ffff888010e31b42 by task exploit/146
Call Trace:
nsim_dbg_netdev_ops_read (drivers/net/netdevsim/ipsec.c:42)
full_proxy_read (fs/debugfs/file.c:379)
vfs_read (fs/read_write.c:552)
ksys_read (fs/read_write.c:696)
do_syscall_64 (arch/x86/entry/syscall_64.c:89)
entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:121)
Allocated by task 146:
...
xfrm_state_alloc (net/xfrm/xfrm_state.c:738)
xfrm_add_sa (net/xfrm/xfrm_user.c:933 net/xfrm/xfrm_user.c:1072)
xfrm_user_rcv_msg (net/xfrm/xfrm_user.c:3934)
netlink_rcv_skb (net/netlink/af_netlink.c:2585)
...
Freed by task 48:
...
kmem_cache_free (mm/slub.c:6764)
xfrm_state_gc_task (net/xfrm/xfrm_state.c:621 net/xfrm/xfrm_state.c:638)
process_one_work (kernel/workqueue.c:3495)
...
Call xfrm_dev_state_delete() before dropping the reference, as the
xfrm_add_sa() failure path already does. It is a no-op when no offload
was installed, since xfrm_dev_state_add() clears xso->dev on failure.
Cc: stab…@vger.kernel.org
Fixes: 7ac64f4598b4 ("xfrm: add mode_cbs module functionality")
Reported-by: co+b…@bugs.sh
Signed-off-by: Xiang Mei <xme…@asu.edu>
---
net/xfrm/xfrm_user.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/net/xfrm/xfrm_user.c b/net/xfrm/xfrm_user.c
index a5fbe38839c4..d680de14ee4a 100644
--- a/net/xfrm/xfrm_user.c
+++ b/net/xfrm/xfrm_user.c
@@ -1050,6 +1050,7 @@ static struct xfrm_state *xfrm_state_construct(struct net *net,
error:
x->km.state = XFRM_STATE_DEAD;
+ xfrm_dev_state_delete(x);
xfrm_state_put(x);
error_no_put:
*errp = err;
--
2.43.0