Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ARM: 9233/1: stacktrace: Skip frame pointer boundary check for call_with_stack()

When using the frame pointer unwinder, it was found that the stack trace
output of stack_trace_save() is incomplete if the stack contains
call_with_stack():

[0x7f00002c] dump_stack_task+0x2c/0x90 [hrtimer]
[0x7f0000a0] hrtimer_hander+0x10/0x18 [hrtimer]
[0x801a67f0] __hrtimer_run_queues+0x1b0/0x3b4
[0x801a7350] hrtimer_run_queues+0xc4/0xd8
[0x801a597c] update_process_times+0x3c/0x88
[0x801b5a98] tick_periodic+0x50/0xd8
[0x801b5bf4] tick_handle_periodic+0x24/0x84
[0x8010ffc4] twd_handler+0x38/0x48
[0x8017d220] handle_percpu_devid_irq+0xa8/0x244
[0x80176e9c] generic_handle_domain_irq+0x2c/0x3c
[0x8052e3a8] gic_handle_irq+0x7c/0x90
[0x808ab15c] generic_handle_arch_irq+0x60/0x80
[0x8051191c] call_with_stack+0x1c/0x20

For the frame pointer unwinder, unwind_frame() checks stackframe::fp by
stackframe::sp. Since call_with_stack() switches the SP from one stack
to another, stackframe::fp and stackframe: :sp will point to different
stacks, so we can no longer check stackframe::fp by stackframe::sp. Skip
checking stackframe::fp at this point to avoid this problem.

Signed-off-by: Li Huafei <lihuafei1@huawei.com>
Reviewed-by: Linus Waleij <linus.walleij@linaro.org>
Signed-off-by: Russell King (Oracle) <rmk+kernel@armlinux.org.uk>

authored by

Li Huafei and committed by
Russell King (Oracle)
5854e4d8 09cffeca

+35 -7
+33 -7
arch/arm/kernel/stacktrace.c
··· 9 9 #include <asm/stacktrace.h> 10 10 #include <asm/traps.h> 11 11 12 + #include "reboot.h" 13 + 12 14 #if defined(CONFIG_FRAME_POINTER) && !defined(CONFIG_ARM_UNWIND) 13 15 /* 14 16 * Unwind the current stack frame and store the new register values in the ··· 41 39 * Note that with framepointer enabled, even the leaf functions have the same 42 40 * prologue and epilogue, therefore we can ignore the LR value in this case. 43 41 */ 44 - int notrace unwind_frame(struct stackframe *frame) 42 + 43 + extern unsigned long call_with_stack_end; 44 + 45 + static int frame_pointer_check(struct stackframe *frame) 45 46 { 46 47 unsigned long high, low; 47 48 unsigned long fp = frame->fp; 49 + unsigned long pc = frame->pc; 50 + 51 + /* 52 + * call_with_stack() is the only place we allow SP to jump from one 53 + * stack to another, with FP and SP pointing to different stacks, 54 + * skipping the FP boundary check at this point. 55 + */ 56 + if (pc >= (unsigned long)&call_with_stack && 57 + pc < (unsigned long)&call_with_stack_end) 58 + return 0; 48 59 49 60 /* only go to a higher address on the stack */ 50 61 low = frame->sp; 51 62 high = ALIGN(low, THREAD_SIZE); 52 63 53 - #ifdef CONFIG_CC_IS_CLANG 54 64 /* check current frame pointer is within bounds */ 65 + #ifdef CONFIG_CC_IS_CLANG 55 66 if (fp < low + 4 || fp > high - 4) 56 67 return -EINVAL; 68 + #else 69 + if (fp < low + 12 || fp > high - 4) 70 + return -EINVAL; 71 + #endif 57 72 73 + return 0; 74 + } 75 + 76 + int notrace unwind_frame(struct stackframe *frame) 77 + { 78 + unsigned long fp = frame->fp; 79 + 80 + if (frame_pointer_check(frame)) 81 + return -EINVAL; 82 + 83 + /* restore the registers from the stack frame */ 84 + #ifdef CONFIG_CC_IS_CLANG 58 85 frame->sp = frame->fp; 59 86 frame->fp = READ_ONCE_NOCHECK(*(unsigned long *)(fp)); 60 87 frame->pc = READ_ONCE_NOCHECK(*(unsigned long *)(fp + 4)); 61 88 #else 62 - /* check current frame pointer is within bounds */ 63 - if (fp < low + 12 || fp > high - 4) 64 - return -EINVAL; 65 - 66 - /* restore the registers from the stack frame */ 67 89 frame->fp = READ_ONCE_NOCHECK(*(unsigned long *)(fp - 12)); 68 90 frame->sp = READ_ONCE_NOCHECK(*(unsigned long *)(fp - 8)); 69 91 frame->pc = READ_ONCE_NOCHECK(*(unsigned long *)(fp - 4));
+2
arch/arm/lib/call_with_stack.S
··· 46 46 pop {fpreg, pc} 47 47 UNWIND( .fnend ) 48 48 #endif 49 + .globl call_with_stack_end 50 + call_with_stack_end: 49 51 ENDPROC(call_with_stack)