Skip to contentSkip to conversation

ARM: breakpoint: CFI breakpoints only on demand

PATCH In review Linus Walleij 5 participants 5 messages Jul 3 – Jul 15

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 ARM: breakpoint: CFI breakpoints only on demand

This removes the stub hw_breakpoint_cfi_handler() from ARM, making it not steal breakpoint type 0x03 (ARM_ENTRY_CFI_BREAKPOINT) unless CFI is actively used in the kernel.

When not instrumenting with CFI, or when a breakpoint is issued in userspace, we fall through to return 1 from hw_breakpoint_pending() "unhandled fault" so userspace can make use of this breakpoint.

Tested with LKDTM and this command line:
echo CFI_FORWARD_PROTO > /sys/kernel/debug/provoke-crash/DIRECT still works as expected.

---
Trying to solve the CFI bug. Let's see of this first
approach is acceptable for the reporter.
---
Changes in v4:
- Dodge the BKPT if we are coming from userspace!
- Would be great if the reporter can test this with and without
CONFIG_CFI.
- Link to v3: https://patch.msgid.link/[email protected]

Changes in v3:
- Actually strip the RFC prefix...
- Link to v2: https://patch.msgid.link/[email protected]

Changes in v2:
- Resending as non-RFC so it can be applied as a band-aid.
- Link to v1: https://patch.msgid.link/[email protected]

To: Will Deacon <[email protected]>
To: Mark Rutland <[email protected]>
To: Russell King <[email protected]>
To: Kees Cook <[email protected]>
To: Sami Tolvanen <[email protected]>
To: "Russell King (Oracle)" <[email protected]>
To: Linus Walleij <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
 arch/arm/kernel/hw_breakpoint.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
arch/arm/kernel/hw_breakpoint.c +6 -5
index cd4b34c96e35..38feb30dfb5f 100644
@@ -929,10 +929,6 @@ static void hw_breakpoint_cfi_handler(struct pt_regs *regs)
 		break;
 	}
 }
-#else
-static void hw_breakpoint_cfi_handler(struct pt_regs *regs)
-{
-}
 #endif
 
 /*
@@ -964,9 +960,14 @@ static int hw_breakpoint_pending(unsigned long addr, unsigned int fsr,
 	case ARM_ENTRY_SYNC_WATCHPOINT:
 		watchpoint_handler(addr, fsr, regs);
 		break;
+#ifdef CONFIG_CFI
 	case ARM_ENTRY_CFI_BREAKPOINT:
-		hw_breakpoint_cfi_handler(regs);
+		if (user_mode(regs))
+			ret = 1; /* Don't handle userspace BKPT */
+		else
+			hw_breakpoint_cfi_handler(regs);
 		break;
+#endif
 	default:
 		ret = 1; /* Unhandled fault. */
 	}

---
base-commit: 8cd9520d35a6c38db6567e97dd93b1f11f185dc6
change-id: 20260626-arm32-cfi-bug-10fb960749c4

Best regards,

Signature · 1 line
Linus Walleij <[email protected]>
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] The CFI breakpoint handler incorrectly assumes the BKPT instruction is always 4 bytes long, which corrupts the instruction pointer in Thumb-2 mode. --

commit 8a3d2d7e6aa4575ee54af7475a100d8d6db4f18a
Author: Linus Walleij <[email protected]>

ARM: breakpoint: CFI breakpoints only on demand

This removes the stub hw_breakpoint_cfi_handler() from ARM, making it not steal breakpoint type 0x03 unless CFI is actively used in the kernel. It falls through to return unhandled fault for userspace breakpoints.

arch/arm/kernel/hw_breakpoint.c static void hw_breakpoint_cfi_handler(struct pt_regs *regs) · line 931

Quoted 8 lines
arch/arm/kernel/hw_breakpoint.c +0 -0
index cd4b34c96e35e..38feb30dfb5f8 100644
@@ -929,10 +929,6 @@ static void hw_breakpoint_cfi_handler(struct pt_regs *regs)
 		break;
 	}
 }

[Severity: High]
This is a pre-existing issue, but I noticed a potential problem in hw_breakpoint_cfi_handler() right above this diff.

When a CFI failure triggers a warning (such as when CONFIG_CFI_PERMISSIVE is enabled), the handler attempts to skip the breaking instruction:

arch/arm/kernel/hw_breakpoint.c:hw_breakpoint_cfi_handler() {
    ...
	case BUG_TRAP_TYPE_WARN:
		/* Skip the breaking instruction */
		instruction_pointer(regs) += 4;
		break;
    ...
}

Does this incorrectly assume the BKPT instruction is always 4 bytes long?

In Thumb-2 mode, BKPT is a 16-bit (2-byte) instruction. If the instruction pointer is advanced by 4 bytes, wouldn't it skip the intended instruction and an additional 2 bytes of the following instruction, potentially corrupting the execution stream and leading to an Oops?

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Linus Walleij
Quoted 85 lines · Linus Walleij

This removes the stub hw_breakpoint_cfi_handler() from ARM, making it not steal breakpoint type 0x03 (ARM_ENTRY_CFI_BREAKPOINT) unless CFI is actively used in the kernel.

When not instrumenting with CFI, or when a breakpoint is issued in userspace, we fall through to return 1 from hw_breakpoint_pending() "unhandled fault" so userspace can make use of this breakpoint.

Tested with LKDTM and this command line:
echo CFI_FORWARD_PROTO > /sys/kernel/debug/provoke-crash/DIRECT still works as expected.

---
Trying to solve the CFI bug. Let's see of this first
approach is acceptable for the reporter.
---
Changes in v4:
- Dodge the BKPT if we are coming from userspace!
- Would be great if the reporter can test this with and without
CONFIG_CFI.
- Link to v3: https://patch.msgid.link/[email protected]

Changes in v3:
- Actually strip the RFC prefix...
- Link to v2: https://patch.msgid.link/[email protected]

Changes in v2:
- Resending as non-RFC so it can be applied as a band-aid.
- Link to v1: https://patch.msgid.link/[email protected]

To: Will Deacon <[email protected]>
To: Mark Rutland <[email protected]>
To: Russell King <[email protected]>
To: Kees Cook <[email protected]>
To: Sami Tolvanen <[email protected]>
To: "Russell King (Oracle)" <[email protected]>
To: Linus Walleij <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
 arch/arm/kernel/hw_breakpoint.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
arch/arm/kernel/hw_breakpoint.c +6 -6
index cd4b34c96e35..38feb30dfb5f 100644
@@ -929,10 +929,6 @@ static void hw_breakpoint_cfi_handler(struct pt_regs *regs)
 		break;
 	}
 }
-#else
-static void hw_breakpoint_cfi_handler(struct pt_regs *regs)
-{
-}
 #endif
 
 /*
@@ -964,9 +960,14 @@ static int hw_breakpoint_pending(unsigned long addr, unsigned int fsr,
 	case ARM_ENTRY_SYNC_WATCHPOINT:
 		watchpoint_handler(addr, fsr, regs);
 		break;
+#ifdef CONFIG_CFI
 	case ARM_ENTRY_CFI_BREAKPOINT:
-		hw_breakpoint_cfi_handler(regs);
+		if (user_mode(regs))
+			ret = 1; /* Don't handle userspace BKPT */
+		else
+			hw_breakpoint_cfi_handler(regs);
 		break;
+#endif
 	default:
 		ret = 1; /* Unhandled fault. */
 	}
 
---

base-commit: 8cd9520d35a6c38db6567e97dd93b1f11f185dc6
change-id: 20260626-arm32-cfi-bug-10fb960749c4

Quoted 5 lines

Best regards,
--
Linus Walleij <[email protected]>

I tested the program experiencing the regression with this patch applied to v7.2-rc2. It works with and without CONFIG_CFI. Thank you everyone!

Reviewed-by Tested-by on v4 1/1
#3 lore raw
In reply to Linus Walleij
Quoted 12 lines · Linus Walleij

This removes the stub hw_breakpoint_cfi_handler() from ARM, making it not steal breakpoint type 0x03 (ARM_ENTRY_CFI_BREAKPOINT) unless CFI is actively used in the kernel.

When not instrumenting with CFI, or when a breakpoint is issued in userspace, we fall through to return 1 from hw_breakpoint_pending() "unhandled fault" so userspace can make use of this breakpoint.

Tested with LKDTM and this command line:
echo CFI_FORWARD_PROTO > /sys/kernel/debug/provoke-crash/DIRECT still works as expected.

Quoted 5 lines
Signature · 2 lines
Cheers,
Nathan
Reviewed-by on v4 1/1
#4 lore raw
In reply to Linus Walleij
Quoted 8 lines · Linus Walleij

This removes the stub hw_breakpoint_cfi_handler() from ARM, making it not steal breakpoint type 0x03 (ARM_ENTRY_CFI_BREAKPOINT) unless CFI is actively used in the kernel.

When not instrumenting with CFI, or when a breakpoint is issued in userspace, we fall through to return 1 from hw_breakpoint_pending() "unhandled fault" so userspace can make use of this breakpoint.

Quoted 5 lines

Tested with LKDTM and this command line:
echo CFI_FORWARD_PROTO > /sys/kernel/debug/provoke-crash/DIRECT still works as expected.

Fixes c3f89986fde7 ("ARM: 9391/2: hw_breakpoint: Handle CFI breakpoints")

BTW, I still would like to get the decoding implemented:
https://lore.kernel.org/lkml/[email protected]/

But I'll need to rework the above patch, as the breakpoint code in c3f89986fde7 seems to be better than the register_undef_hook() approach I used.

-Kees

Quoted 76 lines

---
Trying to solve the CFI bug. Let's see of this first
approach is acceptable for the reporter.
---
Changes in v4:
- Dodge the BKPT if we are coming from userspace!
- Would be great if the reporter can test this with and without
CONFIG_CFI.
- Link to v3: https://patch.msgid.link/[email protected]

Changes in v3:
- Actually strip the RFC prefix...
- Link to v2: https://patch.msgid.link/[email protected]

Changes in v2:
- Resending as non-RFC so it can be applied as a band-aid.
- Link to v1: https://patch.msgid.link/[email protected]

To: Will Deacon <[email protected]>
To: Mark Rutland <[email protected]>
To: Russell King <[email protected]>
To: Kees Cook <[email protected]>
To: Sami Tolvanen <[email protected]>
To: "Russell King (Oracle)" <[email protected]>
To: Linus Walleij <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
 arch/arm/kernel/hw_breakpoint.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
arch/arm/kernel/hw_breakpoint.c +6 -6
index cd4b34c96e35..38feb30dfb5f 100644
@@ -929,10 +929,6 @@ static void hw_breakpoint_cfi_handler(struct pt_regs *regs)
 		break;
 	}
 }
-#else
-static void hw_breakpoint_cfi_handler(struct pt_regs *regs)
-{
-}
 #endif
 
 /*
@@ -964,9 +960,14 @@ static int hw_breakpoint_pending(unsigned long addr, unsigned int fsr,
 	case ARM_ENTRY_SYNC_WATCHPOINT:
 		watchpoint_handler(addr, fsr, regs);
 		break;
+#ifdef CONFIG_CFI
 	case ARM_ENTRY_CFI_BREAKPOINT:
-		hw_breakpoint_cfi_handler(regs);
+		if (user_mode(regs))
+			ret = 1; /* Don't handle userspace BKPT */
+		else
+			hw_breakpoint_cfi_handler(regs);
 		break;
+#endif
 	default:
 		ret = 1; /* Unhandled fault. */
 	}
 
---

base-commit: 8cd9520d35a6c38db6567e97dd93b1f11f185dc6
change-id: 20260626-arm32-cfi-bug-10fb960749c4

Best regards,

Signature · 1 line
Linus Walleij <[email protected]>
Signature · 1 line
Kees Cook