1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * This file contains generic 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
kasan_find_first_bad_addr(void * addr,size_t size)33 void *kasan_find_first_bad_addr(void *addr, size_t size)
34 {
35 void *p = addr;
36
37 if (!addr_has_metadata(p))
38 return p;
39
40 while (p < addr + size && !(*(u8 *)kasan_mem_to_shadow(p)))
41 p += KASAN_GRANULE_SIZE;
42
43 return p;
44 }
45
get_shadow_bug_type(struct kasan_report_info * info)46 static const char *get_shadow_bug_type(struct kasan_report_info *info)
47 {
48 const char *bug_type = "unknown-crash";
49 u8 *shadow_addr;
50
51 shadow_addr = (u8 *)kasan_mem_to_shadow(info->first_bad_addr);
52
53 /*
54 * If shadow byte value is in [0, KASAN_GRANULE_SIZE) we can look
55 * at the next shadow byte to determine the type of the bad access.
56 */
57 if (*shadow_addr > 0 && *shadow_addr <= KASAN_GRANULE_SIZE - 1)
58 shadow_addr++;
59
60 switch (*shadow_addr) {
61 case 0 ... KASAN_GRANULE_SIZE - 1:
62 /*
63 * In theory it's still possible to see these shadow values
64 * due to a data race in the kernel code.
65 */
66 bug_type = "out-of-bounds";
67 break;
68 case KASAN_PAGE_REDZONE:
69 case KASAN_SLAB_REDZONE:
70 bug_type = "slab-out-of-bounds";
71 break;
72 case KASAN_GLOBAL_REDZONE:
73 bug_type = "global-out-of-bounds";
74 break;
75 case KASAN_STACK_LEFT:
76 case KASAN_STACK_MID:
77 case KASAN_STACK_RIGHT:
78 case KASAN_STACK_PARTIAL:
79 bug_type = "stack-out-of-bounds";
80 break;
81 case KASAN_PAGE_FREE:
82 case KASAN_SLAB_FREE:
83 case KASAN_SLAB_FREETRACK:
84 bug_type = "use-after-free";
85 break;
86 case KASAN_ALLOCA_LEFT:
87 case KASAN_ALLOCA_RIGHT:
88 bug_type = "alloca-out-of-bounds";
89 break;
90 case KASAN_VMALLOC_INVALID:
91 bug_type = "vmalloc-out-of-bounds";
92 break;
93 }
94
95 return bug_type;
96 }
97
get_wild_bug_type(struct kasan_report_info * info)98 static const char *get_wild_bug_type(struct kasan_report_info *info)
99 {
100 const char *bug_type = "unknown-crash";
101
102 if ((unsigned long)info->access_addr < PAGE_SIZE)
103 bug_type = "null-ptr-deref";
104 else if ((unsigned long)info->access_addr < TASK_SIZE)
105 bug_type = "user-memory-access";
106 else
107 bug_type = "wild-memory-access";
108
109 return bug_type;
110 }
111
get_bug_type(struct kasan_report_info * info)112 static const char *get_bug_type(struct kasan_report_info *info)
113 {
114 /*
115 * If access_size is a negative number, then it has reason to be
116 * defined as out-of-bounds bug type.
117 *
118 * Casting negative numbers to size_t would indeed turn up as
119 * a large size_t and its value will be larger than ULONG_MAX/2,
120 * so that this can qualify as out-of-bounds.
121 */
122 if (info->access_addr + info->access_size < info->access_addr)
123 return "out-of-bounds";
124
125 if (addr_has_metadata(info->access_addr))
126 return get_shadow_bug_type(info);
127 return get_wild_bug_type(info);
128 }
129
kasan_complete_mode_report_info(struct kasan_report_info * info)130 void kasan_complete_mode_report_info(struct kasan_report_info *info)
131 {
132 struct kasan_alloc_meta *alloc_meta;
133 struct kasan_free_meta *free_meta;
134
135 if (!info->bug_type)
136 info->bug_type = get_bug_type(info);
137
138 if (!info->cache || !info->object)
139 return;
140
141 alloc_meta = kasan_get_alloc_meta(info->cache, info->object);
142 if (alloc_meta)
143 memcpy(&info->alloc_track, &alloc_meta->alloc_track,
144 sizeof(info->alloc_track));
145
146 if (*(u8 *)kasan_mem_to_shadow(info->object) == KASAN_SLAB_FREETRACK) {
147 /* Free meta must be present with KASAN_SLAB_FREETRACK. */
148 free_meta = kasan_get_free_meta(info->cache, info->object);
149 memcpy(&info->free_track, &free_meta->free_track,
150 sizeof(info->free_track));
151 }
152 }
153
kasan_metadata_fetch_row(char * buffer,void * row)154 void kasan_metadata_fetch_row(char *buffer, void *row)
155 {
156 memcpy(buffer, kasan_mem_to_shadow(row), META_BYTES_PER_ROW);
157 }
158
kasan_print_aux_stacks(struct kmem_cache * cache,const void * object)159 void kasan_print_aux_stacks(struct kmem_cache *cache, const void *object)
160 {
161 struct kasan_alloc_meta *alloc_meta;
162
163 alloc_meta = kasan_get_alloc_meta(cache, object);
164 if (!alloc_meta)
165 return;
166
167 if (alloc_meta->aux_stack[0]) {
168 pr_err("Last potentially related work creation:\n");
169 stack_depot_print(alloc_meta->aux_stack[0]);
170 pr_err("\n");
171 }
172 if (alloc_meta->aux_stack[1]) {
173 pr_err("Second to last potentially related work creation:\n");
174 stack_depot_print(alloc_meta->aux_stack[1]);
175 pr_err("\n");
176 }
177 }
178
179 #ifdef CONFIG_KASAN_STACK
tokenize_frame_descr(const char ** frame_descr,char * token,size_t max_tok_len,unsigned long * value)180 static bool __must_check tokenize_frame_descr(const char **frame_descr,
181 char *token, size_t max_tok_len,
182 unsigned long *value)
183 {
184 const char *sep = strchr(*frame_descr, ' ');
185
186 if (sep == NULL)
187 sep = *frame_descr + strlen(*frame_descr);
188
189 if (token != NULL) {
190 const size_t tok_len = sep - *frame_descr;
191
192 if (tok_len + 1 > max_tok_len) {
193 pr_err("KASAN internal error: frame description too long: %s\n",
194 *frame_descr);
195 return false;
196 }
197
198 /* Copy token (+ 1 byte for '\0'). */
199 strscpy(token, *frame_descr, tok_len + 1);
200 }
201
202 /* Advance frame_descr past separator. */
203 *frame_descr = sep + 1;
204
205 if (value != NULL && kstrtoul(token, 10, value)) {
206 pr_err("KASAN internal error: not a valid number: %s\n", token);
207 return false;
208 }
209
210 return true;
211 }
212
print_decoded_frame_descr(const char * frame_descr)213 static void print_decoded_frame_descr(const char *frame_descr)
214 {
215 /*
216 * We need to parse the following string:
217 * "n alloc_1 alloc_2 ... alloc_n"
218 * where alloc_i looks like
219 * "offset size len name"
220 * or "offset size len name:line".
221 */
222
223 char token[64];
224 unsigned long num_objects;
225
226 if (!tokenize_frame_descr(&frame_descr, token, sizeof(token),
227 &num_objects))
228 return;
229
230 pr_err("\n");
231 pr_err("This frame has %lu %s:\n", num_objects,
232 num_objects == 1 ? "object" : "objects");
233
234 while (num_objects--) {
235 unsigned long offset;
236 unsigned long size;
237
238 /* access offset */
239 if (!tokenize_frame_descr(&frame_descr, token, sizeof(token),
240 &offset))
241 return;
242 /* access size */
243 if (!tokenize_frame_descr(&frame_descr, token, sizeof(token),
244 &size))
245 return;
246 /* name length (unused) */
247 if (!tokenize_frame_descr(&frame_descr, NULL, 0, NULL))
248 return;
249 /* object name */
250 if (!tokenize_frame_descr(&frame_descr, token, sizeof(token),
251 NULL))
252 return;
253
254 /* Strip line number; without filename it's not very helpful. */
255 strreplace(token, ':', '\0');
256
257 /* Finally, print object information. */
258 pr_err(" [%lu, %lu) '%s'", offset, offset + size, token);
259 }
260 }
261
262 /* Returns true only if the address is on the current task's stack. */
get_address_stack_frame_info(const void * addr,unsigned long * offset,const char ** frame_descr,const void ** frame_pc)263 static bool __must_check get_address_stack_frame_info(const void *addr,
264 unsigned long *offset,
265 const char **frame_descr,
266 const void **frame_pc)
267 {
268 unsigned long aligned_addr;
269 unsigned long mem_ptr;
270 const u8 *shadow_bottom;
271 const u8 *shadow_ptr;
272 const unsigned long *frame;
273
274 BUILD_BUG_ON(IS_ENABLED(CONFIG_STACK_GROWSUP));
275
276 aligned_addr = round_down((unsigned long)addr, sizeof(long));
277 mem_ptr = round_down(aligned_addr, KASAN_GRANULE_SIZE);
278 shadow_ptr = kasan_mem_to_shadow((void *)aligned_addr);
279 shadow_bottom = kasan_mem_to_shadow(end_of_stack(current));
280
281 while (shadow_ptr >= shadow_bottom && *shadow_ptr != KASAN_STACK_LEFT) {
282 shadow_ptr--;
283 mem_ptr -= KASAN_GRANULE_SIZE;
284 }
285
286 while (shadow_ptr >= shadow_bottom && *shadow_ptr == KASAN_STACK_LEFT) {
287 shadow_ptr--;
288 mem_ptr -= KASAN_GRANULE_SIZE;
289 }
290
291 if (shadow_ptr < shadow_bottom)
292 return false;
293
294 frame = (const unsigned long *)(mem_ptr + KASAN_GRANULE_SIZE);
295 if (frame[0] != KASAN_CURRENT_STACK_FRAME_MAGIC) {
296 pr_err("KASAN internal error: frame info validation failed; invalid marker: %lu\n",
297 frame[0]);
298 return false;
299 }
300
301 *offset = (unsigned long)addr - (unsigned long)frame;
302 *frame_descr = (const char *)frame[1];
303 *frame_pc = (void *)frame[2];
304
305 return true;
306 }
307
kasan_print_address_stack_frame(const void * addr)308 void kasan_print_address_stack_frame(const void *addr)
309 {
310 unsigned long offset;
311 const char *frame_descr;
312 const void *frame_pc;
313
314 if (WARN_ON(!object_is_on_stack(addr)))
315 return;
316
317 pr_err("The buggy address belongs to stack of task %s/%d\n",
318 current->comm, task_pid_nr(current));
319
320 if (!get_address_stack_frame_info(addr, &offset, &frame_descr,
321 &frame_pc))
322 return;
323
324 pr_err(" and is located at offset %lu in frame:\n", offset);
325 pr_err(" %pS\n", frame_pc);
326
327 if (!frame_descr)
328 return;
329
330 print_decoded_frame_descr(frame_descr);
331 }
332 #endif /* CONFIG_KASAN_STACK */
333
334 #define DEFINE_ASAN_REPORT_LOAD(size) \
335 void __asan_report_load##size##_noabort(unsigned long addr) \
336 { \
337 kasan_report(addr, size, false, _RET_IP_); \
338 } \
339 EXPORT_SYMBOL(__asan_report_load##size##_noabort)
340
341 #define DEFINE_ASAN_REPORT_STORE(size) \
342 void __asan_report_store##size##_noabort(unsigned long addr) \
343 { \
344 kasan_report(addr, size, true, _RET_IP_); \
345 } \
346 EXPORT_SYMBOL(__asan_report_store##size##_noabort)
347
348 DEFINE_ASAN_REPORT_LOAD(1);
349 DEFINE_ASAN_REPORT_LOAD(2);
350 DEFINE_ASAN_REPORT_LOAD(4);
351 DEFINE_ASAN_REPORT_LOAD(8);
352 DEFINE_ASAN_REPORT_LOAD(16);
353 DEFINE_ASAN_REPORT_STORE(1);
354 DEFINE_ASAN_REPORT_STORE(2);
355 DEFINE_ASAN_REPORT_STORE(4);
356 DEFINE_ASAN_REPORT_STORE(8);
357 DEFINE_ASAN_REPORT_STORE(16);
358
__asan_report_load_n_noabort(unsigned long addr,size_t size)359 void __asan_report_load_n_noabort(unsigned long addr, size_t size)
360 {
361 kasan_report(addr, size, false, _RET_IP_);
362 }
363 EXPORT_SYMBOL(__asan_report_load_n_noabort);
364
__asan_report_store_n_noabort(unsigned long addr,size_t size)365 void __asan_report_store_n_noabort(unsigned long addr, size_t size)
366 {
367 kasan_report(addr, size, true, _RET_IP_);
368 }
369 EXPORT_SYMBOL(__asan_report_store_n_noabort);
370