Skip to contentSkip to conversation

KASan ppc64 support

PATCH In review Aneesh Kumar K.V 3 participants 29 messages Aug 26, 2015 – Jul 6, 2018

Subsystems (8)
lore
Share & tools
lore
#0 lore raw

v2 cover KASan ppc64 support

Hi,

This patchset implements kernel address sanitizer for ppc64. Since ppc64 virtual address range is divided into different regions, we can't have one contigous area for the kasan shadow range. Hence we don't support the INLINE kasan instrumentation. With Outline instrumentation, we override the shadow_to_mem and mem_to_shadow callbacks, so that we map only the kernel linear range (ie, region with ID 0xc). For region with ID 0xd and 0xf (vmalloc and vmemmap ) we return the address of the zero page. This works because kasan doesn't track both vmemmap and vmalloc address.

Known issues:
* Kasan is not yet enabled for arch/powerpc/kvm
* kexec hang
* outline stack and global support

Once we fix the kexec hang, we can look at merging ppc64 patch. IMHO kasan changes can be reviewed/merged earlier

Aneesh Kumar K.V (10):
powerpc/mm: Add virt_to_pfn and use this instead of opencoding
kasan: MODULE_VADDR is not available on all archs
kasan: Rename kasan_enabled to kasan_report_enabled
kasan: Don't use kasan shadow pointer in generic functions
kasan: Enable arch to hook into kasan callbacks.
kasan: Allow arch to overrride kasan shadow offsets
kasan: Make INLINE KASan support arch selectable
kasan: Update feature support file
kasan: Prevent deadlock in kasan reporting powerpc/mm: kasan: Add kasan support for ppc64

 .../debug/KASAN/KASAN_INLINE/arch-support.txt      | 40 ++++++++++++
 .../KASAN/{ => KASAN_OUTLINE}/arch-support.txt     |  0
 arch/powerpc/include/asm/kasan.h                   | 74 ++++++++++++++++++++++
 arch/powerpc/include/asm/page.h                    |  5 +-
 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                       |  5 ++
 arch/powerpc/kernel/prom_init_check.sh             |  2 +-
 arch/powerpc/kernel/setup_64.c                     |  3 +
 arch/powerpc/kvm/Makefile                          |  1 +
 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                           |  7 ++
 arch/powerpc/mm/kasan_init.c                       | 44 +++++++++++++
 arch/powerpc/mm/slb_low.S                          |  4 ++
 arch/powerpc/platforms/Kconfig.cputype             |  1 +
 arch/x86/Kconfig                                   |  1 +
 include/linux/kasan.h                              |  3 +
 lib/Kconfig.kasan                                  |  2 +
 mm/kasan/kasan.c                                   |  9 +++
 mm/kasan/kasan.h                                   | 20 +++++-
 mm/kasan/report.c                                  | 29 ++++++---
 scripts/Makefile.kasan                             | 28 ++++----
 25 files changed, 290 insertions(+), 31 deletions(-)
 create mode 100644 Documentation/features/debug/KASAN/KASAN_INLINE/arch-support.txt
 rename Documentation/features/debug/KASAN/{ => KASAN_OUTLINE}/arch-support.txt (100%)
 create mode 100644 arch/powerpc/include/asm/kasan.h
 create mode 100644 arch/powerpc/mm/kasan_init.c
Signature · 1 line
2.5.0
#1 lore raw

PATCH v2 1/10 powerpc/mm: Add virt_to_pfn and use this instead of opencoding

This add helper virt_to_pfn and remove the opencoded usage of the same.

---
 arch/powerpc/include/asm/page.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/powerpc/include/asm/page.h +3 -2
index 71294a6e976e..168ca67e39b3 100644
@@ -127,9 +127,10 @@ extern long long virt_phys_offset;
 #define pfn_valid(pfn)		((pfn) >= ARCH_PFN_OFFSET && (pfn) < max_mapnr)
 #endif
 
-#define virt_to_page(kaddr)	pfn_to_page(__pa(kaddr) >> PAGE_SHIFT)
+#define virt_to_pfn(kaddr)	(__pa(kaddr) >> PAGE_SHIFT)
+#define virt_to_page(kaddr)	pfn_to_page(virt_to_pfn(kaddr))
 #define pfn_to_kaddr(pfn)	__va((pfn) << PAGE_SHIFT)
-#define virt_addr_valid(kaddr)	pfn_valid(__pa(kaddr) >> PAGE_SHIFT)
+#define virt_addr_valid(kaddr)	pfn_valid(virt_to_pfn(kaddr))
 
 /*
  * On Book-E parts we need __va to parse the device tree and we can't
Signature · 1 line
2.5.0
#2 lore raw

PATCH v2 2/10 kasan: MODULE_VADDR is not available on all archs

In reply to Aneesh Kumar K.V

Conditionalize the check using #ifdef

---
 mm/kasan/report.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
mm/kasan/report.c +8 -3
index e07c94fbd0ac..71ce7548d914 100644
@@ -85,9 +85,14 @@ static void print_error_description(struct kasan_access_info *info)
 
 static inline bool kernel_or_module_addr(const void *addr)
 {
-	return (addr >= (void *)_stext && addr < (void *)_end)
-		|| (addr >= (void *)MODULES_VADDR
-			&& addr < (void *)MODULES_END);
+	if (addr >= (void *)_stext && addr < (void *)_end)
+		return true;
+#if defined(CONFIG_MODULES) && defined(MODULES_VADDR)
+	if (addr >= (void *)MODULES_VADDR
+			&& addr < (void *)MODULES_END)
+		return true;
+#endif
+	return false;
 }
 
 static inline bool init_task_stack_addr(const void *addr)
Signature · 1 line
2.5.0
maintainer
#3 lore raw

Re: [PATCH V2 02/10] kasan: MODULE_VADDR is not available on all archs

2015-08-26 11:26 GMT+03:00 Aneesh Kumar K.V <[email protected]>:

Quoted 15 lines

Conditionalize the check using #ifdef

---
 mm/kasan/report.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
mm/kasan/report.c +0 -0
index e07c94fbd0ac..71ce7548d914 100644
@@ -85,9 +85,14 @@ static void print_error_description(struct kasan_access_info *info)
 
 static inline bool kernel_or_module_addr(const void *addr)
 {

mm/kasan/report.c static void print_error_description(struct kasan_access_info *info) · line 94

Quoted 10 lines
mm/kasan/report.c +7 -3
-       return (addr >= (void *)_stext && addr < (void *)_end)
-               || (addr >= (void *)MODULES_VADDR
-                       && addr < (void *)MODULES_END);
+       if (addr >= (void *)_stext && addr < (void *)_end)
+               return true;
+#if defined(CONFIG_MODULES) && defined(MODULES_VADDR)
+       if (addr >= (void *)MODULES_VADDR
+                       && addr < (void *)MODULES_END)
+               return true;
+#endif

I don't think that this is correct change.
On ppc64 modules are in VMALLOC, so you should check for this. Yes, we don't handle VMALLOC now, but we will at some point.

So I think we should use is_module_address() here.
It will be slower, but we don't care about performance in error reporting route.

#4 lore raw

Re: [PATCH V2 02/10] kasan: MODULE_VADDR is not available on all archs

Quoted 31 lines · Andrey Ryabinin

2015-08-26 11:26 GMT+03:00 Aneesh Kumar K.V <[email protected]>:

Quoted 15 lines

Conditionalize the check using #ifdef

---
 mm/kasan/report.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
mm/kasan/report.c +0 -0
index e07c94fbd0ac..71ce7548d914 100644
@@ -85,9 +85,14 @@ static void print_error_description(struct kasan_access_info *info)
 
 static inline bool kernel_or_module_addr(const void *addr)
 {

mm/kasan/report.c static void print_error_description(struct kasan_access_info *info) · line 94

Quoted 10 lines
mm/kasan/report.c +7 -3
-       return (addr >= (void *)_stext && addr < (void *)_end)
-               || (addr >= (void *)MODULES_VADDR
-                       && addr < (void *)MODULES_END);
+       if (addr >= (void *)_stext && addr < (void *)_end)
+               return true;
+#if defined(CONFIG_MODULES) && defined(MODULES_VADDR)
+       if (addr >= (void *)MODULES_VADDR
+                       && addr < (void *)MODULES_END)
+               return true;
+#endif

I don't think that this is correct change.
On ppc64 modules are in VMALLOC, so you should check for this. Yes, we don't handle VMALLOC now, but we will at some point.

Quoted 2 lines

So I think we should use is_module_address() here.
It will be slower, but we don't care about performance in error reporting route.

Will fix in the next update.

-aneesh

#5 lore raw

PATCH v2 3/10 kasan: Rename kasan_enabled to kasan_report_enabled

In reply to Aneesh Kumar K.V

The function only disable/enable reporting. In the later patch we will be adding a kasan early enable/disable. Rename kasan_enabled to properly reflect its function.

---
 mm/kasan/kasan.h  | 2 +-
 mm/kasan/report.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
mm/kasan/kasan.h +1 -1
index c242adf6bc85..a6b46cc94907 100644
@@ -63,7 +63,7 @@ static inline const void *kasan_shadow_to_mem(const void *shadow_addr)
 		<< KASAN_SHADOW_SCALE_SHIFT);
 }
 
-static inline bool kasan_enabled(void)
+static inline bool kasan_report_enabled(void)
 {
 	return !current->kasan_depth;
 }
mm/kasan/report.c +1 -1
index 71ce7548d914..d19d01823a68 100644
@@ -225,7 +225,7 @@ void kasan_report(unsigned long addr, size_t size,
 {
 	struct kasan_access_info info;
 
-	if (likely(!kasan_enabled()))
+	if (likely(!kasan_report_enabled()))
 		return;
 
 	info.access_addr = (void *)addr;
Signature · 1 line
2.5.0
maintainer Reviewed-by on v2 3/10
#6 lore raw

Re: [PATCH V2 03/10] kasan: Rename kasan_enabled to kasan_report_enabled

2015-08-26 11:26 GMT+03:00 Aneesh Kumar K.V <[email protected]>:

Quoted 5 lines

The function only disable/enable reporting. In the later patch we will be adding a kasan early enable/disable. Rename kasan_enabled to properly reflect its function.

#7 lore raw

PATCH v2 4/10 kasan: Don't use kasan shadow pointer in generic functions

In reply to Aneesh Kumar K.V

We can't use generic functions like print_hex_dump to access kasan shadow region. This require us to setup another kasan shadow region for the address passed (kasan shadow address). Most architecture won't be able to do that. Hence remove dumping kasan shadow region dump. If we really want to do this we will have to have a kasan internal implemen tation of print_hex_dump for which we will disable address sanitizer operation.

---
 mm/kasan/report.c | 6 ------
 1 file changed, 6 deletions(-)
mm/kasan/report.c +0 -6
index d19d01823a68..79fbc5d14bd2 100644
@@ -170,12 +170,6 @@ static void print_shadow_for_address(const void *addr)
 		snprintf(buffer, sizeof(buffer),
 			(i == 0) ? ">%p: " : " %p: ", kaddr);
 
-		kasan_disable_current();
-		print_hex_dump(KERN_ERR, buffer,
-			DUMP_PREFIX_NONE, SHADOW_BYTES_PER_ROW, 1,
-			shadow_row, SHADOW_BYTES_PER_ROW, 0);
-		kasan_enable_current();
-
 		if (row_is_guilty(shadow_row, shadow))
 			pr_err("%*c\n",
 				shadow_pointer_offset(shadow_row, shadow),
Signature · 1 line
2.5.0
#8 lore raw

Re: [PATCH V2 04/10] kasan: Don't use kasan shadow pointer in generic functions

Missed to cherry-pick the updated version of this patch, before sending the series out.

commit aeb324e09d95c189eda4ce03790da94b535d1dfc

Author: Aneesh Kumar K.V <[email protected]>
Date:   Fri Aug 14 12:28:58 2015 +0530

kasan: Don't use kasan shadow pointer in generic functions

We can't use generic functions like print_hex_dump to access kasan shadow region. This require us to setup another kasan shadow region for the address passed (kasan shadow address). Most architecture won't be able to do that. Hence make a copy of the shadow region row and pass that to generic functions.

mm/kasan/report.c +8 -2
index d19d01823a68..60fdb0413f3b 100644
@@ -166,14 +166,20 @@ static void print_shadow_for_address(const void *addr)
 	for (i = -SHADOW_ROWS_AROUND_ADDR; i <= SHADOW_ROWS_AROUND_ADDR; i++) {
 		const void *kaddr = kasan_shadow_to_mem(shadow_row);
 		char buffer[4 + (BITS_PER_LONG/8)*2];
+		char shadow_buf[SHADOW_BYTES_PER_ROW];
 
 		snprintf(buffer, sizeof(buffer),
 			(i == 0) ? ">%p: " : " %p: ", kaddr);
-
+		/*
+		 * We should not pass a shadow pointer to generic
+		 * function, because generic functions may try to
+		 * kasan mapping for the passed address.
+		 */
+		memcpy(shadow_buf, shadow_row, SHADOW_BYTES_PER_ROW);
 		kasan_disable_current();
 		print_hex_dump(KERN_ERR, buffer,
 			DUMP_PREFIX_NONE, SHADOW_BYTES_PER_ROW, 1,
-			shadow_row, SHADOW_BYTES_PER_ROW, 0);
+			shadow_buf, SHADOW_BYTES_PER_ROW, 0);
 		kasan_enable_current();
 
 		if (row_is_guilty(shadow_row, shadow))
maintainer
#9 lore raw

Re: [PATCH V2 04/10] kasan: Don't use kasan shadow pointer in generic functions

2015-08-26 11:54 GMT+03:00 Aneesh Kumar K.V <[email protected]>:

Quoted 25 lines

Missed to cherry-pick the updated version of this patch, before sending the series out.

commit aeb324e09d95c189eda4ce03790da94b535d1dfc

Author: Aneesh Kumar K.V <[email protected]>
Date:   Fri Aug 14 12:28:58 2015 +0530

kasan: Don't use kasan shadow pointer in generic functions

We can't use generic functions like print_hex_dump to access kasan shadow region. This require us to setup another kasan shadow region for the address passed (kasan shadow address). Most architecture won't be able to do that. Hence make a copy of the shadow region row and pass that to generic functions.

mm/kasan/report.c +0 -0
index d19d01823a68..60fdb0413f3b 100644
@@ -166,14 +166,20 @@ static void print_shadow_for_address(const void *addr)
        for (i = -SHADOW_ROWS_AROUND_ADDR; i <= SHADOW_ROWS_AROUND_ADDR; i++) {
                const void *kaddr = kasan_shadow_to_mem(shadow_row);

mm/kasan/report.c static void print_shadow_for_address(const void *addr) · line 176

Quoted 10 lines
mm/kasan/report.c +5 -1
                char buffer[4 + (BITS_PER_LONG/8)*2];
+               char shadow_buf[SHADOW_BYTES_PER_ROW];
 
                snprintf(buffer, sizeof(buffer),
                        (i == 0) ? ">%p: " : " %p: ", kaddr);
-
+               /*
+                * We should not pass a shadow pointer to generic
+                * function, because generic functions may try to
+                * kasan mapping for the passed address.

may try to *access* kasan mapping?

Quoted 11 lines
mm/kasan/report.c +3 -1
+                */
+               memcpy(shadow_buf, shadow_row, SHADOW_BYTES_PER_ROW);
                kasan_disable_current();
                print_hex_dump(KERN_ERR, buffer,
                        DUMP_PREFIX_NONE, SHADOW_BYTES_PER_ROW, 1,
-                       shadow_row, SHADOW_BYTES_PER_ROW, 0);
+                       shadow_buf, SHADOW_BYTES_PER_ROW, 0);
                kasan_enable_current();
 
                if (row_is_guilty(shadow_row, shadow))
#10 lore raw

PATCH v2 5/10 kasan: Enable arch to hook into kasan callbacks.

In reply to Aneesh Kumar K.V

We add enable/disable callbacks in this patch which architecture can implemement. We will use this in the later patches for architecture like ppc64, that cannot have early zero page kasan shadow region for the entire virtual address space. Such architectures also cannot use inline kasan support.

---
 mm/kasan/kasan.c |  9 +++++++++
 mm/kasan/kasan.h | 15 +++++++++++++++
 2 files changed, 24 insertions(+)
mm/kasan/kasan.c +9 -0
index 7b28e9cdf1c7..e4d33afd0eaf 100644
@@ -43,6 +43,9 @@ static void kasan_poison_shadow(const void *address, size_t size, u8 value)
 {
 	void *shadow_start, *shadow_end;
 
+	if (!kasan_enabled())
+		return;
+
 	shadow_start = kasan_mem_to_shadow(address);
 	shadow_end = kasan_mem_to_shadow(address + size);
 
@@ -51,6 +54,9 @@ static void kasan_poison_shadow(const void *address, size_t size, u8 value)
 
 void kasan_unpoison_shadow(const void *address, size_t size)
 {
+	if (!kasan_enabled())
+		return;
+
 	kasan_poison_shadow(address, size, 0);
 
 	if (size & KASAN_SHADOW_MASK) {
@@ -238,6 +244,9 @@ static __always_inline void check_memory_region(unsigned long addr,
 {
 	struct kasan_access_info info;
 
+	if (!kasan_enabled())
+		return;
+
 	if (unlikely(size == 0))
 		return;
 
mm/kasan/kasan.h +15 -0
index a6b46cc94907..deb547d5a916 100644
@@ -68,6 +68,21 @@ static inline bool kasan_report_enabled(void)
 	return !current->kasan_depth;
 }
 
+#ifndef kasan_enabled
+/*
+ * Some archs may want to disable kasan callbacks.
+ */
+static inline bool kasan_enabled(void)
+{
+	return true;
+}
+#define kasan_enabled kasan_enabled
+#else
+#ifdef CONFIG_KASAN_INLINE
+#error "Kasan inline support cannot work with KASAN arch hooks"
+#endif
+#endif
+
 void kasan_report(unsigned long addr, size_t size,
 		bool is_write, unsigned long ip);
 
Signature · 1 line
2.5.0
maintainer
#11 lore raw

Re: [PATCH V2 05/10] kasan: Enable arch to hook into kasan callbacks.

2015-08-26 11:26 GMT+03:00 Aneesh Kumar K.V <[email protected]>:

Quoted 17 lines

We add enable/disable callbacks in this patch which architecture can implemement. We will use this in the later patches for architecture like ppc64, that cannot have early zero page kasan shadow region for the entire virtual address space. Such architectures also cannot use inline kasan support.

---
 mm/kasan/kasan.c |  9 +++++++++
 mm/kasan/kasan.h | 15 +++++++++++++++
 2 files changed, 24 insertions(+)
mm/kasan/kasan.c +0 -0
index 7b28e9cdf1c7..e4d33afd0eaf 100644

mm/kasan/kasan.c static void kasan_poison_shadow(const void *address, size_t size, u8 value) · line 48

Quoted 6 lines
mm/kasan/kasan.c +3 -0
@@ -43,6 +43,9 @@ static void kasan_poison_shadow(const void *address, size_t size, u8 value)
 {
        void *shadow_start, *shadow_end;
 
+       if (!kasan_enabled())
+               return;
+

By the time this function called we already should have shadow, so it should be safe to remove this check.

mm/kasan/kasan.c static void kasan_poison_shadow(const void *address, size_t size, u8 value) · line 59

Quoted 10 lines
mm/kasan/kasan.c +3 -0
        shadow_start = kasan_mem_to_shadow(address);
        shadow_end = kasan_mem_to_shadow(address + size);
 
@@ -51,6 +54,9 @@ static void kasan_poison_shadow(const void *address, size_t size, u8 value)
 
 void kasan_unpoison_shadow(const void *address, size_t size)
 {
+       if (!kasan_enabled())
+               return;
+

Ditto.

Quoted 20 lines
mm/kasan/kasan.c +3 -0
        kasan_poison_shadow(address, size, 0);
 
        if (size & KASAN_SHADOW_MASK) {
@@ -238,6 +244,9 @@ static __always_inline void check_memory_region(unsigned long addr,
 {
        struct kasan_access_info info;
 
+       if (!kasan_enabled())
+               return;
+
        if (unlikely(size == 0))
                return;
 
mm/kasan/kasan.h +0 -0
index a6b46cc94907..deb547d5a916 100644
@@ -68,6 +68,21 @@ static inline bool kasan_report_enabled(void)
        return !current->kasan_depth;
 }

mm/kasan/kasan.h static inline bool kasan_report_enabled(void) · line 79

Quoted 10 lines
mm/kasan/kasan.h +9 -0
 
+#ifndef kasan_enabled
+/*
+ * Some archs may want to disable kasan callbacks.
+ */
+static inline bool kasan_enabled(void)
+{
+       return true;
+}
+#define kasan_enabled kasan_enabled

Why we need this define?

Quoted 12 lines
mm/kasan/kasan.h +6 -1
+#else
+#ifdef CONFIG_KASAN_INLINE
+#error "Kasan inline support cannot work with KASAN arch hooks"
+#endif
+#endif
+
 void kasan_report(unsigned long addr, size_t size,
                bool is_write, unsigned long ip);
 
--

2.5.0

#12 lore raw

Re: [PATCH V2 05/10] kasan: Enable arch to hook into kasan callbacks.

Quoted 21 lines · Andrey Ryabinin

2015-08-26 11:26 GMT+03:00 Aneesh Kumar K.V <[email protected]>:

Quoted 20 lines

We add enable/disable callbacks in this patch which architecture can implemement. We will use this in the later patches for architecture like ppc64, that cannot have early zero page kasan shadow region for the entire virtual address space. Such architectures also cannot use inline kasan support.

---
 mm/kasan/kasan.c |  9 +++++++++
 mm/kasan/kasan.h | 15 +++++++++++++++
 2 files changed, 24 insertions(+)
mm/kasan/kasan.c +0 -0
index 7b28e9cdf1c7..e4d33afd0eaf 100644
@@ -43,6 +43,9 @@ static void kasan_poison_shadow(const void *address, size_t size, u8 value)
 {
        void *shadow_start, *shadow_end;
 
Quoted 7 lines

mm/kasan/kasan.c static void kasan_poison_shadow(const void *address, size_t size, u8 value) · line 48

Quoted 3 lines
mm/kasan/kasan.c +3 -0
+       if (!kasan_enabled())
+               return;
+

By the time this function called we already should have shadow, so it should be safe to remove this check.

I remember hitting that call before enabling kasan completely. Will check that again.

Quoted 35 lines

mm/kasan/kasan.c static void kasan_poison_shadow(const void *address, size_t size, u8 value) · line 59

Quoted 11 lines
mm/kasan/kasan.c +3 -0
        shadow_start = kasan_mem_to_shadow(address);
        shadow_end = kasan_mem_to_shadow(address + size);
 
@@ -51,6 +54,9 @@ static void kasan_poison_shadow(const void *address, size_t size, u8 value)
 
 void kasan_unpoison_shadow(const void *address, size_t size)
 {
+       if (!kasan_enabled())
+               return;
+

Ditto.

Quoted 21 lines
mm/kasan/kasan.c +3 -0
        kasan_poison_shadow(address, size, 0);
 
        if (size & KASAN_SHADOW_MASK) {
@@ -238,6 +244,9 @@ static __always_inline void check_memory_region(unsigned long addr,
 {
        struct kasan_access_info info;
 
+       if (!kasan_enabled())
+               return;
+
        if (unlikely(size == 0))
                return;
 
mm/kasan/kasan.h +0 -0
index a6b46cc94907..deb547d5a916 100644
@@ -68,6 +68,21 @@ static inline bool kasan_report_enabled(void)
        return !current->kasan_depth;
 }
 
Quoted 11 lines

mm/kasan/kasan.h static inline bool kasan_report_enabled(void) · line 79

Quoted 9 lines
mm/kasan/kasan.h +9 -0
+#ifndef kasan_enabled
+/*
+ * Some archs may want to disable kasan callbacks.
+ */
+static inline bool kasan_enabled(void)
+{
+       return true;
+}
+#define kasan_enabled kasan_enabled

Why we need this define?

That is to make sure that we don't endup with different definition of kasan_enabled due to header include errors. Once we select a particular definition of the overloaded function, it is always good to mark the function as defined, so that checks like #ifndef kasan_enabled will always fail.

Quoted 10 lines
Quoted 10 lines
mm/kasan/kasan.h +6 -0
+#else
+#ifdef CONFIG_KASAN_INLINE
+#error "Kasan inline support cannot work with KASAN arch hooks"
+#endif
+#endif
+
 void kasan_report(unsigned long addr, size_t size,
                bool is_write, unsigned long ip);
 
Quoted 3 lines
Quoted 3 lines

--
2.5.0

-aneesh

#13 lore raw

PATCH v2 6/10 kasan: Allow arch to overrride kasan shadow offsets

In reply to Aneesh Kumar K.V

Some archs may want to provide kasan shadow memory as a constant offset from the address. Such arch even though cannot use inline kasan support, they can work with outofline kasan support.

---
 include/linux/kasan.h | 3 +++
 mm/kasan/kasan.h      | 3 +++
 2 files changed, 6 insertions(+)
include/linux/kasan.h +3 -0
index 5486d777b706..e458ca64cdaf 100644
@@ -15,11 +15,14 @@ struct vm_struct;
 #include <asm/kasan.h>
 #include <linux/sched.h>
 
+#ifndef kasan_mem_to_shadow
 static inline void *kasan_mem_to_shadow(const void *addr)
 {
 	return (void *)((unsigned long)addr >> KASAN_SHADOW_SCALE_SHIFT)
 		+ KASAN_SHADOW_OFFSET;
 }
+#define kasan_mem_to_shadow kasan_mem_to_shadow
+#endif
 
 /* Enable reporting bugs after kasan_disable_current() */
 static inline void kasan_enable_current(void)
mm/kasan/kasan.h +3 -0
index deb547d5a916..c0686f2b1224 100644
@@ -57,11 +57,14 @@ struct kasan_global {
 void kasan_report_error(struct kasan_access_info *info);
 void kasan_report_user_access(struct kasan_access_info *info);
 
+#ifndef kasan_shadow_to_mem
 static inline const void *kasan_shadow_to_mem(const void *shadow_addr)
 {
 	return (void *)(((unsigned long)shadow_addr - KASAN_SHADOW_OFFSET)
 		<< KASAN_SHADOW_SCALE_SHIFT);
 }
+#define kasan_shadow_to_mem kasan_shadow_to_mem
+#endif
 
 static inline bool kasan_report_enabled(void)
 {
Signature · 1 line
2.5.0
maintainer
#14 lore raw

Re: [PATCH V2 06/10] kasan: Allow arch to overrride kasan shadow offsets

2015-08-26 11:26 GMT+03:00 Aneesh Kumar K.V <[email protected]>:

Quoted 15 lines

Some archs may want to provide kasan shadow memory as a constant offset from the address. Such arch even though cannot use inline kasan support, they can work with outofline kasan support.

---
 include/linux/kasan.h | 3 +++
 mm/kasan/kasan.h      | 3 +++
 2 files changed, 6 insertions(+)
include/linux/kasan.h +0 -0
index 5486d777b706..e458ca64cdaf 100644

include/linux/kasan.h struct vm_struct; · line 24

Quoted 10 lines
include/linux/kasan.h +2 -0
@@ -15,11 +15,14 @@ struct vm_struct;
 #include <asm/kasan.h>
 #include <linux/sched.h>
 
+#ifndef kasan_mem_to_shadow
 static inline void *kasan_mem_to_shadow(const void *addr)
 {
        return (void *)((unsigned long)addr >> KASAN_SHADOW_SCALE_SHIFT)
                + KASAN_SHADOW_OFFSET;
 }
+#define kasan_mem_to_shadow kasan_mem_to_shadow

Again, why we need this define here? I think it should be safe to remove it.

Quoted 9 lines
include/linux/kasan.h +1 -0
+#endif
 
 /* Enable reporting bugs after kasan_disable_current() */
 static inline void kasan_enable_current(void)
mm/kasan/kasan.h +0 -0
index deb547d5a916..c0686f2b1224 100644

mm/kasan/kasan.h struct kasan_global { · line 66

Quoted 10 lines
mm/kasan/kasan.h +2 -0
@@ -57,11 +57,14 @@ struct kasan_global {
 void kasan_report_error(struct kasan_access_info *info);
 void kasan_report_user_access(struct kasan_access_info *info);
 
+#ifndef kasan_shadow_to_mem
 static inline const void *kasan_shadow_to_mem(const void *shadow_addr)
 {
        return (void *)(((unsigned long)shadow_addr - KASAN_SHADOW_OFFSET)
                << KASAN_SHADOW_SCALE_SHIFT);
 }
+#define kasan_shadow_to_mem kasan_shadow_to_mem

ditto

Quoted 7 lines
mm/kasan/kasan.h +1 -1
+#endif
 
 static inline bool kasan_report_enabled(void)
 {
--

2.5.0

#15 lore raw

PATCH v2 7/10 kasan: Make INLINE KASan support arch selectable

In reply to Aneesh Kumar K.V

Some of the archs, may find it difficult to support inline KASan mode. Add HAVE_ARCH_KASAN_INLINE so that we can disable inline support at config time.

---
 arch/x86/Kconfig       |  1 +
 lib/Kconfig.kasan      |  2 ++
 scripts/Makefile.kasan | 28 ++++++++++++++--------------
 3 files changed, 17 insertions(+), 14 deletions(-)
arch/x86/Kconfig +1 -0
index b3a1a5d77d92..4416f80580fb 100644
@@ -78,6 +78,7 @@ config X86
 	select HAVE_ARCH_HUGE_VMAP		if X86_64 || X86_PAE
 	select HAVE_ARCH_JUMP_LABEL
 	select HAVE_ARCH_KASAN			if X86_64 && SPARSEMEM_VMEMMAP
+	select HAVE_ARCH_KASAN_INLINE			if X86_64 && SPARSEMEM_VMEMMAP
 	select HAVE_ARCH_KGDB
 	select HAVE_ARCH_KMEMCHECK
 	select HAVE_ARCH_SECCOMP_FILTER
lib/Kconfig.kasan +2 -0
index 39f24d6721e5..e9d1bb1175b8 100644
@@ -32,6 +32,7 @@ config KASAN_OUTLINE
 	  however it doesn't bloat size of kernel's .text section so
 	  much as inline does.
 
+if HAVE_ARCH_KASAN_INLINE
 config KASAN_INLINE
 	bool "Inline instrumentation"
 	help
@@ -40,6 +41,7 @@ config KASAN_INLINE
 	  it gives about x2 boost over outline instrumentation), but
 	  make kernel's .text size much bigger.
 	  This requires a gcc version of 5.0 or later.
+endif
 
 endchoice
 
scripts/Makefile.kasan +14 -14
index 3f874d24234f..c1c06e9e107a 100644
@@ -1,29 +1,29 @@
 ifdef CONFIG_KASAN
-ifdef CONFIG_KASAN_INLINE
-	call_threshold := 10000
-else
-	call_threshold := 0
-endif
-
-CFLAGS_KASAN_MINIMAL := -fsanitize=kernel-address
 
 CFLAGS_KASAN := $(call cc-option, -fsanitize=kernel-address \
-		-fasan-shadow-offset=$(CONFIG_KASAN_SHADOW_OFFSET) \
-		--param asan-stack=1 --param asan-globals=1 \
-		--param asan-instrumentation-with-call-threshold=$(call_threshold))
-
-ifeq ($(call cc-option, $(CFLAGS_KASAN_MINIMAL) -Werror),)
+                   --param asan-instrumentation-with-call-threshold=0)
+ifeq ($(CFLAGS_KASAN),)
    ifneq ($(CONFIG_COMPILE_TEST),y)
         $(warning Cannot use CONFIG_KASAN: \
             -fsanitize=kernel-address is not supported by compiler)
    endif
 else
-    ifeq ($(CFLAGS_KASAN),)
+
+   ifdef CONFIG_KASAN_INLINE
+   CFLAGS_KASAN_INLINE := $(call cc-option, -fsanitize=kernel-address \
+                        -fasan-shadow-offset=$(CONFIG_KASAN_SHADOW_OFFSET) \
+                        --param asan-stack=1 --param asan-globals=1 \
+                        --param asan-instrumentation-with-call-threshold=10000)
+
+    ifeq ($(CFLAGS_KASAN_INLINE),)
         ifneq ($(CONFIG_COMPILE_TEST),y)
             $(warning CONFIG_KASAN: compiler does not support all options.\
                 Trying minimal configuration)
         endif
-        CFLAGS_KASAN := $(CFLAGS_KASAN_MINIMAL)
+    else
+        CFLAGS_KASAN := $(CFLAGS_KASAN_INLINE)
     endif
+    endif
+
 endif
 endif
Signature · 1 line
2.5.0
maintainer
#16 lore raw

Re: [PATCH V2 07/10] kasan: Make INLINE KASan support arch selectable

2015-08-26 11:26 GMT+03:00 Aneesh Kumar K.V <[email protected]>:

Quoted 16 lines

Some of the archs, may find it difficult to support inline KASan mode. Add HAVE_ARCH_KASAN_INLINE so that we can disable inline support at config time.

---
 arch/x86/Kconfig       |  1 +
 lib/Kconfig.kasan      |  2 ++
 scripts/Makefile.kasan | 28 ++++++++++++++--------------
 3 files changed, 17 insertions(+), 14 deletions(-)
arch/x86/Kconfig +0 -0
index b3a1a5d77d92..4416f80580fb 100644

arch/x86/Kconfig config X86 · line 81

Quoted 4 lines
arch/x86/Kconfig +1 -0
@@ -78,6 +78,7 @@ config X86
        select HAVE_ARCH_HUGE_VMAP              if X86_64 || X86_PAE
        select HAVE_ARCH_JUMP_LABEL
        select HAVE_ARCH_KASAN                  if X86_64 && SPARSEMEM_VMEMMAP
+       select HAVE_ARCH_KASAN_INLINE                   if X86_64 && SPARSEMEM_VMEMMAP

This will not work because config HAVE_ARCH_KASAN_INLINE is not defined. Instead of you can just add following in this file:

config HAVE_ARCH_KASAN_INLINE
def_bool y
depends on KASAN

Quoted 8 lines
arch/x86/Kconfig +0 -0
        select HAVE_ARCH_KGDB
        select HAVE_ARCH_KMEMCHECK
        select HAVE_ARCH_SECCOMP_FILTER
lib/Kconfig.kasan +0 -0
index 39f24d6721e5..e9d1bb1175b8 100644

lib/Kconfig.kasan config KASAN_OUTLINE · line 37

Quoted 6 lines
lib/Kconfig.kasan +1 -0
@@ -32,6 +32,7 @@ config KASAN_OUTLINE
          however it doesn't bloat size of kernel's .text section so
          much as inline does.
 
+if HAVE_ARCH_KASAN_INLINE
 config KASAN_INLINE
        bool "Inline instrumentation"

depends on HAVE_ARCH_KASAN_INLINE

Quoted 45 lines
lib/Kconfig.kasan +1 -0
        help
@@ -40,6 +41,7 @@ config KASAN_INLINE
          it gives about x2 boost over outline instrumentation), but
          make kernel's .text size much bigger.
          This requires a gcc version of 5.0 or later.
+endif
 
 endchoice
 
scripts/Makefile.kasan +10 -13
index 3f874d24234f..c1c06e9e107a 100644
@@ -1,29 +1,29 @@
 ifdef CONFIG_KASAN
-ifdef CONFIG_KASAN_INLINE
-       call_threshold := 10000
-else
-       call_threshold := 0
-endif
-
-CFLAGS_KASAN_MINIMAL := -fsanitize=kernel-address
 
 CFLAGS_KASAN := $(call cc-option, -fsanitize=kernel-address \
-               -fasan-shadow-offset=$(CONFIG_KASAN_SHADOW_OFFSET) \
-               --param asan-stack=1 --param asan-globals=1 \
-               --param asan-instrumentation-with-call-threshold=$(call_threshold))
-
-ifeq ($(call cc-option, $(CFLAGS_KASAN_MINIMAL) -Werror),)
+                   --param asan-instrumentation-with-call-threshold=0)
+ifeq ($(CFLAGS_KASAN),)
    ifneq ($(CONFIG_COMPILE_TEST),y)
         $(warning Cannot use CONFIG_KASAN: \
             -fsanitize=kernel-address is not supported by compiler)
    endif
 else
-    ifeq ($(CFLAGS_KASAN),)
+
+   ifdef CONFIG_KASAN_INLINE
+   CFLAGS_KASAN_INLINE := $(call cc-option, -fsanitize=kernel-address \
+                        -fasan-shadow-offset=$(CONFIG_KASAN_SHADOW_OFFSET) \
+                        --param asan-stack=1 --param asan-globals=1 \
+                        --param asan-instrumentation-with-call-threshold=10000)
+
+    ifeq ($(CFLAGS_KASAN_INLINE),)

scripts/Makefile.kasan line 27

Quoted 10 lines
scripts/Makefile.kasan +4 -1
         ifneq ($(CONFIG_COMPILE_TEST),y)
             $(warning CONFIG_KASAN: compiler does not support all options.\
                 Trying minimal configuration)
         endif
-        CFLAGS_KASAN := $(CFLAGS_KASAN_MINIMAL)
+    else
+        CFLAGS_KASAN := $(CFLAGS_KASAN_INLINE)
     endif
+    endif
+

This removes stack and globals for CONFIG_KASAN_OUTLINE=y. Why? Those are completely separate features. So this patch shouldn't touch this Makefile at all.
Depends on HAVE_ARCH_KASAN_INLINE in CONFIG_KASAN_INLINE should be enough.

But you need to disable 'asan-stack' and 'asan-globals' for pcc64. I'd suggest to introduce CFLAGS_ARCH_KASAN.
Define it in ppc64 Makefile:

CFLAGS_ARCH_KASAN := --param asan-globals=0 --param asan-stack=0

and add these flags to CFLAGS_KASAN_MINIMAL and CFLAGS_KASAN in Makefile.kasan.

Quoted 5 lines
scripts/Makefile.kasan +0 -1
 endif
 endif
--

2.5.0

#17 lore raw

Re: [PATCH V2 07/10] kasan: Make INLINE KASan support arch selectable

Quoted 25 lines · Andrey Ryabinin

2015-08-26 11:26 GMT+03:00 Aneesh Kumar K.V <[email protected]>:

Quoted 16 lines

Some of the archs, may find it difficult to support inline KASan mode. Add HAVE_ARCH_KASAN_INLINE so that we can disable inline support at config time.

---
 arch/x86/Kconfig       |  1 +
 lib/Kconfig.kasan      |  2 ++
 scripts/Makefile.kasan | 28 ++++++++++++++--------------
 3 files changed, 17 insertions(+), 14 deletions(-)
arch/x86/Kconfig +0 -0
index b3a1a5d77d92..4416f80580fb 100644

arch/x86/Kconfig config X86 · line 81

Quoted 4 lines
arch/x86/Kconfig +1 -0
@@ -78,6 +78,7 @@ config X86
        select HAVE_ARCH_HUGE_VMAP              if X86_64 || X86_PAE
        select HAVE_ARCH_JUMP_LABEL
        select HAVE_ARCH_KASAN                  if X86_64 && SPARSEMEM_VMEMMAP
+       select HAVE_ARCH_KASAN_INLINE                   if X86_64 && SPARSEMEM_VMEMMAP

This will not work because config HAVE_ARCH_KASAN_INLINE is not defined. Instead of you can just add following in this file:

Quoted 4 lines

config HAVE_ARCH_KASAN_INLINE
def_bool y
depends on KASAN

Missed cherry-pick

lib/Kconfig.kasan +3 -0
index e9d1bb1175b8..5dba03bc3f01 100644
@@ -1,6 +1,9 @@
 config HAVE_ARCH_KASAN
 	bool
 
+config HAVE_ARCH_KASAN_INLINE
+	bool
+
 if HAVE_ARCH_KASAN
 
 config KASAN
Quoted 70 lines
Quoted 9 lines
arch/x86/Kconfig +0 -0
        select HAVE_ARCH_KGDB
        select HAVE_ARCH_KMEMCHECK
        select HAVE_ARCH_SECCOMP_FILTER
lib/Kconfig.kasan +0 -0
index 39f24d6721e5..e9d1bb1175b8 100644

lib/Kconfig.kasan config KASAN_OUTLINE · line 37

Quoted 6 lines
lib/Kconfig.kasan +1 -0
@@ -32,6 +32,7 @@ config KASAN_OUTLINE
          however it doesn't bloat size of kernel's .text section so
          much as inline does.
 
+if HAVE_ARCH_KASAN_INLINE
 config KASAN_INLINE
        bool "Inline instrumentation"

depends on HAVE_ARCH_KASAN_INLINE

Quoted 52 lines
lib/Kconfig.kasan +1 -0
        help
@@ -40,6 +41,7 @@ config KASAN_INLINE
          it gives about x2 boost over outline instrumentation), but
          make kernel's .text size much bigger.
          This requires a gcc version of 5.0 or later.
+endif
 
 endchoice
 
scripts/Makefile.kasan +12 -14
index 3f874d24234f..c1c06e9e107a 100644
@@ -1,29 +1,29 @@
 ifdef CONFIG_KASAN
-ifdef CONFIG_KASAN_INLINE
-       call_threshold := 10000
-else
-       call_threshold := 0
-endif
-
-CFLAGS_KASAN_MINIMAL := -fsanitize=kernel-address
 
 CFLAGS_KASAN := $(call cc-option, -fsanitize=kernel-address \
-               -fasan-shadow-offset=$(CONFIG_KASAN_SHADOW_OFFSET) \
-               --param asan-stack=1 --param asan-globals=1 \
-               --param asan-instrumentation-with-call-threshold=$(call_threshold))
-
-ifeq ($(call cc-option, $(CFLAGS_KASAN_MINIMAL) -Werror),)
+                   --param asan-instrumentation-with-call-threshold=0)
+ifeq ($(CFLAGS_KASAN),)
    ifneq ($(CONFIG_COMPILE_TEST),y)
         $(warning Cannot use CONFIG_KASAN: \
             -fsanitize=kernel-address is not supported by compiler)
    endif
 else
-    ifeq ($(CFLAGS_KASAN),)
+
+   ifdef CONFIG_KASAN_INLINE
+   CFLAGS_KASAN_INLINE := $(call cc-option, -fsanitize=kernel-address \
+                        -fasan-shadow-offset=$(CONFIG_KASAN_SHADOW_OFFSET) \
+                        --param asan-stack=1 --param asan-globals=1 \
+                        --param asan-instrumentation-with-call-threshold=10000)
+
+    ifeq ($(CFLAGS_KASAN_INLINE),)
         ifneq ($(CONFIG_COMPILE_TEST),y)
             $(warning CONFIG_KASAN: compiler does not support all options.\
                 Trying minimal configuration)
         endif
-        CFLAGS_KASAN := $(CFLAGS_KASAN_MINIMAL)
+    else
+        CFLAGS_KASAN := $(CFLAGS_KASAN_INLINE)
Quoted 8 lines

scripts/Makefile.kasan line 27

Quoted 3 lines
scripts/Makefile.kasan +2 -0
     endif
+    endif
+

This removes stack and globals for CONFIG_KASAN_OUTLINE=y. Why? Those are completely separate features. So this patch shouldn't touch this Makefile at all.
Depends on HAVE_ARCH_KASAN_INLINE in CONFIG_KASAN_INLINE should be enough.

Correct. I have changes that will make kasan_global and kasan_stack as config items. I didn't add it as part of this series because I still haven't got outline global and stack work on ppc64. Right now the split config only applies to ppc64. Hence didn't want to make the patch more complicated.

Quoted 13 lines

But you need to disable 'asan-stack' and 'asan-globals' for pcc64. I'd suggest to introduce CFLAGS_ARCH_KASAN.
Define it in ppc64 Makefile:

CFLAGS_ARCH_KASAN := --param asan-globals=0 --param asan-stack=0

and add these flags to CFLAGS_KASAN_MINIMAL and CFLAGS_KASAN in Makefile.kasan.

Quoted 3 lines
scripts/Makefile.kasan +0 -0
 endif
 endif

-aneesh

#18 lore raw

PATCH v2 8/10 kasan: Update feature support file

In reply to Aneesh Kumar K.V

Now that we have two features KASAN and KASAN_INLINE, add new feature support file for the same.

---
 .../debug/KASAN/KASAN_INLINE/arch-support.txt      | 40 ++++++++++++++++++++++
 .../KASAN/{ => KASAN_OUTLINE}/arch-support.txt     |  0
 2 files changed, 40 insertions(+)
 create mode 100644 Documentation/features/debug/KASAN/KASAN_INLINE/arch-support.txt
 rename Documentation/features/debug/KASAN/{ => KASAN_OUTLINE}/arch-support.txt (100%)
Documentation/features/debug/KASAN/KASAN_INLINE/arch-support.txt new file +40 -0
new file mode 100644
index 000000000000..834818fba910
@@ -0,0 +1,40 @@
+#
+# Feature name:          KASAN
+#         Kconfig:       HAVE_ARCH_KASAN_INLINE
+#         description:   arch supports the KASAN runtime memory checker
+#
+    -----------------------
+    |         arch |status|
+    -----------------------
+    |       alpha: | TODO |
+    |         arc: | TODO |
+    |         arm: | TODO |
+    |       arm64: | TODO |
+    |       avr32: | TODO |
+    |    blackfin: | TODO |
+    |         c6x: | TODO |
+    |        cris: | TODO |
+    |         frv: | TODO |
+    |       h8300: | TODO |
+    |     hexagon: | TODO |
+    |        ia64: | TODO |
+    |        m32r: | TODO |
+    |        m68k: | TODO |
+    |       metag: | TODO |
+    |  microblaze: | TODO |
+    |        mips: | TODO |
+    |     mn10300: | TODO |
+    |       nios2: | TODO |
+    |    openrisc: | TODO |
+    |      parisc: | TODO |
+    |     powerpc: | TODO |
+    |        s390: | TODO |
+    |       score: | TODO |
+    |          sh: | TODO |
+    |       sparc: | TODO |
+    |        tile: | TODO |
+    |          um: | TODO |
+    |   unicore32: | TODO |
+    |         x86: |  ok  |
+    |      xtensa: | TODO |
+    -----------------------
Documentation/features/debug/KASAN/KASAN_OUTLINE/arch-support.txt renamed from Documentation/features/debug/KASAN/arch-support.txt +0 -0
similarity index 100%
rename from Documentation/features/debug/KASAN/arch-support.txt
rename to Documentation/features/debug/KASAN/KASAN_OUTLINE/arch-support.txt
Signature · 1 line
2.5.0
#19 lore raw

PATCH v2 9/10 kasan: Prevent deadlock in kasan reporting

In reply to Aneesh Kumar K.V

We we end up calling kasan_report in real mode, our shadow mapping for even spinlock variable will show poisoned. This will result in us calling kasan_report_error with lock_report spin lock held. To prevent this disable kasan reporting when we are priting error w.r.t kasan.

---
 mm/kasan/report.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
mm/kasan/report.c +10 -0
index 79fbc5d14bd2..82b41eb83e43 100644
@@ -185,6 +185,10 @@ void kasan_report_error(struct kasan_access_info *info)
 {
 	unsigned long flags;
 
+	/*
+	 * Make sure we don't end up in loop.
+	 */
+	kasan_disable_current();
 	spin_lock_irqsave(&report_lock, flags);
 	pr_err("================================="
 		"=================================\n");
@@ -194,12 +198,17 @@ void kasan_report_error(struct kasan_access_info *info)
 	pr_err("================================="
 		"=================================\n");
 	spin_unlock_irqrestore(&report_lock, flags);
+	kasan_enable_current();
 }
 
 void kasan_report_user_access(struct kasan_access_info *info)
 {
 	unsigned long flags;
 
+	/*
+	 * Make sure we don't end up in loop.
+	 */
+	kasan_disable_current();
 	spin_lock_irqsave(&report_lock, flags);
 	pr_err("================================="
 		"=================================\n");
@@ -212,6 +221,7 @@ void kasan_report_user_access(struct kasan_access_info *info)
 	pr_err("================================="
 		"=================================\n");
 	spin_unlock_irqrestore(&report_lock, flags);
+	kasan_enable_current();
 }
 
 void kasan_report(unsigned long addr, size_t size,
Signature · 1 line
2.5.0
maintainer
#20 lore raw

Re: [PATCH V2 09/10] kasan: Prevent deadlock in kasan reporting

2015-08-26 11:26 GMT+03:00 Aneesh Kumar K.V <[email protected]>:

Quoted 2 lines

We we end up calling kasan_report in real mode, our shadow mapping for even spinlock variable will show poisoned.

Generally I agree with this patch. We should disable reports when we print report as early
as possible to prevent recursion in case of bug in spinlock or printk etc.

But I don't understand what is the problem that you observing. How we ended up with shadow poisoned for a valid spinlock struct? And since shadow poisoned for some valid memory we should get enormous amount of false positive reports.

Quoted 54 lines

This will result
in us calling kasan_report_error with lock_report spin lock held. To prevent this disable kasan reporting when we are priting error w.r.t kasan.

---
 mm/kasan/report.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
mm/kasan/report.c +10 -1
index 79fbc5d14bd2..82b41eb83e43 100644
@@ -185,6 +185,10 @@ void kasan_report_error(struct kasan_access_info *info)
 {
        unsigned long flags;
 
+       /*
+        * Make sure we don't end up in loop.
+        */
+       kasan_disable_current();
        spin_lock_irqsave(&report_lock, flags);
        pr_err("================================="
                "=================================\n");
@@ -194,12 +198,17 @@ void kasan_report_error(struct kasan_access_info *info)
        pr_err("================================="
                "=================================\n");
        spin_unlock_irqrestore(&report_lock, flags);
+       kasan_enable_current();
 }
 
 void kasan_report_user_access(struct kasan_access_info *info)
 {
        unsigned long flags;
 
+       /*
+        * Make sure we don't end up in loop.
+        */
+       kasan_disable_current();
        spin_lock_irqsave(&report_lock, flags);
        pr_err("================================="
                "=================================\n");
@@ -212,6 +221,7 @@ void kasan_report_user_access(struct kasan_access_info *info)
        pr_err("================================="
                "=================================\n");
        spin_unlock_irqrestore(&report_lock, flags);
+       kasan_enable_current();
 }
 
 void kasan_report(unsigned long addr, size_t size,
--

2.5.0

#21 lore raw

Re: [PATCH V2 09/10] kasan: Prevent deadlock in kasan reporting

Quoted 4 lines · Andrey Ryabinin

2015-08-26 11:26 GMT+03:00 Aneesh Kumar K.V <[email protected]>:

Quoted 2 lines

We we end up calling kasan_report in real mode, our shadow mapping for even spinlock variable will show poisoned.

Quoted 9 lines

Generally I agree with this patch. We should disable reports when we print report as early
as possible to prevent recursion in case of bug in spinlock or printk etc.

But I don't understand what is the problem that you observing. How we ended up with shadow poisoned for a valid spinlock struct? And since shadow poisoned for some valid memory we should get enormous amount of false positive reports.

I still haven't fully isolated all the .c files which should not be kasan instrumented. That means in case of ppc64 i ended up calling kasan _load/_store in real mode. That will result in failure w.r.t to the above spin_lock code.

-aneesh

#22 lore raw

PATCH v2 10/10 powerpc/mm: kasan: Add kasan support for ppc64

In reply to 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

---
 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             |  5 +++
 arch/powerpc/kernel/prom_init_check.sh   |  2 +-
 arch/powerpc/kernel/setup_64.c           |  3 ++
 arch/powerpc/kvm/Makefile                |  1 +
 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                 |  7 +++
 arch/powerpc/mm/kasan_init.c             | 44 +++++++++++++++++++
 arch/powerpc/mm/slb_low.S                |  4 ++
 arch/powerpc/platforms/Kconfig.cputype   |  1 +
 15 files changed, 180 insertions(+), 4 deletions(-)
 create mode 100644 arch/powerpc/include/asm/kasan.h
 create mode 100644 arch/powerpc/mm/kasan_init.c
arch/powerpc/include/asm/kasan.h new file +74 -0
new file mode 100644
index 000000000000..51e76e698bb9
@@ -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 0xc000000000000000 -> 0xe000000000000000
+ * 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
arch/powerpc/include/asm/pgtable-ppc64.h +1 -0
index 3bb7488bd24b..369ce5442aa6 100644
@@ -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
arch/powerpc/include/asm/ppc_asm.h +10 -0
index dd0fc18d8103..e75ae67e804e 100644
@@ -226,6 +226,11 @@ name:
 
 #define DOTSYM(a)	a
 
+#define KASAN_OVERRIDE(x, y) \
+	.weak x;	     \
+	.set x, y
+
+
 #else
 
 #define XGLUE(a,b) a##b
@@ -263,6 +268,11 @@ GLUE(.,name):
 
 #define DOTSYM(a)	GLUE(.,a)
 
+#define KASAN_OVERRIDE(x, y)	\
+	.weak x;		\
+	.set x, y;		\
+	.weak DOTSYM(x);	\
+	.set DOTSYM(x), DOTSYM(y)
 #endif
 
 #else /* 32-bit */
arch/powerpc/include/asm/string.h +13 -0
index e40010abcaf1..b10a4c01cdbf 100644
@@ -27,6 +27,19 @@ extern void * memmove(void *,const void *,__kernel_size_t);
 extern int memcmp(const void *,const void *,__kernel_size_t);
 extern void * memchr(const void *,int,__kernel_size_t);
 
+extern void * __memset(void *, int, __kernel_size_t);
+extern void * __memcpy(void *, const void *, __kernel_size_t);
+extern void * __memmove(void *, const void *, __kernel_size_t);
+
+#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)
+#endif
 #endif /* __KERNEL__ */
 
 #endif	/* _ASM_POWERPC_STRING_H */
arch/powerpc/kernel/Makefile +5 -0
index 12868b1c4e05..7b205628fd1b 100644
@@ -26,6 +26,11 @@ CFLAGS_REMOVE_ftrace.o = -pg -mno-sched-epilog
 CFLAGS_REMOVE_time.o = -pg -mno-sched-epilog
 endif
 
+KASAN_SANITIZE_prom_init.o := n
+KASAN_SANITIZE_align.o := n
+KASAN_SANITIZE_dbell.o := n
+KASAN_SANITIZE_setup_64.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/prom_init_check.sh +1 -1
index 12640f7e726b..e25777956123 100644
@@ -17,7 +17,7 @@
 # it to the list below:
 
 WHITELIST="add_reloc_offset __bss_start __bss_stop copy_and_flush
-_end enter_prom memcpy memset reloc_offset __secondary_hold
+_end enter_prom __memcpy __memset memcpy memset reloc_offset __secondary_hold
 __secondary_hold_acknowledge __secondary_hold_spinloop __start
 strcmp strcpy strlcpy strlen strncmp strstr logo_linux_clut224
 reloc_got2 kernstart_addr memstart_addr linux_banner _stext
arch/powerpc/kernel/setup_64.c +3 -0
index bdcbb716f4d6..4b766638ead9 100644
@@ -69,6 +69,7 @@
 #include <asm/kvm_ppc.h>
 #include <asm/hugetlb.h>
 #include <asm/epapr_hcalls.h>
+#include <asm/kasan.h>
 
 #ifdef DEBUG
 #define DBG(fmt...) udbg_printf(fmt)
@@ -708,6 +709,8 @@ void __init setup_arch(char **cmdline_p)
 	/* Initialize the MMU context management stuff */
 	mmu_context_init();
 
+	kasan_init();
+
 	/* Interrupt code needs to be 64K-aligned */
 	if ((unsigned long)_stext & 0xffff)
 		panic("Kernelbase not 64K-aligned (0x%lx)!\n",
arch/powerpc/kvm/Makefile +1 -0
index 0570eef83fba..26288d16899e 100644
@@ -3,6 +3,7 @@
 #
 
 subdir-ccflags-$(CONFIG_PPC_WERROR) := -Werror
+KASAN_SANITIZE :=n
 
 ccflags-y := -Ivirt/kvm -Iarch/powerpc/kvm
 KVM := ../../../virt/kvm
arch/powerpc/lib/mem_64.S +4 -2
index 43435c6892fb..0e1f811babdd 100644
@@ -12,7 +12,8 @@
 #include <asm/errno.h>
 #include <asm/ppc_asm.h>
 
-_GLOBAL(memset)
+KASAN_OVERRIDE(memset,__memset)
+_GLOBAL(__memset)
 	neg	r0,r3
 	rlwimi	r4,r4,8,16,23
 	andi.	r0,r0,7			/* # bytes to be 8-byte aligned */
@@ -77,7 +78,8 @@ _GLOBAL(memset)
 	stb	r4,0(r6)
 	blr
 
-_GLOBAL_TOC(memmove)
+KASAN_OVERRIDE(memmove,__memmove)
+_GLOBAL_TOC(__memmove)
 	cmplw	0,r3,r4
 	bgt	backwards_memcpy
 	b	memcpy
arch/powerpc/lib/memcpy_64.S +2 -1
index 32a06ec395d2..396b44181ec1 100644
@@ -10,7 +10,8 @@
 #include <asm/ppc_asm.h>
 
 	.align	7
-_GLOBAL_TOC(memcpy)
+KASAN_OVERRIDE(memcpy,__memcpy)
+_GLOBAL_TOC(__memcpy)
 BEGIN_FTR_SECTION
 #ifdef __LITTLE_ENDIAN__
 	cmpdi	cr7,r5,0
arch/powerpc/lib/ppc_ksyms.c +10 -0
index c7f8e9586316..3a27b08bee26 100644
@@ -9,6 +9,16 @@ EXPORT_SYMBOL(memmove);
 EXPORT_SYMBOL(memcmp);
 EXPORT_SYMBOL(memchr);
 
+#ifdef CONFIG_PPC64
+/*
+ * There symbols are needed with kasan. We only
+ * have that enabled for ppc64 now.
+ */
+EXPORT_SYMBOL(__memcpy);
+EXPORT_SYMBOL(__memset);
+EXPORT_SYMBOL(__memmove);
+#endif
+
 EXPORT_SYMBOL(strcpy);
 EXPORT_SYMBOL(strncpy);
 EXPORT_SYMBOL(strcat);
arch/powerpc/mm/Makefile +7 -0
index 3eb73a38220d..ad7d589c7e44 100644
@@ -6,6 +6,11 @@ subdir-ccflags-$(CONFIG_PPC_WERROR) := -Werror
 
 ccflags-$(CONFIG_PPC64)	:= $(NO_MINIMAL_TOC)
 
+KASAN_SANITIZE_kasan_init.o	:= n
+KASAN_SANITIZE_hash_utils_64.o	:= n
+KASAN_SANITIZE_hugetlbpage.o	:= n
+KASAN_SANITIZE_slb.o	:= n
+
 obj-y				:= fault.o mem.o pgtable.o mmap.o \
 				   init_$(CONFIG_WORD_SIZE).o \
 				   pgtable_$(CONFIG_WORD_SIZE).o
@@ -37,3 +42,5 @@ obj-$(CONFIG_NOT_COHERENT_CACHE) += dma-noncoherent.o
 obj-$(CONFIG_HIGHMEM)		+= highmem.o
 obj-$(CONFIG_PPC_COPRO_BASE)	+= copro_fault.o
 obj-$(CONFIG_SPAPR_TCE_IOMMU)	+= mmu_context_iommu.o
+
+obj-$(CONFIG_KASAN)		+= kasan_init.o
arch/powerpc/mm/kasan_init.c new file +44 -0

44 lines not on this page · raw message

arch/powerpc/mm/slb_low.S +4 -0
index 736d18b3cefd..154bd8a0b437 100644
@@ -80,11 +80,15 @@ END_MMU_FTR_SECTION_IFCLR(MMU_FTR_1T_SEGMENT)
 	/* Check virtual memmap region. To be patches at kernel boot */
 	cmpldi	cr0,r9,0xf
 	bne	1f
+2:
 .globl slb_miss_kernel_load_vmemmap
 slb_miss_kernel_load_vmemmap:
 	li	r11,0
 	b	6f
 1:
+	/* Kasan region same as vmemmap mapping */
+	cmpldi	cr0,r9,0xe
+	beq	2b
 #endif /* CONFIG_SPARSEMEM_VMEMMAP */
 
 	/* vmalloc mapping gets the encoding from the PACA as the mapping
arch/powerpc/platforms/Kconfig.cputype +1 -0
index c140e94c7c72..7a7c9d54f80e 100644
@@ -75,6 +75,7 @@ config PPC_BOOK3S_64
 	select HAVE_ARCH_TRANSPARENT_HUGEPAGE if PPC_64K_PAGES
 	select ARCH_SUPPORTS_NUMA_BALANCING
 	select IRQ_WORK
+	select HAVE_ARCH_KASAN if SPARSEMEM_VMEMMAP
 
 config PPC_BOOK3E_64
 	bool "Embedded processors"
Signature · 1 line
2.5.0
maintainer
#23 lore raw

Re: [PATCH V2 10/10] powerpc/mm: kasan: Add kasan support for ppc64

2015-08-26 11:26 GMT+03:00 Aneesh Kumar K.V <[email protected]>:

arch/powerpc/mm/kasan_init.c line 30

Quoted 7 lines
arch/powerpc/mm/kasan_init.c new file +7 -0
+               k_start = (unsigned long)kasan_mem_to_shadow(start);
+               k_end = (unsigned long)kasan_mem_to_shadow(end);
+               for (; k_start < k_end; k_start += page_size) {
+                       p = vmemmap_alloc_block(page_size, node);
+                       if (!p) {
+                               pr_info("Disabled Kasan, for lack of free mem\n");
+                               /* Free the stuff or panic ? */

vmemmap_alloc_block() panics on allocation failure, so you don't need this if block.
You could replace this with memblock_virt_alloc_try_nid_nopanic(), but note that if/when
we will have working asan-stack=1 there will be no way for fallback.

Quoted 49 lines
arch/powerpc/mm/kasan_init.c new file +14 -0
+                               return;
+                       }
+                       htab_bolt_mapping(k_start, k_start + page_size,
+                                         __pa(p), pgprot_val(PAGE_KERNEL),
+                                         mmu_vmemmap_psize, mmu_kernel_ssize);
+               }
+       }
+       /*
+        * At this point kasan is fully initialized. Enable error messages
+        */
+       init_task.kasan_depth = 0;
+       __kasan_enabled = true;
+       pr_info("Kernel address sanitizer initialized\n");
+}
arch/powerpc/mm/slb_low.S +4 -0
index 736d18b3cefd..154bd8a0b437 100644
@@ -80,11 +80,15 @@ END_MMU_FTR_SECTION_IFCLR(MMU_FTR_1T_SEGMENT)
        /* Check virtual memmap region. To be patches at kernel boot */
        cmpldi  cr0,r9,0xf
        bne     1f
+2:
 .globl slb_miss_kernel_load_vmemmap
 slb_miss_kernel_load_vmemmap:
        li      r11,0
        b       6f
 1:
+       /* Kasan region same as vmemmap mapping */
+       cmpldi  cr0,r9,0xe
+       beq     2b
 #endif /* CONFIG_SPARSEMEM_VMEMMAP */
 
        /* vmalloc mapping gets the encoding from the PACA as the mapping
arch/powerpc/platforms/Kconfig.cputype +1 -1
index c140e94c7c72..7a7c9d54f80e 100644
@@ -75,6 +75,7 @@ config PPC_BOOK3S_64
        select HAVE_ARCH_TRANSPARENT_HUGEPAGE if PPC_64K_PAGES
        select ARCH_SUPPORTS_NUMA_BALANCING
        select IRQ_WORK
+       select HAVE_ARCH_KASAN if SPARSEMEM_VMEMMAP
 
 config PPC_BOOK3E_64
        bool "Embedded processors"
--

2.5.0

maintainer
#24 lore raw
In reply to Aneesh Kumar K.V

2015-08-26 11:26 GMT+03:00 Aneesh Kumar K.V <[email protected]>:

Quoted 12 lines

Hi,

This patchset implements kernel address sanitizer for ppc64. Since ppc64 virtual address range is divided into different regions, we can't have one contigous area for the kasan shadow range. Hence we don't support the INLINE kasan instrumentation. With Outline instrumentation, we override the shadow_to_mem and mem_to_shadow callbacks, so that we map only the kernel linear range (ie, region with ID 0xc). For region with ID 0xd and 0xf (vmalloc and vmemmap ) we return the address of the zero page. This works because kasan doesn't track both vmemmap and vmalloc address.

Quoted 5 lines

Known issues:
* Kasan is not yet enabled for arch/powerpc/kvm
* kexec hang
* outline stack and global support

Is there any problem with globals or you just didn't try it yet? I think it should just work. You need only to add --param asan-globals=0 to KBUILD_CFLAGS_MODULE
to disable it for modules.

#25 lore raw
Quoted 19 lines · Andrey Ryabinin

2015-08-26 11:26 GMT+03:00 Aneesh Kumar K.V <[email protected]>:

Quoted 12 lines

Hi,

This patchset implements kernel address sanitizer for ppc64. Since ppc64 virtual address range is divided into different regions, we can't have one contigous area for the kasan shadow range. Hence we don't support the INLINE kasan instrumentation. With Outline instrumentation, we override the shadow_to_mem and mem_to_shadow callbacks, so that we map only the kernel linear range (ie, region with ID 0xc). For region with ID 0xd and 0xf (vmalloc and vmemmap ) we return the address of the zero page. This works because kasan doesn't track both vmemmap and vmalloc address.

Quoted 5 lines

Known issues:
* Kasan is not yet enabled for arch/powerpc/kvm
* kexec hang
* outline stack and global support

Quoted 4 lines

Is there any problem with globals or you just didn't try it yet? I think it should just work. You need only to add --param asan-globals=0 to KBUILD_CFLAGS_MODULE
to disable it for modules.

I am hitting BUG_ON in early vmalloc code. I still haven't got time to debug it further. Should get to that soon.

-aneesh

#26 lore raw

Hi Aneesh,

Are you still working on support for KASan for ppc64 ?

Thanks,
Christophe

Le 26/08/2015 à 19:14, Aneesh Kumar K.V a écrit :

Quoted 36 lines
Quoted 19 lines · Andrey Ryabinin

2015-08-26 11:26 GMT+03:00 Aneesh Kumar K.V <[email protected]>:

Quoted 12 lines

Hi,

This patchset implements kernel address sanitizer for ppc64. Since ppc64 virtual address range is divided into different regions, we can't have one contigous area for the kasan shadow range. Hence we don't support the INLINE kasan instrumentation. With Outline instrumentation, we override the shadow_to_mem and mem_to_shadow callbacks, so that we map only the kernel linear range (ie, region with ID 0xc). For region with ID 0xd and 0xf (vmalloc and vmemmap ) we return the address of the zero page. This works because kasan doesn't track both vmemmap and vmalloc address.

Quoted 5 lines

Known issues:
* Kasan is not yet enabled for arch/powerpc/kvm
* kexec hang
* outline stack and global support

Quoted 4 lines

Is there any problem with globals or you just didn't try it yet? I think it should just work. You need only to add --param asan-globals=0 to KBUILD_CFLAGS_MODULE
to disable it for modules.

I am hitting BUG_ON in early vmalloc code. I still haven't got time to debug it further. Should get to that soon.

-aneesh

_______________________________________________
Linuxppc-dev mailing list
[email protected]
https://lists.ozlabs.org/listinfo/linuxppc-dev

#27 lore raw
Quoted 4 lines · Christophe LEROY

Hi Aneesh,

Are you still working on support for KASan for ppc64 ?

Haven't got time to work on this. The hash memory layout makes it a bit complicated to implement this.

-aneesh

#28 lore raw

Le 06/07/2018 à 16:11, Aneesh Kumar K.V a écrit :

Quoted 10 lines
Quoted 4 lines · Christophe LEROY

Hi Aneesh,

Are you still working on support for KASan for ppc64 ?

Haven't got time to work on this. The hash memory layout makes it a bit complicated to implement this.

Ok, maybe would be easier to start with nohash.

Is there some literature somewhere about what an arch has to implement to use KAsan ?

Thanks,
Christophe