netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] net: tls: fix possible info leak in tls_set_device_offload()
@ 2023-02-23  9:05 Hangyu Hua
  2023-02-23  9:25 ` Yunsheng Lin
  2023-02-23 11:15 ` Sabrina Dubroca
  0 siblings, 2 replies; 7+ messages in thread
From: Hangyu Hua @ 2023-02-23  9:05 UTC (permalink / raw)
  To: borisp, john.fastabend, kuba, davem, edumazet, pabeni
  Cc: netdev, linux-kernel, Hangyu Hua

After tls_set_device_offload() fails, we enter tls_set_sw_offload(). But
tls_set_sw_offload can't set cctx->iv and cctx->rec_seq to NULL if it fails
before kmalloc cctx->iv. This may cause info leak when we call
do_tls_getsockopt_conf().

Fixes: e8f69799810c ("net/tls: Add generic NIC offload infrastructure")
Signed-off-by: Hangyu Hua <hbh25y@gmail.com>
---
 net/tls/tls_device.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/net/tls/tls_device.c b/net/tls/tls_device.c
index 6c593788dc25..a63f6f727f58 100644
--- a/net/tls/tls_device.c
+++ b/net/tls/tls_device.c
@@ -1241,8 +1241,10 @@ int tls_set_device_offload(struct sock *sk, struct tls_context *ctx)
 	kfree(start_marker_record);
 free_rec_seq:
 	kfree(ctx->tx.rec_seq);
+	ctx->tx.rec_seq = NULL;
 free_iv:
 	kfree(ctx->tx.iv);
+	ctx->tx.iv = NULL;
 release_netdev:
 	dev_put(netdev);
 	return rc;
-- 
2.34.1


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

* Re: [PATCH] net: tls: fix possible info leak in tls_set_device_offload()
  2023-02-23  9:05 [PATCH] net: tls: fix possible info leak in tls_set_device_offload() Hangyu Hua
@ 2023-02-23  9:25 ` Yunsheng Lin
  2023-02-23 11:15 ` Sabrina Dubroca
  1 sibling, 0 replies; 7+ messages in thread
From: Yunsheng Lin @ 2023-02-23  9:25 UTC (permalink / raw)
  To: Hangyu Hua, borisp, john.fastabend, kuba, davem, edumazet, pabeni
  Cc: netdev, linux-kernel

On 2023/2/23 17:05, Hangyu Hua wrote:
> After tls_set_device_offload() fails, we enter tls_set_sw_offload(). But
> tls_set_sw_offload can't set cctx->iv and cctx->rec_seq to NULL if it fails
> before kmalloc cctx->iv. This may cause info leak when we call
> do_tls_getsockopt_conf().

Should we use kfree_sensitive() here if info leaking is what we want to
avoid?

> 
> Fixes: e8f69799810c ("net/tls: Add generic NIC offload infrastructure")
> Signed-off-by: Hangyu Hua <hbh25y@gmail.com>
> ---
>  net/tls/tls_device.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/net/tls/tls_device.c b/net/tls/tls_device.c
> index 6c593788dc25..a63f6f727f58 100644
> --- a/net/tls/tls_device.c
> +++ b/net/tls/tls_device.c
> @@ -1241,8 +1241,10 @@ int tls_set_device_offload(struct sock *sk, struct tls_context *ctx)
>  	kfree(start_marker_record);
>  free_rec_seq:
>  	kfree(ctx->tx.rec_seq);
> +	ctx->tx.rec_seq = NULL;
>  free_iv:
>  	kfree(ctx->tx.iv);
> +	ctx->tx.iv = NULL;
>  release_netdev:
>  	dev_put(netdev);
>  	return rc;
> 

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

* Re: [PATCH] net: tls: fix possible info leak in tls_set_device_offload()
  2023-02-23  9:05 [PATCH] net: tls: fix possible info leak in tls_set_device_offload() Hangyu Hua
  2023-02-23  9:25 ` Yunsheng Lin
@ 2023-02-23 11:15 ` Sabrina Dubroca
  2023-02-24  3:07   ` Hangyu Hua
  1 sibling, 1 reply; 7+ messages in thread
From: Sabrina Dubroca @ 2023-02-23 11:15 UTC (permalink / raw)
  To: Hangyu Hua
  Cc: borisp, john.fastabend, kuba, davem, edumazet, pabeni, netdev,
	linux-kernel

2023-02-23, 17:05:08 +0800, Hangyu Hua wrote:
> After tls_set_device_offload() fails, we enter tls_set_sw_offload(). But
> tls_set_sw_offload can't set cctx->iv and cctx->rec_seq to NULL if it fails
> before kmalloc cctx->iv. This may cause info leak when we call
> do_tls_getsockopt_conf().

Is there really an issue here?

If both tls_set_device_offload and tls_set_sw_offload fail,
do_tls_setsockopt_conf will clear crypto_{send,recv} from the context.
Then the TLS_CRYPTO_INFO_READY in do_tls_getsockopt_conf will fail, so
we won't try to access iv or rec_seq.

-- 
Sabrina


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

* Re: [PATCH] net: tls: fix possible info leak in tls_set_device_offload()
  2023-02-23 11:15 ` Sabrina Dubroca
@ 2023-02-24  3:07   ` Hangyu Hua
  2023-02-24  3:33     ` Hangyu Hua
  0 siblings, 1 reply; 7+ messages in thread
From: Hangyu Hua @ 2023-02-24  3:07 UTC (permalink / raw)
  To: Sabrina Dubroca
  Cc: borisp, john.fastabend, kuba, davem, edumazet, pabeni, netdev,
	linux-kernel

On 23/2/2023 19:15, Sabrina Dubroca wrote:
> 2023-02-23, 17:05:08 +0800, Hangyu Hua wrote:
>> After tls_set_device_offload() fails, we enter tls_set_sw_offload(). But
>> tls_set_sw_offload can't set cctx->iv and cctx->rec_seq to NULL if it fails
>> before kmalloc cctx->iv. This may cause info leak when we call
>> do_tls_getsockopt_conf().
> 
> Is there really an issue here?
> 
> If both tls_set_device_offload and tls_set_sw_offload fail,
> do_tls_setsockopt_conf will clear crypto_{send,recv} from the context.
> Then the TLS_CRYPTO_INFO_READY in do_tls_getsockopt_conf will fail, so
> we won't try to access iv or rec_seq.
> 

My bad. I forget memzero_explicit. Then this is harmless. But I still 
think it is better to set them to NULL like tls_set_sw_offload's error 
path because we don't know there are another way to do this(I will 
change the commit log). What do you think?

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

* Re: [PATCH] net: tls: fix possible info leak in tls_set_device_offload()
  2023-02-24  3:07   ` Hangyu Hua
@ 2023-02-24  3:33     ` Hangyu Hua
  2023-02-24  7:57       ` Sabrina Dubroca
  0 siblings, 1 reply; 7+ messages in thread
From: Hangyu Hua @ 2023-02-24  3:33 UTC (permalink / raw)
  To: Sabrina Dubroca
  Cc: borisp, john.fastabend, kuba, davem, edumazet, pabeni, netdev,
	linux-kernel

On 24/2/2023 11:07, Hangyu Hua wrote:
> On 23/2/2023 19:15, Sabrina Dubroca wrote:
>> 2023-02-23, 17:05:08 +0800, Hangyu Hua wrote:
>>> After tls_set_device_offload() fails, we enter tls_set_sw_offload(). But
>>> tls_set_sw_offload can't set cctx->iv and cctx->rec_seq to NULL if it 
>>> fails
>>> before kmalloc cctx->iv. This may cause info leak when we call
>>> do_tls_getsockopt_conf().
>>
>> Is there really an issue here?
>>
>> If both tls_set_device_offload and tls_set_sw_offload fail,
>> do_tls_setsockopt_conf will clear crypto_{send,recv} from the context.
>> Then the TLS_CRYPTO_INFO_READY in do_tls_getsockopt_conf will fail, so
>> we won't try to access iv or rec_seq.
>>
> 
> My bad. I forget memzero_explicit. Then this is harmless. But I still 
> think it is better to set them to NULL like tls_set_sw_offload's error 
> path because we don't know there are another way to do this(I will 
> change the commit log). What do you think?

Like a rare case, there is a race condition between
do_tls_getsockopt_conf and do_tls_setsockopt_conf while the previous
condition is met. TLS_CRYPTO_INFO_READY(crypto_info) is not
protected by lock_sock in do_tls_getsockopt_conf. It's just too
difficult to satisfy both conditions at the same time.

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

* Re: [PATCH] net: tls: fix possible info leak in tls_set_device_offload()
  2023-02-24  3:33     ` Hangyu Hua
@ 2023-02-24  7:57       ` Sabrina Dubroca
  2023-02-24  9:35         ` Hangyu Hua
  0 siblings, 1 reply; 7+ messages in thread
From: Sabrina Dubroca @ 2023-02-24  7:57 UTC (permalink / raw)
  To: Hangyu Hua
  Cc: borisp, john.fastabend, kuba, davem, edumazet, pabeni, netdev,
	linux-kernel

2023-02-24, 11:33:29 +0800, Hangyu Hua wrote:
> On 24/2/2023 11:07, Hangyu Hua wrote:
> > On 23/2/2023 19:15, Sabrina Dubroca wrote:
> > > 2023-02-23, 17:05:08 +0800, Hangyu Hua wrote:
> > > > After tls_set_device_offload() fails, we enter tls_set_sw_offload(). But
> > > > tls_set_sw_offload can't set cctx->iv and cctx->rec_seq to NULL
> > > > if it fails
> > > > before kmalloc cctx->iv. This may cause info leak when we call
> > > > do_tls_getsockopt_conf().
> > > 
> > > Is there really an issue here?
> > > 
> > > If both tls_set_device_offload and tls_set_sw_offload fail,
> > > do_tls_setsockopt_conf will clear crypto_{send,recv} from the context.
> > > Then the TLS_CRYPTO_INFO_READY in do_tls_getsockopt_conf will fail, so
> > > we won't try to access iv or rec_seq.
> > > 
> > 
> > My bad. I forget memzero_explicit. Then this is harmless. But I still
> > think it is better to set them to NULL like tls_set_sw_offload's error
> > path because we don't know there are another way to do this(I will
> > change the commit log). What do you think?

Yes, I guess for consistency between functions it would be ok.

> Like a rare case, there is a race condition between
> do_tls_getsockopt_conf and do_tls_setsockopt_conf while the previous
> condition is met. TLS_CRYPTO_INFO_READY(crypto_info) is not
> protected by lock_sock in do_tls_getsockopt_conf. It's just too
> difficult to satisfy both conditions at the same time.

Ugh, thanks for noticing this. We should move the lock_sock in
getsockopt before TLS_CRYPTO_INFO_READY. Do you want to write that
patch?

Thanks.

-- 
Sabrina


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

* Re: [PATCH] net: tls: fix possible info leak in tls_set_device_offload()
  2023-02-24  7:57       ` Sabrina Dubroca
@ 2023-02-24  9:35         ` Hangyu Hua
  0 siblings, 0 replies; 7+ messages in thread
From: Hangyu Hua @ 2023-02-24  9:35 UTC (permalink / raw)
  To: Sabrina Dubroca
  Cc: borisp, john.fastabend, kuba, davem, edumazet, pabeni, netdev,
	linux-kernel

On 24/2/2023 15:57, Sabrina Dubroca wrote:
> 2023-02-24, 11:33:29 +0800, Hangyu Hua wrote:
>> On 24/2/2023 11:07, Hangyu Hua wrote:
>>> On 23/2/2023 19:15, Sabrina Dubroca wrote:
>>>> 2023-02-23, 17:05:08 +0800, Hangyu Hua wrote:
>>>>> After tls_set_device_offload() fails, we enter tls_set_sw_offload(). But
>>>>> tls_set_sw_offload can't set cctx->iv and cctx->rec_seq to NULL
>>>>> if it fails
>>>>> before kmalloc cctx->iv. This may cause info leak when we call
>>>>> do_tls_getsockopt_conf().
>>>>
>>>> Is there really an issue here?
>>>>
>>>> If both tls_set_device_offload and tls_set_sw_offload fail,
>>>> do_tls_setsockopt_conf will clear crypto_{send,recv} from the context.
>>>> Then the TLS_CRYPTO_INFO_READY in do_tls_getsockopt_conf will fail, so
>>>> we won't try to access iv or rec_seq.
>>>>
>>>
>>> My bad. I forget memzero_explicit. Then this is harmless. But I still
>>> think it is better to set them to NULL like tls_set_sw_offload's error
>>> path because we don't know there are another way to do this(I will
>>> change the commit log). What do you think?
> 
> Yes, I guess for consistency between functions it would be ok.
> 
>> Like a rare case, there is a race condition between
>> do_tls_getsockopt_conf and do_tls_setsockopt_conf while the previous
>> condition is met. TLS_CRYPTO_INFO_READY(crypto_info) is not
>> protected by lock_sock in do_tls_getsockopt_conf. It's just too
>> difficult to satisfy both conditions at the same time.
> 
> Ugh, thanks for noticing this. We should move the lock_sock in
> getsockopt before TLS_CRYPTO_INFO_READY. Do you want to write that
> patch?
> 
> Thanks.
> 

I see. I will make a new patch to fix the race and send v2 of this.

Thanks,
Hangyu

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

end of thread, other threads:[~2023-02-24  9:35 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-02-23  9:05 [PATCH] net: tls: fix possible info leak in tls_set_device_offload() Hangyu Hua
2023-02-23  9:25 ` Yunsheng Lin
2023-02-23 11:15 ` Sabrina Dubroca
2023-02-24  3:07   ` Hangyu Hua
2023-02-24  3:33     ` Hangyu Hua
2023-02-24  7:57       ` Sabrina Dubroca
2023-02-24  9:35         ` Hangyu Hua

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).