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