x86/alternatives: Disable interrupts and sync when optimizing NOPs in place
authorThomas Gleixner <tglx@linutronix.de>
Thu, 7 Dec 2023 19:49:26 +0000 (20:49 +0100)
committerBorislav Petkov (AMD) <bp@alien8.de>
Fri, 15 Dec 2023 18:34:42 +0000 (19:34 +0100)
commit2dc4196138055eb0340231aecac4d78c2ec2bea5
tree8d46bf1b41409e0005c01f2fe005340126c9f072
parent3ea1704a92967834bf0e64ca1205db4680d04048
x86/alternatives: Disable interrupts and sync when optimizing NOPs in place

apply_alternatives() treats alternatives with the ALT_FLAG_NOT flag set
special as it optimizes the existing NOPs in place.

Unfortunately, this happens with interrupts enabled and does not provide any
form of core synchronization.

So an interrupt hitting in the middle of the update and using the affected code
path will observe a half updated NOP and crash and burn. The following
3 NOP sequence was observed to expose this crash halfway reliably under QEMU
  32bit:

   0x90 0x90 0x90

which is replaced by the optimized 3 byte NOP:

   0x8d 0x76 0x00

So an interrupt can observe:

   1) 0x90 0x90 0x90 nop nop nop
   2) 0x8d 0x90 0x90 undefined
   3) 0x8d 0x76 0x90 lea    -0x70(%esi),%esi
   4) 0x8d 0x76 0x00 lea     0x0(%esi),%esi

Where only #1 and #4 are true NOPs. The same problem exists for 64bit obviously.

Disable interrupts around this NOP optimization and invoke sync_core()
before re-enabling them.

Fixes: 270a69c4485d ("x86/alternative: Support relocations in alternatives")
Reported-by: Paul Gortmaker <paul.gortmaker@windriver.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/r/ZT6narvE%2BLxX%2B7Be@windriver.com
arch/x86/kernel/alternative.c