.. | .. |
---|
29 | 29 | |
---|
30 | 30 | static struct pt_regs exec_summary_regs; |
---|
31 | 31 | |
---|
32 | | -bool in_task_stack(unsigned long *stack, struct task_struct *task, |
---|
33 | | - struct stack_info *info) |
---|
| 32 | +bool noinstr in_task_stack(unsigned long *stack, struct task_struct *task, |
---|
| 33 | + struct stack_info *info) |
---|
34 | 34 | { |
---|
35 | 35 | unsigned long *begin = task_stack_page(task); |
---|
36 | 36 | unsigned long *end = task_stack_page(task) + THREAD_SIZE; |
---|
.. | .. |
---|
46 | 46 | return true; |
---|
47 | 47 | } |
---|
48 | 48 | |
---|
49 | | -bool in_entry_stack(unsigned long *stack, struct stack_info *info) |
---|
| 49 | +/* Called from get_stack_info_noinstr - so must be noinstr too */ |
---|
| 50 | +bool noinstr in_entry_stack(unsigned long *stack, struct stack_info *info) |
---|
50 | 51 | { |
---|
51 | 52 | struct entry_stack *ss = cpu_entry_stack(smp_processor_id()); |
---|
52 | 53 | |
---|
.. | .. |
---|
65 | 66 | } |
---|
66 | 67 | |
---|
67 | 68 | static void printk_stack_address(unsigned long address, int reliable, |
---|
68 | | - char *log_lvl) |
---|
| 69 | + const char *log_lvl) |
---|
69 | 70 | { |
---|
70 | 71 | touch_nmi_watchdog(); |
---|
71 | 72 | printk("%s %s%pB\n", log_lvl, reliable ? "" : "? ", (void *)address); |
---|
| 73 | +} |
---|
| 74 | + |
---|
| 75 | +static int copy_code(struct pt_regs *regs, u8 *buf, unsigned long src, |
---|
| 76 | + unsigned int nbytes) |
---|
| 77 | +{ |
---|
| 78 | + if (!user_mode(regs)) |
---|
| 79 | + return copy_from_kernel_nofault(buf, (u8 *)src, nbytes); |
---|
| 80 | + |
---|
| 81 | + /* The user space code from other tasks cannot be accessed. */ |
---|
| 82 | + if (regs != task_pt_regs(current)) |
---|
| 83 | + return -EPERM; |
---|
| 84 | + /* |
---|
| 85 | + * Make sure userspace isn't trying to trick us into dumping kernel |
---|
| 86 | + * memory by pointing the userspace instruction pointer at it. |
---|
| 87 | + */ |
---|
| 88 | + if (__chk_range_not_ok(src, nbytes, TASK_SIZE_MAX)) |
---|
| 89 | + return -EINVAL; |
---|
| 90 | + |
---|
| 91 | + /* |
---|
| 92 | + * Even if named copy_from_user_nmi() this can be invoked from |
---|
| 93 | + * other contexts and will not try to resolve a pagefault, which is |
---|
| 94 | + * the correct thing to do here as this code can be called from any |
---|
| 95 | + * context. |
---|
| 96 | + */ |
---|
| 97 | + return copy_from_user_nmi(buf, (void __user *)src, nbytes); |
---|
72 | 98 | } |
---|
73 | 99 | |
---|
74 | 100 | /* |
---|
.. | .. |
---|
97 | 123 | #define OPCODE_BUFSIZE (PROLOGUE_SIZE + 1 + EPILOGUE_SIZE) |
---|
98 | 124 | u8 opcodes[OPCODE_BUFSIZE]; |
---|
99 | 125 | unsigned long prologue = regs->ip - PROLOGUE_SIZE; |
---|
100 | | - bool bad_ip; |
---|
101 | 126 | |
---|
102 | | - /* |
---|
103 | | - * Make sure userspace isn't trying to trick us into dumping kernel |
---|
104 | | - * memory by pointing the userspace instruction pointer at it. |
---|
105 | | - */ |
---|
106 | | - bad_ip = user_mode(regs) && |
---|
107 | | - __chk_range_not_ok(prologue, OPCODE_BUFSIZE, TASK_SIZE_MAX); |
---|
108 | | - |
---|
109 | | - if (bad_ip || probe_kernel_read(opcodes, (u8 *)prologue, |
---|
110 | | - OPCODE_BUFSIZE)) { |
---|
111 | | - printk("%sCode: Bad RIP value.\n", loglvl); |
---|
112 | | - } else { |
---|
| 127 | + switch (copy_code(regs, opcodes, prologue, sizeof(opcodes))) { |
---|
| 128 | + case 0: |
---|
113 | 129 | printk("%sCode: %" __stringify(PROLOGUE_SIZE) "ph <%02x> %" |
---|
114 | 130 | __stringify(EPILOGUE_SIZE) "ph\n", loglvl, opcodes, |
---|
115 | 131 | opcodes[PROLOGUE_SIZE], opcodes + PROLOGUE_SIZE + 1); |
---|
| 132 | + break; |
---|
| 133 | + case -EPERM: |
---|
| 134 | + /* No access to the user space stack of other tasks. Ignore. */ |
---|
| 135 | + break; |
---|
| 136 | + default: |
---|
| 137 | + printk("%sCode: Unable to access opcode bytes at RIP 0x%lx.\n", |
---|
| 138 | + loglvl, prologue); |
---|
| 139 | + break; |
---|
116 | 140 | } |
---|
117 | 141 | } |
---|
118 | 142 | |
---|
.. | .. |
---|
126 | 150 | show_opcodes(regs, loglvl); |
---|
127 | 151 | } |
---|
128 | 152 | |
---|
129 | | -void show_iret_regs(struct pt_regs *regs) |
---|
| 153 | +void show_iret_regs(struct pt_regs *regs, const char *log_lvl) |
---|
130 | 154 | { |
---|
131 | | - show_ip(regs, KERN_DEFAULT); |
---|
132 | | - printk(KERN_DEFAULT "RSP: %04x:%016lx EFLAGS: %08lx", (int)regs->ss, |
---|
| 155 | + show_ip(regs, log_lvl); |
---|
| 156 | + printk("%sRSP: %04x:%016lx EFLAGS: %08lx", log_lvl, (int)regs->ss, |
---|
133 | 157 | regs->sp, regs->flags); |
---|
134 | 158 | } |
---|
135 | 159 | |
---|
136 | 160 | static void show_regs_if_on_stack(struct stack_info *info, struct pt_regs *regs, |
---|
137 | | - bool partial) |
---|
| 161 | + bool partial, const char *log_lvl) |
---|
138 | 162 | { |
---|
139 | 163 | /* |
---|
140 | 164 | * These on_stack() checks aren't strictly necessary: the unwind code |
---|
.. | .. |
---|
146 | 170 | * they can be printed in the right context. |
---|
147 | 171 | */ |
---|
148 | 172 | if (!partial && on_stack(info, regs, sizeof(*regs))) { |
---|
149 | | - __show_regs(regs, SHOW_REGS_SHORT); |
---|
| 173 | + __show_regs(regs, SHOW_REGS_SHORT, log_lvl); |
---|
150 | 174 | |
---|
151 | 175 | } else if (partial && on_stack(info, (void *)regs + IRET_FRAME_OFFSET, |
---|
152 | 176 | IRET_FRAME_SIZE)) { |
---|
.. | .. |
---|
155 | 179 | * full pt_regs might not have been saved yet. In that case |
---|
156 | 180 | * just print the iret frame. |
---|
157 | 181 | */ |
---|
158 | | - show_iret_regs(regs); |
---|
| 182 | + show_iret_regs(regs, log_lvl); |
---|
159 | 183 | } |
---|
160 | 184 | } |
---|
161 | 185 | |
---|
162 | 186 | void show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs, |
---|
163 | | - unsigned long *stack, char *log_lvl) |
---|
| 187 | + unsigned long *stack, const char *log_lvl) |
---|
164 | 188 | { |
---|
165 | 189 | struct unwind_state state; |
---|
166 | 190 | struct stack_info stack_info = {0}; |
---|
.. | .. |
---|
171 | 195 | printk("%sCall Trace:\n", log_lvl); |
---|
172 | 196 | |
---|
173 | 197 | unwind_start(&state, task, regs, stack); |
---|
174 | | - stack = stack ? : get_stack_pointer(task, regs); |
---|
175 | 198 | regs = unwind_get_entry_regs(&state, &partial); |
---|
176 | 199 | |
---|
177 | 200 | /* |
---|
.. | .. |
---|
190 | 213 | * - hardirq stack |
---|
191 | 214 | * - entry stack |
---|
192 | 215 | */ |
---|
193 | | - for ( ; stack; stack = PTR_ALIGN(stack_info.next_sp, sizeof(long))) { |
---|
| 216 | + for (stack = stack ?: get_stack_pointer(task, regs); |
---|
| 217 | + stack; |
---|
| 218 | + stack = stack_info.next_sp) { |
---|
194 | 219 | const char *stack_name; |
---|
| 220 | + |
---|
| 221 | + stack = PTR_ALIGN(stack, sizeof(long)); |
---|
195 | 222 | |
---|
196 | 223 | if (get_stack_info(stack, task, &stack_info, &visit_mask)) { |
---|
197 | 224 | /* |
---|
.. | .. |
---|
210 | 237 | printk("%s <%s>\n", log_lvl, stack_name); |
---|
211 | 238 | |
---|
212 | 239 | if (regs) |
---|
213 | | - show_regs_if_on_stack(&stack_info, regs, partial); |
---|
| 240 | + show_regs_if_on_stack(&stack_info, regs, partial, log_lvl); |
---|
214 | 241 | |
---|
215 | 242 | /* |
---|
216 | 243 | * Scan the stack, printing any text addresses we find. At the |
---|
.. | .. |
---|
271 | 298 | /* if the frame has entry regs, print them */ |
---|
272 | 299 | regs = unwind_get_entry_regs(&state, &partial); |
---|
273 | 300 | if (regs) |
---|
274 | | - show_regs_if_on_stack(&stack_info, regs, partial); |
---|
| 301 | + show_regs_if_on_stack(&stack_info, regs, partial, log_lvl); |
---|
275 | 302 | } |
---|
276 | 303 | |
---|
277 | 304 | if (stack_name) |
---|
.. | .. |
---|
279 | 306 | } |
---|
280 | 307 | } |
---|
281 | 308 | |
---|
282 | | -void show_stack(struct task_struct *task, unsigned long *sp) |
---|
| 309 | +void show_stack(struct task_struct *task, unsigned long *sp, |
---|
| 310 | + const char *loglvl) |
---|
283 | 311 | { |
---|
284 | 312 | task = task ? : current; |
---|
285 | 313 | |
---|
.. | .. |
---|
290 | 318 | if (!sp && task == current) |
---|
291 | 319 | sp = get_stack_pointer(current, NULL); |
---|
292 | 320 | |
---|
293 | | - show_trace_log_lvl(task, NULL, sp, KERN_DEFAULT); |
---|
| 321 | + show_trace_log_lvl(task, NULL, sp, loglvl); |
---|
294 | 322 | } |
---|
295 | 323 | |
---|
296 | 324 | void show_stack_regs(struct pt_regs *regs) |
---|
.. | .. |
---|
326 | 354 | } |
---|
327 | 355 | NOKPROBE_SYMBOL(oops_begin); |
---|
328 | 356 | |
---|
329 | | -void __noreturn rewind_stack_do_exit(int signr); |
---|
| 357 | +void __noreturn rewind_stack_and_make_dead(int signr); |
---|
330 | 358 | |
---|
331 | 359 | void oops_end(unsigned long flags, struct pt_regs *regs, int signr) |
---|
332 | 360 | { |
---|
.. | .. |
---|
344 | 372 | oops_exit(); |
---|
345 | 373 | |
---|
346 | 374 | /* Executive summary in case the oops scrolled away */ |
---|
347 | | - __show_regs(&exec_summary_regs, SHOW_REGS_ALL); |
---|
| 375 | + __show_regs(&exec_summary_regs, SHOW_REGS_ALL, KERN_DEFAULT); |
---|
348 | 376 | |
---|
349 | 377 | if (!signr) |
---|
350 | 378 | return; |
---|
.. | .. |
---|
361 | 389 | * reuse the task stack and that existing poisons are invalid. |
---|
362 | 390 | */ |
---|
363 | 391 | kasan_unpoison_task_stack(current); |
---|
364 | | - rewind_stack_do_exit(signr); |
---|
| 392 | + rewind_stack_and_make_dead(signr); |
---|
365 | 393 | } |
---|
366 | 394 | NOKPROBE_SYMBOL(oops_end); |
---|
367 | 395 | |
---|
368 | | -int __die(const char *str, struct pt_regs *regs, long err) |
---|
| 396 | +static void __die_header(const char *str, struct pt_regs *regs, long err) |
---|
369 | 397 | { |
---|
| 398 | + const char *pr = ""; |
---|
| 399 | + |
---|
370 | 400 | /* Save the regs of the first oops for the executive summary later. */ |
---|
371 | 401 | if (!die_counter) |
---|
372 | 402 | exec_summary_regs = *regs; |
---|
373 | 403 | |
---|
| 404 | + if (IS_ENABLED(CONFIG_PREEMPTION)) |
---|
| 405 | + pr = IS_ENABLED(CONFIG_PREEMPT_RT) ? " PREEMPT_RT" : " PREEMPT"; |
---|
| 406 | + |
---|
374 | 407 | printk(KERN_DEFAULT |
---|
375 | 408 | "%s: %04lx [#%d]%s%s%s%s%s\n", str, err & 0xffff, ++die_counter, |
---|
376 | | - IS_ENABLED(CONFIG_PREEMPT) ? " PREEMPT" : "", |
---|
| 409 | + pr, |
---|
377 | 410 | IS_ENABLED(CONFIG_SMP) ? " SMP" : "", |
---|
378 | 411 | debug_pagealloc_enabled() ? " DEBUG_PAGEALLOC" : "", |
---|
379 | 412 | IS_ENABLED(CONFIG_KASAN) ? " KASAN" : "", |
---|
380 | 413 | IS_ENABLED(CONFIG_PAGE_TABLE_ISOLATION) ? |
---|
381 | 414 | (boot_cpu_has(X86_FEATURE_PTI) ? " PTI" : " NOPTI") : ""); |
---|
| 415 | +} |
---|
| 416 | +NOKPROBE_SYMBOL(__die_header); |
---|
382 | 417 | |
---|
| 418 | +static int __die_body(const char *str, struct pt_regs *regs, long err) |
---|
| 419 | +{ |
---|
383 | 420 | show_regs(regs); |
---|
384 | 421 | print_modules(); |
---|
385 | 422 | |
---|
.. | .. |
---|
388 | 425 | return 1; |
---|
389 | 426 | |
---|
390 | 427 | return 0; |
---|
| 428 | +} |
---|
| 429 | +NOKPROBE_SYMBOL(__die_body); |
---|
| 430 | + |
---|
| 431 | +int __die(const char *str, struct pt_regs *regs, long err) |
---|
| 432 | +{ |
---|
| 433 | + __die_header(str, regs, err); |
---|
| 434 | + return __die_body(str, regs, err); |
---|
391 | 435 | } |
---|
392 | 436 | NOKPROBE_SYMBOL(__die); |
---|
393 | 437 | |
---|
.. | .. |
---|
405 | 449 | oops_end(flags, regs, sig); |
---|
406 | 450 | } |
---|
407 | 451 | |
---|
| 452 | +void die_addr(const char *str, struct pt_regs *regs, long err, long gp_addr) |
---|
| 453 | +{ |
---|
| 454 | + unsigned long flags = oops_begin(); |
---|
| 455 | + int sig = SIGSEGV; |
---|
| 456 | + |
---|
| 457 | + __die_header(str, regs, err); |
---|
| 458 | + if (gp_addr) |
---|
| 459 | + kasan_non_canonical_hook(gp_addr); |
---|
| 460 | + if (__die_body(str, regs, err)) |
---|
| 461 | + sig = 0; |
---|
| 462 | + oops_end(flags, regs, sig); |
---|
| 463 | +} |
---|
| 464 | + |
---|
408 | 465 | void show_regs(struct pt_regs *regs) |
---|
409 | 466 | { |
---|
| 467 | + enum show_regs_mode print_kernel_regs; |
---|
| 468 | + |
---|
410 | 469 | show_regs_print_info(KERN_DEFAULT); |
---|
411 | 470 | |
---|
412 | | - __show_regs(regs, user_mode(regs) ? SHOW_REGS_USER : SHOW_REGS_ALL); |
---|
| 471 | + print_kernel_regs = user_mode(regs) ? SHOW_REGS_USER : SHOW_REGS_ALL; |
---|
| 472 | + __show_regs(regs, print_kernel_regs, KERN_DEFAULT); |
---|
413 | 473 | |
---|
414 | 474 | /* |
---|
415 | 475 | * When in-kernel, we also print out the stack at the time of the fault.. |
---|