2019-08-07 07:50:56

by Nick Hu

[permalink] [raw]
Subject: [PATCH 0/2] 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.

There are two patches in this letter:
1. Porting the memmove string operation.
2. Porting the feature KASAN.

Nick Hu (2):
riscv: Add memmove string operation.
riscv: Add KASAN support

arch/riscv/Kconfig | 2 +
arch/riscv/include/asm/kasan.h | 26 +++++++++
arch/riscv/include/asm/pgtable-64.h | 5 ++
arch/riscv/include/asm/string.h | 10 ++++
arch/riscv/kernel/head.S | 3 +
arch/riscv/kernel/riscv_ksyms.c | 4 ++
arch/riscv/kernel/setup.c | 9 +++
arch/riscv/kernel/vmlinux.lds.S | 1 +
arch/riscv/lib/Makefile | 1 +
arch/riscv/lib/memcpy.S | 5 +-
arch/riscv/lib/memmove.S | 64 ++++++++++++++++++++++
arch/riscv/lib/memset.S | 5 +-
arch/riscv/mm/Makefile | 6 ++
arch/riscv/mm/kasan_init.c | 102 +++++++++++++++++++++++++++++++++++
14 files changed, 239 insertions(+), 4 deletions(-)
create mode 100644 arch/riscv/include/asm/kasan.h
create mode 100644 arch/riscv/lib/memmove.S
create mode 100644 arch/riscv/mm/kasan_init.c


2019-08-07 07:52:11

by Nick Hu

[permalink] [raw]
Subject: [PATCH 2/2] riscv: Add KASAN support

This 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.

Signed-off-by: Nick Hu <[email protected]>
---
arch/riscv/Kconfig | 2 +
arch/riscv/include/asm/kasan.h | 26 +++++++++
arch/riscv/include/asm/pgtable-64.h | 5 ++
arch/riscv/include/asm/string.h | 7 +++
arch/riscv/kernel/head.S | 3 +
arch/riscv/kernel/riscv_ksyms.c | 3 +
arch/riscv/kernel/setup.c | 9 +++
arch/riscv/kernel/vmlinux.lds.S | 1 +
arch/riscv/lib/memcpy.S | 5 +-
arch/riscv/lib/memmove.S | 5 +-
arch/riscv/lib/memset.S | 5 +-
arch/riscv/mm/Makefile | 6 ++
arch/riscv/mm/kasan_init.c | 102 +++++++++++++++++++++++++++++++++++
13 files changed, 173 insertions(+), 6 deletions(-)
create mode 100644 arch/riscv/include/asm/kasan.h
create mode 100644 arch/riscv/mm/kasan_init.c

diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
index 59a4727..4878b7a 100644
--- a/arch/riscv/Kconfig
+++ b/arch/riscv/Kconfig
@@ -54,6 +54,8 @@ config RISCV
select EDAC_SUPPORT
select ARCH_HAS_GIGANTIC_PAGE
select ARCH_WANT_HUGE_PMD_SHARE if 64BIT
+ select GENERIC_STRNCPY_FROM_USER if KASAN
+ select HAVE_ARCH_KASAN if MMU

config MMU
def_bool y
diff --git a/arch/riscv/include/asm/kasan.h b/arch/riscv/include/asm/kasan.h
new file mode 100644
index 0000000..e0c1f27
--- /dev/null
+++ b/arch/riscv/include/asm/kasan.h
@@ -0,0 +1,26 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#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
diff --git a/arch/riscv/include/asm/pgtable-64.h b/arch/riscv/include/asm/pgtable-64.h
index 7df8daa..777a1dd 100644
--- a/arch/riscv/include/asm/pgtable-64.h
+++ b/arch/riscv/include/asm/pgtable-64.h
@@ -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)
diff --git a/arch/riscv/include/asm/string.h b/arch/riscv/include/asm/string.h
index 11210f1..ab90f44 100644
--- a/arch/riscv/include/asm/string.h
+++ b/arch/riscv/include/asm/string.h
@@ -11,11 +11,18 @@

#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);

#define __HAVE_ARCH_MEMMOVE
extern asmlinkage void *memmove(void *, const void *, size_t);
+extern asmlinkage void *__memmove(void *, const void *, size_t);
+
+#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 /* _ASM_RISCV_STRING_H */
diff --git a/arch/riscv/kernel/head.S b/arch/riscv/kernel/head.S
index 0f1ba17..2f7bc8b 100644
--- a/arch/riscv/kernel/head.S
+++ b/arch/riscv/kernel/head.S
@@ -97,6 +97,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
diff --git a/arch/riscv/kernel/riscv_ksyms.c b/arch/riscv/kernel/riscv_ksyms.c
index ffabaf1..ad9f007 100644
--- a/arch/riscv/kernel/riscv_ksyms.c
+++ b/arch/riscv/kernel/riscv_ksyms.c
@@ -15,3 +15,6 @@
EXPORT_SYMBOL(memset);
EXPORT_SYMBOL(memcpy);
EXPORT_SYMBOL(memmove);
+EXPORT_SYMBOL(__memset);
+EXPORT_SYMBOL(__memcpy);
+EXPORT_SYMBOL(__memmove);
diff --git a/arch/riscv/kernel/setup.c b/arch/riscv/kernel/setup.c
index a990a6c..9954c0b 100644
--- a/arch/riscv/kernel/setup.c
+++ b/arch/riscv/kernel/setup.c
@@ -24,6 +24,10 @@
#include <asm/tlbflush.h>
#include <asm/thread_info.h>

+#ifdef CONFIG_KASAN
+#include <asm/kasan.h>
+#endif
+
#ifdef CONFIG_DUMMY_CONSOLE
struct screen_info screen_info = {
.orig_video_lines = 30,
@@ -64,12 +68,17 @@ void __init setup_arch(char **cmdline_p)

setup_bootmem();
paging_init();
+
unflatten_device_tree();

#ifdef CONFIG_SWIOTLB
swiotlb_init(1);
#endif

+#ifdef CONFIG_KASAN
+ kasan_init();
+#endif
+
#ifdef CONFIG_SMP
setup_smp();
#endif
diff --git a/arch/riscv/kernel/vmlinux.lds.S b/arch/riscv/kernel/vmlinux.lds.S
index 23cd1a9..9700980 100644
--- a/arch/riscv/kernel/vmlinux.lds.S
+++ b/arch/riscv/kernel/vmlinux.lds.S
@@ -46,6 +46,7 @@ SECTIONS
KPROBES_TEXT
ENTRY_TEXT
IRQENTRY_TEXT
+ SOFTIRQENTRY_TEXT
*(.fixup)
_etext = .;
}
diff --git a/arch/riscv/lib/memcpy.S b/arch/riscv/lib/memcpy.S
index b4c4778..51ab716 100644
--- a/arch/riscv/lib/memcpy.S
+++ b/arch/riscv/lib/memcpy.S
@@ -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)
diff --git a/arch/riscv/lib/memmove.S b/arch/riscv/lib/memmove.S
index 3657a06..ef8ba3c 100644
--- a/arch/riscv/lib/memmove.S
+++ b/arch/riscv/lib/memmove.S
@@ -3,7 +3,8 @@
#include <linux/linkage.h>
#include <asm/asm.h>

-ENTRY(memmove)
+ENTRY(__memmove)
+WEAK(memmove)
move t0, a0
move t1, a1

@@ -60,4 +61,4 @@ exit_memcpy:
move a1, t1
ret

-END(memmove)
+END(__memmove)
diff --git a/arch/riscv/lib/memset.S b/arch/riscv/lib/memset.S
index 5a7386b..34c5360 100644
--- a/arch/riscv/lib/memset.S
+++ b/arch/riscv/lib/memset.S
@@ -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)
diff --git a/arch/riscv/mm/Makefile b/arch/riscv/mm/Makefile
index 74055e1..cabe179 100644
--- a/arch/riscv/mm/Makefile
+++ b/arch/riscv/mm/Makefile
@@ -14,3 +14,9 @@ obj-y += context.o
obj-y += sifive_l2_cache.o

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
diff --git a/arch/riscv/mm/kasan_init.c b/arch/riscv/mm/kasan_init.c
new file mode 100644
index 0000000..4b7830e
--- /dev/null
+++ b/arch/riscv/mm/kasan_init.c
@@ -0,0 +1,102 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/pfn.h>
+#include <linux/init_task.h>
+#include <linux/kasan.h>
+#include <linux/kernel.h>
+#include <linux/memblock.h>
+#include <asm/tlbflush.h>
+#include <asm/pgtable.h>
+#include <asm/fixmap.h>
+
+extern pgd_t early_pg_dir[PTRS_PER_PGD];
+asmlinkage void __init kasan_early_init(void)
+{
+ uintptr_t i;
+ pgd_t *pgd = early_pg_dir + pgd_index(KASAN_SHADOW_START);
+
+ for (i = 0; i < PTRS_PER_PTE; ++i)
+ set_pte(kasan_early_shadow_pte + i,
+ mk_pte(virt_to_page(kasan_early_shadow_page),
+ PAGE_KERNEL));
+
+ for (i = 0; i < PTRS_PER_PMD; ++i)
+ set_pmd(kasan_early_shadow_pmd + i,
+ pfn_pmd(PFN_DOWN(__pa((uintptr_t)kasan_early_shadow_pte)),
+ __pgprot(_PAGE_TABLE)));
+
+ for (i = KASAN_SHADOW_START; i < KASAN_SHADOW_END;
+ i += PGDIR_SIZE, ++pgd)
+ set_pgd(pgd,
+ pfn_pgd(PFN_DOWN(__pa(((uintptr_t)kasan_early_shadow_pmd))),
+ __pgprot(_PAGE_TABLE)));
+
+ // init for swapper_pg_dir
+ pgd = pgd_offset_k(KASAN_SHADOW_START);
+
+ for (i = KASAN_SHADOW_START; i < KASAN_SHADOW_END;
+ i += PGDIR_SIZE, ++pgd)
+ set_pgd(pgd,
+ pfn_pgd(PFN_DOWN(__pa(((uintptr_t)kasan_early_shadow_pmd))),
+ __pgprot(_PAGE_TABLE)));
+}
+
+static void __init populate(void *start, void *end)
+{
+ unsigned long i;
+ unsigned long vaddr = (unsigned long)start & PAGE_MASK;
+ unsigned long vend = PAGE_ALIGN((unsigned long)end);
+ unsigned long n_pages = (vend - vaddr) / PAGE_SIZE;
+ unsigned long n_pmds =
+ (n_pages % PTRS_PER_PTE) ? n_pages / PTRS_PER_PTE + 1 :
+ n_pages / PTRS_PER_PTE;
+ pgd_t *pgd = pgd_offset_k(vaddr);
+ pmd_t *pmd = memblock_alloc(n_pmds * sizeof(pmd_t), PAGE_SIZE);
+ pte_t *pte = memblock_alloc(n_pages * sizeof(pte_t), PAGE_SIZE);
+
+ for (i = 0; i < n_pages; i++) {
+ phys_addr_t phys = memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
+
+ set_pte(pte + i, pfn_pte(PHYS_PFN(phys), PAGE_KERNEL));
+ }
+
+ for (i = 0; i < n_pages; ++pmd, i += PTRS_PER_PTE)
+ set_pmd(pmd, pfn_pmd(PFN_DOWN(__pa((uintptr_t)(pte + i))),
+ __pgprot(_PAGE_TABLE)));
+
+ for (i = vaddr; i < vend; i += PGDIR_SIZE, ++pgd)
+ set_pgd(pgd, pfn_pgd(PFN_DOWN(__pa(((uintptr_t)pmd))),
+ __pgprot(_PAGE_TABLE)));
+
+ flush_tlb_all();
+ memset(start, 0, end - start);
+}
+
+void __init kasan_init(void)
+{
+ struct memblock_region *reg;
+ unsigned long i;
+
+ kasan_populate_early_shadow((void *)KASAN_SHADOW_START,
+ (void *)kasan_mem_to_shadow((void *)VMALLOC_END));
+
+ for_each_memblock(memory, reg) {
+ void *start = (void *)__va(reg->base);
+ void *end = (void *)__va(reg->base + reg->size);
+
+ if (start >= end)
+ break;
+
+ populate(kasan_mem_to_shadow(start),
+ kasan_mem_to_shadow(end));
+ };
+
+ for (i = 0; i < PTRS_PER_PTE; i++)
+ set_pte(&kasan_early_shadow_pte[i],
+ mk_pte(virt_to_page(kasan_early_shadow_page),
+ __pgprot(_PAGE_PRESENT | _PAGE_READ | _PAGE_ACCESSED)));
+
+ memset(kasan_early_shadow_page, 0, PAGE_SIZE);
+
+ init_task.kasan_depth = 0;
+}
--
1.7.1

2019-08-07 08:16:41

by Nick Hu

[permalink] [raw]
Subject: [PATCH 1/2] riscv: Add memmove string operation.

There are some features which need this string operation for compilation,
like KASAN. So the purpose of this porting is for the features like KASAN
which cannot be compiled without it.

KASAN's string operations would replace the original string operations and
call for the architecture defined string operations. Since we don't have
this in current kernel, this patch provides the implementation.

This porting refers to the 'arch/nds32/lib/memmove.S'.

Signed-off-by: Nick Hu <[email protected]>
---
arch/riscv/include/asm/string.h | 3 ++
arch/riscv/kernel/riscv_ksyms.c | 1 +
arch/riscv/lib/Makefile | 1 +
arch/riscv/lib/memmove.S | 63 +++++++++++++++++++++++++++++++++++++++
4 files changed, 68 insertions(+), 0 deletions(-)
create mode 100644 arch/riscv/lib/memmove.S

diff --git a/arch/riscv/include/asm/string.h b/arch/riscv/include/asm/string.h
index 1b5d445..11210f1 100644
--- a/arch/riscv/include/asm/string.h
+++ b/arch/riscv/include/asm/string.h
@@ -15,4 +15,7 @@
#define __HAVE_ARCH_MEMCPY
extern asmlinkage void *memcpy(void *, const void *, size_t);

+#define __HAVE_ARCH_MEMMOVE
+extern asmlinkage void *memmove(void *, const void *, size_t);
+
#endif /* _ASM_RISCV_STRING_H */
diff --git a/arch/riscv/kernel/riscv_ksyms.c b/arch/riscv/kernel/riscv_ksyms.c
index 4800cf7..ffabaf1 100644
--- a/arch/riscv/kernel/riscv_ksyms.c
+++ b/arch/riscv/kernel/riscv_ksyms.c
@@ -14,3 +14,4 @@
EXPORT_SYMBOL(__asm_copy_from_user);
EXPORT_SYMBOL(memset);
EXPORT_SYMBOL(memcpy);
+EXPORT_SYMBOL(memmove);
diff --git a/arch/riscv/lib/Makefile b/arch/riscv/lib/Makefile
index 8e364eb..9a4d5b3 100644
--- a/arch/riscv/lib/Makefile
+++ b/arch/riscv/lib/Makefile
@@ -2,6 +2,7 @@
lib-y += delay.o
lib-y += memcpy.o
lib-y += memset.o
+lib-y += memmove.o
lib-y += uaccess.o

lib-$(CONFIG_64BIT) += tishift.o
diff --git a/arch/riscv/lib/memmove.S b/arch/riscv/lib/memmove.S
new file mode 100644
index 0000000..3657a06
--- /dev/null
+++ b/arch/riscv/lib/memmove.S
@@ -0,0 +1,63 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#include <linux/linkage.h>
+#include <asm/asm.h>
+
+ENTRY(memmove)
+ move t0, a0
+ move t1, a1
+
+ beq a0, a1, exit_memcpy
+ beqz a2, exit_memcpy
+ srli t2, a2, 0x2
+
+ slt t3, a0, a1
+ beqz t3, do_reverse
+
+ andi a2, a2, 0x3
+ li t4, 1
+ beqz t2, byte_copy
+
+word_copy:
+ lw t3, 0(a1)
+ addi t2, t2, -1
+ addi a1, a1, 4
+ sw t3, 0(a0)
+ addi a0, a0, 4
+ bnez t2, word_copy
+ beqz a2, exit_memcpy
+ j byte_copy
+
+do_reverse:
+ add a0, a0, a2
+ add a1, a1, a2
+ andi a2, a2, 0x3
+ li t4, -1
+ beqz t2, reverse_byte_copy
+
+reverse_word_copy:
+ addi a1, a1, -4
+ addi t2, t2, -1
+ lw t3, 0(a1)
+ addi a0, a0, -4
+ sw t3, 0(a0)
+ bnez t2, reverse_word_copy
+ beqz a2, exit_memcpy
+
+reverse_byte_copy:
+ addi a0, a0, -1
+ addi a1, a1, -1
+byte_copy:
+ lb t3, 0(a1)
+ addi a2, a2, -1
+ sb t3, 0(a0)
+ add a1, a1, t4
+ add a0, a0, t4
+ bnez a2, byte_copy
+
+exit_memcpy:
+ move a0, t0
+ move a1, t1
+ ret
+
+END(memmove)
--
1.7.1

2019-08-12 15:05:58

by Christoph Hellwig

[permalink] [raw]
Subject: Re: [PATCH 1/2] riscv: Add memmove string operation.

On Wed, Aug 07, 2019 at 03:19:14PM +0800, Nick Hu wrote:
> There are some features which need this string operation for compilation,
> like KASAN. So the purpose of this porting is for the features like KASAN
> which cannot be compiled without it.
>
> KASAN's string operations would replace the original string operations and
> call for the architecture defined string operations. Since we don't have
> this in current kernel, this patch provides the implementation.
>
> This porting refers to the 'arch/nds32/lib/memmove.S'.

This looks sensible to me, although my stringop asm is rather rusty,
so just an ack and not a real review-by:

Acked-by: Christoph Hellwig <[email protected]>

2019-08-12 15:12:08

by Christoph Hellwig

[permalink] [raw]
Subject: Re: [PATCH 2/2] riscv: Add KASAN support

> 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.

How do other architectures solve this problem?

> @@ -54,6 +54,8 @@ config RISCV
> select EDAC_SUPPORT
> select ARCH_HAS_GIGANTIC_PAGE
> select ARCH_WANT_HUGE_PMD_SHARE if 64BIT
> + select GENERIC_STRNCPY_FROM_USER if KASAN

Is there any reason why we can't always enabled this? Also just
enabling the generic efficient strncpy_from_user should probably be
a separate patch.

> + select HAVE_ARCH_KASAN if MMU

Based on your cover letter this should be if MMU && 64BIT

> #define __HAVE_ARCH_MEMCPY
> extern asmlinkage void *memcpy(void *, const void *, size_t);
> +extern asmlinkage void *__memcpy(void *, const void *, size_t);
>
> #define __HAVE_ARCH_MEMMOVE
> extern asmlinkage void *memmove(void *, const void *, size_t);
> +extern asmlinkage void *__memmove(void *, const void *, size_t);
> +
> +#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)

This looks weird and at least needs a very good comment. Also
with this we effectively don't need the non-prefixed prototypes
anymore. Also you probably want to split the renaming of the mem*
routines into a separate patch with a proper changelog.

> #include <asm/tlbflush.h>
> #include <asm/thread_info.h>
>
> +#ifdef CONFIG_KASAN
> +#include <asm/kasan.h>
> +#endif

Any good reason to not just always include the header?

> +
> #ifdef CONFIG_DUMMY_CONSOLE
> struct screen_info screen_info = {
> .orig_video_lines = 30,
> @@ -64,12 +68,17 @@ void __init setup_arch(char **cmdline_p)
>
> setup_bootmem();
> paging_init();
> +
> unflatten_device_tree();

spurious whitespace change.

> diff --git a/arch/riscv/kernel/vmlinux.lds.S b/arch/riscv/kernel/vmlinux.lds.S
> index 23cd1a9..9700980 100644
> --- a/arch/riscv/kernel/vmlinux.lds.S
> +++ b/arch/riscv/kernel/vmlinux.lds.S
> @@ -46,6 +46,7 @@ SECTIONS
> KPROBES_TEXT
> ENTRY_TEXT
> IRQENTRY_TEXT
> + SOFTIRQENTRY_TEXT

Hmm. What is the relation to kasan here? Maybe we should add this
separately with a good changelog?

> +++ b/arch/riscv/mm/kasan_init.c
> @@ -0,0 +1,102 @@
> +// SPDX-License-Identifier: GPL-2.0

This probably also wants a copyright statement.

> + // init for swapper_pg_dir

Please use /* */ style comments.

2019-08-13 23:53:08

by Palmer Dabbelt

[permalink] [raw]
Subject: Re: [PATCH 1/2] riscv: Add memmove string operation.

On Mon, 12 Aug 2019 08:04:46 PDT (-0700), Christoph Hellwig wrote:
> On Wed, Aug 07, 2019 at 03:19:14PM +0800, Nick Hu wrote:
>> There are some features which need this string operation for compilation,
>> like KASAN. So the purpose of this porting is for the features like KASAN
>> which cannot be compiled without it.
>>
>> KASAN's string operations would replace the original string operations and
>> call for the architecture defined string operations. Since we don't have
>> this in current kernel, this patch provides the implementation.
>>
>> This porting refers to the 'arch/nds32/lib/memmove.S'.
>
> This looks sensible to me, although my stringop asm is rather rusty,
> so just an ack and not a real review-by:
>
> Acked-by: Christoph Hellwig <[email protected]>

FWIW, we just write this in C everywhere else and rely on the compiler to
unroll the loops. I always prefer C to assembly when possible, so I'd prefer
if we just adopt the string code from newlib. We have a RISC-V-specific memcpy
in there, but just use the generic memmove.

Maybe the best bet here would be to adopt the newlib memcpy/memmove as generic
Linux functions? They're both in C so they should be fine, and they both look
faster than what's in lib/string.c. Then everyone would benefit and we don't
need this tricky RISC-V assembly. Also, from the look of it the newlib code is
faster because the inner loop is unrolled.

2019-08-14 02:23:51

by Paul Walmsley

[permalink] [raw]
Subject: Re: [PATCH 1/2] riscv: Add memmove string operation.

On Tue, 13 Aug 2019, Palmer Dabbelt wrote:

> On Mon, 12 Aug 2019 08:04:46 PDT (-0700), Christoph Hellwig wrote:
> > On Wed, Aug 07, 2019 at 03:19:14PM +0800, Nick Hu wrote:
> > > There are some features which need this string operation for compilation,
> > > like KASAN. So the purpose of this porting is for the features like KASAN
> > > which cannot be compiled without it.
> > >
> > > KASAN's string operations would replace the original string operations and
> > > call for the architecture defined string operations. Since we don't have
> > > this in current kernel, this patch provides the implementation.
> > >
> > > This porting refers to the 'arch/nds32/lib/memmove.S'.
> >
> > This looks sensible to me, although my stringop asm is rather rusty,
> > so just an ack and not a real review-by:
> >
> > Acked-by: Christoph Hellwig <[email protected]>
>
> FWIW, we just write this in C everywhere else and rely on the compiler to
> unroll the loops. I always prefer C to assembly when possible, so I'd prefer
> if we just adopt the string code from newlib. We have a RISC-V-specific
> memcpy in there, but just use the generic memmove.
>
> Maybe the best bet here would be to adopt the newlib memcpy/memmove as generic
> Linux functions? They're both in C so they should be fine, and they both look
> faster than what's in lib/string.c. Then everyone would benefit and we don't
> need this tricky RISC-V assembly. Also, from the look of it the newlib code
> is faster because the inner loop is unrolled.

There's a generic memmove implementation in the kernel already:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/linux/string.h#n362

Nick, could you tell us more about why the generic memmove() isn't
suitable?


- Paul

2019-08-14 03:29:58

by Nick Hu

[permalink] [raw]
Subject: Re: [PATCH 1/2] riscv: Add memmove string operation.

On Wed, Aug 14, 2019 at 10:22:15AM +0800, Paul Walmsley wrote:
> On Tue, 13 Aug 2019, Palmer Dabbelt wrote:
>
> > On Mon, 12 Aug 2019 08:04:46 PDT (-0700), Christoph Hellwig wrote:
> > > On Wed, Aug 07, 2019 at 03:19:14PM +0800, Nick Hu wrote:
> > > > There are some features which need this string operation for compilation,
> > > > like KASAN. So the purpose of this porting is for the features like KASAN
> > > > which cannot be compiled without it.
> > > >
> > > > KASAN's string operations would replace the original string operations and
> > > > call for the architecture defined string operations. Since we don't have
> > > > this in current kernel, this patch provides the implementation.
> > > >
> > > > This porting refers to the 'arch/nds32/lib/memmove.S'.
> > >
> > > This looks sensible to me, although my stringop asm is rather rusty,
> > > so just an ack and not a real review-by:
> > >
> > > Acked-by: Christoph Hellwig <[email protected]>
> >
> > FWIW, we just write this in C everywhere else and rely on the compiler to
> > unroll the loops. I always prefer C to assembly when possible, so I'd prefer
> > if we just adopt the string code from newlib. We have a RISC-V-specific
> > memcpy in there, but just use the generic memmove.
> >
> > Maybe the best bet here would be to adopt the newlib memcpy/memmove as generic
> > Linux functions? They're both in C so they should be fine, and they both look
> > faster than what's in lib/string.c. Then everyone would benefit and we don't
> > need this tricky RISC-V assembly. Also, from the look of it the newlib code
> > is faster because the inner loop is unrolled.
>
> There's a generic memmove implementation in the kernel already:
>
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/linux/string.h#n362
>
> Nick, could you tell us more about why the generic memmove() isn't
> suitable?
>
>
> - Paul

Hi Paul,

KASAN has its own string operations(memcpy/memmove/memset) because it needs to
hook some code to check memory region. It would undefined the original string
operations and called the string operations with the prefix '__'. But the
generic string operations didn't declare with the prefix. Other archs with
KASAN support like arm64 and xtensa all have their own string operations and
defined with the prefix.

Nick

2019-08-14 07:47:50

by Nick Hu

[permalink] [raw]
Subject: Re: [PATCH 2/2] riscv: Add KASAN support

Hi Christoph,

Thanks for your reply. I will answer one by one.

Hi Alexander,

Would you help me for the question about SOFTIRQENTRY_TEXT?

On Mon, Aug 12, 2019 at 11:10:50PM +0800, Christoph Hellwig wrote:
> > 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.
>
> How do other architectures solve this problem?
>
Other archs like arm64 and x86 allocate modules in their module region.

> > @@ -54,6 +54,8 @@ config RISCV
> > select EDAC_SUPPORT
> > select ARCH_HAS_GIGANTIC_PAGE
> > select ARCH_WANT_HUGE_PMD_SHARE if 64BIT
> > + select GENERIC_STRNCPY_FROM_USER if KASAN
>
> Is there any reason why we can't always enabled this? Also just
> enabling the generic efficient strncpy_from_user should probably be
> a separate patch.
>
You're right, always enable it would be better.

> > + select HAVE_ARCH_KASAN if MMU
>
> Based on your cover letter this should be if MMU && 64BIT
>
> > #define __HAVE_ARCH_MEMCPY
> > extern asmlinkage void *memcpy(void *, const void *, size_t);
> > +extern asmlinkage void *__memcpy(void *, const void *, size_t);
> >
> > #define __HAVE_ARCH_MEMMOVE
> > extern asmlinkage void *memmove(void *, const void *, size_t);
> > +extern asmlinkage void *__memmove(void *, const void *, size_t);
> > +
> > +#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)
>
> This looks weird and at least needs a very good comment. Also
> with this we effectively don't need the non-prefixed prototypes
> anymore. Also you probably want to split the renaming of the mem*
> routines into a separate patch with a proper changelog.
>
I made some mistakes on this porting, this would be better:

#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);

#define __HAVE_ARCH_MEMMOVE
extern asmlinkage void *memmove(void *, const void *, size_t);
extern asmlinkage void *__memmove(void *, const void *, size_t);

#if defined(CONFIG_KASAN) && !defined(__SANITIZE_ADDRESS__)

#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

> > #include <asm/tlbflush.h>
> > #include <asm/thread_info.h>
> >
> > +#ifdef CONFIG_KASAN
> > +#include <asm/kasan.h>
> > +#endif
>
> Any good reason to not just always include the header?
>
Nope, I would remove the '#ifdef CONFIG_KASAN', and do the logic in the header
instead.

> > +
> > #ifdef CONFIG_DUMMY_CONSOLE
> > struct screen_info screen_info = {
> > .orig_video_lines = 30,
> > @@ -64,12 +68,17 @@ void __init setup_arch(char **cmdline_p)
> >
> > setup_bootmem();
> > paging_init();
> > +
> > unflatten_device_tree();
>
> spurious whitespace change.
>
> > diff --git a/arch/riscv/kernel/vmlinux.lds.S b/arch/riscv/kernel/vmlinux.lds.S
> > index 23cd1a9..9700980 100644
> > --- a/arch/riscv/kernel/vmlinux.lds.S
> > +++ b/arch/riscv/kernel/vmlinux.lds.S
> > @@ -46,6 +46,7 @@ SECTIONS
> > KPROBES_TEXT
> > ENTRY_TEXT
> > IRQENTRY_TEXT
> > + SOFTIRQENTRY_TEXT
>
> Hmm. What is the relation to kasan here? Maybe we should add this
> separately with a good changelog?
>
There is a commit for it:

Author: Alexander Potapenko <[email protected]>
Date: Fri Mar 25 14:22:05 2016 -0700

arch, ftrace: for KASAN put hard/soft IRQ entries into separate sections

KASAN needs to know whether the allocation happens in an IRQ handler.
This lets us strip everything below the IRQ entry point to reduce the
number of unique stack traces needed to be stored.

Move the definition of __irq_entry to <linux/interrupt.h> so that the
users don't need to pull in <linux/ftrace.h>. Also introduce the
__softirq_entry macro which is similar to __irq_entry, but puts the
corresponding functions to the .softirqentry.text section.

After reading the patch I understand that soft/hard IRQ entries should be
separated for KASAN to work, but why?

Alexender, do you have any comments on this?

> > +++ b/arch/riscv/mm/kasan_init.c
> > @@ -0,0 +1,102 @@
> > +// SPDX-License-Identifier: GPL-2.0
>
> This probably also wants a copyright statement.
>
> > + // init for swapper_pg_dir
>
> Please use /* */ style comments.

2019-08-14 18:34:46

by Palmer Dabbelt

[permalink] [raw]
Subject: Re: [PATCH 1/2] riscv: Add memmove string operation.

On Tue, 13 Aug 2019 19:22:15 PDT (-0700), Paul Walmsley wrote:
> On Tue, 13 Aug 2019, Palmer Dabbelt wrote:
>
>> On Mon, 12 Aug 2019 08:04:46 PDT (-0700), Christoph Hellwig wrote:
>> > On Wed, Aug 07, 2019 at 03:19:14PM +0800, Nick Hu wrote:
>> > > There are some features which need this string operation for compilation,
>> > > like KASAN. So the purpose of this porting is for the features like KASAN
>> > > which cannot be compiled without it.
>> > >
>> > > KASAN's string operations would replace the original string operations and
>> > > call for the architecture defined string operations. Since we don't have
>> > > this in current kernel, this patch provides the implementation.
>> > >
>> > > This porting refers to the 'arch/nds32/lib/memmove.S'.
>> >
>> > This looks sensible to me, although my stringop asm is rather rusty,
>> > so just an ack and not a real review-by:
>> >
>> > Acked-by: Christoph Hellwig <[email protected]>
>>
>> FWIW, we just write this in C everywhere else and rely on the compiler to
>> unroll the loops. I always prefer C to assembly when possible, so I'd prefer
>> if we just adopt the string code from newlib. We have a RISC-V-specific
>> memcpy in there, but just use the generic memmove.
>>
>> Maybe the best bet here would be to adopt the newlib memcpy/memmove as generic
>> Linux functions? They're both in C so they should be fine, and they both look
>> faster than what's in lib/string.c. Then everyone would benefit and we don't
>> need this tricky RISC-V assembly. Also, from the look of it the newlib code
>> is faster because the inner loop is unrolled.
>
> There's a generic memmove implementation in the kernel already:
>
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/linux/string.h#n362

That ends up at __builtin_memcpy(), which ends up looking for memcpy() for
large copies, which is in lib/string.c. The code in there is just byte at a
time memcpy()/memmove(), which is way slower than the newlib stuff.

>
> Nick, could you tell us more about why the generic memmove() isn't
> suitable?
>
>
> - Paul

2019-08-15 03:17:12

by Nick Hu

[permalink] [raw]
Subject: Re: [PATCH 1/2] riscv: Add memmove string operation.

Hi Paul,

On Wed, Aug 14, 2019 at 10:03:39AM -0700, Paul Walmsley wrote:
> Hi Nick,
>
> On Wed, 14 Aug 2019, Nick Hu wrote:
>
> > On Wed, Aug 14, 2019 at 10:22:15AM +0800, Paul Walmsley wrote:
> > > On Tue, 13 Aug 2019, Palmer Dabbelt wrote:
> > >
> > > > On Mon, 12 Aug 2019 08:04:46 PDT (-0700), Christoph Hellwig wrote:
> > > > > On Wed, Aug 07, 2019 at 03:19:14PM +0800, Nick Hu wrote:
> > > > > > There are some features which need this string operation for compilation,
> > > > > > like KASAN. So the purpose of this porting is for the features like KASAN
> > > > > > which cannot be compiled without it.
> > > > > >
> > > > > > KASAN's string operations would replace the original string operations and
> > > > > > call for the architecture defined string operations. Since we don't have
> > > > > > this in current kernel, this patch provides the implementation.
> > > > > >
> > > > > > This porting refers to the 'arch/nds32/lib/memmove.S'.
> > > > >
> > > > > This looks sensible to me, although my stringop asm is rather rusty,
> > > > > so just an ack and not a real review-by:
> > > >
> > > > FWIW, we just write this in C everywhere else and rely on the compiler to
> > > > unroll the loops. I always prefer C to assembly when possible, so I'd prefer
> > > > if we just adopt the string code from newlib. We have a RISC-V-specific
> > > > memcpy in there, but just use the generic memmove.
> > > >
> > > > Maybe the best bet here would be to adopt the newlib memcpy/memmove as generic
> > > > Linux functions? They're both in C so they should be fine, and they both look
> > > > faster than what's in lib/string.c. Then everyone would benefit and we don't
> > > > need this tricky RISC-V assembly. Also, from the look of it the newlib code
> > > > is faster because the inner loop is unrolled.
> > >
> > > There's a generic memmove implementation in the kernel already:
> > >
> > > https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/linux/string.h#n362
> > >
> > > Nick, could you tell us more about why the generic memmove() isn't
> > > suitable?
> >
> > KASAN has its own string operations(memcpy/memmove/memset) because it needs to
> > hook some code to check memory region. It would undefined the original string
> > operations and called the string operations with the prefix '__'. But the
> > generic string operations didn't declare with the prefix. Other archs with
> > KASAN support like arm64 and xtensa all have their own string operations and
> > defined with the prefix.
>
> Thanks for the explanation. What do you think about Palmer's idea to
> define a generic C set of KASAN string operations, derived from the newlib
> code?
>
>
> - Paul

That sounds good to me. But it should be another topic. We need to investigate
it further about replacing something generic and fundamental in lib/string.c
with newlib C functions. Some blind spots may exist. So I suggest, let's
consider KASAN for now.

Nick

2019-08-19 06:32:31

by Nick Hu

[permalink] [raw]
Subject: Re: [PATCH 1/2] riscv: Add memmove string operation.

Hi Paul,

On Thu, Aug 15, 2019 at 11:27:51AM -0700, Paul Walmsley wrote:
> On Thu, 15 Aug 2019, Nick Hu wrote:
>
> > On Wed, Aug 14, 2019 at 10:03:39AM -0700, Paul Walmsley wrote:
> >
> > > Thanks for the explanation. What do you think about Palmer's idea to
> > > define a generic C set of KASAN string operations, derived from the newlib
> > > code?
> >
> > That sounds good to me. But it should be another topic. We need to investigate
> > it further about replacing something generic and fundamental in lib/string.c
> > with newlib C functions. Some blind spots may exist. So I suggest, let's
> > consider KASAN for now.
>
> OK. Here is the problem for us as maintainers. You, Palmer, and I all
> agree that a C-language version would be better. We'd rather not merge a
> pure assembly-language version unless it had significant advantages, and
> right now we're not anticipating that. So that suggests that a C-language
> memmove() is the right way to go.
>
> But if we merge a C-language memmove() into arch/riscv, other kernel
> developers would probably ask us why we're doing that, since there's
> nothing RISC-V-specific about it. So do you think you might reconsider
> sending patches to add a generic C-language memmove()?
>
>
> - Paul

About pushing mem*() generic, let's start with the reason why in the first place
KASAN needs re-implement its own string operations:

In mm/kasan/common.c:

#undef memset
void *memset(void *addr, int c, size_t len)
{
check_memory_region((unsigned long)addr, len, true, _RET_IP_);

return __memset(addr, c, len);
}

KASAN would call the string operations with the prefix '__', which should be
just an alias to the proper one.

In the past, every architecture that supports KASAN does this in assembly.
E.g. ARM64:

In arch/arm64/lib/memset.S:

ENTRY(__memset)
ENTRY(memset)
...
...
EXPORT_SYMBOL(memset)
EXPORT_SYMBOL(__memset) // export this as an alias

In arch/arm64/include/asm/string.h

#define __HAVE_ARCH_MEMSET
extern void *memset(void *, int, __kernel_size_t);
extern void *__memset(void *, int, __kernel_size_t);

Now, if we are going to replace the current string operations with newlib ones
and let KASAN use them, we must provide something like this:

In lib/string.c:
void *___memset(...)
{
...
}

In include/linux/string.h:

#ifndef __HAVE_ARCH_MEMCPY
#ifdef CONFIG_KASAN
static inline void* __memset(...)
{
___memset(...);
}
extern void memset(...); // force those who include this header uses the
memset wrapped by KASAN
#else
static inline void *memset(...)
{
___memset(...);
}
#endif
#endif

Does this look OK to you?

Nick

2019-08-22 19:54:19

by Andrey Ryabinin

[permalink] [raw]
Subject: Re: [PATCH 1/2] riscv: Add memmove string operation.

On 8/7/19 10:19 AM, Nick Hu wrote:
> There are some features which need this string operation for compilation,
> like KASAN. So the purpose of this porting is for the features like KASAN
> which cannot be compiled without it.
>

Compilation error can be fixed by diff bellow (I didn't test it).
If you don't need memmove very early (before kasan_early_init()) than arch-specific not-instrumented memmove()
isn't necessary to have.

---
mm/kasan/common.c | 2 ++
1 file changed, 2 insertions(+)

diff --git a/mm/kasan/common.c b/mm/kasan/common.c
index 6814d6d6a023..897f9520bab3 100644
--- a/mm/kasan/common.c
+++ b/mm/kasan/common.c
@@ -107,6 +107,7 @@ void *memset(void *addr, int c, size_t len)
return __memset(addr, c, len);
}

+#ifdef __HAVE_ARCH_MEMMOVE
#undef memmove
void *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 memcpy
void *memcpy(void *dest, const void *src, size_t len)
--
2.21.0



2019-08-22 21:05:03

by Andrey Ryabinin

[permalink] [raw]
Subject: Re: [PATCH 2/2] riscv: Add KASAN support



On 8/14/19 10:44 AM, Nick Hu wrote:

>>
>>> diff --git a/arch/riscv/kernel/vmlinux.lds.S b/arch/riscv/kernel/vmlinux.lds.S
>>> index 23cd1a9..9700980 100644
>>> --- a/arch/riscv/kernel/vmlinux.lds.S
>>> +++ b/arch/riscv/kernel/vmlinux.lds.S
>>> @@ -46,6 +46,7 @@ SECTIONS
>>> KPROBES_TEXT
>>> ENTRY_TEXT
>>> IRQENTRY_TEXT
>>> + SOFTIRQENTRY_TEXT
>>
>> Hmm. What is the relation to kasan here? Maybe we should add this
>> separately with a good changelog?
>>
> There is a commit for it:
>
> Author: Alexander Potapenko <[email protected]>
> Date: Fri Mar 25 14:22:05 2016 -0700
>
> arch, ftrace: for KASAN put hard/soft IRQ entries into separate sections
>
> KASAN needs to know whether the allocation happens in an IRQ handler.
> This lets us strip everything below the IRQ entry point to reduce the
> number of unique stack traces needed to be stored.
>
> Move the definition of __irq_entry to <linux/interrupt.h> so that the
> users don't need to pull in <linux/ftrace.h>. Also introduce the
> __softirq_entry macro which is similar to __irq_entry, but puts the
> corresponding functions to the .softirqentry.text section.
>
> After reading the patch I understand that soft/hard IRQ entries should be
> separated for KASAN to work, but why?
>

KASAN doesn't need soft/hard IRQ entries separated. KASAN wants to know the entry
point of IRQ (hard or soft) to filter out random non-irq part of the stacktrace before feeding it to
stack_depot_save. See filter_irq_stacks().


2019-08-27 09:10:04

by Nick Hu

[permalink] [raw]
Subject: Re: [PATCH 1/2] riscv: Add memmove string operation.

Hi Andrey

On Thu, Aug 22, 2019 at 11:59:02PM +0800, Andrey Ryabinin wrote:
> On 8/7/19 10:19 AM, Nick Hu wrote:
> > There are some features which need this string operation for compilation,
> > like KASAN. So the purpose of this porting is for the features like KASAN
> > which cannot be compiled without it.
> >
>
> Compilation error can be fixed by diff bellow (I didn't test it).
> If you don't need memmove very early (before kasan_early_init()) than arch-specific not-instrumented memmove()
> isn't necessary to have.
>
> ---
> mm/kasan/common.c | 2 ++
> 1 file changed, 2 insertions(+)
>
> diff --git a/mm/kasan/common.c b/mm/kasan/common.c
> index 6814d6d6a023..897f9520bab3 100644
> --- a/mm/kasan/common.c
> +++ b/mm/kasan/common.c
> @@ -107,6 +107,7 @@ void *memset(void *addr, int c, size_t len)
> return __memset(addr, c, len);
> }
>
> +#ifdef __HAVE_ARCH_MEMMOVE
> #undef memmove
> void *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 memcpy
> void *memcpy(void *dest, const void *src, size_t len)
> --
> 2.21.0
>
>
>
I have confirmed that the string operations are not used before kasan_early_init().
But I can't make sure whether other ARCHs would need it before kasan_early_init().
Do you have any idea to check that? Should I cc all other ARCH maintainers?

Nick

2019-08-27 09:34:35

by Andrey Ryabinin

[permalink] [raw]
Subject: Re: [PATCH 1/2] riscv: Add memmove string operation.



On 8/27/19 12:07 PM, Nick Hu wrote:
> Hi Andrey
>
> On Thu, Aug 22, 2019 at 11:59:02PM +0800, Andrey Ryabinin wrote:
>> On 8/7/19 10:19 AM, Nick Hu wrote:
>>> There are some features which need this string operation for compilation,
>>> like KASAN. So the purpose of this porting is for the features like KASAN
>>> which cannot be compiled without it.
>>>
>>
>> Compilation error can be fixed by diff bellow (I didn't test it).
>> If you don't need memmove very early (before kasan_early_init()) than arch-specific not-instrumented memmove()
>> isn't necessary to have.
>>
>> ---
>> mm/kasan/common.c | 2 ++
>> 1 file changed, 2 insertions(+)
>>
>> diff --git a/mm/kasan/common.c b/mm/kasan/common.c
>> index 6814d6d6a023..897f9520bab3 100644
>> --- a/mm/kasan/common.c
>> +++ b/mm/kasan/common.c
>> @@ -107,6 +107,7 @@ void *memset(void *addr, int c, size_t len)
>> return __memset(addr, c, len);
>> }
>>
>> +#ifdef __HAVE_ARCH_MEMMOVE
>> #undef memmove
>> void *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 memcpy
>> void *memcpy(void *dest, const void *src, size_t len)
>> --
>> 2.21.0
>>
>>
>>
> I have confirmed that the string operations are not used before kasan_early_init().
> But I can't make sure whether other ARCHs would need it before kasan_early_init().
> Do you have any idea to check that? Should I cc all other ARCH maintainers?


This doesn't affect other ARCHes in any way. If other arches have their own not-instrumented
memmove implementation (and they do), they will continue to be able to use it early.

2019-08-28 03:08:58

by Nick Hu

[permalink] [raw]
Subject: Re: [PATCH 1/2] riscv: Add memmove string operation.

Hi Paul,

On Tue, Aug 27, 2019 at 05:33:11PM +0800, Andrey Ryabinin wrote:
>
>
> On 8/27/19 12:07 PM, Nick Hu wrote:
> > Hi Andrey
> >
> > On Thu, Aug 22, 2019 at 11:59:02PM +0800, Andrey Ryabinin wrote:
> >> On 8/7/19 10:19 AM, Nick Hu wrote:
> >>> There are some features which need this string operation for compilation,
> >>> like KASAN. So the purpose of this porting is for the features like KASAN
> >>> which cannot be compiled without it.
> >>>
> >>
> >> Compilation error can be fixed by diff bellow (I didn't test it).
> >> If you don't need memmove very early (before kasan_early_init()) than arch-specific not-instrumented memmove()
> >> isn't necessary to have.
> >>
> >> ---
> >> mm/kasan/common.c | 2 ++
> >> 1 file changed, 2 insertions(+)
> >>
> >> diff --git a/mm/kasan/common.c b/mm/kasan/common.c
> >> index 6814d6d6a023..897f9520bab3 100644
> >> --- a/mm/kasan/common.c
> >> +++ b/mm/kasan/common.c
> >> @@ -107,6 +107,7 @@ void *memset(void *addr, int c, size_t len)
> >> return __memset(addr, c, len);
> >> }
> >>
> >> +#ifdef __HAVE_ARCH_MEMMOVE
> >> #undef memmove
> >> void *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 memcpy
> >> void *memcpy(void *dest, const void *src, size_t len)
> >> --
> >> 2.21.0
> >>
> >>
> >>
> > I have confirmed that the string operations are not used before kasan_early_init().
> > But I can't make sure whether other ARCHs would need it before kasan_early_init().
> > Do you have any idea to check that? Should I cc all other ARCH maintainers?
>
>
> This doesn't affect other ARCHes in any way. If other arches have their own not-instrumented
> memmove implementation (and they do), they will continue to be able to use it early.

I prefer Andrey's method since porting the generic string operations with newlib ones should
be a separated patch from KASAN.

Nick

2019-09-03 15:11:47

by Daniel Axtens

[permalink] [raw]
Subject: Re: [PATCH 2/2] riscv: Add KASAN support

Nick Hu <[email protected]> writes:

> Hi Christoph,
>
> Thanks for your reply. I will answer one by one.
>
> Hi Alexander,
>
> Would you help me for the question about SOFTIRQENTRY_TEXT?
>
> On Mon, Aug 12, 2019 at 11:10:50PM +0800, Christoph Hellwig wrote:
>> > 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.
>>
>> How do other architectures solve this problem?
>>
> Other archs like arm64 and x86 allocate modules in their module region.

I've run in to a similar difficulty in ppc64. My approach has been to
add a generic feature to allow kasan to handle vmalloc areas:

https://lore.kernel.org/linux-mm/[email protected]/

I link this with ppc64 in this series:

https://lore.kernel.org/linuxppc-dev/[email protected]/

However, see Christophe Leroy's comments: he thinks I should take a
different approach in a number of places, including just adding a
separate module area. I haven't had time to think through all of his
proposals yet; in particular I'd want to think through what the
implication of a separate module area is for KASLR.

Regards,
Daniel

>
>> > @@ -54,6 +54,8 @@ config RISCV
>> > select EDAC_SUPPORT
>> > select ARCH_HAS_GIGANTIC_PAGE
>> > select ARCH_WANT_HUGE_PMD_SHARE if 64BIT
>> > + select GENERIC_STRNCPY_FROM_USER if KASAN
>>
>> Is there any reason why we can't always enabled this? Also just
>> enabling the generic efficient strncpy_from_user should probably be
>> a separate patch.
>>
> You're right, always enable it would be better.
>
>> > + select HAVE_ARCH_KASAN if MMU
>>
>> Based on your cover letter this should be if MMU && 64BIT
>>
>> > #define __HAVE_ARCH_MEMCPY
>> > extern asmlinkage void *memcpy(void *, const void *, size_t);
>> > +extern asmlinkage void *__memcpy(void *, const void *, size_t);
>> >
>> > #define __HAVE_ARCH_MEMMOVE
>> > extern asmlinkage void *memmove(void *, const void *, size_t);
>> > +extern asmlinkage void *__memmove(void *, const void *, size_t);
>> > +
>> > +#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)
>>
>> This looks weird and at least needs a very good comment. Also
>> with this we effectively don't need the non-prefixed prototypes
>> anymore. Also you probably want to split the renaming of the mem*
>> routines into a separate patch with a proper changelog.
>>
> I made some mistakes on this porting, this would be better:
>
> #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);
>
> #define __HAVE_ARCH_MEMMOVE
> extern asmlinkage void *memmove(void *, const void *, size_t);
> extern asmlinkage void *__memmove(void *, const void *, size_t);
>
> #if defined(CONFIG_KASAN) && !defined(__SANITIZE_ADDRESS__)
>
> #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
>
>> > #include <asm/tlbflush.h>
>> > #include <asm/thread_info.h>
>> >
>> > +#ifdef CONFIG_KASAN
>> > +#include <asm/kasan.h>
>> > +#endif
>>
>> Any good reason to not just always include the header?
>>
> Nope, I would remove the '#ifdef CONFIG_KASAN', and do the logic in the header
> instead.
>
>> > +
>> > #ifdef CONFIG_DUMMY_CONSOLE
>> > struct screen_info screen_info = {
>> > .orig_video_lines = 30,
>> > @@ -64,12 +68,17 @@ void __init setup_arch(char **cmdline_p)
>> >
>> > setup_bootmem();
>> > paging_init();
>> > +
>> > unflatten_device_tree();
>>
>> spurious whitespace change.
>>
>> > diff --git a/arch/riscv/kernel/vmlinux.lds.S b/arch/riscv/kernel/vmlinux.lds.S
>> > index 23cd1a9..9700980 100644
>> > --- a/arch/riscv/kernel/vmlinux.lds.S
>> > +++ b/arch/riscv/kernel/vmlinux.lds.S
>> > @@ -46,6 +46,7 @@ SECTIONS
>> > KPROBES_TEXT
>> > ENTRY_TEXT
>> > IRQENTRY_TEXT
>> > + SOFTIRQENTRY_TEXT
>>
>> Hmm. What is the relation to kasan here? Maybe we should add this
>> separately with a good changelog?
>>
> There is a commit for it:
>
> Author: Alexander Potapenko <[email protected]>
> Date: Fri Mar 25 14:22:05 2016 -0700
>
> arch, ftrace: for KASAN put hard/soft IRQ entries into separate sections
>
> KASAN needs to know whether the allocation happens in an IRQ handler.
> This lets us strip everything below the IRQ entry point to reduce the
> number of unique stack traces needed to be stored.
>
> Move the definition of __irq_entry to <linux/interrupt.h> so that the
> users don't need to pull in <linux/ftrace.h>. Also introduce the
> __softirq_entry macro which is similar to __irq_entry, but puts the
> corresponding functions to the .softirqentry.text section.
>
> After reading the patch I understand that soft/hard IRQ entries should be
> separated for KASAN to work, but why?
>
> Alexender, do you have any comments on this?
>
>> > +++ b/arch/riscv/mm/kasan_init.c
>> > @@ -0,0 +1,102 @@
>> > +// SPDX-License-Identifier: GPL-2.0
>>
>> This probably also wants a copyright statement.
>>
>> > + // init for swapper_pg_dir
>>
>> Please use /* */ style comments.
>
> --
> You received this message because you are subscribed to the Google Groups "kasan-dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
> To view this discussion on the web visit https://groups.google.com/d/msgid/kasan-dev/20190814074417.GA21929%40andestech.com.

2019-09-04 02:26:39

by Nick Hu

[permalink] [raw]
Subject: Re: [PATCH 2/2] riscv: Add KASAN support

Hi Daniel,

On Wed, Sep 04, 2019 at 01:08:51AM +1000, Daniel Axtens wrote:
> Nick Hu <[email protected]> writes:
>
> > Hi Christoph,
> >
> > Thanks for your reply. I will answer one by one.
> >
> > Hi Alexander,
> >
> > Would you help me for the question about SOFTIRQENTRY_TEXT?
> >
> > On Mon, Aug 12, 2019 at 11:10:50PM +0800, Christoph Hellwig wrote:
> >> > 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.
> >>
> >> How do other architectures solve this problem?
> >>
> > Other archs like arm64 and x86 allocate modules in their module region.
>
> I've run in to a similar difficulty in ppc64. My approach has been to
> add a generic feature to allow kasan to handle vmalloc areas:
>
> https://lore.kernel.org/linux-mm/[email protected]/
>
> I link this with ppc64 in this series:
>
> https://lore.kernel.org/linuxppc-dev/[email protected]/
>
> However, see Christophe Leroy's comments: he thinks I should take a
> different approach in a number of places, including just adding a
> separate module area. I haven't had time to think through all of his
> proposals yet; in particular I'd want to think through what the
> implication of a separate module area is for KASLR.
>
> Regards,
> Daniel
>

Thanks for the advice! I would study on it.

Regards,
Nick

> >
> >> > @@ -54,6 +54,8 @@ config RISCV
> >> > select EDAC_SUPPORT
> >> > select ARCH_HAS_GIGANTIC_PAGE
> >> > select ARCH_WANT_HUGE_PMD_SHARE if 64BIT
> >> > + select GENERIC_STRNCPY_FROM_USER if KASAN
> >>
> >> Is there any reason why we can't always enabled this? Also just
> >> enabling the generic efficient strncpy_from_user should probably be
> >> a separate patch.
> >>
> > You're right, always enable it would be better.
> >
> >> > + select HAVE_ARCH_KASAN if MMU
> >>
> >> Based on your cover letter this should be if MMU && 64BIT
> >>
> >> > #define __HAVE_ARCH_MEMCPY
> >> > extern asmlinkage void *memcpy(void *, const void *, size_t);
> >> > +extern asmlinkage void *__memcpy(void *, const void *, size_t);
> >> >
> >> > #define __HAVE_ARCH_MEMMOVE
> >> > extern asmlinkage void *memmove(void *, const void *, size_t);
> >> > +extern asmlinkage void *__memmove(void *, const void *, size_t);
> >> > +
> >> > +#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)
> >>
> >> This looks weird and at least needs a very good comment. Also
> >> with this we effectively don't need the non-prefixed prototypes
> >> anymore. Also you probably want to split the renaming of the mem*
> >> routines into a separate patch with a proper changelog.
> >>
> > I made some mistakes on this porting, this would be better:
> >
> > #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);
> >
> > #define __HAVE_ARCH_MEMMOVE
> > extern asmlinkage void *memmove(void *, const void *, size_t);
> > extern asmlinkage void *__memmove(void *, const void *, size_t);
> >
> > #if defined(CONFIG_KASAN) && !defined(__SANITIZE_ADDRESS__)
> >
> > #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
> >
> >> > #include <asm/tlbflush.h>
> >> > #include <asm/thread_info.h>
> >> >
> >> > +#ifdef CONFIG_KASAN
> >> > +#include <asm/kasan.h>
> >> > +#endif
> >>
> >> Any good reason to not just always include the header?
> >>
> > Nope, I would remove the '#ifdef CONFIG_KASAN', and do the logic in the header
> > instead.
> >
> >> > +
> >> > #ifdef CONFIG_DUMMY_CONSOLE
> >> > struct screen_info screen_info = {
> >> > .orig_video_lines = 30,
> >> > @@ -64,12 +68,17 @@ void __init setup_arch(char **cmdline_p)
> >> >
> >> > setup_bootmem();
> >> > paging_init();
> >> > +
> >> > unflatten_device_tree();
> >>
> >> spurious whitespace change.
> >>
> >> > diff --git a/arch/riscv/kernel/vmlinux.lds.S b/arch/riscv/kernel/vmlinux.lds.S
> >> > index 23cd1a9..9700980 100644
> >> > --- a/arch/riscv/kernel/vmlinux.lds.S
> >> > +++ b/arch/riscv/kernel/vmlinux.lds.S
> >> > @@ -46,6 +46,7 @@ SECTIONS
> >> > KPROBES_TEXT
> >> > ENTRY_TEXT
> >> > IRQENTRY_TEXT
> >> > + SOFTIRQENTRY_TEXT
> >>
> >> Hmm. What is the relation to kasan here? Maybe we should add this
> >> separately with a good changelog?
> >>
> > There is a commit for it:
> >
> > Author: Alexander Potapenko <[email protected]>
> > Date: Fri Mar 25 14:22:05 2016 -0700
> >
> > arch, ftrace: for KASAN put hard/soft IRQ entries into separate sections
> >
> > KASAN needs to know whether the allocation happens in an IRQ handler.
> > This lets us strip everything below the IRQ entry point to reduce the
> > number of unique stack traces needed to be stored.
> >
> > Move the definition of __irq_entry to <linux/interrupt.h> so that the
> > users don't need to pull in <linux/ftrace.h>. Also introduce the
> > __softirq_entry macro which is similar to __irq_entry, but puts the
> > corresponding functions to the .softirqentry.text section.
> >
> > After reading the patch I understand that soft/hard IRQ entries should be
> > separated for KASAN to work, but why?
> >
> > Alexender, do you have any comments on this?
> >
> >> > +++ b/arch/riscv/mm/kasan_init.c
> >> > @@ -0,0 +1,102 @@
> >> > +// SPDX-License-Identifier: GPL-2.0
> >>
> >> This probably also wants a copyright statement.
> >>
> >> > + // init for swapper_pg_dir
> >>
> >> Please use /* */ style comments.
> >
> > --
> > You received this message because you are subscribed to the Google Groups "kasan-dev" group.
> > To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
> > To view this discussion on the web visit https://groups.google.com/d/msgid/kasan-dev/20190814074417.GA21929%40andestech.com.