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