blob: 80bbe62b16cd2427d2e3819478188d88804b4b0a [file] [log] [blame]
Andrey Konovalove886bf92018-12-28 00:31:14 -08001// SPDX-License-Identifier: GPL-2.0
Andrey Konovalovbffa9862018-12-28 00:29:45 -08002/*
3 * This file contains common generic and tag-based KASAN 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 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 *
15 */
16
Arnd Bergmannbcf6f552019-03-05 15:41:27 -080017#define __KASAN_INTERNAL
18
Andrey Konovalovbffa9862018-12-28 00:29:45 -080019#include <linux/export.h>
20#include <linux/interrupt.h>
21#include <linux/init.h>
22#include <linux/kasan.h>
23#include <linux/kernel.h>
24#include <linux/kmemleak.h>
25#include <linux/linkage.h>
26#include <linux/memblock.h>
27#include <linux/memory.h>
28#include <linux/mm.h>
29#include <linux/module.h>
30#include <linux/printk.h>
31#include <linux/sched.h>
32#include <linux/sched/task_stack.h>
33#include <linux/slab.h>
34#include <linux/stacktrace.h>
35#include <linux/string.h>
36#include <linux/types.h>
37#include <linux/vmalloc.h>
38#include <linux/bug.h>
39
40#include "kasan.h"
41#include "../slab.h"
42
43static inline int in_irqentry_text(unsigned long ptr)
44{
45 return (ptr >= (unsigned long)&__irqentry_text_start &&
46 ptr < (unsigned long)&__irqentry_text_end) ||
47 (ptr >= (unsigned long)&__softirqentry_text_start &&
48 ptr < (unsigned long)&__softirqentry_text_end);
49}
50
51static inline void filter_irq_stacks(struct stack_trace *trace)
52{
53 int i;
54
55 if (!trace->nr_entries)
56 return;
57 for (i = 0; i < trace->nr_entries; i++)
58 if (in_irqentry_text(trace->entries[i])) {
59 /* Include the irqentry function into the stack. */
60 trace->nr_entries = i + 1;
61 break;
62 }
63}
64
65static inline depot_stack_handle_t save_stack(gfp_t flags)
66{
67 unsigned long entries[KASAN_STACK_DEPTH];
68 struct stack_trace trace = {
69 .nr_entries = 0,
70 .entries = entries,
71 .max_entries = KASAN_STACK_DEPTH,
72 .skip = 0
73 };
74
75 save_stack_trace(&trace);
76 filter_irq_stacks(&trace);
77 if (trace.nr_entries != 0 &&
78 trace.entries[trace.nr_entries-1] == ULONG_MAX)
79 trace.nr_entries--;
80
81 return depot_save_stack(&trace, flags);
82}
83
84static inline void set_track(struct kasan_track *track, gfp_t flags)
85{
86 track->pid = current->pid;
87 track->stack = save_stack(flags);
88}
89
90void kasan_enable_current(void)
91{
92 current->kasan_depth++;
93}
94
95void kasan_disable_current(void)
96{
97 current->kasan_depth--;
98}
99
100void kasan_check_read(const volatile void *p, unsigned int size)
101{
102 check_memory_region((unsigned long)p, size, false, _RET_IP_);
103}
104EXPORT_SYMBOL(kasan_check_read);
105
106void kasan_check_write(const volatile void *p, unsigned int size)
107{
108 check_memory_region((unsigned long)p, size, true, _RET_IP_);
109}
110EXPORT_SYMBOL(kasan_check_write);
111
112#undef memset
113void *memset(void *addr, int c, size_t len)
114{
115 check_memory_region((unsigned long)addr, len, true, _RET_IP_);
116
117 return __memset(addr, c, len);
118}
119
120#undef memmove
121void *memmove(void *dest, const void *src, size_t len)
122{
123 check_memory_region((unsigned long)src, len, false, _RET_IP_);
124 check_memory_region((unsigned long)dest, len, true, _RET_IP_);
125
126 return __memmove(dest, src, len);
127}
128
129#undef memcpy
130void *memcpy(void *dest, const void *src, size_t len)
131{
132 check_memory_region((unsigned long)src, len, false, _RET_IP_);
133 check_memory_region((unsigned long)dest, len, true, _RET_IP_);
134
135 return __memcpy(dest, src, len);
136}
137
138/*
139 * Poisons the shadow memory for 'size' bytes starting from 'addr'.
140 * Memory addresses should be aligned to KASAN_SHADOW_SCALE_SIZE.
141 */
142void kasan_poison_shadow(const void *address, size_t size, u8 value)
143{
144 void *shadow_start, *shadow_end;
145
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800146 /*
147 * Perform shadow offset calculation based on untagged address, as
148 * some of the callers (e.g. kasan_poison_object_data) pass tagged
149 * addresses to this function.
150 */
151 address = reset_tag(address);
152
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800153 shadow_start = kasan_mem_to_shadow(address);
154 shadow_end = kasan_mem_to_shadow(address + size);
155
156 __memset(shadow_start, value, shadow_end - shadow_start);
157}
158
159void kasan_unpoison_shadow(const void *address, size_t size)
160{
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800161 u8 tag = get_tag(address);
162
163 /*
164 * Perform shadow offset calculation based on untagged address, as
165 * some of the callers (e.g. kasan_unpoison_object_data) pass tagged
166 * addresses to this function.
167 */
168 address = reset_tag(address);
169
170 kasan_poison_shadow(address, size, tag);
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800171
172 if (size & KASAN_SHADOW_MASK) {
173 u8 *shadow = (u8 *)kasan_mem_to_shadow(address + size);
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800174
175 if (IS_ENABLED(CONFIG_KASAN_SW_TAGS))
176 *shadow = tag;
177 else
178 *shadow = size & KASAN_SHADOW_MASK;
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800179 }
180}
181
182static void __kasan_unpoison_stack(struct task_struct *task, const void *sp)
183{
184 void *base = task_stack_page(task);
185 size_t size = sp - base;
186
187 kasan_unpoison_shadow(base, size);
188}
189
190/* Unpoison the entire stack for a task. */
191void kasan_unpoison_task_stack(struct task_struct *task)
192{
193 __kasan_unpoison_stack(task, task_stack_page(task) + THREAD_SIZE);
194}
195
196/* Unpoison the stack for the current task beyond a watermark sp value. */
197asmlinkage void kasan_unpoison_task_stack_below(const void *watermark)
198{
199 /*
200 * Calculate the task stack base address. Avoid using 'current'
201 * because this function is called by early resume code which hasn't
202 * yet set up the percpu register (%gs).
203 */
204 void *base = (void *)((unsigned long)watermark & ~(THREAD_SIZE - 1));
205
206 kasan_unpoison_shadow(base, watermark - base);
207}
208
209/*
210 * Clear all poison for the region between the current SP and a provided
211 * watermark value, as is sometimes required prior to hand-crafted asm function
212 * returns in the middle of functions.
213 */
214void kasan_unpoison_stack_above_sp_to(const void *watermark)
215{
216 const void *sp = __builtin_frame_address(0);
217 size_t size = watermark - sp;
218
219 if (WARN_ON(sp > watermark))
220 return;
221 kasan_unpoison_shadow(sp, size);
222}
223
224void kasan_alloc_pages(struct page *page, unsigned int order)
225{
Andrey Konovalov2813b9c2018-12-28 00:30:57 -0800226 u8 tag;
227 unsigned long i;
228
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800229 if (unlikely(PageHighMem(page)))
230 return;
Andrey Konovalov2813b9c2018-12-28 00:30:57 -0800231
232 tag = random_tag();
233 for (i = 0; i < (1 << order); i++)
234 page_kasan_tag_set(page + i, tag);
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800235 kasan_unpoison_shadow(page_address(page), PAGE_SIZE << order);
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800236}
237
238void kasan_free_pages(struct page *page, unsigned int order)
239{
240 if (likely(!PageHighMem(page)))
241 kasan_poison_shadow(page_address(page),
242 PAGE_SIZE << order,
243 KASAN_FREE_PAGE);
244}
245
246/*
247 * Adaptive redzone policy taken from the userspace AddressSanitizer runtime.
248 * For larger allocations larger redzones are used.
249 */
250static inline unsigned int optimal_redzone(unsigned int object_size)
251{
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800252 if (IS_ENABLED(CONFIG_KASAN_SW_TAGS))
253 return 0;
254
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800255 return
256 object_size <= 64 - 16 ? 16 :
257 object_size <= 128 - 32 ? 32 :
258 object_size <= 512 - 64 ? 64 :
259 object_size <= 4096 - 128 ? 128 :
260 object_size <= (1 << 14) - 256 ? 256 :
261 object_size <= (1 << 15) - 512 ? 512 :
262 object_size <= (1 << 16) - 1024 ? 1024 : 2048;
263}
264
265void kasan_cache_create(struct kmem_cache *cache, unsigned int *size,
266 slab_flags_t *flags)
267{
268 unsigned int orig_size = *size;
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800269 unsigned int redzone_size;
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800270 int redzone_adjust;
271
272 /* Add alloc meta. */
273 cache->kasan_info.alloc_meta_offset = *size;
274 *size += sizeof(struct kasan_alloc_meta);
275
276 /* Add free meta. */
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800277 if (IS_ENABLED(CONFIG_KASAN_GENERIC) &&
278 (cache->flags & SLAB_TYPESAFE_BY_RCU || cache->ctor ||
279 cache->object_size < sizeof(struct kasan_free_meta))) {
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800280 cache->kasan_info.free_meta_offset = *size;
281 *size += sizeof(struct kasan_free_meta);
282 }
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800283
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800284 redzone_size = optimal_redzone(cache->object_size);
285 redzone_adjust = redzone_size - (*size - cache->object_size);
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800286 if (redzone_adjust > 0)
287 *size += redzone_adjust;
288
289 *size = min_t(unsigned int, KMALLOC_MAX_SIZE,
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800290 max(*size, cache->object_size + redzone_size));
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800291
292 /*
293 * If the metadata doesn't fit, don't enable KASAN at all.
294 */
295 if (*size <= cache->kasan_info.alloc_meta_offset ||
296 *size <= cache->kasan_info.free_meta_offset) {
297 cache->kasan_info.alloc_meta_offset = 0;
298 cache->kasan_info.free_meta_offset = 0;
299 *size = orig_size;
300 return;
301 }
302
303 *flags |= SLAB_KASAN;
304}
305
306size_t kasan_metadata_size(struct kmem_cache *cache)
307{
308 return (cache->kasan_info.alloc_meta_offset ?
309 sizeof(struct kasan_alloc_meta) : 0) +
310 (cache->kasan_info.free_meta_offset ?
311 sizeof(struct kasan_free_meta) : 0);
312}
313
314struct kasan_alloc_meta *get_alloc_info(struct kmem_cache *cache,
315 const void *object)
316{
317 BUILD_BUG_ON(sizeof(struct kasan_alloc_meta) > 32);
318 return (void *)object + cache->kasan_info.alloc_meta_offset;
319}
320
321struct kasan_free_meta *get_free_info(struct kmem_cache *cache,
322 const void *object)
323{
324 BUILD_BUG_ON(sizeof(struct kasan_free_meta) > 32);
325 return (void *)object + cache->kasan_info.free_meta_offset;
326}
327
328void kasan_poison_slab(struct page *page)
329{
Andrey Konovalov2813b9c2018-12-28 00:30:57 -0800330 unsigned long i;
331
332 for (i = 0; i < (1 << compound_order(page)); i++)
333 page_kasan_tag_reset(page + i);
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800334 kasan_poison_shadow(page_address(page),
335 PAGE_SIZE << compound_order(page),
336 KASAN_KMALLOC_REDZONE);
337}
338
339void kasan_unpoison_object_data(struct kmem_cache *cache, void *object)
340{
341 kasan_unpoison_shadow(object, cache->object_size);
342}
343
344void kasan_poison_object_data(struct kmem_cache *cache, void *object)
345{
346 kasan_poison_shadow(object,
347 round_up(cache->object_size, KASAN_SHADOW_SCALE_SIZE),
348 KASAN_KMALLOC_REDZONE);
349}
350
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800351/*
Andrey Konovalova3fe7cd2019-01-08 15:23:18 -0800352 * This function assigns a tag to an object considering the following:
353 * 1. A cache might have a constructor, which might save a pointer to a slab
354 * object somewhere (e.g. in the object itself). We preassign a tag for
355 * each object in caches with constructors during slab creation and reuse
356 * the same tag each time a particular object is allocated.
357 * 2. A cache might be SLAB_TYPESAFE_BY_RCU, which means objects can be
358 * accessed after being freed. We preassign tags for objects in these
359 * caches as well.
360 * 3. For SLAB allocator we can't preassign tags randomly since the freelist
361 * is stored as an array of indexes instead of a linked list. Assign tags
362 * based on objects indexes, so that objects that are next to each other
363 * get different tags.
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800364 */
Andrey Konovalova3fe7cd2019-01-08 15:23:18 -0800365static u8 assign_tag(struct kmem_cache *cache, const void *object,
Andrey Konovalove1db95b2019-02-20 22:19:01 -0800366 bool init, bool keep_tag)
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800367{
Andrey Konovalove1db95b2019-02-20 22:19:01 -0800368 /*
369 * 1. When an object is kmalloc()'ed, two hooks are called:
370 * kasan_slab_alloc() and kasan_kmalloc(). We assign the
371 * tag only in the first one.
372 * 2. We reuse the same tag for krealloc'ed objects.
373 */
374 if (keep_tag)
Andrey Konovalova3fe7cd2019-01-08 15:23:18 -0800375 return get_tag(object);
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800376
Andrey Konovalova3fe7cd2019-01-08 15:23:18 -0800377 /*
378 * If the cache neither has a constructor nor has SLAB_TYPESAFE_BY_RCU
379 * set, assign a tag when the object is being allocated (init == false).
380 */
381 if (!cache->ctor && !(cache->flags & SLAB_TYPESAFE_BY_RCU))
382 return init ? KASAN_TAG_KERNEL : random_tag();
383
384 /* For caches that either have a constructor or SLAB_TYPESAFE_BY_RCU: */
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800385#ifdef CONFIG_SLAB
Andrey Konovalova3fe7cd2019-01-08 15:23:18 -0800386 /* For SLAB assign tags based on the object index in the freelist. */
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800387 return (u8)obj_to_index(cache, virt_to_page(object), (void *)object);
388#else
Andrey Konovalova3fe7cd2019-01-08 15:23:18 -0800389 /*
390 * For SLUB assign a random tag during slab creation, otherwise reuse
391 * the already assigned tag.
392 */
393 return init ? random_tag() : get_tag(object);
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800394#endif
395}
396
Andrey Konovalov66afc7f2018-12-28 00:31:01 -0800397void * __must_check kasan_init_slab_obj(struct kmem_cache *cache,
398 const void *object)
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800399{
400 struct kasan_alloc_meta *alloc_info;
401
402 if (!(cache->flags & SLAB_KASAN))
403 return (void *)object;
404
405 alloc_info = get_alloc_info(cache, object);
406 __memset(alloc_info, 0, sizeof(*alloc_info));
407
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800408 if (IS_ENABLED(CONFIG_KASAN_SW_TAGS))
Andrey Konovalova3fe7cd2019-01-08 15:23:18 -0800409 object = set_tag(object,
410 assign_tag(cache, object, true, false));
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800411
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800412 return (void *)object;
413}
414
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800415static inline bool shadow_invalid(u8 tag, s8 shadow_byte)
416{
417 if (IS_ENABLED(CONFIG_KASAN_GENERIC))
418 return shadow_byte < 0 ||
419 shadow_byte >= KASAN_SHADOW_SCALE_SIZE;
420 else
421 return tag != (u8)shadow_byte;
422}
423
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800424static bool __kasan_slab_free(struct kmem_cache *cache, void *object,
425 unsigned long ip, bool quarantine)
426{
427 s8 shadow_byte;
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800428 u8 tag;
429 void *tagged_object;
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800430 unsigned long rounded_up_size;
431
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800432 tag = get_tag(object);
433 tagged_object = object;
434 object = reset_tag(object);
435
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800436 if (unlikely(nearest_obj(cache, virt_to_head_page(object), object) !=
437 object)) {
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800438 kasan_report_invalid_free(tagged_object, ip);
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800439 return true;
440 }
441
442 /* RCU slabs could be legally used after free within the RCU period */
443 if (unlikely(cache->flags & SLAB_TYPESAFE_BY_RCU))
444 return false;
445
446 shadow_byte = READ_ONCE(*(s8 *)kasan_mem_to_shadow(object));
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800447 if (shadow_invalid(tag, shadow_byte)) {
448 kasan_report_invalid_free(tagged_object, ip);
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800449 return true;
450 }
451
452 rounded_up_size = round_up(cache->object_size, KASAN_SHADOW_SCALE_SIZE);
453 kasan_poison_shadow(object, rounded_up_size, KASAN_KMALLOC_FREE);
454
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800455 if ((IS_ENABLED(CONFIG_KASAN_GENERIC) && !quarantine) ||
456 unlikely(!(cache->flags & SLAB_KASAN)))
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800457 return false;
458
459 set_track(&get_alloc_info(cache, object)->free_track, GFP_NOWAIT);
460 quarantine_put(get_free_info(cache, object), cache);
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800461
462 return IS_ENABLED(CONFIG_KASAN_GENERIC);
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800463}
464
465bool kasan_slab_free(struct kmem_cache *cache, void *object, unsigned long ip)
466{
467 return __kasan_slab_free(cache, object, ip, true);
468}
469
Andrey Konovalova3fe7cd2019-01-08 15:23:18 -0800470static void *__kasan_kmalloc(struct kmem_cache *cache, const void *object,
Andrey Konovalove1db95b2019-02-20 22:19:01 -0800471 size_t size, gfp_t flags, bool keep_tag)
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800472{
473 unsigned long redzone_start;
474 unsigned long redzone_end;
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800475 u8 tag;
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800476
477 if (gfpflags_allow_blocking(flags))
478 quarantine_reduce();
479
480 if (unlikely(object == NULL))
481 return NULL;
482
483 redzone_start = round_up((unsigned long)(object + size),
484 KASAN_SHADOW_SCALE_SIZE);
485 redzone_end = round_up((unsigned long)object + cache->object_size,
486 KASAN_SHADOW_SCALE_SIZE);
487
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800488 if (IS_ENABLED(CONFIG_KASAN_SW_TAGS))
Andrey Konovalove1db95b2019-02-20 22:19:01 -0800489 tag = assign_tag(cache, object, false, keep_tag);
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800490
491 /* Tag is ignored in set_tag without CONFIG_KASAN_SW_TAGS */
492 kasan_unpoison_shadow(set_tag(object, tag), size);
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800493 kasan_poison_shadow((void *)redzone_start, redzone_end - redzone_start,
494 KASAN_KMALLOC_REDZONE);
495
496 if (cache->flags & SLAB_KASAN)
497 set_track(&get_alloc_info(cache, object)->alloc_track, flags);
498
Andrey Konovalov7f94ffb2018-12-28 00:30:50 -0800499 return set_tag(object, tag);
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800500}
Andrey Konovalova3fe7cd2019-01-08 15:23:18 -0800501
Andrey Konovalove1db95b2019-02-20 22:19:01 -0800502void * __must_check kasan_slab_alloc(struct kmem_cache *cache, void *object,
503 gfp_t flags)
504{
505 return __kasan_kmalloc(cache, object, cache->object_size, flags, false);
506}
507
Andrey Konovalova3fe7cd2019-01-08 15:23:18 -0800508void * __must_check kasan_kmalloc(struct kmem_cache *cache, const void *object,
509 size_t size, gfp_t flags)
510{
Andrey Konovalove1db95b2019-02-20 22:19:01 -0800511 return __kasan_kmalloc(cache, object, size, flags, true);
Andrey Konovalova3fe7cd2019-01-08 15:23:18 -0800512}
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800513EXPORT_SYMBOL(kasan_kmalloc);
514
Andrey Konovalov66afc7f2018-12-28 00:31:01 -0800515void * __must_check kasan_kmalloc_large(const void *ptr, size_t size,
516 gfp_t flags)
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800517{
518 struct page *page;
519 unsigned long redzone_start;
520 unsigned long redzone_end;
521
522 if (gfpflags_allow_blocking(flags))
523 quarantine_reduce();
524
525 if (unlikely(ptr == NULL))
526 return NULL;
527
528 page = virt_to_page(ptr);
529 redzone_start = round_up((unsigned long)(ptr + size),
530 KASAN_SHADOW_SCALE_SIZE);
531 redzone_end = (unsigned long)ptr + (PAGE_SIZE << compound_order(page));
532
533 kasan_unpoison_shadow(ptr, size);
534 kasan_poison_shadow((void *)redzone_start, redzone_end - redzone_start,
535 KASAN_PAGE_REDZONE);
536
537 return (void *)ptr;
538}
539
Andrey Konovalov66afc7f2018-12-28 00:31:01 -0800540void * __must_check kasan_krealloc(const void *object, size_t size, gfp_t flags)
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800541{
542 struct page *page;
543
544 if (unlikely(object == ZERO_SIZE_PTR))
545 return (void *)object;
546
547 page = virt_to_head_page(object);
548
549 if (unlikely(!PageSlab(page)))
550 return kasan_kmalloc_large(object, size, flags);
551 else
Andrey Konovalova3fe7cd2019-01-08 15:23:18 -0800552 return __kasan_kmalloc(page->slab_cache, object, size,
553 flags, true);
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800554}
555
556void kasan_poison_kfree(void *ptr, unsigned long ip)
557{
558 struct page *page;
559
560 page = virt_to_head_page(ptr);
561
562 if (unlikely(!PageSlab(page))) {
Andrey Konovalov2813b9c2018-12-28 00:30:57 -0800563 if (ptr != page_address(page)) {
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800564 kasan_report_invalid_free(ptr, ip);
565 return;
566 }
567 kasan_poison_shadow(ptr, PAGE_SIZE << compound_order(page),
568 KASAN_FREE_PAGE);
569 } else {
570 __kasan_slab_free(page->slab_cache, ptr, ip, false);
571 }
572}
573
574void kasan_kfree_large(void *ptr, unsigned long ip)
575{
Andrey Konovalov2813b9c2018-12-28 00:30:57 -0800576 if (ptr != page_address(virt_to_head_page(ptr)))
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800577 kasan_report_invalid_free(ptr, ip);
578 /* The object will be poisoned by page_alloc. */
579}
580
581int kasan_module_alloc(void *addr, size_t size)
582{
583 void *ret;
584 size_t scaled_size;
585 size_t shadow_size;
586 unsigned long shadow_start;
587
588 shadow_start = (unsigned long)kasan_mem_to_shadow(addr);
589 scaled_size = (size + KASAN_SHADOW_MASK) >> KASAN_SHADOW_SCALE_SHIFT;
590 shadow_size = round_up(scaled_size, PAGE_SIZE);
591
592 if (WARN_ON(!PAGE_ALIGNED(shadow_start)))
593 return -EINVAL;
594
595 ret = __vmalloc_node_range(shadow_size, 1, shadow_start,
596 shadow_start + shadow_size,
Andrey Konovalov080eb832018-12-28 00:30:09 -0800597 GFP_KERNEL,
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800598 PAGE_KERNEL, VM_NO_GUARD, NUMA_NO_NODE,
599 __builtin_return_address(0));
600
601 if (ret) {
Andrey Konovalov080eb832018-12-28 00:30:09 -0800602 __memset(ret, KASAN_SHADOW_INIT, shadow_size);
Andrey Konovalovbffa9862018-12-28 00:29:45 -0800603 find_vm_area(addr)->flags |= VM_KASAN;
604 kmemleak_ignore(ret);
605 return 0;
606 }
607
608 return -ENOMEM;
609}
610
611void kasan_free_shadow(const struct vm_struct *vm)
612{
613 if (vm->flags & VM_KASAN)
614 vfree(kasan_mem_to_shadow(vm->addr));
615}
616
617#ifdef CONFIG_MEMORY_HOTPLUG
618static bool shadow_mapped(unsigned long addr)
619{
620 pgd_t *pgd = pgd_offset_k(addr);
621 p4d_t *p4d;
622 pud_t *pud;
623 pmd_t *pmd;
624 pte_t *pte;
625
626 if (pgd_none(*pgd))
627 return false;
628 p4d = p4d_offset(pgd, addr);
629 if (p4d_none(*p4d))
630 return false;
631 pud = pud_offset(p4d, addr);
632 if (pud_none(*pud))
633 return false;
634
635 /*
636 * We can't use pud_large() or pud_huge(), the first one is
637 * arch-specific, the last one depends on HUGETLB_PAGE. So let's abuse
638 * pud_bad(), if pud is bad then it's bad because it's huge.
639 */
640 if (pud_bad(*pud))
641 return true;
642 pmd = pmd_offset(pud, addr);
643 if (pmd_none(*pmd))
644 return false;
645
646 if (pmd_bad(*pmd))
647 return true;
648 pte = pte_offset_kernel(pmd, addr);
649 return !pte_none(*pte);
650}
651
652static int __meminit kasan_mem_notifier(struct notifier_block *nb,
653 unsigned long action, void *data)
654{
655 struct memory_notify *mem_data = data;
656 unsigned long nr_shadow_pages, start_kaddr, shadow_start;
657 unsigned long shadow_end, shadow_size;
658
659 nr_shadow_pages = mem_data->nr_pages >> KASAN_SHADOW_SCALE_SHIFT;
660 start_kaddr = (unsigned long)pfn_to_kaddr(mem_data->start_pfn);
661 shadow_start = (unsigned long)kasan_mem_to_shadow((void *)start_kaddr);
662 shadow_size = nr_shadow_pages << PAGE_SHIFT;
663 shadow_end = shadow_start + shadow_size;
664
665 if (WARN_ON(mem_data->nr_pages % KASAN_SHADOW_SCALE_SIZE) ||
666 WARN_ON(start_kaddr % (KASAN_SHADOW_SCALE_SIZE << PAGE_SHIFT)))
667 return NOTIFY_BAD;
668
669 switch (action) {
670 case MEM_GOING_ONLINE: {
671 void *ret;
672
673 /*
674 * If shadow is mapped already than it must have been mapped
675 * during the boot. This could happen if we onlining previously
676 * offlined memory.
677 */
678 if (shadow_mapped(shadow_start))
679 return NOTIFY_OK;
680
681 ret = __vmalloc_node_range(shadow_size, PAGE_SIZE, shadow_start,
682 shadow_end, GFP_KERNEL,
683 PAGE_KERNEL, VM_NO_GUARD,
684 pfn_to_nid(mem_data->start_pfn),
685 __builtin_return_address(0));
686 if (!ret)
687 return NOTIFY_BAD;
688
689 kmemleak_ignore(ret);
690 return NOTIFY_OK;
691 }
692 case MEM_CANCEL_ONLINE:
693 case MEM_OFFLINE: {
694 struct vm_struct *vm;
695
696 /*
697 * shadow_start was either mapped during boot by kasan_init()
698 * or during memory online by __vmalloc_node_range().
699 * In the latter case we can use vfree() to free shadow.
700 * Non-NULL result of the find_vm_area() will tell us if
701 * that was the second case.
702 *
703 * Currently it's not possible to free shadow mapped
704 * during boot by kasan_init(). It's because the code
705 * to do that hasn't been written yet. So we'll just
706 * leak the memory.
707 */
708 vm = find_vm_area((void *)shadow_start);
709 if (vm)
710 vfree((void *)shadow_start);
711 }
712 }
713
714 return NOTIFY_OK;
715}
716
717static int __init kasan_memhotplug_init(void)
718{
719 hotplug_memory_notifier(kasan_mem_notifier, 0);
720
721 return 0;
722}
723
724core_initcall(kasan_memhotplug_init);
725#endif