kmsan: core: kmsan_in_runtime() should return true in NMI context
Subsystems (5)
Alexander Potapenko
maintainerPATCH 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
@@ -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
Alexander Potapenko
maintainerPATCH 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.
--- arch/x86/lib/usercopy.c | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/lib/usercopy.c +3 -0
@@ -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
Re: [PATCH 2/5] x86/uaccess: instrument copy_from_user_nmi()
Quoted 11 lines · Alexander Potapenko
Make sure usercopy hooks from
linux/instrumented.hare invoked forcopy_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]>Signed-off-by Alexander Potapenko <[email protected]>
Quoted 29 lines
--- arch/x86/lib/usercopy.c | 3 +++ 1 file changed, 3 insertions(+)arch/x86/lib/usercopy.c +3
-0@@ -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
Alexander Potapenko
maintainerPATCH 3/5 Kconfig.debug: ensure early check for KMSAN in CONFIG_KMSAN_WARN
In reply to Alexander PotapenkoAs 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.
--- lib/Kconfig.debug | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/Kconfig.debug +1 -1
@@ -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
Alexander Potapenko
maintainerPATCH 4/5 kmsan: make sure PREEMPT_RT is off
In reply to Alexander PotapenkoAs 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
@@ -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
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 whenPREEMPT_RTis on, because in that mode evenGFP_ATOMICallocations 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
Cc Andrew Morton <[email protected]>Cc Dmitry Vyukov <[email protected]>Cc Marco Elver <[email protected]>Cc Peter Zijlstra (Intel) <[email protected]>Signed-off-by Alexander Potapenko <[email protected]>
Quoted 19 lines
--- lib/Kconfig.kmsan | 1 + 1 file changed, 1 insertion(+)lib/Kconfig.kmsan +1
-0@@ -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 helpSignature · 1 line
2.38.1.273.g43a17bfeac-goog
Alexander Potapenko
maintainerPATCH 5/5 x86/traps: avoid KMSAN bugs originating from handle_bug()
In reply to Alexander PotapenkoThere 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.
--- arch/x86/kernel/traps.c | 7 +++++++ 1 file changed, 7 insertions(+)
arch/x86/kernel/traps.c +7 -0
@@ -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
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_ophandler 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_regspassed to the interrupt handler is never unpoisoned by KMSAN (this is normally done inirqentry_enter()), which leads to false positives insidehandle_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.
Alexander Potapenko
maintainerRe: [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_ophandler 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_regspassed to the interrupt handler is never unpoisoned by KMSAN (this is normally done inirqentry_enter()), which leads to false positives insidehandle_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
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_ophandler 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_regspassed to the interrupt handler is never unpoisoned by KMSAN (this is normally done inirqentry_enter()), which leads to false positives insidehandle_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.
Re: [PATCH 5/5] x86/traps: avoid KMSAN bugs originating from handle_bug()
In reply to Alexander PotapenkoQuoted 15 lines · Alexander Potapenko
arch/x86/kernel/traps.c +1
-0@@ -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() ?
Alexander Potapenko
maintainerRe: [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@@ -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 theinstrumentation_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
Quoted 9 lines · Alexander Potapenko
Without that, every call to
__msan_poison_alloca()in NMI may end up allocating memory, which is NMI-unsafe.Cc Andrew Morton <[email protected]>Cc Dmitry Vyukov <[email protected]>Cc Marco Elver <[email protected]>Cc Peter Zijlstra (Intel) <[email protected]>Signed-off-by Alexander Potapenko <[email protected]>
Quoted 20 lines
--- mm/kmsan/kmsan.h | 2 ++ 1 file changed, 2 insertions(+)mm/kmsan/kmsan.h +2
-0@@ -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