stacktrace.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Stack trace management functions
  4. *
  5. * Copyright IBM Corp. 2006
  6. */
  7. #include <linux/perf_event.h>
  8. #include <linux/stacktrace.h>
  9. #include <linux/uaccess.h>
  10. #include <linux/compat.h>
  11. #include <asm/stacktrace.h>
  12. #include <asm/unwind.h>
  13. #include <asm/kprobes.h>
  14. #include <asm/ptrace.h>
  15. void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie,
  16. struct task_struct *task, struct pt_regs *regs)
  17. {
  18. struct unwind_state state;
  19. unsigned long addr;
  20. unwind_for_each_frame(&state, task, regs, 0) {
  21. addr = unwind_get_return_address(&state);
  22. if (!addr || !consume_entry(cookie, addr))
  23. break;
  24. }
  25. }
  26. int arch_stack_walk_reliable(stack_trace_consume_fn consume_entry,
  27. void *cookie, struct task_struct *task)
  28. {
  29. struct unwind_state state;
  30. unsigned long addr;
  31. unwind_for_each_frame(&state, task, NULL, 0) {
  32. if (state.stack_info.type != STACK_TYPE_TASK)
  33. return -EINVAL;
  34. if (state.regs)
  35. return -EINVAL;
  36. addr = unwind_get_return_address(&state);
  37. if (!addr)
  38. return -EINVAL;
  39. #ifdef CONFIG_RETHOOK
  40. /*
  41. * Mark stacktraces with krethook functions on them
  42. * as unreliable.
  43. */
  44. if (state.ip == (unsigned long)arch_rethook_trampoline)
  45. return -EINVAL;
  46. #endif
  47. if (!consume_entry(cookie, addr))
  48. return -EINVAL;
  49. }
  50. /* Check for stack corruption */
  51. if (unwind_error(&state))
  52. return -EINVAL;
  53. return 0;
  54. }
  55. static inline bool store_ip(stack_trace_consume_fn consume_entry, void *cookie,
  56. struct perf_callchain_entry_ctx *entry, bool perf,
  57. unsigned long ip)
  58. {
  59. #ifdef CONFIG_PERF_EVENTS
  60. if (perf) {
  61. if (perf_callchain_store(entry, ip))
  62. return false;
  63. return true;
  64. }
  65. #endif
  66. return consume_entry(cookie, ip);
  67. }
  68. static inline bool ip_invalid(unsigned long ip)
  69. {
  70. /*
  71. * Perform some basic checks if an instruction address taken
  72. * from unreliable source is invalid.
  73. */
  74. if (ip & 1)
  75. return true;
  76. if (ip < mmap_min_addr)
  77. return true;
  78. if (ip >= current->mm->context.asce_limit)
  79. return true;
  80. return false;
  81. }
  82. static inline bool ip_within_vdso(unsigned long ip)
  83. {
  84. return in_range(ip, current->mm->context.vdso_base, vdso_text_size());
  85. }
  86. void arch_stack_walk_user_common(stack_trace_consume_fn consume_entry, void *cookie,
  87. struct perf_callchain_entry_ctx *entry,
  88. const struct pt_regs *regs, bool perf)
  89. {
  90. struct stack_frame_vdso_wrapper __user *sf_vdso;
  91. struct stack_frame_user __user *sf;
  92. unsigned long ip, sp;
  93. bool first = true;
  94. if (is_compat_task())
  95. return;
  96. if (!current->mm)
  97. return;
  98. ip = instruction_pointer(regs);
  99. if (!store_ip(consume_entry, cookie, entry, perf, ip))
  100. return;
  101. sf = (void __user *)user_stack_pointer(regs);
  102. pagefault_disable();
  103. while (1) {
  104. if (__get_user(sp, &sf->back_chain))
  105. break;
  106. /*
  107. * VDSO entry code has a non-standard stack frame layout.
  108. * See VDSO user wrapper code for details.
  109. */
  110. if (!sp && ip_within_vdso(ip)) {
  111. sf_vdso = (void __user *)sf;
  112. if (__get_user(ip, &sf_vdso->return_address))
  113. break;
  114. sp = (unsigned long)sf + STACK_FRAME_VDSO_OVERHEAD;
  115. sf = (void __user *)sp;
  116. if (__get_user(sp, &sf->back_chain))
  117. break;
  118. } else {
  119. sf = (void __user *)sp;
  120. if (__get_user(ip, &sf->gprs[8]))
  121. break;
  122. }
  123. /* Sanity check: ABI requires SP to be 8 byte aligned. */
  124. if (sp & 0x7)
  125. break;
  126. if (ip_invalid(ip)) {
  127. /*
  128. * If the instruction address is invalid, and this
  129. * is the first stack frame, assume r14 has not
  130. * been written to the stack yet. Otherwise exit.
  131. */
  132. if (!first)
  133. break;
  134. ip = regs->gprs[14];
  135. if (ip_invalid(ip))
  136. break;
  137. }
  138. if (!store_ip(consume_entry, cookie, entry, perf, ip))
  139. break;
  140. first = false;
  141. }
  142. pagefault_enable();
  143. }
  144. void arch_stack_walk_user(stack_trace_consume_fn consume_entry, void *cookie,
  145. const struct pt_regs *regs)
  146. {
  147. arch_stack_walk_user_common(consume_entry, cookie, NULL, regs, false);
  148. }