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 #ifdef HAVE_GET_KERNEL_NOFAULT
17 #define copy_from_kernel_nofault_loop(dst, src, len, type, err_label) \
18 while (len >= sizeof(type)) { \
19 __get_kernel_nofault(dst, src, type, err_label); \
20 dst += sizeof(type); \
21 src += sizeof(type); \
22 len -= sizeof(type); \
25 long copy_from_kernel_nofault(void *dst, const void *src, size_t size)
27 unsigned long align = 0;
29 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
30 align = (unsigned long)dst | (unsigned long)src;
32 if (!copy_from_kernel_nofault_allowed(src, size))
37 copy_from_kernel_nofault_loop(dst, src, size, u64, Efault);
39 copy_from_kernel_nofault_loop(dst, src, size, u32, Efault);
41 copy_from_kernel_nofault_loop(dst, src, size, u16, Efault);
42 copy_from_kernel_nofault_loop(dst, src, size, u8, Efault);
49 EXPORT_SYMBOL_GPL(copy_from_kernel_nofault);
51 #define copy_to_kernel_nofault_loop(dst, src, len, type, err_label) \
52 while (len >= sizeof(type)) { \
53 __put_kernel_nofault(dst, src, type, err_label); \
54 dst += sizeof(type); \
55 src += sizeof(type); \
56 len -= sizeof(type); \
59 long copy_to_kernel_nofault(void *dst, const void *src, size_t size)
61 unsigned long align = 0;
63 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
64 align = (unsigned long)dst | (unsigned long)src;
68 copy_to_kernel_nofault_loop(dst, src, size, u64, Efault);
70 copy_to_kernel_nofault_loop(dst, src, size, u32, Efault);
72 copy_to_kernel_nofault_loop(dst, src, size, u16, Efault);
73 copy_to_kernel_nofault_loop(dst, src, size, u8, Efault);
81 long strncpy_from_kernel_nofault(char *dst, const void *unsafe_addr, long count)
83 const void *src = unsafe_addr;
85 if (unlikely(count <= 0))
87 if (!copy_from_kernel_nofault_allowed(unsafe_addr, count))
92 __get_kernel_nofault(dst, src, u8, Efault);
95 } while (dst[-1] && src - unsafe_addr < count);
99 return src - unsafe_addr;
105 #else /* HAVE_GET_KERNEL_NOFAULT */
107 * copy_from_kernel_nofault(): safely attempt to read from kernel-space
108 * @dst: pointer to the buffer that shall take the data
109 * @src: address to read from
110 * @size: size of the data chunk
112 * Safely read from kernel address @src to the buffer at @dst. If a kernel
113 * fault happens, handle that and return -EFAULT. If @src is not a valid kernel
114 * address, return -ERANGE.
116 * We ensure that the copy_from_user is executed in atomic context so that
117 * do_page_fault() doesn't attempt to take mmap_lock. This makes
118 * copy_from_kernel_nofault() suitable for use within regions where the caller
119 * already holds mmap_lock, or other locks which nest inside mmap_lock.
121 long copy_from_kernel_nofault(void *dst, const void *src, size_t size)
124 mm_segment_t old_fs = get_fs();
126 if (!copy_from_kernel_nofault_allowed(src, size))
131 ret = __copy_from_user_inatomic(dst, (__force const void __user *)src,
140 EXPORT_SYMBOL_GPL(copy_from_kernel_nofault);
143 * copy_to_kernel_nofault(): safely attempt to write to a location
144 * @dst: address to write to
145 * @src: pointer to the data that shall be written
146 * @size: size of the data chunk
148 * Safely write to address @dst from the buffer at @src. If a kernel fault
149 * happens, handle that and return -EFAULT.
151 long copy_to_kernel_nofault(void *dst, const void *src, size_t size)
154 mm_segment_t old_fs = get_fs();
158 ret = __copy_to_user_inatomic((__force void __user *)dst, src, size);
168 * strncpy_from_kernel_nofault: - Copy a NUL terminated string from unsafe
170 * @dst: Destination address, in kernel space. This buffer must be at
171 * least @count bytes long.
172 * @unsafe_addr: Unsafe address.
173 * @count: Maximum number of bytes to copy, including the trailing NUL.
175 * Copies a NUL-terminated string from unsafe address to kernel buffer.
177 * On success, returns the length of the string INCLUDING the trailing NUL.
179 * If access fails, returns -EFAULT (some data may have been copied and the
180 * trailing NUL added). If @unsafe_addr is not a valid kernel address, return
183 * If @count is smaller than the length of the string, copies @count-1 bytes,
184 * sets the last byte of @dst buffer to NUL and returns @count.
186 long strncpy_from_kernel_nofault(char *dst, const void *unsafe_addr, long count)
188 mm_segment_t old_fs = get_fs();
189 const void *src = unsafe_addr;
192 if (unlikely(count <= 0))
194 if (!copy_from_kernel_nofault_allowed(unsafe_addr, count))
201 ret = __get_user(*dst++, (const char __user __force *)src++);
202 } while (dst[-1] && ret == 0 && src - unsafe_addr < count);
208 return ret ? -EFAULT : src - unsafe_addr;
210 #endif /* HAVE_GET_KERNEL_NOFAULT */
213 * copy_from_user_nofault(): safely attempt to read from a user-space location
214 * @dst: pointer to the buffer that shall take the data
215 * @src: address to read from. This must be a user address.
216 * @size: size of the data chunk
218 * Safely read from user address @src to the buffer at @dst. If a kernel fault
219 * happens, handle that and return -EFAULT.
221 long copy_from_user_nofault(void *dst, const void __user *src, size_t size)
224 mm_segment_t old_fs = force_uaccess_begin();
226 if (access_ok(src, size)) {
228 ret = __copy_from_user_inatomic(dst, src, size);
231 force_uaccess_end(old_fs);
237 EXPORT_SYMBOL_GPL(copy_from_user_nofault);
240 * copy_to_user_nofault(): safely attempt to write to a user-space location
241 * @dst: address to write to
242 * @src: pointer to the data that shall be written
243 * @size: size of the data chunk
245 * Safely write to address @dst from the buffer at @src. If a kernel fault
246 * happens, handle that and return -EFAULT.
248 long copy_to_user_nofault(void __user *dst, const void *src, size_t size)
251 mm_segment_t old_fs = force_uaccess_begin();
253 if (access_ok(dst, size)) {
255 ret = __copy_to_user_inatomic(dst, src, size);
258 force_uaccess_end(old_fs);
264 EXPORT_SYMBOL_GPL(copy_to_user_nofault);
267 * strncpy_from_user_nofault: - Copy a NUL terminated string from unsafe user
269 * @dst: Destination address, in kernel space. This buffer must be at
270 * least @count bytes long.
271 * @unsafe_addr: Unsafe user address.
272 * @count: Maximum number of bytes to copy, including the trailing NUL.
274 * Copies a NUL-terminated string from unsafe user address to kernel buffer.
276 * On success, returns the length of the string INCLUDING the trailing NUL.
278 * If access fails, returns -EFAULT (some data may have been copied
279 * and the trailing NUL added).
281 * If @count is smaller than the length of the string, copies @count-1 bytes,
282 * sets the last byte of @dst buffer to NUL and returns @count.
284 long strncpy_from_user_nofault(char *dst, const void __user *unsafe_addr,
290 if (unlikely(count <= 0))
293 old_fs = force_uaccess_begin();
295 ret = strncpy_from_user(dst, unsafe_addr, count);
297 force_uaccess_end(old_fs);
302 } else if (ret > 0) {
310 * strnlen_user_nofault: - Get the size of a user string INCLUDING final NUL.
311 * @unsafe_addr: The string to measure.
312 * @count: Maximum count (including NUL)
314 * Get the size of a NUL-terminated string in user space without pagefault.
316 * Returns the size of the string INCLUDING the terminating NUL.
318 * If the string is too long, returns a number larger than @count. User
319 * has to check the return value against "> count".
320 * On exception (or invalid count), returns 0.
322 * Unlike strnlen_user, this can be used from IRQ handler etc. because
323 * it disables pagefaults.
325 long strnlen_user_nofault(const void __user *unsafe_addr, long count)
330 old_fs = force_uaccess_begin();
332 ret = strnlen_user(unsafe_addr, count);
334 force_uaccess_end(old_fs);