hc
2023-12-06 08f87f769b595151be1afeff53e144f543faa614
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};
....@@ -210,7 +234,7 @@
210234 printk("%s <%s>\n", log_lvl, stack_name);
211235
212236 if (regs)
213
- show_regs_if_on_stack(&stack_info, regs, partial);
237
+ show_regs_if_on_stack(&stack_info, regs, partial, log_lvl);
214238
215239 /*
216240 * Scan the stack, printing any text addresses we find. At the
....@@ -271,7 +295,7 @@
271295 /* if the frame has entry regs, print them */
272296 regs = unwind_get_entry_regs(&state, &partial);
273297 if (regs)
274
- show_regs_if_on_stack(&stack_info, regs, partial);
298
+ show_regs_if_on_stack(&stack_info, regs, partial, log_lvl);
275299 }
276300
277301 if (stack_name)
....@@ -279,7 +303,8 @@
279303 }
280304 }
281305
282
-void show_stack(struct task_struct *task, unsigned long *sp)
306
+void show_stack(struct task_struct *task, unsigned long *sp,
307
+ const char *loglvl)
283308 {
284309 task = task ? : current;
285310
....@@ -290,7 +315,7 @@
290315 if (!sp && task == current)
291316 sp = get_stack_pointer(current, NULL);
292317
293
- show_trace_log_lvl(task, NULL, sp, KERN_DEFAULT);
318
+ show_trace_log_lvl(task, NULL, sp, loglvl);
294319 }
295320
296321 void show_stack_regs(struct pt_regs *regs)
....@@ -344,7 +369,7 @@
344369 oops_exit();
345370
346371 /* Executive summary in case the oops scrolled away */
347
- __show_regs(&exec_summary_regs, SHOW_REGS_ALL);
372
+ __show_regs(&exec_summary_regs, SHOW_REGS_ALL, KERN_DEFAULT);
348373
349374 if (!signr)
350375 return;
....@@ -365,21 +390,30 @@
365390 }
366391 NOKPROBE_SYMBOL(oops_end);
367392
368
-int __die(const char *str, struct pt_regs *regs, long err)
393
+static void __die_header(const char *str, struct pt_regs *regs, long err)
369394 {
395
+ const char *pr = "";
396
+
370397 /* Save the regs of the first oops for the executive summary later. */
371398 if (!die_counter)
372399 exec_summary_regs = *regs;
373400
401
+ if (IS_ENABLED(CONFIG_PREEMPTION))
402
+ pr = IS_ENABLED(CONFIG_PREEMPT_RT) ? " PREEMPT_RT" : " PREEMPT";
403
+
374404 printk(KERN_DEFAULT
375405 "%s: %04lx [#%d]%s%s%s%s%s\n", str, err & 0xffff, ++die_counter,
376
- IS_ENABLED(CONFIG_PREEMPT) ? " PREEMPT" : "",
406
+ pr,
377407 IS_ENABLED(CONFIG_SMP) ? " SMP" : "",
378408 debug_pagealloc_enabled() ? " DEBUG_PAGEALLOC" : "",
379409 IS_ENABLED(CONFIG_KASAN) ? " KASAN" : "",
380410 IS_ENABLED(CONFIG_PAGE_TABLE_ISOLATION) ?
381411 (boot_cpu_has(X86_FEATURE_PTI) ? " PTI" : " NOPTI") : "");
412
+}
413
+NOKPROBE_SYMBOL(__die_header);
382414
415
+static int __die_body(const char *str, struct pt_regs *regs, long err)
416
+{
383417 show_regs(regs);
384418 print_modules();
385419
....@@ -388,6 +422,13 @@
388422 return 1;
389423
390424 return 0;
425
+}
426
+NOKPROBE_SYMBOL(__die_body);
427
+
428
+int __die(const char *str, struct pt_regs *regs, long err)
429
+{
430
+ __die_header(str, regs, err);
431
+ return __die_body(str, regs, err);
391432 }
392433 NOKPROBE_SYMBOL(__die);
393434
....@@ -405,11 +446,27 @@
405446 oops_end(flags, regs, sig);
406447 }
407448
449
+void die_addr(const char *str, struct pt_regs *regs, long err, long gp_addr)
450
+{
451
+ unsigned long flags = oops_begin();
452
+ int sig = SIGSEGV;
453
+
454
+ __die_header(str, regs, err);
455
+ if (gp_addr)
456
+ kasan_non_canonical_hook(gp_addr);
457
+ if (__die_body(str, regs, err))
458
+ sig = 0;
459
+ oops_end(flags, regs, sig);
460
+}
461
+
408462 void show_regs(struct pt_regs *regs)
409463 {
464
+ enum show_regs_mode print_kernel_regs;
465
+
410466 show_regs_print_info(KERN_DEFAULT);
411467
412
- __show_regs(regs, user_mode(regs) ? SHOW_REGS_USER : SHOW_REGS_ALL);
468
+ print_kernel_regs = user_mode(regs) ? SHOW_REGS_USER : SHOW_REGS_ALL;
469
+ __show_regs(regs, print_kernel_regs, KERN_DEFAULT);
413470
414471 /*
415472 * When in-kernel, we also print out the stack at the time of the fault..