linux/mm/kasan/report.c
<<
>>
Prefs
   1/*
   2 * This file contains error reporting code.
   3 *
   4 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
   5 * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
   6 *
   7 * Some code borrowed from https://github.com/xairy/kasan-prototype by
   8 *        Andrey Konovalov <adech.fo@gmail.com>
   9 *
  10 * This program is free software; you can redistribute it and/or modify
  11 * it under the terms of the GNU General Public License version 2 as
  12 * published by the Free Software Foundation.
  13 *
  14 */
  15
  16#include <linux/kernel.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
  28#include <asm/sections.h>
  29
  30#include "kasan.h"
  31#include "../slab.h"
  32
  33/* Shadow layout customization. */
  34#define SHADOW_BYTES_PER_BLOCK 1
  35#define SHADOW_BLOCKS_PER_ROW 16
  36#define SHADOW_BYTES_PER_ROW (SHADOW_BLOCKS_PER_ROW * SHADOW_BYTES_PER_BLOCK)
  37#define SHADOW_ROWS_AROUND_ADDR 2
  38
  39static const void *find_first_bad_addr(const void *addr, size_t size)
  40{
  41        u8 shadow_val = *(u8 *)kasan_mem_to_shadow(addr);
  42        const void *first_bad_addr = addr;
  43
  44        while (!shadow_val && first_bad_addr < addr + size) {
  45                first_bad_addr += KASAN_SHADOW_SCALE_SIZE;
  46                shadow_val = *(u8 *)kasan_mem_to_shadow(first_bad_addr);
  47        }
  48        return first_bad_addr;
  49}
  50
  51static void print_error_description(struct kasan_access_info *info)
  52{
  53        const char *bug_type = "unknown-crash";
  54        u8 *shadow_addr;
  55
  56        info->first_bad_addr = find_first_bad_addr(info->access_addr,
  57                                                info->access_size);
  58
  59        shadow_addr = (u8 *)kasan_mem_to_shadow(info->first_bad_addr);
  60
  61        /*
  62         * If shadow byte value is in [0, KASAN_SHADOW_SCALE_SIZE) we can look
  63         * at the next shadow byte to determine the type of the bad access.
  64         */
  65        if (*shadow_addr > 0 && *shadow_addr <= KASAN_SHADOW_SCALE_SIZE - 1)
  66                shadow_addr++;
  67
  68        switch (*shadow_addr) {
  69        case 0 ... KASAN_SHADOW_SCALE_SIZE - 1:
  70                /*
  71                 * In theory it's still possible to see these shadow values
  72                 * due to a data race in the kernel code.
  73                 */
  74                bug_type = "out-of-bounds";
  75                break;
  76        case KASAN_PAGE_REDZONE:
  77        case KASAN_KMALLOC_REDZONE:
  78                bug_type = "slab-out-of-bounds";
  79                break;
  80        case KASAN_GLOBAL_REDZONE:
  81                bug_type = "global-out-of-bounds";
  82                break;
  83        case KASAN_STACK_LEFT:
  84        case KASAN_STACK_MID:
  85        case KASAN_STACK_RIGHT:
  86        case KASAN_STACK_PARTIAL:
  87                bug_type = "stack-out-of-bounds";
  88                break;
  89        case KASAN_FREE_PAGE:
  90        case KASAN_KMALLOC_FREE:
  91                bug_type = "use-after-free";
  92                break;
  93        case KASAN_USE_AFTER_SCOPE:
  94                bug_type = "use-after-scope";
  95                break;
  96        }
  97
  98        pr_err("BUG: KASAN: %s in %pS at addr %p\n",
  99                bug_type, (void *)info->ip,
 100                info->access_addr);
 101        pr_err("%s of size %zu by task %s/%d\n",
 102                info->is_write ? "Write" : "Read",
 103                info->access_size, current->comm, task_pid_nr(current));
 104}
 105
 106static inline bool kernel_or_module_addr(const void *addr)
 107{
 108        if (addr >= (void *)_stext && addr < (void *)_end)
 109                return true;
 110        if (is_module_address((unsigned long)addr))
 111                return true;
 112        return false;
 113}
 114
 115static inline bool init_task_stack_addr(const void *addr)
 116{
 117        return addr >= (void *)&init_thread_union.stack &&
 118                (addr <= (void *)&init_thread_union.stack +
 119                        sizeof(init_thread_union.stack));
 120}
 121
 122static DEFINE_SPINLOCK(report_lock);
 123
 124static void kasan_start_report(unsigned long *flags)
 125{
 126        /*
 127         * Make sure we don't end up in loop.
 128         */
 129        kasan_disable_current();
 130        spin_lock_irqsave(&report_lock, *flags);
 131        pr_err("==================================================================\n");
 132}
 133
 134static void kasan_end_report(unsigned long *flags)
 135{
 136        pr_err("==================================================================\n");
 137        add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
 138        spin_unlock_irqrestore(&report_lock, *flags);
 139        kasan_enable_current();
 140}
 141
 142static void print_track(struct kasan_track *track)
 143{
 144        pr_err("PID = %u\n", track->pid);
 145        if (track->stack) {
 146                struct stack_trace trace;
 147
 148                depot_fetch_stack(track->stack, &trace);
 149                print_stack_trace(&trace, 0);
 150        } else {
 151                pr_err("(stack is not available)\n");
 152        }
 153}
 154
 155static void kasan_object_err(struct kmem_cache *cache, void *object)
 156{
 157        struct kasan_alloc_meta *alloc_info = get_alloc_info(cache, object);
 158
 159        dump_stack();
 160        pr_err("Object at %p, in cache %s size: %d\n", object, cache->name,
 161                cache->object_size);
 162
 163        if (!(cache->flags & SLAB_KASAN))
 164                return;
 165
 166        pr_err("Allocated:\n");
 167        print_track(&alloc_info->alloc_track);
 168        pr_err("Freed:\n");
 169        print_track(&alloc_info->free_track);
 170}
 171
 172void kasan_report_double_free(struct kmem_cache *cache, void *object,
 173                        s8 shadow)
 174{
 175        unsigned long flags;
 176
 177        kasan_start_report(&flags);
 178        pr_err("BUG: Double free or freeing an invalid pointer\n");
 179        pr_err("Unexpected shadow byte: 0x%hhX\n", shadow);
 180        kasan_object_err(cache, object);
 181        kasan_end_report(&flags);
 182}
 183
 184static void print_address_description(struct kasan_access_info *info)
 185{
 186        const void *addr = info->access_addr;
 187
 188        if ((addr >= (void *)PAGE_OFFSET) &&
 189                (addr < high_memory)) {
 190                struct page *page = virt_to_head_page(addr);
 191
 192                if (PageSlab(page)) {
 193                        void *object;
 194                        struct kmem_cache *cache = page->slab_cache;
 195                        object = nearest_obj(cache, page,
 196                                                (void *)info->access_addr);
 197                        kasan_object_err(cache, object);
 198                        return;
 199                }
 200                dump_page(page, "kasan: bad access detected");
 201        }
 202
 203        if (kernel_or_module_addr(addr)) {
 204                if (!init_task_stack_addr(addr))
 205                        pr_err("Address belongs to variable %pS\n", addr);
 206        }
 207        dump_stack();
 208}
 209
 210static bool row_is_guilty(const void *row, const void *guilty)
 211{
 212        return (row <= guilty) && (guilty < row + SHADOW_BYTES_PER_ROW);
 213}
 214
 215static int shadow_pointer_offset(const void *row, const void *shadow)
 216{
 217        /* The length of ">ff00ff00ff00ff00: " is
 218         *    3 + (BITS_PER_LONG/8)*2 chars.
 219         */
 220        return 3 + (BITS_PER_LONG/8)*2 + (shadow - row)*2 +
 221                (shadow - row) / SHADOW_BYTES_PER_BLOCK + 1;
 222}
 223
 224static void print_shadow_for_address(const void *addr)
 225{
 226        int i;
 227        const void *shadow = kasan_mem_to_shadow(addr);
 228        const void *shadow_row;
 229
 230        shadow_row = (void *)round_down((unsigned long)shadow,
 231                                        SHADOW_BYTES_PER_ROW)
 232                - SHADOW_ROWS_AROUND_ADDR * SHADOW_BYTES_PER_ROW;
 233
 234        pr_err("Memory state around the buggy address:\n");
 235
 236        for (i = -SHADOW_ROWS_AROUND_ADDR; i <= SHADOW_ROWS_AROUND_ADDR; i++) {
 237                const void *kaddr = kasan_shadow_to_mem(shadow_row);
 238                char buffer[4 + (BITS_PER_LONG/8)*2];
 239                char shadow_buf[SHADOW_BYTES_PER_ROW];
 240
 241                snprintf(buffer, sizeof(buffer),
 242                        (i == 0) ? ">%p: " : " %p: ", kaddr);
 243                /*
 244                 * We should not pass a shadow pointer to generic
 245                 * function, because generic functions may try to
 246                 * access kasan mapping for the passed address.
 247                 */
 248                memcpy(shadow_buf, shadow_row, SHADOW_BYTES_PER_ROW);
 249                print_hex_dump(KERN_ERR, buffer,
 250                        DUMP_PREFIX_NONE, SHADOW_BYTES_PER_ROW, 1,
 251                        shadow_buf, SHADOW_BYTES_PER_ROW, 0);
 252
 253                if (row_is_guilty(shadow_row, shadow))
 254                        pr_err("%*c\n",
 255                                shadow_pointer_offset(shadow_row, shadow),
 256                                '^');
 257
 258                shadow_row += SHADOW_BYTES_PER_ROW;
 259        }
 260}
 261
 262static void kasan_report_error(struct kasan_access_info *info)
 263{
 264        unsigned long flags;
 265        const char *bug_type;
 266
 267        kasan_start_report(&flags);
 268
 269        if (info->access_addr <
 270                        kasan_shadow_to_mem((void *)KASAN_SHADOW_START)) {
 271                if ((unsigned long)info->access_addr < PAGE_SIZE)
 272                        bug_type = "null-ptr-deref";
 273                else if ((unsigned long)info->access_addr < TASK_SIZE)
 274                        bug_type = "user-memory-access";
 275                else
 276                        bug_type = "wild-memory-access";
 277                pr_err("BUG: KASAN: %s on address %p\n",
 278                        bug_type, info->access_addr);
 279                pr_err("%s of size %zu by task %s/%d\n",
 280                        info->is_write ? "Write" : "Read",
 281                        info->access_size, current->comm,
 282                        task_pid_nr(current));
 283                dump_stack();
 284        } else {
 285                print_error_description(info);
 286                print_address_description(info);
 287                print_shadow_for_address(info->first_bad_addr);
 288        }
 289
 290        kasan_end_report(&flags);
 291}
 292
 293void kasan_report(unsigned long addr, size_t size,
 294                bool is_write, unsigned long ip)
 295{
 296        struct kasan_access_info info;
 297
 298        if (likely(!kasan_report_enabled()))
 299                return;
 300
 301        info.access_addr = (void *)addr;
 302        info.access_size = size;
 303        info.is_write = is_write;
 304        info.ip = ip;
 305
 306        kasan_report_error(&info);
 307}
 308
 309
 310#define DEFINE_ASAN_REPORT_LOAD(size)                     \
 311void __asan_report_load##size##_noabort(unsigned long addr) \
 312{                                                         \
 313        kasan_report(addr, size, false, _RET_IP_);        \
 314}                                                         \
 315EXPORT_SYMBOL(__asan_report_load##size##_noabort)
 316
 317#define DEFINE_ASAN_REPORT_STORE(size)                     \
 318void __asan_report_store##size##_noabort(unsigned long addr) \
 319{                                                          \
 320        kasan_report(addr, size, true, _RET_IP_);          \
 321}                                                          \
 322EXPORT_SYMBOL(__asan_report_store##size##_noabort)
 323
 324DEFINE_ASAN_REPORT_LOAD(1);
 325DEFINE_ASAN_REPORT_LOAD(2);
 326DEFINE_ASAN_REPORT_LOAD(4);
 327DEFINE_ASAN_REPORT_LOAD(8);
 328DEFINE_ASAN_REPORT_LOAD(16);
 329DEFINE_ASAN_REPORT_STORE(1);
 330DEFINE_ASAN_REPORT_STORE(2);
 331DEFINE_ASAN_REPORT_STORE(4);
 332DEFINE_ASAN_REPORT_STORE(8);
 333DEFINE_ASAN_REPORT_STORE(16);
 334
 335void __asan_report_load_n_noabort(unsigned long addr, size_t size)
 336{
 337        kasan_report(addr, size, false, _RET_IP_);
 338}
 339EXPORT_SYMBOL(__asan_report_load_n_noabort);
 340
 341void __asan_report_store_n_noabort(unsigned long addr, size_t size)
 342{
 343        kasan_report(addr, size, true, _RET_IP_);
 344}
 345EXPORT_SYMBOL(__asan_report_store_n_noabort);
 346