0117-ACPI-APEI-Replace-ioremap_page_range-with-fixmap.patch 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. From c18bf2e2bd1efc5430965d0110a8dfc4a25d84ad Mon Sep 17 00:00:00 2001
  2. From: James Morse <[email protected]>
  3. Date: Mon, 6 Nov 2017 18:44:24 +0000
  4. Subject: [PATCH 117/241] ACPI / APEI: Replace ioremap_page_range() with fixmap
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. CVE-2017-5754
  9. Replace ghes_io{re,un}map_pfn_{nmi,irq}()s use of ioremap_page_range()
  10. with __set_fixmap() as ioremap_page_range() may sleep to allocate a new
  11. level of page-table, even if its passed an existing final-address to
  12. use in the mapping.
  13. The GHES driver can only be enabled for architectures that select
  14. HAVE_ACPI_APEI: Add fixmap entries to both x86 and arm64.
  15. clear_fixmap() does the TLB invalidation in __set_fixmap() for arm64
  16. and __set_pte_vaddr() for x86. In each case its the same as the
  17. respective arch_apei_flush_tlb_one().
  18. Reported-by: Fengguang Wu <[email protected]>
  19. Suggested-by: Linus Torvalds <[email protected]>
  20. Signed-off-by: James Morse <[email protected]>
  21. Reviewed-by: Borislav Petkov <[email protected]>
  22. Tested-by: Tyler Baicar <[email protected]>
  23. Tested-by: Toshi Kani <[email protected]>
  24. [ For the arm64 bits: ]
  25. Acked-by: Will Deacon <[email protected]>
  26. [ For the x86 bits: ]
  27. Acked-by: Ingo Molnar <[email protected]>
  28. Signed-off-by: Rafael J. Wysocki <[email protected]>
  29. Cc: All applicable <[email protected]>
  30. (cherry picked from commit 4f89fa286f6729312e227e7c2d764e8e7b9d340e)
  31. Signed-off-by: Andy Whitcroft <[email protected]>
  32. Signed-off-by: Kleber Sacilotto de Souza <[email protected]>
  33. (cherry picked from commit eda363b23c1601f733cb1d7d66d1a4975c4c5d09)
  34. Signed-off-by: Fabian Grünbichler <[email protected]>
  35. ---
  36. arch/arm64/include/asm/fixmap.h | 7 +++++++
  37. arch/x86/include/asm/fixmap.h | 6 ++++++
  38. drivers/acpi/apei/ghes.c | 44 +++++++++++++----------------------------
  39. 3 files changed, 27 insertions(+), 30 deletions(-)
  40. diff --git a/arch/arm64/include/asm/fixmap.h b/arch/arm64/include/asm/fixmap.h
  41. index caf86be815ba..4052ec39e8db 100644
  42. --- a/arch/arm64/include/asm/fixmap.h
  43. +++ b/arch/arm64/include/asm/fixmap.h
  44. @@ -51,6 +51,13 @@ enum fixed_addresses {
  45. FIX_EARLYCON_MEM_BASE,
  46. FIX_TEXT_POKE0,
  47. +
  48. +#ifdef CONFIG_ACPI_APEI_GHES
  49. + /* Used for GHES mapping from assorted contexts */
  50. + FIX_APEI_GHES_IRQ,
  51. + FIX_APEI_GHES_NMI,
  52. +#endif /* CONFIG_ACPI_APEI_GHES */
  53. +
  54. __end_of_permanent_fixed_addresses,
  55. /*
  56. diff --git a/arch/x86/include/asm/fixmap.h b/arch/x86/include/asm/fixmap.h
  57. index b65155cc3760..81c2b11f50a6 100644
  58. --- a/arch/x86/include/asm/fixmap.h
  59. +++ b/arch/x86/include/asm/fixmap.h
  60. @@ -104,6 +104,12 @@ enum fixed_addresses {
  61. FIX_GDT_REMAP_BEGIN,
  62. FIX_GDT_REMAP_END = FIX_GDT_REMAP_BEGIN + NR_CPUS - 1,
  63. +#ifdef CONFIG_ACPI_APEI_GHES
  64. + /* Used for GHES mapping from assorted contexts */
  65. + FIX_APEI_GHES_IRQ,
  66. + FIX_APEI_GHES_NMI,
  67. +#endif
  68. +
  69. __end_of_permanent_fixed_addresses,
  70. /*
  71. diff --git a/drivers/acpi/apei/ghes.c b/drivers/acpi/apei/ghes.c
  72. index 4827176f838d..f9f106e62e74 100644
  73. --- a/drivers/acpi/apei/ghes.c
  74. +++ b/drivers/acpi/apei/ghes.c
  75. @@ -51,6 +51,7 @@
  76. #include <acpi/actbl1.h>
  77. #include <acpi/ghes.h>
  78. #include <acpi/apei.h>
  79. +#include <asm/fixmap.h>
  80. #include <asm/tlbflush.h>
  81. #include <ras/ras_event.h>
  82. @@ -112,7 +113,7 @@ static DEFINE_MUTEX(ghes_list_mutex);
  83. * Because the memory area used to transfer hardware error information
  84. * from BIOS to Linux can be determined only in NMI, IRQ or timer
  85. * handler, but general ioremap can not be used in atomic context, so
  86. - * a special version of atomic ioremap is implemented for that.
  87. + * the fixmap is used instead.
  88. */
  89. /*
  90. @@ -126,8 +127,8 @@ static DEFINE_MUTEX(ghes_list_mutex);
  91. /* virtual memory area for atomic ioremap */
  92. static struct vm_struct *ghes_ioremap_area;
  93. /*
  94. - * These 2 spinlock is used to prevent atomic ioremap virtual memory
  95. - * area from being mapped simultaneously.
  96. + * These 2 spinlocks are used to prevent the fixmap entries from being used
  97. + * simultaneously.
  98. */
  99. static DEFINE_RAW_SPINLOCK(ghes_ioremap_lock_nmi);
  100. static DEFINE_SPINLOCK(ghes_ioremap_lock_irq);
  101. @@ -159,53 +160,36 @@ static void ghes_ioremap_exit(void)
  102. static void __iomem *ghes_ioremap_pfn_nmi(u64 pfn)
  103. {
  104. - unsigned long vaddr;
  105. phys_addr_t paddr;
  106. pgprot_t prot;
  107. - vaddr = (unsigned long)GHES_IOREMAP_NMI_PAGE(ghes_ioremap_area->addr);
  108. -
  109. paddr = pfn << PAGE_SHIFT;
  110. prot = arch_apei_get_mem_attribute(paddr);
  111. - ioremap_page_range(vaddr, vaddr + PAGE_SIZE, paddr, prot);
  112. + __set_fixmap(FIX_APEI_GHES_NMI, paddr, prot);
  113. - return (void __iomem *)vaddr;
  114. + return (void __iomem *) fix_to_virt(FIX_APEI_GHES_NMI);
  115. }
  116. static void __iomem *ghes_ioremap_pfn_irq(u64 pfn)
  117. {
  118. - unsigned long vaddr;
  119. phys_addr_t paddr;
  120. pgprot_t prot;
  121. - vaddr = (unsigned long)GHES_IOREMAP_IRQ_PAGE(ghes_ioremap_area->addr);
  122. -
  123. paddr = pfn << PAGE_SHIFT;
  124. prot = arch_apei_get_mem_attribute(paddr);
  125. + __set_fixmap(FIX_APEI_GHES_IRQ, paddr, prot);
  126. - ioremap_page_range(vaddr, vaddr + PAGE_SIZE, paddr, prot);
  127. -
  128. - return (void __iomem *)vaddr;
  129. + return (void __iomem *) fix_to_virt(FIX_APEI_GHES_IRQ);
  130. }
  131. -static void ghes_iounmap_nmi(void __iomem *vaddr_ptr)
  132. +static void ghes_iounmap_nmi(void)
  133. {
  134. - unsigned long vaddr = (unsigned long __force)vaddr_ptr;
  135. - void *base = ghes_ioremap_area->addr;
  136. -
  137. - BUG_ON(vaddr != (unsigned long)GHES_IOREMAP_NMI_PAGE(base));
  138. - unmap_kernel_range_noflush(vaddr, PAGE_SIZE);
  139. - arch_apei_flush_tlb_one(vaddr);
  140. + clear_fixmap(FIX_APEI_GHES_NMI);
  141. }
  142. -static void ghes_iounmap_irq(void __iomem *vaddr_ptr)
  143. +static void ghes_iounmap_irq(void)
  144. {
  145. - unsigned long vaddr = (unsigned long __force)vaddr_ptr;
  146. - void *base = ghes_ioremap_area->addr;
  147. -
  148. - BUG_ON(vaddr != (unsigned long)GHES_IOREMAP_IRQ_PAGE(base));
  149. - unmap_kernel_range_noflush(vaddr, PAGE_SIZE);
  150. - arch_apei_flush_tlb_one(vaddr);
  151. + clear_fixmap(FIX_APEI_GHES_IRQ);
  152. }
  153. static int ghes_estatus_pool_init(void)
  154. @@ -361,10 +345,10 @@ static void ghes_copy_tofrom_phys(void *buffer, u64 paddr, u32 len,
  155. paddr += trunk;
  156. buffer += trunk;
  157. if (in_nmi) {
  158. - ghes_iounmap_nmi(vaddr);
  159. + ghes_iounmap_nmi();
  160. raw_spin_unlock(&ghes_ioremap_lock_nmi);
  161. } else {
  162. - ghes_iounmap_irq(vaddr);
  163. + ghes_iounmap_irq();
  164. spin_unlock_irqrestore(&ghes_ioremap_lock_irq, flags);
  165. }
  166. }
  167. --
  168. 2.14.2