Skip to contentSkip to conversation

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

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

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

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

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 Peter Zijlstra (Intel) <[email protected]>
Cc Thomas Gleixner <[email protected]>

---

v2:
- move the call to kmsan_unpoison_entry_regs() after the instrumentation_begin(), as suggested by Peter Zijlstra

---
 arch/x86/kernel/traps.c | 8 ++++++++
 1 file changed, 8 insertions(+)
arch/x86/kernel/traps.c +8 -0
index 178015a820f08..66701b4d99acd 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>
@@ -308,6 +309,13 @@ static noinstr bool handle_bug(struct pt_regs *regs)
 	 * All lies, just get the WARN/BUG out.
 	 */
 	instrumentation_begin();
+
+	/*
+	 * 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);
 	/*
 	 * Since we're emulating a CALL with exceptions, restore the interrupt
 	 * state to what it was at the exception site.
Signature · 1 line
2.38.1.273.g43a17bfeac-goog
Acked-by on v2 1/1
#1 lore raw
Quoted 20 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.

Cc Andrew Morton <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra (Intel) <[email protected]>
Cc Thomas Gleixner <[email protected]>
Quoted 5 lines

---

v2:
- move the call to kmsan_unpoison_entry_regs() after the instrumentation_begin(), as suggested by Peter Zijlstra

Thanks!