Skip to content

Commit

Permalink
mm: add kvfree_sensitive() for freeing sensitive data objects
Browse files Browse the repository at this point in the history
For kvmalloc'ed data object that contains sensitive information like
cryptographic keys, 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 relevant places where
kvfree_sensitive() can be used are modified to use it.

Fixes: 4f08824 ("KEYS: Avoid false positive ENOMEM error on key read")
Suggested-by: Linus Torvalds <[email protected]>
Signed-off-by: Waiman Long <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Reviewed-by: Eric Biggers <[email protected]>
Acked-by: David Howells <[email protected]>
Cc: Jarkko Sakkinen <[email protected]>
Cc: James Morris <[email protected]>
Cc: "Serge E. Hallyn" <[email protected]>
Cc: Joe Perches <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Cc: David Rientjes <[email protected]>
Cc: Uladzislau Rezki <[email protected]>
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
Waiman-Long authored and torvalds committed Jun 5, 2020
1 parent 090e77e commit d4eaa28
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 22 deletions.
1 change: 1 addition & 0 deletions include/linux/mm.h
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,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);

/*
* Mapcount of compound page as a whole, does not include mapped sub-pages.
Expand Down
18 changes: 18 additions & 0 deletions mm/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 0 additions & 11 deletions security/keys/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
16 changes: 5 additions & 11 deletions security/keys/keyctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 */
}
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit d4eaa28

Please sign in to comment.