vsock: only undo SS_CONNECTING when connect() fails
Subsystems (4)
Bobby Eshleman
reviewerPATCH v3 1/1 vsock: only undo SS_CONNECTING when connect() fails
vsock_connect() sleeps with the socket unlocked. Meanwhile the transport can complete the handshake, setting sock->state to SS_CONNECTED, and then reset the connection, e.g. in vhost_vsock_reset_orphans(), virtio_vsock_reset_sock() or vmci_transport_handle_detach(). When connect() gets the lock back, it finds sk_err set and moves the socket back to SS_UNCONNECTED. Userspace can then connect() the socket again, and a connect() to a CID on another transport makes vsock_assign_transport() release the transport the socket was connected on.
Only move the socket back to SS_UNCONNECTED if it is still SS_CONNECTING. A socket that got connected stays SS_CONNECTED, and another connect() fails with -EISCONN.
Likewise, only move sk_state to TCP_CLOSE if it is still TCP_SYN_SENT. Otherwise a second thread blocked in connect() on the same socket can time out after the first one failed and called listen(), and overwrite TCP_LISTEN.
---
Changes in v3:
- Explain why vsock_connect_close() undoes only the state that connect()
set (Stefano)
- Pick up Michal's RoB
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Only reset sk_state if it is still TCP_SYN_SENT, so a concurrent
connect() caller that times out late doesn't overwrite TCP_LISTEN
(Sashiko).
- Drop Michal's Reviewed-by since the code changed.
- Link to v1: https://lore.kernel.org/r/[email protected]
--- net/vmw_vsock/af_vsock.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-)
net/vmw_vsock/af_vsock.c +18 -6
@@ -1704,6 +1704,21 @@ static void vsock_connect_timeout(struct work_struct *work)sock_put (sk); } +/* Undo only the state this connect() attempt set. While we slept, the +* transport may have connected and then reset the socket (it is still in +* vsock_connected_table, so it must not go back to SS_UNCONNECTED), or +* another thread may have turned it into a listener. +*/ +static void vsock_connect_close (struct socket *sock) +{ +struct sock *sk = sock->sk; + +if (sk->sk_state ==TCP_SYN_SENT ) + sk->sk_state =TCP_CLOSE ; +if (sock->state ==SS_CONNECTING ) + sock->state =SS_UNCONNECTED ; +} +static int vsock_connect (struct socket *sock,struct sockaddr_unsized *addr,int addr_len,int flags) {
@@ -1862,8 +1877,7 @@ static int vsock_connect(struct socket *sock, struct sockaddr_unsized *addr,* sk_state == TCP_SYN_SENT, which hereby we break. * In such case VIRTIO_VSOCK_OP_RST will follow. */ - sk->sk_state =TCP_CLOSE ; - sock->state =SS_UNCONNECTED ; +vsock_connect_close (sock);/* Try to cancel VIRTIO_VSOCK_OP_REQUEST skb sent out by * transport->connect().
@@ -1877,10 +1891,8 @@ static int vsock_connect(struct socket *sock, struct sockaddr_unsized *addr, } err =sock_error (sk); -if (err) { - sk->sk_state =TCP_CLOSE ; - sock->state =SS_UNCONNECTED ; - } +if (err) +vsock_connect_close (sock); out_wait:finish_wait (sk_sleep (sk), &wait);
---
base-commit: 76fca30e8d34eb151fcf4f363b51c21b7e8c4709
change-id: 20261116-vsock-connect-close-1beb7cb51e7a
Best regards,
Signature · 1 line
Bobby Eshleman <[email protected]>
[email protected]
AI reviewThis is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
Hi!
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.
- Whether the issue was actually triggered, or is only theoretical (e.g. found by LLM and not proven at runtime). If it was triggered please include the symptoms, like the stack trace or kernel messages.
Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account.
*IMPORTANT*: If you do repost (for other reasons) - please include the relevant info in the cover letter or commit messages.
Signature · 2 lines
The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 19 lines
vsock_connect()sleeps with the socket unlocked. Meanwhile the transport can complete the handshake, settingsock->statetoSS_CONNECTED, and then reset the connection, e.g. invhost_vsock_reset_orphans(),virtio_vsock_reset_sock()orvmci_transport_handle_detach(). Whenconnect()gets the lock back, it findssk_errset and moves the socket back toSS_UNCONNECTED. Userspace can thenconnect()the socket again, and aconnect()to a CID on another transport makesvsock_assign_transport()release the transport the socket was connected on.Only move the socket back to
SS_UNCONNECTEDif it is stillSS_CONNECTING. A socket that got connected staysSS_CONNECTED, and anotherconnect()fails with-EISCONN.Likewise, only move
sk_statetoTCP_CLOSEif it is stillTCP_SYN_SENT. Otherwise a second thread blocked inconnect()on the same socket can time out after the first one failed and calledlisten(), and overwriteTCP_LISTEN.
Quoted 6 lines
Fixes d021c344051a ("VSOCK: Introduce VM Sockets")Reported-by Kyle Zeng <[email protected]>Suggested-by Michal Luczaj <[email protected]>Reviewed-by Michal Luczaj <[email protected]>Signed-off-by Bobby Eshleman <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1