1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/bitops.h>
3 #include <linux/fault-inject-usercopy.h>
4 #include <linux/instrumented.h>
5 #include <linux/uaccess.h>
6 #include <linux/nospec.h>
8 /* out-of-line parts */
10 #ifndef INLINE_COPY_FROM_USER
11 unsigned long _copy_from_user(void *to, const void __user *from, unsigned long n)
13 unsigned long res = n;
15 if (!should_fail_usercopy() && likely(access_ok(from, n))) {
17 * Ensure that bad access_ok() speculation will not
18 * lead to nasty side effects *after* the copy is
22 instrument_copy_from_user(to, from, n);
23 res = raw_copy_from_user(to, from, n);
26 memset(to + (n - res), 0, res);
29 EXPORT_SYMBOL(_copy_from_user);
32 #ifndef INLINE_COPY_TO_USER
33 unsigned long _copy_to_user(void __user *to, const void *from, unsigned long n)
36 if (should_fail_usercopy())
38 if (likely(access_ok(to, n))) {
39 instrument_copy_to_user(to, from, n);
40 n = raw_copy_to_user(to, from, n);
44 EXPORT_SYMBOL(_copy_to_user);
48 * check_zeroed_user: check if a userspace buffer only contains zero bytes
49 * @from: Source address, in userspace.
50 * @size: Size of buffer.
52 * This is effectively shorthand for "memchr_inv(from, 0, size) == NULL" for
53 * userspace addresses (and is more efficient because we don't care where the
54 * first non-zero byte is).
57 * * 0: There were non-zero bytes present in the buffer.
58 * * 1: The buffer was full of zero bytes.
59 * * -EFAULT: access to userspace failed.
61 int check_zeroed_user(const void __user *from, size_t size)
64 uintptr_t align = (uintptr_t) from % sizeof(unsigned long);
66 if (unlikely(size == 0))
72 if (!user_read_access_begin(from, size))
75 unsafe_get_user(val, (unsigned long __user *) from, err_fault);
77 val &= ~aligned_byte_mask(align);
79 while (size > sizeof(unsigned long)) {
83 from += sizeof(unsigned long);
84 size -= sizeof(unsigned long);
86 unsafe_get_user(val, (unsigned long __user *) from, err_fault);
89 if (size < sizeof(unsigned long))
90 val &= aligned_byte_mask(size);
93 user_read_access_end();
96 user_read_access_end();
99 EXPORT_SYMBOL(check_zeroed_user);