Skip to content

Commit

Permalink
vsock: fix the race conditions in multi-transport support
Browse files Browse the repository at this point in the history
commit c518ada upstream.

There are multiple similar bugs implicitly introduced by the
commit c0cfa2d ("vsock: add multi-transports support") and
commit 6a2c096 ("vsock: prevent transport modules unloading").

The bug pattern:
 [1] vsock_sock.transport pointer is copied to a local variable,
 [2] lock_sock() is called,
 [3] the local variable is used.
VSOCK multi-transport support introduced the race condition:
vsock_sock.transport value may change between [1] and [2].

Let's copy vsock_sock.transport pointer to local variables after
the lock_sock() call.

Fixes: c0cfa2d ("vsock: add multi-transports support")
Signed-off-by: Alexander Popov <alex.popov@linux.com>
Reviewed-by: Stefano Garzarella <sgarzare@redhat.com>
Reviewed-by: Jorgen Hansen <jhansen@vmware.com>
Link: https://lore.kernel.org/r/20210201084719.2257066-1-alex.popov@linux.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
a13xp0p0v authored and gregkh committed Feb 3, 2021
1 parent a9cd144 commit 55d9004
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions net/vmw_vsock/af_vsock.c
Expand Up @@ -997,9 +997,12 @@ static __poll_t vsock_poll(struct file *file, struct socket *sock,
mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;

} else if (sock->type == SOCK_STREAM) {
const struct vsock_transport *transport = vsk->transport;
const struct vsock_transport *transport;

lock_sock(sk);

transport = vsk->transport;

/* Listening sockets that have connections in their accept
* queue can be read.
*/
Expand Down Expand Up @@ -1082,10 +1085,11 @@ static int vsock_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
err = 0;
sk = sock->sk;
vsk = vsock_sk(sk);
transport = vsk->transport;

lock_sock(sk);

transport = vsk->transport;

err = vsock_auto_bind(vsk);
if (err)
goto out;
Expand Down Expand Up @@ -1544,10 +1548,11 @@ static int vsock_stream_setsockopt(struct socket *sock,
err = 0;
sk = sock->sk;
vsk = vsock_sk(sk);
transport = vsk->transport;

lock_sock(sk);

transport = vsk->transport;

switch (optname) {
case SO_VM_SOCKETS_BUFFER_SIZE:
COPY_IN(val);
Expand Down Expand Up @@ -1680,7 +1685,6 @@ static int vsock_stream_sendmsg(struct socket *sock, struct msghdr *msg,

sk = sock->sk;
vsk = vsock_sk(sk);
transport = vsk->transport;
total_written = 0;
err = 0;

Expand All @@ -1689,6 +1693,8 @@ static int vsock_stream_sendmsg(struct socket *sock, struct msghdr *msg,

lock_sock(sk);

transport = vsk->transport;

/* Callers should not provide a destination with stream sockets. */
if (msg->msg_namelen) {
err = sk->sk_state == TCP_ESTABLISHED ? -EISCONN : -EOPNOTSUPP;
Expand Down Expand Up @@ -1823,11 +1829,12 @@ vsock_stream_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,

sk = sock->sk;
vsk = vsock_sk(sk);
transport = vsk->transport;
err = 0;

lock_sock(sk);

transport = vsk->transport;

if (!transport || sk->sk_state != TCP_ESTABLISHED) {
/* Recvmsg is supposed to return 0 if a peer performs an
* orderly shutdown. Differentiate between that case and when a
Expand Down

0 comments on commit 55d9004

Please sign in to comment.