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>
10 bool __weak
copy_from_kernel_nofault_allowed(const void *unsafe_src
,
17 * The below only uses kmsan_check_memory() to ensure uninitialized kernel
18 * memory isn't leaked.
20 #define copy_from_kernel_nofault_loop(dst, src, len, type, err_label) \
21 while (len >= sizeof(type)) { \
22 __get_kernel_nofault(dst, src, type, err_label); \
23 kmsan_check_memory(src, sizeof(type)); \
24 dst += sizeof(type); \
25 src += sizeof(type); \
26 len -= sizeof(type); \
29 long copy_from_kernel_nofault(void *dst
, const void *src
, size_t size
)
31 unsigned long align
= 0;
33 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
))
34 align
= (unsigned long)dst
| (unsigned long)src
;
36 if (!copy_from_kernel_nofault_allowed(src
, size
))
41 copy_from_kernel_nofault_loop(dst
, src
, size
, u64
, Efault
);
43 copy_from_kernel_nofault_loop(dst
, src
, size
, u32
, Efault
);
45 copy_from_kernel_nofault_loop(dst
, src
, size
, u16
, Efault
);
46 copy_from_kernel_nofault_loop(dst
, src
, size
, u8
, Efault
);
53 EXPORT_SYMBOL_GPL(copy_from_kernel_nofault
);
55 #define copy_to_kernel_nofault_loop(dst, src, len, type, err_label) \
56 while (len >= sizeof(type)) { \
57 __put_kernel_nofault(dst, src, type, err_label); \
58 instrument_write(dst, sizeof(type)); \
59 dst += sizeof(type); \
60 src += sizeof(type); \
61 len -= sizeof(type); \
64 long copy_to_kernel_nofault(void *dst
, const void *src
, size_t size
)
66 unsigned long align
= 0;
68 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
))
69 align
= (unsigned long)dst
| (unsigned long)src
;
73 copy_to_kernel_nofault_loop(dst
, src
, size
, u64
, Efault
);
75 copy_to_kernel_nofault_loop(dst
, src
, size
, u32
, Efault
);
77 copy_to_kernel_nofault_loop(dst
, src
, size
, u16
, Efault
);
78 copy_to_kernel_nofault_loop(dst
, src
, size
, u8
, Efault
);
85 EXPORT_SYMBOL_GPL(copy_to_kernel_nofault
);
87 long strncpy_from_kernel_nofault(char *dst
, const void *unsafe_addr
, long count
)
89 const void *src
= unsafe_addr
;
91 if (unlikely(count
<= 0))
93 if (!copy_from_kernel_nofault_allowed(unsafe_addr
, count
))
98 __get_kernel_nofault(dst
, src
, u8
, Efault
);
101 } while (dst
[-1] && src
- unsafe_addr
< count
);
105 return src
- unsafe_addr
;
113 * copy_from_user_nofault(): safely attempt to read from a user-space location
114 * @dst: pointer to the buffer that shall take the data
115 * @src: address to read from. This must be a user address.
116 * @size: size of the data chunk
118 * Safely read from user address @src to the buffer at @dst. If a kernel fault
119 * happens, handle that and return -EFAULT.
121 long copy_from_user_nofault(void *dst
, const void __user
*src
, size_t size
)
125 if (!__access_ok(src
, size
))
128 if (!nmi_uaccess_okay())
132 ret
= __copy_from_user_inatomic(dst
, src
, size
);
139 EXPORT_SYMBOL_GPL(copy_from_user_nofault
);
142 * copy_to_user_nofault(): safely attempt to write to a user-space location
143 * @dst: address to write to
144 * @src: pointer to the data that shall be written
145 * @size: size of the data chunk
147 * Safely write to address @dst from the buffer at @src. If a kernel fault
148 * happens, handle that and return -EFAULT.
150 long copy_to_user_nofault(void __user
*dst
, const void *src
, size_t size
)
154 if (access_ok(dst
, size
)) {
156 ret
= __copy_to_user_inatomic(dst
, src
, size
);
164 EXPORT_SYMBOL_GPL(copy_to_user_nofault
);
167 * strncpy_from_user_nofault: - Copy a NUL terminated string from unsafe user
169 * @dst: Destination address, in kernel space. This buffer must be at
170 * least @count bytes long.
171 * @unsafe_addr: Unsafe user address.
172 * @count: Maximum number of bytes to copy, including the trailing NUL.
174 * Copies a NUL-terminated string from unsafe user address to kernel buffer.
176 * On success, returns the length of the string INCLUDING the trailing NUL.
178 * If access fails, returns -EFAULT (some data may have been copied
179 * and the trailing NUL added).
181 * If @count is smaller than the length of the string, copies @count-1 bytes,
182 * sets the last byte of @dst buffer to NUL and returns @count.
184 long strncpy_from_user_nofault(char *dst
, const void __user
*unsafe_addr
,
189 if (unlikely(count
<= 0))
193 ret
= strncpy_from_user(dst
, unsafe_addr
, count
);
199 } else if (ret
> 0) {
207 * strnlen_user_nofault: - Get the size of a user string INCLUDING final NUL.
208 * @unsafe_addr: The string to measure.
209 * @count: Maximum count (including NUL)
211 * Get the size of a NUL-terminated string in user space without pagefault.
213 * Returns the size of the string INCLUDING the terminating NUL.
215 * If the string is too long, returns a number larger than @count. User
216 * has to check the return value against "> count".
217 * On exception (or invalid count), returns 0.
219 * Unlike strnlen_user, this can be used from IRQ handler etc. because
220 * it disables pagefaults.
222 long strnlen_user_nofault(const void __user
*unsafe_addr
, long count
)
227 ret
= strnlen_user(unsafe_addr
, count
);
233 void __copy_overflow(int size
, unsigned long count
)
235 WARN(1, "Buffer overflow detected (%d < %lu)!\n", size
, count
);
237 EXPORT_SYMBOL(__copy_overflow
);