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