6521134057e8f9ef34dbfaf8a0a4e46672a32d3c
[linux-2.6-block.git] / arch / x86 / mm / extable.c
1 #include <linux/extable.h>
2 #include <linux/uaccess.h>
3 #include <linux/sched/debug.h>
4 #include <xen/xen.h>
5
6 #include <asm/fpu/internal.h>
7 #include <asm/traps.h>
8 #include <asm/kdebug.h>
9
10 typedef bool (*ex_handler_t)(const struct exception_table_entry *,
11                             struct pt_regs *, int, unsigned long,
12                             unsigned long);
13
14 static inline unsigned long
15 ex_fixup_addr(const struct exception_table_entry *x)
16 {
17         return (unsigned long)&x->fixup + x->fixup;
18 }
19 static inline ex_handler_t
20 ex_fixup_handler(const struct exception_table_entry *x)
21 {
22         return (ex_handler_t)((unsigned long)&x->handler + x->handler);
23 }
24
25 __visible bool ex_handler_default(const struct exception_table_entry *fixup,
26                                   struct pt_regs *regs, int trapnr,
27                                   unsigned long error_code,
28                                   unsigned long fault_addr)
29 {
30         regs->ip = ex_fixup_addr(fixup);
31         return true;
32 }
33 EXPORT_SYMBOL(ex_handler_default);
34
35 __visible bool ex_handler_fault(const struct exception_table_entry *fixup,
36                                 struct pt_regs *regs, int trapnr,
37                                 unsigned long error_code,
38                                 unsigned long fault_addr)
39 {
40         regs->ip = ex_fixup_addr(fixup);
41         regs->ax = trapnr;
42         return true;
43 }
44 EXPORT_SYMBOL_GPL(ex_handler_fault);
45
46 /*
47  * Handler for UD0 exception following a failed test against the
48  * result of a refcount inc/dec/add/sub.
49  */
50 __visible bool ex_handler_refcount(const struct exception_table_entry *fixup,
51                                    struct pt_regs *regs, int trapnr,
52                                    unsigned long error_code,
53                                    unsigned long fault_addr)
54 {
55         /* First unconditionally saturate the refcount. */
56         *(int *)regs->cx = INT_MIN / 2;
57
58         /*
59          * Strictly speaking, this reports the fixup destination, not
60          * the fault location, and not the actually overflowing
61          * instruction, which is the instruction before the "js", but
62          * since that instruction could be a variety of lengths, just
63          * report the location after the overflow, which should be close
64          * enough for finding the overflow, as it's at least back in
65          * the function, having returned from .text.unlikely.
66          */
67         regs->ip = ex_fixup_addr(fixup);
68
69         /*
70          * This function has been called because either a negative refcount
71          * value was seen by any of the refcount functions, or a zero
72          * refcount value was seen by refcount_dec().
73          *
74          * If we crossed from INT_MAX to INT_MIN, OF (Overflow Flag: result
75          * wrapped around) will be set. Additionally, seeing the refcount
76          * reach 0 will set ZF (Zero Flag: result was zero). In each of
77          * these cases we want a report, since it's a boundary condition.
78          * The SF case is not reported since it indicates post-boundary
79          * manipulations below zero or above INT_MAX. And if none of the
80          * flags are set, something has gone very wrong, so report it.
81          */
82         if (regs->flags & (X86_EFLAGS_OF | X86_EFLAGS_ZF)) {
83                 bool zero = regs->flags & X86_EFLAGS_ZF;
84
85                 refcount_error_report(regs, zero ? "hit zero" : "overflow");
86         } else if ((regs->flags & X86_EFLAGS_SF) == 0) {
87                 /* Report if none of OF, ZF, nor SF are set. */
88                 refcount_error_report(regs, "unexpected saturation");
89         }
90
91         return true;
92 }
93 EXPORT_SYMBOL(ex_handler_refcount);
94
95 /*
96  * Handler for when we fail to restore a task's FPU state.  We should never get
97  * here because the FPU state of a task using the FPU (task->thread.fpu.state)
98  * should always be valid.  However, past bugs have allowed userspace to set
99  * reserved bits in the XSAVE area using PTRACE_SETREGSET or sys_rt_sigreturn().
100  * These caused XRSTOR to fail when switching to the task, leaking the FPU
101  * registers of the task previously executing on the CPU.  Mitigate this class
102  * of vulnerability by restoring from the initial state (essentially, zeroing
103  * out all the FPU registers) if we can't restore from the task's FPU state.
104  */
105 __visible bool ex_handler_fprestore(const struct exception_table_entry *fixup,
106                                     struct pt_regs *regs, int trapnr,
107                                     unsigned long error_code,
108                                     unsigned long fault_addr)
109 {
110         regs->ip = ex_fixup_addr(fixup);
111
112         WARN_ONCE(1, "Bad FPU state detected at %pB, reinitializing FPU registers.",
113                   (void *)instruction_pointer(regs));
114
115         __copy_kernel_to_fpregs(&init_fpstate, -1);
116         return true;
117 }
118 EXPORT_SYMBOL_GPL(ex_handler_fprestore);
119
120 /* Helper to check whether a uaccess fault indicates a kernel bug. */
121 static bool bogus_uaccess(struct pt_regs *regs, int trapnr,
122                           unsigned long fault_addr)
123 {
124         /* This is the normal case: #PF with a fault address in userspace. */
125         if (trapnr == X86_TRAP_PF && fault_addr < TASK_SIZE_MAX)
126                 return false;
127
128         /*
129          * This code can be reached for machine checks, but only if the #MC
130          * handler has already decided that it looks like a candidate for fixup.
131          * This e.g. happens when attempting to access userspace memory which
132          * the CPU can't access because of uncorrectable bad memory.
133          */
134         if (trapnr == X86_TRAP_MC)
135                 return false;
136
137         /*
138          * There are two remaining exception types we might encounter here:
139          *  - #PF for faulting accesses to kernel addresses
140          *  - #GP for faulting accesses to noncanonical addresses
141          * Complain about anything else.
142          */
143         if (trapnr != X86_TRAP_PF && trapnr != X86_TRAP_GP) {
144                 WARN(1, "unexpected trap %d in uaccess\n", trapnr);
145                 return false;
146         }
147
148         /*
149          * This is a faulting memory access in kernel space, on a kernel
150          * address, in a usercopy function. This can e.g. be caused by improper
151          * use of helpers like __put_user and by improper attempts to access
152          * userspace addresses in KERNEL_DS regions.
153          * The one (semi-)legitimate exception are probe_kernel_{read,write}(),
154          * which can be invoked from places like kgdb, /dev/mem (for reading)
155          * and privileged BPF code (for reading).
156          * The probe_kernel_*() functions set the kernel_uaccess_faults_ok flag
157          * to tell us that faulting on kernel addresses, and even noncanonical
158          * addresses, in a userspace accessor does not necessarily imply a
159          * kernel bug, root might just be doing weird stuff.
160          */
161         if (current->kernel_uaccess_faults_ok)
162                 return false;
163
164         /* This is bad. Refuse the fixup so that we go into die(). */
165         if (trapnr == X86_TRAP_PF) {
166                 pr_emerg("BUG: pagefault on kernel address 0x%lx in non-whitelisted uaccess\n",
167                          fault_addr);
168         } else {
169                 pr_emerg("BUG: GPF in non-whitelisted uaccess (non-canonical address?)\n");
170         }
171         return true;
172 }
173
174 __visible bool ex_handler_uaccess(const struct exception_table_entry *fixup,
175                                   struct pt_regs *regs, int trapnr,
176                                   unsigned long error_code,
177                                   unsigned long fault_addr)
178 {
179         if (bogus_uaccess(regs, trapnr, fault_addr))
180                 return false;
181         regs->ip = ex_fixup_addr(fixup);
182         return true;
183 }
184 EXPORT_SYMBOL(ex_handler_uaccess);
185
186 __visible bool ex_handler_ext(const struct exception_table_entry *fixup,
187                               struct pt_regs *regs, int trapnr,
188                               unsigned long error_code,
189                               unsigned long fault_addr)
190 {
191         if (bogus_uaccess(regs, trapnr, fault_addr))
192                 return false;
193         /* Special hack for uaccess_err */
194         current->thread.uaccess_err = 1;
195         regs->ip = ex_fixup_addr(fixup);
196         return true;
197 }
198 EXPORT_SYMBOL(ex_handler_ext);
199
200 __visible bool ex_handler_rdmsr_unsafe(const struct exception_table_entry *fixup,
201                                        struct pt_regs *regs, int trapnr,
202                                        unsigned long error_code,
203                                        unsigned long fault_addr)
204 {
205         if (pr_warn_once("unchecked MSR access error: RDMSR from 0x%x at rIP: 0x%lx (%pF)\n",
206                          (unsigned int)regs->cx, regs->ip, (void *)regs->ip))
207                 show_stack_regs(regs);
208
209         /* Pretend that the read succeeded and returned 0. */
210         regs->ip = ex_fixup_addr(fixup);
211         regs->ax = 0;
212         regs->dx = 0;
213         return true;
214 }
215 EXPORT_SYMBOL(ex_handler_rdmsr_unsafe);
216
217 __visible bool ex_handler_wrmsr_unsafe(const struct exception_table_entry *fixup,
218                                        struct pt_regs *regs, int trapnr,
219                                        unsigned long error_code,
220                                        unsigned long fault_addr)
221 {
222         if (pr_warn_once("unchecked MSR access error: WRMSR to 0x%x (tried to write 0x%08x%08x) at rIP: 0x%lx (%pF)\n",
223                          (unsigned int)regs->cx, (unsigned int)regs->dx,
224                          (unsigned int)regs->ax,  regs->ip, (void *)regs->ip))
225                 show_stack_regs(regs);
226
227         /* Pretend that the write succeeded. */
228         regs->ip = ex_fixup_addr(fixup);
229         return true;
230 }
231 EXPORT_SYMBOL(ex_handler_wrmsr_unsafe);
232
233 __visible bool ex_handler_clear_fs(const struct exception_table_entry *fixup,
234                                    struct pt_regs *regs, int trapnr,
235                                    unsigned long error_code,
236                                    unsigned long fault_addr)
237 {
238         if (static_cpu_has(X86_BUG_NULL_SEG))
239                 asm volatile ("mov %0, %%fs" : : "rm" (__USER_DS));
240         asm volatile ("mov %0, %%fs" : : "rm" (0));
241         return ex_handler_default(fixup, regs, trapnr, error_code, fault_addr);
242 }
243 EXPORT_SYMBOL(ex_handler_clear_fs);
244
245 __visible bool ex_has_fault_handler(unsigned long ip)
246 {
247         const struct exception_table_entry *e;
248         ex_handler_t handler;
249
250         e = search_exception_tables(ip);
251         if (!e)
252                 return false;
253         handler = ex_fixup_handler(e);
254
255         return handler == ex_handler_fault;
256 }
257
258 int fixup_exception(struct pt_regs *regs, int trapnr, unsigned long error_code,
259                     unsigned long fault_addr)
260 {
261         const struct exception_table_entry *e;
262         ex_handler_t handler;
263
264 #ifdef CONFIG_PNPBIOS
265         if (unlikely(SEGMENT_IS_PNP_CODE(regs->cs))) {
266                 extern u32 pnp_bios_fault_eip, pnp_bios_fault_esp;
267                 extern u32 pnp_bios_is_utter_crap;
268                 pnp_bios_is_utter_crap = 1;
269                 printk(KERN_CRIT "PNPBIOS fault.. attempting recovery.\n");
270                 __asm__ volatile(
271                         "movl %0, %%esp\n\t"
272                         "jmp *%1\n\t"
273                         : : "g" (pnp_bios_fault_esp), "g" (pnp_bios_fault_eip));
274                 panic("do_trap: can't hit this");
275         }
276 #endif
277
278         e = search_exception_tables(regs->ip);
279         if (!e)
280                 return 0;
281
282         handler = ex_fixup_handler(e);
283         return handler(e, regs, trapnr, error_code, fault_addr);
284 }
285
286 extern unsigned int early_recursion_flag;
287
288 /* Restricted version used during very early boot */
289 void __init early_fixup_exception(struct pt_regs *regs, int trapnr)
290 {
291         /* Ignore early NMIs. */
292         if (trapnr == X86_TRAP_NMI)
293                 return;
294
295         if (early_recursion_flag > 2)
296                 goto halt_loop;
297
298         /*
299          * Old CPUs leave the high bits of CS on the stack
300          * undefined.  I'm not sure which CPUs do this, but at least
301          * the 486 DX works this way.
302          * Xen pv domains are not using the default __KERNEL_CS.
303          */
304         if (!xen_pv_domain() && regs->cs != __KERNEL_CS)
305                 goto fail;
306
307         /*
308          * The full exception fixup machinery is available as soon as
309          * the early IDT is loaded.  This means that it is the
310          * responsibility of extable users to either function correctly
311          * when handlers are invoked early or to simply avoid causing
312          * exceptions before they're ready to handle them.
313          *
314          * This is better than filtering which handlers can be used,
315          * because refusing to call a handler here is guaranteed to
316          * result in a hard-to-debug panic.
317          *
318          * Keep in mind that not all vectors actually get here.  Early
319          * page faults, for example, are special.
320          */
321         if (fixup_exception(regs, trapnr, regs->orig_ax, 0))
322                 return;
323
324         if (fixup_bug(regs, trapnr))
325                 return;
326
327 fail:
328         early_printk("PANIC: early exception 0x%02x IP %lx:%lx error %lx cr2 0x%lx\n",
329                      (unsigned)trapnr, (unsigned long)regs->cs, regs->ip,
330                      regs->orig_ax, read_cr2());
331
332         show_regs(regs);
333
334 halt_loop:
335         while (true)
336                 halt();
337 }