]>
Commit | Line | Data |
---|---|---|
e886bf9d | 1 | // SPDX-License-Identifier: GPL-2.0 |
0b24becc | 2 | /* |
11cd3cd6 | 3 | * This file contains common generic and tag-based KASAN error reporting code. |
0b24becc AR |
4 | * |
5 | * Copyright (c) 2014 Samsung Electronics Co., Ltd. | |
2baf9e89 | 6 | * Author: Andrey Ryabinin <[email protected]> |
0b24becc | 7 | * |
5d0926ef | 8 | * Some code borrowed from https://github.com/xairy/kasan-prototype by |
5f21f3a8 | 9 | * Andrey Konovalov <[email protected]> |
0b24becc AR |
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 | ||
b0845ce5 | 17 | #include <linux/bitops.h> |
4f40c6e5 | 18 | #include <linux/ftrace.h> |
b0845ce5 | 19 | #include <linux/init.h> |
0b24becc AR |
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> | |
cd11016e | 25 | #include <linux/stackdepot.h> |
0b24becc AR |
26 | #include <linux/stacktrace.h> |
27 | #include <linux/string.h> | |
28 | #include <linux/types.h> | |
29 | #include <linux/kasan.h> | |
527f215b | 30 | #include <linux/module.h> |
0b24becc | 31 | |
bebf56a1 AR |
32 | #include <asm/sections.h> |
33 | ||
0b24becc | 34 | #include "kasan.h" |
0316bec2 | 35 | #include "../slab.h" |
0b24becc AR |
36 | |
37 | /* Shadow layout customization. */ | |
38 | #define SHADOW_BYTES_PER_BLOCK 1 | |
39 | #define SHADOW_BLOCKS_PER_ROW 16 | |
40 | #define SHADOW_BYTES_PER_ROW (SHADOW_BLOCKS_PER_ROW * SHADOW_BYTES_PER_BLOCK) | |
41 | #define SHADOW_ROWS_AROUND_ADDR 2 | |
42 | ||
11cd3cd6 | 43 | static unsigned long kasan_flags; |
0b24becc | 44 | |
11cd3cd6 AK |
45 | #define KASAN_BIT_REPORTED 0 |
46 | #define KASAN_BIT_MULTI_SHOT 1 | |
5e82cd12 | 47 | |
11cd3cd6 | 48 | bool kasan_save_enable_multi_shot(void) |
0b24becc | 49 | { |
11cd3cd6 | 50 | return test_and_set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags); |
5e82cd12 | 51 | } |
11cd3cd6 | 52 | EXPORT_SYMBOL_GPL(kasan_save_enable_multi_shot); |
5e82cd12 | 53 | |
11cd3cd6 | 54 | void kasan_restore_multi_shot(bool enabled) |
5e82cd12 | 55 | { |
11cd3cd6 AK |
56 | if (!enabled) |
57 | clear_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags); | |
5e82cd12 | 58 | } |
11cd3cd6 | 59 | EXPORT_SYMBOL_GPL(kasan_restore_multi_shot); |
5e82cd12 | 60 | |
11cd3cd6 | 61 | static int __init kasan_set_multi_shot(char *str) |
7d418f7b | 62 | { |
11cd3cd6 AK |
63 | set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags); |
64 | return 1; | |
7d418f7b | 65 | } |
11cd3cd6 | 66 | __setup("kasan_multi_shot", kasan_set_multi_shot); |
7d418f7b | 67 | |
121e8f81 | 68 | static void print_error_description(struct kasan_access_info *info) |
5e82cd12 | 69 | { |
7f0a84c2 | 70 | pr_err("BUG: KASAN: %s in %pS\n", |
121e8f81 | 71 | get_bug_type(info), (void *)info->ip); |
6424f6bb | 72 | pr_err("%s of size %zu at addr %px by task %s/%d\n", |
7d418f7b | 73 | info->is_write ? "Write" : "Read", info->access_size, |
7f0a84c2 | 74 | info->access_addr, current->comm, task_pid_nr(current)); |
0b24becc AR |
75 | } |
76 | ||
7e088978 AR |
77 | static DEFINE_SPINLOCK(report_lock); |
78 | ||
11cd3cd6 | 79 | static void start_report(unsigned long *flags) |
7e088978 AR |
80 | { |
81 | /* | |
82 | * Make sure we don't end up in loop. | |
83 | */ | |
84 | kasan_disable_current(); | |
85 | spin_lock_irqsave(&report_lock, *flags); | |
86 | pr_err("==================================================================\n"); | |
87 | } | |
88 | ||
11cd3cd6 | 89 | static void end_report(unsigned long *flags) |
7e088978 AR |
90 | { |
91 | pr_err("==================================================================\n"); | |
92 | add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); | |
93 | spin_unlock_irqrestore(&report_lock, *flags); | |
5c5c1f36 DV |
94 | if (panic_on_warn) |
95 | panic("panic_on_warn set ...\n"); | |
7e088978 AR |
96 | kasan_enable_current(); |
97 | } | |
98 | ||
b6b72f49 | 99 | static void print_track(struct kasan_track *track, const char *prefix) |
7ed2f9e6 | 100 | { |
b6b72f49 | 101 | pr_err("%s by task %u:\n", prefix, track->pid); |
cd11016e AP |
102 | if (track->stack) { |
103 | struct stack_trace trace; | |
104 | ||
105 | depot_fetch_stack(track->stack, &trace); | |
106 | print_stack_trace(&trace, 0); | |
107 | } else { | |
108 | pr_err("(stack is not available)\n"); | |
109 | } | |
7ed2f9e6 AP |
110 | } |
111 | ||
db429f16 AK |
112 | static struct page *addr_to_page(const void *addr) |
113 | { | |
114 | if ((addr >= (void *)PAGE_OFFSET) && | |
115 | (addr < high_memory)) | |
116 | return virt_to_head_page(addr); | |
117 | return NULL; | |
118 | } | |
119 | ||
0c06f1f8 AK |
120 | static void describe_object_addr(struct kmem_cache *cache, void *object, |
121 | const void *addr) | |
7ed2f9e6 | 122 | { |
0c06f1f8 AK |
123 | unsigned long access_addr = (unsigned long)addr; |
124 | unsigned long object_addr = (unsigned long)object; | |
125 | const char *rel_type; | |
126 | int rel_bytes; | |
7ed2f9e6 | 127 | |
6424f6bb | 128 | pr_err("The buggy address belongs to the object at %px\n" |
0c06f1f8 AK |
129 | " which belongs to the cache %s of size %d\n", |
130 | object, cache->name, cache->object_size); | |
47b5c2a0 | 131 | |
0c06f1f8 | 132 | if (!addr) |
7ed2f9e6 | 133 | return; |
b3cbd9bf | 134 | |
0c06f1f8 AK |
135 | if (access_addr < object_addr) { |
136 | rel_type = "to the left"; | |
137 | rel_bytes = object_addr - access_addr; | |
138 | } else if (access_addr >= object_addr + cache->object_size) { | |
139 | rel_type = "to the right"; | |
140 | rel_bytes = access_addr - (object_addr + cache->object_size); | |
141 | } else { | |
142 | rel_type = "inside"; | |
143 | rel_bytes = access_addr - object_addr; | |
144 | } | |
145 | ||
146 | pr_err("The buggy address is located %d bytes %s of\n" | |
6424f6bb | 147 | " %d-byte region [%px, %px)\n", |
0c06f1f8 AK |
148 | rel_bytes, rel_type, cache->object_size, (void *)object_addr, |
149 | (void *)(object_addr + cache->object_size)); | |
150 | } | |
151 | ||
152 | static void describe_object(struct kmem_cache *cache, void *object, | |
153 | const void *addr) | |
154 | { | |
155 | struct kasan_alloc_meta *alloc_info = get_alloc_info(cache, object); | |
156 | ||
157 | if (cache->flags & SLAB_KASAN) { | |
158 | print_track(&alloc_info->alloc_track, "Allocated"); | |
b1938599 | 159 | pr_err("\n"); |
0c06f1f8 | 160 | print_track(&alloc_info->free_track, "Freed"); |
b1938599 | 161 | pr_err("\n"); |
0c06f1f8 AK |
162 | } |
163 | ||
164 | describe_object_addr(cache, object, addr); | |
7ed2f9e6 | 165 | } |
7ed2f9e6 | 166 | |
11cd3cd6 AK |
167 | static inline bool kernel_or_module_addr(const void *addr) |
168 | { | |
169 | if (addr >= (void *)_stext && addr < (void *)_end) | |
170 | return true; | |
171 | if (is_module_address((unsigned long)addr)) | |
172 | return true; | |
173 | return false; | |
174 | } | |
175 | ||
176 | static inline bool init_task_stack_addr(const void *addr) | |
177 | { | |
178 | return addr >= (void *)&init_thread_union.stack && | |
179 | (addr <= (void *)&init_thread_union.stack + | |
180 | sizeof(init_thread_union.stack)); | |
181 | } | |
182 | ||
5ab6d91a | 183 | static void print_address_description(void *addr) |
0b24becc | 184 | { |
db429f16 | 185 | struct page *page = addr_to_page(addr); |
b8c73fc2 | 186 | |
db429f16 | 187 | dump_stack(); |
b1938599 | 188 | pr_err("\n"); |
db429f16 AK |
189 | |
190 | if (page && PageSlab(page)) { | |
191 | struct kmem_cache *cache = page->slab_cache; | |
0c06f1f8 | 192 | void *object = nearest_obj(cache, page, addr); |
db429f16 | 193 | |
0c06f1f8 | 194 | describe_object(cache, object, addr); |
b8c73fc2 AR |
195 | } |
196 | ||
430a05f9 AK |
197 | if (kernel_or_module_addr(addr) && !init_task_stack_addr(addr)) { |
198 | pr_err("The buggy address belongs to the variable:\n"); | |
199 | pr_err(" %pS\n", addr); | |
200 | } | |
201 | ||
202 | if (page) { | |
203 | pr_err("The buggy address belongs to the page:\n"); | |
204 | dump_page(page, "kasan: bad access detected"); | |
bebf56a1 | 205 | } |
0b24becc AR |
206 | } |
207 | ||
208 | static bool row_is_guilty(const void *row, const void *guilty) | |
209 | { | |
210 | return (row <= guilty) && (guilty < row + SHADOW_BYTES_PER_ROW); | |
211 | } | |
212 | ||
213 | static int shadow_pointer_offset(const void *row, const void *shadow) | |
214 | { | |
215 | /* The length of ">ff00ff00ff00ff00: " is | |
216 | * 3 + (BITS_PER_LONG/8)*2 chars. | |
217 | */ | |
218 | return 3 + (BITS_PER_LONG/8)*2 + (shadow - row)*2 + | |
219 | (shadow - row) / SHADOW_BYTES_PER_BLOCK + 1; | |
220 | } | |
221 | ||
222 | static void print_shadow_for_address(const void *addr) | |
223 | { | |
224 | int i; | |
225 | const void *shadow = kasan_mem_to_shadow(addr); | |
226 | const void *shadow_row; | |
227 | ||
228 | shadow_row = (void *)round_down((unsigned long)shadow, | |
229 | SHADOW_BYTES_PER_ROW) | |
230 | - SHADOW_ROWS_AROUND_ADDR * SHADOW_BYTES_PER_ROW; | |
231 | ||
232 | pr_err("Memory state around the buggy address:\n"); | |
233 | ||
234 | for (i = -SHADOW_ROWS_AROUND_ADDR; i <= SHADOW_ROWS_AROUND_ADDR; i++) { | |
235 | const void *kaddr = kasan_shadow_to_mem(shadow_row); | |
236 | char buffer[4 + (BITS_PER_LONG/8)*2]; | |
f2377d4e | 237 | char shadow_buf[SHADOW_BYTES_PER_ROW]; |
0b24becc AR |
238 | |
239 | snprintf(buffer, sizeof(buffer), | |
6424f6bb | 240 | (i == 0) ? ">%px: " : " %px: ", kaddr); |
f2377d4e AK |
241 | /* |
242 | * We should not pass a shadow pointer to generic | |
243 | * function, because generic functions may try to | |
244 | * access kasan mapping for the passed address. | |
245 | */ | |
f2377d4e | 246 | memcpy(shadow_buf, shadow_row, SHADOW_BYTES_PER_ROW); |
0b24becc AR |
247 | print_hex_dump(KERN_ERR, buffer, |
248 | DUMP_PREFIX_NONE, SHADOW_BYTES_PER_ROW, 1, | |
f2377d4e | 249 | shadow_buf, SHADOW_BYTES_PER_ROW, 0); |
0b24becc AR |
250 | |
251 | if (row_is_guilty(shadow_row, shadow)) | |
252 | pr_err("%*c\n", | |
253 | shadow_pointer_offset(shadow_row, shadow), | |
254 | '^'); | |
255 | ||
256 | shadow_row += SHADOW_BYTES_PER_ROW; | |
257 | } | |
258 | } | |
259 | ||
11cd3cd6 AK |
260 | static bool report_enabled(void) |
261 | { | |
262 | if (current->kasan_depth) | |
263 | return false; | |
264 | if (test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags)) | |
265 | return true; | |
266 | return !test_and_set_bit(KASAN_BIT_REPORTED, &kasan_flags); | |
267 | } | |
268 | ||
ee3ce779 | 269 | void kasan_report_invalid_free(void *object, unsigned long ip) |
5ab6d91a AK |
270 | { |
271 | unsigned long flags; | |
272 | ||
11cd3cd6 | 273 | start_report(&flags); |
ee3ce779 | 274 | pr_err("BUG: KASAN: double-free or invalid-free in %pS\n", (void *)ip); |
121e8f81 AK |
275 | print_tags(get_tag(object), reset_tag(object)); |
276 | object = reset_tag(object); | |
b1938599 | 277 | pr_err("\n"); |
5ab6d91a | 278 | print_address_description(object); |
b1938599 | 279 | pr_err("\n"); |
5ab6d91a | 280 | print_shadow_for_address(object); |
11cd3cd6 | 281 | end_report(&flags); |
5ab6d91a AK |
282 | } |
283 | ||
0b24becc AR |
284 | void kasan_report(unsigned long addr, size_t size, |
285 | bool is_write, unsigned long ip) | |
286 | { | |
287 | struct kasan_access_info info; | |
121e8f81 AK |
288 | void *tagged_addr; |
289 | void *untagged_addr; | |
290 | unsigned long flags; | |
0b24becc | 291 | |
11cd3cd6 | 292 | if (likely(!report_enabled())) |
0b24becc AR |
293 | return; |
294 | ||
4f40c6e5 PZ |
295 | disable_trace_on_warning(); |
296 | ||
121e8f81 AK |
297 | tagged_addr = (void *)addr; |
298 | untagged_addr = reset_tag(tagged_addr); | |
299 | ||
300 | info.access_addr = tagged_addr; | |
301 | if (addr_has_shadow(untagged_addr)) | |
302 | info.first_bad_addr = find_first_bad_addr(tagged_addr, size); | |
303 | else | |
304 | info.first_bad_addr = untagged_addr; | |
0b24becc AR |
305 | info.access_size = size; |
306 | info.is_write = is_write; | |
307 | info.ip = ip; | |
e9121076 | 308 | |
121e8f81 AK |
309 | start_report(&flags); |
310 | ||
311 | print_error_description(&info); | |
312 | if (addr_has_shadow(untagged_addr)) | |
313 | print_tags(get_tag(tagged_addr), info.first_bad_addr); | |
314 | pr_err("\n"); | |
315 | ||
316 | if (addr_has_shadow(untagged_addr)) { | |
317 | print_address_description(untagged_addr); | |
318 | pr_err("\n"); | |
319 | print_shadow_for_address(info.first_bad_addr); | |
320 | } else { | |
321 | dump_stack(); | |
322 | } | |
323 | ||
324 | end_report(&flags); | |
0b24becc | 325 | } |