report.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. #include <linux/ftrace.h>
  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/stacktrace.h>
  22. #include <linux/string.h>
  23. #include <linux/types.h>
  24. #include <linux/kasan.h>
  25. #include <linux/module.h>
  26. #include <asm/sections.h>
  27. #include "kasan.h"
  28. #include "../slab.h"
  29. /* Shadow layout customization. */
  30. #define SHADOW_BYTES_PER_BLOCK 1
  31. #define SHADOW_BLOCKS_PER_ROW 16
  32. #define SHADOW_BYTES_PER_ROW (SHADOW_BLOCKS_PER_ROW * SHADOW_BYTES_PER_BLOCK)
  33. #define SHADOW_ROWS_AROUND_ADDR 2
  34. static const void *find_first_bad_addr(const void *addr, size_t size)
  35. {
  36. u8 shadow_val = *(u8 *)kasan_mem_to_shadow(addr);
  37. const void *first_bad_addr = addr;
  38. while (!shadow_val && first_bad_addr < addr + size) {
  39. first_bad_addr += KASAN_SHADOW_SCALE_SIZE;
  40. shadow_val = *(u8 *)kasan_mem_to_shadow(first_bad_addr);
  41. }
  42. return first_bad_addr;
  43. }
  44. static void print_error_description(struct kasan_access_info *info)
  45. {
  46. const char *bug_type = "unknown-crash";
  47. u8 *shadow_addr;
  48. info->first_bad_addr = find_first_bad_addr(info->access_addr,
  49. info->access_size);
  50. shadow_addr = (u8 *)kasan_mem_to_shadow(info->first_bad_addr);
  51. /*
  52. * If shadow byte value is in [0, KASAN_SHADOW_SCALE_SIZE) we can look
  53. * at the next shadow byte to determine the type of the bad access.
  54. */
  55. if (*shadow_addr > 0 && *shadow_addr <= KASAN_SHADOW_SCALE_SIZE - 1)
  56. shadow_addr++;
  57. switch (*shadow_addr) {
  58. case 0 ... KASAN_SHADOW_SCALE_SIZE - 1:
  59. /*
  60. * In theory it's still possible to see these shadow values
  61. * due to a data race in the kernel code.
  62. */
  63. bug_type = "out-of-bounds";
  64. break;
  65. case KASAN_PAGE_REDZONE:
  66. case KASAN_KMALLOC_REDZONE:
  67. bug_type = "slab-out-of-bounds";
  68. break;
  69. case KASAN_GLOBAL_REDZONE:
  70. bug_type = "global-out-of-bounds";
  71. break;
  72. case KASAN_STACK_LEFT:
  73. case KASAN_STACK_MID:
  74. case KASAN_STACK_RIGHT:
  75. case KASAN_STACK_PARTIAL:
  76. bug_type = "stack-out-of-bounds";
  77. break;
  78. case KASAN_FREE_PAGE:
  79. case KASAN_KMALLOC_FREE:
  80. bug_type = "use-after-free";
  81. break;
  82. }
  83. pr_err("BUG: KASAN: %s in %pS at addr %p\n",
  84. bug_type, (void *)info->ip,
  85. info->access_addr);
  86. pr_err("%s of size %zu by task %s/%d\n",
  87. info->is_write ? "Write" : "Read",
  88. info->access_size, current->comm, task_pid_nr(current));
  89. }
  90. static inline bool kernel_or_module_addr(const void *addr)
  91. {
  92. if (addr >= (void *)_stext && addr < (void *)_end)
  93. return true;
  94. if (is_module_address((unsigned long)addr))
  95. return true;
  96. return false;
  97. }
  98. static inline bool init_task_stack_addr(const void *addr)
  99. {
  100. return addr >= (void *)&init_thread_union.stack &&
  101. (addr <= (void *)&init_thread_union.stack +
  102. sizeof(init_thread_union.stack));
  103. }
  104. static void print_address_description(struct kasan_access_info *info)
  105. {
  106. const void *addr = info->access_addr;
  107. if ((addr >= (void *)PAGE_OFFSET) &&
  108. (addr < high_memory)) {
  109. struct page *page = virt_to_head_page(addr);
  110. if (PageSlab(page)) {
  111. void *object;
  112. struct kmem_cache *cache = page->slab_cache;
  113. void *last_object;
  114. object = virt_to_obj(cache, page_address(page), addr);
  115. last_object = page_address(page) +
  116. page->objects * cache->size;
  117. if (unlikely(object > last_object))
  118. object = last_object; /* we hit into padding */
  119. object_err(cache, page, object,
  120. "kasan: bad access detected");
  121. return;
  122. }
  123. dump_page(page, "kasan: bad access detected");
  124. }
  125. if (kernel_or_module_addr(addr)) {
  126. if (!init_task_stack_addr(addr))
  127. pr_err("Address belongs to variable %pS\n", addr);
  128. }
  129. dump_stack();
  130. }
  131. static bool row_is_guilty(const void *row, const void *guilty)
  132. {
  133. return (row <= guilty) && (guilty < row + SHADOW_BYTES_PER_ROW);
  134. }
  135. static int shadow_pointer_offset(const void *row, const void *shadow)
  136. {
  137. /* The length of ">ff00ff00ff00ff00: " is
  138. * 3 + (BITS_PER_LONG/8)*2 chars.
  139. */
  140. return 3 + (BITS_PER_LONG/8)*2 + (shadow - row)*2 +
  141. (shadow - row) / SHADOW_BYTES_PER_BLOCK + 1;
  142. }
  143. static void print_shadow_for_address(const void *addr)
  144. {
  145. int i;
  146. const void *shadow = kasan_mem_to_shadow(addr);
  147. const void *shadow_row;
  148. shadow_row = (void *)round_down((unsigned long)shadow,
  149. SHADOW_BYTES_PER_ROW)
  150. - SHADOW_ROWS_AROUND_ADDR * SHADOW_BYTES_PER_ROW;
  151. pr_err("Memory state around the buggy address:\n");
  152. for (i = -SHADOW_ROWS_AROUND_ADDR; i <= SHADOW_ROWS_AROUND_ADDR; i++) {
  153. const void *kaddr = kasan_shadow_to_mem(shadow_row);
  154. char buffer[4 + (BITS_PER_LONG/8)*2];
  155. char shadow_buf[SHADOW_BYTES_PER_ROW];
  156. snprintf(buffer, sizeof(buffer),
  157. (i == 0) ? ">%p: " : " %p: ", kaddr);
  158. /*
  159. * We should not pass a shadow pointer to generic
  160. * function, because generic functions may try to
  161. * access kasan mapping for the passed address.
  162. */
  163. memcpy(shadow_buf, shadow_row, SHADOW_BYTES_PER_ROW);
  164. print_hex_dump(KERN_ERR, buffer,
  165. DUMP_PREFIX_NONE, SHADOW_BYTES_PER_ROW, 1,
  166. shadow_buf, SHADOW_BYTES_PER_ROW, 0);
  167. if (row_is_guilty(shadow_row, shadow))
  168. pr_err("%*c\n",
  169. shadow_pointer_offset(shadow_row, shadow),
  170. '^');
  171. shadow_row += SHADOW_BYTES_PER_ROW;
  172. }
  173. }
  174. static DEFINE_SPINLOCK(report_lock);
  175. static void kasan_report_error(struct kasan_access_info *info)
  176. {
  177. unsigned long flags;
  178. const char *bug_type;
  179. /*
  180. * Make sure we don't end up in loop.
  181. */
  182. kasan_disable_current();
  183. spin_lock_irqsave(&report_lock, flags);
  184. pr_err("================================="
  185. "=================================\n");
  186. if (info->access_addr <
  187. kasan_shadow_to_mem((void *)KASAN_SHADOW_START)) {
  188. if ((unsigned long)info->access_addr < PAGE_SIZE)
  189. bug_type = "null-ptr-deref";
  190. else if ((unsigned long)info->access_addr < TASK_SIZE)
  191. bug_type = "user-memory-access";
  192. else
  193. bug_type = "wild-memory-access";
  194. pr_err("BUG: KASAN: %s on address %p\n",
  195. bug_type, info->access_addr);
  196. pr_err("%s of size %zu by task %s/%d\n",
  197. info->is_write ? "Write" : "Read",
  198. info->access_size, current->comm,
  199. task_pid_nr(current));
  200. dump_stack();
  201. } else {
  202. print_error_description(info);
  203. print_address_description(info);
  204. print_shadow_for_address(info->first_bad_addr);
  205. }
  206. pr_err("================================="
  207. "=================================\n");
  208. add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
  209. spin_unlock_irqrestore(&report_lock, flags);
  210. kasan_enable_current();
  211. }
  212. void kasan_report(unsigned long addr, size_t size,
  213. bool is_write, unsigned long ip)
  214. {
  215. struct kasan_access_info info;
  216. if (likely(!kasan_report_enabled()))
  217. return;
  218. disable_trace_on_warning();
  219. info.access_addr = (void *)addr;
  220. info.access_size = size;
  221. info.is_write = is_write;
  222. info.ip = ip;
  223. kasan_report_error(&info);
  224. }
  225. #define DEFINE_ASAN_REPORT_LOAD(size) \
  226. void __asan_report_load##size##_noabort(unsigned long addr) \
  227. { \
  228. kasan_report(addr, size, false, _RET_IP_); \
  229. } \
  230. EXPORT_SYMBOL(__asan_report_load##size##_noabort)
  231. #define DEFINE_ASAN_REPORT_STORE(size) \
  232. void __asan_report_store##size##_noabort(unsigned long addr) \
  233. { \
  234. kasan_report(addr, size, true, _RET_IP_); \
  235. } \
  236. EXPORT_SYMBOL(__asan_report_store##size##_noabort)
  237. DEFINE_ASAN_REPORT_LOAD(1);
  238. DEFINE_ASAN_REPORT_LOAD(2);
  239. DEFINE_ASAN_REPORT_LOAD(4);
  240. DEFINE_ASAN_REPORT_LOAD(8);
  241. DEFINE_ASAN_REPORT_LOAD(16);
  242. DEFINE_ASAN_REPORT_STORE(1);
  243. DEFINE_ASAN_REPORT_STORE(2);
  244. DEFINE_ASAN_REPORT_STORE(4);
  245. DEFINE_ASAN_REPORT_STORE(8);
  246. DEFINE_ASAN_REPORT_STORE(16);
  247. void __asan_report_load_n_noabort(unsigned long addr, size_t size)
  248. {
  249. kasan_report(addr, size, false, _RET_IP_);
  250. }
  251. EXPORT_SYMBOL(__asan_report_load_n_noabort);
  252. void __asan_report_store_n_noabort(unsigned long addr, size_t size)
  253. {
  254. kasan_report(addr, size, true, _RET_IP_);
  255. }
  256. EXPORT_SYMBOL(__asan_report_store_n_noabort);