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