linux-brain/security/keys
Waiman Long e4a281c7da KEYS: Avoid false positive ENOMEM error on key read
[ Upstream commit 4f0882491a148059a52480e753b7f07fc550e188 ]

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 <longman@redhat.com>
Signed-off-by: David Howells <dhowells@redhat.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
2020-04-29 16:31:23 +02:00
..
encrypted-keys KEYS: Don't write out to userspace while holding key semaphore 2020-04-23 10:30:24 +02:00
Kconfig security/keys: BIG_KEY requires CONFIG_CRYPTO 2017-10-18 09:12:40 +01:00
Makefile License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
big_key.c KEYS: Don't write out to userspace while holding key semaphore 2020-04-23 10:30:24 +02:00
compat.c KEYS: add SP800-56A KDF support for DH 2017-04-04 22:33:38 +01:00
compat_dh.c KEYS: DH: validate __spare field 2017-07-14 11:01:38 +10:00
dh.c Revert "uapi/linux/keyctl.h: don't use C++ reserved keyword as a struct member name" 2018-09-25 13:28:58 +02:00
gc.c Merge branch 'timers-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip 2017-11-25 08:37:16 -10:00
internal.h KEYS: Avoid false positive ENOMEM error on key read 2020-04-29 16:31:23 +02:00
key.c KEYS: reaching the keys quotas correctly 2020-04-17 10:48:46 +02:00
keyctl.c KEYS: Avoid false positive ENOMEM error on key read 2020-04-29 16:31:23 +02:00
keyring.c KEYS: Don't write out to userspace while holding key semaphore 2020-04-23 10:30:24 +02:00
permission.c security: keys: Replace time_t/timespec with time64_t 2017-11-15 16:38:45 +00:00
persistent.c sched/headers: Prepare to remove <linux/cred.h> inclusion from <linux/sched.h> 2017-03-02 08:42:31 +01:00
proc.c keys: Fix proc_keys_next to increase position index 2020-04-21 09:03:08 +02:00
process_keys.c keys: Fix dependency loop between construction record and auth key 2019-03-23 20:09:48 +01:00
request_key.c keys: Fix dependency loop between construction record and auth key 2019-03-23 20:09:48 +01:00
request_key_auth.c KEYS: Don't write out to userspace while holding key semaphore 2020-04-23 10:30:24 +02:00
sysctl.c security: Convert use of typedef ctl_table to struct ctl_table 2014-04-15 13:39:58 +10:00
trusted.c KEYS: Don't write out to userspace while holding key semaphore 2020-04-23 10:30:24 +02:00
trusted.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
user_defined.c KEYS: Don't write out to userspace while holding key semaphore 2020-04-23 10:30:24 +02:00