diff mbox series

[v2] arm64: symbolize user-space stack on SIGSEGV

Message ID 20241101181325.2755536-1-pcc@google.com (mailing list archive)
State New
Headers show
Series [v2] arm64: symbolize user-space stack on SIGSEGV | expand

Commit Message

Peter Collingbourne Nov. 1, 2024, 6:13 p.m. UTC
On arm64, dump the userspace stack to the console when a SIGSEGV
occurs. Print filename+offset to allow symbolization.

This is a patch that I frequently need to apply locally in order to
debug segfaults that occur in environments where attaching a debugger
is infeasible.

Although I don't expect this patch to be applied to the mainline
kernel, I am posting it in case anyone else would find it useful
(and because I almost lost track of it recently, and I don't want to
have to write it again).

Signed-off-by: Peter Collingbourne <pcc@google.com>
---
v2:
- rebased to 6.12-rc5

 arch/arm64/mm/fault.c | 36 +++++++++++++++++++++++++++++++++++-
 1 file changed, 35 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c
index 8b281cf308b30..9391a0d44a99f 100644
--- a/arch/arm64/mm/fault.c
+++ b/arch/arm64/mm/fault.c
@@ -518,6 +518,38 @@  static bool is_write_abort(unsigned long esr)
 	return (esr & ESR_ELx_WNR) && !(esr & ESR_ELx_CM);
 }
 
+static void symbolize_addr(const char *prefix, unsigned long addr)
+{
+	struct vm_area_struct *vma = find_vma(current->mm, addr);
+	if (vma && vma->vm_file) {
+		char buf[1024];
+		char *c = d_path(&vma->vm_file->f_path, buf, 1024);
+		printk(KERN_ERR "%s = 0x%lx (%s + 0x%lx)\n", prefix, addr, c,
+		       addr - vma->vm_start + vma->vm_pgoff * PAGE_SIZE);
+	} else {
+		printk(KERN_ERR "%s = 0x%lx", prefix, addr);
+	}
+}
+
+static void symbolize_stack(void)
+{
+	int i;
+	unsigned long frame = current_pt_regs()->regs[29];
+
+	symbolize_addr("pc", current_pt_regs()->pc);
+	symbolize_addr("lr", current_pt_regs()->regs[30]);
+
+	for (i = 0; i != 64; ++i) {
+		unsigned long ret_addr;
+		unsafe_get_user(ret_addr, (unsigned long __user *)(frame + 8),
+				end);
+		symbolize_addr("ret_addr", ptrauth_strip_user_insn_pac(ret_addr));
+
+		unsafe_get_user(frame, (unsigned long __user *)frame, end);
+	}
+end:;
+}
+
 static int __kprobes do_page_fault(unsigned long far, unsigned long esr,
 				   struct pt_regs *regs)
 {
@@ -722,8 +754,10 @@  static int __kprobes do_page_fault(unsigned long far, unsigned long esr,
 		/* Something tried to access memory that out of memory map */
 		if (si_code == SEGV_PKUERR)
 			arm64_force_sig_fault_pkey(far, inf->name, pkey);
-		else
+		else {
+			symbolize_stack();
 			arm64_force_sig_fault(SIGSEGV, si_code, far, inf->name);
+		}
 	}
 
 	return 0;