1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * This file contains common KASAN error reporting code.
4 *
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
7 *
8 * Some code borrowed from https://github.com/xairy/kasan-prototype by
9 * Andrey Konovalov <andreyknvl@gmail.com>
10 */
11
12 #include <linux/bitops.h>
13 #include <linux/ftrace.h>
14 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/mm.h>
17 #include <linux/printk.h>
18 #include <linux/sched.h>
19 #include <linux/slab.h>
20 #include <linux/stackdepot.h>
21 #include <linux/stacktrace.h>
22 #include <linux/string.h>
23 #include <linux/types.h>
24 #include <linux/kasan.h>
25 #include <linux/module.h>
26 #include <linux/sched/task_stack.h>
27 #include <linux/uaccess.h>
28 #include <trace/events/error_report.h>
29
30 #include <asm/sections.h>
31
32 #include <kunit/test.h>
33
34 #include "kasan.h"
35 #include "../slab.h"
36
37 static unsigned long kasan_flags;
38
39 #define KASAN_BIT_REPORTED 0
40 #define KASAN_BIT_MULTI_SHOT 1
41
kasan_save_enable_multi_shot(void)42 bool kasan_save_enable_multi_shot(void)
43 {
44 return test_and_set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
45 }
46 EXPORT_SYMBOL_GPL(kasan_save_enable_multi_shot);
47
kasan_restore_multi_shot(bool enabled)48 void kasan_restore_multi_shot(bool enabled)
49 {
50 if (!enabled)
51 clear_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
52 }
53 EXPORT_SYMBOL_GPL(kasan_restore_multi_shot);
54
kasan_set_multi_shot(char * str)55 static int __init kasan_set_multi_shot(char *str)
56 {
57 set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
58 return 1;
59 }
60 __setup("kasan_multi_shot", kasan_set_multi_shot);
61
print_error_description(struct kasan_access_info * info)62 static void print_error_description(struct kasan_access_info *info)
63 {
64 pr_err("BUG: KASAN: %s in %pS\n",
65 kasan_get_bug_type(info), (void *)info->ip);
66 if (info->access_size)
67 pr_err("%s of size %zu at addr %px by task %s/%d\n",
68 info->is_write ? "Write" : "Read", info->access_size,
69 info->access_addr, current->comm, task_pid_nr(current));
70 else
71 pr_err("%s at addr %px by task %s/%d\n",
72 info->is_write ? "Write" : "Read",
73 info->access_addr, current->comm, task_pid_nr(current));
74 }
75
76 static DEFINE_SPINLOCK(report_lock);
77
start_report(unsigned long * flags)78 static void start_report(unsigned long *flags)
79 {
80 /*
81 * Make sure we don't end up in loop.
82 */
83 kasan_disable_current();
84 spin_lock_irqsave(&report_lock, *flags);
85 pr_err("==================================================================\n");
86 }
87
end_report(unsigned long * flags,unsigned long addr)88 static void end_report(unsigned long *flags, unsigned long addr)
89 {
90 if (!kasan_async_mode_enabled())
91 trace_error_report_end(ERROR_DETECTOR_KASAN, addr);
92 pr_err("==================================================================\n");
93 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
94 spin_unlock_irqrestore(&report_lock, *flags);
95 if (!test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags))
96 check_panic_on_warn("KASAN");
97 #ifdef CONFIG_KASAN_HW_TAGS
98 if (kasan_flag_panic)
99 panic("kasan.fault=panic set ...\n");
100 #endif
101 kasan_enable_current();
102 }
103
print_stack(depot_stack_handle_t stack)104 static void print_stack(depot_stack_handle_t stack)
105 {
106 unsigned long *entries;
107 unsigned int nr_entries;
108
109 nr_entries = stack_depot_fetch(stack, &entries);
110 stack_trace_print(entries, nr_entries, 0);
111 }
112
print_track(struct kasan_track * track,const char * prefix)113 static void print_track(struct kasan_track *track, const char *prefix)
114 {
115 pr_err("%s by task %u:\n", prefix, track->pid);
116 if (track->stack) {
117 print_stack(track->stack);
118 } else {
119 pr_err("(stack is not available)\n");
120 }
121 }
122
kasan_addr_to_page(const void * addr)123 struct page *kasan_addr_to_page(const void *addr)
124 {
125 if ((addr >= (void *)PAGE_OFFSET) &&
126 (addr < high_memory))
127 return virt_to_head_page(addr);
128 return NULL;
129 }
130
describe_object_addr(struct kmem_cache * cache,void * object,const void * addr)131 static void describe_object_addr(struct kmem_cache *cache, void *object,
132 const void *addr)
133 {
134 unsigned long access_addr = (unsigned long)addr;
135 unsigned long object_addr = (unsigned long)object;
136 const char *rel_type;
137 int rel_bytes;
138
139 pr_err("The buggy address belongs to the object at %px\n"
140 " which belongs to the cache %s of size %d\n",
141 object, cache->name, cache->object_size);
142
143 if (!addr)
144 return;
145
146 if (access_addr < object_addr) {
147 rel_type = "to the left";
148 rel_bytes = object_addr - access_addr;
149 } else if (access_addr >= object_addr + cache->object_size) {
150 rel_type = "to the right";
151 rel_bytes = access_addr - (object_addr + cache->object_size);
152 } else {
153 rel_type = "inside";
154 rel_bytes = access_addr - object_addr;
155 }
156
157 pr_err("The buggy address is located %d bytes %s of\n"
158 " %d-byte region [%px, %px)\n",
159 rel_bytes, rel_type, cache->object_size, (void *)object_addr,
160 (void *)(object_addr + cache->object_size));
161 }
162
describe_object_stacks(struct kmem_cache * cache,void * object,const void * addr,u8 tag)163 static void describe_object_stacks(struct kmem_cache *cache, void *object,
164 const void *addr, u8 tag)
165 {
166 struct kasan_alloc_meta *alloc_meta;
167 struct kasan_track *free_track;
168
169 alloc_meta = kasan_get_alloc_meta(cache, object);
170 if (alloc_meta) {
171 print_track(&alloc_meta->alloc_track, "Allocated");
172 pr_err("\n");
173 }
174
175 free_track = kasan_get_free_track(cache, object, tag);
176 if (free_track) {
177 print_track(free_track, "Freed");
178 pr_err("\n");
179 }
180
181 #ifdef CONFIG_KASAN_GENERIC
182 if (!alloc_meta)
183 return;
184 if (alloc_meta->aux_stack[0]) {
185 pr_err("Last potentially related work creation:\n");
186 print_stack(alloc_meta->aux_stack[0]);
187 pr_err("\n");
188 }
189 if (alloc_meta->aux_stack[1]) {
190 pr_err("Second to last potentially related work creation:\n");
191 print_stack(alloc_meta->aux_stack[1]);
192 pr_err("\n");
193 }
194 #endif
195 }
196
describe_object(struct kmem_cache * cache,void * object,const void * addr,u8 tag)197 static void describe_object(struct kmem_cache *cache, void *object,
198 const void *addr, u8 tag)
199 {
200 if (kasan_stack_collection_enabled())
201 describe_object_stacks(cache, object, addr, tag);
202 describe_object_addr(cache, object, addr);
203 }
204
kernel_or_module_addr(const void * addr)205 static inline bool kernel_or_module_addr(const void *addr)
206 {
207 if (addr >= (void *)_stext && addr < (void *)_end)
208 return true;
209 if (is_module_address((unsigned long)addr))
210 return true;
211 return false;
212 }
213
init_task_stack_addr(const void * addr)214 static inline bool init_task_stack_addr(const void *addr)
215 {
216 return addr >= (void *)&init_thread_union.stack &&
217 (addr <= (void *)&init_thread_union.stack +
218 sizeof(init_thread_union.stack));
219 }
220
print_address_description(void * addr,u8 tag)221 static void print_address_description(void *addr, u8 tag)
222 {
223 struct page *page = kasan_addr_to_page(addr);
224
225 dump_stack_lvl(KERN_ERR);
226 pr_err("\n");
227
228 if (page && PageSlab(page)) {
229 struct kmem_cache *cache = page->slab_cache;
230 void *object = nearest_obj(cache, page, addr);
231
232 describe_object(cache, object, addr, tag);
233 }
234
235 if (kernel_or_module_addr(addr) && !init_task_stack_addr(addr)) {
236 pr_err("The buggy address belongs to the variable:\n");
237 pr_err(" %pS\n", addr);
238 }
239
240 if (page) {
241 pr_err("The buggy address belongs to the page:\n");
242 dump_page(page, "kasan: bad access detected");
243 }
244
245 kasan_print_address_stack_frame(addr);
246 }
247
meta_row_is_guilty(const void * row,const void * addr)248 static bool meta_row_is_guilty(const void *row, const void *addr)
249 {
250 return (row <= addr) && (addr < row + META_MEM_BYTES_PER_ROW);
251 }
252
meta_pointer_offset(const void * row,const void * addr)253 static int meta_pointer_offset(const void *row, const void *addr)
254 {
255 /*
256 * Memory state around the buggy address:
257 * ff00ff00ff00ff00: 00 00 00 05 fe fe fe fe fe fe fe fe fe fe fe fe
258 * ...
259 *
260 * The length of ">ff00ff00ff00ff00: " is
261 * 3 + (BITS_PER_LONG / 8) * 2 chars.
262 * The length of each granule metadata is 2 bytes
263 * plus 1 byte for space.
264 */
265 return 3 + (BITS_PER_LONG / 8) * 2 +
266 (addr - row) / KASAN_GRANULE_SIZE * 3 + 1;
267 }
268
print_memory_metadata(const void * addr)269 static void print_memory_metadata(const void *addr)
270 {
271 int i;
272 void *row;
273
274 row = (void *)round_down((unsigned long)addr, META_MEM_BYTES_PER_ROW)
275 - META_ROWS_AROUND_ADDR * META_MEM_BYTES_PER_ROW;
276
277 pr_err("Memory state around the buggy address:\n");
278
279 for (i = -META_ROWS_AROUND_ADDR; i <= META_ROWS_AROUND_ADDR; i++) {
280 char buffer[4 + (BITS_PER_LONG / 8) * 2];
281 char metadata[META_BYTES_PER_ROW];
282
283 snprintf(buffer, sizeof(buffer),
284 (i == 0) ? ">%px: " : " %px: ", row);
285
286 /*
287 * We should not pass a shadow pointer to generic
288 * function, because generic functions may try to
289 * access kasan mapping for the passed address.
290 */
291 kasan_metadata_fetch_row(&metadata[0], row);
292
293 print_hex_dump(KERN_ERR, buffer,
294 DUMP_PREFIX_NONE, META_BYTES_PER_ROW, 1,
295 metadata, META_BYTES_PER_ROW, 0);
296
297 if (meta_row_is_guilty(row, addr))
298 pr_err("%*c\n", meta_pointer_offset(row, addr), '^');
299
300 row += META_MEM_BYTES_PER_ROW;
301 }
302 }
303
report_enabled(void)304 static bool report_enabled(void)
305 {
306 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
307 if (current->kasan_depth)
308 return false;
309 #endif
310 if (test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags))
311 return true;
312 return !test_and_set_bit(KASAN_BIT_REPORTED, &kasan_flags);
313 }
314
315 #if IS_ENABLED(CONFIG_KUNIT)
kasan_update_kunit_status(struct kunit * cur_test)316 static void kasan_update_kunit_status(struct kunit *cur_test)
317 {
318 struct kunit_resource *resource;
319 struct kunit_kasan_expectation *kasan_data;
320
321 resource = kunit_find_named_resource(cur_test, "kasan_data");
322
323 if (!resource) {
324 kunit_set_failure(cur_test);
325 return;
326 }
327
328 kasan_data = (struct kunit_kasan_expectation *)resource->data;
329 WRITE_ONCE(kasan_data->report_found, true);
330 kunit_put_resource(resource);
331 }
332 #endif /* IS_ENABLED(CONFIG_KUNIT) */
333
kasan_report_invalid_free(void * object,unsigned long ip)334 void kasan_report_invalid_free(void *object, unsigned long ip)
335 {
336 unsigned long flags;
337 u8 tag = get_tag(object);
338
339 object = kasan_reset_tag(object);
340
341 #if IS_ENABLED(CONFIG_KUNIT)
342 if (current->kunit_test)
343 kasan_update_kunit_status(current->kunit_test);
344 #endif /* IS_ENABLED(CONFIG_KUNIT) */
345
346 start_report(&flags);
347 pr_err("BUG: KASAN: double-free or invalid-free in %pS\n", (void *)ip);
348 kasan_print_tags(tag, object);
349 pr_err("\n");
350 print_address_description(object, tag);
351 pr_err("\n");
352 print_memory_metadata(object);
353 end_report(&flags, (unsigned long)object);
354 }
355
356 #ifdef CONFIG_KASAN_HW_TAGS
kasan_report_async(void)357 void kasan_report_async(void)
358 {
359 unsigned long flags;
360
361 #if IS_ENABLED(CONFIG_KUNIT)
362 if (current->kunit_test)
363 kasan_update_kunit_status(current->kunit_test);
364 #endif /* IS_ENABLED(CONFIG_KUNIT) */
365
366 start_report(&flags);
367 pr_err("BUG: KASAN: invalid-access\n");
368 pr_err("Asynchronous mode enabled: no access details available\n");
369 pr_err("\n");
370 dump_stack_lvl(KERN_ERR);
371 end_report(&flags, 0);
372 }
373 #endif /* CONFIG_KASAN_HW_TAGS */
374
__kasan_report(unsigned long addr,size_t size,bool is_write,unsigned long ip)375 static void __kasan_report(unsigned long addr, size_t size, bool is_write,
376 unsigned long ip)
377 {
378 struct kasan_access_info info;
379 void *tagged_addr;
380 void *untagged_addr;
381 unsigned long flags;
382
383 #if IS_ENABLED(CONFIG_KUNIT)
384 if (current->kunit_test)
385 kasan_update_kunit_status(current->kunit_test);
386 #endif /* IS_ENABLED(CONFIG_KUNIT) */
387
388 disable_trace_on_warning();
389
390 tagged_addr = (void *)addr;
391 untagged_addr = kasan_reset_tag(tagged_addr);
392
393 info.access_addr = tagged_addr;
394 if (addr_has_metadata(untagged_addr))
395 info.first_bad_addr =
396 kasan_find_first_bad_addr(tagged_addr, size);
397 else
398 info.first_bad_addr = untagged_addr;
399 info.access_size = size;
400 info.is_write = is_write;
401 info.ip = ip;
402
403 start_report(&flags);
404
405 print_error_description(&info);
406 if (addr_has_metadata(untagged_addr))
407 kasan_print_tags(get_tag(tagged_addr), info.first_bad_addr);
408 pr_err("\n");
409
410 if (addr_has_metadata(untagged_addr)) {
411 print_address_description(untagged_addr, get_tag(tagged_addr));
412 pr_err("\n");
413 print_memory_metadata(info.first_bad_addr);
414 } else {
415 dump_stack_lvl(KERN_ERR);
416 }
417
418 end_report(&flags, addr);
419 }
420
kasan_report(unsigned long addr,size_t size,bool is_write,unsigned long ip)421 bool kasan_report(unsigned long addr, size_t size, bool is_write,
422 unsigned long ip)
423 {
424 unsigned long flags = user_access_save();
425 bool ret = false;
426
427 if (likely(report_enabled())) {
428 __kasan_report(addr, size, is_write, ip);
429 ret = true;
430 }
431
432 user_access_restore(flags);
433
434 return ret;
435 }
436
437 #ifdef CONFIG_KASAN_INLINE
438 /*
439 * With CONFIG_KASAN_INLINE, accesses to bogus pointers (outside the high
440 * canonical half of the address space) cause out-of-bounds shadow memory reads
441 * before the actual access. For addresses in the low canonical half of the
442 * address space, as well as most non-canonical addresses, that out-of-bounds
443 * shadow memory access lands in the non-canonical part of the address space.
444 * Help the user figure out what the original bogus pointer was.
445 */
kasan_non_canonical_hook(unsigned long addr)446 void kasan_non_canonical_hook(unsigned long addr)
447 {
448 unsigned long orig_addr;
449 const char *bug_type;
450
451 if (addr < KASAN_SHADOW_OFFSET)
452 return;
453
454 orig_addr = (addr - KASAN_SHADOW_OFFSET) << KASAN_SHADOW_SCALE_SHIFT;
455 /*
456 * For faults near the shadow address for NULL, we can be fairly certain
457 * that this is a KASAN shadow memory access.
458 * For faults that correspond to shadow for low canonical addresses, we
459 * can still be pretty sure - that shadow region is a fairly narrow
460 * chunk of the non-canonical address space.
461 * But faults that look like shadow for non-canonical addresses are a
462 * really large chunk of the address space. In that case, we still
463 * print the decoded address, but make it clear that this is not
464 * necessarily what's actually going on.
465 */
466 if (orig_addr < PAGE_SIZE)
467 bug_type = "null-ptr-deref";
468 else if (orig_addr < TASK_SIZE)
469 bug_type = "probably user-memory-access";
470 else
471 bug_type = "maybe wild-memory-access";
472 pr_alert("KASAN: %s in range [0x%016lx-0x%016lx]\n", bug_type,
473 orig_addr, orig_addr + KASAN_GRANULE_SIZE - 1);
474 }
475 #endif
476