diff mbox series

net/vmw_vsock: improve locking in vsock_connect_timeout()

Message ID trinity-f8e0937a-cf0e-4d80-a76e-d9a958ba3ef1-1612535522360@3c-app-gmx-bap12 (mailing list archive)
State Accepted
Delegated to: Netdev Maintainers
Headers show
Series net/vmw_vsock: improve locking in vsock_connect_timeout() | expand

Checks

Context Check Description
netdev/cover_letter success Link
netdev/fixes_present success Link
netdev/patch_count success Link
netdev/tree_selection success Guessed tree name to be net-next
netdev/subject_prefix warning Target tree name not specified in the subject
netdev/cc_maintainers warning 7 maintainers not CCed: jhansen@vmware.com colin.king@canonical.com stefanha@redhat.com andraprs@amazon.com bergwolf@gmail.com davem@davemloft.net jeffv@google.com
netdev/source_inline success Was 0 now: 0
netdev/verify_signedoff success Link
netdev/module_param success Was 0 now: 0
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/verify_fixes success Link
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 19 lines checked
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/header_inline success Link
netdev/stable success Stable not CCed

Commit Message

Norbert Slusarek Feb. 5, 2021, 2:32 p.m. UTC
From: Norbert Slusarek <nslusarek@gmx.net>
Date: Fri, 5 Feb 2021 13:14:05 +0100
Subject: [PATCH] net/vmw_vsock: improve locking in vsock_connect_timeout()

A possible locking issue in vsock_connect_timeout() was recognized by
Eric Dumazet which might cause a null pointer dereference in
vsock_transport_cancel_pkt(). This patch assures that
vsock_transport_cancel_pkt() will be called within the lock, so a race
condition won't occur which could result in vsk->transport to be set to NULL.

Fixes: 380feae0def7 ("vsock: cancel packets when failing to connect")
Reported-by: Eric Dumazet <eric.dumazet@gmail.com>
Signed-off-by: Norbert Slusarek <nslusarek@gmx.net>
---
 net/vmw_vsock/af_vsock.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

--
2.30.0

Comments

Stefano Garzarella Feb. 5, 2021, 5:19 p.m. UTC | #1
On Fri, Feb 05, 2021 at 03:32:02PM +0100, Norbert Slusarek wrote:
>From: Norbert Slusarek <nslusarek@gmx.net>
>Date: Fri, 5 Feb 2021 13:14:05 +0100
>Subject: [PATCH] net/vmw_vsock: improve locking in vsock_connect_timeout()
>
>A possible locking issue in vsock_connect_timeout() was recognized by
>Eric Dumazet which might cause a null pointer dereference in
>vsock_transport_cancel_pkt(). This patch assures that
>vsock_transport_cancel_pkt() will be called within the lock, so a race
>condition won't occur which could result in vsk->transport to be set to NULL.
>
>Fixes: 380feae0def7 ("vsock: cancel packets when failing to connect")

I have a doubt about the tag to use, since until we introduced 
transports in commit c0cfa2d8a788 ("vsock: add multi-transports 
support") this issue didn't cause many problems.

But it must be said that in the commit 380feae0def7 ("vsock: cancel 
packets when failing to connect") the vsock_transport_cancel_pkt() was 
called with the lock held in vsock_stream_connect() and without lock in 
vsock_connect_timeout(), so maybe this tag is okay.

Anyway, the patch LGTM:

Reviewed-by: Stefano Garzarella <sgarzare@redhat.com>


>Reported-by: Eric Dumazet <eric.dumazet@gmail.com>
>Signed-off-by: Norbert Slusarek <nslusarek@gmx.net>
>---
> net/vmw_vsock/af_vsock.c | 5 +----
> 1 file changed, 1 insertion(+), 4 deletions(-)
>
>diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c
>index 6894f21dc147..ad7dd9d93b5b 100644
>--- a/net/vmw_vsock/af_vsock.c
>+++ b/net/vmw_vsock/af_vsock.c
>@@ -1243,7 +1243,6 @@ static void vsock_connect_timeout(struct work_struct *work)
> {
> 	struct sock *sk;
> 	struct vsock_sock *vsk;
>-	int cancel = 0;
>
> 	vsk = container_of(work, struct vsock_sock, connect_work.work);
> 	sk = sk_vsock(vsk);
>@@ -1254,11 +1253,9 @@ static void vsock_connect_timeout(struct work_struct *work)
> 		sk->sk_state = TCP_CLOSE;
> 		sk->sk_err = ETIMEDOUT;
> 		sk->sk_error_report(sk);
>-		cancel = 1;
>+		vsock_transport_cancel_pkt(vsk);
> 	}
> 	release_sock(sk);
>-	if (cancel)
>-		vsock_transport_cancel_pkt(vsk);
>
> 	sock_put(sk);
> }
>--
>2.30.0
>
Jakub Kicinski Feb. 7, 2021, 12:56 a.m. UTC | #2
On Fri, 5 Feb 2021 18:19:51 +0100 Stefano Garzarella wrote:
> On Fri, Feb 05, 2021 at 03:32:02PM +0100, Norbert Slusarek wrote:
> >From: Norbert Slusarek <nslusarek@gmx.net>
> >Date: Fri, 5 Feb 2021 13:14:05 +0100
> >Subject: [PATCH] net/vmw_vsock: improve locking in vsock_connect_timeout()
> >
> >A possible locking issue in vsock_connect_timeout() was recognized by
> >Eric Dumazet which might cause a null pointer dereference in
> >vsock_transport_cancel_pkt(). This patch assures that
> >vsock_transport_cancel_pkt() will be called within the lock, so a race
> >condition won't occur which could result in vsk->transport to be set to NULL.
> >
> >Fixes: 380feae0def7 ("vsock: cancel packets when failing to connect")  
> 
> I have a doubt about the tag to use, since until we introduced 
> transports in commit c0cfa2d8a788 ("vsock: add multi-transports 
> support") this issue didn't cause many problems.
> 
> But it must be said that in the commit 380feae0def7 ("vsock: cancel 
> packets when failing to connect") the vsock_transport_cancel_pkt() was 
> called with the lock held in vsock_stream_connect() and without lock in 
> vsock_connect_timeout(), so maybe this tag is okay.
> 
> Anyway, the patch LGTM:
> 
> Reviewed-by: Stefano Garzarella <sgarzare@redhat.com>

Applied, thanks!
diff mbox series

Patch

diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c
index 6894f21dc147..ad7dd9d93b5b 100644
--- a/net/vmw_vsock/af_vsock.c
+++ b/net/vmw_vsock/af_vsock.c
@@ -1243,7 +1243,6 @@  static void vsock_connect_timeout(struct work_struct *work)
 {
 	struct sock *sk;
 	struct vsock_sock *vsk;
-	int cancel = 0;

 	vsk = container_of(work, struct vsock_sock, connect_work.work);
 	sk = sk_vsock(vsk);
@@ -1254,11 +1253,9 @@  static void vsock_connect_timeout(struct work_struct *work)
 		sk->sk_state = TCP_CLOSE;
 		sk->sk_err = ETIMEDOUT;
 		sk->sk_error_report(sk);
-		cancel = 1;
+		vsock_transport_cancel_pkt(vsk);
 	}
 	release_sock(sk);
-	if (cancel)
-		vsock_transport_cancel_pkt(vsk);

 	sock_put(sk);
 }