Linux 5.7.7
[linux/fpc-iii.git] / mm / kasan / report.c
blob80f23c9da6b08062f32f668ede36b81a50ba4c95
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * This file contains common generic and tag-based KASAN error reporting code.
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
8 * Some code borrowed from https://github.com/xairy/kasan-prototype by
9 * Andrey Konovalov <andreyknvl@gmail.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
17 #include <linux/bitops.h>
18 #include <linux/ftrace.h>
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/mm.h>
22 #include <linux/printk.h>
23 #include <linux/sched.h>
24 #include <linux/slab.h>
25 #include <linux/stackdepot.h>
26 #include <linux/stacktrace.h>
27 #include <linux/string.h>
28 #include <linux/types.h>
29 #include <linux/kasan.h>
30 #include <linux/module.h>
31 #include <linux/sched/task_stack.h>
33 #include <asm/sections.h>
35 #include "kasan.h"
36 #include "../slab.h"
38 /* Shadow layout customization. */
39 #define SHADOW_BYTES_PER_BLOCK 1
40 #define SHADOW_BLOCKS_PER_ROW 16
41 #define SHADOW_BYTES_PER_ROW (SHADOW_BLOCKS_PER_ROW * SHADOW_BYTES_PER_BLOCK)
42 #define SHADOW_ROWS_AROUND_ADDR 2
44 static unsigned long kasan_flags;
46 #define KASAN_BIT_REPORTED 0
47 #define KASAN_BIT_MULTI_SHOT 1
49 bool kasan_save_enable_multi_shot(void)
51 return test_and_set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
53 EXPORT_SYMBOL_GPL(kasan_save_enable_multi_shot);
55 void kasan_restore_multi_shot(bool enabled)
57 if (!enabled)
58 clear_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
60 EXPORT_SYMBOL_GPL(kasan_restore_multi_shot);
62 static int __init kasan_set_multi_shot(char *str)
64 set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
65 return 1;
67 __setup("kasan_multi_shot", kasan_set_multi_shot);
69 static void print_error_description(struct kasan_access_info *info)
71 pr_err("BUG: KASAN: %s in %pS\n",
72 get_bug_type(info), (void *)info->ip);
73 pr_err("%s of size %zu at addr %px by task %s/%d\n",
74 info->is_write ? "Write" : "Read", info->access_size,
75 info->access_addr, current->comm, task_pid_nr(current));
78 static DEFINE_SPINLOCK(report_lock);
80 static void start_report(unsigned long *flags)
83 * Make sure we don't end up in loop.
85 kasan_disable_current();
86 spin_lock_irqsave(&report_lock, *flags);
87 pr_err("==================================================================\n");
90 static void end_report(unsigned long *flags)
92 pr_err("==================================================================\n");
93 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
94 spin_unlock_irqrestore(&report_lock, *flags);
95 if (panic_on_warn) {
97 * This thread may hit another WARN() in the panic path.
98 * Resetting this prevents additional WARN() from panicking the
99 * system on this thread. Other threads are blocked by the
100 * panic_mutex in panic().
102 panic_on_warn = 0;
103 panic("panic_on_warn set ...\n");
105 kasan_enable_current();
108 static void print_track(struct kasan_track *track, const char *prefix)
110 pr_err("%s by task %u:\n", prefix, track->pid);
111 if (track->stack) {
112 unsigned long *entries;
113 unsigned int nr_entries;
115 nr_entries = stack_depot_fetch(track->stack, &entries);
116 stack_trace_print(entries, nr_entries, 0);
117 } else {
118 pr_err("(stack is not available)\n");
122 struct page *kasan_addr_to_page(const void *addr)
124 if ((addr >= (void *)PAGE_OFFSET) &&
125 (addr < high_memory))
126 return virt_to_head_page(addr);
127 return NULL;
130 static void describe_object_addr(struct kmem_cache *cache, void *object,
131 const void *addr)
133 unsigned long access_addr = (unsigned long)addr;
134 unsigned long object_addr = (unsigned long)object;
135 const char *rel_type;
136 int rel_bytes;
138 pr_err("The buggy address belongs to the object at %px\n"
139 " which belongs to the cache %s of size %d\n",
140 object, cache->name, cache->object_size);
142 if (!addr)
143 return;
145 if (access_addr < object_addr) {
146 rel_type = "to the left";
147 rel_bytes = object_addr - access_addr;
148 } else if (access_addr >= object_addr + cache->object_size) {
149 rel_type = "to the right";
150 rel_bytes = access_addr - (object_addr + cache->object_size);
151 } else {
152 rel_type = "inside";
153 rel_bytes = access_addr - object_addr;
156 pr_err("The buggy address is located %d bytes %s of\n"
157 " %d-byte region [%px, %px)\n",
158 rel_bytes, rel_type, cache->object_size, (void *)object_addr,
159 (void *)(object_addr + cache->object_size));
162 static struct kasan_track *kasan_get_free_track(struct kmem_cache *cache,
163 void *object, u8 tag)
165 struct kasan_alloc_meta *alloc_meta;
166 int i = 0;
168 alloc_meta = get_alloc_info(cache, object);
170 #ifdef CONFIG_KASAN_SW_TAGS_IDENTIFY
171 for (i = 0; i < KASAN_NR_FREE_STACKS; i++) {
172 if (alloc_meta->free_pointer_tag[i] == tag)
173 break;
175 if (i == KASAN_NR_FREE_STACKS)
176 i = alloc_meta->free_track_idx;
177 #endif
179 return &alloc_meta->free_track[i];
182 static void describe_object(struct kmem_cache *cache, void *object,
183 const void *addr, u8 tag)
185 struct kasan_alloc_meta *alloc_info = get_alloc_info(cache, object);
187 if (cache->flags & SLAB_KASAN) {
188 struct kasan_track *free_track;
190 print_track(&alloc_info->alloc_track, "Allocated");
191 pr_err("\n");
192 free_track = kasan_get_free_track(cache, object, tag);
193 print_track(free_track, "Freed");
194 pr_err("\n");
197 describe_object_addr(cache, object, addr);
200 static inline bool kernel_or_module_addr(const void *addr)
202 if (addr >= (void *)_stext && addr < (void *)_end)
203 return true;
204 if (is_module_address((unsigned long)addr))
205 return true;
206 return false;
209 static inline bool init_task_stack_addr(const void *addr)
211 return addr >= (void *)&init_thread_union.stack &&
212 (addr <= (void *)&init_thread_union.stack +
213 sizeof(init_thread_union.stack));
216 static bool __must_check tokenize_frame_descr(const char **frame_descr,
217 char *token, size_t max_tok_len,
218 unsigned long *value)
220 const char *sep = strchr(*frame_descr, ' ');
222 if (sep == NULL)
223 sep = *frame_descr + strlen(*frame_descr);
225 if (token != NULL) {
226 const size_t tok_len = sep - *frame_descr;
228 if (tok_len + 1 > max_tok_len) {
229 pr_err("KASAN internal error: frame description too long: %s\n",
230 *frame_descr);
231 return false;
234 /* Copy token (+ 1 byte for '\0'). */
235 strlcpy(token, *frame_descr, tok_len + 1);
238 /* Advance frame_descr past separator. */
239 *frame_descr = sep + 1;
241 if (value != NULL && kstrtoul(token, 10, value)) {
242 pr_err("KASAN internal error: not a valid number: %s\n", token);
243 return false;
246 return true;
249 static void print_decoded_frame_descr(const char *frame_descr)
252 * We need to parse the following string:
253 * "n alloc_1 alloc_2 ... alloc_n"
254 * where alloc_i looks like
255 * "offset size len name"
256 * or "offset size len name:line".
259 char token[64];
260 unsigned long num_objects;
262 if (!tokenize_frame_descr(&frame_descr, token, sizeof(token),
263 &num_objects))
264 return;
266 pr_err("\n");
267 pr_err("this frame has %lu %s:\n", num_objects,
268 num_objects == 1 ? "object" : "objects");
270 while (num_objects--) {
271 unsigned long offset;
272 unsigned long size;
274 /* access offset */
275 if (!tokenize_frame_descr(&frame_descr, token, sizeof(token),
276 &offset))
277 return;
278 /* access size */
279 if (!tokenize_frame_descr(&frame_descr, token, sizeof(token),
280 &size))
281 return;
282 /* name length (unused) */
283 if (!tokenize_frame_descr(&frame_descr, NULL, 0, NULL))
284 return;
285 /* object name */
286 if (!tokenize_frame_descr(&frame_descr, token, sizeof(token),
287 NULL))
288 return;
290 /* Strip line number; without filename it's not very helpful. */
291 strreplace(token, ':', '\0');
293 /* Finally, print object information. */
294 pr_err(" [%lu, %lu) '%s'", offset, offset + size, token);
298 static bool __must_check get_address_stack_frame_info(const void *addr,
299 unsigned long *offset,
300 const char **frame_descr,
301 const void **frame_pc)
303 unsigned long aligned_addr;
304 unsigned long mem_ptr;
305 const u8 *shadow_bottom;
306 const u8 *shadow_ptr;
307 const unsigned long *frame;
309 BUILD_BUG_ON(IS_ENABLED(CONFIG_STACK_GROWSUP));
312 * NOTE: We currently only support printing frame information for
313 * accesses to the task's own stack.
315 if (!object_is_on_stack(addr))
316 return false;
318 aligned_addr = round_down((unsigned long)addr, sizeof(long));
319 mem_ptr = round_down(aligned_addr, KASAN_SHADOW_SCALE_SIZE);
320 shadow_ptr = kasan_mem_to_shadow((void *)aligned_addr);
321 shadow_bottom = kasan_mem_to_shadow(end_of_stack(current));
323 while (shadow_ptr >= shadow_bottom && *shadow_ptr != KASAN_STACK_LEFT) {
324 shadow_ptr--;
325 mem_ptr -= KASAN_SHADOW_SCALE_SIZE;
328 while (shadow_ptr >= shadow_bottom && *shadow_ptr == KASAN_STACK_LEFT) {
329 shadow_ptr--;
330 mem_ptr -= KASAN_SHADOW_SCALE_SIZE;
333 if (shadow_ptr < shadow_bottom)
334 return false;
336 frame = (const unsigned long *)(mem_ptr + KASAN_SHADOW_SCALE_SIZE);
337 if (frame[0] != KASAN_CURRENT_STACK_FRAME_MAGIC) {
338 pr_err("KASAN internal error: frame info validation failed; invalid marker: %lu\n",
339 frame[0]);
340 return false;
343 *offset = (unsigned long)addr - (unsigned long)frame;
344 *frame_descr = (const char *)frame[1];
345 *frame_pc = (void *)frame[2];
347 return true;
350 static void print_address_stack_frame(const void *addr)
352 unsigned long offset;
353 const char *frame_descr;
354 const void *frame_pc;
356 if (IS_ENABLED(CONFIG_KASAN_SW_TAGS))
357 return;
359 if (!get_address_stack_frame_info(addr, &offset, &frame_descr,
360 &frame_pc))
361 return;
364 * get_address_stack_frame_info only returns true if the given addr is
365 * on the current task's stack.
367 pr_err("\n");
368 pr_err("addr %px is located in stack of task %s/%d at offset %lu in frame:\n",
369 addr, current->comm, task_pid_nr(current), offset);
370 pr_err(" %pS\n", frame_pc);
372 if (!frame_descr)
373 return;
375 print_decoded_frame_descr(frame_descr);
378 static void print_address_description(void *addr, u8 tag)
380 struct page *page = kasan_addr_to_page(addr);
382 dump_stack();
383 pr_err("\n");
385 if (page && PageSlab(page)) {
386 struct kmem_cache *cache = page->slab_cache;
387 void *object = nearest_obj(cache, page, addr);
389 describe_object(cache, object, addr, tag);
392 if (kernel_or_module_addr(addr) && !init_task_stack_addr(addr)) {
393 pr_err("The buggy address belongs to the variable:\n");
394 pr_err(" %pS\n", addr);
397 if (page) {
398 pr_err("The buggy address belongs to the page:\n");
399 dump_page(page, "kasan: bad access detected");
402 print_address_stack_frame(addr);
405 static bool row_is_guilty(const void *row, const void *guilty)
407 return (row <= guilty) && (guilty < row + SHADOW_BYTES_PER_ROW);
410 static int shadow_pointer_offset(const void *row, const void *shadow)
412 /* The length of ">ff00ff00ff00ff00: " is
413 * 3 + (BITS_PER_LONG/8)*2 chars.
415 return 3 + (BITS_PER_LONG/8)*2 + (shadow - row)*2 +
416 (shadow - row) / SHADOW_BYTES_PER_BLOCK + 1;
419 static void print_shadow_for_address(const void *addr)
421 int i;
422 const void *shadow = kasan_mem_to_shadow(addr);
423 const void *shadow_row;
425 shadow_row = (void *)round_down((unsigned long)shadow,
426 SHADOW_BYTES_PER_ROW)
427 - SHADOW_ROWS_AROUND_ADDR * SHADOW_BYTES_PER_ROW;
429 pr_err("Memory state around the buggy address:\n");
431 for (i = -SHADOW_ROWS_AROUND_ADDR; i <= SHADOW_ROWS_AROUND_ADDR; i++) {
432 const void *kaddr = kasan_shadow_to_mem(shadow_row);
433 char buffer[4 + (BITS_PER_LONG/8)*2];
434 char shadow_buf[SHADOW_BYTES_PER_ROW];
436 snprintf(buffer, sizeof(buffer),
437 (i == 0) ? ">%px: " : " %px: ", kaddr);
439 * We should not pass a shadow pointer to generic
440 * function, because generic functions may try to
441 * access kasan mapping for the passed address.
443 memcpy(shadow_buf, shadow_row, SHADOW_BYTES_PER_ROW);
444 print_hex_dump(KERN_ERR, buffer,
445 DUMP_PREFIX_NONE, SHADOW_BYTES_PER_ROW, 1,
446 shadow_buf, SHADOW_BYTES_PER_ROW, 0);
448 if (row_is_guilty(shadow_row, shadow))
449 pr_err("%*c\n",
450 shadow_pointer_offset(shadow_row, shadow),
451 '^');
453 shadow_row += SHADOW_BYTES_PER_ROW;
457 bool report_enabled(void)
459 if (current->kasan_depth)
460 return false;
461 if (test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags))
462 return true;
463 return !test_and_set_bit(KASAN_BIT_REPORTED, &kasan_flags);
466 void kasan_report_invalid_free(void *object, unsigned long ip)
468 unsigned long flags;
469 u8 tag = get_tag(object);
471 object = reset_tag(object);
472 start_report(&flags);
473 pr_err("BUG: KASAN: double-free or invalid-free in %pS\n", (void *)ip);
474 print_tags(tag, object);
475 pr_err("\n");
476 print_address_description(object, tag);
477 pr_err("\n");
478 print_shadow_for_address(object);
479 end_report(&flags);
482 void __kasan_report(unsigned long addr, size_t size, bool is_write, unsigned long ip)
484 struct kasan_access_info info;
485 void *tagged_addr;
486 void *untagged_addr;
487 unsigned long flags;
489 disable_trace_on_warning();
491 tagged_addr = (void *)addr;
492 untagged_addr = reset_tag(tagged_addr);
494 info.access_addr = tagged_addr;
495 if (addr_has_shadow(untagged_addr))
496 info.first_bad_addr = find_first_bad_addr(tagged_addr, size);
497 else
498 info.first_bad_addr = untagged_addr;
499 info.access_size = size;
500 info.is_write = is_write;
501 info.ip = ip;
503 start_report(&flags);
505 print_error_description(&info);
506 if (addr_has_shadow(untagged_addr))
507 print_tags(get_tag(tagged_addr), info.first_bad_addr);
508 pr_err("\n");
510 if (addr_has_shadow(untagged_addr)) {
511 print_address_description(untagged_addr, get_tag(tagged_addr));
512 pr_err("\n");
513 print_shadow_for_address(info.first_bad_addr);
514 } else {
515 dump_stack();
518 end_report(&flags);
521 #ifdef CONFIG_KASAN_INLINE
523 * With CONFIG_KASAN_INLINE, accesses to bogus pointers (outside the high
524 * canonical half of the address space) cause out-of-bounds shadow memory reads
525 * before the actual access. For addresses in the low canonical half of the
526 * address space, as well as most non-canonical addresses, that out-of-bounds
527 * shadow memory access lands in the non-canonical part of the address space.
528 * Help the user figure out what the original bogus pointer was.
530 void kasan_non_canonical_hook(unsigned long addr)
532 unsigned long orig_addr;
533 const char *bug_type;
535 if (addr < KASAN_SHADOW_OFFSET)
536 return;
538 orig_addr = (addr - KASAN_SHADOW_OFFSET) << KASAN_SHADOW_SCALE_SHIFT;
540 * For faults near the shadow address for NULL, we can be fairly certain
541 * that this is a KASAN shadow memory access.
542 * For faults that correspond to shadow for low canonical addresses, we
543 * can still be pretty sure - that shadow region is a fairly narrow
544 * chunk of the non-canonical address space.
545 * But faults that look like shadow for non-canonical addresses are a
546 * really large chunk of the address space. In that case, we still
547 * print the decoded address, but make it clear that this is not
548 * necessarily what's actually going on.
550 if (orig_addr < PAGE_SIZE)
551 bug_type = "null-ptr-deref";
552 else if (orig_addr < TASK_SIZE)
553 bug_type = "probably user-memory-access";
554 else
555 bug_type = "maybe wild-memory-access";
556 pr_alert("KASAN: %s in range [0x%016lx-0x%016lx]\n", bug_type,
557 orig_addr, orig_addr + KASAN_SHADOW_MASK);
559 #endif