Skip to content

Commit 41b7a34

Browse files
daxtensmpe
authored andcommitted
powerpc: Book3S 64-bit outline-only KASAN support
Implement a limited form of KASAN for Book3S 64-bit machines running under the Radix MMU, supporting only outline mode. - Enable the compiler instrumentation to check addresses and maintain the shadow region. (This is the guts of KASAN which we can easily reuse.) - Require kasan-vmalloc support to handle modules and anything else in vmalloc space. - KASAN needs to be able to validate all pointer accesses, but we can't instrument all kernel addresses - only linear map and vmalloc. On boot, set up a single page of read-only shadow that marks all iomap and vmemmap accesses as valid. - Document KASAN in powerpc docs. Background ---------- 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 with translations off ("real mode") during boot, including a lot of generic device-tree parsing code which is used to determine MMU features. [ppc64 mm note: The kernel installs a linear mapping at effective address c000...-c008.... 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.] - Some code - most notably a lot of KVM code - also runs with translations off after boot. - Therefore any offset has to point to memory that is valid with translations on or off. One approach is just to give up on inline instrumentation. This way boot-time checks can be delayed until after the MMU is set is up, and we can just not instrument any code that runs with translations off after booting. Take this approach for now and require outline instrumentation. Previous attempts allowed inline instrumentation. However, they came with some unfortunate restrictions: only physically contiguous memory could be used and it had to be specified at compile time. Maybe we can do better in the future. [[email protected] - Rebased onto 5.17. Note that a kernel with CONFIG_KASAN=y will crash during boot on a machine using HPT translation because not all the entry points to the generic KASAN code are protected with a call to kasan_arch_is_ready().] Originally-by: Balbir Singh <[email protected]> # ppc64 out-of-line radix version Signed-off-by: Daniel Axtens <[email protected]> Signed-off-by: Paul Mackerras <[email protected]> [mpe: Update copyright year and comment formatting] Signed-off-by: Michael Ellerman <[email protected]> Link: https://lore.kernel.org/r/YoTE69OQwiG7z+Gu@cleo
1 parent 2ab2d57 commit 41b7a34

File tree

16 files changed

+232
-7
lines changed

16 files changed

+232
-7
lines changed

Documentation/powerpc/kasan.txt

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
KASAN is supported on powerpc on 32-bit only.
1+
KASAN is supported on powerpc on 32-bit and Radix 64-bit only.
22

33
32 bit support
44
==============
@@ -10,3 +10,49 @@ fixmap area and occupies one eighth of the total kernel virtual memory space.
1010

1111
Instrumentation of the vmalloc area is optional, unless built with modules,
1212
in which case it is required.
13+
14+
64 bit support
15+
==============
16+
17+
Currently, only the radix MMU is supported. There have been versions for hash
18+
and Book3E processors floating around on the mailing list, but nothing has been
19+
merged.
20+
21+
KASAN support on Book3S is a bit tricky to get right:
22+
23+
- It would be good to support inline instrumentation so as to be able to catch
24+
stack issues that cannot be caught with outline mode.
25+
26+
- Inline instrumentation requires a fixed offset.
27+
28+
- Book3S runs code with translations off ("real mode") during boot, including a
29+
lot of generic device-tree parsing code which is used to determine MMU
30+
features.
31+
32+
- Some code - most notably a lot of KVM code - also runs with translations off
33+
after boot.
34+
35+
- Therefore any offset has to point to memory that is valid with
36+
translations on or off.
37+
38+
One approach is just to give up on inline instrumentation. This way boot-time
39+
checks can be delayed until after the MMU is set is up, and we can just not
40+
instrument any code that runs with translations off after booting. This is the
41+
current approach.
42+
43+
To avoid this limitiation, the KASAN shadow would have to be placed inside the
44+
linear mapping, using the same high-bits trick we use for the rest of the linear
45+
mapping. This is tricky:
46+
47+
- We'd like to place it near the start of physical memory. In theory we can do
48+
this at run-time based on how much physical memory we have, but this requires
49+
being able to arbitrarily relocate the kernel, which is basically the tricky
50+
part of KASLR. Not being game to implement both tricky things at once, this
51+
is hopefully something we can revisit once we get KASLR for Book3S.
52+
53+
- Alternatively, we can place the shadow at the _end_ of memory, but this
54+
requires knowing how much contiguous physical memory a system has _at compile
55+
time_. This is a big hammer, and has some unfortunate consequences: inablity
56+
to handle discontiguous physical memory, total failure to boot on machines
57+
with less memory than specified, and that machines with more memory than
58+
specified can't use it. This was deemed unacceptable.

arch/powerpc/Kconfig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ config PPC
109109
# Please keep this list sorted alphabetically.
110110
#
111111
select ARCH_32BIT_OFF_T if PPC32
112+
select ARCH_DISABLE_KASAN_INLINE if PPC_RADIX_MMU
112113
select ARCH_ENABLE_MEMORY_HOTPLUG
113114
select ARCH_ENABLE_MEMORY_HOTREMOVE
114115
select ARCH_HAS_COPY_MC if PPC64
@@ -157,6 +158,7 @@ config PPC
157158
select ARCH_WANT_IPC_PARSE_VERSION
158159
select ARCH_WANT_IRQS_OFF_ACTIVATE_MM
159160
select ARCH_WANT_LD_ORPHAN_WARN
161+
select ARCH_WANTS_NO_INSTR
160162
select ARCH_WEAK_RELEASE_ACQUIRE
161163
select BINFMT_ELF
162164
select BUILDTIME_TABLE_SORT
@@ -188,7 +190,8 @@ config PPC
188190
select HAVE_ARCH_JUMP_LABEL
189191
select HAVE_ARCH_JUMP_LABEL_RELATIVE
190192
select HAVE_ARCH_KASAN if PPC32 && PPC_PAGE_SHIFT <= 14
191-
select HAVE_ARCH_KASAN_VMALLOC if PPC32 && PPC_PAGE_SHIFT <= 14
193+
select HAVE_ARCH_KASAN if PPC_RADIX_MMU
194+
select HAVE_ARCH_KASAN_VMALLOC if HAVE_ARCH_KASAN
192195
select HAVE_ARCH_KFENCE if PPC_BOOK3S_32 || PPC_8xx || 40x
193196
select HAVE_ARCH_KGDB
194197
select HAVE_ARCH_MMAP_RND_BITS

arch/powerpc/Kconfig.debug

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,4 +374,5 @@ config PPC_FAST_ENDIAN_SWITCH
374374
config KASAN_SHADOW_OFFSET
375375
hex
376376
depends on KASAN
377-
default 0xe0000000
377+
default 0xe0000000 if PPC32
378+
default 0xa80e000000000000 if PPC64

arch/powerpc/include/asm/book3s/64/hash.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
#include <asm/book3s/64/hash-4k.h>
1919
#endif
2020

21+
#define H_PTRS_PER_PTE (1 << H_PTE_INDEX_SIZE)
22+
#define H_PTRS_PER_PMD (1 << H_PMD_INDEX_SIZE)
23+
#define H_PTRS_PER_PUD (1 << H_PUD_INDEX_SIZE)
24+
2125
/* Bits to set in a PMD/PUD/PGD entry valid bit*/
2226
#define HASH_PMD_VAL_BITS (0x8000000000000000UL)
2327
#define HASH_PUD_VAL_BITS (0x8000000000000000UL)

arch/powerpc/include/asm/book3s/64/pgtable.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@ extern unsigned long __pmd_frag_size_shift;
232232
#define PTRS_PER_PUD (1 << PUD_INDEX_SIZE)
233233
#define PTRS_PER_PGD (1 << PGD_INDEX_SIZE)
234234

235+
#define MAX_PTRS_PER_PTE ((H_PTRS_PER_PTE > R_PTRS_PER_PTE) ? H_PTRS_PER_PTE : R_PTRS_PER_PTE)
236+
#define MAX_PTRS_PER_PMD ((H_PTRS_PER_PMD > R_PTRS_PER_PMD) ? H_PTRS_PER_PMD : R_PTRS_PER_PMD)
237+
#define MAX_PTRS_PER_PUD ((H_PTRS_PER_PUD > R_PTRS_PER_PUD) ? H_PTRS_PER_PUD : R_PTRS_PER_PUD)
235238
#define MAX_PTRS_PER_PGD (1 << (H_PGD_INDEX_SIZE > RADIX_PGD_INDEX_SIZE ? \
236239
H_PGD_INDEX_SIZE : RADIX_PGD_INDEX_SIZE))
237240

arch/powerpc/include/asm/book3s/64/radix.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
#define RADIX_PMD_SHIFT (PAGE_SHIFT + RADIX_PTE_INDEX_SIZE)
3636
#define RADIX_PUD_SHIFT (RADIX_PMD_SHIFT + RADIX_PMD_INDEX_SIZE)
3737
#define RADIX_PGD_SHIFT (RADIX_PUD_SHIFT + RADIX_PUD_INDEX_SIZE)
38+
39+
#define R_PTRS_PER_PTE (1 << RADIX_PTE_INDEX_SIZE)
40+
#define R_PTRS_PER_PMD (1 << RADIX_PMD_INDEX_SIZE)
41+
#define R_PTRS_PER_PUD (1 << RADIX_PUD_INDEX_SIZE)
42+
3843
/*
3944
* Size of EA range mapped by our pagetables.
4045
*/
@@ -68,11 +73,11 @@
6873
*
6974
*
7075
* 3rd quadrant expanded:
71-
* +------------------------------+
72-
* | |
76+
* +------------------------------+ Highest address (0xc010000000000000)
77+
* +------------------------------+ KASAN shadow end (0xc00fc00000000000)
7378
* | |
7479
* | |
75-
* +------------------------------+ Kernel vmemmap end (0xc010000000000000)
80+
* +------------------------------+ Kernel vmemmap end/shadow start (0xc00e000000000000)
7681
* | |
7782
* | 512TB |
7883
* | |
@@ -91,6 +96,7 @@
9196
* +------------------------------+ Kernel linear (0xc.....)
9297
*/
9398

99+
/* For the sizes of the shadow area, see kasan.h */
94100

95101
/*
96102
* If we store section details in page->flags we can't increase the MAX_PHYSMEM_BITS

arch/powerpc/include/asm/kasan.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,31 @@
3030

3131
#define KASAN_SHADOW_OFFSET ASM_CONST(CONFIG_KASAN_SHADOW_OFFSET)
3232

33+
#ifdef CONFIG_PPC32
3334
#define KASAN_SHADOW_END (-(-KASAN_SHADOW_START >> KASAN_SHADOW_SCALE_SHIFT))
35+
#elif defined(CONFIG_PPC_BOOK3S_64)
36+
/*
37+
* The shadow ends before the highest accessible address
38+
* because we don't need a shadow for the shadow. Instead:
39+
* c00e000000000000 << 3 + a80e000000000000 = c00fc00000000000
40+
*/
41+
#define KASAN_SHADOW_END 0xc00fc00000000000UL
42+
#endif
3443

3544
#ifdef CONFIG_KASAN
45+
#ifdef CONFIG_PPC_BOOK3S_64
46+
DECLARE_STATIC_KEY_FALSE(powerpc_kasan_enabled_key);
47+
48+
static __always_inline bool kasan_arch_is_ready(void)
49+
{
50+
if (static_branch_likely(&powerpc_kasan_enabled_key))
51+
return true;
52+
return false;
53+
}
54+
55+
#define kasan_arch_is_ready kasan_arch_is_ready
56+
#endif
57+
3658
void kasan_early_init(void);
3759
void kasan_mmu_init(void);
3860
void kasan_init(void);

arch/powerpc/kernel/Makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ KASAN_SANITIZE_early_32.o := n
3333
KASAN_SANITIZE_cputable.o := n
3434
KASAN_SANITIZE_prom_init.o := n
3535
KASAN_SANITIZE_btext.o := n
36+
KASAN_SANITIZE_paca.o := n
37+
KASAN_SANITIZE_setup_64.o := n
38+
KASAN_SANITIZE_mce.o := n
39+
KASAN_SANITIZE_mce_power.o := n
40+
41+
# we have to be particularly careful in ppc64 to exclude code that
42+
# runs with translations off, as we cannot access the shadow with
43+
# translations off. However, ppc32 can sanitize this.
44+
ifdef CONFIG_PPC64
45+
KASAN_SANITIZE_traps.o := n
46+
endif
3647

3748
ifdef CONFIG_KASAN
3849
CFLAGS_early_32.o += -DDISABLE_BRANCH_PROFILING

arch/powerpc/kvm/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,8 @@ obj-$(CONFIG_KVM_BOOK3S_64_PR) += kvm-pr.o
131131
obj-$(CONFIG_KVM_BOOK3S_64_HV) += kvm-hv.o
132132

133133
obj-y += $(kvm-book3s_64-builtin-objs-y)
134+
135+
# KVM does a lot in real-mode, and 64-bit Book3S KASAN doesn't support that
136+
ifdef CONFIG_PPC_BOOK3S_64
137+
KASAN_SANITIZE := n
138+
endif

arch/powerpc/mm/book3s64/Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,12 @@ obj-$(CONFIG_PPC_PKEY) += pkeys.o
2424

2525
# Instrumenting the SLB fault path can lead to duplicate SLB entries
2626
KCOV_INSTRUMENT_slb.o := n
27+
28+
# Parts of these can run in real mode and therefore are
29+
# not safe with the current outline KASAN implementation
30+
KASAN_SANITIZE_mmu_context.o := n
31+
KASAN_SANITIZE_pgtable.o := n
32+
KASAN_SANITIZE_radix_pgtable.o := n
33+
KASAN_SANITIZE_radix_tlb.o := n
34+
KASAN_SANITIZE_slb.o := n
35+
KASAN_SANITIZE_pkeys.o := n

arch/powerpc/mm/kasan/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ KASAN_SANITIZE := n
55
obj-$(CONFIG_PPC32) += init_32.o
66
obj-$(CONFIG_PPC_8xx) += 8xx.o
77
obj-$(CONFIG_PPC_BOOK3S_32) += book3s_32.o
8+
obj-$(CONFIG_PPC_BOOK3S_64) += init_book3s_64.o
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* KASAN for 64-bit Book3S powerpc
4+
*
5+
* Copyright 2019-2022, Daniel Axtens, IBM Corporation.
6+
*/
7+
8+
/*
9+
* ppc64 turns on virtual memory late in boot, after calling into generic code
10+
* like the device-tree parser, so it uses this in conjunction with a hook in
11+
* outline mode to avoid invalid access early in boot.
12+
*/
13+
14+
#define DISABLE_BRANCH_PROFILING
15+
16+
#include <linux/kasan.h>
17+
#include <linux/printk.h>
18+
#include <linux/sched/task.h>
19+
#include <linux/memblock.h>
20+
#include <asm/pgalloc.h>
21+
22+
DEFINE_STATIC_KEY_FALSE(powerpc_kasan_enabled_key);
23+
24+
static void __init kasan_init_phys_region(void *start, void *end)
25+
{
26+
unsigned long k_start, k_end, k_cur;
27+
void *va;
28+
29+
if (start >= end)
30+
return;
31+
32+
k_start = ALIGN_DOWN((unsigned long)kasan_mem_to_shadow(start), PAGE_SIZE);
33+
k_end = ALIGN((unsigned long)kasan_mem_to_shadow(end), PAGE_SIZE);
34+
35+
va = memblock_alloc(k_end - k_start, PAGE_SIZE);
36+
for (k_cur = k_start; k_cur < k_end; k_cur += PAGE_SIZE, va += PAGE_SIZE)
37+
map_kernel_page(k_cur, __pa(va), PAGE_KERNEL);
38+
}
39+
40+
void __init kasan_init(void)
41+
{
42+
/*
43+
* We want to do the following things:
44+
* 1) Map real memory into the shadow for all physical memblocks
45+
* This takes us from c000... to c008...
46+
* 2) Leave a hole over the shadow of vmalloc space. KASAN_VMALLOC
47+
* will manage this for us.
48+
* This takes us from c008... to c00a...
49+
* 3) Map the 'early shadow'/zero page over iomap and vmemmap space.
50+
* This takes us up to where we start at c00e...
51+
*/
52+
53+
void *k_start = kasan_mem_to_shadow((void *)RADIX_VMALLOC_END);
54+
void *k_end = kasan_mem_to_shadow((void *)RADIX_VMEMMAP_END);
55+
phys_addr_t start, end;
56+
u64 i;
57+
pte_t zero_pte = pfn_pte(virt_to_pfn(kasan_early_shadow_page), PAGE_KERNEL);
58+
59+
if (!early_radix_enabled()) {
60+
pr_warn("KASAN not enabled as it requires radix!");
61+
return;
62+
}
63+
64+
for_each_mem_range(i, &start, &end)
65+
kasan_init_phys_region((void *)start, (void *)end);
66+
67+
for (i = 0; i < PTRS_PER_PTE; i++)
68+
__set_pte_at(&init_mm, (unsigned long)kasan_early_shadow_page,
69+
&kasan_early_shadow_pte[i], zero_pte, 0);
70+
71+
for (i = 0; i < PTRS_PER_PMD; i++)
72+
pmd_populate_kernel(&init_mm, &kasan_early_shadow_pmd[i],
73+
kasan_early_shadow_pte);
74+
75+
for (i = 0; i < PTRS_PER_PUD; i++)
76+
pud_populate(&init_mm, &kasan_early_shadow_pud[i],
77+
kasan_early_shadow_pmd);
78+
79+
/* map the early shadow over the iomap and vmemmap space */
80+
kasan_populate_early_shadow(k_start, k_end);
81+
82+
/* mark early shadow region as RO and wipe it */
83+
zero_pte = pfn_pte(virt_to_pfn(kasan_early_shadow_page), PAGE_KERNEL_RO);
84+
for (i = 0; i < PTRS_PER_PTE; i++)
85+
__set_pte_at(&init_mm, (unsigned long)kasan_early_shadow_page,
86+
&kasan_early_shadow_pte[i], zero_pte, 0);
87+
88+
/*
89+
* clear_page relies on some cache info that hasn't been set up yet.
90+
* It ends up looping ~forever and blows up other data.
91+
* Use memset instead.
92+
*/
93+
memset(kasan_early_shadow_page, 0, PAGE_SIZE);
94+
95+
static_branch_inc(&powerpc_kasan_enabled_key);
96+
97+
/* Enable error messages */
98+
init_task.kasan_depth = 0;
99+
pr_info("KASAN init done\n");
100+
}
101+
102+
void __init kasan_late_init(void) { }

arch/powerpc/mm/ptdump/ptdump.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <linux/seq_file.h>
2222
#include <asm/fixmap.h>
2323
#include <linux/const.h>
24+
#include <linux/kasan.h>
2425
#include <asm/page.h>
2526
#include <asm/hugetlb.h>
2627

@@ -289,11 +290,11 @@ static void populate_markers(void)
289290
#endif
290291
address_markers[i++].start_address = FIXADDR_START;
291292
address_markers[i++].start_address = FIXADDR_TOP;
293+
#endif /* CONFIG_PPC64 */
292294
#ifdef CONFIG_KASAN
293295
address_markers[i++].start_address = KASAN_SHADOW_START;
294296
address_markers[i++].start_address = KASAN_SHADOW_END;
295297
#endif
296-
#endif /* CONFIG_PPC64 */
297298
}
298299

299300
static int ptdump_show(struct seq_file *m, void *v)

arch/powerpc/platforms/Kconfig.cputype

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ config PPC_BOOK3S_64
104104
select HAVE_MOVE_PUD
105105
select IRQ_WORK
106106
select PPC_64S_HASH_MMU if !PPC_RADIX_MMU
107+
select KASAN_VMALLOC if KASAN
107108

108109
config PPC_BOOK3E_64
109110
bool "Embedded processors"

arch/powerpc/platforms/powernv/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
# SPDX-License-Identifier: GPL-2.0
2+
3+
# nothing that deals with real mode is safe to KASAN
4+
# in particular, idle code runs a bunch of things in real mode
5+
KASAN_SANITIZE_idle.o := n
6+
KASAN_SANITIZE_pci-ioda.o := n
7+
# pnv_machine_check_early
8+
KASAN_SANITIZE_setup.o := n
9+
210
obj-y += setup.o opal-call.o opal-wrappers.o opal.o opal-async.o
311
obj-y += idle.o opal-rtc.o opal-nvram.o opal-lpc.o opal-flash.o
412
obj-y += rng.o opal-elog.o opal-dump.o opal-sysparam.o opal-sensor.o

arch/powerpc/platforms/pseries/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,6 @@ obj-$(CONFIG_PPC_VAS) += vas.o vas-sysfs.o
3434

3535
obj-$(CONFIG_ARCH_HAS_CC_PLATFORM) += cc_platform.o
3636

37+
# nothing that operates in real mode is safe for KASAN
38+
KASAN_SANITIZE_ras.o := n
3739
KASAN_SANITIZE_kexec.o := n

0 commit comments

Comments
 (0)