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_before(to, from, n);
23 res = raw_copy_from_user(to, from, n);
24 instrument_copy_from_user_after(to, from, n, res);
27 memset(to + (n - res), 0, res);
30 EXPORT_SYMBOL(_copy_from_user);
33 #ifndef INLINE_COPY_TO_USER
34 unsigned long _copy_to_user(void __user *to, const void *from, unsigned long n)
37 if (should_fail_usercopy())
39 if (likely(access_ok(to, n))) {
40 instrument_copy_to_user(to, from, n);
41 n = raw_copy_to_user(to, from, n);
45 EXPORT_SYMBOL(_copy_to_user);
49 * check_zeroed_user: check if a userspace buffer only contains zero bytes
50 * @from: Source address, in userspace.
51 * @size: Size of buffer.
53 * This is effectively shorthand for "memchr_inv(from, 0, size) == NULL" for
54 * userspace addresses (and is more efficient because we don't care where the
55 * first non-zero byte is).
58 * * 0: There were non-zero bytes present in the buffer.
59 * * 1: The buffer was full of zero bytes.
60 * * -EFAULT: access to userspace failed.
62 int check_zeroed_user(const void __user *from, size_t size)
65 uintptr_t align = (uintptr_t) from % sizeof(unsigned long);
67 if (unlikely(size == 0))
73 if (!user_read_access_begin(from, size))
76 unsafe_get_user(val, (unsigned long __user *) from, err_fault);
78 val &= ~aligned_byte_mask(align);
80 while (size > sizeof(unsigned long)) {
84 from += sizeof(unsigned long);
85 size -= sizeof(unsigned long);
87 unsafe_get_user(val, (unsigned long __user *) from, err_fault);
90 if (size < sizeof(unsigned long))
91 val &= aligned_byte_mask(size);
94 user_read_access_end();
97 user_read_access_end();
100 EXPORT_SYMBOL(check_zeroed_user);