kmsan: Enable on powerpc
Subsystems (6)
cover kmsan: Enable on powerpc
This series provides the minimal support for Kernal Memory Sanitizer on powerpc pseries le guests. Kernal Memory Sanitizer is a tool which detects uses of uninitialized memory. Currently KMSAN is clang only.
The clang support for powerpc has not yet been merged, the pull request can be found here [1].
In addition to this series, there are a number of changes required in generic kmsan code. These changes are already on mailing lists as part of the series implementing KMSAN for s390 [2]. This series is intended to be rebased on top of the s390 series.
In addition, I found a bug in the rtc driver used on powerpc. I have sent a fix to this in a seperate series [3].
With this series and the two series mentioned above, I can successfully boot pseries le defconfig without KMSAN warnings. I have not tested other powerpc platforms.
[1] https://github.com/llvm/llvm-project/pull/73611
[2] https://lore.kernel.org/linux-mm/[email protected]/
[3] https://lore.kernel.org/linux-rtc/[email protected]/
Nicholas Miehlbradt (13):
kmsan: Export kmsan_handle_dma
hvc: Fix use of uninitialized array in udbg_hvc_putc
powerpc: Disable KMSAN santitization for prom_init, vdso and purgatory
powerpc: Disable CONFIG_DCACHE_WORD_ACCESS when KMSAN is enabled
powerpc: Unpoison buffers populated by hcalls powerpc/pseries/nvram: Unpoison buffer populated by rtas_call powerpc/kprobes: Unpoison instruction in kprobe struct
powerpc: Unpoison pt_regs
powerpc: Disable KMSAN checks on functions which walk the stack
powerpc: Define KMSAN metadata address ranges for vmalloc and ioremap
powerpc: Implement architecture specific KMSAN interface powerpc/string: Add KMSAN support
powerpc: Enable KMSAN on powerpc
arch/powerpc/Kconfig | 3 +- arch/powerpc/include/asm/book3s/64/pgtable.h | 42 +++++++++++++++ arch/powerpc/include/asm/interrupt.h | 2 + arch/powerpc/include/asm/kmsan.h | 51 +++++++++++++++++++ arch/powerpc/include/asm/string.h | 18 ++++++- arch/powerpc/kernel/Makefile | 2 + arch/powerpc/kernel/irq_64.c | 2 + arch/powerpc/kernel/kprobes.c | 2 + arch/powerpc/kernel/module.c | 2 +- arch/powerpc/kernel/process.c | 6 +-- arch/powerpc/kernel/stacktrace.c | 10 ++-- arch/powerpc/kernel/vdso/Makefile | 1 + arch/powerpc/lib/Makefile | 2 + arch/powerpc/lib/mem_64.S | 5 +- arch/powerpc/lib/memcpy_64.S | 2 + arch/powerpc/perf/callchain.c | 2 +- arch/powerpc/platforms/pseries/hvconsole.c | 2 + arch/powerpc/platforms/pseries/nvram.c | 4 ++ arch/powerpc/purgatory/Makefile | 1 + arch/powerpc/sysdev/xive/spapr.c | 3 ++ drivers/tty/hvc/hvc_vio.c | 2 +- mm/kmsan/hooks.c | 1 + .../selftests/powerpc/copyloops/asm/kmsan.h | 0 .../powerpc/copyloops/linux/export.h | 1 + 24 files changed, 152 insertions(+), 14 deletions(-) create mode 100644 arch/powerpc/include/asm/kmsan.h create mode 100644 tools/testing/selftests/powerpc/copyloops/asm/kmsan.h
Signature · 1 line
2.40.1
PATCH 1/13 kmsan: Export kmsan_handle_dma
kmsan_handle_dma is required by virtio drivers. Export kmsan_handle_dma so that the drivers can be compiled as modules.
--- mm/kmsan/hooks.c | 1 + 1 file changed, 1 insertion(+)
mm/kmsan/hooks.c +1 -0
@@ -358,6 +358,7 @@ void kmsan_handle_dma(struct page *page, size_t offset, size_t size, size -= to_go; } } +EXPORT_SYMBOL (kmsan_handle_dma);void kmsan_handle_dma_sg (struct scatterlist *sg,int nents,enum dma_data_direction dir)
Signature · 1 line
2.40.1
Re: [PATCH 01/13] kmsan: Export kmsan_handle_dma
Le 14/12/2023 à 06:55, Nicholas Miehlbradt a écrit :
Quoted 13 lines
kmsan_handle_dmais required by virtio drivers. Exportkmsan_handle_dmaso that the drivers can be compiled as modules.Signed-off-by Nicholas Miehlbradt <[email protected]>--- mm/kmsan/hooks.c | 1 + 1 file changed, 1 insertion(+)mm/kmsan/hooks.c +0
-0
mm/kmsan/hooks.c void kmsan_handle_dma(struct page *page, size_t offset, size_t size, · line 361
Quoted 4 lines
mm/kmsan/hooks.c +1
-0@@ -358,6 +358,7 @@ void kmsan_handle_dma(struct page *page, size_t offset, size_t size, size -= to_go; } } +EXPORT_SYMBOL (kmsan_handle_dma);
virtio is GPL and all exports inside virtio are EXPORT_SYMBOL_GPL(). Should this one be _GPL as well ?
Quoted 3 lines
mm/kmsan/hooks.c +0
-0void kmsan_handle_dma_sg (struct scatterlist *sg,int nents,enum dma_data_direction dir)
PATCH 2/13 hvc: Fix use of uninitialized array in udbg_hvc_putc
In reply to Nicholas MiehlbradtAll elements of bounce_buffer are eventually read and passed to the hypervisor so it should probably be fully initialized.
--- drivers/tty/hvc/hvc_vio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/tty/hvc/hvc_vio.c +1 -1
@@ -227,7 +227,7 @@ static const struct hv_ops hvterm_hvsi_ops = {static void udbg_hvc_putc (char c) {int count =-1 ; -unsigned char bounce_buffer[16 ]; +unsigned char bounce_buffer[16 ] = {0 };if (!hvterm_privs[0 ])return ;
Signature · 1 line
2.40.1
Re: [PATCH 02/13] hvc: Fix use of uninitialized array in udbg_hvc_putc
Le 14/12/2023 à 06:55, Nicholas Miehlbradt a écrit :
Quoted 2 lines
All elements of
bounce_bufferare eventually read and passed to the hypervisor so it should probably be fully initialized.
should or shall ?
Quoted 2 lines
Signed-off-by Nicholas Miehlbradt <[email protected]>
Should be a Fixed: tag ?
Quoted 9 lines
--- drivers/tty/hvc/hvc_vio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/tty/hvc/hvc_vio.c +0
-0
drivers/tty/hvc/hvc_vio.c static const struct hv_ops hvterm_hvsi_ops = { · line 230
Quoted 5 lines
drivers/tty/hvc/hvc_vio.c +1
-1@@ -227,7 +227,7 @@ static const struct hv_ops hvterm_hvsi_ops = {static void udbg_hvc_putc (char c) {int count =-1 ; -unsigned char bounce_buffer[16 ]; +unsigned char bounce_buffer[16 ] = {0 };
Why 16 while we have a count of 1 in the call to hvterm_raw_put_chars() ?
Quoted 3 lines
drivers/tty/hvc/hvc_vio.c +0
-0if (!hvterm_privs[0 ])return ;
Michael Ellerman
reviewerRe: [PATCH 02/13] hvc: Fix use of uninitialized array in udbg_hvc_putc
Quoted 15 lines · Christophe Leroy
Le 14/12/2023 à 06:55, Nicholas Miehlbradt a écrit :
Quoted 2 lines
All elements of
bounce_bufferare eventually read and passed to the hypervisor so it should probably be fully initialized.should or shall ?
Quoted 2 lines
Signed-off-by Nicholas Miehlbradt <[email protected]>Should be a Fixed: tag ?
Quoted 4 lines
--- drivers/tty/hvc/hvc_vio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 12 lines
drivers/tty/hvc/hvc_vio.c static const struct hv_ops hvterm_hvsi_ops = { · line 230
Quoted 10 lines
drivers/tty/hvc/hvc_vio.c +1
-1@@ -227,7 +227,7 @@ static const struct hv_ops hvterm_hvsi_ops = {static void udbg_hvc_putc (char c) {int count =-1 ; -unsigned char bounce_buffer[16 ]; +unsigned char bounce_buffer[16 ] = {0 };Why 16 while we have a count of 1 in the call to
hvterm_raw_put_chars()?
Because hvterm_raw_put_chars() calls hvc_put_chars() which requires a 16 byte buffer, because it passes the buffer directly to firmware which expects a 16 byte buffer.
It's a pretty horrible calling convention, but I guess it's to avoid needing another bounce buffer inside hvc_put_chars().
We should probably do the change below, to at least document the interface better.
cheers
arch/powerpc/include/asm/hvconsole.h +1 -1
@@ -22,7 +22,7 @@* parm is included to conform to put_chars() function pointer template */ extern int hvc_get_chars (uint32_t vtermno,char *buf,int count); -extern int hvc_put_chars (uint32_t vtermno,const char *buf,int count); +extern int hvc_put_chars (uint32_t vtermno,const char buf[16 ],int count);/* Provided by HVC VIO */ void hvc_vio_init_early (void );
arch/powerpc/platforms/pseries/hvconsole.c +1 -1
@@ -52,7 +52,7 @@ EXPORT_SYMBOL(hvc_get_chars);* firmware. Must be at least 16 bytes, even if count is less than 16. * @count: Send this number of characters. */ -int hvc_put_chars (uint32_t vtermno,const char *buf,int count) +int hvc_put_chars (uint32_t vtermno,const char buf[16 ],int count) {unsigned long *lbuf = (unsigned long *) buf;long ret;
drivers/tty/hvc/hvc_vio.c +1 -1
@@ -115,7 +115,7 @@ static int hvterm_raw_get_chars(uint32_t vtermno, char *buf, int count)* you are sending fewer chars. * @count: number of chars to send. */ -static int hvterm_raw_put_chars (uint32_t vtermno,const char *buf,int count) +static int hvterm_raw_put_chars (uint32_t vtermno,const char buf[16 ],int count) {struct hvterm_priv *pv = hvterm_privs[vtermno];
PATCH 3/13 powerpc: Disable KMSAN santitization for prom_init, vdso and purgatory
In reply to Nicholas MiehlbradtOther sanitizers are disabled for these, disable KMSAN too.
prom_init.o can only reference a limited set of external symbols. KMSAN adds additional references which are not permitted so disable sanitization.
--- arch/powerpc/kernel/Makefile | 2 ++ arch/powerpc/kernel/vdso/Makefile | 1 + arch/powerpc/purgatory/Makefile | 1 + 3 files changed, 4 insertions(+)
arch/powerpc/kernel/Makefile +2 -0
@@ -61,6 +61,8 @@ KCSAN_SANITIZE_btext.o := n KCSAN_SANITIZE_paca.o := n KCSAN_SANITIZE_setup_64.o := n +KMSAN_SANITIZE_prom_init.o := n + #ifdef CONFIG_RANDOMIZE_KSTACK_OFFSET # Remove stack protector to avoid triggering unneeded stack canary # checks due to randomize_kstack_offset.
arch/powerpc/kernel/vdso/Makefile +1 -0
@@ -52,6 +52,7 @@ KCOV_INSTRUMENT := n UBSAN_SANITIZE := n KASAN_SANITIZE := n KCSAN_SANITIZE := n +KMSAN_SANITIZE := n ccflags-y := -fno-common -fno-builtin ldflags-y := -Wl,--hash-style=both -nostdlib -shared -z noexecstack $(CLANG_FLAGS)
arch/powerpc/purgatory/Makefile +1 -0
@@ -2,6 +2,7 @@ KASAN_SANITIZE := n KCSAN_SANITIZE := n +KMSAN_SANITIZE := n targets += trampoline_$(BITS).o purgatory.ro
Signature · 1 line
2.40.1
PATCH 4/13 powerpc: Disable CONFIG_DCACHE_WORD_ACCESS when KMSAN is enabled
In reply to Nicholas MiehlbradtWord sized accesses may read uninitialized data when optimizing loads. Disable this optimization when KMSAN is enabled to prevent false positives.
--- arch/powerpc/Kconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/Kconfig +1 -1
@@ -182,7 +182,7 @@ config PPC select BUILDTIME_TABLE_SORT select CLONE_BACKWARDS select CPUMASK_OFFSTACK if NR_CPUS >= 8192 - select DCACHE_WORD_ACCESS if PPC64 && CPU_LITTLE_ENDIAN + select DCACHE_WORD_ACCESS if PPC64 && CPU_LITTLE_ENDIAN && !KMSAN select DMA_OPS_BYPASS if PPC64 select DMA_OPS if PPC64 select DYNAMIC_FTRACE if FUNCTION_TRACER
Signature · 1 line
2.40.1
Re: [PATCH 04/13] powerpc: Disable CONFIG_DCACHE_WORD_ACCESS when KMSAN is enabled
Le 14/12/2023 à 06:55, Nicholas Miehlbradt a écrit :
Quoted 14 lines
Word sized accesses may read uninitialized data when optimizing loads. Disable this optimization when KMSAN is enabled to prevent false positives.
Signed-off-by Nicholas Miehlbradt <[email protected]>--- arch/powerpc/Kconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/powerpc/Kconfig +0
-0
arch/powerpc/Kconfig config PPC · line 188
Quoted 8 lines
arch/powerpc/Kconfig +1
-1@@ -182,7 +182,7 @@ config PPC select BUILDTIME_TABLE_SORT select CLONE_BACKWARDS select CPUMASK_OFFSTACK if NR_CPUS >= 8192 - select DCACHE_WORD_ACCESS if PPC64 && CPU_LITTLE_ENDIAN + select DCACHE_WORD_ACCESS if PPC64 && CPU_LITTLE_ENDIAN && !KMSAN select DMA_OPS_BYPASS if PPC64 select DMA_OPS if PPC64 select DYNAMIC_FTRACE if FUNCTION_TRACER
Seems like all archs do this. Maybe a better approach would be to define a HAVE_DCACHE_WORD_ACCESS that is selected by arches, and then the core part select DCACHE_WORD_ACCESS when HAVE_DCACHE_WORD_ACCESS && !KMSAN
Christophe
PATCH 5/13 powerpc: Unpoison buffers populated by hcalls
In reply to Nicholas Miehlbradtplpar_hcall provides to the hypervisor a buffer where return data should be placed. The hypervisor initializes the buffers which is not visible to KMSAN so unpoison them manually.
--- arch/powerpc/platforms/pseries/hvconsole.c | 2 ++ arch/powerpc/sysdev/xive/spapr.c | 3 +++ 2 files changed, 5 insertions(+)
arch/powerpc/platforms/pseries/hvconsole.c +2 -0
@@ -13,6 +13,7 @@#include <linux/kernel.h> #include <linux/export.h> #include <linux/errno.h> +#include <linux/kmsan-checks.h> #include <asm/hvcall.h> #include <asm/hvconsole.h> #include <asm/plpar_wrappers.h>
@@ -32,6 +33,7 @@ int hvc_get_chars(uint32_t vtermno, char *buf, int count)unsigned long *lbuf = (unsigned long *)buf; ret =plpar_hcall (H_GET_TERM_CHAR , retbuf, vtermno); +kmsan_unpoison_memory (retbuf,sizeof (retbuf)); lbuf[0 ] =be64_to_cpu (retbuf[1 ]); lbuf[1 ] =be64_to_cpu (retbuf[2 ]);
arch/powerpc/sysdev/xive/spapr.c +3 -0
@@ -20,6 +20,7 @@#include <linux/mm.h> #include <linux/delay.h> #include <linux/libfdt.h> +#include <linux/kmsan-checks.h> #include <asm/machdep.h> #include <asm/prom.h>
@@ -191,6 +192,8 @@ static long plpar_int_get_source_info(unsigned long flags,return rc; } +kmsan_unpoison_memory (retbuf,sizeof (retbuf)); + *src_flags = retbuf[0 ]; *eoi_page = retbuf[1 ]; *trig_page = retbuf[2 ];
Signature · 1 line
2.40.1
PATCH 6/13 powerpc/pseries/nvram: Unpoison buffer populated by rtas_call
In reply to Nicholas Miehlbradtrtas_call provides a buffer where the return data should be placed. Rtas initializes the buffer which is not visible to KMSAN so unpoison it manually.
--- arch/powerpc/platforms/pseries/nvram.c | 4 ++++ 1 file changed, 4 insertions(+)
arch/powerpc/platforms/pseries/nvram.c +4 -0
@@ -14,6 +14,7 @@#include <linux/ctype.h> #include <linux/uaccess.h> #include <linux/of.h> +#include <linux/kmsan-checks.h> #include <asm/nvram.h> #include <asm/rtas.h> #include <asm/machdep.h>
@@ -41,6 +42,7 @@ static ssize_t pSeries_nvram_read(char *buf, size_t count, loff_t *index)int done;unsigned long flags;char *p = buf; +size_t l;if (nvram_size ==0 || nvram_fetch ==RTAS_UNKNOWN_SERVICE )
@@ -53,6 +55,7 @@ static ssize_t pSeries_nvram_read(char *buf, size_t count, loff_t *index)if (i + count > nvram_size) count = nvram_size - i; + l = count;spin_lock_irqsave (&nvram_lock, flags);for (; count !=0 ; count -= len) {
@@ -73,6 +76,7 @@ static ssize_t pSeries_nvram_read(char *buf, size_t count, loff_t *index) }spin_unlock_irqrestore (&nvram_lock, flags); +kmsan_unpoison_memory (buf, l); *index = i;return p - buf;
Signature · 1 line
2.40.1
PATCH 7/13 powerpc/kprobes: Unpoison instruction in kprobe struct
In reply to Nicholas MiehlbradtKMSAN does not unpoison the ainsn field of a kprobe struct correctly. Manually unpoison it to prevent false positives.
--- arch/powerpc/kernel/kprobes.c | 2 ++ 1 file changed, 2 insertions(+)
arch/powerpc/kernel/kprobes.c +2 -0
@@ -27,6 +27,7 @@#include <asm/sections.h> #include <asm/inst.h> #include <linux/uaccess.h> +#include <linux/kmsan-checks.h> DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL; DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
@@ -179,6 +180,7 @@ int arch_prepare_kprobe(struct kprobe *p)if (!ret) {patch_instruction (p->ainsn.insn, insn); +kmsan_unpoison_memory (p->ainsn.insn,sizeof (kprobe_opcode_t)); p->opcode =ppc_inst_val (insn); }
Signature · 1 line
2.40.1
Re: [PATCH 07/13] powerpc/kprobes: Unpoison instruction in kprobe struct
Quoted 21 lines · Nicholas Miehlbradt
KMSAN does not unpoison the ainsn field of a kprobe struct correctly. Manually unpoison it to prevent false positives.
Signed-off-by Nicholas Miehlbradt <[email protected]>--- arch/powerpc/kernel/kprobes.c | 2 ++ 1 file changed, 2 insertions(+)arch/powerpc/kernel/kprobes.c +1
-0@@ -27,6 +27,7 @@#include <asm/sections.h> #include <asm/inst.h> #include <linux/uaccess.h> +#include <linux/kmsan-checks.h> DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL; DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
arch/powerpc/kernel/kprobes.c int arch_prepare_kprobe(struct kprobe *p) · line 183
Quoted 4 lines
arch/powerpc/kernel/kprobes.c +1
-0@@ -179,6 +180,7 @@ int arch_prepare_kprobe(struct kprobe *p)if (!ret) {patch_instruction (p->ainsn.insn, insn); +kmsan_unpoison_memory (p->ainsn.insn,sizeof (kprobe_opcode_t));
kprobe_opcode_t is u32, but we could be probing a prefixed instruction. You can pass the instruction length through ppc_inst_len(insn).
- Naveen
PATCH 8/13 powerpc: Unpoison pt_regs
In reply to Nicholas Miehlbradtpt_regs is initialized ppc_save_regs which is implemented in assembly and therefore does not mark the struct as initialized. Unpoison it so that it will not generate false positives.
--- arch/powerpc/include/asm/interrupt.h | 2 ++ arch/powerpc/kernel/irq_64.c | 2 ++ 2 files changed, 4 insertions(+)
arch/powerpc/include/asm/interrupt.h +2 -0
@@ -68,6 +68,7 @@#include <linux/context_tracking.h> #include <linux/hardirq.h> +#include <linux/kmsan.h> #include <asm/cputime.h> #include <asm/firmware.h> #include <asm/ftrace.h>
@@ -170,6 +171,7 @@ static inline void interrupt_enter_prepare(struct pt_regs *regs) __hard_RI_enable(); }/* Enable MSR[RI] early, to support kernel SLB and hash faults */ +kmsan_unpoison_entry_regs (regs);#endif if (!arch_irq_disabled_regs (regs))
arch/powerpc/kernel/irq_64.c +2 -0
@@ -45,6 +45,7 @@#include <linux/vmalloc.h> #include <linux/pgtable.h> #include <linux/static_call.h> +#include <linux/kmsan.h> #include <linux/uaccess.h> #include <asm/interrupt.h>
@@ -117,6 +118,7 @@ static __no_kcsan void __replay_soft_interrupts(void) local_paca->irq_happened |=PACA_IRQ_REPLAYING ;ppc_save_regs (®s); +kmsan_unpoison_entry_regs (®s); regs.softe =IRQS_ENABLED ; regs.msr |=MSR_EE ;
Signature · 1 line
2.40.1
PATCH 9/13 powerpc: Disable KMSAN checks on functions which walk the stack
In reply to Nicholas MiehlbradtFunctions which walk the stack read parts of the stack which cannot be instrumented by KMSAN e.g. the backchain. Disable KMSAN sanitization of these functions to prevent false positives.
--- arch/powerpc/kernel/process.c | 6 +++--- arch/powerpc/kernel/stacktrace.c | 10 ++++++---- arch/powerpc/perf/callchain.c | 2 +- 3 files changed, 10 insertions(+), 8 deletions(-)
arch/powerpc/kernel/process.c +3 -3
@@ -2276,9 +2276,9 @@ static bool empty_user_regs(struct pt_regs *regs, struct task_struct *tsk)static int kstack_depth_to_print =CONFIG_PRINT_STACK_DEPTH ; -void __no_sanitize_address show_stack(struct task_struct *tsk, -unsigned long *stack, -const char *loglvl) +void __no_sanitize_address __no_kmsan_checks show_stack(struct task_struct *tsk, +unsigned long *stack, +const char *loglvl) {unsigned long sp, ip, lr, newsp;int count =0 ;
arch/powerpc/kernel/stacktrace.c +6 -4
@@ -24,8 +24,9 @@#include <asm/paca.h> -void __no_sanitize_address arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie, -struct task_struct *task,struct pt_regs *regs) +void __no_sanitize_address __no_kmsan_checks +arch_stack_walk (stack_trace_consume_fn consume_entry,void *cookie, +struct task_struct *task,struct pt_regs *regs) {unsigned long sp;
@@ -62,8 +63,9 @@ void __no_sanitize_address arch_stack_walk(stack_trace_consume_fn consume_entry,* * If the task is not 'current', the caller *must* ensure the task is inactive. */ -int __no_sanitize_address arch_stack_walk_reliable(stack_trace_consume_fn consume_entry, -void *cookie,struct task_struct *task) +int __no_sanitize_address __no_kmsan_checks +arch_stack_walk_reliable (stack_trace_consume_fn consume_entry,void *cookie, +struct task_struct *task) {unsigned long sp;unsigned long newsp;
arch/powerpc/perf/callchain.c +1 -1
@@ -40,7 +40,7 @@ static int valid_next_sp(unsigned long sp, unsigned long prev_sp)return 0 ; } -void __no_sanitize_address +void __no_sanitize_address __no_kmsan_checksperf_callchain_kernel (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) {unsigned long sp, next_sp;
Signature · 1 line
2.40.1
Re: [PATCH 09/13] powerpc: Disable KMSAN checks on functions which walk the stack
Le 14/12/2023 à 06:55, Nicholas Miehlbradt a écrit :
Quoted 3 lines
Functions which walk the stack read parts of the stack which cannot be instrumented by KMSAN e.g. the backchain. Disable KMSAN sanitization of these functions to prevent false positives.
Do other architectures have to do it as well ?
I don't see it for show_stack(), is that a specific need for powerpc ?
Quoted 66 lines
Signed-off-by Nicholas Miehlbradt <[email protected]>--- arch/powerpc/kernel/process.c | 6 +++--- arch/powerpc/kernel/stacktrace.c | 10 ++++++---- arch/powerpc/perf/callchain.c | 2 +- 3 files changed, 10 insertions(+), 8 deletions(-)arch/powerpc/kernel/process.c +3
-3@@ -2276,9 +2276,9 @@ static bool empty_user_regs(struct pt_regs *regs, struct task_struct *tsk)static int kstack_depth_to_print =CONFIG_PRINT_STACK_DEPTH ; -void __no_sanitize_address show_stack(struct task_struct *tsk, -unsigned long *stack, -const char *loglvl) +void __no_sanitize_address __no_kmsan_checks show_stack(struct task_struct *tsk, +unsigned long *stack, +const char *loglvl) {unsigned long sp, ip, lr, newsp;int count =0 ;arch/powerpc/kernel/stacktrace.c +6
-4@@ -24,8 +24,9 @@#include <asm/paca.h> -void __no_sanitize_address arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie, -struct task_struct *task,struct pt_regs *regs) +void __no_sanitize_address __no_kmsan_checks +arch_stack_walk (stack_trace_consume_fn consume_entry,void *cookie, +struct task_struct *task,struct pt_regs *regs) {unsigned long sp;@@ -62,8 +63,9 @@ void __no_sanitize_address arch_stack_walk(stack_trace_consume_fn consume_entry,* * If the task is not 'current', the caller *must* ensure the task is inactive. */ -int __no_sanitize_address arch_stack_walk_reliable(stack_trace_consume_fn consume_entry, -void *cookie,struct task_struct *task) +int __no_sanitize_address __no_kmsan_checks +arch_stack_walk_reliable (stack_trace_consume_fn consume_entry,void *cookie, +struct task_struct *task) {unsigned long sp;unsigned long newsp;arch/powerpc/perf/callchain.c +1
-1@@ -40,7 +40,7 @@ static int valid_next_sp(unsigned long sp, unsigned long prev_sp)return 0 ; } -void __no_sanitize_address +void __no_sanitize_address __no_kmsan_checksperf_callchain_kernel (struct perf_callchain_entry_ctx *entry,struct pt_regs *regs) {unsigned long sp, next_sp;
Re: [PATCH 09/13] powerpc: Disable KMSAN checks on functions which walk the stack
Quoted 11 lines · Christophe Leroy
Le 14/12/2023 à 06:55, Nicholas Miehlbradt a écrit :
Quoted 3 lines
Functions which walk the stack read parts of the stack which cannot be instrumented by KMSAN e.g. the backchain. Disable KMSAN sanitization of these functions to prevent false positives.
Do other architectures have to do it as well ?
I don't see it for
show_stack(), is that a specific need for powerpc ? Other archs have the annotation on functions called byshow_stack(). For
x86 it's on show_trace_log_lvl() and for s390 it's on __unwind_start() and unwind_next_frame().
Re: [PATCH 09/13] powerpc: Disable KMSAN checks on functions which walk the stack
In reply to Nicholas MiehlbradtQuoted 4 lines · Nicholas Miehlbradt
Functions which walk the stack read parts of the stack which cannot be instrumented by KMSAN e.g. the backchain. Disable KMSAN sanitization of these functions to prevent false positives.
Is the annotation needed to avoid uninitialized access check when reading parts of the stack? Can you provide a false positive example for the commit message?
-aneesh
PATCH 10/13 powerpc: Define KMSAN metadata address ranges for vmalloc and ioremap
In reply to Nicholas MiehlbradtSplits the vmalloc region into four. The first quarter is the new vmalloc region, the second is used to store shadow metadata and the third is used to store origin metadata. The fourth quarter is unused.
Do the same for the ioremap region.
Module data is stored in the vmalloc region so alias the modules metadata addresses to the respective vmalloc metadata addresses. Define MODULES_VADDR and MODULES_END to the start and end of the vmalloc region.
Since MODULES_VADDR was previously only defined on ppc32 targets checks for if this macro is defined need to be updated to include defined(CONFIG_PPC32).
--- arch/powerpc/include/asm/book3s/64/pgtable.h | 42 ++++++++++++++++++++ arch/powerpc/kernel/module.c | 2 +- 2 files changed, 43 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/book3s/64/pgtable.h +42 -0
@@ -249,7 +249,38 @@ enum pgtable_index {extern unsigned long __vmalloc_start;extern unsigned long __vmalloc_end;#define VMALLOC_START __vmalloc_start + +#ifndef CONFIG_KMSAN #define VMALLOC_END __vmalloc_end +#else +/* +* In KMSAN builds vmalloc area is four times smaller, and the remaining 3/4 +* are used to keep the metadata for virtual pages. The memory formerly +* belonging to vmalloc area is now laid out as follows: +* +* 1st quarter: VMALLOC_START to VMALLOC_END - new vmalloc area +* 2nd quarter: KMSAN_VMALLOC_SHADOW_START to +* KMSAN_VMALLOC_SHADOW_START+VMALLOC_LEN - vmalloc area shadow +* 3rd quarter: KMSAN_VMALLOC_ORIGIN_START to +* KMSAN_VMALLOC_ORIGIN_START+VMALLOC_LEN - vmalloc area origins +* 4th quarter: unused +*/ +#define VMALLOC_LEN ((__vmalloc_end - __vmalloc_start) >> 2) +#define VMALLOC_END (VMALLOC_START + VMALLOC_LEN) + +#define KMSAN_VMALLOC_SHADOW_START VMALLOC_END +#define KMSAN_VMALLOC_ORIGIN_START (VMALLOC_END + VMALLOC_LEN) + +/* +* Module metadata is stored in the corresponding vmalloc metadata regions +*/ +#define KMSAN_MODULES_SHADOW_START KMSAN_VMALLOC_SHADOW_START +#define KMSAN_MODULES_ORIGIN_START KMSAN_VMALLOC_ORIGIN_START +#endif /* CONFIG_KMSAN */ + +#define MODULES_VADDR VMALLOC_START +#define MODULES_END VMALLOC_END +#define MODULES_LEN (MODULES_END - MODULES_VADDR)static inline unsigned int ioremap_max_order (void ) {
@@ -264,7 +295,18 @@ extern unsigned long __kernel_io_start;extern unsigned long __kernel_io_end;#define KERN_VIRT_START __kernel_virt_start#define KERN_IO_START __kernel_io_start +#ifndef CONFIG_KMSAN #define KERN_IO_END __kernel_io_end +#else +/* +* In KMSAN builds IO space is 4 times smaller, the remaining space is used to +* store metadata. See comment for vmalloc regions above. +*/ +#define KERN_IO_LEN ((__kernel_io_end - __kernel_io_start) >> 2) +#define KERN_IO_END (KERN_IO_START + KERN_IO_LEN) +#define KERN_IO_SHADOW_START KERN_IO_END +#define KERN_IO_ORIGIN_START (KERN_IO_SHADOW_START + KERN_IO_LEN) +#endif /* !CONFIG_KMSAN */ extern struct page *vmemmap;extern unsigned long pci_io_base;
arch/powerpc/kernel/module.c +1 -1
@@ -107,7 +107,7 @@ __module_alloc(unsigned long size, unsigned long start, unsigned long end, boolvoid *module_alloc (unsigned long size) { -#ifdef MODULES_VADDR +#if defined(MODULES_VADDR ) && defined(CONFIG_PPC32 )unsigned long limit = (unsigned long )_etext -SZ_32M ;void *ptr =NULL ;
Signature · 1 line
2.40.1
Re: [PATCH 10/13] powerpc: Define KMSAN metadata address ranges for vmalloc and ioremap
Le 14/12/2023 à 06:55, Nicholas Miehlbradt a écrit :
Quoted 6 lines
Splits the vmalloc region into four. The first quarter is the new vmalloc region, the second is used to store shadow metadata and the third is used to store origin metadata. The fourth quarter is unused.
Do the same for the ioremap region.
Quoted 8 lines
Module data is stored in the vmalloc region so alias the modules metadata addresses to the respective vmalloc metadata addresses. Define
MODULES_VADDRandMODULES_ENDto the start and end of the vmalloc region.Since
MODULES_VADDRwas previously only defined on ppc32 targets checks for if this macro is defined need to be updated to includedefined(CONFIG_PPC32).
Why ?
In your case MODULES_VADDR is above PAGE_OFFSET so there should be no difference.
Christophe
Quoted 82 lines
Signed-off-by Nicholas Miehlbradt <[email protected]>--- arch/powerpc/include/asm/book3s/64/pgtable.h | 42 ++++++++++++++++++++ arch/powerpc/kernel/module.c | 2 +- 2 files changed, 43 insertions(+), 1 deletion(-)arch/powerpc/include/asm/book3s/64/pgtable.h +42
-0@@ -249,7 +249,38 @@ enum pgtable_index {extern unsigned long __vmalloc_start;extern unsigned long __vmalloc_end;#define VMALLOC_START __vmalloc_start + +#ifndef CONFIG_KMSAN #define VMALLOC_END __vmalloc_end +#else +/* +* In KMSAN builds vmalloc area is four times smaller, and the remaining 3/4 +* are used to keep the metadata for virtual pages. The memory formerly +* belonging to vmalloc area is now laid out as follows: +* +* 1st quarter: VMALLOC_START to VMALLOC_END - new vmalloc area +* 2nd quarter: KMSAN_VMALLOC_SHADOW_START to +* KMSAN_VMALLOC_SHADOW_START+VMALLOC_LEN - vmalloc area shadow +* 3rd quarter: KMSAN_VMALLOC_ORIGIN_START to +* KMSAN_VMALLOC_ORIGIN_START+VMALLOC_LEN - vmalloc area origins +* 4th quarter: unused +*/ +#define VMALLOC_LEN ((__vmalloc_end - __vmalloc_start) >> 2) +#define VMALLOC_END (VMALLOC_START + VMALLOC_LEN) + +#define KMSAN_VMALLOC_SHADOW_START VMALLOC_END +#define KMSAN_VMALLOC_ORIGIN_START (VMALLOC_END + VMALLOC_LEN) + +/* +* Module metadata is stored in the corresponding vmalloc metadata regions +*/ +#define KMSAN_MODULES_SHADOW_START KMSAN_VMALLOC_SHADOW_START +#define KMSAN_MODULES_ORIGIN_START KMSAN_VMALLOC_ORIGIN_START +#endif /* CONFIG_KMSAN */ + +#define MODULES_VADDR VMALLOC_START +#define MODULES_END VMALLOC_END +#define MODULES_LEN (MODULES_END - MODULES_VADDR)static inline unsigned int ioremap_max_order (void ) {@@ -264,7 +295,18 @@ extern unsigned long __kernel_io_start;extern unsigned long __kernel_io_end;#define KERN_VIRT_START __kernel_virt_start#define KERN_IO_START __kernel_io_start +#ifndef CONFIG_KMSAN #define KERN_IO_END __kernel_io_end +#else +/* +* In KMSAN builds IO space is 4 times smaller, the remaining space is used to +* store metadata. See comment for vmalloc regions above. +*/ +#define KERN_IO_LEN ((__kernel_io_end - __kernel_io_start) >> 2) +#define KERN_IO_END (KERN_IO_START + KERN_IO_LEN) +#define KERN_IO_SHADOW_START KERN_IO_END +#define KERN_IO_ORIGIN_START (KERN_IO_SHADOW_START + KERN_IO_LEN) +#endif /* !CONFIG_KMSAN */ extern struct page *vmemmap;extern unsigned long pci_io_base;arch/powerpc/kernel/module.c +1
-1@@ -107,7 +107,7 @@ __module_alloc(unsigned long size, unsigned long start, unsigned long end, boolvoid *module_alloc (unsigned long size) { -#ifdef MODULES_VADDR +#if defined(MODULES_VADDR ) && defined(CONFIG_PPC32 )unsigned long limit = (unsigned long )_etext -SZ_32M ;void *ptr =NULL ;
Re: [PATCH 10/13] powerpc: Define KMSAN metadata address ranges for vmalloc and ioremap
Quoted 18 lines · Christophe Leroy
Le 14/12/2023 à 06:55, Nicholas Miehlbradt a écrit :
Quoted 6 lines
Splits the vmalloc region into four. The first quarter is the new vmalloc region, the second is used to store shadow metadata and the third is used to store origin metadata. The fourth quarter is unused.
Do the same for the ioremap region.
Quoted 8 lines
Module data is stored in the vmalloc region so alias the modules metadata addresses to the respective vmalloc metadata addresses. Define
MODULES_VADDRandMODULES_ENDto the start and end of the vmalloc region.Since
MODULES_VADDRwas previously only defined on ppc32 targets checks for if this macro is defined need to be updated to includedefined(CONFIG_PPC32).
Quoted 7 lines
Why ?
In your case
MODULES_VADDRis abovePAGE_OFFSETso there should be no difference.Christophe
On 64 bit builds the BUILD_BUG always triggers since MODULES_VADDR expands to __vmalloc_start which is defined in a different translation unit. I can restrict the #ifdef CONFIG_PPC32 to just around the BUILD_BUG since as you pointed out there is no difference otherwise.
Quoted 82 lines
Quoted 82 lines
Signed-off-by Nicholas Miehlbradt <[email protected]>--- arch/powerpc/include/asm/book3s/64/pgtable.h | 42 ++++++++++++++++++++ arch/powerpc/kernel/module.c | 2 +- 2 files changed, 43 insertions(+), 1 deletion(-)arch/powerpc/include/asm/book3s/64/pgtable.h +42
-0@@ -249,7 +249,38 @@ enum pgtable_index {extern unsigned long __vmalloc_start;extern unsigned long __vmalloc_end;#define VMALLOC_START __vmalloc_start + +#ifndef CONFIG_KMSAN #define VMALLOC_END __vmalloc_end +#else +/* +* In KMSAN builds vmalloc area is four times smaller, and the remaining 3/4 +* are used to keep the metadata for virtual pages. The memory formerly +* belonging to vmalloc area is now laid out as follows: +* +* 1st quarter: VMALLOC_START to VMALLOC_END - new vmalloc area +* 2nd quarter: KMSAN_VMALLOC_SHADOW_START to +* KMSAN_VMALLOC_SHADOW_START+VMALLOC_LEN - vmalloc area shadow +* 3rd quarter: KMSAN_VMALLOC_ORIGIN_START to +* KMSAN_VMALLOC_ORIGIN_START+VMALLOC_LEN - vmalloc area origins +* 4th quarter: unused +*/ +#define VMALLOC_LEN ((__vmalloc_end - __vmalloc_start) >> 2) +#define VMALLOC_END (VMALLOC_START + VMALLOC_LEN) + +#define KMSAN_VMALLOC_SHADOW_START VMALLOC_END +#define KMSAN_VMALLOC_ORIGIN_START (VMALLOC_END + VMALLOC_LEN) + +/* +* Module metadata is stored in the corresponding vmalloc metadata regions +*/ +#define KMSAN_MODULES_SHADOW_START KMSAN_VMALLOC_SHADOW_START +#define KMSAN_MODULES_ORIGIN_START KMSAN_VMALLOC_ORIGIN_START +#endif /* CONFIG_KMSAN */ + +#define MODULES_VADDR VMALLOC_START +#define MODULES_END VMALLOC_END +#define MODULES_LEN (MODULES_END - MODULES_VADDR)static inline unsigned int ioremap_max_order (void ) {@@ -264,7 +295,18 @@ extern unsigned long __kernel_io_start;extern unsigned long __kernel_io_end;#define KERN_VIRT_START __kernel_virt_start#define KERN_IO_START __kernel_io_start +#ifndef CONFIG_KMSAN #define KERN_IO_END __kernel_io_end +#else +/* +* In KMSAN builds IO space is 4 times smaller, the remaining space is used to +* store metadata. See comment for vmalloc regions above. +*/ +#define KERN_IO_LEN ((__kernel_io_end - __kernel_io_start) >> 2) +#define KERN_IO_END (KERN_IO_START + KERN_IO_LEN) +#define KERN_IO_SHADOW_START KERN_IO_END +#define KERN_IO_ORIGIN_START (KERN_IO_SHADOW_START + KERN_IO_LEN) +#endif /* !CONFIG_KMSAN */ extern struct page *vmemmap;extern unsigned long pci_io_base;arch/powerpc/kernel/module.c +1
-1@@ -107,7 +107,7 @@ __module_alloc(unsigned long size, unsigned long start, unsigned long end, boolvoid *module_alloc (unsigned long size) { -#ifdef MODULES_VADDR +#if defined(MODULES_VADDR ) && defined(CONFIG_PPC32 )unsigned long limit = (unsigned long )_etext -SZ_32M ;void *ptr =NULL ;
Re: [PATCH 10/13] powerpc: Define KMSAN metadata address ranges for vmalloc and ioremap
In reply to Nicholas MiehlbradtQuoted 4 lines · Nicholas Miehlbradt
Splits the vmalloc region into four. The first quarter is the new vmalloc region, the second is used to store shadow metadata and the third is used to store origin metadata. The fourth quarter is unused.
Do we support KMSAN for both hash and radix? If hash is not supported can we then using radix.h for these changes?
Quoted 10 lines
Do the same for the ioremap region.
Module data is stored in the vmalloc region so alias the modules metadata addresses to the respective vmalloc metadata addresses. Define
MODULES_VADDRandMODULES_ENDto the start and end of the vmalloc region.Since
MODULES_VADDRwas previously only defined on ppc32 targets checks for if this macro is defined need to be updated to includedefined(CONFIG_PPC32).
-aneesh
PATCH 11/13 powerpc: Implement architecture specific KMSAN interface
In reply to Nicholas Miehlbradtarch_kmsan_get_meta_or_null finds the metadata addresses for addresses in the ioremap region which is mapped separately on powerpc.
kmsan_vir_addr_valid is the same as virt_addr_valid except excludes the check that addr is less than high_memory since this function can be called on addresses higher than this.
--- arch/powerpc/include/asm/kmsan.h | 44 ++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 arch/powerpc/include/asm/kmsan.h
arch/powerpc/include/asm/kmsan.h new file +44 -0
@@ -0,0 +1,44 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* powerpc KMSAN support. +* +*/ + +#ifndef _ASM_POWERPC_KMSAN_H +#define _ASM_POWERPC_KMSAN_H + +#ifndef __ASSEMBLY__ +#ifndef MODULE + +#include <linux/mmzone.h> +#include <asm/page.h> +#include <asm/book3s/64/pgtable.h> + +/* +* Functions below are declared in the header to make sure they are inlined. +* They all are called from kmsan_get_metadata() for every memory access in +* the kernel, so speed is important here. +*/ + +/* +* No powerpc specific metadata locations +*/ +static inline void *arch_kmsan_get_meta_or_null (void *addr,bool is_origin) +{ +unsigned long addr64 = (unsigned long )addr, off; +if (KERN_IO_START <= addr64 && addr64 <KERN_IO_END ) { + off = addr64 -KERN_IO_START ; +return (void *)off + (is_origin ?KERN_IO_ORIGIN_START :KERN_IO_SHADOW_START ); + }else { +return 0 ; + } +} + +static inline bool kmsan_virt_addr_valid (void *addr) +{ +return (unsigned long )addr >=PAGE_OFFSET &&pfn_valid (virt_to_pfn (addr)); +} + +#endif /* !MODULE */ +#endif /* !__ASSEMBLY__ */ +#endif /* _ASM_POWERPC_KMSAN_H */
Signature · 1 line
2.40.1
Re: [PATCH 11/13] powerpc: Implement architecture specific KMSAN interface
Le 14/12/2023 à 06:55, Nicholas Miehlbradt a écrit :
Quoted 37 lines
arch_kmsan_get_meta_or_nullfinds the metadata addresses for addresses in the ioremap region which is mapped separately on powerpc.
kmsan_vir_addr_validis the same asvirt_addr_validexcept excludes the check that addr is less thanhigh_memorysince this function can be called on addresses higher than this.Signed-off-by Nicholas Miehlbradt <[email protected]>--- arch/powerpc/include/asm/kmsan.h | 44 ++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+)create mode 100644
arch/powerpc/include/asm/kmsan.harch/powerpc/include/asm/kmsan.h new file +18
-0@@ -0,0 +1,44 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* powerpc KMSAN support. +* +*/ + +#ifndef _ASM_POWERPC_KMSAN_H +#define _ASM_POWERPC_KMSAN_H + +#ifndef __ASSEMBLY__ +#ifndef MODULE + +#include <linux/mmzone.h> +#include <asm/page.h> +#include <asm/book3s/64/pgtable.h> + +/* +* Functions below are declared in the header to make sure they are inlined.
arch/powerpc/include/asm/kmsan.h line 28
Quoted 10 lines
arch/powerpc/include/asm/kmsan.h new file +10
-0+* They all are called from kmsan_get_metadata() for every memory access in +* the kernel, so speed is important here. +*/ + +/* +* No powerpc specific metadata locations +*/ +static inline void *arch_kmsan_get_meta_or_null (void *addr,bool is_origin) +{ +unsigned long addr64 = (unsigned long )addr, off;
Missing blank line.
arch/powerpc/include/asm/kmsan.h line 29
Quoted 1 line
arch/powerpc/include/asm/kmsan.h new file +1
-0+if (KERN_IO_START <= addr64 && addr64 <KERN_IO_END ) {
off is only used in that block so it should be declared here, can be done as a single line (followed by a blank line too):
unsigned long off = addr64 - KERN_IO_START;
Quoted 15 lines
arch/powerpc/include/asm/kmsan.h new file +15
-0+ off = addr64 -KERN_IO_START ; +return (void *)off + (is_origin ?KERN_IO_ORIGIN_START :KERN_IO_SHADOW_START ); + } else { +return 0 ; + } +} + +static inline bool kmsan_virt_addr_valid (void *addr) +{ +return (unsigned long )addr >=PAGE_OFFSET &&pfn_valid (virt_to_pfn (addr)); +} + +#endif /* !MODULE */ +#endif /* !__ASSEMBLY__ */ +#endif /* _ASM_POWERPC_KMSAN_H */
PATCH 12/13 powerpc/string: Add KMSAN support
In reply to Nicholas MiehlbradtKMSAN expects functions __mem{set,cpy,move} so add aliases pointing to the respective functions.
Disable use of architecture specific memset{16,32,64} to ensure that metadata is correctly updated and strn{cpy,cmp} and mem{chr,cmp} which are implemented in assembly and therefore cannot be instrumented to propagate/check metadata.
Alias calls to mem{set,cpy,move} to __msan_mem{set,cpy,move} in instrumented code to correctly propagate metadata.
--- arch/powerpc/include/asm/kmsan.h | 7 +++++++ arch/powerpc/include/asm/string.h | 18 ++++++++++++++++-- arch/powerpc/lib/Makefile | 2 ++ arch/powerpc/lib/mem_64.S | 5 ++++- arch/powerpc/lib/memcpy_64.S | 2 ++ .../selftests/powerpc/copyloops/asm/kmsan.h | 0 .../selftests/powerpc/copyloops/linux/export.h | 1 + 7 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 tools/testing/selftests/powerpc/copyloops/asm/kmsan.h
arch/powerpc/include/asm/kmsan.h +7 -0
@@ -7,6 +7,13 @@#ifndef _ASM_POWERPC_KMSAN_H#define _ASM_POWERPC_KMSAN_H +#ifdef CONFIG_KMSAN +#define EXPORT_SYMBOL_KMSAN (fn) SYM_FUNC_ALIAS(__##fn, fn) \ + EXPORT_SYMBOL(__##fn) +#else +#define EXPORT_SYMBOL_KMSAN (fn) +#endif +#ifndef __ASSEMBLY__#ifndef MODULE
arch/powerpc/include/asm/string.h +16 -2
@@ -4,7 +4,7 @@#ifdef __KERNEL__ -#ifndef CONFIG_KASAN +#if !defined(CONFIG_KASAN ) && !defined(CONFIG_KMSAN )#define __HAVE_ARCH_STRNCPY#define __HAVE_ARCH_STRNCMP#define __HAVE_ARCH_MEMCHR
@@ -56,8 +56,22 @@ void *__memmove(void *to, const void *from, __kernel_size_t n);#endif /* CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX */ #endif /* CONFIG_KASAN */ +#ifdef CONFIG_KMSAN + +void *__memset (void *s,int c,__kernel_size_t count); +void *__memcpy (void *to,const void *from,__kernel_size_t n); +void *__memmove (void *to,const void *from,__kernel_size_t n); + +#ifdef __SANITIZE_MEMORY__ +#include <linux/kmsan_string.h> +#define memset __msan_memset +#define memcpy __msan_memcpy +#define memmove __msan_memmove +#endif +#endif /* CONFIG_KMSAN */ +#ifdef CONFIG_PPC64 -#ifndef CONFIG_KASAN +#if !defined(CONFIG_KASAN ) && !defined(CONFIG_KMSAN )#define __HAVE_ARCH_MEMSET32#define __HAVE_ARCH_MEMSET64
arch/powerpc/lib/Makefile +2 -0
@@ -32,9 +32,11 @@ obj-y += code-patching.o feature-fixups.o pmem.o obj-$(CONFIG_CODE_PATCHING_SELFTEST) += test-code-patching.o ifndef CONFIG_KASAN +ifndef CONFIG_KMSAN obj-y += string.o memcmp_$(BITS).o obj-$(CONFIG_PPC32) += strlen_32.o endif +endif obj-$(CONFIG_PPC32) += div64.o copy_32.o crtsavres.o
arch/powerpc/lib/mem_64.S +4 -1
@@ -9,8 +9,9 @@ #include <asm/errno.h> #include <asm/ppc_asm.h> #include <asm/kasan.h> +#include <asm/kmsan.h> -#ifndef CONFIG_KASAN +#if !defined(CONFIG_KASAN) && !defined(CONFIG_KMSAN) _GLOBAL(__memset16) rlwimi r4,r4,16,0,15 /* fall through */
@@ -96,6 +97,7 @@ _GLOBAL_KASAN(memset) blr EXPORT_SYMBOL(memset) EXPORT_SYMBOL_KASAN(memset) +EXPORT_SYMBOL_KMSAN(memset) _GLOBAL_TOC_KASAN(memmove) cmplw 0,r3,r4
@@ -140,3 +142,4 @@ _GLOBAL(backwards_memcpy) b 1b EXPORT_SYMBOL(memmove) EXPORT_SYMBOL_KASAN(memmove) +EXPORT_SYMBOL_KMSAN(memmove)
arch/powerpc/lib/memcpy_64.S +2 -0
@@ -8,6 +8,7 @@ #include <asm/asm-compat.h> #include <asm/feature-fixups.h> #include <asm/kasan.h> +#include <asm/kmsan.h> #ifndef SELFTEST_CASE /* For big-endian, 0 == most CPUs, 1 == POWER6, 2 == Cell */
@@ -228,3 +229,4 @@ END_FTR_SECTION_IFCLR(CPU_FTR_UNALIGNED_LD_STD) #endif EXPORT_SYMBOL(memcpy) EXPORT_SYMBOL_KASAN(memcpy) +EXPORT_SYMBOL_KMSAN(memcpy)
tools/testing/selftests/powerpc/copyloops/asm/kmsan.h new file +0 -0
tools/testing/selftests/powerpc/copyloops/linux/export.h +1 -0
@@ -2,3 +2,4 @@#define EXPORT_SYMBOL (x)#define EXPORT_SYMBOL_GPL (x)#define EXPORT_SYMBOL_KASAN (x) +#define EXPORT_SYMBOL_KMSAN (x)
Signature · 1 line
2.40.1
Re: [PATCH 12/13] powerpc/string: Add KMSAN support
Le 14/12/2023 à 06:55, Nicholas Miehlbradt a écrit :
Quoted 55 lines
KMSAN expects functions __mem{set,cpy,move} so add aliases pointing to the respective functions.
Disable use of architecture specific memset{16,32,64} to ensure that metadata is correctly updated and strn{cpy,cmp} and mem{chr,cmp} which are implemented in assembly and therefore cannot be instrumented to propagate/check metadata.
Alias calls to mem{set,cpy,move} to __msan_mem{set,cpy,move} in instrumented code to correctly propagate metadata.
Signed-off-by Nicholas Miehlbradt <[email protected]>--- arch/powerpc/include/asm/kmsan.h | 7 +++++++ arch/powerpc/include/asm/string.h | 18 ++++++++++++++++-- arch/powerpc/lib/Makefile | 2 ++ arch/powerpc/lib/mem_64.S | 5 ++++- arch/powerpc/lib/memcpy_64.S | 2 ++ .../selftests/powerpc/copyloops/asm/kmsan.h | 0 .../selftests/powerpc/copyloops/linux/export.h | 1 + 7 files changed, 32 insertions(+), 3 deletions(-)create mode 100644
tools/testing/selftests/powerpc/copyloops/asm/kmsan.harch/powerpc/include/asm/kmsan.h +7
-0@@ -7,6 +7,13 @@#ifndef _ASM_POWERPC_KMSAN_H#define _ASM_POWERPC_KMSAN_H +#ifdef CONFIG_KMSAN +#define EXPORT_SYMBOL_KMSAN (fn) SYM_FUNC_ALIAS(__##fn, fn) \ + EXPORT_SYMBOL(__##fn) +#else +#define EXPORT_SYMBOL_KMSAN (fn) +#endif +#ifndef __ASSEMBLY__#ifndef MODULE arch/powerpc/include/asm/string.h +1
-1@@ -4,7 +4,7 @@#ifdef __KERNEL__ -#ifndef CONFIG_KASAN +#if !defined(CONFIG_KASAN ) && !defined(CONFIG_KMSAN )#define __HAVE_ARCH_STRNCPY#define __HAVE_ARCH_STRNCMP#define __HAVE_ARCH_MEMCHR
arch/powerpc/include/asm/string.h void *__memmove(void *to, const void *from, __kernel_size_t n); · line 64
Quoted 9 lines
arch/powerpc/include/asm/string.h +6
-0@@ -56,8 +56,22 @@ void *__memmove(void *to, const void *from, __kernel_size_t n);#endif /* CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX */ #endif /* CONFIG_KASAN */ +#ifdef CONFIG_KMSAN + +void *__memset (void *s,int c,__kernel_size_t count); +void *__memcpy (void *to,const void *from,__kernel_size_t n); +void *__memmove (void *to,const void *from,__kernel_size_t n); +
The same is done for KASAN, can't you reuse it ?
arch/powerpc/include/asm/string.h void *__memmove(void *to, const void *from, __kernel_size_t n); · line 70
Quoted 6 lines
arch/powerpc/include/asm/string.h +6
-0+#ifdef __SANITIZE_MEMORY__ +#include <linux/kmsan_string.h> +#define memset __msan_memset +#define memcpy __msan_memcpy +#define memmove __msan_memmove +#endif
Will that work as you wish ?
What about the calls to memset() or memcpy() emited directly by GCC ?
Quoted 81 lines
arch/powerpc/include/asm/string.h +3
-1+#endif /* CONFIG_KMSAN */ +#ifdef CONFIG_PPC64 -#ifndef CONFIG_KASAN +#if !defined(CONFIG_KASAN ) && !defined(CONFIG_KMSAN )#define __HAVE_ARCH_MEMSET32#define __HAVE_ARCH_MEMSET64arch/powerpc/lib/Makefile +2
-0@@ -32,9 +32,11 @@ obj-y += code-patching.o feature-fixups.o pmem.o obj-$(CONFIG_CODE_PATCHING_SELFTEST) += test-code-patching.o ifndef CONFIG_KASAN +ifndef CONFIG_KMSAN obj-y += string.o memcmp_$(BITS).o obj-$(CONFIG_PPC32) += strlen_32.o endif +endif obj-$(CONFIG_PPC32) += div64.o copy_32.o crtsavres.oarch/powerpc/lib/mem_64.S +4
-1@@ -9,8 +9,9 @@ #include <asm/errno.h> #include <asm/ppc_asm.h> #include <asm/kasan.h> +#include <asm/kmsan.h> -#ifndef CONFIG_KASAN +#if !defined(CONFIG_KASAN) && !defined(CONFIG_KMSAN) _GLOBAL(__memset16) rlwimi r4,r4,16,0,15 /* fall through */@@ -96,6 +97,7 @@ _GLOBAL_KASAN(memset) blr EXPORT_SYMBOL(memset) EXPORT_SYMBOL_KASAN(memset) +EXPORT_SYMBOL_KMSAN(memset) _GLOBAL_TOC_KASAN(memmove) cmplw 0,r3,r4@@ -140,3 +142,4 @@ _GLOBAL(backwards_memcpy) b 1b EXPORT_SYMBOL(memmove) EXPORT_SYMBOL_KASAN(memmove) +EXPORT_SYMBOL_KMSAN(memmove)arch/powerpc/lib/memcpy_64.S +2
-0@@ -8,6 +8,7 @@ #include <asm/asm-compat.h> #include <asm/feature-fixups.h> #include <asm/kasan.h> +#include <asm/kmsan.h> #ifndef SELFTEST_CASE /* For big-endian, 0 == most CPUs, 1 == POWER6, 2 == Cell */@@ -228,3 +229,4 @@ END_FTR_SECTION_IFCLR(CPU_FTR_UNALIGNED_LD_STD) #endif EXPORT_SYMBOL(memcpy) EXPORT_SYMBOL_KASAN(memcpy) +EXPORT_SYMBOL_KMSAN(memcpy)tools/testing/selftests/powerpc/copyloops/asm/kmsan.h new file +0
-0tools/testing/selftests/powerpc/copyloops/linux/export.h +1
-0@@ -2,3 +2,4 @@#define EXPORT_SYMBOL (x)#define EXPORT_SYMBOL_GPL (x)#define EXPORT_SYMBOL_KASAN (x) +#define EXPORT_SYMBOL_KMSAN (x)
Re: [PATCH 12/13] powerpc/string: Add KMSAN support
Quoted 61 lines · Christophe Leroy
Le 14/12/2023 à 06:55, Nicholas Miehlbradt a écrit :
Quoted 58 lines
KMSAN expects functions __mem{set,cpy,move} so add aliases pointing to the respective functions.
Disable use of architecture specific memset{16,32,64} to ensure that metadata is correctly updated and strn{cpy,cmp} and mem{chr,cmp} which are implemented in assembly and therefore cannot be instrumented to propagate/check metadata.
Alias calls to mem{set,cpy,move} to __msan_mem{set,cpy,move} in instrumented code to correctly propagate metadata.
Signed-off-by Nicholas Miehlbradt <[email protected]>--- arch/powerpc/include/asm/kmsan.h | 7 +++++++ arch/powerpc/include/asm/string.h | 18 ++++++++++++++++-- arch/powerpc/lib/Makefile | 2 ++ arch/powerpc/lib/mem_64.S | 5 ++++- arch/powerpc/lib/memcpy_64.S | 2 ++ .../selftests/powerpc/copyloops/asm/kmsan.h | 0 .../selftests/powerpc/copyloops/linux/export.h | 1 + 7 files changed, 32 insertions(+), 3 deletions(-)create mode 100644
tools/testing/selftests/powerpc/copyloops/asm/kmsan.harch/powerpc/include/asm/kmsan.h +7
-0@@ -7,6 +7,13 @@#ifndef _ASM_POWERPC_KMSAN_H#define _ASM_POWERPC_KMSAN_H +#ifdef CONFIG_KMSAN +#define EXPORT_SYMBOL_KMSAN (fn) SYM_FUNC_ALIAS(__##fn, fn) \ + EXPORT_SYMBOL(__##fn) +#else +#define EXPORT_SYMBOL_KMSAN (fn) +#endif +#ifndef __ASSEMBLY__#ifndef MODULE arch/powerpc/include/asm/string.h +1
-1@@ -4,7 +4,7 @@#ifdef __KERNEL__ -#ifndef CONFIG_KASAN +#if !defined(CONFIG_KASAN ) && !defined(CONFIG_KMSAN )#define __HAVE_ARCH_STRNCPY#define __HAVE_ARCH_STRNCMP#define __HAVE_ARCH_MEMCHR@@ -56,8 +56,22 @@ void *__memmove(void *to, const void *from, __kernel_size_t n);#endif /* CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX */ #endif /* CONFIG_KASAN */
Quoted 9 lines
arch/powerpc/include/asm/string.h void *__memmove(void *to, const void *from, __kernel_size_t n); · line 64
Quoted 6 lines
arch/powerpc/include/asm/string.h +6
-0+#ifdef CONFIG_KMSAN + +void *__memset (void *s,int c,__kernel_size_t count); +void *__memcpy (void *to,const void *from,__kernel_size_t n); +void *__memmove (void *to,const void *from,__kernel_size_t n); +The same is done for KASAN, can't you reuse it ?
I tried this but I believe it makes the file more disorganised and difficult to edit since there ends up being a set of definitions for each intersection of features e.g. the definitions needed for both KASAN and KMSAN, just KASAN, just KMSAN, etc.
This way it's clearer what each sanitizer needs and changing definitions for one one sanitizer won't require refactors affecting other sanitizers.
Quoted 10 lines
arch/powerpc/include/asm/string.h void *__memmove(void *to, const void *from, __kernel_size_t n); · line 70
Quoted 6 lines
arch/powerpc/include/asm/string.h +6
-0+#ifdef __SANITIZE_MEMORY__ +#include <linux/kmsan_string.h> +#define memset __msan_memset +#define memcpy __msan_memcpy +#define memmove __msan_memmove +#endif Will that work as you wish ?
What about the calls tomemset()ormemcpy()emited directly by GCC ?
These are handled by the compiler instrumentation which replaces these with calls to the instrumented equivalent.
Quoted 81 lines
Quoted 81 lines
arch/powerpc/include/asm/string.h +3
-1+#endif /* CONFIG_KMSAN */ +#ifdef CONFIG_PPC64 -#ifndef CONFIG_KASAN +#if !defined(CONFIG_KASAN ) && !defined(CONFIG_KMSAN )#define __HAVE_ARCH_MEMSET32#define __HAVE_ARCH_MEMSET64arch/powerpc/lib/Makefile +2
-0@@ -32,9 +32,11 @@ obj-y += code-patching.o feature-fixups.o pmem.o obj-$(CONFIG_CODE_PATCHING_SELFTEST) += test-code-patching.o ifndef CONFIG_KASAN +ifndef CONFIG_KMSAN obj-y += string.o memcmp_$(BITS).o obj-$(CONFIG_PPC32) += strlen_32.o endif +endif obj-$(CONFIG_PPC32) += div64.o copy_32.o crtsavres.oarch/powerpc/lib/mem_64.S +4
-1@@ -9,8 +9,9 @@ #include <asm/errno.h> #include <asm/ppc_asm.h> #include <asm/kasan.h> +#include <asm/kmsan.h> -#ifndef CONFIG_KASAN +#if !defined(CONFIG_KASAN) && !defined(CONFIG_KMSAN) _GLOBAL(__memset16) rlwimi r4,r4,16,0,15 /* fall through */@@ -96,6 +97,7 @@ _GLOBAL_KASAN(memset) blr EXPORT_SYMBOL(memset) EXPORT_SYMBOL_KASAN(memset) +EXPORT_SYMBOL_KMSAN(memset) _GLOBAL_TOC_KASAN(memmove) cmplw 0,r3,r4@@ -140,3 +142,4 @@ _GLOBAL(backwards_memcpy) b 1b EXPORT_SYMBOL(memmove) EXPORT_SYMBOL_KASAN(memmove) +EXPORT_SYMBOL_KMSAN(memmove)arch/powerpc/lib/memcpy_64.S +2
-0@@ -8,6 +8,7 @@ #include <asm/asm-compat.h> #include <asm/feature-fixups.h> #include <asm/kasan.h> +#include <asm/kmsan.h> #ifndef SELFTEST_CASE /* For big-endian, 0 == most CPUs, 1 == POWER6, 2 == Cell */@@ -228,3 +229,4 @@ END_FTR_SECTION_IFCLR(CPU_FTR_UNALIGNED_LD_STD) #endif EXPORT_SYMBOL(memcpy) EXPORT_SYMBOL_KASAN(memcpy) +EXPORT_SYMBOL_KMSAN(memcpy)tools/testing/selftests/powerpc/copyloops/asm/kmsan.h new file +0
-0tools/testing/selftests/powerpc/copyloops/linux/export.h +1
-0@@ -2,3 +2,4 @@#define EXPORT_SYMBOL (x)#define EXPORT_SYMBOL_GPL (x)#define EXPORT_SYMBOL_KASAN (x) +#define EXPORT_SYMBOL_KMSAN (x)
PATCH 13/13 powerpc: Enable KMSAN on powerpc
In reply to Nicholas MiehlbradtEnable KMSAN in the Kconfig.
--- arch/powerpc/Kconfig | 1 + 1 file changed, 1 insertion(+)
arch/powerpc/Kconfig +1 -0
@@ -217,6 +217,7 @@ config PPC select HAVE_ARCH_KASAN_VMALLOC if HAVE_ARCH_KASAN select HAVE_ARCH_KCSAN select HAVE_ARCH_KFENCE if ARCH_SUPPORTS_DEBUG_PAGEALLOC + select HAVE_ARCH_KMSAN if PPC64 select HAVE_ARCH_RANDOMIZE_KSTACK_OFFSET select HAVE_ARCH_WITHIN_STACK_FRAMES select HAVE_ARCH_KGDB
Signature · 1 line
2.40.1
Re: [PATCH 13/13] powerpc: Enable KMSAN on powerpc
Le 14/12/2023 à 06:55, Nicholas Miehlbradt a écrit :
Quoted 12 lines
Enable KMSAN in the
Kconfig.Signed-off-by Nicholas Miehlbradt <[email protected]>--- arch/powerpc/Kconfig | 1 + 1 file changed, 1 insertion(+)arch/powerpc/Kconfig +0
-0
arch/powerpc/Kconfig config PPC · line 220
Quoted 4 lines
arch/powerpc/Kconfig +1
-0@@ -217,6 +217,7 @@ config PPC select HAVE_ARCH_KASAN_VMALLOC if HAVE_ARCH_KASAN select HAVE_ARCH_KCSAN select HAVE_ARCH_KFENCE if ARCH_SUPPORTS_DEBUG_PAGEALLOC + select HAVE_ARCH_KMSAN if PPC64
You said in cover letter you are doing it for "pseries le guests".
Will it also work on BE and also on nohash/64 ?
Quoted 3 lines
arch/powerpc/Kconfig +0
-0select HAVE_ARCH_RANDOMIZE_KSTACK_OFFSET select HAVE_ARCH_WITHIN_STACK_FRAMES select HAVE_ARCH_KGDB
Le 14/12/2023 à 06:55, Nicholas Miehlbradt a écrit :
Quoted 6 lines
This series provides the minimal support for Kernal Memory Sanitizer on powerpc pseries le guests. Kernal Memory Sanitizer is a tool which detects uses of uninitialized memory. Currently KMSAN is clang only.
The clang support for powerpc has not yet been merged, the pull request can be found here [1].
As clang doesn't support it yet, it is probably prematurate to merge that in the kernel.
I have open https://github.com/linuxppc/issues/issues/475 to follow through
In the meantime I flag this series as "change requested" for a revisit it when time comes
Christophe
Quoted 60 lines
In addition to this series, there are a number of changes required in generic kmsan code. These changes are already on mailing lists as part of the series implementing KMSAN for s390 [2]. This series is intended to be rebased on top of the s390 series.
In addition, I found a bug in the rtc driver used on powerpc. I have sent a fix to this in a seperate series [3].
With this series and the two series mentioned above, I can successfully boot pseries le defconfig without KMSAN warnings. I have not tested other powerpc platforms.
[1] https://github.com/llvm/llvm-project/pull/73611
[2] https://lore.kernel.org/linux-mm/[email protected]/
[3] https://lore.kernel.org/linux-rtc/[email protected]/Nicholas Miehlbradt (13):
kmsan: Exportkmsan_handle_dma
hvc: Fix use of uninitialized array inudbg_hvc_putc
powerpc: Disable KMSAN santitization forprom_init, vdso and purgatory
powerpc: DisableCONFIG_DCACHE_WORD_ACCESSwhen KMSAN is enabled
powerpc: Unpoison buffers populated by hcalls powerpc/pseries/nvram: Unpoison buffer populated byrtas_callpowerpc/kprobes: Unpoison instruction in kprobe struct
powerpc: Unpoisonpt_regs
powerpc: Disable KMSAN checks on functions which walk the stack
powerpc: Define KMSAN metadata address ranges for vmalloc and ioremap
powerpc: Implement architecture specific KMSAN interface powerpc/string: Add KMSAN support
powerpc: Enable KMSAN on powerpcarch/powerpc/Kconfig | 3 +- arch/powerpc/include/asm/book3s/64/pgtable.h | 42 +++++++++++++++ arch/powerpc/include/asm/interrupt.h | 2 + arch/powerpc/include/asm/kmsan.h | 51 +++++++++++++++++++ arch/powerpc/include/asm/string.h | 18 ++++++- arch/powerpc/kernel/Makefile | 2 + arch/powerpc/kernel/irq_64.c | 2 + arch/powerpc/kernel/kprobes.c | 2 + arch/powerpc/kernel/module.c | 2 +- arch/powerpc/kernel/process.c | 6 +-- arch/powerpc/kernel/stacktrace.c | 10 ++-- arch/powerpc/kernel/vdso/Makefile | 1 + arch/powerpc/lib/Makefile | 2 + arch/powerpc/lib/mem_64.S | 5 +- arch/powerpc/lib/memcpy_64.S | 2 + arch/powerpc/perf/callchain.c | 2 +- arch/powerpc/platforms/pseries/hvconsole.c | 2 + arch/powerpc/platforms/pseries/nvram.c | 4 ++ arch/powerpc/purgatory/Makefile | 1 + arch/powerpc/sysdev/xive/spapr.c | 3 ++ drivers/tty/hvc/hvc_vio.c | 2 +- mm/kmsan/hooks.c | 1 + .../selftests/powerpc/copyloops/asm/kmsan.h | 0 .../powerpc/copyloops/linux/export.h | 1 + 24 files changed, 152 insertions(+), 14 deletions(-)create mode 100644
arch/powerpc/include/asm/kmsan.h
create mode 100644tools/testing/selftests/powerpc/copyloops/asm/kmsan.h