Home | History | Annotate | Line # | Download | only in include
asan.h revision 1.2
      1 /*	$NetBSD: asan.h,v 1.2 2019/02/04 15:07:34 maxv Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2018 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Maxime Villard.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/ksyms.h>
     33 
     34 #include <amd64/pmap.h>
     35 #include <amd64/vmparam.h>
     36 
     37 #ifdef __HAVE_PCPU_AREA
     38 #error "PCPU area not allowed with KASAN"
     39 #endif
     40 #ifdef __HAVE_DIRECT_MAP
     41 #error "DMAP not allowed with KASAN"
     42 #endif
     43 
     44 #define __MD_VIRTUAL_SHIFT	47	/* 48bit address space, cut half */
     45 #define __MD_CANONICAL_BASE	0xFFFF800000000000
     46 
     47 #define __MD_SHADOW_SIZE	(1ULL << (__MD_VIRTUAL_SHIFT - KASAN_SHADOW_SCALE_SHIFT))
     48 #define KASAN_MD_SHADOW_START	(VA_SIGN_NEG((L4_SLOT_KASAN * NBPD_L4)))
     49 #define KASAN_MD_SHADOW_END	(KASAN_MD_SHADOW_START + __MD_SHADOW_SIZE)
     50 
     51 static bool __md_early __read_mostly = true;
     52 static uint8_t __md_earlypages[8 * PAGE_SIZE] __aligned(PAGE_SIZE);
     53 static size_t __md_earlytaken = 0;
     54 
     55 static inline int8_t *
     56 kasan_md_addr_to_shad(const void *addr)
     57 {
     58 	vaddr_t va = (vaddr_t)addr;
     59 	return (int8_t *)(KASAN_MD_SHADOW_START +
     60 	    ((va - __MD_CANONICAL_BASE) >> KASAN_SHADOW_SCALE_SHIFT));
     61 }
     62 
     63 static inline bool
     64 kasan_md_unsupported(vaddr_t addr)
     65 {
     66 	return (addr >= (vaddr_t)PTE_BASE &&
     67 	    addr < ((vaddr_t)PTE_BASE + NBPD_L4));
     68 }
     69 
     70 static paddr_t
     71 __md_early_palloc(void)
     72 {
     73 	paddr_t ret;
     74 
     75 	KASSERT(__md_earlytaken < 8);
     76 
     77 	ret = (paddr_t)(&__md_earlypages[0] + __md_earlytaken * PAGE_SIZE);
     78 	__md_earlytaken++;
     79 
     80 	ret -= KERNBASE;
     81 
     82 	return ret;
     83 }
     84 
     85 static paddr_t
     86 __md_palloc(void)
     87 {
     88 	paddr_t pa;
     89 
     90 	if (__predict_false(__md_early))
     91 		pa = __md_early_palloc();
     92 	else
     93 		pa = pmap_get_physpage();
     94 
     95 	return pa;
     96 }
     97 
     98 static void
     99 kasan_md_shadow_map_page(vaddr_t va)
    100 {
    101 	paddr_t pa;
    102 
    103 	if (!pmap_valid_entry(L4_BASE[pl4_i(va)])) {
    104 		pa = __md_palloc();
    105 		L4_BASE[pl4_i(va)] = pa | PG_KW | pmap_pg_nx | PG_V;
    106 	}
    107 	if (!pmap_valid_entry(L3_BASE[pl3_i(va)])) {
    108 		pa = __md_palloc();
    109 		L3_BASE[pl3_i(va)] = pa | PG_KW | pmap_pg_nx | PG_V;
    110 	}
    111 	if (!pmap_valid_entry(L2_BASE[pl2_i(va)])) {
    112 		pa = __md_palloc();
    113 		L2_BASE[pl2_i(va)] = pa | PG_KW | pmap_pg_nx | PG_V;
    114 	}
    115 	if (!pmap_valid_entry(L1_BASE[pl1_i(va)])) {
    116 		pa = __md_palloc();
    117 		L1_BASE[pl1_i(va)] = pa | PG_KW | pmap_pg_g | pmap_pg_nx | PG_V;
    118 	}
    119 }
    120 
    121 /*
    122  * Map only the current stack. We will map the rest in kasan_init.
    123  */
    124 static void
    125 kasan_md_early_init(void *stack)
    126 {
    127 	kasan_shadow_map(stack, USPACE);
    128 	__md_early = false;
    129 }
    130 
    131 /*
    132  * Create the shadow mapping. We don't create the 'User' area, because we
    133  * exclude it from the monitoring. The 'Main' area is created dynamically
    134  * in pmap_growkernel.
    135  */
    136 static void
    137 kasan_md_init(void)
    138 {
    139 	extern struct bootspace bootspace;
    140 	size_t i;
    141 
    142 	CTASSERT((__MD_SHADOW_SIZE / NBPD_L4) == NL4_SLOT_KASAN);
    143 
    144 	/* Kernel. */
    145 	for (i = 0; i < BTSPACE_NSEGS; i++) {
    146 		if (bootspace.segs[i].type == BTSEG_NONE) {
    147 			continue;
    148 		}
    149 		kasan_shadow_map((void *)bootspace.segs[i].va,
    150 		    bootspace.segs[i].sz);
    151 	}
    152 
    153 	/* Boot region. */
    154 	kasan_shadow_map((void *)bootspace.boot.va, bootspace.boot.sz);
    155 
    156 	/* Module map. */
    157 	kasan_shadow_map((void *)bootspace.smodule,
    158 	    (size_t)(bootspace.emodule - bootspace.smodule));
    159 
    160 	/* The bootstrap spare va. */
    161 	kasan_shadow_map((void *)bootspace.spareva, PAGE_SIZE);
    162 }
    163 
    164 static inline bool
    165 __md_unwind_end(const char *name)
    166 {
    167 	if (!strcmp(name, "syscall") ||
    168 	    !strcmp(name, "alltraps") ||
    169 	    !strcmp(name, "handle_syscall") ||
    170 	    !strncmp(name, "Xtrap", 5) ||
    171 	    !strncmp(name, "Xintr", 5) ||
    172 	    !strncmp(name, "Xhandle", 7) ||
    173 	    !strncmp(name, "Xresume", 7) ||
    174 	    !strncmp(name, "Xstray", 6) ||
    175 	    !strncmp(name, "Xhold", 5) ||
    176 	    !strncmp(name, "Xrecurse", 8) ||
    177 	    !strcmp(name, "Xdoreti") ||
    178 	    !strncmp(name, "Xsoft", 5)) {
    179 		return true;
    180 	}
    181 
    182 	return false;
    183 }
    184 
    185 static void
    186 kasan_md_unwind(void)
    187 {
    188 	uint64_t *rbp, rip;
    189 	const char *mod;
    190 	const char *sym;
    191 	size_t nsym;
    192 	int error;
    193 
    194 	rbp = (uint64_t *)__builtin_frame_address(0);
    195 	nsym = 0;
    196 
    197 	while (1) {
    198 		/* 8(%rbp) contains the saved %rip. */
    199 		rip = *(rbp + 1);
    200 
    201 		if (rip < KERNBASE) {
    202 			break;
    203 		}
    204 		error = ksyms_getname(&mod, &sym, (vaddr_t)rip, KSYMS_PROC);
    205 		if (error) {
    206 			break;
    207 		}
    208 		printf("#%zu %p in %s <%s>\n", nsym, (void *)rip, sym, mod);
    209 		if (__md_unwind_end(sym)) {
    210 			break;
    211 		}
    212 
    213 		rbp = (uint64_t *)*(rbp);
    214 		if (rbp == 0) {
    215 			break;
    216 		}
    217 		nsym++;
    218 
    219 		if (nsym >= 15) {
    220 			break;
    221 		}
    222 	}
    223 }
    224