kasan_init.c 7.4 KB

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