entry: Fix KMSAN false positives in IRQ and NMI exit code
Subsystems (2)
PATCH 1/1 entry: Fix KMSAN false positives in IRQ and NMI exit code
syzbot reported a KMSAN uninit-value warning in irqentry_exit_to_kernel_mode_preempt(). This is a false positive caused by the initialization of ret in irqentry_enter_from_kernel_mode() occurring in uninstrumented (noinstr) code. Because the initialization is untracked, KMSAN considers the state variable uninitialized when it is later passed into the instrumented code of irqentry_exit_to_kernel_mode_preempt().
The same issue exists in irqentry_nmi_enter(), where irq_state is initialized in noinstr code and later passed to the instrumented irqentry_nmi_exit().
Fix this by explicitly calling kmsan_unpoison_memory() on the ret and irq_state objects inside the instrumentation_begin() blocks of irqentry_enter_from_kernel_mode() and irqentry_nmi_enter(), respectively, immediately alongside the kmsan_unpoison_entry_regs() calls.
--- include/linux/irq-entry-common.h | 2 ++ kernel/entry/common.c | 1 + 2 files changed, 3 insertions(+)
include/linux/irq-entry-common.h +2 -0
@@ -427,6 +427,7 @@ static __always_inline irqentry_state_t irqentry_enter_from_kernel_mode(struct pct_irq_enter ();instrumentation_begin ();kmsan_unpoison_entry_regs (regs); +kmsan_unpoison_memory (&ret,sizeof (ret));trace_hardirqs_off_finish ();instrumentation_end ();
@@ -443,6 +444,7 @@ static __always_inline irqentry_state_t irqentry_enter_from_kernel_mode(struct plockdep_hardirqs_off (CALLER_ADDR0 );instrumentation_begin ();kmsan_unpoison_entry_regs (regs); +kmsan_unpoison_memory (&ret,sizeof (ret));rcu_irq_enter_check_tick ();trace_hardirqs_off_finish ();instrumentation_end ();
kernel/entry/common.c +1 -0
@@ -177,6 +177,7 @@ irqentry_state_t noinstr irqentry_nmi_enter(struct pt_regs *regs)instrumentation_begin ();kmsan_unpoison_entry_regs (regs); +kmsan_unpoison_memory (&irq_state,sizeof (irq_state));trace_hardirqs_off_finish ();ftrace_nmi_enter ();instrumentation_end ();
Signature · 1 line
2.54.0.563.g4f69b47b94-goog
For some reason, the entry maintainers (Thomas, Peter, Andy) weren't on Cc. I've added them now, but given the various subtle concerns in this code, please make sure that they are Cc'd in future.
There are a some entry fixes scheduled to go through the tip tree in the near future, so this should probably be picked up with those and go via the tip tree.
Minor comments below.
Quoted 12 lines · Alexander Potapenko
syzbot reported a KMSAN uninit-value warning in
irqentry_exit_to_kernel_mode_preempt(). This is a false positive caused by the initialization ofretinirqentry_enter_from_kernel_mode()occurring in uninstrumented (noinstr) code. Because the initialization is untracked, KMSAN considers the state variable uninitialized when it is later passed into the instrumented code ofirqentry_exit_to_kernel_mode_preempt().The same issue exists in
irqentry_nmi_enter(), whereirq_stateis initialized in noinstr code and later passed to the instrumentedirqentry_nmi_exit().
Quoted 6 lines
Fix this by explicitly calling
kmsan_unpoison_memory()on theretandirq_stateobjects inside theinstrumentation_begin()blocks ofirqentry_enter_from_kernel_mode()andirqentry_nmi_enter(), respectively, immediately alongside thekmsan_unpoison_entry_regs()calls.Fixes c5538d0141b3 ("entry: Split kernel mode logic from irqentry_{enter,exit}()")
Surely that should be:
041aa7a85390 ("entry: Split preemption from irqentry_exit_to_kernel_mode()")
... ?
That's the commit which adds irqentry_exit_to_kernel_mode_preempt().
The commit which split the logic kept everything as noinstr (or __always_inline only called from noinstr), so I don't think that commit alone introduced any breakage, but maybe I'm missing something? Did a bisect finger that?
Other than the above, the patch below looks right to me.
Mark.
Quoted 50 lines
Fixes 6cae637fa26d ("entry: kmsan: introduce kmsan_unpoison_entry_regs()")Cc Dmitry Vyukov <[email protected]>Cc Jinjie Ruan <[email protected]>Cc Kuniyuki Iwashima <[email protected]>Cc Matthieu Baerts (NGI0) <[email protected]>Cc Mark Rutland <[email protected]>Cc Paolo Abeni <[email protected]>Reported-by [email protected]Signed-off-by Alexander Potapenko <[email protected]>--- include/linux/irq-entry-common.h | 2 ++ kernel/entry/common.c | 1 + 2 files changed, 3 insertions(+)include/linux/irq-entry-common.h +2
-0@@ -427,6 +427,7 @@ static __always_inline irqentry_state_t irqentry_enter_from_kernel_mode(struct pct_irq_enter ();instrumentation_begin ();kmsan_unpoison_entry_regs (regs); +kmsan_unpoison_memory (&ret,sizeof (ret));trace_hardirqs_off_finish ();instrumentation_end ();@@ -443,6 +444,7 @@ static __always_inline irqentry_state_t irqentry_enter_from_kernel_mode(struct plockdep_hardirqs_off (CALLER_ADDR0 );instrumentation_begin ();kmsan_unpoison_entry_regs (regs); +kmsan_unpoison_memory (&ret,sizeof (ret));rcu_irq_enter_check_tick ();trace_hardirqs_off_finish ();instrumentation_end ();kernel/entry/common.c +1
-0@@ -177,6 +177,7 @@ irqentry_state_t noinstr irqentry_nmi_enter(struct pt_regs *regs)instrumentation_begin ();kmsan_unpoison_entry_regs (regs); +kmsan_unpoison_memory (&irq_state,sizeof (irq_state));trace_hardirqs_off_finish ();ftrace_nmi_enter ();instrumentation_end ();Signature · 1 line
2.54.0.563.g4f69b47b94-goog