Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys Move the MI parts of KASAN into kern/subr_asan.c. This f...



details:   https://anonhg.NetBSD.org/src/rev/3c473571133c
branches:  trunk
changeset: 836687:3c473571133c
user:      maxv <maxv%NetBSD.org@localhost>
date:      Wed Oct 31 06:26:25 2018 +0000

description:
Move the MI parts of KASAN into kern/subr_asan.c. This file includes
machine/asan.h, which contains the MD functions. We use an include rather
than a plain C file, because we want GCC to optimize/inline some functions
into one single block.

The amd64 MD parts of KASAN are moved accordingly.

The naming convention we use is:

        kasan_*
                a generic kasan object, declared in subr_asan.c
        kasan_md_*
                an MD kasan object, declared in machine/asan.h, and used
                in subr_asan.c
        __md_*
                an MD object, declared in machine/asan.h, and not used
                outside

Overall this makes it easier to add KASAN support on more architectures.

Discussed with several people.

diffstat:

 sys/arch/amd64/amd64/asan.c        |  725 -------------------------------------
 sys/arch/amd64/amd64/machdep.c     |    9 +-
 sys/arch/amd64/conf/Makefile.amd64 |    4 +-
 sys/arch/amd64/conf/files.amd64    |    3 +-
 sys/arch/amd64/include/Makefile    |    4 +-
 sys/arch/amd64/include/asan.h      |  221 +++++++++++
 sys/arch/x86/x86/pmap.c            |    6 +-
 sys/kern/files.kern                |    3 +-
 sys/kern/subr_asan.c               |  536 +++++++++++++++++++++++++++
 sys/sys/asan.h                     |    7 +-
 10 files changed, 777 insertions(+), 741 deletions(-)

diffs (truncated from 1659 to 300 lines):

diff -r 7b06a616e61a -r 3c473571133c sys/arch/amd64/amd64/asan.c
--- a/sys/arch/amd64/amd64/asan.c       Wed Oct 31 06:04:48 2018 +0000
+++ /dev/null   Thu Jan 01 00:00:00 1970 +0000
@@ -1,725 +0,0 @@
-/*     $NetBSD: asan.c,v 1.10 2018/10/27 06:35:54 maxv Exp $   */
-
-/*
- * Copyright (c) 2018 The NetBSD Foundation, Inc.
- * All rights reserved.
- *
- * This code is derived from software contributed to The NetBSD Foundation
- * by Maxime Villard, and Siddharth Muralee.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
- * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
- * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: asan.c,v 1.10 2018/10/27 06:35:54 maxv Exp $");
-
-#include <sys/param.h>
-#include <sys/device.h>
-#include <sys/kernel.h>
-#include <sys/module.h>
-#include <sys/param.h>
-#include <sys/conf.h>
-#include <sys/systm.h>
-#include <sys/types.h>
-#include <sys/ksyms.h>
-#include <sys/asan.h>
-
-#include <uvm/uvm.h>
-#include <amd64/pmap.h>
-#include <amd64/vmparam.h>
-
-#define VIRTUAL_SHIFT          47      /* 48bit address space, cut half */
-#define CANONICAL_BASE         0xFFFF800000000000
-
-#define KASAN_SHADOW_SCALE_SHIFT       3
-#define KASAN_SHADOW_SCALE_SIZE                (1UL << KASAN_SHADOW_SCALE_SHIFT)
-#define KASAN_SHADOW_MASK              (KASAN_SHADOW_SCALE_SIZE - 1)
-
-#define KASAN_SHADOW_SIZE      (1ULL << (VIRTUAL_SHIFT - KASAN_SHADOW_SCALE_SHIFT))
-#define KASAN_SHADOW_START     (VA_SIGN_NEG((L4_SLOT_KASAN * NBPD_L4)))
-#define KASAN_SHADOW_END       (KASAN_SHADOW_START + KASAN_SHADOW_SIZE)
-
-#define __RET_ADDR     (unsigned long)__builtin_return_address(0)
-
-void kasan_softint(struct lwp *);
-void kasan_shadow_map(void *, size_t);
-void kasan_early_init(void);
-void kasan_init(void);
-
-static bool kasan_enabled __read_mostly = false;
-
-static inline int8_t *kasan_addr_to_shad(const void *addr)
-{
-       vaddr_t va = (vaddr_t)addr;
-       return (int8_t *)(KASAN_SHADOW_START +
-           ((va - CANONICAL_BASE) >> KASAN_SHADOW_SCALE_SHIFT));
-}
-
-static __always_inline bool
-kasan_unsupported(vaddr_t addr)
-{
-       return (addr >= (vaddr_t)PTE_BASE &&
-           addr < ((vaddr_t)PTE_BASE + NBPD_L4));
-}
-
-/* -------------------------------------------------------------------------- */
-
-static bool kasan_early __read_mostly = true;
-static uint8_t earlypages[8 * PAGE_SIZE] __aligned(PAGE_SIZE);
-static size_t earlytaken = 0;
-
-static paddr_t
-kasan_early_palloc(void)
-{
-       paddr_t ret;
-
-       KASSERT(earlytaken < 8);
-
-       ret = (paddr_t)(&earlypages[0] + earlytaken * PAGE_SIZE);
-       earlytaken++;
-
-       ret -= KERNBASE;
-
-       return ret;
-}
-
-static paddr_t
-kasan_palloc(void)
-{
-       paddr_t pa;
-
-       if (__predict_false(kasan_early))
-               pa = kasan_early_palloc();
-       else
-               pa = pmap_get_physpage();
-
-       return pa;
-}
-
-static void
-kasan_shadow_map_page(vaddr_t va)
-{
-       paddr_t pa;
-
-       if (!pmap_valid_entry(L4_BASE[pl4_i(va)])) {
-               pa = kasan_palloc();
-               L4_BASE[pl4_i(va)] = pa | PG_KW | pmap_pg_nx | PG_V;
-       }
-       if (!pmap_valid_entry(L3_BASE[pl3_i(va)])) {
-               pa = kasan_palloc();
-               L3_BASE[pl3_i(va)] = pa | PG_KW | pmap_pg_nx | PG_V;
-       }
-       if (!pmap_valid_entry(L2_BASE[pl2_i(va)])) {
-               pa = kasan_palloc();
-               L2_BASE[pl2_i(va)] = pa | PG_KW | pmap_pg_nx | PG_V;
-       }
-       if (!pmap_valid_entry(L1_BASE[pl1_i(va)])) {
-               pa = kasan_palloc();
-               L1_BASE[pl1_i(va)] = pa | PG_KW | pmap_pg_g | pmap_pg_nx | PG_V;
-       }
-}
-
-/*
- * Allocate the necessary stuff in the shadow, so that we can monitor the
- * passed area.
- */
-void
-kasan_shadow_map(void *addr, size_t size)
-{
-       size_t sz, npages, i;
-       vaddr_t sva, eva;
-
-       KASSERT((vaddr_t)addr % KASAN_SHADOW_SCALE_SIZE == 0);
-
-       sz = roundup(size, KASAN_SHADOW_SCALE_SIZE) / KASAN_SHADOW_SCALE_SIZE;
-
-       sva = (vaddr_t)kasan_addr_to_shad(addr);
-       eva = (vaddr_t)kasan_addr_to_shad(addr) + sz;
-
-       sva = rounddown(sva, PAGE_SIZE);
-       eva = roundup(eva, PAGE_SIZE);
-
-       npages = (eva - sva) / PAGE_SIZE;
-
-       KASSERT(sva >= KASAN_SHADOW_START && eva < KASAN_SHADOW_END);
-
-       for (i = 0; i < npages; i++) {
-               kasan_shadow_map_page(sva + i * PAGE_SIZE);
-       }
-}
-
-/* -------------------------------------------------------------------------- */
-
-#ifdef __HAVE_PCPU_AREA
-#error "PCPU area not allowed with KASAN"
-#endif
-#ifdef __HAVE_DIRECT_MAP
-#error "DMAP not allowed with KASAN"
-#endif
-
-static void
-kasan_ctors(void)
-{
-       extern uint64_t __CTOR_LIST__, __CTOR_END__;
-       size_t nentries, i;
-       uint64_t *ptr;
-
-       nentries = ((size_t)&__CTOR_END__ - (size_t)&__CTOR_LIST__) /
-           sizeof(uintptr_t);
-
-       ptr = &__CTOR_LIST__;
-       for (i = 0; i < nentries; i++) {
-               void (*func)(void);
-
-               func = (void *)(*ptr);
-               (*func)();
-
-               ptr++;
-       }
-}
-
-/*
- * Map only the current stack. We will map the rest in kasan_init.
- */
-void
-kasan_early_init(void)
-{
-       extern vaddr_t lwp0uarea;
-
-       kasan_shadow_map((void *)lwp0uarea, USPACE);
-       kasan_early = false;
-}
-
-/*
- * Create the shadow mapping. We don't create the 'User' area, because we
- * exclude it from the monitoring. The 'Main' area is created dynamically
- * in pmap_growkernel.
- */
-void
-kasan_init(void)
-{
-       extern struct bootspace bootspace;
-       size_t i;
-
-       CTASSERT((KASAN_SHADOW_SIZE / NBPD_L4) == NL4_SLOT_KASAN);
-
-       /* Kernel. */
-       for (i = 0; i < BTSPACE_NSEGS; i++) {
-               if (bootspace.segs[i].type == BTSEG_NONE) {
-                       continue;
-               }
-               kasan_shadow_map((void *)bootspace.segs[i].va,
-                   bootspace.segs[i].sz);
-       }
-
-       /* Boot region. */
-       kasan_shadow_map((void *)bootspace.boot.va, bootspace.boot.sz);
-
-       /* Module map. */
-       kasan_shadow_map((void *)bootspace.smodule,
-           (size_t)(bootspace.emodule - bootspace.smodule));
-
-       /* The bootstrap spare va. */
-       kasan_shadow_map((void *)bootspace.spareva, PAGE_SIZE);
-
-       kasan_enabled = true;
-
-       /* Call the ASAN constructors. */
-       kasan_ctors();
-}
-
-/* -------------------------------------------------------------------------- */
-
-static inline bool
-kasan_unwind_end(const char *name)
-{
-       if (!strcmp(name, "syscall") ||
-           !strcmp(name, "handle_syscall") ||
-           !strncmp(name, "Xintr", 5) ||
-           !strncmp(name, "Xhandle", 7) ||
-           !strncmp(name, "Xresume", 7) ||
-           !strncmp(name, "Xstray", 6) ||
-           !strncmp(name, "Xhold", 5) ||
-           !strncmp(name, "Xrecurse", 8) ||
-           !strcmp(name, "Xdoreti") ||
-           !strncmp(name, "Xsoft", 5)) {
-               return true;
-       }
-
-       return false;
-}
-
-static void
-kasan_unwind(void)
-{
-       uint64_t *rbp, rip;
-       const char *mod;
-       const char *sym;
-       size_t nsym;
-       int error;
-
-       rbp = (uint64_t *)__builtin_frame_address(0);
-       nsym = 0;
-
-       while (1) {
-               /* 8(%rbp) contains the saved %rip. */
-               rip = *(rbp + 1);
-
-               if (rip < KERNBASE) {
-                       break;
-               }
-               error = ksyms_getname(&mod, &sym, (vaddr_t)rip, KSYMS_PROC);
-               if (error) {
-                       break;
-               }
-               printf("#%zu %p in %s <%s>\n", nsym, (void *)rip, sym, mod);
-               if (kasan_unwind_end(sym)) {
-                       break;



Home | Main Index | Thread Index | Old Index