From: Balbir Singh <bsingharora@gmail.com>
To: Waiman Long <longman@redhat.com>,
Andrew Morton <akpm@linux-foundation.org>,
David Howells <dhowells@redhat.com>,
Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>,
James Morris <jmorris@namei.org>,
"Serge E. Hallyn" <serge@hallyn.com>
Cc: linux-mm@kvack.org, keyrings@vger.kernel.org,
linux-kernel@vger.kernel.org,
Linus Torvalds <torvalds@linux-foundation.org>,
Joe Perches <joe@perches.com>,
Matthew Wilcox <willy@infradead.org>,
David Rientjes <rientjes@google.com>
Subject: Re: [PATCH v3] mm: Add kvfree_sensitive() for freeing sensitive data objects
Date: Thu, 14 May 2020 21:00:40 +1000 [thread overview]
Message-ID: <1158ff38-c65d-379f-8ae7-6f507d9fc8dd@gmail.com> (raw)
In-Reply-To: <20200407200318.11711-1-longman@redhat.com>
On 8/4/20 6:03 am, Waiman Long wrote:
> For kvmalloc'ed data object that contains sensitive information like
> cryptographic key, we need to make sure that the buffer is always
> cleared before freeing it. Using memset() alone for buffer clearing may
> not provide certainty as the compiler may compile it away. To be sure,
> the special memzero_explicit() has to be used.
>
> This patch introduces a new kvfree_sensitive() for freeing those
> sensitive data objects allocated by kvmalloc(). The relevnat places
> where kvfree_sensitive() can be used are modified to use it.
>
> Fixes: 4f0882491a14 ("KEYS: Avoid false positive ENOMEM error on key read")
> Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
> Signed-off-by: Waiman Long <longman@redhat.com>
> ---
> include/linux/mm.h | 1 +
> mm/util.c | 18 ++++++++++++++++++
> security/keys/internal.h | 11 -----------
> security/keys/keyctl.c | 16 +++++-----------
> 4 files changed, 24 insertions(+), 22 deletions(-)
>
> [v3: Fix kerneldoc errors]
>
> diff --git a/include/linux/mm.h b/include/linux/mm.h
> index 7dd5c4ccbf85..9b3130b20f42 100644
> --- a/include/linux/mm.h
> +++ b/include/linux/mm.h
> @@ -757,6 +757,7 @@ static inline void *kvcalloc(size_t n, size_t size, gfp_t flags)
> }
>
> extern void kvfree(const void *addr);
> +extern void kvfree_sensitive(const void *addr, size_t len);
>
> static inline int compound_mapcount(struct page *page)
> {
> diff --git a/mm/util.c b/mm/util.c
> index 988d11e6c17c..dc1c877d5481 100644
> --- a/mm/util.c
> +++ b/mm/util.c
> @@ -604,6 +604,24 @@ void kvfree(const void *addr)
> }
> EXPORT_SYMBOL(kvfree);
>
> +/**
> + * kvfree_sensitive - Free a data object containing sensitive information.
> + * @addr: address of the data object to be freed.
> + * @len: length of the data object.
> + *
> + * Use the special memzero_explicit() function to clear the content of a
> + * kvmalloc'ed object containing sensitive data to make sure that the
> + * compiler won't optimize out the data clearing.
> + */
> +void kvfree_sensitive(const void *addr, size_t len)
> +{
> + if (likely(!ZERO_OR_NULL_PTR(addr))) {
> + memzero_explicit((void *)addr, len);
> + kvfree(addr);
> + }
> +}
> +EXPORT_SYMBOL(kvfree_sensitive);
> +
I wonder if the right thing to do is also to disable pre-emption, just so that the thread does not linger on with sensitive data.
void kvfree_sensitive(const void *addr, size_t len)
{
preempt_disable();
if (likely(!ZERO_OR_NULL_PTR(addr))) {
memzero_explicit((void *)addr, len);
kvfree(addr);
}
preempt_enable();
}
EXPORT_SYMBOL(kvfree_sensitive);
Balbir Singh.
next prev parent reply other threads:[~2020-05-14 11:00 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-04-07 20:03 [PATCH v3] mm: Add kvfree_sensitive() for freeing sensitive data objects Waiman Long
2020-04-07 20:09 ` Linus Torvalds
2020-04-07 20:19 ` David Howells
2020-04-07 20:24 ` Waiman Long
2020-04-07 20:21 ` David Howells
2020-05-05 20:35 ` Andrew Morton
2020-05-06 1:29 ` Waiman Long
2020-04-07 20:31 ` Joe Perches
2020-04-07 20:45 ` Waiman Long
2020-04-07 21:01 ` Linus Torvalds
2020-04-07 21:24 ` Uladzislau Rezki
2020-04-07 21:30 ` Linus Torvalds
2020-04-07 22:12 ` Matthew Wilcox
2020-04-08 0:35 ` Joe Perches
2020-04-08 13:38 ` Jarkko Sakkinen
2020-05-01 23:22 ` Eric Biggers
2020-05-04 2:57 ` Waiman Long
2020-05-14 11:00 ` Balbir Singh [this message]
2020-05-14 12:00 ` Matthew Wilcox
2020-05-14 12:08 ` Joe Perches
2020-05-17 0:27 ` Balbir Singh
2020-05-17 0:44 ` Matthew Wilcox
2020-05-18 2:39 ` Waiman Long
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1158ff38-c65d-379f-8ae7-6f507d9fc8dd@gmail.com \
--to=bsingharora@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=dhowells@redhat.com \
--cc=jarkko.sakkinen@linux.intel.com \
--cc=jmorris@namei.org \
--cc=joe@perches.com \
--cc=keyrings@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=longman@redhat.com \
--cc=rientjes@google.com \
--cc=serge@hallyn.com \
--cc=torvalds@linux-foundation.org \
--cc=willy@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).