Skip to contentSkip to conversation

Retpoline: Avoid speculative indirect calls in kernel

PATCH Superseded David Woodhouse 16 participants 66 messages Jan 7, 2018 – Feb 16, 2018

Subsystems (8)

v6 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v6 cover Retpoline: Avoid speculative indirect calls in kernel

This is a mitigation for the 'variant 2' attack described in
https://googleprojectzero.blogspot.com/2018/01/reading-privileged-memory-with-side.html

Using GCC patches available from the hjl/indirect/gcc-7-branch/master branch of https://github.com/hjl-tools/gcc/commits/hjl and by manually patching assembler code, all vulnerable indirect branches (that occur after userspace first runs) are eliminated from the kernel.

They are replaced with a 'retpoline' call sequence which deliberately prevents speculation.

Fedora 27 packages of the updated compiler are available at
https://koji.fedoraproject.org/koji/taskinfo?taskID=24065739

v1: Initial post.
v2: Add CONFIG_RETPOLINE to build kernel without it.
Change warning messages.
Hide modpost warning message
v3: Update to the latest CET-capable retpoline version
Reinstate ALTERNATIVE support
v4: Finish reconciling Andi's and my patch sets, bug fixes.
Exclude objtool support for now
Add 'noretpoline' boot option
Add AMD retpoline alternative
v5: Silence MODVERSIONS warnings
Use pause;jmp loop instead of lfence;jmp
Switch to X86_FEATURE_RETPOLINE positive feature logic Emit thunks inline from assembler macros
Merge AMD support into initial patch
v6: Update to latest GCC patches with no dots in symbols
Fix MODVERSIONS properly(ish)
Fix typo breaking 32-bit, introduced in V5
Never set X86_FEATURE_RETPOLINE_AMD yet, pending confirmation

Andi Kleen (3):
x86/retpoline/irq32: Convert assembler indirect jumps x86/retpoline: Add boot time option to disable retpoline x86/retpoline: Exclude objtool with retpoline

David Woodhouse (7):
x86/retpoline: Add initial retpoline support x86/retpoline/crypto: Convert crypto assembler indirect jumps x86/retpoline/entry: Convert entry assembler indirect jumps x86/retpoline/ftrace: Convert ftrace assembler indirect jumps x86/retpoline/hyperv: Convert assembler indirect jumps x86/retpoline/xen: Convert Xen hypercall indirect jumps x86/retpoline/checksum32: Convert assembler indirect jumps

 Documentation/admin-guide/kernel-parameters.txt |  3 +
 arch/x86/Kconfig                                | 17 ++++-
 arch/x86/Kconfig.debug                          |  6 +-
 arch/x86/Makefile                               | 10 +++
 arch/x86/crypto/aesni-intel_asm.S               |  5 +-
 arch/x86/crypto/camellia-aesni-avx-asm_64.S     |  3 +-
 arch/x86/crypto/camellia-aesni-avx2-asm_64.S    |  3 +-
 arch/x86/crypto/crc32c-pcl-intel-asm_64.S       |  3 +-
 arch/x86/entry/entry_32.S                       |  5 +-
 arch/x86/entry/entry_64.S                       | 12 +++-
 arch/x86/include/asm/asm-prototypes.h           | 25 +++++++
 arch/x86/include/asm/cpufeatures.h              |  2 +
 arch/x86/include/asm/mshyperv.h                 | 18 ++---
 arch/x86/include/asm/nospec-branch.h            | 92 +++++++++++++++++++++++++
 arch/x86/include/asm/xen/hypercall.h            |  5 +-
 arch/x86/kernel/cpu/common.c                    |  3 +
 arch/x86/kernel/cpu/intel.c                     | 11 +++
 arch/x86/kernel/ftrace_32.S                     |  6 +-
 arch/x86/kernel/ftrace_64.S                     |  8 +--
 arch/x86/kernel/irq_32.c                        |  9 +--
 arch/x86/lib/Makefile                           |  1 +
 arch/x86/lib/checksum_32.S                      |  7 +-
 arch/x86/lib/retpoline.S                        | 48 +++++++++++++
 23 files changed, 264 insertions(+), 38 deletions(-)
 create mode 100644 arch/x86/include/asm/nospec-branch.h
 create mode 100644 arch/x86/lib/retpoline.S
Signature · 1 line
2.7.4
#1 lore raw

PATCH v6 1/10 x86/retpoline: Add initial retpoline support

Enable the use of -mindirect-branch=thunk-extern in newer GCC, and provide the corresponding thunks. Provide assembler macros for invoking the thunks in the same way that GCC does, from native and inline assembler.

This adds X86_FEATURE_RETPOLINE and sets it by default on all CPUs. In some circumstances, IBRS microcode features may be used instead, and the retpoline can be disabled.

On AMD CPUs if lfence is serialising, the retpoline can be dramatically simplified to a simple "lfence; jmp *\reg". A future patch, after it has been verified that lfence really is serialising in all circumstances, can enable this by setting the X86_FEATURE_RETPOLINE_AMD feature bit in addition to X86_FEATURE_RETPOLINE.

[Andi Kleen: Rename the macros, add CONFIG_RETPOLINE option, export thunks]

---
 arch/x86/Kconfig                      | 13 +++++
 arch/x86/Makefile                     | 10 ++++
 arch/x86/include/asm/asm-prototypes.h | 25 ++++++++++
 arch/x86/include/asm/cpufeatures.h    |  2 +
 arch/x86/include/asm/nospec-branch.h  | 92 +++++++++++++++++++++++++++++++++++
 arch/x86/kernel/cpu/common.c          |  3 ++
 arch/x86/lib/Makefile                 |  1 +
 arch/x86/lib/retpoline.S              | 48 ++++++++++++++++++
 8 files changed, 194 insertions(+)
 create mode 100644 arch/x86/include/asm/nospec-branch.h
 create mode 100644 arch/x86/lib/retpoline.S
arch/x86/Kconfig +13 -0
index cd5199d..77c58ae 100644
@@ -428,6 +428,19 @@ config GOLDFISH
        def_bool y
        depends on X86_GOLDFISH
 
+config RETPOLINE
+	bool "Avoid speculative indirect branches in kernel"
+	default y
+	help
+	  Compile kernel with the retpoline compiler options to guard against
+	  kernel-to-user data leaks by avoiding speculative indirect
+	  branches. Requires a compiler with -mindirect-branch=thunk-extern
+	  support for full protection. The kernel may run slower.
+
+	  Without compiler support, at least indirect branches in assembler
+	  code are eliminated. Since this includes the syscall entry path,
+	  it is not entirely pointless.
+
 config INTEL_RDT
 	bool "Intel Resource Director Technology support"
 	default n
arch/x86/Makefile +10 -0
index a20eacd..918e550 100644
@@ -235,6 +235,16 @@ KBUILD_CFLAGS += -Wno-sign-compare
 #
 KBUILD_CFLAGS += -fno-asynchronous-unwind-tables
 
+# Avoid indirect branches in kernel to deal with Spectre
+ifdef CONFIG_RETPOLINE
+    RETPOLINE_CFLAGS += $(call cc-option,-mindirect-branch=thunk-extern -mindirect-branch-register)
+    ifneq ($(RETPOLINE_CFLAGS),)
+        KBUILD_CFLAGS += $(RETPOLINE_CFLAGS) -DRETPOLINE
+    else
+        $(warning Retpoline not supported in compiler. System may be insecure.)
+    endif
+endif
+
 archscripts: scripts_basic
 	$(Q)$(MAKE) $(build)=arch/x86/tools relocs
 
arch/x86/include/asm/asm-prototypes.h +25 -0
index ff700d8..0927cdc 100644
@@ -11,7 +11,32 @@
 #include <asm/pgtable.h>
 #include <asm/special_insns.h>
 #include <asm/preempt.h>
+#include <asm/asm.h>
 
 #ifndef CONFIG_X86_CMPXCHG64
 extern void cmpxchg8b_emu(void);
 #endif
+
+#ifdef CONFIG_RETPOLINE
+#ifdef CONFIG_X86_32
+#define INDIRECT_THUNK(reg) extern asmlinkage void __x86_indirect_thunk_e ## reg(void);
+#else
+#define INDIRECT_THUNK(reg) extern asmlinkage void __x86_indirect_thunk_r ## reg(void);
+INDIRECT_THUNK(8)
+INDIRECT_THUNK(9)
+INDIRECT_THUNK(10)
+INDIRECT_THUNK(11)
+INDIRECT_THUNK(12)
+INDIRECT_THUNK(13)
+INDIRECT_THUNK(14)
+INDIRECT_THUNK(15)
+#endif
+INDIRECT_THUNK(ax)
+INDIRECT_THUNK(bx)
+INDIRECT_THUNK(cx)
+INDIRECT_THUNK(dx)
+INDIRECT_THUNK(si)
+INDIRECT_THUNK(di)
+INDIRECT_THUNK(bp)
+INDIRECT_THUNK(sp)
+#endif /* CONFIG_RETPOLINE */
arch/x86/include/asm/cpufeatures.h +2 -0
index 511d909..2d65438 100644
@@ -203,6 +203,8 @@
 #define X86_FEATURE_PROC_FEEDBACK	( 7*32+ 9) /* AMD ProcFeedbackInterface */
 #define X86_FEATURE_SME			( 7*32+10) /* AMD Secure Memory Encryption */
 #define X86_FEATURE_PTI			( 7*32+11) /* Kernel Page Table Isolation enabled */
+#define X86_FEATURE_RETPOLINE		( 7*32+12) /* Intel Retpoline mitigation for Spectre variant 2 */
+#define X86_FEATURE_RETPOLINE_AMD	( 7*32+13) /* AMD Retpoline mitigation for Spectre variant 2 */
 #define X86_FEATURE_INTEL_PPIN		( 7*32+14) /* Intel Processor Inventory Number */
 #define X86_FEATURE_INTEL_PT		( 7*32+15) /* Intel Processor Trace */
 #define X86_FEATURE_AVX512_4VNNIW	( 7*32+16) /* AVX-512 Neural Network Instructions */
arch/x86/include/asm/nospec-branch.h new file +92 -0

92 lines not on this page · raw message

arch/x86/kernel/cpu/common.c +3 -0
index 372ba3f..cfa5042 100644
@@ -904,6 +904,9 @@ static void __init early_identify_cpu(struct cpuinfo_x86 *c)
 
 	setup_force_cpu_bug(X86_BUG_SPECTRE_V1);
 	setup_force_cpu_bug(X86_BUG_SPECTRE_V2);
+#ifdef CONFIG_RETPOLINE
+	setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
+#endif
 
 	fpu__init_system(c);
 
arch/x86/lib/Makefile +1 -0
index 457f681..d435c89 100644
@@ -26,6 +26,7 @@ lib-y += memcpy_$(BITS).o
 lib-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem.o
 lib-$(CONFIG_INSTRUCTION_DECODER) += insn.o inat.o
 lib-$(CONFIG_RANDOMIZE_BASE) += kaslr.o
+lib-$(CONFIG_RETPOLINE) += retpoline.o
 
 obj-y += msr.o msr-reg.o msr-reg-export.o hweight.o
 
arch/x86/lib/retpoline.S new file +48 -0

48 lines not on this page · raw message

Signature · 1 line
2.7.4
maintainer
#2 lore raw

Re: [PATCH v6 01/10] x86/retpoline: Add initial retpoline support

Quoted 4 lines · David Woodhouse
arch/x86/include/asm/nospec-branch.h new file +4 -0
+#ifdef __ASSEMBLY__
+
+/*
+ * These are the bare retpoline primitives for indirect jmp and call.

arch/x86/include/asm/nospec-branch.h line 23

Quoted 10 lines
arch/x86/include/asm/nospec-branch.h new file +10 -0
+ * Do not use these directly; they only exist to make the ALTERNATIVE
+ * invocation below less ugly.
+ */
+.macro RETPOLINE_JMP reg:req
+	call	1112f
+1111:	pause
+	jmp	1111b
+1112:	mov	\reg, (%_ASM_SP)
+	ret
+.endm

Should this not use local name labels instead?

.macro RETPOLINE_JMP reg:req

	call	.Ldo_rop_\@
.Lspec_trap_\@:
	pause
	jmp .Lspec_trap_\@
.Ldo_rop_\@:
	mov	\reg, (%_ASM_SP)
	ret

.endm

And I suppose it might be nice to put a little comment with them explaining how they work.

Quoted 6 lines
arch/x86/include/asm/nospec-branch.h new file +6 -0
+/*
+ * For i386 we use the original ret-equivalent retpoline, because
+ * otherwise we'll run out of registers. We don't care about CET
+ * here, anyway.
+ */
+# define NOSPEC_CALL ALTERNATIVE(				\

arch/x86/include/asm/nospec-branch.h line 84

Quoted 10 lines
arch/x86/include/asm/nospec-branch.h new file +10 -0
+	"call	*%[thunk_target]\n",				\
+	"       jmp    1113f; "					\
+	"1110:  call   1112f; "					\
+	"1111:	pause; "					\
+	"       jmp    1111b; "					\
+	"1112:	addl   $4, %%esp; "				\
+	"       pushl  %[thunk_target]; "			\
+	"       ret; "						\
+	"1113:  call   1110b;\n",				\
+	X86_FEATURE_RETPOLINE)

Ideally this would too, just not sure that works in inline asm.

#3 lore raw

Re: [PATCH v6 01/10] x86/retpoline: Add initial retpoline support

Quoted 4 lines · Peter Zijlstra

Should this not use local name labels instead?

Quoted 9 lines

.macro RETPOLINE_JMP reg:req
        call    .Ldo_rop_\@
.Lspec_trap_\@:
        pause
        jmp .Lspec_trap_\@
.Ldo_rop_\@:
        mov     \reg, (%_ASM_SP)
        ret
.endm

Not if you want to be able to use them twice in the same .S file.

maintainer
#4 lore raw

Re: [PATCH v6 01/10] x86/retpoline: Add initial retpoline support

Quoted 15 lines · David Woodhouse
Quoted 4 lines · Peter Zijlstra

Should this not use local name labels instead?

Quoted 9 lines

.macro RETPOLINE_JMP reg:req
        call    .Ldo_rop_\@
.Lspec_trap_\@:
        pause
        jmp .Lspec_trap_\@
.Ldo_rop_\@:
        mov     \reg, (%_ASM_SP)
        ret
.endm

Quoted 1 line

Not if you want to be able to use them twice in the same .S file.

Should work fine, the \@ expands to a per-instance magic thing IIRC. All the PTI helpers do this too and that works just fine, see arch/x86/entry/calling.h

#5 lore raw

Re: [PATCH v6 01/10] x86/retpoline: Add initial retpoline support

Quoted 17 lines · David Woodhouse
Quoted 15 lines · Peter Zijlstra
Quoted 13 lines

Should this not use local name labels instead?

.macro RETPOLINE_JMP reg:req
        call    .Ldo_rop_\@
.Lspec_trap_\@:
        pause
        jmp .Lspec_trap_\@
.Ldo_rop_\@:
        mov     \reg, (%_ASM_SP)
        ret
.endm

Quoted 6 lines
Quoted 1 line

Not if you want to be able to use them twice in the same .S file.

Should work fine, the \@ expands to a per-instance magic thing IIRC. All the PTI helpers do this too and that works just fine, see arch/x86/entry/calling.h

Ah, OK. Will do that when home in a few hours then.

Signature · 1 line
dwmw2
#6 lore raw

Re: [PATCH v6 01/10] x86/retpoline: Add initial retpoline support

In reply to David Woodhouse
Quoted 7 lines · David Woodhouse
arch/x86/Makefile +0 -0
index a20eacd..918e550 100644
@@ -235,6 +235,16 @@ KBUILD_CFLAGS += -Wno-sign-compare
 #
 KBUILD_CFLAGS += -fno-asynchronous-unwind-tables

arch/x86/Makefile KBUILD_CFLAGS += -Wno-sign-compare · line 246

Quoted 10 lines
arch/x86/Makefile +9 -0
 
+# Avoid indirect branches in kernel to deal with Spectre
+ifdef CONFIG_RETPOLINE
+    RETPOLINE_CFLAGS += $(call cc-option,-mindirect-branch=thunk-extern -mindirect-branch-register)
+    ifneq ($(RETPOLINE_CFLAGS),)
+        KBUILD_CFLAGS += $(RETPOLINE_CFLAGS) -DRETPOLINE
+    else
+        $(warning Retpoline not supported in compiler. System may be insecure.)
+    endif
+endif

I wonder if an error might be more appropriate than a warning. I learned from experience that a lot of people don't see these Makefile warnings, and this would be a dangerous one to miss.

Also if this were an error, you could get rid of the RETPOLINE define, and that would be one less define cluttering up the already way-too-long GCC arg list.

Signature · 1 line
Josh
#7 lore raw

Re: [PATCH v6 01/10] x86/retpoline: Add initial retpoline support

Quoted 23 lines · Josh Poimboeuf
Quoted 7 lines · David Woodhouse
arch/x86/Makefile +0 -0
index a20eacd..918e550 100644
@@ -235,6 +235,16 @@ KBUILD_CFLAGS += -Wno-sign-compare
 #
 KBUILD_CFLAGS += -fno-asynchronous-unwind-tables

arch/x86/Makefile KBUILD_CFLAGS += -Wno-sign-compare · line 246

Quoted 10 lines
arch/x86/Makefile +9 -0
 
+# Avoid indirect branches in kernel to deal with Spectre
+ifdef CONFIG_RETPOLINE
+    RETPOLINE_CFLAGS += $(call cc-option,-mindirect-branch=thunk-extern -mindirect-branch-register)
+    ifneq ($(RETPOLINE_CFLAGS),)
+        KBUILD_CFLAGS += $(RETPOLINE_CFLAGS) -DRETPOLINE
+    else
+        $(warning Retpoline not supported in compiler. System may be insecure.)
+    endif
+endif

I wonder if an error might be more appropriate than a warning. I learned from experience that a lot of people don't see these Makefile warnings, and this would be a dangerous one to miss.

Quoted 3 lines

Also if this were an error, you could get rid of the RETPOLINE define, and that would be one less define cluttering up the already way-too-long GCC arg list.

It still allows to get the ASM part covered. If that's worth it I can't tell.

Thanks,

tglx

#8 lore raw

Re: [PATCH v6 01/10] x86/retpoline: Add initial retpoline support

Quoted 24 lines · Thomas Gleixner
Quoted 23 lines · Josh Poimboeuf
Quoted 7 lines · David Woodhouse
arch/x86/Makefile +0 -0
index a20eacd..918e550 100644
@@ -235,6 +235,16 @@ KBUILD_CFLAGS += -Wno-sign-compare
 #
 KBUILD_CFLAGS += -fno-asynchronous-unwind-tables

arch/x86/Makefile KBUILD_CFLAGS += -Wno-sign-compare · line 246

Quoted 10 lines
arch/x86/Makefile +9 -0
 
+# Avoid indirect branches in kernel to deal with Spectre
+ifdef CONFIG_RETPOLINE
+    RETPOLINE_CFLAGS += $(call cc-option,-mindirect-branch=thunk-extern -mindirect-branch-register)
+    ifneq ($(RETPOLINE_CFLAGS),)
+        KBUILD_CFLAGS += $(RETPOLINE_CFLAGS) -DRETPOLINE
+    else
+        $(warning Retpoline not supported in compiler. System may be insecure.)
+    endif
+endif

I wonder if an error might be more appropriate than a warning. I learned from experience that a lot of people don't see these Makefile warnings, and this would be a dangerous one to miss.

Quoted 5 lines
Quoted 3 lines

Also if this were an error, you could get rid of the RETPOLINE define, and that would be one less define cluttering up the already way-too-long GCC arg list.

It still allows to get the ASM part covered. If that's worth it I can't tell.

If there's a makefile error above, then CONFIG_RETPOLINE would already imply compiler support, so the ASM code with the new '%V' option could just do 'ifdef CONFIG_RETPOLINE'.

Signature · 1 line
Josh
#9 lore raw

Re: [PATCH v6 01/10] x86/retpoline: Add initial retpoline support

Quoted 31 lines · Thomas Gleixner
Quoted 29 lines · Josh Poimboeuf
Quoted 9 lines · David Woodhouse
Quoted 8 lines
arch/x86/Makefile +0 -0
index a20eacd..918e550 100644
@@ -235,6 +235,16 @@ KBUILD_CFLAGS += -Wno-sign-compare
 #
 KBUILD_CFLAGS += -fno-asynchronous-unwind-tables
 
Quoted 3 lines

arch/x86/Makefile KBUILD_CFLAGS += -Wno-sign-compare

Quoted 3 lines
arch/x86/Makefile +3 -0
+# Avoid indirect branches in kernel to deal with Spectre
+ifdef CONFIG_RETPOLINE
+    RETPOLINE_CFLAGS += $(call

cc-option,-mindirect-branch=thunk-extern -mindirect-branch-register)

Quoted 4 lines

arch/x86/Makefile KBUILD_CFLAGS += -Wno-sign-compare

Quoted 4 lines
arch/x86/Makefile +4 -0
+    ifneq ($(RETPOLINE_CFLAGS),)
+        KBUILD_CFLAGS += $(RETPOLINE_CFLAGS) -DRETPOLINE
+    else
+        $(warning Retpoline not supported in compiler. System may

be insecure.)

Quoted 9 lines

arch/x86/Makefile KBUILD_CFLAGS += -Wno-sign-compare · line 246

Quoted 2 lines
arch/x86/Makefile +2 -0
+    endif
+endif

I wonder if an error might be more appropriate than a warning. I learned from experience that a lot of people don't see these Makefile warnings, and this would be a dangerous one to miss.

Also if this were an error, you could get rid of the RETPOLINE define, and that would be one less define cluttering up the already

way-too-long

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

GCC arg list.

It still allows to get the ASM part covered. If that's worth it I can't tell.

If there's a makefile error above, then CONFIG_RETPOLINE would already imply compiler support, so the ASM code with the new '%V' option could just do 'ifdef CONFIG_RETPOLINE'.

I did look at ditching the -DRETPOLINE but there is benefit in doing the sys_call_table jump even when GCC isn't updated. So I put it back.

Signature · 1 line
dwmw2
#10 lore raw

Re: [PATCH v6 01/10] x86/retpoline: Add initial retpoline support

Quoted 36 lines · David Woodhouse
Quoted 36 lines · Thomas Gleixner
Quoted 33 lines · Josh Poimboeuf
Quoted 9 lines · David Woodhouse
Quoted 8 lines
arch/x86/Makefile +0 -0
index a20eacd..918e550 100644
@@ -235,6 +235,16 @@ KBUILD_CFLAGS += -Wno-sign-compare
 #
 KBUILD_CFLAGS += -fno-asynchronous-unwind-tables
 
Quoted 3 lines

arch/x86/Makefile KBUILD_CFLAGS += -Wno-sign-compare

Quoted 3 lines
arch/x86/Makefile +3 -0
+# Avoid indirect branches in kernel to deal with Spectre
+ifdef CONFIG_RETPOLINE
+    RETPOLINE_CFLAGS += $(call

cc-option,-mindirect-branch=thunk-extern -mindirect-branch-register)

Quoted 4 lines

arch/x86/Makefile KBUILD_CFLAGS += -Wno-sign-compare

Quoted 4 lines
arch/x86/Makefile +4 -0
+    ifneq ($(RETPOLINE_CFLAGS),)
+        KBUILD_CFLAGS += $(RETPOLINE_CFLAGS) -DRETPOLINE
+    else
+        $(warning Retpoline not supported in compiler. System may

be insecure.)

Quoted 9 lines

arch/x86/Makefile KBUILD_CFLAGS += -Wno-sign-compare · line 246

Quoted 2 lines
arch/x86/Makefile +2 -0
+    endif
+endif

I wonder if an error might be more appropriate than a warning. I learned from experience that a lot of people don't see these Makefile warnings, and this would be a dangerous one to miss.

Also if this were an error, you could get rid of the RETPOLINE define, and that would be one less define cluttering up the already

way-too-long

Quoted 1 line

GCC arg list.

It still allows to get the ASM part covered. If that's worth it I can't tell.

Quoted 6 lines
Quoted 3 lines

If there's a makefile error above, then CONFIG_RETPOLINE would already imply compiler support, so the ASM code with the new '%V' option could just do 'ifdef CONFIG_RETPOLINE'.

I did look at ditching the -DRETPOLINE but there is benefit in doing the sys_call_table jump even when GCC isn't updated. So I put it back.

What benefit is that? Doesn't it give the user a false sense of security, since there's no shortage of other indirect branches to attack?

Signature · 1 line
Josh
maintainer
#11 lore raw

Re: [PATCH v6 01/10] x86/retpoline: Add initial retpoline support

In reply to Thomas Gleixner
Quoted 11 lines · Thomas Gleixner
Quoted 7 lines · Josh Poimboeuf

I wonder if an error might be more appropriate than a warning. I learned from experience that a lot of people don't see these Makefile warnings, and this would be a dangerous one to miss.

Also if this were an error, you could get rid of the RETPOLINE define, and that would be one less define cluttering up the already way-too-long GCC arg list.

It still allows to get the ASM part covered. If that's worth it I can't tell.

So elsewhere you stated we're dropping support for GCC without asm-goto (<4.5), does it then make sense to make one more step and mandate a retpoline capable compiler, which would put us at >=4.9 (for x86).

That would get rid of this weird case as well.

#12 lore raw

Re: [PATCH v6 01/10] x86/retpoline: Add initial retpoline support

Quoted 11 lines · Peter Zijlstra
Quoted 10 lines · Thomas Gleixner
Quoted 7 lines · Josh Poimboeuf

I wonder if an error might be more appropriate than a warning. I learned from experience that a lot of people don't see these Makefile warnings, and this would be a dangerous one to miss.

Also if this were an error, you could get rid of the RETPOLINE define, and that would be one less define cluttering up the already way-too-long GCC arg list.

Quoted 7 lines
Quoted 1 line

It still allows to get the ASM part covered. If that's worth it I can't tell.

So elsewhere you stated we're dropping support for GCC without asm-goto (<4.5), does it then make sense to make one more step and mandate a retpoline capable compiler, which would put us at >=4.9 (for x86).

That would get rid of this weird case as well.

I agree in principle, though the difference is that the retpoline compilers are not available today, gcc with asm goto are.

The reasoning for the minimal thing was to cover at least the obvious easy targets, eg. sys_call_table as the deeper ones are harder.

Thanks,

tglx

#13 lore raw

Re: [PATCH v6 01/10] x86/retpoline: Add initial retpoline support

In reply to Peter Zijlstra
Quoted 10 lines · Peter Zijlstra
Quoted 9 lines · Thomas Gleixner
Quoted 7 lines · Josh Poimboeuf

I wonder if an error might be more appropriate than a warning.  I learned from experience that a lot of people don't see these Makefile warnings, and this would be a dangerous one to miss.

Also if this were an error, you could get rid of the RETPOLINE define, and that would be one less define cluttering up the already way-too-long GCC arg list.

Quoted 8 lines
Quoted 2 lines


It still allows to get the ASM part covered. If that's worth it I can't tell.

So elsewhere you stated we're dropping support for GCC without asm-goto (<4.5), does it then make sense to make one more step and mandate a retpoline capable compiler, which would put us at >=4.9 (for x86).

That would get rid of this weird case as well.

Yeah... I don't have strong feelings there.

Arjan (IIRC) had asked me to keep it this way.

The idea was that those were the *easy* targets for an attacker to find; especially in entry_64.S it's asm all the way to the indirect branch. A rootkit might be targeted at entirely unpatched systems which leave that vulnerable, and *even* though there are other targets which could be found with more work, doing just the asm code might well end up protecting from such an attack in practice.

The CONFIG_RETPOLINE/!RETPOLINE case isn't really *that* much complexity; I don't really care about that either.

On the whole, I'm inclined to leave it as it is without further bikeshedding for now. We can change it later once all those GCC releases have been made with the backports, perhaps.

#14 lore raw

PATCH v6 2/10 x86/retpoline/crypto: Convert crypto assembler indirect jumps

In reply to David Woodhouse

Convert all indirect jumps in crypto assembler code to use non-speculative sequences when CONFIG_RETPOLINE is enabled.

---
 arch/x86/crypto/aesni-intel_asm.S            | 5 +++--
 arch/x86/crypto/camellia-aesni-avx-asm_64.S  | 3 ++-
 arch/x86/crypto/camellia-aesni-avx2-asm_64.S | 3 ++-
 arch/x86/crypto/crc32c-pcl-intel-asm_64.S    | 3 ++-
 4 files changed, 9 insertions(+), 5 deletions(-)
arch/x86/crypto/aesni-intel_asm.S +3 -2
index 16627fe..f128680 100644
@@ -32,6 +32,7 @@
 #include <linux/linkage.h>
 #include <asm/inst.h>
 #include <asm/frame.h>
+#include <asm/nospec-branch.h>
 
 /*
  * The following macros are used to move an (un)aligned 16 byte value to/from
@@ -2884,7 +2885,7 @@ ENTRY(aesni_xts_crypt8)
 	pxor INC, STATE4
 	movdqu IV, 0x30(OUTP)
 
-	call *%r11
+	NOSPEC_CALL %r11
 
 	movdqu 0x00(OUTP), INC
 	pxor INC, STATE1
@@ -2929,7 +2930,7 @@ ENTRY(aesni_xts_crypt8)
 	_aesni_gf128mul_x_ble()
 	movups IV, (IVP)
 
-	call *%r11
+	NOSPEC_CALL %r11
 
 	movdqu 0x40(OUTP), INC
 	pxor INC, STATE1
arch/x86/crypto/camellia-aesni-avx-asm_64.S +2 -1
index f7c495e..ba3f075 100644
@@ -17,6 +17,7 @@
 
 #include <linux/linkage.h>
 #include <asm/frame.h>
+#include <asm/nospec-branch.h>
 
 #define CAMELLIA_TABLE_BYTE_LEN 272
 
@@ -1227,7 +1228,7 @@ camellia_xts_crypt_16way:
 	vpxor 14 * 16(%rax), %xmm15, %xmm14;
 	vpxor 15 * 16(%rax), %xmm15, %xmm15;
 
-	call *%r9;
+	NOSPEC_CALL %r9;
 
 	addq $(16 * 16), %rsp;
 
arch/x86/crypto/camellia-aesni-avx2-asm_64.S +2 -1
index eee5b39..9b0a88a 100644
@@ -12,6 +12,7 @@
 
 #include <linux/linkage.h>
 #include <asm/frame.h>
+#include <asm/nospec-branch.h>
 
 #define CAMELLIA_TABLE_BYTE_LEN 272
 
@@ -1343,7 +1344,7 @@ camellia_xts_crypt_32way:
 	vpxor 14 * 32(%rax), %ymm15, %ymm14;
 	vpxor 15 * 32(%rax), %ymm15, %ymm15;
 
-	call *%r9;
+	NOSPEC_CALL %r9;
 
 	addq $(16 * 32), %rsp;
 
arch/x86/crypto/crc32c-pcl-intel-asm_64.S +2 -1
index 7a7de27..05178b44 100644
@@ -45,6 +45,7 @@
 
 #include <asm/inst.h>
 #include <linux/linkage.h>
+#include <asm/nospec-branch.h>
 
 ## ISCSI CRC 32 Implementation with crc32 and pclmulqdq Instruction
 
@@ -172,7 +173,7 @@ continue_block:
 	movzxw  (bufp, %rax, 2), len
 	lea	crc_array(%rip), bufp
 	lea     (bufp, len, 1), bufp
-	jmp     *bufp
+	NOSPEC_JMP bufp
 
 	################################################################
 	## 2a) PROCESS FULL BLOCKS:
Signature · 1 line
2.7.4
#15 lore raw

Re: [PATCH v6 02/10] x86/retpoline/crypto: Convert crypto assembler indirect jumps

Quoted 25 lines · David Woodhouse

Convert all indirect jumps in crypto assembler code to use non-speculative sequences when CONFIG_RETPOLINE is enabled.

---
 arch/x86/crypto/aesni-intel_asm.S            | 5 +++--
 arch/x86/crypto/camellia-aesni-avx-asm_64.S  | 3 ++-
 arch/x86/crypto/camellia-aesni-avx2-asm_64.S | 3 ++-
 arch/x86/crypto/crc32c-pcl-intel-asm_64.S    | 3 ++-
 4 files changed, 9 insertions(+), 5 deletions(-)
arch/x86/crypto/aesni-intel_asm.S +1 -0
index 16627fe..f128680 100644
@@ -32,6 +32,7 @@
 #include <linux/linkage.h>
 #include <asm/inst.h>
 #include <asm/frame.h>
+#include <asm/nospec-branch.h>
 
 /*
  * The following macros are used to move an (un)aligned 16 byte value to/from

arch/x86/crypto/aesni-intel_asm.S ENTRY(aesni_xts_crypt8) · line 2888

Quoted 5 lines
arch/x86/crypto/aesni-intel_asm.S +1 -1
@@ -2884,7 +2885,7 @@ ENTRY(aesni_xts_crypt8)
 	pxor INC, STATE4
 	movdqu IV, 0x30(OUTP)
 
-	call *%r11
+	NOSPEC_CALL %r11

Personally I find

CALL_NOSPEC %r11

to be more readable. The call is the action and the nospec is a detail.

And similarly for NOSPEC_JMP -> JMP_NOSPEC.

Signature · 1 line
Josh
#16 lore raw

PATCH v6 3/10 x86/retpoline/entry: Convert entry assembler indirect jumps

In reply to David Woodhouse

Convert indirect jumps in core 32/64bit entry assembler code to use non-speculative sequences when CONFIG_RETPOLINE is enabled.

Don't use NOSPEC_CALL in entry_SYSCALL_64_fastpath because the return address after the 'call' instruction must be *precisely* at the .Lentry_SYSCALL_64_after_fastpath label for stub_ptregs_64 to work, and the use of alternatives will mess that up unless we play horrid games to prepend with NOPs and make the variants the same length. It's not worth it; in the case where we ALTERNATIVE out the retpoline, the first instruction at __x86.indirect_thunk.rax is going to be a bare jmp *%rax anyway.

---
 arch/x86/entry/entry_32.S |  5 +++--
 arch/x86/entry/entry_64.S | 12 +++++++++---
 2 files changed, 12 insertions(+), 5 deletions(-)
arch/x86/entry/entry_32.S +3 -2
index ace8f32..cf9ef33 100644
@@ -44,6 +44,7 @@
 #include <asm/asm.h>
 #include <asm/smap.h>
 #include <asm/frame.h>
+#include <asm/nospec-branch.h>
 
 	.section .entry.text, "ax"
 
@@ -290,7 +291,7 @@ ENTRY(ret_from_fork)
 
 	/* kernel thread */
 1:	movl	%edi, %eax
-	call	*%ebx
+	NOSPEC_CALL %ebx
 	/*
 	 * A kernel thread is allowed to return here after successfully
 	 * calling do_execve().  Exit to userspace to complete the execve()
@@ -919,7 +920,7 @@ common_exception:
 	movl	%ecx, %es
 	TRACE_IRQS_OFF
 	movl	%esp, %eax			# pt_regs pointer
-	call	*%edi
+	NOSPEC_CALL %edi
 	jmp	ret_from_exception
 END(common_exception)
 
arch/x86/entry/entry_64.S +9 -3
index ed31d00..9bce6ed 100644
@@ -37,6 +37,7 @@
 #include <asm/pgtable_types.h>
 #include <asm/export.h>
 #include <asm/frame.h>
+#include <asm/nospec-branch.h>
 #include <linux/err.h>
 
 #include "calling.h"
@@ -187,7 +188,7 @@ ENTRY(entry_SYSCALL_64_trampoline)
 	 */
 	pushq	%rdi
 	movq	$entry_SYSCALL_64_stage2, %rdi
-	jmp	*%rdi
+	NOSPEC_JMP %rdi
 END(entry_SYSCALL_64_trampoline)
 
 	.popsection
@@ -266,7 +267,12 @@ entry_SYSCALL_64_fastpath:
 	 * It might end up jumping to the slow path.  If it jumps, RAX
 	 * and all argument registers are clobbered.
 	 */
+#ifdef CONFIG_RETPOLINE
+	movq	sys_call_table(, %rax, 8), %rax
+	call	__x86_indirect_thunk_rax
+#else
 	call	*sys_call_table(, %rax, 8)
+#endif
 .Lentry_SYSCALL_64_after_fastpath_call:
 
 	movq	%rax, RAX(%rsp)
@@ -438,7 +444,7 @@ ENTRY(stub_ptregs_64)
 	jmp	entry_SYSCALL64_slow_path
 
 1:
-	jmp	*%rax				/* Called from C */
+	NOSPEC_JMP %rax				/* Called from C */
 END(stub_ptregs_64)
 
 .macro ptregs_stub func
@@ -517,7 +523,7 @@ ENTRY(ret_from_fork)
 1:
 	/* kernel thread */
 	movq	%r12, %rdi
-	call	*%rbx
+	NOSPEC_CALL %rbx
 	/*
 	 * A kernel thread is allowed to return here after successfully
 	 * calling do_execve().  Exit to userspace to complete the execve()
Signature · 1 line
2.7.4
#17 lore raw

PATCH v6 4/10 x86/retpoline/ftrace: Convert ftrace assembler indirect jumps

In reply to David Woodhouse

Convert all indirect jumps in ftrace assembler code to use non-speculative sequences when CONFIG_RETPOLINE is enabled.

---
 arch/x86/kernel/ftrace_32.S | 6 ++++--
 arch/x86/kernel/ftrace_64.S | 8 ++++----
 2 files changed, 8 insertions(+), 6 deletions(-)
arch/x86/kernel/ftrace_32.S +4 -2
index b6c6468..c3842c9 100644
@@ -8,6 +8,7 @@
 #include <asm/segment.h>
 #include <asm/export.h>
 #include <asm/ftrace.h>
+#include <asm/nospec-branch.h>
 
 #ifdef CC_USING_FENTRY
 # define function_hook	__fentry__
@@ -197,7 +198,8 @@ ftrace_stub:
 	movl	0x4(%ebp), %edx
 	subl	$MCOUNT_INSN_SIZE, %eax
 
-	call	*ftrace_trace_function
+	movl	ftrace_trace_function, %ecx
+	NOSPEC_CALL %ecx
 
 	popl	%edx
 	popl	%ecx
@@ -241,5 +243,5 @@ return_to_handler:
 	movl	%eax, %ecx
 	popl	%edx
 	popl	%eax
-	jmp	*%ecx
+	NOSPEC_JMP %ecx
 #endif
arch/x86/kernel/ftrace_64.S +4 -4
index c832291..0893068 100644
@@ -7,7 +7,7 @@
 #include <asm/ptrace.h>
 #include <asm/ftrace.h>
 #include <asm/export.h>
-
+#include <asm/nospec-branch.h>
 
 	.code64
 	.section .entry.text, "ax"
@@ -286,8 +286,8 @@ trace:
 	 * ip and parent ip are used and the list function is called when
 	 * function tracing is enabled.
 	 */
-	call   *ftrace_trace_function
-
+	movq ftrace_trace_function, %r8
+	NOSPEC_CALL %r8
 	restore_mcount_regs
 
 	jmp fgraph_trace
@@ -329,5 +329,5 @@ GLOBAL(return_to_handler)
 	movq 8(%rsp), %rdx
 	movq (%rsp), %rax
 	addq $24, %rsp
-	jmp *%rdi
+	NOSPEC_JMP %rdi
 #endif
Signature · 1 line
2.7.4
#18 lore raw

PATCH v6 5/10 x86/retpoline/hyperv: Convert assembler indirect jumps

In reply to David Woodhouse

Convert all indirect jumps in hyperv inline asm code to use non-speculative sequences when CONFIG_RETPOLINE is enabled.

---
 arch/x86/include/asm/mshyperv.h | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
arch/x86/include/asm/mshyperv.h +10 -8
index 581bb54..6534e57 100644
@@ -7,6 +7,7 @@
 #include <linux/nmi.h>
 #include <asm/io.h>
 #include <asm/hyperv.h>
+#include <asm/nospec-branch.h>
 
 /*
  * The below CPUID leaves are present if VersionAndFeatures.HypervisorPresent
@@ -186,10 +187,11 @@ static inline u64 hv_do_hypercall(u64 control, void *input, void *output)
 		return U64_MAX;
 
 	__asm__ __volatile__("mov %4, %%r8\n"
-			     "call *%5"
+			     NOSPEC_CALL
 			     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
 			       "+c" (control), "+d" (input_address)
-			     :  "r" (output_address), "m" (hv_hypercall_pg)
+			     :  "r" (output_address),
+				THUNK_TARGET(hv_hypercall_pg)
 			     : "cc", "memory", "r8", "r9", "r10", "r11");
 #else
 	u32 input_address_hi = upper_32_bits(input_address);
@@ -200,13 +202,13 @@ static inline u64 hv_do_hypercall(u64 control, void *input, void *output)
 	if (!hv_hypercall_pg)
 		return U64_MAX;
 
-	__asm__ __volatile__("call *%7"
+	__asm__ __volatile__(NOSPEC_CALL
 			     : "=A" (hv_status),
 			       "+c" (input_address_lo), ASM_CALL_CONSTRAINT
 			     : "A" (control),
 			       "b" (input_address_hi),
 			       "D"(output_address_hi), "S"(output_address_lo),
-			       "m" (hv_hypercall_pg)
+			       THUNK_TARGET(hv_hypercall_pg)
 			     : "cc", "memory");
 #endif /* !x86_64 */
 	return hv_status;
@@ -227,10 +229,10 @@ static inline u64 hv_do_fast_hypercall8(u16 code, u64 input1)
 
 #ifdef CONFIG_X86_64
 	{
-		__asm__ __volatile__("call *%4"
+		__asm__ __volatile__(NOSPEC_CALL
 				     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
 				       "+c" (control), "+d" (input1)
-				     : "m" (hv_hypercall_pg)
+				     : THUNK_TARGET(hv_hypercall_pg)
 				     : "cc", "r8", "r9", "r10", "r11");
 	}
 #else
@@ -238,13 +240,13 @@ static inline u64 hv_do_fast_hypercall8(u16 code, u64 input1)
 		u32 input1_hi = upper_32_bits(input1);
 		u32 input1_lo = lower_32_bits(input1);
 
-		__asm__ __volatile__ ("call *%5"
+		__asm__ __volatile__ (NOSPEC_CALL
 				      : "=A"(hv_status),
 					"+c"(input1_lo),
 					ASM_CALL_CONSTRAINT
 				      :	"A" (control),
 					"b" (input1_hi),
-					"m" (hv_hypercall_pg)
+					THUNK_TARGET(hv_hypercall_pg)
 				      : "cc", "edi", "esi");
 	}
 #endif
Signature · 1 line
2.7.4
#19 lore raw

PATCH v6 6/10 x86/retpoline/xen: Convert Xen hypercall indirect jumps

In reply to David Woodhouse

Convert indirect call in Xen hypercall to use non-speculative sequence, when CONFIG_RETPOLINE is enabled.

---
 arch/x86/include/asm/xen/hypercall.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/x86/include/asm/xen/hypercall.h +3 -2
index 7cb282e..393c004 100644
@@ -44,6 +44,7 @@
 #include <asm/page.h>
 #include <asm/pgtable.h>
 #include <asm/smap.h>
+#include <asm/nospec-branch.h>
 
 #include <xen/interface/xen.h>
 #include <xen/interface/sched.h>
@@ -217,9 +218,9 @@ privcmd_call(unsigned call,
 	__HYPERCALL_5ARG(a1, a2, a3, a4, a5);
 
 	stac();
-	asm volatile("call *%[call]"
+	asm volatile(NOSPEC_CALL
 		     : __HYPERCALL_5PARAM
-		     : [call] "a" (&hypercall_page[call])
+		     : [thunk_target] "a" (&hypercall_page[call])
 		     : __HYPERCALL_CLOBBER5);
 	clac();
 
Signature · 1 line
2.7.4
#20 lore raw

PATCH v6 7/10 x86/retpoline/checksum32: Convert assembler indirect jumps

In reply to David Woodhouse

Convert all indirect jumps in 32bit checksum assembler code to use non-speculative sequences when CONFIG_RETPOLINE is enabled.

---
 arch/x86/lib/checksum_32.S | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
arch/x86/lib/checksum_32.S +4 -3
index 4d34bb5..98cf15d 100644
@@ -29,7 +29,8 @@
 #include <asm/errno.h>
 #include <asm/asm.h>
 #include <asm/export.h>
-				
+#include <asm/nospec-branch.h>
+
 /*
  * computes a partial checksum, e.g. for TCP/UDP fragments
  */
@@ -156,7 +157,7 @@ ENTRY(csum_partial)
 	negl %ebx
 	lea 45f(%ebx,%ebx,2), %ebx
 	testl %esi, %esi
-	jmp *%ebx
+	NOSPEC_JMP %ebx
 
 	# Handle 2-byte-aligned regions
 20:	addw (%esi), %ax
@@ -439,7 +440,7 @@ ENTRY(csum_partial_copy_generic)
 	andl $-32,%edx
 	lea 3f(%ebx,%ebx), %ebx
 	testl %esi, %esi 
-	jmp *%ebx
+	NOSPEC_JMP %ebx
 1:	addl $64,%esi
 	addl $64,%edi 
 	SRC(movb -32(%edx),%bl)	; SRC(movb (%edx),%bl)
Signature · 1 line
2.7.4
#21 lore raw

PATCH v6 8/10 x86/retpoline/irq32: Convert assembler indirect jumps

In reply to David Woodhouse

From: Andi Kleen <[email protected]>

Convert all indirect jumps in 32bit irq inline asm code to use non speculative sequences.

---
 arch/x86/kernel/irq_32.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
arch/x86/kernel/irq_32.c +5 -4
index a83b334..e1e58f7 100644
@@ -20,6 +20,7 @@
 #include <linux/mm.h>
 
 #include <asm/apic.h>
+#include <asm/nospec-branch.h>
 
 #ifdef CONFIG_DEBUG_STACKOVERFLOW
 
@@ -55,11 +56,11 @@ DEFINE_PER_CPU(struct irq_stack *, softirq_stack);
 static void call_on_stack(void *func, void *stack)
 {
 	asm volatile("xchgl	%%ebx,%%esp	\n"
-		     "call	*%%edi		\n"
+		     NOSPEC_CALL
 		     "movl	%%ebx,%%esp	\n"
 		     : "=b" (stack)
 		     : "0" (stack),
-		       "D"(func)
+		       [thunk_target] "D"(func)
 		     : "memory", "cc", "edx", "ecx", "eax");
 }
 
@@ -95,11 +96,11 @@ static inline int execute_on_irq_stack(int overflow, struct irq_desc *desc)
 		call_on_stack(print_stack_overflow, isp);
 
 	asm volatile("xchgl	%%ebx,%%esp	\n"
-		     "call	*%%edi		\n"
+		     NOSPEC_CALL
 		     "movl	%%ebx,%%esp	\n"
 		     : "=a" (arg1), "=b" (isp)
 		     :  "0" (desc),   "1" (isp),
-			"D" (desc->handle_irq)
+			[thunk_target] "D" (desc->handle_irq)
 		     : "memory", "cc", "ecx");
 	return 1;
 }
Signature · 1 line
2.7.4
#22 lore raw

PATCH v6 9/10 x86/retpoline: Add boot time option to disable retpoline

In reply to David Woodhouse

From: Andi Kleen <[email protected]>

Add a noretpoline option boot to disable retpoline and patch out the extra sequences. It cannot patch out the jumps to the thunk functions from code generated by the compiler, but those thunks turn into a single indirect branch now.

---
 Documentation/admin-guide/kernel-parameters.txt |  3 +++
 arch/x86/kernel/cpu/intel.c                     | 11 +++++++++++
 2 files changed, 14 insertions(+)
Documentation/admin-guide/kernel-parameters.txt +3 -0
index 9059917..d443141 100644
@@ -2596,6 +2596,9 @@
 
 	nohugeiomap	[KNL,x86] Disable kernel huge I/O mappings.
 
+	noretpoline	[X86] Disable the retpoline kernel indirect branch speculation
+			workarounds. System may allow data leaks with this option.
+
 	nosmt		[KNL,S390] Disable symmetric multithreading (SMT).
 			Equivalent to smt=1.
 
arch/x86/kernel/cpu/intel.c +11 -0
index b720dac..35e123e 100644
@@ -31,6 +31,17 @@
 #include <asm/apic.h>
 #endif
 
+#ifdef RETPOLINE
+static int __init noretpoline_setup(char *__unused)
+{
+	pr_info("Retpoline runtime disabled\n");
+	setup_clear_cpu_cap(X86_FEATURE_RETPOLINE);
+	setup_clear_cpu_cap(X86_FEATURE_RETPOLINE_AMD);
+	return 1;
+}
+__setup("noretpoline", noretpoline_setup);
+#endif
+
 /*
  * Just in case our CPU detection goes bad, or you have a weird system,
  * allow a way to override the automatic disabling of MPX.
Signature · 1 line
2.7.4
#23 lore raw

PATCH v6 10/10 x86/retpoline: Exclude objtool with retpoline

In reply to David Woodhouse

From: Andi Kleen <[email protected]>

objtool's assembler nanny currently cannot deal with the code generated by the retpoline compiler and throws hundreds of warnings, mostly because it sees calls that don't have a symbolic target.

Exclude all the options that rely on objtool when RETPOLINE is active.

This mainly means that we use the frame pointer unwinder and livepatch is not supported.

Eventually objtool can be fixed to handle this.

---
 arch/x86/Kconfig       | 4 ++--
 arch/x86/Kconfig.debug | 6 +++---
 2 files changed, 5 insertions(+), 5 deletions(-)
arch/x86/Kconfig +2 -2
index 77c58ae..651d25f 100644
@@ -171,8 +171,8 @@ config X86
 	select HAVE_PERF_USER_STACK_DUMP
 	select HAVE_RCU_TABLE_FREE
 	select HAVE_REGS_AND_STACK_ACCESS_API
-	select HAVE_RELIABLE_STACKTRACE		if X86_64 && UNWINDER_FRAME_POINTER && STACK_VALIDATION
-	select HAVE_STACK_VALIDATION		if X86_64
+	select HAVE_RELIABLE_STACKTRACE		if X86_64 && UNWINDER_FRAME_POINTER && STACK_VALIDATION && !RETPOLINE
+	select HAVE_STACK_VALIDATION		if X86_64 && !RETPOLINE
 	select HAVE_SYSCALL_TRACEPOINTS
 	select HAVE_UNSTABLE_SCHED_CLOCK
 	select HAVE_USER_RETURN_NOTIFIER
arch/x86/Kconfig.debug +3 -3
index 6293a87..9f3928d 100644
@@ -359,8 +359,8 @@ config PUNIT_ATOM_DEBUG
 
 choice
 	prompt "Choose kernel unwinder"
-	default UNWINDER_ORC if X86_64
-	default UNWINDER_FRAME_POINTER if X86_32
+	default UNWINDER_ORC if X86_64 && !RETPOLINE
+	default UNWINDER_FRAME_POINTER if X86_32 || RETPOLINE
 	---help---
 	  This determines which method will be used for unwinding kernel stack
 	  traces for panics, oopses, bugs, warnings, perf, /proc/<pid>/stack,
@@ -368,7 +368,7 @@ choice
 
 config UNWINDER_ORC
 	bool "ORC unwinder"
-	depends on X86_64
+	depends on X86_64 && !RETPOLINE
 	select STACK_VALIDATION
 	---help---
 	  This option enables the ORC (Oops Rewind Capability) unwinder for
Signature · 1 line
2.7.4
#24 lore raw

Re: [PATCH v6 10/10] x86/retpoline: Exclude objtool with retpoline

On Sun, 7 Jan 2018, David Woodhouse wrote:

Cc+ Josh Poimboeuf <[email protected]>

Sigh....

Quoted 64 lines

From: Andi Kleen <[email protected]>

objtool's assembler nanny currently cannot deal with the code generated by the retpoline compiler and throws hundreds of warnings, mostly because it sees calls that don't have a symbolic target.

Exclude all the options that rely on objtool when RETPOLINE is active.

This mainly means that we use the frame pointer unwinder and livepatch is not supported.

Eventually objtool can be fixed to handle this.

---
 arch/x86/Kconfig       | 4 ++--
 arch/x86/Kconfig.debug | 6 +++---
 2 files changed, 5 insertions(+), 5 deletions(-)
arch/x86/Kconfig +2 -2
index 77c58ae..651d25f 100644
@@ -171,8 +171,8 @@ config X86
 	select HAVE_PERF_USER_STACK_DUMP
 	select HAVE_RCU_TABLE_FREE
 	select HAVE_REGS_AND_STACK_ACCESS_API
-	select HAVE_RELIABLE_STACKTRACE		if X86_64 && UNWINDER_FRAME_POINTER && STACK_VALIDATION
-	select HAVE_STACK_VALIDATION		if X86_64
+	select HAVE_RELIABLE_STACKTRACE		if X86_64 && UNWINDER_FRAME_POINTER && STACK_VALIDATION && !RETPOLINE
+	select HAVE_STACK_VALIDATION		if X86_64 && !RETPOLINE
 	select HAVE_SYSCALL_TRACEPOINTS
 	select HAVE_UNSTABLE_SCHED_CLOCK
 	select HAVE_USER_RETURN_NOTIFIER
arch/x86/Kconfig.debug +3 -3
index 6293a87..9f3928d 100644
@@ -359,8 +359,8 @@ config PUNIT_ATOM_DEBUG
 
 choice
 	prompt "Choose kernel unwinder"
-	default UNWINDER_ORC if X86_64
-	default UNWINDER_FRAME_POINTER if X86_32
+	default UNWINDER_ORC if X86_64 && !RETPOLINE
+	default UNWINDER_FRAME_POINTER if X86_32 || RETPOLINE
 	---help---
 	  This determines which method will be used for unwinding kernel stack
 	  traces for panics, oopses, bugs, warnings, perf, /proc/<pid>/stack,
@@ -368,7 +368,7 @@ choice
 
 config UNWINDER_ORC
 	bool "ORC unwinder"
-	depends on X86_64
+	depends on X86_64 && !RETPOLINE
 	select STACK_VALIDATION
 	---help---
 	  This option enables the ORC (Oops Rewind Capability) unwinder for
Signature · 1 line
2.7.4
#25 lore raw

Re: [PATCH v6 10/10] x86/retpoline: Exclude objtool with retpoline

Quoted 18 lines · Thomas Gleixner

On Sun, 7 Jan 2018, David Woodhouse wrote:

Cc+ Josh Poimboeuf <[email protected]>

Sigh....

Quoted 12 lines

From: Andi Kleen <[email protected]>

objtool's assembler nanny currently cannot deal with the code generated by the retpoline compiler and throws hundreds of warnings, mostly because it sees calls that don't have a symbolic target.

Exclude all the options that rely on objtool when RETPOLINE is active.
This mainly means that we use the frame pointer unwinder and livepatch is not supported.

Eventually objtool can be fixed to handle this.

I believe Josh is on vacation and I've asked Amit to take a look at this, at least as far as ensuring that it doesn't actually disable kpatch.

#26 lore raw

Re: [PATCH v6 10/10] x86/retpoline: Exclude objtool with retpoline

Quoted 20 lines · David
Quoted 18 lines · Thomas Gleixner

On Sun, 7 Jan 2018, David Woodhouse wrote:

Cc+ Josh Poimboeuf <[email protected]>

Sigh....

Quoted 12 lines

From: Andi Kleen <[email protected]>

objtool's assembler nanny currently cannot deal with the code generated by the retpoline compiler and throws hundreds of warnings, mostly because it sees calls that don't have a symbolic target.

Exclude all the options that rely on objtool when RETPOLINE is active.
This mainly means that we use the frame pointer unwinder and livepatch is not supported.

Eventually objtool can be fixed to handle this.

Quoted 3 lines

I believe Josh is on vacation and I've asked Amit to take a look at this, at least as far as ensuring that it doesn't actually disable kpatch.

I'm back now and will start looking at it this week.

For future revisions of the patch set, can you add me to CC? And also it would be good to add [email protected] as I'm sure the other x86 maintainers (mingo and hpa) want to see these patches.

Signature · 1 line
Josh
#27 lore raw
In reply to David Woodhouse
Quoted 2 lines · David Woodhouse

This is a mitigation for the 'variant 2' attack described in
https://googleprojectzero.blogspot.com/2018/01/reading-privileged-memory-with-side.html

Ok, I don't love the patches, but I see nothing horribly wrong here either, and I assume the performance impact of this is pretty minimal.

Thomas? I'm obviously doing rc7 today without these, but I assume the x86 maintainers are resigned to this all. And yes, we'll have at least an rc8 this release..

Linus

#28 lore raw
Quoted 9 lines · Linus Torvalds
Quoted 2 lines · David Woodhouse

This is a mitigation for the 'variant 2' attack described in
https://googleprojectzero.blogspot.com/2018/01/reading-privileged-memory-with-side.html

Ok, I don't love the patches, but I see nothing horribly wrong here either, and I assume the performance impact of this is pretty minimal.

Thomas? I'm obviously doing rc7 today without these, but I assume the x86 maintainers are resigned to this all.

That seems to be the general mental state for lots of involved people.

Thanks,

tglx

Acked-by
#29 lore raw
In reply to Linus Torvalds
Quoted 10 lines · Linus Torvalds
Quoted 2 lines · David Woodhouse

This is a mitigation for the 'variant 2' attack described in
https://googleprojectzero.blogspot.com/2018/01/reading-privileged-memory-with-side.html

Ok, I don't love the patches, but I see nothing horribly wrong here either, and I assume the performance impact of this is pretty minimal.

Thomas? I'm obviously doing rc7 today without these, but I assume the x86 maintainers are resigned to this all. And yes, we'll have at least an rc8 this release..

I'm definitely resigned to them, and with these patches being disclosed so late we don't have any good choices left, so a tentative:

Thanks,

Ingo

#30 lore raw
Quoted 9 lines · Ingo Molnar
Quoted 7 lines · Linus Torvalds
Quoted 2 lines · David Woodhouse

This is a mitigation for the 'variant 2' attack described in
https://googleprojectzero.blogspot.com/2018/01/reading-privileged-memory-with-side.html

Ok, I don't love the patches, but I see nothing horribly wrong here either, and I assume the performance impact of this is pretty minimal.

Quoted 8 lines
Quoted 3 lines

Thomas? I'm obviously doing rc7 today without these, but I assume the x86 maintainers are resigned to this all. And yes, we'll have at least an rc8 this release..

I'm definitely resigned to them, and with these patches being disclosed so late we don't have any good choices left, so a tentative:

Just doing the last polishing on that. I'll add your ack

#31 lore raw
In reply to David Woodhouse

One detail that is missing is that we still need RSB refill in some cases. This is not because the retpoline sequence itself will underflow (it is actually guaranteed not to, since it consumes only RSB entries that it generates.
But either to avoid poisoning of the RSB entries themselves, or to avoid the hardware turning to alternate predictors on RSB underflow.

Enumerating the cases we care about:

user->kernel in the absence of SMEP:
In the absence of SMEP, we must worry about user-generated RSB entries being consumable by kernel execution.
Generally speaking, for synchronous execution this will not occur (e.g. syscall, interrupt), however, one important case remains.
When we context switch between two threads, we should flush the RSB so that execution generated from the unbalanced return path on the thread that we just scheduled into, cannot consume RSB entries potentially installed by the prior thread.

kernel->kernel independent of SMEP:
While much harder to coordinate, facilities such as eBPF potentially allow exploitable return targets to be created.
Generally speaking (particularly if eBPF has been disabled) the risk is _much_ lower here, since we can only return into kernel execution that was already occurring on another thread (which could e.g. likely be attacked there directly independent of RSB poisoning.)

guest->hypervisor, independent of SMEP:
For guest ring0 -> host ring0 transitions, it is possible that the tagging only includes that the entry was only generated in a ring0 context. Meaning that a guest generated entry may be consumed by the host. This admits:

hypervisor_run_vcpu_implementation() {
<enter hardware virtualization context>
… run virtualized work (1)
<leave hardware virtualization context>
< update vmcs state, prior to any function return > (2)
< return from hypervisor_run_vcpu_implementation() to handle VMEXIT > (3)
}

A guest to craft poisoned entries at (1) which, if not flushed at (2), may immediately be eligible for consumption at (3).

While the cases above involve the crafting and use of poisoned entries. Recall also that one of the initial conditions was that we should avoid RSB underflow as some CPUs may try to use other indirect predictors when this occurs.

The cases we care about here are:
- When we return _into_ protected execution. For the kernel, this means when we exit interrupt context into kernel context, since may have emptied or reduced the number of RSB entries while in iinterrupt context.
- Context switch (even if we are returning to user code, we need to at unwind the scheduler/triggering frames that preempted it previously, considering that detail, this is a subset of the above, but listed for completeness)
- On VMEXIT (it turns out we need to worry about both poisoned entries, and no entries, the solution is a single refill nonetheless).
- Leaving deeper (>C1) c-states, which may have flushed hardware state
- Where we are unwinding call-chains of >16 entries[*]

[*] This is obviously the trickiest case. Fortunately, it is tough to exploit since such call-chains are reasonably rare, and action must typically be predicted at a considerable distance from where current execution lies. Both dramatically increasing the feasibility of an attack and lowering the bit-rate (number of ops per attempt is necessarily increased). For our systems, since we control the binary image we can determine this through aggregate profiling of every machine in the fleet. I'm happy to provide those symbols; but it's obviously restricted from complete coverage due to code differences. Generally, this is a level of paranoia no typical user will likely care about and only applies to a subset of CPUs.

Quoted 82 lines · David Woodhouse

This is a mitigation for the 'variant 2' attack described in
https://googleprojectzero.blogspot.com/2018/01/reading-
privileged-memory-with-side.html

Using GCC patches available from the hjl/indirect/gcc-7-branch/master branch of https://github.com/hjl-tools/gcc/commits/hjl and by manually patching assembler code, all vulnerable indirect branches (that occur after userspace first runs) are eliminated from the kernel.

They are replaced with a 'retpoline' call sequence which deliberately prevents speculation.

Fedora 27 packages of the updated compiler are available at
https://koji.fedoraproject.org/koji/taskinfo?taskID=24065739

v1: Initial post.
v2: Add CONFIG_RETPOLINE to build kernel without it.
Change warning messages.
Hide modpost warning message
v3: Update to the latest CET-capable retpoline version
Reinstate ALTERNATIVE support
v4: Finish reconciling Andi's and my patch sets, bug fixes.
Exclude objtool support for now
Add 'noretpoline' boot option
Add AMD retpoline alternative
v5: Silence MODVERSIONS warnings
Use pause;jmp loop instead of lfence;jmp
Switch to X86_FEATURE_RETPOLINE positive feature logic Emit thunks inline from assembler macros
Merge AMD support into initial patch
v6: Update to latest GCC patches with no dots in symbols
Fix MODVERSIONS properly(ish)
Fix typo breaking 32-bit, introduced in V5
Never set X86_FEATURE_RETPOLINE_AMD yet, pending confirmation

Andi Kleen (3):
x86/retpoline/irq32: Convert assembler indirect jumps x86/retpoline: Add boot time option to disable retpoline x86/retpoline: Exclude objtool with retpoline

David Woodhouse (7):
x86/retpoline: Add initial retpoline support x86/retpoline/crypto: Convert crypto assembler indirect jumps x86/retpoline/entry: Convert entry assembler indirect jumps x86/retpoline/ftrace: Convert ftrace assembler indirect jumps x86/retpoline/hyperv: Convert assembler indirect jumps x86/retpoline/xen: Convert Xen hypercall indirect jumps x86/retpoline/checksum32: Convert assembler indirect jumps

 Documentation/admin-guide/kernel-parameters.txt |  3 +
 arch/x86/Kconfig                                | 17 ++++-
 arch/x86/Kconfig.debug                          |  6 +-
 arch/x86/Makefile                               | 10 +++
 arch/x86/crypto/aesni-intel_asm.S               |  5 +-
 arch/x86/crypto/camellia-aesni-avx-asm_64.S     |  3 +-
 arch/x86/crypto/camellia-aesni-avx2-asm_64.S    |  3 +-
 arch/x86/crypto/crc32c-pcl-intel-asm_64.S       |  3 +-
 arch/x86/entry/entry_32.S                       |  5 +-
 arch/x86/entry/entry_64.S                       | 12 +++-
 arch/x86/include/asm/asm-prototypes.h           | 25 +++++++
 arch/x86/include/asm/cpufeatures.h              |  2 +
 arch/x86/include/asm/mshyperv.h                 | 18 ++---
 arch/x86/include/asm/nospec-branch.h            | 92
+++++++++++++++++++++++++
 arch/x86/include/asm/xen/hypercall.h            |  5 +-
 arch/x86/kernel/cpu/common.c                    |  3 +
 arch/x86/kernel/cpu/intel.c                     | 11 +++
 arch/x86/kernel/ftrace_32.S                     |  6 +-
 arch/x86/kernel/ftrace_64.S                     |  8 +--
 arch/x86/kernel/irq_32.c                        |  9 +--
 arch/x86/lib/Makefile                           |  1 +
 arch/x86/lib/checksum_32.S                      |  7 +-
 arch/x86/lib/retpoline.S                        | 48 +++++++++++++
 23 files changed, 264 insertions(+), 38 deletions(-)
 create mode 100644 arch/x86/include/asm/nospec-branch.h
 create mode 100644 arch/x86/lib/retpoline.S

--
2.7.4

#32 lore raw
Quoted 9 lines · Paul Turner

user->kernel in the absence of SMEP:
In the absence of SMEP, we must worry about user-generated RSB entries being consumable by kernel execution.
Generally speaking, for synchronous execution this will not occur (e.g. syscall, interrupt), however, one important case remains.
When we context switch between two threads, we should flush the RSB so that execution generated from the unbalanced return path on the thread that we just scheduled into, cannot consume RSB entries potentially installed by the prior thread.

I am still unclear whether this closes it completely, as when HT is on, the RSB is shared between the threads, right? Therefore one thread can poision it for the other without even context switch happening.

Signature · 2 lines
Jiri Kosina
SUSE Labs
#33 lore raw
Quoted 12 lines · Jiri Kosina
Quoted 9 lines · Paul Turner

user->kernel in the absence of SMEP:
In the absence of SMEP, we must worry about user-generated RSB entries being consumable by kernel execution.
Generally speaking, for synchronous execution this will not occur (e.g. syscall, interrupt), however, one important case remains.
When we context switch between two threads, we should flush the RSB so that execution generated from the unbalanced return path on the thread that we just scheduled into, cannot consume RSB entries potentially installed by the prior thread.

Quoted 4 lines

I am still unclear whether this closes it completely, as when HT is on, the RSB is shared between the threads, right? Therefore one thread can poision it for the other without even context switch happening.

See 2.6.1.1 [Replicated resources]:
"The return stack predictor is replicated to improve branch
prediction of return instructions"

(This is part of the reason that the sequence is attractive; its use of the RSB to control prediction naturally prevents cross-sibling attack.)

Quoted 4 lines

--
Jiri Kosina
SUSE Labs

#34 lore raw
In reply to Paul Turner

[ First send did not make list because gmail ate its plain-text force when I pasted content. ]

One detail that is missing is that we still need RSB refill in some cases. This is not because the retpoline sequence itself will underflow (it is actually guaranteed not to, since it consumes only RSB entries that it generates.
But either to avoid poisoning of the RSB entries themselves, or to avoid the hardware turning to alternate predictors on RSB underflow.

Enumerating the cases we care about:

user->kernel in the absence of SMEP:
In the absence of SMEP, we must worry about user-generated RSB entries being consumable by kernel execution.
Generally speaking, for synchronous execution this will not occur (e.g. syscall, interrupt), however, one important case remains. When we context switch between two threads, we should flush the RSB so that execution generated from the unbalanced return path on the thread that we just scheduled into, cannot consume RSB entries potentially installed by the prior thread.

kernel->kernel independent of SMEP:
While much harder to coordinate, facilities such as eBPF potentially allow exploitable return targets to be created.
Generally speaking (particularly if eBPF has been disabled) the risk is _much_ lower here, since we can only return into kernel execution that was already occurring on another thread (which could e.g. likely be attacked there directly independent of RSB poisoning.)

guest->hypervisor, independent of SMEP:
For guest ring0 -> host ring0 transitions, it is possible that the tagging only includes that the entry was only generated in a ring0 context. Meaning that a guest generated entry may be consumed by the host. This admits:

hypervisor_run_vcpu_implementation() {
<enter hardware virtualization context>
… run virtualized work (1)
<leave hardware virtualization context>
< update vmcs state, prior to any function return > (2)
< return from hypervisor_run_vcpu_implementation() to handle VMEXIT > (3)
}

A guest to craft poisoned entries at (1) which, if not flushed at (2), may immediately be eligible for consumption at (3).

While the cases above involve the crafting and use of poisoned entries. Recall also that one of the initial conditions was that we should avoid RSB underflow as some CPUs may try to use other indirect predictors when this occurs.

The cases we care about here are:
- When we return _into_ protected execution. For the kernel, this means when we exit interrupt context into kernel context, since may have emptied or reduced the number of RSB entries while in iinterrupt context.
- Context switch (even if we are returning to user code, we need to at unwind the scheduler/triggering frames that preempted it previously, considering that detail, this is a subset of the above, but listed for completeness)
- On VMEXIT (it turns out we need to worry about both poisoned entries, and no entries, the solution is a single refill nonetheless).
- Leaving deeper (>C1) c-states, which may have flushed hardware state
- Where we are unwinding call-chains of >16 entries[*]

[*] This is obviously the trickiest case. Fortunately, it is tough to exploit since such call-chains are reasonably rare, and action must typically be predicted at a considerable distance from where current execution lies. Both dramatically increasing the feasibility of an attack and lowering the bit-rate (number of ops per attempt is necessarily increased). For our systems, since we control the binary image we can determine this through aggregate profiling of every machine in the fleet. I'm happy to provide those symbols; but it's obviously restricted from complete coverage due to code differences. Generally, this is a level of paranoia no typical user will likely care about and only applies to a subset of CPUs.

A sequence for efficiently refilling the RSB is:

    mov $8, %rax;
    .align 16;
   3: call 4f;
  3p: pause; call 3p;
     .align 16;
  4: call 5f;
  4p: pause; call 4p;
     .align 16;
   5: dec %rax;
      jnz 3b;
      add $(16*8), %rsp;

This implementation uses 8 loops, with 2 calls per iteration. This is marginally faster than a single call per iteration. We did not observe useful benefit (particularly relative to text size) from further unrolling. This may also be usefully split into smaller (e.g. 4 or 8 call) segments where we can usefully pipeline/intermix with other operations. It includes retpoline type traps so that if an entry is consumed, it cannot lead to controlled speculation. On my test system it took ~43 cycles on average. Note that non-zero displacement calls should be used as these may be optimized to not interact with the RSB due to their use in fetching RIP for 32-bit relocations.

Quoted 161 lines · Paul Turner

One detail that is missing is that we still need RSB refill in some cases. This is not because the retpoline sequence itself will underflow (it is actually guaranteed not to, since it consumes only RSB entries that it generates.
But either to avoid poisoning of the RSB entries themselves, or to avoid the hardware turning to alternate predictors on RSB underflow.

Enumerating the cases we care about:

user->kernel in the absence of SMEP:
In the absence of SMEP, we must worry about user-generated RSB entries being consumable by kernel execution.
Generally speaking, for synchronous execution this will not occur (e.g. syscall, interrupt), however, one important case remains.
When we context switch between two threads, we should flush the RSB so that execution generated from the unbalanced return path on the thread that we just scheduled into, cannot consume RSB entries potentially installed by the prior thread.

kernel->kernel independent of SMEP:
While much harder to coordinate, facilities such as eBPF potentially allow exploitable return targets to be created.
Generally speaking (particularly if eBPF has been disabled) the risk is _much_ lower here, since we can only return into kernel execution that was already occurring on another thread (which could e.g. likely be attacked there directly independent of RSB poisoning.)

guest->hypervisor, independent of SMEP:
For guest ring0 -> host ring0 transitions, it is possible that the tagging only includes that the entry was only generated in a ring0 context. Meaning that a guest generated entry may be consumed by the host. This admits:

hypervisor_run_vcpu_implementation() {
<enter hardware virtualization context>
… run virtualized work (1)
<leave hardware virtualization context>
< update vmcs state, prior to any function return > (2)
< return from hypervisor_run_vcpu_implementation() to handle VMEXIT > (3)
}

A guest to craft poisoned entries at (1) which, if not flushed at (2), may immediately be eligible for consumption at (3).

While the cases above involve the crafting and use of poisoned entries. Recall also that one of the initial conditions was that we should avoid RSB underflow as some CPUs may try to use other indirect predictors when this occurs.

The cases we care about here are:
- When we return _into_ protected execution. For the kernel, this means when we exit interrupt context into kernel context, since may have emptied or reduced the number of RSB entries while in iinterrupt context.
- Context switch (even if we are returning to user code, we need to at unwind the scheduler/triggering frames that preempted it previously, considering that detail, this is a subset of the above, but listed for completeness)
- On VMEXIT (it turns out we need to worry about both poisoned entries, and no entries, the solution is a single refill nonetheless).
- Leaving deeper (>C1) c-states, which may have flushed hardware state
- Where we are unwinding call-chains of >16 entries[*]

[*] This is obviously the trickiest case. Fortunately, it is tough to exploit since such call-chains are reasonably rare, and action must typically be predicted at a considerable distance from where current execution lies. Both dramatically increasing the feasibility of an attack and lowering the bit-rate (number of ops per attempt is necessarily increased). For our systems, since we control the binary image we can determine this through aggregate profiling of every machine in the fleet. I'm happy to provide those symbols; but it's obviously restricted from complete coverage due to code differences. Generally, this is a level of paranoia no typical user will likely care about and only applies to a subset of CPUs.

Quoted 82 lines · David Woodhouse

This is a mitigation for the 'variant 2' attack described in

https://googleprojectzero.blogspot.com/2018/01/reading-privileged-memory-with-side.html

Using GCC patches available from the hjl/indirect/gcc-7-branch/master branch of https://github.com/hjl-tools/gcc/commits/hjl and by manually patching assembler code, all vulnerable indirect branches (that occur after userspace first runs) are eliminated from the kernel.

They are replaced with a 'retpoline' call sequence which deliberately prevents speculation.

Fedora 27 packages of the updated compiler are available at
https://koji.fedoraproject.org/koji/taskinfo?taskID=24065739

v1: Initial post.
v2: Add CONFIG_RETPOLINE to build kernel without it.
Change warning messages.
Hide modpost warning message
v3: Update to the latest CET-capable retpoline version
Reinstate ALTERNATIVE support
v4: Finish reconciling Andi's and my patch sets, bug fixes.
Exclude objtool support for now
Add 'noretpoline' boot option
Add AMD retpoline alternative
v5: Silence MODVERSIONS warnings
Use pause;jmp loop instead of lfence;jmp
Switch to X86_FEATURE_RETPOLINE positive feature logic Emit thunks inline from assembler macros
Merge AMD support into initial patch
v6: Update to latest GCC patches with no dots in symbols
Fix MODVERSIONS properly(ish)
Fix typo breaking 32-bit, introduced in V5
Never set X86_FEATURE_RETPOLINE_AMD yet, pending confirmation

Andi Kleen (3):
x86/retpoline/irq32: Convert assembler indirect jumps x86/retpoline: Add boot time option to disable retpoline x86/retpoline: Exclude objtool with retpoline

David Woodhouse (7):
x86/retpoline: Add initial retpoline support x86/retpoline/crypto: Convert crypto assembler indirect jumps x86/retpoline/entry: Convert entry assembler indirect jumps x86/retpoline/ftrace: Convert ftrace assembler indirect jumps x86/retpoline/hyperv: Convert assembler indirect jumps x86/retpoline/xen: Convert Xen hypercall indirect jumps x86/retpoline/checksum32: Convert assembler indirect jumps

 Documentation/admin-guide/kernel-parameters.txt |  3 +
 arch/x86/Kconfig                                | 17 ++++-
 arch/x86/Kconfig.debug                          |  6 +-
 arch/x86/Makefile                               | 10 +++
 arch/x86/crypto/aesni-intel_asm.S               |  5 +-
 arch/x86/crypto/camellia-aesni-avx-asm_64.S     |  3 +-
 arch/x86/crypto/camellia-aesni-avx2-asm_64.S    |  3 +-
 arch/x86/crypto/crc32c-pcl-intel-asm_64.S       |  3 +-
 arch/x86/entry/entry_32.S                       |  5 +-
 arch/x86/entry/entry_64.S                       | 12 +++-
 arch/x86/include/asm/asm-prototypes.h           | 25 +++++++
 arch/x86/include/asm/cpufeatures.h              |  2 +
 arch/x86/include/asm/mshyperv.h                 | 18 ++---
 arch/x86/include/asm/nospec-branch.h            | 92
+++++++++++++++++++++++++
 arch/x86/include/asm/xen/hypercall.h            |  5 +-
 arch/x86/kernel/cpu/common.c                    |  3 +
 arch/x86/kernel/cpu/intel.c                     | 11 +++
 arch/x86/kernel/ftrace_32.S                     |  6 +-
 arch/x86/kernel/ftrace_64.S                     |  8 +--
 arch/x86/kernel/irq_32.c                        |  9 +--
 arch/x86/lib/Makefile                           |  1 +
 arch/x86/lib/checksum_32.S                      |  7 +-
 arch/x86/lib/retpoline.S                        | 48 +++++++++++++
 23 files changed, 264 insertions(+), 38 deletions(-)
 create mode 100644 arch/x86/include/asm/nospec-branch.h
 create mode 100644 arch/x86/lib/retpoline.S

--
2.7.4

#35 lore raw
Quoted 12 lines · Paul Turner

A sequence for efficiently refilling the RSB is:

    mov $8, %rax;
    .align 16;
   3: call 4f;
  3p: pause; call 3p;
     .align 16;
  4: call 5f;
  4p: pause; call 4p;
     .align 16;
   5: dec %rax;
      jnz 3b;
      add $(16*8), %rsp;
Quoted 11 lines

This implementation uses 8 loops, with 2 calls per iteration. This is marginally faster than a single call per iteration. We did not observe useful benefit (particularly relative to text size) from further unrolling. This may also be usefully split into smaller (e.g. 4 or 8 call) segments where we can usefully pipeline/intermix with other operations. It includes retpoline type traps so that if an entry is consumed, it cannot lead to controlled speculation. On my test system it took ~43 cycles on average. Note that non-zero displacement calls should be used as these may be optimized to not interact with the RSB due to their use in fetching RIP for 32-bit relocations.

Guidance from both Intel and AMD still states that 32 calls are required in general.  Is your above code optimised for a specific processor which you know the RSB to be smaller on?

~Andrew

#36 lore raw

For Intel the manuals state that it's 16 entries -- 2.5.2.1 Agner also reports 16 (presumably experimentally measured) e.g.
http://www.agner.org/optimize/microarchitecture.pdf [3.8] For AMD it can be larger, for example 32 entries on Fam17h (but 16 entries on Fam16h).

For future proofing a binary, or a new AMD processor, 32 calls are required. I would suggest tuning this based on the current CPU (which also covers the future case while saving cycles now) to save overhead.

Quoted 30 lines · Andrew Cooper
Quoted 12 lines · Paul Turner

A sequence for efficiently refilling the RSB is:

    mov $8, %rax;
    .align 16;
   3: call 4f;
  3p: pause; call 3p;
     .align 16;
  4: call 5f;
  4p: pause; call 4p;
     .align 16;
   5: dec %rax;
      jnz 3b;
      add $(16*8), %rsp;
Quoted 11 lines

This implementation uses 8 loops, with 2 calls per iteration. This is marginally faster than a single call per iteration. We did not observe useful benefit (particularly relative to text size) from further unrolling. This may also be usefully split into smaller (e.g. 4 or 8 call) segments where we can usefully pipeline/intermix with other operations. It includes retpoline type traps so that if an entry is consumed, it cannot lead to controlled speculation. On my test system it took ~43 cycles on average. Note that non-zero displacement calls should be used as these may be optimized to not interact with the RSB due to their use in fetching RIP for 32-bit relocations.

Guidance from both Intel and AMD still states that 32 calls are required in general. Is your above code optimised for a specific processor which you know the RSB to be smaller on?

~Andrew

#37 lore raw
In reply to Paul Turner
Quoted 8 lines · Paul Turner

kernel->kernel independent of SMEP:
While much harder to coordinate, facilities such as eBPF potentially allow exploitable return targets to be created.
Generally speaking (particularly if eBPF has been disabled) the risk is _much_ lower here, since we can only return into kernel execution that was already occurring on another thread (which could e.g. likely be attacked there directly independent of RSB poisoning.)

we can remove bpf interpreter without losing features:
https://patchwork.ozlabs.org/patch/856694/
Ironically JIT is more secure than interpreter.

#38 lore raw
In reply to Paul Turner
Quoted 5 lines · Paul Turner

While the cases above involve the crafting and use of poisoned entries.  Recall also that one of the initial conditions was that we should avoid RSB underflow as some CPUs may try to use other indirect predictors when this occurs.

I think we should start by deliberately ignoring the CPUs which use the other indirect predictors on RSB underflow. Those CPUs don't perform *quite* so badly with IBRS anyway.

Let's get the minimum amount of RSB handling in to cope with the pre- SKL CPUs, and then see if we really do want to extend it to make SKL 100% secure in retpoline mode or not.

So let's go through your list of cases and attempt to distinguish the underflow concerns (which I declare we don't care about for now) from the pollution (which we care about especially for non-SMEP) concerns...

Quoted 5 lines

The cases we care about here are:
- When we return _into_ protected execution.  For the kernel, this means when we exit interrupt context into kernel context, since may have emptied or reduced the number of RSB entries while in iinterrupt context.

Don't care about that particular example. That's underflow-only.

However, we *do* care about entry to kernel code from userspace, for interrupts and system calls etc. Basically everywhere that the IBRS code would be setting IBRS, we need to flush the RSB (if !SMEP, I think).

Quoted 4 lines

- Context switch (even if we are returning to user code, we need to at unwind the scheduler/triggering frames that preempted it previously, considering that detail, this is a subset of the above, but listed for completeness)

Don't care. This is underflow-only. (Which means I think we want to drop Andi's patch?)

Quoted 3 lines

- On VMEXIT (it turns out we need to worry about both poisoned entries, and no entries, the solution is a single refill nonetheless).

Do care. This fixes pollution from the guest, and even SMEP isn't enough to make us not care.

Quoted 2 lines

- Leaving deeper (>C1) c-states, which may have flushed hardware state

Don't care.

Quoted 1 line

- Where we are unwinding call-chains of >16 entries[*]

Don't care.

Overall, I think the RSB-stuffing is needed in all the same places that it's needed with IBRS.

#39 lore raw
Quoted 7 lines · * Woodhouse, David ([email protected]) wrote:
Quoted 5 lines · Paul Turner

While the cases above involve the crafting and use of poisoned entries.  Recall also that one of the initial conditions was that we should avoid RSB underflow as some CPUs may try to use other indirect predictors when this occurs.

Quoted 7 lines

I think we should start by deliberately ignoring the CPUs which use the other indirect predictors on RSB underflow. Those CPUs don't perform *quite* so badly with IBRS anyway.

Let's get the minimum amount of RSB handling in to cope with the pre- SKL CPUs, and then see if we really do want to extend it to make SKL 100% secure in retpoline mode or not.

How do you make decisions on which CPU you're running on? I'm worried about the case of a VM that starts off on an older host and then gets live migrated to a new Skylake.
For Intel CPUs we've historically been safe to live migrate to any newer host based on having all the features that the old one had; with the guest still seeing the flags etc for the old CPU.

Dave
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#40 lore raw
In reply to Paul Turner
Quoted 10 lines · Paul Turner

One detail that is missing is that we still need RSB refill in some cases.
This is not because the retpoline sequence itself will underflow (it is actually guaranteed not to, since it consumes only RSB entries that it generates.
But either to avoid poisoning of the RSB entries themselves, or to avoid the hardware turning to alternate predictors on RSB underflow.

Enumerating the cases we care about:

Quoted 9 lines

• user->kernel in the absence of SMEP:
In the absence of SMEP, we must worry about user-generated RSB entries being consumable by kernel execution.
Generally speaking, for synchronous execution this will not occur (e.g. syscall, interrupt), however, one important case remains. When we context switch between two threads, we should flush the RSB so that execution generated from the unbalanced return path on the thread that we just scheduled into, cannot consume RSB entries potentially installed by the prior thread.

Or IBPB here, yes? That's what we had in the original patch set when retpoline came last, and what I assume will be put back again once we *finally* get our act together and reinstate the full set of microcode patches.

Quoted 8 lines

kernel->kernel independent of SMEP:
While much harder to coordinate, facilities such as eBPF potentially allow exploitable return targets to be created.
Generally speaking (particularly if eBPF has been disabled) the risk is _much_ lower here, since we can only return into kernel execution that was already occurring on another thread (which could e.g. likely be attacked there directly independent of RSB poisoning.)

Quoted 5 lines

guest->hypervisor, independent of SMEP:
For guest ring0 -> host ring0 transitions, it is possible that the tagging only includes that the entry was only generated in a ring0 context.  Meaning that a guest generated entry may be consumed by the host.  This admits:

We are also stuffing the RSB on vmexit in the IBRS/IBPB patch set, aren't we?

#41 lore raw
Quoted 21 lines · David Woodhouse
Quoted 10 lines · Paul Turner

One detail that is missing is that we still need RSB refill in some cases.
This is not because the retpoline sequence itself will underflow (it is actually guaranteed not to, since it consumes only RSB entries that it generates.
But either to avoid poisoning of the RSB entries themselves, or to avoid the hardware turning to alternate predictors on RSB underflow.

Enumerating the cases we care about:

Quoted 9 lines

• user->kernel in the absence of SMEP:
In the absence of SMEP, we must worry about user-generated RSB entries being consumable by kernel execution.
Generally speaking, for synchronous execution this will not occur (e.g. syscall, interrupt), however, one important case remains. When we context switch between two threads, we should flush the RSB so that execution generated from the unbalanced return path on the thread that we just scheduled into, cannot consume RSB entries potentially installed by the prior thread.

Quoted 4 lines

Or IBPB here, yes? That's what we had in the original patch set when retpoline came last, and what I assume will be put back again once we *finally* get our act together and reinstate the full set of microcode patches.

IBPB is *much* more expensive than the sequence I suggested. If the kernel has been protected with a retpoline compilation, it is much faster to not use IBPB here; we only need to prevent ret-poisoning in this case.

Quoted 9 lines
Quoted 9 lines

kernel->kernel independent of SMEP:
While much harder to coordinate, facilities such as eBPF potentially allow exploitable return targets to be created.
Generally speaking (particularly if eBPF has been disabled) the risk is _much_ lower here, since we can only return into kernel execution that was already occurring on another thread (which could e.g. likely be attacked there directly independent of RSB poisoning.)

Quoted 8 lines
Quoted 5 lines

guest->hypervisor, independent of SMEP:
For guest ring0 -> host ring0 transitions, it is possible that the tagging only includes that the entry was only generated in a ring0 context. Meaning that a guest generated entry may be consumed by the host. This admits:

We are also stuffing the RSB on vmexit in the IBRS/IBPB patch set, aren't we?

A) I am enumerating all of the cases for completeness. It was missed by many that this detail was necessary on this patch, independently of IBRS.
B) On the parts duplicated in (A), for specifics that are contributory to correctness in both cases, we should not hand-wave over the fact that they may or may not be covered by another patch-set. Users need to understand what's required for complete protection. Particularly if they are backporting.

#42 lore raw
Quoted 29 lines
Quoted 21 lines · David Woodhouse
Quoted 10 lines · Paul Turner

One detail that is missing is that we still need RSB refill in some cases.
This is not because the retpoline sequence itself will underflow (it is actually guaranteed not to, since it consumes only RSB entries that it generates.
But either to avoid poisoning of the RSB entries themselves, or to avoid the hardware turning to alternate predictors on RSB underflow.

Enumerating the cases we care about:

Quoted 9 lines

• user->kernel in the absence of SMEP:
In the absence of SMEP, we must worry about user-generated RSB entries being consumable by kernel execution.
Generally speaking, for synchronous execution this will not occur (e.g. syscall, interrupt), however, one important case remains. When we context switch between two threads, we should flush the RSB so that execution generated from the unbalanced return path on the thread that we just scheduled into, cannot consume RSB entries potentially installed by the prior thread.

Quoted 4 lines

Or IBPB here, yes? That's what we had in the original patch set when retpoline came last, and what I assume will be put back again once we *finally* get our act together and reinstate the full set of microcode patches.

Quoted 4 lines

IBPB is *much* more expensive than the sequence I suggested. If the kernel has been protected with a retpoline compilation, it is much faster to not use IBPB here; we only need to prevent ret-poisoning in this case.

Retpoline protects the kernel but IBPB is needed on context switch anyway to protect userspace processes from each other.

But...

Quoted 8 lines

A) I am enumerating all of the cases for completeness. It was missed by many that this detail was necessary on this patch, independently of IBRS.
B) On the parts duplicated in (A), for specifics that are contributory to correctness in both cases, we should not hand-wave over the fact that they may or may not be covered by another patch-set. Users need to understand what's required for complete protection. Particularly if they are backporting.

... yes, agreed. Now we are putting retpoline first we shouldn't miss things that we *were* doing anyway. TBH I really don't think we should have spilt the patch sets apart; we'll work on getting the rest on top ASAP.

Signature · 1 line
dwmw2
#43 lore raw

[PATCH v6 11/10] x86/retpoline: Avoid return buffer underflows on context switch

In reply to David Woodhouse

This patch further hardens retpoline.

CPUs have return buffers which store the return address for RET to predict function returns. Some CPUs (Skylake, some Broadwells) can fall back to indirect branch prediction on return buffer underflow.

With retpoline we want to avoid uncontrolled indirect branches, which could be poisoned by ring 3, so we need to avoid uncontrolled return buffer underflows in the kernel.

This can happen when we're context switching from a shallower to a deeper kernel stack. The deeper kernel stack would eventually underflow the return buffer, which again would fall back to the indirect branch predictor.

To guard against this fill the return buffer with controlled content during context switch. This prevents any underflows.

We always fill the buffer with 30 entries: 32 minus 2 for at least one call from entry_{64,32}.S to C code and another into the function doing the filling.

That's pessimistic because we likely did more controlled kernel calls. So in principle we could do less. However it's hard to maintain such an invariant, and it may be broken with more aggressive compilers. So err on the side of safety and always fill 30.

[dwmw2: Fix comments about nop between calls,
Move #ifdef CONFIG_RETPOLINE to call sites not macro]

---
 arch/x86/entry/entry_32.S            | 17 +++++++++++++++++
 arch/x86/entry/entry_64.S            | 17 +++++++++++++++++
 arch/x86/include/asm/nospec-branch.h | 30 ++++++++++++++++++++++++++++++
 3 files changed, 64 insertions(+)
arch/x86/entry/entry_32.S +17 -0
index cf9ef33d299b..b6b83b9d3a0b 100644
@@ -250,6 +250,23 @@ ENTRY(__switch_to_asm)
 	popl	%ebx
 	popl	%ebp
 
+#ifdef CONFIG_RETPOLINE
+	/*
+	 * When we switch from a shallower to a deeper call stack
+	 * the call stack will underflow in the kernel in the next task.
+	 * This could cause the CPU to fall back to indirect branch
+	 * prediction, which may be poisoned.
+	 *
+	 * To guard against that always fill the return stack with
+	 * known values.
+	 *
+	 * We do this in assembler because it needs to be before
+	 * any calls on the new stack, and this can be difficult to
+	 * ensure in a complex C function like __switch_to.
+	 */
+	ALTERNATIVE "jmp __switch_to", "", X86_FEATURE_RETPOLINE
+	FILL_RETURN_BUFFER
+#endif
 	jmp	__switch_to
 END(__switch_to_asm)
 
arch/x86/entry/entry_64.S +17 -0
index 9bce6ed03353..1622e07c5ae8 100644
@@ -495,6 +495,23 @@ ENTRY(__switch_to_asm)
 	popq	%rbx
 	popq	%rbp
 
+#ifdef CONFIG_RETPOLINE
+	/*
+	 * When we switch from a shallower to a deeper call stack
+	 * the call stack will underflow in the kernel in the next task.
+	 * This could cause the CPU to fall back to indirect branch
+	 * prediction, which may be poisoned.
+	 *
+	 * To guard against that always fill the return stack with
+	 * known values.
+	 *
+	 * We do this in assembler because it needs to be before
+	 * any calls on the new stack, and this can be difficult to
+	 * ensure in a complex C function like __switch_to.
+	 */
+	ALTERNATIVE "jmp __switch_to", "", X86_FEATURE_RETPOLINE
+	FILL_RETURN_BUFFER
+#endif
 	jmp	__switch_to
 END(__switch_to_asm)
 
arch/x86/include/asm/nospec-branch.h +30 -0
index b8c8eeacb4be..3022b1a4de17 100644
@@ -53,6 +53,36 @@
 #endif
 .endm
 
+/*
+ * We use 32-N: 32 is the max return buffer size, but there should
+ * have been at a minimum two controlled calls already: one into the
+ * kernel from entry*.S and another into the function containing this
+ * macro. So N=2, thus 30.
+ */
+#define NUM_BRANCHES_TO_FILL	30
+
+/*
+ * Fill the CPU return stack buffer to prevent indirect branch
+ * prediction on underflow. We need a 'nop' after each call so it
+ * isn't interpreted by the CPU as a simple 'push %eip', which would
+ * be handled specially and not put anything in the RSB.
+ *
+ * Required in various cases for retpoline and IBRS-based mitigations
+ * for Spectre variant 2 vulnerability.
+ */
+.macro FILL_RETURN_BUFFER
+	.rept	NUM_BRANCHES_TO_FILL
+	call	1221f
+	nop
+1221:
+	.endr
+#ifdef CONFIG_64BIT
+	addq	$8*NUM_BRANCHES_TO_FILL, %rsp
+#else
+	addl    $4*NUM_BRANCHES_TO_FILL, %esp
+#endif
+.endm
+
 #else /* __ASSEMBLY__ */
 
 #if defined(CONFIG_X86_64) && defined(RETPOLINE)
Signature · 1 line
2.14.3
#44 lore raw

Re: [PATCH v6 11/10] x86/retpoline: Avoid return buffer underflows on context switch

Quoted 3 lines · David Woodhouse

To guard against this fill the return buffer with controlled content during context switch. This prevents any underflows.

Ugh. I really dislike this patch. Everything else in the retpoline patches makes me go "ok, that's reasonable". This one makes me go "Eww".

It's hacky, it's ugly, and it looks pretty expensive too.

Is there really nothing more clever we can do?

Linus

#45 lore raw

Re: [PATCH v6 11/10] x86/retpoline: Avoid return buffer underflows on context switch

Quoted 5 lines · Linus Torvalds
Quoted 3 lines · David Woodhouse

To guard against this fill the return buffer with controlled content during context switch. This prevents any underflows.

Quoted 7 lines

Ugh. I really dislike this patch. Everything else in the retpoline patches makes me go "ok, that's reasonable". This one makes me go "Eww".

It's hacky, it's ugly, and it looks pretty expensive too.

Is there really nothing more clever we can do?

You get this part in the IBRS/microcode solution too. The IBRS MSR doesn't catch everything; you still need to stuff the RSB in very similar places (and/or use the IBPB MSR in some).

#46 lore raw

Re: [PATCH v6 11/10] x86/retpoline: Avoid return buffer underflows on context switch

Quoted 6 lines · David
Quoted 2 lines

Is there really nothing more clever we can do?

You get this part in the IBRS/microcode solution too. The IBRS MSR doesn't catch everything; you still need to stuff the RSB in very similar places (and/or use the IBPB MSR in some).

So I was really hoping that in places like context switching etc, we'd be able to instead effectively kill off any exploits by clearing registers.

That should make it pretty damn hard to then find a matching "gadget" that actually does anything interesting/powerful.

Together with Spectre already being pretty hard to take advantage of, and the eBPF people making those user-proivided gadgets inaccessible, it really should be a pretty powerful fix.

Hmm?

Linus

#47 lore raw

Re: [PATCH v6 11/10] x86/retpoline: Avoid return buffer underflows on context switch

Quoted 12 lines · Linus Torvalds
Quoted 6 lines · David
Quoted 2 lines

Is there really nothing more clever we can do?

You get this part in the IBRS/microcode solution too. The IBRS MSR doesn't catch everything; you still need to stuff the RSB in very similar places (and/or use the IBPB MSR in some).

So I was really hoping that in places like context switching etc, we'd be able to instead effectively kill off any exploits by clearing registers.

Quoted 6 lines

That should make it pretty damn hard to then find a matching "gadget" that actually does anything interesting/powerful.

Together with Spectre already being pretty hard to take advantage of, and the eBPF people making those user-proivided gadgets inaccessible, it really should be a pretty powerful fix.

Hm... on a context switch you're reloading the registers that were in the other saved context. If the attacker does something they know will go into a deep call stack and then sleep, and they pollute the BTB for every 'ret' instruction in that stack¹, then the registers at the time a 'ret' goes AWOL are not necessarily something you can 'clear' at the time of the context switch.

And yes, a lot of this v2 stuff down past the sys_call_table branch really *is* pretty damn hard already, especially the 'ret' based ones.

¹ perhaps they do the BTB-pollution in their other CPU-hogging thread that runs on the same CPU when the attack thread is sleeping? I'm not really trying to actually write an attack here, just thinking out loud and being uncertain that you've actually proved there *isn't* one :)

#48 lore raw

Re: [PATCH v6 11/10] x86/retpoline: Avoid return buffer underflows on context switch

Quoted 3 lines · David Woodhouse

Hm... on a context switch you're reloading the registers that were in the other saved context.

Actually, iirc we used to very actively try to minimize that by having the inline asm mark a lot of registers as clobbered.

We moved away from that and now have that "switch_to_asm()" call instead, but that was for unrelated reasons.

If I remember our old inline asm, we actually had *very* little real data that was actually live on context switch, particularly that last "branch to new EIP" point.

Partly because we had different targets, one of which was that "return from fork" case.

But maybe I mis-remember. Wouldn't be the first time. This is code I used to know well, but that was many many moons ago, now there are other suckers^W maintainers who actually work with it.

Linus

#49 lore raw

Re: [PATCH v6 11/10] x86/retpoline: Avoid return buffer underflows on context switch

Quoted 8 lines · Linus Torvalds
Quoted 4 lines · David Woodhouse

Hm... on a context switch you're reloading the registers that were in the other saved context.

Actually, iirc we used to very actively try to minimize that by having the inline asm mark a lot of registers as clobbered.

Sure, in that case it makes sense purely as a matter of hygiene to explicitly clear the registers which were marked as clobbered.

In the original patch set from that Intel were collecting — before they threw it all out the fucking window and started again from scratch on the day the embargo broke — there were patches to do the same thing on syscall entry too, for precisely the same reason.

#50 lore raw

Re: [PATCH v6 11/10] x86/retpoline: Avoid return buffer underflows on context switch

In reply to Linus Torvalds
Quoted 4 lines

So I was really hoping that in places like context switching etc, we'd be able to instead effectively kill off any exploits by clearing registers.

Quoted 8 lines

That should make it pretty damn hard to then find a matching "gadget" that actually does anything interesting/powerful.

Together with Spectre already being pretty hard to take advantage of, and the eBPF people making those user-proivided gadgets inaccessible, it really should be a pretty powerful fix.

Hmm?

Essentially the RSB are hidden registers, and the only way to clear them is the FILL_RETURN_BUFFER sequence. I don't see how clearing anything else would help?

-Andi

#51 lore raw

Re: [PATCH v6 11/10] x86/retpoline: Avoid return buffer underflows on context switch

Quoted 4 lines · Andi Kleen

Essentially the RSB are hidden registers, and the only way to clear them is the FILL_RETURN_BUFFER sequence. I don't see how clearing anything else would help?

Forget theory. Look at practice.

Let's just assume that the attacker can write arbitrarily to the RSB state. Just accept it.

If you accept that, then you turn the question instead into: are there things we can do to make that useless to an attacker.

And there the point is that even if you control the RSB contents, you need to find something relevant to *put* in the RSB. You need to find the gadget that makes that control of the RSB useful.

That's where clearing the other registers comes in. Particularly for the shallow cases (maybe the attack involves calling a shallow system call that goes to the scheduler immediately, like "pause()").

Finding those gadgets is already hard. And then you have to find them in such a way that you can control some state that you can start reading out arbitrary memory. So you need to not just find the gadget, you need to pass in an interesting pointer to it.

Preferably a pointer you control easily, like one of the registers that nobody used on the way from the attacking user space to the scheduler() call. That's already going to be damn hard, but with the C compiler saving many registers by default, it might not be impossible.

And THAT is where "clear the registers" comes in. It adds _another_ huge barrier to an attack that was already pretty hard to begin with.

If we clear the registers, what the hell are you going to put in the RSB that helps you?

I really think that people need to think about the actual _practical_ side here. We're never ever going to solve all theoretical timing attacks. But when it comes to something like Spectre, it's already very much non-trivial to attack. When then looking at something like "a few call chains deep in the scheduler", it got harder still. If we clear registers and don't give the attacker a way to leak data, that's yet another big barrier.

So instead of saying "we have to flush the return stack", I'm saying that we should look at things that make flushing the return stack _unnecessary_, simply because even if the attacker were to control it entirely, they'd still be up shit creek without a paddle.

Linus

#52 lore raw

Re: [PATCH v6 11/10] x86/retpoline: Avoid return buffer underflows on context switch

Quoted 2 lines

If we clear the registers, what the hell are you going to put in the RSB that helps you?

RSB allows you to control chains of gadgets.

You can likely find some chain of gadgets that set up constants in registers in a lot of useful ways. Perhaps not any way (so may be hard to scan through all of memory), but it's likely you could find gadgets that result in a lot of useful direct mapped addresses, which the next gadget can then reference.

Especially RAX is quite vulnerable to this because there will be a lot of code that does "modify RAX in interesting ways ; RET"

Quoted 4 lines

So instead of saying "we have to flush the return stack", I'm saying that we should look at things that make flushing the return stack _unnecessary_, simply because even if the attacker were to control it entirely, they'd still be up shit creek without a paddle.

I agree that clearing registers is useful (was just hacking on that patch).

-Andi

#53 lore raw

Re: [PATCH v6 11/10] x86/retpoline: Avoid return buffer underflows on context switch II

Quoted 4 lines · Andi Kleen
Quoted 2 lines

If we clear the registers, what the hell are you going to put in the RSB that helps you?

RSB allows you to control chains of gadgets.

I admit the gadget thing is a bit obscure.

There's another case we were actually more worried about:

On Skylake and Broadwell when the RSB underflows it will fall back to the indirect branch predictor, which can be poisoned and we try to avoid using with retpoline. So we try to avoid underflows, and this filling helps us with that.

Does that make more sense?

-Andi

#54 lore raw

Re: [PATCH v6 11/10] x86/retpoline: Avoid return buffer underflows on context switch II

Quoted 6 lines · Andi Kleen
Quoted 4 lines · Andi Kleen
Quoted 2 lines

If we clear the registers, what the hell are you going to put in the RSB that helps you?


RSB allows you to control chains of gadgets.

Quoted 8 lines

I admit the gadget thing is a bit obscure.

There's another case we were actually more worried about:

On Skylake and Broadwell when the RSB underflows it will fall back to the indirect branch predictor, which can be poisoned and we try to avoid using with retpoline. So we try to avoid underflows, and this filling helps us with that.

That's no longer true for Broadwell with the latest microcode, right?

And is why we *were* talking about using retpoline only for pre- Skylake, and using IBRS on Skylake onwards (where IBRS isn't quite so horribly slow anyway).

#55 lore raw

Re: [PATCH v6 11/10] x86/retpoline: Avoid return buffer underflows on context switch II

Quoted 6 lines
Quoted 4 lines

On Skylake and Broadwell when the RSB underflows it will fall back to the indirect branch predictor, which can be poisoned and we try to avoid using with retpoline. So we try to avoid underflows, and this filling helps us with that.

That's no longer true for Broadwell with the latest microcode, right?

That's right.

-Andi

#56 lore raw

Re: [PATCH v6 11/10] x86/retpoline: Avoid return buffer underflows on context switch II

In reply to Andi Kleen
Quoted 11 lines · Andi Kleen
Quoted 4 lines · Andi Kleen
Quoted 2 lines

If we clear the registers, what the hell are you going to put in the RSB that helps you?

RSB allows you to control chains of gadgets.

I admit the gadget thing is a bit obscure.

There's another case we were actually more worried about:

On Skylake and Broadwell when the RSB underflows it will fall back to the

(Broadwell without Microcode)

Quoted 5 lines

indirect branch predictor, which can be poisoned and we try to avoid using with retpoline. So we try to avoid underflows, and this filling helps us with that.

Does that make more sense?

The majority of the confusion does not stem from this being complicated. It's that there's been great reluctance to document the details which are different -- or changed by the microcode -- even at a high level such as this.
Because of this, some of the details instead include vague "things are different on Skylake" notes, with no exposition.

Quoted 2 lines

-Andi

#57 lore raw

Re: [PATCH v6 11/10] x86/retpoline: Avoid return buffer underflows on context switch

In reply to Linus Torvalds
Quoted 9 lines · Linus Torvalds
Quoted 3 lines · Andi Kleen

Essentially the RSB are hidden registers, and the only way to clear them is the FILL_RETURN_BUFFER sequence. I don't see how clearing anything else would help?

Forget theory. Look at practice.

Let's just assume that the attacker can write arbitrarily to the RSB state. Just accept it.

Quoted 6 lines

If you accept that, then you turn the question instead into: are there things we can do to make that useless to an attacker.

And there the point is that even if you control the RSB contents, you need to find something relevant to *put* in the RSB. You need to find the gadget that makes that control of the RSB useful.

This is where the problem lies.  An attacker with arbitrary control of the RSB can redirect speculation arbitrarily.  (And I agree, this is a good default assumption to take).

If SMEP is not active, speculation can go anywhere, including to a user controlled gadget which can reload any registers it needs, including with immediate constants.

If SMEP is active, the attackers control of speculation is restricted to supervisor executable mappings.

The real question is whether it is worth special casing the SMEP-active case given that for the SMEP-inactive case, your only viable option is to refill the RSB and discard any potentially poisoned mappings.

~Andrew

#58 lore raw

Re: [PATCH v6 11/10] x86/retpoline: Avoid return buffer underflows on context switch

Quoted 20 lines · Andrew Cooper
Quoted 11 lines · Linus Torvalds
Quoted 4 lines · Andi Kleen

Essentially the RSB are hidden registers, and the only way to clear them is the FILL_RETURN_BUFFER sequence. I don't see how clearing anything else would help?

Forget theory. Look at practice.

Let's just assume that the attacker can write arbitrarily to the RSB state. Just accept it.

Quoted 6 lines

If you accept that, then you turn the question instead into: are there things we can do to make that useless to an attacker.

And there the point is that even if you control the RSB contents, you need to find something relevant to *put* in the RSB. You need to find the gadget that makes that control of the RSB useful.

Quoted 7 lines

This is where the problem lies. An attacker with arbitrary control of the RSB can redirect speculation arbitrarily. (And I agree, this is a good default assumption to take).

If SMEP is not active, speculation can go anywhere, including to a user controlled gadget which can reload any registers it needs, including with immediate constants.

I thought that, even on pre-SMEP hardware, the CPU wouldn't speculatively execute from NX pages. And PTI marks user memory NX in kernel mode.

Quoted 9 lines

If SMEP is active, the attackers control of speculation is restricted to supervisor executable mappings.

The real question is whether it is worth special casing the SMEP-active case given that for the SMEP-inactive case, your only viable option is to refill the RSB and discard any potentially poisoned mappings.

~Andrew

#59 lore raw

Re: [PATCH v6 11/10] x86/retpoline: Avoid return buffer underflows on context switch

Quoted 8 lines · Andy Lutomirski
Quoted 4 lines


If SMEP is not active, speculation can go anywhere, including to a user controlled gadget which can reload any registers it needs, including with immediate constants.

I thought that, even on pre-SMEP hardware, the CPU wouldn't speculatively execute from NX pages.  And PTI marks user memory NX in kernel mode.

Hm, now that could be useful.

Do *all* the KPTI backports (some of which are reimplementations rather than strictly backports) mark user memory NX?

maintainer
#60 lore raw

Re: [PATCH v6 11/10] x86/retpoline: Avoid return buffer underflows on context switch

Quoted 6 lines · David Woodhouse
Quoted 5 lines · Andy Lutomirski
Quoted 4 lines


If SMEP is not active, speculation can go anywhere, including to a user controlled gadget which can reload any registers it needs, including with immediate constants.

Quoted 8 lines
Quoted 3 lines

I thought that, even on pre-SMEP hardware, the CPU wouldn't speculatively execute from NX pages.  And PTI marks user memory NX in kernel mode.

Hm, now that could be useful.

Do *all* the KPTI backports (some of which are reimplementations rather than strictly backports) mark user memory NX?

That doesn't really matter for upstream though; but see here:

https://lkml.kernel.org/r/[email protected]

#61 lore raw

Re: [PATCH v6 11/10] x86/retpoline: Avoid return buffer underflows on context switch

In reply to David Woodhouse
Quoted 6 lines · David Woodhouse
Quoted 5 lines · Andy Lutomirski
Quoted 4 lines

If SMEP is not active, speculation can go anywhere, including to a user controlled gadget which can reload any registers it needs, including with immediate constants.

Quoted 8 lines
Quoted 3 lines

I thought that, even on pre-SMEP hardware, the CPU wouldn't speculatively execute from NX pages. And PTI marks user memory NX in kernel mode.

Hm, now that could be useful.

Do *all* the KPTI backports (some of which are reimplementations rather than strictly backports) mark user memory NX?

Yup. The KAISERish ports (4.9 and 4.4) have the same feature.

-Kees

Signature · 2 lines
Kees Cook
Pixel Security
#62 lore raw

Re: [PATCH v6 11/10] x86/retpoline: Avoid return buffer underflows on context switch

In reply to David Woodhouse
Quoted 10 lines · David Woodhouse
Quoted 4 lines · Andy Lutomirski

I thought that, even on pre-SMEP hardware, the CPU wouldn't speculatively execute from NX pages. And PTI marks user memory NX in kernel mode.

Hm, now that could be useful.

Do *all* the KPTI backports (some of which are reimplementations rather than strictly backports) mark user memory NX?

As Kees said - yes. We're looking at patches to mark some processes trusted and turn off KPTI for those, and then they'll obviously not have that, but the whole point is that you'd trust them.

Also notice that the fundamental statement of "If SMEP is not active, speculation can go anywhere" is not necessarily true either.

One of the reasons why the BTB can be poisoned from user space in the first place is because the BTB doesn't have the full range of virtual address bits for BTB lookup.

But at least on Intel, I think they don't have the full range of virtual address bits for the _result_ either. The upper bits are simply taken from the target. So the prediction only looks at - and only affects - the lower bits of the indirect branch.

Which means that the whole "speculation can go anywhere" is garbage, at least in general. You can't force the kernel to speculatively jump to a user space address, because even if you have complete control of the BTB, you only end up controlling the low bits of the predicted address, because those are the only ones that exist in the BTB.

That all is obviously very implementation-dependent. You could have a BTB that uses a small set of bits for the index checking, but the full set of bits for target.

But since the problem (for the kernel) mostly arises from exactly the fact that the BTB doesn't have the full bits for indexing, I suspect the "not full bits in the target" is common too.

Linus

#63 lore raw

Re: [PATCH v6 11/10] x86/retpoline: Avoid return buffer underflows on context switch

In reply to David Woodhouse
Quoted 6 lines · David Woodhouse
Quoted 5 lines · Andy Lutomirski
Quoted 4 lines


If SMEP is not active, speculation can go anywhere, including to a user controlled gadget which can reload any registers it needs, including with immediate constants.

Quoted 8 lines
Quoted 3 lines

I thought that, even on pre-SMEP hardware, the CPU wouldn't speculatively execute from NX pages.  And PTI marks user memory NX in kernel mode.

Hm, now that could be useful.

Do *all* the KPTI backports (some of which are reimplementations rather than strictly backports) mark user memory NX?

Hmm. We'd still want to do something on 32-bit, and those might not even have NX support in hardware.

Pentium 4 (and such) is probably advanced enough to be vulnerable to spectre, but not new enough to support NX...

Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#64 lore raw

Re: [PATCH v6 11/10] x86/retpoline: Avoid return buffer underflows on context switch

In reply to Linus Torvalds
Quoted 6 lines · Linus Torvalds

And THAT is where "clear the registers" comes in. It adds _another_ huge barrier to an attack that was already pretty hard to begin with.

If we clear the registers, what the hell are you going to put in the RSB that helps you?

As you go back up the call stack, a lot of caller-saved registers are going to end up being reloaded, before you hit the mispredicted 'ret'. Much of those could be attacker-controlled, by data passed in to the syscall or pre-populated in other data structures.

Clearing the call-clobbered registers in context switch will probably eliminate a few attack vectors, but I'm not sure it's even the majority.

Amazon Web Services UK Limited. Registered in England and Wales with registration number 08650665 and which has its registered office at 60 Holborn Viaduct, London EC1A 2FD, United Kingdom.

#65 lore raw

Re: [PATCH v6 11/10] x86/retpoline: Avoid return buffer underflows on context switch

In reply to Linus Torvalds
Quoted 10 lines · Linus Torvalds
Quoted 3 lines · David Woodhouse

To guard against this fill the return buffer with controlled content during context switch. This prevents any underflows.

Ugh. I really dislike this patch. Everything else in the retpoline patches makes me go "ok, that's reasonable". This one makes me go "Eww".

It's hacky, it's ugly, and it looks pretty expensive too.

Modern cores are quite fast at executing calls.

Quoted 2 lines

Is there really nothing more clever we can do?

We could be a cleverer in selecting how many dummy calls to do. But that would likely be fragile and hard to maintain and likely be more complicated, and I doubt it would buy that much.

Don't really have a better proposal, sorry.

-Andi