Skip to content

Commit 4f08824

Browse files
Waiman-Longdhowells
authored andcommitted
KEYS: Avoid false positive ENOMEM error on key read
By allocating a kernel buffer with a user-supplied buffer length, it is possible that a false positive ENOMEM error may be returned because the user-supplied length is just too large even if the system do have enough memory to hold the actual key data. Moreover, if the buffer length is larger than the maximum amount of memory that can be returned by kmalloc() (2^(MAX_ORDER-1) number of pages), a warning message will also be printed. To reduce this possibility, we set a threshold (PAGE_SIZE) over which we do check the actual key length first before allocating a buffer of the right size to hold it. The threshold is arbitrary, it is just used to trigger a buffer length check. It does not limit the actual key length as long as there is enough memory to satisfy the memory request. To further avoid large buffer allocation failure due to page fragmentation, kvmalloc() is used to allocate the buffer so that vmapped pages can be used when there is not a large enough contiguous set of pages available for allocation. In the extremely unlikely scenario that the key keeps on being changed and made longer (still <= buflen) in between 2 __keyctl_read_key() calls, the __keyctl_read_key() calling loop in keyctl_read_key() may have to be iterated a large number of times, but definitely not infinite. Signed-off-by: Waiman Long <[email protected]> Signed-off-by: David Howells <[email protected]>
1 parent d3ec10a commit 4f08824

File tree

2 files changed

+55
-15
lines changed

2 files changed

+55
-15
lines changed

security/keys/internal.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include <linux/keyctl.h>
1717
#include <linux/refcount.h>
1818
#include <linux/compat.h>
19+
#include <linux/mm.h>
20+
#include <linux/vmalloc.h>
1921

2022
struct iovec;
2123

@@ -349,4 +351,14 @@ static inline void key_check(const struct key *key)
349351

350352
#endif
351353

354+
/*
355+
* Helper function to clear and free a kvmalloc'ed memory object.
356+
*/
357+
static inline void __kvzfree(const void *addr, size_t len)
358+
{
359+
if (addr) {
360+
memset((void *)addr, 0, len);
361+
kvfree(addr);
362+
}
363+
}
352364
#endif /* _INTERNAL_H */

security/keys/keyctl.c

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ long keyctl_update_key(key_serial_t id,
339339
payload = NULL;
340340
if (plen) {
341341
ret = -ENOMEM;
342-
payload = kmalloc(plen, GFP_KERNEL);
342+
payload = kvmalloc(plen, GFP_KERNEL);
343343
if (!payload)
344344
goto error;
345345

@@ -360,7 +360,7 @@ long keyctl_update_key(key_serial_t id,
360360

361361
key_ref_put(key_ref);
362362
error2:
363-
kzfree(payload);
363+
__kvzfree(payload, plen);
364364
error:
365365
return ret;
366366
}
@@ -827,7 +827,8 @@ long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
827827
struct key *key;
828828
key_ref_t key_ref;
829829
long ret;
830-
char *key_data;
830+
char *key_data = NULL;
831+
size_t key_data_len;
831832

832833
/* find the key first */
833834
key_ref = lookup_user_key(keyid, 0, 0);
@@ -878,24 +879,51 @@ long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
878879
* Allocating a temporary buffer to hold the keys before
879880
* transferring them to user buffer to avoid potential
880881
* deadlock involving page fault and mmap_sem.
882+
*
883+
* key_data_len = (buflen <= PAGE_SIZE)
884+
* ? buflen : actual length of key data
885+
*
886+
* This prevents allocating arbitrary large buffer which can
887+
* be much larger than the actual key length. In the latter case,
888+
* at least 2 passes of this loop is required.
881889
*/
882-
key_data = kmalloc(buflen, GFP_KERNEL);
890+
key_data_len = (buflen <= PAGE_SIZE) ? buflen : 0;
891+
for (;;) {
892+
if (key_data_len) {
893+
key_data = kvmalloc(key_data_len, GFP_KERNEL);
894+
if (!key_data) {
895+
ret = -ENOMEM;
896+
goto key_put_out;
897+
}
898+
}
883899

884-
if (!key_data) {
885-
ret = -ENOMEM;
886-
goto key_put_out;
887-
}
888-
ret = __keyctl_read_key(key, key_data, buflen);
900+
ret = __keyctl_read_key(key, key_data, key_data_len);
901+
902+
/*
903+
* Read methods will just return the required length without
904+
* any copying if the provided length isn't large enough.
905+
*/
906+
if (ret <= 0 || ret > buflen)
907+
break;
908+
909+
/*
910+
* The key may change (unlikely) in between 2 consecutive
911+
* __keyctl_read_key() calls. In this case, we reallocate
912+
* a larger buffer and redo the key read when
913+
* key_data_len < ret <= buflen.
914+
*/
915+
if (ret > key_data_len) {
916+
if (unlikely(key_data))
917+
__kvzfree(key_data, key_data_len);
918+
key_data_len = ret;
919+
continue; /* Allocate buffer */
920+
}
889921

890-
/*
891-
* Read methods will just return the required length without
892-
* any copying if the provided length isn't large enough.
893-
*/
894-
if (ret > 0 && ret <= buflen) {
895922
if (copy_to_user(buffer, key_data, ret))
896923
ret = -EFAULT;
924+
break;
897925
}
898-
kzfree(key_data);
926+
__kvzfree(key_data, key_data_len);
899927

900928
key_put_out:
901929
key_put(key);

0 commit comments

Comments
 (0)