blob: 30bb0bd3b1b88896a4c858f76fe879e15c88f7c5 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Paul Gortmaker744c1932016-09-19 17:04:18 -04002#include <linux/extable.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -08003#include <linux/uaccess.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +01004#include <linux/sched/debug.h>
Juergen Gross42b3a4c2017-11-24 09:42:21 +01005#include <xen/xen.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +01006
Eric Biggersd5c80282017-09-23 15:00:09 +02007#include <asm/fpu/internal.h>
Andy Lutomirski0d0efc02016-04-02 07:01:33 -07008#include <asm/traps.h>
Borislav Petkov81c29492016-07-05 00:31:27 +02009#include <asm/kdebug.h>
Harvey Harrison6d485832008-01-30 13:31:41 +010010
Tony Luck548acf12016-02-17 10:20:12 -080011typedef bool (*ex_handler_t)(const struct exception_table_entry *,
Jann Horn81fd9c12018-08-28 22:14:19 +020012 struct pt_regs *, int, unsigned long,
13 unsigned long);
Tony Luck548acf12016-02-17 10:20:12 -080014
H. Peter Anvin70627652012-04-20 17:12:48 -070015static inline unsigned long
H. Peter Anvin70627652012-04-20 17:12:48 -070016ex_fixup_addr(const struct exception_table_entry *x)
17{
18 return (unsigned long)&x->fixup + x->fixup;
19}
Tony Luck548acf12016-02-17 10:20:12 -080020static inline ex_handler_t
21ex_fixup_handler(const struct exception_table_entry *x)
Harvey Harrison6d485832008-01-30 13:31:41 +010022{
Tony Luck548acf12016-02-17 10:20:12 -080023 return (ex_handler_t)((unsigned long)&x->handler + x->handler);
24}
25
Andi Kleen80a3e392017-12-21 16:18:20 -080026__visible bool ex_handler_default(const struct exception_table_entry *fixup,
Jann Horn81fd9c12018-08-28 22:14:19 +020027 struct pt_regs *regs, int trapnr,
28 unsigned long error_code,
29 unsigned long fault_addr)
Tony Luck548acf12016-02-17 10:20:12 -080030{
31 regs->ip = ex_fixup_addr(fixup);
32 return true;
33}
34EXPORT_SYMBOL(ex_handler_default);
35
Andi Kleen80a3e392017-12-21 16:18:20 -080036__visible bool ex_handler_fault(const struct exception_table_entry *fixup,
Jann Horn81fd9c12018-08-28 22:14:19 +020037 struct pt_regs *regs, int trapnr,
38 unsigned long error_code,
39 unsigned long fault_addr)
Tony Luck548acf12016-02-17 10:20:12 -080040{
41 regs->ip = ex_fixup_addr(fixup);
42 regs->ax = trapnr;
43 return true;
44}
45EXPORT_SYMBOL_GPL(ex_handler_fault);
46
Kees Cook7a46ec02017-08-15 09:19:24 -070047/*
Eric Biggersd5c80282017-09-23 15:00:09 +020048 * Handler for when we fail to restore a task's FPU state. We should never get
49 * here because the FPU state of a task using the FPU (task->thread.fpu.state)
50 * should always be valid. However, past bugs have allowed userspace to set
51 * reserved bits in the XSAVE area using PTRACE_SETREGSET or sys_rt_sigreturn().
52 * These caused XRSTOR to fail when switching to the task, leaking the FPU
53 * registers of the task previously executing on the CPU. Mitigate this class
54 * of vulnerability by restoring from the initial state (essentially, zeroing
55 * out all the FPU registers) if we can't restore from the task's FPU state.
56 */
Andi Kleen80a3e392017-12-21 16:18:20 -080057__visible bool ex_handler_fprestore(const struct exception_table_entry *fixup,
Jann Horn81fd9c12018-08-28 22:14:19 +020058 struct pt_regs *regs, int trapnr,
59 unsigned long error_code,
60 unsigned long fault_addr)
Eric Biggersd5c80282017-09-23 15:00:09 +020061{
62 regs->ip = ex_fixup_addr(fixup);
63
64 WARN_ONCE(1, "Bad FPU state detected at %pB, reinitializing FPU registers.",
65 (void *)instruction_pointer(regs));
66
67 __copy_kernel_to_fpregs(&init_fpstate, -1);
68 return true;
69}
70EXPORT_SYMBOL_GPL(ex_handler_fprestore);
71
Jann Horn75045f72018-08-28 22:14:18 +020072__visible bool ex_handler_uaccess(const struct exception_table_entry *fixup,
Jann Horn81fd9c12018-08-28 22:14:19 +020073 struct pt_regs *regs, int trapnr,
74 unsigned long error_code,
75 unsigned long fault_addr)
Jann Horn75045f72018-08-28 22:14:18 +020076{
Linus Torvalds00c42372019-02-26 09:16:04 -080077 WARN_ONCE(trapnr == X86_TRAP_GP, "General protection fault in user access. Non-canonical address?");
Jann Horn75045f72018-08-28 22:14:18 +020078 regs->ip = ex_fixup_addr(fixup);
79 return true;
80}
81EXPORT_SYMBOL(ex_handler_uaccess);
82
Andi Kleen80a3e392017-12-21 16:18:20 -080083__visible bool ex_handler_ext(const struct exception_table_entry *fixup,
Jann Horn81fd9c12018-08-28 22:14:19 +020084 struct pt_regs *regs, int trapnr,
85 unsigned long error_code,
86 unsigned long fault_addr)
Tony Luck548acf12016-02-17 10:20:12 -080087{
88 /* Special hack for uaccess_err */
Andy Lutomirskidfa9a942016-07-14 13:22:56 -070089 current->thread.uaccess_err = 1;
Tony Luck548acf12016-02-17 10:20:12 -080090 regs->ip = ex_fixup_addr(fixup);
91 return true;
92}
93EXPORT_SYMBOL(ex_handler_ext);
94
Andi Kleen80a3e392017-12-21 16:18:20 -080095__visible bool ex_handler_rdmsr_unsafe(const struct exception_table_entry *fixup,
Jann Horn81fd9c12018-08-28 22:14:19 +020096 struct pt_regs *regs, int trapnr,
97 unsigned long error_code,
98 unsigned long fault_addr)
Andy Lutomirskifbd70432016-04-02 07:01:37 -070099{
Sakari Ailusd75f7732019-03-25 21:32:28 +0200100 if (pr_warn_once("unchecked MSR access error: RDMSR from 0x%x at rIP: 0x%lx (%pS)\n",
Borislav Petkov81c29492016-07-05 00:31:27 +0200101 (unsigned int)regs->cx, regs->ip, (void *)regs->ip))
102 show_stack_regs(regs);
Andy Lutomirskifbd70432016-04-02 07:01:37 -0700103
104 /* Pretend that the read succeeded and returned 0. */
105 regs->ip = ex_fixup_addr(fixup);
106 regs->ax = 0;
107 regs->dx = 0;
108 return true;
109}
110EXPORT_SYMBOL(ex_handler_rdmsr_unsafe);
111
Andi Kleen80a3e392017-12-21 16:18:20 -0800112__visible bool ex_handler_wrmsr_unsafe(const struct exception_table_entry *fixup,
Jann Horn81fd9c12018-08-28 22:14:19 +0200113 struct pt_regs *regs, int trapnr,
114 unsigned long error_code,
115 unsigned long fault_addr)
Andy Lutomirskifbd70432016-04-02 07:01:37 -0700116{
Sakari Ailusd75f7732019-03-25 21:32:28 +0200117 if (pr_warn_once("unchecked MSR access error: WRMSR to 0x%x (tried to write 0x%08x%08x) at rIP: 0x%lx (%pS)\n",
Borislav Petkov81c29492016-07-05 00:31:27 +0200118 (unsigned int)regs->cx, (unsigned int)regs->dx,
119 (unsigned int)regs->ax, regs->ip, (void *)regs->ip))
120 show_stack_regs(regs);
Andy Lutomirskifbd70432016-04-02 07:01:37 -0700121
122 /* Pretend that the write succeeded. */
123 regs->ip = ex_fixup_addr(fixup);
124 return true;
125}
126EXPORT_SYMBOL(ex_handler_wrmsr_unsafe);
127
Andi Kleen80a3e392017-12-21 16:18:20 -0800128__visible bool ex_handler_clear_fs(const struct exception_table_entry *fixup,
Jann Horn81fd9c12018-08-28 22:14:19 +0200129 struct pt_regs *regs, int trapnr,
130 unsigned long error_code,
131 unsigned long fault_addr)
Andy Lutomirski45e876f2016-04-26 12:23:26 -0700132{
133 if (static_cpu_has(X86_BUG_NULL_SEG))
134 asm volatile ("mov %0, %%fs" : : "rm" (__USER_DS));
135 asm volatile ("mov %0, %%fs" : : "rm" (0));
Jann Horn81fd9c12018-08-28 22:14:19 +0200136 return ex_handler_default(fixup, regs, trapnr, error_code, fault_addr);
Andy Lutomirski45e876f2016-04-26 12:23:26 -0700137}
138EXPORT_SYMBOL(ex_handler_clear_fs);
139
Andi Kleen80a3e392017-12-21 16:18:20 -0800140__visible bool ex_has_fault_handler(unsigned long ip)
Tony Luck548acf12016-02-17 10:20:12 -0800141{
142 const struct exception_table_entry *e;
143 ex_handler_t handler;
144
145 e = search_exception_tables(ip);
146 if (!e)
147 return false;
148 handler = ex_fixup_handler(e);
149
150 return handler == ex_handler_fault;
151}
152
Jann Horn81fd9c12018-08-28 22:14:19 +0200153int fixup_exception(struct pt_regs *regs, int trapnr, unsigned long error_code,
154 unsigned long fault_addr)
Tony Luck548acf12016-02-17 10:20:12 -0800155{
156 const struct exception_table_entry *e;
157 ex_handler_t handler;
Harvey Harrison6d485832008-01-30 13:31:41 +0100158
159#ifdef CONFIG_PNPBIOS
160 if (unlikely(SEGMENT_IS_PNP_CODE(regs->cs))) {
161 extern u32 pnp_bios_fault_eip, pnp_bios_fault_esp;
162 extern u32 pnp_bios_is_utter_crap;
163 pnp_bios_is_utter_crap = 1;
164 printk(KERN_CRIT "PNPBIOS fault.. attempting recovery.\n");
165 __asm__ volatile(
166 "movl %0, %%esp\n\t"
167 "jmp *%1\n\t"
168 : : "g" (pnp_bios_fault_esp), "g" (pnp_bios_fault_eip));
169 panic("do_trap: can't hit this");
170 }
171#endif
172
Tony Luck548acf12016-02-17 10:20:12 -0800173 e = search_exception_tables(regs->ip);
174 if (!e)
175 return 0;
H. Peter Anvin70627652012-04-20 17:12:48 -0700176
Tony Luck548acf12016-02-17 10:20:12 -0800177 handler = ex_fixup_handler(e);
Jann Horn81fd9c12018-08-28 22:14:19 +0200178 return handler(e, regs, trapnr, error_code, fault_addr);
Harvey Harrison6d485832008-01-30 13:31:41 +0100179}
H. Peter Anvin6a1ea272012-04-19 15:24:20 -0700180
Andy Lutomirski0e861fb2016-04-02 07:01:34 -0700181extern unsigned int early_recursion_flag;
182
H. Peter Anvin6a1ea272012-04-19 15:24:20 -0700183/* Restricted version used during very early boot */
Andy Lutomirski0e861fb2016-04-02 07:01:34 -0700184void __init early_fixup_exception(struct pt_regs *regs, int trapnr)
H. Peter Anvin6a1ea272012-04-19 15:24:20 -0700185{
Andy Lutomirski0d0efc02016-04-02 07:01:33 -0700186 /* Ignore early NMIs. */
187 if (trapnr == X86_TRAP_NMI)
Andy Lutomirski0e861fb2016-04-02 07:01:34 -0700188 return;
189
190 if (early_recursion_flag > 2)
191 goto halt_loop;
192
Andy Lutomirskifc0e81b2016-11-19 18:42:40 -0800193 /*
194 * Old CPUs leave the high bits of CS on the stack
195 * undefined. I'm not sure which CPUs do this, but at least
196 * the 486 DX works this way.
Juergen Gross42b3a4c2017-11-24 09:42:21 +0100197 * Xen pv domains are not using the default __KERNEL_CS.
Andy Lutomirskifc0e81b2016-11-19 18:42:40 -0800198 */
Juergen Gross42b3a4c2017-11-24 09:42:21 +0100199 if (!xen_pv_domain() && regs->cs != __KERNEL_CS)
Andy Lutomirski0e861fb2016-04-02 07:01:34 -0700200 goto fail;
Andy Lutomirski0d0efc02016-04-02 07:01:33 -0700201
Andy Lutomirski60a0e202016-04-04 08:46:22 -0700202 /*
203 * The full exception fixup machinery is available as soon as
204 * the early IDT is loaded. This means that it is the
205 * responsibility of extable users to either function correctly
206 * when handlers are invoked early or to simply avoid causing
207 * exceptions before they're ready to handle them.
208 *
209 * This is better than filtering which handlers can be used,
210 * because refusing to call a handler here is guaranteed to
211 * result in a hard-to-debug panic.
212 *
213 * Keep in mind that not all vectors actually get here. Early
Jann Horn81fd9c12018-08-28 22:14:19 +0200214 * page faults, for example, are special.
Andy Lutomirski60a0e202016-04-04 08:46:22 -0700215 */
Jann Horn81fd9c12018-08-28 22:14:19 +0200216 if (fixup_exception(regs, trapnr, regs->orig_ax, 0))
Andy Lutomirskiae7ef452016-04-02 07:01:35 -0700217 return;
Andy Lutomirski0e861fb2016-04-02 07:01:34 -0700218
Peter Zijlstra8a524f82017-06-12 13:52:46 +0200219 if (fixup_bug(regs, trapnr))
220 return;
221
Andy Lutomirski0e861fb2016-04-02 07:01:34 -0700222fail:
223 early_printk("PANIC: early exception 0x%02x IP %lx:%lx error %lx cr2 0x%lx\n",
224 (unsigned)trapnr, (unsigned long)regs->cs, regs->ip,
225 regs->orig_ax, read_cr2());
226
227 show_regs(regs);
228
229halt_loop:
230 while (true)
231 halt();
H. Peter Anvin6a1ea272012-04-19 15:24:20 -0700232}