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>
7 /* out-of-line parts */
9 #ifndef INLINE_COPY_FROM_USER
10 unsigned long _copy_from_user(void *to, const void __user *from, unsigned long n)
12 unsigned long res = n;
14 if (!should_fail_usercopy() && likely(access_ok(from, n))) {
15 instrument_copy_from_user(to, from, n);
16 res = raw_copy_from_user(to, from, n);
19 memset(to + (n - res), 0, res);
22 EXPORT_SYMBOL(_copy_from_user);
25 #ifndef INLINE_COPY_TO_USER
26 unsigned long _copy_to_user(void __user *to, const void *from, unsigned long n)
29 if (should_fail_usercopy())
31 if (likely(access_ok(to, n))) {
32 instrument_copy_to_user(to, from, n);
33 n = raw_copy_to_user(to, from, n);
37 EXPORT_SYMBOL(_copy_to_user);
41 * check_zeroed_user: check if a userspace buffer only contains zero bytes
42 * @from: Source address, in userspace.
43 * @size: Size of buffer.
45 * This is effectively shorthand for "memchr_inv(from, 0, size) == NULL" for
46 * userspace addresses (and is more efficient because we don't care where the
47 * first non-zero byte is).
50 * * 0: There were non-zero bytes present in the buffer.
51 * * 1: The buffer was full of zero bytes.
52 * * -EFAULT: access to userspace failed.
54 int check_zeroed_user(const void __user *from, size_t size)
57 uintptr_t align = (uintptr_t) from % sizeof(unsigned long);
59 if (unlikely(size == 0))
65 if (!user_read_access_begin(from, size))
68 unsafe_get_user(val, (unsigned long __user *) from, err_fault);
70 val &= ~aligned_byte_mask(align);
72 while (size > sizeof(unsigned long)) {
76 from += sizeof(unsigned long);
77 size -= sizeof(unsigned long);
79 unsafe_get_user(val, (unsigned long __user *) from, err_fault);
82 if (size < sizeof(unsigned long))
83 val &= aligned_byte_mask(size);
86 user_read_access_end();
89 user_read_access_end();
92 EXPORT_SYMBOL(check_zeroed_user);