1 // SPDX-License-Identifier: GPL-2.0-only
3 * Access kernel or user memory without faulting.
5 #include <linux/export.h>
7 #include <linux/uaccess.h>
9 bool __weak copy_from_kernel_nofault_allowed(const void *unsafe_src,
15 #define copy_from_kernel_nofault_loop(dst, src, len, type, err_label) \
16 while (len >= sizeof(type)) { \
17 __get_kernel_nofault(dst, src, type, err_label); \
18 dst += sizeof(type); \
19 src += sizeof(type); \
20 len -= sizeof(type); \
23 long copy_from_kernel_nofault(void *dst, const void *src, size_t size)
25 unsigned long align = 0;
27 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
28 align = (unsigned long)dst | (unsigned long)src;
30 if (!copy_from_kernel_nofault_allowed(src, size))
35 copy_from_kernel_nofault_loop(dst, src, size, u64, Efault);
37 copy_from_kernel_nofault_loop(dst, src, size, u32, Efault);
39 copy_from_kernel_nofault_loop(dst, src, size, u16, Efault);
40 copy_from_kernel_nofault_loop(dst, src, size, u8, Efault);
47 EXPORT_SYMBOL_GPL(copy_from_kernel_nofault);
49 #define copy_to_kernel_nofault_loop(dst, src, len, type, err_label) \
50 while (len >= sizeof(type)) { \
51 __put_kernel_nofault(dst, src, type, err_label); \
52 dst += sizeof(type); \
53 src += sizeof(type); \
54 len -= sizeof(type); \
57 long copy_to_kernel_nofault(void *dst, const void *src, size_t size)
59 unsigned long align = 0;
61 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
62 align = (unsigned long)dst | (unsigned long)src;
66 copy_to_kernel_nofault_loop(dst, src, size, u64, Efault);
68 copy_to_kernel_nofault_loop(dst, src, size, u32, Efault);
70 copy_to_kernel_nofault_loop(dst, src, size, u16, Efault);
71 copy_to_kernel_nofault_loop(dst, src, size, u8, Efault);
79 long strncpy_from_kernel_nofault(char *dst, const void *unsafe_addr, long count)
81 const void *src = unsafe_addr;
83 if (unlikely(count <= 0))
85 if (!copy_from_kernel_nofault_allowed(unsafe_addr, count))
90 __get_kernel_nofault(dst, src, u8, Efault);
93 } while (dst[-1] && src - unsafe_addr < count);
97 return src - unsafe_addr;
105 * copy_from_user_nofault(): safely attempt to read from a user-space location
106 * @dst: pointer to the buffer that shall take the data
107 * @src: address to read from. This must be a user address.
108 * @size: size of the data chunk
110 * Safely read from user address @src to the buffer at @dst. If a kernel fault
111 * happens, handle that and return -EFAULT.
113 long copy_from_user_nofault(void *dst, const void __user *src, size_t size)
116 if (access_ok(src, size)) {
118 ret = __copy_from_user_inatomic(dst, src, size);
126 EXPORT_SYMBOL_GPL(copy_from_user_nofault);
129 * copy_to_user_nofault(): safely attempt to write to a user-space location
130 * @dst: address to write to
131 * @src: pointer to the data that shall be written
132 * @size: size of the data chunk
134 * Safely write to address @dst from the buffer at @src. If a kernel fault
135 * happens, handle that and return -EFAULT.
137 long copy_to_user_nofault(void __user *dst, const void *src, size_t size)
141 if (access_ok(dst, size)) {
143 ret = __copy_to_user_inatomic(dst, src, size);
151 EXPORT_SYMBOL_GPL(copy_to_user_nofault);
154 * strncpy_from_user_nofault: - Copy a NUL terminated string from unsafe user
156 * @dst: Destination address, in kernel space. This buffer must be at
157 * least @count bytes long.
158 * @unsafe_addr: Unsafe user address.
159 * @count: Maximum number of bytes to copy, including the trailing NUL.
161 * Copies a NUL-terminated string from unsafe user address to kernel buffer.
163 * On success, returns the length of the string INCLUDING the trailing NUL.
165 * If access fails, returns -EFAULT (some data may have been copied
166 * and the trailing NUL added).
168 * If @count is smaller than the length of the string, copies @count-1 bytes,
169 * sets the last byte of @dst buffer to NUL and returns @count.
171 long strncpy_from_user_nofault(char *dst, const void __user *unsafe_addr,
176 if (unlikely(count <= 0))
180 ret = strncpy_from_user(dst, unsafe_addr, count);
186 } else if (ret > 0) {
194 * strnlen_user_nofault: - Get the size of a user string INCLUDING final NUL.
195 * @unsafe_addr: The string to measure.
196 * @count: Maximum count (including NUL)
198 * Get the size of a NUL-terminated string in user space without pagefault.
200 * Returns the size of the string INCLUDING the terminating NUL.
202 * If the string is too long, returns a number larger than @count. User
203 * has to check the return value against "> count".
204 * On exception (or invalid count), returns 0.
206 * Unlike strnlen_user, this can be used from IRQ handler etc. because
207 * it disables pagefaults.
209 long strnlen_user_nofault(const void __user *unsafe_addr, long count)
214 ret = strnlen_user(unsafe_addr, count);
220 void __copy_overflow(int size, unsigned long count)
222 WARN(1, "Buffer overflow detected (%d < %lu)!\n", size, count);
224 EXPORT_SYMBOL(__copy_overflow);