LCOV - code coverage report
Current view: top level - mm/kasan - report.c (source / functions) Hit Total Coverage
Test: landlock.info Lines: 12 192 6.2 %
Date: 2021-04-22 12:43:58 Functions: 3 19 15.8 %

          Line data    Source code
       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/mm.h>
      17             : #include <linux/printk.h>
      18             : #include <linux/sched.h>
      19             : #include <linux/slab.h>
      20             : #include <linux/stackdepot.h>
      21             : #include <linux/stacktrace.h>
      22             : #include <linux/string.h>
      23             : #include <linux/types.h>
      24             : #include <linux/kasan.h>
      25             : #include <linux/module.h>
      26             : #include <linux/sched/task_stack.h>
      27             : #include <linux/uaccess.h>
      28             : #include <trace/events/error_report.h>
      29             : 
      30             : #include <asm/sections.h>
      31             : 
      32             : #include <kunit/test.h>
      33             : 
      34             : #include "kasan.h"
      35             : #include "../slab.h"
      36             : 
      37             : static unsigned long kasan_flags;
      38             : 
      39             : #define KASAN_BIT_REPORTED      0
      40             : #define KASAN_BIT_MULTI_SHOT    1
      41             : 
      42           0 : bool kasan_save_enable_multi_shot(void)
      43             : {
      44           0 :         return test_and_set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
      45             : }
      46             : EXPORT_SYMBOL_GPL(kasan_save_enable_multi_shot);
      47             : 
      48           0 : void kasan_restore_multi_shot(bool enabled)
      49             : {
      50           0 :         if (!enabled)
      51           0 :                 clear_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
      52           0 : }
      53             : EXPORT_SYMBOL_GPL(kasan_restore_multi_shot);
      54             : 
      55           0 : static int __init kasan_set_multi_shot(char *str)
      56             : {
      57           0 :         set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
      58           0 :         return 1;
      59             : }
      60             : __setup("kasan_multi_shot", kasan_set_multi_shot);
      61             : 
      62           0 : static void print_error_description(struct kasan_access_info *info)
      63             : {
      64           0 :         pr_err("BUG: KASAN: %s in %pS\n",
      65             :                 kasan_get_bug_type(info), (void *)info->ip);
      66           0 :         if (info->access_size)
      67           0 :                 pr_err("%s of size %zu at addr %px by task %s/%d\n",
      68             :                         info->is_write ? "Write" : "Read", info->access_size,
      69             :                         info->access_addr, current->comm, task_pid_nr(current));
      70             :         else
      71           0 :                 pr_err("%s at addr %px by task %s/%d\n",
      72             :                         info->is_write ? "Write" : "Read",
      73             :                         info->access_addr, current->comm, task_pid_nr(current));
      74           0 : }
      75             : 
      76             : static DEFINE_SPINLOCK(report_lock);
      77             : 
      78           0 : static void start_report(unsigned long *flags)
      79             : {
      80             :         /*
      81             :          * Make sure we don't end up in loop.
      82             :          */
      83           0 :         kasan_disable_current();
      84           0 :         spin_lock_irqsave(&report_lock, *flags);
      85           0 :         pr_err("==================================================================\n");
      86           0 : }
      87             : 
      88           0 : static void end_report(unsigned long *flags, unsigned long addr)
      89             : {
      90           0 :         trace_error_report_end(ERROR_DETECTOR_KASAN, addr);
      91           0 :         pr_err("==================================================================\n");
      92           0 :         add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
      93           0 :         spin_unlock_irqrestore(&report_lock, *flags);
      94           0 :         if (panic_on_warn && !test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags)) {
      95             :                 /*
      96             :                  * This thread may hit another WARN() in the panic path.
      97             :                  * Resetting this prevents additional WARN() from panicking the
      98             :                  * system on this thread.  Other threads are blocked by the
      99             :                  * panic_mutex in panic().
     100             :                  */
     101           0 :                 panic_on_warn = 0;
     102           0 :                 panic("panic_on_warn set ...\n");
     103             :         }
     104             : #ifdef CONFIG_KASAN_HW_TAGS
     105             :         if (kasan_flag_panic)
     106             :                 panic("kasan.fault=panic set ...\n");
     107             : #endif
     108           0 :         kasan_enable_current();
     109           0 : }
     110             : 
     111           0 : static void print_stack(depot_stack_handle_t stack)
     112             : {
     113           0 :         unsigned long *entries;
     114           0 :         unsigned int nr_entries;
     115             : 
     116           0 :         nr_entries = stack_depot_fetch(stack, &entries);
     117           0 :         stack_trace_print(entries, nr_entries, 0);
     118           0 : }
     119             : 
     120           0 : static void print_track(struct kasan_track *track, const char *prefix)
     121             : {
     122           0 :         pr_err("%s by task %u:\n", prefix, track->pid);
     123           0 :         if (track->stack) {
     124           0 :                 print_stack(track->stack);
     125             :         } else {
     126           0 :                 pr_err("(stack is not available)\n");
     127             :         }
     128           0 : }
     129             : 
     130      734509 : struct page *kasan_addr_to_page(const void *addr)
     131             : {
     132      734509 :         if ((addr >= (void *)PAGE_OFFSET) &&
     133      734509 :                         (addr < high_memory))
     134      733951 :                 return virt_to_head_page(addr);
     135             :         return NULL;
     136             : }
     137             : 
     138           0 : static void describe_object_addr(struct kmem_cache *cache, void *object,
     139             :                                 const void *addr)
     140             : {
     141           0 :         unsigned long access_addr = (unsigned long)addr;
     142           0 :         unsigned long object_addr = (unsigned long)object;
     143           0 :         const char *rel_type;
     144           0 :         int rel_bytes;
     145             : 
     146           0 :         pr_err("The buggy address belongs to the object at %px\n"
     147             :                " which belongs to the cache %s of size %d\n",
     148             :                 object, cache->name, cache->object_size);
     149             : 
     150           0 :         if (!addr)
     151             :                 return;
     152             : 
     153           0 :         if (access_addr < object_addr) {
     154           0 :                 rel_type = "to the left";
     155           0 :                 rel_bytes = object_addr - access_addr;
     156           0 :         } else if (access_addr >= object_addr + cache->object_size) {
     157           0 :                 rel_type = "to the right";
     158           0 :                 rel_bytes = access_addr - (object_addr + cache->object_size);
     159             :         } else {
     160           0 :                 rel_type = "inside";
     161           0 :                 rel_bytes = access_addr - object_addr;
     162             :         }
     163             : 
     164           0 :         pr_err("The buggy address is located %d bytes %s of\n"
     165             :                " %d-byte region [%px, %px)\n",
     166             :                 rel_bytes, rel_type, cache->object_size, (void *)object_addr,
     167             :                 (void *)(object_addr + cache->object_size));
     168             : }
     169             : 
     170           0 : static void describe_object_stacks(struct kmem_cache *cache, void *object,
     171             :                                         const void *addr, u8 tag)
     172             : {
     173           0 :         struct kasan_alloc_meta *alloc_meta;
     174           0 :         struct kasan_track *free_track;
     175             : 
     176           0 :         alloc_meta = kasan_get_alloc_meta(cache, object);
     177           0 :         if (alloc_meta) {
     178           0 :                 print_track(&alloc_meta->alloc_track, "Allocated");
     179           0 :                 pr_err("\n");
     180             :         }
     181             : 
     182           0 :         free_track = kasan_get_free_track(cache, object, tag);
     183           0 :         if (free_track) {
     184           0 :                 print_track(free_track, "Freed");
     185           0 :                 pr_err("\n");
     186             :         }
     187             : 
     188             : #ifdef CONFIG_KASAN_GENERIC
     189           0 :         if (!alloc_meta)
     190             :                 return;
     191           0 :         if (alloc_meta->aux_stack[0]) {
     192           0 :                 pr_err("Last potentially related work creation:\n");
     193           0 :                 print_stack(alloc_meta->aux_stack[0]);
     194           0 :                 pr_err("\n");
     195             :         }
     196           0 :         if (alloc_meta->aux_stack[1]) {
     197           0 :                 pr_err("Second to last potentially related work creation:\n");
     198           0 :                 print_stack(alloc_meta->aux_stack[1]);
     199           0 :                 pr_err("\n");
     200             :         }
     201             : #endif
     202             : }
     203             : 
     204           0 : static void describe_object(struct kmem_cache *cache, void *object,
     205             :                                 const void *addr, u8 tag)
     206             : {
     207           0 :         if (kasan_stack_collection_enabled())
     208           0 :                 describe_object_stacks(cache, object, addr, tag);
     209           0 :         describe_object_addr(cache, object, addr);
     210           0 : }
     211             : 
     212           0 : static inline bool kernel_or_module_addr(const void *addr)
     213             : {
     214           0 :         if (addr >= (void *)_stext && addr < (void *)_end)
     215           0 :                 return true;
     216           0 :         if (is_module_address((unsigned long)addr))
     217             :                 return true;
     218             :         return false;
     219             : }
     220             : 
     221           0 : static inline bool init_task_stack_addr(const void *addr)
     222             : {
     223           0 :         return addr >= (void *)&init_thread_union.stack &&
     224             :                 (addr <= (void *)&init_thread_union.stack +
     225             :                         sizeof(init_thread_union.stack));
     226             : }
     227             : 
     228           0 : static void print_address_description(void *addr, u8 tag)
     229             : {
     230           0 :         struct page *page = kasan_addr_to_page(addr);
     231             : 
     232           0 :         dump_stack();
     233           0 :         pr_err("\n");
     234             : 
     235           0 :         if (page && PageSlab(page)) {
     236           0 :                 struct kmem_cache *cache = page->slab_cache;
     237           0 :                 void *object = nearest_obj(cache, page, addr);
     238             : 
     239           0 :                 describe_object(cache, object, addr, tag);
     240             :         }
     241             : 
     242           0 :         if (kernel_or_module_addr(addr) && !init_task_stack_addr(addr)) {
     243           0 :                 pr_err("The buggy address belongs to the variable:\n");
     244           0 :                 pr_err(" %pS\n", addr);
     245             :         }
     246             : 
     247           0 :         if (page) {
     248           0 :                 pr_err("The buggy address belongs to the page:\n");
     249           0 :                 dump_page(page, "kasan: bad access detected");
     250             :         }
     251             : 
     252           0 :         kasan_print_address_stack_frame(addr);
     253           0 : }
     254             : 
     255           0 : static bool meta_row_is_guilty(const void *row, const void *addr)
     256             : {
     257           0 :         return (row <= addr) && (addr < row + META_MEM_BYTES_PER_ROW);
     258             : }
     259             : 
     260           0 : static int meta_pointer_offset(const void *row, const void *addr)
     261             : {
     262             :         /*
     263             :          * Memory state around the buggy address:
     264             :          *  ff00ff00ff00ff00: 00 00 00 05 fe fe fe fe fe fe fe fe fe fe fe fe
     265             :          *  ...
     266             :          *
     267             :          * The length of ">ff00ff00ff00ff00: " is
     268             :          *    3 + (BITS_PER_LONG / 8) * 2 chars.
     269             :          * The length of each granule metadata is 2 bytes
     270             :          *    plus 1 byte for space.
     271             :          */
     272           0 :         return 3 + (BITS_PER_LONG / 8) * 2 +
     273           0 :                 (addr - row) / KASAN_GRANULE_SIZE * 3 + 1;
     274             : }
     275             : 
     276           0 : static void print_memory_metadata(const void *addr)
     277             : {
     278           0 :         int i;
     279           0 :         void *row;
     280             : 
     281           0 :         row = (void *)round_down((unsigned long)addr, META_MEM_BYTES_PER_ROW)
     282           0 :                         - META_ROWS_AROUND_ADDR * META_MEM_BYTES_PER_ROW;
     283             : 
     284           0 :         pr_err("Memory state around the buggy address:\n");
     285             : 
     286           0 :         for (i = -META_ROWS_AROUND_ADDR; i <= META_ROWS_AROUND_ADDR; i++) {
     287           0 :                 char buffer[4 + (BITS_PER_LONG / 8) * 2];
     288           0 :                 char metadata[META_BYTES_PER_ROW];
     289             : 
     290           0 :                 snprintf(buffer, sizeof(buffer),
     291             :                                 (i == 0) ? ">%px: " : " %px: ", row);
     292             : 
     293             :                 /*
     294             :                  * We should not pass a shadow pointer to generic
     295             :                  * function, because generic functions may try to
     296             :                  * access kasan mapping for the passed address.
     297             :                  */
     298           0 :                 kasan_metadata_fetch_row(&metadata[0], row);
     299             : 
     300           0 :                 print_hex_dump(KERN_ERR, buffer,
     301             :                         DUMP_PREFIX_NONE, META_BYTES_PER_ROW, 1,
     302             :                         metadata, META_BYTES_PER_ROW, 0);
     303             : 
     304           0 :                 if (meta_row_is_guilty(row, addr))
     305           0 :                         pr_err("%*c\n", meta_pointer_offset(row, addr), '^');
     306             : 
     307           0 :                 row += META_MEM_BYTES_PER_ROW;
     308             :         }
     309           0 : }
     310             : 
     311       59947 : static bool report_enabled(void)
     312             : {
     313             : #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
     314       59947 :         if (current->kasan_depth)
     315             :                 return false;
     316             : #endif
     317           0 :         if (test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags))
     318             :                 return true;
     319           0 :         return !test_and_set_bit(KASAN_BIT_REPORTED, &kasan_flags);
     320             : }
     321             : 
     322             : #if IS_ENABLED(CONFIG_KUNIT)
     323             : static void kasan_update_kunit_status(struct kunit *cur_test)
     324             : {
     325             :         struct kunit_resource *resource;
     326             :         struct kunit_kasan_expectation *kasan_data;
     327             : 
     328             :         resource = kunit_find_named_resource(cur_test, "kasan_data");
     329             : 
     330             :         if (!resource) {
     331             :                 kunit_set_failure(cur_test);
     332             :                 return;
     333             :         }
     334             : 
     335             :         kasan_data = (struct kunit_kasan_expectation *)resource->data;
     336             :         WRITE_ONCE(kasan_data->report_found, true);
     337             :         kunit_put_resource(resource);
     338             : }
     339             : #endif /* IS_ENABLED(CONFIG_KUNIT) */
     340             : 
     341           0 : void kasan_report_invalid_free(void *object, unsigned long ip)
     342             : {
     343           0 :         unsigned long flags;
     344           0 :         u8 tag = get_tag(object);
     345             : 
     346           0 :         object = kasan_reset_tag(object);
     347             : 
     348             : #if IS_ENABLED(CONFIG_KUNIT)
     349             :         if (current->kunit_test)
     350             :                 kasan_update_kunit_status(current->kunit_test);
     351             : #endif /* IS_ENABLED(CONFIG_KUNIT) */
     352             : 
     353           0 :         start_report(&flags);
     354           0 :         pr_err("BUG: KASAN: double-free or invalid-free in %pS\n", (void *)ip);
     355           0 :         kasan_print_tags(tag, object);
     356           0 :         pr_err("\n");
     357           0 :         print_address_description(object, tag);
     358           0 :         pr_err("\n");
     359           0 :         print_memory_metadata(object);
     360           0 :         end_report(&flags, (unsigned long)object);
     361           0 : }
     362             : 
     363           0 : static void __kasan_report(unsigned long addr, size_t size, bool is_write,
     364             :                                 unsigned long ip)
     365             : {
     366           0 :         struct kasan_access_info info;
     367           0 :         void *tagged_addr;
     368           0 :         void *untagged_addr;
     369           0 :         unsigned long flags;
     370             : 
     371             : #if IS_ENABLED(CONFIG_KUNIT)
     372             :         if (current->kunit_test)
     373             :                 kasan_update_kunit_status(current->kunit_test);
     374             : #endif /* IS_ENABLED(CONFIG_KUNIT) */
     375             : 
     376           0 :         disable_trace_on_warning();
     377             : 
     378           0 :         tagged_addr = (void *)addr;
     379           0 :         untagged_addr = kasan_reset_tag(tagged_addr);
     380             : 
     381           0 :         info.access_addr = tagged_addr;
     382           0 :         if (addr_has_metadata(untagged_addr))
     383           0 :                 info.first_bad_addr =
     384           0 :                         kasan_find_first_bad_addr(tagged_addr, size);
     385             :         else
     386           0 :                 info.first_bad_addr = untagged_addr;
     387           0 :         info.access_size = size;
     388           0 :         info.is_write = is_write;
     389           0 :         info.ip = ip;
     390             : 
     391           0 :         start_report(&flags);
     392             : 
     393           0 :         print_error_description(&info);
     394           0 :         if (addr_has_metadata(untagged_addr))
     395           0 :                 kasan_print_tags(get_tag(tagged_addr), info.first_bad_addr);
     396           0 :         pr_err("\n");
     397             : 
     398           0 :         if (addr_has_metadata(untagged_addr)) {
     399           0 :                 print_address_description(untagged_addr, get_tag(tagged_addr));
     400           0 :                 pr_err("\n");
     401           0 :                 print_memory_metadata(info.first_bad_addr);
     402             :         } else {
     403           0 :                 dump_stack();
     404             :         }
     405             : 
     406           0 :         end_report(&flags, addr);
     407           0 : }
     408             : 
     409       59947 : bool kasan_report(unsigned long addr, size_t size, bool is_write,
     410             :                         unsigned long ip)
     411             : {
     412       59947 :         unsigned long flags = user_access_save();
     413       59947 :         bool ret = false;
     414             : 
     415       59947 :         if (likely(report_enabled())) {
     416           0 :                 __kasan_report(addr, size, is_write, ip);
     417           0 :                 ret = true;
     418             :         }
     419             : 
     420       59947 :         user_access_restore(flags);
     421             : 
     422       59947 :         return ret;
     423             : }
     424             : 
     425             : #ifdef CONFIG_KASAN_INLINE
     426             : /*
     427             :  * With CONFIG_KASAN_INLINE, accesses to bogus pointers (outside the high
     428             :  * canonical half of the address space) cause out-of-bounds shadow memory reads
     429             :  * before the actual access. For addresses in the low canonical half of the
     430             :  * address space, as well as most non-canonical addresses, that out-of-bounds
     431             :  * shadow memory access lands in the non-canonical part of the address space.
     432             :  * Help the user figure out what the original bogus pointer was.
     433             :  */
     434           0 : void kasan_non_canonical_hook(unsigned long addr)
     435             : {
     436           0 :         unsigned long orig_addr;
     437           0 :         const char *bug_type;
     438             : 
     439           0 :         if (addr < KASAN_SHADOW_OFFSET)
     440             :                 return;
     441             : 
     442           0 :         orig_addr = (addr - KASAN_SHADOW_OFFSET) << KASAN_SHADOW_SCALE_SHIFT;
     443             :         /*
     444             :          * For faults near the shadow address for NULL, we can be fairly certain
     445             :          * that this is a KASAN shadow memory access.
     446             :          * For faults that correspond to shadow for low canonical addresses, we
     447             :          * can still be pretty sure - that shadow region is a fairly narrow
     448             :          * chunk of the non-canonical address space.
     449             :          * But faults that look like shadow for non-canonical addresses are a
     450             :          * really large chunk of the address space. In that case, we still
     451             :          * print the decoded address, but make it clear that this is not
     452             :          * necessarily what's actually going on.
     453             :          */
     454           0 :         if (orig_addr < PAGE_SIZE)
     455             :                 bug_type = "null-ptr-deref";
     456           0 :         else if (orig_addr < TASK_SIZE)
     457             :                 bug_type = "probably user-memory-access";
     458             :         else
     459           0 :                 bug_type = "maybe wild-memory-access";
     460           0 :         pr_alert("KASAN: %s in range [0x%016lx-0x%016lx]\n", bug_type,
     461             :                  orig_addr, orig_addr + KASAN_GRANULE_SIZE - 1);
     462             : }
     463             : #endif

Generated by: LCOV version 1.14