GNU Linux-libre 4.19.286-gnu1
[releases.git] / lib / usercopy.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/uaccess.h>
3 #include <linux/nospec.h>
4
5 /* out-of-line parts */
6
7 #ifndef INLINE_COPY_FROM_USER
8 unsigned long _copy_from_user(void *to, const void __user *from, unsigned long n)
9 {
10         unsigned long res = n;
11         might_fault();
12         if (likely(access_ok(VERIFY_READ, from, n))) {
13                 /*
14                  * Ensure that bad access_ok() speculation will not
15                  * lead to nasty side effects *after* the copy is
16                  * finished:
17                  */
18                 barrier_nospec();
19                 kasan_check_write(to, n);
20                 res = raw_copy_from_user(to, from, n);
21         }
22         if (unlikely(res))
23                 memset(to + (n - res), 0, res);
24         return res;
25 }
26 EXPORT_SYMBOL(_copy_from_user);
27 #endif
28
29 #ifndef INLINE_COPY_TO_USER
30 unsigned long _copy_to_user(void __user *to, const void *from, unsigned long n)
31 {
32         might_fault();
33         if (likely(access_ok(VERIFY_WRITE, to, n))) {
34                 kasan_check_read(from, n);
35                 n = raw_copy_to_user(to, from, n);
36         }
37         return n;
38 }
39 EXPORT_SYMBOL(_copy_to_user);
40 #endif