0196-x86-mm-pti-Add-functions-to-clone-kernel-PMDs.patch 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. From bcb5ffbfba8c6c557ad536eb9084040b8e52923e Mon Sep 17 00:00:00 2001
  2. From: Andy Lutomirski <[email protected]>
  3. Date: Mon, 4 Dec 2017 15:07:42 +0100
  4. Subject: [PATCH 196/232] x86/mm/pti: Add functions to clone kernel PMDs
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. CVE-2017-5754
  9. Provide infrastructure to:
  10. - find a kernel PMD for a mapping which must be visible to user space for
  11. the entry/exit code to work.
  12. - walk an address range and share the kernel PMD with it.
  13. This reuses a small part of the original KAISER patches to populate the
  14. user space page table.
  15. [ tglx: Made it universally usable so it can be used for any kind of shared
  16. mapping. Add a mechanism to clear specific bits in the user space
  17. visible PMD entry. Folded Andys simplifactions ]
  18. Originally-by: Dave Hansen <[email protected]>
  19. Signed-off-by: Andy Lutomirski <[email protected]>
  20. Signed-off-by: Thomas Gleixner <[email protected]>
  21. Cc: Boris Ostrovsky <[email protected]>
  22. Cc: Borislav Petkov <[email protected]>
  23. Cc: Borislav Petkov <[email protected]>
  24. Cc: Brian Gerst <[email protected]>
  25. Cc: Dave Hansen <[email protected]>
  26. Cc: David Laight <[email protected]>
  27. Cc: Denys Vlasenko <[email protected]>
  28. Cc: Eduardo Valentin <[email protected]>
  29. Cc: Greg KH <[email protected]>
  30. Cc: H. Peter Anvin <[email protected]>
  31. Cc: Josh Poimboeuf <[email protected]>
  32. Cc: Juergen Gross <[email protected]>
  33. Cc: Linus Torvalds <[email protected]>
  34. Cc: Peter Zijlstra <[email protected]>
  35. Cc: Will Deacon <[email protected]>
  36. Cc: [email protected]
  37. Cc: [email protected]
  38. Cc: [email protected]
  39. Cc: [email protected]
  40. Signed-off-by: Ingo Molnar <[email protected]>
  41. (cherry picked from commit 03f4424f348e8be95eb1bbeba09461cd7b867828)
  42. Signed-off-by: Andy Whitcroft <[email protected]>
  43. Signed-off-by: Kleber Sacilotto de Souza <[email protected]>
  44. (cherry picked from commit 262ab7e8665e88581d20ccaefa107340457224bb)
  45. Signed-off-by: Fabian Grünbichler <[email protected]>
  46. ---
  47. arch/x86/mm/pti.c | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
  48. 1 file changed, 127 insertions(+)
  49. diff --git a/arch/x86/mm/pti.c b/arch/x86/mm/pti.c
  50. index 69a983365392..d58bcee470fc 100644
  51. --- a/arch/x86/mm/pti.c
  52. +++ b/arch/x86/mm/pti.c
  53. @@ -48,6 +48,11 @@
  54. #undef pr_fmt
  55. #define pr_fmt(fmt) "Kernel/User page tables isolation: " fmt
  56. +/* Backporting helper */
  57. +#ifndef __GFP_NOTRACK
  58. +#define __GFP_NOTRACK 0
  59. +#endif
  60. +
  61. static void __init pti_print_if_insecure(const char *reason)
  62. {
  63. if (boot_cpu_has_bug(X86_BUG_CPU_INSECURE))
  64. @@ -137,6 +142,128 @@ pgd_t __pti_set_user_pgd(pgd_t *pgdp, pgd_t pgd)
  65. return pgd;
  66. }
  67. +/*
  68. + * Walk the user copy of the page tables (optionally) trying to allocate
  69. + * page table pages on the way down.
  70. + *
  71. + * Returns a pointer to a P4D on success, or NULL on failure.
  72. + */
  73. +static p4d_t *pti_user_pagetable_walk_p4d(unsigned long address)
  74. +{
  75. + pgd_t *pgd = kernel_to_user_pgdp(pgd_offset_k(address));
  76. + gfp_t gfp = (GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO);
  77. +
  78. + if (address < PAGE_OFFSET) {
  79. + WARN_ONCE(1, "attempt to walk user address\n");
  80. + return NULL;
  81. + }
  82. +
  83. + if (pgd_none(*pgd)) {
  84. + unsigned long new_p4d_page = __get_free_page(gfp);
  85. + if (!new_p4d_page)
  86. + return NULL;
  87. +
  88. + if (pgd_none(*pgd)) {
  89. + set_pgd(pgd, __pgd(_KERNPG_TABLE | __pa(new_p4d_page)));
  90. + new_p4d_page = 0;
  91. + }
  92. + if (new_p4d_page)
  93. + free_page(new_p4d_page);
  94. + }
  95. + BUILD_BUG_ON(pgd_large(*pgd) != 0);
  96. +
  97. + return p4d_offset(pgd, address);
  98. +}
  99. +
  100. +/*
  101. + * Walk the user copy of the page tables (optionally) trying to allocate
  102. + * page table pages on the way down.
  103. + *
  104. + * Returns a pointer to a PMD on success, or NULL on failure.
  105. + */
  106. +static pmd_t *pti_user_pagetable_walk_pmd(unsigned long address)
  107. +{
  108. + gfp_t gfp = (GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO);
  109. + p4d_t *p4d = pti_user_pagetable_walk_p4d(address);
  110. + pud_t *pud;
  111. +
  112. + BUILD_BUG_ON(p4d_large(*p4d) != 0);
  113. + if (p4d_none(*p4d)) {
  114. + unsigned long new_pud_page = __get_free_page(gfp);
  115. + if (!new_pud_page)
  116. + return NULL;
  117. +
  118. + if (p4d_none(*p4d)) {
  119. + set_p4d(p4d, __p4d(_KERNPG_TABLE | __pa(new_pud_page)));
  120. + new_pud_page = 0;
  121. + }
  122. + if (new_pud_page)
  123. + free_page(new_pud_page);
  124. + }
  125. +
  126. + pud = pud_offset(p4d, address);
  127. + /* The user page tables do not use large mappings: */
  128. + if (pud_large(*pud)) {
  129. + WARN_ON(1);
  130. + return NULL;
  131. + }
  132. + if (pud_none(*pud)) {
  133. + unsigned long new_pmd_page = __get_free_page(gfp);
  134. + if (!new_pmd_page)
  135. + return NULL;
  136. +
  137. + if (pud_none(*pud)) {
  138. + set_pud(pud, __pud(_KERNPG_TABLE | __pa(new_pmd_page)));
  139. + new_pmd_page = 0;
  140. + }
  141. + if (new_pmd_page)
  142. + free_page(new_pmd_page);
  143. + }
  144. +
  145. + return pmd_offset(pud, address);
  146. +}
  147. +
  148. +static void __init
  149. +pti_clone_pmds(unsigned long start, unsigned long end, pmdval_t clear)
  150. +{
  151. + unsigned long addr;
  152. +
  153. + /*
  154. + * Clone the populated PMDs which cover start to end. These PMD areas
  155. + * can have holes.
  156. + */
  157. + for (addr = start; addr < end; addr += PMD_SIZE) {
  158. + pmd_t *pmd, *target_pmd;
  159. + pgd_t *pgd;
  160. + p4d_t *p4d;
  161. + pud_t *pud;
  162. +
  163. + pgd = pgd_offset_k(addr);
  164. + if (WARN_ON(pgd_none(*pgd)))
  165. + return;
  166. + p4d = p4d_offset(pgd, addr);
  167. + if (WARN_ON(p4d_none(*p4d)))
  168. + return;
  169. + pud = pud_offset(p4d, addr);
  170. + if (pud_none(*pud))
  171. + continue;
  172. + pmd = pmd_offset(pud, addr);
  173. + if (pmd_none(*pmd))
  174. + continue;
  175. +
  176. + target_pmd = pti_user_pagetable_walk_pmd(addr);
  177. + if (WARN_ON(!target_pmd))
  178. + return;
  179. +
  180. + /*
  181. + * Copy the PMD. That is, the kernelmode and usermode
  182. + * tables will share the last-level page tables of this
  183. + * address range
  184. + */
  185. + *target_pmd = pmd_clear_flags(*pmd, clear);
  186. + }
  187. +}
  188. +
  189. /*
  190. * Initialize kernel page table isolation
  191. */
  192. --
  193. 2.14.2