1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/extable.h>
3 #include <linux/uaccess.h>
4 #include <linux/sched/debug.h>
5 #include <linux/bitfield.h>
6 #include <xen/xen.h>
7
8 #include <asm/fpu/internal.h>
9 #include <asm/sev.h>
10 #include <asm/traps.h>
11 #include <asm/kdebug.h>
12 #include <asm/insn-eval.h>
13
pt_regs_nr(struct pt_regs * regs,int nr)14 static inline unsigned long *pt_regs_nr(struct pt_regs *regs, int nr)
15 {
16 int reg_offset = pt_regs_offset(regs, nr);
17 static unsigned long __dummy;
18
19 if (WARN_ON_ONCE(reg_offset < 0))
20 return &__dummy;
21
22 return (unsigned long *)((unsigned long)regs + reg_offset);
23 }
24
25 static inline unsigned long
ex_fixup_addr(const struct exception_table_entry * x)26 ex_fixup_addr(const struct exception_table_entry *x)
27 {
28 return (unsigned long)&x->fixup + x->fixup;
29 }
30
ex_handler_default(const struct exception_table_entry * e,struct pt_regs * regs)31 static bool ex_handler_default(const struct exception_table_entry *e,
32 struct pt_regs *regs)
33 {
34 if (e->data & EX_FLAG_CLEAR_AX)
35 regs->ax = 0;
36 if (e->data & EX_FLAG_CLEAR_DX)
37 regs->dx = 0;
38
39 regs->ip = ex_fixup_addr(e);
40 return true;
41 }
42
ex_handler_fault(const struct exception_table_entry * fixup,struct pt_regs * regs,int trapnr)43 static bool ex_handler_fault(const struct exception_table_entry *fixup,
44 struct pt_regs *regs, int trapnr)
45 {
46 regs->ax = trapnr;
47 return ex_handler_default(fixup, regs);
48 }
49
50 /*
51 * Handler for when we fail to restore a task's FPU state. We should never get
52 * here because the FPU state of a task using the FPU (task->thread.fpu.state)
53 * should always be valid. However, past bugs have allowed userspace to set
54 * reserved bits in the XSAVE area using PTRACE_SETREGSET or sys_rt_sigreturn().
55 * These caused XRSTOR to fail when switching to the task, leaking the FPU
56 * registers of the task previously executing on the CPU. Mitigate this class
57 * of vulnerability by restoring from the initial state (essentially, zeroing
58 * out all the FPU registers) if we can't restore from the task's FPU state.
59 */
ex_handler_fprestore(const struct exception_table_entry * fixup,struct pt_regs * regs)60 static bool ex_handler_fprestore(const struct exception_table_entry *fixup,
61 struct pt_regs *regs)
62 {
63 regs->ip = ex_fixup_addr(fixup);
64
65 WARN_ONCE(1, "Bad FPU state detected at %pB, reinitializing FPU registers.",
66 (void *)instruction_pointer(regs));
67
68 __restore_fpregs_from_fpstate(&init_fpstate, xfeatures_mask_fpstate());
69 return true;
70 }
71
ex_handler_uaccess(const struct exception_table_entry * fixup,struct pt_regs * regs,int trapnr)72 static bool ex_handler_uaccess(const struct exception_table_entry *fixup,
73 struct pt_regs *regs, int trapnr)
74 {
75 WARN_ONCE(trapnr == X86_TRAP_GP, "General protection fault in user access. Non-canonical address?");
76 return ex_handler_default(fixup, regs);
77 }
78
ex_handler_copy(const struct exception_table_entry * fixup,struct pt_regs * regs,int trapnr)79 static bool ex_handler_copy(const struct exception_table_entry *fixup,
80 struct pt_regs *regs, int trapnr)
81 {
82 WARN_ONCE(trapnr == X86_TRAP_GP, "General protection fault in user access. Non-canonical address?");
83 return ex_handler_fault(fixup, regs, trapnr);
84 }
85
ex_handler_msr(const struct exception_table_entry * fixup,struct pt_regs * regs,bool wrmsr,bool safe,int reg)86 static bool ex_handler_msr(const struct exception_table_entry *fixup,
87 struct pt_regs *regs, bool wrmsr, bool safe, int reg)
88 {
89 if (__ONCE_LITE_IF(!safe && wrmsr)) {
90 pr_warn("unchecked MSR access error: WRMSR to 0x%x (tried to write 0x%08x%08x) at rIP: 0x%lx (%pS)\n",
91 (unsigned int)regs->cx, (unsigned int)regs->dx,
92 (unsigned int)regs->ax, regs->ip, (void *)regs->ip);
93 show_stack_regs(regs);
94 }
95
96 if (__ONCE_LITE_IF(!safe && !wrmsr)) {
97 pr_warn("unchecked MSR access error: RDMSR from 0x%x at rIP: 0x%lx (%pS)\n",
98 (unsigned int)regs->cx, regs->ip, (void *)regs->ip);
99 show_stack_regs(regs);
100 }
101
102 if (!wrmsr) {
103 /* Pretend that the read succeeded and returned 0. */
104 regs->ax = 0;
105 regs->dx = 0;
106 }
107
108 if (safe)
109 *pt_regs_nr(regs, reg) = -EIO;
110
111 return ex_handler_default(fixup, regs);
112 }
113
ex_handler_clear_fs(const struct exception_table_entry * fixup,struct pt_regs * regs)114 static bool ex_handler_clear_fs(const struct exception_table_entry *fixup,
115 struct pt_regs *regs)
116 {
117 if (static_cpu_has(X86_BUG_NULL_SEG))
118 asm volatile ("mov %0, %%fs" : : "rm" (__USER_DS));
119 asm volatile ("mov %0, %%fs" : : "rm" (0));
120 return ex_handler_default(fixup, regs);
121 }
122
ex_handler_imm_reg(const struct exception_table_entry * fixup,struct pt_regs * regs,int reg,int imm)123 static bool ex_handler_imm_reg(const struct exception_table_entry *fixup,
124 struct pt_regs *regs, int reg, int imm)
125 {
126 *pt_regs_nr(regs, reg) = (long)imm;
127 return ex_handler_default(fixup, regs);
128 }
129
ex_get_fixup_type(unsigned long ip)130 int ex_get_fixup_type(unsigned long ip)
131 {
132 const struct exception_table_entry *e = search_exception_tables(ip);
133
134 return e ? FIELD_GET(EX_DATA_TYPE_MASK, e->data) : EX_TYPE_NONE;
135 }
136
fixup_exception(struct pt_regs * regs,int trapnr,unsigned long error_code,unsigned long fault_addr)137 int fixup_exception(struct pt_regs *regs, int trapnr, unsigned long error_code,
138 unsigned long fault_addr)
139 {
140 const struct exception_table_entry *e;
141 int type, reg, imm;
142
143 #ifdef CONFIG_PNPBIOS
144 if (unlikely(SEGMENT_IS_PNP_CODE(regs->cs))) {
145 extern u32 pnp_bios_fault_eip, pnp_bios_fault_esp;
146 extern u32 pnp_bios_is_utter_crap;
147 pnp_bios_is_utter_crap = 1;
148 printk(KERN_CRIT "PNPBIOS fault.. attempting recovery.\n");
149 __asm__ volatile(
150 "movl %0, %%esp\n\t"
151 "jmp *%1\n\t"
152 : : "g" (pnp_bios_fault_esp), "g" (pnp_bios_fault_eip));
153 panic("do_trap: can't hit this");
154 }
155 #endif
156
157 e = search_exception_tables(regs->ip);
158 if (!e)
159 return 0;
160
161 type = FIELD_GET(EX_DATA_TYPE_MASK, e->data);
162 reg = FIELD_GET(EX_DATA_REG_MASK, e->data);
163 imm = FIELD_GET(EX_DATA_IMM_MASK, e->data);
164
165 switch (type) {
166 case EX_TYPE_DEFAULT:
167 case EX_TYPE_DEFAULT_MCE_SAFE:
168 return ex_handler_default(e, regs);
169 case EX_TYPE_FAULT:
170 case EX_TYPE_FAULT_MCE_SAFE:
171 return ex_handler_fault(e, regs, trapnr);
172 case EX_TYPE_UACCESS:
173 return ex_handler_uaccess(e, regs, trapnr);
174 case EX_TYPE_COPY:
175 return ex_handler_copy(e, regs, trapnr);
176 case EX_TYPE_CLEAR_FS:
177 return ex_handler_clear_fs(e, regs);
178 case EX_TYPE_FPU_RESTORE:
179 return ex_handler_fprestore(e, regs);
180 case EX_TYPE_BPF:
181 return ex_handler_bpf(e, regs);
182 case EX_TYPE_WRMSR:
183 return ex_handler_msr(e, regs, true, false, reg);
184 case EX_TYPE_RDMSR:
185 return ex_handler_msr(e, regs, false, false, reg);
186 case EX_TYPE_WRMSR_SAFE:
187 return ex_handler_msr(e, regs, true, true, reg);
188 case EX_TYPE_RDMSR_SAFE:
189 return ex_handler_msr(e, regs, false, true, reg);
190 case EX_TYPE_WRMSR_IN_MCE:
191 ex_handler_msr_mce(regs, true);
192 break;
193 case EX_TYPE_RDMSR_IN_MCE:
194 ex_handler_msr_mce(regs, false);
195 break;
196 case EX_TYPE_POP_REG:
197 regs->sp += sizeof(long);
198 fallthrough;
199 case EX_TYPE_IMM_REG:
200 return ex_handler_imm_reg(e, regs, reg, imm);
201 }
202 BUG();
203 }
204
205 extern unsigned int early_recursion_flag;
206
207 /* Restricted version used during very early boot */
early_fixup_exception(struct pt_regs * regs,int trapnr)208 void __init early_fixup_exception(struct pt_regs *regs, int trapnr)
209 {
210 /* Ignore early NMIs. */
211 if (trapnr == X86_TRAP_NMI)
212 return;
213
214 if (early_recursion_flag > 2)
215 goto halt_loop;
216
217 /*
218 * Old CPUs leave the high bits of CS on the stack
219 * undefined. I'm not sure which CPUs do this, but at least
220 * the 486 DX works this way.
221 * Xen pv domains are not using the default __KERNEL_CS.
222 */
223 if (!xen_pv_domain() && regs->cs != __KERNEL_CS)
224 goto fail;
225
226 /*
227 * The full exception fixup machinery is available as soon as
228 * the early IDT is loaded. This means that it is the
229 * responsibility of extable users to either function correctly
230 * when handlers are invoked early or to simply avoid causing
231 * exceptions before they're ready to handle them.
232 *
233 * This is better than filtering which handlers can be used,
234 * because refusing to call a handler here is guaranteed to
235 * result in a hard-to-debug panic.
236 *
237 * Keep in mind that not all vectors actually get here. Early
238 * page faults, for example, are special.
239 */
240 if (fixup_exception(regs, trapnr, regs->orig_ax, 0))
241 return;
242
243 if (trapnr == X86_TRAP_UD) {
244 if (report_bug(regs->ip, regs) == BUG_TRAP_TYPE_WARN) {
245 /* Skip the ud2. */
246 regs->ip += LEN_UD2;
247 return;
248 }
249
250 /*
251 * If this was a BUG and report_bug returns or if this
252 * was just a normal #UD, we want to continue onward and
253 * crash.
254 */
255 }
256
257 fail:
258 early_printk("PANIC: early exception 0x%02x IP %lx:%lx error %lx cr2 0x%lx\n",
259 (unsigned)trapnr, (unsigned long)regs->cs, regs->ip,
260 regs->orig_ax, read_cr2());
261
262 show_regs(regs);
263
264 halt_loop:
265 while (true)
266 halt();
267 }
268