Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Ingo Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Access kernel memory without faulting. |
| 4 | */ |
Paul Gortmaker | b95f1b31 | 2011-10-16 02:01:52 -0400 | [diff] [blame] | 5 | #include <linux/export.h> |
Ingo Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame] | 6 | #include <linux/mm.h> |
David Howells | 7c7fcf7 | 2010-10-27 17:29:01 +0100 | [diff] [blame] | 7 | #include <linux/uaccess.h> |
Ingo Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame] | 8 | |
Masami Hiramatsu | 3d70818 | 2019-05-15 14:38:18 +0900 | [diff] [blame] | 9 | static __always_inline long |
| 10 | probe_read_common(void *dst, const void __user *src, size_t size) |
| 11 | { |
| 12 | long ret; |
| 13 | |
| 14 | pagefault_disable(); |
| 15 | ret = __copy_from_user_inatomic(dst, src, size); |
| 16 | pagefault_enable(); |
| 17 | |
| 18 | return ret ? -EFAULT : 0; |
| 19 | } |
| 20 | |
Daniel Borkmann | 1d1585c | 2019-11-02 00:17:56 +0100 | [diff] [blame] | 21 | static __always_inline long |
| 22 | probe_write_common(void __user *dst, const void *src, size_t size) |
| 23 | { |
| 24 | long ret; |
| 25 | |
| 26 | pagefault_disable(); |
| 27 | ret = __copy_to_user_inatomic(dst, src, size); |
| 28 | pagefault_enable(); |
| 29 | |
| 30 | return ret ? -EFAULT : 0; |
| 31 | } |
| 32 | |
Ingo Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame] | 33 | /** |
Masami Hiramatsu | 3d70818 | 2019-05-15 14:38:18 +0900 | [diff] [blame] | 34 | * probe_kernel_read(): safely attempt to read from a kernel-space location |
Ingo Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame] | 35 | * @dst: pointer to the buffer that shall take the data |
| 36 | * @src: address to read from |
| 37 | * @size: size of the data chunk |
| 38 | * |
| 39 | * Safely read from address @src to the buffer at @dst. If a kernel fault |
| 40 | * happens, handle that and return -EFAULT. |
Andrew Morton | 0ab32b6 | 2015-11-05 18:46:03 -0800 | [diff] [blame] | 41 | * |
| 42 | * We ensure that the copy_from_user is executed in atomic context so that |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 43 | * do_page_fault() doesn't attempt to take mmap_lock. This makes |
Andrew Morton | 0ab32b6 | 2015-11-05 18:46:03 -0800 | [diff] [blame] | 44 | * probe_kernel_read() suitable for use within regions where the caller |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 45 | * already holds mmap_lock, or other locks which nest inside mmap_lock. |
Daniel Borkmann | 75a1a60 | 2019-11-02 00:17:57 +0100 | [diff] [blame] | 46 | * |
| 47 | * probe_kernel_read_strict() is the same as probe_kernel_read() except for |
| 48 | * the case where architectures have non-overlapping user and kernel address |
| 49 | * ranges: probe_kernel_read_strict() will additionally return -EFAULT for |
| 50 | * probing memory on a user address range where probe_user_read() is supposed |
| 51 | * to be used instead. |
Ingo Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame] | 52 | */ |
Jason Wessel | 6144a85 | 2010-01-07 11:58:36 -0600 | [diff] [blame] | 53 | |
Steven Rostedt | f29c504 | 2011-05-19 14:35:33 -0400 | [diff] [blame] | 54 | 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] | 55 | __attribute__((alias("__probe_kernel_read"))); |
| 56 | |
Daniel Borkmann | 75a1a60 | 2019-11-02 00:17:57 +0100 | [diff] [blame] | 57 | long __weak probe_kernel_read_strict(void *dst, const void *src, size_t size) |
| 58 | __attribute__((alias("__probe_kernel_read"))); |
| 59 | |
Steven Rostedt | f29c504 | 2011-05-19 14:35:33 -0400 | [diff] [blame] | 60 | long __probe_kernel_read(void *dst, const void *src, size_t size) |
Ingo Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame] | 61 | { |
| 62 | long ret; |
Jason Wessel | b4b8ac5 | 2008-02-20 13:33:38 -0600 | [diff] [blame] | 63 | mm_segment_t old_fs = get_fs(); |
Ingo Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame] | 64 | |
Jason Wessel | b4b8ac5 | 2008-02-20 13:33:38 -0600 | [diff] [blame] | 65 | set_fs(KERNEL_DS); |
Masami Hiramatsu | 3d70818 | 2019-05-15 14:38:18 +0900 | [diff] [blame] | 66 | ret = probe_read_common(dst, (__force const void __user *)src, size); |
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 | |
Masami Hiramatsu | 3d70818 | 2019-05-15 14:38:18 +0900 | [diff] [blame] | 69 | return ret; |
Ingo Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame] | 70 | } |
| 71 | EXPORT_SYMBOL_GPL(probe_kernel_read); |
| 72 | |
| 73 | /** |
Masami Hiramatsu | 3d70818 | 2019-05-15 14:38:18 +0900 | [diff] [blame] | 74 | * probe_user_read(): safely attempt to read from a user-space location |
| 75 | * @dst: pointer to the buffer that shall take the data |
| 76 | * @src: address to read from. This must be a user address. |
| 77 | * @size: size of the data chunk |
| 78 | * |
| 79 | * Safely read from user address @src to the buffer at @dst. If a kernel fault |
| 80 | * happens, handle that and return -EFAULT. |
| 81 | */ |
Christoph Hellwig | 48c49c0 | 2020-06-08 21:34:01 -0700 | [diff] [blame^] | 82 | long probe_user_read(void *dst, const void __user *src, size_t size) |
Masami Hiramatsu | 3d70818 | 2019-05-15 14:38:18 +0900 | [diff] [blame] | 83 | { |
| 84 | long ret = -EFAULT; |
| 85 | mm_segment_t old_fs = get_fs(); |
| 86 | |
| 87 | set_fs(USER_DS); |
| 88 | if (access_ok(src, size)) |
| 89 | ret = probe_read_common(dst, src, size); |
| 90 | set_fs(old_fs); |
| 91 | |
| 92 | return ret; |
| 93 | } |
| 94 | EXPORT_SYMBOL_GPL(probe_user_read); |
| 95 | |
| 96 | /** |
Ingo Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame] | 97 | * probe_kernel_write(): safely attempt to write to a location |
| 98 | * @dst: address to write to |
| 99 | * @src: pointer to the data that shall be written |
| 100 | * @size: size of the data chunk |
| 101 | * |
| 102 | * Safely write to address @dst from the buffer at @src. If a kernel fault |
| 103 | * happens, handle that and return -EFAULT. |
| 104 | */ |
Christoph Hellwig | 48c49c0 | 2020-06-08 21:34:01 -0700 | [diff] [blame^] | 105 | long probe_kernel_write(void *dst, const void *src, size_t size) |
Ingo Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame] | 106 | { |
| 107 | long ret; |
Jason Wessel | b4b8ac5 | 2008-02-20 13:33:38 -0600 | [diff] [blame] | 108 | mm_segment_t old_fs = get_fs(); |
Ingo Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame] | 109 | |
Jason Wessel | b4b8ac5 | 2008-02-20 13:33:38 -0600 | [diff] [blame] | 110 | set_fs(KERNEL_DS); |
Daniel Borkmann | 1d1585c | 2019-11-02 00:17:56 +0100 | [diff] [blame] | 111 | ret = probe_write_common((__force void __user *)dst, src, size); |
Jason Wessel | b4b8ac5 | 2008-02-20 13:33:38 -0600 | [diff] [blame] | 112 | set_fs(old_fs); |
Ingo Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame] | 113 | |
Daniel Borkmann | 1d1585c | 2019-11-02 00:17:56 +0100 | [diff] [blame] | 114 | return ret; |
Ingo Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame] | 115 | } |
Alexei Starovoitov | dbb7ee0 | 2015-08-31 08:57:10 -0700 | [diff] [blame] | 116 | |
Daniel Borkmann | 1d1585c | 2019-11-02 00:17:56 +0100 | [diff] [blame] | 117 | /** |
| 118 | * probe_user_write(): safely attempt to write to a user-space location |
| 119 | * @dst: address to write to |
| 120 | * @src: pointer to the data that shall be written |
| 121 | * @size: size of the data chunk |
| 122 | * |
| 123 | * Safely write to address @dst from the buffer at @src. If a kernel fault |
| 124 | * happens, handle that and return -EFAULT. |
| 125 | */ |
Christoph Hellwig | 48c49c0 | 2020-06-08 21:34:01 -0700 | [diff] [blame^] | 126 | long probe_user_write(void __user *dst, const void *src, size_t size) |
Daniel Borkmann | 1d1585c | 2019-11-02 00:17:56 +0100 | [diff] [blame] | 127 | { |
| 128 | long ret = -EFAULT; |
| 129 | mm_segment_t old_fs = get_fs(); |
| 130 | |
| 131 | set_fs(USER_DS); |
| 132 | if (access_ok(dst, size)) |
| 133 | ret = probe_write_common(dst, src, size); |
| 134 | set_fs(old_fs); |
| 135 | |
| 136 | return ret; |
| 137 | } |
| 138 | EXPORT_SYMBOL_GPL(probe_user_write); |
Masami Hiramatsu | 3d70818 | 2019-05-15 14:38:18 +0900 | [diff] [blame] | 139 | |
Alexei Starovoitov | dbb7ee0 | 2015-08-31 08:57:10 -0700 | [diff] [blame] | 140 | /** |
| 141 | * strncpy_from_unsafe: - Copy a NUL terminated string from unsafe address. |
| 142 | * @dst: Destination address, in kernel space. This buffer must be at |
| 143 | * least @count bytes long. |
Mike Rapoport | f144c39 | 2018-02-06 15:42:16 -0800 | [diff] [blame] | 144 | * @unsafe_addr: Unsafe address. |
Alexei Starovoitov | dbb7ee0 | 2015-08-31 08:57:10 -0700 | [diff] [blame] | 145 | * @count: Maximum number of bytes to copy, including the trailing NUL. |
| 146 | * |
| 147 | * Copies a NUL-terminated string from unsafe address to kernel buffer. |
| 148 | * |
| 149 | * On success, returns the length of the string INCLUDING the trailing NUL. |
| 150 | * |
| 151 | * If access fails, returns -EFAULT (some data may have been copied |
| 152 | * and the trailing NUL added). |
| 153 | * |
| 154 | * If @count is smaller than the length of the string, copies @count-1 bytes, |
| 155 | * sets the last byte of @dst buffer to NUL and returns @count. |
Daniel Borkmann | 75a1a60 | 2019-11-02 00:17:57 +0100 | [diff] [blame] | 156 | * |
| 157 | * strncpy_from_unsafe_strict() is the same as strncpy_from_unsafe() except |
| 158 | * for the case where architectures have non-overlapping user and kernel address |
| 159 | * ranges: strncpy_from_unsafe_strict() will additionally return -EFAULT for |
| 160 | * probing memory on a user address range where strncpy_from_unsafe_user() is |
| 161 | * supposed to be used instead. |
Alexei Starovoitov | dbb7ee0 | 2015-08-31 08:57:10 -0700 | [diff] [blame] | 162 | */ |
Daniel Borkmann | 75a1a60 | 2019-11-02 00:17:57 +0100 | [diff] [blame] | 163 | long __weak strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count) |
| 164 | __attribute__((alias("__strncpy_from_unsafe"))); |
| 165 | |
| 166 | long __weak strncpy_from_unsafe_strict(char *dst, const void *unsafe_addr, |
| 167 | long count) |
| 168 | __attribute__((alias("__strncpy_from_unsafe"))); |
| 169 | |
| 170 | long __strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count) |
Alexei Starovoitov | dbb7ee0 | 2015-08-31 08:57:10 -0700 | [diff] [blame] | 171 | { |
| 172 | mm_segment_t old_fs = get_fs(); |
| 173 | const void *src = unsafe_addr; |
| 174 | long ret; |
| 175 | |
| 176 | if (unlikely(count <= 0)) |
| 177 | return 0; |
| 178 | |
| 179 | set_fs(KERNEL_DS); |
| 180 | pagefault_disable(); |
| 181 | |
| 182 | do { |
Linus Torvalds | bd28b14 | 2016-05-22 17:21:27 -0700 | [diff] [blame] | 183 | ret = __get_user(*dst++, (const char __user __force *)src++); |
Alexei Starovoitov | dbb7ee0 | 2015-08-31 08:57:10 -0700 | [diff] [blame] | 184 | } while (dst[-1] && ret == 0 && src - unsafe_addr < count); |
| 185 | |
| 186 | dst[-1] = '\0'; |
| 187 | pagefault_enable(); |
| 188 | set_fs(old_fs); |
| 189 | |
Rasmus Villemoes | 9dd861d | 2015-11-05 18:50:11 -0800 | [diff] [blame] | 190 | return ret ? -EFAULT : src - unsafe_addr; |
Alexei Starovoitov | dbb7ee0 | 2015-08-31 08:57:10 -0700 | [diff] [blame] | 191 | } |
Masami Hiramatsu | 3d70818 | 2019-05-15 14:38:18 +0900 | [diff] [blame] | 192 | |
| 193 | /** |
| 194 | * strncpy_from_unsafe_user: - Copy a NUL terminated string from unsafe user |
| 195 | * address. |
| 196 | * @dst: Destination address, in kernel space. This buffer must be at |
| 197 | * least @count bytes long. |
| 198 | * @unsafe_addr: Unsafe user address. |
| 199 | * @count: Maximum number of bytes to copy, including the trailing NUL. |
| 200 | * |
| 201 | * Copies a NUL-terminated string from unsafe user address to kernel buffer. |
| 202 | * |
| 203 | * On success, returns the length of the string INCLUDING the trailing NUL. |
| 204 | * |
| 205 | * If access fails, returns -EFAULT (some data may have been copied |
| 206 | * and the trailing NUL added). |
| 207 | * |
| 208 | * If @count is smaller than the length of the string, copies @count-1 bytes, |
| 209 | * sets the last byte of @dst buffer to NUL and returns @count. |
| 210 | */ |
| 211 | long strncpy_from_unsafe_user(char *dst, const void __user *unsafe_addr, |
| 212 | long count) |
| 213 | { |
| 214 | mm_segment_t old_fs = get_fs(); |
| 215 | long ret; |
| 216 | |
| 217 | if (unlikely(count <= 0)) |
| 218 | return 0; |
| 219 | |
| 220 | set_fs(USER_DS); |
| 221 | pagefault_disable(); |
| 222 | ret = strncpy_from_user(dst, unsafe_addr, count); |
| 223 | pagefault_enable(); |
| 224 | set_fs(old_fs); |
| 225 | |
| 226 | if (ret >= count) { |
| 227 | ret = count; |
| 228 | dst[ret - 1] = '\0'; |
| 229 | } else if (ret > 0) { |
| 230 | ret++; |
| 231 | } |
| 232 | |
| 233 | return ret; |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * strnlen_unsafe_user: - Get the size of a user string INCLUDING final NUL. |
| 238 | * @unsafe_addr: The string to measure. |
| 239 | * @count: Maximum count (including NUL) |
| 240 | * |
| 241 | * Get the size of a NUL-terminated string in user space without pagefault. |
| 242 | * |
| 243 | * Returns the size of the string INCLUDING the terminating NUL. |
| 244 | * |
| 245 | * If the string is too long, returns a number larger than @count. User |
| 246 | * has to check the return value against "> count". |
| 247 | * On exception (or invalid count), returns 0. |
| 248 | * |
| 249 | * Unlike strnlen_user, this can be used from IRQ handler etc. because |
| 250 | * it disables pagefaults. |
| 251 | */ |
| 252 | long strnlen_unsafe_user(const void __user *unsafe_addr, long count) |
| 253 | { |
| 254 | mm_segment_t old_fs = get_fs(); |
| 255 | int ret; |
| 256 | |
| 257 | set_fs(USER_DS); |
| 258 | pagefault_disable(); |
| 259 | ret = strnlen_user(unsafe_addr, count); |
| 260 | pagefault_enable(); |
| 261 | set_fs(old_fs); |
| 262 | |
| 263 | return ret; |
| 264 | } |