kaslr.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * This file implements KASLR memory randomization for x86_64. It randomizes
  3. * the virtual address space of kernel memory regions (physical memory
  4. * mapping, vmalloc & vmemmap) for x86_64. This security feature mitigates
  5. * exploits relying on predictable kernel addresses.
  6. *
  7. * Entropy is generated using the KASLR early boot functions now shared in
  8. * the lib directory (originally written by Kees Cook). Randomization is
  9. * done on PGD & P4D/PUD page table levels to increase possible addresses.
  10. * The physical memory mapping code was adapted to support P4D/PUD level
  11. * virtual addresses. This implementation on the best configuration provides
  12. * 30,000 possible virtual addresses in average for each memory region.
  13. * An additional low memory page is used to ensure each CPU can start with
  14. * a PGD aligned virtual address (for realmode).
  15. *
  16. * The order of each memory region is not changed. The feature looks at
  17. * the available space for the regions based on different configuration
  18. * options and randomizes the base and space between each. The size of the
  19. * physical memory mapping is the available physical memory.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/init.h>
  23. #include <linux/random.h>
  24. #include <asm/pgalloc.h>
  25. #include <asm/pgtable.h>
  26. #include <asm/setup.h>
  27. #include <asm/kaslr.h>
  28. #include "mm_internal.h"
  29. #define TB_SHIFT 40
  30. /*
  31. * Virtual address start and end range for randomization. The end changes base
  32. * on configuration to have the highest amount of space for randomization.
  33. * It increases the possible random position for each randomized region.
  34. *
  35. * You need to add an if/def entry if you introduce a new memory region
  36. * compatible with KASLR. Your entry must be in logical order with memory
  37. * layout. For example, ESPFIX is before EFI because its virtual address is
  38. * before. You also need to add a BUILD_BUG_ON() in kernel_randomize_memory() to
  39. * ensure that this order is correct and won't be changed.
  40. */
  41. static const unsigned long vaddr_start = __PAGE_OFFSET_BASE;
  42. #if defined(CONFIG_X86_ESPFIX64)
  43. static const unsigned long vaddr_end = ESPFIX_BASE_ADDR;
  44. #elif defined(CONFIG_EFI)
  45. static const unsigned long vaddr_end = EFI_VA_END;
  46. #else
  47. static const unsigned long vaddr_end = __START_KERNEL_map;
  48. #endif
  49. /* Default values */
  50. unsigned long page_offset_base = __PAGE_OFFSET_BASE;
  51. EXPORT_SYMBOL(page_offset_base);
  52. unsigned long vmalloc_base = __VMALLOC_BASE;
  53. EXPORT_SYMBOL(vmalloc_base);
  54. unsigned long vmemmap_base = __VMEMMAP_BASE;
  55. EXPORT_SYMBOL(vmemmap_base);
  56. /*
  57. * Memory regions randomized by KASLR (except modules that use a separate logic
  58. * earlier during boot). The list is ordered based on virtual addresses. This
  59. * order is kept after randomization.
  60. */
  61. static __initdata struct kaslr_memory_region {
  62. unsigned long *base;
  63. unsigned long size_tb;
  64. } kaslr_regions[] = {
  65. { &page_offset_base, 1 << (__PHYSICAL_MASK_SHIFT - TB_SHIFT) /* Maximum */ },
  66. { &vmalloc_base, VMALLOC_SIZE_TB },
  67. { &vmemmap_base, 1 },
  68. };
  69. /* Get size in bytes used by the memory region */
  70. static inline unsigned long get_padding(struct kaslr_memory_region *region)
  71. {
  72. return (region->size_tb << TB_SHIFT);
  73. }
  74. /*
  75. * Apply no randomization if KASLR was disabled at boot or if KASAN
  76. * is enabled. KASAN shadow mappings rely on regions being PGD aligned.
  77. */
  78. static inline bool kaslr_memory_enabled(void)
  79. {
  80. return kaslr_enabled() && !IS_ENABLED(CONFIG_KASAN);
  81. }
  82. /* Initialize base and padding for each memory region randomized with KASLR */
  83. void __init kernel_randomize_memory(void)
  84. {
  85. size_t i;
  86. unsigned long vaddr = vaddr_start;
  87. unsigned long rand, memory_tb;
  88. struct rnd_state rand_state;
  89. unsigned long remain_entropy;
  90. /*
  91. * All these BUILD_BUG_ON checks ensures the memory layout is
  92. * consistent with the vaddr_start/vaddr_end variables.
  93. */
  94. BUILD_BUG_ON(vaddr_start >= vaddr_end);
  95. BUILD_BUG_ON(IS_ENABLED(CONFIG_X86_ESPFIX64) &&
  96. vaddr_end >= EFI_VA_END);
  97. BUILD_BUG_ON((IS_ENABLED(CONFIG_X86_ESPFIX64) ||
  98. IS_ENABLED(CONFIG_EFI)) &&
  99. vaddr_end >= __START_KERNEL_map);
  100. BUILD_BUG_ON(vaddr_end > __START_KERNEL_map);
  101. if (!kaslr_memory_enabled())
  102. return;
  103. /*
  104. * Update Physical memory mapping to available and
  105. * add padding if needed (especially for memory hotplug support).
  106. */
  107. BUG_ON(kaslr_regions[0].base != &page_offset_base);
  108. memory_tb = DIV_ROUND_UP(max_pfn << PAGE_SHIFT, 1UL << TB_SHIFT) +
  109. CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING;
  110. /* Adapt phyiscal memory region size based on available memory */
  111. if (memory_tb < kaslr_regions[0].size_tb)
  112. kaslr_regions[0].size_tb = memory_tb;
  113. /* Calculate entropy available between regions */
  114. remain_entropy = vaddr_end - vaddr_start;
  115. for (i = 0; i < ARRAY_SIZE(kaslr_regions); i++)
  116. remain_entropy -= get_padding(&kaslr_regions[i]);
  117. prandom_seed_state(&rand_state, kaslr_get_random_long("Memory"));
  118. for (i = 0; i < ARRAY_SIZE(kaslr_regions); i++) {
  119. unsigned long entropy;
  120. /*
  121. * Select a random virtual address using the extra entropy
  122. * available.
  123. */
  124. entropy = remain_entropy / (ARRAY_SIZE(kaslr_regions) - i);
  125. prandom_bytes_state(&rand_state, &rand, sizeof(rand));
  126. if (IS_ENABLED(CONFIG_X86_5LEVEL))
  127. entropy = (rand % (entropy + 1)) & P4D_MASK;
  128. else
  129. entropy = (rand % (entropy + 1)) & PUD_MASK;
  130. vaddr += entropy;
  131. *kaslr_regions[i].base = vaddr;
  132. /*
  133. * Jump the region and add a minimum padding based on
  134. * randomization alignment.
  135. */
  136. vaddr += get_padding(&kaslr_regions[i]);
  137. if (IS_ENABLED(CONFIG_X86_5LEVEL))
  138. vaddr = round_up(vaddr + 1, P4D_SIZE);
  139. else
  140. vaddr = round_up(vaddr + 1, PUD_SIZE);
  141. remain_entropy -= entropy;
  142. }
  143. }
  144. static void __meminit init_trampoline_pud(void)
  145. {
  146. unsigned long paddr, paddr_next;
  147. pgd_t *pgd;
  148. pud_t *pud_page, *pud_page_tramp;
  149. int i;
  150. pud_page_tramp = alloc_low_page();
  151. paddr = 0;
  152. pgd = pgd_offset_k((unsigned long)__va(paddr));
  153. pud_page = (pud_t *) pgd_page_vaddr(*pgd);
  154. for (i = pud_index(paddr); i < PTRS_PER_PUD; i++, paddr = paddr_next) {
  155. pud_t *pud, *pud_tramp;
  156. unsigned long vaddr = (unsigned long)__va(paddr);
  157. pud_tramp = pud_page_tramp + pud_index(paddr);
  158. pud = pud_page + pud_index(vaddr);
  159. paddr_next = (paddr & PUD_MASK) + PUD_SIZE;
  160. *pud_tramp = *pud;
  161. }
  162. set_pgd(&trampoline_pgd_entry,
  163. __pgd(_KERNPG_TABLE | __pa(pud_page_tramp)));
  164. }
  165. static void __meminit init_trampoline_p4d(void)
  166. {
  167. unsigned long paddr, paddr_next;
  168. pgd_t *pgd;
  169. p4d_t *p4d_page, *p4d_page_tramp;
  170. int i;
  171. p4d_page_tramp = alloc_low_page();
  172. paddr = 0;
  173. pgd = pgd_offset_k((unsigned long)__va(paddr));
  174. p4d_page = (p4d_t *) pgd_page_vaddr(*pgd);
  175. for (i = p4d_index(paddr); i < PTRS_PER_P4D; i++, paddr = paddr_next) {
  176. p4d_t *p4d, *p4d_tramp;
  177. unsigned long vaddr = (unsigned long)__va(paddr);
  178. p4d_tramp = p4d_page_tramp + p4d_index(paddr);
  179. p4d = p4d_page + p4d_index(vaddr);
  180. paddr_next = (paddr & P4D_MASK) + P4D_SIZE;
  181. *p4d_tramp = *p4d;
  182. }
  183. set_pgd(&trampoline_pgd_entry,
  184. __pgd(_KERNPG_TABLE | __pa(p4d_page_tramp)));
  185. }
  186. /*
  187. * Create PGD aligned trampoline table to allow real mode initialization
  188. * of additional CPUs. Consume only 1 low memory page.
  189. */
  190. void __meminit init_trampoline(void)
  191. {
  192. if (!kaslr_memory_enabled()) {
  193. init_trampoline_default();
  194. return;
  195. }
  196. if (IS_ENABLED(CONFIG_X86_5LEVEL))
  197. init_trampoline_p4d();
  198. else
  199. init_trampoline_pud();
  200. }