1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * KMSAN shadow implementation. 4 * 5 * Copyright (C) 2017-2022 Google LLC 6 * Author: Alexander Potapenko <glider@google.com> 7 * 8 */ 9 10 #include <asm/kmsan.h> 11 #include <asm/tlbflush.h> 12 #include <linux/cacheflush.h> 13 #include <linux/memblock.h> 14 #include <linux/mm_types.h> 15 #include <linux/slab.h> 16 #include <linux/smp.h> 17 #include <linux/stddef.h> 18 19 #include "../internal.h" 20 #include "kmsan.h" 21 22 #define shadow_page_for(page) ((page)->kmsan_shadow) 23 24 #define origin_page_for(page) ((page)->kmsan_origin) 25 26 static void *shadow_ptr_for(struct page *page) 27 { 28 return page_address(shadow_page_for(page)); 29 } 30 31 static void *origin_ptr_for(struct page *page) 32 { 33 return page_address(origin_page_for(page)); 34 } 35 36 static bool page_has_metadata(struct page *page) 37 { 38 return shadow_page_for(page) && origin_page_for(page); 39 } 40 41 static void set_no_shadow_origin_page(struct page *page) 42 { 43 shadow_page_for(page) = NULL; 44 origin_page_for(page) = NULL; 45 } 46 47 /* 48 * Dummy load and store pages to be used when the real metadata is unavailable. 49 * There are separate pages for loads and stores, so that every load returns a 50 * zero, and every store doesn't affect other loads. 51 */ 52 static char dummy_load_page[PAGE_SIZE] __aligned(PAGE_SIZE); 53 static char dummy_store_page[PAGE_SIZE] __aligned(PAGE_SIZE); 54 55 static unsigned long vmalloc_meta(void *addr, bool is_origin) 56 { 57 unsigned long addr64 = (unsigned long)addr, off; 58 59 KMSAN_WARN_ON(is_origin && !IS_ALIGNED(addr64, KMSAN_ORIGIN_SIZE)); 60 if (kmsan_internal_is_vmalloc_addr(addr)) { 61 off = addr64 - VMALLOC_START; 62 return off + (is_origin ? KMSAN_VMALLOC_ORIGIN_START : 63 KMSAN_VMALLOC_SHADOW_START); 64 } 65 if (kmsan_internal_is_module_addr(addr)) { 66 off = addr64 - MODULES_VADDR; 67 return off + (is_origin ? KMSAN_MODULES_ORIGIN_START : 68 KMSAN_MODULES_SHADOW_START); 69 } 70 return 0; 71 } 72 73 static struct page *virt_to_page_or_null(void *vaddr) 74 { 75 if (kmsan_virt_addr_valid(vaddr)) 76 return virt_to_page(vaddr); 77 else 78 return NULL; 79 } 80 81 struct shadow_origin_ptr kmsan_get_shadow_origin_ptr(void *address, u64 size, 82 bool store) 83 { 84 struct shadow_origin_ptr ret; 85 void *shadow; 86 87 /* 88 * Even if we redirect this memory access to the dummy page, it will 89 * go out of bounds. 90 */ 91 KMSAN_WARN_ON(size > PAGE_SIZE); 92 93 if (!kmsan_enabled) 94 goto return_dummy; 95 96 KMSAN_WARN_ON(!kmsan_metadata_is_contiguous(address, size)); 97 shadow = kmsan_get_metadata(address, KMSAN_META_SHADOW); 98 if (!shadow) 99 goto return_dummy; 100 101 ret.shadow = shadow; 102 ret.origin = kmsan_get_metadata(address, KMSAN_META_ORIGIN); 103 return ret; 104 105 return_dummy: 106 if (store) { 107 /* Ignore this store. */ 108 ret.shadow = dummy_store_page; 109 ret.origin = dummy_store_page; 110 } else { 111 /* This load will return zero. */ 112 ret.shadow = dummy_load_page; 113 ret.origin = dummy_load_page; 114 } 115 return ret; 116 } 117 118 /* 119 * Obtain the shadow or origin pointer for the given address, or NULL if there's 120 * none. The caller must check the return value for being non-NULL if needed. 121 * The return value of this function should not depend on whether we're in the 122 * runtime or not. 123 */ 124 void *kmsan_get_metadata(void *address, bool is_origin) 125 { 126 u64 addr = (u64)address, pad, off; 127 struct page *page; 128 void *ret; 129 130 if (is_origin && !IS_ALIGNED(addr, KMSAN_ORIGIN_SIZE)) { 131 pad = addr % KMSAN_ORIGIN_SIZE; 132 addr -= pad; 133 } 134 address = (void *)addr; 135 if (kmsan_internal_is_vmalloc_addr(address) || 136 kmsan_internal_is_module_addr(address)) 137 return (void *)vmalloc_meta(address, is_origin); 138 139 ret = arch_kmsan_get_meta_or_null(address, is_origin); 140 if (ret) 141 return ret; 142 143 page = virt_to_page_or_null(address); 144 if (!page) 145 return NULL; 146 if (!page_has_metadata(page)) 147 return NULL; 148 off = addr % PAGE_SIZE; 149 150 return (is_origin ? origin_ptr_for(page) : shadow_ptr_for(page)) + off; 151 } 152 153 void kmsan_copy_page_meta(struct page *dst, struct page *src) 154 { 155 if (!kmsan_enabled || kmsan_in_runtime()) 156 return; 157 if (!dst || !page_has_metadata(dst)) 158 return; 159 if (!src || !page_has_metadata(src)) { 160 kmsan_internal_unpoison_memory(page_address(dst), PAGE_SIZE, 161 /*checked*/ false); 162 return; 163 } 164 165 kmsan_enter_runtime(); 166 __memcpy(shadow_ptr_for(dst), shadow_ptr_for(src), PAGE_SIZE); 167 __memcpy(origin_ptr_for(dst), origin_ptr_for(src), PAGE_SIZE); 168 kmsan_leave_runtime(); 169 } 170 171 void kmsan_alloc_page(struct page *page, unsigned int order, gfp_t flags) 172 { 173 bool initialized = (flags & __GFP_ZERO) || !kmsan_enabled; 174 struct page *shadow, *origin; 175 depot_stack_handle_t handle; 176 int pages = 1 << order; 177 178 if (!page) 179 return; 180 181 shadow = shadow_page_for(page); 182 origin = origin_page_for(page); 183 184 if (initialized) { 185 __memset(page_address(shadow), 0, PAGE_SIZE * pages); 186 __memset(page_address(origin), 0, PAGE_SIZE * pages); 187 return; 188 } 189 190 /* Zero pages allocated by the runtime should also be initialized. */ 191 if (kmsan_in_runtime()) 192 return; 193 194 __memset(page_address(shadow), -1, PAGE_SIZE * pages); 195 kmsan_enter_runtime(); 196 handle = kmsan_save_stack_with_flags(flags, /*extra_bits*/ 0); 197 kmsan_leave_runtime(); 198 /* 199 * Addresses are page-aligned, pages are contiguous, so it's ok 200 * to just fill the origin pages with @handle. 201 */ 202 for (int i = 0; i < PAGE_SIZE * pages / sizeof(handle); i++) 203 ((depot_stack_handle_t *)page_address(origin))[i] = handle; 204 } 205 206 void kmsan_free_page(struct page *page, unsigned int order) 207 { 208 if (!kmsan_enabled || kmsan_in_runtime()) 209 return; 210 kmsan_enter_runtime(); 211 kmsan_internal_poison_memory(page_address(page), 212 PAGE_SIZE << compound_order(page), 213 GFP_KERNEL, 214 KMSAN_POISON_CHECK | KMSAN_POISON_FREE); 215 kmsan_leave_runtime(); 216 } 217 218 void kmsan_vmap_pages_range_noflush(unsigned long start, unsigned long end, 219 pgprot_t prot, struct page **pages, 220 unsigned int page_shift) 221 { 222 unsigned long shadow_start, origin_start, shadow_end, origin_end; 223 struct page **s_pages, **o_pages; 224 int nr, mapped; 225 226 if (!kmsan_enabled) 227 return; 228 229 shadow_start = vmalloc_meta((void *)start, KMSAN_META_SHADOW); 230 shadow_end = vmalloc_meta((void *)end, KMSAN_META_SHADOW); 231 if (!shadow_start) 232 return; 233 234 nr = (end - start) / PAGE_SIZE; 235 s_pages = kcalloc(nr, sizeof(*s_pages), GFP_KERNEL); 236 o_pages = kcalloc(nr, sizeof(*o_pages), GFP_KERNEL); 237 if (!s_pages || !o_pages) 238 goto ret; 239 for (int i = 0; i < nr; i++) { 240 s_pages[i] = shadow_page_for(pages[i]); 241 o_pages[i] = origin_page_for(pages[i]); 242 } 243 prot = __pgprot(pgprot_val(prot) | _PAGE_NX); 244 prot = PAGE_KERNEL; 245 246 origin_start = vmalloc_meta((void *)start, KMSAN_META_ORIGIN); 247 origin_end = vmalloc_meta((void *)end, KMSAN_META_ORIGIN); 248 kmsan_enter_runtime(); 249 mapped = __vmap_pages_range_noflush(shadow_start, shadow_end, prot, 250 s_pages, page_shift); 251 KMSAN_WARN_ON(mapped); 252 mapped = __vmap_pages_range_noflush(origin_start, origin_end, prot, 253 o_pages, page_shift); 254 KMSAN_WARN_ON(mapped); 255 kmsan_leave_runtime(); 256 flush_tlb_kernel_range(shadow_start, shadow_end); 257 flush_tlb_kernel_range(origin_start, origin_end); 258 flush_cache_vmap(shadow_start, shadow_end); 259 flush_cache_vmap(origin_start, origin_end); 260 261 ret: 262 kfree(s_pages); 263 kfree(o_pages); 264 } 265 266 /* Allocate metadata for pages allocated at boot time. */ 267 void __init kmsan_init_alloc_meta_for_range(void *start, void *end) 268 { 269 struct page *shadow_p, *origin_p; 270 void *shadow, *origin; 271 struct page *page; 272 u64 size; 273 274 start = (void *)ALIGN_DOWN((u64)start, PAGE_SIZE); 275 size = ALIGN((u64)end - (u64)start, PAGE_SIZE); 276 shadow = memblock_alloc(size, PAGE_SIZE); 277 origin = memblock_alloc(size, PAGE_SIZE); 278 for (u64 addr = 0; addr < size; addr += PAGE_SIZE) { 279 page = virt_to_page_or_null((char *)start + addr); 280 shadow_p = virt_to_page_or_null((char *)shadow + addr); 281 set_no_shadow_origin_page(shadow_p); 282 shadow_page_for(page) = shadow_p; 283 origin_p = virt_to_page_or_null((char *)origin + addr); 284 set_no_shadow_origin_page(origin_p); 285 origin_page_for(page) = origin_p; 286 } 287 } 288 289 void kmsan_setup_meta(struct page *page, struct page *shadow, 290 struct page *origin, int order) 291 { 292 for (int i = 0; i < (1 << order); i++) { 293 set_no_shadow_origin_page(&shadow[i]); 294 set_no_shadow_origin_page(&origin[i]); 295 shadow_page_for(&page[i]) = &shadow[i]; 296 origin_page_for(&page[i]) = &origin[i]; 297 } 298 } 299