kasan_init.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * This file contains kasan initialization code for ARM64.
  4. *
  5. * Copyright (c) 2015 Samsung Electronics Co., Ltd.
  6. * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
  7. */
  8. #define pr_fmt(fmt) "kasan: " fmt
  9. #include <linux/kasan.h>
  10. #include <linux/kernel.h>
  11. #include <linux/sched/task.h>
  12. #include <linux/memblock.h>
  13. #include <linux/start_kernel.h>
  14. #include <linux/mm.h>
  15. #include <asm/mmu_context.h>
  16. #include <asm/kernel-pgtable.h>
  17. #include <asm/page.h>
  18. #include <asm/pgalloc.h>
  19. #include <asm/sections.h>
  20. #include <asm/tlbflush.h>
  21. #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
  22. static pgd_t tmp_pg_dir[PTRS_PER_PTE] __initdata __aligned(PAGE_SIZE);
  23. /*
  24. * The p*d_populate functions call virt_to_phys implicitly so they can't be used
  25. * directly on kernel symbols (bm_p*d). All the early functions are called too
  26. * early to use lm_alias so __p*d_populate functions must be used to populate
  27. * with the physical address from __pa_symbol.
  28. */
  29. static phys_addr_t __init kasan_alloc_zeroed_page(int node)
  30. {
  31. void *p = memblock_alloc_try_nid(PAGE_SIZE, PAGE_SIZE,
  32. __pa(MAX_DMA_ADDRESS),
  33. MEMBLOCK_ALLOC_NOLEAKTRACE, node);
  34. if (!p)
  35. panic("%s: Failed to allocate %lu bytes align=0x%lx nid=%d from=%llx\n",
  36. __func__, PAGE_SIZE, PAGE_SIZE, node,
  37. __pa(MAX_DMA_ADDRESS));
  38. return __pa(p);
  39. }
  40. static phys_addr_t __init kasan_alloc_raw_page(int node)
  41. {
  42. void *p = memblock_alloc_try_nid_raw(PAGE_SIZE, PAGE_SIZE,
  43. __pa(MAX_DMA_ADDRESS),
  44. MEMBLOCK_ALLOC_NOLEAKTRACE,
  45. node);
  46. if (!p)
  47. panic("%s: Failed to allocate %lu bytes align=0x%lx nid=%d from=%llx\n",
  48. __func__, PAGE_SIZE, PAGE_SIZE, node,
  49. __pa(MAX_DMA_ADDRESS));
  50. return __pa(p);
  51. }
  52. static pte_t *__init kasan_pte_offset(pmd_t *pmdp, unsigned long addr, int node,
  53. bool early)
  54. {
  55. if (pmd_none(READ_ONCE(*pmdp))) {
  56. phys_addr_t pte_phys = early ?
  57. __pa_symbol(kasan_early_shadow_pte)
  58. : kasan_alloc_zeroed_page(node);
  59. __pmd_populate(pmdp, pte_phys, PMD_TYPE_TABLE);
  60. }
  61. return early ? pte_offset_kimg(pmdp, addr)
  62. : pte_offset_kernel(pmdp, addr);
  63. }
  64. static pmd_t *__init kasan_pmd_offset(pud_t *pudp, unsigned long addr, int node,
  65. bool early)
  66. {
  67. if (pud_none(READ_ONCE(*pudp))) {
  68. phys_addr_t pmd_phys = early ?
  69. __pa_symbol(kasan_early_shadow_pmd)
  70. : kasan_alloc_zeroed_page(node);
  71. __pud_populate(pudp, pmd_phys, PUD_TYPE_TABLE);
  72. }
  73. return early ? pmd_offset_kimg(pudp, addr) : pmd_offset(pudp, addr);
  74. }
  75. static pud_t *__init kasan_pud_offset(p4d_t *p4dp, unsigned long addr, int node,
  76. bool early)
  77. {
  78. if (p4d_none(READ_ONCE(*p4dp))) {
  79. phys_addr_t pud_phys = early ?
  80. __pa_symbol(kasan_early_shadow_pud)
  81. : kasan_alloc_zeroed_page(node);
  82. __p4d_populate(p4dp, pud_phys, P4D_TYPE_TABLE);
  83. }
  84. return early ? pud_offset_kimg(p4dp, addr) : pud_offset(p4dp, addr);
  85. }
  86. static p4d_t *__init kasan_p4d_offset(pgd_t *pgdp, unsigned long addr, int node,
  87. bool early)
  88. {
  89. if (pgd_none(READ_ONCE(*pgdp))) {
  90. phys_addr_t p4d_phys = early ?
  91. __pa_symbol(kasan_early_shadow_p4d)
  92. : kasan_alloc_zeroed_page(node);
  93. __pgd_populate(pgdp, p4d_phys, PGD_TYPE_TABLE);
  94. }
  95. return early ? p4d_offset_kimg(pgdp, addr) : p4d_offset(pgdp, addr);
  96. }
  97. static void __init kasan_pte_populate(pmd_t *pmdp, unsigned long addr,
  98. unsigned long end, int node, bool early)
  99. {
  100. unsigned long next;
  101. pte_t *ptep = kasan_pte_offset(pmdp, addr, node, early);
  102. do {
  103. phys_addr_t page_phys = early ?
  104. __pa_symbol(kasan_early_shadow_page)
  105. : kasan_alloc_raw_page(node);
  106. if (!early)
  107. memset(__va(page_phys), KASAN_SHADOW_INIT, PAGE_SIZE);
  108. next = addr + PAGE_SIZE;
  109. __set_pte(ptep, pfn_pte(__phys_to_pfn(page_phys), PAGE_KERNEL));
  110. } while (ptep++, addr = next, addr != end && pte_none(__ptep_get(ptep)));
  111. }
  112. static void __init kasan_pmd_populate(pud_t *pudp, unsigned long addr,
  113. unsigned long end, int node, bool early)
  114. {
  115. unsigned long next;
  116. pmd_t *pmdp = kasan_pmd_offset(pudp, addr, node, early);
  117. do {
  118. next = pmd_addr_end(addr, end);
  119. kasan_pte_populate(pmdp, addr, next, node, early);
  120. } while (pmdp++, addr = next, addr != end && pmd_none(READ_ONCE(*pmdp)));
  121. }
  122. static void __init kasan_pud_populate(p4d_t *p4dp, unsigned long addr,
  123. unsigned long end, int node, bool early)
  124. {
  125. unsigned long next;
  126. pud_t *pudp = kasan_pud_offset(p4dp, addr, node, early);
  127. do {
  128. next = pud_addr_end(addr, end);
  129. kasan_pmd_populate(pudp, addr, next, node, early);
  130. } while (pudp++, addr = next, addr != end && pud_none(READ_ONCE(*pudp)));
  131. }
  132. static void __init kasan_p4d_populate(pgd_t *pgdp, unsigned long addr,
  133. unsigned long end, int node, bool early)
  134. {
  135. unsigned long next;
  136. p4d_t *p4dp = kasan_p4d_offset(pgdp, addr, node, early);
  137. do {
  138. next = p4d_addr_end(addr, end);
  139. kasan_pud_populate(p4dp, addr, next, node, early);
  140. } while (p4dp++, addr = next, addr != end && p4d_none(READ_ONCE(*p4dp)));
  141. }
  142. static void __init kasan_pgd_populate(unsigned long addr, unsigned long end,
  143. int node, bool early)
  144. {
  145. unsigned long next;
  146. pgd_t *pgdp;
  147. pgdp = pgd_offset_k(addr);
  148. do {
  149. next = pgd_addr_end(addr, end);
  150. kasan_p4d_populate(pgdp, addr, next, node, early);
  151. } while (pgdp++, addr = next, addr != end);
  152. }
  153. #if defined(CONFIG_ARM64_64K_PAGES) || CONFIG_PGTABLE_LEVELS > 4
  154. #define SHADOW_ALIGN P4D_SIZE
  155. #else
  156. #define SHADOW_ALIGN PUD_SIZE
  157. #endif
  158. /*
  159. * Return whether 'addr' is aligned to the size covered by a root level
  160. * descriptor.
  161. */
  162. static bool __init root_level_aligned(u64 addr)
  163. {
  164. int shift = (ARM64_HW_PGTABLE_LEVELS(vabits_actual) - 1) * (PAGE_SHIFT - 3);
  165. return (addr % (PAGE_SIZE << shift)) == 0;
  166. }
  167. /* The early shadow maps everything to a single page of zeroes */
  168. asmlinkage void __init kasan_early_init(void)
  169. {
  170. BUILD_BUG_ON(KASAN_SHADOW_OFFSET !=
  171. KASAN_SHADOW_END - (1UL << (64 - KASAN_SHADOW_SCALE_SHIFT)));
  172. BUILD_BUG_ON(!IS_ALIGNED(_KASAN_SHADOW_START(VA_BITS), SHADOW_ALIGN));
  173. BUILD_BUG_ON(!IS_ALIGNED(_KASAN_SHADOW_START(VA_BITS_MIN), SHADOW_ALIGN));
  174. BUILD_BUG_ON(!IS_ALIGNED(KASAN_SHADOW_END, SHADOW_ALIGN));
  175. if (!root_level_aligned(KASAN_SHADOW_START)) {
  176. /*
  177. * The start address is misaligned, and so the next level table
  178. * will be shared with the linear region. This can happen with
  179. * 4 or 5 level paging, so install a generic pte_t[] as the
  180. * next level. This prevents the kasan_pgd_populate call below
  181. * from inserting an entry that refers to the shared KASAN zero
  182. * shadow pud_t[]/p4d_t[], which could end up getting corrupted
  183. * when the linear region is mapped.
  184. */
  185. static pte_t tbl[PTRS_PER_PTE] __page_aligned_bss;
  186. pgd_t *pgdp = pgd_offset_k(KASAN_SHADOW_START);
  187. set_pgd(pgdp, __pgd(__pa_symbol(tbl) | PGD_TYPE_TABLE));
  188. }
  189. kasan_pgd_populate(KASAN_SHADOW_START, KASAN_SHADOW_END, NUMA_NO_NODE,
  190. true);
  191. }
  192. /* Set up full kasan mappings, ensuring that the mapped pages are zeroed */
  193. static void __init kasan_map_populate(unsigned long start, unsigned long end,
  194. int node)
  195. {
  196. kasan_pgd_populate(start & PAGE_MASK, PAGE_ALIGN(end), node, false);
  197. }
  198. /*
  199. * Return the descriptor index of 'addr' in the root level table
  200. */
  201. static int __init root_level_idx(u64 addr)
  202. {
  203. /*
  204. * On 64k pages, the TTBR1 range root tables are extended for 52-bit
  205. * virtual addressing, and TTBR1 will simply point to the pgd_t entry
  206. * that covers the start of the 48-bit addressable VA space if LVA is
  207. * not implemented. This means we need to index the table as usual,
  208. * instead of masking off bits based on vabits_actual.
  209. */
  210. u64 vabits = IS_ENABLED(CONFIG_ARM64_64K_PAGES) ? VA_BITS
  211. : vabits_actual;
  212. int shift = (ARM64_HW_PGTABLE_LEVELS(vabits) - 1) * (PAGE_SHIFT - 3);
  213. return (addr & ~_PAGE_OFFSET(vabits)) >> (shift + PAGE_SHIFT);
  214. }
  215. /*
  216. * Clone a next level table from swapper_pg_dir into tmp_pg_dir
  217. */
  218. static void __init clone_next_level(u64 addr, pgd_t *tmp_pg_dir, pud_t *pud)
  219. {
  220. int idx = root_level_idx(addr);
  221. pgd_t pgd = READ_ONCE(swapper_pg_dir[idx]);
  222. pud_t *pudp = (pud_t *)__phys_to_kimg(__pgd_to_phys(pgd));
  223. memcpy(pud, pudp, PAGE_SIZE);
  224. tmp_pg_dir[idx] = __pgd(__phys_to_pgd_val(__pa_symbol(pud)) |
  225. PUD_TYPE_TABLE);
  226. }
  227. /*
  228. * Return the descriptor index of 'addr' in the next level table
  229. */
  230. static int __init next_level_idx(u64 addr)
  231. {
  232. int shift = (ARM64_HW_PGTABLE_LEVELS(vabits_actual) - 2) * (PAGE_SHIFT - 3);
  233. return (addr >> (shift + PAGE_SHIFT)) % PTRS_PER_PTE;
  234. }
  235. /*
  236. * Dereference the table descriptor at 'pgd_idx' and clear the entries from
  237. * 'start' to 'end' (exclusive) from the table.
  238. */
  239. static void __init clear_next_level(int pgd_idx, int start, int end)
  240. {
  241. pgd_t pgd = READ_ONCE(swapper_pg_dir[pgd_idx]);
  242. pud_t *pudp = (pud_t *)__phys_to_kimg(__pgd_to_phys(pgd));
  243. memset(&pudp[start], 0, (end - start) * sizeof(pud_t));
  244. }
  245. static void __init clear_shadow(u64 start, u64 end)
  246. {
  247. int l = root_level_idx(start), m = root_level_idx(end);
  248. if (!root_level_aligned(start))
  249. clear_next_level(l++, next_level_idx(start), PTRS_PER_PTE);
  250. if (!root_level_aligned(end))
  251. clear_next_level(m, 0, next_level_idx(end));
  252. memset(&swapper_pg_dir[l], 0, (m - l) * sizeof(pgd_t));
  253. }
  254. static void __init kasan_init_shadow(void)
  255. {
  256. static pud_t pud[2][PTRS_PER_PUD] __initdata __aligned(PAGE_SIZE);
  257. u64 kimg_shadow_start, kimg_shadow_end;
  258. u64 mod_shadow_start;
  259. u64 vmalloc_shadow_end;
  260. phys_addr_t pa_start, pa_end;
  261. u64 i;
  262. kimg_shadow_start = (u64)kasan_mem_to_shadow(KERNEL_START) & PAGE_MASK;
  263. kimg_shadow_end = PAGE_ALIGN((u64)kasan_mem_to_shadow(KERNEL_END));
  264. mod_shadow_start = (u64)kasan_mem_to_shadow((void *)MODULES_VADDR);
  265. vmalloc_shadow_end = (u64)kasan_mem_to_shadow((void *)VMALLOC_END);
  266. /*
  267. * We are going to perform proper setup of shadow memory.
  268. * At first we should unmap early shadow (clear_pgds() call below).
  269. * However, instrumented code couldn't execute without shadow memory.
  270. * tmp_pg_dir used to keep early shadow mapped until full shadow
  271. * setup will be finished.
  272. */
  273. memcpy(tmp_pg_dir, swapper_pg_dir, sizeof(tmp_pg_dir));
  274. /*
  275. * If the start or end address of the shadow region is not aligned to
  276. * the root level size, we have to allocate a temporary next-level table
  277. * in each case, clone the next level of descriptors, and install the
  278. * table into tmp_pg_dir. Note that with 5 levels of paging, the next
  279. * level will in fact be p4d_t, but that makes no difference in this
  280. * case.
  281. */
  282. if (!root_level_aligned(KASAN_SHADOW_START))
  283. clone_next_level(KASAN_SHADOW_START, tmp_pg_dir, pud[0]);
  284. if (!root_level_aligned(KASAN_SHADOW_END))
  285. clone_next_level(KASAN_SHADOW_END, tmp_pg_dir, pud[1]);
  286. dsb(ishst);
  287. cpu_replace_ttbr1(lm_alias(tmp_pg_dir));
  288. clear_shadow(KASAN_SHADOW_START, KASAN_SHADOW_END);
  289. kasan_map_populate(kimg_shadow_start, kimg_shadow_end,
  290. early_pfn_to_nid(virt_to_pfn(lm_alias(KERNEL_START))));
  291. kasan_populate_early_shadow(kasan_mem_to_shadow((void *)PAGE_END),
  292. (void *)mod_shadow_start);
  293. BUILD_BUG_ON(VMALLOC_START != MODULES_END);
  294. kasan_populate_early_shadow((void *)vmalloc_shadow_end,
  295. (void *)KASAN_SHADOW_END);
  296. for_each_mem_range(i, &pa_start, &pa_end) {
  297. void *start = (void *)__phys_to_virt(pa_start);
  298. void *end = (void *)__phys_to_virt(pa_end);
  299. if (start >= end)
  300. break;
  301. kasan_map_populate((unsigned long)kasan_mem_to_shadow(start),
  302. (unsigned long)kasan_mem_to_shadow(end),
  303. early_pfn_to_nid(virt_to_pfn(start)));
  304. }
  305. /*
  306. * KAsan may reuse the contents of kasan_early_shadow_pte directly,
  307. * so we should make sure that it maps the zero page read-only.
  308. */
  309. for (i = 0; i < PTRS_PER_PTE; i++)
  310. __set_pte(&kasan_early_shadow_pte[i],
  311. pfn_pte(sym_to_pfn(kasan_early_shadow_page),
  312. PAGE_KERNEL_RO));
  313. memset(kasan_early_shadow_page, KASAN_SHADOW_INIT, PAGE_SIZE);
  314. cpu_replace_ttbr1(lm_alias(swapper_pg_dir));
  315. }
  316. static void __init kasan_init_depth(void)
  317. {
  318. init_task.kasan_depth = 0;
  319. }
  320. #ifdef CONFIG_KASAN_VMALLOC
  321. void __init kasan_populate_early_vm_area_shadow(void *start, unsigned long size)
  322. {
  323. unsigned long shadow_start, shadow_end;
  324. if (!is_vmalloc_or_module_addr(start))
  325. return;
  326. shadow_start = (unsigned long)kasan_mem_to_shadow(start);
  327. shadow_start = ALIGN_DOWN(shadow_start, PAGE_SIZE);
  328. shadow_end = (unsigned long)kasan_mem_to_shadow(start + size);
  329. shadow_end = ALIGN(shadow_end, PAGE_SIZE);
  330. kasan_map_populate(shadow_start, shadow_end, NUMA_NO_NODE);
  331. }
  332. #endif
  333. void __init kasan_init(void)
  334. {
  335. kasan_init_shadow();
  336. kasan_init_depth();
  337. #if defined(CONFIG_KASAN_GENERIC)
  338. /*
  339. * Generic KASAN is now fully initialized.
  340. * Software and Hardware Tag-Based modes still require
  341. * kasan_init_sw_tags() and kasan_init_hw_tags() correspondingly.
  342. */
  343. pr_info("KernelAddressSanitizer initialized (generic)\n");
  344. #endif
  345. }
  346. #endif /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */