1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * This file contains core tag-based KASAN code. 4 * 5 * Copyright (c) 2018 Google, Inc. 6 * Author: Andrey Konovalov <andreyknvl@google.com> 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License version 2 as 10 * published by the Free Software Foundation. 11 * 12 */ 13 14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 15 16 #include <linux/export.h> 17 #include <linux/interrupt.h> 18 #include <linux/init.h> 19 #include <linux/kasan.h> 20 #include <linux/kernel.h> 21 #include <linux/kmemleak.h> 22 #include <linux/linkage.h> 23 #include <linux/memblock.h> 24 #include <linux/memory.h> 25 #include <linux/mm.h> 26 #include <linux/module.h> 27 #include <linux/printk.h> 28 #include <linux/random.h> 29 #include <linux/sched.h> 30 #include <linux/sched/task_stack.h> 31 #include <linux/slab.h> 32 #include <linux/stacktrace.h> 33 #include <linux/string.h> 34 #include <linux/types.h> 35 #include <linux/vmalloc.h> 36 #include <linux/bug.h> 37 38 #include "kasan.h" 39 #include "../slab.h" 40 41 static DEFINE_PER_CPU(u32, prng_state); 42 43 void kasan_init_tags(void) 44 { 45 int cpu; 46 47 for_each_possible_cpu(cpu) 48 per_cpu(prng_state, cpu) = (u32)get_cycles(); 49 } 50 51 /* 52 * If a preemption happens between this_cpu_read and this_cpu_write, the only 53 * side effect is that we'll give a few allocated in different contexts objects 54 * the same tag. Since tag-based KASAN is meant to be used a probabilistic 55 * bug-detection debug feature, this doesn't have significant negative impact. 56 * 57 * Ideally the tags use strong randomness to prevent any attempts to predict 58 * them during explicit exploit attempts. But strong randomness is expensive, 59 * and we did an intentional trade-off to use a PRNG. This non-atomic RMW 60 * sequence has in fact positive effect, since interrupts that randomly skew 61 * PRNG at unpredictable points do only good. 62 */ 63 u8 random_tag(void) 64 { 65 u32 state = this_cpu_read(prng_state); 66 67 state = 1664525 * state + 1013904223; 68 this_cpu_write(prng_state, state); 69 70 return (u8)(state % (KASAN_TAG_MAX + 1)); 71 } 72 73 void *kasan_reset_tag(const void *addr) 74 { 75 return reset_tag(addr); 76 } 77 78 bool check_memory_region(unsigned long addr, size_t size, bool write, 79 unsigned long ret_ip) 80 { 81 u8 tag; 82 u8 *shadow_first, *shadow_last, *shadow; 83 void *untagged_addr; 84 85 if (unlikely(size == 0)) 86 return true; 87 88 if (unlikely(addr + size < addr)) 89 return !kasan_report(addr, size, write, ret_ip); 90 91 tag = get_tag((const void *)addr); 92 93 /* 94 * Ignore accesses for pointers tagged with 0xff (native kernel 95 * pointer tag) to suppress false positives caused by kmap. 96 * 97 * Some kernel code was written to account for archs that don't keep 98 * high memory mapped all the time, but rather map and unmap particular 99 * pages when needed. Instead of storing a pointer to the kernel memory, 100 * this code saves the address of the page structure and offset within 101 * that page for later use. Those pages are then mapped and unmapped 102 * with kmap/kunmap when necessary and virt_to_page is used to get the 103 * virtual address of the page. For arm64 (that keeps the high memory 104 * mapped all the time), kmap is turned into a page_address call. 105 106 * The issue is that with use of the page_address + virt_to_page 107 * sequence the top byte value of the original pointer gets lost (gets 108 * set to KASAN_TAG_KERNEL (0xFF)). 109 */ 110 if (tag == KASAN_TAG_KERNEL) 111 return true; 112 113 untagged_addr = reset_tag((const void *)addr); 114 if (unlikely(untagged_addr < 115 kasan_shadow_to_mem((void *)KASAN_SHADOW_START))) { 116 return !kasan_report(addr, size, write, ret_ip); 117 } 118 shadow_first = kasan_mem_to_shadow(untagged_addr); 119 shadow_last = kasan_mem_to_shadow(untagged_addr + size - 1); 120 for (shadow = shadow_first; shadow <= shadow_last; shadow++) { 121 if (*shadow != tag) { 122 return !kasan_report(addr, size, write, ret_ip); 123 } 124 } 125 126 return true; 127 } 128 129 #define DEFINE_HWASAN_LOAD_STORE(size) \ 130 void __hwasan_load##size##_noabort(unsigned long addr) \ 131 { \ 132 check_memory_region(addr, size, false, _RET_IP_); \ 133 } \ 134 EXPORT_SYMBOL(__hwasan_load##size##_noabort); \ 135 void __hwasan_store##size##_noabort(unsigned long addr) \ 136 { \ 137 check_memory_region(addr, size, true, _RET_IP_); \ 138 } \ 139 EXPORT_SYMBOL(__hwasan_store##size##_noabort) 140 141 DEFINE_HWASAN_LOAD_STORE(1); 142 DEFINE_HWASAN_LOAD_STORE(2); 143 DEFINE_HWASAN_LOAD_STORE(4); 144 DEFINE_HWASAN_LOAD_STORE(8); 145 DEFINE_HWASAN_LOAD_STORE(16); 146 147 void __hwasan_loadN_noabort(unsigned long addr, unsigned long size) 148 { 149 check_memory_region(addr, size, false, _RET_IP_); 150 } 151 EXPORT_SYMBOL(__hwasan_loadN_noabort); 152 153 void __hwasan_storeN_noabort(unsigned long addr, unsigned long size) 154 { 155 check_memory_region(addr, size, true, _RET_IP_); 156 } 157 EXPORT_SYMBOL(__hwasan_storeN_noabort); 158 159 void __hwasan_tag_memory(unsigned long addr, u8 tag, unsigned long size) 160 { 161 kasan_poison_shadow((void *)addr, size, tag); 162 } 163 EXPORT_SYMBOL(__hwasan_tag_memory); 164 165 void kasan_set_free_info(struct kmem_cache *cache, 166 void *object, u8 tag) 167 { 168 struct kasan_alloc_meta *alloc_meta; 169 u8 idx = 0; 170 171 alloc_meta = get_alloc_info(cache, object); 172 173 #ifdef CONFIG_KASAN_SW_TAGS_IDENTIFY 174 idx = alloc_meta->free_track_idx; 175 alloc_meta->free_pointer_tag[idx] = tag; 176 alloc_meta->free_track_idx = (idx + 1) % KASAN_NR_FREE_STACKS; 177 #endif 178 179 kasan_set_track(&alloc_meta->free_track[idx], GFP_NOWAIT); 180 } 181 182 struct kasan_track *kasan_get_free_track(struct kmem_cache *cache, 183 void *object, u8 tag) 184 { 185 struct kasan_alloc_meta *alloc_meta; 186 int i = 0; 187 188 alloc_meta = get_alloc_info(cache, object); 189 190 #ifdef CONFIG_KASAN_SW_TAGS_IDENTIFY 191 for (i = 0; i < KASAN_NR_FREE_STACKS; i++) { 192 if (alloc_meta->free_pointer_tag[i] == tag) 193 break; 194 } 195 if (i == KASAN_NR_FREE_STACKS) 196 i = alloc_meta->free_track_idx; 197 #endif 198 199 return &alloc_meta->free_track[i]; 200 } 201