mm: Add kvfree_sensitive() for freeing sensitive data objects
Subsystems (4)
PATCH v3 1/1 mm: Add kvfree_sensitive() for freeing sensitive data objects
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.
--- 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]
include/linux/mm.h +1 -0
@@ -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) {
mm/util.c +18 -0
@@ -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); +static inline void *__page_rmapping (struct page *page) {unsigned long mapping;
security/keys/internal.h +0 -11
@@ -350,15 +350,4 @@ static inline void key_check(const struct key *key)#define key_check (key) do {} while(0)#endif - -/* -* Helper function to clear and free a kvmalloc'ed memory object. -*/ -static inline void __kvzfree (const void *addr,size_t len) -{ -if (addr) { -memset ((void *)addr,0 , len); -kvfree (addr); - } -}#endif /* _INTERNAL_H */
security/keys/keyctl.c +5 -11
@@ -142,10 +142,7 @@ SYSCALL_DEFINE5(add_key, const char __user *, _type,key_ref_put (keyring_ref); error3: -if (payload) { -memzero_explicit (payload, plen); -kvfree (payload); - } +kvfree_sensitive (payload, plen); error2:kfree (description); error:
@@ -360,7 +357,7 @@ long keyctl_update_key(key_serial_t id,key_ref_put (key_ref); error2: -__kvzfree (payload, plen); +kvfree_sensitive (payload, plen); error:return ret; }
@@ -914,7 +911,7 @@ long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)*/ if (ret > key_data_len) {if (unlikely (key_data)) -__kvzfree (key_data, key_data_len); +kvfree_sensitive (key_data, key_data_len); key_data_len = ret;continue ;/* Allocate buffer */ }
@@ -923,7 +920,7 @@ long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen) ret = -EFAULT ;break ; } -__kvzfree (key_data, key_data_len); +kvfree_sensitive (key_data, key_data_len); key_put_out:key_put (key);
@@ -1225,10 +1222,7 @@ long keyctl_instantiate_key_common(key_serial_t id,keyctl_change_reqkey_auth (NULL ); error2: -if (payload) { -memzero_explicit (payload, plen); -kvfree (payload); - } +kvfree_sensitive (payload, plen); error:return ret; }
Signature · 1 line
2.18.1
Quoted 6 lines · Waiman Long
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 specialmemzero_explicit()has to be used.
Ack. Since this isn't exactly high-priority, I'm assuming it will go through the usual channels (ie Andrew).
Linus
David Howells
maintainerQuoted 1 line · Waiman Long
sensitive data objects allocated by
kvmalloc(). The relevnat places
"relevant".
security/keys/keyctl.c long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen) · line 914
Quoted 3 lines
security/keys/keyctl.c +1
-1if (unlikely (key_data)) -__kvzfree (key_data, key_data_len); +kvfree_sensitive (key_data, key_data_len);
I think the if-statement is redundant.
David
David Howells
maintainerQuoted 5 lines · David Howells
security/keys/keyctl.c long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen) · line 914
Quoted 3 lines
security/keys/keyctl.c +1
-1if (unlikely (key_data)) -__kvzfree (key_data, key_data_len); +kvfree_sensitive (key_data, key_data_len);I think the if-statement is redundant.
Ah - I see that you explicitly wanted to keep it. There's a good chance it'll get janitored at some point.
David
Andrew Morton
maintainerQuoted 9 lines · On Tue, 07 Apr 2020 21:21:57 +0100 David Howells <[email protected]> wrote:
Quoted 5 lines · David Howells
security/keys/keyctl.c long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen) · line 914
Quoted 3 lines
security/keys/keyctl.c +1
-1if (unlikely (key_data)) -__kvzfree (key_data, key_data_len); +kvfree_sensitive (key_data, key_data_len);I think the if-statement is redundant.
Ah - I see that you explicitly wanted to keep it.
Why's that?
Quoted 1 line
There's a good chance it'll get janitored at some point.
Indeed. Perhaps add a few little comments to explain the reasoning and to keep the janitorial fingers away?
Quoted 10 lines · Andrew Morton
Quoted 7 lines · On Tue, 07 Apr 2020 21:21:57 +0100 David Howells <[email protected]> wrote:
Quoted 4 lines · David Howells
Review
Quoted 3 lines
if (
unlikely(key_data))(quoted diff) +1
-1- __kvzfree(key_data, key_data_len); + kvfree_sensitive(key_data, key_data_len);I think the if-statement is redundant.
Ah - I see that you explicitly wanted to keep it.
Why's that?
There is a comment above it:
/*
* The key may change (unlikely) in between 2 consecutive
* __keyctl_read_key() calls. In this case, we reallocate
* a larger buffer and redo the key read when
* key_data_len < ret <= buflen.
*/
if (ret > key_data_len) {
if (unlikely(key_data)) __kvzfree(key_data, key_data_len);
key_data will be defined only if the unlikely case that the key increase in length between the 2 consecutive __keyctl_read_key() call and we have to enlarge the buffer and read the key again. I want to keep the unlikely() macro to emphasize the fact that this condition should not happen.
Quoted 4 lines
Quoted 1 line
There's a good chance it'll get janitored at some point.
Indeed. Perhaps add a few little comments to explain the reasoning and to keep the janitorial fingers away?
I can reword the comment to make it more explicit and send a v4 if you think the current comment is not clear enough.
Cheers,
Longman
Quoted 4 lines · David Howells
Quoted 1 line · Waiman Long
sensitive data objects allocated by
kvmalloc(). The relevnat places"relevant".
Oh, sorry about the typo. Maybe Andrew can fix it.
Cheers,
Longman
Quoted 9 lines · Waiman Long
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 specialmemzero_explicit()has to be used.This patch introduces a new
kvfree_sensitive()for freeing those sensitive data objects allocated bykvmalloc(). The relevnat places wherekvfree_sensitive()can be used are modified to use it.
[]
include/linux/mm.h
Quoted 1 line
include/linux/mm.h +0
-0
[]
include/linux/mm.h static inline void *kvcalloc(size_t n, size_t size, gfp_t flags) · line 760
Quoted 5 lines
include/linux/mm.h +1
-0@@ -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);
Why should size_t len be required?
Why not do what kzfree does and memset
the entire allocation? (area->size)
Quoted 16 lines · Joe Perches
Quoted 9 lines · Waiman Long
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 specialmemzero_explicit()has to be used.This patch introduces a new
kvfree_sensitive()for freeing those sensitive data objects allocated bykvmalloc(). The relevnat places wherekvfree_sensitive()can be used are modified to use it.[]
include/linux/mm.h
Quoted 1 line
include/linux/mm.h +0
-0[]
Quoted 3 lines
include/linux/mm.h +0
-0@@ -757,6 +757,7 @@ static inline void *kvcalloc(size_t n, size_t size, gfp_t flags) }
Quoted 6 lines
include/linux/mm.h static inline void *kvcalloc(size_t n, size_t size, gfp_t flags) · line 760
Quoted 2 lines
include/linux/mm.h +1
-0extern void kvfree (const void *addr); +extern void kvfree_sensitive (const void *addr,size_t len);Why should
size_tlen be required?Why not do what kzfree does and memset
the entire allocation? (area->size)
If the memory is really virtually mapped, the only way to find out the size of the object is to use find_vm_area() which can be relatively high cost and no simple helper function is available. On the other hand, the length is readily available in the callers. So passing the length directly to the kvfree_sensitive is simpler.
Cheers,
Longman
Quoted 4 lines · Waiman Long
If the memory is really virtually mapped, the only way to find out the size of the object is to use
find_vm_area()which can be relatively high cost and no simple helper function is available.
We _could_ just push it down to a "vfree_sensitive()", and do it inside the vfree logic. That ends up obviously figuring out the size of the area eventually.
But since the vmalloc data structures fundamentally aren't irq-safe, vfree() actually has magical things like "if called in an interrupt, we'll delay it to work context".
So that "eventually" can be quite a bit later, and it would delay the overwriting of the sensitive data if we did that.
So this patch does end up simpler, but for vfree data it is actually technically the better approach too (since overwriting the sensitive data asap is what you want).
Linus
Quoted 6 lines · Linus Torvalds
Quoted 4 lines · Waiman Long
If the memory is really virtually mapped, the only way to find out the size of the object is to use
find_vm_area()which can be relatively high cost and no simple helper function is available.
Quoted 8 lines
We _could_ just push it down to a "
vfree_sensitive()", and do it inside the vfree logic. That ends up obviously figuring out the size of the area eventually.But since the vmalloc data structures fundamentally aren't irq-safe,
vfree()actually has magical things like "if called in an interrupt, we'll delay it to work context".
Just some thoughts. Sorry for jumping in.
Seems like there is only one place where we can "sleep". I mean when we call vfree(). That is free_vmap_area_noflush() -> try_purge_vmap_area_lazy(). Basically try_purge_vmap_area_lazy() can call the schedule() what is not allowed for IRQs. Instead of inlining the try_purge_vmap_area_lazy() into current context we can schedule_work(). And i think it makes sense from many point of views.
Also, we can end up in zeroed non-existance vmap area if we do not find_vmap_area().
Thanks!
--
Vlad Rezki
Quoted 7 lines · Uladzislau Rezki
Seems like there is only one place where we can "sleep". I mean when we call
vfree(). That isfree_vmap_area_noflush()->try_purge_vmap_area_lazy(). Basicallytry_purge_vmap_area_lazy()can call theschedule()what is not allowed for IRQs. Instead of inlining thetry_purge_vmap_area_lazy()into current context we canschedule_work(). And i think it makes sense from many point of views.
I don't think that's the only case.
Or rather, that may be the only case of _sleeping_, but we also aren't irq-safe wrt locking.
And I'm not just talking about the vmap_purge_lock mutex, but all the spinlocks etc we have.
That said, I haven't looked at that code in _ages_. Maybe those things would be trivial to just turn into irq-safe ones and there are no real latency issues anywhere.
Linus
Quoted 5 lines · Waiman Long
Quoted 4 lines · Joe Perches
Quoted 1 line · Waiman Long
+extern void
kvfree_sensitive(const void *addr, size_t len);Why should
size_tlen be required?
Quoted 8 lines
Quoted 2 lines
Why not do what kzfree does and memset
the entire allocation? (area->size)If the memory is really virtually mapped, the only way to find out the size of the object is to use
find_vm_area()which can be relatively high cost and no simple helper function is available. On the other hand, the length is readily available in the callers. So passing the length directly to thekvfree_sensitiveis simpler.
Also it lets us zero only the first N bytes of the allocation. That might be good for performance, if only the first N bytes of an M byte allocation are actually sensitive. I don't know if we have any such cases, but they could exist.
Quoted 15 lines · Matthew Wilcox
Quoted 5 lines · Waiman Long
Quoted 4 lines · Joe Perches
Quoted 1 line · Waiman Long
+extern void
kvfree_sensitive(const void *addr, size_t len);Why should
size_tlen be required?Quoted 8 lines
Quoted 2 lines
Why not do what kzfree does and memset
the entire allocation? (area->size)If the memory is really virtually mapped, the only way to find out the size of the object is to use
find_vm_area()which can be relatively high cost and no simple helper function is available. On the other hand, the length is readily available in the callers. So passing the length directly to thekvfree_sensitiveis simpler.
Quoted 4 lines
Also it lets us zero only the first N bytes of the allocation. That might be good for performance, if only the first N bytes of an M byte allocation are actually sensitive. I don't know if we have any such cases, but they could exist.
I would really doubt it as the allocation of
sensitive data should generally be separate.
Also, a similar argument could apply to
kzfree/kfree_sensitive.
Quoted 6 lines · Waiman Long
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 specialmemzero_explicit()has to be used.
Quoted 7 lines
This patch introduces a new
kvfree_sensitive()for freeing those sensitive data objects allocated bykvmalloc(). The relevnat places wherekvfree_sensitive()can be used are modified to use it.Fixes 4f0882491a14 ("KEYS: Avoid false positive ENOMEM error on key read")Suggested-by Linus Torvalds <[email protected]>Signed-off-by Waiman Long <[email protected]>
David, you want to pick this one?
/Jarkko
Quoted 6 lines · Waiman Long
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 specialmemzero_explicit()has to be used.
Quoted 7 lines
This patch introduces a new
kvfree_sensitive()for freeing those sensitive data objects allocated bykvmalloc(). The relevnat places wherekvfree_sensitive()can be used are modified to use it.Fixes 4f0882491a14 ("KEYS: Avoid false positive ENOMEM error on key read")Suggested-by Linus Torvalds <[email protected]>Signed-off-by Waiman Long <[email protected]>
Looks good, feel free to add:
(I don't really buy the argument that the compiler could compile away memset() before kvfree(). But I agree with using memzero_explicit() anyway to make the intent explicit.)
I don't see this patch in linux-next yet. Who is planning to take this patch? Presumably David through the keyrings tree, or Andrew through mm?
- Eric
Quoted 18 lines · Eric Biggers
Quoted 6 lines · Waiman Long
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 specialmemzero_explicit()has to be used.Quoted 7 lines
This patch introduces a new
kvfree_sensitive()for freeing those sensitive data objects allocated bykvmalloc(). The relevnat places wherekvfree_sensitive()can be used are modified to use it.Fixes 4f0882491a14 ("KEYS: Avoid false positive ENOMEM error on key read")Suggested-by Linus Torvalds <[email protected]>Signed-off-by Waiman Long <[email protected]>Looks good, feel free to add:
Reviewed-by Eric Biggers <[email protected]>
Quoted 9 lines
(I don't really buy the argument that the compiler could compile away
memset()beforekvfree(). But I agree with usingmemzero_explicit()anyway to make the intent explicit.)I don't see this patch in linux-next yet. Who is planning to take this patch? Presumably David through the keyrings tree, or Andrew through mm?
- Eric
Andrew, would you mind taking this patch into the mm-tree?
Thanks,
Longman
Quoted 50 lines · Waiman Long
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 specialmemzero_explicit()has to be used.This patch introduces a new
kvfree_sensitive()for freeing those sensitive data objects allocated bykvmalloc(). The relevnat places wherekvfree_sensitive()can be used are modified to use it.Fixes 4f0882491a14 ("KEYS: Avoid false positive ENOMEM error on key read")Suggested-by Linus Torvalds <[email protected]>Signed-off-by Waiman Long <[email protected]>--- 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]
include/linux/mm.h +1
-0@@ -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) {mm/util.c +8
-0@@ -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.
mm/util.c void kvfree(const void *addr) · line 624
Quoted 10 lines
mm/util.c +10
-0+*/ +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.
Quoted 11 lines · Balbir Singh
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(); }
Quoted 1 line
EXPORT_SYMBOL(kvfree_sensitive);
If it's _that_ sensitive then the caller should have disabled preemption. Because preemption could otherwise have occurred immediately before kvfree_sensitive() was called.
Quoted 14 lines · Matthew Wilcox
Quoted 11 lines · Balbir Singh
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(); }Quoted 1 line
EXPORT_SYMBOL(kvfree_sensitive);
Quoted 3 lines
If it's _that_ sensitive then the caller should have disabled preemption. Because preemption could otherwise have occurred immediately before
kvfree_sensitive()was called.
static inline ?
Quoted 14 lines · Matthew Wilcox
Quoted 11 lines · Balbir Singh
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(); }Quoted 1 line
EXPORT_SYMBOL(kvfree_sensitive);
Quoted 4 lines
If it's _that_ sensitive then the caller should have disabled preemption. Because preemption could otherwise have occurred immediately before
kvfree_sensitive()was called.
May be, but the callers of the API have to be explictly aware of the contract. I don't disagree with you on what you've said, but I was referring to the intent of freeing sensitive data vs the turn around time for doing so.
Balbir Singh.
Quoted 15 lines · Balbir Singh
Quoted 14 lines · Matthew Wilcox
Quoted 12 lines · Balbir Singh
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);
Quoted 8 lines
Quoted 4 lines
If it's _that_ sensitive then the caller should have disabled preemption. Because preemption could otherwise have occurred immediately before
kvfree_sensitive()was called.May be, but the callers of the API have to be explictly aware of the contract. I don't disagree with you on what you've said, but I was referring to the intent of freeing sensitive data vs the turn around time for doing so.
It's the caller's information. They should be aware of their own requirements. If they do something like:
p = kmalloc();preempt_disable();
construct(p);
use(p);preempt_enable();kvfree_sensitive(p);
there's really nothing we can do to help them inside kvfree_sensitive(). Actually, can you come up with a scenario where disabling preemption inside kvfree_sensitive() will help with anything?
Quoted 15 lines · Balbir Singh
Quoted 13 lines · Matthew Wilcox
Quoted 12 lines · Balbir Singh
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);
Quoted 7 lines
Quoted 4 lines
If it's _that_ sensitive then the caller should have disabled preemption. Because preemption could otherwise have occurred immediately before
kvfree_sensitive()was called.May be, but the callers of the API have to be explictly aware of the contract. I don't disagree with you on what you've said, but I was referring to the intent of freeing sensitive data vs the turn around time for doing so.
We can't disable preemption like that. The vfree() call may potentially sleep. It could be a mess to keep track of the preemption state to make that works.
The purpose of this API is to make sure that a newly allocated memory block won't contain secret left behind from another task. There is no guarantee on how long the freeing process will take.
Cheers,
Longman