Skip to content

Commit 18b1ab7

Browse files
magnus-karlssonborkmann
authored andcommitted
xsk: Fix race at socket teardown
Fix a race in the xsk socket teardown code that can lead to a NULL pointer dereference splat. The current xsk unbind code in xsk_unbind_dev() starts by setting xs->state to XSK_UNBOUND, sets xs->dev to NULL and then waits for any NAPI processing to terminate using synchronize_net(). After that, the release code starts to tear down the socket state and free allocated memory. BUG: kernel NULL pointer dereference, address: 00000000000000c0 PGD 8000000932469067 P4D 8000000932469067 PUD 0 Oops: 0000 [#1] PREEMPT SMP PTI CPU: 25 PID: 69132 Comm: grpcpp_sync_ser Tainted: G I 5.16.0+ #2 Hardware name: Dell Inc. PowerEdge R730/0599V5, BIOS 1.2.10 03/09/2015 RIP: 0010:__xsk_sendmsg+0x2c/0x690 [...] RSP: 0018:ffffa2348bd13d50 EFLAGS: 00010246 RAX: 0000000000000000 RBX: 0000000000000040 RCX: ffff8d5fc632d258 RDX: 0000000000400000 RSI: ffffa2348bd13e10 RDI: ffff8d5fc5489800 RBP: ffffa2348bd13db0 R08: 0000000000000000 R09: 00007ffffffff000 R10: 0000000000000000 R11: 0000000000000000 R12: ffff8d5fc5489800 R13: ffff8d5fcb0f5140 R14: ffff8d5fcb0f5140 R15: 0000000000000000 FS: 00007f991cff9400(0000) GS:ffff8d6f1f700000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 00000000000000c0 CR3: 0000000114888005 CR4: 00000000001706e0 Call Trace: <TASK> ? aa_sk_perm+0x43/0x1b0 xsk_sendmsg+0xf0/0x110 sock_sendmsg+0x65/0x70 __sys_sendto+0x113/0x190 ? debug_smp_processor_id+0x17/0x20 ? fpregs_assert_state_consistent+0x23/0x50 ? exit_to_user_mode_prepare+0xa5/0x1d0 __x64_sys_sendto+0x29/0x30 do_syscall_64+0x3b/0xc0 entry_SYSCALL_64_after_hwframe+0x44/0xae There are two problems with the current code. First, setting xs->dev to NULL before waiting for all users to stop using the socket is not correct. The entry to the data plane functions xsk_poll(), xsk_sendmsg(), and xsk_recvmsg() are all guarded by a test that xs->state is in the state XSK_BOUND and if not, it returns right away. But one process might have passed this test but still have not gotten to the point in which it uses xs->dev in the code. In this interim, a second process executing xsk_unbind_dev() might have set xs->dev to NULL which will lead to a crash for the first process. The solution here is just to get rid of this NULL assignment since it is not used anymore. Before commit 42fddcc ("xsk: use state member for socket synchronization"), xs->dev was the gatekeeper to admit processes into the data plane functions, but it was replaced with the state variable xs->state in the aforementioned commit. The second problem is that synchronize_net() does not wait for any process in xsk_poll(), xsk_sendmsg(), or xsk_recvmsg() to complete, which means that the state they rely on might be cleaned up prematurely. This can happen when the notifier gets called (at driver unload for example) as it uses xsk_unbind_dev(). Solve this by extending the RCU critical region from just the ndo_xsk_wakeup to the whole functions mentioned above, so that both the test of xs->state == XSK_BOUND and the last use of any member of xs is covered by the RCU critical section. This will guarantee that when synchronize_net() completes, there will be no processes left executing xsk_poll(), xsk_sendmsg(), or xsk_recvmsg() and state can be cleaned up safely. Note that we need to drop the RCU lock for the skb xmit path as it uses functions that might sleep. Due to this, we have to retest the xs->state after we grab the mutex that protects the skb xmit code from, among a number of things, an xsk_unbind_dev() being executed from the notifier at the same time. Fixes: 42fddcc ("xsk: use state member for socket synchronization") Reported-by: Elza Mathew <[email protected]> Signed-off-by: Magnus Karlsson <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Björn Töpel <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent f54eeae commit 18b1ab7

File tree

1 file changed

+50
-19
lines changed

1 file changed

+50
-19
lines changed

net/xdp/xsk.c

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -403,18 +403,8 @@ EXPORT_SYMBOL(xsk_tx_peek_release_desc_batch);
403403
static int xsk_wakeup(struct xdp_sock *xs, u8 flags)
404404
{
405405
struct net_device *dev = xs->dev;
406-
int err;
407-
408-
rcu_read_lock();
409-
err = dev->netdev_ops->ndo_xsk_wakeup(dev, xs->queue_id, flags);
410-
rcu_read_unlock();
411-
412-
return err;
413-
}
414406

415-
static int xsk_zc_xmit(struct xdp_sock *xs)
416-
{
417-
return xsk_wakeup(xs, XDP_WAKEUP_TX);
407+
return dev->netdev_ops->ndo_xsk_wakeup(dev, xs->queue_id, flags);
418408
}
419409

420410
static void xsk_destruct_skb(struct sk_buff *skb)
@@ -533,6 +523,12 @@ static int xsk_generic_xmit(struct sock *sk)
533523

534524
mutex_lock(&xs->mutex);
535525

526+
/* Since we dropped the RCU read lock, the socket state might have changed. */
527+
if (unlikely(!xsk_is_bound(xs))) {
528+
err = -ENXIO;
529+
goto out;
530+
}
531+
536532
if (xs->queue_id >= xs->dev->real_num_tx_queues)
537533
goto out;
538534

@@ -596,16 +592,26 @@ static int xsk_generic_xmit(struct sock *sk)
596592
return err;
597593
}
598594

599-
static int __xsk_sendmsg(struct sock *sk)
595+
static int xsk_xmit(struct sock *sk)
600596
{
601597
struct xdp_sock *xs = xdp_sk(sk);
598+
int ret;
602599

603600
if (unlikely(!(xs->dev->flags & IFF_UP)))
604601
return -ENETDOWN;
605602
if (unlikely(!xs->tx))
606603
return -ENOBUFS;
607604

608-
return xs->zc ? xsk_zc_xmit(xs) : xsk_generic_xmit(sk);
605+
if (xs->zc)
606+
return xsk_wakeup(xs, XDP_WAKEUP_TX);
607+
608+
/* Drop the RCU lock since the SKB path might sleep. */
609+
rcu_read_unlock();
610+
ret = xsk_generic_xmit(sk);
611+
/* Reaquire RCU lock before going into common code. */
612+
rcu_read_lock();
613+
614+
return ret;
609615
}
610616

611617
static bool xsk_no_wakeup(struct sock *sk)
@@ -619,7 +625,7 @@ static bool xsk_no_wakeup(struct sock *sk)
619625
#endif
620626
}
621627

622-
static int xsk_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
628+
static int __xsk_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
623629
{
624630
bool need_wait = !(m->msg_flags & MSG_DONTWAIT);
625631
struct sock *sk = sock->sk;
@@ -639,11 +645,22 @@ static int xsk_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
639645

640646
pool = xs->pool;
641647
if (pool->cached_need_wakeup & XDP_WAKEUP_TX)
642-
return __xsk_sendmsg(sk);
648+
return xsk_xmit(sk);
643649
return 0;
644650
}
645651

646-
static int xsk_recvmsg(struct socket *sock, struct msghdr *m, size_t len, int flags)
652+
static int xsk_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
653+
{
654+
int ret;
655+
656+
rcu_read_lock();
657+
ret = __xsk_sendmsg(sock, m, total_len);
658+
rcu_read_unlock();
659+
660+
return ret;
661+
}
662+
663+
static int __xsk_recvmsg(struct socket *sock, struct msghdr *m, size_t len, int flags)
647664
{
648665
bool need_wait = !(flags & MSG_DONTWAIT);
649666
struct sock *sk = sock->sk;
@@ -669,6 +686,17 @@ static int xsk_recvmsg(struct socket *sock, struct msghdr *m, size_t len, int fl
669686
return 0;
670687
}
671688

689+
static int xsk_recvmsg(struct socket *sock, struct msghdr *m, size_t len, int flags)
690+
{
691+
int ret;
692+
693+
rcu_read_lock();
694+
ret = __xsk_recvmsg(sock, m, len, flags);
695+
rcu_read_unlock();
696+
697+
return ret;
698+
}
699+
672700
static __poll_t xsk_poll(struct file *file, struct socket *sock,
673701
struct poll_table_struct *wait)
674702
{
@@ -679,8 +707,11 @@ static __poll_t xsk_poll(struct file *file, struct socket *sock,
679707

680708
sock_poll_wait(file, sock, wait);
681709

682-
if (unlikely(!xsk_is_bound(xs)))
710+
rcu_read_lock();
711+
if (unlikely(!xsk_is_bound(xs))) {
712+
rcu_read_unlock();
683713
return mask;
714+
}
684715

685716
pool = xs->pool;
686717

@@ -689,14 +720,15 @@ static __poll_t xsk_poll(struct file *file, struct socket *sock,
689720
xsk_wakeup(xs, pool->cached_need_wakeup);
690721
else
691722
/* Poll needs to drive Tx also in copy mode */
692-
__xsk_sendmsg(sk);
723+
xsk_xmit(sk);
693724
}
694725

695726
if (xs->rx && !xskq_prod_is_empty(xs->rx))
696727
mask |= EPOLLIN | EPOLLRDNORM;
697728
if (xs->tx && xsk_tx_writeable(xs))
698729
mask |= EPOLLOUT | EPOLLWRNORM;
699730

731+
rcu_read_unlock();
700732
return mask;
701733
}
702734

@@ -728,7 +760,6 @@ static void xsk_unbind_dev(struct xdp_sock *xs)
728760

729761
/* Wait for driver to stop using the xdp socket. */
730762
xp_del_xsk(xs->pool, xs);
731-
xs->dev = NULL;
732763
synchronize_net();
733764
dev_put(dev);
734765
}

0 commit comments

Comments
 (0)