kaslr.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * This file implements KASLR memory randomization for x86_64. It randomizes
  4. * the virtual address space of kernel memory regions (physical memory
  5. * mapping, vmalloc & vmemmap) for x86_64. This security feature mitigates
  6. * exploits relying on predictable kernel addresses.
  7. *
  8. * Entropy is generated using the KASLR early boot functions now shared in
  9. * the lib directory (originally written by Kees Cook). Randomization is
  10. * done on PGD & P4D/PUD page table levels to increase possible addresses.
  11. * The physical memory mapping code was adapted to support P4D/PUD level
  12. * virtual addresses. This implementation on the best configuration provides
  13. * 30,000 possible virtual addresses in average for each memory region.
  14. * An additional low memory page is used to ensure each CPU can start with
  15. * a PGD aligned virtual address (for realmode).
  16. *
  17. * The order of each memory region is not changed. The feature looks at
  18. * the available space for the regions based on different configuration
  19. * options and randomizes the base and space between each. The size of the
  20. * physical memory mapping is the available physical memory.
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/init.h>
  24. #include <linux/random.h>
  25. #include <linux/memblock.h>
  26. #include <linux/pgtable.h>
  27. #include <asm/setup.h>
  28. #include <asm/kaslr.h>
  29. #include "mm_internal.h"
  30. #define TB_SHIFT 40
  31. /*
  32. * The end address could depend on more configuration options to make the
  33. * highest amount of space for randomization available, but that's too hard
  34. * to keep straight and caused issues already.
  35. */
  36. static const unsigned long vaddr_end = CPU_ENTRY_AREA_BASE;
  37. /*
  38. * Memory regions randomized by KASLR (except modules that use a separate logic
  39. * earlier during boot). The list is ordered based on virtual addresses. This
  40. * order is kept after randomization.
  41. */
  42. static __initdata struct kaslr_memory_region {
  43. unsigned long *base;
  44. unsigned long size_tb;
  45. } kaslr_regions[] = {
  46. { &page_offset_base, 0 },
  47. { &vmalloc_base, 0 },
  48. { &vmemmap_base, 0 },
  49. };
  50. /* Get size in bytes used by the memory region */
  51. static inline unsigned long get_padding(struct kaslr_memory_region *region)
  52. {
  53. return (region->size_tb << TB_SHIFT);
  54. }
  55. /* Initialize base and padding for each memory region randomized with KASLR */
  56. void __init kernel_randomize_memory(void)
  57. {
  58. size_t i;
  59. unsigned long vaddr_start, vaddr;
  60. unsigned long rand, memory_tb;
  61. struct rnd_state rand_state;
  62. unsigned long remain_entropy;
  63. unsigned long vmemmap_size;
  64. vaddr_start = pgtable_l5_enabled() ? __PAGE_OFFSET_BASE_L5 : __PAGE_OFFSET_BASE_L4;
  65. vaddr = vaddr_start;
  66. /*
  67. * These BUILD_BUG_ON checks ensure the memory layout is consistent
  68. * with the vaddr_start/vaddr_end variables. These checks are very
  69. * limited....
  70. */
  71. BUILD_BUG_ON(vaddr_start >= vaddr_end);
  72. BUILD_BUG_ON(vaddr_end != CPU_ENTRY_AREA_BASE);
  73. BUILD_BUG_ON(vaddr_end > __START_KERNEL_map);
  74. if (!kaslr_memory_enabled())
  75. return;
  76. kaslr_regions[0].size_tb = 1 << (MAX_PHYSMEM_BITS - TB_SHIFT);
  77. kaslr_regions[1].size_tb = VMALLOC_SIZE_TB;
  78. /*
  79. * Update Physical memory mapping to available and
  80. * add padding if needed (especially for memory hotplug support).
  81. */
  82. BUG_ON(kaslr_regions[0].base != &page_offset_base);
  83. memory_tb = DIV_ROUND_UP(max_pfn << PAGE_SHIFT, 1UL << TB_SHIFT) +
  84. CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING;
  85. /* Adapt physical memory region size based on available memory */
  86. if (memory_tb < kaslr_regions[0].size_tb)
  87. kaslr_regions[0].size_tb = memory_tb;
  88. /*
  89. * Calculate the vmemmap region size in TBs, aligned to a TB
  90. * boundary.
  91. */
  92. vmemmap_size = (kaslr_regions[0].size_tb << (TB_SHIFT - PAGE_SHIFT)) *
  93. sizeof(struct page);
  94. kaslr_regions[2].size_tb = DIV_ROUND_UP(vmemmap_size, 1UL << TB_SHIFT);
  95. /* Calculate entropy available between regions */
  96. remain_entropy = vaddr_end - vaddr_start;
  97. for (i = 0; i < ARRAY_SIZE(kaslr_regions); i++)
  98. remain_entropy -= get_padding(&kaslr_regions[i]);
  99. prandom_seed_state(&rand_state, kaslr_get_random_long("Memory"));
  100. for (i = 0; i < ARRAY_SIZE(kaslr_regions); i++) {
  101. unsigned long entropy;
  102. /*
  103. * Select a random virtual address using the extra entropy
  104. * available.
  105. */
  106. entropy = remain_entropy / (ARRAY_SIZE(kaslr_regions) - i);
  107. prandom_bytes_state(&rand_state, &rand, sizeof(rand));
  108. entropy = (rand % (entropy + 1)) & PUD_MASK;
  109. vaddr += entropy;
  110. *kaslr_regions[i].base = vaddr;
  111. /*
  112. * Jump the region and add a minimum padding based on
  113. * randomization alignment.
  114. */
  115. vaddr += get_padding(&kaslr_regions[i]);
  116. vaddr = round_up(vaddr + 1, PUD_SIZE);
  117. remain_entropy -= entropy;
  118. }
  119. }
  120. void __meminit init_trampoline_kaslr(void)
  121. {
  122. pud_t *pud_page_tramp, *pud, *pud_tramp;
  123. p4d_t *p4d_page_tramp, *p4d, *p4d_tramp;
  124. unsigned long paddr, vaddr;
  125. pgd_t *pgd;
  126. pud_page_tramp = alloc_low_page();
  127. /*
  128. * There are two mappings for the low 1MB area, the direct mapping
  129. * and the 1:1 mapping for the real mode trampoline:
  130. *
  131. * Direct mapping: virt_addr = phys_addr + PAGE_OFFSET
  132. * 1:1 mapping: virt_addr = phys_addr
  133. */
  134. paddr = 0;
  135. vaddr = (unsigned long)__va(paddr);
  136. pgd = pgd_offset_k(vaddr);
  137. p4d = p4d_offset(pgd, vaddr);
  138. pud = pud_offset(p4d, vaddr);
  139. pud_tramp = pud_page_tramp + pud_index(paddr);
  140. *pud_tramp = *pud;
  141. if (pgtable_l5_enabled()) {
  142. p4d_page_tramp = alloc_low_page();
  143. p4d_tramp = p4d_page_tramp + p4d_index(paddr);
  144. set_p4d(p4d_tramp,
  145. __p4d(_KERNPG_TABLE | __pa(pud_page_tramp)));
  146. trampoline_pgd_entry =
  147. __pgd(_KERNPG_TABLE | __pa(p4d_page_tramp));
  148. } else {
  149. trampoline_pgd_entry =
  150. __pgd(_KERNPG_TABLE | __pa(pud_page_tramp));
  151. }
  152. }