The following BTI exception was seen when loading a livepatch module:
Internal error: Oops - BTI: 0000000036000001 [#1] SMP
pstate: 634004c9 (nZCv daIF +PAN -UAO +TCO +DIT -SSBS BTYPE=jc)
pc : kill_orphaned_pgrp+0x0/0x150
lr : do_exit+0x498/0xaf0 [livepatch_combined]
The problem is that the patch module's do_exit() is branching to a
static function in vmlinux using a module PLT veneer (indirect branch),
but the target function doesn't have a BTI landing pad.
Clang 21+ omits the landing pad for static functions which can only be
reached by a direct branch. That's fine for ordinary modules which only
branch to global exported functions. But livepatch modules use klp
relocations to reference arbitrary kernel symbols, and with
CONFIG_RANDOMIZE_MODULE_REGION_FULL the module is far enough from the
kernel that every R_AARCH64_CALL26 needs a PLT.
RET is exempt from BTI checking, so use it instead of BR when the target
has no landing pad, similar to what ftrace and BPF do.
This was found by testing with klp-build and Clang 21, but the issue is
not specific to klp-build. It's inherent to any livepatch module use of
klp relocations.
Previous tests with Clang 20 did not show this problem, as older Clang
unconditionally emits "bti c" for every C function.
Fixes: fd1e0fd71f65 ("arm64: Implement HAVE_LIVEPATCH")
Signed-off-by: Josh Poimboeuf <[email protected]>
---
arch/arm64/kernel/module-plts.c | 42 +++++++++++++++++++++++++++++----
1 file changed, 38 insertions(+), 4 deletions(-)
diff --git a/arch/arm64/kernel/module-plts.c b/arch/arm64/kernel/module-plts.c
index 7afd370da9f48..4249b477daa8b 100644
--- a/arch/arm64/kernel/module-plts.c
+++ b/arch/arm64/kernel/module-plts.c
@@ -10,6 +10,9 @@
#include <linux/moduleloader.h>
#include <linux/sort.h>
+#include <asm/cpufeature.h>
+#include <asm/text-patching.h>
+
static struct plt_entry __get_adrp_add_pair(u64 dst, u64 pc,
enum aarch64_insn_register reg)
{
@@ -23,14 +26,45 @@ static struct plt_entry __get_adrp_add_pair(u64 dst, u64 pc,
return (struct plt_entry){ cpu_to_le32(adrp), cpu_to_le32(add) };
}
+static bool plt_target_has_landing_pad(u64 dst)
+{
+ u32 insn;
+
+ if (!system_supports_bti_kernel())
+ return true;
+
+ if (aarch64_insn_read((void *)dst, &insn))
+ return false;
+
+ if (!aarch64_insn_is_hint(insn))
+ return false;
+
+ switch (insn & 0xFE0) {
+ case AARCH64_INSN_HINT_BTIC:
+ case AARCH64_INSN_HINT_BTIJ:
+ case AARCH64_INSN_HINT_BTIJC:
+ case AARCH64_INSN_HINT_PACIASP:
+ case AARCH64_INSN_HINT_PACIBSP:
+ return true;
+ }
+
+ return false;
+}
+
struct plt_entry get_plt_entry(u64 dst, void *pc)
{
+ enum aarch64_insn_branch_type type;
struct plt_entry plt;
- static u32 br;
+ u32 br;
- if (!br)
- br = aarch64_insn_gen_branch_reg(AARCH64_INSN_REG_16,
- AARCH64_INSN_BRANCH_NOLINK);
+ /*
+ * Livepatch modules can branch to static functions without landing
+ * pads, in which case RET is needed.
+ */
+ type = plt_target_has_landing_pad(dst) ? AARCH64_INSN_BRANCH_NOLINK
+ : AARCH64_INSN_BRANCH_RETURN;
+
+ br = aarch64_insn_gen_branch_reg(AARCH64_INSN_REG_16, type);
plt = __get_adrp_add_pair(dst, (u64)pc, AARCH64_INSN_REG_16);
plt.br = cpu_to_le32(br);
--
2.54.0