0166-x86-vsyscall-64-Explicitly-set-_PAGE_USER-in-the-pag.patch 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. From 1150912f8311cdf3d7f394528dcacf0f95d892d6 Mon Sep 17 00:00:00 2001
  2. From: Andy Lutomirski <[email protected]>
  3. Date: Sun, 10 Dec 2017 22:47:19 -0800
  4. Subject: [PATCH 166/242] x86/vsyscall/64: Explicitly set _PAGE_USER in the
  5. pagetable hierarchy
  6. MIME-Version: 1.0
  7. Content-Type: text/plain; charset=UTF-8
  8. Content-Transfer-Encoding: 8bit
  9. CVE-2017-5754
  10. The kernel is very erratic as to which pagetables have _PAGE_USER set. The
  11. vsyscall page gets lucky: it seems that all of the relevant pagetables are
  12. among the apparently arbitrary ones that set _PAGE_USER. Rather than
  13. relying on chance, just explicitly set _PAGE_USER.
  14. This will let us clean up pagetable setup to stop setting _PAGE_USER. The
  15. added code can also be reused by pagetable isolation to manage the
  16. _PAGE_USER bit in the usermode tables.
  17. [ tglx: Folded paravirt fix from Juergen Gross ]
  18. Signed-off-by: Andy Lutomirski <[email protected]>
  19. Signed-off-by: Thomas Gleixner <[email protected]>
  20. Cc: Borislav Petkov <[email protected]>
  21. Cc: Brian Gerst <[email protected]>
  22. Cc: Dave Hansen <[email protected]>
  23. Cc: David Laight <[email protected]>
  24. Cc: H. Peter Anvin <[email protected]>
  25. Cc: Josh Poimboeuf <[email protected]>
  26. Cc: Juergen Gross <[email protected]>
  27. Cc: Kees Cook <[email protected]>
  28. Cc: Linus Torvalds <[email protected]>
  29. Cc: Peter Zijlstra <[email protected]>
  30. Signed-off-by: Ingo Molnar <[email protected]>
  31. (cherry picked from commit 49275fef986abfb8b476e4708aaecc07e7d3e087)
  32. Signed-off-by: Andy Whitcroft <[email protected]>
  33. Signed-off-by: Kleber Sacilotto de Souza <[email protected]>
  34. (cherry picked from commit 445742d3632efea229c0b974f91e56a19cf31996)
  35. Signed-off-by: Fabian Grünbichler <[email protected]>
  36. ---
  37. arch/x86/entry/vsyscall/vsyscall_64.c | 34 +++++++++++++++++++++++++++++++++-
  38. 1 file changed, 33 insertions(+), 1 deletion(-)
  39. diff --git a/arch/x86/entry/vsyscall/vsyscall_64.c b/arch/x86/entry/vsyscall/vsyscall_64.c
  40. index ce1d7534fa53..91f3133cf5f1 100644
  41. --- a/arch/x86/entry/vsyscall/vsyscall_64.c
  42. +++ b/arch/x86/entry/vsyscall/vsyscall_64.c
  43. @@ -36,6 +36,7 @@
  44. #include <asm/unistd.h>
  45. #include <asm/fixmap.h>
  46. #include <asm/traps.h>
  47. +#include <asm/paravirt.h>
  48. #define CREATE_TRACE_POINTS
  49. #include "vsyscall_trace.h"
  50. @@ -328,16 +329,47 @@ int in_gate_area_no_mm(unsigned long addr)
  51. return vsyscall_mode != NONE && (addr & PAGE_MASK) == VSYSCALL_ADDR;
  52. }
  53. +/*
  54. + * The VSYSCALL page is the only user-accessible page in the kernel address
  55. + * range. Normally, the kernel page tables can have _PAGE_USER clear, but
  56. + * the tables covering VSYSCALL_ADDR need _PAGE_USER set if vsyscalls
  57. + * are enabled.
  58. + *
  59. + * Some day we may create a "minimal" vsyscall mode in which we emulate
  60. + * vsyscalls but leave the page not present. If so, we skip calling
  61. + * this.
  62. + */
  63. +static void __init set_vsyscall_pgtable_user_bits(void)
  64. +{
  65. + pgd_t *pgd;
  66. + p4d_t *p4d;
  67. + pud_t *pud;
  68. + pmd_t *pmd;
  69. +
  70. + pgd = pgd_offset_k(VSYSCALL_ADDR);
  71. + set_pgd(pgd, __pgd(pgd_val(*pgd) | _PAGE_USER));
  72. + p4d = p4d_offset(pgd, VSYSCALL_ADDR);
  73. +#if CONFIG_PGTABLE_LEVELS >= 5
  74. + p4d->p4d |= _PAGE_USER;
  75. +#endif
  76. + pud = pud_offset(p4d, VSYSCALL_ADDR);
  77. + set_pud(pud, __pud(pud_val(*pud) | _PAGE_USER));
  78. + pmd = pmd_offset(pud, VSYSCALL_ADDR);
  79. + set_pmd(pmd, __pmd(pmd_val(*pmd) | _PAGE_USER));
  80. +}
  81. +
  82. void __init map_vsyscall(void)
  83. {
  84. extern char __vsyscall_page;
  85. unsigned long physaddr_vsyscall = __pa_symbol(&__vsyscall_page);
  86. - if (vsyscall_mode != NONE)
  87. + if (vsyscall_mode != NONE) {
  88. __set_fixmap(VSYSCALL_PAGE, physaddr_vsyscall,
  89. vsyscall_mode == NATIVE
  90. ? PAGE_KERNEL_VSYSCALL
  91. : PAGE_KERNEL_VVAR);
  92. + set_vsyscall_pgtable_user_bits();
  93. + }
  94. BUILD_BUG_ON((unsigned long)__fix_to_virt(VSYSCALL_PAGE) !=
  95. (unsigned long)VSYSCALL_ADDR);
  96. --
  97. 2.14.2