]>
git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - mm/maccess.c
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 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 any 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 * Same as probe_kernel_read_strict() except that for architectures with
40 * not fully separated user and kernel address spaces this function also works
41 * for user address tanges.
43 * DO NOT USE THIS FUNCTION - it is broken on architectures with entirely
44 * separate kernel and user address spaces, and also a bad idea otherwise.
46 long __weak
probe_kernel_read(void *dst
, const void *src
, size_t size
)
47 __attribute__((alias("__probe_kernel_read")));
50 * probe_kernel_read_strict(): safely attempt to read from kernel-space
51 * @dst: pointer to the buffer that shall take the data
52 * @src: address to read from
53 * @size: size of the data chunk
55 * Safely read from kernel address @src to the buffer at @dst. If a kernel
56 * fault happens, handle that and return -EFAULT.
58 * We ensure that the copy_from_user is executed in atomic context so that
59 * do_page_fault() doesn't attempt to take mmap_lock. This makes
60 * probe_kernel_read() suitable for use within regions where the caller
61 * already holds mmap_lock, or other locks which nest inside mmap_lock.
64 long __weak
probe_kernel_read_strict(void *dst
, const void *src
, size_t size
)
65 __attribute__((alias("__probe_kernel_read")));
67 long __probe_kernel_read(void *dst
, const void *src
, size_t size
)
70 mm_segment_t old_fs
= get_fs();
73 ret
= probe_read_common(dst
, (__force
const void __user
*)src
, size
);
78 EXPORT_SYMBOL_GPL(probe_kernel_read
);
81 * probe_user_read(): safely attempt to read from a user-space location
82 * @dst: pointer to the buffer that shall take the data
83 * @src: address to read from. This must be a user address.
84 * @size: size of the data chunk
86 * Safely read from user address @src to the buffer at @dst. If a kernel fault
87 * happens, handle that and return -EFAULT.
89 long probe_user_read(void *dst
, const void __user
*src
, size_t size
)
92 mm_segment_t old_fs
= get_fs();
95 if (access_ok(src
, size
))
96 ret
= probe_read_common(dst
, src
, size
);
101 EXPORT_SYMBOL_GPL(probe_user_read
);
104 * probe_kernel_write(): safely attempt to write to a location
105 * @dst: address to write to
106 * @src: pointer to the data that shall be written
107 * @size: size of the data chunk
109 * Safely write to address @dst from the buffer at @src. If a kernel fault
110 * happens, handle that and return -EFAULT.
112 long probe_kernel_write(void *dst
, const void *src
, size_t size
)
115 mm_segment_t old_fs
= get_fs();
118 ret
= probe_write_common((__force
void __user
*)dst
, src
, size
);
125 * probe_user_write(): safely attempt to write to a user-space location
126 * @dst: address to write to
127 * @src: pointer to the data that shall be written
128 * @size: size of the data chunk
130 * Safely write to address @dst from the buffer at @src. If a kernel fault
131 * happens, handle that and return -EFAULT.
133 long probe_user_write(void __user
*dst
, const void *src
, size_t size
)
136 mm_segment_t old_fs
= get_fs();
139 if (access_ok(dst
, size
))
140 ret
= probe_write_common(dst
, src
, size
);
145 EXPORT_SYMBOL_GPL(probe_user_write
);
148 * strncpy_from_unsafe: - Copy a NUL terminated string from unsafe address.
149 * @dst: Destination address, in kernel space. This buffer must be at
150 * least @count bytes long.
151 * @unsafe_addr: Unsafe address.
152 * @count: Maximum number of bytes to copy, including the trailing NUL.
154 * Copies a NUL-terminated string from unsafe address to kernel buffer.
156 * On success, returns the length of the string INCLUDING the trailing NUL.
158 * If access fails, returns -EFAULT (some data may have been copied
159 * and the trailing NUL added).
161 * If @count is smaller than the length of the string, copies @count-1 bytes,
162 * sets the last byte of @dst buffer to NUL and returns @count.
164 * Same as strncpy_from_unsafe_strict() except that for architectures with
165 * not fully separated user and kernel address spaces this function also works
166 * for user address tanges.
168 * DO NOT USE THIS FUNCTION - it is broken on architectures with entirely
169 * separate kernel and user address spaces, and also a bad idea otherwise.
171 long __weak
strncpy_from_unsafe(char *dst
, const void *unsafe_addr
, long count
)
172 __attribute__((alias("__strncpy_from_unsafe")));
175 * strncpy_from_unsafe_strict: - Copy a NUL terminated string from unsafe
177 * @dst: Destination address, in kernel space. This buffer must be at
178 * least @count bytes long.
179 * @unsafe_addr: Unsafe address.
180 * @count: Maximum number of bytes to copy, including the trailing NUL.
182 * Copies a NUL-terminated string from unsafe address to kernel buffer.
184 * On success, returns the length of the string INCLUDING the trailing NUL.
186 * If access fails, returns -EFAULT (some data may have been copied
187 * and the trailing NUL added).
189 * If @count is smaller than the length of the string, copies @count-1 bytes,
190 * sets the last byte of @dst buffer to NUL and returns @count.
192 long __weak
strncpy_from_unsafe_strict(char *dst
, const void *unsafe_addr
,
194 __attribute__((alias("__strncpy_from_unsafe")));
196 long __strncpy_from_unsafe(char *dst
, const void *unsafe_addr
, long count
)
198 mm_segment_t old_fs
= get_fs();
199 const void *src
= unsafe_addr
;
202 if (unlikely(count
<= 0))
209 ret
= __get_user(*dst
++, (const char __user __force
*)src
++);
210 } while (dst
[-1] && ret
== 0 && src
- unsafe_addr
< count
);
216 return ret
? -EFAULT
: src
- unsafe_addr
;
220 * strncpy_from_user_nofault: - Copy a NUL terminated string from unsafe user
222 * @dst: Destination address, in kernel space. This buffer must be at
223 * least @count bytes long.
224 * @unsafe_addr: Unsafe user address.
225 * @count: Maximum number of bytes to copy, including the trailing NUL.
227 * Copies a NUL-terminated string from unsafe user address to kernel buffer.
229 * On success, returns the length of the string INCLUDING the trailing NUL.
231 * If access fails, returns -EFAULT (some data may have been copied
232 * and the trailing NUL added).
234 * If @count is smaller than the length of the string, copies @count-1 bytes,
235 * sets the last byte of @dst buffer to NUL and returns @count.
237 long strncpy_from_user_nofault(char *dst
, const void __user
*unsafe_addr
,
240 mm_segment_t old_fs
= get_fs();
243 if (unlikely(count
<= 0))
248 ret
= strncpy_from_user(dst
, unsafe_addr
, count
);
255 } else if (ret
> 0) {
263 * strnlen_unsafe_user: - Get the size of a user string INCLUDING final NUL.
264 * @unsafe_addr: The string to measure.
265 * @count: Maximum count (including NUL)
267 * Get the size of a NUL-terminated string in user space without pagefault.
269 * Returns the size of the string INCLUDING the terminating NUL.
271 * If the string is too long, returns a number larger than @count. User
272 * has to check the return value against "> count".
273 * On exception (or invalid count), returns 0.
275 * Unlike strnlen_user, this can be used from IRQ handler etc. because
276 * it disables pagefaults.
278 long strnlen_unsafe_user(const void __user
*unsafe_addr
, long count
)
280 mm_segment_t old_fs
= get_fs();
285 ret
= strnlen_user(unsafe_addr
, count
);