1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/bitops.h>
3 #include <linux/instrumented.h>
4 #include <linux/uaccess.h>
6 /* out-of-line parts */
8 #ifndef INLINE_COPY_FROM_USER
9 unsigned long _copy_from_user(void *to
, const void __user
*from
, unsigned long n
)
11 unsigned long res
= n
;
13 if (likely(access_ok(from
, n
))) {
14 instrument_copy_from_user(to
, from
, n
);
15 res
= raw_copy_from_user(to
, from
, n
);
18 memset(to
+ (n
- res
), 0, res
);
21 EXPORT_SYMBOL(_copy_from_user
);
24 #ifndef INLINE_COPY_TO_USER
25 unsigned long _copy_to_user(void __user
*to
, const void *from
, unsigned long n
)
28 if (likely(access_ok(to
, n
))) {
29 instrument_copy_to_user(to
, from
, n
);
30 n
= raw_copy_to_user(to
, from
, n
);
34 EXPORT_SYMBOL(_copy_to_user
);
38 * check_zeroed_user: check if a userspace buffer only contains zero bytes
39 * @from: Source address, in userspace.
40 * @size: Size of buffer.
42 * This is effectively shorthand for "memchr_inv(from, 0, size) == NULL" for
43 * userspace addresses (and is more efficient because we don't care where the
44 * first non-zero byte is).
47 * * 0: There were non-zero bytes present in the buffer.
48 * * 1: The buffer was full of zero bytes.
49 * * -EFAULT: access to userspace failed.
51 int check_zeroed_user(const void __user
*from
, size_t size
)
54 uintptr_t align
= (uintptr_t) from
% sizeof(unsigned long);
56 if (unlikely(size
== 0))
62 if (!user_read_access_begin(from
, size
))
65 unsafe_get_user(val
, (unsigned long __user
*) from
, err_fault
);
67 val
&= ~aligned_byte_mask(align
);
69 while (size
> sizeof(unsigned long)) {
73 from
+= sizeof(unsigned long);
74 size
-= sizeof(unsigned long);
76 unsafe_get_user(val
, (unsigned long __user
*) from
, err_fault
);
79 if (size
< sizeof(unsigned long))
80 val
&= aligned_byte_mask(size
);
83 user_read_access_end();
86 user_read_access_end();
89 EXPORT_SYMBOL(check_zeroed_user
);