blob: 2526d30739addf7fc6ac8e66e87bead3d5a85f9e [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/*
Christoph Hellwig3f0acb12020-06-08 21:34:11 -07003 * Access kernel or user memory without faulting.
Ingo Molnarc33fa9f2008-04-17 20:05:36 +02004 */
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
9/**
Christoph Hellwig4f6de122020-06-08 21:34:07 -070010 * probe_kernel_read(): safely attempt to read from any location
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020011 * @dst: pointer to the buffer that shall take the data
12 * @src: address to read from
13 * @size: size of the data chunk
14 *
Christoph Hellwig4f6de122020-06-08 21:34:07 -070015 * Same as probe_kernel_read_strict() except that for architectures with
16 * not fully separated user and kernel address spaces this function also works
17 * for user address tanges.
18 *
19 * DO NOT USE THIS FUNCTION - it is broken on architectures with entirely
20 * separate kernel and user address spaces, and also a bad idea otherwise.
21 */
22long __weak probe_kernel_read(void *dst, const void *src, size_t size)
23 __attribute__((alias("__probe_kernel_read")));
24
25/**
26 * probe_kernel_read_strict(): safely attempt to read from kernel-space
27 * @dst: pointer to the buffer that shall take the data
28 * @src: address to read from
29 * @size: size of the data chunk
30 *
31 * Safely read from kernel address @src to the buffer at @dst. If a kernel
32 * fault happens, handle that and return -EFAULT.
Andrew Morton0ab32b6f2015-11-05 18:46:03 -080033 *
34 * We ensure that the copy_from_user is executed in atomic context so that
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -070035 * do_page_fault() doesn't attempt to take mmap_lock. This makes
Andrew Morton0ab32b6f2015-11-05 18:46:03 -080036 * probe_kernel_read() suitable for use within regions where the caller
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -070037 * already holds mmap_lock, or other locks which nest inside mmap_lock.
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020038 */
Jason Wessel6144a852010-01-07 11:58:36 -060039
Daniel Borkmann75a1a602019-11-02 00:17:57 +010040long __weak probe_kernel_read_strict(void *dst, const void *src, size_t size)
41 __attribute__((alias("__probe_kernel_read")));
42
Steven Rostedtf29c5042011-05-19 14:35:33 -040043long __probe_kernel_read(void *dst, const void *src, size_t size)
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020044{
45 long ret;
Jason Wesselb4b8ac52008-02-20 13:33:38 -060046 mm_segment_t old_fs = get_fs();
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020047
Jason Wesselb4b8ac52008-02-20 13:33:38 -060048 set_fs(KERNEL_DS);
Christoph Hellwigcd030902020-06-08 21:34:24 -070049 pagefault_disable();
50 ret = __copy_from_user_inatomic(dst, (__force const void __user *)src,
51 size);
52 pagefault_enable();
Jason Wesselb4b8ac52008-02-20 13:33:38 -060053 set_fs(old_fs);
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020054
Christoph Hellwigcd030902020-06-08 21:34:24 -070055 if (ret)
56 return -EFAULT;
57 return 0;
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020058}
59EXPORT_SYMBOL_GPL(probe_kernel_read);
60
61/**
Masami Hiramatsu3d708182019-05-15 14:38:18 +090062 * probe_user_read(): safely attempt to read from a user-space location
63 * @dst: pointer to the buffer that shall take the data
64 * @src: address to read from. This must be a user address.
65 * @size: size of the data chunk
66 *
67 * Safely read from user address @src to the buffer at @dst. If a kernel fault
68 * happens, handle that and return -EFAULT.
69 */
Christoph Hellwig48c49c02020-06-08 21:34:01 -070070long probe_user_read(void *dst, const void __user *src, size_t size)
Masami Hiramatsu3d708182019-05-15 14:38:18 +090071{
72 long ret = -EFAULT;
73 mm_segment_t old_fs = get_fs();
74
75 set_fs(USER_DS);
Christoph Hellwigcd030902020-06-08 21:34:24 -070076 if (access_ok(src, size)) {
77 pagefault_disable();
78 ret = __copy_from_user_inatomic(dst, src, size);
79 pagefault_enable();
80 }
Masami Hiramatsu3d708182019-05-15 14:38:18 +090081 set_fs(old_fs);
82
Christoph Hellwigcd030902020-06-08 21:34:24 -070083 if (ret)
84 return -EFAULT;
85 return 0;
Masami Hiramatsu3d708182019-05-15 14:38:18 +090086}
87EXPORT_SYMBOL_GPL(probe_user_read);
88
89/**
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020090 * probe_kernel_write(): safely attempt to write to a location
91 * @dst: address to write to
92 * @src: pointer to the data that shall be written
93 * @size: size of the data chunk
94 *
95 * Safely write to address @dst from the buffer at @src. If a kernel fault
96 * happens, handle that and return -EFAULT.
97 */
Christoph Hellwig48c49c02020-06-08 21:34:01 -070098long probe_kernel_write(void *dst, const void *src, size_t size)
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020099{
100 long ret;
Jason Wesselb4b8ac52008-02-20 13:33:38 -0600101 mm_segment_t old_fs = get_fs();
Ingo Molnarc33fa9f2008-04-17 20:05:36 +0200102
Jason Wesselb4b8ac52008-02-20 13:33:38 -0600103 set_fs(KERNEL_DS);
Christoph Hellwigcd030902020-06-08 21:34:24 -0700104 pagefault_disable();
105 ret = __copy_to_user_inatomic((__force void __user *)dst, src, size);
106 pagefault_enable();
Jason Wesselb4b8ac52008-02-20 13:33:38 -0600107 set_fs(old_fs);
Ingo Molnarc33fa9f2008-04-17 20:05:36 +0200108
Christoph Hellwigcd030902020-06-08 21:34:24 -0700109 if (ret)
110 return -EFAULT;
111 return 0;
Ingo Molnarc33fa9f2008-04-17 20:05:36 +0200112}
Alexei Starovoitovdbb7ee02015-08-31 08:57:10 -0700113
Daniel Borkmann1d1585c2019-11-02 00:17:56 +0100114/**
115 * probe_user_write(): safely attempt to write to a user-space location
116 * @dst: address to write to
117 * @src: pointer to the data that shall be written
118 * @size: size of the data chunk
119 *
120 * Safely write to address @dst from the buffer at @src. If a kernel fault
121 * happens, handle that and return -EFAULT.
122 */
Christoph Hellwig48c49c02020-06-08 21:34:01 -0700123long probe_user_write(void __user *dst, const void *src, size_t size)
Daniel Borkmann1d1585c2019-11-02 00:17:56 +0100124{
125 long ret = -EFAULT;
126 mm_segment_t old_fs = get_fs();
127
128 set_fs(USER_DS);
Christoph Hellwigcd030902020-06-08 21:34:24 -0700129 if (access_ok(dst, size)) {
130 pagefault_disable();
131 ret = __copy_to_user_inatomic(dst, src, size);
132 pagefault_enable();
133 }
Daniel Borkmann1d1585c2019-11-02 00:17:56 +0100134 set_fs(old_fs);
135
Christoph Hellwigcd030902020-06-08 21:34:24 -0700136 if (ret)
137 return -EFAULT;
138 return 0;
Daniel Borkmann1d1585c2019-11-02 00:17:56 +0100139}
140EXPORT_SYMBOL_GPL(probe_user_write);
Masami Hiramatsu3d708182019-05-15 14:38:18 +0900141
Alexei Starovoitovdbb7ee02015-08-31 08:57:10 -0700142/**
143 * strncpy_from_unsafe: - Copy a NUL terminated string from unsafe address.
144 * @dst: Destination address, in kernel space. This buffer must be at
145 * least @count bytes long.
Mike Rapoportf144c392018-02-06 15:42:16 -0800146 * @unsafe_addr: Unsafe address.
Alexei Starovoitovdbb7ee02015-08-31 08:57:10 -0700147 * @count: Maximum number of bytes to copy, including the trailing NUL.
148 *
149 * Copies a NUL-terminated string from unsafe address to kernel buffer.
150 *
151 * On success, returns the length of the string INCLUDING the trailing NUL.
152 *
153 * If access fails, returns -EFAULT (some data may have been copied
154 * and the trailing NUL added).
155 *
156 * If @count is smaller than the length of the string, copies @count-1 bytes,
157 * sets the last byte of @dst buffer to NUL and returns @count.
Daniel Borkmann75a1a602019-11-02 00:17:57 +0100158 *
Christoph Hellwigc4cb1642020-06-08 21:34:17 -0700159 * Same as strncpy_from_kernel_nofault() except that for architectures with
Christoph Hellwig4f6de122020-06-08 21:34:07 -0700160 * not fully separated user and kernel address spaces this function also works
161 * for user address tanges.
162 *
163 * DO NOT USE THIS FUNCTION - it is broken on architectures with entirely
164 * separate kernel and user address spaces, and also a bad idea otherwise.
Alexei Starovoitovdbb7ee02015-08-31 08:57:10 -0700165 */
Daniel Borkmann75a1a602019-11-02 00:17:57 +0100166long __weak strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count)
167 __attribute__((alias("__strncpy_from_unsafe")));
168
Christoph Hellwig4f6de122020-06-08 21:34:07 -0700169/**
Christoph Hellwigc4cb1642020-06-08 21:34:17 -0700170 * strncpy_from_kernel_nofault: - Copy a NUL terminated string from unsafe
Christoph Hellwig4f6de122020-06-08 21:34:07 -0700171 * address.
172 * @dst: Destination address, in kernel space. This buffer must be at
173 * least @count bytes long.
174 * @unsafe_addr: Unsafe address.
175 * @count: Maximum number of bytes to copy, including the trailing NUL.
176 *
177 * Copies a NUL-terminated string from unsafe address to kernel buffer.
178 *
179 * On success, returns the length of the string INCLUDING the trailing NUL.
180 *
181 * If access fails, returns -EFAULT (some data may have been copied
182 * and the trailing NUL added).
183 *
184 * If @count is smaller than the length of the string, copies @count-1 bytes,
185 * sets the last byte of @dst buffer to NUL and returns @count.
186 */
Christoph Hellwigc4cb1642020-06-08 21:34:17 -0700187long __weak strncpy_from_kernel_nofault(char *dst, const void *unsafe_addr,
Daniel Borkmann75a1a602019-11-02 00:17:57 +0100188 long count)
189 __attribute__((alias("__strncpy_from_unsafe")));
190
191long __strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count)
Alexei Starovoitovdbb7ee02015-08-31 08:57:10 -0700192{
193 mm_segment_t old_fs = get_fs();
194 const void *src = unsafe_addr;
195 long ret;
196
197 if (unlikely(count <= 0))
198 return 0;
199
200 set_fs(KERNEL_DS);
201 pagefault_disable();
202
203 do {
Linus Torvaldsbd28b142016-05-22 17:21:27 -0700204 ret = __get_user(*dst++, (const char __user __force *)src++);
Alexei Starovoitovdbb7ee02015-08-31 08:57:10 -0700205 } while (dst[-1] && ret == 0 && src - unsafe_addr < count);
206
207 dst[-1] = '\0';
208 pagefault_enable();
209 set_fs(old_fs);
210
Rasmus Villemoes9dd861d2015-11-05 18:50:11 -0800211 return ret ? -EFAULT : src - unsafe_addr;
Alexei Starovoitovdbb7ee02015-08-31 08:57:10 -0700212}
Masami Hiramatsu3d708182019-05-15 14:38:18 +0900213
214/**
Christoph Hellwigbd88bb52020-06-08 21:34:14 -0700215 * strncpy_from_user_nofault: - Copy a NUL terminated string from unsafe user
Masami Hiramatsu3d708182019-05-15 14:38:18 +0900216 * address.
217 * @dst: Destination address, in kernel space. This buffer must be at
218 * least @count bytes long.
219 * @unsafe_addr: Unsafe user address.
220 * @count: Maximum number of bytes to copy, including the trailing NUL.
221 *
222 * Copies a NUL-terminated string from unsafe user address to kernel buffer.
223 *
224 * On success, returns the length of the string INCLUDING the trailing NUL.
225 *
226 * If access fails, returns -EFAULT (some data may have been copied
227 * and the trailing NUL added).
228 *
229 * If @count is smaller than the length of the string, copies @count-1 bytes,
230 * sets the last byte of @dst buffer to NUL and returns @count.
231 */
Christoph Hellwigbd88bb52020-06-08 21:34:14 -0700232long strncpy_from_user_nofault(char *dst, const void __user *unsafe_addr,
Masami Hiramatsu3d708182019-05-15 14:38:18 +0900233 long count)
234{
235 mm_segment_t old_fs = get_fs();
236 long ret;
237
238 if (unlikely(count <= 0))
239 return 0;
240
241 set_fs(USER_DS);
242 pagefault_disable();
243 ret = strncpy_from_user(dst, unsafe_addr, count);
244 pagefault_enable();
245 set_fs(old_fs);
246
247 if (ret >= count) {
248 ret = count;
249 dst[ret - 1] = '\0';
250 } else if (ret > 0) {
251 ret++;
252 }
253
254 return ret;
255}
256
257/**
Christoph Hellwig02dddb12020-06-08 21:34:20 -0700258 * strnlen_user_nofault: - Get the size of a user string INCLUDING final NUL.
Masami Hiramatsu3d708182019-05-15 14:38:18 +0900259 * @unsafe_addr: The string to measure.
260 * @count: Maximum count (including NUL)
261 *
262 * Get the size of a NUL-terminated string in user space without pagefault.
263 *
264 * Returns the size of the string INCLUDING the terminating NUL.
265 *
266 * If the string is too long, returns a number larger than @count. User
267 * has to check the return value against "> count".
268 * On exception (or invalid count), returns 0.
269 *
270 * Unlike strnlen_user, this can be used from IRQ handler etc. because
271 * it disables pagefaults.
272 */
Christoph Hellwig02dddb12020-06-08 21:34:20 -0700273long strnlen_user_nofault(const void __user *unsafe_addr, long count)
Masami Hiramatsu3d708182019-05-15 14:38:18 +0900274{
275 mm_segment_t old_fs = get_fs();
276 int ret;
277
278 set_fs(USER_DS);
279 pagefault_disable();
280 ret = strnlen_user(unsafe_addr, count);
281 pagefault_enable();
282 set_fs(old_fs);
283
284 return ret;
285}