blob: f3416632e5a4137c960434c9a59f92a79e0e0204 [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();
Jann Horn9da3f2b2018-08-28 22:14:20 +020033 current->kernel_uaccess_faults_ok++;
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020034 ret = __copy_from_user_inatomic(dst,
35 (__force const void __user *)src, size);
Jann Horn9da3f2b2018-08-28 22:14:20 +020036 current->kernel_uaccess_faults_ok--;
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020037 pagefault_enable();
Jason Wesselb4b8ac52008-02-20 13:33:38 -060038 set_fs(old_fs);
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020039
40 return ret ? -EFAULT : 0;
41}
42EXPORT_SYMBOL_GPL(probe_kernel_read);
43
44/**
45 * probe_kernel_write(): safely attempt to write to a location
46 * @dst: address to write to
47 * @src: pointer to the data that shall be written
48 * @size: size of the data chunk
49 *
50 * Safely write to address @dst from the buffer at @src. If a kernel fault
51 * happens, handle that and return -EFAULT.
52 */
Steven Rostedtf29c5042011-05-19 14:35:33 -040053long __weak probe_kernel_write(void *dst, const void *src, size_t size)
Jason Wessel6144a852010-01-07 11:58:36 -060054 __attribute__((alias("__probe_kernel_write")));
55
Steven Rostedtf29c5042011-05-19 14:35:33 -040056long __probe_kernel_write(void *dst, const void *src, size_t size)
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020057{
58 long ret;
Jason Wesselb4b8ac52008-02-20 13:33:38 -060059 mm_segment_t old_fs = get_fs();
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020060
Jason Wesselb4b8ac52008-02-20 13:33:38 -060061 set_fs(KERNEL_DS);
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020062 pagefault_disable();
Jann Horn9da3f2b2018-08-28 22:14:20 +020063 current->kernel_uaccess_faults_ok++;
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020064 ret = __copy_to_user_inatomic((__force void __user *)dst, src, size);
Jann Horn9da3f2b2018-08-28 22:14:20 +020065 current->kernel_uaccess_faults_ok--;
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020066 pagefault_enable();
Jason Wesselb4b8ac52008-02-20 13:33:38 -060067 set_fs(old_fs);
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020068
69 return ret ? -EFAULT : 0;
70}
71EXPORT_SYMBOL_GPL(probe_kernel_write);
Alexei Starovoitovdbb7ee02015-08-31 08:57:10 -070072
73/**
74 * strncpy_from_unsafe: - Copy a NUL terminated string from unsafe address.
75 * @dst: Destination address, in kernel space. This buffer must be at
76 * least @count bytes long.
Mike Rapoportf144c392018-02-06 15:42:16 -080077 * @unsafe_addr: Unsafe address.
Alexei Starovoitovdbb7ee02015-08-31 08:57:10 -070078 * @count: Maximum number of bytes to copy, including the trailing NUL.
79 *
80 * Copies a NUL-terminated string from unsafe address to kernel buffer.
81 *
82 * On success, returns the length of the string INCLUDING the trailing NUL.
83 *
84 * If access fails, returns -EFAULT (some data may have been copied
85 * and the trailing NUL added).
86 *
87 * If @count is smaller than the length of the string, copies @count-1 bytes,
88 * sets the last byte of @dst buffer to NUL and returns @count.
89 */
90long strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count)
91{
92 mm_segment_t old_fs = get_fs();
93 const void *src = unsafe_addr;
94 long ret;
95
96 if (unlikely(count <= 0))
97 return 0;
98
99 set_fs(KERNEL_DS);
100 pagefault_disable();
Jann Horn9da3f2b2018-08-28 22:14:20 +0200101 current->kernel_uaccess_faults_ok++;
Alexei Starovoitovdbb7ee02015-08-31 08:57:10 -0700102
103 do {
Linus Torvaldsbd28b142016-05-22 17:21:27 -0700104 ret = __get_user(*dst++, (const char __user __force *)src++);
Alexei Starovoitovdbb7ee02015-08-31 08:57:10 -0700105 } while (dst[-1] && ret == 0 && src - unsafe_addr < count);
106
Jann Horn9da3f2b2018-08-28 22:14:20 +0200107 current->kernel_uaccess_faults_ok--;
Alexei Starovoitovdbb7ee02015-08-31 08:57:10 -0700108 dst[-1] = '\0';
109 pagefault_enable();
110 set_fs(old_fs);
111
Rasmus Villemoes9dd861d2015-11-05 18:50:11 -0800112 return ret ? -EFAULT : src - unsafe_addr;
Alexei Starovoitovdbb7ee02015-08-31 08:57:10 -0700113}