blob: a2b22295d8ea30f9546d4174651f0e766c087ddf [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Ingo Molnarc33fa9f2008-04-17 20:05:36 +02002/*
3 * Access kernel memory without faulting.
4 */
Paul Gortmakerb95f1b312011-10-16 02:01:52 -04005#include <linux/export.h>
Ingo Molnarc33fa9f2008-04-17 20:05:36 +02006#include <linux/mm.h>
David Howells7c7fcf72010-10-27 17:29:01 +01007#include <linux/uaccess.h>
Ingo Molnarc33fa9f2008-04-17 20:05:36 +02008
Masami Hiramatsu3d708182019-05-15 14:38:18 +09009static __always_inline long
10probe_read_common(void *dst, const void __user *src, size_t size)
11{
12 long ret;
13
14 pagefault_disable();
15 ret = __copy_from_user_inatomic(dst, src, size);
16 pagefault_enable();
17
18 return ret ? -EFAULT : 0;
19}
20
Daniel Borkmann1d1585c2019-11-02 00:17:56 +010021static __always_inline long
22probe_write_common(void __user *dst, const void *src, size_t size)
23{
24 long ret;
25
26 pagefault_disable();
27 ret = __copy_to_user_inatomic(dst, src, size);
28 pagefault_enable();
29
30 return ret ? -EFAULT : 0;
31}
32
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020033/**
Masami Hiramatsu3d708182019-05-15 14:38:18 +090034 * probe_kernel_read(): safely attempt to read from a kernel-space location
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020035 * @dst: pointer to the buffer that shall take the data
36 * @src: address to read from
37 * @size: size of the data chunk
38 *
39 * Safely read from address @src to the buffer at @dst. If a kernel fault
40 * happens, handle that and return -EFAULT.
Andrew Morton0ab32b62015-11-05 18:46:03 -080041 *
42 * We ensure that the copy_from_user is executed in atomic context so that
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -070043 * do_page_fault() doesn't attempt to take mmap_lock. This makes
Andrew Morton0ab32b62015-11-05 18:46:03 -080044 * probe_kernel_read() suitable for use within regions where the caller
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -070045 * already holds mmap_lock, or other locks which nest inside mmap_lock.
Daniel Borkmann75a1a602019-11-02 00:17:57 +010046 *
47 * probe_kernel_read_strict() is the same as probe_kernel_read() except for
48 * the case where architectures have non-overlapping user and kernel address
49 * ranges: probe_kernel_read_strict() will additionally return -EFAULT for
50 * probing memory on a user address range where probe_user_read() is supposed
51 * to be used instead.
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020052 */
Jason Wessel6144a852010-01-07 11:58:36 -060053
Steven Rostedtf29c5042011-05-19 14:35:33 -040054long __weak probe_kernel_read(void *dst, const void *src, size_t size)
Jason Wessel6144a852010-01-07 11:58:36 -060055 __attribute__((alias("__probe_kernel_read")));
56
Daniel Borkmann75a1a602019-11-02 00:17:57 +010057long __weak probe_kernel_read_strict(void *dst, const void *src, size_t size)
58 __attribute__((alias("__probe_kernel_read")));
59
Steven Rostedtf29c5042011-05-19 14:35:33 -040060long __probe_kernel_read(void *dst, const void *src, size_t size)
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020061{
62 long ret;
Jason Wesselb4b8ac52008-02-20 13:33:38 -060063 mm_segment_t old_fs = get_fs();
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020064
Jason Wesselb4b8ac52008-02-20 13:33:38 -060065 set_fs(KERNEL_DS);
Masami Hiramatsu3d708182019-05-15 14:38:18 +090066 ret = probe_read_common(dst, (__force const void __user *)src, size);
Jason Wesselb4b8ac52008-02-20 13:33:38 -060067 set_fs(old_fs);
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020068
Masami Hiramatsu3d708182019-05-15 14:38:18 +090069 return ret;
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020070}
71EXPORT_SYMBOL_GPL(probe_kernel_read);
72
73/**
Masami Hiramatsu3d708182019-05-15 14:38:18 +090074 * probe_user_read(): safely attempt to read from a user-space location
75 * @dst: pointer to the buffer that shall take the data
76 * @src: address to read from. This must be a user address.
77 * @size: size of the data chunk
78 *
79 * Safely read from user address @src to the buffer at @dst. If a kernel fault
80 * happens, handle that and return -EFAULT.
81 */
82
83long __weak probe_user_read(void *dst, const void __user *src, size_t size)
84 __attribute__((alias("__probe_user_read")));
85
86long __probe_user_read(void *dst, const void __user *src, size_t size)
87{
88 long ret = -EFAULT;
89 mm_segment_t old_fs = get_fs();
90
91 set_fs(USER_DS);
92 if (access_ok(src, size))
93 ret = probe_read_common(dst, src, size);
94 set_fs(old_fs);
95
96 return ret;
97}
98EXPORT_SYMBOL_GPL(probe_user_read);
99
100/**
Ingo Molnarc33fa9f2008-04-17 20:05:36 +0200101 * probe_kernel_write(): safely attempt to write to a location
102 * @dst: address to write to
103 * @src: pointer to the data that shall be written
104 * @size: size of the data chunk
105 *
106 * Safely write to address @dst from the buffer at @src. If a kernel fault
107 * happens, handle that and return -EFAULT.
108 */
Daniel Borkmann1d1585c2019-11-02 00:17:56 +0100109
Steven Rostedtf29c5042011-05-19 14:35:33 -0400110long __weak probe_kernel_write(void *dst, const void *src, size_t size)
Jason Wessel6144a852010-01-07 11:58:36 -0600111 __attribute__((alias("__probe_kernel_write")));
112
Steven Rostedtf29c5042011-05-19 14:35:33 -0400113long __probe_kernel_write(void *dst, const void *src, size_t size)
Ingo Molnarc33fa9f2008-04-17 20:05:36 +0200114{
115 long ret;
Jason Wesselb4b8ac52008-02-20 13:33:38 -0600116 mm_segment_t old_fs = get_fs();
Ingo Molnarc33fa9f2008-04-17 20:05:36 +0200117
Jason Wesselb4b8ac52008-02-20 13:33:38 -0600118 set_fs(KERNEL_DS);
Daniel Borkmann1d1585c2019-11-02 00:17:56 +0100119 ret = probe_write_common((__force void __user *)dst, src, size);
Jason Wesselb4b8ac52008-02-20 13:33:38 -0600120 set_fs(old_fs);
Ingo Molnarc33fa9f2008-04-17 20:05:36 +0200121
Daniel Borkmann1d1585c2019-11-02 00:17:56 +0100122 return ret;
Ingo Molnarc33fa9f2008-04-17 20:05:36 +0200123}
Alexei Starovoitovdbb7ee02015-08-31 08:57:10 -0700124
Daniel Borkmann1d1585c2019-11-02 00:17:56 +0100125/**
126 * probe_user_write(): safely attempt to write to a user-space location
127 * @dst: address to write to
128 * @src: pointer to the data that shall be written
129 * @size: size of the data chunk
130 *
131 * Safely write to address @dst from the buffer at @src. If a kernel fault
132 * happens, handle that and return -EFAULT.
133 */
134
135long __weak probe_user_write(void __user *dst, const void *src, size_t size)
136 __attribute__((alias("__probe_user_write")));
137
138long __probe_user_write(void __user *dst, const void *src, size_t size)
139{
140 long ret = -EFAULT;
141 mm_segment_t old_fs = get_fs();
142
143 set_fs(USER_DS);
144 if (access_ok(dst, size))
145 ret = probe_write_common(dst, src, size);
146 set_fs(old_fs);
147
148 return ret;
149}
150EXPORT_SYMBOL_GPL(probe_user_write);
Masami Hiramatsu3d708182019-05-15 14:38:18 +0900151
Alexei Starovoitovdbb7ee02015-08-31 08:57:10 -0700152/**
153 * strncpy_from_unsafe: - Copy a NUL terminated string from unsafe address.
154 * @dst: Destination address, in kernel space. This buffer must be at
155 * least @count bytes long.
Mike Rapoportf144c392018-02-06 15:42:16 -0800156 * @unsafe_addr: Unsafe address.
Alexei Starovoitovdbb7ee02015-08-31 08:57:10 -0700157 * @count: Maximum number of bytes to copy, including the trailing NUL.
158 *
159 * Copies a NUL-terminated string from unsafe address to kernel buffer.
160 *
161 * On success, returns the length of the string INCLUDING the trailing NUL.
162 *
163 * If access fails, returns -EFAULT (some data may have been copied
164 * and the trailing NUL added).
165 *
166 * If @count is smaller than the length of the string, copies @count-1 bytes,
167 * sets the last byte of @dst buffer to NUL and returns @count.
Daniel Borkmann75a1a602019-11-02 00:17:57 +0100168 *
169 * strncpy_from_unsafe_strict() is the same as strncpy_from_unsafe() except
170 * for the case where architectures have non-overlapping user and kernel address
171 * ranges: strncpy_from_unsafe_strict() will additionally return -EFAULT for
172 * probing memory on a user address range where strncpy_from_unsafe_user() is
173 * supposed to be used instead.
Alexei Starovoitovdbb7ee02015-08-31 08:57:10 -0700174 */
Daniel Borkmann75a1a602019-11-02 00:17:57 +0100175
176long __weak strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count)
177 __attribute__((alias("__strncpy_from_unsafe")));
178
179long __weak strncpy_from_unsafe_strict(char *dst, const void *unsafe_addr,
180 long count)
181 __attribute__((alias("__strncpy_from_unsafe")));
182
183long __strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count)
Alexei Starovoitovdbb7ee02015-08-31 08:57:10 -0700184{
185 mm_segment_t old_fs = get_fs();
186 const void *src = unsafe_addr;
187 long ret;
188
189 if (unlikely(count <= 0))
190 return 0;
191
192 set_fs(KERNEL_DS);
193 pagefault_disable();
194
195 do {
Linus Torvaldsbd28b142016-05-22 17:21:27 -0700196 ret = __get_user(*dst++, (const char __user __force *)src++);
Alexei Starovoitovdbb7ee02015-08-31 08:57:10 -0700197 } while (dst[-1] && ret == 0 && src - unsafe_addr < count);
198
199 dst[-1] = '\0';
200 pagefault_enable();
201 set_fs(old_fs);
202
Rasmus Villemoes9dd861d2015-11-05 18:50:11 -0800203 return ret ? -EFAULT : src - unsafe_addr;
Alexei Starovoitovdbb7ee02015-08-31 08:57:10 -0700204}
Masami Hiramatsu3d708182019-05-15 14:38:18 +0900205
206/**
207 * strncpy_from_unsafe_user: - Copy a NUL terminated string from unsafe user
208 * address.
209 * @dst: Destination address, in kernel space. This buffer must be at
210 * least @count bytes long.
211 * @unsafe_addr: Unsafe user address.
212 * @count: Maximum number of bytes to copy, including the trailing NUL.
213 *
214 * Copies a NUL-terminated string from unsafe user address to kernel buffer.
215 *
216 * On success, returns the length of the string INCLUDING the trailing NUL.
217 *
218 * If access fails, returns -EFAULT (some data may have been copied
219 * and the trailing NUL added).
220 *
221 * If @count is smaller than the length of the string, copies @count-1 bytes,
222 * sets the last byte of @dst buffer to NUL and returns @count.
223 */
224long strncpy_from_unsafe_user(char *dst, const void __user *unsafe_addr,
225 long count)
226{
227 mm_segment_t old_fs = get_fs();
228 long ret;
229
230 if (unlikely(count <= 0))
231 return 0;
232
233 set_fs(USER_DS);
234 pagefault_disable();
235 ret = strncpy_from_user(dst, unsafe_addr, count);
236 pagefault_enable();
237 set_fs(old_fs);
238
239 if (ret >= count) {
240 ret = count;
241 dst[ret - 1] = '\0';
242 } else if (ret > 0) {
243 ret++;
244 }
245
246 return ret;
247}
248
249/**
250 * strnlen_unsafe_user: - Get the size of a user string INCLUDING final NUL.
251 * @unsafe_addr: The string to measure.
252 * @count: Maximum count (including NUL)
253 *
254 * Get the size of a NUL-terminated string in user space without pagefault.
255 *
256 * Returns the size of the string INCLUDING the terminating NUL.
257 *
258 * If the string is too long, returns a number larger than @count. User
259 * has to check the return value against "> count".
260 * On exception (or invalid count), returns 0.
261 *
262 * Unlike strnlen_user, this can be used from IRQ handler etc. because
263 * it disables pagefaults.
264 */
265long strnlen_unsafe_user(const void __user *unsafe_addr, long count)
266{
267 mm_segment_t old_fs = get_fs();
268 int ret;
269
270 set_fs(USER_DS);
271 pagefault_disable();
272 ret = strnlen_user(unsafe_addr, count);
273 pagefault_enable();
274 set_fs(old_fs);
275
276 return ret;
277}