report.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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/bitops.h>
  16. #include <linux/ftrace.h>
  17. #include <linux/init.h>
  18. #include <linux/kernel.h>
  19. #include <linux/mm.h>
  20. #include <linux/printk.h>
  21. #include <linux/sched.h>
  22. #include <linux/slab.h>
  23. #include <linux/stackdepot.h>
  24. #include <linux/stacktrace.h>
  25. #include <linux/string.h>
  26. #include <linux/types.h>
  27. #include <linux/kasan.h>
  28. #include <linux/module.h>
  29. #include <asm/sections.h>
  30. #include "kasan.h"
  31. #include "../slab.h"
  32. /* Shadow layout customization. */
  33. #define SHADOW_BYTES_PER_BLOCK 1
  34. #define SHADOW_BLOCKS_PER_ROW 16
  35. #define SHADOW_BYTES_PER_ROW (SHADOW_BLOCKS_PER_ROW * SHADOW_BYTES_PER_BLOCK)
  36. #define SHADOW_ROWS_AROUND_ADDR 2
  37. static const void *find_first_bad_addr(const void *addr, size_t size)
  38. {
  39. u8 shadow_val = *(u8 *)kasan_mem_to_shadow(addr);
  40. const void *first_bad_addr = addr;
  41. while (!shadow_val && first_bad_addr < addr + size) {
  42. first_bad_addr += KASAN_SHADOW_SCALE_SIZE;
  43. shadow_val = *(u8 *)kasan_mem_to_shadow(first_bad_addr);
  44. }
  45. return first_bad_addr;
  46. }
  47. static bool addr_has_shadow(struct kasan_access_info *info)
  48. {
  49. return (info->access_addr >=
  50. kasan_shadow_to_mem((void *)KASAN_SHADOW_START));
  51. }
  52. static const char *get_shadow_bug_type(struct kasan_access_info *info)
  53. {
  54. const char *bug_type = "unknown-crash";
  55. u8 *shadow_addr;
  56. info->first_bad_addr = find_first_bad_addr(info->access_addr,
  57. info->access_size);
  58. shadow_addr = (u8 *)kasan_mem_to_shadow(info->first_bad_addr);
  59. /*
  60. * If shadow byte value is in [0, KASAN_SHADOW_SCALE_SIZE) we can look
  61. * at the next shadow byte to determine the type of the bad access.
  62. */
  63. if (*shadow_addr > 0 && *shadow_addr <= KASAN_SHADOW_SCALE_SIZE - 1)
  64. shadow_addr++;
  65. switch (*shadow_addr) {
  66. case 0 ... KASAN_SHADOW_SCALE_SIZE - 1:
  67. /*
  68. * In theory it's still possible to see these shadow values
  69. * due to a data race in the kernel code.
  70. */
  71. bug_type = "out-of-bounds";
  72. break;
  73. case KASAN_PAGE_REDZONE:
  74. case KASAN_KMALLOC_REDZONE:
  75. bug_type = "slab-out-of-bounds";
  76. break;
  77. case KASAN_GLOBAL_REDZONE:
  78. bug_type = "global-out-of-bounds";
  79. break;
  80. case KASAN_STACK_LEFT:
  81. case KASAN_STACK_MID:
  82. case KASAN_STACK_RIGHT:
  83. case KASAN_STACK_PARTIAL:
  84. bug_type = "stack-out-of-bounds";
  85. break;
  86. case KASAN_FREE_PAGE:
  87. case KASAN_KMALLOC_FREE:
  88. bug_type = "use-after-free";
  89. break;
  90. case KASAN_USE_AFTER_SCOPE:
  91. bug_type = "use-after-scope";
  92. break;
  93. case KASAN_ALLOCA_LEFT:
  94. case KASAN_ALLOCA_RIGHT:
  95. bug_type = "alloca-out-of-bounds";
  96. break;
  97. }
  98. return bug_type;
  99. }
  100. static const char *get_wild_bug_type(struct kasan_access_info *info)
  101. {
  102. const char *bug_type = "unknown-crash";
  103. if ((unsigned long)info->access_addr < PAGE_SIZE)
  104. bug_type = "null-ptr-deref";
  105. else if ((unsigned long)info->access_addr < TASK_SIZE)
  106. bug_type = "user-memory-access";
  107. else
  108. bug_type = "wild-memory-access";
  109. return bug_type;
  110. }
  111. static const char *get_bug_type(struct kasan_access_info *info)
  112. {
  113. if (addr_has_shadow(info))
  114. return get_shadow_bug_type(info);
  115. return get_wild_bug_type(info);
  116. }
  117. static void print_error_description(struct kasan_access_info *info)
  118. {
  119. const char *bug_type = get_bug_type(info);
  120. pr_err("BUG: KASAN: %s in %pS\n",
  121. bug_type, (void *)info->ip);
  122. pr_err("%s of size %zu at addr %px by task %s/%d\n",
  123. info->is_write ? "Write" : "Read", info->access_size,
  124. info->access_addr, current->comm, task_pid_nr(current));
  125. }
  126. static inline bool kernel_or_module_addr(const void *addr)
  127. {
  128. if (addr >= (void *)_stext && addr < (void *)_end)
  129. return true;
  130. if (is_module_address((unsigned long)addr))
  131. return true;
  132. return false;
  133. }
  134. static inline bool init_task_stack_addr(const void *addr)
  135. {
  136. return addr >= (void *)&init_thread_union.stack &&
  137. (addr <= (void *)&init_thread_union.stack +
  138. sizeof(init_thread_union.stack));
  139. }
  140. static DEFINE_SPINLOCK(report_lock);
  141. static void kasan_start_report(unsigned long *flags)
  142. {
  143. /*
  144. * Make sure we don't end up in loop.
  145. */
  146. kasan_disable_current();
  147. spin_lock_irqsave(&report_lock, *flags);
  148. pr_err("==================================================================\n");
  149. }
  150. static void kasan_end_report(unsigned long *flags)
  151. {
  152. pr_err("==================================================================\n");
  153. add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
  154. spin_unlock_irqrestore(&report_lock, *flags);
  155. #ifndef CONFIG_KASAN_PANIC_ON_WARN
  156. if (panic_on_warn)
  157. #endif
  158. panic("panic_on_warn set ...\n");
  159. kasan_enable_current();
  160. }
  161. static void print_track(struct kasan_track *track, const char *prefix)
  162. {
  163. pr_err("%s by task %u:\n", prefix, track->pid);
  164. if (track->stack) {
  165. struct stack_trace trace;
  166. depot_fetch_stack(track->stack, &trace);
  167. print_stack_trace(&trace, 0);
  168. } else {
  169. pr_err("(stack is not available)\n");
  170. }
  171. }
  172. static struct page *addr_to_page(const void *addr)
  173. {
  174. if ((addr >= (void *)PAGE_OFFSET) &&
  175. (addr < high_memory))
  176. return virt_to_head_page(addr);
  177. return NULL;
  178. }
  179. static void describe_object_addr(struct kmem_cache *cache, void *object,
  180. const void *addr)
  181. {
  182. unsigned long access_addr = (unsigned long)addr;
  183. unsigned long object_addr = (unsigned long)object;
  184. const char *rel_type;
  185. int rel_bytes;
  186. pr_err("The buggy address belongs to the object at %px\n"
  187. " which belongs to the cache %s of size %d\n",
  188. object, cache->name, cache->object_size);
  189. if (!addr)
  190. return;
  191. if (access_addr < object_addr) {
  192. rel_type = "to the left";
  193. rel_bytes = object_addr - access_addr;
  194. } else if (access_addr >= object_addr + cache->object_size) {
  195. rel_type = "to the right";
  196. rel_bytes = access_addr - (object_addr + cache->object_size);
  197. } else {
  198. rel_type = "inside";
  199. rel_bytes = access_addr - object_addr;
  200. }
  201. pr_err("The buggy address is located %d bytes %s of\n"
  202. " %d-byte region [%px, %px)\n",
  203. rel_bytes, rel_type, cache->object_size, (void *)object_addr,
  204. (void *)(object_addr + cache->object_size));
  205. }
  206. static void describe_object(struct kmem_cache *cache, void *object,
  207. const void *addr)
  208. {
  209. struct kasan_alloc_meta *alloc_info = get_alloc_info(cache, object);
  210. if (cache->flags & SLAB_KASAN) {
  211. print_track(&alloc_info->alloc_track, "Allocated");
  212. pr_err("\n");
  213. print_track(&alloc_info->free_track, "Freed");
  214. pr_err("\n");
  215. }
  216. describe_object_addr(cache, object, addr);
  217. }
  218. static void print_address_description(void *addr)
  219. {
  220. struct page *page = addr_to_page(addr);
  221. dump_stack();
  222. pr_err("\n");
  223. if (page && PageSlab(page)) {
  224. struct kmem_cache *cache = page->slab_cache;
  225. void *object = nearest_obj(cache, page, addr);
  226. describe_object(cache, object, addr);
  227. }
  228. if (kernel_or_module_addr(addr) && !init_task_stack_addr(addr)) {
  229. pr_err("The buggy address belongs to the variable:\n");
  230. pr_err(" %pS\n", addr);
  231. }
  232. if (page) {
  233. pr_err("The buggy address belongs to the page:\n");
  234. dump_page(page, "kasan: bad access detected");
  235. }
  236. }
  237. static bool row_is_guilty(const void *row, const void *guilty)
  238. {
  239. return (row <= guilty) && (guilty < row + SHADOW_BYTES_PER_ROW);
  240. }
  241. static int shadow_pointer_offset(const void *row, const void *shadow)
  242. {
  243. /* The length of ">ff00ff00ff00ff00: " is
  244. * 3 + (BITS_PER_LONG/8)*2 chars.
  245. */
  246. return 3 + (BITS_PER_LONG/8)*2 + (shadow - row)*2 +
  247. (shadow - row) / SHADOW_BYTES_PER_BLOCK + 1;
  248. }
  249. static void print_shadow_for_address(const void *addr)
  250. {
  251. int i;
  252. const void *shadow = kasan_mem_to_shadow(addr);
  253. const void *shadow_row;
  254. shadow_row = (void *)round_down((unsigned long)shadow,
  255. SHADOW_BYTES_PER_ROW)
  256. - SHADOW_ROWS_AROUND_ADDR * SHADOW_BYTES_PER_ROW;
  257. pr_err("Memory state around the buggy address:\n");
  258. for (i = -SHADOW_ROWS_AROUND_ADDR; i <= SHADOW_ROWS_AROUND_ADDR; i++) {
  259. const void *kaddr = kasan_shadow_to_mem(shadow_row);
  260. char buffer[4 + (BITS_PER_LONG/8)*2];
  261. char shadow_buf[SHADOW_BYTES_PER_ROW];
  262. snprintf(buffer, sizeof(buffer),
  263. (i == 0) ? ">%px: " : " %px: ", kaddr);
  264. /*
  265. * We should not pass a shadow pointer to generic
  266. * function, because generic functions may try to
  267. * access kasan mapping for the passed address.
  268. */
  269. memcpy(shadow_buf, shadow_row, SHADOW_BYTES_PER_ROW);
  270. print_hex_dump(KERN_ERR, buffer,
  271. DUMP_PREFIX_NONE, SHADOW_BYTES_PER_ROW, 1,
  272. shadow_buf, SHADOW_BYTES_PER_ROW, 0);
  273. if (row_is_guilty(shadow_row, shadow))
  274. pr_err("%*c\n",
  275. shadow_pointer_offset(shadow_row, shadow),
  276. '^');
  277. shadow_row += SHADOW_BYTES_PER_ROW;
  278. }
  279. }
  280. void kasan_report_double_free(struct kmem_cache *cache, void *object,
  281. void *ip)
  282. {
  283. unsigned long flags;
  284. kasan_start_report(&flags);
  285. pr_err("BUG: KASAN: double-free or invalid-free in %pS\n", ip);
  286. pr_err("\n");
  287. print_address_description(object);
  288. pr_err("\n");
  289. print_shadow_for_address(object);
  290. kasan_end_report(&flags);
  291. }
  292. static void kasan_report_error(struct kasan_access_info *info)
  293. {
  294. unsigned long flags;
  295. kasan_start_report(&flags);
  296. print_error_description(info);
  297. pr_err("\n");
  298. if (!addr_has_shadow(info)) {
  299. dump_stack();
  300. } else {
  301. print_address_description((void *)info->access_addr);
  302. pr_err("\n");
  303. print_shadow_for_address(info->first_bad_addr);
  304. }
  305. kasan_end_report(&flags);
  306. }
  307. static unsigned long kasan_flags;
  308. #define KASAN_BIT_REPORTED 0
  309. #define KASAN_BIT_MULTI_SHOT 1
  310. bool kasan_save_enable_multi_shot(void)
  311. {
  312. return test_and_set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
  313. }
  314. EXPORT_SYMBOL_GPL(kasan_save_enable_multi_shot);
  315. void kasan_restore_multi_shot(bool enabled)
  316. {
  317. if (!enabled)
  318. clear_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
  319. }
  320. EXPORT_SYMBOL_GPL(kasan_restore_multi_shot);
  321. static int __init kasan_set_multi_shot(char *str)
  322. {
  323. set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
  324. return 1;
  325. }
  326. __setup("kasan_multi_shot", kasan_set_multi_shot);
  327. static inline bool kasan_report_enabled(void)
  328. {
  329. if (current->kasan_depth)
  330. return false;
  331. if (test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags))
  332. return true;
  333. return !test_and_set_bit(KASAN_BIT_REPORTED, &kasan_flags);
  334. }
  335. void kasan_report(unsigned long addr, size_t size,
  336. bool is_write, unsigned long ip)
  337. {
  338. struct kasan_access_info info;
  339. if (likely(!kasan_report_enabled()))
  340. return;
  341. disable_trace_on_warning();
  342. info.access_addr = (void *)addr;
  343. info.first_bad_addr = (void *)addr;
  344. info.access_size = size;
  345. info.is_write = is_write;
  346. info.ip = ip;
  347. info.first_bad_addr = NULL;
  348. kasan_report_error(&info);
  349. }
  350. #define DEFINE_ASAN_REPORT_LOAD(size) \
  351. void __asan_report_load##size##_noabort(unsigned long addr) \
  352. { \
  353. kasan_report(addr, size, false, _RET_IP_); \
  354. } \
  355. EXPORT_SYMBOL(__asan_report_load##size##_noabort)
  356. #define DEFINE_ASAN_REPORT_STORE(size) \
  357. void __asan_report_store##size##_noabort(unsigned long addr) \
  358. { \
  359. kasan_report(addr, size, true, _RET_IP_); \
  360. } \
  361. EXPORT_SYMBOL(__asan_report_store##size##_noabort)
  362. DEFINE_ASAN_REPORT_LOAD(1);
  363. DEFINE_ASAN_REPORT_LOAD(2);
  364. DEFINE_ASAN_REPORT_LOAD(4);
  365. DEFINE_ASAN_REPORT_LOAD(8);
  366. DEFINE_ASAN_REPORT_LOAD(16);
  367. DEFINE_ASAN_REPORT_STORE(1);
  368. DEFINE_ASAN_REPORT_STORE(2);
  369. DEFINE_ASAN_REPORT_STORE(4);
  370. DEFINE_ASAN_REPORT_STORE(8);
  371. DEFINE_ASAN_REPORT_STORE(16);
  372. void __asan_report_load_n_noabort(unsigned long addr, size_t size)
  373. {
  374. kasan_report(addr, size, false, _RET_IP_);
  375. }
  376. EXPORT_SYMBOL(__asan_report_load_n_noabort);
  377. void __asan_report_store_n_noabort(unsigned long addr, size_t size)
  378. {
  379. kasan_report(addr, size, true, _RET_IP_);
  380. }
  381. EXPORT_SYMBOL(__asan_report_store_n_noabort);