0032-x86-xen-Get-rid-of-paravirt-op-adjust_exception_fram.patch 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: Juergen Gross <[email protected]>
  3. Date: Thu, 31 Aug 2017 19:42:49 +0200
  4. Subject: [PATCH] x86/xen: Get rid of paravirt op adjust_exception_frame
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. CVE-2017-5754
  9. When running as Xen pv-guest the exception frame on the stack contains
  10. %r11 and %rcx additional to the other data pushed by the processor.
  11. Instead of having a paravirt op being called for each exception type
  12. prepend the Xen specific code to each exception entry. When running as
  13. Xen pv-guest just use the exception entry with prepended instructions,
  14. otherwise use the entry without the Xen specific code.
  15. [ tglx: Merged through tip to avoid ugly merge conflict ]
  16. Signed-off-by: Juergen Gross <[email protected]>
  17. Signed-off-by: Thomas Gleixner <[email protected]>
  18. Cc: [email protected]
  19. Cc: [email protected]
  20. Cc: [email protected]
  21. Link: http://lkml.kernel.org/r/[email protected]
  22. (backported from commit 5878d5d6fdef6447d73b0acc121ba445bef37f53)
  23. Signed-off-by: Andy Whitcroft <[email protected]>
  24. Signed-off-by: Kleber Sacilotto de Souza <[email protected]>
  25. (cherry picked from commit 9a6fb927deb3ebbe831741ca82081714637181a7)
  26. Signed-off-by: Fabian Grünbichler <[email protected]>
  27. ---
  28. arch/x86/include/asm/paravirt.h | 5 --
  29. arch/x86/include/asm/paravirt_types.h | 3 --
  30. arch/x86/include/asm/proto.h | 3 ++
  31. arch/x86/include/asm/traps.h | 28 ++++++++--
  32. arch/x86/xen/xen-ops.h | 1 -
  33. arch/x86/kernel/asm-offsets_64.c | 1 -
  34. arch/x86/kernel/paravirt.c | 3 --
  35. arch/x86/xen/enlighten_pv.c | 98 +++++++++++++++++++++++------------
  36. arch/x86/xen/irq.c | 3 --
  37. arch/x86/entry/entry_64.S | 23 ++------
  38. arch/x86/entry/entry_64_compat.S | 1 -
  39. arch/x86/xen/xen-asm_64.S | 41 +++++++++++++--
  40. 12 files changed, 133 insertions(+), 77 deletions(-)
  41. diff --git a/arch/x86/include/asm/paravirt.h b/arch/x86/include/asm/paravirt.h
  42. index 9ccac1926587..c25dd22f7c70 100644
  43. --- a/arch/x86/include/asm/paravirt.h
  44. +++ b/arch/x86/include/asm/paravirt.h
  45. @@ -960,11 +960,6 @@ extern void default_banner(void);
  46. #define GET_CR2_INTO_RAX \
  47. call PARA_INDIRECT(pv_mmu_ops+PV_MMU_read_cr2)
  48. -#define PARAVIRT_ADJUST_EXCEPTION_FRAME \
  49. - PARA_SITE(PARA_PATCH(pv_irq_ops, PV_IRQ_adjust_exception_frame), \
  50. - CLBR_NONE, \
  51. - call PARA_INDIRECT(pv_irq_ops+PV_IRQ_adjust_exception_frame))
  52. -
  53. #define USERGS_SYSRET64 \
  54. PARA_SITE(PARA_PATCH(pv_cpu_ops, PV_CPU_usergs_sysret64), \
  55. CLBR_NONE, \
  56. diff --git a/arch/x86/include/asm/paravirt_types.h b/arch/x86/include/asm/paravirt_types.h
  57. index 9ffc36bfe4cd..6b64fc6367f2 100644
  58. --- a/arch/x86/include/asm/paravirt_types.h
  59. +++ b/arch/x86/include/asm/paravirt_types.h
  60. @@ -196,9 +196,6 @@ struct pv_irq_ops {
  61. void (*safe_halt)(void);
  62. void (*halt)(void);
  63. -#ifdef CONFIG_X86_64
  64. - void (*adjust_exception_frame)(void);
  65. -#endif
  66. } __no_randomize_layout;
  67. struct pv_mmu_ops {
  68. diff --git a/arch/x86/include/asm/proto.h b/arch/x86/include/asm/proto.h
  69. index 8d3964fc5f91..b408b1886195 100644
  70. --- a/arch/x86/include/asm/proto.h
  71. +++ b/arch/x86/include/asm/proto.h
  72. @@ -24,6 +24,9 @@ void entry_SYSENTER_compat(void);
  73. void __end_entry_SYSENTER_compat(void);
  74. void entry_SYSCALL_compat(void);
  75. void entry_INT80_compat(void);
  76. +#if defined(CONFIG_X86_64) && defined(CONFIG_XEN_PV)
  77. +void xen_entry_INT80_compat(void);
  78. +#endif
  79. #endif
  80. void x86_configure_nx(void);
  81. diff --git a/arch/x86/include/asm/traps.h b/arch/x86/include/asm/traps.h
  82. index b4f322d6c95f..feb89dbe359d 100644
  83. --- a/arch/x86/include/asm/traps.h
  84. +++ b/arch/x86/include/asm/traps.h
  85. @@ -13,9 +13,6 @@ asmlinkage void divide_error(void);
  86. asmlinkage void debug(void);
  87. asmlinkage void nmi(void);
  88. asmlinkage void int3(void);
  89. -asmlinkage void xen_debug(void);
  90. -asmlinkage void xen_int3(void);
  91. -asmlinkage void xen_stack_segment(void);
  92. asmlinkage void overflow(void);
  93. asmlinkage void bounds(void);
  94. asmlinkage void invalid_op(void);
  95. @@ -56,6 +53,31 @@ asmlinkage void simd_coprocessor_error(void);
  96. #define trace_page_fault page_fault
  97. #endif
  98. +#if defined(CONFIG_X86_64) && defined(CONFIG_XEN_PV)
  99. +asmlinkage void xen_divide_error(void);
  100. +asmlinkage void xen_xendebug(void);
  101. +asmlinkage void xen_xenint3(void);
  102. +asmlinkage void xen_nmi(void);
  103. +asmlinkage void xen_overflow(void);
  104. +asmlinkage void xen_bounds(void);
  105. +asmlinkage void xen_invalid_op(void);
  106. +asmlinkage void xen_device_not_available(void);
  107. +asmlinkage void xen_double_fault(void);
  108. +asmlinkage void xen_coprocessor_segment_overrun(void);
  109. +asmlinkage void xen_invalid_TSS(void);
  110. +asmlinkage void xen_segment_not_present(void);
  111. +asmlinkage void xen_stack_segment(void);
  112. +asmlinkage void xen_general_protection(void);
  113. +asmlinkage void xen_page_fault(void);
  114. +asmlinkage void xen_spurious_interrupt_bug(void);
  115. +asmlinkage void xen_coprocessor_error(void);
  116. +asmlinkage void xen_alignment_check(void);
  117. +#ifdef CONFIG_X86_MCE
  118. +asmlinkage void xen_machine_check(void);
  119. +#endif /* CONFIG_X86_MCE */
  120. +asmlinkage void xen_simd_coprocessor_error(void);
  121. +#endif
  122. +
  123. dotraplinkage void do_divide_error(struct pt_regs *, long);
  124. dotraplinkage void do_debug(struct pt_regs *, long);
  125. dotraplinkage void do_nmi(struct pt_regs *, long);
  126. diff --git a/arch/x86/xen/xen-ops.h b/arch/x86/xen/xen-ops.h
  127. index 70301ac0d414..c8a6d224f7ed 100644
  128. --- a/arch/x86/xen/xen-ops.h
  129. +++ b/arch/x86/xen/xen-ops.h
  130. @@ -138,7 +138,6 @@ __visible void xen_restore_fl_direct(unsigned long);
  131. __visible void xen_iret(void);
  132. __visible void xen_sysret32(void);
  133. __visible void xen_sysret64(void);
  134. -__visible void xen_adjust_exception_frame(void);
  135. extern int xen_panic_handler_init(void);
  136. diff --git a/arch/x86/kernel/asm-offsets_64.c b/arch/x86/kernel/asm-offsets_64.c
  137. index 99332f550c48..cf42206926af 100644
  138. --- a/arch/x86/kernel/asm-offsets_64.c
  139. +++ b/arch/x86/kernel/asm-offsets_64.c
  140. @@ -20,7 +20,6 @@ static char syscalls_ia32[] = {
  141. int main(void)
  142. {
  143. #ifdef CONFIG_PARAVIRT
  144. - OFFSET(PV_IRQ_adjust_exception_frame, pv_irq_ops, adjust_exception_frame);
  145. OFFSET(PV_CPU_usergs_sysret64, pv_cpu_ops, usergs_sysret64);
  146. OFFSET(PV_CPU_swapgs, pv_cpu_ops, swapgs);
  147. BLANK();
  148. diff --git a/arch/x86/kernel/paravirt.c b/arch/x86/kernel/paravirt.c
  149. index bc0a849589bb..a14df9eecfed 100644
  150. --- a/arch/x86/kernel/paravirt.c
  151. +++ b/arch/x86/kernel/paravirt.c
  152. @@ -319,9 +319,6 @@ __visible struct pv_irq_ops pv_irq_ops = {
  153. .irq_enable = __PV_IS_CALLEE_SAVE(native_irq_enable),
  154. .safe_halt = native_safe_halt,
  155. .halt = native_halt,
  156. -#ifdef CONFIG_X86_64
  157. - .adjust_exception_frame = paravirt_nop,
  158. -#endif
  159. };
  160. __visible struct pv_cpu_ops pv_cpu_ops = {
  161. diff --git a/arch/x86/xen/enlighten_pv.c b/arch/x86/xen/enlighten_pv.c
  162. index c76f5ff4d0d7..ae2a2e2d6362 100644
  163. --- a/arch/x86/xen/enlighten_pv.c
  164. +++ b/arch/x86/xen/enlighten_pv.c
  165. @@ -586,6 +586,70 @@ static void xen_write_ldt_entry(struct desc_struct *dt, int entrynum,
  166. preempt_enable();
  167. }
  168. +#ifdef CONFIG_X86_64
  169. +struct trap_array_entry {
  170. + void (*orig)(void);
  171. + void (*xen)(void);
  172. + bool ist_okay;
  173. +};
  174. +
  175. +static struct trap_array_entry trap_array[] = {
  176. + { debug, xen_xendebug, true },
  177. + { int3, xen_xenint3, true },
  178. + { double_fault, xen_double_fault, true },
  179. +#ifdef CONFIG_X86_MCE
  180. + { machine_check, xen_machine_check, true },
  181. +#endif
  182. + { nmi, xen_nmi, true },
  183. + { overflow, xen_overflow, false },
  184. +#ifdef CONFIG_IA32_EMULATION
  185. + { entry_INT80_compat, xen_entry_INT80_compat, false },
  186. +#endif
  187. + { page_fault, xen_page_fault, false },
  188. + { divide_error, xen_divide_error, false },
  189. + { bounds, xen_bounds, false },
  190. + { invalid_op, xen_invalid_op, false },
  191. + { device_not_available, xen_device_not_available, false },
  192. + { coprocessor_segment_overrun, xen_coprocessor_segment_overrun, false },
  193. + { invalid_TSS, xen_invalid_TSS, false },
  194. + { segment_not_present, xen_segment_not_present, false },
  195. + { stack_segment, xen_stack_segment, false },
  196. + { general_protection, xen_general_protection, false },
  197. + { spurious_interrupt_bug, xen_spurious_interrupt_bug, false },
  198. + { coprocessor_error, xen_coprocessor_error, false },
  199. + { alignment_check, xen_alignment_check, false },
  200. + { simd_coprocessor_error, xen_simd_coprocessor_error, false },
  201. +};
  202. +
  203. +static bool get_trap_addr(void **addr, unsigned int ist)
  204. +{
  205. + unsigned int nr;
  206. + bool ist_okay = false;
  207. +
  208. + /*
  209. + * Replace trap handler addresses by Xen specific ones.
  210. + * Check for known traps using IST and whitelist them.
  211. + * The debugger ones are the only ones we care about.
  212. + * Xen will handle faults like double_fault, * so we should never see
  213. + * them. Warn if there's an unexpected IST-using fault handler.
  214. + */
  215. + for (nr = 0; nr < ARRAY_SIZE(trap_array); nr++) {
  216. + struct trap_array_entry *entry = trap_array + nr;
  217. +
  218. + if (*addr == entry->orig) {
  219. + *addr = entry->xen;
  220. + ist_okay = entry->ist_okay;
  221. + break;
  222. + }
  223. + }
  224. +
  225. + if (WARN_ON(ist != 0 && !ist_okay))
  226. + return false;
  227. +
  228. + return true;
  229. +}
  230. +#endif
  231. +
  232. static int cvt_gate_to_trap(int vector, const gate_desc *val,
  233. struct trap_info *info)
  234. {
  235. @@ -598,40 +662,8 @@ static int cvt_gate_to_trap(int vector, const gate_desc *val,
  236. addr = gate_offset(val);
  237. #ifdef CONFIG_X86_64
  238. - /*
  239. - * Look for known traps using IST, and substitute them
  240. - * appropriately. The debugger ones are the only ones we care
  241. - * about. Xen will handle faults like double_fault,
  242. - * so we should never see them. Warn if
  243. - * there's an unexpected IST-using fault handler.
  244. - */
  245. - if (addr == (unsigned long)debug)
  246. - addr = (unsigned long)xen_debug;
  247. - else if (addr == (unsigned long)int3)
  248. - addr = (unsigned long)xen_int3;
  249. - else if (addr == (unsigned long)stack_segment)
  250. - addr = (unsigned long)xen_stack_segment;
  251. - else if (addr == (unsigned long)double_fault) {
  252. - /* Don't need to handle these */
  253. + if (!get_trap_addr((void **)&addr, val->bits.ist))
  254. return 0;
  255. -#ifdef CONFIG_X86_MCE
  256. - } else if (addr == (unsigned long)machine_check) {
  257. - /*
  258. - * when xen hypervisor inject vMCE to guest,
  259. - * use native mce handler to handle it
  260. - */
  261. - ;
  262. -#endif
  263. - } else if (addr == (unsigned long)nmi)
  264. - /*
  265. - * Use the native version as well.
  266. - */
  267. - ;
  268. - else {
  269. - /* Some other trap using IST? */
  270. - if (WARN_ON(val->bits.ist != 0))
  271. - return 0;
  272. - }
  273. #endif /* CONFIG_X86_64 */
  274. info->address = addr;
  275. diff --git a/arch/x86/xen/irq.c b/arch/x86/xen/irq.c
  276. index 33e92955e09d..d4eff5676cfa 100644
  277. --- a/arch/x86/xen/irq.c
  278. +++ b/arch/x86/xen/irq.c
  279. @@ -123,9 +123,6 @@ static const struct pv_irq_ops xen_irq_ops __initconst = {
  280. .safe_halt = xen_safe_halt,
  281. .halt = xen_halt,
  282. -#ifdef CONFIG_X86_64
  283. - .adjust_exception_frame = xen_adjust_exception_frame,
  284. -#endif
  285. };
  286. void __init xen_init_irq_ops(void)
  287. diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S
  288. index dfabcbf8e813..c12260ef3e4b 100644
  289. --- a/arch/x86/entry/entry_64.S
  290. +++ b/arch/x86/entry/entry_64.S
  291. @@ -829,7 +829,6 @@ ENTRY(\sym)
  292. .endif
  293. ASM_CLAC
  294. - PARAVIRT_ADJUST_EXCEPTION_FRAME
  295. .ifeq \has_error_code
  296. pushq $-1 /* ORIG_RAX: no syscall to restart */
  297. @@ -975,7 +974,7 @@ ENTRY(do_softirq_own_stack)
  298. ENDPROC(do_softirq_own_stack)
  299. #ifdef CONFIG_XEN
  300. -idtentry xen_hypervisor_callback xen_do_hypervisor_callback has_error_code=0
  301. +idtentry hypervisor_callback xen_do_hypervisor_callback has_error_code=0
  302. /*
  303. * A note on the "critical region" in our callback handler.
  304. @@ -1042,8 +1041,6 @@ ENTRY(xen_failsafe_callback)
  305. movq 8(%rsp), %r11
  306. addq $0x30, %rsp
  307. pushq $0 /* RIP */
  308. - pushq %r11
  309. - pushq %rcx
  310. UNWIND_HINT_IRET_REGS offset=8
  311. jmp general_protection
  312. 1: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
  313. @@ -1074,9 +1071,8 @@ idtentry int3 do_int3 has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK
  314. idtentry stack_segment do_stack_segment has_error_code=1
  315. #ifdef CONFIG_XEN
  316. -idtentry xen_debug do_debug has_error_code=0
  317. -idtentry xen_int3 do_int3 has_error_code=0
  318. -idtentry xen_stack_segment do_stack_segment has_error_code=1
  319. +idtentry xendebug do_debug has_error_code=0
  320. +idtentry xenint3 do_int3 has_error_code=0
  321. #endif
  322. idtentry general_protection do_general_protection has_error_code=1
  323. @@ -1240,20 +1236,9 @@ ENTRY(error_exit)
  324. END(error_exit)
  325. /* Runs on exception stack */
  326. +/* XXX: broken on Xen PV */
  327. ENTRY(nmi)
  328. UNWIND_HINT_IRET_REGS
  329. - /*
  330. - * Fix up the exception frame if we're on Xen.
  331. - * PARAVIRT_ADJUST_EXCEPTION_FRAME is guaranteed to push at most
  332. - * one value to the stack on native, so it may clobber the rdx
  333. - * scratch slot, but it won't clobber any of the important
  334. - * slots past it.
  335. - *
  336. - * Xen is a different story, because the Xen frame itself overlaps
  337. - * the "NMI executing" variable.
  338. - */
  339. - PARAVIRT_ADJUST_EXCEPTION_FRAME
  340. -
  341. /*
  342. * We allow breakpoints in NMIs. If a breakpoint occurs, then
  343. * the iretq it performs will take us out of NMI context.
  344. diff --git a/arch/x86/entry/entry_64_compat.S b/arch/x86/entry/entry_64_compat.S
  345. index 5314d7b8e5ad..d8468ba24be0 100644
  346. --- a/arch/x86/entry/entry_64_compat.S
  347. +++ b/arch/x86/entry/entry_64_compat.S
  348. @@ -293,7 +293,6 @@ ENTRY(entry_INT80_compat)
  349. /*
  350. * Interrupts are off on entry.
  351. */
  352. - PARAVIRT_ADJUST_EXCEPTION_FRAME
  353. ASM_CLAC /* Do this early to minimize exposure */
  354. SWAPGS
  355. diff --git a/arch/x86/xen/xen-asm_64.S b/arch/x86/xen/xen-asm_64.S
  356. index 3a3b6a211584..dae2cc33afb5 100644
  357. --- a/arch/x86/xen/xen-asm_64.S
  358. +++ b/arch/x86/xen/xen-asm_64.S
  359. @@ -16,11 +16,42 @@
  360. #include <linux/linkage.h>
  361. -ENTRY(xen_adjust_exception_frame)
  362. - mov 8+0(%rsp), %rcx
  363. - mov 8+8(%rsp), %r11
  364. - ret $16
  365. -ENDPROC(xen_adjust_exception_frame)
  366. +.macro xen_pv_trap name
  367. +ENTRY(xen_\name)
  368. + pop %rcx
  369. + pop %r11
  370. + jmp \name
  371. +END(xen_\name)
  372. +.endm
  373. +
  374. +xen_pv_trap divide_error
  375. +xen_pv_trap debug
  376. +xen_pv_trap xendebug
  377. +xen_pv_trap int3
  378. +xen_pv_trap xenint3
  379. +xen_pv_trap nmi
  380. +xen_pv_trap overflow
  381. +xen_pv_trap bounds
  382. +xen_pv_trap invalid_op
  383. +xen_pv_trap device_not_available
  384. +xen_pv_trap double_fault
  385. +xen_pv_trap coprocessor_segment_overrun
  386. +xen_pv_trap invalid_TSS
  387. +xen_pv_trap segment_not_present
  388. +xen_pv_trap stack_segment
  389. +xen_pv_trap general_protection
  390. +xen_pv_trap page_fault
  391. +xen_pv_trap spurious_interrupt_bug
  392. +xen_pv_trap coprocessor_error
  393. +xen_pv_trap alignment_check
  394. +#ifdef CONFIG_X86_MCE
  395. +xen_pv_trap machine_check
  396. +#endif /* CONFIG_X86_MCE */
  397. +xen_pv_trap simd_coprocessor_error
  398. +#ifdef CONFIG_IA32_EMULATION
  399. +xen_pv_trap entry_INT80_compat
  400. +#endif
  401. +xen_pv_trap hypervisor_callback
  402. hypercall_iret = hypercall_page + __HYPERVISOR_iret * 32
  403. /*
  404. --
  405. 2.14.2