1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/uaccess.h>
3 #include <linux/bitops.h>
5 /* out-of-line parts */
7 #ifndef INLINE_COPY_FROM_USER
8 unsigned long _copy_from_user(void *to, const void __user *from, unsigned long n)
10 unsigned long res = n;
12 if (likely(access_ok(from, n))) {
13 kasan_check_write(to, n);
14 res = raw_copy_from_user(to, from, n);
17 memset(to + (n - res), 0, res);
20 EXPORT_SYMBOL(_copy_from_user);
23 #ifndef INLINE_COPY_TO_USER
24 unsigned long _copy_to_user(void __user *to, const void *from, unsigned long n)
27 if (likely(access_ok(to, n))) {
28 kasan_check_read(from, n);
29 n = raw_copy_to_user(to, from, n);
33 EXPORT_SYMBOL(_copy_to_user);
37 * check_zeroed_user: check if a userspace buffer only contains zero bytes
38 * @from: Source address, in userspace.
39 * @size: Size of buffer.
41 * This is effectively shorthand for "memchr_inv(from, 0, size) == NULL" for
42 * userspace addresses (and is more efficient because we don't care where the
43 * first non-zero byte is).
46 * * 0: There were non-zero bytes present in the buffer.
47 * * 1: The buffer was full of zero bytes.
48 * * -EFAULT: access to userspace failed.
50 int check_zeroed_user(const void __user *from, size_t size)
53 uintptr_t align = (uintptr_t) from % sizeof(unsigned long);
55 if (unlikely(size == 0))
61 if (!user_read_access_begin(from, size))
64 unsafe_get_user(val, (unsigned long __user *) from, err_fault);
66 val &= ~aligned_byte_mask(align);
68 while (size > sizeof(unsigned long)) {
72 from += sizeof(unsigned long);
73 size -= sizeof(unsigned long);
75 unsafe_get_user(val, (unsigned long __user *) from, err_fault);
78 if (size < sizeof(unsigned long))
79 val &= aligned_byte_mask(size);
82 user_read_access_end();
85 user_read_access_end();
88 EXPORT_SYMBOL(check_zeroed_user);