linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/2] riscv asan-stack fixes
@ 2021-10-29  4:59 Alexandre Ghiti
  2021-10-29  4:59 ` [PATCH v2 1/2] riscv: Do not re-populate shadow memory with kasan_populate_early_shadow Alexandre Ghiti
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Alexandre Ghiti @ 2021-10-29  4:59 UTC (permalink / raw)
  To: Paul Walmsley, Palmer Dabbelt, Albert Ou, Andrey Ryabinin,
	Alexander Potapenko, Andrey Konovalov, Dmitry Vyukov,
	linux-riscv, linux-kernel, kasan-dev
  Cc: Alexandre Ghiti

This small patchset fixes asan-stack for riscv.

Changes in v2:
 * fix KASAN_VMALLOC=n
 * swap both patches in order not to have a non-bootable kernel commit

Alexandre Ghiti (2):
  riscv: Do not re-populate shadow memory with
    kasan_populate_early_shadow
  riscv: Fix asan-stack clang build

 arch/riscv/Kconfig             |  6 ++++++
 arch/riscv/include/asm/kasan.h |  3 +--
 arch/riscv/mm/kasan_init.c     | 14 +++-----------
 3 files changed, 10 insertions(+), 13 deletions(-)

-- 
2.30.2


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 1/2] riscv: Do not re-populate shadow memory with kasan_populate_early_shadow
  2021-10-29  4:59 [PATCH v2 0/2] riscv asan-stack fixes Alexandre Ghiti
@ 2021-10-29  4:59 ` Alexandre Ghiti
  2021-10-29  4:59 ` [PATCH v2 2/2] riscv: Fix asan-stack clang build Alexandre Ghiti
  2021-10-29 18:08 ` [PATCH v2 0/2] riscv asan-stack fixes Palmer Dabbelt
  2 siblings, 0 replies; 4+ messages in thread
From: Alexandre Ghiti @ 2021-10-29  4:59 UTC (permalink / raw)
  To: Paul Walmsley, Palmer Dabbelt, Albert Ou, Andrey Ryabinin,
	Alexander Potapenko, Andrey Konovalov, Dmitry Vyukov,
	linux-riscv, linux-kernel, kasan-dev
  Cc: Alexandre Ghiti

When calling this function, all the shadow memory is already populated
with kasan_early_shadow_pte which has PAGE_KERNEL protection.
kasan_populate_early_shadow write-protects the mapping of the range
of addresses passed in argument in zero_pte_populate, which actually
write-protects all the shadow memory mapping since kasan_early_shadow_pte
is used for all the shadow memory at this point. And then when using
memblock API to populate the shadow memory, the first write access to the
kernel stack triggers a trap. This becomes visible with the next commit
that contains a fix for asan-stack.

We already manually populate all the shadow memory in kasan_early_init
and we write-protect kasan_early_shadow_pte at the end of kasan_init
which makes the calls to kasan_populate_early_shadow superfluous so
we can remove them.

Signed-off-by: Alexandre Ghiti <alexandre.ghiti@canonical.com>
---
 arch/riscv/mm/kasan_init.c | 11 -----------
 1 file changed, 11 deletions(-)

diff --git a/arch/riscv/mm/kasan_init.c b/arch/riscv/mm/kasan_init.c
index d7189c8714a9..89a8376ce44e 100644
--- a/arch/riscv/mm/kasan_init.c
+++ b/arch/riscv/mm/kasan_init.c
@@ -172,21 +172,10 @@ void __init kasan_init(void)
 	phys_addr_t p_start, p_end;
 	u64 i;
 
-	/*
-	 * Populate all kernel virtual address space with kasan_early_shadow_page
-	 * except for the linear mapping and the modules/kernel/BPF mapping.
-	 */
-	kasan_populate_early_shadow((void *)KASAN_SHADOW_START,
-				    (void *)kasan_mem_to_shadow((void *)
-								VMEMMAP_END));
 	if (IS_ENABLED(CONFIG_KASAN_VMALLOC))
 		kasan_shallow_populate(
 			(void *)kasan_mem_to_shadow((void *)VMALLOC_START),
 			(void *)kasan_mem_to_shadow((void *)VMALLOC_END));
-	else
-		kasan_populate_early_shadow(
-			(void *)kasan_mem_to_shadow((void *)VMALLOC_START),
-			(void *)kasan_mem_to_shadow((void *)VMALLOC_END));
 
 	/* Populate the linear mapping */
 	for_each_mem_range(i, &p_start, &p_end) {
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v2 2/2] riscv: Fix asan-stack clang build
  2021-10-29  4:59 [PATCH v2 0/2] riscv asan-stack fixes Alexandre Ghiti
  2021-10-29  4:59 ` [PATCH v2 1/2] riscv: Do not re-populate shadow memory with kasan_populate_early_shadow Alexandre Ghiti
@ 2021-10-29  4:59 ` Alexandre Ghiti
  2021-10-29 18:08 ` [PATCH v2 0/2] riscv asan-stack fixes Palmer Dabbelt
  2 siblings, 0 replies; 4+ messages in thread
From: Alexandre Ghiti @ 2021-10-29  4:59 UTC (permalink / raw)
  To: Paul Walmsley, Palmer Dabbelt, Albert Ou, Andrey Ryabinin,
	Alexander Potapenko, Andrey Konovalov, Dmitry Vyukov,
	linux-riscv, linux-kernel, kasan-dev
  Cc: Alexandre Ghiti, Nathan Chancellor

Nathan reported that because KASAN_SHADOW_OFFSET was not defined in
Kconfig, it prevents asan-stack from getting disabled with clang even
when CONFIG_KASAN_STACK is disabled: fix this by defining the
corresponding config.

Reported-by: Nathan Chancellor <nathan@kernel.org>
Signed-off-by: Alexandre Ghiti <alexandre.ghiti@canonical.com>
---
 arch/riscv/Kconfig             | 6 ++++++
 arch/riscv/include/asm/kasan.h | 3 +--
 arch/riscv/mm/kasan_init.c     | 3 +++
 3 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
index c3f3fd583e04..6d5b63bd4bd9 100644
--- a/arch/riscv/Kconfig
+++ b/arch/riscv/Kconfig
@@ -163,6 +163,12 @@ config PAGE_OFFSET
 	default 0xffffffff80000000 if 64BIT && MAXPHYSMEM_2GB
 	default 0xffffffe000000000 if 64BIT && MAXPHYSMEM_128GB
 
+config KASAN_SHADOW_OFFSET
+	hex
+	depends on KASAN_GENERIC
+	default 0xdfffffc800000000 if 64BIT
+	default 0xffffffff if 32BIT
+
 config ARCH_FLATMEM_ENABLE
 	def_bool !NUMA
 
diff --git a/arch/riscv/include/asm/kasan.h b/arch/riscv/include/asm/kasan.h
index a2b3d9cdbc86..b00f503ec124 100644
--- a/arch/riscv/include/asm/kasan.h
+++ b/arch/riscv/include/asm/kasan.h
@@ -30,8 +30,7 @@
 #define KASAN_SHADOW_SIZE	(UL(1) << ((CONFIG_VA_BITS - 1) - KASAN_SHADOW_SCALE_SHIFT))
 #define KASAN_SHADOW_START	KERN_VIRT_START
 #define KASAN_SHADOW_END	(KASAN_SHADOW_START + KASAN_SHADOW_SIZE)
-#define KASAN_SHADOW_OFFSET	(KASAN_SHADOW_END - (1ULL << \
-					(64 - KASAN_SHADOW_SCALE_SHIFT)))
+#define KASAN_SHADOW_OFFSET	_AC(CONFIG_KASAN_SHADOW_OFFSET, UL)
 
 void kasan_init(void);
 asmlinkage void kasan_early_init(void);
diff --git a/arch/riscv/mm/kasan_init.c b/arch/riscv/mm/kasan_init.c
index 89a8376ce44e..54294f83513d 100644
--- a/arch/riscv/mm/kasan_init.c
+++ b/arch/riscv/mm/kasan_init.c
@@ -17,6 +17,9 @@ asmlinkage void __init kasan_early_init(void)
 	uintptr_t i;
 	pgd_t *pgd = early_pg_dir + pgd_index(KASAN_SHADOW_START);
 
+	BUILD_BUG_ON(KASAN_SHADOW_OFFSET !=
+		KASAN_SHADOW_END - (1UL << (64 - KASAN_SHADOW_SCALE_SHIFT)));
+
 	for (i = 0; i < PTRS_PER_PTE; ++i)
 		set_pte(kasan_early_shadow_pte + i,
 			mk_pte(virt_to_page(kasan_early_shadow_page),
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2 0/2] riscv asan-stack fixes
  2021-10-29  4:59 [PATCH v2 0/2] riscv asan-stack fixes Alexandre Ghiti
  2021-10-29  4:59 ` [PATCH v2 1/2] riscv: Do not re-populate shadow memory with kasan_populate_early_shadow Alexandre Ghiti
  2021-10-29  4:59 ` [PATCH v2 2/2] riscv: Fix asan-stack clang build Alexandre Ghiti
@ 2021-10-29 18:08 ` Palmer Dabbelt
  2 siblings, 0 replies; 4+ messages in thread
From: Palmer Dabbelt @ 2021-10-29 18:08 UTC (permalink / raw)
  To: alexandre.ghiti
  Cc: Paul Walmsley, aou, ryabinin.a.a, glider, andreyknvl, dvyukov,
	linux-riscv, linux-kernel, kasan-dev, alexandre.ghiti

On Thu, 28 Oct 2021 21:59:25 PDT (-0700), alexandre.ghiti@canonical.com wrote:
> This small patchset fixes asan-stack for riscv.
>
> Changes in v2:
>  * fix KASAN_VMALLOC=n
>  * swap both patches in order not to have a non-bootable kernel commit
>
> Alexandre Ghiti (2):
>   riscv: Do not re-populate shadow memory with
>     kasan_populate_early_shadow
>   riscv: Fix asan-stack clang build
>
>  arch/riscv/Kconfig             |  6 ++++++
>  arch/riscv/include/asm/kasan.h |  3 +--
>  arch/riscv/mm/kasan_init.c     | 14 +++-----------
>  3 files changed, 10 insertions(+), 13 deletions(-)

Thanks, these are on fixes.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-10-29 18:08 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-29  4:59 [PATCH v2 0/2] riscv asan-stack fixes Alexandre Ghiti
2021-10-29  4:59 ` [PATCH v2 1/2] riscv: Do not re-populate shadow memory with kasan_populate_early_shadow Alexandre Ghiti
2021-10-29  4:59 ` [PATCH v2 2/2] riscv: Fix asan-stack clang build Alexandre Ghiti
2021-10-29 18:08 ` [PATCH v2 0/2] riscv asan-stack fixes Palmer Dabbelt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).