KASan ppc64 support
Subsystems (8)
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.cSignature · 1 line
2.5.0
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
@@ -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
PATCH v2 2/10 kasan: MODULE_VADDR is not available on all archs
In reply to Aneesh Kumar K.VConditionalize the check using #ifdef
--- mm/kasan/report.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
mm/kasan/report.c +8 -3
@@ -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
Andrey Ryabinin
maintainerRe: [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
Signed-off-by Aneesh Kumar K.V <[email protected]>--- mm/kasan/report.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)mm/kasan/report.c +0
-0@@ -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.
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
Signed-off-by Aneesh Kumar K.V <[email protected]>--- mm/kasan/report.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)mm/kasan/report.c +0
-0@@ -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
PATCH v2 3/10 kasan: Rename kasan_enabled to kasan_report_enabled
In reply to Aneesh Kumar K.VThe 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
@@ -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
@@ -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
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_enabledto properly reflect its function.Signed-off-by Aneesh Kumar K.V <[email protected]>
PATCH v2 4/10 kasan: Don't use kasan shadow pointer in generic functions
In reply to Aneesh Kumar K.VWe 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
@@ -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
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
@@ -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))
Andrey Ryabinin
maintainerRe: [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
aeb324e09d95c189eda4ce03790da94b535d1dfcAuthor: Aneesh Kumar K.V <[email protected]> Date: Fri Aug 14 12:28:58 2015 +0530kasan: Don't use kasan shadow pointer in generic functions
We can't use generic functions like
print_hex_dumpto 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.Signed-off-by Aneesh Kumar K.V <[email protected]>mm/kasan/report.c +0
-0@@ -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
-1char 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))
PATCH v2 5/10 kasan: Enable arch to hook into kasan callbacks.
In reply to Aneesh Kumar K.VWe 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
@@ -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
@@ -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
Andrey Ryabinin
maintainerRe: [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.
Signed-off-by Aneesh Kumar K.V <[email protected]>--- mm/kasan/kasan.c | 9 +++++++++ mm/kasan/kasan.h | 15 +++++++++++++++ 2 files changed, 24 insertions(+)mm/kasan/kasan.c +0
-0
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
-0shadow_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
-0kasan_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@@ -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
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.
Signed-off-by Aneesh Kumar K.V <[email protected]>--- mm/kasan/kasan.c | 9 +++++++++ mm/kasan/kasan.h | 15 +++++++++++++++ 2 files changed, 24 insertions(+)mm/kasan/kasan.c +0
-0@@ -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
-0shadow_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
-0kasan_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@@ -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_enabledWhy 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
PATCH v2 6/10 kasan: Allow arch to overrride kasan shadow offsets
In reply to Aneesh Kumar K.VSome 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
@@ -15,11 +15,14 @@ struct vm_struct;#include <asm/kasan.h> #include <linux/sched.h> +#ifndef kasan_mem_to_shadowstatic 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
@@ -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_memstatic 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
Andrey Ryabinin
maintainerRe: [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.
Signed-off-by Aneesh Kumar K.V <[email protected]>--- include/linux/kasan.h | 3 +++ mm/kasan/kasan.h | 3 +++ 2 files changed, 6 insertions(+)include/linux/kasan.h +0
-0
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_shadowstatic 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
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_memstatic 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
PATCH v2 7/10 kasan: Make INLINE KASan support arch selectable
In reply to Aneesh Kumar K.VSome 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
@@ -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
@@ -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
@@ -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
Andrey Ryabinin
maintainerRe: [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_INLINEso that we can disable inline support at config time.Signed-off-by Aneesh Kumar K.V <[email protected]>--- arch/x86/Kconfig | 1 + lib/Kconfig.kasan | 2 ++ scripts/Makefile.kasan | 28 ++++++++++++++-------------- 3 files changed, 17 insertions(+), 14 deletions(-)arch/x86/Kconfig +0
-0
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
-0select HAVE_ARCH_KGDB select HAVE_ARCH_KMEMCHECK select HAVE_ARCH_SECCOMP_FILTERlib/Kconfig.kasan +0
-0
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
-0help@@ -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 endchoicescripts/Makefile.kasan +10
-13@@ -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
-1ifneq ($(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
-1endif endif --2.5.0
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_INLINEso that we can disable inline support at config time.Signed-off-by Aneesh Kumar K.V <[email protected]>--- arch/x86/Kconfig | 1 + lib/Kconfig.kasan | 2 ++ scripts/Makefile.kasan | 28 ++++++++++++++-------------- 3 files changed, 17 insertions(+), 14 deletions(-)arch/x86/Kconfig +0
-0arch/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_VMEMMAPThis will not work because config
HAVE_ARCH_KASAN_INLINEis not defined. Instead of you can just add following in this file:
Quoted 4 lines
config
HAVE_ARCH_KASAN_INLINE
def_booly
depends on KASAN
Missed cherry-pick
lib/Kconfig.kasan +3 -0
@@ -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
-0select HAVE_ARCH_KGDB select HAVE_ARCH_KMEMCHECK select HAVE_ARCH_SECCOMP_FILTERlib/Kconfig.kasan +0
-0lib/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_INLINEQuoted 52 lines
lib/Kconfig.kasan +1
-0help@@ -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 endchoicescripts/Makefile.kasan +12
-14@@ -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
-0endif + endif +This removes stack and globals for
CONFIG_KASAN_OUTLINE=y. Why? Those are completely separate features. So this patch shouldn't touch thisMakefileat all.
Depends onHAVE_ARCH_KASAN_INLINEinCONFIG_KASAN_INLINEshould 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 ppc64Makefile:
CFLAGS_ARCH_KASAN:= --param asan-globals=0 --param asan-stack=0and add these flags to
CFLAGS_KASAN_MINIMALandCFLAGS_KASANin Makefile.kasan.Quoted 3 lines
scripts/Makefile.kasan +0
-0endif endif
-aneesh
PATCH v2 8/10 kasan: Update feature support file
In reply to Aneesh Kumar K.VNow 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
@@ -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
Signature · 1 line
2.5.0
PATCH v2 9/10 kasan: Prevent deadlock in kasan reporting
In reply to Aneesh Kumar K.VWe 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
@@ -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
Andrey Ryabinin
maintainerRe: [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_reportin 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 callingkasan_report_errorwithlock_reportspin lock held. To prevent this disable kasan reporting when we are priting error w.r.t kasan.Signed-off-by Aneesh Kumar K.V <[email protected]>--- mm/kasan/report.c | 10 ++++++++++ 1 file changed, 10 insertions(+)mm/kasan/report.c +10
-1@@ -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
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_reportin 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
PATCH v2 10/10 powerpc/mm: kasan: Add kasan support for ppc64
In reply to Aneesh Kumar K.VWe 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
Andrey Ryabinin
maintainerRe: [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@@ -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 mappingarch/powerpc/platforms/Kconfig.cputype +1
-1@@ -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
Andrey Ryabinin
maintainer2015-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_memandmem_to_shadowcallbacks, so that we map only the kernel linear range (ie, region with ID0xc). For region with ID0xdand0xf(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 forarch/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.
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_memandmem_to_shadowcallbacks, so that we map only the kernel linear range (ie, region with ID0xc). For region with ID0xdand0xf(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 forarch/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
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_memandmem_to_shadowcallbacks, so that we map only the kernel linear range (ie, region with ID0xc). For region with ID0xdand0xf(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 forarch/powerpc/kvm
* kexec hang
* outline stack and global supportQuoted 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_ONin 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
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
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