Skip to content

Commit 2dc4196

Browse files
KAGA-KOKObp3tk0v
authored andcommitted
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: 270a69c ("x86/alternative: Support relocations in alternatives") Reported-by: Paul Gortmaker <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Signed-off-by: Borislav Petkov (AMD) <[email protected]> Acked-by: Peter Zijlstra (Intel) <[email protected]> Cc: [email protected] Link: https://lore.kernel.org/r/ZT6narvE%2BLxX%[email protected]
1 parent 3ea1704 commit 2dc4196

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

arch/x86/kernel/alternative.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,16 @@ static void __init_or_module noinline optimize_nops(u8 *instr, size_t len)
255255
}
256256
}
257257

258+
static void __init_or_module noinline optimize_nops_inplace(u8 *instr, size_t len)
259+
{
260+
unsigned long flags;
261+
262+
local_irq_save(flags);
263+
optimize_nops(instr, len);
264+
sync_core();
265+
local_irq_restore(flags);
266+
}
267+
258268
/*
259269
* In this context, "source" is where the instructions are placed in the
260270
* section .altinstr_replacement, for example during kernel build by the
@@ -438,7 +448,7 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,
438448
* patch if feature is *NOT* present.
439449
*/
440450
if (!boot_cpu_has(a->cpuid) == !(a->flags & ALT_FLAG_NOT)) {
441-
optimize_nops(instr, a->instrlen);
451+
optimize_nops_inplace(instr, a->instrlen);
442452
continue;
443453
}
444454

0 commit comments

Comments
 (0)