blob: 98b27195e38b07fc1b6e20c1f9c49db9bc112303 [file] [log] [blame]
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -08001/*
2 * This file contains shadow memory manipulation code.
3 *
4 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
Andrey Ryabinin2baf9e82015-08-14 15:35:13 -07005 * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -08006 *
Andrey Konovalov5d0926e2015-11-05 18:51:12 -08007 * Some code borrowed from https://github.com/xairy/kasan-prototype by
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -08008 * 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#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17#define DISABLE_BRANCH_PROFILING
18
19#include <linux/export.h>
Alexander Potapenkocd110162016-03-25 14:22:08 -070020#include <linux/interrupt.h>
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080021#include <linux/init.h>
Alexander Potapenkocd110162016-03-25 14:22:08 -070022#include <linux/kasan.h>
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080023#include <linux/kernel.h>
Andrey Ryabinin45937252015-11-20 15:57:18 -080024#include <linux/kmemleak.h>
Mark Rutlande3ae1162016-03-09 14:08:15 -080025#include <linux/linkage.h>
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080026#include <linux/memblock.h>
Andrey Ryabinin786a8952015-02-13 14:39:21 -080027#include <linux/memory.h>
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080028#include <linux/mm.h>
Andrey Ryabininbebf56a2015-02-13 14:40:17 -080029#include <linux/module.h>
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080030#include <linux/printk.h>
31#include <linux/sched.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010032#include <linux/sched/task_stack.h>
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080033#include <linux/slab.h>
34#include <linux/stacktrace.h>
35#include <linux/string.h>
36#include <linux/types.h>
Andrey Ryabinina5af5aa2015-03-12 16:26:11 -070037#include <linux/vmalloc.h>
Dmitry Vyukov9f7d4162016-10-14 16:07:23 +020038#include <linux/bug.h>
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080039
40#include "kasan.h"
Andrey Ryabinin0316bec2015-02-13 14:39:42 -080041#include "../slab.h"
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080042
Ingo Molnaraf8601a2017-02-03 09:57:00 +010043void kasan_enable_current(void)
44{
45 current->kasan_depth++;
46}
47
48void kasan_disable_current(void)
49{
50 current->kasan_depth--;
51}
52
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080053/*
54 * Poisons the shadow memory for 'size' bytes starting from 'addr'.
55 * Memory addresses should be aligned to KASAN_SHADOW_SCALE_SIZE.
56 */
57static void kasan_poison_shadow(const void *address, size_t size, u8 value)
58{
59 void *shadow_start, *shadow_end;
60
61 shadow_start = kasan_mem_to_shadow(address);
62 shadow_end = kasan_mem_to_shadow(address + size);
63
64 memset(shadow_start, value, shadow_end - shadow_start);
65}
66
67void kasan_unpoison_shadow(const void *address, size_t size)
68{
69 kasan_poison_shadow(address, size, 0);
70
71 if (size & KASAN_SHADOW_MASK) {
72 u8 *shadow = (u8 *)kasan_mem_to_shadow(address + size);
73 *shadow = size & KASAN_SHADOW_MASK;
74 }
75}
76
Dmitry Vyukov9f7d4162016-10-14 16:07:23 +020077static void __kasan_unpoison_stack(struct task_struct *task, const void *sp)
Mark Rutlande3ae1162016-03-09 14:08:15 -080078{
79 void *base = task_stack_page(task);
80 size_t size = sp - base;
81
82 kasan_unpoison_shadow(base, size);
83}
84
85/* Unpoison the entire stack for a task. */
86void kasan_unpoison_task_stack(struct task_struct *task)
87{
88 __kasan_unpoison_stack(task, task_stack_page(task) + THREAD_SIZE);
89}
90
91/* Unpoison the stack for the current task beyond a watermark sp value. */
Dmitry Vyukov9f7d4162016-10-14 16:07:23 +020092asmlinkage void kasan_unpoison_task_stack_below(const void *watermark)
Mark Rutlande3ae1162016-03-09 14:08:15 -080093{
Josh Poimboeufb53f40d2016-12-02 11:42:21 -060094 /*
95 * Calculate the task stack base address. Avoid using 'current'
96 * because this function is called by early resume code which hasn't
97 * yet set up the percpu register (%gs).
98 */
99 void *base = (void *)((unsigned long)watermark & ~(THREAD_SIZE - 1));
100
101 kasan_unpoison_shadow(base, watermark - base);
Dmitry Vyukov9f7d4162016-10-14 16:07:23 +0200102}
103
104/*
105 * Clear all poison for the region between the current SP and a provided
106 * watermark value, as is sometimes required prior to hand-crafted asm function
107 * returns in the middle of functions.
108 */
109void kasan_unpoison_stack_above_sp_to(const void *watermark)
110{
111 const void *sp = __builtin_frame_address(0);
112 size_t size = watermark - sp;
113
114 if (WARN_ON(sp > watermark))
115 return;
116 kasan_unpoison_shadow(sp, size);
Mark Rutlande3ae1162016-03-09 14:08:15 -0800117}
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800118
119/*
120 * All functions below always inlined so compiler could
121 * perform better optimizations in each of __asan_loadX/__assn_storeX
122 * depending on memory access size X.
123 */
124
125static __always_inline bool memory_is_poisoned_1(unsigned long addr)
126{
127 s8 shadow_value = *(s8 *)kasan_mem_to_shadow((void *)addr);
128
129 if (unlikely(shadow_value)) {
130 s8 last_accessible_byte = addr & KASAN_SHADOW_MASK;
131 return unlikely(last_accessible_byte >= shadow_value);
132 }
133
134 return false;
135}
136
137static __always_inline bool memory_is_poisoned_2(unsigned long addr)
138{
139 u16 *shadow_addr = (u16 *)kasan_mem_to_shadow((void *)addr);
140
141 if (unlikely(*shadow_addr)) {
142 if (memory_is_poisoned_1(addr + 1))
143 return true;
144
Xishi Qiu10f70262015-11-05 18:51:21 -0800145 /*
146 * If single shadow byte covers 2-byte access, we don't
147 * need to do anything more. Otherwise, test the first
148 * shadow byte.
149 */
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800150 if (likely(((addr + 1) & KASAN_SHADOW_MASK) != 0))
151 return false;
152
153 return unlikely(*(u8 *)shadow_addr);
154 }
155
156 return false;
157}
158
159static __always_inline bool memory_is_poisoned_4(unsigned long addr)
160{
161 u16 *shadow_addr = (u16 *)kasan_mem_to_shadow((void *)addr);
162
163 if (unlikely(*shadow_addr)) {
164 if (memory_is_poisoned_1(addr + 3))
165 return true;
166
Xishi Qiu10f70262015-11-05 18:51:21 -0800167 /*
168 * If single shadow byte covers 4-byte access, we don't
169 * need to do anything more. Otherwise, test the first
170 * shadow byte.
171 */
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800172 if (likely(((addr + 3) & KASAN_SHADOW_MASK) >= 3))
173 return false;
174
175 return unlikely(*(u8 *)shadow_addr);
176 }
177
178 return false;
179}
180
181static __always_inline bool memory_is_poisoned_8(unsigned long addr)
182{
183 u16 *shadow_addr = (u16 *)kasan_mem_to_shadow((void *)addr);
184
185 if (unlikely(*shadow_addr)) {
186 if (memory_is_poisoned_1(addr + 7))
187 return true;
188
Xishi Qiu10f70262015-11-05 18:51:21 -0800189 /*
190 * If single shadow byte covers 8-byte access, we don't
191 * need to do anything more. Otherwise, test the first
192 * shadow byte.
193 */
194 if (likely(IS_ALIGNED(addr, KASAN_SHADOW_SCALE_SIZE)))
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800195 return false;
196
197 return unlikely(*(u8 *)shadow_addr);
198 }
199
200 return false;
201}
202
203static __always_inline bool memory_is_poisoned_16(unsigned long addr)
204{
205 u32 *shadow_addr = (u32 *)kasan_mem_to_shadow((void *)addr);
206
207 if (unlikely(*shadow_addr)) {
208 u16 shadow_first_bytes = *(u16 *)shadow_addr;
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800209
210 if (unlikely(shadow_first_bytes))
211 return true;
212
Xishi Qiu10f70262015-11-05 18:51:21 -0800213 /*
214 * If two shadow bytes covers 16-byte access, we don't
215 * need to do anything more. Otherwise, test the last
216 * shadow byte.
217 */
218 if (likely(IS_ALIGNED(addr, KASAN_SHADOW_SCALE_SIZE)))
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800219 return false;
220
221 return memory_is_poisoned_1(addr + 15);
222 }
223
224 return false;
225}
226
227static __always_inline unsigned long bytes_is_zero(const u8 *start,
228 size_t size)
229{
230 while (size) {
231 if (unlikely(*start))
232 return (unsigned long)start;
233 start++;
234 size--;
235 }
236
237 return 0;
238}
239
240static __always_inline unsigned long memory_is_zero(const void *start,
241 const void *end)
242{
243 unsigned int words;
244 unsigned long ret;
245 unsigned int prefix = (unsigned long)start % 8;
246
247 if (end - start <= 16)
248 return bytes_is_zero(start, end - start);
249
250 if (prefix) {
251 prefix = 8 - prefix;
252 ret = bytes_is_zero(start, prefix);
253 if (unlikely(ret))
254 return ret;
255 start += prefix;
256 }
257
258 words = (end - start) / 8;
259 while (words) {
260 if (unlikely(*(u64 *)start))
261 return bytes_is_zero(start, 8);
262 start += 8;
263 words--;
264 }
265
266 return bytes_is_zero(start, (end - start) % 8);
267}
268
269static __always_inline bool memory_is_poisoned_n(unsigned long addr,
270 size_t size)
271{
272 unsigned long ret;
273
274 ret = memory_is_zero(kasan_mem_to_shadow((void *)addr),
275 kasan_mem_to_shadow((void *)addr + size - 1) + 1);
276
277 if (unlikely(ret)) {
278 unsigned long last_byte = addr + size - 1;
279 s8 *last_shadow = (s8 *)kasan_mem_to_shadow((void *)last_byte);
280
281 if (unlikely(ret != (unsigned long)last_shadow ||
Wang Longe0d57712015-11-05 18:51:18 -0800282 ((long)(last_byte & KASAN_SHADOW_MASK) >= *last_shadow)))
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800283 return true;
284 }
285 return false;
286}
287
288static __always_inline bool memory_is_poisoned(unsigned long addr, size_t size)
289{
290 if (__builtin_constant_p(size)) {
291 switch (size) {
292 case 1:
293 return memory_is_poisoned_1(addr);
294 case 2:
295 return memory_is_poisoned_2(addr);
296 case 4:
297 return memory_is_poisoned_4(addr);
298 case 8:
299 return memory_is_poisoned_8(addr);
300 case 16:
301 return memory_is_poisoned_16(addr);
302 default:
303 BUILD_BUG();
304 }
305 }
306
307 return memory_is_poisoned_n(addr, size);
308}
309
Andrey Ryabinin936bb4b2016-05-20 16:59:20 -0700310static __always_inline void check_memory_region_inline(unsigned long addr,
311 size_t size, bool write,
312 unsigned long ret_ip)
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800313{
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800314 if (unlikely(size == 0))
315 return;
316
317 if (unlikely((void *)addr <
318 kasan_shadow_to_mem((void *)KASAN_SHADOW_START))) {
Andrey Ryabinin936bb4b2016-05-20 16:59:20 -0700319 kasan_report(addr, size, write, ret_ip);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800320 return;
321 }
322
323 if (likely(!memory_is_poisoned(addr, size)))
324 return;
325
Andrey Ryabinin936bb4b2016-05-20 16:59:20 -0700326 kasan_report(addr, size, write, ret_ip);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800327}
328
Andrey Ryabinin936bb4b2016-05-20 16:59:20 -0700329static void check_memory_region(unsigned long addr,
330 size_t size, bool write,
331 unsigned long ret_ip)
332{
333 check_memory_region_inline(addr, size, write, ret_ip);
334}
Andrey Ryabinin393f2032015-02-13 14:39:56 -0800335
Andrey Ryabinin64f8eba2016-05-20 16:59:28 -0700336void kasan_check_read(const void *p, unsigned int size)
337{
338 check_memory_region((unsigned long)p, size, false, _RET_IP_);
339}
340EXPORT_SYMBOL(kasan_check_read);
341
342void kasan_check_write(const void *p, unsigned int size)
343{
344 check_memory_region((unsigned long)p, size, true, _RET_IP_);
345}
346EXPORT_SYMBOL(kasan_check_write);
347
Andrey Ryabinin393f2032015-02-13 14:39:56 -0800348#undef memset
349void *memset(void *addr, int c, size_t len)
350{
Andrey Ryabinin936bb4b2016-05-20 16:59:20 -0700351 check_memory_region((unsigned long)addr, len, true, _RET_IP_);
Andrey Ryabinin393f2032015-02-13 14:39:56 -0800352
353 return __memset(addr, c, len);
354}
355
356#undef memmove
357void *memmove(void *dest, const void *src, size_t len)
358{
Andrey Ryabinin936bb4b2016-05-20 16:59:20 -0700359 check_memory_region((unsigned long)src, len, false, _RET_IP_);
360 check_memory_region((unsigned long)dest, len, true, _RET_IP_);
Andrey Ryabinin393f2032015-02-13 14:39:56 -0800361
362 return __memmove(dest, src, len);
363}
364
365#undef memcpy
366void *memcpy(void *dest, const void *src, size_t len)
367{
Andrey Ryabinin936bb4b2016-05-20 16:59:20 -0700368 check_memory_region((unsigned long)src, len, false, _RET_IP_);
369 check_memory_region((unsigned long)dest, len, true, _RET_IP_);
Andrey Ryabinin393f2032015-02-13 14:39:56 -0800370
371 return __memcpy(dest, src, len);
372}
373
Andrey Ryabininb8c73fc2015-02-13 14:39:28 -0800374void kasan_alloc_pages(struct page *page, unsigned int order)
375{
376 if (likely(!PageHighMem(page)))
377 kasan_unpoison_shadow(page_address(page), PAGE_SIZE << order);
378}
379
380void kasan_free_pages(struct page *page, unsigned int order)
381{
382 if (likely(!PageHighMem(page)))
383 kasan_poison_shadow(page_address(page),
384 PAGE_SIZE << order,
385 KASAN_FREE_PAGE);
386}
387
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700388/*
389 * Adaptive redzone policy taken from the userspace AddressSanitizer runtime.
390 * For larger allocations larger redzones are used.
391 */
392static size_t optimal_redzone(size_t object_size)
393{
394 int rz =
395 object_size <= 64 - 16 ? 16 :
396 object_size <= 128 - 32 ? 32 :
397 object_size <= 512 - 64 ? 64 :
398 object_size <= 4096 - 128 ? 128 :
399 object_size <= (1 << 14) - 256 ? 256 :
400 object_size <= (1 << 15) - 512 ? 512 :
401 object_size <= (1 << 16) - 1024 ? 1024 : 2048;
402 return rz;
403}
404
405void kasan_cache_create(struct kmem_cache *cache, size_t *size,
406 unsigned long *flags)
407{
408 int redzone_adjust;
Alexander Potapenko80a92012016-07-28 15:49:07 -0700409 int orig_size = *size;
410
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700411 /* Add alloc meta. */
412 cache->kasan_info.alloc_meta_offset = *size;
413 *size += sizeof(struct kasan_alloc_meta);
414
415 /* Add free meta. */
416 if (cache->flags & SLAB_DESTROY_BY_RCU || cache->ctor ||
417 cache->object_size < sizeof(struct kasan_free_meta)) {
418 cache->kasan_info.free_meta_offset = *size;
419 *size += sizeof(struct kasan_free_meta);
420 }
421 redzone_adjust = optimal_redzone(cache->object_size) -
422 (*size - cache->object_size);
Alexander Potapenko80a92012016-07-28 15:49:07 -0700423
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700424 if (redzone_adjust > 0)
425 *size += redzone_adjust;
Alexander Potapenko80a92012016-07-28 15:49:07 -0700426
427 *size = min(KMALLOC_MAX_SIZE, max(*size, cache->object_size +
428 optimal_redzone(cache->object_size)));
429
430 /*
431 * If the metadata doesn't fit, don't enable KASAN at all.
432 */
433 if (*size <= cache->kasan_info.alloc_meta_offset ||
434 *size <= cache->kasan_info.free_meta_offset) {
435 cache->kasan_info.alloc_meta_offset = 0;
436 cache->kasan_info.free_meta_offset = 0;
437 *size = orig_size;
438 return;
439 }
440
441 *flags |= SLAB_KASAN;
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700442}
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700443
Alexander Potapenko55834c52016-05-20 16:59:11 -0700444void kasan_cache_shrink(struct kmem_cache *cache)
445{
446 quarantine_remove_cache(cache);
447}
448
Greg Thelenf9fa1d92017-02-24 15:00:05 -0800449void kasan_cache_shutdown(struct kmem_cache *cache)
Alexander Potapenko55834c52016-05-20 16:59:11 -0700450{
451 quarantine_remove_cache(cache);
452}
453
Alexander Potapenko80a92012016-07-28 15:49:07 -0700454size_t kasan_metadata_size(struct kmem_cache *cache)
455{
456 return (cache->kasan_info.alloc_meta_offset ?
457 sizeof(struct kasan_alloc_meta) : 0) +
458 (cache->kasan_info.free_meta_offset ?
459 sizeof(struct kasan_free_meta) : 0);
460}
461
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800462void kasan_poison_slab(struct page *page)
463{
464 kasan_poison_shadow(page_address(page),
465 PAGE_SIZE << compound_order(page),
466 KASAN_KMALLOC_REDZONE);
467}
468
469void kasan_unpoison_object_data(struct kmem_cache *cache, void *object)
470{
471 kasan_unpoison_shadow(object, cache->object_size);
472}
473
474void kasan_poison_object_data(struct kmem_cache *cache, void *object)
475{
476 kasan_poison_shadow(object,
477 round_up(cache->object_size, KASAN_SHADOW_SCALE_SIZE),
478 KASAN_KMALLOC_REDZONE);
479}
480
Alexander Potapenkocd110162016-03-25 14:22:08 -0700481static inline int in_irqentry_text(unsigned long ptr)
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700482{
Alexander Potapenkocd110162016-03-25 14:22:08 -0700483 return (ptr >= (unsigned long)&__irqentry_text_start &&
484 ptr < (unsigned long)&__irqentry_text_end) ||
485 (ptr >= (unsigned long)&__softirqentry_text_start &&
486 ptr < (unsigned long)&__softirqentry_text_end);
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700487}
488
Alexander Potapenkocd110162016-03-25 14:22:08 -0700489static inline void filter_irq_stacks(struct stack_trace *trace)
490{
491 int i;
492
493 if (!trace->nr_entries)
494 return;
495 for (i = 0; i < trace->nr_entries; i++)
496 if (in_irqentry_text(trace->entries[i])) {
497 /* Include the irqentry function into the stack. */
498 trace->nr_entries = i + 1;
499 break;
500 }
501}
502
503static inline depot_stack_handle_t save_stack(gfp_t flags)
504{
505 unsigned long entries[KASAN_STACK_DEPTH];
506 struct stack_trace trace = {
507 .nr_entries = 0,
508 .entries = entries,
509 .max_entries = KASAN_STACK_DEPTH,
510 .skip = 0
511 };
512
513 save_stack_trace(&trace);
514 filter_irq_stacks(&trace);
515 if (trace.nr_entries != 0 &&
516 trace.entries[trace.nr_entries-1] == ULONG_MAX)
517 trace.nr_entries--;
518
519 return depot_save_stack(&trace, flags);
520}
521
522static inline void set_track(struct kasan_track *track, gfp_t flags)
523{
524 track->pid = current->pid;
525 track->stack = save_stack(flags);
526}
527
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700528struct kasan_alloc_meta *get_alloc_info(struct kmem_cache *cache,
529 const void *object)
530{
Alexander Potapenkocd110162016-03-25 14:22:08 -0700531 BUILD_BUG_ON(sizeof(struct kasan_alloc_meta) > 32);
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700532 return (void *)object + cache->kasan_info.alloc_meta_offset;
533}
534
535struct kasan_free_meta *get_free_info(struct kmem_cache *cache,
536 const void *object)
537{
Alexander Potapenkocd110162016-03-25 14:22:08 -0700538 BUILD_BUG_ON(sizeof(struct kasan_free_meta) > 32);
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700539 return (void *)object + cache->kasan_info.free_meta_offset;
540}
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700541
Andrey Ryabininb3cbd9b2016-08-02 14:02:52 -0700542void kasan_init_slab_obj(struct kmem_cache *cache, const void *object)
543{
544 struct kasan_alloc_meta *alloc_info;
545
546 if (!(cache->flags & SLAB_KASAN))
547 return;
548
549 alloc_info = get_alloc_info(cache, object);
550 __memset(alloc_info, 0, sizeof(*alloc_info));
551}
552
Alexander Potapenko505f5dc2016-03-25 14:22:02 -0700553void kasan_slab_alloc(struct kmem_cache *cache, void *object, gfp_t flags)
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800554{
Alexander Potapenko505f5dc2016-03-25 14:22:02 -0700555 kasan_kmalloc(cache, object, cache->object_size, flags);
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800556}
557
Andrey Ryabinin9b75a862016-06-24 14:49:34 -0700558static void kasan_poison_slab_free(struct kmem_cache *cache, void *object)
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800559{
560 unsigned long size = cache->object_size;
561 unsigned long rounded_up_size = round_up(size, KASAN_SHADOW_SCALE_SIZE);
562
563 /* RCU slabs could be legally used after free within the RCU period */
564 if (unlikely(cache->flags & SLAB_DESTROY_BY_RCU))
565 return;
566
Alexander Potapenko55834c52016-05-20 16:59:11 -0700567 kasan_poison_shadow(object, rounded_up_size, KASAN_KMALLOC_FREE);
568}
569
570bool kasan_slab_free(struct kmem_cache *cache, void *object)
571{
Andrey Ryabininb3cbd9b2016-08-02 14:02:52 -0700572 s8 shadow_byte;
573
Alexander Potapenko55834c52016-05-20 16:59:11 -0700574 /* RCU slabs could be legally used after free within the RCU period */
575 if (unlikely(cache->flags & SLAB_DESTROY_BY_RCU))
576 return false;
577
Andrey Ryabininb3cbd9b2016-08-02 14:02:52 -0700578 shadow_byte = READ_ONCE(*(s8 *)kasan_mem_to_shadow(object));
579 if (shadow_byte < 0 || shadow_byte >= KASAN_SHADOW_SCALE_SIZE) {
Andrey Ryabinin7e088972016-08-02 14:02:55 -0700580 kasan_report_double_free(cache, object, shadow_byte);
Andrey Ryabininb3cbd9b2016-08-02 14:02:52 -0700581 return true;
Alexander Potapenko55834c52016-05-20 16:59:11 -0700582 }
Andrey Ryabininb3cbd9b2016-08-02 14:02:52 -0700583
584 kasan_poison_slab_free(cache, object);
585
586 if (unlikely(!(cache->flags & SLAB_KASAN)))
587 return false;
588
589 set_track(&get_alloc_info(cache, object)->free_track, GFP_NOWAIT);
590 quarantine_put(get_free_info(cache, object), cache);
591 return true;
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800592}
593
Alexander Potapenko505f5dc2016-03-25 14:22:02 -0700594void kasan_kmalloc(struct kmem_cache *cache, const void *object, size_t size,
595 gfp_t flags)
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800596{
597 unsigned long redzone_start;
598 unsigned long redzone_end;
599
Andrey Ryabinin4b3ec5a2016-08-02 14:02:43 -0700600 if (gfpflags_allow_blocking(flags))
Alexander Potapenko55834c52016-05-20 16:59:11 -0700601 quarantine_reduce();
602
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800603 if (unlikely(object == NULL))
604 return;
605
606 redzone_start = round_up((unsigned long)(object + size),
607 KASAN_SHADOW_SCALE_SIZE);
608 redzone_end = round_up((unsigned long)object + cache->object_size,
609 KASAN_SHADOW_SCALE_SIZE);
610
611 kasan_unpoison_shadow(object, size);
612 kasan_poison_shadow((void *)redzone_start, redzone_end - redzone_start,
613 KASAN_KMALLOC_REDZONE);
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700614
Andrey Ryabininb3cbd9b2016-08-02 14:02:52 -0700615 if (cache->flags & SLAB_KASAN)
616 set_track(&get_alloc_info(cache, object)->alloc_track, flags);
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800617}
618EXPORT_SYMBOL(kasan_kmalloc);
619
Alexander Potapenko505f5dc2016-03-25 14:22:02 -0700620void kasan_kmalloc_large(const void *ptr, size_t size, gfp_t flags)
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800621{
622 struct page *page;
623 unsigned long redzone_start;
624 unsigned long redzone_end;
625
Andrey Ryabinin4b3ec5a2016-08-02 14:02:43 -0700626 if (gfpflags_allow_blocking(flags))
Alexander Potapenko55834c52016-05-20 16:59:11 -0700627 quarantine_reduce();
628
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800629 if (unlikely(ptr == NULL))
630 return;
631
632 page = virt_to_page(ptr);
633 redzone_start = round_up((unsigned long)(ptr + size),
634 KASAN_SHADOW_SCALE_SIZE);
635 redzone_end = (unsigned long)ptr + (PAGE_SIZE << compound_order(page));
636
637 kasan_unpoison_shadow(ptr, size);
638 kasan_poison_shadow((void *)redzone_start, redzone_end - redzone_start,
639 KASAN_PAGE_REDZONE);
640}
641
Alexander Potapenko505f5dc2016-03-25 14:22:02 -0700642void kasan_krealloc(const void *object, size_t size, gfp_t flags)
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800643{
644 struct page *page;
645
646 if (unlikely(object == ZERO_SIZE_PTR))
647 return;
648
649 page = virt_to_head_page(object);
650
651 if (unlikely(!PageSlab(page)))
Alexander Potapenko505f5dc2016-03-25 14:22:02 -0700652 kasan_kmalloc_large(object, size, flags);
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800653 else
Alexander Potapenko505f5dc2016-03-25 14:22:02 -0700654 kasan_kmalloc(page->slab_cache, object, size, flags);
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800655}
656
Andrey Ryabinin9b75a862016-06-24 14:49:34 -0700657void kasan_poison_kfree(void *ptr)
Andrey Ryabinin92393612015-04-15 16:15:05 -0700658{
659 struct page *page;
660
661 page = virt_to_head_page(ptr);
662
663 if (unlikely(!PageSlab(page)))
664 kasan_poison_shadow(ptr, PAGE_SIZE << compound_order(page),
665 KASAN_FREE_PAGE);
666 else
Andrey Ryabinin9b75a862016-06-24 14:49:34 -0700667 kasan_poison_slab_free(page->slab_cache, ptr);
Andrey Ryabinin92393612015-04-15 16:15:05 -0700668}
669
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800670void kasan_kfree_large(const void *ptr)
671{
672 struct page *page = virt_to_page(ptr);
673
674 kasan_poison_shadow(ptr, PAGE_SIZE << compound_order(page),
675 KASAN_FREE_PAGE);
676}
677
Andrey Ryabininbebf56a2015-02-13 14:40:17 -0800678int kasan_module_alloc(void *addr, size_t size)
679{
680 void *ret;
681 size_t shadow_size;
682 unsigned long shadow_start;
683
684 shadow_start = (unsigned long)kasan_mem_to_shadow(addr);
685 shadow_size = round_up(size >> KASAN_SHADOW_SCALE_SHIFT,
686 PAGE_SIZE);
687
688 if (WARN_ON(!PAGE_ALIGNED(shadow_start)))
689 return -EINVAL;
690
691 ret = __vmalloc_node_range(shadow_size, 1, shadow_start,
692 shadow_start + shadow_size,
693 GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
694 PAGE_KERNEL, VM_NO_GUARD, NUMA_NO_NODE,
695 __builtin_return_address(0));
Andrey Ryabinina5af5aa2015-03-12 16:26:11 -0700696
697 if (ret) {
698 find_vm_area(addr)->flags |= VM_KASAN;
Andrey Ryabinin45937252015-11-20 15:57:18 -0800699 kmemleak_ignore(ret);
Andrey Ryabinina5af5aa2015-03-12 16:26:11 -0700700 return 0;
701 }
702
703 return -ENOMEM;
Andrey Ryabininbebf56a2015-02-13 14:40:17 -0800704}
705
Andrey Ryabinina5af5aa2015-03-12 16:26:11 -0700706void kasan_free_shadow(const struct vm_struct *vm)
Andrey Ryabininbebf56a2015-02-13 14:40:17 -0800707{
Andrey Ryabinina5af5aa2015-03-12 16:26:11 -0700708 if (vm->flags & VM_KASAN)
709 vfree(kasan_mem_to_shadow(vm->addr));
Andrey Ryabininbebf56a2015-02-13 14:40:17 -0800710}
711
712static void register_global(struct kasan_global *global)
713{
714 size_t aligned_size = round_up(global->size, KASAN_SHADOW_SCALE_SIZE);
715
716 kasan_unpoison_shadow(global->beg, global->size);
717
718 kasan_poison_shadow(global->beg + aligned_size,
719 global->size_with_redzone - aligned_size,
720 KASAN_GLOBAL_REDZONE);
721}
722
723void __asan_register_globals(struct kasan_global *globals, size_t size)
724{
725 int i;
726
727 for (i = 0; i < size; i++)
728 register_global(&globals[i]);
729}
730EXPORT_SYMBOL(__asan_register_globals);
731
732void __asan_unregister_globals(struct kasan_global *globals, size_t size)
733{
734}
735EXPORT_SYMBOL(__asan_unregister_globals);
736
Andrey Ryabinin936bb4b2016-05-20 16:59:20 -0700737#define DEFINE_ASAN_LOAD_STORE(size) \
738 void __asan_load##size(unsigned long addr) \
739 { \
740 check_memory_region_inline(addr, size, false, _RET_IP_);\
741 } \
742 EXPORT_SYMBOL(__asan_load##size); \
743 __alias(__asan_load##size) \
744 void __asan_load##size##_noabort(unsigned long); \
745 EXPORT_SYMBOL(__asan_load##size##_noabort); \
746 void __asan_store##size(unsigned long addr) \
747 { \
748 check_memory_region_inline(addr, size, true, _RET_IP_); \
749 } \
750 EXPORT_SYMBOL(__asan_store##size); \
751 __alias(__asan_store##size) \
752 void __asan_store##size##_noabort(unsigned long); \
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800753 EXPORT_SYMBOL(__asan_store##size##_noabort)
754
755DEFINE_ASAN_LOAD_STORE(1);
756DEFINE_ASAN_LOAD_STORE(2);
757DEFINE_ASAN_LOAD_STORE(4);
758DEFINE_ASAN_LOAD_STORE(8);
759DEFINE_ASAN_LOAD_STORE(16);
760
761void __asan_loadN(unsigned long addr, size_t size)
762{
Andrey Ryabinin936bb4b2016-05-20 16:59:20 -0700763 check_memory_region(addr, size, false, _RET_IP_);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800764}
765EXPORT_SYMBOL(__asan_loadN);
766
767__alias(__asan_loadN)
768void __asan_loadN_noabort(unsigned long, size_t);
769EXPORT_SYMBOL(__asan_loadN_noabort);
770
771void __asan_storeN(unsigned long addr, size_t size)
772{
Andrey Ryabinin936bb4b2016-05-20 16:59:20 -0700773 check_memory_region(addr, size, true, _RET_IP_);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800774}
775EXPORT_SYMBOL(__asan_storeN);
776
777__alias(__asan_storeN)
778void __asan_storeN_noabort(unsigned long, size_t);
779EXPORT_SYMBOL(__asan_storeN_noabort);
780
781/* to shut up compiler complaints */
782void __asan_handle_no_return(void) {}
783EXPORT_SYMBOL(__asan_handle_no_return);
Andrey Ryabinin786a8952015-02-13 14:39:21 -0800784
Dmitry Vyukov828347f2016-11-30 15:54:16 -0800785/* Emitted by compiler to poison large objects when they go out of scope. */
786void __asan_poison_stack_memory(const void *addr, size_t size)
787{
788 /*
789 * Addr is KASAN_SHADOW_SCALE_SIZE-aligned and the object is surrounded
790 * by redzones, so we simply round up size to simplify logic.
791 */
792 kasan_poison_shadow(addr, round_up(size, KASAN_SHADOW_SCALE_SIZE),
793 KASAN_USE_AFTER_SCOPE);
794}
795EXPORT_SYMBOL(__asan_poison_stack_memory);
796
797/* Emitted by compiler to unpoison large objects when they go into scope. */
798void __asan_unpoison_stack_memory(const void *addr, size_t size)
799{
800 kasan_unpoison_shadow(addr, size);
801}
802EXPORT_SYMBOL(__asan_unpoison_stack_memory);
803
Andrey Ryabinin786a8952015-02-13 14:39:21 -0800804#ifdef CONFIG_MEMORY_HOTPLUG
805static int kasan_mem_notifier(struct notifier_block *nb,
806 unsigned long action, void *data)
807{
808 return (action == MEM_GOING_ONLINE) ? NOTIFY_BAD : NOTIFY_OK;
809}
810
811static int __init kasan_memhotplug_init(void)
812{
Shuah Khan91a4c272016-06-08 15:33:45 -0700813 pr_info("WARNING: KASAN doesn't support memory hot-add\n");
814 pr_info("Memory hot-add will be disabled\n");
Andrey Ryabinin786a8952015-02-13 14:39:21 -0800815
816 hotplug_memory_notifier(kasan_mem_notifier, 0);
817
818 return 0;
819}
820
821module_init(kasan_memhotplug_init);
822#endif