blob: 78f9274dd49d06f11f87beae9520a0e1267dc3ff [file] [log] [blame]
Ingo Molnarc33fa9f2008-04-17 20:05:36 +02001/*
2 * Access kernel memory without faulting.
3 */
Paul Gortmakerb95f1b312011-10-16 02:01:52 -04004#include <linux/export.h>
Ingo Molnarc33fa9f2008-04-17 20:05:36 +02005#include <linux/mm.h>
David Howells7c7fcf72010-10-27 17:29:01 +01006#include <linux/uaccess.h>
Ingo Molnarc33fa9f2008-04-17 20:05:36 +02007
8/**
9 * probe_kernel_read(): safely attempt to read from a location
10 * @dst: pointer to the buffer that shall take the data
11 * @src: address to read from
12 * @size: size of the data chunk
13 *
14 * Safely read from address @src to the buffer at @dst. If a kernel fault
15 * happens, handle that and return -EFAULT.
Andrew Morton0ab32b62015-11-05 18:46:03 -080016 *
17 * We ensure that the copy_from_user is executed in atomic context so that
18 * do_page_fault() doesn't attempt to take mmap_sem. This makes
19 * probe_kernel_read() suitable for use within regions where the caller
20 * already holds mmap_sem, or other locks which nest inside mmap_sem.
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020021 */
Jason Wessel6144a852010-01-07 11:58:36 -060022
Steven Rostedtf29c5042011-05-19 14:35:33 -040023long __weak probe_kernel_read(void *dst, const void *src, size_t size)
Jason Wessel6144a852010-01-07 11:58:36 -060024 __attribute__((alias("__probe_kernel_read")));
25
Steven Rostedtf29c5042011-05-19 14:35:33 -040026long __probe_kernel_read(void *dst, const void *src, size_t size)
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020027{
28 long ret;
Jason Wesselb4b8ac52008-02-20 13:33:38 -060029 mm_segment_t old_fs = get_fs();
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020030
Jason Wesselb4b8ac52008-02-20 13:33:38 -060031 set_fs(KERNEL_DS);
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020032 pagefault_disable();
33 ret = __copy_from_user_inatomic(dst,
34 (__force const void __user *)src, size);
35 pagefault_enable();
Jason Wesselb4b8ac52008-02-20 13:33:38 -060036 set_fs(old_fs);
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020037
38 return ret ? -EFAULT : 0;
39}
40EXPORT_SYMBOL_GPL(probe_kernel_read);
41
42/**
43 * probe_kernel_write(): safely attempt to write to a location
44 * @dst: address to write to
45 * @src: pointer to the data that shall be written
46 * @size: size of the data chunk
47 *
48 * Safely write to address @dst from the buffer at @src. If a kernel fault
49 * happens, handle that and return -EFAULT.
50 */
Steven Rostedtf29c5042011-05-19 14:35:33 -040051long __weak probe_kernel_write(void *dst, const void *src, size_t size)
Jason Wessel6144a852010-01-07 11:58:36 -060052 __attribute__((alias("__probe_kernel_write")));
53
Steven Rostedtf29c5042011-05-19 14:35:33 -040054long __probe_kernel_write(void *dst, const void *src, size_t size)
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020055{
56 long ret;
Jason Wesselb4b8ac52008-02-20 13:33:38 -060057 mm_segment_t old_fs = get_fs();
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020058
Jason Wesselb4b8ac52008-02-20 13:33:38 -060059 set_fs(KERNEL_DS);
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020060 pagefault_disable();
61 ret = __copy_to_user_inatomic((__force void __user *)dst, src, size);
62 pagefault_enable();
Jason Wesselb4b8ac52008-02-20 13:33:38 -060063 set_fs(old_fs);
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020064
65 return ret ? -EFAULT : 0;
66}
67EXPORT_SYMBOL_GPL(probe_kernel_write);
Alexei Starovoitovdbb7ee02015-08-31 08:57:10 -070068
69/**
70 * strncpy_from_unsafe: - Copy a NUL terminated string from unsafe address.
71 * @dst: Destination address, in kernel space. This buffer must be at
72 * least @count bytes long.
73 * @src: Unsafe address.
74 * @count: Maximum number of bytes to copy, including the trailing NUL.
75 *
76 * Copies a NUL-terminated string from unsafe address to kernel buffer.
77 *
78 * On success, returns the length of the string INCLUDING the trailing NUL.
79 *
80 * If access fails, returns -EFAULT (some data may have been copied
81 * and the trailing NUL added).
82 *
83 * If @count is smaller than the length of the string, copies @count-1 bytes,
84 * sets the last byte of @dst buffer to NUL and returns @count.
85 */
86long strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count)
87{
88 mm_segment_t old_fs = get_fs();
89 const void *src = unsafe_addr;
90 long ret;
91
92 if (unlikely(count <= 0))
93 return 0;
94
95 set_fs(KERNEL_DS);
96 pagefault_disable();
97
98 do {
Linus Torvaldsbd28b142016-05-22 17:21:27 -070099 ret = __get_user(*dst++, (const char __user __force *)src++);
Alexei Starovoitovdbb7ee02015-08-31 08:57:10 -0700100 } while (dst[-1] && ret == 0 && src - unsafe_addr < count);
101
102 dst[-1] = '\0';
103 pagefault_enable();
104 set_fs(old_fs);
105
Rasmus Villemoes9dd861d2015-11-05 18:50:11 -0800106 return ret ? -EFAULT : src - unsafe_addr;
Alexei Starovoitovdbb7ee02015-08-31 08:57:10 -0700107}