Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
[linux-2.6-block.git] / mm / kasan / tags_report.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * This file contains 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  * 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.
14  *
15  */
16
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
32 #include <asm/sections.h>
33
34 #include "kasan.h"
35 #include "../slab.h"
36
37 const char *get_bug_type(struct kasan_access_info *info)
38 {
39 #ifdef CONFIG_KASAN_SW_TAGS_IDENTIFY
40         struct kasan_alloc_meta *alloc_meta;
41         struct kmem_cache *cache;
42         struct page *page;
43         const void *addr;
44         void *object;
45         u8 tag;
46         int i;
47
48         tag = get_tag(info->access_addr);
49         addr = reset_tag(info->access_addr);
50         page = kasan_addr_to_page(addr);
51         if (page && PageSlab(page)) {
52                 cache = page->slab_cache;
53                 object = nearest_obj(cache, page, (void *)addr);
54                 alloc_meta = get_alloc_info(cache, object);
55
56                 for (i = 0; i < KASAN_NR_FREE_STACKS; i++)
57                         if (alloc_meta->free_pointer_tag[i] == tag)
58                                 return "use-after-free";
59                 return "out-of-bounds";
60         }
61
62 #endif
63         return "invalid-access";
64 }
65
66 void *find_first_bad_addr(void *addr, size_t size)
67 {
68         u8 tag = get_tag(addr);
69         void *p = reset_tag(addr);
70         void *end = p + size;
71
72         while (p < end && tag == *(u8 *)kasan_mem_to_shadow(p))
73                 p += KASAN_SHADOW_SCALE_SIZE;
74         return p;
75 }
76
77 void print_tags(u8 addr_tag, const void *addr)
78 {
79         u8 *shadow = (u8 *)kasan_mem_to_shadow(addr);
80
81         pr_err("Pointer tag: [%02x], memory tag: [%02x]\n", addr_tag, *shadow);
82 }