Skip to contentSkip to conversation

KASAN for powerpc/32

PATCH Superseded Christophe Leroy 1 participant 8 messages Feb 19, 2019 – Feb 20, 2019

Subsystems (3)

v6 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v6 cover KASAN for powerpc/32

This serie adds KASAN support to powerpc/32

Tested on nohash/32 (8xx) and book3s/32 (mpc832x ie 603). Boot tested on qemu mac99

Changes in v6:
- Fixed oops on module loading (due to access to RO shadow zero area).
- Added support for hash book3s/32, thanks to Daniel's patch to differ KASAN activation.
- Reworked handling of optimised string functions (dedicated patch for it)
- Reordered some files to ease adding of book3e/64 support.

Changes in v5:
- Added KASAN_SHADOW_OFFSET in Makefile, otherwise we fallback to KASAN_MINIMAL
and some stuff like stack instrumentation is not performed
- Moved calls to kasan_early_init() in head.S because stack instrumentation
in machine_init was performed before the call to kasan_early_init()
- Mapping kasan_early_shadow_page RW in kasan_early_init() and
remaping RO later in kasan_init()
- Allocating a big memblock() for shadow area, falling back to PAGE_SIZE blocks in case of failure.

Changes in v4:
- Comments from Andrey (DISABLE_BRANCH_PROFILING, Activation of reports)
- Proper initialisation of shadow area in kasan_init()
- Panic in case Hash table is required.
- Added comments in patch one to explain why *t = *s becomes memcpy(t, s, ...)
- Call of kasan_init_tags()

Changes in v3:
- Removed the printk() in kasan_early_init() to avoid build failure (see https://github.com/linuxppc/issues/issues/218)
- Added necessary changes in asm/book3s/32/pgtable.h to get it work on powerpc 603 family
- Added a few KASAN_SANITIZE_xxx.o := n to successfully boot on powerpc 603 family

Changes in v2:
- Rebased.
- Using __set_pte_at() to build the early table.
- Worked around and got rid of the patch adding asm/page.h in asm/pgtable-types.h

    ==> might be fixed independently but not needed for this serie.

For book3s/32 we have to stick to KASAN_MINIMAL because Hash table management is not active early enough at the time being.

Christophe Leroy (6):
powerpc/mm: prepare kernel for KAsan on PPC32 powerpc/32: Move early_init() in a separate file
powerpc: prepare string/mem functions for KASAN powerpc/32: Add KASAN support
kasan: allow architectures to provide an outline readiness check powerpc/32: enable CONFIG_KASAN for book3s hash

 arch/powerpc/Kconfig                          |   1 +
 arch/powerpc/Makefile                         |   9 ++
 arch/powerpc/include/asm/book3s/32/pgtable.h  |   2 +
 arch/powerpc/include/asm/highmem.h            |  10 +-
 arch/powerpc/include/asm/kasan.h              |  51 ++++++++
 arch/powerpc/include/asm/nohash/32/pgtable.h  |   2 +
 arch/powerpc/include/asm/setup.h              |   5 +
 arch/powerpc/include/asm/string.h             |  26 +++-
 arch/powerpc/kernel/Makefile                  |  11 +-
 arch/powerpc/kernel/asm-offsets.c             |   4 +
 arch/powerpc/kernel/cputable.c                |  13 +-
 arch/powerpc/kernel/early_32.c                |  36 ++++++
 arch/powerpc/kernel/head_32.S                 |   3 +
 arch/powerpc/kernel/head_40x.S                |   3 +
 arch/powerpc/kernel/head_44x.S                |   3 +
 arch/powerpc/kernel/head_8xx.S                |   3 +
 arch/powerpc/kernel/head_fsl_booke.S          |   3 +
 arch/powerpc/kernel/prom_init_check.sh        |  10 +-
 arch/powerpc/kernel/setup-common.c            |   2 +
 arch/powerpc/kernel/setup_32.c                |  28 -----
 arch/powerpc/lib/Makefile                     |  16 ++-
 arch/powerpc/lib/copy_32.S                    |  13 +-
 arch/powerpc/lib/mem_64.S                     |   8 +-
 arch/powerpc/lib/memcpy_64.S                  |   4 +-
 arch/powerpc/mm/Makefile                      |   1 +
 arch/powerpc/mm/kasan/Makefile                |   5 +
 arch/powerpc/mm/kasan/kasan_init_32.c         | 170 ++++++++++++++++++++++++++
 arch/powerpc/mm/mem.c                         |   4 +
 arch/powerpc/mm/ptdump/dump_linuxpagetables.c |   8 ++
 arch/powerpc/purgatory/Makefile               |   3 +
 arch/powerpc/xmon/Makefile                    |   1 +
 include/linux/kasan.h                         |   4 +
 mm/kasan/generic.c                            |   3 +
 33 files changed, 412 insertions(+), 53 deletions(-)
 create mode 100644 arch/powerpc/include/asm/kasan.h
 create mode 100644 arch/powerpc/kernel/early_32.c
 create mode 100644 arch/powerpc/mm/kasan/Makefile
 create mode 100644 arch/powerpc/mm/kasan/kasan_init_32.c
Signature · 1 line
2.13.3
#1 lore raw

PATCH v6 1/6 powerpc/mm: prepare kernel for KAsan on PPC32

In kernel/cputable.c, explicitly use memcpy() in order to allow GCC to replace it with __memcpy() when KASAN is selected.

Since commit 400c47d81ca38 ("powerpc32: memset: only use dcbz once cache is enabled"), memset() can be used before activation of the cache, so no need to use memset_io() for zeroing the BSS.

---
 arch/powerpc/kernel/cputable.c | 13 ++++++++++---
 arch/powerpc/kernel/setup_32.c |  6 ++----
 2 files changed, 12 insertions(+), 7 deletions(-)
arch/powerpc/kernel/cputable.c +10 -3
index 1eab54bc6ee9..cd12f362b61f 100644
@@ -2147,7 +2147,11 @@ void __init set_cur_cpu_spec(struct cpu_spec *s)
 	struct cpu_spec *t = &the_cpu_spec;
 
 	t = PTRRELOC(t);
-	*t = *s;
+	/*
+	 * use memcpy() instead of *t = *s so that GCC replaces it
+	 * by __memcpy() when KASAN is active
+	 */
+	memcpy(t, s, sizeof(*t));
 
 	*PTRRELOC(&cur_cpu_spec) = &the_cpu_spec;
 }
@@ -2161,8 +2165,11 @@ static struct cpu_spec * __init setup_cpu_spec(unsigned long offset,
 	t = PTRRELOC(t);
 	old = *t;
 
-	/* Copy everything, then do fixups */
-	*t = *s;
+	/*
+	 * Copy everything, then do fixups. Use memcpy() instead of *t = *s
+	 * so that GCC replaces it by __memcpy() when KASAN is active
+	 */
+	memcpy(t, s, sizeof(*t));
 
 	/*
 	 * If we are overriding a previous value derived from the real
arch/powerpc/kernel/setup_32.c +2 -4
index 947f904688b0..5e761eb16a6d 100644
@@ -73,10 +73,8 @@ notrace unsigned long __init early_init(unsigned long dt_ptr)
 {
 	unsigned long offset = reloc_offset();
 
-	/* First zero the BSS -- use memset_io, some platforms don't have
-	 * caches on yet */
-	memset_io((void __iomem *)PTRRELOC(&__bss_start), 0,
-			__bss_stop - __bss_start);
+	/* First zero the BSS */
+	memset(PTRRELOC(&__bss_start), 0, __bss_stop - __bss_start);
 
 	/*
 	 * Identify the CPU type and fix up code sections
Signature · 1 line
2.13.3
#2 lore raw

PATCH v6 2/6 powerpc/32: Move early_init() in a separate file

In reply to Christophe Leroy

In preparation of KASAN, move early_init() into a separate file in order to allow deactivation of KASAN for that function.

---
 arch/powerpc/kernel/Makefile   |  2 +-
 arch/powerpc/kernel/early_32.c | 36 ++++++++++++++++++++++++++++++++++++
 arch/powerpc/kernel/setup_32.c | 26 --------------------------
 3 files changed, 37 insertions(+), 27 deletions(-)
 create mode 100644 arch/powerpc/kernel/early_32.c
arch/powerpc/kernel/Makefile +1 -1
index cb7f0bb9ee71..879b36602748 100644
@@ -93,7 +93,7 @@ extra-y				+= vmlinux.lds
 
 obj-$(CONFIG_RELOCATABLE)	+= reloc_$(BITS).o
 
-obj-$(CONFIG_PPC32)		+= entry_32.o setup_32.o
+obj-$(CONFIG_PPC32)		+= entry_32.o setup_32.o early_32.o
 obj-$(CONFIG_PPC64)		+= dma-iommu.o iommu.o
 obj-$(CONFIG_KGDB)		+= kgdb.o
 obj-$(CONFIG_BOOTX_TEXT)	+= btext.o
arch/powerpc/kernel/early_32.c new file +36 -0
new file mode 100644
index 000000000000..3482118ffe76
@@ -0,0 +1,36 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Early init before relocation
+ */
+
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <asm/setup.h>
+#include <asm/sections.h>
+#include <asm/asm-prototypes.h>
+
+/*
+ * We're called here very early in the boot.
+ *
+ * Note that the kernel may be running at an address which is different
+ * from the address that it was linked at, so we must use RELOC/PTRRELOC
+ * to access static data (including strings).  -- paulus
+ */
+notrace unsigned long __init early_init(unsigned long dt_ptr)
+{
+	unsigned long offset = reloc_offset();
+
+	/* First zero the BSS */
+	memset(PTRRELOC(&__bss_start), 0, __bss_stop - __bss_start);
+
+	/*
+	 * Identify the CPU type and fix up code sections
+	 * that depend on which cpu we have.
+	 */
+	identify_cpu(offset, mfspr(SPRN_PVR));
+
+	apply_feature_fixups();
+
+	return KERNELBASE + offset;
+}
arch/powerpc/kernel/setup_32.c +0 -26
index 5e761eb16a6d..b46a9a33225b 100644
@@ -63,32 +63,6 @@ EXPORT_SYMBOL(DMA_MODE_READ);
 EXPORT_SYMBOL(DMA_MODE_WRITE);
 
 /*
- * We're called here very early in the boot.
- *
- * Note that the kernel may be running at an address which is different
- * from the address that it was linked at, so we must use RELOC/PTRRELOC
- * to access static data (including strings).  -- paulus
- */
-notrace unsigned long __init early_init(unsigned long dt_ptr)
-{
-	unsigned long offset = reloc_offset();
-
-	/* First zero the BSS */
-	memset(PTRRELOC(&__bss_start), 0, __bss_stop - __bss_start);
-
-	/*
-	 * Identify the CPU type and fix up code sections
-	 * that depend on which cpu we have.
-	 */
-	identify_cpu(offset, mfspr(SPRN_PVR));
-
-	apply_feature_fixups();
-
-	return KERNELBASE + offset;
-}
-
-
-/*
  * This is run before start_kernel(), the kernel has been relocated
  * and we are running with enough of the MMU enabled to have our
  * proper kernel virtual addresses
Signature · 1 line
2.13.3
#3 lore raw

PATCH v6 3/6 powerpc: prepare string/mem functions for KASAN

In reply to Christophe Leroy

CONFIG_KASAN implements wrappers for memcpy() memmove() and memset() Those wrappers are doing the verification then call respectively __memcpy() __memmove() and __memset(). The arches are therefore expected to rename their optimised functions that way.

For files on which KASAN is inhibited, #defines are used to allow them to directly call optimised versions of the functions without going through the KASAN wrappers.

See 393f203f5fd5 ("x86_64: kasan: add interceptors for memset/memmove/memcpy functions") for details.

Other string / mem functions do not (yet) have kasan wrappers, we therefore have to fallback to the generic versions when KASAN is active, otherwise KASAN checks will be skipped.

---
 arch/powerpc/include/asm/kasan.h       | 15 +++++++++++++++
 arch/powerpc/include/asm/string.h      | 26 ++++++++++++++++++++++++--
 arch/powerpc/kernel/prom_init_check.sh | 10 +++++++++-
 arch/powerpc/lib/Makefile              |  8 ++++++--
 arch/powerpc/lib/copy_32.S             | 13 +++++++------
 arch/powerpc/lib/mem_64.S              |  8 ++++----
 arch/powerpc/lib/memcpy_64.S           |  4 ++--
 7 files changed, 67 insertions(+), 17 deletions(-)
 create mode 100644 arch/powerpc/include/asm/kasan.h
arch/powerpc/include/asm/kasan.h new file +15 -0
new file mode 100644
index 000000000000..2efd0e42cfc9
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __ASM_KASAN_H
+#define __ASM_KASAN_H
+
+#ifdef CONFIG_KASAN
+#define _GLOBAL_KASAN(fn)	.weak fn ; _GLOBAL(__##fn) ; _GLOBAL(fn)
+#define _GLOBAL_KASAN_TOC(fn)	.weak fn ; _GLOBAL_TOC(__##fn) ; _GLOBAL_TOC(fn)
+#define EXPORT_SYMBOL_KASAN(fn)	EXPORT_SYMBOL(__##fn) ; EXPORT_SYMBOL(fn)
+#else
+#define _GLOBAL_KASAN(fn)	_GLOBAL(fn)
+#define _GLOBAL_KASAN_TOC(fn)	_GLOBAL_TOC(fn)
+#define EXPORT_SYMBOL_KASAN(fn)	EXPORT_SYMBOL(fn)
+#endif
+
+#endif
arch/powerpc/include/asm/string.h +24 -2
index 1647de15a31e..2aa9ea6751cd 100644
@@ -4,13 +4,16 @@
 
 #ifdef __KERNEL__
 
+#ifndef CONFIG_KASAN
 #define __HAVE_ARCH_STRNCPY
 #define __HAVE_ARCH_STRNCMP
+#define __HAVE_ARCH_MEMCHR
+#define __HAVE_ARCH_MEMCMP
+#endif
+
 #define __HAVE_ARCH_MEMSET
 #define __HAVE_ARCH_MEMCPY
 #define __HAVE_ARCH_MEMMOVE
-#define __HAVE_ARCH_MEMCMP
-#define __HAVE_ARCH_MEMCHR
 #define __HAVE_ARCH_MEMSET16
 #define __HAVE_ARCH_MEMCPY_FLUSHCACHE
 
@@ -27,6 +30,25 @@ extern int memcmp(const void *,const void *,__kernel_size_t);
 extern void * memchr(const void *,int,__kernel_size_t);
 extern void * memcpy_flushcache(void *,const void *,__kernel_size_t);
 
+void *__memset(void *s, int c, __kernel_size_t count);
+void *__memcpy(void *to, const void *from, __kernel_size_t n);
+void *__memmove(void *to, const void *from, __kernel_size_t n);
+
+#if defined(CONFIG_KASAN) && !defined(__SANITIZE_ADDRESS__)
+/*
+ * For files that are not instrumented (e.g. mm/slub.c) we
+ * should use not instrumented version of mem* functions.
+ */
+#define memcpy(dst, src, len) __memcpy(dst, src, len)
+#define memmove(dst, src, len) __memmove(dst, src, len)
+#define memset(s, c, n) __memset(s, c, n)
+
+#ifndef __NO_FORTIFY
+#define __NO_FORTIFY /* FORTIFY_SOURCE uses __builtin_memcpy, etc. */
+#endif
+
+#endif
+
 #ifdef CONFIG_PPC64
 #define __HAVE_ARCH_MEMSET32
 #define __HAVE_ARCH_MEMSET64
arch/powerpc/kernel/prom_init_check.sh +9 -1
index 667df97d2595..da6bb16e0876 100644
@@ -16,8 +16,16 @@
 # If you really need to reference something from prom_init.o add
 # it to the list below:
 
+grep CONFIG_KASAN=y .config >/dev/null
+if [ $? -eq 0 ]
+then
+	MEMFCT="__memcpy __memset"
+else
+	MEMFCT="memcpy memset"
+fi
+
 WHITELIST="add_reloc_offset __bss_start __bss_stop copy_and_flush
-_end enter_prom memcpy memset reloc_offset __secondary_hold
+_end enter_prom $MEMFCT reloc_offset __secondary_hold
 __secondary_hold_acknowledge __secondary_hold_spinloop __start
 strcmp strcpy strlcpy strlen strncmp strstr kstrtobool logo_linux_clut224
 reloc_got2 kernstart_addr memstart_addr linux_banner _stext
arch/powerpc/lib/Makefile +6 -2
index 3bf9fc6fd36c..ee08a7e1bcdf 100644
@@ -8,7 +8,11 @@ ccflags-$(CONFIG_PPC64)	:= $(NO_MINIMAL_TOC)
 CFLAGS_REMOVE_code-patching.o = $(CC_FLAGS_FTRACE)
 CFLAGS_REMOVE_feature-fixups.o = $(CC_FLAGS_FTRACE)
 
-obj-y += string.o alloc.o code-patching.o feature-fixups.o
+obj-y += alloc.o code-patching.o feature-fixups.o
+
+ifndef CONFIG_KASAN
+obj-y	+=	string.o memcmp_$(BITS).o
+endif
 
 obj-$(CONFIG_PPC32)	+= div64.o copy_32.o crtsavres.o strlen_32.o
 
@@ -33,7 +37,7 @@ obj64-$(CONFIG_ALTIVEC)	+= vmx-helper.o
 obj64-$(CONFIG_KPROBES_SANITY_TEST) += test_emulate_step.o
 
 obj-y			+= checksum_$(BITS).o checksum_wrappers.o \
-			   string_$(BITS).o memcmp_$(BITS).o
+			   string_$(BITS).o
 
 obj-y			+= sstep.o ldstfp.o quad.o
 obj64-y			+= quad.o
arch/powerpc/lib/copy_32.S +7 -6
index ba66846fe973..57f48d99fbe3 100644
@@ -14,6 +14,7 @@
 #include <asm/ppc_asm.h>
 #include <asm/export.h>
 #include <asm/code-patching-asm.h>
+#include <asm/kasan.h>
 
 #define COPY_16_BYTES		\
 	lwz	r7,4(r4);	\
@@ -91,7 +92,7 @@ EXPORT_SYMBOL(memset16)
  * We therefore skip the optimised bloc that uses dcbz. This jump is
  * replaced by a nop once cache is active. This is done in machine_init()
  */
-_GLOBAL(memset)
+_GLOBAL_KASAN(memset)
 	cmplwi	0,r5,4
 	blt	7f
 
@@ -150,7 +151,7 @@ _GLOBAL(memset)
 9:	stbu	r4,1(r6)
 	bdnz	9b
 	blr
-EXPORT_SYMBOL(memset)
+EXPORT_SYMBOL_KASAN(memset)
 
 /*
  * This version uses dcbz on the complete cache lines in the
@@ -163,12 +164,12 @@ EXPORT_SYMBOL(memset)
  * We therefore jump to generic_memcpy which doesn't use dcbz. This jump is
  * replaced by a nop once cache is active. This is done in machine_init()
  */
-_GLOBAL(memmove)
+_GLOBAL_KASAN(memmove)
 	cmplw	0,r3,r4
 	bgt	backwards_memcpy
 	/* fall through */
 
-_GLOBAL(memcpy)
+_GLOBAL_KASAN(memcpy)
 1:	b	generic_memcpy
 	patch_site	1b, patch__memcpy_nocache
 
@@ -242,8 +243,8 @@ _GLOBAL(memcpy)
 	stbu	r0,1(r6)
 	bdnz	40b
 65:	blr
-EXPORT_SYMBOL(memcpy)
-EXPORT_SYMBOL(memmove)
+EXPORT_SYMBOL_KASAN(memcpy)
+EXPORT_SYMBOL_KASAN(memmove)
 
 generic_memcpy:
 	srwi.	r7,r5,3
arch/powerpc/lib/mem_64.S +4 -4
index 3c3be02f33b7..57c8a940c29c 100644
@@ -30,7 +30,7 @@ EXPORT_SYMBOL(__memset16)
 EXPORT_SYMBOL(__memset32)
 EXPORT_SYMBOL(__memset64)
 
-_GLOBAL(memset)
+_GLOBAL_KASAN(memset)
 	neg	r0,r3
 	rlwimi	r4,r4,8,16,23
 	andi.	r0,r0,7			/* # bytes to be 8-byte aligned */
@@ -95,9 +95,9 @@ _GLOBAL(memset)
 10:	bflr	31
 	stb	r4,0(r6)
 	blr
-EXPORT_SYMBOL(memset)
+EXPORT_SYMBOL_KASAN(memset)
 
-_GLOBAL_TOC(memmove)
+_GLOBAL_TOC_KASAN(memmove)
 	cmplw	0,r3,r4
 	bgt	backwards_memcpy
 	b	memcpy
@@ -138,4 +138,4 @@ _GLOBAL(backwards_memcpy)
 	beq	2b
 	mtctr	r7
 	b	1b
-EXPORT_SYMBOL(memmove)
+EXPORT_SYMBOL_KASAN(memmove)
arch/powerpc/lib/memcpy_64.S +2 -2
index 273ea67e60a1..2d5358cee711 100644
@@ -18,7 +18,7 @@
 #endif
 
 	.align	7
-_GLOBAL_TOC(memcpy)
+_GLOBAL_TOC_KASAN(memcpy)
 BEGIN_FTR_SECTION
 #ifdef __LITTLE_ENDIAN__
 	cmpdi	cr7,r5,0
@@ -229,4 +229,4 @@ END_FTR_SECTION_IFCLR(CPU_FTR_UNALIGNED_LD_STD)
 4:	ld	r3,-STACKFRAMESIZE+STK_REG(R31)(r1)	/* return dest pointer */
 	blr
 #endif
-EXPORT_SYMBOL(memcpy)
+EXPORT_SYMBOL_KASAN(memcpy)
Signature · 1 line
2.13.3
#4 lore raw

PATCH v6 4/6 powerpc/32: Add KASAN support

In reply to Christophe Leroy

This patch adds KASAN support for PPC32.

The KASAN shadow area is located between the vmalloc area and the fixmap area.

KASAN_SHADOW_OFFSET is calculated in asm/kasan.h and extracted by Makefile prepare rule via asm-offsets.h

For modules, the shadow area is allocated at module_alloc().

Note that on book3s it will only work on the 603 because the other ones use hash table and can therefore not share a single PTE table covering the entire early KASAN shadow area.

---
 arch/powerpc/Kconfig                          |   1 +
 arch/powerpc/Makefile                         |   7 ++
 arch/powerpc/include/asm/book3s/32/pgtable.h  |   2 +
 arch/powerpc/include/asm/highmem.h            |  10 +-
 arch/powerpc/include/asm/kasan.h              |  23 ++++
 arch/powerpc/include/asm/nohash/32/pgtable.h  |   2 +
 arch/powerpc/include/asm/setup.h              |   5 +
 arch/powerpc/kernel/Makefile                  |   9 +-
 arch/powerpc/kernel/asm-offsets.c             |   4 +
 arch/powerpc/kernel/head_32.S                 |   3 +
 arch/powerpc/kernel/head_40x.S                |   3 +
 arch/powerpc/kernel/head_44x.S                |   3 +
 arch/powerpc/kernel/head_8xx.S                |   3 +
 arch/powerpc/kernel/head_fsl_booke.S          |   3 +
 arch/powerpc/kernel/setup-common.c            |   2 +
 arch/powerpc/lib/Makefile                     |   8 ++
 arch/powerpc/mm/Makefile                      |   1 +
 arch/powerpc/mm/kasan/Makefile                |   5 +
 arch/powerpc/mm/kasan/kasan_init_32.c         | 147 ++++++++++++++++++++++++++
 arch/powerpc/mm/mem.c                         |   4 +
 arch/powerpc/mm/ptdump/dump_linuxpagetables.c |   8 ++
 arch/powerpc/purgatory/Makefile               |   3 +
 arch/powerpc/xmon/Makefile                    |   1 +
 23 files changed, 253 insertions(+), 4 deletions(-)
 create mode 100644 arch/powerpc/mm/kasan/Makefile
 create mode 100644 arch/powerpc/mm/kasan/kasan_init_32.c
arch/powerpc/Kconfig +1 -0
index 08908219fba9..850b06def84f 100644
@@ -175,6 +175,7 @@ config PPC
 	select GENERIC_TIME_VSYSCALL
 	select HAVE_ARCH_AUDITSYSCALL
 	select HAVE_ARCH_JUMP_LABEL
+	select HAVE_ARCH_KASAN			if PPC32
 	select HAVE_ARCH_KGDB
 	select HAVE_ARCH_MMAP_RND_BITS
 	select HAVE_ARCH_MMAP_RND_COMPAT_BITS	if COMPAT
arch/powerpc/Makefile +7 -0
index ac033341ed55..f0738099e31e 100644
@@ -427,6 +427,13 @@ else
 endif
 endif
 
+ifdef CONFIG_KASAN
+prepare: kasan_prepare
+
+kasan_prepare: prepare0
+       $(eval KASAN_SHADOW_OFFSET = $(shell awk '{if ($$2 == "KASAN_SHADOW_OFFSET") print $$3;}' include/generated/asm-offsets.h))
+endif
+
 # Check toolchain versions:
 # - gcc-4.6 is the minimum kernel-wide version so nothing required.
 checkbin:
arch/powerpc/include/asm/book3s/32/pgtable.h +2 -0
index 49d76adb9bc5..4543016f80ca 100644
@@ -141,6 +141,8 @@ static inline bool pte_user(pte_t pte)
  */
 #ifdef CONFIG_HIGHMEM
 #define KVIRT_TOP	PKMAP_BASE
+#elif defined(CONFIG_KASAN)
+#define KVIRT_TOP	KASAN_SHADOW_START
 #else
 #define KVIRT_TOP	(0xfe000000UL)	/* for now, could be FIXMAP_BASE ? */
 #endif
arch/powerpc/include/asm/highmem.h +8 -2
index a4b65b186ec6..483b90025bef 100644
@@ -28,6 +28,7 @@
 #include <asm/cacheflush.h>
 #include <asm/page.h>
 #include <asm/fixmap.h>
+#include <asm/kasan.h>
 
 extern pte_t *kmap_pte;
 extern pgprot_t kmap_prot;
@@ -50,10 +51,15 @@ extern pte_t *pkmap_page_table;
 #define PKMAP_ORDER	9
 #endif
 #define LAST_PKMAP	(1 << PKMAP_ORDER)
+#ifdef CONFIG_KASAN
+#define PKMAP_TOP	KASAN_SHADOW_START
+#else
+#define PKMAP_TOP	FIXADDR_START
+#endif
 #ifndef CONFIG_PPC_4K_PAGES
-#define PKMAP_BASE	(FIXADDR_START - PAGE_SIZE*(LAST_PKMAP + 1))
+#define PKMAP_BASE	(PKMAP_TOP - PAGE_SIZE*(LAST_PKMAP + 1))
 #else
-#define PKMAP_BASE	((FIXADDR_START - PAGE_SIZE*(LAST_PKMAP + 1)) & PMD_MASK)
+#define PKMAP_BASE	((PKMAP_TOP - PAGE_SIZE*(LAST_PKMAP + 1)) & PMD_MASK)
 #endif
 #define LAST_PKMAP_MASK	(LAST_PKMAP-1)
 #define PKMAP_NR(virt)  ((virt-PKMAP_BASE) >> PAGE_SHIFT)
arch/powerpc/include/asm/kasan.h +23 -0
index 2efd0e42cfc9..0bc9148f5d87 100644
@@ -12,4 +12,27 @@
 #define EXPORT_SYMBOL_KASAN(fn)	EXPORT_SYMBOL(fn)
 #endif
 
+#ifndef __ASSEMBLY__
+
+#include <asm/page.h>
+#include <asm/pgtable-types.h>
+
+#define KASAN_SHADOW_SCALE_SHIFT	3
+
+#define KASAN_SHADOW_OFFSET	(KASAN_SHADOW_START - \
+				 (PAGE_OFFSET >> KASAN_SHADOW_SCALE_SHIFT))
+
+#define KASAN_SHADOW_END	(KASAN_SHADOW_START + KASAN_SHADOW_SIZE)
+
+#include <asm/fixmap.h>
+
+#define KASAN_SHADOW_START	(ALIGN_DOWN(FIXADDR_START - KASAN_SHADOW_SIZE, \
+					    PGDIR_SIZE))
+
+#define KASAN_SHADOW_SIZE	((~0UL - PAGE_OFFSET + 1) >> KASAN_SHADOW_SCALE_SHIFT)
+
+void kasan_early_init(void);
+void kasan_init(void);
+
+#endif /* __ASSEMBLY */
 #endif
arch/powerpc/include/asm/nohash/32/pgtable.h +2 -0
index bed433358260..b3b52f02be1a 100644
@@ -71,6 +71,8 @@ extern int icache_44x_need_flush;
  */
 #ifdef CONFIG_HIGHMEM
 #define KVIRT_TOP	PKMAP_BASE
+#elif defined(CONFIG_KASAN)
+#define KVIRT_TOP	KASAN_SHADOW_START
 #else
 #define KVIRT_TOP	(0xfe000000UL)	/* for now, could be FIXMAP_BASE ? */
 #endif
arch/powerpc/include/asm/setup.h +5 -0
index 65676e2325b8..da7768aa996a 100644
@@ -74,6 +74,11 @@ static inline void setup_spectre_v2(void) {};
 #endif
 void do_btb_flush_fixups(void);
 
+#ifndef CONFIG_KASAN
+static inline void kasan_early_init(void) { }
+static inline void kasan_init(void) { }
+#endif
+
 #endif /* !__ASSEMBLY__ */
 
 #endif	/* _ASM_POWERPC_SETUP_H */
arch/powerpc/kernel/Makefile +7 -2
index 879b36602748..fc4c42262694 100644
@@ -16,8 +16,9 @@ CFLAGS_prom_init.o      += -fPIC
 CFLAGS_btext.o		+= -fPIC
 endif
 
-CFLAGS_cputable.o += $(DISABLE_LATENT_ENTROPY_PLUGIN)
-CFLAGS_prom_init.o += $(DISABLE_LATENT_ENTROPY_PLUGIN)
+CFLAGS_early_32.o += -DDISABLE_BRANCH_PROFILING
+CFLAGS_cputable.o += $(DISABLE_LATENT_ENTROPY_PLUGIN) -DDISABLE_BRANCH_PROFILING
+CFLAGS_prom_init.o += $(DISABLE_LATENT_ENTROPY_PLUGIN) -DDISABLE_BRANCH_PROFILING
 CFLAGS_btext.o += $(DISABLE_LATENT_ENTROPY_PLUGIN)
 CFLAGS_prom.o += $(DISABLE_LATENT_ENTROPY_PLUGIN)
 
@@ -31,6 +32,10 @@ CFLAGS_REMOVE_btext.o = $(CC_FLAGS_FTRACE)
 CFLAGS_REMOVE_prom.o = $(CC_FLAGS_FTRACE)
 endif
 
+KASAN_SANITIZE_early_32.o := n
+KASAN_SANITIZE_cputable.o := n
+KASAN_SANITIZE_prom_init.o := n
+
 obj-y				:= cputable.o ptrace.o syscalls.o \
 				   irq.o align.o signal_32.o pmc.o vdso.o \
 				   process.o systbl.o idle.o \
arch/powerpc/kernel/asm-offsets.c +4 -0
index 9ffc72ded73a..846fb30b1190 100644
@@ -783,5 +783,9 @@ int main(void)
 	DEFINE(VIRT_IMMR_BASE, (u64)__fix_to_virt(FIX_IMMR_BASE));
 #endif
 
+#ifdef CONFIG_KASAN
+	DEFINE(KASAN_SHADOW_OFFSET, KASAN_SHADOW_OFFSET);
+#endif
+
 	return 0;
 }
arch/powerpc/kernel/head_32.S +3 -0
index 05b08db3901d..0ec9dec06bc2 100644
@@ -962,6 +962,9 @@ start_here:
  * Do early platform-specific initialization,
  * and set up the MMU.
  */
+#ifdef CONFIG_KASAN
+	bl	kasan_early_init
+#endif
 	li	r3,0
 	mr	r4,r31
 	bl	machine_init
arch/powerpc/kernel/head_40x.S +3 -0
index b19d78410511..5d6ff8fa7e2b 100644
@@ -848,6 +848,9 @@ start_here:
 /*
  * Decide what sort of machine this is and initialize the MMU.
  */
+#ifdef CONFIG_KASAN
+	bl	kasan_early_init
+#endif
 	li	r3,0
 	mr	r4,r31
 	bl	machine_init
arch/powerpc/kernel/head_44x.S +3 -0
index bf23c19c92d6..7ca14dff6192 100644
@@ -203,6 +203,9 @@ _ENTRY(_start);
 /*
  * Decide what sort of machine this is and initialize the MMU.
  */
+#ifdef CONFIG_KASAN
+	bl	kasan_early_init
+#endif
 	li	r3,0
 	mr	r4,r31
 	bl	machine_init
arch/powerpc/kernel/head_8xx.S +3 -0
index fe2857ef0309..88f0cb34cef4 100644
@@ -823,6 +823,9 @@ start_here:
 /*
  * Decide what sort of machine this is and initialize the MMU.
  */
+#ifdef CONFIG_KASAN
+	bl	kasan_early_init
+#endif
 	li	r3,0
 	mr	r4,r31
 	bl	machine_init
arch/powerpc/kernel/head_fsl_booke.S +3 -0
index 2386ce2a9c6e..4f4585a68850 100644
@@ -274,6 +274,9 @@ set_ivor:
 /*
  * Decide what sort of machine this is and initialize the MMU.
  */
+#ifdef CONFIG_KASAN
+	bl	kasan_early_init
+#endif
 	mr	r3,r30
 	mr	r4,r31
 	bl	machine_init
arch/powerpc/kernel/setup-common.c +2 -0
index ca00fbb97cf8..16ff1ea66805 100644
@@ -978,6 +978,8 @@ void __init setup_arch(char **cmdline_p)
 
 	paging_init();
 
+	kasan_init();
+
 	/* Initialize the MMU context management stuff. */
 	mmu_context_init();
 
arch/powerpc/lib/Makefile +8 -0
index ee08a7e1bcdf..7efbd5122c74 100644
@@ -8,6 +8,14 @@ ccflags-$(CONFIG_PPC64)	:= $(NO_MINIMAL_TOC)
 CFLAGS_REMOVE_code-patching.o = $(CC_FLAGS_FTRACE)
 CFLAGS_REMOVE_feature-fixups.o = $(CC_FLAGS_FTRACE)
 
+KASAN_SANITIZE_code-patching.o := n
+KASAN_SANITIZE_feature-fixups.o := n
+
+ifdef CONFIG_KASAN
+CFLAGS_code-patching.o += -DDISABLE_BRANCH_PROFILING
+CFLAGS_feature-fixups.o += -DDISABLE_BRANCH_PROFILING
+endif
+
 obj-y += alloc.o code-patching.o feature-fixups.o
 
 ifndef CONFIG_KASAN
arch/powerpc/mm/Makefile +1 -0
index ee1efa3b3382..292b96ce1efc 100644
@@ -47,3 +47,4 @@ obj-$(CONFIG_PPC_COPRO_BASE)	+= copro_fault.o
 obj-$(CONFIG_SPAPR_TCE_IOMMU)	+= mmu_context_iommu.o
 obj-$(CONFIG_PPC_PTDUMP)	+= ptdump/
 obj-$(CONFIG_PPC_MEM_KEYS)	+= pkeys.o
+obj-$(CONFIG_KASAN)		+= kasan/
arch/powerpc/mm/kasan/Makefile new file +5 -0
new file mode 100644
index 000000000000..6577897673dd
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: GPL-2.0
+
+KASAN_SANITIZE := n
+
+obj-$(CONFIG_PPC32)           += kasan_init_32.o
arch/powerpc/mm/kasan/kasan_init_32.c new file +147 -0

147 lines not on this page · raw message

arch/powerpc/mm/mem.c +4 -0
index 81f251fc4169..1bb055775e60 100644
@@ -336,6 +336,10 @@ void __init mem_init(void)
 	pr_info("  * 0x%08lx..0x%08lx  : highmem PTEs\n",
 		PKMAP_BASE, PKMAP_ADDR(LAST_PKMAP));
 #endif /* CONFIG_HIGHMEM */
+#ifdef CONFIG_KASAN
+	pr_info("  * 0x%08lx..0x%08lx  : kasan shadow mem\n",
+		KASAN_SHADOW_START, KASAN_SHADOW_END);
+#endif
 #ifdef CONFIG_NOT_COHERENT_CACHE
 	pr_info("  * 0x%08lx..0x%08lx  : consistent mem\n",
 		IOREMAP_TOP, IOREMAP_TOP + CONFIG_CONSISTENT_SIZE);
arch/powerpc/mm/ptdump/dump_linuxpagetables.c +8 -0
index b0da447197d4..4a03910974e2 100644
@@ -94,6 +94,10 @@ static struct addr_marker address_markers[] = {
 	{ 0,	"Consistent mem start" },
 	{ 0,	"Consistent mem end" },
 #endif
+#ifdef CONFIG_KASAN
+	{ 0,	"kasan shadow mem start" },
+	{ 0,	"kasan shadow mem end" },
+#endif
 #ifdef CONFIG_HIGHMEM
 	{ 0,	"Highmem PTEs start" },
 	{ 0,	"Highmem PTEs end" },
@@ -316,6 +320,10 @@ static void populate_markers(void)
 	address_markers[i++].start_address = IOREMAP_TOP +
 					     CONFIG_CONSISTENT_SIZE;
 #endif
+#ifdef CONFIG_KASAN
+	address_markers[i++].start_address = KASAN_SHADOW_START;
+	address_markers[i++].start_address = KASAN_SHADOW_END;
+#endif
 #ifdef CONFIG_HIGHMEM
 	address_markers[i++].start_address = PKMAP_BASE;
 	address_markers[i++].start_address = PKMAP_ADDR(LAST_PKMAP);
arch/powerpc/purgatory/Makefile +3 -0
index 4314ba5baf43..7c6d8b14f440 100644
@@ -1,4 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0
+
+KASAN_SANITIZE := n
+
 targets += trampoline.o purgatory.ro kexec-purgatory.c
 
 LDFLAGS_purgatory.ro := -e purgatory_start -r --no-undefined
arch/powerpc/xmon/Makefile +1 -0
index 878f9c1d3615..064f7062c0a3 100644
@@ -6,6 +6,7 @@ subdir-ccflags-y := $(call cc-disable-warning, builtin-requires-header)
 
 GCOV_PROFILE := n
 UBSAN_SANITIZE := n
+KASAN_SANITIZE := n
 
 # Disable ftrace for the entire directory
 ORIG_CFLAGS := $(KBUILD_CFLAGS)
Signature · 1 line
2.13.3
#5 lore raw

Re: [PATCH v6 4/6] powerpc/32: Add KASAN support

Le 19/02/2019 à 18:23, Christophe Leroy a écrit :

Quoted 29 lines

This patch adds KASAN support for PPC32.

The KASAN shadow area is located between the vmalloc area and the fixmap area.

KASAN_SHADOW_OFFSET is calculated in asm/kasan.h and extracted by Makefile prepare rule via asm-offsets.h

For modules, the shadow area is allocated at module_alloc().

Note that on book3s it will only work on the 603 because the other ones use hash table and can therefore not share a single PTE table covering the entire early KASAN shadow area.

---
  arch/powerpc/Kconfig                          |   1 +
  arch/powerpc/Makefile                         |   7 ++
  arch/powerpc/include/asm/book3s/32/pgtable.h  |   2 +
  arch/powerpc/include/asm/highmem.h            |  10 +-
  arch/powerpc/include/asm/kasan.h              |  23 ++++
  arch/powerpc/include/asm/nohash/32/pgtable.h  |   2 +
  arch/powerpc/include/asm/setup.h              |   5 +
  arch/powerpc/kernel/Makefile                  |   9 +-
  arch/powerpc/kernel/asm-offsets.c             |   4 +
  arch/powerpc/kernel/head_32.S                 |   3 +
  arch/powerpc/kernel/head_40x.S                |   3 +
  arch/powerpc/kernel/head_44x.S                |   3 +
  arch/powerpc/kernel/head_8xx.S                |   3 +
Quoted 8 lines
  arch/powerpc/kernel/head_fsl_booke.S          |   3 +
  arch/powerpc/kernel/setup-common.c            |   2 +
  arch/powerpc/lib/Makefile                     |   8 ++
  arch/powerpc/mm/Makefile                      |   1 +
  arch/powerpc/mm/kasan/Makefile                |   5 +
  arch/powerpc/mm/kasan/kasan_init_32.c         | 147 ++++++++++++++++++++++++++
  arch/powerpc/mm/mem.c                         |   4 +
  arch/powerpc/mm/ptdump/dump_linuxpagetables.c |   8 ++

@Daniel (and others), note that to apply properly, this requires my other patch which moves the dumping files in a arch/powerpc/mm/ptdump/ subdir.

Christophe

Quoted 522 lines
  arch/powerpc/purgatory/Makefile               |   3 +
  arch/powerpc/xmon/Makefile                    |   1 +
  23 files changed, 253 insertions(+), 4 deletions(-)

create mode 100644 arch/powerpc/mm/kasan/Makefile create mode 100644 arch/powerpc/mm/kasan/kasan_init_32.c

arch/powerpc/Kconfig +1 -0
index 08908219fba9..850b06def84f 100644
@@ -175,6 +175,7 @@ config PPC
  	select GENERIC_TIME_VSYSCALL
  	select HAVE_ARCH_AUDITSYSCALL
  	select HAVE_ARCH_JUMP_LABEL
+	select HAVE_ARCH_KASAN			if PPC32
  	select HAVE_ARCH_KGDB
  	select HAVE_ARCH_MMAP_RND_BITS
  	select HAVE_ARCH_MMAP_RND_COMPAT_BITS	if COMPAT
arch/powerpc/Makefile +7 -0
index ac033341ed55..f0738099e31e 100644
@@ -427,6 +427,13 @@ else
  endif
  endif
 
+ifdef CONFIG_KASAN
+prepare: kasan_prepare
+
+kasan_prepare: prepare0
+       $(eval KASAN_SHADOW_OFFSET = $(shell awk '{if ($$2 == "KASAN_SHADOW_OFFSET") print $$3;}' include/generated/asm-offsets.h))
+endif
+
  # Check toolchain versions:
  # - gcc-4.6 is the minimum kernel-wide version so nothing required.
  checkbin:
arch/powerpc/include/asm/book3s/32/pgtable.h +2 -0
index 49d76adb9bc5..4543016f80ca 100644
@@ -141,6 +141,8 @@ static inline bool pte_user(pte_t pte)
   */
  #ifdef CONFIG_HIGHMEM
  #define KVIRT_TOP	PKMAP_BASE
+#elif defined(CONFIG_KASAN)
+#define KVIRT_TOP	KASAN_SHADOW_START
  #else
  #define KVIRT_TOP	(0xfe000000UL)	/* for now, could be FIXMAP_BASE ? */
  #endif
arch/powerpc/include/asm/highmem.h +8 -2
index a4b65b186ec6..483b90025bef 100644
@@ -28,6 +28,7 @@
  #include <asm/cacheflush.h>
  #include <asm/page.h>
  #include <asm/fixmap.h>
+#include <asm/kasan.h>
 
  extern pte_t *kmap_pte;
  extern pgprot_t kmap_prot;
@@ -50,10 +51,15 @@ extern pte_t *pkmap_page_table;
  #define PKMAP_ORDER	9
  #endif
  #define LAST_PKMAP	(1 << PKMAP_ORDER)
+#ifdef CONFIG_KASAN
+#define PKMAP_TOP	KASAN_SHADOW_START
+#else
+#define PKMAP_TOP	FIXADDR_START
+#endif
  #ifndef CONFIG_PPC_4K_PAGES
-#define PKMAP_BASE	(FIXADDR_START - PAGE_SIZE*(LAST_PKMAP + 1))
+#define PKMAP_BASE	(PKMAP_TOP - PAGE_SIZE*(LAST_PKMAP + 1))
  #else
-#define PKMAP_BASE	((FIXADDR_START - PAGE_SIZE*(LAST_PKMAP + 1)) & PMD_MASK)
+#define PKMAP_BASE	((PKMAP_TOP - PAGE_SIZE*(LAST_PKMAP + 1)) & PMD_MASK)
  #endif
  #define LAST_PKMAP_MASK	(LAST_PKMAP-1)
  #define PKMAP_NR(virt)  ((virt-PKMAP_BASE) >> PAGE_SHIFT)
arch/powerpc/include/asm/kasan.h +23 -0
index 2efd0e42cfc9..0bc9148f5d87 100644
@@ -12,4 +12,27 @@
  #define EXPORT_SYMBOL_KASAN(fn)	EXPORT_SYMBOL(fn)
  #endif
 
+#ifndef __ASSEMBLY__
+
+#include <asm/page.h>
+#include <asm/pgtable-types.h>
+
+#define KASAN_SHADOW_SCALE_SHIFT	3
+
+#define KASAN_SHADOW_OFFSET	(KASAN_SHADOW_START - \
+				 (PAGE_OFFSET >> KASAN_SHADOW_SCALE_SHIFT))
+
+#define KASAN_SHADOW_END	(KASAN_SHADOW_START + KASAN_SHADOW_SIZE)
+
+#include <asm/fixmap.h>
+
+#define KASAN_SHADOW_START	(ALIGN_DOWN(FIXADDR_START - KASAN_SHADOW_SIZE, \
+					    PGDIR_SIZE))
+
+#define KASAN_SHADOW_SIZE	((~0UL - PAGE_OFFSET + 1) >> KASAN_SHADOW_SCALE_SHIFT)
+
+void kasan_early_init(void);
+void kasan_init(void);
+
+#endif /* __ASSEMBLY */
  #endif
arch/powerpc/include/asm/nohash/32/pgtable.h +2 -0
index bed433358260..b3b52f02be1a 100644
@@ -71,6 +71,8 @@ extern int icache_44x_need_flush;
   */
  #ifdef CONFIG_HIGHMEM
  #define KVIRT_TOP	PKMAP_BASE
+#elif defined(CONFIG_KASAN)
+#define KVIRT_TOP	KASAN_SHADOW_START
  #else
  #define KVIRT_TOP	(0xfe000000UL)	/* for now, could be FIXMAP_BASE ? */
  #endif
arch/powerpc/include/asm/setup.h +5 -0
index 65676e2325b8..da7768aa996a 100644
@@ -74,6 +74,11 @@ static inline void setup_spectre_v2(void) {};
  #endif
  void do_btb_flush_fixups(void);
 
+#ifndef CONFIG_KASAN
+static inline void kasan_early_init(void) { }
+static inline void kasan_init(void) { }
+#endif
+
  #endif /* !__ASSEMBLY__ */
 
  #endif	/* _ASM_POWERPC_SETUP_H */
arch/powerpc/kernel/Makefile +7 -2
index 879b36602748..fc4c42262694 100644
@@ -16,8 +16,9 @@ CFLAGS_prom_init.o      += -fPIC
  CFLAGS_btext.o		+= -fPIC
  endif
 
-CFLAGS_cputable.o += $(DISABLE_LATENT_ENTROPY_PLUGIN)
-CFLAGS_prom_init.o += $(DISABLE_LATENT_ENTROPY_PLUGIN)
+CFLAGS_early_32.o += -DDISABLE_BRANCH_PROFILING
+CFLAGS_cputable.o += $(DISABLE_LATENT_ENTROPY_PLUGIN) -DDISABLE_BRANCH_PROFILING
+CFLAGS_prom_init.o += $(DISABLE_LATENT_ENTROPY_PLUGIN) -DDISABLE_BRANCH_PROFILING
  CFLAGS_btext.o += $(DISABLE_LATENT_ENTROPY_PLUGIN)
  CFLAGS_prom.o += $(DISABLE_LATENT_ENTROPY_PLUGIN)
 
@@ -31,6 +32,10 @@ CFLAGS_REMOVE_btext.o = $(CC_FLAGS_FTRACE)
  CFLAGS_REMOVE_prom.o = $(CC_FLAGS_FTRACE)
  endif
 
+KASAN_SANITIZE_early_32.o := n
+KASAN_SANITIZE_cputable.o := n
+KASAN_SANITIZE_prom_init.o := n
+
  obj-y				:= cputable.o ptrace.o syscalls.o \
  				   irq.o align.o signal_32.o pmc.o vdso.o \
  				   process.o systbl.o idle.o \
arch/powerpc/kernel/asm-offsets.c +4 -0
index 9ffc72ded73a..846fb30b1190 100644
@@ -783,5 +783,9 @@ int main(void)
  	DEFINE(VIRT_IMMR_BASE, (u64)__fix_to_virt(FIX_IMMR_BASE));
  #endif
 
+#ifdef CONFIG_KASAN
+	DEFINE(KASAN_SHADOW_OFFSET, KASAN_SHADOW_OFFSET);
+#endif
+
  	return 0;
  }
arch/powerpc/kernel/head_32.S +3 -0
index 05b08db3901d..0ec9dec06bc2 100644
@@ -962,6 +962,9 @@ start_here:
   * Do early platform-specific initialization,
   * and set up the MMU.
   */
+#ifdef CONFIG_KASAN
+	bl	kasan_early_init
+#endif
  	li	r3,0
  	mr	r4,r31
  	bl	machine_init
arch/powerpc/kernel/head_40x.S +3 -0
index b19d78410511..5d6ff8fa7e2b 100644
@@ -848,6 +848,9 @@ start_here:
  /*
   * Decide what sort of machine this is and initialize the MMU.
   */
+#ifdef CONFIG_KASAN
+	bl	kasan_early_init
+#endif
  	li	r3,0
  	mr	r4,r31
  	bl	machine_init
arch/powerpc/kernel/head_44x.S +3 -0
index bf23c19c92d6..7ca14dff6192 100644
@@ -203,6 +203,9 @@ _ENTRY(_start);
  /*
   * Decide what sort of machine this is and initialize the MMU.
   */
+#ifdef CONFIG_KASAN
+	bl	kasan_early_init
+#endif
  	li	r3,0
  	mr	r4,r31
  	bl	machine_init
arch/powerpc/kernel/head_8xx.S +3 -0
index fe2857ef0309..88f0cb34cef4 100644
@@ -823,6 +823,9 @@ start_here:
  /*
   * Decide what sort of machine this is and initialize the MMU.
   */
+#ifdef CONFIG_KASAN
+	bl	kasan_early_init
+#endif
  	li	r3,0
  	mr	r4,r31
  	bl	machine_init
arch/powerpc/kernel/head_fsl_booke.S +3 -0
index 2386ce2a9c6e..4f4585a68850 100644
@@ -274,6 +274,9 @@ set_ivor:
  /*
   * Decide what sort of machine this is and initialize the MMU.
   */
+#ifdef CONFIG_KASAN
+	bl	kasan_early_init
+#endif
  	mr	r3,r30
  	mr	r4,r31
  	bl	machine_init
arch/powerpc/kernel/setup-common.c +2 -0
index ca00fbb97cf8..16ff1ea66805 100644
@@ -978,6 +978,8 @@ void __init setup_arch(char **cmdline_p)
 
  	paging_init();
 
+	kasan_init();
+
  	/* Initialize the MMU context management stuff. */
  	mmu_context_init();
 
arch/powerpc/lib/Makefile +8 -0
index ee08a7e1bcdf..7efbd5122c74 100644
@@ -8,6 +8,14 @@ ccflags-$(CONFIG_PPC64)	:= $(NO_MINIMAL_TOC)
  CFLAGS_REMOVE_code-patching.o = $(CC_FLAGS_FTRACE)
  CFLAGS_REMOVE_feature-fixups.o = $(CC_FLAGS_FTRACE)
 
+KASAN_SANITIZE_code-patching.o := n
+KASAN_SANITIZE_feature-fixups.o := n
+
+ifdef CONFIG_KASAN
+CFLAGS_code-patching.o += -DDISABLE_BRANCH_PROFILING
+CFLAGS_feature-fixups.o += -DDISABLE_BRANCH_PROFILING
+endif
+
  obj-y += alloc.o code-patching.o feature-fixups.o
 
  ifndef CONFIG_KASAN
arch/powerpc/mm/Makefile +1 -0
index ee1efa3b3382..292b96ce1efc 100644
@@ -47,3 +47,4 @@ obj-$(CONFIG_PPC_COPRO_BASE)	+= copro_fault.o
  obj-$(CONFIG_SPAPR_TCE_IOMMU)	+= mmu_context_iommu.o
  obj-$(CONFIG_PPC_PTDUMP)	+= ptdump/
  obj-$(CONFIG_PPC_MEM_KEYS)	+= pkeys.o
+obj-$(CONFIG_KASAN)		+= kasan/
arch/powerpc/mm/kasan/Makefile new file +5 -0
new file mode 100644
index 000000000000..6577897673dd
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: GPL-2.0
+
+KASAN_SANITIZE := n
+
+obj-$(CONFIG_PPC32)           += kasan_init_32.o
arch/powerpc/mm/kasan/kasan_init_32.c new file +147 -0
new file mode 100644
index 000000000000..495c908d6ee6
@@ -0,0 +1,147 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#define DISABLE_BRANCH_PROFILING
+
+#include <linux/kasan.h>
+#include <linux/printk.h>
+#include <linux/memblock.h>
+#include <linux/sched/task.h>
+#include <linux/vmalloc.h>
+#include <asm/pgalloc.h>
+
+void __init kasan_early_init(void)
+{
+	unsigned long addr = KASAN_SHADOW_START;
+	unsigned long end = KASAN_SHADOW_END;
+	unsigned long next;
+	pmd_t *pmd = pmd_offset(pud_offset(pgd_offset_k(addr), addr), addr);
+	int i;
+	phys_addr_t pa = __pa(kasan_early_shadow_page);
+
+	BUILD_BUG_ON(KASAN_SHADOW_START & ~PGDIR_MASK);
+
+	if (early_mmu_has_feature(MMU_FTR_HPTE_TABLE))
+		panic("KASAN not supported with Hash MMU\n");
+
+	for (i = 0; i < PTRS_PER_PTE; i++)
+		__set_pte_at(&init_mm, (unsigned long)kasan_early_shadow_page,
+			     kasan_early_shadow_pte + i,
+			     pfn_pte(PHYS_PFN(pa), PAGE_KERNEL), 0);
+
+	do {
+		next = pgd_addr_end(addr, end);
+		pmd_populate_kernel(&init_mm, pmd, kasan_early_shadow_pte);
+	} while (pmd++, addr = next, addr != end);
+}
+
+static void __ref *kasan_get_one_page(void)
+{
+	if (slab_is_available())
+		return (void *)__get_free_page(GFP_KERNEL|__GFP_ZERO);
+
+	return memblock_alloc(PAGE_SIZE, PAGE_SIZE);
+}
+
+static int __ref kasan_init_region(void *start, size_t size)
+{
+	void *end = start + size;
+	unsigned long k_start, k_end, k_cur, k_next;
+	pmd_t *pmd;
+	void *block = NULL;
+
+	if (start >= end)
+		return 0;
+
+	k_start = (unsigned long)kasan_mem_to_shadow(start);
+	k_end = (unsigned long)kasan_mem_to_shadow(end);
+	pmd = pmd_offset(pud_offset(pgd_offset_k(k_start), k_start), k_start);
+
+	for (k_cur = k_start; k_cur != k_end; k_cur = k_next, pmd++) {
+		k_next = pgd_addr_end(k_cur, k_end);
+		if ((void *)pmd_page_vaddr(*pmd) == kasan_early_shadow_pte) {
+			pte_t *new = pte_alloc_one_kernel(&init_mm);
+
+			if (!new)
+				return -ENOMEM;
+			memcpy(new, kasan_early_shadow_pte, PTE_TABLE_SIZE);
+			pmd_populate_kernel(&init_mm, pmd, new);
+		}
+	};
+
+	if (!slab_is_available())
+		block = memblock_alloc(k_end - k_start, PAGE_SIZE);
+
+	for (k_cur = k_start; k_cur < k_end; k_cur += PAGE_SIZE) {
+		void *va = block ? block + k_cur - k_start :
+				   kasan_get_one_page();
+		pte_t pte = pfn_pte(PHYS_PFN(__pa(va)), PAGE_KERNEL);
+
+		if (!va)
+			return -ENOMEM;
+
+		pmd = pmd_offset(pud_offset(pgd_offset_k(k_cur), k_cur), k_cur);
+		pte_update(pte_offset_kernel(pmd, k_cur), ~0, pte_val(pte));
+	}
+	flush_tlb_kernel_range(k_start, k_end);
+	return 0;
+}
+
+static void __init kasan_remap_early_shadow_ro(void)
+{
+	unsigned long k_cur;
+	phys_addr_t pa = __pa(kasan_early_shadow_page);
+	int i;
+
+	for (i = 0; i < PTRS_PER_PTE; i++)
+		ptep_set_wrprotect(&init_mm, 0, kasan_early_shadow_pte + i);
+
+	for (k_cur = PAGE_OFFSET & PAGE_MASK; k_cur; k_cur += PAGE_SIZE) {
+		pmd_t *pmd = pmd_offset(pud_offset(pgd_offset_k(k_cur), k_cur), k_cur);
+		pte_t *ptep = pte_offset_kernel(pmd, k_cur);
+
+		if ((void *)pmd_page_vaddr(*pmd) == kasan_early_shadow_pte)
+			continue;
+		if ((pte_val(*ptep) & PAGE_MASK) != pa)
+			continue;
+
+		ptep_set_wrprotect(&init_mm, k_cur, ptep);
+	}
+	flush_tlb_mm(&init_mm);
+}
+
+void __init kasan_init(void)
+{
+	struct memblock_region *reg;
+
+	for_each_memblock(memory, reg) {
+		int ret = kasan_init_region(__va(reg->base), reg->size);
+
+		if (ret)
+			panic("kasan: kasan_init_region() failed");
+	}
+
+	kasan_remap_early_shadow_ro();
+
+	clear_page(kasan_early_shadow_page);
+
+	/* At this point kasan is fully initialized. Enable error messages */
+	init_task.kasan_depth = 0;
+	pr_info("KASAN init done\n");
+}
+
+#ifdef CONFIG_MODULES
+void *module_alloc(unsigned long size)
+{
+	void *base = vmalloc_exec(size);
+
+	if (!base)
+		return NULL;
+
+	if (!kasan_init_region(base, size))
+		return base;
+
+	vfree(base);
+
+	return NULL;
+}
+#endif
arch/powerpc/mm/mem.c +4 -0
index 81f251fc4169..1bb055775e60 100644
@@ -336,6 +336,10 @@ void __init mem_init(void)
  	pr_info("  * 0x%08lx..0x%08lx  : highmem PTEs\n",
  		PKMAP_BASE, PKMAP_ADDR(LAST_PKMAP));
  #endif /* CONFIG_HIGHMEM */
+#ifdef CONFIG_KASAN
+	pr_info("  * 0x%08lx..0x%08lx  : kasan shadow mem\n",
+		KASAN_SHADOW_START, KASAN_SHADOW_END);
+#endif
  #ifdef CONFIG_NOT_COHERENT_CACHE
  	pr_info("  * 0x%08lx..0x%08lx  : consistent mem\n",
  		IOREMAP_TOP, IOREMAP_TOP + CONFIG_CONSISTENT_SIZE);
arch/powerpc/mm/ptdump/dump_linuxpagetables.c +8 -0
index b0da447197d4..4a03910974e2 100644
@@ -94,6 +94,10 @@ static struct addr_marker address_markers[] = {
  	{ 0,	"Consistent mem start" },
  	{ 0,	"Consistent mem end" },
  #endif
+#ifdef CONFIG_KASAN
+	{ 0,	"kasan shadow mem start" },
+	{ 0,	"kasan shadow mem end" },
+#endif
  #ifdef CONFIG_HIGHMEM
  	{ 0,	"Highmem PTEs start" },
  	{ 0,	"Highmem PTEs end" },
@@ -316,6 +320,10 @@ static void populate_markers(void)
  	address_markers[i++].start_address = IOREMAP_TOP +
  					     CONFIG_CONSISTENT_SIZE;
  #endif
+#ifdef CONFIG_KASAN
+	address_markers[i++].start_address = KASAN_SHADOW_START;
+	address_markers[i++].start_address = KASAN_SHADOW_END;
+#endif
  #ifdef CONFIG_HIGHMEM
  	address_markers[i++].start_address = PKMAP_BASE;
  	address_markers[i++].start_address = PKMAP_ADDR(LAST_PKMAP);
arch/powerpc/purgatory/Makefile +3 -0
index 4314ba5baf43..7c6d8b14f440 100644
@@ -1,4 +1,7 @@
  # SPDX-License-Identifier: GPL-2.0
+
+KASAN_SANITIZE := n
+
  targets += trampoline.o purgatory.ro kexec-purgatory.c
 
  LDFLAGS_purgatory.ro := -e purgatory_start -r --no-undefined
arch/powerpc/xmon/Makefile +1 -0
index 878f9c1d3615..064f7062c0a3 100644
@@ -6,6 +6,7 @@ subdir-ccflags-y := $(call cc-disable-warning, builtin-requires-header)
 
  GCOV_PROFILE := n
  UBSAN_SANITIZE := n
+KASAN_SANITIZE := n
 
  # Disable ftrace for the entire directory
  ORIG_CFLAGS := $(KBUILD_CFLAGS)
#6 lore raw

PATCH v6 5/6 kasan: allow architectures to provide an outline readiness check

In reply to Christophe Leroy

From: Daniel Axtens <[email protected]>

In powerpc (as I understand it), we spend a lot of time in boot running in real mode before MMU paging is initalised. During this time we call a lot of generic code, including printk(). If we try to access the shadow region during this time, things fail.

My attempts to move early init before the first printk have not been successful. (Both previous RFCs for ppc64 - by 2 different people - have needed this trick too!)

So, allow architectures to define a kasan_arch_is_ready() hook that bails out of check_memory_region_inline() unless the arch has done all of the init.

Cc Aneesh Kumar K.V <[email protected]>
[check_return_arch_not_ready() ==> static inline kasan_arch_is_ready()]
---
 include/linux/kasan.h | 4 ++++
 mm/kasan/generic.c    | 3 +++
 2 files changed, 7 insertions(+)
include/linux/kasan.h +4 -0
index b40ea104dd36..b91c40af9f31 100644
@@ -14,6 +14,10 @@ struct task_struct;
 #include <asm/kasan.h>
 #include <asm/pgtable.h>
 
+#ifndef kasan_arch_is_ready
+static inline bool kasan_arch_is_ready(void)	{ return true; }
+#endif
+
 extern unsigned char kasan_early_shadow_page[PAGE_SIZE];
 extern pte_t kasan_early_shadow_pte[PTRS_PER_PTE];
 extern pmd_t kasan_early_shadow_pmd[PTRS_PER_PMD];
mm/kasan/generic.c +3 -0
index ccb6207276e3..696c2f5b902b 100644
@@ -170,6 +170,9 @@ static __always_inline void check_memory_region_inline(unsigned long addr,
 						size_t size, bool write,
 						unsigned long ret_ip)
 {
+	if (!kasan_arch_is_ready())
+		return;
+
 	if (unlikely(size == 0))
 		return;
 
Signature · 1 line
2.13.3
#7 lore raw

PATCH v6 6/6 powerpc/32: enable CONFIG_KASAN for book3s hash

In reply to Christophe Leroy

The challenge with book3s/32 is that the hash table management has to be set up before being able to use KASAN.

This patch adds a kasan_arch_is_ready() helper to defer the activation of KASAN until paging is ready.

This limits KASAN to KASAN_MINIMAL mode. The downside of it is that the 603, which doesn't use hash table, also gets downgraded to KASAN_MINIMAL because this is no way to activate full support dynamically because that's compiled-in.

---
 arch/powerpc/Makefile                 |  2 ++
 arch/powerpc/include/asm/kasan.h      | 13 +++++++++++++
 arch/powerpc/mm/kasan/kasan_init_32.c | 27 +++++++++++++++++++++++++--
 3 files changed, 40 insertions(+), 2 deletions(-)
arch/powerpc/Makefile +2 -0
index f0738099e31e..06d085558d21 100644
@@ -428,11 +428,13 @@ endif
 endif
 
 ifdef CONFIG_KASAN
+ifndef CONFIG_PPC_BOOK3S_32
 prepare: kasan_prepare
 
 kasan_prepare: prepare0
        $(eval KASAN_SHADOW_OFFSET = $(shell awk '{if ($$2 == "KASAN_SHADOW_OFFSET") print $$3;}' include/generated/asm-offsets.h))
 endif
+endif
 
 # Check toolchain versions:
 # - gcc-4.6 is the minimum kernel-wide version so nothing required.
arch/powerpc/include/asm/kasan.h +13 -0
index 0bc9148f5d87..97b5ccf0702f 100644
@@ -16,6 +16,7 @@
 
 #include <asm/page.h>
 #include <asm/pgtable-types.h>
+#include <linux/jump_label.h>
 
 #define KASAN_SHADOW_SCALE_SHIFT	3
 
@@ -34,5 +35,17 @@
 void kasan_early_init(void);
 void kasan_init(void);
 
+extern struct static_key_false powerpc_kasan_enabled_key;
+
+static inline bool kasan_arch_is_ready(void)
+{
+	if (!IS_ENABLED(CONFIG_BOOK3S_32))
+		return true;
+	if (static_branch_likely(&powerpc_kasan_enabled_key))
+		return true;
+	return false;
+}
+#define kasan_arch_is_ready kasan_arch_is_ready
+
 #endif /* __ASSEMBLY */
 #endif
arch/powerpc/mm/kasan/kasan_init_32.c +25 -2
index 495c908d6ee6..f24f8f56d450 100644
@@ -9,6 +9,9 @@
 #include <linux/vmalloc.h>
 #include <asm/pgalloc.h>
 
+/* Used by BOOK3S_32 only */
+DEFINE_STATIC_KEY_FALSE(powerpc_kasan_enabled_key);
+
 void __init kasan_early_init(void)
 {
 	unsigned long addr = KASAN_SHADOW_START;
@@ -21,7 +24,7 @@ void __init kasan_early_init(void)
 	BUILD_BUG_ON(KASAN_SHADOW_START & ~PGDIR_MASK);
 
 	if (early_mmu_has_feature(MMU_FTR_HPTE_TABLE))
-		panic("KASAN not supported with Hash MMU\n");
+		return;
 
 	for (i = 0; i < PTRS_PER_PTE; i++)
 		__set_pte_at(&init_mm, (unsigned long)kasan_early_shadow_page,
@@ -32,6 +35,22 @@ void __init kasan_early_init(void)
 		next = pgd_addr_end(addr, end);
 		pmd_populate_kernel(&init_mm, pmd, kasan_early_shadow_pte);
 	} while (pmd++, addr = next, addr != end);
+
+	if (IS_ENABLED(CONFIG_PPC_BOOK3S_32)) {
+		jump_label_init();
+		static_branch_enable(&powerpc_kasan_enabled_key);
+	}
+}
+
+static void __init kasan_late_init(void)
+{
+	unsigned long addr;
+	phys_addr_t pa = __pa(kasan_early_shadow_page);
+
+	for (addr = KASAN_SHADOW_START; addr < KASAN_SHADOW_END; addr += PAGE_SIZE)
+		map_kernel_page(addr, pa, PAGE_KERNEL_RO);
+
+	static_branch_enable(&powerpc_kasan_enabled_key);
 }
 
 static void __ref *kasan_get_one_page(void)
@@ -113,6 +132,9 @@ void __init kasan_init(void)
 {
 	struct memblock_region *reg;
 
+	if (early_mmu_has_feature(MMU_FTR_HPTE_TABLE))
+		kasan_late_init();
+
 	for_each_memblock(memory, reg) {
 		int ret = kasan_init_region(__va(reg->base), reg->size);
 
@@ -120,7 +142,8 @@ void __init kasan_init(void)
 			panic("kasan: kasan_init_region() failed");
 	}
 
-	kasan_remap_early_shadow_ro();
+	if (!early_mmu_has_feature(MMU_FTR_HPTE_TABLE))
+		kasan_remap_early_shadow_ro();
 
 	clear_page(kasan_early_shadow_page);
 
Signature · 1 line
2.13.3