blob: 139615ef326b99bbc58107f3654e5e3ca71551e0 [file] [log] [blame]
Andrey Konovalove886bf92018-12-28 00:31:14 -08001// SPDX-License-Identifier: GPL-2.0
Andrey Konovalov11cd3cd2018-12-28 00:30:38 -08002/*
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>
Andrey Konovalov11cd3cd2018-12-28 00:30:38 -080010 */
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>
Andrey Konovalov97fc7122020-12-22 12:00:49 -080019#include <linux/sched/task_stack.h>
Andrey Konovalov11cd3cd2018-12-28 00:30:38 -080020#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
Andrey Konovalovf00748b2021-02-24 12:05:05 -080033void *kasan_find_first_bad_addr(void *addr, size_t size)
Andrey Konovalov11cd3cd2018-12-28 00:30:38 -080034{
Andrey Konovalov121e8f82018-12-28 00:30:42 -080035 void *p = addr;
Andrey Konovalov11cd3cd2018-12-28 00:30:38 -080036
Andrey Konovalov121e8f82018-12-28 00:30:42 -080037 while (p < addr + size && !(*(u8 *)kasan_mem_to_shadow(p)))
Andrey Konovalov1f600622020-12-22 12:00:24 -080038 p += KASAN_GRANULE_SIZE;
Andrey Konovalov121e8f82018-12-28 00:30:42 -080039 return p;
Andrey Konovalov11cd3cd2018-12-28 00:30:38 -080040}
41
42static const char *get_shadow_bug_type(struct kasan_access_info *info)
43{
44 const char *bug_type = "unknown-crash";
45 u8 *shadow_addr;
46
Andrey Konovalov11cd3cd2018-12-28 00:30:38 -080047 shadow_addr = (u8 *)kasan_mem_to_shadow(info->first_bad_addr);
48
49 /*
Andrey Konovalov1f600622020-12-22 12:00:24 -080050 * If shadow byte value is in [0, KASAN_GRANULE_SIZE) we can look
Andrey Konovalov11cd3cd2018-12-28 00:30:38 -080051 * at the next shadow byte to determine the type of the bad access.
52 */
Andrey Konovalov1f600622020-12-22 12:00:24 -080053 if (*shadow_addr > 0 && *shadow_addr <= KASAN_GRANULE_SIZE - 1)
Andrey Konovalov11cd3cd2018-12-28 00:30:38 -080054 shadow_addr++;
55
56 switch (*shadow_addr) {
Andrey Konovalov1f600622020-12-22 12:00:24 -080057 case 0 ... KASAN_GRANULE_SIZE - 1:
Andrey Konovalov11cd3cd2018-12-28 00:30:38 -080058 /*
59 * In theory it's still possible to see these shadow values
60 * due to a data race in the kernel code.
61 */
62 bug_type = "out-of-bounds";
63 break;
64 case KASAN_PAGE_REDZONE:
65 case KASAN_KMALLOC_REDZONE:
66 bug_type = "slab-out-of-bounds";
67 break;
68 case KASAN_GLOBAL_REDZONE:
69 bug_type = "global-out-of-bounds";
70 break;
71 case KASAN_STACK_LEFT:
72 case KASAN_STACK_MID:
73 case KASAN_STACK_RIGHT:
74 case KASAN_STACK_PARTIAL:
75 bug_type = "stack-out-of-bounds";
76 break;
77 case KASAN_FREE_PAGE:
78 case KASAN_KMALLOC_FREE:
Walter Wue4b78182020-08-06 23:24:39 -070079 case KASAN_KMALLOC_FREETRACK:
Andrey Konovalov11cd3cd2018-12-28 00:30:38 -080080 bug_type = "use-after-free";
81 break;
Andrey Konovalov11cd3cd2018-12-28 00:30:38 -080082 case KASAN_ALLOCA_LEFT:
83 case KASAN_ALLOCA_RIGHT:
84 bug_type = "alloca-out-of-bounds";
85 break;
Daniel Axtens3c5c3cf2019-11-30 17:54:50 -080086 case KASAN_VMALLOC_INVALID:
87 bug_type = "vmalloc-out-of-bounds";
88 break;
Andrey Konovalov11cd3cd2018-12-28 00:30:38 -080089 }
90
91 return bug_type;
92}
93
94static const char *get_wild_bug_type(struct kasan_access_info *info)
95{
96 const char *bug_type = "unknown-crash";
97
98 if ((unsigned long)info->access_addr < PAGE_SIZE)
99 bug_type = "null-ptr-deref";
100 else if ((unsigned long)info->access_addr < TASK_SIZE)
101 bug_type = "user-memory-access";
102 else
103 bug_type = "wild-memory-access";
104
105 return bug_type;
106}
107
Andrey Konovalovf00748b2021-02-24 12:05:05 -0800108const char *kasan_get_bug_type(struct kasan_access_info *info)
Andrey Konovalov11cd3cd2018-12-28 00:30:38 -0800109{
Walter Wu8cceeff2020-04-01 21:09:37 -0700110 /*
111 * If access_size is a negative number, then it has reason to be
112 * defined as out-of-bounds bug type.
113 *
114 * Casting negative numbers to size_t would indeed turn up as
115 * a large size_t and its value will be larger than ULONG_MAX/2,
116 * so that this can qualify as out-of-bounds.
117 */
118 if (info->access_addr + info->access_size < info->access_addr)
119 return "out-of-bounds";
120
Andrey Konovalov68824642020-12-22 12:01:07 -0800121 if (addr_has_metadata(info->access_addr))
Andrey Konovalov11cd3cd2018-12-28 00:30:38 -0800122 return get_shadow_bug_type(info);
123 return get_wild_bug_type(info);
124}
125
Andrey Konovalovf00748b2021-02-24 12:05:05 -0800126void kasan_metadata_fetch_row(char *buffer, void *row)
Andrey Konovalov96e02792020-12-22 12:01:17 -0800127{
128 memcpy(buffer, kasan_mem_to_shadow(row), META_BYTES_PER_ROW);
129}
130
Walter Wu02c58772021-04-16 15:46:00 -0700131#ifdef CONFIG_KASAN_STACK
Andrey Konovalov97fc7122020-12-22 12:00:49 -0800132static bool __must_check tokenize_frame_descr(const char **frame_descr,
133 char *token, size_t max_tok_len,
134 unsigned long *value)
135{
136 const char *sep = strchr(*frame_descr, ' ');
137
138 if (sep == NULL)
139 sep = *frame_descr + strlen(*frame_descr);
140
141 if (token != NULL) {
142 const size_t tok_len = sep - *frame_descr;
143
144 if (tok_len + 1 > max_tok_len) {
145 pr_err("KASAN internal error: frame description too long: %s\n",
146 *frame_descr);
147 return false;
148 }
149
150 /* Copy token (+ 1 byte for '\0'). */
Zhiyuan Daif76e0c42021-04-29 22:59:43 -0700151 strscpy(token, *frame_descr, tok_len + 1);
Andrey Konovalov97fc7122020-12-22 12:00:49 -0800152 }
153
154 /* Advance frame_descr past separator. */
155 *frame_descr = sep + 1;
156
157 if (value != NULL && kstrtoul(token, 10, value)) {
158 pr_err("KASAN internal error: not a valid number: %s\n", token);
159 return false;
160 }
161
162 return true;
163}
164
165static void print_decoded_frame_descr(const char *frame_descr)
166{
167 /*
168 * We need to parse the following string:
169 * "n alloc_1 alloc_2 ... alloc_n"
170 * where alloc_i looks like
171 * "offset size len name"
172 * or "offset size len name:line".
173 */
174
175 char token[64];
176 unsigned long num_objects;
177
178 if (!tokenize_frame_descr(&frame_descr, token, sizeof(token),
179 &num_objects))
180 return;
181
182 pr_err("\n");
183 pr_err("this frame has %lu %s:\n", num_objects,
184 num_objects == 1 ? "object" : "objects");
185
186 while (num_objects--) {
187 unsigned long offset;
188 unsigned long size;
189
190 /* access offset */
191 if (!tokenize_frame_descr(&frame_descr, token, sizeof(token),
192 &offset))
193 return;
194 /* access size */
195 if (!tokenize_frame_descr(&frame_descr, token, sizeof(token),
196 &size))
197 return;
198 /* name length (unused) */
199 if (!tokenize_frame_descr(&frame_descr, NULL, 0, NULL))
200 return;
201 /* object name */
202 if (!tokenize_frame_descr(&frame_descr, token, sizeof(token),
203 NULL))
204 return;
205
206 /* Strip line number; without filename it's not very helpful. */
207 strreplace(token, ':', '\0');
208
209 /* Finally, print object information. */
210 pr_err(" [%lu, %lu) '%s'", offset, offset + size, token);
211 }
212}
213
214static bool __must_check get_address_stack_frame_info(const void *addr,
215 unsigned long *offset,
216 const char **frame_descr,
217 const void **frame_pc)
218{
219 unsigned long aligned_addr;
220 unsigned long mem_ptr;
221 const u8 *shadow_bottom;
222 const u8 *shadow_ptr;
223 const unsigned long *frame;
224
225 BUILD_BUG_ON(IS_ENABLED(CONFIG_STACK_GROWSUP));
226
227 /*
228 * NOTE: We currently only support printing frame information for
229 * accesses to the task's own stack.
230 */
231 if (!object_is_on_stack(addr))
232 return false;
233
234 aligned_addr = round_down((unsigned long)addr, sizeof(long));
235 mem_ptr = round_down(aligned_addr, KASAN_GRANULE_SIZE);
236 shadow_ptr = kasan_mem_to_shadow((void *)aligned_addr);
237 shadow_bottom = kasan_mem_to_shadow(end_of_stack(current));
238
239 while (shadow_ptr >= shadow_bottom && *shadow_ptr != KASAN_STACK_LEFT) {
240 shadow_ptr--;
241 mem_ptr -= KASAN_GRANULE_SIZE;
242 }
243
244 while (shadow_ptr >= shadow_bottom && *shadow_ptr == KASAN_STACK_LEFT) {
245 shadow_ptr--;
246 mem_ptr -= KASAN_GRANULE_SIZE;
247 }
248
249 if (shadow_ptr < shadow_bottom)
250 return false;
251
252 frame = (const unsigned long *)(mem_ptr + KASAN_GRANULE_SIZE);
253 if (frame[0] != KASAN_CURRENT_STACK_FRAME_MAGIC) {
254 pr_err("KASAN internal error: frame info validation failed; invalid marker: %lu\n",
255 frame[0]);
256 return false;
257 }
258
259 *offset = (unsigned long)addr - (unsigned long)frame;
260 *frame_descr = (const char *)frame[1];
261 *frame_pc = (void *)frame[2];
262
263 return true;
264}
265
Andrey Konovalovf00748b2021-02-24 12:05:05 -0800266void kasan_print_address_stack_frame(const void *addr)
Andrey Konovalov97fc7122020-12-22 12:00:49 -0800267{
268 unsigned long offset;
269 const char *frame_descr;
270 const void *frame_pc;
271
272 if (!get_address_stack_frame_info(addr, &offset, &frame_descr,
273 &frame_pc))
274 return;
275
276 /*
277 * get_address_stack_frame_info only returns true if the given addr is
278 * on the current task's stack.
279 */
280 pr_err("\n");
281 pr_err("addr %px is located in stack of task %s/%d at offset %lu in frame:\n",
282 addr, current->comm, task_pid_nr(current), offset);
283 pr_err(" %pS\n", frame_pc);
284
285 if (!frame_descr)
286 return;
287
288 print_decoded_frame_descr(frame_descr);
289}
290#endif /* CONFIG_KASAN_STACK */
291
Andrey Konovalov11cd3cd2018-12-28 00:30:38 -0800292#define DEFINE_ASAN_REPORT_LOAD(size) \
293void __asan_report_load##size##_noabort(unsigned long addr) \
294{ \
295 kasan_report(addr, size, false, _RET_IP_); \
296} \
297EXPORT_SYMBOL(__asan_report_load##size##_noabort)
298
299#define DEFINE_ASAN_REPORT_STORE(size) \
300void __asan_report_store##size##_noabort(unsigned long addr) \
301{ \
302 kasan_report(addr, size, true, _RET_IP_); \
303} \
304EXPORT_SYMBOL(__asan_report_store##size##_noabort)
305
306DEFINE_ASAN_REPORT_LOAD(1);
307DEFINE_ASAN_REPORT_LOAD(2);
308DEFINE_ASAN_REPORT_LOAD(4);
309DEFINE_ASAN_REPORT_LOAD(8);
310DEFINE_ASAN_REPORT_LOAD(16);
311DEFINE_ASAN_REPORT_STORE(1);
312DEFINE_ASAN_REPORT_STORE(2);
313DEFINE_ASAN_REPORT_STORE(4);
314DEFINE_ASAN_REPORT_STORE(8);
315DEFINE_ASAN_REPORT_STORE(16);
316
317void __asan_report_load_n_noabort(unsigned long addr, size_t size)
318{
319 kasan_report(addr, size, false, _RET_IP_);
320}
321EXPORT_SYMBOL(__asan_report_load_n_noabort);
322
323void __asan_report_store_n_noabort(unsigned long addr, size_t size)
324{
325 kasan_report(addr, size, true, _RET_IP_);
326}
327EXPORT_SYMBOL(__asan_report_store_n_noabort);