blob: 77c103748728cdbe4e1e9d3e319fe3039402fcdf [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
Andrey Ryabininc634d802017-07-10 15:50:24 -0700137static __always_inline bool memory_is_poisoned_2_4_8(unsigned long addr,
138 unsigned long size)
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800139{
Andrey Ryabininc634d802017-07-10 15:50:24 -0700140 u8 *shadow_addr = (u8 *)kasan_mem_to_shadow((void *)addr);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800141
Andrey Ryabininc634d802017-07-10 15:50:24 -0700142 /*
143 * Access crosses 8(shadow size)-byte boundary. Such access maps
144 * into 2 shadow bytes, so we need to check them both.
145 */
146 if (unlikely(((addr + size - 1) & KASAN_SHADOW_MASK) < size - 1))
147 return *shadow_addr || memory_is_poisoned_1(addr + size - 1);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800148
Andrey Ryabininc634d802017-07-10 15:50:24 -0700149 return memory_is_poisoned_1(addr + size - 1);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800150}
151
152static __always_inline bool memory_is_poisoned_16(unsigned long addr)
153{
Andrey Ryabininc634d802017-07-10 15:50:24 -0700154 u16 *shadow_addr = (u16 *)kasan_mem_to_shadow((void *)addr);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800155
Andrey Ryabininc634d802017-07-10 15:50:24 -0700156 /* Unaligned 16-bytes access maps into 3 shadow bytes. */
157 if (unlikely(!IS_ALIGNED(addr, KASAN_SHADOW_SCALE_SIZE)))
158 return *shadow_addr || memory_is_poisoned_1(addr + 15);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800159
Andrey Ryabininc634d802017-07-10 15:50:24 -0700160 return *shadow_addr;
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800161}
162
Joonsoo Kimf5bd62c2017-07-10 15:50:37 -0700163static __always_inline unsigned long bytes_is_nonzero(const u8 *start,
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800164 size_t size)
165{
166 while (size) {
167 if (unlikely(*start))
168 return (unsigned long)start;
169 start++;
170 size--;
171 }
172
173 return 0;
174}
175
Joonsoo Kimf5bd62c2017-07-10 15:50:37 -0700176static __always_inline unsigned long memory_is_nonzero(const void *start,
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800177 const void *end)
178{
179 unsigned int words;
180 unsigned long ret;
181 unsigned int prefix = (unsigned long)start % 8;
182
183 if (end - start <= 16)
Joonsoo Kimf5bd62c2017-07-10 15:50:37 -0700184 return bytes_is_nonzero(start, end - start);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800185
186 if (prefix) {
187 prefix = 8 - prefix;
Joonsoo Kimf5bd62c2017-07-10 15:50:37 -0700188 ret = bytes_is_nonzero(start, prefix);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800189 if (unlikely(ret))
190 return ret;
191 start += prefix;
192 }
193
194 words = (end - start) / 8;
195 while (words) {
196 if (unlikely(*(u64 *)start))
Joonsoo Kimf5bd62c2017-07-10 15:50:37 -0700197 return bytes_is_nonzero(start, 8);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800198 start += 8;
199 words--;
200 }
201
Joonsoo Kimf5bd62c2017-07-10 15:50:37 -0700202 return bytes_is_nonzero(start, (end - start) % 8);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800203}
204
205static __always_inline bool memory_is_poisoned_n(unsigned long addr,
206 size_t size)
207{
208 unsigned long ret;
209
Joonsoo Kimf5bd62c2017-07-10 15:50:37 -0700210 ret = memory_is_nonzero(kasan_mem_to_shadow((void *)addr),
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800211 kasan_mem_to_shadow((void *)addr + size - 1) + 1);
212
213 if (unlikely(ret)) {
214 unsigned long last_byte = addr + size - 1;
215 s8 *last_shadow = (s8 *)kasan_mem_to_shadow((void *)last_byte);
216
217 if (unlikely(ret != (unsigned long)last_shadow ||
Wang Longe0d57712015-11-05 18:51:18 -0800218 ((long)(last_byte & KASAN_SHADOW_MASK) >= *last_shadow)))
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800219 return true;
220 }
221 return false;
222}
223
224static __always_inline bool memory_is_poisoned(unsigned long addr, size_t size)
225{
226 if (__builtin_constant_p(size)) {
227 switch (size) {
228 case 1:
229 return memory_is_poisoned_1(addr);
230 case 2:
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800231 case 4:
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800232 case 8:
Andrey Ryabininc634d802017-07-10 15:50:24 -0700233 return memory_is_poisoned_2_4_8(addr, size);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800234 case 16:
235 return memory_is_poisoned_16(addr);
236 default:
237 BUILD_BUG();
238 }
239 }
240
241 return memory_is_poisoned_n(addr, size);
242}
243
Andrey Ryabinin936bb4b2016-05-20 16:59:20 -0700244static __always_inline void check_memory_region_inline(unsigned long addr,
245 size_t size, bool write,
246 unsigned long ret_ip)
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800247{
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800248 if (unlikely(size == 0))
249 return;
250
251 if (unlikely((void *)addr <
252 kasan_shadow_to_mem((void *)KASAN_SHADOW_START))) {
Andrey Ryabinin936bb4b2016-05-20 16:59:20 -0700253 kasan_report(addr, size, write, ret_ip);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800254 return;
255 }
256
257 if (likely(!memory_is_poisoned(addr, size)))
258 return;
259
Andrey Ryabinin936bb4b2016-05-20 16:59:20 -0700260 kasan_report(addr, size, write, ret_ip);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800261}
262
Andrey Ryabinin936bb4b2016-05-20 16:59:20 -0700263static void check_memory_region(unsigned long addr,
264 size_t size, bool write,
265 unsigned long ret_ip)
266{
267 check_memory_region_inline(addr, size, write, ret_ip);
268}
Andrey Ryabinin393f2032015-02-13 14:39:56 -0800269
Dmitry Vyukovf06e8c52017-06-22 16:14:17 +0200270void kasan_check_read(const volatile void *p, unsigned int size)
Andrey Ryabinin64f8eba2016-05-20 16:59:28 -0700271{
272 check_memory_region((unsigned long)p, size, false, _RET_IP_);
273}
274EXPORT_SYMBOL(kasan_check_read);
275
Dmitry Vyukovf06e8c52017-06-22 16:14:17 +0200276void kasan_check_write(const volatile void *p, unsigned int size)
Andrey Ryabinin64f8eba2016-05-20 16:59:28 -0700277{
278 check_memory_region((unsigned long)p, size, true, _RET_IP_);
279}
280EXPORT_SYMBOL(kasan_check_write);
281
Andrey Ryabinin393f2032015-02-13 14:39:56 -0800282#undef memset
283void *memset(void *addr, int c, size_t len)
284{
Andrey Ryabinin936bb4b2016-05-20 16:59:20 -0700285 check_memory_region((unsigned long)addr, len, true, _RET_IP_);
Andrey Ryabinin393f2032015-02-13 14:39:56 -0800286
287 return __memset(addr, c, len);
288}
289
290#undef memmove
291void *memmove(void *dest, const void *src, size_t len)
292{
Andrey Ryabinin936bb4b2016-05-20 16:59:20 -0700293 check_memory_region((unsigned long)src, len, false, _RET_IP_);
294 check_memory_region((unsigned long)dest, len, true, _RET_IP_);
Andrey Ryabinin393f2032015-02-13 14:39:56 -0800295
296 return __memmove(dest, src, len);
297}
298
299#undef memcpy
300void *memcpy(void *dest, const void *src, size_t len)
301{
Andrey Ryabinin936bb4b2016-05-20 16:59:20 -0700302 check_memory_region((unsigned long)src, len, false, _RET_IP_);
303 check_memory_region((unsigned long)dest, len, true, _RET_IP_);
Andrey Ryabinin393f2032015-02-13 14:39:56 -0800304
305 return __memcpy(dest, src, len);
306}
307
Andrey Ryabininb8c73fc2015-02-13 14:39:28 -0800308void kasan_alloc_pages(struct page *page, unsigned int order)
309{
310 if (likely(!PageHighMem(page)))
311 kasan_unpoison_shadow(page_address(page), PAGE_SIZE << order);
312}
313
314void kasan_free_pages(struct page *page, unsigned int order)
315{
316 if (likely(!PageHighMem(page)))
317 kasan_poison_shadow(page_address(page),
318 PAGE_SIZE << order,
319 KASAN_FREE_PAGE);
320}
321
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700322/*
323 * Adaptive redzone policy taken from the userspace AddressSanitizer runtime.
324 * For larger allocations larger redzones are used.
325 */
326static size_t optimal_redzone(size_t object_size)
327{
328 int rz =
329 object_size <= 64 - 16 ? 16 :
330 object_size <= 128 - 32 ? 32 :
331 object_size <= 512 - 64 ? 64 :
332 object_size <= 4096 - 128 ? 128 :
333 object_size <= (1 << 14) - 256 ? 256 :
334 object_size <= (1 << 15) - 512 ? 512 :
335 object_size <= (1 << 16) - 1024 ? 1024 : 2048;
336 return rz;
337}
338
339void kasan_cache_create(struct kmem_cache *cache, size_t *size,
Alexey Dobriyand50112e2017-11-15 17:32:18 -0800340 slab_flags_t *flags)
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700341{
342 int redzone_adjust;
Alexander Potapenko80a92012016-07-28 15:49:07 -0700343 int orig_size = *size;
344
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700345 /* Add alloc meta. */
346 cache->kasan_info.alloc_meta_offset = *size;
347 *size += sizeof(struct kasan_alloc_meta);
348
349 /* Add free meta. */
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -0800350 if (cache->flags & SLAB_TYPESAFE_BY_RCU || cache->ctor ||
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700351 cache->object_size < sizeof(struct kasan_free_meta)) {
352 cache->kasan_info.free_meta_offset = *size;
353 *size += sizeof(struct kasan_free_meta);
354 }
355 redzone_adjust = optimal_redzone(cache->object_size) -
356 (*size - cache->object_size);
Alexander Potapenko80a92012016-07-28 15:49:07 -0700357
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700358 if (redzone_adjust > 0)
359 *size += redzone_adjust;
Alexander Potapenko80a92012016-07-28 15:49:07 -0700360
361 *size = min(KMALLOC_MAX_SIZE, max(*size, cache->object_size +
362 optimal_redzone(cache->object_size)));
363
364 /*
365 * If the metadata doesn't fit, don't enable KASAN at all.
366 */
367 if (*size <= cache->kasan_info.alloc_meta_offset ||
368 *size <= cache->kasan_info.free_meta_offset) {
369 cache->kasan_info.alloc_meta_offset = 0;
370 cache->kasan_info.free_meta_offset = 0;
371 *size = orig_size;
372 return;
373 }
374
375 *flags |= SLAB_KASAN;
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700376}
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700377
Alexander Potapenko55834c52016-05-20 16:59:11 -0700378void kasan_cache_shrink(struct kmem_cache *cache)
379{
380 quarantine_remove_cache(cache);
381}
382
Greg Thelenf9fa1d92017-02-24 15:00:05 -0800383void kasan_cache_shutdown(struct kmem_cache *cache)
Alexander Potapenko55834c52016-05-20 16:59:11 -0700384{
385 quarantine_remove_cache(cache);
386}
387
Alexander Potapenko80a92012016-07-28 15:49:07 -0700388size_t kasan_metadata_size(struct kmem_cache *cache)
389{
390 return (cache->kasan_info.alloc_meta_offset ?
391 sizeof(struct kasan_alloc_meta) : 0) +
392 (cache->kasan_info.free_meta_offset ?
393 sizeof(struct kasan_free_meta) : 0);
394}
395
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800396void kasan_poison_slab(struct page *page)
397{
398 kasan_poison_shadow(page_address(page),
399 PAGE_SIZE << compound_order(page),
400 KASAN_KMALLOC_REDZONE);
401}
402
403void kasan_unpoison_object_data(struct kmem_cache *cache, void *object)
404{
405 kasan_unpoison_shadow(object, cache->object_size);
406}
407
408void kasan_poison_object_data(struct kmem_cache *cache, void *object)
409{
410 kasan_poison_shadow(object,
411 round_up(cache->object_size, KASAN_SHADOW_SCALE_SIZE),
412 KASAN_KMALLOC_REDZONE);
413}
414
Alexander Potapenkocd110162016-03-25 14:22:08 -0700415static inline int in_irqentry_text(unsigned long ptr)
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700416{
Alexander Potapenkocd110162016-03-25 14:22:08 -0700417 return (ptr >= (unsigned long)&__irqentry_text_start &&
418 ptr < (unsigned long)&__irqentry_text_end) ||
419 (ptr >= (unsigned long)&__softirqentry_text_start &&
420 ptr < (unsigned long)&__softirqentry_text_end);
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700421}
422
Alexander Potapenkocd110162016-03-25 14:22:08 -0700423static inline void filter_irq_stacks(struct stack_trace *trace)
424{
425 int i;
426
427 if (!trace->nr_entries)
428 return;
429 for (i = 0; i < trace->nr_entries; i++)
430 if (in_irqentry_text(trace->entries[i])) {
431 /* Include the irqentry function into the stack. */
432 trace->nr_entries = i + 1;
433 break;
434 }
435}
436
437static inline depot_stack_handle_t save_stack(gfp_t flags)
438{
439 unsigned long entries[KASAN_STACK_DEPTH];
440 struct stack_trace trace = {
441 .nr_entries = 0,
442 .entries = entries,
443 .max_entries = KASAN_STACK_DEPTH,
444 .skip = 0
445 };
446
447 save_stack_trace(&trace);
448 filter_irq_stacks(&trace);
449 if (trace.nr_entries != 0 &&
450 trace.entries[trace.nr_entries-1] == ULONG_MAX)
451 trace.nr_entries--;
452
453 return depot_save_stack(&trace, flags);
454}
455
456static inline void set_track(struct kasan_track *track, gfp_t flags)
457{
458 track->pid = current->pid;
459 track->stack = save_stack(flags);
460}
461
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700462struct kasan_alloc_meta *get_alloc_info(struct kmem_cache *cache,
463 const void *object)
464{
Alexander Potapenkocd110162016-03-25 14:22:08 -0700465 BUILD_BUG_ON(sizeof(struct kasan_alloc_meta) > 32);
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700466 return (void *)object + cache->kasan_info.alloc_meta_offset;
467}
468
469struct kasan_free_meta *get_free_info(struct kmem_cache *cache,
470 const void *object)
471{
Alexander Potapenkocd110162016-03-25 14:22:08 -0700472 BUILD_BUG_ON(sizeof(struct kasan_free_meta) > 32);
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700473 return (void *)object + cache->kasan_info.free_meta_offset;
474}
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700475
Andrey Ryabininb3cbd9b2016-08-02 14:02:52 -0700476void kasan_init_slab_obj(struct kmem_cache *cache, const void *object)
477{
478 struct kasan_alloc_meta *alloc_info;
479
480 if (!(cache->flags & SLAB_KASAN))
481 return;
482
483 alloc_info = get_alloc_info(cache, object);
484 __memset(alloc_info, 0, sizeof(*alloc_info));
485}
486
Alexander Potapenko505f5dc2016-03-25 14:22:02 -0700487void kasan_slab_alloc(struct kmem_cache *cache, void *object, gfp_t flags)
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800488{
Alexander Potapenko505f5dc2016-03-25 14:22:02 -0700489 kasan_kmalloc(cache, object, cache->object_size, flags);
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800490}
491
Andrey Ryabinin9b75a862016-06-24 14:49:34 -0700492static void kasan_poison_slab_free(struct kmem_cache *cache, void *object)
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800493{
494 unsigned long size = cache->object_size;
495 unsigned long rounded_up_size = round_up(size, KASAN_SHADOW_SCALE_SIZE);
496
497 /* RCU slabs could be legally used after free within the RCU period */
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -0800498 if (unlikely(cache->flags & SLAB_TYPESAFE_BY_RCU))
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800499 return;
500
Alexander Potapenko55834c52016-05-20 16:59:11 -0700501 kasan_poison_shadow(object, rounded_up_size, KASAN_KMALLOC_FREE);
502}
503
Dmitry Vyukovee3ce772018-02-06 15:36:27 -0800504bool kasan_slab_free(struct kmem_cache *cache, void *object, unsigned long ip)
Alexander Potapenko55834c52016-05-20 16:59:11 -0700505{
Andrey Ryabininb3cbd9b2016-08-02 14:02:52 -0700506 s8 shadow_byte;
507
Alexander Potapenko55834c52016-05-20 16:59:11 -0700508 /* RCU slabs could be legally used after free within the RCU period */
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -0800509 if (unlikely(cache->flags & SLAB_TYPESAFE_BY_RCU))
Alexander Potapenko55834c52016-05-20 16:59:11 -0700510 return false;
511
Andrey Ryabininb3cbd9b2016-08-02 14:02:52 -0700512 shadow_byte = READ_ONCE(*(s8 *)kasan_mem_to_shadow(object));
513 if (shadow_byte < 0 || shadow_byte >= KASAN_SHADOW_SCALE_SIZE) {
Dmitry Vyukovee3ce772018-02-06 15:36:27 -0800514 kasan_report_invalid_free(object, ip);
Andrey Ryabininb3cbd9b2016-08-02 14:02:52 -0700515 return true;
Alexander Potapenko55834c52016-05-20 16:59:11 -0700516 }
Andrey Ryabininb3cbd9b2016-08-02 14:02:52 -0700517
518 kasan_poison_slab_free(cache, object);
519
520 if (unlikely(!(cache->flags & SLAB_KASAN)))
521 return false;
522
523 set_track(&get_alloc_info(cache, object)->free_track, GFP_NOWAIT);
524 quarantine_put(get_free_info(cache, object), cache);
525 return true;
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800526}
527
Alexander Potapenko505f5dc2016-03-25 14:22:02 -0700528void kasan_kmalloc(struct kmem_cache *cache, const void *object, size_t size,
529 gfp_t flags)
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800530{
531 unsigned long redzone_start;
532 unsigned long redzone_end;
533
Andrey Ryabinin4b3ec5a2016-08-02 14:02:43 -0700534 if (gfpflags_allow_blocking(flags))
Alexander Potapenko55834c52016-05-20 16:59:11 -0700535 quarantine_reduce();
536
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800537 if (unlikely(object == NULL))
538 return;
539
540 redzone_start = round_up((unsigned long)(object + size),
541 KASAN_SHADOW_SCALE_SIZE);
542 redzone_end = round_up((unsigned long)object + cache->object_size,
543 KASAN_SHADOW_SCALE_SIZE);
544
545 kasan_unpoison_shadow(object, size);
546 kasan_poison_shadow((void *)redzone_start, redzone_end - redzone_start,
547 KASAN_KMALLOC_REDZONE);
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700548
Andrey Ryabininb3cbd9b2016-08-02 14:02:52 -0700549 if (cache->flags & SLAB_KASAN)
550 set_track(&get_alloc_info(cache, object)->alloc_track, flags);
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800551}
552EXPORT_SYMBOL(kasan_kmalloc);
553
Alexander Potapenko505f5dc2016-03-25 14:22:02 -0700554void kasan_kmalloc_large(const void *ptr, size_t size, gfp_t flags)
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800555{
556 struct page *page;
557 unsigned long redzone_start;
558 unsigned long redzone_end;
559
Andrey Ryabinin4b3ec5a2016-08-02 14:02:43 -0700560 if (gfpflags_allow_blocking(flags))
Alexander Potapenko55834c52016-05-20 16:59:11 -0700561 quarantine_reduce();
562
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800563 if (unlikely(ptr == NULL))
564 return;
565
566 page = virt_to_page(ptr);
567 redzone_start = round_up((unsigned long)(ptr + size),
568 KASAN_SHADOW_SCALE_SIZE);
569 redzone_end = (unsigned long)ptr + (PAGE_SIZE << compound_order(page));
570
571 kasan_unpoison_shadow(ptr, size);
572 kasan_poison_shadow((void *)redzone_start, redzone_end - redzone_start,
573 KASAN_PAGE_REDZONE);
574}
575
Alexander Potapenko505f5dc2016-03-25 14:22:02 -0700576void kasan_krealloc(const void *object, size_t size, gfp_t flags)
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800577{
578 struct page *page;
579
580 if (unlikely(object == ZERO_SIZE_PTR))
581 return;
582
583 page = virt_to_head_page(object);
584
585 if (unlikely(!PageSlab(page)))
Alexander Potapenko505f5dc2016-03-25 14:22:02 -0700586 kasan_kmalloc_large(object, size, flags);
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800587 else
Alexander Potapenko505f5dc2016-03-25 14:22:02 -0700588 kasan_kmalloc(page->slab_cache, object, size, flags);
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800589}
590
Dmitry Vyukov6860f632018-02-06 15:36:30 -0800591void kasan_poison_kfree(void *ptr, unsigned long ip)
Andrey Ryabinin92393612015-04-15 16:15:05 -0700592{
593 struct page *page;
594
595 page = virt_to_head_page(ptr);
596
Dmitry Vyukov6860f632018-02-06 15:36:30 -0800597 if (unlikely(!PageSlab(page))) {
598 if (ptr != page_address(page)) {
599 kasan_report_invalid_free(ptr, ip);
600 return;
601 }
Andrey Ryabinin92393612015-04-15 16:15:05 -0700602 kasan_poison_shadow(ptr, PAGE_SIZE << compound_order(page),
603 KASAN_FREE_PAGE);
Dmitry Vyukov6860f632018-02-06 15:36:30 -0800604 } else {
Andrey Ryabinin9b75a862016-06-24 14:49:34 -0700605 kasan_poison_slab_free(page->slab_cache, ptr);
Dmitry Vyukov6860f632018-02-06 15:36:30 -0800606 }
Andrey Ryabinin92393612015-04-15 16:15:05 -0700607}
608
Dmitry Vyukovee3ce772018-02-06 15:36:27 -0800609void kasan_kfree_large(void *ptr, unsigned long ip)
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800610{
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800611 if (ptr != page_address(virt_to_head_page(ptr)))
Dmitry Vyukovee3ce772018-02-06 15:36:27 -0800612 kasan_report_invalid_free(ptr, ip);
Dmitry Vyukov47adccc2018-02-06 15:36:23 -0800613 /* The object will be poisoned by page_alloc. */
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800614}
615
Andrey Ryabininbebf56a2015-02-13 14:40:17 -0800616int kasan_module_alloc(void *addr, size_t size)
617{
618 void *ret;
619 size_t shadow_size;
620 unsigned long shadow_start;
621
622 shadow_start = (unsigned long)kasan_mem_to_shadow(addr);
623 shadow_size = round_up(size >> KASAN_SHADOW_SCALE_SHIFT,
624 PAGE_SIZE);
625
626 if (WARN_ON(!PAGE_ALIGNED(shadow_start)))
627 return -EINVAL;
628
629 ret = __vmalloc_node_range(shadow_size, 1, shadow_start,
630 shadow_start + shadow_size,
Michal Hocko19809c22017-05-08 15:57:44 -0700631 GFP_KERNEL | __GFP_ZERO,
Andrey Ryabininbebf56a2015-02-13 14:40:17 -0800632 PAGE_KERNEL, VM_NO_GUARD, NUMA_NO_NODE,
633 __builtin_return_address(0));
Andrey Ryabinina5af5aa2015-03-12 16:26:11 -0700634
635 if (ret) {
636 find_vm_area(addr)->flags |= VM_KASAN;
Andrey Ryabinin45937252015-11-20 15:57:18 -0800637 kmemleak_ignore(ret);
Andrey Ryabinina5af5aa2015-03-12 16:26:11 -0700638 return 0;
639 }
640
641 return -ENOMEM;
Andrey Ryabininbebf56a2015-02-13 14:40:17 -0800642}
643
Andrey Ryabinina5af5aa2015-03-12 16:26:11 -0700644void kasan_free_shadow(const struct vm_struct *vm)
Andrey Ryabininbebf56a2015-02-13 14:40:17 -0800645{
Andrey Ryabinina5af5aa2015-03-12 16:26:11 -0700646 if (vm->flags & VM_KASAN)
647 vfree(kasan_mem_to_shadow(vm->addr));
Andrey Ryabininbebf56a2015-02-13 14:40:17 -0800648}
649
650static void register_global(struct kasan_global *global)
651{
652 size_t aligned_size = round_up(global->size, KASAN_SHADOW_SCALE_SIZE);
653
654 kasan_unpoison_shadow(global->beg, global->size);
655
656 kasan_poison_shadow(global->beg + aligned_size,
657 global->size_with_redzone - aligned_size,
658 KASAN_GLOBAL_REDZONE);
659}
660
661void __asan_register_globals(struct kasan_global *globals, size_t size)
662{
663 int i;
664
665 for (i = 0; i < size; i++)
666 register_global(&globals[i]);
667}
668EXPORT_SYMBOL(__asan_register_globals);
669
670void __asan_unregister_globals(struct kasan_global *globals, size_t size)
671{
672}
673EXPORT_SYMBOL(__asan_unregister_globals);
674
Andrey Ryabinin936bb4b2016-05-20 16:59:20 -0700675#define DEFINE_ASAN_LOAD_STORE(size) \
676 void __asan_load##size(unsigned long addr) \
677 { \
678 check_memory_region_inline(addr, size, false, _RET_IP_);\
679 } \
680 EXPORT_SYMBOL(__asan_load##size); \
681 __alias(__asan_load##size) \
682 void __asan_load##size##_noabort(unsigned long); \
683 EXPORT_SYMBOL(__asan_load##size##_noabort); \
684 void __asan_store##size(unsigned long addr) \
685 { \
686 check_memory_region_inline(addr, size, true, _RET_IP_); \
687 } \
688 EXPORT_SYMBOL(__asan_store##size); \
689 __alias(__asan_store##size) \
690 void __asan_store##size##_noabort(unsigned long); \
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800691 EXPORT_SYMBOL(__asan_store##size##_noabort)
692
693DEFINE_ASAN_LOAD_STORE(1);
694DEFINE_ASAN_LOAD_STORE(2);
695DEFINE_ASAN_LOAD_STORE(4);
696DEFINE_ASAN_LOAD_STORE(8);
697DEFINE_ASAN_LOAD_STORE(16);
698
699void __asan_loadN(unsigned long addr, size_t size)
700{
Andrey Ryabinin936bb4b2016-05-20 16:59:20 -0700701 check_memory_region(addr, size, false, _RET_IP_);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800702}
703EXPORT_SYMBOL(__asan_loadN);
704
705__alias(__asan_loadN)
706void __asan_loadN_noabort(unsigned long, size_t);
707EXPORT_SYMBOL(__asan_loadN_noabort);
708
709void __asan_storeN(unsigned long addr, size_t size)
710{
Andrey Ryabinin936bb4b2016-05-20 16:59:20 -0700711 check_memory_region(addr, size, true, _RET_IP_);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800712}
713EXPORT_SYMBOL(__asan_storeN);
714
715__alias(__asan_storeN)
716void __asan_storeN_noabort(unsigned long, size_t);
717EXPORT_SYMBOL(__asan_storeN_noabort);
718
719/* to shut up compiler complaints */
720void __asan_handle_no_return(void) {}
721EXPORT_SYMBOL(__asan_handle_no_return);
Andrey Ryabinin786a8952015-02-13 14:39:21 -0800722
Dmitry Vyukov828347f2016-11-30 15:54:16 -0800723/* Emitted by compiler to poison large objects when they go out of scope. */
724void __asan_poison_stack_memory(const void *addr, size_t size)
725{
726 /*
727 * Addr is KASAN_SHADOW_SCALE_SIZE-aligned and the object is surrounded
728 * by redzones, so we simply round up size to simplify logic.
729 */
730 kasan_poison_shadow(addr, round_up(size, KASAN_SHADOW_SCALE_SIZE),
731 KASAN_USE_AFTER_SCOPE);
732}
733EXPORT_SYMBOL(__asan_poison_stack_memory);
734
735/* Emitted by compiler to unpoison large objects when they go into scope. */
736void __asan_unpoison_stack_memory(const void *addr, size_t size)
737{
738 kasan_unpoison_shadow(addr, size);
739}
740EXPORT_SYMBOL(__asan_unpoison_stack_memory);
741
Paul Lawrence342061e2018-02-06 15:36:11 -0800742/* Emitted by compiler to poison alloca()ed objects. */
743void __asan_alloca_poison(unsigned long addr, size_t size)
744{
745 size_t rounded_up_size = round_up(size, KASAN_SHADOW_SCALE_SIZE);
746 size_t padding_size = round_up(size, KASAN_ALLOCA_REDZONE_SIZE) -
747 rounded_up_size;
748 size_t rounded_down_size = round_down(size, KASAN_SHADOW_SCALE_SIZE);
749
750 const void *left_redzone = (const void *)(addr -
751 KASAN_ALLOCA_REDZONE_SIZE);
752 const void *right_redzone = (const void *)(addr + rounded_up_size);
753
754 WARN_ON(!IS_ALIGNED(addr, KASAN_ALLOCA_REDZONE_SIZE));
755
756 kasan_unpoison_shadow((const void *)(addr + rounded_down_size),
757 size - rounded_down_size);
758 kasan_poison_shadow(left_redzone, KASAN_ALLOCA_REDZONE_SIZE,
759 KASAN_ALLOCA_LEFT);
760 kasan_poison_shadow(right_redzone,
761 padding_size + KASAN_ALLOCA_REDZONE_SIZE,
762 KASAN_ALLOCA_RIGHT);
763}
764EXPORT_SYMBOL(__asan_alloca_poison);
765
766/* Emitted by compiler to unpoison alloca()ed areas when the stack unwinds. */
767void __asan_allocas_unpoison(const void *stack_top, const void *stack_bottom)
768{
769 if (unlikely(!stack_top || stack_top > stack_bottom))
770 return;
771
772 kasan_unpoison_shadow(stack_top, stack_bottom - stack_top);
773}
774EXPORT_SYMBOL(__asan_allocas_unpoison);
775
Alexander Potapenkod3215992018-02-06 15:36:20 -0800776/* Emitted by the compiler to [un]poison local variables. */
777#define DEFINE_ASAN_SET_SHADOW(byte) \
778 void __asan_set_shadow_##byte(const void *addr, size_t size) \
779 { \
780 __memset((void *)addr, 0x##byte, size); \
781 } \
782 EXPORT_SYMBOL(__asan_set_shadow_##byte)
783
784DEFINE_ASAN_SET_SHADOW(00);
785DEFINE_ASAN_SET_SHADOW(f1);
786DEFINE_ASAN_SET_SHADOW(f2);
787DEFINE_ASAN_SET_SHADOW(f3);
788DEFINE_ASAN_SET_SHADOW(f5);
789DEFINE_ASAN_SET_SHADOW(f8);
790
Andrey Ryabinin786a8952015-02-13 14:39:21 -0800791#ifdef CONFIG_MEMORY_HOTPLUG
Andrey Ryabininfa69b592017-07-10 15:50:34 -0700792static int __meminit kasan_mem_notifier(struct notifier_block *nb,
Andrey Ryabinin786a8952015-02-13 14:39:21 -0800793 unsigned long action, void *data)
794{
Andrey Ryabininfa69b592017-07-10 15:50:34 -0700795 struct memory_notify *mem_data = data;
796 unsigned long nr_shadow_pages, start_kaddr, shadow_start;
797 unsigned long shadow_end, shadow_size;
798
799 nr_shadow_pages = mem_data->nr_pages >> KASAN_SHADOW_SCALE_SHIFT;
800 start_kaddr = (unsigned long)pfn_to_kaddr(mem_data->start_pfn);
801 shadow_start = (unsigned long)kasan_mem_to_shadow((void *)start_kaddr);
802 shadow_size = nr_shadow_pages << PAGE_SHIFT;
803 shadow_end = shadow_start + shadow_size;
804
805 if (WARN_ON(mem_data->nr_pages % KASAN_SHADOW_SCALE_SIZE) ||
806 WARN_ON(start_kaddr % (KASAN_SHADOW_SCALE_SIZE << PAGE_SHIFT)))
807 return NOTIFY_BAD;
808
809 switch (action) {
810 case MEM_GOING_ONLINE: {
811 void *ret;
812
813 ret = __vmalloc_node_range(shadow_size, PAGE_SIZE, shadow_start,
814 shadow_end, GFP_KERNEL,
815 PAGE_KERNEL, VM_NO_GUARD,
816 pfn_to_nid(mem_data->start_pfn),
817 __builtin_return_address(0));
818 if (!ret)
819 return NOTIFY_BAD;
820
821 kmemleak_ignore(ret);
822 return NOTIFY_OK;
823 }
824 case MEM_OFFLINE:
825 vfree((void *)shadow_start);
826 }
827
828 return NOTIFY_OK;
Andrey Ryabinin786a8952015-02-13 14:39:21 -0800829}
830
831static int __init kasan_memhotplug_init(void)
832{
Andrey Ryabinin786a8952015-02-13 14:39:21 -0800833 hotplug_memory_notifier(kasan_mem_notifier, 0);
834
835 return 0;
836}
837
838module_init(kasan_memhotplug_init);
839#endif