unwind_frame.c 11 KB

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