2 * Access kernel memory without faulting.
4 #include <linux/export.h>
6 #include <linux/uaccess.h>
8 static __always_inline long
9 probe_read_common(void *dst, const void __user *src, size_t size)
14 ret = __copy_from_user_inatomic(dst, src, size);
17 return ret ? -EFAULT : 0;
20 static __always_inline long
21 probe_write_common(void __user *dst, const void *src, size_t size)
26 ret = __copy_to_user_inatomic(dst, src, size);
29 return ret ? -EFAULT : 0;
33 * probe_kernel_read(): safely attempt to read from a kernel-space location
34 * @dst: pointer to the buffer that shall take the data
35 * @src: address to read from
36 * @size: size of the data chunk
38 * Safely read from address @src to the buffer at @dst. If a kernel fault
39 * happens, handle that and return -EFAULT.
41 * We ensure that the copy_from_user is executed in atomic context so that
42 * do_page_fault() doesn't attempt to take mmap_sem. This makes
43 * probe_kernel_read() suitable for use within regions where the caller
44 * already holds mmap_sem, or other locks which nest inside mmap_sem.
47 long __weak probe_kernel_read(void *dst, const void *src, size_t size)
48 __attribute__((alias("__probe_kernel_read")));
50 long __probe_kernel_read(void *dst, const void *src, size_t size)
53 mm_segment_t old_fs = get_fs();
56 ret = probe_read_common(dst, (__force const void __user *)src, size);
61 EXPORT_SYMBOL_GPL(probe_kernel_read);
64 * probe_user_read(): safely attempt to read from a user-space location
65 * @dst: pointer to the buffer that shall take the data
66 * @src: address to read from. This must be a user address.
67 * @size: size of the data chunk
69 * Safely read from user address @src to the buffer at @dst. If a kernel fault
70 * happens, handle that and return -EFAULT.
73 long __weak probe_user_read(void *dst, const void __user *src, size_t size)
74 __attribute__((alias("__probe_user_read")));
76 long __probe_user_read(void *dst, const void __user *src, size_t size)
79 mm_segment_t old_fs = get_fs();
82 if (access_ok(VERIFY_READ, src, size))
83 ret = probe_read_common(dst, src, size);
88 EXPORT_SYMBOL_GPL(probe_user_read);
91 * probe_kernel_write(): safely attempt to write to a location
92 * @dst: address to write to
93 * @src: pointer to the data that shall be written
94 * @size: size of the data chunk
96 * Safely write to address @dst from the buffer at @src. If a kernel fault
97 * happens, handle that and return -EFAULT.
100 long __weak probe_kernel_write(void *dst, const void *src, size_t size)
101 __attribute__((alias("__probe_kernel_write")));
103 long __probe_kernel_write(void *dst, const void *src, size_t size)
106 mm_segment_t old_fs = get_fs();
109 ret = probe_write_common((__force void __user *)dst, src, size);
114 EXPORT_SYMBOL_GPL(probe_kernel_write);
117 * probe_user_write(): safely attempt to write to a user-space location
118 * @dst: address to write to
119 * @src: pointer to the data that shall be written
120 * @size: size of the data chunk
122 * Safely write to address @dst from the buffer at @src. If a kernel fault
123 * happens, handle that and return -EFAULT.
126 long __weak probe_user_write(void __user *dst, const void *src, size_t size)
127 __attribute__((alias("__probe_user_write")));
129 long __probe_user_write(void __user *dst, const void *src, size_t size)
132 mm_segment_t old_fs = get_fs();
135 if (access_ok(VERIFY_WRITE, dst, size))
136 ret = probe_write_common(dst, src, size);
141 EXPORT_SYMBOL_GPL(probe_user_write);
144 * strncpy_from_unsafe: - Copy a NUL terminated string from unsafe address.
145 * @dst: Destination address, in kernel space. This buffer must be at
146 * least @count bytes long.
147 * @unsafe_addr: Unsafe address.
148 * @count: Maximum number of bytes to copy, including the trailing NUL.
150 * Copies a NUL-terminated string from unsafe address to kernel buffer.
152 * On success, returns the length of the string INCLUDING the trailing NUL.
154 * If access fails, returns -EFAULT (some data may have been copied
155 * and the trailing NUL added).
157 * If @count is smaller than the length of the string, copies @count-1 bytes,
158 * sets the last byte of @dst buffer to NUL and returns @count.
160 long strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count)
162 mm_segment_t old_fs = get_fs();
163 const void *src = unsafe_addr;
166 if (unlikely(count <= 0))
173 ret = __get_user(*dst++, (const char __user __force *)src++);
174 } while (dst[-1] && ret == 0 && src - unsafe_addr < count);
180 return ret ? -EFAULT : src - unsafe_addr;
184 * strncpy_from_unsafe_user: - Copy a NUL terminated string from unsafe user
186 * @dst: Destination address, in kernel space. This buffer must be at
187 * least @count bytes long.
188 * @unsafe_addr: Unsafe user address.
189 * @count: Maximum number of bytes to copy, including the trailing NUL.
191 * Copies a NUL-terminated string from unsafe user address to kernel buffer.
193 * On success, returns the length of the string INCLUDING the trailing NUL.
195 * If access fails, returns -EFAULT (some data may have been copied
196 * and the trailing NUL added).
198 * If @count is smaller than the length of the string, copies @count-1 bytes,
199 * sets the last byte of @dst buffer to NUL and returns @count.
201 long strncpy_from_unsafe_user(char *dst, const void __user *unsafe_addr,
204 mm_segment_t old_fs = get_fs();
207 if (unlikely(count <= 0))
212 ret = strncpy_from_user(dst, unsafe_addr, count);
219 } else if (ret > 0) {
227 * strnlen_unsafe_user: - Get the size of a user string INCLUDING final NUL.
228 * @unsafe_addr: The string to measure.
229 * @count: Maximum count (including NUL)
231 * Get the size of a NUL-terminated string in user space without pagefault.
233 * Returns the size of the string INCLUDING the terminating NUL.
235 * If the string is too long, returns a number larger than @count. User
236 * has to check the return value against "> count".
237 * On exception (or invalid count), returns 0.
239 * Unlike strnlen_user, this can be used from IRQ handler etc. because
240 * it disables pagefaults.
242 long strnlen_unsafe_user(const void __user *unsafe_addr, long count)
244 mm_segment_t old_fs = get_fs();
249 ret = strnlen_user(unsafe_addr, count);