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