Ingo Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Access kernel memory without faulting. |
| 3 | */ |
Paul Gortmaker | b95f1b31 | 2011-10-16 02:01:52 -0400 | [diff] [blame] | 4 | #include <linux/export.h> |
Ingo Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame] | 5 | #include <linux/mm.h> |
David Howells | 7c7fcf7 | 2010-10-27 17:29:01 +0100 | [diff] [blame] | 6 | #include <linux/uaccess.h> |
Ingo Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame] | 7 | |
| 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 Morton | 0ab32b6 | 2015-11-05 18:46:03 -0800 | [diff] [blame] | 16 | * |
| 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 Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame] | 21 | */ |
Jason Wessel | 6144a85 | 2010-01-07 11:58:36 -0600 | [diff] [blame] | 22 | |
Steven Rostedt | f29c504 | 2011-05-19 14:35:33 -0400 | [diff] [blame] | 23 | long __weak probe_kernel_read(void *dst, const void *src, size_t size) |
Jason Wessel | 6144a85 | 2010-01-07 11:58:36 -0600 | [diff] [blame] | 24 | __attribute__((alias("__probe_kernel_read"))); |
| 25 | |
Steven Rostedt | f29c504 | 2011-05-19 14:35:33 -0400 | [diff] [blame] | 26 | long __probe_kernel_read(void *dst, const void *src, size_t size) |
Ingo Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame] | 27 | { |
| 28 | long ret; |
Jason Wessel | b4b8ac5 | 2008-02-20 13:33:38 -0600 | [diff] [blame] | 29 | mm_segment_t old_fs = get_fs(); |
Ingo Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame] | 30 | |
Jason Wessel | b4b8ac5 | 2008-02-20 13:33:38 -0600 | [diff] [blame] | 31 | set_fs(KERNEL_DS); |
Ingo Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame] | 32 | pagefault_disable(); |
Jann Horn | 9da3f2b | 2018-08-28 22:14:20 +0200 | [diff] [blame] | 33 | current->kernel_uaccess_faults_ok++; |
Ingo Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame] | 34 | ret = __copy_from_user_inatomic(dst, |
| 35 | (__force const void __user *)src, size); |
Jann Horn | 9da3f2b | 2018-08-28 22:14:20 +0200 | [diff] [blame] | 36 | current->kernel_uaccess_faults_ok--; |
Ingo Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame] | 37 | pagefault_enable(); |
Jason Wessel | b4b8ac5 | 2008-02-20 13:33:38 -0600 | [diff] [blame] | 38 | set_fs(old_fs); |
Ingo Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame] | 39 | |
| 40 | return ret ? -EFAULT : 0; |
| 41 | } |
| 42 | EXPORT_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 Rostedt | f29c504 | 2011-05-19 14:35:33 -0400 | [diff] [blame] | 53 | long __weak probe_kernel_write(void *dst, const void *src, size_t size) |
Jason Wessel | 6144a85 | 2010-01-07 11:58:36 -0600 | [diff] [blame] | 54 | __attribute__((alias("__probe_kernel_write"))); |
| 55 | |
Steven Rostedt | f29c504 | 2011-05-19 14:35:33 -0400 | [diff] [blame] | 56 | long __probe_kernel_write(void *dst, const void *src, size_t size) |
Ingo Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame] | 57 | { |
| 58 | long ret; |
Jason Wessel | b4b8ac5 | 2008-02-20 13:33:38 -0600 | [diff] [blame] | 59 | mm_segment_t old_fs = get_fs(); |
Ingo Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame] | 60 | |
Jason Wessel | b4b8ac5 | 2008-02-20 13:33:38 -0600 | [diff] [blame] | 61 | set_fs(KERNEL_DS); |
Ingo Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame] | 62 | pagefault_disable(); |
Jann Horn | 9da3f2b | 2018-08-28 22:14:20 +0200 | [diff] [blame] | 63 | current->kernel_uaccess_faults_ok++; |
Ingo Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame] | 64 | ret = __copy_to_user_inatomic((__force void __user *)dst, src, size); |
Jann Horn | 9da3f2b | 2018-08-28 22:14:20 +0200 | [diff] [blame] | 65 | current->kernel_uaccess_faults_ok--; |
Ingo Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame] | 66 | pagefault_enable(); |
Jason Wessel | b4b8ac5 | 2008-02-20 13:33:38 -0600 | [diff] [blame] | 67 | set_fs(old_fs); |
Ingo Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame] | 68 | |
| 69 | return ret ? -EFAULT : 0; |
| 70 | } |
| 71 | EXPORT_SYMBOL_GPL(probe_kernel_write); |
Alexei Starovoitov | dbb7ee0 | 2015-08-31 08:57:10 -0700 | [diff] [blame] | 72 | |
| 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 Rapoport | f144c39 | 2018-02-06 15:42:16 -0800 | [diff] [blame] | 77 | * @unsafe_addr: Unsafe address. |
Alexei Starovoitov | dbb7ee0 | 2015-08-31 08:57:10 -0700 | [diff] [blame] | 78 | * @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 | */ |
| 90 | long 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 Horn | 9da3f2b | 2018-08-28 22:14:20 +0200 | [diff] [blame] | 101 | current->kernel_uaccess_faults_ok++; |
Alexei Starovoitov | dbb7ee0 | 2015-08-31 08:57:10 -0700 | [diff] [blame] | 102 | |
| 103 | do { |
Linus Torvalds | bd28b14 | 2016-05-22 17:21:27 -0700 | [diff] [blame] | 104 | ret = __get_user(*dst++, (const char __user __force *)src++); |
Alexei Starovoitov | dbb7ee0 | 2015-08-31 08:57:10 -0700 | [diff] [blame] | 105 | } while (dst[-1] && ret == 0 && src - unsafe_addr < count); |
| 106 | |
Jann Horn | 9da3f2b | 2018-08-28 22:14:20 +0200 | [diff] [blame] | 107 | current->kernel_uaccess_faults_ok--; |
Alexei Starovoitov | dbb7ee0 | 2015-08-31 08:57:10 -0700 | [diff] [blame] | 108 | dst[-1] = '\0'; |
| 109 | pagefault_enable(); |
| 110 | set_fs(old_fs); |
| 111 | |
Rasmus Villemoes | 9dd861d | 2015-11-05 18:50:11 -0800 | [diff] [blame] | 112 | return ret ? -EFAULT : src - unsafe_addr; |
Alexei Starovoitov | dbb7ee0 | 2015-08-31 08:57:10 -0700 | [diff] [blame] | 113 | } |