blob: a4604cceae59416fee51e1affa15c508404393d4 [file] [log] [blame]
Andrey Konovalov11cd3cd2018-12-28 00:30:38 -08001/*
2 * This file contains generic KASAN specific error reporting code.
3 *
4 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
5 * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
6 *
7 * Some code borrowed from https://github.com/xairy/kasan-prototype by
8 * Andrey Konovalov <andreyknvl@gmail.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 *
14 */
15
16#include <linux/bitops.h>
17#include <linux/ftrace.h>
18#include <linux/init.h>
19#include <linux/kernel.h>
20#include <linux/mm.h>
21#include <linux/printk.h>
22#include <linux/sched.h>
23#include <linux/slab.h>
24#include <linux/stackdepot.h>
25#include <linux/stacktrace.h>
26#include <linux/string.h>
27#include <linux/types.h>
28#include <linux/kasan.h>
29#include <linux/module.h>
30
31#include <asm/sections.h>
32
33#include "kasan.h"
34#include "../slab.h"
35
Andrey Konovalov121e8f82018-12-28 00:30:42 -080036void *find_first_bad_addr(void *addr, size_t size)
Andrey Konovalov11cd3cd2018-12-28 00:30:38 -080037{
Andrey Konovalov121e8f82018-12-28 00:30:42 -080038 void *p = addr;
Andrey Konovalov11cd3cd2018-12-28 00:30:38 -080039
Andrey Konovalov121e8f82018-12-28 00:30:42 -080040 while (p < addr + size && !(*(u8 *)kasan_mem_to_shadow(p)))
41 p += KASAN_SHADOW_SCALE_SIZE;
42 return p;
Andrey Konovalov11cd3cd2018-12-28 00:30:38 -080043}
44
45static const char *get_shadow_bug_type(struct kasan_access_info *info)
46{
47 const char *bug_type = "unknown-crash";
48 u8 *shadow_addr;
49
Andrey Konovalov11cd3cd2018-12-28 00:30:38 -080050 shadow_addr = (u8 *)kasan_mem_to_shadow(info->first_bad_addr);
51
52 /*
53 * If shadow byte value is in [0, KASAN_SHADOW_SCALE_SIZE) we can look
54 * at the next shadow byte to determine the type of the bad access.
55 */
56 if (*shadow_addr > 0 && *shadow_addr <= KASAN_SHADOW_SCALE_SIZE - 1)
57 shadow_addr++;
58
59 switch (*shadow_addr) {
60 case 0 ... KASAN_SHADOW_SCALE_SIZE - 1:
61 /*
62 * In theory it's still possible to see these shadow values
63 * due to a data race in the kernel code.
64 */
65 bug_type = "out-of-bounds";
66 break;
67 case KASAN_PAGE_REDZONE:
68 case KASAN_KMALLOC_REDZONE:
69 bug_type = "slab-out-of-bounds";
70 break;
71 case KASAN_GLOBAL_REDZONE:
72 bug_type = "global-out-of-bounds";
73 break;
74 case KASAN_STACK_LEFT:
75 case KASAN_STACK_MID:
76 case KASAN_STACK_RIGHT:
77 case KASAN_STACK_PARTIAL:
78 bug_type = "stack-out-of-bounds";
79 break;
80 case KASAN_FREE_PAGE:
81 case KASAN_KMALLOC_FREE:
82 bug_type = "use-after-free";
83 break;
84 case KASAN_USE_AFTER_SCOPE:
85 bug_type = "use-after-scope";
86 break;
87 case KASAN_ALLOCA_LEFT:
88 case KASAN_ALLOCA_RIGHT:
89 bug_type = "alloca-out-of-bounds";
90 break;
91 }
92
93 return bug_type;
94}
95
96static const char *get_wild_bug_type(struct kasan_access_info *info)
97{
98 const char *bug_type = "unknown-crash";
99
100 if ((unsigned long)info->access_addr < PAGE_SIZE)
101 bug_type = "null-ptr-deref";
102 else if ((unsigned long)info->access_addr < TASK_SIZE)
103 bug_type = "user-memory-access";
104 else
105 bug_type = "wild-memory-access";
106
107 return bug_type;
108}
109
110const char *get_bug_type(struct kasan_access_info *info)
111{
112 if (addr_has_shadow(info->access_addr))
113 return get_shadow_bug_type(info);
114 return get_wild_bug_type(info);
115}
116
117#define DEFINE_ASAN_REPORT_LOAD(size) \
118void __asan_report_load##size##_noabort(unsigned long addr) \
119{ \
120 kasan_report(addr, size, false, _RET_IP_); \
121} \
122EXPORT_SYMBOL(__asan_report_load##size##_noabort)
123
124#define DEFINE_ASAN_REPORT_STORE(size) \
125void __asan_report_store##size##_noabort(unsigned long addr) \
126{ \
127 kasan_report(addr, size, true, _RET_IP_); \
128} \
129EXPORT_SYMBOL(__asan_report_store##size##_noabort)
130
131DEFINE_ASAN_REPORT_LOAD(1);
132DEFINE_ASAN_REPORT_LOAD(2);
133DEFINE_ASAN_REPORT_LOAD(4);
134DEFINE_ASAN_REPORT_LOAD(8);
135DEFINE_ASAN_REPORT_LOAD(16);
136DEFINE_ASAN_REPORT_STORE(1);
137DEFINE_ASAN_REPORT_STORE(2);
138DEFINE_ASAN_REPORT_STORE(4);
139DEFINE_ASAN_REPORT_STORE(8);
140DEFINE_ASAN_REPORT_STORE(16);
141
142void __asan_report_load_n_noabort(unsigned long addr, size_t size)
143{
144 kasan_report(addr, size, false, _RET_IP_);
145}
146EXPORT_SYMBOL(__asan_report_load_n_noabort);
147
148void __asan_report_store_n_noabort(unsigned long addr, size_t size)
149{
150 kasan_report(addr, size, true, _RET_IP_);
151}
152EXPORT_SYMBOL(__asan_report_store_n_noabort);