kasan, mm: don't save alloc stacks twice
[linux-block.git] / mm / kasan / shadow.c
CommitLineData
bb359dbc
AK
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * This file contains KASAN runtime code that manages shadow memory for
4 * generic and software tag-based KASAN modes.
5 *
6 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
7 * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
8 *
9 * Some code borrowed from https://github.com/xairy/kasan-prototype by
10 * Andrey Konovalov <andreyknvl@gmail.com>
11 */
12
13#include <linux/init.h>
14#include <linux/kasan.h>
15#include <linux/kernel.h>
2b830526 16#include <linux/kfence.h>
bb359dbc
AK
17#include <linux/kmemleak.h>
18#include <linux/memory.h>
19#include <linux/mm.h>
20#include <linux/string.h>
21#include <linux/types.h>
22#include <linux/vmalloc.h>
23
24#include <asm/cacheflush.h>
25#include <asm/tlbflush.h>
26
27#include "kasan.h"
28
29bool __kasan_check_read(const volatile void *p, unsigned int size)
30{
f00748bf 31 return kasan_check_range((unsigned long)p, size, false, _RET_IP_);
bb359dbc
AK
32}
33EXPORT_SYMBOL(__kasan_check_read);
34
35bool __kasan_check_write(const volatile void *p, unsigned int size)
36{
f00748bf 37 return kasan_check_range((unsigned long)p, size, true, _RET_IP_);
bb359dbc
AK
38}
39EXPORT_SYMBOL(__kasan_check_write);
40
41#undef memset
42void *memset(void *addr, int c, size_t len)
43{
f00748bf 44 if (!kasan_check_range((unsigned long)addr, len, true, _RET_IP_))
bb359dbc
AK
45 return NULL;
46
47 return __memset(addr, c, len);
48}
49
50#ifdef __HAVE_ARCH_MEMMOVE
51#undef memmove
52void *memmove(void *dest, const void *src, size_t len)
53{
f00748bf
AK
54 if (!kasan_check_range((unsigned long)src, len, false, _RET_IP_) ||
55 !kasan_check_range((unsigned long)dest, len, true, _RET_IP_))
bb359dbc
AK
56 return NULL;
57
58 return __memmove(dest, src, len);
59}
60#endif
61
62#undef memcpy
63void *memcpy(void *dest, const void *src, size_t len)
64{
f00748bf
AK
65 if (!kasan_check_range((unsigned long)src, len, false, _RET_IP_) ||
66 !kasan_check_range((unsigned long)dest, len, true, _RET_IP_))
bb359dbc
AK
67 return NULL;
68
69 return __memcpy(dest, src, len);
70}
71
72/*
73 * Poisons the shadow memory for 'size' bytes starting from 'addr'.
74 * Memory addresses should be aligned to KASAN_GRANULE_SIZE.
75 */
f00748bf 76void kasan_poison(const void *address, size_t size, u8 value)
bb359dbc
AK
77{
78 void *shadow_start, *shadow_end;
79
80 /*
81 * Perform shadow offset calculation based on untagged address, as
82 * some of the callers (e.g. kasan_poison_object_data) pass tagged
83 * addresses to this function.
84 */
c0054c56 85 address = kasan_reset_tag(address);
d99f6a10 86 size = round_up(size, KASAN_GRANULE_SIZE);
bb359dbc 87
2b830526
AP
88 /* Skip KFENCE memory if called explicitly outside of sl*b. */
89 if (is_kfence_address(address))
90 return;
91
bb359dbc
AK
92 shadow_start = kasan_mem_to_shadow(address);
93 shadow_end = kasan_mem_to_shadow(address + size);
94
95 __memset(shadow_start, value, shadow_end - shadow_start);
96}
573a4809 97EXPORT_SYMBOL(kasan_poison);
bb359dbc 98
f00748bf 99void kasan_unpoison(const void *address, size_t size)
bb359dbc
AK
100{
101 u8 tag = get_tag(address);
102
103 /*
104 * Perform shadow offset calculation based on untagged address, as
105 * some of the callers (e.g. kasan_unpoison_object_data) pass tagged
106 * addresses to this function.
107 */
c0054c56 108 address = kasan_reset_tag(address);
bb359dbc 109
2b830526
AP
110 /*
111 * Skip KFENCE memory if called explicitly outside of sl*b. Also note
112 * that calls to ksize(), where size is not a multiple of machine-word
113 * size, would otherwise poison the invalid portion of the word.
114 */
115 if (is_kfence_address(address))
116 return;
117
f00748bf 118 kasan_poison(address, size, tag);
bb359dbc
AK
119
120 if (size & KASAN_GRANULE_MASK) {
121 u8 *shadow = (u8 *)kasan_mem_to_shadow(address + size);
122
123 if (IS_ENABLED(CONFIG_KASAN_SW_TAGS))
124 *shadow = tag;
2e903b91 125 else /* CONFIG_KASAN_GENERIC */
bb359dbc
AK
126 *shadow = size & KASAN_GRANULE_MASK;
127 }
128}
129
130#ifdef CONFIG_MEMORY_HOTPLUG
131static bool shadow_mapped(unsigned long addr)
132{
133 pgd_t *pgd = pgd_offset_k(addr);
134 p4d_t *p4d;
135 pud_t *pud;
136 pmd_t *pmd;
137 pte_t *pte;
138
139 if (pgd_none(*pgd))
140 return false;
141 p4d = p4d_offset(pgd, addr);
142 if (p4d_none(*p4d))
143 return false;
144 pud = pud_offset(p4d, addr);
145 if (pud_none(*pud))
146 return false;
147
148 /*
149 * We can't use pud_large() or pud_huge(), the first one is
150 * arch-specific, the last one depends on HUGETLB_PAGE. So let's abuse
151 * pud_bad(), if pud is bad then it's bad because it's huge.
152 */
153 if (pud_bad(*pud))
154 return true;
155 pmd = pmd_offset(pud, addr);
156 if (pmd_none(*pmd))
157 return false;
158
159 if (pmd_bad(*pmd))
160 return true;
161 pte = pte_offset_kernel(pmd, addr);
162 return !pte_none(*pte);
163}
164
165static int __meminit kasan_mem_notifier(struct notifier_block *nb,
166 unsigned long action, void *data)
167{
168 struct memory_notify *mem_data = data;
169 unsigned long nr_shadow_pages, start_kaddr, shadow_start;
170 unsigned long shadow_end, shadow_size;
171
172 nr_shadow_pages = mem_data->nr_pages >> KASAN_SHADOW_SCALE_SHIFT;
173 start_kaddr = (unsigned long)pfn_to_kaddr(mem_data->start_pfn);
174 shadow_start = (unsigned long)kasan_mem_to_shadow((void *)start_kaddr);
175 shadow_size = nr_shadow_pages << PAGE_SHIFT;
176 shadow_end = shadow_start + shadow_size;
177
178 if (WARN_ON(mem_data->nr_pages % KASAN_GRANULE_SIZE) ||
affc3f07 179 WARN_ON(start_kaddr % KASAN_MEMORY_PER_SHADOW_PAGE))
bb359dbc
AK
180 return NOTIFY_BAD;
181
182 switch (action) {
183 case MEM_GOING_ONLINE: {
184 void *ret;
185
186 /*
187 * If shadow is mapped already than it must have been mapped
188 * during the boot. This could happen if we onlining previously
189 * offlined memory.
190 */
191 if (shadow_mapped(shadow_start))
192 return NOTIFY_OK;
193
194 ret = __vmalloc_node_range(shadow_size, PAGE_SIZE, shadow_start,
195 shadow_end, GFP_KERNEL,
196 PAGE_KERNEL, VM_NO_GUARD,
197 pfn_to_nid(mem_data->start_pfn),
198 __builtin_return_address(0));
199 if (!ret)
200 return NOTIFY_BAD;
201
202 kmemleak_ignore(ret);
203 return NOTIFY_OK;
204 }
205 case MEM_CANCEL_ONLINE:
206 case MEM_OFFLINE: {
207 struct vm_struct *vm;
208
209 /*
210 * shadow_start was either mapped during boot by kasan_init()
211 * or during memory online by __vmalloc_node_range().
212 * In the latter case we can use vfree() to free shadow.
213 * Non-NULL result of the find_vm_area() will tell us if
214 * that was the second case.
215 *
216 * Currently it's not possible to free shadow mapped
217 * during boot by kasan_init(). It's because the code
218 * to do that hasn't been written yet. So we'll just
219 * leak the memory.
220 */
221 vm = find_vm_area((void *)shadow_start);
222 if (vm)
223 vfree((void *)shadow_start);
224 }
225 }
226
227 return NOTIFY_OK;
228}
229
230static int __init kasan_memhotplug_init(void)
231{
232 hotplug_memory_notifier(kasan_mem_notifier, 0);
233
234 return 0;
235}
236
237core_initcall(kasan_memhotplug_init);
238#endif
239
240#ifdef CONFIG_KASAN_VMALLOC
241
242static int kasan_populate_vmalloc_pte(pte_t *ptep, unsigned long addr,
243 void *unused)
244{
245 unsigned long page;
246 pte_t pte;
247
248 if (likely(!pte_none(*ptep)))
249 return 0;
250
251 page = __get_free_page(GFP_KERNEL);
252 if (!page)
253 return -ENOMEM;
254
255 memset((void *)page, KASAN_VMALLOC_INVALID, PAGE_SIZE);
256 pte = pfn_pte(PFN_DOWN(__pa(page)), PAGE_KERNEL);
257
258 spin_lock(&init_mm.page_table_lock);
259 if (likely(pte_none(*ptep))) {
260 set_pte_at(&init_mm, addr, ptep, pte);
261 page = 0;
262 }
263 spin_unlock(&init_mm.page_table_lock);
264 if (page)
265 free_page(page);
266 return 0;
267}
268
269int kasan_populate_vmalloc(unsigned long addr, unsigned long size)
270{
271 unsigned long shadow_start, shadow_end;
272 int ret;
273
274 if (!is_vmalloc_or_module_addr((void *)addr))
275 return 0;
276
277 shadow_start = (unsigned long)kasan_mem_to_shadow((void *)addr);
278 shadow_start = ALIGN_DOWN(shadow_start, PAGE_SIZE);
279 shadow_end = (unsigned long)kasan_mem_to_shadow((void *)addr + size);
280 shadow_end = ALIGN(shadow_end, PAGE_SIZE);
281
282 ret = apply_to_page_range(&init_mm, shadow_start,
283 shadow_end - shadow_start,
284 kasan_populate_vmalloc_pte, NULL);
285 if (ret)
286 return ret;
287
288 flush_cache_vmap(shadow_start, shadow_end);
289
290 /*
291 * We need to be careful about inter-cpu effects here. Consider:
292 *
293 * CPU#0 CPU#1
294 * WRITE_ONCE(p, vmalloc(100)); while (x = READ_ONCE(p)) ;
295 * p[99] = 1;
296 *
297 * With compiler instrumentation, that ends up looking like this:
298 *
299 * CPU#0 CPU#1
300 * // vmalloc() allocates memory
301 * // let a = area->addr
302 * // we reach kasan_populate_vmalloc
f00748bf 303 * // and call kasan_unpoison:
bb359dbc
AK
304 * STORE shadow(a), unpoison_val
305 * ...
306 * STORE shadow(a+99), unpoison_val x = LOAD p
307 * // rest of vmalloc process <data dependency>
308 * STORE p, a LOAD shadow(x+99)
309 *
310 * If there is no barrier between the end of unpoisioning the shadow
311 * and the store of the result to p, the stores could be committed
312 * in a different order by CPU#0, and CPU#1 could erroneously observe
313 * poison in the shadow.
314 *
315 * We need some sort of barrier between the stores.
316 *
317 * In the vmalloc() case, this is provided by a smp_wmb() in
318 * clear_vm_uninitialized_flag(). In the per-cpu allocator and in
319 * get_vm_area() and friends, the caller gets shadow allocated but
320 * doesn't have any pages mapped into the virtual address space that
321 * has been reserved. Mapping those pages in will involve taking and
322 * releasing a page-table lock, which will provide the barrier.
323 */
324
325 return 0;
326}
327
328/*
329 * Poison the shadow for a vmalloc region. Called as part of the
330 * freeing process at the time the region is freed.
331 */
332void kasan_poison_vmalloc(const void *start, unsigned long size)
333{
334 if (!is_vmalloc_or_module_addr(start))
335 return;
336
337 size = round_up(size, KASAN_GRANULE_SIZE);
f00748bf 338 kasan_poison(start, size, KASAN_VMALLOC_INVALID);
bb359dbc
AK
339}
340
341void kasan_unpoison_vmalloc(const void *start, unsigned long size)
342{
343 if (!is_vmalloc_or_module_addr(start))
344 return;
345
f00748bf 346 kasan_unpoison(start, size);
bb359dbc
AK
347}
348
349static int kasan_depopulate_vmalloc_pte(pte_t *ptep, unsigned long addr,
350 void *unused)
351{
352 unsigned long page;
353
354 page = (unsigned long)__va(pte_pfn(*ptep) << PAGE_SHIFT);
355
356 spin_lock(&init_mm.page_table_lock);
357
358 if (likely(!pte_none(*ptep))) {
359 pte_clear(&init_mm, addr, ptep);
360 free_page(page);
361 }
362 spin_unlock(&init_mm.page_table_lock);
363
364 return 0;
365}
366
367/*
368 * Release the backing for the vmalloc region [start, end), which
369 * lies within the free region [free_region_start, free_region_end).
370 *
371 * This can be run lazily, long after the region was freed. It runs
372 * under vmap_area_lock, so it's not safe to interact with the vmalloc/vmap
373 * infrastructure.
374 *
375 * How does this work?
376 * -------------------
377 *
378 * We have a region that is page aligned, labelled as A.
379 * That might not map onto the shadow in a way that is page-aligned:
380 *
381 * start end
382 * v v
383 * |????????|????????|AAAAAAAA|AA....AA|AAAAAAAA|????????| < vmalloc
384 * -------- -------- -------- -------- --------
385 * | | | | |
386 * | | | /-------/ |
387 * \-------\|/------/ |/---------------/
388 * ||| ||
389 * |??AAAAAA|AAAAAAAA|AA??????| < shadow
390 * (1) (2) (3)
391 *
392 * First we align the start upwards and the end downwards, so that the
393 * shadow of the region aligns with shadow page boundaries. In the
394 * example, this gives us the shadow page (2). This is the shadow entirely
395 * covered by this allocation.
396 *
397 * Then we have the tricky bits. We want to know if we can free the
398 * partially covered shadow pages - (1) and (3) in the example. For this,
399 * we are given the start and end of the free region that contains this
400 * allocation. Extending our previous example, we could have:
401 *
402 * free_region_start free_region_end
403 * | start end |
404 * v v v v
405 * |FFFFFFFF|FFFFFFFF|AAAAAAAA|AA....AA|AAAAAAAA|FFFFFFFF| < vmalloc
406 * -------- -------- -------- -------- --------
407 * | | | | |
408 * | | | /-------/ |
409 * \-------\|/------/ |/---------------/
410 * ||| ||
411 * |FFAAAAAA|AAAAAAAA|AAF?????| < shadow
412 * (1) (2) (3)
413 *
414 * Once again, we align the start of the free region up, and the end of
415 * the free region down so that the shadow is page aligned. So we can free
416 * page (1) - we know no allocation currently uses anything in that page,
417 * because all of it is in the vmalloc free region. But we cannot free
418 * page (3), because we can't be sure that the rest of it is unused.
419 *
420 * We only consider pages that contain part of the original region for
421 * freeing: we don't try to free other pages from the free region or we'd
422 * end up trying to free huge chunks of virtual address space.
423 *
424 * Concurrency
425 * -----------
426 *
427 * How do we know that we're not freeing a page that is simultaneously
428 * being used for a fresh allocation in kasan_populate_vmalloc(_pte)?
429 *
430 * We _can_ have kasan_release_vmalloc and kasan_populate_vmalloc running
431 * at the same time. While we run under free_vmap_area_lock, the population
432 * code does not.
433 *
434 * free_vmap_area_lock instead operates to ensure that the larger range
435 * [free_region_start, free_region_end) is safe: because __alloc_vmap_area and
436 * the per-cpu region-finding algorithm both run under free_vmap_area_lock,
437 * no space identified as free will become used while we are running. This
438 * means that so long as we are careful with alignment and only free shadow
439 * pages entirely covered by the free region, we will not run in to any
440 * trouble - any simultaneous allocations will be for disjoint regions.
441 */
442void kasan_release_vmalloc(unsigned long start, unsigned long end,
443 unsigned long free_region_start,
444 unsigned long free_region_end)
445{
446 void *shadow_start, *shadow_end;
447 unsigned long region_start, region_end;
448 unsigned long size;
449
affc3f07
AK
450 region_start = ALIGN(start, KASAN_MEMORY_PER_SHADOW_PAGE);
451 region_end = ALIGN_DOWN(end, KASAN_MEMORY_PER_SHADOW_PAGE);
bb359dbc 452
affc3f07 453 free_region_start = ALIGN(free_region_start, KASAN_MEMORY_PER_SHADOW_PAGE);
bb359dbc
AK
454
455 if (start != region_start &&
456 free_region_start < region_start)
affc3f07 457 region_start -= KASAN_MEMORY_PER_SHADOW_PAGE;
bb359dbc 458
affc3f07 459 free_region_end = ALIGN_DOWN(free_region_end, KASAN_MEMORY_PER_SHADOW_PAGE);
bb359dbc
AK
460
461 if (end != region_end &&
462 free_region_end > region_end)
affc3f07 463 region_end += KASAN_MEMORY_PER_SHADOW_PAGE;
bb359dbc
AK
464
465 shadow_start = kasan_mem_to_shadow((void *)region_start);
466 shadow_end = kasan_mem_to_shadow((void *)region_end);
467
468 if (shadow_end > shadow_start) {
469 size = shadow_end - shadow_start;
470 apply_to_existing_page_range(&init_mm,
471 (unsigned long)shadow_start,
472 size, kasan_depopulate_vmalloc_pte,
473 NULL);
474 flush_tlb_kernel_range((unsigned long)shadow_start,
475 (unsigned long)shadow_end);
476 }
477}
478
479#else /* CONFIG_KASAN_VMALLOC */
480
481int kasan_module_alloc(void *addr, size_t size)
482{
483 void *ret;
484 size_t scaled_size;
485 size_t shadow_size;
486 unsigned long shadow_start;
487
488 shadow_start = (unsigned long)kasan_mem_to_shadow(addr);
489 scaled_size = (size + KASAN_GRANULE_SIZE - 1) >>
490 KASAN_SHADOW_SCALE_SHIFT;
491 shadow_size = round_up(scaled_size, PAGE_SIZE);
492
493 if (WARN_ON(!PAGE_ALIGNED(shadow_start)))
494 return -EINVAL;
495
496 ret = __vmalloc_node_range(shadow_size, 1, shadow_start,
497 shadow_start + shadow_size,
498 GFP_KERNEL,
499 PAGE_KERNEL, VM_NO_GUARD, NUMA_NO_NODE,
500 __builtin_return_address(0));
501
502 if (ret) {
503 __memset(ret, KASAN_SHADOW_INIT, shadow_size);
504 find_vm_area(addr)->flags |= VM_KASAN;
505 kmemleak_ignore(ret);
506 return 0;
507 }
508
509 return -ENOMEM;
510}
511
512void kasan_free_shadow(const struct vm_struct *vm)
513{
514 if (vm->flags & VM_KASAN)
515 vfree(kasan_mem_to_shadow(vm->addr));
516}
517
518#endif