1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * This file contains common KASAN error reporting 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/bitops.h> 13 #include <linux/ftrace.h> 14 #include <linux/init.h> 15 #include <linux/kernel.h> 16 #include <linux/lockdep.h> 17 #include <linux/mm.h> 18 #include <linux/printk.h> 19 #include <linux/sched.h> 20 #include <linux/slab.h> 21 #include <linux/stackdepot.h> 22 #include <linux/stacktrace.h> 23 #include <linux/string.h> 24 #include <linux/types.h> 25 #include <linux/kasan.h> 26 #include <linux/module.h> 27 #include <linux/sched/task_stack.h> 28 #include <linux/uaccess.h> 29 #include <trace/events/error_report.h> 30 31 #include <asm/sections.h> 32 33 #include <kunit/test.h> 34 35 #include "kasan.h" 36 #include "../slab.h" 37 38 static unsigned long kasan_flags; 39 40 #define KASAN_BIT_REPORTED 0 41 #define KASAN_BIT_MULTI_SHOT 1 42 43 enum kasan_arg_fault { 44 KASAN_ARG_FAULT_DEFAULT, 45 KASAN_ARG_FAULT_REPORT, 46 KASAN_ARG_FAULT_PANIC, 47 }; 48 49 static enum kasan_arg_fault kasan_arg_fault __ro_after_init = KASAN_ARG_FAULT_DEFAULT; 50 51 /* kasan.fault=report/panic */ 52 static int __init early_kasan_fault(char *arg) 53 { 54 if (!arg) 55 return -EINVAL; 56 57 if (!strcmp(arg, "report")) 58 kasan_arg_fault = KASAN_ARG_FAULT_REPORT; 59 else if (!strcmp(arg, "panic")) 60 kasan_arg_fault = KASAN_ARG_FAULT_PANIC; 61 else 62 return -EINVAL; 63 64 return 0; 65 } 66 early_param("kasan.fault", early_kasan_fault); 67 68 static int __init kasan_set_multi_shot(char *str) 69 { 70 set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags); 71 return 1; 72 } 73 __setup("kasan_multi_shot", kasan_set_multi_shot); 74 75 /* 76 * Used to suppress reports within kasan_disable/enable_current() critical 77 * sections, which are used for marking accesses to slab metadata. 78 */ 79 static bool report_suppressed(void) 80 { 81 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS) 82 if (current->kasan_depth) 83 return true; 84 #endif 85 return false; 86 } 87 88 /* 89 * Used to avoid reporting more than one KASAN bug unless kasan_multi_shot 90 * is enabled. Note that KASAN tests effectively enable kasan_multi_shot 91 * for their duration. 92 */ 93 static bool report_enabled(void) 94 { 95 if (test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags)) 96 return true; 97 return !test_and_set_bit(KASAN_BIT_REPORTED, &kasan_flags); 98 } 99 100 #if IS_ENABLED(CONFIG_KASAN_KUNIT_TEST) || IS_ENABLED(CONFIG_KASAN_MODULE_TEST) 101 102 bool kasan_save_enable_multi_shot(void) 103 { 104 return test_and_set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags); 105 } 106 EXPORT_SYMBOL_GPL(kasan_save_enable_multi_shot); 107 108 void kasan_restore_multi_shot(bool enabled) 109 { 110 if (!enabled) 111 clear_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags); 112 } 113 EXPORT_SYMBOL_GPL(kasan_restore_multi_shot); 114 115 #endif 116 117 #if IS_ENABLED(CONFIG_KASAN_KUNIT_TEST) 118 static void update_kunit_status(bool sync) 119 { 120 struct kunit *test; 121 struct kunit_resource *resource; 122 struct kunit_kasan_status *status; 123 124 test = current->kunit_test; 125 if (!test) 126 return; 127 128 resource = kunit_find_named_resource(test, "kasan_status"); 129 if (!resource) { 130 kunit_set_failure(test); 131 return; 132 } 133 134 status = (struct kunit_kasan_status *)resource->data; 135 WRITE_ONCE(status->report_found, true); 136 WRITE_ONCE(status->sync_fault, sync); 137 138 kunit_put_resource(resource); 139 } 140 #else 141 static void update_kunit_status(bool sync) { } 142 #endif 143 144 static DEFINE_SPINLOCK(report_lock); 145 146 static void start_report(unsigned long *flags, bool sync) 147 { 148 /* Respect the /proc/sys/kernel/traceoff_on_warning interface. */ 149 disable_trace_on_warning(); 150 /* Update status of the currently running KASAN test. */ 151 update_kunit_status(sync); 152 /* Do not allow LOCKDEP mangling KASAN reports. */ 153 lockdep_off(); 154 /* Make sure we don't end up in loop. */ 155 kasan_disable_current(); 156 spin_lock_irqsave(&report_lock, *flags); 157 pr_err("==================================================================\n"); 158 } 159 160 static void end_report(unsigned long *flags, void *addr) 161 { 162 if (addr) 163 trace_error_report_end(ERROR_DETECTOR_KASAN, 164 (unsigned long)addr); 165 pr_err("==================================================================\n"); 166 spin_unlock_irqrestore(&report_lock, *flags); 167 if (panic_on_warn && !test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags)) 168 panic("panic_on_warn set ...\n"); 169 if (kasan_arg_fault == KASAN_ARG_FAULT_PANIC) 170 panic("kasan.fault=panic set ...\n"); 171 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); 172 lockdep_on(); 173 kasan_enable_current(); 174 } 175 176 static void print_error_description(struct kasan_report_info *info) 177 { 178 if (info->type == KASAN_REPORT_INVALID_FREE) { 179 pr_err("BUG: KASAN: invalid-free in %pS\n", (void *)info->ip); 180 return; 181 } 182 183 if (info->type == KASAN_REPORT_DOUBLE_FREE) { 184 pr_err("BUG: KASAN: double-free in %pS\n", (void *)info->ip); 185 return; 186 } 187 188 pr_err("BUG: KASAN: %s in %pS\n", 189 kasan_get_bug_type(info), (void *)info->ip); 190 if (info->access_size) 191 pr_err("%s of size %zu at addr %px by task %s/%d\n", 192 info->is_write ? "Write" : "Read", info->access_size, 193 info->access_addr, current->comm, task_pid_nr(current)); 194 else 195 pr_err("%s at addr %px by task %s/%d\n", 196 info->is_write ? "Write" : "Read", 197 info->access_addr, current->comm, task_pid_nr(current)); 198 } 199 200 static void print_track(struct kasan_track *track, const char *prefix) 201 { 202 pr_err("%s by task %u:\n", prefix, track->pid); 203 if (track->stack) 204 stack_depot_print(track->stack); 205 else 206 pr_err("(stack is not available)\n"); 207 } 208 209 static inline struct page *addr_to_page(const void *addr) 210 { 211 if (virt_addr_valid(addr)) 212 return virt_to_head_page(addr); 213 return NULL; 214 } 215 216 static void describe_object_addr(struct kmem_cache *cache, void *object, 217 const void *addr) 218 { 219 unsigned long access_addr = (unsigned long)addr; 220 unsigned long object_addr = (unsigned long)object; 221 const char *rel_type; 222 int rel_bytes; 223 224 pr_err("The buggy address belongs to the object at %px\n" 225 " which belongs to the cache %s of size %d\n", 226 object, cache->name, cache->object_size); 227 228 if (access_addr < object_addr) { 229 rel_type = "to the left"; 230 rel_bytes = object_addr - access_addr; 231 } else if (access_addr >= object_addr + cache->object_size) { 232 rel_type = "to the right"; 233 rel_bytes = access_addr - (object_addr + cache->object_size); 234 } else { 235 rel_type = "inside"; 236 rel_bytes = access_addr - object_addr; 237 } 238 239 pr_err("The buggy address is located %d bytes %s of\n" 240 " %d-byte region [%px, %px)\n", 241 rel_bytes, rel_type, cache->object_size, (void *)object_addr, 242 (void *)(object_addr + cache->object_size)); 243 } 244 245 static void describe_object_stacks(struct kmem_cache *cache, void *object, 246 const void *addr, u8 tag) 247 { 248 struct kasan_track *alloc_track; 249 struct kasan_track *free_track; 250 251 alloc_track = kasan_get_alloc_track(cache, object); 252 if (alloc_track) { 253 print_track(alloc_track, "Allocated"); 254 pr_err("\n"); 255 } 256 257 free_track = kasan_get_free_track(cache, object, tag); 258 if (free_track) { 259 print_track(free_track, "Freed"); 260 pr_err("\n"); 261 } 262 263 kasan_print_aux_stacks(cache, object); 264 } 265 266 static void describe_object(struct kmem_cache *cache, void *object, 267 const void *addr, u8 tag) 268 { 269 if (kasan_stack_collection_enabled()) 270 describe_object_stacks(cache, object, addr, tag); 271 describe_object_addr(cache, object, addr); 272 } 273 274 static inline bool kernel_or_module_addr(const void *addr) 275 { 276 if (is_kernel((unsigned long)addr)) 277 return true; 278 if (is_module_address((unsigned long)addr)) 279 return true; 280 return false; 281 } 282 283 static inline bool init_task_stack_addr(const void *addr) 284 { 285 return addr >= (void *)&init_thread_union.stack && 286 (addr <= (void *)&init_thread_union.stack + 287 sizeof(init_thread_union.stack)); 288 } 289 290 static void print_address_description(void *addr, u8 tag, 291 struct kasan_report_info *info) 292 { 293 struct page *page = addr_to_page(addr); 294 295 dump_stack_lvl(KERN_ERR); 296 pr_err("\n"); 297 298 if (info->cache && info->object) { 299 describe_object(info->cache, info->object, addr, tag); 300 pr_err("\n"); 301 } 302 303 if (kernel_or_module_addr(addr) && !init_task_stack_addr(addr)) { 304 pr_err("The buggy address belongs to the variable:\n"); 305 pr_err(" %pS\n", addr); 306 pr_err("\n"); 307 } 308 309 if (object_is_on_stack(addr)) { 310 /* 311 * Currently, KASAN supports printing frame information only 312 * for accesses to the task's own stack. 313 */ 314 kasan_print_address_stack_frame(addr); 315 pr_err("\n"); 316 } 317 318 if (is_vmalloc_addr(addr)) { 319 struct vm_struct *va = find_vm_area(addr); 320 321 if (va) { 322 pr_err("The buggy address belongs to the virtual mapping at\n" 323 " [%px, %px) created by:\n" 324 " %pS\n", 325 va->addr, va->addr + va->size, va->caller); 326 pr_err("\n"); 327 328 page = vmalloc_to_page(addr); 329 } 330 } 331 332 if (page) { 333 pr_err("The buggy address belongs to the physical page:\n"); 334 dump_page(page, "kasan: bad access detected"); 335 pr_err("\n"); 336 } 337 } 338 339 static bool meta_row_is_guilty(const void *row, const void *addr) 340 { 341 return (row <= addr) && (addr < row + META_MEM_BYTES_PER_ROW); 342 } 343 344 static int meta_pointer_offset(const void *row, const void *addr) 345 { 346 /* 347 * Memory state around the buggy address: 348 * ff00ff00ff00ff00: 00 00 00 05 fe fe fe fe fe fe fe fe fe fe fe fe 349 * ... 350 * 351 * The length of ">ff00ff00ff00ff00: " is 352 * 3 + (BITS_PER_LONG / 8) * 2 chars. 353 * The length of each granule metadata is 2 bytes 354 * plus 1 byte for space. 355 */ 356 return 3 + (BITS_PER_LONG / 8) * 2 + 357 (addr - row) / KASAN_GRANULE_SIZE * 3 + 1; 358 } 359 360 static void print_memory_metadata(const void *addr) 361 { 362 int i; 363 void *row; 364 365 row = (void *)round_down((unsigned long)addr, META_MEM_BYTES_PER_ROW) 366 - META_ROWS_AROUND_ADDR * META_MEM_BYTES_PER_ROW; 367 368 pr_err("Memory state around the buggy address:\n"); 369 370 for (i = -META_ROWS_AROUND_ADDR; i <= META_ROWS_AROUND_ADDR; i++) { 371 char buffer[4 + (BITS_PER_LONG / 8) * 2]; 372 char metadata[META_BYTES_PER_ROW]; 373 374 snprintf(buffer, sizeof(buffer), 375 (i == 0) ? ">%px: " : " %px: ", row); 376 377 /* 378 * We should not pass a shadow pointer to generic 379 * function, because generic functions may try to 380 * access kasan mapping for the passed address. 381 */ 382 kasan_metadata_fetch_row(&metadata[0], row); 383 384 print_hex_dump(KERN_ERR, buffer, 385 DUMP_PREFIX_NONE, META_BYTES_PER_ROW, 1, 386 metadata, META_BYTES_PER_ROW, 0); 387 388 if (meta_row_is_guilty(row, addr)) 389 pr_err("%*c\n", meta_pointer_offset(row, addr), '^'); 390 391 row += META_MEM_BYTES_PER_ROW; 392 } 393 } 394 395 static void print_report(struct kasan_report_info *info) 396 { 397 void *addr = kasan_reset_tag(info->access_addr); 398 u8 tag = get_tag(info->access_addr); 399 400 print_error_description(info); 401 if (addr_has_metadata(addr)) 402 kasan_print_tags(tag, info->first_bad_addr); 403 pr_err("\n"); 404 405 if (addr_has_metadata(addr)) { 406 print_address_description(addr, tag, info); 407 print_memory_metadata(info->first_bad_addr); 408 } else { 409 dump_stack_lvl(KERN_ERR); 410 } 411 } 412 413 static void complete_report_info(struct kasan_report_info *info) 414 { 415 void *addr = kasan_reset_tag(info->access_addr); 416 struct slab *slab; 417 418 if (info->type == KASAN_REPORT_ACCESS) 419 info->first_bad_addr = kasan_find_first_bad_addr( 420 info->access_addr, info->access_size); 421 else 422 info->first_bad_addr = addr; 423 424 slab = kasan_addr_to_slab(addr); 425 if (slab) { 426 info->cache = slab->slab_cache; 427 info->object = nearest_obj(info->cache, slab, addr); 428 } else 429 info->cache = info->object = NULL; 430 } 431 432 void kasan_report_invalid_free(void *ptr, unsigned long ip, enum kasan_report_type type) 433 { 434 unsigned long flags; 435 struct kasan_report_info info; 436 437 /* 438 * Do not check report_suppressed(), as an invalid-free cannot be 439 * caused by accessing slab metadata and thus should not be 440 * suppressed by kasan_disable/enable_current() critical sections. 441 */ 442 if (unlikely(!report_enabled())) 443 return; 444 445 start_report(&flags, true); 446 447 info.type = type; 448 info.access_addr = ptr; 449 info.access_size = 0; 450 info.is_write = false; 451 info.ip = ip; 452 453 complete_report_info(&info); 454 455 print_report(&info); 456 457 end_report(&flags, ptr); 458 } 459 460 /* 461 * kasan_report() is the only reporting function that uses 462 * user_access_save/restore(): kasan_report_invalid_free() cannot be called 463 * from a UACCESS region, and kasan_report_async() is not used on x86. 464 */ 465 bool kasan_report(unsigned long addr, size_t size, bool is_write, 466 unsigned long ip) 467 { 468 bool ret = true; 469 void *ptr = (void *)addr; 470 unsigned long ua_flags = user_access_save(); 471 unsigned long irq_flags; 472 struct kasan_report_info info; 473 474 if (unlikely(report_suppressed()) || unlikely(!report_enabled())) { 475 ret = false; 476 goto out; 477 } 478 479 start_report(&irq_flags, true); 480 481 info.type = KASAN_REPORT_ACCESS; 482 info.access_addr = ptr; 483 info.access_size = size; 484 info.is_write = is_write; 485 info.ip = ip; 486 487 complete_report_info(&info); 488 489 print_report(&info); 490 491 end_report(&irq_flags, ptr); 492 493 out: 494 user_access_restore(ua_flags); 495 496 return ret; 497 } 498 499 #ifdef CONFIG_KASAN_HW_TAGS 500 void kasan_report_async(void) 501 { 502 unsigned long flags; 503 504 /* 505 * Do not check report_suppressed(), as kasan_disable/enable_current() 506 * critical sections do not affect Hardware Tag-Based KASAN. 507 */ 508 if (unlikely(!report_enabled())) 509 return; 510 511 start_report(&flags, false); 512 pr_err("BUG: KASAN: invalid-access\n"); 513 pr_err("Asynchronous fault: no details available\n"); 514 pr_err("\n"); 515 dump_stack_lvl(KERN_ERR); 516 end_report(&flags, NULL); 517 } 518 #endif /* CONFIG_KASAN_HW_TAGS */ 519 520 #ifdef CONFIG_KASAN_INLINE 521 /* 522 * With CONFIG_KASAN_INLINE, accesses to bogus pointers (outside the high 523 * canonical half of the address space) cause out-of-bounds shadow memory reads 524 * before the actual access. For addresses in the low canonical half of the 525 * address space, as well as most non-canonical addresses, that out-of-bounds 526 * shadow memory access lands in the non-canonical part of the address space. 527 * Help the user figure out what the original bogus pointer was. 528 */ 529 void kasan_non_canonical_hook(unsigned long addr) 530 { 531 unsigned long orig_addr; 532 const char *bug_type; 533 534 if (addr < KASAN_SHADOW_OFFSET) 535 return; 536 537 orig_addr = (addr - KASAN_SHADOW_OFFSET) << KASAN_SHADOW_SCALE_SHIFT; 538 /* 539 * For faults near the shadow address for NULL, we can be fairly certain 540 * that this is a KASAN shadow memory access. 541 * For faults that correspond to shadow for low canonical addresses, we 542 * can still be pretty sure - that shadow region is a fairly narrow 543 * chunk of the non-canonical address space. 544 * But faults that look like shadow for non-canonical addresses are a 545 * really large chunk of the address space. In that case, we still 546 * print the decoded address, but make it clear that this is not 547 * necessarily what's actually going on. 548 */ 549 if (orig_addr < PAGE_SIZE) 550 bug_type = "null-ptr-deref"; 551 else if (orig_addr < TASK_SIZE) 552 bug_type = "probably user-memory-access"; 553 else 554 bug_type = "maybe wild-memory-access"; 555 pr_alert("KASAN: %s in range [0x%016lx-0x%016lx]\n", bug_type, 556 orig_addr, orig_addr + KASAN_GRANULE_SIZE - 1); 557 } 558 #endif 559