0158-x86-entry-Clean-up-the-SYSENTER_stack-code.patch 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. From f9bbe642761e841720222bcb912d920875f6ead2 Mon Sep 17 00:00:00 2001
  2. From: Andy Lutomirski <[email protected]>
  3. Date: Mon, 4 Dec 2017 15:07:28 +0100
  4. Subject: [PATCH 158/231] x86/entry: Clean up the SYSENTER_stack code
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. CVE-2017-5754
  9. The existing code was a mess, mainly because C arrays are nasty. Turn
  10. SYSENTER_stack into a struct, add a helper to find it, and do all the
  11. obvious cleanups this enables.
  12. Signed-off-by: Andy Lutomirski <[email protected]>
  13. Signed-off-by: Thomas Gleixner <[email protected]>
  14. Reviewed-by: Thomas Gleixner <[email protected]>
  15. Reviewed-by: Borislav Petkov <[email protected]>
  16. Cc: Boris Ostrovsky <[email protected]>
  17. Cc: Borislav Petkov <[email protected]>
  18. Cc: Brian Gerst <[email protected]>
  19. Cc: Dave Hansen <[email protected]>
  20. Cc: Dave Hansen <[email protected]>
  21. Cc: David Laight <[email protected]>
  22. Cc: Denys Vlasenko <[email protected]>
  23. Cc: Eduardo Valentin <[email protected]>
  24. Cc: Greg KH <[email protected]>
  25. Cc: H. Peter Anvin <[email protected]>
  26. Cc: Josh Poimboeuf <[email protected]>
  27. Cc: Juergen Gross <[email protected]>
  28. Cc: Linus Torvalds <[email protected]>
  29. Cc: Peter Zijlstra <[email protected]>
  30. Cc: Rik van Riel <[email protected]>
  31. Cc: Will Deacon <[email protected]>
  32. Cc: [email protected]
  33. Cc: [email protected]
  34. Cc: [email protected]
  35. Cc: [email protected]
  36. Link: https://lkml.kernel.org/r/[email protected]
  37. Signed-off-by: Ingo Molnar <[email protected]>
  38. (cherry picked from commit 0f9a48100fba3f189724ae88a450c2261bf91c80)
  39. Signed-off-by: Andy Whitcroft <[email protected]>
  40. Signed-off-by: Kleber Sacilotto de Souza <[email protected]>
  41. (cherry picked from commit a308af33c794110c52427ad11d3a6d35ffc14b76)
  42. Signed-off-by: Fabian Grünbichler <[email protected]>
  43. ---
  44. arch/x86/include/asm/fixmap.h | 5 +++++
  45. arch/x86/include/asm/processor.h | 6 +++++-
  46. arch/x86/kernel/asm-offsets.c | 6 ++----
  47. arch/x86/kernel/cpu/common.c | 14 +++-----------
  48. arch/x86/kernel/dumpstack.c | 7 +++----
  49. arch/x86/entry/entry_32.S | 4 ++--
  50. arch/x86/entry/entry_64.S | 2 +-
  51. 7 files changed, 21 insertions(+), 23 deletions(-)
  52. diff --git a/arch/x86/include/asm/fixmap.h b/arch/x86/include/asm/fixmap.h
  53. index 953aed54cb5e..56aaffbbffd6 100644
  54. --- a/arch/x86/include/asm/fixmap.h
  55. +++ b/arch/x86/include/asm/fixmap.h
  56. @@ -225,5 +225,10 @@ static inline struct cpu_entry_area *get_cpu_entry_area(int cpu)
  57. return (struct cpu_entry_area *)__fix_to_virt(__get_cpu_entry_area_page_index(cpu, 0));
  58. }
  59. +static inline struct SYSENTER_stack *cpu_SYSENTER_stack(int cpu)
  60. +{
  61. + return &get_cpu_entry_area(cpu)->tss.SYSENTER_stack;
  62. +}
  63. +
  64. #endif /* !__ASSEMBLY__ */
  65. #endif /* _ASM_X86_FIXMAP_H */
  66. diff --git a/arch/x86/include/asm/processor.h b/arch/x86/include/asm/processor.h
  67. index 4737d378d7b5..2d489a414a86 100644
  68. --- a/arch/x86/include/asm/processor.h
  69. +++ b/arch/x86/include/asm/processor.h
  70. @@ -330,12 +330,16 @@ struct x86_hw_tss {
  71. #define IO_BITMAP_OFFSET (offsetof(struct tss_struct, io_bitmap) - offsetof(struct tss_struct, x86_tss))
  72. #define INVALID_IO_BITMAP_OFFSET 0x8000
  73. +struct SYSENTER_stack {
  74. + unsigned long words[64];
  75. +};
  76. +
  77. struct tss_struct {
  78. /*
  79. * Space for the temporary SYSENTER stack, used for SYSENTER
  80. * and the entry trampoline as well.
  81. */
  82. - unsigned long SYSENTER_stack[64];
  83. + struct SYSENTER_stack SYSENTER_stack;
  84. /*
  85. * The fixed hardware portion. This must not cross a page boundary
  86. diff --git a/arch/x86/kernel/asm-offsets.c b/arch/x86/kernel/asm-offsets.c
  87. index 822be00c85ff..00ea20bfa857 100644
  88. --- a/arch/x86/kernel/asm-offsets.c
  89. +++ b/arch/x86/kernel/asm-offsets.c
  90. @@ -93,10 +93,8 @@ void common(void) {
  91. BLANK();
  92. DEFINE(PTREGS_SIZE, sizeof(struct pt_regs));
  93. - /* Offset from cpu_tss to SYSENTER_stack */
  94. - OFFSET(CPU_TSS_SYSENTER_stack, tss_struct, SYSENTER_stack);
  95. - /* Size of SYSENTER_stack */
  96. - DEFINE(SIZEOF_SYSENTER_stack, sizeof(((struct tss_struct *)0)->SYSENTER_stack));
  97. + OFFSET(TSS_STRUCT_SYSENTER_stack, tss_struct, SYSENTER_stack);
  98. + DEFINE(SIZEOF_SYSENTER_stack, sizeof(struct SYSENTER_stack));
  99. /* Layout info for cpu_entry_area */
  100. OFFSET(CPU_ENTRY_AREA_tss, cpu_entry_area, tss);
  101. diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
  102. index f487766855d3..f9541c48c290 100644
  103. --- a/arch/x86/kernel/cpu/common.c
  104. +++ b/arch/x86/kernel/cpu/common.c
  105. @@ -1306,12 +1306,7 @@ void enable_sep_cpu(void)
  106. tss->x86_tss.ss1 = __KERNEL_CS;
  107. wrmsr(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1, 0);
  108. -
  109. - wrmsr(MSR_IA32_SYSENTER_ESP,
  110. - (unsigned long)&get_cpu_entry_area(cpu)->tss +
  111. - offsetofend(struct tss_struct, SYSENTER_stack),
  112. - 0);
  113. -
  114. + wrmsr(MSR_IA32_SYSENTER_ESP, (unsigned long)(cpu_SYSENTER_stack(cpu) + 1), 0);
  115. wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long)entry_SYSENTER_32, 0);
  116. put_cpu();
  117. @@ -1437,9 +1432,7 @@ void syscall_init(void)
  118. * AMD doesn't allow SYSENTER in long mode (either 32- or 64-bit).
  119. */
  120. wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
  121. - wrmsrl_safe(MSR_IA32_SYSENTER_ESP,
  122. - (unsigned long)&get_cpu_entry_area(cpu)->tss +
  123. - offsetofend(struct tss_struct, SYSENTER_stack));
  124. + wrmsrl_safe(MSR_IA32_SYSENTER_ESP, (unsigned long)(cpu_SYSENTER_stack(cpu) + 1));
  125. wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)entry_SYSENTER_compat);
  126. #else
  127. wrmsrl(MSR_CSTAR, (unsigned long)ignore_sysret);
  128. @@ -1653,8 +1646,7 @@ void cpu_init(void)
  129. */
  130. set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss);
  131. load_TR_desc();
  132. - load_sp0((unsigned long)&get_cpu_entry_area(cpu)->tss +
  133. - offsetofend(struct tss_struct, SYSENTER_stack));
  134. + load_sp0((unsigned long)(cpu_SYSENTER_stack(cpu) + 1));
  135. load_mm_ldt(&init_mm);
  136. diff --git a/arch/x86/kernel/dumpstack.c b/arch/x86/kernel/dumpstack.c
  137. index c32c6cce9dcc..b005e5ef6738 100644
  138. --- a/arch/x86/kernel/dumpstack.c
  139. +++ b/arch/x86/kernel/dumpstack.c
  140. @@ -45,11 +45,10 @@ bool in_task_stack(unsigned long *stack, struct task_struct *task,
  141. bool in_sysenter_stack(unsigned long *stack, struct stack_info *info)
  142. {
  143. - int cpu = smp_processor_id();
  144. - struct tss_struct *tss = &get_cpu_entry_area(cpu)->tss;
  145. + struct SYSENTER_stack *ss = cpu_SYSENTER_stack(smp_processor_id());
  146. - void *begin = &tss->SYSENTER_stack;
  147. - void *end = (void *)&tss->SYSENTER_stack + sizeof(tss->SYSENTER_stack);
  148. + void *begin = ss;
  149. + void *end = ss + 1;
  150. if ((void *)stack < begin || (void *)stack >= end)
  151. return false;
  152. diff --git a/arch/x86/entry/entry_32.S b/arch/x86/entry/entry_32.S
  153. index 41e0e103f090..04abcd3f8e2d 100644
  154. --- a/arch/x86/entry/entry_32.S
  155. +++ b/arch/x86/entry/entry_32.S
  156. @@ -949,7 +949,7 @@ ENTRY(debug)
  157. /* Are we currently on the SYSENTER stack? */
  158. movl PER_CPU_VAR(cpu_entry_area), %ecx
  159. - addl $CPU_ENTRY_AREA_tss + CPU_TSS_SYSENTER_stack + SIZEOF_SYSENTER_stack, %ecx
  160. + addl $CPU_ENTRY_AREA_tss + TSS_STRUCT_SYSENTER_stack + SIZEOF_SYSENTER_stack, %ecx
  161. subl %eax, %ecx /* ecx = (end of SYSENTER_stack) - esp */
  162. cmpl $SIZEOF_SYSENTER_stack, %ecx
  163. jb .Ldebug_from_sysenter_stack
  164. @@ -993,7 +993,7 @@ ENTRY(nmi)
  165. /* Are we currently on the SYSENTER stack? */
  166. movl PER_CPU_VAR(cpu_entry_area), %ecx
  167. - addl $CPU_ENTRY_AREA_tss + CPU_TSS_SYSENTER_stack + SIZEOF_SYSENTER_stack, %ecx
  168. + addl $CPU_ENTRY_AREA_tss + TSS_STRUCT_SYSENTER_stack + SIZEOF_SYSENTER_stack, %ecx
  169. subl %eax, %ecx /* ecx = (end of SYSENTER_stack) - esp */
  170. cmpl $SIZEOF_SYSENTER_stack, %ecx
  171. jb .Lnmi_from_sysenter_stack
  172. diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S
  173. index dc100a7052ee..7a5e9edcdaf4 100644
  174. --- a/arch/x86/entry/entry_64.S
  175. +++ b/arch/x86/entry/entry_64.S
  176. @@ -153,7 +153,7 @@ END(native_usergs_sysret64)
  177. _entry_trampoline - CPU_ENTRY_AREA_entry_trampoline(%rip)
  178. /* The top word of the SYSENTER stack is hot and is usable as scratch space. */
  179. -#define RSP_SCRATCH CPU_ENTRY_AREA_tss + CPU_TSS_SYSENTER_stack + \
  180. +#define RSP_SCRATCH CPU_ENTRY_AREA_tss + TSS_STRUCT_SYSENTER_stack + \
  181. SIZEOF_SYSENTER_stack - 8 + CPU_ENTRY_AREA
  182. ENTRY(entry_SYSCALL_64_trampoline)
  183. --
  184. 2.14.2