Module Name:    src
Committed By:   maxv
Date:           Sat May  2 16:28:37 UTC 2020

Modified Files:
        src/sys/arch/amd64/amd64: locore.S machdep.c
        src/sys/arch/amd64/include: asan.h
        src/sys/arch/x86/x86: pmap.c

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


To generate a diff of this commit:
cvs rdiff -u -r1.204 -r1.205 src/sys/arch/amd64/amd64/locore.S
cvs rdiff -u -r1.350 -r1.351 src/sys/arch/amd64/amd64/machdep.c
cvs rdiff -u -r1.5 -r1.6 src/sys/arch/amd64/include/asan.h
cvs rdiff -u -r1.385 -r1.386 src/sys/arch/x86/x86/pmap.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/amd64/locore.S
diff -u src/sys/arch/amd64/amd64/locore.S:1.204 src/sys/arch/amd64/amd64/locore.S:1.205
--- src/sys/arch/amd64/amd64/locore.S:1.204	Thu Apr 30 17:21:12 2020
+++ src/sys/arch/amd64/amd64/locore.S	Sat May  2 16:28:37 2020
@@ -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 @@ longmode_hi:
 
 	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)

Index: src/sys/arch/amd64/amd64/machdep.c
diff -u src/sys/arch/amd64/amd64/machdep.c:1.350 src/sys/arch/amd64/amd64/machdep.c:1.351
--- src/sys/arch/amd64/amd64/machdep.c:1.350	Thu Apr 30 03:29:19 2020
+++ src/sys/arch/amd64/amd64/machdep.c	Sat May  2 16:28:37 2020
@@ -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 @@ init_bootspace(void)
 	bootspace.emodule = KERNBASE + NKL2_KIMG_ENTRIES * NBPD_L2;
 }
 
-static void __noasan
+static void
 init_pte(void)
 {
 #ifndef XENPV
@@ -1586,7 +1586,7 @@ init_pte(void)
 	normal_pdes[2] = L4_BASE;
 }
 
-void __noasan
+void
 init_slotspace(void)
 {
 	vaddr_t va;
@@ -1656,7 +1656,7 @@ init_slotspace(void)
 #endif
 }
 
-void __noasan
+void
 init_x86_64(paddr_t first_avail)
 {
 	extern void consinit(void);
@@ -1678,8 +1678,6 @@ init_x86_64(paddr_t first_avail)
 
 	init_pte();
 
-	kasan_early_init((void *)lwp0uarea);
-
 	uvm_lwp_setuarea(&lwp0, lwp0uarea);
 
 	cpu_probe(&cpu_info_primary);

Index: src/sys/arch/amd64/include/asan.h
diff -u src/sys/arch/amd64/include/asan.h:1.5 src/sys/arch/amd64/include/asan.h:1.6
--- src/sys/arch/amd64/include/asan.h:1.5	Sat Apr 25 15:26:16 2020
+++ src/sys/arch/amd64/include/asan.h	Sat May  2 16:28:37 2020
@@ -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,25 +48,17 @@
 #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 inline int8_t *
-kasan_md_addr_to_shad(const void *addr)
-{
-	vaddr_t va = (vaddr_t)addr;
-	return (int8_t *)(KASAN_MD_SHADOW_START +
-	    ((va - __MD_CANONICAL_BASE) >> KASAN_SHADOW_SCALE_SHIFT));
-}
-
-static inline bool
-kasan_md_unsupported(vaddr_t addr)
-{
-	return (addr >= (vaddr_t)PTE_BASE &&
-	    addr < ((vaddr_t)PTE_BASE + NBPD_L4));
-}
-
 static paddr_t
 __md_early_palloc(void)
 {
@@ -82,18 +74,60 @@ __md_early_palloc(void)
 	return ret;
 }
 
-static paddr_t
-__md_palloc(void)
+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 (__predict_false(__md_early))
+	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();
-	else
-		pa = pmap_get_physpage();
+		pdir[pl1_pi(va)] = pa | pteflags | pmap_pg_g;
+	}
+}
+
+/* -------------------------------------------------------------------------- */
+
+static inline int8_t *
+kasan_md_addr_to_shad(const void *addr)
+{
+	vaddr_t va = (vaddr_t)addr;
+	return (int8_t *)(KASAN_MD_SHADOW_START +
+	    ((va - __MD_CANONICAL_BASE) >> KASAN_SHADOW_SCALE_SHIFT));
+}
 
+static inline bool
+kasan_md_unsupported(vaddr_t addr)
+{
+	return (addr >= (vaddr_t)PTE_BASE &&
+	    addr < ((vaddr_t)PTE_BASE + NBPD_L4));
+}
+
+static paddr_t
+__md_palloc(void)
+{
 	/* The page is zeroed. */
-	return pa;
+	return pmap_get_physpage();
 }
 
 static inline paddr_t
@@ -102,8 +136,6 @@ __md_palloc_large(void)
 	struct pglist pglist;
 	int ret;
 
-	if (__predict_false(__md_early))
-		return 0;
 	if (!uvm.page_init_done)
 		return 0;
 
@@ -122,6 +154,11 @@ kasan_md_shadow_map_page(vaddr_t va)
 	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;

Index: src/sys/arch/x86/x86/pmap.c
diff -u src/sys/arch/x86/x86/pmap.c:1.385 src/sys/arch/x86/x86/pmap.c:1.386
--- src/sys/arch/x86/x86/pmap.c:1.385	Thu Apr 30 03:29:20 2020
+++ src/sys/arch/x86/x86/pmap.c	Sat May  2 16:28:37 2020
@@ -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 @@ slotspace_copy(int type, pd_entry_t *dst
  * 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 {

Reply via email to