extable.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #include <linux/extable.h>
  2. #include <linux/uaccess.h>
  3. #include <linux/sched/debug.h>
  4. #include <xen/xen.h>
  5. #include <asm/fpu/internal.h>
  6. #include <asm/traps.h>
  7. #include <asm/kdebug.h>
  8. typedef bool (*ex_handler_t)(const struct exception_table_entry *,
  9. struct pt_regs *, int);
  10. static inline unsigned long
  11. ex_fixup_addr(const struct exception_table_entry *x)
  12. {
  13. return (unsigned long)&x->fixup + x->fixup;
  14. }
  15. static inline ex_handler_t
  16. ex_fixup_handler(const struct exception_table_entry *x)
  17. {
  18. return (ex_handler_t)((unsigned long)&x->handler + x->handler);
  19. }
  20. __visible bool ex_handler_default(const struct exception_table_entry *fixup,
  21. struct pt_regs *regs, int trapnr)
  22. {
  23. regs->ip = ex_fixup_addr(fixup);
  24. return true;
  25. }
  26. EXPORT_SYMBOL(ex_handler_default);
  27. __visible bool ex_handler_fault(const struct exception_table_entry *fixup,
  28. struct pt_regs *regs, int trapnr)
  29. {
  30. regs->ip = ex_fixup_addr(fixup);
  31. regs->ax = trapnr;
  32. return true;
  33. }
  34. EXPORT_SYMBOL_GPL(ex_handler_fault);
  35. /*
  36. * Handler for UD0 exception following a failed test against the
  37. * result of a refcount inc/dec/add/sub.
  38. */
  39. __visible bool ex_handler_refcount(const struct exception_table_entry *fixup,
  40. struct pt_regs *regs, int trapnr)
  41. {
  42. /* First unconditionally saturate the refcount. */
  43. *(int *)regs->cx = INT_MIN / 2;
  44. /*
  45. * Strictly speaking, this reports the fixup destination, not
  46. * the fault location, and not the actually overflowing
  47. * instruction, which is the instruction before the "js", but
  48. * since that instruction could be a variety of lengths, just
  49. * report the location after the overflow, which should be close
  50. * enough for finding the overflow, as it's at least back in
  51. * the function, having returned from .text.unlikely.
  52. */
  53. regs->ip = ex_fixup_addr(fixup);
  54. /*
  55. * This function has been called because either a negative refcount
  56. * value was seen by any of the refcount functions, or a zero
  57. * refcount value was seen by refcount_dec().
  58. *
  59. * If we crossed from INT_MAX to INT_MIN, OF (Overflow Flag: result
  60. * wrapped around) will be set. Additionally, seeing the refcount
  61. * reach 0 will set ZF (Zero Flag: result was zero). In each of
  62. * these cases we want a report, since it's a boundary condition.
  63. * The SF case is not reported since it indicates post-boundary
  64. * manipulations below zero or above INT_MAX. And if none of the
  65. * flags are set, something has gone very wrong, so report it.
  66. */
  67. if (regs->flags & (X86_EFLAGS_OF | X86_EFLAGS_ZF)) {
  68. bool zero = regs->flags & X86_EFLAGS_ZF;
  69. refcount_error_report(regs, zero ? "hit zero" : "overflow");
  70. } else if ((regs->flags & X86_EFLAGS_SF) == 0) {
  71. /* Report if none of OF, ZF, nor SF are set. */
  72. refcount_error_report(regs, "unexpected saturation");
  73. }
  74. return true;
  75. }
  76. EXPORT_SYMBOL(ex_handler_refcount);
  77. /*
  78. * Handler for when we fail to restore a task's FPU state. We should never get
  79. * here because the FPU state of a task using the FPU (task->thread.fpu.state)
  80. * should always be valid. However, past bugs have allowed userspace to set
  81. * reserved bits in the XSAVE area using PTRACE_SETREGSET or sys_rt_sigreturn().
  82. * These caused XRSTOR to fail when switching to the task, leaking the FPU
  83. * registers of the task previously executing on the CPU. Mitigate this class
  84. * of vulnerability by restoring from the initial state (essentially, zeroing
  85. * out all the FPU registers) if we can't restore from the task's FPU state.
  86. */
  87. __visible bool ex_handler_fprestore(const struct exception_table_entry *fixup,
  88. struct pt_regs *regs, int trapnr)
  89. {
  90. regs->ip = ex_fixup_addr(fixup);
  91. WARN_ONCE(1, "Bad FPU state detected at %pB, reinitializing FPU registers.",
  92. (void *)instruction_pointer(regs));
  93. __copy_kernel_to_fpregs(&init_fpstate, -1);
  94. return true;
  95. }
  96. EXPORT_SYMBOL_GPL(ex_handler_fprestore);
  97. __visible bool ex_handler_ext(const struct exception_table_entry *fixup,
  98. struct pt_regs *regs, int trapnr)
  99. {
  100. /* Special hack for uaccess_err */
  101. current->thread.uaccess_err = 1;
  102. regs->ip = ex_fixup_addr(fixup);
  103. return true;
  104. }
  105. EXPORT_SYMBOL(ex_handler_ext);
  106. __visible bool ex_handler_rdmsr_unsafe(const struct exception_table_entry *fixup,
  107. struct pt_regs *regs, int trapnr)
  108. {
  109. if (pr_warn_once("unchecked MSR access error: RDMSR from 0x%x at rIP: 0x%lx (%pF)\n",
  110. (unsigned int)regs->cx, regs->ip, (void *)regs->ip))
  111. show_stack_regs(regs);
  112. /* Pretend that the read succeeded and returned 0. */
  113. regs->ip = ex_fixup_addr(fixup);
  114. regs->ax = 0;
  115. regs->dx = 0;
  116. return true;
  117. }
  118. EXPORT_SYMBOL(ex_handler_rdmsr_unsafe);
  119. __visible bool ex_handler_wrmsr_unsafe(const struct exception_table_entry *fixup,
  120. struct pt_regs *regs, int trapnr)
  121. {
  122. if (pr_warn_once("unchecked MSR access error: WRMSR to 0x%x (tried to write 0x%08x%08x) at rIP: 0x%lx (%pF)\n",
  123. (unsigned int)regs->cx, (unsigned int)regs->dx,
  124. (unsigned int)regs->ax, regs->ip, (void *)regs->ip))
  125. show_stack_regs(regs);
  126. /* Pretend that the write succeeded. */
  127. regs->ip = ex_fixup_addr(fixup);
  128. return true;
  129. }
  130. EXPORT_SYMBOL(ex_handler_wrmsr_unsafe);
  131. __visible bool ex_handler_clear_fs(const struct exception_table_entry *fixup,
  132. struct pt_regs *regs, int trapnr)
  133. {
  134. if (static_cpu_has(X86_BUG_NULL_SEG))
  135. asm volatile ("mov %0, %%fs" : : "rm" (__USER_DS));
  136. asm volatile ("mov %0, %%fs" : : "rm" (0));
  137. return ex_handler_default(fixup, regs, trapnr);
  138. }
  139. EXPORT_SYMBOL(ex_handler_clear_fs);
  140. __visible bool ex_has_fault_handler(unsigned long ip)
  141. {
  142. const struct exception_table_entry *e;
  143. ex_handler_t handler;
  144. e = search_exception_tables(ip);
  145. if (!e)
  146. return false;
  147. handler = ex_fixup_handler(e);
  148. return handler == ex_handler_fault;
  149. }
  150. int fixup_exception(struct pt_regs *regs, int trapnr)
  151. {
  152. const struct exception_table_entry *e;
  153. ex_handler_t handler;
  154. #ifdef CONFIG_PNPBIOS
  155. if (unlikely(SEGMENT_IS_PNP_CODE(regs->cs))) {
  156. extern u32 pnp_bios_fault_eip, pnp_bios_fault_esp;
  157. extern u32 pnp_bios_is_utter_crap;
  158. pnp_bios_is_utter_crap = 1;
  159. printk(KERN_CRIT "PNPBIOS fault.. attempting recovery.\n");
  160. __asm__ volatile(
  161. "movl %0, %%esp\n\t"
  162. "jmp *%1\n\t"
  163. : : "g" (pnp_bios_fault_esp), "g" (pnp_bios_fault_eip));
  164. panic("do_trap: can't hit this");
  165. }
  166. #endif
  167. e = search_exception_tables(regs->ip);
  168. if (!e)
  169. return 0;
  170. handler = ex_fixup_handler(e);
  171. return handler(e, regs, trapnr);
  172. }
  173. extern unsigned int early_recursion_flag;
  174. /* Restricted version used during very early boot */
  175. void __init early_fixup_exception(struct pt_regs *regs, int trapnr)
  176. {
  177. /* Ignore early NMIs. */
  178. if (trapnr == X86_TRAP_NMI)
  179. return;
  180. if (early_recursion_flag > 2)
  181. goto halt_loop;
  182. /*
  183. * Old CPUs leave the high bits of CS on the stack
  184. * undefined. I'm not sure which CPUs do this, but at least
  185. * the 486 DX works this way.
  186. * Xen pv domains are not using the default __KERNEL_CS.
  187. */
  188. if (!xen_pv_domain() && regs->cs != __KERNEL_CS)
  189. goto fail;
  190. /*
  191. * The full exception fixup machinery is available as soon as
  192. * the early IDT is loaded. This means that it is the
  193. * responsibility of extable users to either function correctly
  194. * when handlers are invoked early or to simply avoid causing
  195. * exceptions before they're ready to handle them.
  196. *
  197. * This is better than filtering which handlers can be used,
  198. * because refusing to call a handler here is guaranteed to
  199. * result in a hard-to-debug panic.
  200. *
  201. * Keep in mind that not all vectors actually get here. Early
  202. * fage faults, for example, are special.
  203. */
  204. if (fixup_exception(regs, trapnr))
  205. return;
  206. if (fixup_bug(regs, trapnr))
  207. return;
  208. fail:
  209. early_printk("PANIC: early exception 0x%02x IP %lx:%lx error %lx cr2 0x%lx\n",
  210. (unsigned)trapnr, (unsigned long)regs->cs, regs->ip,
  211. regs->orig_ax, read_cr2());
  212. show_regs(regs);
  213. halt_loop:
  214. while (true)
  215. halt();
  216. }