Skip to content

Commit c3f8998

Browse files
linuswRussell King (Oracle)
authored and
Russell King (Oracle)
committed
ARM: 9391/2: hw_breakpoint: Handle CFI breakpoints
This registers a breakpoint handler for the new breakpoint type (0x03) inserted by LLVM CLANG for CFI breakpoints. If we are in permissive mode, just print a backtrace and continue. Example with CONFIG_CFI_PERMISSIVE enabled: > echo CFI_FORWARD_PROTO > /sys/kernel/debug/provoke-crash/DIRECT lkdtm: Performing direct entry CFI_FORWARD_PROTO lkdtm: Calling matched prototype ... lkdtm: Calling mismatched prototype ... CFI failure at lkdtm_indirect_call+0x40/0x4c (target: 0x0; expected type: 0x00000000) WARNING: CPU: 1 PID: 112 at lkdtm_indirect_call+0x40/0x4c CPU: 1 PID: 112 Comm: sh Not tainted 6.8.0-rc1+ torvalds#150 Hardware name: ARM-Versatile Express (...) lkdtm: FAIL: survived mismatched prototype function call! lkdtm: Unexpected! This kernel (6.8.0-rc1+ armv7l) was built with CONFIG_CFI_CLANG=y As you can see the LKDTM test fails, but I expect that this would be expected behaviour in the permissive mode. We are currently not implementing target and type for the CFI breakpoint as this requires additional operand bundling compiler extensions. CPUs without breakpoint support cannot handle breakpoints naturally, in these cases the permissive mode will not work, CFI will fall over on an undefined instruction: Internal error: Oops - undefined instruction: 0 [#1] PREEMPT ARM CPU: 0 PID: 186 Comm: ash Tainted: G W 6.9.0-rc1+ torvalds#7 Hardware name: Gemini (Device Tree) PC is at lkdtm_indirect_call+0x38/0x4c LR is at lkdtm_CFI_FORWARD_PROTO+0x30/0x6c This is reasonable I think: it's the best CFI can do to ascertain the the control flow is not broken on these CPUs. Reviewed-by: Kees Cook <keescook@chromium.org> Tested-by: Kees Cook <keescook@chromium.org> Reviewed-by: Sami Tolvanen <samitolvanen@google.com> Signed-off-by: Linus Walleij <linus.walleij@linaro.org> Signed-off-by: Russell King (Oracle) <rmk+kernel@armlinux.org.uk>
1 parent 7339fb1 commit c3f8998

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

arch/arm/include/asm/hw_breakpoint.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ static inline void decode_ctrl_reg(u32 reg,
8484
#define ARM_DSCR_MOE(x) ((x >> 2) & 0xf)
8585
#define ARM_ENTRY_BREAKPOINT 0x1
8686
#define ARM_ENTRY_ASYNC_WATCHPOINT 0x2
87+
#define ARM_ENTRY_CFI_BREAKPOINT 0x3
8788
#define ARM_ENTRY_SYNC_WATCHPOINT 0xa
8889

8990
/* DSCR monitor/halting bits. */

arch/arm/kernel/hw_breakpoint.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <linux/perf_event.h>
1818
#include <linux/hw_breakpoint.h>
1919
#include <linux/smp.h>
20+
#include <linux/cfi.h>
2021
#include <linux/cpu_pm.h>
2122
#include <linux/coresight.h>
2223

@@ -903,6 +904,37 @@ static void breakpoint_handler(unsigned long unknown, struct pt_regs *regs)
903904
watchpoint_single_step_handler(addr);
904905
}
905906

907+
#ifdef CONFIG_CFI_CLANG
908+
static void hw_breakpoint_cfi_handler(struct pt_regs *regs)
909+
{
910+
/*
911+
* TODO: implementing target and type to pass to CFI using the more
912+
* elaborate report_cfi_failure() requires compiler work. To be able
913+
* to properly extract target information the compiler needs to
914+
* emit a stable instructions sequence for the CFI checks so we can
915+
* decode the instructions preceding the trap and figure out which
916+
* registers were used.
917+
*/
918+
919+
switch (report_cfi_failure_noaddr(regs, instruction_pointer(regs))) {
920+
case BUG_TRAP_TYPE_BUG:
921+
die("Oops - CFI", regs, 0);
922+
break;
923+
case BUG_TRAP_TYPE_WARN:
924+
/* Skip the breaking instruction */
925+
instruction_pointer(regs) += 4;
926+
break;
927+
default:
928+
die("Unknown CFI error", regs, 0);
929+
break;
930+
}
931+
}
932+
#else
933+
static void hw_breakpoint_cfi_handler(struct pt_regs *regs)
934+
{
935+
}
936+
#endif
937+
906938
/*
907939
* Called from either the Data Abort Handler [watchpoint] or the
908940
* Prefetch Abort Handler [breakpoint] with interrupts disabled.
@@ -932,6 +964,9 @@ static int hw_breakpoint_pending(unsigned long addr, unsigned int fsr,
932964
case ARM_ENTRY_SYNC_WATCHPOINT:
933965
watchpoint_handler(addr, fsr, regs);
934966
break;
967+
case ARM_ENTRY_CFI_BREAKPOINT:
968+
hw_breakpoint_cfi_handler(regs);
969+
break;
935970
default:
936971
ret = 1; /* Unhandled fault. */
937972
}

0 commit comments

Comments
 (0)