Skip to contentSkip to conversation

retpoline: add clang support + Kconfig selectable

PATCH Superseded Roger Pau Monne 2 participants 8 messages Feb 16, 2022

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover retpoline: add clang support + Kconfig selectable

Hello,

The following series adds retpoline support for clang builds, and also allows the user to select whether to enable retpoline support at build time via a new Kconfig option.

I've tried adding a suitable description to the Kconfig option, but I'm sure there's room for improvement.

Thanks, Roger.

Roger Pau Monne (3):
x86/retpoline: rename retpoline Kconfig check to include GCC prefix x86/clang: add retpoline support
x86/Kconfig: introduce option to select retpoline usage

 xen/arch/x86/Kconfig |  5 ++++-
 xen/arch/x86/arch.mk | 13 +++++++++----
 xen/common/Kconfig   | 16 ++++++++++++++++
 3 files changed, 29 insertions(+), 5 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/3 x86/retpoline: rename retpoline Kconfig check to include GCC prefix

Current retpoline checks apply to GCC only, so make it obvious by prefixing the Kconfig option with GCC. Keep the previous option as a way to signal generic retpoline support regardless of the underlying compiler.

No functional change intended.

---
 xen/arch/x86/Kconfig | 6 +++++-
 xen/arch/x86/arch.mk | 8 ++++----
 2 files changed, 9 insertions(+), 5 deletions(-)
xen/arch/x86/Kconfig +5 -1
index b4abfca46f..2cd713724f 100644
@@ -32,9 +32,13 @@ config ARCH_DEFCONFIG
 	string
 	default "arch/x86/configs/x86_64_defconfig"
 
-config INDIRECT_THUNK
+config GCC_INDIRECT_THUNK
 	def_bool $(cc-option,-mindirect-branch-register)
 
+config INDIRECT_THUNK
+	depends on GCC_INDIRECT_THUNK
+	def_bool y
+
 config HAS_AS_CET_SS
 	# binutils >= 2.29 or LLVM >= 6
 	def_bool $(as-instr,wrssq %rax$(comma)0;setssbsy)
xen/arch/x86/arch.mk +4 -4
index fa7cf38443..2da4bdb1ed 100644
@@ -42,10 +42,10 @@ CFLAGS += -mno-red-zone -fpic
 # SSE setup for variadic function calls.
 CFLAGS += -mno-sse $(call cc-option,$(CC),-mskip-rax-setup)
 
-# Compile with thunk-extern, indirect-branch-register if avaiable.
-CFLAGS-$(CONFIG_INDIRECT_THUNK) += -mindirect-branch=thunk-extern
-CFLAGS-$(CONFIG_INDIRECT_THUNK) += -mindirect-branch-register
-CFLAGS-$(CONFIG_INDIRECT_THUNK) += -fno-jump-tables
+# Compile with gcc thunk-extern, indirect-branch-register if available.
+CFLAGS-$(CONFIG_GCC_INDIRECT_THUNK) += -mindirect-branch=thunk-extern
+CFLAGS-$(CONFIG_GCC_INDIRECT_THUNK) += -mindirect-branch-register
+CFLAGS-$(CONFIG_GCC_INDIRECT_THUNK) += -fno-jump-tables
 
 # If supported by the compiler, reduce stack alignment to 8 bytes. But allow
 # this to be overridden elsewhere.
Signature · 1 line
2.34.1
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] x86/retpoline: rename retpoline Kconfig check to include GCC prefix

Quoted 8 lines · Roger Pau Monne

Current retpoline checks apply to GCC only, so make it obvious by prefixing the Kconfig option with GCC. Keep the previous option as a way to signal generic retpoline support regardless of the underlying compiler.

No functional change intended.

with one remark:

xen/arch/x86/Kconfig config ARCH_DEFCONFIG · line 40

Quoted 13 lines
xen/arch/x86/Kconfig +4 -1
@@ -32,9 +32,13 @@ config ARCH_DEFCONFIG
 	string
 	default "arch/x86/configs/x86_64_defconfig"
 
-config INDIRECT_THUNK
+config GCC_INDIRECT_THUNK
 	def_bool $(cc-option,-mindirect-branch-register)
 
+config INDIRECT_THUNK
+	depends on GCC_INDIRECT_THUNK
+	def_bool y

I think it is more logical (and more consistent) if "depends on" would come after the basic type (i.e. the "def_bool" here).

Jan

#3 lore raw

PATCH 2/3 x86/clang: add retpoline support

In reply to Roger Pau Monne

Detect whether the compiler supports clang retpoline option and enable by default if available, just like it's done for gcc.

Note clang already disables jump tables when retpoline is enabled, so there's no need to also pass the fno-jump-tables parameter.

---
 xen/arch/x86/Kconfig | 5 ++++-
 xen/arch/x86/arch.mk | 3 +++
 2 files changed, 7 insertions(+), 1 deletion(-)
xen/arch/x86/Kconfig +4 -1
index 2cd713724f..fe00b4598b 100644
@@ -35,8 +35,11 @@ config ARCH_DEFCONFIG
 config GCC_INDIRECT_THUNK
 	def_bool $(cc-option,-mindirect-branch-register)
 
+config CLANG_INDIRECT_THUNK
+	def_bool $(cc-option,-mretpoline-external-thunk)
+
 config INDIRECT_THUNK
-	depends on GCC_INDIRECT_THUNK
+	depends on GCC_INDIRECT_THUNK || CLANG_INDIRECT_THUNK
 	def_bool y
 
 config HAS_AS_CET_SS
xen/arch/x86/arch.mk +3 -0
index 2da4bdb1ed..f2aa2a515f 100644
@@ -47,6 +47,9 @@ CFLAGS-$(CONFIG_GCC_INDIRECT_THUNK) += -mindirect-branch=thunk-extern
 CFLAGS-$(CONFIG_GCC_INDIRECT_THUNK) += -mindirect-branch-register
 CFLAGS-$(CONFIG_GCC_INDIRECT_THUNK) += -fno-jump-tables
 
+# Enable clang retpoline support if available.
+CFLAGS-$(CONFIG_CLANG_INDIRECT_THUNK) += -mretpoline-external-thunk
+
 # If supported by the compiler, reduce stack alignment to 8 bytes. But allow
 # this to be overridden elsewhere.
 $(call cc-option-add,CFLAGS_stack_boundary,CC,-mpreferred-stack-boundary=3)
Signature · 1 line
2.34.1
#4 lore raw

Re: [PATCH 2/3] x86/clang: add retpoline support

Quoted 5 lines · Roger Pau Monne

Detect whether the compiler supports clang retpoline option and enable by default if available, just like it's done for gcc.

Note clang already disables jump tables when retpoline is enabled, so there's no need to also pass the fno-jump-tables parameter.

That's one of the secondary gcc side arrangements. What about the other (-mindirect-branch-register)?

Quoted 1 line

That's Andrew, not me?

Jan

Quoted 37 lines
---
 xen/arch/x86/Kconfig | 5 ++++-
 xen/arch/x86/arch.mk | 3 +++
 2 files changed, 7 insertions(+), 1 deletion(-)
xen/arch/x86/Kconfig +4 -1
index 2cd713724f..fe00b4598b 100644
@@ -35,8 +35,11 @@ config ARCH_DEFCONFIG
 config GCC_INDIRECT_THUNK
 	def_bool $(cc-option,-mindirect-branch-register)
 
+config CLANG_INDIRECT_THUNK
+	def_bool $(cc-option,-mretpoline-external-thunk)
+
 config INDIRECT_THUNK
-	depends on GCC_INDIRECT_THUNK
+	depends on GCC_INDIRECT_THUNK || CLANG_INDIRECT_THUNK
 	def_bool y
 
 config HAS_AS_CET_SS
xen/arch/x86/arch.mk +3 -0
index 2da4bdb1ed..f2aa2a515f 100644
@@ -47,6 +47,9 @@ CFLAGS-$(CONFIG_GCC_INDIRECT_THUNK) += -mindirect-branch=thunk-extern
 CFLAGS-$(CONFIG_GCC_INDIRECT_THUNK) += -mindirect-branch-register
 CFLAGS-$(CONFIG_GCC_INDIRECT_THUNK) += -fno-jump-tables
 
+# Enable clang retpoline support if available.
+CFLAGS-$(CONFIG_CLANG_INDIRECT_THUNK) += -mretpoline-external-thunk
+
 # If supported by the compiler, reduce stack alignment to 8 bytes. But allow
 # this to be overridden elsewhere.
 $(call cc-option-add,CFLAGS_stack_boundary,CC,-mpreferred-stack-boundary=3)
#5 lore raw

Re: [PATCH 2/3] x86/clang: add retpoline support

Quoted 9 lines · Jan Beulich
Quoted 5 lines · Roger Pau Monne

Detect whether the compiler supports clang retpoline option and enable by default if available, just like it's done for gcc.

Note clang already disables jump tables when retpoline is enabled, so there's no need to also pass the fno-jump-tables parameter.

That's one of the secondary gcc side arrangements. What about the other (-mindirect-branch-register)?

clang already passes the return address on a register always on amd64 so that's not needed either. I can add a comment like I've done for no-jump-tables.

Quoted 3 lines
Quoted 1 line

That's Andrew, not me?

Ops, sorry, I got that mixed up then.

Thanks, Roger.

Reviewed-by
#6 lore raw

Re: [PATCH 2/3] x86/clang: add retpoline support

Quoted 15 lines · Roger Pau Monné
Quoted 9 lines · Jan Beulich
Quoted 5 lines · Roger Pau Monne

Detect whether the compiler supports clang retpoline option and enable by default if available, just like it's done for gcc.

Note clang already disables jump tables when retpoline is enabled, so there's no need to also pass the fno-jump-tables parameter.

That's one of the secondary gcc side arrangements. What about the other (-mindirect-branch-register)?

clang already passes the return address on a register always on amd64 so that's not needed either. I can add a comment like I've done for no-jump-tables.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

That's Andrew, not me?

Ops, sorry, I got that mixed up then.

With the adjustments then

#7 lore raw

PATCH 3/3 x86/Kconfig: introduce option to select retpoline usage

In reply to Roger Pau Monne

Add a new Kconfig option under the "Speculative hardening" section that allows selecting whether to enable retpoline. This depends on the underlying compiler having retpoline support.

---
 xen/arch/x86/Kconfig |  4 ----
 xen/arch/x86/arch.mk |  2 ++
 xen/common/Kconfig   | 16 ++++++++++++++++
 3 files changed, 18 insertions(+), 4 deletions(-)
xen/arch/x86/Kconfig +0 -4
index fe00b4598b..7c73802adc 100644
@@ -38,10 +38,6 @@ config GCC_INDIRECT_THUNK
 config CLANG_INDIRECT_THUNK
 	def_bool $(cc-option,-mretpoline-external-thunk)
 
-config INDIRECT_THUNK
-	depends on GCC_INDIRECT_THUNK || CLANG_INDIRECT_THUNK
-	def_bool y
-
 config HAS_AS_CET_SS
 	# binutils >= 2.29 or LLVM >= 6
 	def_bool $(as-instr,wrssq %rax$(comma)0;setssbsy)
xen/arch/x86/arch.mk +2 -0
index f2aa2a515f..0597e714f9 100644
@@ -42,6 +42,7 @@ CFLAGS += -mno-red-zone -fpic
 # SSE setup for variadic function calls.
 CFLAGS += -mno-sse $(call cc-option,$(CC),-mskip-rax-setup)
 
+ifeq ($(CONFIG_INDIRECT_THUNK),y)
 # Compile with gcc thunk-extern, indirect-branch-register if available.
 CFLAGS-$(CONFIG_GCC_INDIRECT_THUNK) += -mindirect-branch=thunk-extern
 CFLAGS-$(CONFIG_GCC_INDIRECT_THUNK) += -mindirect-branch-register
@@ -49,6 +50,7 @@ CFLAGS-$(CONFIG_GCC_INDIRECT_THUNK) += -fno-jump-tables
 
 # Enable clang retpoline support if available.
 CFLAGS-$(CONFIG_CLANG_INDIRECT_THUNK) += -mretpoline-external-thunk
+endif
 
 # If supported by the compiler, reduce stack alignment to 8 bytes. But allow
 # this to be overridden elsewhere.
xen/common/Kconfig +16 -0
index db687b1785..3b0ba16a6a 100644
@@ -146,6 +146,22 @@ config SPECULATIVE_HARDEN_GUEST_ACCESS
 
 	  If unsure, say Y.
 
+config INDIRECT_THUNK
+	bool "Speculative Branch Target Injection Protection"
+	depends on X86 && (GCC_INDIRECT_THUNK || CLANG_INDIRECT_THUNK)
+	default y
+	help
+	  Contemporary processors may use speculative execution as a
+	  performance optimisation, but this can potentially be abused by an
+	  attacker to leak data via speculative sidechannels.
+
+	  One source of data leakage is via branch target injection.
+
+	  When enabled, indirect calls are implemented using a new construct
+	  called "retpoline" that prevents speculation.
+
+	  If unsure, say Y.
+
 endmenu
 
 config HYPFS
Signature · 1 line
2.34.1