mm: slub: add kernel address sanitizer support for slub allocator
[cascardo/linux.git] / mm / kasan / report.c
1 /*
2  * This file contains error reporting code.
3  *
4  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
5  * Author: Andrey Ryabinin <a.ryabinin@samsung.com>
6  *
7  * Some of code borrowed from https://github.com/xairy/linux by
8  *        Andrey Konovalov <adech.fo@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/kernel.h>
17 #include <linux/mm.h>
18 #include <linux/printk.h>
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/stacktrace.h>
22 #include <linux/string.h>
23 #include <linux/types.h>
24 #include <linux/kasan.h>
25
26 #include "kasan.h"
27 #include "../slab.h"
28
29 /* Shadow layout customization. */
30 #define SHADOW_BYTES_PER_BLOCK 1
31 #define SHADOW_BLOCKS_PER_ROW 16
32 #define SHADOW_BYTES_PER_ROW (SHADOW_BLOCKS_PER_ROW * SHADOW_BYTES_PER_BLOCK)
33 #define SHADOW_ROWS_AROUND_ADDR 2
34
35 static const void *find_first_bad_addr(const void *addr, size_t size)
36 {
37         u8 shadow_val = *(u8 *)kasan_mem_to_shadow(addr);
38         const void *first_bad_addr = addr;
39
40         while (!shadow_val && first_bad_addr < addr + size) {
41                 first_bad_addr += KASAN_SHADOW_SCALE_SIZE;
42                 shadow_val = *(u8 *)kasan_mem_to_shadow(first_bad_addr);
43         }
44         return first_bad_addr;
45 }
46
47 static void print_error_description(struct kasan_access_info *info)
48 {
49         const char *bug_type = "unknown crash";
50         u8 shadow_val;
51
52         info->first_bad_addr = find_first_bad_addr(info->access_addr,
53                                                 info->access_size);
54
55         shadow_val = *(u8 *)kasan_mem_to_shadow(info->first_bad_addr);
56
57         switch (shadow_val) {
58         case KASAN_FREE_PAGE:
59         case KASAN_KMALLOC_FREE:
60                 bug_type = "use after free";
61                 break;
62         case KASAN_PAGE_REDZONE:
63         case KASAN_KMALLOC_REDZONE:
64         case 0 ... KASAN_SHADOW_SCALE_SIZE - 1:
65                 bug_type = "out of bounds access";
66                 break;
67         }
68
69         pr_err("BUG: KASan: %s in %pS at addr %p\n",
70                 bug_type, (void *)info->ip,
71                 info->access_addr);
72         pr_err("%s of size %zu by task %s/%d\n",
73                 info->is_write ? "Write" : "Read",
74                 info->access_size, current->comm, task_pid_nr(current));
75 }
76
77 static void print_address_description(struct kasan_access_info *info)
78 {
79         const void *addr = info->access_addr;
80
81         if ((addr >= (void *)PAGE_OFFSET) &&
82                 (addr < high_memory)) {
83                 struct page *page = virt_to_head_page(addr);
84
85                 if (PageSlab(page)) {
86                         void *object;
87                         struct kmem_cache *cache = page->slab_cache;
88                         void *last_object;
89
90                         object = virt_to_obj(cache, page_address(page), addr);
91                         last_object = page_address(page) +
92                                 page->objects * cache->size;
93
94                         if (unlikely(object > last_object))
95                                 object = last_object; /* we hit into padding */
96
97                         object_err(cache, page, object,
98                                 "kasan: bad access detected");
99                         return;
100                 }
101                 dump_page(page, "kasan: bad access detected");
102         }
103
104         dump_stack();
105 }
106
107 static bool row_is_guilty(const void *row, const void *guilty)
108 {
109         return (row <= guilty) && (guilty < row + SHADOW_BYTES_PER_ROW);
110 }
111
112 static int shadow_pointer_offset(const void *row, const void *shadow)
113 {
114         /* The length of ">ff00ff00ff00ff00: " is
115          *    3 + (BITS_PER_LONG/8)*2 chars.
116          */
117         return 3 + (BITS_PER_LONG/8)*2 + (shadow - row)*2 +
118                 (shadow - row) / SHADOW_BYTES_PER_BLOCK + 1;
119 }
120
121 static void print_shadow_for_address(const void *addr)
122 {
123         int i;
124         const void *shadow = kasan_mem_to_shadow(addr);
125         const void *shadow_row;
126
127         shadow_row = (void *)round_down((unsigned long)shadow,
128                                         SHADOW_BYTES_PER_ROW)
129                 - SHADOW_ROWS_AROUND_ADDR * SHADOW_BYTES_PER_ROW;
130
131         pr_err("Memory state around the buggy address:\n");
132
133         for (i = -SHADOW_ROWS_AROUND_ADDR; i <= SHADOW_ROWS_AROUND_ADDR; i++) {
134                 const void *kaddr = kasan_shadow_to_mem(shadow_row);
135                 char buffer[4 + (BITS_PER_LONG/8)*2];
136
137                 snprintf(buffer, sizeof(buffer),
138                         (i == 0) ? ">%p: " : " %p: ", kaddr);
139
140                 kasan_disable_current();
141                 print_hex_dump(KERN_ERR, buffer,
142                         DUMP_PREFIX_NONE, SHADOW_BYTES_PER_ROW, 1,
143                         shadow_row, SHADOW_BYTES_PER_ROW, 0);
144                 kasan_enable_current();
145
146                 if (row_is_guilty(shadow_row, shadow))
147                         pr_err("%*c\n",
148                                 shadow_pointer_offset(shadow_row, shadow),
149                                 '^');
150
151                 shadow_row += SHADOW_BYTES_PER_ROW;
152         }
153 }
154
155 static DEFINE_SPINLOCK(report_lock);
156
157 void kasan_report_error(struct kasan_access_info *info)
158 {
159         unsigned long flags;
160
161         spin_lock_irqsave(&report_lock, flags);
162         pr_err("================================="
163                 "=================================\n");
164         print_error_description(info);
165         print_address_description(info);
166         print_shadow_for_address(info->first_bad_addr);
167         pr_err("================================="
168                 "=================================\n");
169         spin_unlock_irqrestore(&report_lock, flags);
170 }
171
172 void kasan_report_user_access(struct kasan_access_info *info)
173 {
174         unsigned long flags;
175
176         spin_lock_irqsave(&report_lock, flags);
177         pr_err("================================="
178                 "=================================\n");
179         pr_err("BUG: KASan: user-memory-access on address %p\n",
180                 info->access_addr);
181         pr_err("%s of size %zu by task %s/%d\n",
182                 info->is_write ? "Write" : "Read",
183                 info->access_size, current->comm, task_pid_nr(current));
184         dump_stack();
185         pr_err("================================="
186                 "=================================\n");
187         spin_unlock_irqrestore(&report_lock, flags);
188 }
189
190 void kasan_report(unsigned long addr, size_t size,
191                 bool is_write, unsigned long ip)
192 {
193         struct kasan_access_info info;
194
195         if (likely(!kasan_enabled()))
196                 return;
197
198         info.access_addr = (void *)addr;
199         info.access_size = size;
200         info.is_write = is_write;
201         info.ip = ip;
202         kasan_report_error(&info);
203 }
204
205
206 #define DEFINE_ASAN_REPORT_LOAD(size)                     \
207 void __asan_report_load##size##_noabort(unsigned long addr) \
208 {                                                         \
209         kasan_report(addr, size, false, _RET_IP_);        \
210 }                                                         \
211 EXPORT_SYMBOL(__asan_report_load##size##_noabort)
212
213 #define DEFINE_ASAN_REPORT_STORE(size)                     \
214 void __asan_report_store##size##_noabort(unsigned long addr) \
215 {                                                          \
216         kasan_report(addr, size, true, _RET_IP_);          \
217 }                                                          \
218 EXPORT_SYMBOL(__asan_report_store##size##_noabort)
219
220 DEFINE_ASAN_REPORT_LOAD(1);
221 DEFINE_ASAN_REPORT_LOAD(2);
222 DEFINE_ASAN_REPORT_LOAD(4);
223 DEFINE_ASAN_REPORT_LOAD(8);
224 DEFINE_ASAN_REPORT_LOAD(16);
225 DEFINE_ASAN_REPORT_STORE(1);
226 DEFINE_ASAN_REPORT_STORE(2);
227 DEFINE_ASAN_REPORT_STORE(4);
228 DEFINE_ASAN_REPORT_STORE(8);
229 DEFINE_ASAN_REPORT_STORE(16);
230
231 void __asan_report_load_n_noabort(unsigned long addr, size_t size)
232 {
233         kasan_report(addr, size, false, _RET_IP_);
234 }
235 EXPORT_SYMBOL(__asan_report_load_n_noabort);
236
237 void __asan_report_store_n_noabort(unsigned long addr, size_t size)
238 {
239         kasan_report(addr, size, true, _RET_IP_);
240 }
241 EXPORT_SYMBOL(__asan_report_store_n_noabort);