Skip to contentSkip to conversation

vsock: fix stale sk_err handling after a failed connect

PATCH Applied Nguyen Dinh Phi 3 participants 7 messages Aug 13 – Aug 18

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v6 cover vsock: fix stale sk_err handling after a failed connect

A socket whose connect() failed keeps sk_err set. If that socket is later reused as a listener, vsock_accept() rejects an unrelated incoming connection, and on virtio/hyperv the resulting child socket is leaked.

Patch 1 removes the listener's sk_err check from vsock_accept(), since no vsock transport ever sets sk_err on a TCP_LISTEN socket. This will fix what the syzbot reported.

Patch 2 removes vsock_sock.rejected, now unreachable after patch 1.

Patch 3 is a related but separate fix: vsock_connect() now consumes sk_err via sock_error() once it has been returned to userspace, so a failed blocking connect() doesn't keep reporting the same error a second time.

---
Changes in v6:
- Reorder if/else in vsock_accept() to put the smaller block first.
- Add historical context to patch 2
v5: https://lore.kernel.org/netdev/[email protected]/
- Split into a series
- Remove the now-unused rejected flag from vsock_sock
v4: https://lore.kernel.org/netdev/[email protected]/
- Remove sk_err checks from vsock_accept()
v3: https://lore.kernel.org/netdev/[email protected]/
- Fix truncated title and add annotations to reproducer steps.
v2: https://lore.kernel.org/netdev/[email protected]/
- Add reproducer steps to commit message.
v1: https://lore.kernel.org/netdev/[email protected]/

Nguyen Dinh Phi (3):
vsock: don't check the listener's sk_err in vsock_accept()
vsock: remove the now-unused rejected flag
vsock: use sock_error() to consume sk_err after a failed connect

 include/net/af_vsock.h   |  5 +---
 net/vmw_vsock/af_vsock.c | 62 +++++++++++++---------------------------
 2 files changed, 21 insertions(+), 46 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH v6 1/3 vsock: don't check the listener's sk_err in vsock_accept()

Syzbot reported an issue which can be reproduced with these steps:

	r0 = socket(AF_VSOCK, SOCK_STREAM, 0)
	bind(r0, {VMADDR_CID_ANY, PORT})
	connect(r0, {VMADDR_CID_LOCAL, PORT}) -> -1, EPROTO (self-connect)
	listen(r0, backlog)                   -> 0
	r1 = socket(AF_VSOCK, SOCK_STREAM, 0)
	connect(r1, {VMADDR_CID_LOCAL, PORT}) -> 0
	accept(r0)                            -> -1, EPROTO (stale sk_err)

Basically, it creates a socket (r0) and triggers a self-connect after binding it. This self-connect fails with EPROTO because it loops back to r0 while the socket is still in the TCP_SYN_SENT state, causing it to be incorrectly dispatched to the connecting-client path. The unexpected packet type encountered there sets sk_err to EPROTO.

After that, it invokes a listen() call on the same socket. This listen() call succeeds because the kernel's listening path never inspects or clears sk_err. Then, a new socket (r1) is created as a normal client and connects to r0. However, vsock_accept() rejects this incoming connection because the listener's sk_err still holds the EPROTO error from the earlier failed self-connect.

This rejection causes the child socket created for r1's connection to never be freed on virtio or hyperv transports; only the VMCI transport implements pending_work to revisit and clean up a rejected socket.

For a non-blocking connect(), vsock_connect() may return -EINPROGRESS immediately, and vsock_connect_timeout() can later set sk->sk_err asynchronously.

Since no vsock transport ever sets sk_err on a socket while it is in TCP_LISTEN state, checking it in vsock_accept() serves no purpose and only carries forward errors left behind by earlier, unrelated connection attempts on the same socket. Remove the checks so accept() no longer rejects valid incoming connections because of a stale error, which also avoids the resource leak described above.

---
 net/vmw_vsock/af_vsock.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
net/vmw_vsock/af_vsock.c +3 -7
index 622dbd046799..3cd5c3561be3 100644
@@ -1893,7 +1893,7 @@ static int vsock_accept(struct socket *sock, struct socket *newsock,
 	timeout = sock_rcvtimeo(listener, arg->flags & O_NONBLOCK);
 
 	while ((connected = vsock_dequeue_accept(listener)) == NULL &&
-	       listener->sk_err == 0 && timeout != 0) {
+		timeout != 0) {
 		prepare_to_wait(sk_sleep(listener), &wait, TASK_INTERRUPTIBLE);
 		release_sock(listener);
 		timeout = schedule_timeout(timeout);
@@ -1906,13 +1906,9 @@ static int vsock_accept(struct socket *sock, struct socket *newsock,
 		}
 	}
 
-	if (listener->sk_err) {
-		err = -listener->sk_err;
-	} else if (!connected) {
+	if (!connected) {
 		err = -EAGAIN;
-	}
-
-	if (connected) {
+	} else {
 		sk_acceptq_removed(listener);
 
 		lock_sock_nested(connected, SINGLE_DEPTH_NESTING);
Signature · 1 line
2.53.0
maintainer Reviewed-by on v6 1/3
#2 lore raw

Re: [PATCH v6 1/3] vsock: don't check the listener's sk_err in vsock_accept()

Quoted 51 lines · Nguyen Dinh Phi

Syzbot reported an issue which can be reproduced with these steps:

	r0 = socket(AF_VSOCK, SOCK_STREAM, 0)
	bind(r0, {VMADDR_CID_ANY, PORT})
	connect(r0, {VMADDR_CID_LOCAL, PORT}) -> -1, EPROTO (self-connect)
	listen(r0, backlog)                   -> 0
	r1 = socket(AF_VSOCK, SOCK_STREAM, 0)
	connect(r1, {VMADDR_CID_LOCAL, PORT}) -> 0
	accept(r0)                            -> -1, EPROTO (stale sk_err)

Basically, it creates a socket (r0) and triggers a self-connect after binding it. This self-connect fails with EPROTO because it loops back to r0 while the socket is still in the TCP_SYN_SENT state, causing it to be incorrectly dispatched to the connecting-client path. The unexpected packet type encountered there sets sk_err to EPROTO.

After that, it invokes a listen() call on the same socket. This listen() call succeeds because the kernel's listening path never inspects or clears sk_err. Then, a new socket (r1) is created as a normal client and connects to r0. However, vsock_accept() rejects this incoming connection because the listener's sk_err still holds the EPROTO error from the earlier failed self-connect.

This rejection causes the child socket created for r1's connection to never be freed on virtio or hyperv transports; only the VMCI transport implements pending_work to revisit and clean up a rejected socket.

For a non-blocking connect(), vsock_connect() may return -EINPROGRESS immediately, and vsock_connect_timeout() can later set sk->sk_err asynchronously.

Since no vsock transport ever sets sk_err on a socket while it is in TCP_LISTEN state, checking it in vsock_accept() serves no purpose and only carries forward errors left behind by earlier, unrelated connection attempts on the same socket. Remove the checks so accept() no longer rejects valid incoming connections because of a stale error, which also avoids the resource leak described above.

---
net/vmw_vsock/af_vsock.c | 10 +++-------

1 file changed, 3 insertions(+), 7 deletions(-)
net/vmw_vsock/af_vsock.c +0 -0
index 622dbd046799..3cd5c3561be3 100644

net/vmw_vsock/af_vsock.c static int vsock_accept(struct socket *sock, struct socket *newsock, · line 1896

Quoted 5 lines
net/vmw_vsock/af_vsock.c +1 -1
@@ -1893,7 +1893,7 @@ static int vsock_accept(struct socket *sock, struct socket *newsock,
 	timeout = sock_rcvtimeo(listener, arg->flags & O_NONBLOCK);
 
 	while ((connected = vsock_dequeue_accept(listener)) == NULL &&
-	       listener->sk_err == 0 && timeout != 0) {
+		timeout != 0) {
                ^

nit: I think the alignment is wrong, maybe if you need to respin you can fix it

Anyway, this LGTM

#3 lore raw

PATCH v6 2/3 vsock: remove the now-unused rejected flag

In reply to Nguyen Dinh Phi

After previous patch, the branch marking a socket rejected in vsock_accept() is unreachable, and nothing ever sets vsk->rejected elsewhere.

In fact, since commit d021c344051a ("VSOCK: Introduce VM Sockets"), where rejected was introduced, there has never been a path that sets sk_err on a listening socket, so that branch has been dead code since the beginning.

Therefore, we can remove the rejected field from vsock_sock structure.

---
 include/net/af_vsock.h   |  5 +----
 net/vmw_vsock/af_vsock.c | 46 +++++++++++++---------------------------
 2 files changed, 16 insertions(+), 35 deletions(-)
include/net/af_vsock.h +1 -4
index 30046a3c20f7..3357ee62d10b 100644
@@ -52,13 +52,10 @@ struct vsock_sock {
 	 * The listening socket is the head for both lists.  Sockets created
 	 * for connection requests are placed in the pending list until they
 	 * are connected, at which point they are put in the accept queue list
-	 * so they can be accepted in accept().  If accept() cannot accept the
-	 * connection, it is marked as rejected so the cleanup function knows
-	 * to clean up the socket.
+	 * so they can be accepted in accept().
 	 */
 	struct list_head pending_links;
 	struct list_head accept_queue;
-	bool rejected;
 	struct delayed_work connect_work;
 	struct delayed_work pending_work;
 	struct delayed_work close_work;
net/vmw_vsock/af_vsock.c +15 -31

78 lines not on this page · raw message

Signature · 1 line
2.53.0
maintainer Reviewed-by on v6 2/3
#4 lore raw

Re: [PATCH v6 2/3] vsock: remove the now-unused rejected flag

Quoted 9 lines · Nguyen Dinh Phi

After previous patch, the branch marking a socket rejected in vsock_accept() is unreachable, and nothing ever sets vsk->rejected elsewhere.

In fact, since commit d021c344051a ("VSOCK: Introduce VM Sockets"), where rejected was introduced, there has never been a path that sets sk_err on a listening socket, so that branch has been dead code since the beginning.

Quoted 8 lines

Therefore, we can remove the rejected field from vsock_sock structure.

---
include/net/af_vsock.h | 5 +----
net/vmw_vsock/af_vsock.c | 46 +++++++++++++---------------------------

2 files changed, 16 insertions(+), 35 deletions(-)

Thanks for improving the commit description!

#5 lore raw

PATCH v6 3/3 vsock: use sock_error() to consume sk_err after a failed connect

In reply to Nguyen Dinh Phi

vsock_connect() returns sk_err to userspace but does not clear it:

  if (sk->sk_err) {
            err = -sk->sk_err;

For a blocking connect() the error has already been delivered as connect()'s return value, so leaving it set causes subsequent operations like poll()/epoll() to keep reporting POLLERR even though the connect failure was already delivered.

The error should be consumed once it has been returned to userspace. Switch to sock_error(), which reads and clears sk_err atomically, matching the behavior of other protocol implementations such as __inet_stream_connect().

Fixes d021c344051a ("VSOCK: Introduce VM Sockets")
---
 net/vmw_vsock/af_vsock.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
net/vmw_vsock/af_vsock.c +2 -4
index 62e22c4b13c0..e89cb84b8d73 100644
@@ -1842,12 +1842,10 @@ static int vsock_connect(struct socket *sock, struct sockaddr_unsized *addr,
 		prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
 	}
 
-	if (sk->sk_err) {
-		err = -sk->sk_err;
+	err = sock_error(sk);
+	if (err) {
 		sk->sk_state = TCP_CLOSE;
 		sock->state = SS_UNCONNECTED;
-	} else {
-		err = 0;
 	}
 
 out_wait:
Signature · 1 line
2.53.0
#6 lore raw
In reply to Nguyen Dinh Phi
This series was applied to netdev/net.git (main) by Paolo Abeni <[email protected]>:

Hello:

This series was applied to netdev/net.git (main)
by Paolo Abeni <[email protected]>:

Quoted 10 lines · On Fri, 14 Aug 2026 01:30:17 +0800 you wrote:

A socket whose connect() failed keeps sk_err set. If that socket is later reused as a listener, vsock_accept() rejects an unrelated incoming connection, and on virtio/hyperv the resulting child socket is leaked.

Patch 1 removes the listener's sk_err check from vsock_accept(), since no vsock transport ever sets sk_err on a TCP_LISTEN socket. This will fix what the syzbot reported.

[...]

Here is the summary with links:
- [v6,1/3] vsock: don't check the listener's sk_err in vsock_accept()

    https://git.kernel.org/netdev/net/c/b8c899cf5e7b
  - [v6,2/3] vsock: remove the now-unused rejected flag
    https://git.kernel.org/netdev/net/c/81fc0f369637
  - [v6,3/3] vsock: use sock_error() to consume sk_err after a failed connect
    https://git.kernel.org/netdev/net/c/96cbf8999309

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html