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
,
16 #define copy_from_kernel_nofault_loop(dst, src, len, type, err_label) \
17 while (len >= sizeof(type)) { \
18 __get_kernel_nofault(dst, src, type, err_label); \
19 dst += sizeof(type); \
20 src += sizeof(type); \
21 len -= sizeof(type); \
24 long copy_from_kernel_nofault(void *dst
, const void *src
, size_t size
)
26 unsigned long align
= 0;
28 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
))
29 align
= (unsigned long)dst
| (unsigned long)src
;
31 if (!copy_from_kernel_nofault_allowed(src
, size
))
36 copy_from_kernel_nofault_loop(dst
, src
, size
, u64
, Efault
);
38 copy_from_kernel_nofault_loop(dst
, src
, size
, u32
, Efault
);
40 copy_from_kernel_nofault_loop(dst
, src
, size
, u16
, Efault
);
41 copy_from_kernel_nofault_loop(dst
, src
, size
, u8
, Efault
);
48 EXPORT_SYMBOL_GPL(copy_from_kernel_nofault
);
50 #define copy_to_kernel_nofault_loop(dst, src, len, type, err_label) \
51 while (len >= sizeof(type)) { \
52 __put_kernel_nofault(dst, src, type, err_label); \
53 dst += sizeof(type); \
54 src += sizeof(type); \
55 len -= sizeof(type); \
58 long copy_to_kernel_nofault(void *dst
, const void *src
, size_t size
)
60 unsigned long align
= 0;
62 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
))
63 align
= (unsigned long)dst
| (unsigned long)src
;
67 copy_to_kernel_nofault_loop(dst
, src
, size
, u64
, Efault
);
69 copy_to_kernel_nofault_loop(dst
, src
, size
, u32
, Efault
);
71 copy_to_kernel_nofault_loop(dst
, src
, size
, u16
, Efault
);
72 copy_to_kernel_nofault_loop(dst
, src
, size
, u8
, Efault
);
80 long strncpy_from_kernel_nofault(char *dst
, const void *unsafe_addr
, long count
)
82 const void *src
= unsafe_addr
;
84 if (unlikely(count
<= 0))
86 if (!copy_from_kernel_nofault_allowed(unsafe_addr
, count
))
91 __get_kernel_nofault(dst
, src
, u8
, Efault
);
94 } while (dst
[-1] && src
- unsafe_addr
< count
);
98 return src
- unsafe_addr
;
106 * copy_from_user_nofault(): safely attempt to read from a user-space location
107 * @dst: pointer to the buffer that shall take the data
108 * @src: address to read from. This must be a user address.
109 * @size: size of the data chunk
111 * Safely read from user address @src to the buffer at @dst. If a kernel fault
112 * happens, handle that and return -EFAULT.
114 long copy_from_user_nofault(void *dst
, const void __user
*src
, size_t size
)
118 if (!__access_ok(src
, size
))
121 if (!nmi_uaccess_okay())
125 ret
= __copy_from_user_inatomic(dst
, src
, size
);
132 EXPORT_SYMBOL_GPL(copy_from_user_nofault
);
135 * copy_to_user_nofault(): safely attempt to write to a user-space location
136 * @dst: address to write to
137 * @src: pointer to the data that shall be written
138 * @size: size of the data chunk
140 * Safely write to address @dst from the buffer at @src. If a kernel fault
141 * happens, handle that and return -EFAULT.
143 long copy_to_user_nofault(void __user
*dst
, const void *src
, size_t size
)
147 if (access_ok(dst
, size
)) {
149 ret
= __copy_to_user_inatomic(dst
, src
, size
);
157 EXPORT_SYMBOL_GPL(copy_to_user_nofault
);
160 * strncpy_from_user_nofault: - Copy a NUL terminated string from unsafe user
162 * @dst: Destination address, in kernel space. This buffer must be at
163 * least @count bytes long.
164 * @unsafe_addr: Unsafe user address.
165 * @count: Maximum number of bytes to copy, including the trailing NUL.
167 * Copies a NUL-terminated string from unsafe user address to kernel buffer.
169 * On success, returns the length of the string INCLUDING the trailing NUL.
171 * If access fails, returns -EFAULT (some data may have been copied
172 * and the trailing NUL added).
174 * If @count is smaller than the length of the string, copies @count-1 bytes,
175 * sets the last byte of @dst buffer to NUL and returns @count.
177 long strncpy_from_user_nofault(char *dst
, const void __user
*unsafe_addr
,
182 if (unlikely(count
<= 0))
186 ret
= strncpy_from_user(dst
, unsafe_addr
, count
);
192 } else if (ret
> 0) {
200 * strnlen_user_nofault: - Get the size of a user string INCLUDING final NUL.
201 * @unsafe_addr: The string to measure.
202 * @count: Maximum count (including NUL)
204 * Get the size of a NUL-terminated string in user space without pagefault.
206 * Returns the size of the string INCLUDING the terminating NUL.
208 * If the string is too long, returns a number larger than @count. User
209 * has to check the return value against "> count".
210 * On exception (or invalid count), returns 0.
212 * Unlike strnlen_user, this can be used from IRQ handler etc. because
213 * it disables pagefaults.
215 long strnlen_user_nofault(const void __user
*unsafe_addr
, long count
)
220 ret
= strnlen_user(unsafe_addr
, count
);
226 void __copy_overflow(int size
, unsigned long count
)
228 WARN(1, "Buffer overflow detected (%d < %lu)!\n", size
, count
);
230 EXPORT_SYMBOL(__copy_overflow
);