unwind_frame.c 10 KB

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