extable.c 9.7 KB

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