netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] net: tls: fix possible race condition between do_tls_getsockopt_conf() and do_tls_setsockopt_conf()
@ 2023-02-28  2:33 Hangyu Hua
  2023-03-02  4:40 ` patchwork-bot+netdevbpf
  2023-03-26 14:12 ` Guenter Roeck
  0 siblings, 2 replies; 5+ messages in thread
From: Hangyu Hua @ 2023-02-28  2:33 UTC (permalink / raw)
  To: borisp, john.fastabend, kuba, davem, edumazet, pabeni,
	davejwatson, aviadye, ilyal, fw, sd
  Cc: netdev, linux-kernel, Hangyu Hua

ctx->crypto_send.info is not protected by lock_sock in
do_tls_getsockopt_conf(). A race condition between do_tls_getsockopt_conf()
and do_tls_setsockopt_conf() can cause a NULL point dereference or
use-after-free read when memcpy.

Please check the following link for pre-information:
 https://lore.kernel.org/all/Y/ht6gQL+u6fj3dG@hog/

Fixes: 3c4d7559159b ("tls: kernel TLS support")
Signed-off-by: Hangyu Hua <hbh25y@gmail.com>
---

	v2: Follow the advice of Jakub and lock do_tls_getsockopt()

 net/tls/tls_main.c | 23 +++++------------------
 1 file changed, 5 insertions(+), 18 deletions(-)

diff --git a/net/tls/tls_main.c b/net/tls/tls_main.c
index 3735cb00905d..b32c112984dd 100644
--- a/net/tls/tls_main.c
+++ b/net/tls/tls_main.c
@@ -405,13 +405,11 @@ static int do_tls_getsockopt_conf(struct sock *sk, char __user *optval,
 			rc = -EINVAL;
 			goto out;
 		}
-		lock_sock(sk);
 		memcpy(crypto_info_aes_gcm_128->iv,
 		       cctx->iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE,
 		       TLS_CIPHER_AES_GCM_128_IV_SIZE);
 		memcpy(crypto_info_aes_gcm_128->rec_seq, cctx->rec_seq,
 		       TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE);
-		release_sock(sk);
 		if (copy_to_user(optval,
 				 crypto_info_aes_gcm_128,
 				 sizeof(*crypto_info_aes_gcm_128)))
@@ -429,13 +427,11 @@ static int do_tls_getsockopt_conf(struct sock *sk, char __user *optval,
 			rc = -EINVAL;
 			goto out;
 		}
-		lock_sock(sk);
 		memcpy(crypto_info_aes_gcm_256->iv,
 		       cctx->iv + TLS_CIPHER_AES_GCM_256_SALT_SIZE,
 		       TLS_CIPHER_AES_GCM_256_IV_SIZE);
 		memcpy(crypto_info_aes_gcm_256->rec_seq, cctx->rec_seq,
 		       TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE);
-		release_sock(sk);
 		if (copy_to_user(optval,
 				 crypto_info_aes_gcm_256,
 				 sizeof(*crypto_info_aes_gcm_256)))
@@ -451,13 +447,11 @@ static int do_tls_getsockopt_conf(struct sock *sk, char __user *optval,
 			rc = -EINVAL;
 			goto out;
 		}
-		lock_sock(sk);
 		memcpy(aes_ccm_128->iv,
 		       cctx->iv + TLS_CIPHER_AES_CCM_128_SALT_SIZE,
 		       TLS_CIPHER_AES_CCM_128_IV_SIZE);
 		memcpy(aes_ccm_128->rec_seq, cctx->rec_seq,
 		       TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE);
-		release_sock(sk);
 		if (copy_to_user(optval, aes_ccm_128, sizeof(*aes_ccm_128)))
 			rc = -EFAULT;
 		break;
@@ -472,13 +466,11 @@ static int do_tls_getsockopt_conf(struct sock *sk, char __user *optval,
 			rc = -EINVAL;
 			goto out;
 		}
-		lock_sock(sk);
 		memcpy(chacha20_poly1305->iv,
 		       cctx->iv + TLS_CIPHER_CHACHA20_POLY1305_SALT_SIZE,
 		       TLS_CIPHER_CHACHA20_POLY1305_IV_SIZE);
 		memcpy(chacha20_poly1305->rec_seq, cctx->rec_seq,
 		       TLS_CIPHER_CHACHA20_POLY1305_REC_SEQ_SIZE);
-		release_sock(sk);
 		if (copy_to_user(optval, chacha20_poly1305,
 				sizeof(*chacha20_poly1305)))
 			rc = -EFAULT;
@@ -493,13 +485,11 @@ static int do_tls_getsockopt_conf(struct sock *sk, char __user *optval,
 			rc = -EINVAL;
 			goto out;
 		}
-		lock_sock(sk);
 		memcpy(sm4_gcm_info->iv,
 		       cctx->iv + TLS_CIPHER_SM4_GCM_SALT_SIZE,
 		       TLS_CIPHER_SM4_GCM_IV_SIZE);
 		memcpy(sm4_gcm_info->rec_seq, cctx->rec_seq,
 		       TLS_CIPHER_SM4_GCM_REC_SEQ_SIZE);
-		release_sock(sk);
 		if (copy_to_user(optval, sm4_gcm_info, sizeof(*sm4_gcm_info)))
 			rc = -EFAULT;
 		break;
@@ -513,13 +503,11 @@ static int do_tls_getsockopt_conf(struct sock *sk, char __user *optval,
 			rc = -EINVAL;
 			goto out;
 		}
-		lock_sock(sk);
 		memcpy(sm4_ccm_info->iv,
 		       cctx->iv + TLS_CIPHER_SM4_CCM_SALT_SIZE,
 		       TLS_CIPHER_SM4_CCM_IV_SIZE);
 		memcpy(sm4_ccm_info->rec_seq, cctx->rec_seq,
 		       TLS_CIPHER_SM4_CCM_REC_SEQ_SIZE);
-		release_sock(sk);
 		if (copy_to_user(optval, sm4_ccm_info, sizeof(*sm4_ccm_info)))
 			rc = -EFAULT;
 		break;
@@ -535,13 +523,11 @@ static int do_tls_getsockopt_conf(struct sock *sk, char __user *optval,
 			rc = -EINVAL;
 			goto out;
 		}
-		lock_sock(sk);
 		memcpy(crypto_info_aria_gcm_128->iv,
 		       cctx->iv + TLS_CIPHER_ARIA_GCM_128_SALT_SIZE,
 		       TLS_CIPHER_ARIA_GCM_128_IV_SIZE);
 		memcpy(crypto_info_aria_gcm_128->rec_seq, cctx->rec_seq,
 		       TLS_CIPHER_ARIA_GCM_128_REC_SEQ_SIZE);
-		release_sock(sk);
 		if (copy_to_user(optval,
 				 crypto_info_aria_gcm_128,
 				 sizeof(*crypto_info_aria_gcm_128)))
@@ -559,13 +545,11 @@ static int do_tls_getsockopt_conf(struct sock *sk, char __user *optval,
 			rc = -EINVAL;
 			goto out;
 		}
-		lock_sock(sk);
 		memcpy(crypto_info_aria_gcm_256->iv,
 		       cctx->iv + TLS_CIPHER_ARIA_GCM_256_SALT_SIZE,
 		       TLS_CIPHER_ARIA_GCM_256_IV_SIZE);
 		memcpy(crypto_info_aria_gcm_256->rec_seq, cctx->rec_seq,
 		       TLS_CIPHER_ARIA_GCM_256_REC_SEQ_SIZE);
-		release_sock(sk);
 		if (copy_to_user(optval,
 				 crypto_info_aria_gcm_256,
 				 sizeof(*crypto_info_aria_gcm_256)))
@@ -614,11 +598,9 @@ static int do_tls_getsockopt_no_pad(struct sock *sk, char __user *optval,
 	if (len < sizeof(value))
 		return -EINVAL;
 
-	lock_sock(sk);
 	value = -EINVAL;
 	if (ctx->rx_conf == TLS_SW || ctx->rx_conf == TLS_HW)
 		value = ctx->rx_no_pad;
-	release_sock(sk);
 	if (value < 0)
 		return value;
 
@@ -635,6 +617,8 @@ static int do_tls_getsockopt(struct sock *sk, int optname,
 {
 	int rc = 0;
 
+	lock_sock(sk);
+
 	switch (optname) {
 	case TLS_TX:
 	case TLS_RX:
@@ -651,6 +635,9 @@ static int do_tls_getsockopt(struct sock *sk, int optname,
 		rc = -ENOPROTOOPT;
 		break;
 	}
+
+	release_sock(sk);
+
 	return rc;
 }
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] net: tls: fix possible race condition between do_tls_getsockopt_conf() and do_tls_setsockopt_conf()
  2023-02-28  2:33 [PATCH v2] net: tls: fix possible race condition between do_tls_getsockopt_conf() and do_tls_setsockopt_conf() Hangyu Hua
@ 2023-03-02  4:40 ` patchwork-bot+netdevbpf
  2023-03-26 14:12 ` Guenter Roeck
  1 sibling, 0 replies; 5+ messages in thread
From: patchwork-bot+netdevbpf @ 2023-03-02  4:40 UTC (permalink / raw)
  To: Hangyu Hua
  Cc: borisp, john.fastabend, kuba, davem, edumazet, pabeni,
	davejwatson, aviadye, ilyal, fw, sd, netdev, linux-kernel

Hello:

This patch was applied to netdev/net.git (main)
by Jakub Kicinski <kuba@kernel.org>:

On Tue, 28 Feb 2023 10:33:44 +0800 you wrote:
> ctx->crypto_send.info is not protected by lock_sock in
> do_tls_getsockopt_conf(). A race condition between do_tls_getsockopt_conf()
> and do_tls_setsockopt_conf() can cause a NULL point dereference or
> use-after-free read when memcpy.
> 
> Please check the following link for pre-information:
>  https://lore.kernel.org/all/Y/ht6gQL+u6fj3dG@hog/
> 
> [...]

Here is the summary with links:
  - [v2] net: tls: fix possible race condition between do_tls_getsockopt_conf() and do_tls_setsockopt_conf()
    https://git.kernel.org/netdev/net/c/49c47cc21b5b

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] net: tls: fix possible race condition between do_tls_getsockopt_conf() and do_tls_setsockopt_conf()
  2023-02-28  2:33 [PATCH v2] net: tls: fix possible race condition between do_tls_getsockopt_conf() and do_tls_setsockopt_conf() Hangyu Hua
  2023-03-02  4:40 ` patchwork-bot+netdevbpf
@ 2023-03-26 14:12 ` Guenter Roeck
  2023-03-27  9:05   ` Hangyu Hua
  1 sibling, 1 reply; 5+ messages in thread
From: Guenter Roeck @ 2023-03-26 14:12 UTC (permalink / raw)
  To: Hangyu Hua
  Cc: borisp, john.fastabend, kuba, davem, edumazet, pabeni,
	davejwatson, aviadye, ilyal, fw, sd, netdev, linux-kernel

Hi,

On Tue, Feb 28, 2023 at 10:33:44AM +0800, Hangyu Hua wrote:
> ctx->crypto_send.info is not protected by lock_sock in
> do_tls_getsockopt_conf(). A race condition between do_tls_getsockopt_conf()
> and do_tls_setsockopt_conf() can cause a NULL point dereference or
> use-after-free read when memcpy.
> 
> Please check the following link for pre-information:
>  https://lore.kernel.org/all/Y/ht6gQL+u6fj3dG@hog/
> 
> Fixes: 3c4d7559159b ("tls: kernel TLS support")
> Signed-off-by: Hangyu Hua <hbh25y@gmail.com>

This patch has been applied to v6.1.y. Should it be applied to older kernel
branches as well ? I know it doesn't apply cleanly, but the conflicts
should be easy to resolve. I'll be happy to send backports to stable@ if
needed.

Thanks,
Guenter

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] net: tls: fix possible race condition between do_tls_getsockopt_conf() and do_tls_setsockopt_conf()
  2023-03-26 14:12 ` Guenter Roeck
@ 2023-03-27  9:05   ` Hangyu Hua
  2023-03-27 11:07     ` Guenter Roeck
  0 siblings, 1 reply; 5+ messages in thread
From: Hangyu Hua @ 2023-03-27  9:05 UTC (permalink / raw)
  To: Guenter Roeck
  Cc: borisp, john.fastabend, kuba, davem, edumazet, pabeni,
	davejwatson, aviadye, ilyal, fw, sd, netdev, linux-kernel

On 26/3/2023 22:12, Guenter Roeck wrote:
> Hi,
> 
> On Tue, Feb 28, 2023 at 10:33:44AM +0800, Hangyu Hua wrote:
>> ctx->crypto_send.info is not protected by lock_sock in
>> do_tls_getsockopt_conf(). A race condition between do_tls_getsockopt_conf()
>> and do_tls_setsockopt_conf() can cause a NULL point dereference or
>> use-after-free read when memcpy.
>>
>> Please check the following link for pre-information:
>>   https://lore.kernel.org/all/Y/ht6gQL+u6fj3dG@hog/
>>
>> Fixes: 3c4d7559159b ("tls: kernel TLS support")
>> Signed-off-by: Hangyu Hua <hbh25y@gmail.com>
> 
> This patch has been applied to v6.1.y. Should it be applied to older kernel
> branches as well ? I know it doesn't apply cleanly, but the conflicts
> should be easy to resolve. I'll be happy to send backports to stable@ if
> needed.
> 
> Thanks,
> Guenter

Look like Meena Shanmugam is doing this. Please check this:

https://lore.kernel.org/all/20230323005440.518172-2-meenashanmugam@google.com/

Thanks for your attention.

Thanks,
Hangyu

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] net: tls: fix possible race condition between do_tls_getsockopt_conf() and do_tls_setsockopt_conf()
  2023-03-27  9:05   ` Hangyu Hua
@ 2023-03-27 11:07     ` Guenter Roeck
  0 siblings, 0 replies; 5+ messages in thread
From: Guenter Roeck @ 2023-03-27 11:07 UTC (permalink / raw)
  To: Hangyu Hua
  Cc: borisp, john.fastabend, kuba, davem, edumazet, pabeni,
	davejwatson, aviadye, ilyal, fw, sd, netdev, linux-kernel

On Mon, Mar 27, 2023 at 05:05:37PM +0800, Hangyu Hua wrote:
> On 26/3/2023 22:12, Guenter Roeck wrote:
> > Hi,
> > 
> > On Tue, Feb 28, 2023 at 10:33:44AM +0800, Hangyu Hua wrote:
> > > ctx->crypto_send.info is not protected by lock_sock in
> > > do_tls_getsockopt_conf(). A race condition between do_tls_getsockopt_conf()
> > > and do_tls_setsockopt_conf() can cause a NULL point dereference or
> > > use-after-free read when memcpy.
> > > 
> > > Please check the following link for pre-information:
> > >   https://lore.kernel.org/all/Y/ht6gQL+u6fj3dG@hog/
> > > 
> > > Fixes: 3c4d7559159b ("tls: kernel TLS support")
> > > Signed-off-by: Hangyu Hua <hbh25y@gmail.com>
> > 
> > This patch has been applied to v6.1.y. Should it be applied to older kernel
> > branches as well ? I know it doesn't apply cleanly, but the conflicts
> > should be easy to resolve. I'll be happy to send backports to stable@ if
> > needed.
> > 
> > Thanks,
> > Guenter
> 
> Look like Meena Shanmugam is doing this. Please check this:
> 
> https://lore.kernel.org/all/20230323005440.518172-2-meenashanmugam@google.com/
> 

Excellent. Thanks!

Guenter

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-03-27 11:08 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-28  2:33 [PATCH v2] net: tls: fix possible race condition between do_tls_getsockopt_conf() and do_tls_setsockopt_conf() Hangyu Hua
2023-03-02  4:40 ` patchwork-bot+netdevbpf
2023-03-26 14:12 ` Guenter Roeck
2023-03-27  9:05   ` Hangyu Hua
2023-03-27 11:07     ` Guenter Roeck

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).