Skip to contentSkip to conversation

kmsan: core: kmsan_in_runtime() should return true in NMI context

PATCH In review Alexander Potapenko 2 participants 13 messages Nov 2, 2022 – Nov 3, 2022

Subsystems (5)
lore
Share & tools
lore

Alexander Potapenko

author maintainer
#0 lore raw

PATCH 1/5 kmsan: core: kmsan_in_runtime() should return true in NMI context

Without that, every call to __msan_poison_alloca() in NMI may end up allocating memory, which is NMI-unsafe.

---
 mm/kmsan/kmsan.h | 2 ++
 1 file changed, 2 insertions(+)
mm/kmsan/kmsan.h +2 -0
index 961eb658020aa..3cd2050a33e6a 100644
@@ -125,6 +125,8 @@ static __always_inline bool kmsan_in_runtime(void)
 {
 	if ((hardirq_count() >> HARDIRQ_SHIFT) > 1)
 		return true;
+	if (in_nmi())
+		return true;
 	return kmsan_get_context()->kmsan_in_runtime;
 }
 
Signature · 1 line
2.38.1.273.g43a17bfeac-goog
author maintainer
#1 lore raw

PATCH 2/5 x86/uaccess: instrument copy_from_user_nmi()

Make sure usercopy hooks from linux/instrumented.h are invoked for copy_from_user_nmi().
This fixes KMSAN false positives reported when dumping opcodes for a stack trace.

Cc Andrew Morton <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Kees Cook <[email protected]>
Cc Peter Zijlstra (Intel) <[email protected]>
---
 arch/x86/lib/usercopy.c | 3 +++
 1 file changed, 3 insertions(+)
arch/x86/lib/usercopy.c +3 -0
index f1bb186171562..24b48af274173 100644
@@ -6,6 +6,7 @@
 
 #include <linux/uaccess.h>
 #include <linux/export.h>
+#include <linux/instrumented.h>
 
 #include <asm/tlbflush.h>
 
@@ -44,7 +45,9 @@ copy_from_user_nmi(void *to, const void __user *from, unsigned long n)
 	 * called from other contexts.
 	 */
 	pagefault_disable();
+	instrument_copy_from_user_before(to, from, n);
 	ret = raw_copy_from_user(to, from, n);
+	instrument_copy_from_user_after(to, from, n, ret);
 	pagefault_enable();
 
 	return ret;
Signature · 1 line
2.38.1.273.g43a17bfeac-goog
Acked-by on v1 2/5
#2 lore raw

Re: [PATCH 2/5] x86/uaccess: instrument copy_from_user_nmi()

Quoted 11 lines · Alexander Potapenko

Make sure usercopy hooks from linux/instrumented.h are invoked for copy_from_user_nmi().
This fixes KMSAN false positives reported when dumping opcodes for a stack trace.

Cc Andrew Morton <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Kees Cook <[email protected]>
Cc Peter Zijlstra (Intel) <[email protected]>
Quoted 29 lines
---
 arch/x86/lib/usercopy.c | 3 +++
 1 file changed, 3 insertions(+)
arch/x86/lib/usercopy.c +3 -0
index f1bb186171562..24b48af274173 100644
@@ -6,6 +6,7 @@
 
 #include <linux/uaccess.h>
 #include <linux/export.h>
+#include <linux/instrumented.h>
 
 #include <asm/tlbflush.h>
 
@@ -44,7 +45,9 @@ copy_from_user_nmi(void *to, const void __user *from, unsigned long n)
 	 * called from other contexts.
 	 */
 	pagefault_disable();
+	instrument_copy_from_user_before(to, from, n);
 	ret = raw_copy_from_user(to, from, n);
+	instrument_copy_from_user_after(to, from, n, ret);
 	pagefault_enable();
 
 	return ret;
Signature · 1 line
2.38.1.273.g43a17bfeac-goog
author maintainer
#3 lore raw

PATCH 3/5 Kconfig.debug: ensure early check for KMSAN in CONFIG_KMSAN_WARN

In reply to Alexander Potapenko

As pointed out by Masahiro Yamada, Kconfig picks up the first default entry which has true 'if' condition. Hence, the previously added check for KMSAN was never used, because it followed the checks for 64BIT and !64BIT.

Put KMSAN check before others to ensure it is always applied.

Cc Andrew Morton <[email protected]>
Cc Kees Cook <[email protected]>
Cc Masahiro Yamada <[email protected]>
Cc Nick Desaulniers <[email protected]>
Fixes 921757bc9b61 ("Kconfig.debug: disable CONFIG_FRAME_WARN for KMSAN by default")
---
 lib/Kconfig.debug | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/Kconfig.debug +1 -1
index 29280072dc0e4..b4a0988a7ffd2 100644
@@ -395,12 +395,12 @@ endif # DEBUG_INFO
 config FRAME_WARN
 	int "Warn for stack frames larger than"
 	range 0 8192
+	default 0 if KMSAN
 	default 2048 if GCC_PLUGIN_LATENT_ENTROPY
 	default 2048 if PARISC
 	default 1536 if (!64BIT && XTENSA)
 	default 1024 if !64BIT
 	default 2048 if 64BIT
-	default 0 if KMSAN
 	help
 	  Tell the compiler to warn at build time for stack frames larger than this.
 	  Setting this too low will cause a lot of warnings.
Signature · 1 line
2.38.1.273.g43a17bfeac-goog
author maintainer
#4 lore raw

PATCH 4/5 kmsan: make sure PREEMPT_RT is off

In reply to Alexander Potapenko

As pointed out by Peter Zijlstra, __msan_poison_alloca() does not play well with IRQ code when PREEMPT_RT is on, because in that mode even GFP_ATOMIC allocations cannot be performed.

Fixing this would require making stackdepot completely lockless, which is quite challenging and may be excessive for the time being.

Instead, make sure KMSAN is incompatible with PREEMPT_RT, like other debug configs are.

---
 lib/Kconfig.kmsan | 1 +
 1 file changed, 1 insertion(+)
lib/Kconfig.kmsan +1 -0
index b2489dd6503fa..ef2c8f256c57d 100644
@@ -12,6 +12,7 @@ config KMSAN
 	bool "KMSAN: detector of uninitialized values use"
 	depends on HAVE_ARCH_KMSAN && HAVE_KMSAN_COMPILER
 	depends on SLUB && DEBUG_KERNEL && !KASAN && !KCSAN
+	depends on !PREEMPT_RT
 	select STACKDEPOT
 	select STACKDEPOT_ALWAYS_INIT
 	help
Signature · 1 line
2.38.1.273.g43a17bfeac-goog
Acked-by on v1 4/5
#5 lore raw

Re: [PATCH 4/5] kmsan: make sure PREEMPT_RT is off

Quoted 10 lines · Alexander Potapenko

As pointed out by Peter Zijlstra, __msan_poison_alloca() does not play well with IRQ code when PREEMPT_RT is on, because in that mode even GFP_ATOMIC allocations cannot be performed.

Fixing this would require making stackdepot completely lockless, which is quite challenging and may be excessive for the time being.

Instead, make sure KMSAN is incompatible with PREEMPT_RT, like other debug configs are.

Quoted 6 lines
Quoted 19 lines
---
 lib/Kconfig.kmsan | 1 +
 1 file changed, 1 insertion(+)
lib/Kconfig.kmsan +1 -0
index b2489dd6503fa..ef2c8f256c57d 100644
@@ -12,6 +12,7 @@ config KMSAN
 	bool "KMSAN: detector of uninitialized values use"
 	depends on HAVE_ARCH_KMSAN && HAVE_KMSAN_COMPILER
 	depends on SLUB && DEBUG_KERNEL && !KASAN && !KCSAN
+	depends on !PREEMPT_RT
 	select STACKDEPOT
 	select STACKDEPOT_ALWAYS_INIT
 	help
Signature · 1 line
2.38.1.273.g43a17bfeac-goog
author maintainer
#6 lore raw

PATCH 5/5 x86/traps: avoid KMSAN bugs originating from handle_bug()

In reply to Alexander Potapenko

There is a case in exc_invalid_op handler that is executed outside the irqentry_enter()/irqentry_exit() region when an UD2 instruction is used to encode a call to __warn().

In that case the struct pt_regs passed to the interrupt handler is never unpoisoned by KMSAN (this is normally done in irqentry_enter()), which leads to false positives inside handle_bug().

Use kmsan_unpoison_entry_regs() to explicitly unpoison those registers before using them.

Cc Andrew Morton <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Thomas Gleixner <[email protected]>
---
 arch/x86/kernel/traps.c | 7 +++++++
 1 file changed, 7 insertions(+)
arch/x86/kernel/traps.c +7 -0
index 178015a820f08..d3fdec706f1d2 100644
@@ -15,6 +15,7 @@
 #include <linux/context_tracking.h>
 #include <linux/interrupt.h>
 #include <linux/kallsyms.h>
+#include <linux/kmsan.h>
 #include <linux/spinlock.h>
 #include <linux/kprobes.h>
 #include <linux/uaccess.h>
@@ -301,6 +302,12 @@ static noinstr bool handle_bug(struct pt_regs *regs)
 {
 	bool handled = false;
 
+	/*
+	 * Normally @regs are unpoisoned by irqentry_enter(), but handle_bug()
+	 * is a rare case that uses @regs without passing them to
+	 * irqentry_enter().
+	 */
+	kmsan_unpoison_entry_regs(regs);
 	if (!is_valid_bugaddr(regs->ip))
 		return handled;
 
Signature · 1 line
2.38.1.273.g43a17bfeac-goog
#7 lore raw

Re: [PATCH 5/5] x86/traps: avoid KMSAN bugs originating from handle_bug()

Quoted 10 lines · Alexander Potapenko

There is a case in exc_invalid_op handler that is executed outside the irqentry_enter()/irqentry_exit() region when an UD2 instruction is used to encode a call to __warn().

In that case the struct pt_regs passed to the interrupt handler is never unpoisoned by KMSAN (this is normally done in irqentry_enter()), which leads to false positives inside handle_bug().

Use kmsan_unpoison_entry_regs() to explicitly unpoison those registers before using them.

As does poke_int3_handler(); does that need fixing up too? OTOH look *very very* carefully at the contraints there.

author maintainer
#8 lore raw

Re: [PATCH 5/5] x86/traps: avoid KMSAN bugs originating from handle_bug()

Quoted 10 lines · Peter Zijlstra
Quoted 8 lines · Alexander Potapenko

There is a case in exc_invalid_op handler that is executed outside the irqentry_enter()/irqentry_exit() region when an UD2 instruction is used to encode a call to __warn().

In that case the struct pt_regs passed to the interrupt handler is never unpoisoned by KMSAN (this is normally done in irqentry_enter()), which leads to false positives inside handle_bug().

Quoted 5 lines
Quoted 2 lines

Use kmsan_unpoison_entry_regs() to explicitly unpoison those registers before using them.

As does poke_int3_handler(); does that need fixing up too? OTOH look *very very* carefully at the contraints there.

Fortunately poke_int3_handler() is a noinstr function, so KMSAN doesn't add any checks to it.
It also does not pass regs to other instrumented functions, at least for now, so we're good.

Signature · 10 lines
Alexander Potapenko
Software Engineer

Google Germany GmbH
Erika-Mann-Straße, 33
80636 München

Geschäftsführer: Paul Manicle, Liana Sebastian
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg
#9 lore raw

Re: [PATCH 5/5] x86/traps: avoid KMSAN bugs originating from handle_bug()

Quoted 14 lines · Alexander Potapenko
Quoted 13 lines · Peter Zijlstra
Quoted 10 lines · Alexander Potapenko

There is a case in exc_invalid_op handler that is executed outside the irqentry_enter()/irqentry_exit() region when an UD2 instruction is used to encode a call to __warn().

In that case the struct pt_regs passed to the interrupt handler is never unpoisoned by KMSAN (this is normally done in irqentry_enter()), which leads to false positives inside handle_bug().

Use kmsan_unpoison_entry_regs() to explicitly unpoison those registers before using them.

Quoted 7 lines
Quoted 2 lines

As does poke_int3_handler(); does that need fixing up too? OTOH look *very very* carefully at the contraints there.

Fortunately poke_int3_handler() is a noinstr function, so KMSAN doesn't add any checks to it.
It also does not pass regs to other instrumented functions, at least for now, so we're good.

Ah indeed; because it is fully noinstr, nothing will trigger the lack of annotation.

#10 lore raw

Re: [PATCH 5/5] x86/traps: avoid KMSAN bugs originating from handle_bug()

In reply to Alexander Potapenko
Quoted 15 lines · Alexander Potapenko
arch/x86/kernel/traps.c +1 -0
index 178015a820f08..d3fdec706f1d2 100644
@@ -15,6 +15,7 @@
 #include <linux/context_tracking.h>
 #include <linux/interrupt.h>
 #include <linux/kallsyms.h>
+#include <linux/kmsan.h>
 #include <linux/spinlock.h>
 #include <linux/kprobes.h>
 #include <linux/uaccess.h>
@@ -301,6 +302,12 @@ static noinstr bool handle_bug(struct pt_regs *regs)
 {
 	bool handled = false;

arch/x86/kernel/traps.c static noinstr bool handle_bug(struct pt_regs *regs) · line 313

Quoted 10 lines
arch/x86/kernel/traps.c +6 -0
 
+	/*
+	 * Normally @regs are unpoisoned by irqentry_enter(), but handle_bug()
+	 * is a rare case that uses @regs without passing them to
+	 * irqentry_enter().
+	 */
+	kmsan_unpoison_entry_regs(regs);
 	if (!is_valid_bugaddr(regs->ip))
 		return handled;
 

Should we place this kmsan_unpoison_entry_regs() after the instrumentation_begin() ?

author maintainer
#11 lore raw

Re: [PATCH 5/5] x86/traps: avoid KMSAN bugs originating from handle_bug()

Quoted 19 lines · Peter Zijlstra
Quoted 16 lines · Alexander Potapenko
arch/x86/kernel/traps.c +1 -0
index 178015a820f08..d3fdec706f1d2 100644
@@ -15,6 +15,7 @@
 #include <linux/context_tracking.h>
 #include <linux/interrupt.h>
 #include <linux/kallsyms.h>
+#include <linux/kmsan.h>
 #include <linux/spinlock.h>
 #include <linux/kprobes.h>
 #include <linux/uaccess.h>
@@ -301,6 +302,12 @@ static noinstr bool handle_bug(struct pt_regs *regs)
 {
      bool handled = false;
 
Quoted 12 lines

arch/x86/kernel/traps.c static noinstr bool handle_bug(struct pt_regs *regs) · line 313

Quoted 9 lines
arch/x86/kernel/traps.c +6 -0
+     /*
+      * Normally @regs are unpoisoned by irqentry_enter(), but handle_bug()
+      * is a rare case that uses @regs without passing them to
+      * irqentry_enter().
+      */
+     kmsan_unpoison_entry_regs(regs);
      if (!is_valid_bugaddr(regs->ip))
              return handled;
 

Should we place this kmsan_unpoison_entry_regs() after the instrumentation_begin() ?

Agreed, let me send an update.

Signature · 10 lines
Alexander Potapenko
Software Engineer

Google Germany GmbH
Erika-Mann-Straße, 33
80636 München

Geschäftsführer: Paul Manicle, Liana Sebastian
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg
Acked-by on v1 1/5
#12 lore raw
In reply to Alexander Potapenko
Quoted 9 lines · Alexander Potapenko

Without that, every call to __msan_poison_alloca() in NMI may end up allocating memory, which is NMI-unsafe.

Quoted 20 lines
---
 mm/kmsan/kmsan.h | 2 ++
 1 file changed, 2 insertions(+)
mm/kmsan/kmsan.h +2 -0
index 961eb658020aa..3cd2050a33e6a 100644
@@ -125,6 +125,8 @@ static __always_inline bool kmsan_in_runtime(void)
 {
 	if ((hardirq_count() >> HARDIRQ_SHIFT) > 1)
 		return true;
+	if (in_nmi())
+		return true;
 	return kmsan_get_context()->kmsan_in_runtime;
 }
 
Signature · 1 line
2.38.1.273.g43a17bfeac-goog