kasan_init.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * This file contains kasan initialization code for ARM.
  4. *
  5. * Copyright (c) 2018 Samsung Electronics Co., Ltd.
  6. * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
  7. * Author: Linus Walleij <linus.walleij@linaro.org>
  8. */
  9. #define pr_fmt(fmt) "kasan: " fmt
  10. #include <linux/kasan.h>
  11. #include <linux/kernel.h>
  12. #include <linux/memblock.h>
  13. #include <linux/sched/task.h>
  14. #include <linux/start_kernel.h>
  15. #include <linux/pgtable.h>
  16. #include <asm/cputype.h>
  17. #include <asm/highmem.h>
  18. #include <asm/mach/map.h>
  19. #include <asm/page.h>
  20. #include <asm/pgalloc.h>
  21. #include <asm/procinfo.h>
  22. #include <asm/proc-fns.h>
  23. #include "mm.h"
  24. static pgd_t tmp_pgd_table[PTRS_PER_PGD] __initdata __aligned(PGD_SIZE);
  25. pmd_t tmp_pmd_table[PTRS_PER_PMD] __page_aligned_bss;
  26. static __init void *kasan_alloc_block_raw(size_t size)
  27. {
  28. return memblock_alloc_try_nid_raw(size, size, __pa(MAX_DMA_ADDRESS),
  29. MEMBLOCK_ALLOC_NOLEAKTRACE, NUMA_NO_NODE);
  30. }
  31. static __init void *kasan_alloc_block(size_t size)
  32. {
  33. return memblock_alloc_try_nid(size, size, __pa(MAX_DMA_ADDRESS),
  34. MEMBLOCK_ALLOC_NOLEAKTRACE, NUMA_NO_NODE);
  35. }
  36. static void __init kasan_pte_populate(pmd_t *pmdp, unsigned long addr,
  37. unsigned long end, bool early)
  38. {
  39. unsigned long next;
  40. pte_t *ptep = pte_offset_kernel(pmdp, addr);
  41. do {
  42. pte_t entry;
  43. void *p;
  44. next = addr + PAGE_SIZE;
  45. if (!early) {
  46. if (!pte_none(READ_ONCE(*ptep)))
  47. continue;
  48. p = kasan_alloc_block_raw(PAGE_SIZE);
  49. if (!p) {
  50. panic("%s failed to allocate shadow page for address 0x%lx\n",
  51. __func__, addr);
  52. return;
  53. }
  54. memset(p, KASAN_SHADOW_INIT, PAGE_SIZE);
  55. entry = pfn_pte(virt_to_pfn(p),
  56. __pgprot(pgprot_val(PAGE_KERNEL)));
  57. } else if (pte_none(READ_ONCE(*ptep))) {
  58. /*
  59. * The early shadow memory is mapping all KASan
  60. * operations to one and the same page in memory,
  61. * "kasan_early_shadow_page" so that the instrumentation
  62. * will work on a scratch area until we can set up the
  63. * proper KASan shadow memory.
  64. */
  65. entry = pfn_pte(virt_to_pfn(kasan_early_shadow_page),
  66. __pgprot(_L_PTE_DEFAULT | L_PTE_DIRTY | L_PTE_XN));
  67. } else {
  68. /*
  69. * Early shadow mappings are PMD_SIZE aligned, so if the
  70. * first entry is already set, they must all be set.
  71. */
  72. return;
  73. }
  74. set_pte_at(&init_mm, addr, ptep, entry);
  75. } while (ptep++, addr = next, addr != end);
  76. }
  77. /*
  78. * The pmd (page middle directory) is only used on LPAE
  79. */
  80. static void __init kasan_pmd_populate(pud_t *pudp, unsigned long addr,
  81. unsigned long end, bool early)
  82. {
  83. unsigned long next;
  84. pmd_t *pmdp = pmd_offset(pudp, addr);
  85. do {
  86. if (pmd_none(*pmdp)) {
  87. /*
  88. * We attempt to allocate a shadow block for the PMDs
  89. * used by the PTEs for this address if it isn't already
  90. * allocated.
  91. */
  92. void *p = early ? kasan_early_shadow_pte :
  93. kasan_alloc_block(PAGE_SIZE);
  94. if (!p) {
  95. panic("%s failed to allocate shadow block for address 0x%lx\n",
  96. __func__, addr);
  97. return;
  98. }
  99. pmd_populate_kernel(&init_mm, pmdp, p);
  100. flush_pmd_entry(pmdp);
  101. }
  102. next = pmd_addr_end(addr, end);
  103. kasan_pte_populate(pmdp, addr, next, early);
  104. } while (pmdp++, addr = next, addr != end);
  105. }
  106. static void __init kasan_pgd_populate(unsigned long addr, unsigned long end,
  107. bool early)
  108. {
  109. unsigned long next;
  110. pgd_t *pgdp;
  111. p4d_t *p4dp;
  112. pud_t *pudp;
  113. pgdp = pgd_offset_k(addr);
  114. do {
  115. /*
  116. * Allocate and populate the shadow block of p4d folded into
  117. * pud folded into pmd if it doesn't already exist
  118. */
  119. if (!early && pgd_none(*pgdp)) {
  120. void *p = kasan_alloc_block(PAGE_SIZE);
  121. if (!p) {
  122. panic("%s failed to allocate shadow block for address 0x%lx\n",
  123. __func__, addr);
  124. return;
  125. }
  126. pgd_populate(&init_mm, pgdp, p);
  127. }
  128. next = pgd_addr_end(addr, end);
  129. /*
  130. * We just immediately jump over the p4d and pud page
  131. * directories since we believe ARM32 will never gain four
  132. * nor five level page tables.
  133. */
  134. p4dp = p4d_offset(pgdp, addr);
  135. pudp = pud_offset(p4dp, addr);
  136. kasan_pmd_populate(pudp, addr, next, early);
  137. } while (pgdp++, addr = next, addr != end);
  138. }
  139. extern struct proc_info_list *lookup_processor_type(unsigned int);
  140. void __init kasan_early_init(void)
  141. {
  142. struct proc_info_list *list;
  143. /*
  144. * locate processor in the list of supported processor
  145. * types. The linker builds this table for us from the
  146. * entries in arch/arm/mm/proc-*.S
  147. */
  148. list = lookup_processor_type(read_cpuid_id());
  149. if (list) {
  150. #ifdef MULTI_CPU
  151. processor = *list->proc;
  152. #endif
  153. }
  154. BUILD_BUG_ON((KASAN_SHADOW_END - (1UL << 29)) != KASAN_SHADOW_OFFSET);
  155. /*
  156. * We walk the page table and set all of the shadow memory to point
  157. * to the scratch page.
  158. */
  159. kasan_pgd_populate(KASAN_SHADOW_START, KASAN_SHADOW_END, true);
  160. }
  161. static void __init clear_pgds(unsigned long start,
  162. unsigned long end)
  163. {
  164. for (; start && start < end; start += PMD_SIZE)
  165. pmd_clear(pmd_off_k(start));
  166. }
  167. static int __init create_mapping(void *start, void *end)
  168. {
  169. void *shadow_start, *shadow_end;
  170. shadow_start = kasan_mem_to_shadow(start);
  171. shadow_end = kasan_mem_to_shadow(end);
  172. pr_info("Mapping kernel virtual memory block: %px-%px at shadow: %px-%px\n",
  173. start, end, shadow_start, shadow_end);
  174. kasan_pgd_populate((unsigned long)shadow_start & PAGE_MASK,
  175. PAGE_ALIGN((unsigned long)shadow_end), false);
  176. return 0;
  177. }
  178. void __init kasan_init(void)
  179. {
  180. phys_addr_t pa_start, pa_end;
  181. u64 i;
  182. /*
  183. * We are going to perform proper setup of shadow memory.
  184. *
  185. * At first we should unmap early shadow (clear_pgds() call bellow).
  186. * However, instrumented code can't execute without shadow memory.
  187. *
  188. * To keep the early shadow memory MMU tables around while setting up
  189. * the proper shadow memory, we copy swapper_pg_dir (the initial page
  190. * table) to tmp_pgd_table and use that to keep the early shadow memory
  191. * mapped until the full shadow setup is finished. Then we swap back
  192. * to the proper swapper_pg_dir.
  193. */
  194. memcpy(tmp_pgd_table, swapper_pg_dir, sizeof(tmp_pgd_table));
  195. #ifdef CONFIG_ARM_LPAE
  196. /* We need to be in the same PGD or this won't work */
  197. BUILD_BUG_ON(pgd_index(KASAN_SHADOW_START) !=
  198. pgd_index(KASAN_SHADOW_END));
  199. memcpy(tmp_pmd_table,
  200. (void*)pgd_page_vaddr(*pgd_offset_k(KASAN_SHADOW_START)),
  201. sizeof(tmp_pmd_table));
  202. set_pgd(&tmp_pgd_table[pgd_index(KASAN_SHADOW_START)],
  203. __pgd(__pa(tmp_pmd_table) | PMD_TYPE_TABLE | L_PGD_SWAPPER));
  204. #endif
  205. cpu_switch_mm(tmp_pgd_table, &init_mm);
  206. local_flush_tlb_all();
  207. clear_pgds(KASAN_SHADOW_START, KASAN_SHADOW_END);
  208. if (!IS_ENABLED(CONFIG_KASAN_VMALLOC))
  209. kasan_populate_early_shadow(kasan_mem_to_shadow((void *)VMALLOC_START),
  210. kasan_mem_to_shadow((void *)VMALLOC_END));
  211. kasan_populate_early_shadow(kasan_mem_to_shadow((void *)VMALLOC_END),
  212. kasan_mem_to_shadow((void *)-1UL) + 1);
  213. for_each_mem_range(i, &pa_start, &pa_end) {
  214. void *start = __va(pa_start);
  215. void *end = __va(pa_end);
  216. /* Do not attempt to shadow highmem */
  217. if (pa_start >= arm_lowmem_limit) {
  218. pr_info("Skip highmem block at %pa-%pa\n", &pa_start, &pa_end);
  219. continue;
  220. }
  221. if (pa_end > arm_lowmem_limit) {
  222. pr_info("Truncating shadow for memory block at %pa-%pa to lowmem region at %pa\n",
  223. &pa_start, &pa_end, &arm_lowmem_limit);
  224. end = __va(arm_lowmem_limit);
  225. }
  226. if (start >= end) {
  227. pr_info("Skipping invalid memory block %pa-%pa (virtual %p-%p)\n",
  228. &pa_start, &pa_end, start, end);
  229. continue;
  230. }
  231. create_mapping(start, end);
  232. }
  233. /*
  234. * 1. The module global variables are in MODULES_VADDR ~ MODULES_END,
  235. * so we need to map this area if CONFIG_KASAN_VMALLOC=n. With
  236. * VMALLOC support KASAN will manage this region dynamically,
  237. * refer to kasan_populate_vmalloc() and ARM's implementation of
  238. * module_alloc().
  239. * 2. PKMAP_BASE ~ PKMAP_BASE+PMD_SIZE's shadow and MODULES_VADDR
  240. * ~ MODULES_END's shadow is in the same PMD_SIZE, so we can't
  241. * use kasan_populate_zero_shadow.
  242. */
  243. if (!IS_ENABLED(CONFIG_KASAN_VMALLOC) && IS_ENABLED(CONFIG_MODULES))
  244. create_mapping((void *)MODULES_VADDR, (void *)(MODULES_END));
  245. create_mapping((void *)PKMAP_BASE, (void *)(PKMAP_BASE + PMD_SIZE));
  246. /*
  247. * KAsan may reuse the contents of kasan_early_shadow_pte directly, so
  248. * we should make sure that it maps the zero page read-only.
  249. */
  250. for (i = 0; i < PTRS_PER_PTE; i++)
  251. set_pte_at(&init_mm, KASAN_SHADOW_START + i*PAGE_SIZE,
  252. &kasan_early_shadow_pte[i],
  253. pfn_pte(virt_to_pfn(kasan_early_shadow_page),
  254. __pgprot(pgprot_val(PAGE_KERNEL)
  255. | L_PTE_RDONLY)));
  256. cpu_switch_mm(swapper_pg_dir, &init_mm);
  257. local_flush_tlb_all();
  258. memset(kasan_early_shadow_page, 0, PAGE_SIZE);
  259. pr_info("Kernel address sanitizer initialized\n");
  260. init_task.kasan_depth = 0;
  261. }