Skip to contentSkip to conversation

Apply BP hardening for IRQs, PC alignment and Debug faults

PATCH New Will Deacon 3 participants 6 messages Feb 2, 2018 arm64

lore
Share & tools
lore
#0 lore raw

cover Apply BP hardening for IRQs, PC alignment and Debug faults

Hi Marc,

Please can you include these two in your BP hardening series? Dan pointed out that there are a few exception types that could come in before the instruction abort, so we need to make sure we catch them too.

Cheers,

Will

--->8

Will Deacon (2):
arm64: entry: Apply BP hardening for high-priority synchronous
exceptions
arm64: entry: Apply BP hardening for suspicious interrupts from EL0

 arch/arm64/kernel/entry.S | 10 +++++++++-
 arch/arm64/mm/fault.c     | 15 +++++++++++++++
 2 files changed, 24 insertions(+), 1 deletion(-)
Signature · 1 line
2.1.4
#1 lore raw

PATCH 1/2 arm64: entry: Apply BP hardening for high-priority synchronous exceptions

Software-step and PC alignment fault exceptions have higher priority than instruction abort exceptions, so apply the BP hardening hooks there too if the user PC appears to reside in kernel space.

---
 arch/arm64/kernel/entry.S | 5 ++++-
 arch/arm64/mm/fault.c     | 9 +++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)
arch/arm64/kernel/entry.S +4 -1
index fbe1444324b3..394b149825b6 100644
@@ -766,7 +766,10 @@ el0_sp_pc:
 	 * Stack or PC alignment exception handling
 	 */
 	mrs	x26, far_el1
-	enable_daif
+	enable_da_f
+#ifdef CONFIG_TRACE_IRQFLAGS
+	bl	trace_hardirqs_off
+#endif
 	ct_user_exit
 	mov	x0, x26
 	mov	x1, x25
arch/arm64/mm/fault.c +9 -0
index 0e671ddf4855..445aa4475c67 100644
@@ -732,6 +732,12 @@ asmlinkage void __exception do_sp_pc_abort(unsigned long addr,
 	struct siginfo info;
 	struct task_struct *tsk = current;
 
+	if (user_mode(regs)) {
+		if (instruction_pointer(regs) > TASK_SIZE)
+			arm64_apply_bp_hardening();
+		local_irq_enable();
+	}
+
 	if (show_unhandled_signals && unhandled_signal(tsk, SIGBUS))
 		pr_info_ratelimited("%s[%d]: %s exception: pc=%p sp=%p\n",
 				    tsk->comm, task_pid_nr(tsk),
@@ -791,6 +797,9 @@ asmlinkage int __exception do_debug_exception(unsigned long addr,
 	if (interrupts_enabled(regs))
 		trace_hardirqs_off();
 
+	if (user_mode(regs) && instruction_pointer(regs) > TASK_SIZE)
+		arm64_apply_bp_hardening();
+
 	if (!inf->fn(addr, esr, regs)) {
 		rv = 1;
 	} else {
Signature · 1 line
2.1.4
#2 lore raw

PATCH 1/2 arm64: entry: Apply BP hardening for high-priority synchronous exceptions

Quoted 6 lines · Will Deacon

Software-step and PC alignment fault exceptions have higher priority than instruction abort exceptions, so apply the BP hardening hooks there too if the user PC appears to reside in kernel space.

	M.
Signature · 1 line
Jazz is not dead. It just smells funny...
#3 lore raw

PATCH 2/2 arm64: entry: Apply BP hardening for suspicious interrupts from EL0

In reply to Will Deacon

It is possible to take an IRQ from EL0 following a branch to a kernel address in such a way that the IRQ is prioritised over the instruction abort. Whilst an attacker would need to get the starts to align here, it might be sufficient with enough calibration so perform BP hardening in the rare case that we see a kernel address in the ELR when handling an IRQ from EL0.

---
 arch/arm64/kernel/entry.S | 5 +++++
 arch/arm64/mm/fault.c     | 6 ++++++
 2 files changed, 11 insertions(+)
arch/arm64/kernel/entry.S +5 -0
index 394b149825b6..9bba6990e9b5 100644
@@ -827,6 +827,11 @@ el0_irq_naked:
 #endif
 
 	ct_user_exit
+#ifdef CONFIG_HARDEN_BRANCH_PREDICTOR
+	tbz	x22, #55, 1f
+	bl	do_el0_irq_bp_hardening
+1:
+#endif
 	irq_handler
 
 #ifdef CONFIG_TRACE_IRQFLAGS
arch/arm64/mm/fault.c +6 -0
index 445aa4475c67..3d2bcbfdb90b 100644
@@ -708,6 +708,12 @@ asmlinkage void __exception do_mem_abort(unsigned long addr, unsigned int esr,
 	arm64_notify_die("", regs, &info, esr);
 }
 
+asmlinkage void __exception do_el0_irq_bp_hardening(void)
+{
+	/* PC has already been checked in entry.S */
+	arm64_apply_bp_hardening();
+}
+
 asmlinkage void __exception do_el0_ia_bp_hardening(unsigned long addr,
 						   unsigned int esr,
 						   struct pt_regs *regs)
Signature · 1 line
2.1.4
#4 lore raw

PATCH 2/2 arm64: entry: Apply BP hardening for suspicious interrupts from EL0

Quoted 9 lines · Will Deacon

It is possible to take an IRQ from EL0 following a branch to a kernel address in such a way that the IRQ is prioritised over the instruction abort. Whilst an attacker would need to get the starts to align here, it might be sufficient with enough calibration so perform BP hardening in the rare case that we see a kernel address in the ELR when handling an IRQ from EL0.

	M.
Signature · 1 line
Jazz is not dead. It just smells funny...
#5 lore raw

PATCH 2/2 arm64: entry: Apply BP hardening for suspicious interrupts from EL0

In reply to Will Deacon
Quoted 3 lines · Will Deacon

It is possible to take an IRQ from EL0 following a branch to a kernel address in such a way that the IRQ is prioritised over the instruction abort. Whilst an attacker would need to get the starts to align here,

Nit: I'm guessing that should probably be "stars", but given the context of a theoretical race between a BP training sequence and an IRQ-generating sequence, there's a little doubt left ;)

Robin.

Quoted 45 lines

it might be sufficient with enough calibration so perform BP hardening in the rare case that we see a kernel address in the ELR when handling an IRQ from EL0.

---
  arch/arm64/kernel/entry.S | 5 +++++
  arch/arm64/mm/fault.c     | 6 ++++++
  2 files changed, 11 insertions(+)
arch/arm64/kernel/entry.S +5 -0
index 394b149825b6..9bba6990e9b5 100644
@@ -827,6 +827,11 @@ el0_irq_naked:
  #endif
 
  	ct_user_exit
+#ifdef CONFIG_HARDEN_BRANCH_PREDICTOR
+	tbz	x22, #55, 1f
+	bl	do_el0_irq_bp_hardening
+1:
+#endif
  	irq_handler
 
  #ifdef CONFIG_TRACE_IRQFLAGS
arch/arm64/mm/fault.c +6 -0
index 445aa4475c67..3d2bcbfdb90b 100644
@@ -708,6 +708,12 @@ asmlinkage void __exception do_mem_abort(unsigned long addr, unsigned int esr,
  	arm64_notify_die("", regs, &info, esr);
  }
 
+asmlinkage void __exception do_el0_irq_bp_hardening(void)
+{
+	/* PC has already been checked in entry.S */
+	arm64_apply_bp_hardening();
+}
+
  asmlinkage void __exception do_el0_ia_bp_hardening(unsigned long addr,
  						   unsigned int esr,
  						   struct pt_regs *regs)