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