Skip to contentSkip to conversation

x86/alternatives: Guard NOPs optimization

PATCH New Borislav Petkov 2 participants 2 messages Apr 4, 2015 – Apr 7, 2015 x86

lore
Share & tools
lore

Borislav Petkov

author maintainer
#0 lore raw

PATCH 1/1 x86/alternatives: Guard NOPs optimization

From: Borislav Petkov <[email protected]>

Take a look at the first insn byte before optimizing the NOP - there might be something else already, like the ALTERNATIVE_2() in rdtsc_barrier() which nops out on AMD even though we just patched in an MFENCE.

This happens because the alternatives sees X86_FEATURE_MFENCE_RDTSC, AMD CPUs set it, we patch in the MFENCE and right afterwards it sees X86_FEATURE_LFENCE_RDTSC which AMD CPUs don't set and we blindly optimize the NOP.

Checking whether at least the first byte is 0x90 prevents that.

---
 arch/x86/kernel/alternative.c | 3 +++
 1 file changed, 3 insertions(+)
arch/x86/kernel/alternative.c +3 -0
index 7c4ad005d7a0..aef653193160 100644
@@ -325,6 +325,9 @@ done:
 
 static void __init_or_module optimize_nops(struct alt_instr *a, u8 *instr)
 {
+	if (instr[0] != 0x90)
+		return;
+
 	add_nops(instr + (a->instrlen - a->padlen), a->padlen);
 
 	DUMP_BYTES(instr, a->instrlen, "%p: [%d:%d) optimized NOPs: ",
Signature · 1 line
2.3.3
#1 lore raw
x86/alternatives: Guard NOPs optimization
Commit-ID:  69df353ff305805fc16082d0c5bfa6e20fa8b863
Gitweb:     http://git.kernel.org/tip/69df353ff305805fc16082d0c5bfa6e20fa8b863
Author:     Borislav Petkov <[email protected]>
AuthorDate: Sat, 4 Apr 2015 23:07:42 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Mon, 6 Apr 2015 09:24:09 +0200

x86/alternatives: Guard NOPs optimization

Take a look at the first instruction byte before optimizing the NOP - there might be something else there already, like the ALTERNATIVE_2() in rdtsc_barrier() which NOPs out on AMD even though we just patched in an MFENCE.

This happens because the alternatives sees X86_FEATURE_MFENCE_RDTSC, AMD CPUs set it, we patch in the MFENCE and right afterwards it sees X86_FEATURE_LFENCE_RDTSC which AMD CPUs don't set and we blindly optimize the NOP.

Checking whether at least the first byte is 0x90 prevents that.

Cc Andy Lutomirski <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Brian Gerst <[email protected]>
Cc Denys Vlasenko <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Thomas Gleixner <[email protected]>
---
 arch/x86/kernel/alternative.c | 3 +++
 1 file changed, 3 insertions(+)
arch/x86/kernel/alternative.c +3 -0
index 7c4ad00..aef6531 100644
@@ -325,6 +325,9 @@ done:
 
 static void __init_or_module optimize_nops(struct alt_instr *a, u8 *instr)
 {
+	if (instr[0] != 0x90)
+		return;
+
 	add_nops(instr + (a->instrlen - a->padlen), a->padlen);
 
 	DUMP_BYTES(instr, a->instrlen, "%p: [%d:%d) optimized NOPs: ",