linux/lib/ubsan.c
<<
>>
Prefs
   1/*
   2 * UBSAN error reporting functions
   3 *
   4 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
   5 * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
   6 *
   7 * This program is free software; you can redistribute it and/or modify
   8 * it under the terms of the GNU General Public License version 2 as
   9 * published by the Free Software Foundation.
  10 *
  11 */
  12
  13#include <linux/bitops.h>
  14#include <linux/bug.h>
  15#include <linux/ctype.h>
  16#include <linux/init.h>
  17#include <linux/kernel.h>
  18#include <linux/types.h>
  19#include <linux/sched.h>
  20#include <linux/uaccess.h>
  21
  22#include "ubsan.h"
  23
  24const char *type_check_kinds[] = {
  25        "load of",
  26        "store to",
  27        "reference binding to",
  28        "member access within",
  29        "member call on",
  30        "constructor call on",
  31        "downcast of",
  32        "downcast of"
  33};
  34
  35#define REPORTED_BIT 31
  36
  37#if (BITS_PER_LONG == 64) && defined(__BIG_ENDIAN)
  38#define COLUMN_MASK (~(1U << REPORTED_BIT))
  39#define LINE_MASK   (~0U)
  40#else
  41#define COLUMN_MASK   (~0U)
  42#define LINE_MASK (~(1U << REPORTED_BIT))
  43#endif
  44
  45#define VALUE_LENGTH 40
  46
  47static bool was_reported(struct source_location *location)
  48{
  49        return test_and_set_bit(REPORTED_BIT, &location->reported);
  50}
  51
  52static void print_source_location(const char *prefix,
  53                                struct source_location *loc)
  54{
  55        pr_err("%s %s:%d:%d\n", prefix, loc->file_name,
  56                loc->line & LINE_MASK, loc->column & COLUMN_MASK);
  57}
  58
  59static bool suppress_report(struct source_location *loc)
  60{
  61        return current->in_ubsan || was_reported(loc);
  62}
  63
  64static bool type_is_int(struct type_descriptor *type)
  65{
  66        return type->type_kind == type_kind_int;
  67}
  68
  69static bool type_is_signed(struct type_descriptor *type)
  70{
  71        WARN_ON(!type_is_int(type));
  72        return  type->type_info & 1;
  73}
  74
  75static unsigned type_bit_width(struct type_descriptor *type)
  76{
  77        return 1 << (type->type_info >> 1);
  78}
  79
  80static bool is_inline_int(struct type_descriptor *type)
  81{
  82        unsigned inline_bits = sizeof(unsigned long)*8;
  83        unsigned bits = type_bit_width(type);
  84
  85        WARN_ON(!type_is_int(type));
  86
  87        return bits <= inline_bits;
  88}
  89
  90static s_max get_signed_val(struct type_descriptor *type, unsigned long val)
  91{
  92        if (is_inline_int(type)) {
  93                unsigned extra_bits = sizeof(s_max)*8 - type_bit_width(type);
  94                return ((s_max)val) << extra_bits >> extra_bits;
  95        }
  96
  97        if (type_bit_width(type) == 64)
  98                return *(s64 *)val;
  99
 100        return *(s_max *)val;
 101}
 102
 103static bool val_is_negative(struct type_descriptor *type, unsigned long val)
 104{
 105        return type_is_signed(type) && get_signed_val(type, val) < 0;
 106}
 107
 108static u_max get_unsigned_val(struct type_descriptor *type, unsigned long val)
 109{
 110        if (is_inline_int(type))
 111                return val;
 112
 113        if (type_bit_width(type) == 64)
 114                return *(u64 *)val;
 115
 116        return *(u_max *)val;
 117}
 118
 119static void val_to_string(char *str, size_t size, struct type_descriptor *type,
 120        unsigned long value)
 121{
 122        if (type_is_int(type)) {
 123                if (type_bit_width(type) == 128) {
 124#if defined(CONFIG_ARCH_SUPPORTS_INT128) && defined(__SIZEOF_INT128__)
 125                        u_max val = get_unsigned_val(type, value);
 126
 127                        scnprintf(str, size, "0x%08x%08x%08x%08x",
 128                                (u32)(val >> 96),
 129                                (u32)(val >> 64),
 130                                (u32)(val >> 32),
 131                                (u32)(val));
 132#else
 133                        WARN_ON(1);
 134#endif
 135                } else if (type_is_signed(type)) {
 136                        scnprintf(str, size, "%lld",
 137                                (s64)get_signed_val(type, value));
 138                } else {
 139                        scnprintf(str, size, "%llu",
 140                                (u64)get_unsigned_val(type, value));
 141                }
 142        }
 143}
 144
 145static void ubsan_prologue(struct source_location *location)
 146{
 147        current->in_ubsan++;
 148
 149        pr_err("========================================"
 150                "========================================\n");
 151        print_source_location("UBSAN: Undefined behaviour in", location);
 152}
 153
 154static void ubsan_epilogue(void)
 155{
 156        dump_stack();
 157        pr_err("========================================"
 158                "========================================\n");
 159
 160        current->in_ubsan--;
 161}
 162
 163static void handle_overflow(struct overflow_data *data, unsigned long lhs,
 164                        unsigned long rhs, char op)
 165{
 166
 167        struct type_descriptor *type = data->type;
 168        char lhs_val_str[VALUE_LENGTH];
 169        char rhs_val_str[VALUE_LENGTH];
 170
 171        if (suppress_report(&data->location))
 172                return;
 173
 174        ubsan_prologue(&data->location);
 175
 176        val_to_string(lhs_val_str, sizeof(lhs_val_str), type, lhs);
 177        val_to_string(rhs_val_str, sizeof(rhs_val_str), type, rhs);
 178        pr_err("%s integer overflow:\n",
 179                type_is_signed(type) ? "signed" : "unsigned");
 180        pr_err("%s %c %s cannot be represented in type %s\n",
 181                lhs_val_str,
 182                op,
 183                rhs_val_str,
 184                type->type_name);
 185
 186        ubsan_epilogue();
 187}
 188
 189void __ubsan_handle_add_overflow(struct overflow_data *data,
 190                                unsigned long lhs,
 191                                unsigned long rhs)
 192{
 193
 194        handle_overflow(data, lhs, rhs, '+');
 195}
 196EXPORT_SYMBOL(__ubsan_handle_add_overflow);
 197
 198void __ubsan_handle_sub_overflow(struct overflow_data *data,
 199                                unsigned long lhs,
 200                                unsigned long rhs)
 201{
 202        handle_overflow(data, lhs, rhs, '-');
 203}
 204EXPORT_SYMBOL(__ubsan_handle_sub_overflow);
 205
 206void __ubsan_handle_mul_overflow(struct overflow_data *data,
 207                                unsigned long lhs,
 208                                unsigned long rhs)
 209{
 210        handle_overflow(data, lhs, rhs, '*');
 211}
 212EXPORT_SYMBOL(__ubsan_handle_mul_overflow);
 213
 214void __ubsan_handle_negate_overflow(struct overflow_data *data,
 215                                unsigned long old_val)
 216{
 217        char old_val_str[VALUE_LENGTH];
 218
 219        if (suppress_report(&data->location))
 220                return;
 221
 222        ubsan_prologue(&data->location);
 223
 224        val_to_string(old_val_str, sizeof(old_val_str), data->type, old_val);
 225
 226        pr_err("negation of %s cannot be represented in type %s:\n",
 227                old_val_str, data->type->type_name);
 228
 229        ubsan_epilogue();
 230}
 231EXPORT_SYMBOL(__ubsan_handle_negate_overflow);
 232
 233
 234void __ubsan_handle_divrem_overflow(struct overflow_data *data,
 235                                unsigned long lhs,
 236                                unsigned long rhs)
 237{
 238        char rhs_val_str[VALUE_LENGTH];
 239
 240        if (suppress_report(&data->location))
 241                return;
 242
 243        ubsan_prologue(&data->location);
 244
 245        val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
 246
 247        if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
 248                pr_err("division of %s by -1 cannot be represented in type %s\n",
 249                        rhs_val_str, data->type->type_name);
 250        else
 251                pr_err("division by zero\n");
 252
 253        ubsan_epilogue();
 254}
 255EXPORT_SYMBOL(__ubsan_handle_divrem_overflow);
 256
 257static void handle_null_ptr_deref(struct type_mismatch_data_common *data)
 258{
 259        if (suppress_report(data->location))
 260                return;
 261
 262        ubsan_prologue(data->location);
 263
 264        pr_err("%s null pointer of type %s\n",
 265                type_check_kinds[data->type_check_kind],
 266                data->type->type_name);
 267
 268        ubsan_epilogue();
 269}
 270
 271static void handle_misaligned_access(struct type_mismatch_data_common *data,
 272                                unsigned long ptr)
 273{
 274        if (suppress_report(data->location))
 275                return;
 276
 277        ubsan_prologue(data->location);
 278
 279        pr_err("%s misaligned address %p for type %s\n",
 280                type_check_kinds[data->type_check_kind],
 281                (void *)ptr, data->type->type_name);
 282        pr_err("which requires %ld byte alignment\n", data->alignment);
 283
 284        ubsan_epilogue();
 285}
 286
 287static void handle_object_size_mismatch(struct type_mismatch_data_common *data,
 288                                        unsigned long ptr)
 289{
 290        if (suppress_report(data->location))
 291                return;
 292
 293        ubsan_prologue(data->location);
 294        pr_err("%s address %p with insufficient space\n",
 295                type_check_kinds[data->type_check_kind],
 296                (void *) ptr);
 297        pr_err("for an object of type %s\n", data->type->type_name);
 298        ubsan_epilogue();
 299}
 300
 301static void ubsan_type_mismatch_common(struct type_mismatch_data_common *data,
 302                                unsigned long ptr)
 303{
 304        unsigned long flags = user_access_save();
 305
 306        if (!ptr)
 307                handle_null_ptr_deref(data);
 308        else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
 309                handle_misaligned_access(data, ptr);
 310        else
 311                handle_object_size_mismatch(data, ptr);
 312
 313        user_access_restore(flags);
 314}
 315
 316void __ubsan_handle_type_mismatch(struct type_mismatch_data *data,
 317                                unsigned long ptr)
 318{
 319        struct type_mismatch_data_common common_data = {
 320                .location = &data->location,
 321                .type = data->type,
 322                .alignment = data->alignment,
 323                .type_check_kind = data->type_check_kind
 324        };
 325
 326        ubsan_type_mismatch_common(&common_data, ptr);
 327}
 328EXPORT_SYMBOL(__ubsan_handle_type_mismatch);
 329
 330void __ubsan_handle_type_mismatch_v1(struct type_mismatch_data_v1 *data,
 331                                unsigned long ptr)
 332{
 333
 334        struct type_mismatch_data_common common_data = {
 335                .location = &data->location,
 336                .type = data->type,
 337                .alignment = 1UL << data->log_alignment,
 338                .type_check_kind = data->type_check_kind
 339        };
 340
 341        ubsan_type_mismatch_common(&common_data, ptr);
 342}
 343EXPORT_SYMBOL(__ubsan_handle_type_mismatch_v1);
 344
 345void __ubsan_handle_vla_bound_not_positive(struct vla_bound_data *data,
 346                                        unsigned long bound)
 347{
 348        char bound_str[VALUE_LENGTH];
 349
 350        if (suppress_report(&data->location))
 351                return;
 352
 353        ubsan_prologue(&data->location);
 354
 355        val_to_string(bound_str, sizeof(bound_str), data->type, bound);
 356        pr_err("variable length array bound value %s <= 0\n", bound_str);
 357
 358        ubsan_epilogue();
 359}
 360EXPORT_SYMBOL(__ubsan_handle_vla_bound_not_positive);
 361
 362void __ubsan_handle_out_of_bounds(struct out_of_bounds_data *data,
 363                                unsigned long index)
 364{
 365        char index_str[VALUE_LENGTH];
 366
 367        if (suppress_report(&data->location))
 368                return;
 369
 370        ubsan_prologue(&data->location);
 371
 372        val_to_string(index_str, sizeof(index_str), data->index_type, index);
 373        pr_err("index %s is out of range for type %s\n", index_str,
 374                data->array_type->type_name);
 375        ubsan_epilogue();
 376}
 377EXPORT_SYMBOL(__ubsan_handle_out_of_bounds);
 378
 379void __ubsan_handle_shift_out_of_bounds(struct shift_out_of_bounds_data *data,
 380                                        unsigned long lhs, unsigned long rhs)
 381{
 382        struct type_descriptor *rhs_type = data->rhs_type;
 383        struct type_descriptor *lhs_type = data->lhs_type;
 384        char rhs_str[VALUE_LENGTH];
 385        char lhs_str[VALUE_LENGTH];
 386        unsigned long ua_flags = user_access_save();
 387
 388        if (suppress_report(&data->location))
 389                goto out;
 390
 391        ubsan_prologue(&data->location);
 392
 393        val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
 394        val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
 395
 396        if (val_is_negative(rhs_type, rhs))
 397                pr_err("shift exponent %s is negative\n", rhs_str);
 398
 399        else if (get_unsigned_val(rhs_type, rhs) >=
 400                type_bit_width(lhs_type))
 401                pr_err("shift exponent %s is too large for %u-bit type %s\n",
 402                        rhs_str,
 403                        type_bit_width(lhs_type),
 404                        lhs_type->type_name);
 405        else if (val_is_negative(lhs_type, lhs))
 406                pr_err("left shift of negative value %s\n",
 407                        lhs_str);
 408        else
 409                pr_err("left shift of %s by %s places cannot be"
 410                        " represented in type %s\n",
 411                        lhs_str, rhs_str,
 412                        lhs_type->type_name);
 413
 414        ubsan_epilogue();
 415out:
 416        user_access_restore(ua_flags);
 417}
 418EXPORT_SYMBOL(__ubsan_handle_shift_out_of_bounds);
 419
 420
 421void __ubsan_handle_builtin_unreachable(struct unreachable_data *data)
 422{
 423        ubsan_prologue(&data->location);
 424        pr_err("calling __builtin_unreachable()\n");
 425        ubsan_epilogue();
 426        panic("can't return from __builtin_unreachable()");
 427}
 428EXPORT_SYMBOL(__ubsan_handle_builtin_unreachable);
 429
 430void __ubsan_handle_load_invalid_value(struct invalid_value_data *data,
 431                                unsigned long val)
 432{
 433        char val_str[VALUE_LENGTH];
 434
 435        if (suppress_report(&data->location))
 436                return;
 437
 438        ubsan_prologue(&data->location);
 439
 440        val_to_string(val_str, sizeof(val_str), data->type, val);
 441
 442        pr_err("load of value %s is not a valid value for type %s\n",
 443                val_str, data->type->type_name);
 444
 445        ubsan_epilogue();
 446}
 447EXPORT_SYMBOL(__ubsan_handle_load_invalid_value);
 448