0098-x86-entry-64-Pass-SP0-directly-to-load_sp0.patch 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. From 3868ecbc68a9713951f3008ef3af3b9da7e67e60 Mon Sep 17 00:00:00 2001
  2. From: Andy Lutomirski <[email protected]>
  3. Date: Thu, 2 Nov 2017 00:59:10 -0700
  4. Subject: [PATCH 098/242] x86/entry/64: Pass SP0 directly to load_sp0()
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. CVE-2017-5754
  9. load_sp0() had an odd signature:
  10. void load_sp0(struct tss_struct *tss, struct thread_struct *thread);
  11. Simplify it to:
  12. void load_sp0(unsigned long sp0);
  13. Also simplify a few get_cpu()/put_cpu() sequences to
  14. preempt_disable()/preempt_enable().
  15. Signed-off-by: Andy Lutomirski <[email protected]>
  16. Reviewed-by: Borislav Petkov <[email protected]>
  17. Cc: Borislav Petkov <[email protected]>
  18. Cc: Brian Gerst <[email protected]>
  19. Cc: Dave Hansen <[email protected]>
  20. Cc: Linus Torvalds <[email protected]>
  21. Cc: Peter Zijlstra <[email protected]>
  22. Cc: Thomas Gleixner <[email protected]>
  23. Link: http://lkml.kernel.org/r/2655d8b42ed940aa384fe18ee1129bbbcf730a08.1509609304.git.luto@kernel.org
  24. Signed-off-by: Ingo Molnar <[email protected]>
  25. (cherry picked from commit da51da189a24bb9b7e2d5a123be096e51a4695a5)
  26. Signed-off-by: Andy Whitcroft <[email protected]>
  27. Signed-off-by: Kleber Sacilotto de Souza <[email protected]>
  28. (cherry picked from commit 41f6a89b0be4d052a6af59df5e56102d4e4c79ef)
  29. Signed-off-by: Fabian Grünbichler <[email protected]>
  30. ---
  31. arch/x86/include/asm/paravirt.h | 5 ++---
  32. arch/x86/include/asm/paravirt_types.h | 2 +-
  33. arch/x86/include/asm/processor.h | 9 ++++-----
  34. arch/x86/kernel/cpu/common.c | 4 ++--
  35. arch/x86/kernel/process_32.c | 2 +-
  36. arch/x86/kernel/process_64.c | 2 +-
  37. arch/x86/kernel/vm86_32.c | 14 ++++++--------
  38. arch/x86/xen/enlighten_pv.c | 7 +++----
  39. 8 files changed, 20 insertions(+), 25 deletions(-)
  40. diff --git a/arch/x86/include/asm/paravirt.h b/arch/x86/include/asm/paravirt.h
  41. index 12deec722cf0..43d4f90edebc 100644
  42. --- a/arch/x86/include/asm/paravirt.h
  43. +++ b/arch/x86/include/asm/paravirt.h
  44. @@ -15,10 +15,9 @@
  45. #include <linux/cpumask.h>
  46. #include <asm/frame.h>
  47. -static inline void load_sp0(struct tss_struct *tss,
  48. - struct thread_struct *thread)
  49. +static inline void load_sp0(unsigned long sp0)
  50. {
  51. - PVOP_VCALL2(pv_cpu_ops.load_sp0, tss, thread);
  52. + PVOP_VCALL1(pv_cpu_ops.load_sp0, sp0);
  53. }
  54. /* The paravirtualized CPUID instruction. */
  55. diff --git a/arch/x86/include/asm/paravirt_types.h b/arch/x86/include/asm/paravirt_types.h
  56. index 42873edd9f9d..e3953a1e2b57 100644
  57. --- a/arch/x86/include/asm/paravirt_types.h
  58. +++ b/arch/x86/include/asm/paravirt_types.h
  59. @@ -133,7 +133,7 @@ struct pv_cpu_ops {
  60. void (*alloc_ldt)(struct desc_struct *ldt, unsigned entries);
  61. void (*free_ldt)(struct desc_struct *ldt, unsigned entries);
  62. - void (*load_sp0)(struct tss_struct *tss, struct thread_struct *t);
  63. + void (*load_sp0)(unsigned long sp0);
  64. void (*set_iopl_mask)(unsigned mask);
  65. diff --git a/arch/x86/include/asm/processor.h b/arch/x86/include/asm/processor.h
  66. index ee37fb86900a..85ddfc1a9bb5 100644
  67. --- a/arch/x86/include/asm/processor.h
  68. +++ b/arch/x86/include/asm/processor.h
  69. @@ -510,9 +510,9 @@ static inline void native_set_iopl_mask(unsigned mask)
  70. }
  71. static inline void
  72. -native_load_sp0(struct tss_struct *tss, struct thread_struct *thread)
  73. +native_load_sp0(unsigned long sp0)
  74. {
  75. - tss->x86_tss.sp0 = thread->sp0;
  76. + this_cpu_write(cpu_tss.x86_tss.sp0, sp0);
  77. }
  78. static inline void native_swapgs(void)
  79. @@ -537,10 +537,9 @@ static inline unsigned long current_top_of_stack(void)
  80. #else
  81. #define __cpuid native_cpuid
  82. -static inline void load_sp0(struct tss_struct *tss,
  83. - struct thread_struct *thread)
  84. +static inline void load_sp0(unsigned long sp0)
  85. {
  86. - native_load_sp0(tss, thread);
  87. + native_load_sp0(sp0);
  88. }
  89. #define set_iopl_mask native_set_iopl_mask
  90. diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
  91. index ef7b1ba56363..6562acbfc4e0 100644
  92. --- a/arch/x86/kernel/cpu/common.c
  93. +++ b/arch/x86/kernel/cpu/common.c
  94. @@ -1570,7 +1570,7 @@ void cpu_init(void)
  95. BUG_ON(me->mm);
  96. enter_lazy_tlb(&init_mm, me);
  97. - load_sp0(t, &current->thread);
  98. + load_sp0(current->thread.sp0);
  99. set_tss_desc(cpu, t);
  100. load_TR_desc();
  101. load_mm_ldt(&init_mm);
  102. @@ -1624,7 +1624,7 @@ void cpu_init(void)
  103. BUG_ON(curr->mm);
  104. enter_lazy_tlb(&init_mm, curr);
  105. - load_sp0(t, thread);
  106. + load_sp0(thread->sp0);
  107. set_tss_desc(cpu, t);
  108. load_TR_desc();
  109. load_mm_ldt(&init_mm);
  110. diff --git a/arch/x86/kernel/process_32.c b/arch/x86/kernel/process_32.c
  111. index 2e42b66b8ca4..48a3f240f565 100644
  112. --- a/arch/x86/kernel/process_32.c
  113. +++ b/arch/x86/kernel/process_32.c
  114. @@ -287,7 +287,7 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
  115. * current_thread_info(). Refresh the SYSENTER configuration in
  116. * case prev or next is vm86.
  117. */
  118. - load_sp0(tss, next);
  119. + load_sp0(next->sp0);
  120. refresh_sysenter_cs(next);
  121. this_cpu_write(cpu_current_top_of_stack,
  122. (unsigned long)task_stack_page(next_p) +
  123. diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c
  124. index 565daaa6f18d..37b933628a8b 100644
  125. --- a/arch/x86/kernel/process_64.c
  126. +++ b/arch/x86/kernel/process_64.c
  127. @@ -466,7 +466,7 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
  128. this_cpu_write(current_task, next_p);
  129. /* Reload sp0. */
  130. - load_sp0(tss, next);
  131. + load_sp0(next->sp0);
  132. /*
  133. * Now maybe reload the debug registers and handle I/O bitmaps
  134. diff --git a/arch/x86/kernel/vm86_32.c b/arch/x86/kernel/vm86_32.c
  135. index 5bc1c3ab6287..0f1d92cd20ad 100644
  136. --- a/arch/x86/kernel/vm86_32.c
  137. +++ b/arch/x86/kernel/vm86_32.c
  138. @@ -94,7 +94,6 @@
  139. void save_v86_state(struct kernel_vm86_regs *regs, int retval)
  140. {
  141. - struct tss_struct *tss;
  142. struct task_struct *tsk = current;
  143. struct vm86plus_struct __user *user;
  144. struct vm86 *vm86 = current->thread.vm86;
  145. @@ -146,13 +145,13 @@ void save_v86_state(struct kernel_vm86_regs *regs, int retval)
  146. do_exit(SIGSEGV);
  147. }
  148. - tss = &per_cpu(cpu_tss, get_cpu());
  149. + preempt_disable();
  150. tsk->thread.sp0 = vm86->saved_sp0;
  151. tsk->thread.sysenter_cs = __KERNEL_CS;
  152. - load_sp0(tss, &tsk->thread);
  153. + load_sp0(tsk->thread.sp0);
  154. refresh_sysenter_cs(&tsk->thread);
  155. vm86->saved_sp0 = 0;
  156. - put_cpu();
  157. + preempt_enable();
  158. memcpy(&regs->pt, &vm86->regs32, sizeof(struct pt_regs));
  159. @@ -238,7 +237,6 @@ SYSCALL_DEFINE2(vm86, unsigned long, cmd, unsigned long, arg)
  160. static long do_sys_vm86(struct vm86plus_struct __user *user_vm86, bool plus)
  161. {
  162. - struct tss_struct *tss;
  163. struct task_struct *tsk = current;
  164. struct vm86 *vm86 = tsk->thread.vm86;
  165. struct kernel_vm86_regs vm86regs;
  166. @@ -366,8 +364,8 @@ static long do_sys_vm86(struct vm86plus_struct __user *user_vm86, bool plus)
  167. vm86->saved_sp0 = tsk->thread.sp0;
  168. lazy_save_gs(vm86->regs32.gs);
  169. - tss = &per_cpu(cpu_tss, get_cpu());
  170. /* make room for real-mode segments */
  171. + preempt_disable();
  172. tsk->thread.sp0 += 16;
  173. if (static_cpu_has(X86_FEATURE_SEP)) {
  174. @@ -375,8 +373,8 @@ static long do_sys_vm86(struct vm86plus_struct __user *user_vm86, bool plus)
  175. refresh_sysenter_cs(&tsk->thread);
  176. }
  177. - load_sp0(tss, &tsk->thread);
  178. - put_cpu();
  179. + load_sp0(tsk->thread.sp0);
  180. + preempt_enable();
  181. if (vm86->flags & VM86_SCREEN_BITMAP)
  182. mark_screen_rdonly(tsk->mm);
  183. diff --git a/arch/x86/xen/enlighten_pv.c b/arch/x86/xen/enlighten_pv.c
  184. index 8da4eff19c2a..e7b213047724 100644
  185. --- a/arch/x86/xen/enlighten_pv.c
  186. +++ b/arch/x86/xen/enlighten_pv.c
  187. @@ -810,15 +810,14 @@ static void __init xen_write_gdt_entry_boot(struct desc_struct *dt, int entry,
  188. }
  189. }
  190. -static void xen_load_sp0(struct tss_struct *tss,
  191. - struct thread_struct *thread)
  192. +static void xen_load_sp0(unsigned long sp0)
  193. {
  194. struct multicall_space mcs;
  195. mcs = xen_mc_entry(0);
  196. - MULTI_stack_switch(mcs.mc, __KERNEL_DS, thread->sp0);
  197. + MULTI_stack_switch(mcs.mc, __KERNEL_DS, sp0);
  198. xen_mc_issue(PARAVIRT_LAZY_CPU);
  199. - tss->x86_tss.sp0 = thread->sp0;
  200. + this_cpu_write(cpu_tss.x86_tss.sp0, sp0);
  201. }
  202. void xen_set_iopl_mask(unsigned mask)
  203. --
  204. 2.14.2