1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * This file contains common KASAN code. 4 * 5 * Copyright (c) 2014 Samsung Electronics Co., Ltd. 6 * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com> 7 * 8 * Some code borrowed from https://github.com/xairy/kasan-prototype by 9 * Andrey Konovalov <andreyknvl@gmail.com> 10 */ 11 12 #include <linux/export.h> 13 #include <linux/init.h> 14 #include <linux/kasan.h> 15 #include <linux/kernel.h> 16 #include <linux/linkage.h> 17 #include <linux/memblock.h> 18 #include <linux/memory.h> 19 #include <linux/mm.h> 20 #include <linux/module.h> 21 #include <linux/printk.h> 22 #include <linux/sched.h> 23 #include <linux/sched/clock.h> 24 #include <linux/sched/task_stack.h> 25 #include <linux/slab.h> 26 #include <linux/stackdepot.h> 27 #include <linux/stacktrace.h> 28 #include <linux/string.h> 29 #include <linux/types.h> 30 #include <linux/bug.h> 31 #include <linux/vmalloc.h> 32 33 #include "kasan.h" 34 #include "../slab.h" 35 36 #if defined(CONFIG_ARCH_DEFER_KASAN) || defined(CONFIG_KASAN_HW_TAGS) 37 /* 38 * Definition of the unified static key declared in kasan-enabled.h. 39 * This provides consistent runtime enable/disable across KASAN modes. 40 */ 41 DEFINE_STATIC_KEY_FALSE(kasan_flag_enabled); 42 EXPORT_SYMBOL_GPL(kasan_flag_enabled); 43 #endif 44 45 struct slab *kasan_addr_to_slab(const void *addr) 46 { 47 if (virt_addr_valid(addr)) 48 return virt_to_slab(addr); 49 return NULL; 50 } 51 52 depot_stack_handle_t kasan_save_stack(gfp_t flags, depot_flags_t depot_flags) 53 { 54 unsigned long entries[KASAN_STACK_DEPTH]; 55 unsigned int nr_entries; 56 57 nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 0); 58 return stack_depot_save_flags(entries, nr_entries, flags, depot_flags); 59 } 60 61 void kasan_set_track(struct kasan_track *track, depot_stack_handle_t stack) 62 { 63 #ifdef CONFIG_KASAN_EXTRA_INFO 64 u32 cpu = raw_smp_processor_id(); 65 u64 ts_nsec = local_clock(); 66 67 track->cpu = cpu; 68 track->timestamp = ts_nsec >> 9; 69 #endif /* CONFIG_KASAN_EXTRA_INFO */ 70 track->pid = current->pid; 71 track->stack = stack; 72 } 73 74 void kasan_save_track(struct kasan_track *track, gfp_t flags) 75 { 76 depot_stack_handle_t stack; 77 78 stack = kasan_save_stack(flags, STACK_DEPOT_FLAG_CAN_ALLOC); 79 kasan_set_track(track, stack); 80 } 81 82 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS) 83 void kasan_enable_current(void) 84 { 85 current->kasan_depth++; 86 } 87 EXPORT_SYMBOL(kasan_enable_current); 88 89 void kasan_disable_current(void) 90 { 91 current->kasan_depth--; 92 } 93 EXPORT_SYMBOL(kasan_disable_current); 94 95 #endif /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */ 96 97 void __kasan_unpoison_range(const void *address, size_t size) 98 { 99 if (is_kfence_address(address)) 100 return; 101 102 kasan_unpoison(address, size, false); 103 } 104 105 #ifdef CONFIG_KASAN_STACK 106 /* Unpoison the entire stack for a task. */ 107 void kasan_unpoison_task_stack(struct task_struct *task) 108 { 109 void *base = task_stack_page(task); 110 111 kasan_unpoison(base, THREAD_SIZE, false); 112 } 113 114 /* Unpoison the stack for the current task beyond a watermark sp value. */ 115 asmlinkage void kasan_unpoison_task_stack_below(const void *watermark) 116 { 117 void *base; 118 119 /* 120 * Only the generic mode needs this. In the tag-based mode the 121 * compiler fully initializes the shadow of stack variables on 122 * function entry, so stale tags left below the watermark are 123 * harmless. 124 */ 125 if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) 126 return; 127 128 /* 129 * Calculate the task stack base address. Avoid using 'current' 130 * because this function is called by early resume code which hasn't 131 * yet set up the percpu register (%gs). 132 */ 133 base = (void *)((unsigned long)watermark & ~(THREAD_SIZE - 1)); 134 135 kasan_unpoison(base, watermark - base, false); 136 } 137 #endif /* CONFIG_KASAN_STACK */ 138 139 bool __kasan_unpoison_pages(struct page *page, unsigned int order, bool init) 140 { 141 u8 tag; 142 unsigned long i; 143 144 if (unlikely(PageHighMem(page))) 145 return false; 146 147 if (!kasan_sample_page_alloc(order)) 148 return false; 149 150 tag = kasan_random_tag(); 151 kasan_unpoison(set_tag(page_address(page), tag), 152 PAGE_SIZE << order, init); 153 for (i = 0; i < (1 << order); i++) 154 page_kasan_tag_set(page + i, tag); 155 156 return true; 157 } 158 159 void __kasan_poison_pages(struct page *page, unsigned int order, bool init) 160 { 161 if (likely(!PageHighMem(page))) 162 kasan_poison(page_address(page), PAGE_SIZE << order, 163 KASAN_PAGE_FREE, init); 164 } 165 166 void __kasan_poison_slab(struct slab *slab) 167 { 168 struct page *page = slab_page(slab); 169 unsigned long i; 170 171 for (i = 0; i < compound_nr(page); i++) 172 page_kasan_tag_reset(page + i); 173 kasan_poison(page_address(page), page_size(page), 174 KASAN_SLAB_REDZONE, false); 175 } 176 177 void __kasan_unpoison_new_object(struct kmem_cache *cache, void *object) 178 { 179 kasan_unpoison(object, cache->object_size, false); 180 } 181 182 void __kasan_poison_new_object(struct kmem_cache *cache, void *object) 183 { 184 kasan_poison(object, round_up(cache->object_size, KASAN_GRANULE_SIZE), 185 KASAN_SLAB_REDZONE, false); 186 } 187 188 /* 189 * This function assigns a tag to an object considering the following: 190 * 1. A cache might have a constructor, which might save a pointer to a slab 191 * object somewhere (e.g. in the object itself). We preassign a tag for 192 * each object in caches with constructors during slab creation and reuse 193 * the same tag each time a particular object is allocated. 194 * 2. A cache might be SLAB_TYPESAFE_BY_RCU, which means objects can be 195 * accessed after being freed. We preassign tags for objects in these 196 * caches as well. 197 */ 198 static inline u8 assign_tag(struct kmem_cache *cache, 199 const void *object, bool init) 200 { 201 if (IS_ENABLED(CONFIG_KASAN_GENERIC)) 202 return 0xff; 203 204 /* 205 * If the cache neither has a constructor nor has SLAB_TYPESAFE_BY_RCU 206 * set, assign a tag when the object is being allocated (init == false). 207 */ 208 if (!cache->ctor && !(cache->flags & SLAB_TYPESAFE_BY_RCU)) 209 return init ? KASAN_TAG_KERNEL : kasan_random_tag(); 210 211 /* 212 * For caches that either have a constructor or SLAB_TYPESAFE_BY_RCU, 213 * assign a random tag during slab creation, otherwise reuse 214 * the already assigned tag. 215 */ 216 return init ? kasan_random_tag() : get_tag(object); 217 } 218 219 void * __must_check __kasan_init_slab_obj(struct kmem_cache *cache, 220 const void *object) 221 { 222 /* Initialize per-object metadata if it is present. */ 223 if (kasan_requires_meta()) 224 kasan_init_object_meta(cache, object); 225 226 /* Tag is ignored in set_tag() without CONFIG_KASAN_SW/HW_TAGS */ 227 object = set_tag(object, assign_tag(cache, object, true)); 228 229 return (void *)object; 230 } 231 232 /* Returns true when freeing the object is not safe. */ 233 static bool check_slab_allocation(struct kmem_cache *cache, void *object, 234 unsigned long ip) 235 { 236 void *tagged_object = object; 237 238 object = kasan_reset_tag(object); 239 240 if (unlikely(nearest_obj(cache, virt_to_slab(object), object) != object)) { 241 kasan_report_invalid_free(tagged_object, ip, KASAN_REPORT_INVALID_FREE); 242 return true; 243 } 244 245 if (!kasan_byte_accessible(tagged_object)) { 246 kasan_report_invalid_free(tagged_object, ip, KASAN_REPORT_DOUBLE_FREE); 247 return true; 248 } 249 250 return false; 251 } 252 253 static inline void poison_slab_object(struct kmem_cache *cache, void *object, 254 bool init) 255 { 256 void *tagged_object = object; 257 258 object = kasan_reset_tag(object); 259 260 kasan_poison(object, round_up(cache->object_size, KASAN_GRANULE_SIZE), 261 KASAN_SLAB_FREE, init); 262 263 if (kasan_stack_collection_enabled()) 264 kasan_save_free_info(cache, tagged_object); 265 } 266 267 bool __kasan_slab_pre_free(struct kmem_cache *cache, void *object, 268 unsigned long ip) 269 { 270 if (is_kfence_address(object)) 271 return false; 272 return check_slab_allocation(cache, object, ip); 273 } 274 275 bool __kasan_slab_free(struct kmem_cache *cache, void *object, bool init, 276 bool still_accessible, bool no_quarantine) 277 { 278 if (is_kfence_address(object)) 279 return false; 280 281 /* 282 * If this point is reached with an object that must still be 283 * accessible under RCU, we can't poison it; in that case, also skip the 284 * quarantine. This should mostly only happen when CONFIG_SLUB_RCU_DEBUG 285 * has been disabled manually. 286 * 287 * Putting the object on the quarantine wouldn't help catch UAFs (since 288 * we can't poison it here), and it would mask bugs caused by 289 * SLAB_TYPESAFE_BY_RCU users not being careful enough about object 290 * reuse; so overall, putting the object into the quarantine here would 291 * be counterproductive. 292 */ 293 if (still_accessible) 294 return false; 295 296 poison_slab_object(cache, object, init); 297 298 if (no_quarantine) 299 return false; 300 301 /* 302 * If the object is put into quarantine, do not let slab put the object 303 * onto the freelist for now. The object's metadata is kept until the 304 * object gets evicted from quarantine. 305 */ 306 if (kasan_quarantine_put(cache, object)) 307 return true; 308 309 /* 310 * Note: Keep per-object metadata to allow KASAN print stack traces for 311 * use-after-free-before-realloc bugs. 312 */ 313 314 /* Let slab put the object onto the freelist. */ 315 return false; 316 } 317 318 static inline bool check_page_allocation(void *ptr, unsigned long ip) 319 { 320 if (ptr != page_address(virt_to_head_page(ptr))) { 321 kasan_report_invalid_free(ptr, ip, KASAN_REPORT_INVALID_FREE); 322 return true; 323 } 324 325 if (!kasan_byte_accessible(ptr)) { 326 kasan_report_invalid_free(ptr, ip, KASAN_REPORT_DOUBLE_FREE); 327 return true; 328 } 329 330 return false; 331 } 332 333 void __kasan_kfree_large(void *ptr, unsigned long ip) 334 { 335 check_page_allocation(ptr, ip); 336 337 /* The object will be poisoned by kasan_poison_pages(). */ 338 } 339 340 static inline void unpoison_slab_object(struct kmem_cache *cache, void *object, 341 gfp_t flags, bool init) 342 { 343 /* 344 * Unpoison the whole object. For kmalloc() allocations, 345 * poison_kmalloc_redzone() will do precise poisoning. 346 */ 347 kasan_unpoison(object, cache->object_size, init); 348 349 /* Save alloc info (if possible) for non-kmalloc() allocations. */ 350 if (kasan_stack_collection_enabled() && !is_kmalloc_cache(cache)) 351 kasan_save_alloc_info(cache, object, flags); 352 } 353 354 void * __must_check __kasan_slab_alloc(struct kmem_cache *cache, 355 void *object, gfp_t flags, bool init) 356 { 357 u8 tag; 358 void *tagged_object; 359 360 if (gfpflags_allow_blocking(flags)) 361 kasan_quarantine_reduce(); 362 363 if (unlikely(object == NULL)) 364 return NULL; 365 366 if (is_kfence_address(object)) 367 return (void *)object; 368 369 /* 370 * Generate and assign random tag for tag-based modes. 371 * Tag is ignored in set_tag() for the generic mode. 372 */ 373 tag = assign_tag(cache, object, false); 374 tagged_object = set_tag(object, tag); 375 376 /* Unpoison the object and save alloc info for non-kmalloc() allocations. */ 377 unpoison_slab_object(cache, tagged_object, flags, init); 378 379 return tagged_object; 380 } 381 382 static inline void poison_kmalloc_redzone(struct kmem_cache *cache, 383 const void *object, size_t size, gfp_t flags) 384 { 385 unsigned long redzone_start; 386 unsigned long redzone_end; 387 388 /* 389 * The redzone has byte-level precision for the generic mode. 390 * Partially poison the last object granule to cover the unaligned 391 * part of the redzone. 392 */ 393 if (IS_ENABLED(CONFIG_KASAN_GENERIC)) 394 kasan_poison_last_granule((void *)object, size); 395 396 /* Poison the aligned part of the redzone. */ 397 redzone_start = round_up((unsigned long)(object + size), 398 KASAN_GRANULE_SIZE); 399 redzone_end = round_up((unsigned long)(object + cache->object_size), 400 KASAN_GRANULE_SIZE); 401 kasan_poison((void *)redzone_start, redzone_end - redzone_start, 402 KASAN_SLAB_REDZONE, false); 403 404 /* 405 * Save alloc info (if possible) for kmalloc() allocations. 406 * This also rewrites the alloc info when called from kasan_krealloc(). 407 */ 408 if (kasan_stack_collection_enabled() && is_kmalloc_cache(cache)) 409 kasan_save_alloc_info(cache, (void *)object, flags); 410 411 } 412 413 void * __must_check __kasan_kmalloc(struct kmem_cache *cache, const void *object, 414 size_t size, gfp_t flags) 415 { 416 if (gfpflags_allow_blocking(flags)) 417 kasan_quarantine_reduce(); 418 419 if (unlikely(object == NULL)) 420 return NULL; 421 422 if (is_kfence_address(object)) 423 return (void *)object; 424 425 /* The object has already been unpoisoned by kasan_slab_alloc(). */ 426 poison_kmalloc_redzone(cache, object, size, flags); 427 428 /* Keep the tag that was set by kasan_slab_alloc(). */ 429 return (void *)object; 430 } 431 EXPORT_SYMBOL(__kasan_kmalloc); 432 433 static inline void poison_kmalloc_large_redzone(const void *ptr, size_t size, 434 gfp_t flags) 435 { 436 unsigned long redzone_start; 437 unsigned long redzone_end; 438 439 /* 440 * The redzone has byte-level precision for the generic mode. 441 * Partially poison the last object granule to cover the unaligned 442 * part of the redzone. 443 */ 444 if (IS_ENABLED(CONFIG_KASAN_GENERIC)) 445 kasan_poison_last_granule(ptr, size); 446 447 /* Poison the aligned part of the redzone. */ 448 redzone_start = round_up((unsigned long)(ptr + size), KASAN_GRANULE_SIZE); 449 redzone_end = (unsigned long)ptr + page_size(virt_to_page(ptr)); 450 kasan_poison((void *)redzone_start, redzone_end - redzone_start, 451 KASAN_PAGE_REDZONE, false); 452 } 453 454 void * __must_check __kasan_kmalloc_large(const void *ptr, size_t size, 455 gfp_t flags) 456 { 457 if (gfpflags_allow_blocking(flags)) 458 kasan_quarantine_reduce(); 459 460 if (unlikely(ptr == NULL)) 461 return NULL; 462 463 /* The object has already been unpoisoned by kasan_unpoison_pages(). */ 464 poison_kmalloc_large_redzone(ptr, size, flags); 465 466 /* Keep the tag that was set by alloc_pages(). */ 467 return (void *)ptr; 468 } 469 470 void * __must_check __kasan_krealloc(const void *object, size_t size, gfp_t flags) 471 { 472 struct slab *slab; 473 474 if (gfpflags_allow_blocking(flags)) 475 kasan_quarantine_reduce(); 476 477 if (unlikely(object == ZERO_SIZE_PTR)) 478 return (void *)object; 479 480 if (is_kfence_address(object)) 481 return (void *)object; 482 483 /* 484 * Unpoison the object's data. 485 * Part of it might already have been unpoisoned, but it's unknown 486 * how big that part is. 487 */ 488 kasan_unpoison(object, size, false); 489 490 slab = virt_to_slab(object); 491 492 /* Piggy-back on kmalloc() instrumentation to poison the redzone. */ 493 if (unlikely(!slab)) 494 poison_kmalloc_large_redzone(object, size, flags); 495 else 496 poison_kmalloc_redzone(slab->slab_cache, object, size, flags); 497 498 return (void *)object; 499 } 500 501 bool __kasan_mempool_poison_pages(struct page *page, unsigned int order, 502 unsigned long ip) 503 { 504 unsigned long *ptr; 505 506 if (unlikely(PageHighMem(page))) 507 return true; 508 509 /* Bail out if allocation was excluded due to sampling. */ 510 if (!IS_ENABLED(CONFIG_KASAN_GENERIC) && 511 page_kasan_tag(page) == KASAN_TAG_KERNEL) 512 return true; 513 514 ptr = page_address(page); 515 516 if (check_page_allocation(ptr, ip)) 517 return false; 518 519 kasan_poison(ptr, PAGE_SIZE << order, KASAN_PAGE_FREE, false); 520 521 return true; 522 } 523 524 void __kasan_mempool_unpoison_pages(struct page *page, unsigned int order, 525 unsigned long ip) 526 { 527 __kasan_unpoison_pages(page, order, false); 528 } 529 530 bool __kasan_mempool_poison_object(void *ptr, unsigned long ip) 531 { 532 struct page *page = virt_to_page(ptr); 533 struct slab *slab; 534 535 if (unlikely(PageLargeKmalloc(page))) { 536 if (check_page_allocation(ptr, ip)) 537 return false; 538 kasan_poison(ptr, page_size(page), KASAN_PAGE_FREE, false); 539 return true; 540 } 541 542 if (is_kfence_address(ptr)) 543 return true; 544 545 slab = page_slab(page); 546 547 if (check_slab_allocation(slab->slab_cache, ptr, ip)) 548 return false; 549 550 poison_slab_object(slab->slab_cache, ptr, false); 551 return true; 552 } 553 554 void __kasan_mempool_unpoison_object(void *ptr, size_t size, unsigned long ip) 555 { 556 struct slab *slab; 557 gfp_t flags = 0; /* Might be executing under a lock. */ 558 559 slab = virt_to_slab(ptr); 560 561 /* 562 * This function can be called for large kmalloc allocation that get 563 * their memory from page_alloc. 564 */ 565 if (unlikely(!slab)) { 566 kasan_unpoison(ptr, size, false); 567 poison_kmalloc_large_redzone(ptr, size, flags); 568 return; 569 } 570 571 if (is_kfence_address(ptr)) 572 return; 573 574 /* Unpoison the object and save alloc info for non-kmalloc() allocations. */ 575 unpoison_slab_object(slab->slab_cache, ptr, flags, false); 576 577 /* Poison the redzone and save alloc info for kmalloc() allocations. */ 578 if (is_kmalloc_cache(slab->slab_cache)) 579 poison_kmalloc_redzone(slab->slab_cache, ptr, size, flags); 580 } 581 582 bool __kasan_check_byte(const void *address, unsigned long ip) 583 { 584 if (!kasan_byte_accessible(address)) { 585 kasan_report(address, 1, false, ip); 586 return false; 587 } 588 return true; 589 } 590 591 #ifdef CONFIG_KASAN_VMALLOC 592 void __kasan_unpoison_vmap_areas(struct vm_struct **vms, int nr_vms, 593 kasan_vmalloc_flags_t flags) 594 { 595 unsigned long size; 596 void *addr; 597 int area; 598 u8 tag; 599 600 /* 601 * If KASAN_VMALLOC_KEEP_TAG was set at this point, all vms[] pointers 602 * would be unpoisoned with the KASAN_TAG_KERNEL which would disable 603 * KASAN checks down the line. 604 */ 605 if (WARN_ON_ONCE(flags & KASAN_VMALLOC_KEEP_TAG)) 606 return; 607 608 size = vms[0]->size; 609 addr = vms[0]->addr; 610 vms[0]->addr = __kasan_unpoison_vmalloc(addr, size, flags); 611 tag = get_tag(vms[0]->addr); 612 613 for (area = 1 ; area < nr_vms ; area++) { 614 size = vms[area]->size; 615 addr = set_tag(vms[area]->addr, tag); 616 vms[area]->addr = 617 __kasan_unpoison_vmalloc(addr, size, flags | KASAN_VMALLOC_KEEP_TAG); 618 } 619 } 620 621 void __kasan_vrealloc(const void *addr, unsigned long old_size, 622 unsigned long new_size) 623 { 624 if (new_size < old_size) { 625 kasan_poison_last_granule(addr, new_size); 626 627 new_size = round_up(new_size, KASAN_GRANULE_SIZE); 628 old_size = round_up(old_size, KASAN_GRANULE_SIZE); 629 if (new_size < old_size) 630 __kasan_poison_vmalloc(addr + new_size, 631 old_size - new_size); 632 } else if (new_size > old_size) { 633 old_size = round_down(old_size, KASAN_GRANULE_SIZE); 634 __kasan_unpoison_vmalloc(addr + old_size, 635 new_size - old_size, 636 KASAN_VMALLOC_PROT_NORMAL | 637 KASAN_VMALLOC_VM_ALLOC | 638 KASAN_VMALLOC_KEEP_TAG); 639 } 640 } 641 #endif 642