Skip to contentSkip to conversation

arm64: Always force a branch protection mode when the compiler has one

PATCH In review Mark Brown 4 participants 5 messages Mar 31, 2020 – Apr 1, 2020

Subsystems (2)
lore
Share & tools
lore

Mark Brown

author
#0 lore raw

PATCH 1/1 arm64: Always force a branch protection mode when the compiler has one

Compilers with branch protection support can be configured to enable it by default, it is likely that distributions will do this as part of deploying branch protection system wide. As well as the slight overhead from having some extra NOPs for unused branch protection features this can cause more serious problems when the kernel is providing pointer authentication to userspace but not built for pointer authentication itself. In that case our switching of keys for userspace can affect the kernel unexpectedly, causing pointer authentication instructions in the kernel to corrupt addresses.

To ensure that we get consistent and reliable behaviour always explicitly initialise the branch protection mode, ensuring that the kernel is built the same way regardless of the compiler defaults.

Fixes 7503197562567 (arm64: add basic pointer authentication support)
---
 arch/arm64/Kconfig  | 4 ++++
 arch/arm64/Makefile | 7 ++++++-
 2 files changed, 10 insertions(+), 1 deletion(-)
arch/arm64/Kconfig +4 -0
index d3efdc095a17..1e46746e8392 100644
@@ -1537,6 +1537,10 @@ config ARM64_PTR_AUTH
 	  This feature works with FUNCTION_GRAPH_TRACER option only if
 	  DYNAMIC_FTRACE_WITH_REGS is enabled.
 
+config CC_HAS_BRANCH_PROT_NONE
+	# GCC 9 or later, clang 8 or later
+	def_bool $(cc-option,-mbranch-protection=none)
+
 config CC_HAS_BRANCH_PROT_PAC_RET
 	# GCC 9 or later, clang 8 or later
 	def_bool $(cc-option,-mbranch-protection=pac-ret+leaf)
arch/arm64/Makefile +6 -1
index f15f92ba53e6..370fca6663c8 100644
@@ -65,6 +65,10 @@ stack_protector_prepare: prepare0
 					include/generated/asm-offsets.h))
 endif
 
+# Ensure that if the compiler supports branch protection we default it
+# off, this will be overridden if we are using branch protection.
+branch-prot-flags-$(CONFIG_CC_HAS_BRANCH_PROT_NONE) := -mbranch-protection=none
+
 ifeq ($(CONFIG_ARM64_PTR_AUTH),y)
 branch-prot-flags-$(CONFIG_CC_HAS_SIGN_RETURN_ADDRESS) := -msign-return-address=all
 branch-prot-flags-$(CONFIG_CC_HAS_BRANCH_PROT_PAC_RET) := -mbranch-protection=pac-ret+leaf
@@ -73,9 +77,10 @@ branch-prot-flags-$(CONFIG_CC_HAS_BRANCH_PROT_PAC_RET) := -mbranch-protection=pa
 # we pass it only to the assembler. This option is utilized only in case of non
 # integrated assemblers.
 branch-prot-flags-$(CONFIG_AS_HAS_PAC) += -Wa,-march=armv8.3-a
-KBUILD_CFLAGS += $(branch-prot-flags-y)
 endif
 
+KBUILD_CFLAGS += $(branch-prot-flags-y)
+
 ifeq ($(CONFIG_CPU_BIG_ENDIAN), y)
 KBUILD_CPPFLAGS	+= -mbig-endian
 CHECKFLAGS	+= -D__AARCH64EB__
Signature · 1 line
2.20.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Mark Brown

Compilers with branch protection support can be configured to enable it by default, it is likely that distributions will do this as part of deploying branch protection system wide. As well as the slight overhead from having some extra NOPs for unused branch protection features this can cause more serious problems when the kernel is providing pointer authentication to userspace but not built for pointer authentication itself. In that case our switching of keys for userspace can affect the kernel unexpectedly, causing pointer authentication instructions in the kernel to corrupt addresses.

Quoted 7 lines

To ensure that we get consistent and reliable behaviour always explicitly initialise the branch protection mode, ensuring that the kernel is built the same way regardless of the compiler defaults.

Fixes 7503197562567 (arm64: add basic pointer authentication support)

-Kees

Quoted 51 lines
---
 arch/arm64/Kconfig  | 4 ++++
 arch/arm64/Makefile | 7 ++++++-
 2 files changed, 10 insertions(+), 1 deletion(-)
arch/arm64/Kconfig +4 -0
index d3efdc095a17..1e46746e8392 100644
@@ -1537,6 +1537,10 @@ config ARM64_PTR_AUTH
 	  This feature works with FUNCTION_GRAPH_TRACER option only if
 	  DYNAMIC_FTRACE_WITH_REGS is enabled.
 
+config CC_HAS_BRANCH_PROT_NONE
+	# GCC 9 or later, clang 8 or later
+	def_bool $(cc-option,-mbranch-protection=none)
+
 config CC_HAS_BRANCH_PROT_PAC_RET
 	# GCC 9 or later, clang 8 or later
 	def_bool $(cc-option,-mbranch-protection=pac-ret+leaf)
arch/arm64/Makefile +6 -1
index f15f92ba53e6..370fca6663c8 100644
@@ -65,6 +65,10 @@ stack_protector_prepare: prepare0
 					include/generated/asm-offsets.h))
 endif
 
+# Ensure that if the compiler supports branch protection we default it
+# off, this will be overridden if we are using branch protection.
+branch-prot-flags-$(CONFIG_CC_HAS_BRANCH_PROT_NONE) := -mbranch-protection=none
+
 ifeq ($(CONFIG_ARM64_PTR_AUTH),y)
 branch-prot-flags-$(CONFIG_CC_HAS_SIGN_RETURN_ADDRESS) := -msign-return-address=all
 branch-prot-flags-$(CONFIG_CC_HAS_BRANCH_PROT_PAC_RET) := -mbranch-protection=pac-ret+leaf
@@ -73,9 +77,10 @@ branch-prot-flags-$(CONFIG_CC_HAS_BRANCH_PROT_PAC_RET) := -mbranch-protection=pa
 # we pass it only to the assembler. This option is utilized only in case of non
 # integrated assemblers.
 branch-prot-flags-$(CONFIG_AS_HAS_PAC) += -Wa,-march=armv8.3-a
-KBUILD_CFLAGS += $(branch-prot-flags-y)
 endif
 
+KBUILD_CFLAGS += $(branch-prot-flags-y)
+
 ifeq ($(CONFIG_CPU_BIG_ENDIAN), y)
 KBUILD_CPPFLAGS	+= -mbig-endian
 CHECKFLAGS	+= -D__AARCH64EB__
Signature · 1 line
2.20.1
Signature · 1 line
Kees Cook
reviewer Acked-by on v1 1/1
#2 lore raw
In reply to Mark Brown
Quoted 9 lines · Mark Brown

Compilers with branch protection support can be configured to enable it by default, it is likely that distributions will do this as part of deploying branch protection system wide. As well as the slight overhead from having some extra NOPs for unused branch protection features this can cause more serious problems when the kernel is providing pointer authentication to userspace but not built for pointer authentication itself. In that case our switching of keys for userspace can affect the kernel unexpectedly, causing pointer authentication instructions in the kernel to corrupt addresses.

Quoted 8 lines

To ensure that we get consistent and reliable behaviour always explicitly initialise the branch protection mode, ensuring that the kernel is built the same way regardless of the compiler defaults.

Fixes 7503197562567 (arm64: add basic pointer authentication support)

Mark.

Quoted 50 lines
---
 arch/arm64/Kconfig  | 4 ++++
 arch/arm64/Makefile | 7 ++++++-
 2 files changed, 10 insertions(+), 1 deletion(-)
arch/arm64/Kconfig +4 -0
index d3efdc095a17..1e46746e8392 100644
@@ -1537,6 +1537,10 @@ config ARM64_PTR_AUTH
 	  This feature works with FUNCTION_GRAPH_TRACER option only if
 	  DYNAMIC_FTRACE_WITH_REGS is enabled.
 
+config CC_HAS_BRANCH_PROT_NONE
+	# GCC 9 or later, clang 8 or later
+	def_bool $(cc-option,-mbranch-protection=none)
+
 config CC_HAS_BRANCH_PROT_PAC_RET
 	# GCC 9 or later, clang 8 or later
 	def_bool $(cc-option,-mbranch-protection=pac-ret+leaf)
arch/arm64/Makefile +6 -1
index f15f92ba53e6..370fca6663c8 100644
@@ -65,6 +65,10 @@ stack_protector_prepare: prepare0
 					include/generated/asm-offsets.h))
 endif
 
+# Ensure that if the compiler supports branch protection we default it
+# off, this will be overridden if we are using branch protection.
+branch-prot-flags-$(CONFIG_CC_HAS_BRANCH_PROT_NONE) := -mbranch-protection=none
+
 ifeq ($(CONFIG_ARM64_PTR_AUTH),y)
 branch-prot-flags-$(CONFIG_CC_HAS_SIGN_RETURN_ADDRESS) := -msign-return-address=all
 branch-prot-flags-$(CONFIG_CC_HAS_BRANCH_PROT_PAC_RET) := -mbranch-protection=pac-ret+leaf
@@ -73,9 +77,10 @@ branch-prot-flags-$(CONFIG_CC_HAS_BRANCH_PROT_PAC_RET) := -mbranch-protection=pa
 # we pass it only to the assembler. This option is utilized only in case of non
 # integrated assemblers.
 branch-prot-flags-$(CONFIG_AS_HAS_PAC) += -Wa,-march=armv8.3-a
-KBUILD_CFLAGS += $(branch-prot-flags-y)
 endif
 
+KBUILD_CFLAGS += $(branch-prot-flags-y)
+
 ifeq ($(CONFIG_CPU_BIG_ENDIAN), y)
 KBUILD_CPPFLAGS	+= -mbig-endian
 CHECKFLAGS	+= -D__AARCH64EB__
Signature · 1 line
2.20.1
maintainer
#3 lore raw
In reply to Mark Brown
Quoted 6 lines · Mark Brown

Compilers with branch protection support can be configured to enable it by default, it is likely that distributions will do this as part of deploying branch protection system wide. As well as the slight overhead from having some extra NOPs for unused branch protection features this can cause more serious problems when the kernel is providing pointer authentication to userspace but not built for pointer authentication itself.

With 5.7 you won't be able to configure user and kernel PAC support independently. So, I guess that's something only for prior kernel versions.

Quoted 22 lines

In that case our
switching of keys for userspace can affect the kernel unexpectedly, causing pointer authentication instructions in the kernel to corrupt addresses.

To ensure that we get consistent and reliable behaviour always explicitly initialise the branch protection mode, ensuring that the kernel is built the same way regardless of the compiler defaults.

Fixes 7503197562567 (arm64: add basic pointer authentication support)
---
 arch/arm64/Kconfig  | 4 ++++
 arch/arm64/Makefile | 7 ++++++-
 2 files changed, 10 insertions(+), 1 deletion(-)
arch/arm64/Kconfig +0 -0
index d3efdc095a17..1e46746e8392 100644

arch/arm64/Kconfig config ARM64_PTR_AUTH · line 1542

Quoted 6 lines
arch/arm64/Kconfig +3 -0
@@ -1537,6 +1537,10 @@ config ARM64_PTR_AUTH
 	  This feature works with FUNCTION_GRAPH_TRACER option only if
 	  DYNAMIC_FTRACE_WITH_REGS is enabled.
 
+config CC_HAS_BRANCH_PROT_NONE
+	# GCC 9 or later, clang 8 or later
+	def_bool $(cc-option,-mbranch-protection=none)

I don't think we need to bother with a Kconfig entry here. We did it for the other options since CONFIG_ARM64_PTR_AUTH has a dependency on them.

Quoted 9 lines
arch/arm64/Kconfig +1 -0
+
 config CC_HAS_BRANCH_PROT_PAC_RET
 	# GCC 9 or later, clang 8 or later
 	def_bool $(cc-option,-mbranch-protection=pac-ret+leaf)
arch/arm64/Makefile +0 -0
index f15f92ba53e6..370fca6663c8 100644

arch/arm64/Makefile stack_protector_prepare: prepare0 · line 70

Quoted 6 lines
arch/arm64/Makefile +3 -0
@@ -65,6 +65,10 @@ stack_protector_prepare: prepare0
 					include/generated/asm-offsets.h))
 endif
 
+# Ensure that if the compiler supports branch protection we default it
+# off, this will be overridden if we are using branch protection.
+branch-prot-flags-$(CONFIG_CC_HAS_BRANCH_PROT_NONE) := -mbranch-protection=none

And a $(call cc-option,-mbranch-protection=none) here.

branch-prot-flags-y is only introduced in 5.7, so backporting may look slightly weirder.

arch/arm64/Makefile branch-prot-flags-$(CONFIG_CC_HAS_BRANCH_PROT_PAC_RET) := -mbranch-protection=pa · line 82

Quoted 12 lines
arch/arm64/Makefile +2 -1
+
 ifeq ($(CONFIG_ARM64_PTR_AUTH),y)
 branch-prot-flags-$(CONFIG_CC_HAS_SIGN_RETURN_ADDRESS) := -msign-return-address=all
 branch-prot-flags-$(CONFIG_CC_HAS_BRANCH_PROT_PAC_RET) := -mbranch-protection=pac-ret+leaf
@@ -73,9 +77,10 @@ branch-prot-flags-$(CONFIG_CC_HAS_BRANCH_PROT_PAC_RET) := -mbranch-protection=pa
 # we pass it only to the assembler. This option is utilized only in case of non
 # integrated assemblers.
 branch-prot-flags-$(CONFIG_AS_HAS_PAC) += -Wa,-march=armv8.3-a
-KBUILD_CFLAGS += $(branch-prot-flags-y)
 endif
 
+KBUILD_CFLAGS += $(branch-prot-flags-y)

Or just use an else clause here with:

KBUILD_CFLAGS += ($call cc-option,-mbranch-protection=none).

On backports, we just drop else/endif since they don't exist.

Not a strong preference really, just looking to have backports resemble upstream better. I can fix it up locally, whichever variant we go for (or even this one).

Signature · 1 line
Catalin
author
#4 lore raw
Quoted 11 lines · Catalin Marinas
Quoted 6 lines · Mark Brown

Compilers with branch protection support can be configured to enable it by default, it is likely that distributions will do this as part of deploying branch protection system wide. As well as the slight overhead from having some extra NOPs for unused branch protection features this can cause more serious problems when the kernel is providing pointer authentication to userspace but not built for pointer authentication itself.

With 5.7 you won't be able to configure user and kernel PAC support independently. So, I guess that's something only for prior kernel versions.

Yes, it's really for the benefit of stable at this point - hence the Cc. Going forward it's hopefully more for defensiveness than anything else, it's possible something similar might come up with some future stuff but ideally not.