kasan_init.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * This file contains kasan initialization code for ARM64.
  3. *
  4. * Copyright (c) 2015 Samsung Electronics Co., Ltd.
  5. * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. */
  12. #define pr_fmt(fmt) "kasan: " fmt
  13. #include <linux/kasan.h>
  14. #include <linux/kernel.h>
  15. #include <linux/sched/task.h>
  16. #include <linux/memblock.h>
  17. #include <linux/start_kernel.h>
  18. #include <linux/mm.h>
  19. #include <asm/mmu_context.h>
  20. #include <asm/kernel-pgtable.h>
  21. #include <asm/page.h>
  22. #include <asm/pgalloc.h>
  23. #include <asm/pgtable.h>
  24. #include <asm/sections.h>
  25. #include <asm/tlbflush.h>
  26. static pgd_t tmp_pg_dir[PTRS_PER_PGD] __initdata __aligned(PGD_SIZE);
  27. /*
  28. * The p*d_populate functions call virt_to_phys implicitly so they can't be used
  29. * directly on kernel symbols (bm_p*d). All the early functions are called too
  30. * early to use lm_alias so __p*d_populate functions must be used to populate
  31. * with the physical address from __pa_symbol.
  32. */
  33. static phys_addr_t __init kasan_alloc_zeroed_page(int node)
  34. {
  35. void *p = memblock_alloc_try_nid(PAGE_SIZE, PAGE_SIZE,
  36. __pa(MAX_DMA_ADDRESS),
  37. MEMBLOCK_ALLOC_ACCESSIBLE, node);
  38. return __pa(p);
  39. }
  40. static pte_t *__init kasan_pte_offset(pmd_t *pmdp, unsigned long addr, int node,
  41. bool early)
  42. {
  43. if (pmd_none(READ_ONCE(*pmdp))) {
  44. phys_addr_t pte_phys = early ? __pa_symbol(kasan_zero_pte)
  45. : kasan_alloc_zeroed_page(node);
  46. __pmd_populate(pmdp, pte_phys, PMD_TYPE_TABLE);
  47. }
  48. return early ? pte_offset_kimg(pmdp, addr)
  49. : pte_offset_kernel(pmdp, addr);
  50. }
  51. static pmd_t *__init kasan_pmd_offset(pud_t *pudp, unsigned long addr, int node,
  52. bool early)
  53. {
  54. if (pud_none(READ_ONCE(*pudp))) {
  55. phys_addr_t pmd_phys = early ? __pa_symbol(kasan_zero_pmd)
  56. : kasan_alloc_zeroed_page(node);
  57. __pud_populate(pudp, pmd_phys, PMD_TYPE_TABLE);
  58. }
  59. return early ? pmd_offset_kimg(pudp, addr) : pmd_offset(pudp, addr);
  60. }
  61. static pud_t *__init kasan_pud_offset(pgd_t *pgdp, unsigned long addr, int node,
  62. bool early)
  63. {
  64. if (pgd_none(READ_ONCE(*pgdp))) {
  65. phys_addr_t pud_phys = early ? __pa_symbol(kasan_zero_pud)
  66. : kasan_alloc_zeroed_page(node);
  67. __pgd_populate(pgdp, pud_phys, PMD_TYPE_TABLE);
  68. }
  69. return early ? pud_offset_kimg(pgdp, addr) : pud_offset(pgdp, addr);
  70. }
  71. static void __init kasan_pte_populate(pmd_t *pmdp, unsigned long addr,
  72. unsigned long end, int node, bool early)
  73. {
  74. unsigned long next;
  75. pte_t *ptep = kasan_pte_offset(pmdp, addr, node, early);
  76. do {
  77. phys_addr_t page_phys = early ? __pa_symbol(kasan_zero_page)
  78. : kasan_alloc_zeroed_page(node);
  79. next = addr + PAGE_SIZE;
  80. set_pte(ptep, pfn_pte(__phys_to_pfn(page_phys), PAGE_KERNEL));
  81. } while (ptep++, addr = next, addr != end && pte_none(READ_ONCE(*ptep)));
  82. }
  83. static void __init kasan_pmd_populate(pud_t *pudp, unsigned long addr,
  84. unsigned long end, int node, bool early)
  85. {
  86. unsigned long next;
  87. pmd_t *pmdp = kasan_pmd_offset(pudp, addr, node, early);
  88. do {
  89. next = pmd_addr_end(addr, end);
  90. kasan_pte_populate(pmdp, addr, next, node, early);
  91. } while (pmdp++, addr = next, addr != end && pmd_none(READ_ONCE(*pmdp)));
  92. }
  93. static void __init kasan_pud_populate(pgd_t *pgdp, unsigned long addr,
  94. unsigned long end, int node, bool early)
  95. {
  96. unsigned long next;
  97. pud_t *pudp = kasan_pud_offset(pgdp, addr, node, early);
  98. do {
  99. next = pud_addr_end(addr, end);
  100. kasan_pmd_populate(pudp, addr, next, node, early);
  101. } while (pudp++, addr = next, addr != end && pud_none(READ_ONCE(*pudp)));
  102. }
  103. static void __init kasan_pgd_populate(unsigned long addr, unsigned long end,
  104. int node, bool early)
  105. {
  106. unsigned long next;
  107. pgd_t *pgdp;
  108. pgdp = pgd_offset_k(addr);
  109. do {
  110. next = pgd_addr_end(addr, end);
  111. kasan_pud_populate(pgdp, addr, next, node, early);
  112. } while (pgdp++, addr = next, addr != end);
  113. }
  114. /* The early shadow maps everything to a single page of zeroes */
  115. asmlinkage void __init kasan_early_init(void)
  116. {
  117. BUILD_BUG_ON(KASAN_SHADOW_OFFSET !=
  118. KASAN_SHADOW_END - (1UL << (64 - KASAN_SHADOW_SCALE_SHIFT)));
  119. BUILD_BUG_ON(!IS_ALIGNED(KASAN_SHADOW_START, PGDIR_SIZE));
  120. BUILD_BUG_ON(!IS_ALIGNED(KASAN_SHADOW_END, PGDIR_SIZE));
  121. kasan_pgd_populate(KASAN_SHADOW_START, KASAN_SHADOW_END, NUMA_NO_NODE,
  122. true);
  123. }
  124. /* Set up full kasan mappings, ensuring that the mapped pages are zeroed */
  125. static void __init kasan_map_populate(unsigned long start, unsigned long end,
  126. int node)
  127. {
  128. kasan_pgd_populate(start & PAGE_MASK, PAGE_ALIGN(end), node, false);
  129. }
  130. /*
  131. * Copy the current shadow region into a new pgdir.
  132. */
  133. void __init kasan_copy_shadow(pgd_t *pgdir)
  134. {
  135. pgd_t *pgdp, *pgdp_new, *pgdp_end;
  136. pgdp = pgd_offset_k(KASAN_SHADOW_START);
  137. pgdp_end = pgd_offset_k(KASAN_SHADOW_END);
  138. pgdp_new = pgd_offset_raw(pgdir, KASAN_SHADOW_START);
  139. do {
  140. set_pgd(pgdp_new, READ_ONCE(*pgdp));
  141. } while (pgdp++, pgdp_new++, pgdp != pgdp_end);
  142. }
  143. static void __init clear_pgds(unsigned long start,
  144. unsigned long end)
  145. {
  146. /*
  147. * Remove references to kasan page tables from
  148. * swapper_pg_dir. pgd_clear() can't be used
  149. * here because it's nop on 2,3-level pagetable setups
  150. */
  151. for (; start < end; start += PGDIR_SIZE)
  152. set_pgd(pgd_offset_k(start), __pgd(0));
  153. }
  154. void __init kasan_init(void)
  155. {
  156. u64 kimg_shadow_start, kimg_shadow_end;
  157. u64 mod_shadow_start, mod_shadow_end;
  158. struct memblock_region *reg;
  159. int i;
  160. kimg_shadow_start = (u64)kasan_mem_to_shadow(_text) & PAGE_MASK;
  161. kimg_shadow_end = PAGE_ALIGN((u64)kasan_mem_to_shadow(_end));
  162. mod_shadow_start = (u64)kasan_mem_to_shadow((void *)MODULES_VADDR);
  163. mod_shadow_end = (u64)kasan_mem_to_shadow((void *)MODULES_END);
  164. /*
  165. * We are going to perform proper setup of shadow memory.
  166. * At first we should unmap early shadow (clear_pgds() call below).
  167. * However, instrumented code couldn't execute without shadow memory.
  168. * tmp_pg_dir used to keep early shadow mapped until full shadow
  169. * setup will be finished.
  170. */
  171. memcpy(tmp_pg_dir, swapper_pg_dir, sizeof(tmp_pg_dir));
  172. dsb(ishst);
  173. cpu_replace_ttbr1(lm_alias(tmp_pg_dir));
  174. clear_pgds(KASAN_SHADOW_START, KASAN_SHADOW_END);
  175. kasan_map_populate(kimg_shadow_start, kimg_shadow_end,
  176. early_pfn_to_nid(virt_to_pfn(lm_alias(_text))));
  177. kasan_populate_zero_shadow((void *)KASAN_SHADOW_START,
  178. (void *)mod_shadow_start);
  179. kasan_populate_zero_shadow((void *)kimg_shadow_end,
  180. kasan_mem_to_shadow((void *)PAGE_OFFSET));
  181. if (kimg_shadow_start > mod_shadow_end)
  182. kasan_populate_zero_shadow((void *)mod_shadow_end,
  183. (void *)kimg_shadow_start);
  184. for_each_memblock(memory, reg) {
  185. void *start = (void *)__phys_to_virt(reg->base);
  186. void *end = (void *)__phys_to_virt(reg->base + reg->size);
  187. if (start >= end)
  188. break;
  189. kasan_map_populate((unsigned long)kasan_mem_to_shadow(start),
  190. (unsigned long)kasan_mem_to_shadow(end),
  191. early_pfn_to_nid(virt_to_pfn(start)));
  192. }
  193. /*
  194. * KAsan may reuse the contents of kasan_zero_pte directly, so we
  195. * should make sure that it maps the zero page read-only.
  196. */
  197. for (i = 0; i < PTRS_PER_PTE; i++)
  198. set_pte(&kasan_zero_pte[i],
  199. pfn_pte(sym_to_pfn(kasan_zero_page), PAGE_KERNEL_RO));
  200. memset(kasan_zero_page, 0, PAGE_SIZE);
  201. cpu_replace_ttbr1(lm_alias(swapper_pg_dir));
  202. /* At this point kasan is fully initialized. Enable error messages */
  203. init_task.kasan_depth = 0;
  204. pr_info("KernelAddressSanitizer initialized\n");
  205. }