* [PATCH net 0/3] tls: don't leave keys in kernel memory
@ 2018-09-05 13:21 Sabrina Dubroca
2018-09-05 13:21 ` [PATCH net 1/3] tls: don't copy the key out of tls12_crypto_info_aes_gcm_128 Sabrina Dubroca
` (2 more replies)
0 siblings, 3 replies; 8+ messages in thread
From: Sabrina Dubroca @ 2018-09-05 13:21 UTC (permalink / raw)
To: netdev
Cc: Sabrina Dubroca, Aviad Yehezkel, Boris Pismenny, Dave Watson,
Ilya Lesokhin
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(-)
--
2.18.0
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH net 1/3] tls: don't copy the key out of tls12_crypto_info_aes_gcm_128
2018-09-05 13:21 [PATCH net 0/3] tls: don't leave keys in kernel memory Sabrina Dubroca
@ 2018-09-05 13:21 ` Sabrina Dubroca
2018-09-05 13:21 ` [PATCH net 2/3] tls: clear key material from kernel memory when do_tls_setsockopt_conf fails Sabrina Dubroca
2018-09-05 13:21 ` [PATCH net 3/3] tls: zero the crypto information from tls_context before freeing Sabrina Dubroca
2 siblings, 0 replies; 8+ messages in thread
From: Sabrina Dubroca @ 2018-09-05 13:21 UTC (permalink / raw)
To: netdev
Cc: Sabrina Dubroca, Boris Pismenny, Ilya Lesokhin, Aviad Yehezkel,
Dave Watson
There's no need to copy the key to an on-stack buffer before calling
crypto_aead_setkey().
Fixes: 3c4d7559159b ("tls: kernel TLS support")
Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
---
net/tls/tls_sw.c | 5 +----
1 file changed, 1 insertion(+), 4 deletions(-)
diff --git a/net/tls/tls_sw.c b/net/tls/tls_sw.c
index 52fbe727d7c1..268053b04563 100644
--- a/net/tls/tls_sw.c
+++ b/net/tls/tls_sw.c
@@ -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;
--
2.18.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH net 2/3] tls: clear key material from kernel memory when do_tls_setsockopt_conf fails
2018-09-05 13:21 [PATCH net 0/3] tls: don't leave keys in kernel memory Sabrina Dubroca
2018-09-05 13:21 ` [PATCH net 1/3] tls: don't copy the key out of tls12_crypto_info_aes_gcm_128 Sabrina Dubroca
@ 2018-09-05 13:21 ` Sabrina Dubroca
2018-09-05 13:53 ` Boris Pismenny
2018-09-05 13:21 ` [PATCH net 3/3] tls: zero the crypto information from tls_context before freeing Sabrina Dubroca
2 siblings, 1 reply; 8+ messages in thread
From: Sabrina Dubroca @ 2018-09-05 13:21 UTC (permalink / raw)
To: netdev
Cc: Sabrina Dubroca, Boris Pismenny, Ilya Lesokhin, Aviad Yehezkel,
Dave Watson
Fixes: 3c4d7559159b ("tls: kernel TLS support")
Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
---
net/tls/tls_main.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/net/tls/tls_main.c b/net/tls/tls_main.c
index 180b6640e531..0d432d025471 100644
--- a/net/tls/tls_main.c
+++ b/net/tls/tls_main.c
@@ -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;
}
--
2.18.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH net 3/3] tls: zero the crypto information from tls_context before freeing
2018-09-05 13:21 [PATCH net 0/3] tls: don't leave keys in kernel memory Sabrina Dubroca
2018-09-05 13:21 ` [PATCH net 1/3] tls: don't copy the key out of tls12_crypto_info_aes_gcm_128 Sabrina Dubroca
2018-09-05 13:21 ` [PATCH net 2/3] tls: clear key material from kernel memory when do_tls_setsockopt_conf fails Sabrina Dubroca
@ 2018-09-05 13:21 ` Sabrina Dubroca
2018-09-05 13:48 ` Vakul Garg
2 siblings, 1 reply; 8+ messages in thread
From: Sabrina Dubroca @ 2018-09-05 13:21 UTC (permalink / raw)
To: netdev
Cc: Sabrina Dubroca, Boris Pismenny, Ilya Lesokhin, Aviad Yehezkel,
Dave Watson
This contains key material in crypto_send_aes_gcm_128 and
crypto_recv_aes_gcm_128.
Fixes: 3c4d7559159b ("tls: kernel TLS support")
Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
---
include/net/tls.h | 1 +
net/tls/tls_main.c | 14 ++++++++++++--
2 files changed, 13 insertions(+), 2 deletions(-)
diff --git a/include/net/tls.h b/include/net/tls.h
index d5c683e8bb22..2010d23112f9 100644
--- a/include/net/tls.h
+++ b/include/net/tls.h
@@ -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;
diff --git a/net/tls/tls_main.c b/net/tls/tls_main.c
index 0d432d025471..d3a57c0b2182 100644
--- a/net/tls/tls_main.c
+++ b/net/tls/tls_main.c
@@ -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,
--
2.18.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* RE: [PATCH net 3/3] tls: zero the crypto information from tls_context before freeing
2018-09-05 13:21 ` [PATCH net 3/3] tls: zero the crypto information from tls_context before freeing Sabrina Dubroca
@ 2018-09-05 13:48 ` Vakul Garg
2018-09-06 7:42 ` Sabrina Dubroca
0 siblings, 1 reply; 8+ messages in thread
From: Vakul Garg @ 2018-09-05 13:48 UTC (permalink / raw)
To: Sabrina Dubroca, netdev
Cc: Boris Pismenny, Ilya Lesokhin, Aviad Yehezkel, Dave Watson
> -----Original Message-----
> From: netdev-owner@vger.kernel.org <netdev-owner@vger.kernel.org> On
> Behalf Of Sabrina Dubroca
> Sent: Wednesday, September 5, 2018 6:52 PM
> To: netdev@vger.kernel.org
> Cc: Sabrina Dubroca <sd@queasysnail.net>; Boris Pismenny
> <borisp@mellanox.com>; Ilya Lesokhin <ilyal@mellanox.com>; Aviad
> Yehezkel <aviadye@mellanox.com>; Dave Watson <davejwatson@fb.com>
> 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")
> Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
> ---
> include/net/tls.h | 1 +
> net/tls/tls_main.c | 14 ++++++++++++--
> 2 files changed, 13 insertions(+), 2 deletions(-)
>
> diff --git a/include/net/tls.h b/include/net/tls.h index
> d5c683e8bb22..2010d23112f9 100644
> --- a/include/net/tls.h
> +++ b/include/net/tls.h
> @@ -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?
>
> struct list_head list;
> struct net_device *netdev;
> diff --git a/net/tls/tls_main.c b/net/tls/tls_main.c index
> 0d432d025471..d3a57c0b2182 100644
> --- a/net/tls/tls_main.c
> +++ b/net/tls/tls_main.c
> @@ -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,
> --
> 2.18.0
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH net 2/3] tls: clear key material from kernel memory when do_tls_setsockopt_conf fails
2018-09-05 13:21 ` [PATCH net 2/3] tls: clear key material from kernel memory when do_tls_setsockopt_conf fails Sabrina Dubroca
@ 2018-09-05 13:53 ` Boris Pismenny
2018-09-06 7:49 ` Sabrina Dubroca
0 siblings, 1 reply; 8+ messages in thread
From: Boris Pismenny @ 2018-09-05 13:53 UTC (permalink / raw)
To: Sabrina Dubroca, netdev; +Cc: Ilya Lesokhin, Aviad Yehezkel, Dave Watson
Hi Sabrina,
On 9/5/2018 4:21 PM, Sabrina Dubroca wrote:
> Fixes: 3c4d7559159b ("tls: kernel TLS support")
> Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
> ---
> net/tls/tls_main.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/net/tls/tls_main.c b/net/tls/tls_main.c
> index 180b6640e531..0d432d025471 100644
> --- a/net/tls/tls_main.c
> +++ b/net/tls/tls_main.c
> @@ -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.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH net 3/3] tls: zero the crypto information from tls_context before freeing
2018-09-05 13:48 ` Vakul Garg
@ 2018-09-06 7:42 ` Sabrina Dubroca
0 siblings, 0 replies; 8+ messages in thread
From: Sabrina Dubroca @ 2018-09-06 7:42 UTC (permalink / raw)
To: Vakul Garg
Cc: netdev, Boris Pismenny, Ilya Lesokhin, Aviad Yehezkel, Dave Watson
2018-09-05, 13:48:48 +0000, Vakul Garg wrote:
>
>
> > -----Original Message-----
> > From: netdev-owner@vger.kernel.org <netdev-owner@vger.kernel.org> On
> > Behalf Of Sabrina Dubroca
> > Sent: Wednesday, September 5, 2018 6:52 PM
> > To: netdev@vger.kernel.org
> > Cc: Sabrina Dubroca <sd@queasysnail.net>; Boris Pismenny
> > <borisp@mellanox.com>; Ilya Lesokhin <ilyal@mellanox.com>; Aviad
> > Yehezkel <aviadye@mellanox.com>; Dave Watson <davejwatson@fb.com>
> > 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")
> > Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
> > ---
> > include/net/tls.h | 1 +
> > net/tls/tls_main.c | 14 ++++++++++++--
> > 2 files changed, 13 insertions(+), 2 deletions(-)
> >
> > diff --git a/include/net/tls.h b/include/net/tls.h index
> > d5c683e8bb22..2010d23112f9 100644
> > --- a/include/net/tls.h
> > +++ b/include/net/tls.h
> > @@ -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].
> 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;
[...]
}
--
Sabrina
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH net 2/3] tls: clear key material from kernel memory when do_tls_setsockopt_conf fails
2018-09-05 13:53 ` Boris Pismenny
@ 2018-09-06 7:49 ` Sabrina Dubroca
0 siblings, 0 replies; 8+ messages in thread
From: Sabrina Dubroca @ 2018-09-06 7:49 UTC (permalink / raw)
To: Boris Pismenny; +Cc: netdev, Ilya Lesokhin, Aviad Yehezkel, Dave Watson
2018-09-05, 16:53:54 +0300, Boris Pismenny wrote:
> Hi Sabrina,
>
> On 9/5/2018 4:21 PM, Sabrina Dubroca wrote:
> > Fixes: 3c4d7559159b ("tls: kernel TLS support")
> > Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
> > ---
> > net/tls/tls_main.c | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/net/tls/tls_main.c b/net/tls/tls_main.c
> > index 180b6640e531..0d432d025471 100644
> > --- a/net/tls/tls_main.c
> > +++ b/net/tls/tls_main.c
> > @@ -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?
--
Sabrina
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2018-09-06 12:23 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-09-05 13:21 [PATCH net 0/3] tls: don't leave keys in kernel memory Sabrina Dubroca
2018-09-05 13:21 ` [PATCH net 1/3] tls: don't copy the key out of tls12_crypto_info_aes_gcm_128 Sabrina Dubroca
2018-09-05 13:21 ` [PATCH net 2/3] tls: clear key material from kernel memory when do_tls_setsockopt_conf fails Sabrina Dubroca
2018-09-05 13:53 ` Boris Pismenny
2018-09-06 7:49 ` Sabrina Dubroca
2018-09-05 13:21 ` [PATCH net 3/3] tls: zero the crypto information from tls_context before freeing Sabrina Dubroca
2018-09-05 13:48 ` Vakul Garg
2018-09-06 7:42 ` Sabrina Dubroca
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.