Skip to contentSkip to conversation

arm64/bti: Disable in kernel BTI when cross section thunks are broken

PATCH Superseded Mark Brown 2 participants 3 messages Sep 2, 2022 – Sep 5, 2022 arm64

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Mark Brown

author
#0 lore raw

PATCH 1/1 arm64/bti: Disable in kernel BTI when cross section thunks are broken

GCC does not insert a bti c instruction at the beginning of a function when it believes that all callers reach the function through a direct branch[1]. Unfortunately the logic it uses to determine this is not sufficiently robust, for example not taking account of functions being placed in different sections which may be loaded separately, so we may still see thunks being generated to these functions. If that happens, the first instruction in the callee function will result in a Branch Target Exception due to the missing landing pad.

While this has currently only been observed in the case of modules having their main code loaded sufficiently far from their init section to require thunks it could potentially happen for other cases so the safest thing is to disable BTI for the kernel when building with an affected toolchain.

[1]: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106671

[Bits of the commit message are lifted from his report & workaround]

---

I'm sending this as an alternative to Scott's workaround in:

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

in case people aren't comfortable with that, given the GCC bug it seems likely that it is possible to generate some other case where there might be issues.

 arch/arm64/Kconfig | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm64/Kconfig +2 -2
index 9fb9fff08c94..863b807681ce 100644
@@ -1885,8 +1885,8 @@ config ARM64_BTI_KERNEL
 	depends on ARM64_BTI
 	depends on ARM64_PTR_AUTH_KERNEL
 	depends on CC_HAS_BRANCH_PROT_PAC_RET_BTI
-	# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94697
-	depends on !CC_IS_GCC || GCC_VERSION >= 100100
+	# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106671
+	depends on !CC_IS_GCC
 	# https://github.com/llvm/llvm-project/commit/a88c722e687e6780dcd6a58718350dc76fcc4cc9
 	depends on !CC_IS_CLANG || CLANG_VERSION >= 120000
 	depends on (!FUNCTION_GRAPH_TRACER || DYNAMIC_FTRACE_WITH_REGS)
Signature · 7 lines
2.30.2


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
maintainer
#1 lore raw
Quoted 22 lines · Mark Brown

GCC does not insert a bti c instruction at the beginning of a function when it believes that all callers reach the function through a direct branch[1]. Unfortunately the logic it uses to determine this is not sufficiently robust, for example not taking account of functions being placed in different sections which may be loaded separately, so we may still see thunks being generated to these functions. If that happens, the first instruction in the callee function will result in a Branch Target Exception due to the missing landing pad.

While this has currently only been observed in the case of modules having their main code loaded sufficiently far from their init section to require thunks it could potentially happen for other cases so the safest thing is to disable BTI for the kernel when building with an affected toolchain.

[1]: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106671

[Bits of the commit message are lifted from his report & workaround]

---

Quoted 7 lines

I'm sending this as an alternative to Scott's workaround in:

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

in case people aren't comfortable with that, given the GCC bug it seems likely that it is possible to generate some other case where there might be issues.

Thanks; I much prefer this simple fix for now, especially as the GCC bug doesn't seem to have concluded. Just one comment:

arch/arm64/Kconfig config ARM64_BTI_KERNEL · line 1889

Quoted 13 lines
 arch/arm64/Kconfig | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm64/Kconfig +0 -2
index 9fb9fff08c94..863b807681ce 100644
@@ -1885,8 +1885,8 @@ config ARM64_BTI_KERNEL
 	depends on ARM64_BTI
 	depends on ARM64_PTR_AUTH_KERNEL
 	depends on CC_HAS_BRANCH_PROT_PAC_RET_BTI
-	# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94697
-	depends on !CC_IS_GCC || GCC_VERSION >= 100100

Can we keep this dependency too, please? Hopefully we'll be able to add a 'GCC_VERSION >= nnnnnn' for this new issue, and then I think it's helpful to call out the issues separately so people don't think they can cherry-pick just one of the compiler fixes and it will work.

Will

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

author
#2 lore raw
Quoted 9 lines · Will Deacon

arch/arm64/Kconfig config ARM64_BTI_KERNEL · line 1889

Quoted 2 lines · Mark Brown
arch/arm64/Kconfig +0 -2
-	# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94697
-	depends on !CC_IS_GCC || GCC_VERSION >= 100100

Can we keep this dependency too, please? Hopefully we'll be able to add a 'GCC_VERSION >= nnnnnn' for this new issue, and then I think it's helpful to call out the issues separately so people don't think they can cherry-pick just one of the compiler fixes and it will work.

We can do, though note that there were some other issues before the one highlighted here so there'll still be some risk there.

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel