Skip to content

Commit 69e9fd9

Browse files
stefano-garzarellagregkh
authored andcommitted
vsock: fix locking in vsock_shutdown()
commit 1c5fae9 upstream. In vsock_shutdown() we touched some socket fields without holding the socket lock, such as 'state' and 'sk_flags'. Also, after the introduction of multi-transport, we are accessing 'vsk->transport' in vsock_send_shutdown() without holding the lock and this call can be made while the connection is in progress, so the transport can change in the meantime. To avoid issues, we hold the socket lock when we enter in vsock_shutdown() and release it when we leave. Among the transports that implement the 'shutdown' callback, only hyperv_transport acquired the lock. Since the caller now holds it, we no longer take it. Fixes: d021c34 ("VSOCK: Introduce VM Sockets") Signed-off-by: Stefano Garzarella <sgarzare@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent afe3170 commit 69e9fd9

2 files changed

Lines changed: 5 additions & 7 deletions

File tree

‎net/vmw_vsock/af_vsock.c‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -926,10 +926,12 @@ static int vsock_shutdown(struct socket *sock, int mode)
926926
*/
927927

928928
sk = sock->sk;
929+
930+
lock_sock(sk);
929931
if (sock->state == SS_UNCONNECTED) {
930932
err = -ENOTCONN;
931933
if (sk->sk_type == SOCK_STREAM)
932-
return err;
934+
goto out;
933935
} else {
934936
sock->state = SS_DISCONNECTING;
935937
err = 0;
@@ -938,17 +940,17 @@ static int vsock_shutdown(struct socket *sock, int mode)
938940
/* Receive and send shutdowns are treated alike. */
939941
mode = mode & (RCV_SHUTDOWN | SEND_SHUTDOWN);
940942
if (mode) {
941-
lock_sock(sk);
942943
sk->sk_shutdown |= mode;
943944
sk->sk_state_change(sk);
944-
release_sock(sk);
945945

946946
if (sk->sk_type == SOCK_STREAM) {
947947
sock_reset_flag(sk, SOCK_DONE);
948948
vsock_send_shutdown(sk, mode);
949949
}
950950
}
951951

952+
out:
953+
release_sock(sk);
952954
return err;
953955
}
954956

‎net/vmw_vsock/hyperv_transport.c‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -474,14 +474,10 @@ static void hvs_shutdown_lock_held(struct hvsock *hvs, int mode)
474474

475475
static int hvs_shutdown(struct vsock_sock *vsk, int mode)
476476
{
477-
struct sock *sk = sk_vsock(vsk);
478-
479477
if (!(mode & SEND_SHUTDOWN))
480478
return 0;
481479

482-
lock_sock(sk);
483480
hvs_shutdown_lock_held(vsk->trans, mode);
484-
release_sock(sk);
485481
return 0;
486482
}
487483

0 commit comments

Comments
 (0)