xref: /openbmc/linux/mm/maccess.c (revision 9dd861d5)
1c33fa9f5SIngo Molnar /*
2c33fa9f5SIngo Molnar  * Access kernel memory without faulting.
3c33fa9f5SIngo Molnar  */
4b95f1b31SPaul Gortmaker #include <linux/export.h>
5c33fa9f5SIngo Molnar #include <linux/mm.h>
67c7fcf76SDavid Howells #include <linux/uaccess.h>
7c33fa9f5SIngo Molnar 
8c33fa9f5SIngo Molnar /**
9c33fa9f5SIngo Molnar  * probe_kernel_read(): safely attempt to read from a location
10c33fa9f5SIngo Molnar  * @dst: pointer to the buffer that shall take the data
11c33fa9f5SIngo Molnar  * @src: address to read from
12c33fa9f5SIngo Molnar  * @size: size of the data chunk
13c33fa9f5SIngo Molnar  *
14c33fa9f5SIngo Molnar  * Safely read from address @src to the buffer at @dst.  If a kernel fault
15c33fa9f5SIngo Molnar  * happens, handle that and return -EFAULT.
160ab32b6fSAndrew Morton  *
170ab32b6fSAndrew Morton  * We ensure that the copy_from_user is executed in atomic context so that
180ab32b6fSAndrew Morton  * do_page_fault() doesn't attempt to take mmap_sem.  This makes
190ab32b6fSAndrew Morton  * probe_kernel_read() suitable for use within regions where the caller
200ab32b6fSAndrew Morton  * already holds mmap_sem, or other locks which nest inside mmap_sem.
21c33fa9f5SIngo Molnar  */
226144a85aSJason Wessel 
23f29c5041SSteven Rostedt long __weak probe_kernel_read(void *dst, const void *src, size_t size)
246144a85aSJason Wessel     __attribute__((alias("__probe_kernel_read")));
256144a85aSJason Wessel 
26f29c5041SSteven Rostedt long __probe_kernel_read(void *dst, const void *src, size_t size)
27c33fa9f5SIngo Molnar {
28c33fa9f5SIngo Molnar 	long ret;
29b4b8ac52SJason Wessel 	mm_segment_t old_fs = get_fs();
30c33fa9f5SIngo Molnar 
31b4b8ac52SJason Wessel 	set_fs(KERNEL_DS);
32c33fa9f5SIngo Molnar 	pagefault_disable();
33c33fa9f5SIngo Molnar 	ret = __copy_from_user_inatomic(dst,
34c33fa9f5SIngo Molnar 			(__force const void __user *)src, size);
35c33fa9f5SIngo Molnar 	pagefault_enable();
36b4b8ac52SJason Wessel 	set_fs(old_fs);
37c33fa9f5SIngo Molnar 
38c33fa9f5SIngo Molnar 	return ret ? -EFAULT : 0;
39c33fa9f5SIngo Molnar }
40c33fa9f5SIngo Molnar EXPORT_SYMBOL_GPL(probe_kernel_read);
41c33fa9f5SIngo Molnar 
42c33fa9f5SIngo Molnar /**
43c33fa9f5SIngo Molnar  * probe_kernel_write(): safely attempt to write to a location
44c33fa9f5SIngo Molnar  * @dst: address to write to
45c33fa9f5SIngo Molnar  * @src: pointer to the data that shall be written
46c33fa9f5SIngo Molnar  * @size: size of the data chunk
47c33fa9f5SIngo Molnar  *
48c33fa9f5SIngo Molnar  * Safely write to address @dst from the buffer at @src.  If a kernel fault
49c33fa9f5SIngo Molnar  * happens, handle that and return -EFAULT.
50c33fa9f5SIngo Molnar  */
51f29c5041SSteven Rostedt long __weak probe_kernel_write(void *dst, const void *src, size_t size)
526144a85aSJason Wessel     __attribute__((alias("__probe_kernel_write")));
536144a85aSJason Wessel 
54f29c5041SSteven Rostedt long __probe_kernel_write(void *dst, const void *src, size_t size)
55c33fa9f5SIngo Molnar {
56c33fa9f5SIngo Molnar 	long ret;
57b4b8ac52SJason Wessel 	mm_segment_t old_fs = get_fs();
58c33fa9f5SIngo Molnar 
59b4b8ac52SJason Wessel 	set_fs(KERNEL_DS);
60c33fa9f5SIngo Molnar 	pagefault_disable();
61c33fa9f5SIngo Molnar 	ret = __copy_to_user_inatomic((__force void __user *)dst, src, size);
62c33fa9f5SIngo Molnar 	pagefault_enable();
63b4b8ac52SJason Wessel 	set_fs(old_fs);
64c33fa9f5SIngo Molnar 
65c33fa9f5SIngo Molnar 	return ret ? -EFAULT : 0;
66c33fa9f5SIngo Molnar }
67c33fa9f5SIngo Molnar EXPORT_SYMBOL_GPL(probe_kernel_write);
68dbb7ee0eSAlexei Starovoitov 
69dbb7ee0eSAlexei Starovoitov /**
70dbb7ee0eSAlexei Starovoitov  * strncpy_from_unsafe: - Copy a NUL terminated string from unsafe address.
71dbb7ee0eSAlexei Starovoitov  * @dst:   Destination address, in kernel space.  This buffer must be at
72dbb7ee0eSAlexei Starovoitov  *         least @count bytes long.
73dbb7ee0eSAlexei Starovoitov  * @src:   Unsafe address.
74dbb7ee0eSAlexei Starovoitov  * @count: Maximum number of bytes to copy, including the trailing NUL.
75dbb7ee0eSAlexei Starovoitov  *
76dbb7ee0eSAlexei Starovoitov  * Copies a NUL-terminated string from unsafe address to kernel buffer.
77dbb7ee0eSAlexei Starovoitov  *
78dbb7ee0eSAlexei Starovoitov  * On success, returns the length of the string INCLUDING the trailing NUL.
79dbb7ee0eSAlexei Starovoitov  *
80dbb7ee0eSAlexei Starovoitov  * If access fails, returns -EFAULT (some data may have been copied
81dbb7ee0eSAlexei Starovoitov  * and the trailing NUL added).
82dbb7ee0eSAlexei Starovoitov  *
83dbb7ee0eSAlexei Starovoitov  * If @count is smaller than the length of the string, copies @count-1 bytes,
84dbb7ee0eSAlexei Starovoitov  * sets the last byte of @dst buffer to NUL and returns @count.
85dbb7ee0eSAlexei Starovoitov  */
86dbb7ee0eSAlexei Starovoitov long strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count)
87dbb7ee0eSAlexei Starovoitov {
88dbb7ee0eSAlexei Starovoitov 	mm_segment_t old_fs = get_fs();
89dbb7ee0eSAlexei Starovoitov 	const void *src = unsafe_addr;
90dbb7ee0eSAlexei Starovoitov 	long ret;
91dbb7ee0eSAlexei Starovoitov 
92dbb7ee0eSAlexei Starovoitov 	if (unlikely(count <= 0))
93dbb7ee0eSAlexei Starovoitov 		return 0;
94dbb7ee0eSAlexei Starovoitov 
95dbb7ee0eSAlexei Starovoitov 	set_fs(KERNEL_DS);
96dbb7ee0eSAlexei Starovoitov 	pagefault_disable();
97dbb7ee0eSAlexei Starovoitov 
98dbb7ee0eSAlexei Starovoitov 	do {
99dbb7ee0eSAlexei Starovoitov 		ret = __copy_from_user_inatomic(dst++,
100dbb7ee0eSAlexei Starovoitov 						(const void __user __force *)src++, 1);
101dbb7ee0eSAlexei Starovoitov 	} while (dst[-1] && ret == 0 && src - unsafe_addr < count);
102dbb7ee0eSAlexei Starovoitov 
103dbb7ee0eSAlexei Starovoitov 	dst[-1] = '\0';
104dbb7ee0eSAlexei Starovoitov 	pagefault_enable();
105dbb7ee0eSAlexei Starovoitov 	set_fs(old_fs);
106dbb7ee0eSAlexei Starovoitov 
1079dd861d5SRasmus Villemoes 	return ret ? -EFAULT : src - unsafe_addr;
108dbb7ee0eSAlexei Starovoitov }
109