mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2024-12-29 09:13:38 +00:00
f5a1a536fa
A common pattern for syscall extensions is increasing the size of a
struct passed from userspace, such that the zero-value of the new fields
result in the old kernel behaviour (allowing for a mix of userspace and
kernel vintages to operate on one another in most cases).
While this interface exists for communication in both directions, only
one interface is straightforward to have reasonable semantics for
(userspace passing a struct to the kernel). For kernel returns to
userspace, what the correct semantics are (whether there should be an
error if userspace is unaware of a new extension) is very
syscall-dependent and thus probably cannot be unified between syscalls
(a good example of this problem is [1]).
Previously there was no common lib/ function that implemented
the necessary extension-checking semantics (and different syscalls
implemented them slightly differently or incompletely[2]). Future
patches replace common uses of this pattern to make use of
copy_struct_from_user().
Some in-kernel selftests that insure that the handling of alignment and
various byte patterns are all handled identically to memchr_inv() usage.
[1]: commit 1251201c0d
("sched/core: Fix uclamp ABI bug, clean up and
robustify sched_read_attr() ABI logic and code")
[2]: For instance {sched_setattr,perf_event_open,clone3}(2) all do do
similar checks to copy_struct_from_user() while rt_sigprocmask(2)
always rejects differently-sized struct arguments.
Suggested-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Christian Brauner <christian.brauner@ubuntu.com>
Link: https://lore.kernel.org/r/20191001011055.19283-2-cyphar@cyphar.com
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
89 lines
2.0 KiB
C
89 lines
2.0 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
#include <linux/uaccess.h>
|
|
#include <linux/bitops.h>
|
|
|
|
/* out-of-line parts */
|
|
|
|
#ifndef INLINE_COPY_FROM_USER
|
|
unsigned long _copy_from_user(void *to, const void __user *from, unsigned long n)
|
|
{
|
|
unsigned long res = n;
|
|
might_fault();
|
|
if (likely(access_ok(from, n))) {
|
|
kasan_check_write(to, n);
|
|
res = raw_copy_from_user(to, from, n);
|
|
}
|
|
if (unlikely(res))
|
|
memset(to + (n - res), 0, res);
|
|
return res;
|
|
}
|
|
EXPORT_SYMBOL(_copy_from_user);
|
|
#endif
|
|
|
|
#ifndef INLINE_COPY_TO_USER
|
|
unsigned long _copy_to_user(void __user *to, const void *from, unsigned long n)
|
|
{
|
|
might_fault();
|
|
if (likely(access_ok(to, n))) {
|
|
kasan_check_read(from, n);
|
|
n = raw_copy_to_user(to, from, n);
|
|
}
|
|
return n;
|
|
}
|
|
EXPORT_SYMBOL(_copy_to_user);
|
|
#endif
|
|
|
|
/**
|
|
* check_zeroed_user: check if a userspace buffer only contains zero bytes
|
|
* @from: Source address, in userspace.
|
|
* @size: Size of buffer.
|
|
*
|
|
* This is effectively shorthand for "memchr_inv(from, 0, size) == NULL" for
|
|
* userspace addresses (and is more efficient because we don't care where the
|
|
* first non-zero byte is).
|
|
*
|
|
* Returns:
|
|
* * 0: There were non-zero bytes present in the buffer.
|
|
* * 1: The buffer was full of zero bytes.
|
|
* * -EFAULT: access to userspace failed.
|
|
*/
|
|
int check_zeroed_user(const void __user *from, size_t size)
|
|
{
|
|
unsigned long val;
|
|
uintptr_t align = (uintptr_t) from % sizeof(unsigned long);
|
|
|
|
if (unlikely(size == 0))
|
|
return 1;
|
|
|
|
from -= align;
|
|
size += align;
|
|
|
|
if (!user_access_begin(from, size))
|
|
return -EFAULT;
|
|
|
|
unsafe_get_user(val, (unsigned long __user *) from, err_fault);
|
|
if (align)
|
|
val &= ~aligned_byte_mask(align);
|
|
|
|
while (size > sizeof(unsigned long)) {
|
|
if (unlikely(val))
|
|
goto done;
|
|
|
|
from += sizeof(unsigned long);
|
|
size -= sizeof(unsigned long);
|
|
|
|
unsafe_get_user(val, (unsigned long __user *) from, err_fault);
|
|
}
|
|
|
|
if (size < sizeof(unsigned long))
|
|
val &= aligned_byte_mask(size);
|
|
|
|
done:
|
|
user_access_end();
|
|
return (val == 0);
|
|
err_fault:
|
|
user_access_end();
|
|
return -EFAULT;
|
|
}
|
|
EXPORT_SYMBOL(check_zeroed_user);
|