Source-Changes-HG archive

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

[src/trunk]: src/sys/arch Call kasan_early_init earlier, to unbreak KASAN aft...



details:   https://anonhg.NetBSD.org/src/rev/35288de96243
branches:  trunk
changeset: 971722:35288de96243
user:      maxv <maxv%NetBSD.org@localhost>
date:      Sat May 02 16:28:37 2020 +0000

description:
Call kasan_early_init earlier, to unbreak KASAN after the recent RNG
changes. Will also prevent further trouble.

diffstat:

 sys/arch/amd64/amd64/locore.S  |   7 ++-
 sys/arch/amd64/amd64/machdep.c |  12 ++---
 sys/arch/amd64/include/asan.h  |  89 +++++++++++++++++++++++++++++------------
 sys/arch/x86/x86/pmap.c        |   6 +-
 4 files changed, 77 insertions(+), 37 deletions(-)

diffs (240 lines):

diff -r 702aebf08616 -r 35288de96243 sys/arch/amd64/amd64/locore.S
--- a/sys/arch/amd64/amd64/locore.S     Sat May 02 16:25:47 2020 +0000
+++ b/sys/arch/amd64/amd64/locore.S     Sat May 02 16:28:37 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: locore.S,v 1.204 2020/04/30 17:21:12 maxv Exp $        */
+/*     $NetBSD: locore.S,v 1.205 2020/05/02 16:28:37 maxv Exp $        */
 
 /*
  * Copyright-o-rama!
@@ -150,6 +150,7 @@
 
 #include <machine/asm.h>
 
+#include "opt_kasan.h"
 #include "opt_copy_symtab.h"
 #include "opt_ddb.h"
 #include "opt_ddbparam.h"
@@ -973,6 +974,10 @@
 
        pushq   %rdi
        call    _C_LABEL(init_bootspace)
+#ifdef KASAN
+       movq    _C_LABEL(lwp0uarea)(%rip),%rdi
+       call    _C_LABEL(kasan_early_init)
+#endif
        call    _C_LABEL(init_slotspace)
        popq    %rdi
        call    _C_LABEL(init_x86_64)
diff -r 702aebf08616 -r 35288de96243 sys/arch/amd64/amd64/machdep.c
--- a/sys/arch/amd64/amd64/machdep.c    Sat May 02 16:25:47 2020 +0000
+++ b/sys/arch/amd64/amd64/machdep.c    Sat May 02 16:28:37 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: machdep.c,v 1.350 2020/04/30 03:29:19 riastradh Exp $  */
+/*     $NetBSD: machdep.c,v 1.351 2020/05/02 16:28:37 maxv Exp $       */
 
 /*
  * Copyright (c) 1996, 1997, 1998, 2000, 2006, 2007, 2008, 2011
@@ -110,7 +110,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.350 2020/04/30 03:29:19 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.351 2020/05/02 16:28:37 maxv Exp $");
 
 #include "opt_modular.h"
 #include "opt_user_ldt.h"
@@ -1570,7 +1570,7 @@
        bootspace.emodule = KERNBASE + NKL2_KIMG_ENTRIES * NBPD_L2;
 }
 
-static void __noasan
+static void
 init_pte(void)
 {
 #ifndef XENPV
@@ -1586,7 +1586,7 @@
        normal_pdes[2] = L4_BASE;
 }
 
-void __noasan
+void
 init_slotspace(void)
 {
        vaddr_t va;
@@ -1656,7 +1656,7 @@
 #endif
 }
 
-void __noasan
+void
 init_x86_64(paddr_t first_avail)
 {
        extern void consinit(void);
@@ -1678,8 +1678,6 @@
 
        init_pte();
 
-       kasan_early_init((void *)lwp0uarea);
-
        uvm_lwp_setuarea(&lwp0, lwp0uarea);
 
        cpu_probe(&cpu_info_primary);
diff -r 702aebf08616 -r 35288de96243 sys/arch/amd64/include/asan.h
--- a/sys/arch/amd64/include/asan.h     Sat May 02 16:25:47 2020 +0000
+++ b/sys/arch/amd64/include/asan.h     Sat May 02 16:28:37 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: asan.h,v 1.5 2020/04/25 15:26:16 bouyer Exp $  */
+/*     $NetBSD: asan.h,v 1.6 2020/05/02 16:28:37 maxv Exp $    */
 
 /*
  * Copyright (c) 2018 The NetBSD Foundation, Inc.
@@ -48,10 +48,66 @@
 #define KASAN_MD_SHADOW_START  (VA_SIGN_NEG((L4_SLOT_KASAN * NBPD_L4)))
 #define KASAN_MD_SHADOW_END    (KASAN_MD_SHADOW_START + __MD_SHADOW_SIZE)
 
+/* -------------------------------------------------------------------------- */
+
+/*
+ * Early mapping, used to map just the stack at boot time. We rely on the fact
+ * that VA = PA + KERNBASE.
+ */
+
 static bool __md_early __read_mostly = true;
 static uint8_t __md_earlypages[8 * PAGE_SIZE] __aligned(PAGE_SIZE);
 static size_t __md_earlytaken = 0;
 
+static paddr_t
+__md_early_palloc(void)
+{
+       paddr_t ret;
+
+       KASSERT(__md_earlytaken < 8);
+
+       ret = (paddr_t)(&__md_earlypages[0] + __md_earlytaken * PAGE_SIZE);
+       __md_earlytaken++;
+
+       ret -= KERNBASE;
+
+       return ret;
+}
+
+static void
+__md_early_shadow_map_page(vaddr_t va)
+{
+       extern struct bootspace bootspace;
+       const pt_entry_t pteflags = PTE_W | pmap_pg_nx | PTE_P;
+       pt_entry_t *pdir = (pt_entry_t *)bootspace.pdir;
+       paddr_t pa;
+
+       if (!pmap_valid_entry(pdir[pl4_pi(va)])) {
+               pa = __md_early_palloc();
+               pdir[pl4_pi(va)] = pa | pteflags;
+       }
+       pdir = (pt_entry_t *)((pdir[pl4_pi(va)] & PTE_FRAME) + KERNBASE);
+
+       if (!pmap_valid_entry(pdir[pl3_pi(va)])) {
+               pa = __md_early_palloc();
+               pdir[pl3_pi(va)] = pa | pteflags;
+       }
+       pdir = (pt_entry_t *)((pdir[pl3_pi(va)] & PTE_FRAME) + KERNBASE);
+
+       if (!pmap_valid_entry(pdir[pl2_pi(va)])) {
+               pa = __md_early_palloc();
+               pdir[pl2_pi(va)] = pa | pteflags;
+       }
+       pdir = (pt_entry_t *)((pdir[pl2_pi(va)] & PTE_FRAME) + KERNBASE);
+
+       if (!pmap_valid_entry(pdir[pl1_pi(va)])) {
+               pa = __md_early_palloc();
+               pdir[pl1_pi(va)] = pa | pteflags | pmap_pg_g;
+       }
+}
+
+/* -------------------------------------------------------------------------- */
+
 static inline int8_t *
 kasan_md_addr_to_shad(const void *addr)
 {
@@ -68,32 +124,10 @@
 }
 
 static paddr_t
-__md_early_palloc(void)
-{
-       paddr_t ret;
-
-       KASSERT(__md_earlytaken < 8);
-
-       ret = (paddr_t)(&__md_earlypages[0] + __md_earlytaken * PAGE_SIZE);
-       __md_earlytaken++;
-
-       ret -= KERNBASE;
-
-       return ret;
-}
-
-static paddr_t
 __md_palloc(void)
 {
-       paddr_t pa;
-
-       if (__predict_false(__md_early))
-               pa = __md_early_palloc();
-       else
-               pa = pmap_get_physpage();
-
        /* The page is zeroed. */
-       return pa;
+       return pmap_get_physpage();
 }
 
 static inline paddr_t
@@ -102,8 +136,6 @@
        struct pglist pglist;
        int ret;
 
-       if (__predict_false(__md_early))
-               return 0;
        if (!uvm.page_init_done)
                return 0;
 
@@ -122,6 +154,11 @@
        const pt_entry_t pteflags = PTE_W | pmap_pg_nx | PTE_P;
        paddr_t pa;
 
+       if (__predict_false(__md_early)) {
+               __md_early_shadow_map_page(va);
+               return;
+       }
+
        if (!pmap_valid_entry(L4_BASE[pl4_i(va)])) {
                pa = __md_palloc();
                L4_BASE[pl4_i(va)] = pa | pteflags;
diff -r 702aebf08616 -r 35288de96243 sys/arch/x86/x86/pmap.c
--- a/sys/arch/x86/x86/pmap.c   Sat May 02 16:25:47 2020 +0000
+++ b/sys/arch/x86/x86/pmap.c   Sat May 02 16:28:37 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pmap.c,v 1.385 2020/04/30 03:29:20 riastradh Exp $     */
+/*     $NetBSD: pmap.c,v 1.386 2020/05/02 16:28:37 maxv Exp $  */
 
 /*
  * Copyright (c) 2008, 2010, 2016, 2017, 2019, 2020 The NetBSD Foundation, Inc.
@@ -130,7 +130,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.385 2020/04/30 03:29:20 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.386 2020/05/02 16:28:37 maxv Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -1412,7 +1412,7 @@
  * randomly select one hole, and then randomly select an area within that hole.
  * Finally we update the associated entry in the slotspace structure.
  */
-vaddr_t __noasan
+vaddr_t
 slotspace_rand(int type, size_t sz, size_t align)
 {
        struct {



Home | Main Index | Thread Index | Old Index