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;
22 * probe_kernel_read(): safely attempt to read from a kernel-space location
23 * @dst: pointer to the buffer that shall take the data
24 * @src: address to read from
25 * @size: size of the data chunk
27 * Safely read from address @src to the buffer at @dst. If a kernel fault
28 * happens, handle that and return -EFAULT.
30 * We ensure that the copy_from_user is executed in atomic context so that
31 * do_page_fault() doesn't attempt to take mmap_sem. This makes
32 * probe_kernel_read() suitable for use within regions where the caller
33 * already holds mmap_sem, or other locks which nest inside mmap_sem.
36 long __weak
probe_kernel_read(void *dst
, const void *src
, size_t size
)
37 __attribute__((alias("__probe_kernel_read")));
39 long __probe_kernel_read(void *dst
, const void *src
, size_t size
)
42 mm_segment_t old_fs
= get_fs();
45 ret
= probe_read_common(dst
, (__force
const void __user
*)src
, size
);
50 EXPORT_SYMBOL_GPL(probe_kernel_read
);
53 * probe_user_read(): safely attempt to read from a user-space location
54 * @dst: pointer to the buffer that shall take the data
55 * @src: address to read from. This must be a user address.
56 * @size: size of the data chunk
58 * Safely read from user address @src to the buffer at @dst. If a kernel fault
59 * happens, handle that and return -EFAULT.
62 long __weak
probe_user_read(void *dst
, const void __user
*src
, size_t size
)
63 __attribute__((alias("__probe_user_read")));
65 long __probe_user_read(void *dst
, const void __user
*src
, size_t size
)
68 mm_segment_t old_fs
= get_fs();
71 if (access_ok(src
, size
))
72 ret
= probe_read_common(dst
, src
, size
);
77 EXPORT_SYMBOL_GPL(probe_user_read
);
80 * probe_kernel_write(): safely attempt to write to a location
81 * @dst: address to write to
82 * @src: pointer to the data that shall be written
83 * @size: size of the data chunk
85 * Safely write to address @dst from the buffer at @src. If a kernel fault
86 * happens, handle that and return -EFAULT.
88 long __weak
probe_kernel_write(void *dst
, const void *src
, size_t size
)
89 __attribute__((alias("__probe_kernel_write")));
91 long __probe_kernel_write(void *dst
, const void *src
, size_t size
)
94 mm_segment_t old_fs
= get_fs();
98 ret
= __copy_to_user_inatomic((__force
void __user
*)dst
, src
, size
);
102 return ret
? -EFAULT
: 0;
104 EXPORT_SYMBOL_GPL(probe_kernel_write
);
108 * strncpy_from_unsafe: - Copy a NUL terminated string from unsafe address.
109 * @dst: Destination address, in kernel space. This buffer must be at
110 * least @count bytes long.
111 * @unsafe_addr: Unsafe address.
112 * @count: Maximum number of bytes to copy, including the trailing NUL.
114 * Copies a NUL-terminated string from unsafe address to kernel buffer.
116 * On success, returns the length of the string INCLUDING the trailing NUL.
118 * If access fails, returns -EFAULT (some data may have been copied
119 * and the trailing NUL added).
121 * If @count is smaller than the length of the string, copies @count-1 bytes,
122 * sets the last byte of @dst buffer to NUL and returns @count.
124 long strncpy_from_unsafe(char *dst
, const void *unsafe_addr
, long count
)
126 mm_segment_t old_fs
= get_fs();
127 const void *src
= unsafe_addr
;
130 if (unlikely(count
<= 0))
137 ret
= __get_user(*dst
++, (const char __user __force
*)src
++);
138 } while (dst
[-1] && ret
== 0 && src
- unsafe_addr
< count
);
144 return ret
? -EFAULT
: src
- unsafe_addr
;
148 * strncpy_from_unsafe_user: - Copy a NUL terminated string from unsafe user
150 * @dst: Destination address, in kernel space. This buffer must be at
151 * least @count bytes long.
152 * @unsafe_addr: Unsafe user address.
153 * @count: Maximum number of bytes to copy, including the trailing NUL.
155 * Copies a NUL-terminated string from unsafe user address to kernel buffer.
157 * On success, returns the length of the string INCLUDING the trailing NUL.
159 * If access fails, returns -EFAULT (some data may have been copied
160 * and the trailing NUL added).
162 * If @count is smaller than the length of the string, copies @count-1 bytes,
163 * sets the last byte of @dst buffer to NUL and returns @count.
165 long strncpy_from_unsafe_user(char *dst
, const void __user
*unsafe_addr
,
168 mm_segment_t old_fs
= get_fs();
171 if (unlikely(count
<= 0))
176 ret
= strncpy_from_user(dst
, unsafe_addr
, count
);
183 } else if (ret
> 0) {
191 * strnlen_unsafe_user: - Get the size of a user string INCLUDING final NUL.
192 * @unsafe_addr: The string to measure.
193 * @count: Maximum count (including NUL)
195 * Get the size of a NUL-terminated string in user space without pagefault.
197 * Returns the size of the string INCLUDING the terminating NUL.
199 * If the string is too long, returns a number larger than @count. User
200 * has to check the return value against "> count".
201 * On exception (or invalid count), returns 0.
203 * Unlike strnlen_user, this can be used from IRQ handler etc. because
204 * it disables pagefaults.
206 long strnlen_unsafe_user(const void __user
*unsafe_addr
, long count
)
208 mm_segment_t old_fs
= get_fs();
213 ret
= strnlen_user(unsafe_addr
, count
);