kasan_init_64.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #define DISABLE_BRANCH_PROFILING
  2. #define pr_fmt(fmt) "kasan: " fmt
  3. #include <linux/bootmem.h>
  4. #include <linux/kasan.h>
  5. #include <linux/kdebug.h>
  6. #include <linux/mm.h>
  7. #include <linux/sched.h>
  8. #include <linux/vmalloc.h>
  9. #include <asm/tlbflush.h>
  10. #include <asm/sections.h>
  11. extern pgd_t early_level4_pgt[PTRS_PER_PGD];
  12. extern struct range pfn_mapped[E820_X_MAX];
  13. static int __init map_range(struct range *range)
  14. {
  15. unsigned long start;
  16. unsigned long end;
  17. start = (unsigned long)kasan_mem_to_shadow(pfn_to_kaddr(range->start));
  18. end = (unsigned long)kasan_mem_to_shadow(pfn_to_kaddr(range->end));
  19. /*
  20. * end + 1 here is intentional. We check several shadow bytes in advance
  21. * to slightly speed up fastpath. In some rare cases we could cross
  22. * boundary of mapped shadow, so we just map some more here.
  23. */
  24. return vmemmap_populate(start, end + 1, NUMA_NO_NODE);
  25. }
  26. static void __init clear_pgds(unsigned long start,
  27. unsigned long end)
  28. {
  29. for (; start < end; start += PGDIR_SIZE)
  30. pgd_clear(pgd_offset_k(start));
  31. }
  32. static void __init kasan_map_early_shadow(pgd_t *pgd)
  33. {
  34. int i;
  35. unsigned long start = KASAN_SHADOW_START;
  36. unsigned long end = KASAN_SHADOW_END;
  37. for (i = pgd_index(start); start < end; i++) {
  38. pgd[i] = __pgd(__pa_nodebug(kasan_zero_pud)
  39. | _KERNPG_TABLE);
  40. start += PGDIR_SIZE;
  41. }
  42. }
  43. #ifdef CONFIG_KASAN_INLINE
  44. static int kasan_die_handler(struct notifier_block *self,
  45. unsigned long val,
  46. void *data)
  47. {
  48. if (val == DIE_GPF) {
  49. pr_emerg("CONFIG_KASAN_INLINE enabled");
  50. pr_emerg("GPF could be caused by NULL-ptr deref or user memory access");
  51. }
  52. return NOTIFY_OK;
  53. }
  54. static struct notifier_block kasan_die_notifier = {
  55. .notifier_call = kasan_die_handler,
  56. };
  57. #endif
  58. void __init kasan_early_init(void)
  59. {
  60. int i;
  61. pteval_t pte_val = __pa_nodebug(kasan_zero_page) | __PAGE_KERNEL;
  62. pmdval_t pmd_val = __pa_nodebug(kasan_zero_pte) | _KERNPG_TABLE;
  63. pudval_t pud_val = __pa_nodebug(kasan_zero_pmd) | _KERNPG_TABLE;
  64. for (i = 0; i < PTRS_PER_PTE; i++)
  65. kasan_zero_pte[i] = __pte(pte_val);
  66. for (i = 0; i < PTRS_PER_PMD; i++)
  67. kasan_zero_pmd[i] = __pmd(pmd_val);
  68. for (i = 0; i < PTRS_PER_PUD; i++)
  69. kasan_zero_pud[i] = __pud(pud_val);
  70. kasan_map_early_shadow(early_level4_pgt);
  71. kasan_map_early_shadow(init_level4_pgt);
  72. }
  73. void __init kasan_init(void)
  74. {
  75. int i;
  76. #ifdef CONFIG_KASAN_INLINE
  77. register_die_notifier(&kasan_die_notifier);
  78. #endif
  79. memcpy(early_level4_pgt, init_level4_pgt, sizeof(early_level4_pgt));
  80. load_cr3(early_level4_pgt);
  81. __flush_tlb_all();
  82. clear_pgds(KASAN_SHADOW_START, KASAN_SHADOW_END);
  83. kasan_populate_zero_shadow((void *)KASAN_SHADOW_START,
  84. kasan_mem_to_shadow((void *)PAGE_OFFSET));
  85. for (i = 0; i < E820_X_MAX; i++) {
  86. if (pfn_mapped[i].end == 0)
  87. break;
  88. if (map_range(&pfn_mapped[i]))
  89. panic("kasan: unable to allocate shadow!");
  90. }
  91. kasan_populate_zero_shadow(
  92. kasan_mem_to_shadow((void *)PAGE_OFFSET + MAXMEM),
  93. kasan_mem_to_shadow((void *)__START_KERNEL_map));
  94. vmemmap_populate((unsigned long)kasan_mem_to_shadow(_stext),
  95. (unsigned long)kasan_mem_to_shadow(_end),
  96. NUMA_NO_NODE);
  97. kasan_populate_zero_shadow(kasan_mem_to_shadow((void *)MODULES_END),
  98. (void *)KASAN_SHADOW_END);
  99. load_cr3(init_level4_pgt);
  100. __flush_tlb_all();
  101. /*
  102. * kasan_zero_page has been used as early shadow memory, thus it may
  103. * contain some garbage. Now we can clear and write protect it, since
  104. * after the TLB flush no one should write to it.
  105. */
  106. memset(kasan_zero_page, 0, PAGE_SIZE);
  107. for (i = 0; i < PTRS_PER_PTE; i++) {
  108. pte_t pte = __pte(__pa(kasan_zero_page) | __PAGE_KERNEL_RO);
  109. set_pte(&kasan_zero_pte[i], pte);
  110. }
  111. /* Flush TLBs again to be sure that write protection applied. */
  112. __flush_tlb_all();
  113. init_task.kasan_depth = 0;
  114. pr_info("KernelAddressSanitizer initialized\n");
  115. }