unwind_frame.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #include <linux/sched.h>
  2. #include <asm/ptrace.h>
  3. #include <asm/bitops.h>
  4. #include <asm/stacktrace.h>
  5. #include <asm/unwind.h>
  6. #define FRAME_HEADER_SIZE (sizeof(long) * 2)
  7. /*
  8. * This disables KASAN checking when reading a value from another task's stack,
  9. * since the other task could be running on another CPU and could have poisoned
  10. * the stack in the meantime.
  11. */
  12. #define READ_ONCE_TASK_STACK(task, x) \
  13. ({ \
  14. unsigned long val; \
  15. if (task == current) \
  16. val = READ_ONCE(x); \
  17. else \
  18. val = READ_ONCE_NOCHECK(x); \
  19. val; \
  20. })
  21. static void unwind_dump(struct unwind_state *state, unsigned long *sp)
  22. {
  23. static bool dumped_before = false;
  24. bool prev_zero, zero = false;
  25. unsigned long word;
  26. if (dumped_before)
  27. return;
  28. dumped_before = true;
  29. printk_deferred("unwind stack type:%d next_sp:%p mask:%lx graph_idx:%d\n",
  30. state->stack_info.type, state->stack_info.next_sp,
  31. state->stack_mask, state->graph_idx);
  32. for (sp = state->orig_sp; sp < state->stack_info.end; sp++) {
  33. word = READ_ONCE_NOCHECK(*sp);
  34. prev_zero = zero;
  35. zero = word == 0;
  36. if (zero) {
  37. if (!prev_zero)
  38. printk_deferred("%p: %016x ...\n", sp, 0);
  39. continue;
  40. }
  41. printk_deferred("%p: %016lx (%pB)\n", sp, word, (void *)word);
  42. }
  43. }
  44. unsigned long unwind_get_return_address(struct unwind_state *state)
  45. {
  46. unsigned long addr;
  47. unsigned long *addr_p = unwind_get_return_address_ptr(state);
  48. if (unwind_done(state))
  49. return 0;
  50. if (state->regs && user_mode(state->regs))
  51. return 0;
  52. addr = READ_ONCE_TASK_STACK(state->task, *addr_p);
  53. addr = ftrace_graph_ret_addr(state->task, &state->graph_idx, addr,
  54. addr_p);
  55. return __kernel_text_address(addr) ? addr : 0;
  56. }
  57. EXPORT_SYMBOL_GPL(unwind_get_return_address);
  58. static size_t regs_size(struct pt_regs *regs)
  59. {
  60. /* x86_32 regs from kernel mode are two words shorter: */
  61. if (IS_ENABLED(CONFIG_X86_32) && !user_mode(regs))
  62. return sizeof(*regs) - 2*sizeof(long);
  63. return sizeof(*regs);
  64. }
  65. static bool is_last_task_frame(struct unwind_state *state)
  66. {
  67. unsigned long bp = (unsigned long)state->bp;
  68. unsigned long regs = (unsigned long)task_pt_regs(state->task);
  69. /*
  70. * We have to check for the last task frame at two different locations
  71. * because gcc can occasionally decide to realign the stack pointer and
  72. * change the offset of the stack frame by a word in the prologue of a
  73. * function called by head/entry code.
  74. */
  75. return bp == regs - FRAME_HEADER_SIZE ||
  76. bp == regs - FRAME_HEADER_SIZE - sizeof(long);
  77. }
  78. /*
  79. * This determines if the frame pointer actually contains an encoded pointer to
  80. * pt_regs on the stack. See ENCODE_FRAME_POINTER.
  81. */
  82. static struct pt_regs *decode_frame_pointer(unsigned long *bp)
  83. {
  84. unsigned long regs = (unsigned long)bp;
  85. if (!(regs & 0x1))
  86. return NULL;
  87. return (struct pt_regs *)(regs & ~0x1);
  88. }
  89. static bool update_stack_state(struct unwind_state *state, void *addr,
  90. size_t len)
  91. {
  92. struct stack_info *info = &state->stack_info;
  93. enum stack_type orig_type = info->type;
  94. /*
  95. * If addr isn't on the current stack, switch to the next one.
  96. *
  97. * We may have to traverse multiple stacks to deal with the possibility
  98. * that 'info->next_sp' could point to an empty stack and 'addr' could
  99. * be on a subsequent stack.
  100. */
  101. while (!on_stack(info, addr, len))
  102. if (get_stack_info(info->next_sp, state->task, info,
  103. &state->stack_mask))
  104. return false;
  105. if (!state->orig_sp || info->type != orig_type)
  106. state->orig_sp = addr;
  107. return true;
  108. }
  109. bool unwind_next_frame(struct unwind_state *state)
  110. {
  111. struct pt_regs *regs;
  112. unsigned long *next_bp, *next_frame;
  113. size_t next_len;
  114. enum stack_type prev_type = state->stack_info.type;
  115. if (unwind_done(state))
  116. return false;
  117. /* have we reached the end? */
  118. if (state->regs && user_mode(state->regs))
  119. goto the_end;
  120. if (is_last_task_frame(state)) {
  121. regs = task_pt_regs(state->task);
  122. /*
  123. * kthreads (other than the boot CPU's idle thread) have some
  124. * partial regs at the end of their stack which were placed
  125. * there by copy_thread_tls(). But the regs don't have any
  126. * useful information, so we can skip them.
  127. *
  128. * This user_mode() check is slightly broader than a PF_KTHREAD
  129. * check because it also catches the awkward situation where a
  130. * newly forked kthread transitions into a user task by calling
  131. * do_execve(), which eventually clears PF_KTHREAD.
  132. */
  133. if (!user_mode(regs))
  134. goto the_end;
  135. /*
  136. * We're almost at the end, but not quite: there's still the
  137. * syscall regs frame. Entry code doesn't encode the regs
  138. * pointer for syscalls, so we have to set it manually.
  139. */
  140. state->regs = regs;
  141. state->bp = NULL;
  142. return true;
  143. }
  144. /* get the next frame pointer */
  145. if (state->regs)
  146. next_bp = (unsigned long *)state->regs->bp;
  147. else
  148. next_bp = (unsigned long *)READ_ONCE_TASK_STACK(state->task,*state->bp);
  149. /* is the next frame pointer an encoded pointer to pt_regs? */
  150. regs = decode_frame_pointer(next_bp);
  151. if (regs) {
  152. next_frame = (unsigned long *)regs;
  153. next_len = sizeof(*regs);
  154. } else {
  155. next_frame = next_bp;
  156. next_len = FRAME_HEADER_SIZE;
  157. }
  158. /* make sure the next frame's data is accessible */
  159. if (!update_stack_state(state, next_frame, next_len)) {
  160. /*
  161. * Don't warn on bad regs->bp. An interrupt in entry code
  162. * might cause a false positive warning.
  163. */
  164. if (state->regs)
  165. goto the_end;
  166. goto bad_address;
  167. }
  168. /* Make sure it only unwinds up and doesn't overlap the last frame: */
  169. if (state->stack_info.type == prev_type) {
  170. if (state->regs && (void *)next_frame < (void *)state->regs + regs_size(state->regs))
  171. goto bad_address;
  172. if (state->bp && (void *)next_frame < (void *)state->bp + FRAME_HEADER_SIZE)
  173. goto bad_address;
  174. }
  175. /* move to the next frame */
  176. if (regs) {
  177. state->regs = regs;
  178. state->bp = NULL;
  179. } else {
  180. state->bp = next_bp;
  181. state->regs = NULL;
  182. }
  183. return true;
  184. bad_address:
  185. /*
  186. * When unwinding a non-current task, the task might actually be
  187. * running on another CPU, in which case it could be modifying its
  188. * stack while we're reading it. This is generally not a problem and
  189. * can be ignored as long as the caller understands that unwinding
  190. * another task will not always succeed.
  191. */
  192. if (state->task != current)
  193. goto the_end;
  194. if (state->regs) {
  195. printk_deferred_once(KERN_WARNING
  196. "WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n",
  197. state->regs, state->task->comm,
  198. state->task->pid, next_frame);
  199. unwind_dump(state, (unsigned long *)state->regs);
  200. } else {
  201. printk_deferred_once(KERN_WARNING
  202. "WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n",
  203. state->bp, state->task->comm,
  204. state->task->pid, next_frame);
  205. unwind_dump(state, state->bp);
  206. }
  207. the_end:
  208. state->stack_info.type = STACK_TYPE_UNKNOWN;
  209. return false;
  210. }
  211. EXPORT_SYMBOL_GPL(unwind_next_frame);
  212. void __unwind_start(struct unwind_state *state, struct task_struct *task,
  213. struct pt_regs *regs, unsigned long *first_frame)
  214. {
  215. unsigned long *bp, *frame;
  216. size_t len;
  217. memset(state, 0, sizeof(*state));
  218. state->task = task;
  219. /* don't even attempt to start from user mode regs */
  220. if (regs && user_mode(regs)) {
  221. state->stack_info.type = STACK_TYPE_UNKNOWN;
  222. return;
  223. }
  224. /* set up the starting stack frame */
  225. bp = get_frame_pointer(task, regs);
  226. regs = decode_frame_pointer(bp);
  227. if (regs) {
  228. state->regs = regs;
  229. frame = (unsigned long *)regs;
  230. len = sizeof(*regs);
  231. } else {
  232. state->bp = bp;
  233. frame = bp;
  234. len = FRAME_HEADER_SIZE;
  235. }
  236. /* initialize stack info and make sure the frame data is accessible */
  237. get_stack_info(frame, state->task, &state->stack_info,
  238. &state->stack_mask);
  239. update_stack_state(state, frame, len);
  240. /*
  241. * The caller can provide the address of the first frame directly
  242. * (first_frame) or indirectly (regs->sp) to indicate which stack frame
  243. * to start unwinding at. Skip ahead until we reach it.
  244. */
  245. while (!unwind_done(state) &&
  246. (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
  247. state->bp < first_frame))
  248. unwind_next_frame(state);
  249. }
  250. EXPORT_SYMBOL_GPL(__unwind_start);