KASAN support for RISC-V
Subsystems (5)
v4 cover KASAN support for RISC-V
KASAN is an important runtime memory debugging feature in linux kernel which can detect use-after-free and out-of-bounds problems.
Changes in v2:
- Remove the porting of memmove and exclude the check instead.
- Fix some code noted by Christoph Hellwig
Changes in v3:
- Update the KASAN documentation to mention that riscv is supported.
Changes in v4:
- Correct the commit log
- Fix the bug reported by Greentime Hu
Nick Hu (3):
kasan: No KASAN's memmove check if archs don't have it.
riscv: Add KASAN support
kasan: Add riscv to KASAN documentation.
Documentation/dev-tools/kasan.rst | 4 +- arch/riscv/Kconfig | 1 + arch/riscv/include/asm/kasan.h | 27 ++++++++ arch/riscv/include/asm/pgtable-64.h | 5 ++ arch/riscv/include/asm/string.h | 9 +++ arch/riscv/kernel/head.S | 3 + arch/riscv/kernel/riscv_ksyms.c | 2 + arch/riscv/kernel/setup.c | 5 ++ arch/riscv/kernel/vmlinux.lds.S | 1 + arch/riscv/lib/memcpy.S | 5 +- arch/riscv/lib/memset.S | 5 +- arch/riscv/mm/Makefile | 6 ++ arch/riscv/mm/kasan_init.c | 104 ++++++++++++++++++++++++++++ mm/kasan/common.c | 2 + 14 files changed, 173 insertions(+), 6 deletions(-) create mode 100644 arch/riscv/include/asm/kasan.h create mode 100644 arch/riscv/mm/kasan_init.c
Signature · 7 lines
2.17.0 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
PATCH v4 1/3 kasan: No KASAN's memmove check if archs don't have it.
If archs don't have memmove then the C implementation from lib/string.c is used, and then it's instrumented by compiler. So there is no need to add KASAN's memmove to manual checks.
--- mm/kasan/common.c | 2 ++ 1 file changed, 2 insertions(+)
mm/kasan/common.c +2 -0
@@ -107,6 +107,7 @@ void *memset(void *addr, int c, size_t len)return __memset (addr, c, len); } +#ifdef __HAVE_ARCH_MEMMOVE#undef memmovevoid *memmove (void *dest,const void *src,size_t len) {
@@ -115,6 +116,7 @@ void *memmove(void *dest, const void *src, size_t len) return __memmove(dest, src, len); } +#endif #undef memcpyvoid *memcpy (void *dest,const void *src,size_t len)
Signature · 7 lines
2.17.0 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
Re: [PATCH v4 1/3] kasan: No KASAN's memmove check if archs don't have it.
Hello Andrey, Alexander, Dmitry,
Quoted 5 lines · Nick Hu
If archs don't have memmove then the C implementation from
lib/string.cis used, and then it's instrumented by compiler. So there is no need to add KASAN's memmove to manual checks.Signed-off-by Nick Hu <[email protected]>
If you're happy with this revision of this patch, could you please ack it so we can merge it as part of the RISC-V KASAN patch set?
Or if you'd prefer to take this patch yourself, please let me know.
- Paul
Quoted 33 lines
--- mm/kasan/common.c | 2 ++ 1 file changed, 2 insertions(+)mm/kasan/common.c +2
-0@@ -107,6 +107,7 @@ void *memset(void *addr, int c, size_t len)return __memset (addr, c, len); } +#ifdef __HAVE_ARCH_MEMMOVE#undef memmovevoid *memmove (void *dest,const void *src,size_t len) {@@ -115,6 +116,7 @@ void *memmove(void *dest, const void *src, size_t len) return __memmove(dest, src, len); } +#endif #undef memcpyvoid *memcpy (void *dest,const void *src,size_t len)Signature · 7 lines
2.17.0 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
- Paul
Re: [PATCH v4 1/3] kasan: No KASAN's memmove check if archs don't have it.
Quoted 9 lines · Paul Walmsley
Hello Andrey, Alexander, Dmitry,
Quoted 4 lines · Nick Hu
If archs don't have memmove then the C implementation from
lib/string.cis used, and then it's instrumented by compiler. So there is no need to add KASAN's memmove to manual checks.
Quoted 6 lines
Quoted 1 line
Signed-off-by Nick Hu <[email protected]>If you're happy with this revision of this patch, could you please ack it so we can merge it as part of the RISC-V KASAN patch set?
Or if you'd prefer to take this patch yourself, please let me know.
Hi Paul,
We don't have separate tree for kasan. Merging this via RISC-V tree should be fine.
Thanks
Quoted 38 lines
-
Quoted 28 lines
--- mm/kasan/common.c | 2 ++ 1 file changed, 2 insertions(+)mm/kasan/common.c +2
-1@@ -107,6 +107,7 @@ void *memset(void *addr, int c, size_t len)return __memset (addr, c, len); } +#ifdef __HAVE_ARCH_MEMMOVE#undef memmovevoid *memmove (void *dest,const void *src,size_t len) {@@ -115,6 +116,7 @@ void *memmove(void *dest, const void *src, size_t len) return __memmove(dest, src, len); } +#endif #undef memcpyvoid *memcpy (void *dest,const void *src,size_t len) --2.17.0
Quoted 5 lines
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv- Paul
Re: [PATCH v4 1/3] kasan: No KASAN's memmove check if archs don't have it.
In reply to Nick HuQuoted 6 lines · Nick Hu
If archs don't have memmove then the C implementation from
lib/string.cis used, and then it's instrumented by compiler. So there is no need to add KASAN's memmove to manual checks.Signed-off-by Nick Hu <[email protected]>---
PATCH v4 2/3 riscv: Add KASAN support
In reply to Nick HuThis patch ports the feature Kernel Address SANitizer (KASAN).
Note: The start address of shadow memory is at the beginning of kernel space, which is 2^64 - (2^39 / 2) in SV39. The size of the kernel space is 2^38 bytes so the size of shadow memory should be 2^38 / 8. Thus, the shadow memory would not overlap with the fixmap area.
There are currently two limitations in this port,
1. RV64 only: KASAN need large address space for extra shadow memory region.
2. KASAN can't debug the modules since the modules are allocated in VMALLOC area. We mapped the shadow memory, which corresponding to VMALLOC area, to the kasan_early_shadow_page because we don't have enough physical space for all the shadow memory corresponding to VMALLOC area.
--- arch/riscv/Kconfig | 1 + arch/riscv/include/asm/kasan.h | 27 ++++++++ arch/riscv/include/asm/pgtable-64.h | 5 ++ arch/riscv/include/asm/string.h | 9 +++ arch/riscv/kernel/head.S | 3 + arch/riscv/kernel/riscv_ksyms.c | 2 + arch/riscv/kernel/setup.c | 5 ++ arch/riscv/kernel/vmlinux.lds.S | 1 + arch/riscv/lib/memcpy.S | 5 +- arch/riscv/lib/memset.S | 5 +- arch/riscv/mm/Makefile | 6 ++ arch/riscv/mm/kasan_init.c | 104 ++++++++++++++++++++++++++++ 12 files changed, 169 insertions(+), 4 deletions(-) create mode 100644 arch/riscv/include/asm/kasan.h create mode 100644 arch/riscv/mm/kasan_init.c
arch/riscv/Kconfig +1 -0
@@ -61,6 +61,7 @@ config RISCV select SPARSEMEM_STATIC if 32BIT select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT if MMU select HAVE_ARCH_MMAP_RND_BITS + select HAVE_ARCH_KASAN if MMU && 64BIT config ARCH_MMAP_RND_BITS_MIN default 18 if 64BIT
arch/riscv/include/asm/kasan.h new file +27 -0
@@ -0,0 +1,27 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2019 Andes Technology Corporation */ + +#ifndef __ASM_KASAN_H +#define __ASM_KASAN_H + +#ifndef __ASSEMBLY__ + +#ifdef CONFIG_KASAN + +#include <asm/pgtable.h> + +#define KASAN_SHADOW_SCALE_SHIFT 3 + +#define KASAN_SHADOW_SIZE (UL(1) << (38 - KASAN_SHADOW_SCALE_SHIFT)) +#define KASAN_SHADOW_START 0xffffffc000000000/* 2^64 - 2^38 */ +#define KASAN_SHADOW_END (KASAN_SHADOW_START + KASAN_SHADOW_SIZE) + +#define KASAN_SHADOW_OFFSET (KASAN_SHADOW_END - (1ULL << \ + (64 - KASAN_SHADOW_SCALE_SHIFT))) + +void kasan_init (void ); +asmlinkage void kasan_early_init(void); + +#endif +#endif +#endif /* __ASM_KASAN_H */
arch/riscv/include/asm/pgtable-64.h +5 -0
@@ -59,6 +59,11 @@ static inline unsigned long pud_page_vaddr(pud_t pud)return (unsigned long )pfn_to_virt (pud_val (pud) >> _PAGE_PFN_SHIFT); } +static inline struct page *pud_page (pud_t pud) +{ +return pfn_to_page (pud_val (pud) >> _PAGE_PFN_SHIFT); +} +#define pmd_index (addr) (((addr) >> PMD_SHIFT) & (PTRS_PER_PMD - 1))static inline pmd_t *pmd_offset (pud_t *pud,unsigned long addr)
arch/riscv/include/asm/string.h +9 -0
@@ -11,8 +11,17 @@#define __HAVE_ARCH_MEMSET extern asmlinkage void *memset(void *, int, size_t); +extern asmlinkage void *__memset(void *, int, size_t);#define __HAVE_ARCH_MEMCPY extern asmlinkage void *memcpy(void *, const void *, size_t); +extern asmlinkage void *__memcpy(void *, const void *, size_t); +/* For those files which don't want to check by kasan. */ +#if defined(CONFIG_KASAN ) && !defined(__SANITIZE_ADDRESS__) + +#define memcpy (dst, src, len) __memcpy(dst, src, len) +#define memset (s, c, n) __memset(s, c, n) + +#endif #endif /* _ASM_RISCV_STRING_H */
arch/riscv/kernel/head.S +3 -0
@@ -102,6 +102,9 @@ clear_bss_done: sw zero, TASK_TI_CPU(tp) la sp, init_thread_union + THREAD_SIZE +#ifdef CONFIG_KASAN + call kasan_early_init +#endif /* Start the kernel */ call parse_dtb tail start_kernel
arch/riscv/kernel/riscv_ksyms.c +2 -0
@@ -14,3 +14,5 @@ EXPORT_SYMBOL(__asm_copy_to_user);EXPORT_SYMBOL (__asm_copy_from_user);EXPORT_SYMBOL (memset);EXPORT_SYMBOL (memcpy); +EXPORT_SYMBOL (__memset); +EXPORT_SYMBOL (__memcpy);
arch/riscv/kernel/setup.c +5 -0
@@ -23,6 +23,7 @@#include <asm/smp.h> #include <asm/tlbflush.h> #include <asm/thread_info.h> +#include <asm/kasan.h> #ifdef CONFIG_DUMMY_CONSOLE struct screen_info screen_info = {
@@ -70,6 +71,10 @@ void __init setup_arch(char **cmdline_p)swiotlb_init (1 );#endif +#ifdef CONFIG_KASAN +kasan_init (); +#endif +#ifdef CONFIG_SMP setup_smp ();#endif
arch/riscv/kernel/vmlinux.lds.S +1 -0
@@ -46,6 +46,7 @@ SECTIONS KPROBES_TEXT ENTRY_TEXT IRQENTRY_TEXT + SOFTIRQENTRY_TEXT *(.fixup) _etext = .; }
arch/riscv/lib/memcpy.S +3 -2
@@ -7,7 +7,8 @@ #include <asm/asm.h> /* void *memcpy(void *, const void *, size_t) */ -ENTRY(memcpy) +ENTRY(__memcpy) +WEAK(memcpy) move t6, a0 /* Preserve return value */ /* Defer to byte-oriented copy for small sizes */
@@ -104,4 +105,4 @@ ENTRY(memcpy) bltu a1, a3, 5b 6: ret -END(memcpy) +END(__memcpy)
arch/riscv/lib/memset.S +3 -2
@@ -8,7 +8,8 @@ #include <asm/asm.h> /* void *memset(void *, int, size_t) */ -ENTRY(memset) +ENTRY(__memset) +WEAK(memset) move t0, a0 /* Preserve return value */ /* Defer to byte-oriented fill for small sizes */
@@ -109,4 +110,4 @@ ENTRY(memset) bltu t0, a3, 5b 6: ret -END(memset) +END(__memset)
arch/riscv/mm/Makefile +6 -0
@@ -17,3 +17,9 @@ ifeq ($(CONFIG_MMU),y) obj-$(CONFIG_SMP) += tlbflush.o endif obj-$(CONFIG_HUGETLB_PAGE) += hugetlbpage.o +obj-$(CONFIG_KASAN) += kasan_init.o + +ifdef CONFIG_KASAN +KASAN_SANITIZE_kasan_init.o := n +KASAN_SANITIZE_init.o := n +endif
arch/riscv/mm/kasan_init.c new file +104 -0
104 lines not on this page · raw message
Signature · 7 lines
2.17.0 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
PATCH v4 3/3 kasan: Add riscv to KASAN documentation.
In reply to Nick HuAdd riscv to the KASAN documentation to mention that riscv is supporting generic kasan now.
--- Documentation/dev-tools/kasan.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Documentation/dev-tools/kasan.rst +2 -2
@@ -21,8 +21,8 @@ global variables yet. Tag-based KASAN is only supported in Clang and requires version 7.0.0 or later. -Currently generic KASAN is supported for the x86_64, arm64, xtensa and s390 -architectures, and tag-based KASAN is supported only for arm64. +Currently generic KASAN is supported for the x86_64, arm64, xtensa, s390 and +riscv architectures, and tag-based KASAN is supported only for arm64. Usage -----
Signature · 1 line
2.17.0
Nick Hu <[email protected]> 於 2019年10月28日 週一 上午10:41寫道:
Quoted 29 lines
KASAN is an important runtime memory debugging feature in linux kernel which can detect use-after-free and out-of-bounds problems.
Changes in v2:
- Remove the porting of memmove and exclude the check instead.
- Fix some code noted by Christoph HellwigChanges in v3:
- Update the KASAN documentation to mention that riscv is supported.Changes in v4:
- Correct the commit log
- Fix the bug reported by Greentime HuNick Hu (3):
kasan: No KASAN's memmove check if archs don't have it.
riscv: Add KASAN support
kasan: Add riscv to KASAN documentation.Documentation/dev-tools/kasan.rst | 4 +- arch/riscv/Kconfig | 1 + arch/riscv/include/asm/kasan.h | 27 ++++++++ arch/riscv/include/asm/pgtable-64.h | 5 ++ arch/riscv/include/asm/string.h | 9 +++ arch/riscv/kernel/head.S | 3 + arch/riscv/kernel/riscv_ksyms.c | 2 + arch/riscv/kernel/setup.c | 5 ++ arch/riscv/kernel/vmlinux.lds.S | 1 +
Quoted 9 lines
arch/riscv/lib/memcpy.S | 5 +- arch/riscv/lib/memset.S | 5 +- arch/riscv/mm/Makefile | 6 ++ arch/riscv/mm/kasan_init.c | 104 ++++++++++++++++++++++++++++ mm/kasan/common.c | 2 + 14 files changed, 173 insertions(+), 6 deletions(-) create mode 100644 arch/riscv/include/asm/kasan.h create mode 100644 arch/riscv/mm/kasan_init.c
Hi Nick,
I have tested KASAN feature with test_kasan.ko based on commit cd9e72b80090a8cd7d84a47a30a06fa92ff277d1 (tag: riscv/for-v5.4-rc3) and it passed in Qemu and Unleashed board.
Thank you for fixing the bug. :)