Linux Audio

Check our new training course

Loading...
v6.2
  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/api.h>
  9#include <asm/sev.h>
 10#include <asm/traps.h>
 11#include <asm/kdebug.h>
 12#include <asm/insn-eval.h>
 13#include <asm/sgx.h>
 14
 15static inline unsigned long *pt_regs_nr(struct pt_regs *regs, int nr)
 
 16{
 17	int reg_offset = pt_regs_offset(regs, nr);
 18	static unsigned long __dummy;
 19
 20	if (WARN_ON_ONCE(reg_offset < 0))
 21		return &__dummy;
 22
 23	return (unsigned long *)((unsigned long)regs + reg_offset);
 24}
 25
 26static inline unsigned long
 27ex_fixup_addr(const struct exception_table_entry *x)
 28{
 29	return (unsigned long)&x->fixup + x->fixup;
 30}
 31
 32static bool ex_handler_default(const struct exception_table_entry *e,
 33			       struct pt_regs *regs)
 34{
 35	if (e->data & EX_FLAG_CLEAR_AX)
 36		regs->ax = 0;
 37	if (e->data & EX_FLAG_CLEAR_DX)
 38		regs->dx = 0;
 39
 40	regs->ip = ex_fixup_addr(e);
 41	return true;
 42}
 43
 44/*
 45 * This is the *very* rare case where we do a "load_unaligned_zeropad()"
 46 * and it's a page crosser into a non-existent page.
 47 *
 48 * This happens when we optimistically load a pathname a word-at-a-time
 49 * and the name is less than the full word and the  next page is not
 50 * mapped. Typically that only happens for CONFIG_DEBUG_PAGEALLOC.
 51 *
 52 * NOTE! The faulting address is always a 'mov mem,reg' type instruction
 53 * of size 'long', and the exception fixup must always point to right
 54 * after the instruction.
 55 */
 56static bool ex_handler_zeropad(const struct exception_table_entry *e,
 57			       struct pt_regs *regs,
 58			       unsigned long fault_addr)
 59{
 60	struct insn insn;
 61	const unsigned long mask = sizeof(long) - 1;
 62	unsigned long offset, addr, next_ip, len;
 63	unsigned long *reg;
 64
 65	next_ip = ex_fixup_addr(e);
 66	len = next_ip - regs->ip;
 67	if (len > MAX_INSN_SIZE)
 68		return false;
 69
 70	if (insn_decode(&insn, (void *) regs->ip, len, INSN_MODE_KERN))
 71		return false;
 72	if (insn.length != len)
 73		return false;
 74
 75	if (insn.opcode.bytes[0] != 0x8b)
 76		return false;
 77	if (insn.opnd_bytes != sizeof(long))
 78		return false;
 79
 80	addr = (unsigned long) insn_get_addr_ref(&insn, regs);
 81	if (addr == ~0ul)
 82		return false;
 83
 84	offset = addr & mask;
 85	addr = addr & ~mask;
 86	if (fault_addr != addr + sizeof(long))
 87		return false;
 88
 89	reg = insn_get_modrm_reg_ptr(&insn, regs);
 90	if (!reg)
 91		return false;
 92
 93	*reg = *(unsigned long *)addr >> (offset * 8);
 94	return ex_handler_default(e, regs);
 95}
 96
 97static bool ex_handler_fault(const struct exception_table_entry *fixup,
 98			     struct pt_regs *regs, int trapnr)
 99{
100	regs->ax = trapnr;
101	return ex_handler_default(fixup, regs);
102}
103
104static bool ex_handler_sgx(const struct exception_table_entry *fixup,
105			   struct pt_regs *regs, int trapnr)
106{
107	regs->ax = trapnr | SGX_ENCLS_FAULT_FLAG;
108	return ex_handler_default(fixup, regs);
109}
110
111/*
112 * Handler for when we fail to restore a task's FPU state.  We should never get
113 * here because the FPU state of a task using the FPU (task->thread.fpu.state)
114 * should always be valid.  However, past bugs have allowed userspace to set
115 * reserved bits in the XSAVE area using PTRACE_SETREGSET or sys_rt_sigreturn().
116 * These caused XRSTOR to fail when switching to the task, leaking the FPU
117 * registers of the task previously executing on the CPU.  Mitigate this class
118 * of vulnerability by restoring from the initial state (essentially, zeroing
119 * out all the FPU registers) if we can't restore from the task's FPU state.
120 */
121static bool ex_handler_fprestore(const struct exception_table_entry *fixup,
122				 struct pt_regs *regs)
123{
124	regs->ip = ex_fixup_addr(fixup);
125
126	WARN_ONCE(1, "Bad FPU state detected at %pB, reinitializing FPU registers.",
127		  (void *)instruction_pointer(regs));
128
129	fpu_reset_from_exception_fixup();
130	return true;
131}
132
133static bool ex_handler_uaccess(const struct exception_table_entry *fixup,
134			       struct pt_regs *regs, int trapnr)
135{
136	WARN_ONCE(trapnr == X86_TRAP_GP, "General protection fault in user access. Non-canonical address?");
137	return ex_handler_default(fixup, regs);
138}
139
140static bool ex_handler_copy(const struct exception_table_entry *fixup,
141			    struct pt_regs *regs, int trapnr)
142{
143	WARN_ONCE(trapnr == X86_TRAP_GP, "General protection fault in user access. Non-canonical address?");
144	return ex_handler_fault(fixup, regs, trapnr);
145}
146
147static bool ex_handler_msr(const struct exception_table_entry *fixup,
148			   struct pt_regs *regs, bool wrmsr, bool safe, int reg)
149{
150	if (__ONCE_LITE_IF(!safe && wrmsr)) {
151		pr_warn("unchecked MSR access error: WRMSR to 0x%x (tried to write 0x%08x%08x) at rIP: 0x%lx (%pS)\n",
152			(unsigned int)regs->cx, (unsigned int)regs->dx,
153			(unsigned int)regs->ax,  regs->ip, (void *)regs->ip);
154		show_stack_regs(regs);
155	}
156
157	if (__ONCE_LITE_IF(!safe && !wrmsr)) {
158		pr_warn("unchecked MSR access error: RDMSR from 0x%x at rIP: 0x%lx (%pS)\n",
159			(unsigned int)regs->cx, regs->ip, (void *)regs->ip);
160		show_stack_regs(regs);
161	}
162
163	if (!wrmsr) {
164		/* Pretend that the read succeeded and returned 0. */
165		regs->ax = 0;
166		regs->dx = 0;
167	}
168
169	if (safe)
170		*pt_regs_nr(regs, reg) = -EIO;
171
172	return ex_handler_default(fixup, regs);
173}
174
175static bool ex_handler_clear_fs(const struct exception_table_entry *fixup,
176				struct pt_regs *regs)
177{
178	if (static_cpu_has(X86_BUG_NULL_SEG))
179		asm volatile ("mov %0, %%fs" : : "rm" (__USER_DS));
180	asm volatile ("mov %0, %%fs" : : "rm" (0));
181	return ex_handler_default(fixup, regs);
182}
183
184static bool ex_handler_imm_reg(const struct exception_table_entry *fixup,
185			       struct pt_regs *regs, int reg, int imm)
186{
187	*pt_regs_nr(regs, reg) = (long)imm;
188	return ex_handler_default(fixup, regs);
189}
190
191static bool ex_handler_ucopy_len(const struct exception_table_entry *fixup,
192				  struct pt_regs *regs, int trapnr, int reg, int imm)
193{
194	regs->cx = imm * regs->cx + *pt_regs_nr(regs, reg);
195	return ex_handler_uaccess(fixup, regs, trapnr);
196}
197
198int ex_get_fixup_type(unsigned long ip)
199{
200	const struct exception_table_entry *e = search_exception_tables(ip);
201
202	return e ? FIELD_GET(EX_DATA_TYPE_MASK, e->data) : EX_TYPE_NONE;
203}
204
205int fixup_exception(struct pt_regs *regs, int trapnr, unsigned long error_code,
206		    unsigned long fault_addr)
207{
208	const struct exception_table_entry *e;
209	int type, reg, imm;
210
211#ifdef CONFIG_PNPBIOS
212	if (unlikely(SEGMENT_IS_PNP_CODE(regs->cs))) {
213		extern u32 pnp_bios_fault_eip, pnp_bios_fault_esp;
214		extern u32 pnp_bios_is_utter_crap;
215		pnp_bios_is_utter_crap = 1;
216		printk(KERN_CRIT "PNPBIOS fault.. attempting recovery.\n");
217		__asm__ volatile(
218			"movl %0, %%esp\n\t"
219			"jmp *%1\n\t"
220			: : "g" (pnp_bios_fault_esp), "g" (pnp_bios_fault_eip));
221		panic("do_trap: can't hit this");
222	}
223#endif
224
225	e = search_exception_tables(regs->ip);
226	if (!e)
227		return 0;
228
229	type = FIELD_GET(EX_DATA_TYPE_MASK, e->data);
230	reg  = FIELD_GET(EX_DATA_REG_MASK,  e->data);
231	imm  = FIELD_GET(EX_DATA_IMM_MASK,  e->data);
232
233	switch (type) {
234	case EX_TYPE_DEFAULT:
235	case EX_TYPE_DEFAULT_MCE_SAFE:
236		return ex_handler_default(e, regs);
237	case EX_TYPE_FAULT:
238	case EX_TYPE_FAULT_MCE_SAFE:
239		return ex_handler_fault(e, regs, trapnr);
240	case EX_TYPE_UACCESS:
241		return ex_handler_uaccess(e, regs, trapnr);
242	case EX_TYPE_COPY:
243		return ex_handler_copy(e, regs, trapnr);
244	case EX_TYPE_CLEAR_FS:
245		return ex_handler_clear_fs(e, regs);
246	case EX_TYPE_FPU_RESTORE:
247		return ex_handler_fprestore(e, regs);
248	case EX_TYPE_BPF:
249		return ex_handler_bpf(e, regs);
250	case EX_TYPE_WRMSR:
251		return ex_handler_msr(e, regs, true, false, reg);
252	case EX_TYPE_RDMSR:
253		return ex_handler_msr(e, regs, false, false, reg);
254	case EX_TYPE_WRMSR_SAFE:
255		return ex_handler_msr(e, regs, true, true, reg);
256	case EX_TYPE_RDMSR_SAFE:
257		return ex_handler_msr(e, regs, false, true, reg);
258	case EX_TYPE_WRMSR_IN_MCE:
259		ex_handler_msr_mce(regs, true);
260		break;
261	case EX_TYPE_RDMSR_IN_MCE:
262		ex_handler_msr_mce(regs, false);
263		break;
264	case EX_TYPE_POP_REG:
265		regs->sp += sizeof(long);
266		fallthrough;
267	case EX_TYPE_IMM_REG:
268		return ex_handler_imm_reg(e, regs, reg, imm);
269	case EX_TYPE_FAULT_SGX:
270		return ex_handler_sgx(e, regs, trapnr);
271	case EX_TYPE_UCOPY_LEN:
272		return ex_handler_ucopy_len(e, regs, trapnr, reg, imm);
273	case EX_TYPE_ZEROPAD:
274		return ex_handler_zeropad(e, regs, fault_addr);
275	}
276	BUG();
277}
278
279extern unsigned int early_recursion_flag;
 
280
281/* Restricted version used during very early boot */
282void __init early_fixup_exception(struct pt_regs *regs, int trapnr)
283{
284	/* Ignore early NMIs. */
285	if (trapnr == X86_TRAP_NMI)
286		return;
287
288	if (early_recursion_flag > 2)
289		goto halt_loop;
 
 
 
 
 
 
290
291	/*
292	 * Old CPUs leave the high bits of CS on the stack
293	 * undefined.  I'm not sure which CPUs do this, but at least
294	 * the 486 DX works this way.
295	 * Xen pv domains are not using the default __KERNEL_CS.
296	 */
297	if (!xen_pv_domain() && regs->cs != __KERNEL_CS)
298		goto fail;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
299
300	/*
301	 * The full exception fixup machinery is available as soon as
302	 * the early IDT is loaded.  This means that it is the
303	 * responsibility of extable users to either function correctly
304	 * when handlers are invoked early or to simply avoid causing
305	 * exceptions before they're ready to handle them.
306	 *
307	 * This is better than filtering which handlers can be used,
308	 * because refusing to call a handler here is guaranteed to
309	 * result in a hard-to-debug panic.
310	 *
311	 * Keep in mind that not all vectors actually get here.  Early
312	 * page faults, for example, are special.
313	 */
314	if (fixup_exception(regs, trapnr, regs->orig_ax, 0))
315		return;
316
317	if (trapnr == X86_TRAP_UD) {
318		if (report_bug(regs->ip, regs) == BUG_TRAP_TYPE_WARN) {
319			/* Skip the ud2. */
320			regs->ip += LEN_UD2;
321			return;
322		}
 
 
 
 
 
 
 
 
323
324		/*
325		 * If this was a BUG and report_bug returns or if this
326		 * was just a normal #UD, we want to continue onward and
327		 * crash.
328		 */
 
 
 
 
 
329	}
 
330
331fail:
332	early_printk("PANIC: early exception 0x%02x IP %lx:%lx error %lx cr2 0x%lx\n",
333		     (unsigned)trapnr, (unsigned long)regs->cs, regs->ip,
334		     regs->orig_ax, read_cr2());
335
336	show_regs(regs);
337
338halt_loop:
339	while (true)
340		halt();
 
 
 
 
 
 
 
341}
v3.5.6
  1#include <linux/module.h>
  2#include <linux/spinlock.h>
  3#include <linux/sort.h>
  4#include <asm/uaccess.h>
 
 
 
 
 
 
 
 
 
  5
  6static inline unsigned long
  7ex_insn_addr(const struct exception_table_entry *x)
  8{
  9	return (unsigned long)&x->insn + x->insn;
 
 
 
 
 
 
 10}
 
 11static inline unsigned long
 12ex_fixup_addr(const struct exception_table_entry *x)
 13{
 14	return (unsigned long)&x->fixup + x->fixup;
 15}
 16
 17int fixup_exception(struct pt_regs *regs)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 18{
 19	const struct exception_table_entry *fixup;
 20	unsigned long new_ip;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 21
 22#ifdef CONFIG_PNPBIOS
 23	if (unlikely(SEGMENT_IS_PNP_CODE(regs->cs))) {
 24		extern u32 pnp_bios_fault_eip, pnp_bios_fault_esp;
 25		extern u32 pnp_bios_is_utter_crap;
 26		pnp_bios_is_utter_crap = 1;
 27		printk(KERN_CRIT "PNPBIOS fault.. attempting recovery.\n");
 28		__asm__ volatile(
 29			"movl %0, %%esp\n\t"
 30			"jmp *%1\n\t"
 31			: : "g" (pnp_bios_fault_esp), "g" (pnp_bios_fault_eip));
 32		panic("do_trap: can't hit this");
 33	}
 34#endif
 35
 36	fixup = search_exception_tables(regs->ip);
 37	if (fixup) {
 38		new_ip = ex_fixup_addr(fixup);
 39
 40		if (fixup->fixup - fixup->insn >= 0x7ffffff0 - 4) {
 41			/* Special hack for uaccess_err */
 42			current_thread_info()->uaccess_err = 1;
 43			new_ip -= 0x7ffffff0;
 44		}
 45		regs->ip = new_ip;
 46		return 1;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 47	}
 
 
 48
 49	return 0;
 50}
 51
 52/* Restricted version used during very early boot */
 53int __init early_fixup_exception(unsigned long *ip)
 54{
 55	const struct exception_table_entry *fixup;
 56	unsigned long new_ip;
 
 57
 58	fixup = search_exception_tables(*ip);
 59	if (fixup) {
 60		new_ip = ex_fixup_addr(fixup);
 61
 62		if (fixup->fixup - fixup->insn >= 0x7ffffff0 - 4) {
 63			/* uaccess handling not supported during early boot */
 64			return 0;
 65		}
 66
 67		*ip = new_ip;
 68		return 1;
 69	}
 70
 71	return 0;
 72}
 73
 74/*
 75 * Search one exception table for an entry corresponding to the
 76 * given instruction address, and return the address of the entry,
 77 * or NULL if none is found.
 78 * We use a binary search, and thus we assume that the table is
 79 * already sorted.
 80 */
 81const struct exception_table_entry *
 82search_extable(const struct exception_table_entry *first,
 83	       const struct exception_table_entry *last,
 84	       unsigned long value)
 85{
 86	while (first <= last) {
 87		const struct exception_table_entry *mid;
 88		unsigned long addr;
 89
 90		mid = ((last - first) >> 1) + first;
 91		addr = ex_insn_addr(mid);
 92		if (addr < value)
 93			first = mid + 1;
 94		else if (addr > value)
 95			last = mid - 1;
 96		else
 97			return mid;
 98        }
 99        return NULL;
100}
101
102/*
103 * The exception table needs to be sorted so that the binary
104 * search that we use to find entries in it works properly.
105 * This is used both for the kernel exception table and for
106 * the exception tables of modules that get loaded.
107 *
108 */
109static int cmp_ex(const void *a, const void *b)
110{
111	const struct exception_table_entry *x = a, *y = b;
112
113	/*
114	 * This value will always end up fittin in an int, because on
115	 * both i386 and x86-64 the kernel symbol-reachable address
116	 * space is < 2 GiB.
 
 
 
 
 
 
117	 *
118	 * This compare is only valid after normalization.
 
119	 */
120	return x->insn - y->insn;
121}
122
123void sort_extable(struct exception_table_entry *start,
124		  struct exception_table_entry *finish)
125{
126	struct exception_table_entry *p;
127	int i;
128
129	/* Convert all entries to being relative to the start of the section */
130	i = 0;
131	for (p = start; p < finish; p++) {
132		p->insn += i;
133		i += 4;
134		p->fixup += i;
135		i += 4;
136	}
137
138	sort(start, finish - start, sizeof(struct exception_table_entry),
139	     cmp_ex, NULL);
140
141	/* Denormalize all entries */
142	i = 0;
143	for (p = start; p < finish; p++) {
144		p->insn -= i;
145		i += 4;
146		p->fixup -= i;
147		i += 4;
148	}
149}
150
151#ifdef CONFIG_MODULES
152/*
153 * If the exception table is sorted, any referring to the module init
154 * will be at the beginning or the end.
155 */
156void trim_init_extable(struct module *m)
157{
158	/*trim the beginning*/
159	while (m->num_exentries &&
160	       within_module_init(ex_insn_addr(&m->extable[0]), m)) {
161		m->extable++;
162		m->num_exentries--;
163	}
164	/*trim the end*/
165	while (m->num_exentries &&
166	       within_module_init(ex_insn_addr(&m->extable[m->num_exentries-1]), m))
167		m->num_exentries--;
168}
169#endif /* CONFIG_MODULES */