1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * This file contains software tag-based KASAN specific 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/sched/task_stack.h> 20 #include <linux/slab.h> 21 #include <linux/stackdepot.h> 22 #include <linux/stacktrace.h> 23 #include <linux/string.h> 24 #include <linux/types.h> 25 #include <linux/kasan.h> 26 #include <linux/module.h> 27 28 #include <asm/sections.h> 29 30 #include "kasan.h" 31 #include "../slab.h" 32 33 void *kasan_find_first_bad_addr(void *addr, size_t size) 34 { 35 u8 tag = get_tag(addr); 36 void *p = kasan_reset_tag(addr); 37 void *end = p + size; 38 39 if (!addr_has_metadata(p)) 40 return p; 41 42 while (p < end && tag == *(u8 *)kasan_mem_to_shadow(p)) 43 p += KASAN_GRANULE_SIZE; 44 45 return p; 46 } 47 48 void kasan_metadata_fetch_row(char *buffer, void *row) 49 { 50 memcpy(buffer, kasan_mem_to_shadow(row), META_BYTES_PER_ROW); 51 } 52 53 void kasan_print_tags(u8 addr_tag, const void *addr) 54 { 55 u8 *shadow = (u8 *)kasan_mem_to_shadow(addr); 56 57 pr_err("Pointer tag: [%02x], memory tag: [%02x]\n", addr_tag, *shadow); 58 } 59 60 #ifdef CONFIG_KASAN_STACK 61 void kasan_print_address_stack_frame(const void *addr) 62 { 63 if (WARN_ON(!object_is_on_stack(addr))) 64 return; 65 66 pr_err("The buggy address belongs to stack of task %s/%d\n", 67 current->comm, task_pid_nr(current)); 68 } 69 #endif 70