Skip to contentSkip to conversation

net: tls: fix possible info leak in tls_set_device_offload()

PATCH Superseded Hangyu Hua 3 participants 7 messages Feb 23, 2023 – Feb 24, 2023

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Hangyu Hua

author
#0 lore raw

PATCH 1/1 net: tls: fix possible info leak in tls_set_device_offload()

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")
---
 net/tls/tls_device.c | 2 ++
 1 file changed, 2 insertions(+)
net/tls/tls_device.c +2 -0
index 6c593788dc25..a63f6f727f58 100644
@@ -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;
Signature · 1 line
2.34.1
#1 lore raw
Quoted 4 lines · 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().

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

Quoted 23 lines
Fixes e8f69799810c ("net/tls: Add generic NIC offload infrastructure")
---
 net/tls/tls_device.c | 2 ++
 1 file changed, 2 insertions(+)
net/tls/tls_device.c +2 -0
index 6c593788dc25..a63f6f727f58 100644
@@ -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;
maintainer
#2 lore raw
In reply to Hangyu Hua
Quoted 4 lines · 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().

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.

Signature · 1 line
Sabrina
author
#3 lore raw
Quoted 6 lines · Sabrina Dubroca
Quoted 4 lines · 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().

Quoted 7 lines

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?

author
#4 lore raw
Quoted 9 lines · Hangyu Hua
Quoted 8 lines · Sabrina Dubroca
Quoted 4 lines · 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().

Is there really an issue here?

Quoted 7 lines
Quoted 5 lines

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.

maintainer
#5 lore raw
Quoted 17 lines · Hangyu Hua
Quoted 16 lines · Hangyu Hua
Quoted 14 lines · Sabrina Dubroca
Quoted 5 lines · 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().

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.

Quoted 4 lines
Quoted 4 lines

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.

Quoted 5 lines

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.

Signature · 1 line
Sabrina
author
#6 lore raw
Quoted 31 lines · Sabrina Dubroca
Quoted 17 lines · Hangyu Hua
Quoted 16 lines · Hangyu Hua
Quoted 14 lines · Sabrina Dubroca
Quoted 5 lines · 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().

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.

Quoted 4 lines
Quoted 4 lines

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.

Quoted 5 lines

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.

Quoted 6 lines

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