Skip to contentSkip to conversation

tls: don't leave keys in kernel memory

PATCH Superseded Sabrina Dubroca 3 participants 8 messages Sep 5, 2018 – Sep 6, 2018

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Sabrina Dubroca

author maintainer
#0 lore raw

cover tls: don't leave keys in kernel memory

There are a few places where the RX/TX key for a TLS socket is copied to kernel memory. This series clears those memory areas when they're no longer needed.

Sabrina Dubroca (3):
tls: don't copy the key out of tls12_crypto_info_aes_gcm_128
tls: clear key material from kernel memory when do_tls_setsockopt_conf
fails
tls: zero the crypto information from tls_context before freeing

 include/net/tls.h  |  1 +
 net/tls/tls_main.c | 16 +++++++++++++---
 net/tls/tls_sw.c   |  5 +----
 3 files changed, 15 insertions(+), 7 deletions(-)
Signature · 1 line
2.18.0
author maintainer
#1 lore raw

PATCH 1/3 tls: don't copy the key out of tls12_crypto_info_aes_gcm_128

There's no need to copy the key to an on-stack buffer before calling crypto_aead_setkey().

Fixes 3c4d7559159b ("tls: kernel TLS support")
---
 net/tls/tls_sw.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
net/tls/tls_sw.c +1 -4
index 52fbe727d7c1..268053b04563 100644
@@ -1130,7 +1130,6 @@ void tls_sw_free_resources_rx(struct sock *sk)
 
 int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx)
 {
-	char keyval[TLS_CIPHER_AES_GCM_128_KEY_SIZE];
 	struct tls_crypto_info *crypto_info;
 	struct tls12_crypto_info_aes_gcm_128 *gcm_128_info;
 	struct tls_sw_context_tx *sw_ctx_tx = NULL;
@@ -1259,9 +1258,7 @@ int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx)
 
 	ctx->push_pending_record = tls_sw_push_pending_record;
 
-	memcpy(keyval, gcm_128_info->key, TLS_CIPHER_AES_GCM_128_KEY_SIZE);
-
-	rc = crypto_aead_setkey(*aead, keyval,
+	rc = crypto_aead_setkey(*aead, gcm_128_info->key,
 				TLS_CIPHER_AES_GCM_128_KEY_SIZE);
 	if (rc)
 		goto free_aead;
Signature · 1 line
2.18.0
author maintainer
#2 lore raw

PATCH 2/3 tls: clear key material from kernel memory when do_tls_setsockopt_conf fails

In reply to Sabrina Dubroca
Fixes 3c4d7559159b ("tls: kernel TLS support")
---
 net/tls/tls_main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/tls/tls_main.c +1 -1
index 180b6640e531..0d432d025471 100644
@@ -499,7 +499,7 @@ static int do_tls_setsockopt_conf(struct sock *sk, char __user *optval,
 	goto out;
 
 err_crypto_info:
-	memset(crypto_info, 0, sizeof(*crypto_info));
+	memzero_explicit(crypto_info, sizeof(struct tls12_crypto_info_aes_gcm_128));
 out:
 	return rc;
 }
Signature · 1 line
2.18.0
#3 lore raw

Re: [PATCH net 2/3] tls: clear key material from kernel memory when do_tls_setsockopt_conf fails

Hi Sabrina,

Quoted 11 lines · Sabrina Dubroca
Fixes 3c4d7559159b ("tls: kernel TLS support")
---
  net/tls/tls_main.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
net/tls/tls_main.c +0 -0
index 180b6640e531..0d432d025471 100644

net/tls/tls_main.c static int do_tls_setsockopt_conf(struct sock *sk, char __user *optval, · line 502

Quoted 5 lines
net/tls/tls_main.c +1 -1
@@ -499,7 +499,7 @@ static int do_tls_setsockopt_conf(struct sock *sk, char __user *optval,
  	goto out;
 
  err_crypto_info:
-	memset(crypto_info, 0, sizeof(*crypto_info));
+	memzero_explicit(crypto_info, sizeof(struct tls12_crypto_info_aes_gcm_128));

Besides the key, there are other (not secret) information in tls12_crypto_info_aes_gcm_128. I'd prefer you do not delete it to enable users to obtain it (using getsockopt) in case we decide to implement a fallback to userspace in the future. Such a fallback must obtain the kernel's iv, and record sequence number.

Thanks,
Boris.

author maintainer
#4 lore raw

Re: [PATCH net 2/3] tls: clear key material from kernel memory when do_tls_setsockopt_conf fails

Quoted 9 lines · Boris Pismenny

Hi Sabrina,

Quoted 6 lines · Sabrina Dubroca
Fixes 3c4d7559159b ("tls: kernel TLS support")
---
  net/tls/tls_main.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines

net/tls/tls_main.c static int do_tls_setsockopt_conf(struct sock *sk, char __user *optval, · line 501

Quoted 9 lines
net/tls/tls_main.c +1 -1
index 180b6640e531..0d432d025471 100644
@@ -499,7 +499,7 @@ static int do_tls_setsockopt_conf(struct sock *sk, char __user *optval,
  	goto out;
  err_crypto_info:
-	memset(crypto_info, 0, sizeof(*crypto_info));
+	memzero_explicit(crypto_info, sizeof(struct tls12_crypto_info_aes_gcm_128));

Besides the key, there are other (not secret) information in tls12_crypto_info_aes_gcm_128. I'd prefer you do not delete it to enable users to obtain it (using getsockopt) in case we decide to implement a fallback to userspace in the future. Such a fallback must obtain the kernel's iv, and record sequence number.

The operation failed. There's no reason for this stale data to remain in the kernel. And you couldn't recover it with getsockopt, because you'll hit !TLS_CRYPTO_INFO_READY, since we're already resetting tls_crypto_info. Resetting tls_crypto_info on failure is necessary, otherwise your socket will be in a broken state, with TLS not setup but unable to perform a new attempt.

Userspace already has all this information anyway, since it passed it to the kernel, so why would it need to recover it from the kernel?

Signature · 1 line
Sabrina
author maintainer
#5 lore raw

PATCH 3/3 tls: zero the crypto information from tls_context before freeing

In reply to Sabrina Dubroca

This contains key material in crypto_send_aes_gcm_128 and crypto_recv_aes_gcm_128.

Fixes 3c4d7559159b ("tls: kernel TLS support")
---
 include/net/tls.h  |  1 +
 net/tls/tls_main.c | 14 ++++++++++++--
 2 files changed, 13 insertions(+), 2 deletions(-)
include/net/tls.h +1 -0
index d5c683e8bb22..2010d23112f9 100644
@@ -180,6 +180,7 @@ struct tls_context {
 		struct tls_crypto_info crypto_recv;
 		struct tls12_crypto_info_aes_gcm_128 crypto_recv_aes_gcm_128;
 	};
+	char tls_crypto_ctx_end[0];
 
 	struct list_head list;
 	struct net_device *netdev;
net/tls/tls_main.c +12 -2
index 0d432d025471..d3a57c0b2182 100644
@@ -241,6 +241,16 @@ static void tls_write_space(struct sock *sk)
 	ctx->sk_write_space(sk);
 }
 
+static void tls_ctx_free(struct tls_context *ctx)
+{
+	if (!ctx)
+		return;
+
+	memzero_explicit(&ctx->crypto_send,
+			 offsetof(struct tls_context, tls_crypto_ctx_end));
+	kfree(ctx);
+}
+
 static void tls_sk_proto_close(struct sock *sk, long timeout)
 {
 	struct tls_context *ctx = tls_get_ctx(sk);
@@ -294,7 +304,7 @@ static void tls_sk_proto_close(struct sock *sk, long timeout)
 #else
 	{
 #endif
-		kfree(ctx);
+		tls_ctx_free(ctx);
 		ctx = NULL;
 	}
 
@@ -305,7 +315,7 @@ static void tls_sk_proto_close(struct sock *sk, long timeout)
 	 * for sk->sk_prot->unhash [tls_hw_unhash]
 	 */
 	if (free_ctx)
-		kfree(ctx);
+		tls_ctx_free(ctx);
 }
 
 static int do_tls_getsockopt_tx(struct sock *sk, char __user *optval,
Signature · 1 line
2.18.0
#6 lore raw

RE: [PATCH net 3/3] tls: zero the crypto information from tls_context before freeing

Quoted 28 lines

-----Original Message-----
From: [email protected] <[email protected]> On Behalf Of Sabrina Dubroca
Sent: Wednesday, September 5, 2018 6:52 PM
To: [email protected]
Cc: Sabrina Dubroca <[email protected]>; Boris Pismenny <[email protected]>; Ilya Lesokhin <[email protected]>; Aviad Yehezkel <[email protected]>; Dave Watson <[email protected]>
Subject: [PATCH net 3/3] tls: zero the crypto information from tls_context before freeing

This contains key material in crypto_send_aes_gcm_128 and crypto_recv_aes_gcm_128.

Fixes 3c4d7559159b ("tls: kernel TLS support")
---
 include/net/tls.h  |  1 +
 net/tls/tls_main.c | 14 ++++++++++++--
 2 files changed, 13 insertions(+), 2 deletions(-)
include/net/tls.h index renamed from include/net/tls.h +0 -0

d5c683e8bb22..2010d23112f9 100644

include/net/tls.h struct tls_context { · line 184

Quoted 5 lines
include/net/tls.h +1 -0
@@ -180,6 +180,7 @@ struct tls_context {
 		struct tls_crypto_info crypto_recv;
 		struct tls12_crypto_info_aes_gcm_128
 crypto_recv_aes_gcm_128;
 	};
+	char tls_crypto_ctx_end[0];

This makes the key zeroization dependent upon the position of unions in structure. Can you zeroise the crypto_send, crypto_recv separately using two memzero_explicit calls?

Quoted 42 lines
include/net/tls.h +0 -0
 	struct list_head list;
 	struct net_device *netdev;
net/tls/tls_main.c index renamed from net/tls/tls_main.c +0 -0

0d432d025471..d3a57c0b2182 100644

net/tls/tls_main.c +9 -0
@@ -241,6 +241,16 @@ static void tls_write_space(struct sock *sk)
 	ctx->sk_write_space(sk);
 }
 
+static void tls_ctx_free(struct tls_context *ctx) {
+	if (!ctx)
+		return;
+
+	memzero_explicit(&ctx->crypto_send,
+			 offsetof(struct tls_context, tls_crypto_ctx_end));
+	kfree(ctx);
+}
+
 static void tls_sk_proto_close(struct sock *sk, long timeout)  {
 	struct tls_context *ctx = tls_get_ctx(sk); @@ -294,7 +304,7 @@

static void tls_sk_proto_close(struct sock *sk, long timeout) #else

net/tls/tls_main.c +2 -2
 	{
 #endif
-		kfree(ctx);
+		tls_ctx_free(ctx);
 		ctx = NULL;
 	}
@@ -305,7 +315,7 @@ static void tls_sk_proto_close(struct sock *sk, long
 timeout)
 	 * for sk->sk_prot->unhash [tls_hw_unhash]
 	 */
 	if (free_ctx)
-		kfree(ctx);
+		tls_ctx_free(ctx);
 }
 

static int do_tls_getsockopt_tx(struct sock *sk, char __user *optval,
--
2.18.0

author maintainer
#7 lore raw

Re: [PATCH net 3/3] tls: zero the crypto information from tls_context before freeing

Quoted 23 lines · Vakul Garg
Quoted 23 lines

-----Original Message-----
From: [email protected] <[email protected]> On Behalf Of Sabrina Dubroca
Sent: Wednesday, September 5, 2018 6:52 PM
To: [email protected]
Cc: Sabrina Dubroca <[email protected]>; Boris Pismenny <[email protected]>; Ilya Lesokhin <[email protected]>; Aviad Yehezkel <[email protected]>; Dave Watson <[email protected]>
Subject: [PATCH net 3/3] tls: zero the crypto information from tls_context before freeing

This contains key material in crypto_send_aes_gcm_128 and crypto_recv_aes_gcm_128.

Fixes 3c4d7559159b ("tls: kernel TLS support")
---
 include/net/tls.h  |  1 +
 net/tls/tls_main.c | 14 ++++++++++++--
 2 files changed, 13 insertions(+), 2 deletions(-)
Quoted 13 lines

include/net/tls.h struct tls_context { · line 184

Quoted 10 lines
include/net/tls.h index renamed from include/net/tls.h +0 -0

d5c683e8bb22..2010d23112f9 100644

include/net/tls.h +1 -0
@@ -180,6 +180,7 @@ struct tls_context {
 		struct tls_crypto_info crypto_recv;
 		struct tls12_crypto_info_aes_gcm_128
 crypto_recv_aes_gcm_128;
 	};
+	char tls_crypto_ctx_end[0];

This makes the key zeroization dependent upon the position of unions in structure.

Yes. I could add char tls_crypto_ctx_start[0].

Quoted 2 lines

Can you zeroise the crypto_send, crypto_recv separately using two memzero_explicit calls?

It's not crypto_send, it's crypto_send_aes_gcm_128. I don't know how likely it is that another crypto algorithm will ever be added, but then you'd have to switch to zeroing that thing.

I had a different version of the patch that gave a name to those unions, but then I need to change all the references everywhere and the patch becomes a bit ugly, especially for net.

struct tls_context {
	union {
		struct tls_crypto_info info;
		struct tls12_crypto_info_aes_gcm_128 aes_gcm_128;
	} crypto_send;
	union {
		struct tls_crypto_info info;
		struct tls12_crypto_info_aes_gcm_128 aes_gcm_128;
	} crypto_recv;

	[...]
}

Or even:

union tls_crypto_context {
	struct tls_crypto_info info;
	struct tls12_crypto_info_aes_gcm_128 aes_gcm_128;
};
struct tls_context {
	union tls_crypto_context crypto_send;
	union tls_crypto_context crypto_recv;

	[...]
}
Signature · 1 line
Sabrina