GNU Linux-libre 5.4.257-gnu1
[releases.git] / lib / usercopy.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/uaccess.h>
3 #include <linux/bitops.h>
4 #include <linux/nospec.h>
5
6 /* out-of-line parts */
7
8 #ifndef INLINE_COPY_FROM_USER
9 unsigned long _copy_from_user(void *to, const void __user *from, unsigned long n)
10 {
11         unsigned long res = n;
12         might_fault();
13         if (likely(access_ok(from, n))) {
14                 /*
15                  * Ensure that bad access_ok() speculation will not
16                  * lead to nasty side effects *after* the copy is
17                  * finished:
18                  */
19                 barrier_nospec();
20                 kasan_check_write(to, n);
21                 res = raw_copy_from_user(to, from, n);
22         }
23         if (unlikely(res))
24                 memset(to + (n - res), 0, res);
25         return res;
26 }
27 EXPORT_SYMBOL(_copy_from_user);
28 #endif
29
30 #ifndef INLINE_COPY_TO_USER
31 unsigned long _copy_to_user(void __user *to, const void *from, unsigned long n)
32 {
33         might_fault();
34         if (likely(access_ok(to, n))) {
35                 kasan_check_read(from, n);
36                 n = raw_copy_to_user(to, from, n);
37         }
38         return n;
39 }
40 EXPORT_SYMBOL(_copy_to_user);
41 #endif
42
43 /**
44  * check_zeroed_user: check if a userspace buffer only contains zero bytes
45  * @from: Source address, in userspace.
46  * @size: Size of buffer.
47  *
48  * This is effectively shorthand for "memchr_inv(from, 0, size) == NULL" for
49  * userspace addresses (and is more efficient because we don't care where the
50  * first non-zero byte is).
51  *
52  * Returns:
53  *  * 0: There were non-zero bytes present in the buffer.
54  *  * 1: The buffer was full of zero bytes.
55  *  * -EFAULT: access to userspace failed.
56  */
57 int check_zeroed_user(const void __user *from, size_t size)
58 {
59         unsigned long val;
60         uintptr_t align = (uintptr_t) from % sizeof(unsigned long);
61
62         if (unlikely(size == 0))
63                 return 1;
64
65         from -= align;
66         size += align;
67
68         if (!user_access_begin(from, size))
69                 return -EFAULT;
70
71         unsafe_get_user(val, (unsigned long __user *) from, err_fault);
72         if (align)
73                 val &= ~aligned_byte_mask(align);
74
75         while (size > sizeof(unsigned long)) {
76                 if (unlikely(val))
77                         goto done;
78
79                 from += sizeof(unsigned long);
80                 size -= sizeof(unsigned long);
81
82                 unsafe_get_user(val, (unsigned long __user *) from, err_fault);
83         }
84
85         if (size < sizeof(unsigned long))
86                 val &= aligned_byte_mask(size);
87
88 done:
89         user_access_end();
90         return (val == 0);
91 err_fault:
92         user_access_end();
93         return -EFAULT;
94 }
95 EXPORT_SYMBOL(check_zeroed_user);