report.c 9.2 KB

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