0192-x86-mm-pti-Add-mapping-helper-functions.patch 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. From 1cb923a3733ac738f0d96fe4738bdf159db86cfd Mon Sep 17 00:00:00 2001
  2. From: Dave Hansen <[email protected]>
  3. Date: Mon, 4 Dec 2017 15:07:37 +0100
  4. Subject: [PATCH 192/242] x86/mm/pti: Add mapping helper functions
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. CVE-2017-5754
  9. Add the pagetable helper functions do manage the separate user space page
  10. tables.
  11. [ tglx: Split out from the big combo kaiser patch. Folded Andys
  12. simplification and made it out of line as Boris suggested ]
  13. Signed-off-by: Dave Hansen <[email protected]>
  14. Signed-off-by: Thomas Gleixner <[email protected]>
  15. Cc: Andy Lutomirski <[email protected]>
  16. Cc: Boris Ostrovsky <[email protected]>
  17. Cc: Borislav Petkov <[email protected]>
  18. Cc: Brian Gerst <[email protected]>
  19. Cc: David Laight <[email protected]>
  20. Cc: Denys Vlasenko <[email protected]>
  21. Cc: Eduardo Valentin <[email protected]>
  22. Cc: Greg KH <[email protected]>
  23. Cc: H. Peter Anvin <[email protected]>
  24. Cc: Josh Poimboeuf <[email protected]>
  25. Cc: Juergen Gross <[email protected]>
  26. Cc: Linus Torvalds <[email protected]>
  27. Cc: Peter Zijlstra <[email protected]>
  28. Cc: Will Deacon <[email protected]>
  29. Cc: [email protected]
  30. Cc: [email protected]
  31. Cc: [email protected]
  32. Cc: [email protected]
  33. Cc: [email protected]
  34. Signed-off-by: Ingo Molnar <[email protected]>
  35. (cherry picked from commit 61e9b3671007a5da8127955a1a3bda7e0d5f42e8)
  36. Signed-off-by: Andy Whitcroft <[email protected]>
  37. Signed-off-by: Kleber Sacilotto de Souza <[email protected]>
  38. (cherry picked from commit fb45c59197f3134db6e223bb4fec0529774c07e1)
  39. Signed-off-by: Fabian Grünbichler <[email protected]>
  40. ---
  41. arch/x86/include/asm/pgtable.h | 6 ++-
  42. arch/x86/include/asm/pgtable_64.h | 92 +++++++++++++++++++++++++++++++++++++++
  43. arch/x86/mm/pti.c | 41 +++++++++++++++++
  44. 3 files changed, 138 insertions(+), 1 deletion(-)
  45. diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h
  46. index bb8e9ea7deb4..abbb47c75467 100644
  47. --- a/arch/x86/include/asm/pgtable.h
  48. +++ b/arch/x86/include/asm/pgtable.h
  49. @@ -894,7 +894,11 @@ static inline int pgd_none(pgd_t pgd)
  50. * pgd_offset() returns a (pgd_t *)
  51. * pgd_index() is used get the offset into the pgd page's array of pgd_t's;
  52. */
  53. -#define pgd_offset(mm, address) ((mm)->pgd + pgd_index((address)))
  54. +#define pgd_offset_pgd(pgd, address) (pgd + pgd_index((address)))
  55. +/*
  56. + * a shortcut to get a pgd_t in a given mm
  57. + */
  58. +#define pgd_offset(mm, address) pgd_offset_pgd((mm)->pgd, (address))
  59. /*
  60. * a shortcut which implies the use of the kernel's pgd, instead
  61. * of a process's
  62. diff --git a/arch/x86/include/asm/pgtable_64.h b/arch/x86/include/asm/pgtable_64.h
  63. index 2160c1fee920..1ac15b03cf30 100644
  64. --- a/arch/x86/include/asm/pgtable_64.h
  65. +++ b/arch/x86/include/asm/pgtable_64.h
  66. @@ -130,9 +130,97 @@ static inline pud_t native_pudp_get_and_clear(pud_t *xp)
  67. #endif
  68. }
  69. +#ifdef CONFIG_PAGE_TABLE_ISOLATION
  70. +/*
  71. + * All top-level PAGE_TABLE_ISOLATION page tables are order-1 pages
  72. + * (8k-aligned and 8k in size). The kernel one is at the beginning 4k and
  73. + * the user one is in the last 4k. To switch between them, you
  74. + * just need to flip the 12th bit in their addresses.
  75. + */
  76. +#define PTI_PGTABLE_SWITCH_BIT PAGE_SHIFT
  77. +
  78. +/*
  79. + * This generates better code than the inline assembly in
  80. + * __set_bit().
  81. + */
  82. +static inline void *ptr_set_bit(void *ptr, int bit)
  83. +{
  84. + unsigned long __ptr = (unsigned long)ptr;
  85. +
  86. + __ptr |= BIT(bit);
  87. + return (void *)__ptr;
  88. +}
  89. +static inline void *ptr_clear_bit(void *ptr, int bit)
  90. +{
  91. + unsigned long __ptr = (unsigned long)ptr;
  92. +
  93. + __ptr &= ~BIT(bit);
  94. + return (void *)__ptr;
  95. +}
  96. +
  97. +static inline pgd_t *kernel_to_user_pgdp(pgd_t *pgdp)
  98. +{
  99. + return ptr_set_bit(pgdp, PTI_PGTABLE_SWITCH_BIT);
  100. +}
  101. +
  102. +static inline pgd_t *user_to_kernel_pgdp(pgd_t *pgdp)
  103. +{
  104. + return ptr_clear_bit(pgdp, PTI_PGTABLE_SWITCH_BIT);
  105. +}
  106. +
  107. +static inline p4d_t *kernel_to_user_p4dp(p4d_t *p4dp)
  108. +{
  109. + return ptr_set_bit(p4dp, PTI_PGTABLE_SWITCH_BIT);
  110. +}
  111. +
  112. +static inline p4d_t *user_to_kernel_p4dp(p4d_t *p4dp)
  113. +{
  114. + return ptr_clear_bit(p4dp, PTI_PGTABLE_SWITCH_BIT);
  115. +}
  116. +#endif /* CONFIG_PAGE_TABLE_ISOLATION */
  117. +
  118. +/*
  119. + * Page table pages are page-aligned. The lower half of the top
  120. + * level is used for userspace and the top half for the kernel.
  121. + *
  122. + * Returns true for parts of the PGD that map userspace and
  123. + * false for the parts that map the kernel.
  124. + */
  125. +static inline bool pgdp_maps_userspace(void *__ptr)
  126. +{
  127. + unsigned long ptr = (unsigned long)__ptr;
  128. +
  129. + return (ptr & ~PAGE_MASK) < (PAGE_SIZE / 2);
  130. +}
  131. +
  132. +#ifdef CONFIG_PAGE_TABLE_ISOLATION
  133. +pgd_t __pti_set_user_pgd(pgd_t *pgdp, pgd_t pgd);
  134. +
  135. +/*
  136. + * Take a PGD location (pgdp) and a pgd value that needs to be set there.
  137. + * Populates the user and returns the resulting PGD that must be set in
  138. + * the kernel copy of the page tables.
  139. + */
  140. +static inline pgd_t pti_set_user_pgd(pgd_t *pgdp, pgd_t pgd)
  141. +{
  142. + if (!static_cpu_has(X86_FEATURE_PTI))
  143. + return pgd;
  144. + return __pti_set_user_pgd(pgdp, pgd);
  145. +}
  146. +#else
  147. +static inline pgd_t pti_set_user_pgd(pgd_t *pgdp, pgd_t pgd)
  148. +{
  149. + return pgd;
  150. +}
  151. +#endif
  152. +
  153. static inline void native_set_p4d(p4d_t *p4dp, p4d_t p4d)
  154. {
  155. +#if defined(CONFIG_PAGE_TABLE_ISOLATION) && !defined(CONFIG_X86_5LEVEL)
  156. + p4dp->pgd = pti_set_user_pgd(&p4dp->pgd, p4d.pgd);
  157. +#else
  158. *p4dp = p4d;
  159. +#endif
  160. }
  161. static inline void native_p4d_clear(p4d_t *p4d)
  162. @@ -146,7 +234,11 @@ static inline void native_p4d_clear(p4d_t *p4d)
  163. static inline void native_set_pgd(pgd_t *pgdp, pgd_t pgd)
  164. {
  165. +#ifdef CONFIG_PAGE_TABLE_ISOLATION
  166. + *pgdp = pti_set_user_pgd(pgdp, pgd);
  167. +#else
  168. *pgdp = pgd;
  169. +#endif
  170. }
  171. static inline void native_pgd_clear(pgd_t *pgd)
  172. diff --git a/arch/x86/mm/pti.c b/arch/x86/mm/pti.c
  173. index a13f6b109865..69a983365392 100644
  174. --- a/arch/x86/mm/pti.c
  175. +++ b/arch/x86/mm/pti.c
  176. @@ -96,6 +96,47 @@ void __init pti_check_boottime_disable(void)
  177. setup_force_cpu_cap(X86_FEATURE_PTI);
  178. }
  179. +pgd_t __pti_set_user_pgd(pgd_t *pgdp, pgd_t pgd)
  180. +{
  181. + /*
  182. + * Changes to the high (kernel) portion of the kernelmode page
  183. + * tables are not automatically propagated to the usermode tables.
  184. + *
  185. + * Users should keep in mind that, unlike the kernelmode tables,
  186. + * there is no vmalloc_fault equivalent for the usermode tables.
  187. + * Top-level entries added to init_mm's usermode pgd after boot
  188. + * will not be automatically propagated to other mms.
  189. + */
  190. + if (!pgdp_maps_userspace(pgdp))
  191. + return pgd;
  192. +
  193. + /*
  194. + * The user page tables get the full PGD, accessible from
  195. + * userspace:
  196. + */
  197. + kernel_to_user_pgdp(pgdp)->pgd = pgd.pgd;
  198. +
  199. + /*
  200. + * If this is normal user memory, make it NX in the kernel
  201. + * pagetables so that, if we somehow screw up and return to
  202. + * usermode with the kernel CR3 loaded, we'll get a page fault
  203. + * instead of allowing user code to execute with the wrong CR3.
  204. + *
  205. + * As exceptions, we don't set NX if:
  206. + * - _PAGE_USER is not set. This could be an executable
  207. + * EFI runtime mapping or something similar, and the kernel
  208. + * may execute from it
  209. + * - we don't have NX support
  210. + * - we're clearing the PGD (i.e. the new pgd is not present).
  211. + */
  212. + if ((pgd.pgd & (_PAGE_USER|_PAGE_PRESENT)) == (_PAGE_USER|_PAGE_PRESENT) &&
  213. + (__supported_pte_mask & _PAGE_NX))
  214. + pgd.pgd |= _PAGE_NX;
  215. +
  216. + /* return the copy of the PGD we want the kernel to use: */
  217. + return pgd;
  218. +}
  219. +
  220. /*
  221. * Initialize kernel page table isolation
  222. */
  223. --
  224. 2.14.2