Commit | Line | Data |
---|---|---|
e886bf9d | 1 | // SPDX-License-Identifier: GPL-2.0 |
0b24becc | 2 | /* |
59fd51b2 | 3 | * This file contains common KASAN error reporting code. |
0b24becc AR |
4 | * |
5 | * Copyright (c) 2014 Samsung Electronics Co., Ltd. | |
2baf9e89 | 6 | * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com> |
0b24becc | 7 | * |
5d0926ef | 8 | * Some code borrowed from https://github.com/xairy/kasan-prototype by |
5f21f3a8 | 9 | * Andrey Konovalov <andreyknvl@gmail.com> |
0b24becc AR |
10 | */ |
11 | ||
c8c7016f | 12 | #include <kunit/test.h> |
b0845ce5 | 13 | #include <linux/bitops.h> |
4f40c6e5 | 14 | #include <linux/ftrace.h> |
b0845ce5 | 15 | #include <linux/init.h> |
0b24becc | 16 | #include <linux/kernel.h> |
c32caa26 | 17 | #include <linux/lockdep.h> |
0b24becc AR |
18 | #include <linux/mm.h> |
19 | #include <linux/printk.h> | |
20 | #include <linux/sched.h> | |
21 | #include <linux/slab.h> | |
cd11016e | 22 | #include <linux/stackdepot.h> |
0b24becc AR |
23 | #include <linux/stacktrace.h> |
24 | #include <linux/string.h> | |
25 | #include <linux/types.h> | |
26 | #include <linux/kasan.h> | |
527f215b | 27 | #include <linux/module.h> |
e8969219 | 28 | #include <linux/sched/task_stack.h> |
4fba3758 | 29 | #include <linux/uaccess.h> |
d3a61f74 | 30 | #include <trace/events/error_report.h> |
0b24becc | 31 | |
bebf56a1 AR |
32 | #include <asm/sections.h> |
33 | ||
0b24becc | 34 | #include "kasan.h" |
0316bec2 | 35 | #include "../slab.h" |
0b24becc | 36 | |
11cd3cd6 | 37 | static unsigned long kasan_flags; |
0b24becc | 38 | |
11cd3cd6 AK |
39 | #define KASAN_BIT_REPORTED 0 |
40 | #define KASAN_BIT_MULTI_SHOT 1 | |
5e82cd12 | 41 | |
c9d1af2b WL |
42 | enum kasan_arg_fault { |
43 | KASAN_ARG_FAULT_DEFAULT, | |
44 | KASAN_ARG_FAULT_REPORT, | |
45 | KASAN_ARG_FAULT_PANIC, | |
46 | }; | |
47 | ||
48 | static enum kasan_arg_fault kasan_arg_fault __ro_after_init = KASAN_ARG_FAULT_DEFAULT; | |
49 | ||
50 | /* kasan.fault=report/panic */ | |
51 | static int __init early_kasan_fault(char *arg) | |
52 | { | |
53 | if (!arg) | |
54 | return -EINVAL; | |
55 | ||
56 | if (!strcmp(arg, "report")) | |
57 | kasan_arg_fault = KASAN_ARG_FAULT_REPORT; | |
58 | else if (!strcmp(arg, "panic")) | |
59 | kasan_arg_fault = KASAN_ARG_FAULT_PANIC; | |
60 | else | |
61 | return -EINVAL; | |
62 | ||
63 | return 0; | |
64 | } | |
65 | early_param("kasan.fault", early_kasan_fault); | |
66 | ||
11cd3cd6 | 67 | static int __init kasan_set_multi_shot(char *str) |
7d418f7b | 68 | { |
11cd3cd6 AK |
69 | set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags); |
70 | return 1; | |
7d418f7b | 71 | } |
11cd3cd6 | 72 | __setup("kasan_multi_shot", kasan_set_multi_shot); |
7d418f7b | 73 | |
865bfa28 | 74 | /* |
c6a690e0 AK |
75 | * This function is used to check whether KASAN reports are suppressed for |
76 | * software KASAN modes via kasan_disable/enable_current() critical sections. | |
77 | * | |
78 | * This is done to avoid: | |
79 | * 1. False-positive reports when accessing slab metadata, | |
80 | * 2. Deadlocking when poisoned memory is accessed by the reporting code. | |
81 | * | |
82 | * Hardware Tag-Based KASAN instead relies on: | |
83 | * For #1: Resetting tags via kasan_reset_tag(). | |
84 | * For #2: Suppression of tag checks via CPU, see report_suppress_start/end(). | |
865bfa28 | 85 | */ |
c6a690e0 | 86 | static bool report_suppressed_sw(void) |
5e82cd12 | 87 | { |
865bfa28 AK |
88 | #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS) |
89 | if (current->kasan_depth) | |
90 | return true; | |
91 | #endif | |
92 | return false; | |
93 | } | |
31c65110 | 94 | |
c6a690e0 AK |
95 | static void report_suppress_start(void) |
96 | { | |
97 | #ifdef CONFIG_KASAN_HW_TAGS | |
98 | /* | |
99 | * Disable preemption for the duration of printing a KASAN report, as | |
100 | * hw_suppress_tag_checks_start() disables checks on the current CPU. | |
101 | */ | |
102 | preempt_disable(); | |
103 | hw_suppress_tag_checks_start(); | |
104 | #else | |
105 | kasan_disable_current(); | |
106 | #endif | |
107 | } | |
108 | ||
109 | static void report_suppress_stop(void) | |
110 | { | |
111 | #ifdef CONFIG_KASAN_HW_TAGS | |
112 | hw_suppress_tag_checks_stop(); | |
113 | preempt_enable(); | |
114 | #else | |
115 | kasan_enable_current(); | |
116 | #endif | |
117 | } | |
118 | ||
865bfa28 AK |
119 | /* |
120 | * Used to avoid reporting more than one KASAN bug unless kasan_multi_shot | |
121 | * is enabled. Note that KASAN tests effectively enable kasan_multi_shot | |
122 | * for their duration. | |
123 | */ | |
124 | static bool report_enabled(void) | |
125 | { | |
126 | if (test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags)) | |
127 | return true; | |
128 | return !test_and_set_bit(KASAN_BIT_REPORTED, &kasan_flags); | |
0b24becc AR |
129 | } |
130 | ||
80207910 AK |
131 | #if IS_ENABLED(CONFIG_KASAN_KUNIT_TEST) || IS_ENABLED(CONFIG_KASAN_MODULE_TEST) |
132 | ||
133 | bool kasan_save_enable_multi_shot(void) | |
134 | { | |
135 | return test_and_set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags); | |
136 | } | |
137 | EXPORT_SYMBOL_GPL(kasan_save_enable_multi_shot); | |
138 | ||
139 | void kasan_restore_multi_shot(bool enabled) | |
140 | { | |
141 | if (!enabled) | |
142 | clear_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags); | |
143 | } | |
144 | EXPORT_SYMBOL_GPL(kasan_restore_multi_shot); | |
145 | ||
146 | #endif | |
147 | ||
c8c7016f AK |
148 | #if IS_ENABLED(CONFIG_KASAN_KUNIT_TEST) |
149 | ||
150 | /* | |
151 | * Whether the KASAN KUnit test suite is currently being executed. | |
152 | * Updated in kasan_test.c. | |
153 | */ | |
0de4a7f5 | 154 | static bool kasan_kunit_executing; |
c8c7016f AK |
155 | |
156 | void kasan_kunit_test_suite_start(void) | |
157 | { | |
158 | WRITE_ONCE(kasan_kunit_executing, true); | |
159 | } | |
160 | EXPORT_SYMBOL_GPL(kasan_kunit_test_suite_start); | |
161 | ||
162 | void kasan_kunit_test_suite_end(void) | |
163 | { | |
164 | WRITE_ONCE(kasan_kunit_executing, false); | |
165 | } | |
166 | EXPORT_SYMBOL_GPL(kasan_kunit_test_suite_end); | |
167 | ||
168 | static bool kasan_kunit_test_suite_executing(void) | |
169 | { | |
170 | return READ_ONCE(kasan_kunit_executing); | |
171 | } | |
172 | ||
173 | #else /* CONFIG_KASAN_KUNIT_TEST */ | |
174 | ||
175 | static inline bool kasan_kunit_test_suite_executing(void) { return false; } | |
176 | ||
177 | #endif /* CONFIG_KASAN_KUNIT_TEST */ | |
178 | ||
179 | #if IS_ENABLED(CONFIG_KUNIT) | |
180 | ||
181 | static void fail_non_kasan_kunit_test(void) | |
182 | { | |
183 | struct kunit *test; | |
184 | ||
185 | if (kasan_kunit_test_suite_executing()) | |
186 | return; | |
187 | ||
188 | test = current->kunit_test; | |
189 | if (test) | |
190 | kunit_set_failure(test); | |
191 | } | |
192 | ||
193 | #else /* CONFIG_KUNIT */ | |
194 | ||
195 | static inline void fail_non_kasan_kunit_test(void) { } | |
196 | ||
197 | #endif /* CONFIG_KUNIT */ | |
198 | ||
7e088978 AR |
199 | static DEFINE_SPINLOCK(report_lock); |
200 | ||
a260d281 | 201 | static void start_report(unsigned long *flags, bool sync) |
7e088978 | 202 | { |
c8c7016f | 203 | fail_non_kasan_kunit_test(); |
0a6e8a07 AK |
204 | /* Respect the /proc/sys/kernel/traceoff_on_warning interface. */ |
205 | disable_trace_on_warning(); | |
c32caa26 AK |
206 | /* Do not allow LOCKDEP mangling KASAN reports. */ |
207 | lockdep_off(); | |
a260d281 | 208 | /* Make sure we don't end up in loop. */ |
c6a690e0 | 209 | report_suppress_start(); |
7e088978 AR |
210 | spin_lock_irqsave(&report_lock, *flags); |
211 | pr_err("==================================================================\n"); | |
212 | } | |
213 | ||
9d7b7dd9 | 214 | static void end_report(unsigned long *flags, void *addr) |
7e088978 | 215 | { |
476b1dc2 | 216 | if (addr) |
9d7b7dd9 AK |
217 | trace_error_report_end(ERROR_DETECTOR_KASAN, |
218 | (unsigned long)addr); | |
7e088978 | 219 | pr_err("==================================================================\n"); |
7e088978 | 220 | spin_unlock_irqrestore(&report_lock, *flags); |
79cc1ba7 KC |
221 | if (!test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags)) |
222 | check_panic_on_warn("KASAN"); | |
c9d1af2b | 223 | if (kasan_arg_fault == KASAN_ARG_FAULT_PANIC) |
8028caac | 224 | panic("kasan.fault=panic set ...\n"); |
c32caa26 AK |
225 | add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); |
226 | lockdep_on(); | |
c6a690e0 | 227 | report_suppress_stop(); |
7e088978 AR |
228 | } |
229 | ||
865bfa28 AK |
230 | static void print_error_description(struct kasan_report_info *info) |
231 | { | |
dcc57966 | 232 | pr_err("BUG: KASAN: %s in %pS\n", info->bug_type, (void *)info->ip); |
3de0de75 | 233 | |
dcc57966 AK |
234 | if (info->type != KASAN_REPORT_ACCESS) { |
235 | pr_err("Free of addr %px by task %s/%d\n", | |
236 | info->access_addr, current->comm, task_pid_nr(current)); | |
865bfa28 AK |
237 | return; |
238 | } | |
239 | ||
865bfa28 AK |
240 | if (info->access_size) |
241 | pr_err("%s of size %zu at addr %px by task %s/%d\n", | |
242 | info->is_write ? "Write" : "Read", info->access_size, | |
243 | info->access_addr, current->comm, task_pid_nr(current)); | |
244 | else | |
245 | pr_err("%s at addr %px by task %s/%d\n", | |
246 | info->is_write ? "Write" : "Read", | |
247 | info->access_addr, current->comm, task_pid_nr(current)); | |
248 | } | |
249 | ||
b6b72f49 | 250 | static void print_track(struct kasan_track *track, const char *prefix) |
7ed2f9e6 | 251 | { |
b6b72f49 | 252 | pr_err("%s by task %u:\n", prefix, track->pid); |
9ef08d26 | 253 | if (track->stack) |
505be481 | 254 | stack_depot_print(track->stack); |
9ef08d26 | 255 | else |
cd11016e | 256 | pr_err("(stack is not available)\n"); |
7ed2f9e6 AP |
257 | } |
258 | ||
559756e8 | 259 | static inline struct page *addr_to_page(const void *addr) |
db429f16 | 260 | { |
2c9fb1fd | 261 | if (virt_addr_valid(addr)) |
db429f16 AK |
262 | return virt_to_head_page(addr); |
263 | return NULL; | |
264 | } | |
265 | ||
8f17febb | 266 | static void describe_object_addr(const void *addr, struct kasan_report_info *info) |
7ed2f9e6 | 267 | { |
0c06f1f8 | 268 | unsigned long access_addr = (unsigned long)addr; |
8f17febb KYL |
269 | unsigned long object_addr = (unsigned long)info->object; |
270 | const char *rel_type, *region_state = ""; | |
0c06f1f8 | 271 | int rel_bytes; |
7ed2f9e6 | 272 | |
6424f6bb | 273 | pr_err("The buggy address belongs to the object at %px\n" |
0c06f1f8 | 274 | " which belongs to the cache %s of size %d\n", |
8f17febb | 275 | info->object, info->cache->name, info->cache->object_size); |
47b5c2a0 | 276 | |
0c06f1f8 AK |
277 | if (access_addr < object_addr) { |
278 | rel_type = "to the left"; | |
279 | rel_bytes = object_addr - access_addr; | |
8f17febb | 280 | } else if (access_addr >= object_addr + info->alloc_size) { |
0c06f1f8 | 281 | rel_type = "to the right"; |
8f17febb | 282 | rel_bytes = access_addr - (object_addr + info->alloc_size); |
0c06f1f8 AK |
283 | } else { |
284 | rel_type = "inside"; | |
285 | rel_bytes = access_addr - object_addr; | |
286 | } | |
287 | ||
8f17febb KYL |
288 | /* |
289 | * Tag-Based modes use the stack ring to infer the bug type, but the | |
290 | * memory region state description is generated based on the metadata. | |
291 | * Thus, defining the region state as below can contradict the metadata. | |
292 | * Fixing this requires further improvements, so only infer the state | |
293 | * for the Generic mode. | |
294 | */ | |
295 | if (IS_ENABLED(CONFIG_KASAN_GENERIC)) { | |
296 | if (strcmp(info->bug_type, "slab-out-of-bounds") == 0) | |
297 | region_state = "allocated "; | |
298 | else if (strcmp(info->bug_type, "slab-use-after-free") == 0) | |
299 | region_state = "freed "; | |
300 | } | |
301 | ||
0c06f1f8 | 302 | pr_err("The buggy address is located %d bytes %s of\n" |
8f17febb KYL |
303 | " %s%zu-byte region [%px, %px)\n", |
304 | rel_bytes, rel_type, region_state, info->alloc_size, | |
305 | (void *)object_addr, (void *)(object_addr + info->alloc_size)); | |
0c06f1f8 AK |
306 | } |
307 | ||
59e6e098 | 308 | static void describe_object_stacks(struct kasan_report_info *info) |
0c06f1f8 | 309 | { |
59e6e098 AK |
310 | if (info->alloc_track.stack) { |
311 | print_track(&info->alloc_track, "Allocated"); | |
b1938599 | 312 | pr_err("\n"); |
97593cad AK |
313 | } |
314 | ||
59e6e098 AK |
315 | if (info->free_track.stack) { |
316 | print_track(&info->free_track, "Freed"); | |
97593cad AK |
317 | pr_err("\n"); |
318 | } | |
26e760c9 | 319 | |
92a38eac | 320 | kasan_print_aux_stacks(info->cache, info->object); |
8028caac | 321 | } |
0c06f1f8 | 322 | |
59e6e098 | 323 | static void describe_object(const void *addr, struct kasan_report_info *info) |
8028caac AK |
324 | { |
325 | if (kasan_stack_collection_enabled()) | |
59e6e098 | 326 | describe_object_stacks(info); |
8f17febb | 327 | describe_object_addr(addr, info); |
7ed2f9e6 | 328 | } |
7ed2f9e6 | 329 | |
11cd3cd6 AK |
330 | static inline bool kernel_or_module_addr(const void *addr) |
331 | { | |
3298cbe8 | 332 | if (is_kernel((unsigned long)addr)) |
11cd3cd6 AK |
333 | return true; |
334 | if (is_module_address((unsigned long)addr)) | |
335 | return true; | |
336 | return false; | |
337 | } | |
338 | ||
339 | static inline bool init_task_stack_addr(const void *addr) | |
340 | { | |
341 | return addr >= (void *)&init_thread_union.stack && | |
342 | (addr <= (void *)&init_thread_union.stack + | |
343 | sizeof(init_thread_union.stack)); | |
344 | } | |
345 | ||
7fae3dd0 AK |
346 | static void print_address_description(void *addr, u8 tag, |
347 | struct kasan_report_info *info) | |
0b24becc | 348 | { |
559756e8 | 349 | struct page *page = addr_to_page(addr); |
b8c73fc2 | 350 | |
336abff6 | 351 | dump_stack_lvl(KERN_ERR); |
b1938599 | 352 | pr_err("\n"); |
db429f16 | 353 | |
7fae3dd0 | 354 | if (info->cache && info->object) { |
59e6e098 | 355 | describe_object(addr, info); |
038fd2b4 | 356 | pr_err("\n"); |
b8c73fc2 AR |
357 | } |
358 | ||
430a05f9 AK |
359 | if (kernel_or_module_addr(addr) && !init_task_stack_addr(addr)) { |
360 | pr_err("The buggy address belongs to the variable:\n"); | |
361 | pr_err(" %pS\n", addr); | |
038fd2b4 | 362 | pr_err("\n"); |
430a05f9 AK |
363 | } |
364 | ||
0f9b35f3 AK |
365 | if (object_is_on_stack(addr)) { |
366 | /* | |
367 | * Currently, KASAN supports printing frame information only | |
368 | * for accesses to the task's own stack. | |
369 | */ | |
370 | kasan_print_address_stack_frame(addr); | |
371 | pr_err("\n"); | |
372 | } | |
373 | ||
c056a364 AK |
374 | if (is_vmalloc_addr(addr)) { |
375 | struct vm_struct *va = find_vm_area(addr); | |
376 | ||
377 | if (va) { | |
378 | pr_err("The buggy address belongs to the virtual mapping at\n" | |
379 | " [%px, %px) created by:\n" | |
380 | " %pS\n", | |
381 | va->addr, va->addr + va->size, va->caller); | |
038fd2b4 | 382 | pr_err("\n"); |
c056a364 | 383 | |
fbf4df06 | 384 | page = vmalloc_to_page(addr); |
c056a364 AK |
385 | } |
386 | } | |
387 | ||
430a05f9 | 388 | if (page) { |
c056a364 | 389 | pr_err("The buggy address belongs to the physical page:\n"); |
430a05f9 | 390 | dump_page(page, "kasan: bad access detected"); |
038fd2b4 | 391 | pr_err("\n"); |
bebf56a1 | 392 | } |
0b24becc AR |
393 | } |
394 | ||
96e0279d | 395 | static bool meta_row_is_guilty(const void *row, const void *addr) |
0b24becc | 396 | { |
96e0279d | 397 | return (row <= addr) && (addr < row + META_MEM_BYTES_PER_ROW); |
0b24becc AR |
398 | } |
399 | ||
96e0279d | 400 | static int meta_pointer_offset(const void *row, const void *addr) |
0b24becc | 401 | { |
96e0279d AK |
402 | /* |
403 | * Memory state around the buggy address: | |
404 | * ff00ff00ff00ff00: 00 00 00 05 fe fe fe fe fe fe fe fe fe fe fe fe | |
405 | * ... | |
406 | * | |
407 | * The length of ">ff00ff00ff00ff00: " is | |
408 | * 3 + (BITS_PER_LONG / 8) * 2 chars. | |
409 | * The length of each granule metadata is 2 bytes | |
410 | * plus 1 byte for space. | |
0b24becc | 411 | */ |
96e0279d AK |
412 | return 3 + (BITS_PER_LONG / 8) * 2 + |
413 | (addr - row) / KASAN_GRANULE_SIZE * 3 + 1; | |
0b24becc AR |
414 | } |
415 | ||
db3de8f7 | 416 | static void print_memory_metadata(const void *addr) |
0b24becc AR |
417 | { |
418 | int i; | |
96e0279d | 419 | void *row; |
0b24becc | 420 | |
96e0279d AK |
421 | row = (void *)round_down((unsigned long)addr, META_MEM_BYTES_PER_ROW) |
422 | - META_ROWS_AROUND_ADDR * META_MEM_BYTES_PER_ROW; | |
0b24becc AR |
423 | |
424 | pr_err("Memory state around the buggy address:\n"); | |
425 | ||
88b86597 | 426 | for (i = -META_ROWS_AROUND_ADDR; i <= META_ROWS_AROUND_ADDR; i++) { |
96e0279d AK |
427 | char buffer[4 + (BITS_PER_LONG / 8) * 2]; |
428 | char metadata[META_BYTES_PER_ROW]; | |
0b24becc AR |
429 | |
430 | snprintf(buffer, sizeof(buffer), | |
96e0279d AK |
431 | (i == 0) ? ">%px: " : " %px: ", row); |
432 | ||
f2377d4e AK |
433 | /* |
434 | * We should not pass a shadow pointer to generic | |
435 | * function, because generic functions may try to | |
436 | * access kasan mapping for the passed address. | |
437 | */ | |
f00748bf | 438 | kasan_metadata_fetch_row(&metadata[0], row); |
96e0279d | 439 | |
0b24becc | 440 | print_hex_dump(KERN_ERR, buffer, |
88b86597 | 441 | DUMP_PREFIX_NONE, META_BYTES_PER_ROW, 1, |
96e0279d | 442 | metadata, META_BYTES_PER_ROW, 0); |
0b24becc | 443 | |
96e0279d AK |
444 | if (meta_row_is_guilty(row, addr)) |
445 | pr_err("%*c\n", meta_pointer_offset(row, addr), '^'); | |
0b24becc | 446 | |
96e0279d | 447 | row += META_MEM_BYTES_PER_ROW; |
0b24becc AR |
448 | } |
449 | } | |
450 | ||
c965cdd6 | 451 | static void print_report(struct kasan_report_info *info) |
9d7b7dd9 | 452 | { |
a794898a AK |
453 | void *addr = kasan_reset_tag(info->access_addr); |
454 | u8 tag = get_tag(info->access_addr); | |
9d7b7dd9 AK |
455 | |
456 | print_error_description(info); | |
a794898a | 457 | if (addr_has_metadata(addr)) |
9d7b7dd9 AK |
458 | kasan_print_tags(tag, info->first_bad_addr); |
459 | pr_err("\n"); | |
460 | ||
a794898a | 461 | if (addr_has_metadata(addr)) { |
7fae3dd0 | 462 | print_address_description(addr, tag, info); |
9d7b7dd9 AK |
463 | print_memory_metadata(info->first_bad_addr); |
464 | } else { | |
465 | dump_stack_lvl(KERN_ERR); | |
466 | } | |
467 | } | |
468 | ||
015b109f AK |
469 | static void complete_report_info(struct kasan_report_info *info) |
470 | { | |
471 | void *addr = kasan_reset_tag(info->access_addr); | |
7fae3dd0 | 472 | struct slab *slab; |
015b109f AK |
473 | |
474 | if (info->type == KASAN_REPORT_ACCESS) | |
475 | info->first_bad_addr = kasan_find_first_bad_addr( | |
476 | info->access_addr, info->access_size); | |
477 | else | |
478 | info->first_bad_addr = addr; | |
7fae3dd0 AK |
479 | |
480 | slab = kasan_addr_to_slab(addr); | |
481 | if (slab) { | |
482 | info->cache = slab->slab_cache; | |
483 | info->object = nearest_obj(info->cache, slab, addr); | |
8f17febb KYL |
484 | |
485 | /* Try to determine allocation size based on the metadata. */ | |
486 | info->alloc_size = kasan_get_alloc_size(info->object, info->cache); | |
487 | /* Fallback to the object size if failed. */ | |
488 | if (!info->alloc_size) | |
489 | info->alloc_size = info->cache->object_size; | |
7fae3dd0 AK |
490 | } else |
491 | info->cache = info->object = NULL; | |
59e6e098 | 492 | |
dcc57966 AK |
493 | switch (info->type) { |
494 | case KASAN_REPORT_INVALID_FREE: | |
495 | info->bug_type = "invalid-free"; | |
496 | break; | |
497 | case KASAN_REPORT_DOUBLE_FREE: | |
498 | info->bug_type = "double-free"; | |
499 | break; | |
500 | default: | |
501 | /* bug_type filled in by kasan_complete_mode_report_info. */ | |
502 | break; | |
503 | } | |
504 | ||
59e6e098 AK |
505 | /* Fill in mode-specific report info fields. */ |
506 | kasan_complete_mode_report_info(info); | |
015b109f AK |
507 | } |
508 | ||
3de0de75 | 509 | void kasan_report_invalid_free(void *ptr, unsigned long ip, enum kasan_report_type type) |
31c65110 AK |
510 | { |
511 | unsigned long flags; | |
c965cdd6 | 512 | struct kasan_report_info info; |
31c65110 | 513 | |
c068664c | 514 | /* |
c6a690e0 AK |
515 | * Do not check report_suppressed_sw(), as an invalid-free cannot be |
516 | * caused by accessing poisoned memory and thus should not be suppressed | |
517 | * by kasan_disable/enable_current() critical sections. | |
518 | * | |
519 | * Note that for Hardware Tag-Based KASAN, kasan_report_invalid_free() | |
520 | * is triggered by explicit tag checks and not by the ones performed by | |
521 | * the CPU. Thus, reporting invalid-free is not suppressed as well. | |
c068664c AK |
522 | */ |
523 | if (unlikely(!report_enabled())) | |
524 | return; | |
525 | ||
31c65110 AK |
526 | start_report(&flags, true); |
527 | ||
59e6e098 | 528 | memset(&info, 0, sizeof(info)); |
3de0de75 | 529 | info.type = type; |
31c65110 | 530 | info.access_addr = ptr; |
31c65110 AK |
531 | info.access_size = 0; |
532 | info.is_write = false; | |
533 | info.ip = ip; | |
534 | ||
015b109f AK |
535 | complete_report_info(&info); |
536 | ||
31c65110 AK |
537 | print_report(&info); |
538 | ||
539 | end_report(&flags, ptr); | |
540 | } | |
541 | ||
795b760f AK |
542 | /* |
543 | * kasan_report() is the only reporting function that uses | |
544 | * user_access_save/restore(): kasan_report_invalid_free() cannot be called | |
545 | * from a UACCESS region, and kasan_report_async() is not used on x86. | |
546 | */ | |
4fba3758 AK |
547 | bool kasan_report(unsigned long addr, size_t size, bool is_write, |
548 | unsigned long ip) | |
549 | { | |
b3bb1d70 | 550 | bool ret = true; |
be8631a1 AK |
551 | void *ptr = (void *)addr; |
552 | unsigned long ua_flags = user_access_save(); | |
553 | unsigned long irq_flags; | |
c965cdd6 | 554 | struct kasan_report_info info; |
4fba3758 | 555 | |
c6a690e0 | 556 | if (unlikely(report_suppressed_sw()) || unlikely(!report_enabled())) { |
b3bb1d70 AK |
557 | ret = false; |
558 | goto out; | |
4fba3758 AK |
559 | } |
560 | ||
be8631a1 AK |
561 | start_report(&irq_flags, true); |
562 | ||
59e6e098 | 563 | memset(&info, 0, sizeof(info)); |
31c65110 | 564 | info.type = KASAN_REPORT_ACCESS; |
be8631a1 | 565 | info.access_addr = ptr; |
be8631a1 AK |
566 | info.access_size = size; |
567 | info.is_write = is_write; | |
568 | info.ip = ip; | |
569 | ||
015b109f AK |
570 | complete_report_info(&info); |
571 | ||
be8631a1 AK |
572 | print_report(&info); |
573 | ||
574 | end_report(&irq_flags, ptr); | |
b3bb1d70 AK |
575 | |
576 | out: | |
577 | user_access_restore(ua_flags); | |
4fba3758 AK |
578 | |
579 | return ret; | |
580 | } | |
581 | ||
bb2f967c AK |
582 | #ifdef CONFIG_KASAN_HW_TAGS |
583 | void kasan_report_async(void) | |
584 | { | |
585 | unsigned long flags; | |
586 | ||
c068664c | 587 | /* |
c6a690e0 AK |
588 | * Do not check report_suppressed_sw(), as |
589 | * kasan_disable/enable_current() critical sections do not affect | |
590 | * Hardware Tag-Based KASAN. | |
c068664c AK |
591 | */ |
592 | if (unlikely(!report_enabled())) | |
593 | return; | |
594 | ||
bb2f967c AK |
595 | start_report(&flags, false); |
596 | pr_err("BUG: KASAN: invalid-access\n"); | |
597 | pr_err("Asynchronous fault: no details available\n"); | |
598 | pr_err("\n"); | |
599 | dump_stack_lvl(KERN_ERR); | |
600 | end_report(&flags, NULL); | |
601 | } | |
602 | #endif /* CONFIG_KASAN_HW_TAGS */ | |
603 | ||
2f004eea JH |
604 | #ifdef CONFIG_KASAN_INLINE |
605 | /* | |
606 | * With CONFIG_KASAN_INLINE, accesses to bogus pointers (outside the high | |
607 | * canonical half of the address space) cause out-of-bounds shadow memory reads | |
608 | * before the actual access. For addresses in the low canonical half of the | |
609 | * address space, as well as most non-canonical addresses, that out-of-bounds | |
610 | * shadow memory access lands in the non-canonical part of the address space. | |
611 | * Help the user figure out what the original bogus pointer was. | |
612 | */ | |
613 | void kasan_non_canonical_hook(unsigned long addr) | |
614 | { | |
615 | unsigned long orig_addr; | |
616 | const char *bug_type; | |
617 | ||
618 | if (addr < KASAN_SHADOW_OFFSET) | |
619 | return; | |
620 | ||
621 | orig_addr = (addr - KASAN_SHADOW_OFFSET) << KASAN_SHADOW_SCALE_SHIFT; | |
622 | /* | |
623 | * For faults near the shadow address for NULL, we can be fairly certain | |
624 | * that this is a KASAN shadow memory access. | |
625 | * For faults that correspond to shadow for low canonical addresses, we | |
626 | * can still be pretty sure - that shadow region is a fairly narrow | |
627 | * chunk of the non-canonical address space. | |
628 | * But faults that look like shadow for non-canonical addresses are a | |
629 | * really large chunk of the address space. In that case, we still | |
630 | * print the decoded address, but make it clear that this is not | |
631 | * necessarily what's actually going on. | |
632 | */ | |
633 | if (orig_addr < PAGE_SIZE) | |
634 | bug_type = "null-ptr-deref"; | |
635 | else if (orig_addr < TASK_SIZE) | |
636 | bug_type = "probably user-memory-access"; | |
637 | else | |
638 | bug_type = "maybe wild-memory-access"; | |
639 | pr_alert("KASAN: %s in range [0x%016lx-0x%016lx]\n", bug_type, | |
1f600626 | 640 | orig_addr, orig_addr + KASAN_GRANULE_SIZE - 1); |
2f004eea JH |
641 | } |
642 | #endif |