Netdev List
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: mhal@rbox.co
Cc: stefanha@redhat.com, sgarzare@redhat.com, bobbyeshleman@meta.com,
	mst@redhat.com, jasowangio@gmail.com, eperezma@redhat.com,
	davem@davemloft.net, kvm@vger.kernel.org,
	virtualization@lists.linux.dev, netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org, kuba@kernel.org
Subject: Re: [PATCH net] vhost/vsock: Fix socket state constant
Date: Mon, 16 Nov 2026 10:35:55 +0000	[thread overview]
Message-ID: <179482535544.3136.14958523425614003338@kernel.org> (raw)
In-Reply-To: <20261112-transport-reset-state-fix-v1-1-5698375f52a9@rbox.co>

Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.

Critical: 0 · High: 1 · Medium: 1 · Low: 0

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [High] Writing TCP_CLOSE plus sk_err = ECONNRESET in
  vhost_vsock_reset_orphans() (drivers/vhost/vsock.c:792-793) brings…
- [Medium] vhost_vsock_reset_orphans() now moves every orphaned connected
  socket to TCP_CLOSE unconditionally, after setting SOCK_DONE, even…

--

vhost/vsock: Fix socket state constant

This patch replaces SS_UNCONNECTED with TCP_CLOSE in
vhost_vsock_reset_orphans(). SS_UNCONNECTED has the value 1, which is the
same as TCP_ESTABLISHED. So far, orphaned sockets have been left in the
established state after the vhost device was released.

> diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c
> index 88a310390eb0a..fd224f7681d92 100644
> --- a/drivers/vhost/vsock.c
> +++ b/drivers/vhost/vsock.c
> @@ -789,7 +789,7 @@ static void vhost_vsock_reset_orphans(struct sock *sk)
>  
>  	sock_set_flag(sk, SOCK_DONE);
>  	vsk->peer_shutdown = SHUTDOWN_MASK;
> -	sk->sk_state = SS_UNCONNECTED;
> +	sk->sk_state = TCP_CLOSE;

[Severity: High]
Can this let connect() reset a socket that is still linked in
vsock_connected_table? Commit 4f6ac74fa594 ("vsock: treat TCP_CLOSING as
once-established") fixed that problem for the other close paths.

vhost_vsock_reset_orphans() runs without lock_sock(). Suppose it runs
after virtio_transport_recv_connecting() has handled a RESPONSE, but
before the waiter in vsock_connect() gets the socket lock back:

vhost TX worker
virtio_transport_recv_connecting()
    sk->sk_state = TCP_ESTABLISHED;
    sk->sk_socket->state = SS_CONNECTED;
    vsock_insert_connected(vsk);

vhost_vsock_dev_release()
  vhost_vsock_reset_orphans()
    sk->sk_state = TCP_CLOSE;
    sk->sk_err = ECONNRESET;

The waiter then fails the TCP_ESTABLISHED/TCP_CLOSING check and ends up
here:

net/vmw_vsock/af_vsock.c:vsock_connect() {
    ...
	err = sock_error(sk);
	if (err) {
		sk->sk_state = TCP_CLOSE;
		sock->state = SS_UNCONNECTED;
	}
    ...
}

connect() returns -ECONNRESET, but the socket is still in the connected
table. With the old value (1 == TCP_ESTABLISHED), connect() returned 0 and
the socket stayed SS_CONNECTED.

After that, ordinary API calls seem to be enough to insert the socket a
second time:

1. A second connect(): sock->state is SS_UNCONNECTED, so it does not get
   -EISCONN. vsock_assign_transport() returns early because the transport
   is the same. That skips both vsock_remove_sock() and
   sock_reset_flag(sk, SOCK_DONE).

2. shutdown() while the socket is SS_CONNECTING: vsock_shutdown() calls
   sock_reset_flag(sk, SOCK_DONE). Without that flag,
   virtio_transport_recv_pkt() no longer drops the incoming RESPONSE.

3. A RESPONSE from a guest with that CID reaches
   virtio_transport_recv_connecting() in TCP_SYN_SENT. It calls
   vsock_insert_connected() a second time.

__vsock_insert_connected() then does an unconditional sock_hold() and
list_add() on a node that is already linked, which corrupts the bucket.

Any later walk of that bucket under vsock_table_lock can then loop forever,
for example in vsock_find_connected_socket() or
vsock_for_each_connected_socket(). With CONFIG_DEBUG_LIST it hits the
list_add double add BUG instead. One socket reference is also leaked.

Only the first step depends on timing, a race with VM teardown. The rest
is normal use of the socket API.

> +	sk->sk_state = TCP_CLOSE;

[Severity: Medium]
Does this drop data that is still queued in the socket's rx_queue?

vhost_vsock_reset_orphans() now sets SOCK_DONE and then moves the socket
to TCP_CLOSE unconditionally. __vsock_connectible_recvmsg() then returns
early without dequeuing anything:

net/vmw_vsock/af_vsock.c:__vsock_connectible_recvmsg() {
    ...
	if (!transport || sk->sk_state != TCP_ESTABLISHED) {
		...
		if (sock_flag(sk, SOCK_DONE))
			err = 0;
		else
			err = -ENOTCONN;

		goto out;
	}
    ...
}

So recv() returns 0 (EOF), and sk_err is not reported there.

The other close paths only leave the readable state when no data is
pending:

net/vmw_vsock/virtio_transport_common.c:virtio_transport_do_close() {
    ...
	if (vsock_stream_has_data(vsk) <= 0)
		sk->sk_state = TCP_CLOSING;
    ...
}

hvs_do_close_lock_held() in hyperv_transport.c has the same check.
vmci_transport_handle_detach() skips the check only on the host side,
where queue-pair data can't be read once the VM detaches. With vhost, the
data is still in host-kernel skbs and can be read.

One way to hit this:

1. The guest writes data and sends SHUTDOWN.
   virtio_transport_recv_connected() keeps the socket TCP_ESTABLISHED
   because data is pending.

2. The VM exits, or the VMM closes the vhost fd.
   vhost_vsock_dev_release() calls vhost_vsock_reset_orphans().

3. The host application's next recv() returns 0, and the end of the
   stream is lost.

With the old value the socket stayed TCP_ESTABLISHED, so the application
could read the remaining data before seeing EOF.

On the guest side, virtio_vsock_reset_sock() also sets TCP_CLOSE
unconditionally on a transport reset, so this may be the intended reset
behaviour. It is still different from the RST path, which keeps queued
data readable.

Should vhost_vsock_reset_orphans() check vsock_stream_has_data() the way
the other transports do?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261112-transport-reset-state-fix-v1-1-5698375f52a9%40rbox.co

      parent reply	other threads:[~2026-11-16 10:35 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-12 10:34 [PATCH net] vhost/vsock: Fix socket state constant Michal Luczaj
2026-11-12 10:36 ` netdev-bot+sinfo
2026-11-12 10:52   ` Michal Luczaj
2026-11-13  8:44     ` Michal Luczaj
2026-11-16 10:35 ` netdev-bot+sashiko [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=179482535544.3136.14958523425614003338@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=bobbyeshleman@meta.com \
    --cc=davem@davemloft.net \
    --cc=eperezma@redhat.com \
    --cc=jasowangio@gmail.com \
    --cc=kuba@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mhal@rbox.co \
    --cc=mst@redhat.com \
    --cc=netdev@vger.kernel.org \
    --cc=sgarzare@redhat.com \
    --cc=stefanha@redhat.com \
    --cc=virtualization@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox