Re: [PATCH v4 4/4] powerpc: Book3S 64-bit "heavyweight" KASAN support

From: Christophe Leroy
Date: Thu Dec 19 2019 - 05:08:03 EST




On 12/19/2019 10:05 AM, Christophe Leroy wrote:


Le 19/12/2019 Ã 10:50, Daniel Axtens a ÃcritÂ:
Christophe Leroy <christophe.leroy@xxxxxx> writes:

On 12/19/2019 12:36 AM, Daniel Axtens wrote:
KASAN support on Book3S is a bit tricky to get right:

ÂÂ - It would be good to support inline instrumentation so as to be able to
ÂÂÂÂ catch stack issues that cannot be caught with outline mode.

ÂÂ - Inline instrumentation requires a fixed offset.

ÂÂ - Book3S runs code in real mode after booting. Most notably a lot of KVM
ÂÂÂÂ runs in real mode, and it would be good to be able to instrument it.

ÂÂ - Because code runs in real mode after boot, the offset has to point to
ÂÂÂÂ valid memory both in and out of real mode.

ÂÂÂÂÂ [ppc64 mm note: The kernel installs a linear mapping at effective
ÂÂÂÂÂ address c000... onward. This is a one-to-one mapping with physical
ÂÂÂÂÂ memory from 0000... onward. Because of how memory accesses work on
ÂÂÂÂÂ powerpc 64-bit Book3S, a kernel pointer in the linear map accesses the
ÂÂÂÂÂ same memory both with translations on (accessing as an 'effective
ÂÂÂÂÂ address'), and with translations off (accessing as a 'real
ÂÂÂÂÂ address'). This works in both guests and the hypervisor. For more
ÂÂÂÂÂ details, see s5.7 of Book III of version 3 of the ISA, in particular
ÂÂÂÂÂ the Storage Control Overview, s5.7.3, and s5.7.5 - noting that this
ÂÂÂÂÂ KASAN implementation currently only supports Radix.]

One approach is just to give up on inline instrumentation. This way all
checks can be delayed until after everything set is up correctly, and the
address-to-shadow calculations can be overridden. However, the features and
speed boost provided by inline instrumentation are worth trying to do
better.

If _at compile time_ it is known how much contiguous physical memory a
system has, the top 1/8th of the first block of physical memory can be set
aside for the shadow. This is a big hammer and comes with 3 big
consequences:

ÂÂ - there's no nice way to handle physically discontiguous memory, so only
ÂÂÂÂ the first physical memory block can be used.

ÂÂ - kernels will simply fail to boot on machines with less memory than
ÂÂÂÂ specified when compiling.

ÂÂ - kernels running on machines with more memory than specified when
ÂÂÂÂ compiling will simply ignore the extra memory.

Implement and document KASAN this way. The current implementation is Radix
only.

Despite the limitations, it can still find bugs,
e.g. http://patchwork.ozlabs.org/patch/1103775/

At the moment, this physical memory limit must be set _even for outline
mode_. This may be changed in a later series - a different implementation
could be added for outline mode that dynamically allocates shadow at a
fixed offset. For example, see https://patchwork.ozlabs.org/patch/795211/

Suggested-by: Michael Ellerman <mpe@xxxxxxxxxxxxxx>
Cc: Balbir Singh <bsingharora@xxxxxxxxx> # ppc64 out-of-line radix version
Cc: Christophe Leroy <christophe.leroy@xxxxxx> # ppc32 version
Signed-off-by: Daniel Axtens <dja@xxxxxxxxxx>

---
Changes since v3:
ÂÂ - Address further feedback from Christophe.
ÂÂ - Drop changes to stack walking, it looks like the issue I observed is
ÂÂÂÂ related to that particular stack, not stack-walking generally.

Changes since v2:

ÂÂ - Address feedback from Christophe around cleanups and docs.
ÂÂ - Address feedback from Balbir: at this point I don't have a good solution
ÂÂÂÂ for the issues you identify around the limitations of the inline implementation
ÂÂÂÂ but I think that it's worth trying to get the stack instrumentation support.
ÂÂÂÂ I'm happy to have an alternative and more flexible outline mode - I had
ÂÂÂÂ envisoned this would be called 'lightweight' mode as it imposes fewer restrictions.
ÂÂÂÂ I've linked to your implementation. I think it's best to add it in a follow-up series.
ÂÂ - Made the default PHYS_MEM_SIZE_FOR_KASAN value 1024MB. I think most people have
ÂÂÂÂ guests with at least that much memory in the Radix 64s case so it's a much
ÂÂÂÂ saner default - it means that if you just turn on KASAN without reading the
ÂÂÂÂ docs you're much more likely to have a bootable kernel, which you will never
ÂÂÂÂ have if the value is set to zero! I'm happy to bikeshed the value if we want.

Changes since v1:
ÂÂ - Landed kasan vmalloc support upstream
ÂÂ - Lots of feedback from Christophe.

Changes since the rfc:

ÂÂ - Boots real and virtual hardware, kvm works.

ÂÂ - disabled reporting when we're checking the stack for exception
ÂÂÂÂ frames. The behaviour isn't wrong, just incompatible with KASAN.

ÂÂ - Documentation!

ÂÂ - Dropped old module stuff in favour of KASAN_VMALLOC.

The bugs with ftrace and kuap were due to kernel bloat pushing
prom_init calls to be done via the plt. Because we did not have
a relocatable kernel, and they are done very early, this caused
everything to explode. Compile with CONFIG_RELOCATABLE!
---
ÂÂ Documentation/dev-tools/kasan.rstÂÂÂÂÂÂÂÂÂÂÂ |ÂÂ 8 +-
ÂÂ Documentation/powerpc/kasan.txtÂÂÂÂÂÂÂÂÂÂÂÂÂ | 112 ++++++++++++++++++-
ÂÂ arch/powerpc/KconfigÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ |ÂÂ 2 +
ÂÂ arch/powerpc/Kconfig.debugÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ |Â 21 ++++
ÂÂ arch/powerpc/MakefileÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ |Â 11 ++
ÂÂ arch/powerpc/include/asm/book3s/64/hash.hÂÂÂ |ÂÂ 4 +
ÂÂ arch/powerpc/include/asm/book3s/64/pgtable.h |ÂÂ 7 ++
ÂÂ arch/powerpc/include/asm/book3s/64/radix.hÂÂ |ÂÂ 5 +
ÂÂ arch/powerpc/include/asm/kasan.hÂÂÂÂÂÂÂÂÂÂÂÂ |Â 21 +++-
ÂÂ arch/powerpc/kernel/prom.cÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ |Â 61 +++++++++-
ÂÂ arch/powerpc/mm/kasan/MakefileÂÂÂÂÂÂÂÂÂÂÂÂÂÂ |ÂÂ 1 +
ÂÂ arch/powerpc/mm/kasan/init_book3s_64.cÂÂÂÂÂÂ |Â 70 ++++++++++++
ÂÂ arch/powerpc/platforms/Kconfig.cputypeÂÂÂÂÂÂ |ÂÂ 1 +
ÂÂ 13 files changed, 316 insertions(+), 8 deletions(-)
ÂÂ create mode 100644 arch/powerpc/mm/kasan/init_book3s_64.c

diff --git a/arch/powerpc/include/asm/kasan.h b/arch/powerpc/include/asm/kasan.h
index 296e51c2f066..f18268cbdc33 100644
--- a/arch/powerpc/include/asm/kasan.h
+++ b/arch/powerpc/include/asm/kasan.h
@@ -2,6 +2,9 @@
ÂÂ #ifndef __ASM_KASAN_H
ÂÂ #define __ASM_KASAN_H
+#include <asm/page.h>
+#include <asm/pgtable.h>

What do you need asm/pgtable.h for ?

Build failure due to circular inclusion of asm/pgtable.h:

I see there's a lot of ppc32 stuff, I clearly need to bite the bullet
and get a ppc32 toolchain so I can squash these without chewing up any
more of your time. I'll sort that out and send a new spin.


I'm using a powerpc64 toolchain to build both ppc32 and ppc64 kernels (from https://mirrors.edge.kernel.org/pub/tools/crosstool/ )


Another thing, did you test PTDUMP stuff with KASAN ? It looks like KASAN address markers don't depend on PPC32, but are only initialised by populate_markers() for PPC32.

Regarding kasan.h, I think we should be able to end up with something where the definition of KASAN_SHADOW_OFFSET should only depend on the existence of CONFIG_KASAN_SHADOW_OFFSET, and where only KASAN_SHADOW_SIZE should depend on the target (ie PPC32 or BOOK3S64)
Everything else should be common. KASAN_END should be START+SIZE.

It looks like what you have called KASAN_SHADOW_SIZE is not similar to what is called KASAN_SHADOW_SIZE for PPC32, as yours only covers the SHADOW_SIZE for linear mem while PPC32 one covers the full space.


More or less something like that:

/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __ASM_KASAN_H
#define __ASM_KASAN_H

#include <asm/page.h>

#ifdef CONFIG_KASAN
#define _GLOBAL_KASAN(fn) _GLOBAL(__##fn)
#define _GLOBAL_TOC_KASAN(fn) _GLOBAL_TOC(__##fn)
#define EXPORT_SYMBOL_KASAN(fn) EXPORT_SYMBOL(__##fn)
#else
#define _GLOBAL_KASAN(fn) _GLOBAL(fn)
#define _GLOBAL_TOC_KASAN(fn) _GLOBAL_TOC(fn)
#define EXPORT_SYMBOL_KASAN(fn)
#endif

#ifndef __ASSEMBLY__

#define KASAN_SHADOW_SCALE_SHIFT 3

#define KASAN_SHADOW_START (KASAN_SHADOW_OFFSET + \
(PAGE_OFFSET >> KASAN_SHADOW_SCALE_SHIFT))

#define KASAN_SHADOW_END (KASAN_SHADOW_START + KASAN_SHADOW_SIZE)


#ifdef CONFIG_KASAN_SHADOW_OFFSET
#define KASAN_SHADOW_OFFSET ASM_CONST(CONFIG_KASAN_SHADOW_OFFSET)
#endif

#ifdef CONFIG_PPC32
#define KASAN_SHADOW_SIZE ((-PAGE_OFFSET) >> KASAN_SHADOW_SCALE_SHIFT)
#endif

#ifdef CONFIG_PPC_BOOK3S_64
#define KASAN_SHADOW_SIZE (ASM_CONST(CONFIG_PHYS_MEM_SIZE_FOR_KASAN) * SZ_1G) >> \
KASAN_SHADOW_SCALE_SHIFT)
#endif


#ifdef CONFIG_KASAN
void kasan_early_init(void);
void kasan_mmu_init(void);
void kasan_init(void);
#else
static inline void kasan_init(void) { }
static inline void kasan_mmu_init(void) { }
#endif

#endif /* __ASSEMBLY */
#endif



Christophe