blob: cbb4d9ec00f207a8156cab8a4aa52fb8a49ff43e [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Al Virod5975802017-03-20 21:56:06 -04002#include <linux/uaccess.h>
Aleksa Saraif5a1a532019-10-01 11:10:52 +10003#include <linux/bitops.h>
Al Virod5975802017-03-20 21:56:06 -04004
5/* out-of-line parts */
6
7#ifndef INLINE_COPY_FROM_USER
8unsigned long _copy_from_user(void *to, const void __user *from, unsigned long n)
9{
10 unsigned long res = n;
Al Viro9c5f6902017-06-29 21:39:54 -040011 might_fault();
Linus Torvalds96d4f262019-01-03 18:57:57 -080012 if (likely(access_ok(from, n))) {
Al Viro9c5f6902017-06-29 21:39:54 -040013 kasan_check_write(to, n);
Al Virod5975802017-03-20 21:56:06 -040014 res = raw_copy_from_user(to, from, n);
Al Viro9c5f6902017-06-29 21:39:54 -040015 }
Al Virod5975802017-03-20 21:56:06 -040016 if (unlikely(res))
17 memset(to + (n - res), 0, res);
18 return res;
19}
20EXPORT_SYMBOL(_copy_from_user);
21#endif
22
23#ifndef INLINE_COPY_TO_USER
Christophe Leroya0e94592017-12-09 17:24:24 +010024unsigned long _copy_to_user(void __user *to, const void *from, unsigned long n)
Al Virod5975802017-03-20 21:56:06 -040025{
Al Viro9c5f6902017-06-29 21:39:54 -040026 might_fault();
Linus Torvalds96d4f262019-01-03 18:57:57 -080027 if (likely(access_ok(to, n))) {
Al Viro9c5f6902017-06-29 21:39:54 -040028 kasan_check_read(from, n);
Al Virod5975802017-03-20 21:56:06 -040029 n = raw_copy_to_user(to, from, n);
Al Viro9c5f6902017-06-29 21:39:54 -040030 }
Al Virod5975802017-03-20 21:56:06 -040031 return n;
32}
33EXPORT_SYMBOL(_copy_to_user);
34#endif
Aleksa Saraif5a1a532019-10-01 11:10:52 +100035
36/**
37 * check_zeroed_user: check if a userspace buffer only contains zero bytes
38 * @from: Source address, in userspace.
39 * @size: Size of buffer.
40 *
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).
44 *
45 * Returns:
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.
49 */
50int check_zeroed_user(const void __user *from, size_t size)
51{
52 unsigned long val;
53 uintptr_t align = (uintptr_t) from % sizeof(unsigned long);
54
55 if (unlikely(size == 0))
56 return 1;
57
58 from -= align;
59 size += align;
60
61 if (!user_access_begin(from, size))
62 return -EFAULT;
63
64 unsafe_get_user(val, (unsigned long __user *) from, err_fault);
65 if (align)
66 val &= ~aligned_byte_mask(align);
67
68 while (size > sizeof(unsigned long)) {
69 if (unlikely(val))
70 goto done;
71
72 from += sizeof(unsigned long);
73 size -= sizeof(unsigned long);
74
75 unsafe_get_user(val, (unsigned long __user *) from, err_fault);
76 }
77
78 if (size < sizeof(unsigned long))
79 val &= aligned_byte_mask(size);
80
81done:
82 user_access_end();
83 return (val == 0);
84err_fault:
85 user_access_end();
86 return -EFAULT;
87}
88EXPORT_SYMBOL(check_zeroed_user);