Skip to content

Commit 3f74957

Browse files
stefano-garzarelladavem330
authored andcommitted
vsock: fix potential deadlock in transport->release()
Some transports (hyperv, virtio) acquire the sock lock during the .release() callback. In the vsock_stream_connect() we call vsock_assign_transport(); if the socket was previously assigned to another transport, the vsk->transport->release() is called, but the sock lock is already held in the vsock_stream_connect(), causing a deadlock reported by syzbot: INFO: task syz-executor280:9768 blocked for more than 143 seconds. Not tainted 5.6.0-rc1-syzkaller #0 "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message. syz-executor280 D27912 9768 9766 0x00000000 Call Trace: context_switch kernel/sched/core.c:3386 [inline] __schedule+0x934/0x1f90 kernel/sched/core.c:4082 schedule+0xdc/0x2b0 kernel/sched/core.c:4156 __lock_sock+0x165/0x290 net/core/sock.c:2413 lock_sock_nested+0xfe/0x120 net/core/sock.c:2938 virtio_transport_release+0xc4/0xd60 net/vmw_vsock/virtio_transport_common.c:832 vsock_assign_transport+0xf3/0x3b0 net/vmw_vsock/af_vsock.c:454 vsock_stream_connect+0x2b3/0xc70 net/vmw_vsock/af_vsock.c:1288 __sys_connect_file+0x161/0x1c0 net/socket.c:1857 __sys_connect+0x174/0x1b0 net/socket.c:1874 __do_sys_connect net/socket.c:1885 [inline] __se_sys_connect net/socket.c:1882 [inline] __x64_sys_connect+0x73/0xb0 net/socket.c:1882 do_syscall_64+0xfa/0x790 arch/x86/entry/common.c:294 entry_SYSCALL_64_after_hwframe+0x49/0xbe To avoid this issue, this patch remove the lock acquiring in the .release() callback of hyperv and virtio transports, and it holds the lock when we call vsk->transport->release() in the vsock core. Reported-by: [email protected] Fixes: 408624a ("vsock: use local transport when it is loaded") Signed-off-by: Stefano Garzarella <[email protected]> Reviewed-by: Stefan Hajnoczi <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 5c05a16 commit 3f74957

File tree

3 files changed

+12
-13
lines changed

3 files changed

+12
-13
lines changed

net/vmw_vsock/af_vsock.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,12 @@ int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk)
451451
if (vsk->transport == new_transport)
452452
return 0;
453453

454+
/* transport->release() must be called with sock lock acquired.
455+
* This path can only be taken during vsock_stream_connect(),
456+
* where we have already held the sock lock.
457+
* In the other cases, this function is called on a new socket
458+
* which is not assigned to any transport.
459+
*/
454460
vsk->transport->release(vsk);
455461
vsock_deassign_transport(vsk);
456462
}
@@ -753,20 +759,18 @@ static void __vsock_release(struct sock *sk, int level)
753759
vsk = vsock_sk(sk);
754760
pending = NULL; /* Compiler warning. */
755761

756-
/* The release call is supposed to use lock_sock_nested()
757-
* rather than lock_sock(), if a sock lock should be acquired.
758-
*/
759-
if (vsk->transport)
760-
vsk->transport->release(vsk);
761-
else if (sk->sk_type == SOCK_STREAM)
762-
vsock_remove_sock(vsk);
763-
764762
/* When "level" is SINGLE_DEPTH_NESTING, use the nested
765763
* version to avoid the warning "possible recursive locking
766764
* detected". When "level" is 0, lock_sock_nested(sk, level)
767765
* is the same as lock_sock(sk).
768766
*/
769767
lock_sock_nested(sk, level);
768+
769+
if (vsk->transport)
770+
vsk->transport->release(vsk);
771+
else if (sk->sk_type == SOCK_STREAM)
772+
vsock_remove_sock(vsk);
773+
770774
sock_orphan(sk);
771775
sk->sk_shutdown = SHUTDOWN_MASK;
772776

net/vmw_vsock/hyperv_transport.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -526,12 +526,9 @@ static bool hvs_close_lock_held(struct vsock_sock *vsk)
526526

527527
static void hvs_release(struct vsock_sock *vsk)
528528
{
529-
struct sock *sk = sk_vsock(vsk);
530529
bool remove_sock;
531530

532-
lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
533531
remove_sock = hvs_close_lock_held(vsk);
534-
release_sock(sk);
535532
if (remove_sock)
536533
vsock_remove_sock(vsk);
537534
}

net/vmw_vsock/virtio_transport_common.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -829,15 +829,13 @@ void virtio_transport_release(struct vsock_sock *vsk)
829829
struct sock *sk = &vsk->sk;
830830
bool remove_sock = true;
831831

832-
lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
833832
if (sk->sk_type == SOCK_STREAM)
834833
remove_sock = virtio_transport_close(vsk);
835834

836835
list_for_each_entry_safe(pkt, tmp, &vvs->rx_queue, list) {
837836
list_del(&pkt->list);
838837
virtio_transport_free_pkt(pkt);
839838
}
840-
release_sock(sk);
841839

842840
if (remove_sock)
843841
vsock_remove_sock(vsk);

0 commit comments

Comments
 (0)