Re: [RFC PATCH V1 7/8] powerpc/mm: kasan: Add kasan support for ppc64

2015-08-18 Thread Andrey Ryabinin
2015-08-18 8:36 GMT+03:00 Aneesh Kumar K.V aneesh.ku...@linux.vnet.ibm.com:
 Andrey Ryabinin ryabinin@gmail.com writes:

 2015-08-17 15:13 GMT+03:00 Andrey Ryabinin ryabinin@gmail.com:

 Did you disable stack instrumentation (in scripts/Makefile.kasa),
 or you version of gcc doesn't support it (e.g. like 4.9.x on x86) ?

 Because this can't work with stack instrumentation as you don't have shadow 
 for stack in early code.

 But this should be doable, as I think. All you need is to setup shadow for 
 init task's
 stack before executing any instrumented function.

 And you also need to define CONFIG_KASAN_SHADOW_OFFSET, so it will be
 passed to GCC
 via -fasan-shadow-offset= option.

 I am using KASAN minimal config. Hence this was not needed. Do we need
 to pass that option for outline instrumentation ? If not it would be a
 good idea to split that out and make it depend on KASAN_INLINE


We need to pass this for stack instrumentation too.

 -aneesh

___
Linuxppc-dev mailing list
Linuxppc-dev@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/linuxppc-dev

Re: [RFC PATCH V1 7/8] powerpc/mm: kasan: Add kasan support for ppc64

2015-08-17 Thread Andrey Ryabinin
On 08/17/2015 09:36 AM, Aneesh Kumar K.V wrote:
 We use the region with region ID 0xe as the kasan shadow region. Since
 we use hash page table, we can't have the early zero page based shadow
 region support. Hence we disable kasan in the early code and runtime
 enable this. We could imporve the condition using static keys. (but
 that is for a later patch). We also can't support inline instrumentation
 because our kernel mapping doesn't give us a large enough free window
 to map the entire range. For VMALLOC and VMEMMAP region we just
 return a zero page instead of having a translation bolted into the
 htab. This simplifies handling VMALLOC and VMEMAP area. Kasan is not
 tracking both the region as of now
 
 Signed-off-by: Aneesh Kumar K.V aneesh.ku...@linux.vnet.ibm.com
 ---
  arch/powerpc/include/asm/kasan.h | 74 
 
  arch/powerpc/include/asm/pgtable-ppc64.h |  1 +
  arch/powerpc/include/asm/ppc_asm.h   | 10 +
  arch/powerpc/include/asm/string.h| 13 ++
  arch/powerpc/kernel/Makefile |  1 +
  arch/powerpc/kernel/prom_init_check.sh   |  2 +-
  arch/powerpc/kernel/setup_64.c   |  3 ++
  arch/powerpc/lib/mem_64.S|  6 ++-
  arch/powerpc/lib/memcpy_64.S |  3 +-
  arch/powerpc/lib/ppc_ksyms.c | 10 +
  arch/powerpc/mm/Makefile |  3 ++
  arch/powerpc/mm/kasan_init.c | 44 +++
  arch/powerpc/mm/slb_low.S|  4 ++
  arch/powerpc/platforms/Kconfig.cputype   |  1 +
  14 files changed, 171 insertions(+), 4 deletions(-)
  create mode 100644 arch/powerpc/include/asm/kasan.h
  create mode 100644 arch/powerpc/mm/kasan_init.c
 

Did you disable stack instrumentation (in scripts/Makefile.kasa),
or you version of gcc doesn't support it (e.g. like 4.9.x on x86) ?

Because this can't work with stack instrumentation as you don't have shadow for 
stack in early code.

But this should be doable, as I think. All you need is to setup shadow for init 
task's
stack before executing any instrumented function. 
___
Linuxppc-dev mailing list
Linuxppc-dev@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/linuxppc-dev

Re: [RFC PATCH V1 7/8] powerpc/mm: kasan: Add kasan support for ppc64

2015-08-17 Thread Andrey Ryabinin
2015-08-17 15:13 GMT+03:00 Andrey Ryabinin ryabinin@gmail.com:

 Did you disable stack instrumentation (in scripts/Makefile.kasa),
 or you version of gcc doesn't support it (e.g. like 4.9.x on x86) ?

 Because this can't work with stack instrumentation as you don't have shadow 
 for stack in early code.

 But this should be doable, as I think. All you need is to setup shadow for 
 init task's
 stack before executing any instrumented function.

And you also need to define CONFIG_KASAN_SHADOW_OFFSET, so it will be
passed to GCC
via -fasan-shadow-offset= option.
___
Linuxppc-dev mailing list
Linuxppc-dev@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/linuxppc-dev

Re: [RFC PATCH V1 7/8] powerpc/mm: kasan: Add kasan support for ppc64

2015-08-17 Thread Aneesh Kumar K.V
Andrey Ryabinin ryabinin@gmail.com writes:

 On 08/17/2015 09:36 AM, Aneesh Kumar K.V wrote:
 We use the region with region ID 0xe as the kasan shadow region. Since
 we use hash page table, we can't have the early zero page based shadow
 region support. Hence we disable kasan in the early code and runtime
 enable this. We could imporve the condition using static keys. (but
 that is for a later patch). We also can't support inline instrumentation
 because our kernel mapping doesn't give us a large enough free window
 to map the entire range. For VMALLOC and VMEMMAP region we just
 return a zero page instead of having a translation bolted into the
 htab. This simplifies handling VMALLOC and VMEMAP area. Kasan is not
 tracking both the region as of now
 
 Signed-off-by: Aneesh Kumar K.V aneesh.ku...@linux.vnet.ibm.com
 ---
  arch/powerpc/include/asm/kasan.h | 74 
 
  arch/powerpc/include/asm/pgtable-ppc64.h |  1 +
  arch/powerpc/include/asm/ppc_asm.h   | 10 +
  arch/powerpc/include/asm/string.h| 13 ++
  arch/powerpc/kernel/Makefile |  1 +
  arch/powerpc/kernel/prom_init_check.sh   |  2 +-
  arch/powerpc/kernel/setup_64.c   |  3 ++
  arch/powerpc/lib/mem_64.S|  6 ++-
  arch/powerpc/lib/memcpy_64.S |  3 +-
  arch/powerpc/lib/ppc_ksyms.c | 10 +
  arch/powerpc/mm/Makefile |  3 ++
  arch/powerpc/mm/kasan_init.c | 44 +++
  arch/powerpc/mm/slb_low.S|  4 ++
  arch/powerpc/platforms/Kconfig.cputype   |  1 +
  14 files changed, 171 insertions(+), 4 deletions(-)
  create mode 100644 arch/powerpc/include/asm/kasan.h
  create mode 100644 arch/powerpc/mm/kasan_init.c
 

 Did you disable stack instrumentation (in scripts/Makefile.kasa),
 or you version of gcc doesn't support it (e.g. like 4.9.x on x86) ?

I guess the later, because i do see this during compile

scripts/Makefile.kasan:23: CONFIG_KASAN: compiler does not support all options. 
Trying minimal configuration
scripts/kconfig/conf  --silentoldconfig Kconfig


 Because this can't work with stack instrumentation as you don't have shadow 
 for stack in early code.

 But this should be doable, as I think. All you need is to setup shadow for 
 init task's
 stack before executing any instrumented function. 

I still need to look at stack and global support. So that is not yet
there.

-aneesh

___
Linuxppc-dev mailing list
Linuxppc-dev@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/linuxppc-dev

Re: [RFC PATCH V1 7/8] powerpc/mm: kasan: Add kasan support for ppc64

2015-08-17 Thread Aneesh Kumar K.V
Andrey Ryabinin ryabinin@gmail.com writes:

 2015-08-17 15:13 GMT+03:00 Andrey Ryabinin ryabinin@gmail.com:

 Did you disable stack instrumentation (in scripts/Makefile.kasa),
 or you version of gcc doesn't support it (e.g. like 4.9.x on x86) ?

 Because this can't work with stack instrumentation as you don't have shadow 
 for stack in early code.

 But this should be doable, as I think. All you need is to setup shadow for 
 init task's
 stack before executing any instrumented function.

 And you also need to define CONFIG_KASAN_SHADOW_OFFSET, so it will be
 passed to GCC
 via -fasan-shadow-offset= option.

I am using KASAN minimal config. Hence this was not needed. Do we need
to pass that option for outline instrumentation ? If not it would be a
good idea to split that out and make it depend on KASAN_INLINE

-aneesh

___
Linuxppc-dev mailing list
Linuxppc-dev@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/linuxppc-dev

[RFC PATCH V1 7/8] powerpc/mm: kasan: Add kasan support for ppc64

2015-08-17 Thread Aneesh Kumar K.V
We use the region with region ID 0xe as the kasan shadow region. Since
we use hash page table, we can't have the early zero page based shadow
region support. Hence we disable kasan in the early code and runtime
enable this. We could imporve the condition using static keys. (but
that is for a later patch). We also can't support inline instrumentation
because our kernel mapping doesn't give us a large enough free window
to map the entire range. For VMALLOC and VMEMMAP region we just
return a zero page instead of having a translation bolted into the
htab. This simplifies handling VMALLOC and VMEMAP area. Kasan is not
tracking both the region as of now

Signed-off-by: Aneesh Kumar K.V aneesh.ku...@linux.vnet.ibm.com
---
 arch/powerpc/include/asm/kasan.h | 74 
 arch/powerpc/include/asm/pgtable-ppc64.h |  1 +
 arch/powerpc/include/asm/ppc_asm.h   | 10 +
 arch/powerpc/include/asm/string.h| 13 ++
 arch/powerpc/kernel/Makefile |  1 +
 arch/powerpc/kernel/prom_init_check.sh   |  2 +-
 arch/powerpc/kernel/setup_64.c   |  3 ++
 arch/powerpc/lib/mem_64.S|  6 ++-
 arch/powerpc/lib/memcpy_64.S |  3 +-
 arch/powerpc/lib/ppc_ksyms.c | 10 +
 arch/powerpc/mm/Makefile |  3 ++
 arch/powerpc/mm/kasan_init.c | 44 +++
 arch/powerpc/mm/slb_low.S|  4 ++
 arch/powerpc/platforms/Kconfig.cputype   |  1 +
 14 files changed, 171 insertions(+), 4 deletions(-)
 create mode 100644 arch/powerpc/include/asm/kasan.h
 create mode 100644 arch/powerpc/mm/kasan_init.c

diff --git a/arch/powerpc/include/asm/kasan.h b/arch/powerpc/include/asm/kasan.h
new file mode 100644
index ..51e76e698bb9
--- /dev/null
+++ b/arch/powerpc/include/asm/kasan.h
@@ -0,0 +1,74 @@
+#ifndef __ASM_KASAN_H
+#define __ASM_KASAN_H
+
+#ifndef __ASSEMBLY__
+
+#ifdef CONFIG_KASAN
+/*
+ * KASAN_SHADOW_START: We use a new region for kasan mapping
+ * KASAN_SHADOW_END: KASAN_SHADOW_START + 1/8 of kernel virtual addresses.
+ */
+#define KASAN_SHADOW_START  (KASAN_REGION_ID  REGION_SHIFT)
+#define KASAN_SHADOW_END(KASAN_SHADOW_START + (1UL  (PGTABLE_RANGE - 
3)))
+/*
+ * This value is used to map an address to the corresponding shadow
+ * address by the following formula:
+ * shadow_addr = (address  3) + KASAN_SHADOW_OFFSET;
+ *
+ * This applies to the linear mapping.
+ * Hence 0xc000 - 0xe000
+ * We use an internal zero page as the shadow address for vmall and vmemmap
+ * region, since we don't track both of them now.
+ *
+ */
+#define KASAN_SHADOW_KERNEL_OFFSET ((KASAN_REGION_ID  REGION_SHIFT) - \
+(KERNEL_REGION_ID  (REGION_SHIFT - 
3)))
+
+extern unsigned char kasan_zero_page[PAGE_SIZE];
+#define kasan_mem_to_shadow kasan_mem_to_shadow
+static inline void *kasan_mem_to_shadow(const void *addr)
+{
+   unsigned long offset = 0;
+
+   switch (REGION_ID(addr)) {
+   case KERNEL_REGION_ID:
+   offset = KASAN_SHADOW_KERNEL_OFFSET;
+   break;
+   default:
+   return (void *)kasan_zero_page;
+   }
+   return (void *)((unsigned long)addr  KASAN_SHADOW_SCALE_SHIFT)
+   + offset;
+}
+
+#define kasan_shadow_to_mem kasan_shadow_to_mem
+static inline void *kasan_shadow_to_mem(const void *shadow_addr)
+{
+   unsigned long offset = 0;
+
+   switch (REGION_ID(shadow_addr)) {
+   case KASAN_REGION_ID:
+   offset = KASAN_SHADOW_KERNEL_OFFSET;
+   break;
+   default:
+   pr_err(Shadow memory whose origin not found %p\n, 
shadow_addr);
+   BUG();
+   }
+   return (void *)(((unsigned long)shadow_addr - offset)
+KASAN_SHADOW_SCALE_SHIFT);
+}
+
+#define kasan_enabled kasan_enabled
+extern bool __kasan_enabled;
+static inline bool kasan_enabled(void)
+{
+   return __kasan_enabled;
+}
+
+void kasan_init(void);
+#else
+static inline void kasan_init(void) { }
+#endif
+
+#endif
+#endif
diff --git a/arch/powerpc/include/asm/pgtable-ppc64.h 
b/arch/powerpc/include/asm/pgtable-ppc64.h
index 3bb7488bd24b..369ce5442aa6 100644
--- a/arch/powerpc/include/asm/pgtable-ppc64.h
+++ b/arch/powerpc/include/asm/pgtable-ppc64.h
@@ -80,6 +80,7 @@
 #define KERNEL_REGION_ID   (REGION_ID(PAGE_OFFSET))
 #define VMEMMAP_REGION_ID  (0xfUL) /* Server only */
 #define USER_REGION_ID (0UL)
+#define KASAN_REGION_ID(0xeUL) /* Server only */
 
 /*
  * Defines the address of the vmemap area, in its own region on
diff --git a/arch/powerpc/include/asm/ppc_asm.h 
b/arch/powerpc/include/asm/ppc_asm.h
index dd0fc18d8103..e75ae67e804e 100644
--- a/arch/powerpc/include/asm/ppc_asm.h
+++ b/arch/powerpc/include/asm/ppc_asm.h
@@ -226,6 +226,11 @@ name:
 
 #define DOTSYM(a)  a
 
+#define KASAN_OVERRIDE(x, y) \
+   .weak x; \
+   .set x, y
+
+