Merge tag 'nfs-for-6.12-1' of git://git.linux-nfs.org/projects/anna/linux-nfs
[linux-block.git] / lib / usercopy.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
5671dca2
AS
2#include <linux/compiler.h>
3#include <linux/errno.h>
4#include <linux/export.h>
4d0e9df5 5#include <linux/fault-inject-usercopy.h>
76d6f06c 6#include <linux/instrumented.h>
5671dca2
AS
7#include <linux/kernel.h>
8#include <linux/nospec.h>
9#include <linux/string.h>
76d6f06c 10#include <linux/uaccess.h>
9f2c2d6b 11#include <linux/wordpart.h>
d597580d
AV
12
13/* out-of-line parts */
14
1f9a8286 15#if !defined(INLINE_COPY_FROM_USER) || defined(CONFIG_RUST)
d597580d
AV
16unsigned long _copy_from_user(void *to, const void __user *from, unsigned long n)
17{
1f9a8286 18 return _inline_copy_from_user(to, from, n);
d597580d
AV
19}
20EXPORT_SYMBOL(_copy_from_user);
21#endif
22
1f9a8286 23#if !defined(INLINE_COPY_TO_USER) || defined(CONFIG_RUST)
a0e94598 24unsigned long _copy_to_user(void __user *to, const void *from, unsigned long n)
d597580d 25{
1f9a8286 26 return _inline_copy_to_user(to, from, n);
d597580d
AV
27}
28EXPORT_SYMBOL(_copy_to_user);
29#endif
f5a1a536
AS
30
31/**
32 * check_zeroed_user: check if a userspace buffer only contains zero bytes
33 * @from: Source address, in userspace.
34 * @size: Size of buffer.
35 *
36 * This is effectively shorthand for "memchr_inv(from, 0, size) == NULL" for
37 * userspace addresses (and is more efficient because we don't care where the
38 * first non-zero byte is).
39 *
40 * Returns:
41 * * 0: There were non-zero bytes present in the buffer.
42 * * 1: The buffer was full of zero bytes.
43 * * -EFAULT: access to userspace failed.
44 */
45int check_zeroed_user(const void __user *from, size_t size)
46{
47 unsigned long val;
48 uintptr_t align = (uintptr_t) from % sizeof(unsigned long);
49
50 if (unlikely(size == 0))
51 return 1;
52
53 from -= align;
54 size += align;
55
41cd7805 56 if (!user_read_access_begin(from, size))
f5a1a536
AS
57 return -EFAULT;
58
59 unsafe_get_user(val, (unsigned long __user *) from, err_fault);
60 if (align)
61 val &= ~aligned_byte_mask(align);
62
63 while (size > sizeof(unsigned long)) {
64 if (unlikely(val))
65 goto done;
66
67 from += sizeof(unsigned long);
68 size -= sizeof(unsigned long);
69
70 unsafe_get_user(val, (unsigned long __user *) from, err_fault);
71 }
72
73 if (size < sizeof(unsigned long))
74 val &= aligned_byte_mask(size);
75
76done:
41cd7805 77 user_read_access_end();
f5a1a536
AS
78 return (val == 0);
79err_fault:
41cd7805 80 user_read_access_end();
f5a1a536
AS
81 return -EFAULT;
82}
83EXPORT_SYMBOL(check_zeroed_user);