blob: 482d4d670f19eedbb0a9b10719b121bcadebe89f [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
9/**
10 * probe_kernel_read(): safely attempt to read from a location
11 * @dst: pointer to the buffer that shall take the data
12 * @src: address to read from
13 * @size: size of the data chunk
14 *
15 * Safely read from address @src to the buffer at @dst. If a kernel fault
16 * happens, handle that and return -EFAULT.
Andrew Morton0ab32b62015-11-05 18:46:03 -080017 *
18 * We ensure that the copy_from_user is executed in atomic context so that
19 * do_page_fault() doesn't attempt to take mmap_sem. This makes
20 * probe_kernel_read() suitable for use within regions where the caller
21 * already holds mmap_sem, or other locks which nest inside mmap_sem.
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020022 */
Jason Wessel6144a852010-01-07 11:58:36 -060023
Steven Rostedtf29c5042011-05-19 14:35:33 -040024long __weak probe_kernel_read(void *dst, const void *src, size_t size)
Jason Wessel6144a852010-01-07 11:58:36 -060025 __attribute__((alias("__probe_kernel_read")));
26
Steven Rostedtf29c5042011-05-19 14:35:33 -040027long __probe_kernel_read(void *dst, const void *src, size_t size)
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020028{
29 long ret;
Jason Wesselb4b8ac52008-02-20 13:33:38 -060030 mm_segment_t old_fs = get_fs();
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020031
Jason Wesselb4b8ac52008-02-20 13:33:38 -060032 set_fs(KERNEL_DS);
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020033 pagefault_disable();
34 ret = __copy_from_user_inatomic(dst,
35 (__force const void __user *)src, size);
36 pagefault_enable();
Jason Wesselb4b8ac52008-02-20 13:33:38 -060037 set_fs(old_fs);
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020038
39 return ret ? -EFAULT : 0;
40}
41EXPORT_SYMBOL_GPL(probe_kernel_read);
42
43/**
44 * probe_kernel_write(): safely attempt to write to a location
45 * @dst: address to write to
46 * @src: pointer to the data that shall be written
47 * @size: size of the data chunk
48 *
49 * Safely write to address @dst from the buffer at @src. If a kernel fault
50 * happens, handle that and return -EFAULT.
51 */
Steven Rostedtf29c5042011-05-19 14:35:33 -040052long __weak probe_kernel_write(void *dst, const void *src, size_t size)
Jason Wessel6144a852010-01-07 11:58:36 -060053 __attribute__((alias("__probe_kernel_write")));
54
Steven Rostedtf29c5042011-05-19 14:35:33 -040055long __probe_kernel_write(void *dst, const void *src, size_t size)
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020056{
57 long ret;
Jason Wesselb4b8ac52008-02-20 13:33:38 -060058 mm_segment_t old_fs = get_fs();
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020059
Jason Wesselb4b8ac52008-02-20 13:33:38 -060060 set_fs(KERNEL_DS);
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020061 pagefault_disable();
62 ret = __copy_to_user_inatomic((__force void __user *)dst, src, size);
63 pagefault_enable();
Jason Wesselb4b8ac52008-02-20 13:33:38 -060064 set_fs(old_fs);
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020065
66 return ret ? -EFAULT : 0;
67}
68EXPORT_SYMBOL_GPL(probe_kernel_write);
Alexei Starovoitovdbb7ee02015-08-31 08:57:10 -070069
70/**
71 * strncpy_from_unsafe: - Copy a NUL terminated string from unsafe address.
72 * @dst: Destination address, in kernel space. This buffer must be at
73 * least @count bytes long.
Mike Rapoportf144c392018-02-06 15:42:16 -080074 * @unsafe_addr: Unsafe address.
Alexei Starovoitovdbb7ee02015-08-31 08:57:10 -070075 * @count: Maximum number of bytes to copy, including the trailing NUL.
76 *
77 * Copies a NUL-terminated string from unsafe address to kernel buffer.
78 *
79 * On success, returns the length of the string INCLUDING the trailing NUL.
80 *
81 * If access fails, returns -EFAULT (some data may have been copied
82 * and the trailing NUL added).
83 *
84 * If @count is smaller than the length of the string, copies @count-1 bytes,
85 * sets the last byte of @dst buffer to NUL and returns @count.
86 */
87long strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count)
88{
89 mm_segment_t old_fs = get_fs();
90 const void *src = unsafe_addr;
91 long ret;
92
93 if (unlikely(count <= 0))
94 return 0;
95
96 set_fs(KERNEL_DS);
97 pagefault_disable();
98
99 do {
Linus Torvaldsbd28b142016-05-22 17:21:27 -0700100 ret = __get_user(*dst++, (const char __user __force *)src++);
Alexei Starovoitovdbb7ee02015-08-31 08:57:10 -0700101 } while (dst[-1] && ret == 0 && src - unsafe_addr < count);
102
103 dst[-1] = '\0';
104 pagefault_enable();
105 set_fs(old_fs);
106
Rasmus Villemoes9dd861d2015-11-05 18:50:11 -0800107 return ret ? -EFAULT : src - unsafe_addr;
Alexei Starovoitovdbb7ee02015-08-31 08:57:10 -0700108}