tls: don't leave keys in kernel memory
Subsystems (3)
Sabrina Dubroca
maintainercover 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
Sabrina Dubroca
maintainerPATCH 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().
--- net/tls/tls_sw.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
net/tls/tls_sw.c +1 -4
@@ -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
Sabrina Dubroca
maintainerPATCH 2/3 tls: clear key material from kernel memory when do_tls_setsockopt_conf fails
In reply to Sabrina Dubroca--- net/tls/tls_main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
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 )); out:return rc; }
Signature · 1 line
2.18.0
Sabrina Dubroca
maintainerPATCH 3/3 tls: zero the crypto information from tls_context before freeing
In reply to Sabrina DubrocaThis contains key material in crypto_send_aes_gcm_128 and crypto_recv_aes_gcm_128.
--- include/net/tls.h | 1 + net/tls/tls_main.c | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-)
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 ];struct list_head list;struct net_device *netdev;
net/tls/tls_main.c +12 -2
@@ -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
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 fromtls_contextbefore freeingThis contains key material in
crypto_send_aes_gcm_128andcrypto_recv_aes_gcm_128.Fixes 3c4d7559159b ("tls: kernel TLS support")Signed-off-by Sabrina Dubroca <[email protected]>--- 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..2010d23112f9100644
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
-0struct list_head list;struct net_device *netdev;net/tls/tls_main.c index renamed from net/tls/tls_main.c +0
-0
0d432d025471..d3a57c0b2182100644net/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)#elsenet/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, longtimeout) * 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
Re: [PATCH net 2/3] tls: clear key material from kernel memory when do_tls_setsockopt_conf fails
In reply to Sabrina DubrocaHi Sabrina,
Quoted 11 lines · Sabrina Dubroca
Fixes 3c4d7559159b ("tls: kernel TLS support")Signed-off-by Sabrina Dubroca <[email protected]>--- net/tls/tls_main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)net/tls/tls_main.c +0
-0
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.
Sabrina Dubroca
maintainerRe: [PATCH net 3/3] tls: zero the crypto information from tls_context before freeing
In reply to Vakul GargQuoted 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 fromtls_contextbefore freeingThis contains key material in
crypto_send_aes_gcm_128andcrypto_recv_aes_gcm_128.Fixes 3c4d7559159b ("tls: kernel TLS support")Signed-off-by Sabrina Dubroca <[email protected]>--- 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..2010d23112f9100644include/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_recvseparately using twomemzero_explicitcalls?
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
Sabrina Dubroca
maintainerRe: [PATCH net 2/3] tls: clear key material from kernel memory when do_tls_setsockopt_conf fails
In reply to Boris PismennyQuoted 9 lines · Boris Pismenny
Hi Sabrina,
Quoted 6 lines · Sabrina Dubroca
Fixes 3c4d7559159b ("tls: kernel TLS support")Signed-off-by Sabrina Dubroca <[email protected]>--- 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@@ -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