0030-x86-idt-Unify-gate_struct-handling-for-32-64-bit-ker.patch 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: Thomas Gleixner <[email protected]>
  3. Date: Mon, 28 Aug 2017 08:47:37 +0200
  4. Subject: [PATCH] x86/idt: Unify gate_struct handling for 32/64-bit kernels
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. CVE-2017-5754
  9. The first 32 bits of gate struct are the same for 32 and 64 bit kernels.
  10. The 32-bit version uses desc_struct and no designated data structure,
  11. so we need different accessors for 32 and 64 bit kernels.
  12. Aside of that the macros which are necessary to build the 32-bit
  13. gate descriptor are horrible to read.
  14. Unify the gate structs and switch all code fiddling with it over.
  15. Signed-off-by: Thomas Gleixner <[email protected]>
  16. Cc: Andy Lutomirski <[email protected]>
  17. Cc: Borislav Petkov <[email protected]>
  18. Cc: Brian Gerst <[email protected]>
  19. Cc: Denys Vlasenko <[email protected]>
  20. Cc: H. Peter Anvin <[email protected]>
  21. Cc: Josh Poimboeuf <[email protected]>
  22. Cc: Linus Torvalds <[email protected]>
  23. Cc: Peter Zijlstra <[email protected]>
  24. Cc: Steven Rostedt <[email protected]>
  25. Link: http://lkml.kernel.org/r/[email protected]
  26. Signed-off-by: Ingo Molnar <[email protected]>
  27. (cherry picked from commit 64b163fab684e3de47aa8db6cc08ae7d2e194373)
  28. Signed-off-by: Andy Whitcroft <[email protected]>
  29. Signed-off-by: Kleber Sacilotto de Souza <[email protected]>
  30. (cherry picked from commit 587719b1926757eb7531e0631d63fb93cd60d0d3)
  31. Signed-off-by: Fabian Grünbichler <[email protected]>
  32. ---
  33. arch/x86/include/asm/desc.h | 45 ++++++++++++++-----------------
  34. arch/x86/include/asm/desc_defs.h | 57 ++++++++++++++++++++++++++--------------
  35. arch/x86/kvm/vmx.c | 2 +-
  36. arch/x86/xen/enlighten_pv.c | 12 ++++-----
  37. 4 files changed, 63 insertions(+), 53 deletions(-)
  38. diff --git a/arch/x86/include/asm/desc.h b/arch/x86/include/asm/desc.h
  39. index d0a21b12dd58..57e502a4e92f 100644
  40. --- a/arch/x86/include/asm/desc.h
  41. +++ b/arch/x86/include/asm/desc.h
  42. @@ -83,33 +83,25 @@ static inline phys_addr_t get_cpu_gdt_paddr(unsigned int cpu)
  43. return per_cpu_ptr_to_phys(get_cpu_gdt_rw(cpu));
  44. }
  45. -#ifdef CONFIG_X86_64
  46. -
  47. static inline void pack_gate(gate_desc *gate, unsigned type, unsigned long func,
  48. unsigned dpl, unsigned ist, unsigned seg)
  49. {
  50. - gate->offset_low = PTR_LOW(func);
  51. + gate->offset_low = (u16) func;
  52. + gate->bits.p = 1;
  53. + gate->bits.dpl = dpl;
  54. + gate->bits.zero = 0;
  55. + gate->bits.type = type;
  56. + gate->offset_middle = (u16) (func >> 16);
  57. +#ifdef CONFIG_X86_64
  58. gate->segment = __KERNEL_CS;
  59. - gate->ist = ist;
  60. - gate->p = 1;
  61. - gate->dpl = dpl;
  62. - gate->zero0 = 0;
  63. - gate->zero1 = 0;
  64. - gate->type = type;
  65. - gate->offset_middle = PTR_MIDDLE(func);
  66. - gate->offset_high = PTR_HIGH(func);
  67. -}
  68. -
  69. + gate->bits.ist = ist;
  70. + gate->reserved = 0;
  71. + gate->offset_high = (u32) (func >> 32);
  72. #else
  73. -static inline void pack_gate(gate_desc *gate, unsigned char type,
  74. - unsigned long base, unsigned dpl, unsigned flags,
  75. - unsigned short seg)
  76. -{
  77. - gate->a = (seg << 16) | (base & 0xffff);
  78. - gate->b = (base & 0xffff0000) | (((0x80 | type | (dpl << 5)) & 0xff) << 8);
  79. -}
  80. -
  81. + gate->segment = seg;
  82. + gate->bits.ist = 0;
  83. #endif
  84. +}
  85. static inline int desc_empty(const void *ptr)
  86. {
  87. @@ -185,7 +177,8 @@ static inline void pack_descriptor(struct desc_struct *desc, unsigned long base,
  88. }
  89. -static inline void set_tssldt_descriptor(void *d, unsigned long addr, unsigned type, unsigned size)
  90. +static inline void set_tssldt_descriptor(void *d, unsigned long addr,
  91. + unsigned type, unsigned size)
  92. {
  93. #ifdef CONFIG_X86_64
  94. struct ldttss_desc64 *desc = d;
  95. @@ -193,13 +186,13 @@ static inline void set_tssldt_descriptor(void *d, unsigned long addr, unsigned t
  96. memset(desc, 0, sizeof(*desc));
  97. desc->limit0 = size & 0xFFFF;
  98. - desc->base0 = PTR_LOW(addr);
  99. - desc->base1 = PTR_MIDDLE(addr) & 0xFF;
  100. + desc->base0 = (u16) addr;
  101. + desc->base1 = (addr >> 16) & 0xFF;
  102. desc->type = type;
  103. desc->p = 1;
  104. desc->limit1 = (size >> 16) & 0xF;
  105. - desc->base2 = (PTR_MIDDLE(addr) >> 8) & 0xFF;
  106. - desc->base3 = PTR_HIGH(addr);
  107. + desc->base2 = (addr >> 24) & 0xFF;
  108. + desc->base3 = (u32) (addr >> 32);
  109. #else
  110. pack_descriptor((struct desc_struct *)d, addr, size, 0x80 | type, 0);
  111. #endif
  112. diff --git a/arch/x86/include/asm/desc_defs.h b/arch/x86/include/asm/desc_defs.h
  113. index 49265345d4d2..d684bee8a59a 100644
  114. --- a/arch/x86/include/asm/desc_defs.h
  115. +++ b/arch/x86/include/asm/desc_defs.h
  116. @@ -47,20 +47,6 @@ enum {
  117. GATE_TASK = 0x5,
  118. };
  119. -/* 16byte gate */
  120. -struct gate_struct64 {
  121. - u16 offset_low;
  122. - u16 segment;
  123. - unsigned ist : 3, zero0 : 5, type : 5, dpl : 2, p : 1;
  124. - u16 offset_middle;
  125. - u32 offset_high;
  126. - u32 zero1;
  127. -} __attribute__((packed));
  128. -
  129. -#define PTR_LOW(x) ((unsigned long long)(x) & 0xFFFF)
  130. -#define PTR_MIDDLE(x) (((unsigned long long)(x) >> 16) & 0xFFFF)
  131. -#define PTR_HIGH(x) ((unsigned long long)(x) >> 32)
  132. -
  133. enum {
  134. DESC_TSS = 0x9,
  135. DESC_LDT = 0x2,
  136. @@ -77,20 +63,51 @@ struct ldttss_desc64 {
  137. u32 zero1;
  138. } __attribute__((packed));
  139. +
  140. #ifdef CONFIG_X86_64
  141. -typedef struct gate_struct64 gate_desc;
  142. typedef struct ldttss_desc64 ldt_desc;
  143. typedef struct ldttss_desc64 tss_desc;
  144. -#define gate_offset(g) ((g).offset_low | ((unsigned long)(g).offset_middle << 16) | ((unsigned long)(g).offset_high << 32))
  145. -#define gate_segment(g) ((g).segment)
  146. #else
  147. -typedef struct desc_struct gate_desc;
  148. typedef struct desc_struct ldt_desc;
  149. typedef struct desc_struct tss_desc;
  150. -#define gate_offset(g) (((g).b & 0xffff0000) | ((g).a & 0x0000ffff))
  151. -#define gate_segment(g) ((g).a >> 16)
  152. #endif
  153. +struct idt_bits {
  154. + u16 ist : 3,
  155. + zero : 5,
  156. + type : 5,
  157. + dpl : 2,
  158. + p : 1;
  159. +} __attribute__((packed));
  160. +
  161. +struct gate_struct {
  162. + u16 offset_low;
  163. + u16 segment;
  164. + struct idt_bits bits;
  165. + u16 offset_middle;
  166. +#ifdef CONFIG_X86_64
  167. + u32 offset_high;
  168. + u32 reserved;
  169. +#endif
  170. +} __attribute__((packed));
  171. +
  172. +typedef struct gate_struct gate_desc;
  173. +
  174. +static inline unsigned long gate_offset(const gate_desc *g)
  175. +{
  176. +#ifdef CONFIG_X86_64
  177. + return g->offset_low | ((unsigned long)g->offset_middle << 16) |
  178. + ((unsigned long) g->offset_high << 32);
  179. +#else
  180. + return g->offset_low | ((unsigned long)g->offset_middle << 16);
  181. +#endif
  182. +}
  183. +
  184. +static inline unsigned long gate_segment(const gate_desc *g)
  185. +{
  186. + return g->segment;
  187. +}
  188. +
  189. struct desc_ptr {
  190. unsigned short size;
  191. unsigned long address;
  192. diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
  193. index a2c95522ac99..7b447d126d17 100644
  194. --- a/arch/x86/kvm/vmx.c
  195. +++ b/arch/x86/kvm/vmx.c
  196. @@ -8838,7 +8838,7 @@ static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
  197. vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
  198. desc = (gate_desc *)vmx->host_idt_base + vector;
  199. - entry = gate_offset(*desc);
  200. + entry = gate_offset(desc);
  201. asm volatile(
  202. #ifdef CONFIG_X86_64
  203. "mov %%" _ASM_SP ", %[sp]\n\t"
  204. diff --git a/arch/x86/xen/enlighten_pv.c b/arch/x86/xen/enlighten_pv.c
  205. index 6c279c8f0a0e..49ee3315b9f7 100644
  206. --- a/arch/x86/xen/enlighten_pv.c
  207. +++ b/arch/x86/xen/enlighten_pv.c
  208. @@ -591,12 +591,12 @@ static int cvt_gate_to_trap(int vector, const gate_desc *val,
  209. {
  210. unsigned long addr;
  211. - if (val->type != GATE_TRAP && val->type != GATE_INTERRUPT)
  212. + if (val->bits.type != GATE_TRAP && val->bits.type != GATE_INTERRUPT)
  213. return 0;
  214. info->vector = vector;
  215. - addr = gate_offset(*val);
  216. + addr = gate_offset(val);
  217. #ifdef CONFIG_X86_64
  218. /*
  219. * Look for known traps using IST, and substitute them
  220. @@ -629,16 +629,16 @@ static int cvt_gate_to_trap(int vector, const gate_desc *val,
  221. ;
  222. else {
  223. /* Some other trap using IST? */
  224. - if (WARN_ON(val->ist != 0))
  225. + if (WARN_ON(val->bits.ist != 0))
  226. return 0;
  227. }
  228. #endif /* CONFIG_X86_64 */
  229. info->address = addr;
  230. - info->cs = gate_segment(*val);
  231. - info->flags = val->dpl;
  232. + info->cs = gate_segment(val);
  233. + info->flags = val->bits.dpl;
  234. /* interrupt gates clear IF */
  235. - if (val->type == GATE_INTERRUPT)
  236. + if (val->bits.type == GATE_INTERRUPT)
  237. info->flags |= 1 << 2;
  238. return 1;
  239. --
  240. 2.14.2