unwind_frame.c 8.6 KB

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