hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/arch/x86/kernel/dumpstack.c
....@@ -29,8 +29,8 @@
2929
3030 static struct pt_regs exec_summary_regs;
3131
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)
3434 {
3535 unsigned long *begin = task_stack_page(task);
3636 unsigned long *end = task_stack_page(task) + THREAD_SIZE;
....@@ -46,7 +46,8 @@
4646 return true;
4747 }
4848
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)
5051 {
5152 struct entry_stack *ss = cpu_entry_stack(smp_processor_id());
5253
....@@ -65,10 +66,35 @@
6566 }
6667
6768 static void printk_stack_address(unsigned long address, int reliable,
68
- char *log_lvl)
69
+ const char *log_lvl)
6970 {
7071 touch_nmi_watchdog();
7172 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);
7298 }
7399
74100 /*
....@@ -97,22 +123,20 @@
97123 #define OPCODE_BUFSIZE (PROLOGUE_SIZE + 1 + EPILOGUE_SIZE)
98124 u8 opcodes[OPCODE_BUFSIZE];
99125 unsigned long prologue = regs->ip - PROLOGUE_SIZE;
100
- bool bad_ip;
101126
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:
113129 printk("%sCode: %" __stringify(PROLOGUE_SIZE) "ph <%02x> %"
114130 __stringify(EPILOGUE_SIZE) "ph\n", loglvl, opcodes,
115131 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;
116140 }
117141 }
118142
....@@ -126,15 +150,15 @@
126150 show_opcodes(regs, loglvl);
127151 }
128152
129
-void show_iret_regs(struct pt_regs *regs)
153
+void show_iret_regs(struct pt_regs *regs, const char *log_lvl)
130154 {
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,
133157 regs->sp, regs->flags);
134158 }
135159
136160 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)
138162 {
139163 /*
140164 * These on_stack() checks aren't strictly necessary: the unwind code
....@@ -146,7 +170,7 @@
146170 * they can be printed in the right context.
147171 */
148172 if (!partial && on_stack(info, regs, sizeof(*regs))) {
149
- __show_regs(regs, SHOW_REGS_SHORT);
173
+ __show_regs(regs, SHOW_REGS_SHORT, log_lvl);
150174
151175 } else if (partial && on_stack(info, (void *)regs + IRET_FRAME_OFFSET,
152176 IRET_FRAME_SIZE)) {
....@@ -155,12 +179,12 @@
155179 * full pt_regs might not have been saved yet. In that case
156180 * just print the iret frame.
157181 */
158
- show_iret_regs(regs);
182
+ show_iret_regs(regs, log_lvl);
159183 }
160184 }
161185
162186 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)
164188 {
165189 struct unwind_state state;
166190 struct stack_info stack_info = {0};
....@@ -171,7 +195,6 @@
171195 printk("%sCall Trace:\n", log_lvl);
172196
173197 unwind_start(&state, task, regs, stack);
174
- stack = stack ? : get_stack_pointer(task, regs);
175198 regs = unwind_get_entry_regs(&state, &partial);
176199
177200 /*
....@@ -190,8 +213,12 @@
190213 * - hardirq stack
191214 * - entry stack
192215 */
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) {
194219 const char *stack_name;
220
+
221
+ stack = PTR_ALIGN(stack, sizeof(long));
195222
196223 if (get_stack_info(stack, task, &stack_info, &visit_mask)) {
197224 /*
....@@ -210,7 +237,7 @@
210237 printk("%s <%s>\n", log_lvl, stack_name);
211238
212239 if (regs)
213
- show_regs_if_on_stack(&stack_info, regs, partial);
240
+ show_regs_if_on_stack(&stack_info, regs, partial, log_lvl);
214241
215242 /*
216243 * Scan the stack, printing any text addresses we find. At the
....@@ -271,7 +298,7 @@
271298 /* if the frame has entry regs, print them */
272299 regs = unwind_get_entry_regs(&state, &partial);
273300 if (regs)
274
- show_regs_if_on_stack(&stack_info, regs, partial);
301
+ show_regs_if_on_stack(&stack_info, regs, partial, log_lvl);
275302 }
276303
277304 if (stack_name)
....@@ -279,7 +306,8 @@
279306 }
280307 }
281308
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)
283311 {
284312 task = task ? : current;
285313
....@@ -290,7 +318,7 @@
290318 if (!sp && task == current)
291319 sp = get_stack_pointer(current, NULL);
292320
293
- show_trace_log_lvl(task, NULL, sp, KERN_DEFAULT);
321
+ show_trace_log_lvl(task, NULL, sp, loglvl);
294322 }
295323
296324 void show_stack_regs(struct pt_regs *regs)
....@@ -326,7 +354,7 @@
326354 }
327355 NOKPROBE_SYMBOL(oops_begin);
328356
329
-void __noreturn rewind_stack_do_exit(int signr);
357
+void __noreturn rewind_stack_and_make_dead(int signr);
330358
331359 void oops_end(unsigned long flags, struct pt_regs *regs, int signr)
332360 {
....@@ -344,7 +372,7 @@
344372 oops_exit();
345373
346374 /* 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);
348376
349377 if (!signr)
350378 return;
....@@ -361,25 +389,34 @@
361389 * reuse the task stack and that existing poisons are invalid.
362390 */
363391 kasan_unpoison_task_stack(current);
364
- rewind_stack_do_exit(signr);
392
+ rewind_stack_and_make_dead(signr);
365393 }
366394 NOKPROBE_SYMBOL(oops_end);
367395
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)
369397 {
398
+ const char *pr = "";
399
+
370400 /* Save the regs of the first oops for the executive summary later. */
371401 if (!die_counter)
372402 exec_summary_regs = *regs;
373403
404
+ if (IS_ENABLED(CONFIG_PREEMPTION))
405
+ pr = IS_ENABLED(CONFIG_PREEMPT_RT) ? " PREEMPT_RT" : " PREEMPT";
406
+
374407 printk(KERN_DEFAULT
375408 "%s: %04lx [#%d]%s%s%s%s%s\n", str, err & 0xffff, ++die_counter,
376
- IS_ENABLED(CONFIG_PREEMPT) ? " PREEMPT" : "",
409
+ pr,
377410 IS_ENABLED(CONFIG_SMP) ? " SMP" : "",
378411 debug_pagealloc_enabled() ? " DEBUG_PAGEALLOC" : "",
379412 IS_ENABLED(CONFIG_KASAN) ? " KASAN" : "",
380413 IS_ENABLED(CONFIG_PAGE_TABLE_ISOLATION) ?
381414 (boot_cpu_has(X86_FEATURE_PTI) ? " PTI" : " NOPTI") : "");
415
+}
416
+NOKPROBE_SYMBOL(__die_header);
382417
418
+static int __die_body(const char *str, struct pt_regs *regs, long err)
419
+{
383420 show_regs(regs);
384421 print_modules();
385422
....@@ -388,6 +425,13 @@
388425 return 1;
389426
390427 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);
391435 }
392436 NOKPROBE_SYMBOL(__die);
393437
....@@ -405,11 +449,27 @@
405449 oops_end(flags, regs, sig);
406450 }
407451
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
+
408465 void show_regs(struct pt_regs *regs)
409466 {
467
+ enum show_regs_mode print_kernel_regs;
468
+
410469 show_regs_print_info(KERN_DEFAULT);
411470
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);
413473
414474 /*
415475 * When in-kernel, we also print out the stack at the time of the fault..