vsock: fix stale sk_err handling after a failed connect
Subsystems (4)
v5 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 v5:
- 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
PATCH v5 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
@@ -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,12 +1906,6 @@ static int vsock_accept(struct socket *sock, struct socket *newsock, } } -if (listener->sk_err) { - err = -listener->sk_err; - }else if (!connected) { - err = -EAGAIN ; - } -if (connected) {sk_acceptq_removed (listener);
@@ -1941,6 +1935,8 @@ static int vsock_accept(struct socket *sock, struct socket *newsock,release_sock (connected);sock_put (connected); + }else { + err = -EAGAIN ; } out:
Signature · 1 line
2.53.0
PATCH v5 2/3 vsock: remove the now-unused rejected flag
In reply to Nguyen Dinh PhiAfter previous patch, the branch marking a socket rejected in vsock_accept() is unreachable, and nothing ever sets vsk->rejected elsewhere.
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
@@ -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
PATCH v5 3/3 vsock: use sock_error() to consume sk_err after a failed connect
In reply to Nguyen Dinh Phivsock_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().
--- net/vmw_vsock/af_vsock.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
net/vmw_vsock/af_vsock.c +2 -4
@@ -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
Stefano Garzarella
maintainerRe: [PATCH v5 1/3] vsock: don't check the listener's sk_err in vsock_accept()
In reply to Nguyen Dinh PhiQuoted 60 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_SENTstate, causing it to be incorrectly dispatched to the connecting-client path. The unexpected packet type encountered there setssk_errto EPROTO.After that, it invokes a
listen()call on the same socket. Thislisten()call succeeds because the kernel's listening path never inspects or clearssk_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'ssk_errstill 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_workto revisit and clean up a rejected socket.For a non-blocking
connect(),vsock_connect()may return-EINPROGRESSimmediately, andvsock_connect_timeout()can later setsk->sk_errasynchronously.Since no vsock transport ever sets
sk_erron a socket while it is inTCP_LISTENstate, checking it invsock_accept()serves no purpose and only carries forward errors left behind by earlier, unrelated connection attempts on the same socket. Remove the checks soaccept()no longer rejects valid incoming connections because of a stale error, which also avoids the resource leak described above.Fixes d021c344051a ("VSOCK: Introduce VM Sockets")Reported-by [email protected]Suggested-by Michal Luczaj <[email protected]>Signed-off-by Nguyen Dinh Phi <[email protected]>---
net/vmw_vsock/af_vsock.c| 10 +++-------1 file changed, 3 insertions(+), 7 deletions(-)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 ) {prepare_to_wait (sk_sleep (listener), &wait,TASK_INTERRUPTIBLE );release_sock (listener); timeout =schedule_timeout (timeout);
net/vmw_vsock/af_vsock.c static int vsock_accept(struct socket *sock, struct socket *newsock, · line 1914
Quoted 9 lines
net/vmw_vsock/af_vsock.c +0
-6@@ -1906,12 +1906,6 @@ static int vsock_accept(struct socket *sock, struct socket *newsock, } } -if (listener->sk_err) { - err = -listener->sk_err; - }else if (!connected) { - err = -EAGAIN ; - } -
As I suggested in v4, please just add here:
if (!connected) {
err = -EAGAIN;
} else {I prefer the goto, but I understand that for a stable patch it might affect too much code, so we can do that later as part of a cleanup. Anyway having the smallest block at the beginning of an if/else statement greatly improves readability IMO.
The rest LGTM.
Thanks,
Stefano
Quoted 15 lines
net/vmw_vsock/af_vsock.c +2
-0if (connected) {sk_acceptq_removed (listener);@@ -1941,6 +1935,8 @@ static int vsock_accept(struct socket *sock, struct socket *newsock,release_sock (connected);sock_put (connected); + }else { + err = -EAGAIN ; }out:
Signature · 1 line
2.53.0
Stefano Garzarella
maintainerRe: [PATCH v5 2/3] vsock: remove the now-unused rejected flag
In reply to Nguyen Dinh PhiQuoted 5 lines · Nguyen Dinh Phi
After previous patch, the branch marking a socket rejected in
vsock_accept()is unreachable, and nothing ever setsvsk->rejectedelsewhere.Therefore, we can remove the
rejectedfield fromvsock_sockstructure.
I'd like to mention here that since commit d021c344051a ("VSOCK: Introduce VM Sockets") where rejected was introduced, we didn't have any path where sk_err is set on a listener socket, so that path was dead since the beginning.
The rest LGTM!
Thanks,
Stefano
Quoted 116 lines
Suggested-by Stefano Garzarella <[email protected]>Signed-off-by Nguyen Dinh Phi <[email protected]>---
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@@ -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@@ -38,10 +38,9 @@* pending socket. When that socket reaches the connected state, it is removed * from the listener socket's pending list and enqueued in the listener * socket's accept queue. Callers of accept(2) will accept connected sockets -* from the listener socket's accept queue. If the socket cannot be accepted -* for some reason then it is marked rejected. Once the connection is -* accepted, it is owned by the user process and the responsibility for cleanup -* falls with that user process. +* from the listener socket's accept queue. Once the connection is accepted, +* it is owned by the user process and the responsibility for cleanup falls +* with that user process. * * - It is possible that these pending sockets will never reach the connected * state; in fact, we may never receive another packet after the connection @@ -49,9 +48,7 @@* future, after some amount of time passes where a connection should have been * established. This function ensures that the socket is off all lists so it * cannot be retrieved, then drops all references to the socket so it is cleaned -* up (sock_put() -> sk_free() -> our sk_destruct implementation). Note this -* function will also cleanup rejected sockets, those that reach the connected -* state but leave it before they have been accepted. +* up (sock_put() -> sk_free() -> our sk_destruct implementation). * * - Lock ordering for pending or accept queue sockets is: * @@ -774,11 +771,10 @@ static void vsock_pending_work(struct work_struct *work)if (vsock_is_pending (sk)) {vsock_remove_pending (listener, sk); - }else if (!vsk->rejected) { -/* We are not on the pending list and accept() did not reject -* us, so we must have been accepted by our user process. We -* just need to drop our references to the sockets and be on -* our way. + }else { +/* We are not on the pending list so we must have been accepted +* by our user process. We just need to drop our references to +* the sockets and be on our way. */ cleanup = false; goto out;@@ -942,7 +938,6 @@ static struct sock *__vsock_create(struct net *net, vsk->listener =NULL ;INIT_LIST_HEAD (&vsk->pending_links);INIT_LIST_HEAD (&vsk->accept_queue); - vsk->rejected = false; vsk->sent_request = false; vsk->ignore_connecting_rst = false;WRITE_ONCE (vsk->peer_shutdown,0 );@@ -1912,26 +1907,15 @@ static int vsock_accept(struct socket *sock, struct socket *newsock,lock_sock_nested (connected,SINGLE_DEPTH_NESTING ); vconnected =vsock_sk (connected); -/* If the listener socket has received an error, then we should -* reject this socket and return. Note that we simply mark the -* socket rejected, drop our reference, and let the cleanup -* function handle the cleanup; the fact that we found it in -* the listener's accept queue guarantees that the cleanup -* function hasn't run yet. -*/ -if (err) { - vconnected->rejected = true; - }else { - newsock->state =SS_CONNECTED ; -sock_graft (connected, newsock); + newsock->state =SS_CONNECTED ; +sock_graft (connected, newsock); -set_bit (SOCK_CUSTOM_SOCKOPT , - &connected->sk_socket->flags); +set_bit (SOCK_CUSTOM_SOCKOPT , + &connected->sk_socket->flags); -if (vsock_msgzerocopy_allow (vconnected->transport)) -set_bit (SOCK_SUPPORT_ZC , - &connected->sk_socket->flags); - } +if (vsock_msgzerocopy_allow (vconnected->transport)) +set_bit (SOCK_SUPPORT_ZC , + &connected->sk_socket->flags);release_sock (connected);sock_put (connected);Signature · 1 line
2.53.0
Re: [PATCH v5 3/3] vsock: use sock_error() to consume sk_err after a failed connect
In reply to Nguyen Dinh PhiQuoted 14 lines · Nguyen Dinh Phi
vsock_connect()returnssk_errto 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 clearssk_erratomically, matching the behavior of other protocol implementations such as__inet_stream_connect().
Quoted 6 lines
Fixes d021c344051a ("VSOCK: Introduce VM Sockets")Tested-by Wupeng Ma <[email protected]>Signed-off-by Nguyen Dinh Phi <[email protected]>---
net/vmw_vsock/af_vsock.c| 6 ++----1 file changed, 2 insertions(+), 4 deletions(-)