1 // SPDX-License-Identifier: GPL-2.0-only
3 * Access kernel memory without faulting.
5 #include <linux/export.h>
7 #include <linux/uaccess.h>
9 static __always_inline
long
10 probe_read_common(void *dst
, const void __user
*src
, size_t size
)
15 ret
= __copy_from_user_inatomic(dst
, src
, size
);
18 return ret
? -EFAULT
: 0;
21 static __always_inline
long
22 probe_write_common(void __user
*dst
, const void *src
, size_t size
)
27 ret
= __copy_to_user_inatomic(dst
, src
, size
);
30 return ret
? -EFAULT
: 0;
34 * probe_kernel_read(): safely attempt to read from a kernel-space location
35 * @dst: pointer to the buffer that shall take the data
36 * @src: address to read from
37 * @size: size of the data chunk
39 * Safely read from address @src to the buffer at @dst. If a kernel fault
40 * happens, handle that and return -EFAULT.
42 * We ensure that the copy_from_user is executed in atomic context so that
43 * do_page_fault() doesn't attempt to take mmap_sem. This makes
44 * probe_kernel_read() suitable for use within regions where the caller
45 * already holds mmap_sem, or other locks which nest inside mmap_sem.
48 long __weak
probe_kernel_read(void *dst
, const void *src
, size_t size
)
49 __attribute__((alias("__probe_kernel_read")));
51 long __probe_kernel_read(void *dst
, const void *src
, size_t size
)
54 mm_segment_t old_fs
= get_fs();
57 ret
= probe_read_common(dst
, (__force
const void __user
*)src
, size
);
62 EXPORT_SYMBOL_GPL(probe_kernel_read
);
65 * probe_user_read(): safely attempt to read from a user-space location
66 * @dst: pointer to the buffer that shall take the data
67 * @src: address to read from. This must be a user address.
68 * @size: size of the data chunk
70 * Safely read from user address @src to the buffer at @dst. If a kernel fault
71 * happens, handle that and return -EFAULT.
74 long __weak
probe_user_read(void *dst
, const void __user
*src
, size_t size
)
75 __attribute__((alias("__probe_user_read")));
77 long __probe_user_read(void *dst
, const void __user
*src
, size_t size
)
80 mm_segment_t old_fs
= get_fs();
83 if (access_ok(src
, size
))
84 ret
= probe_read_common(dst
, src
, size
);
89 EXPORT_SYMBOL_GPL(probe_user_read
);
92 * probe_kernel_write(): safely attempt to write to a location
93 * @dst: address to write to
94 * @src: pointer to the data that shall be written
95 * @size: size of the data chunk
97 * Safely write to address @dst from the buffer at @src. If a kernel fault
98 * happens, handle that and return -EFAULT.
101 long __weak
probe_kernel_write(void *dst
, const void *src
, size_t size
)
102 __attribute__((alias("__probe_kernel_write")));
104 long __probe_kernel_write(void *dst
, const void *src
, size_t size
)
107 mm_segment_t old_fs
= get_fs();
110 ret
= probe_write_common((__force
void __user
*)dst
, src
, size
);
115 EXPORT_SYMBOL_GPL(probe_kernel_write
);
118 * probe_user_write(): safely attempt to write to a user-space location
119 * @dst: address to write to
120 * @src: pointer to the data that shall be written
121 * @size: size of the data chunk
123 * Safely write to address @dst from the buffer at @src. If a kernel fault
124 * happens, handle that and return -EFAULT.
127 long __weak
probe_user_write(void __user
*dst
, const void *src
, size_t size
)
128 __attribute__((alias("__probe_user_write")));
130 long __probe_user_write(void __user
*dst
, const void *src
, size_t size
)
133 mm_segment_t old_fs
= get_fs();
136 if (access_ok(dst
, size
))
137 ret
= probe_write_common(dst
, src
, size
);
142 EXPORT_SYMBOL_GPL(probe_user_write
);
145 * strncpy_from_unsafe: - Copy a NUL terminated string from unsafe address.
146 * @dst: Destination address, in kernel space. This buffer must be at
147 * least @count bytes long.
148 * @unsafe_addr: Unsafe address.
149 * @count: Maximum number of bytes to copy, including the trailing NUL.
151 * Copies a NUL-terminated string from unsafe address to kernel buffer.
153 * On success, returns the length of the string INCLUDING the trailing NUL.
155 * If access fails, returns -EFAULT (some data may have been copied
156 * and the trailing NUL added).
158 * If @count is smaller than the length of the string, copies @count-1 bytes,
159 * sets the last byte of @dst buffer to NUL and returns @count.
161 long strncpy_from_unsafe(char *dst
, const void *unsafe_addr
, long count
)
163 mm_segment_t old_fs
= get_fs();
164 const void *src
= unsafe_addr
;
167 if (unlikely(count
<= 0))
174 ret
= __get_user(*dst
++, (const char __user __force
*)src
++);
175 } while (dst
[-1] && ret
== 0 && src
- unsafe_addr
< count
);
181 return ret
? -EFAULT
: src
- unsafe_addr
;
185 * strncpy_from_unsafe_user: - Copy a NUL terminated string from unsafe user
187 * @dst: Destination address, in kernel space. This buffer must be at
188 * least @count bytes long.
189 * @unsafe_addr: Unsafe user address.
190 * @count: Maximum number of bytes to copy, including the trailing NUL.
192 * Copies a NUL-terminated string from unsafe user address to kernel buffer.
194 * On success, returns the length of the string INCLUDING the trailing NUL.
196 * If access fails, returns -EFAULT (some data may have been copied
197 * and the trailing NUL added).
199 * If @count is smaller than the length of the string, copies @count-1 bytes,
200 * sets the last byte of @dst buffer to NUL and returns @count.
202 long strncpy_from_unsafe_user(char *dst
, const void __user
*unsafe_addr
,
205 mm_segment_t old_fs
= get_fs();
208 if (unlikely(count
<= 0))
213 ret
= strncpy_from_user(dst
, unsafe_addr
, count
);
220 } else if (ret
> 0) {
228 * strnlen_unsafe_user: - Get the size of a user string INCLUDING final NUL.
229 * @unsafe_addr: The string to measure.
230 * @count: Maximum count (including NUL)
232 * Get the size of a NUL-terminated string in user space without pagefault.
234 * Returns the size of the string INCLUDING the terminating NUL.
236 * If the string is too long, returns a number larger than @count. User
237 * has to check the return value against "> count".
238 * On exception (or invalid count), returns 0.
240 * Unlike strnlen_user, this can be used from IRQ handler etc. because
241 * it disables pagefaults.
243 long strnlen_unsafe_user(const void __user
*unsafe_addr
, long count
)
245 mm_segment_t old_fs
= get_fs();
250 ret
= strnlen_user(unsafe_addr
, count
);