report.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * This file contains common KASAN error reporting code.
  4. *
  5. * Copyright (c) 2014 Samsung Electronics Co., Ltd.
  6. * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
  7. *
  8. * Some code borrowed from https://github.com/xairy/kasan-prototype by
  9. * Andrey Konovalov <andreyknvl@gmail.com>
  10. */
  11. #include <linux/bitops.h>
  12. #include <linux/ftrace.h>
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/mm.h>
  16. #include <linux/printk.h>
  17. #include <linux/sched.h>
  18. #include <linux/slab.h>
  19. #include <linux/stackdepot.h>
  20. #include <linux/stacktrace.h>
  21. #include <linux/string.h>
  22. #include <linux/types.h>
  23. #include <linux/kasan.h>
  24. #include <linux/module.h>
  25. #include <linux/sched/task_stack.h>
  26. #include <linux/uaccess.h>
  27. #include <trace/events/error_report.h>
  28. #include <asm/sections.h>
  29. #include <kunit/test.h>
  30. #include "kasan.h"
  31. #include "../slab.h"
  32. static unsigned long kasan_flags;
  33. #define KASAN_BIT_REPORTED 0
  34. #define KASAN_BIT_MULTI_SHOT 1
  35. bool kasan_save_enable_multi_shot(void)
  36. {
  37. return test_and_set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
  38. }
  39. EXPORT_SYMBOL_GPL(kasan_save_enable_multi_shot);
  40. void kasan_restore_multi_shot(bool enabled)
  41. {
  42. if (!enabled)
  43. clear_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
  44. }
  45. EXPORT_SYMBOL_GPL(kasan_restore_multi_shot);
  46. static int __init kasan_set_multi_shot(char *str)
  47. {
  48. set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
  49. return 1;
  50. }
  51. __setup("kasan_multi_shot", kasan_set_multi_shot);
  52. static void print_error_description(struct kasan_access_info *info)
  53. {
  54. pr_err("BUG: KASAN: %s in %pS\n",
  55. kasan_get_bug_type(info), (void *)info->ip);
  56. if (info->access_size)
  57. pr_err("%s of size %zu at addr %px by task %s/%d\n",
  58. info->is_write ? "Write" : "Read", info->access_size,
  59. info->access_addr, current->comm, task_pid_nr(current));
  60. else
  61. pr_err("%s at addr %px by task %s/%d\n",
  62. info->is_write ? "Write" : "Read",
  63. info->access_addr, current->comm, task_pid_nr(current));
  64. }
  65. static DEFINE_SPINLOCK(report_lock);
  66. static void start_report(unsigned long *flags)
  67. {
  68. /*
  69. * Make sure we don't end up in loop.
  70. */
  71. kasan_disable_current();
  72. spin_lock_irqsave(&report_lock, *flags);
  73. pr_err("==================================================================\n");
  74. }
  75. static void end_report(unsigned long *flags, unsigned long addr)
  76. {
  77. if (!kasan_async_mode_enabled())
  78. trace_error_report_end(ERROR_DETECTOR_KASAN, addr);
  79. pr_err("==================================================================\n");
  80. add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
  81. spin_unlock_irqrestore(&report_lock, *flags);
  82. if (panic_on_warn && !test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags)) {
  83. /*
  84. * This thread may hit another WARN() in the panic path.
  85. * Resetting this prevents additional WARN() from panicking the
  86. * system on this thread. Other threads are blocked by the
  87. * panic_mutex in panic().
  88. */
  89. panic_on_warn = 0;
  90. panic("panic_on_warn set ...\n");
  91. }
  92. #ifdef CONFIG_KASAN_HW_TAGS
  93. if (kasan_flag_panic)
  94. panic("kasan.fault=panic set ...\n");
  95. #endif
  96. kasan_enable_current();
  97. }
  98. static void print_stack(depot_stack_handle_t stack)
  99. {
  100. unsigned long *entries;
  101. unsigned int nr_entries;
  102. nr_entries = stack_depot_fetch(stack, &entries);
  103. stack_trace_print(entries, nr_entries, 0);
  104. }
  105. static void print_track(struct kasan_track *track, const char *prefix)
  106. {
  107. pr_err("%s by task %u:\n", prefix, track->pid);
  108. if (track->stack) {
  109. print_stack(track->stack);
  110. } else {
  111. pr_err("(stack is not available)\n");
  112. }
  113. }
  114. struct page *kasan_addr_to_page(const void *addr)
  115. {
  116. if ((addr >= (void *)PAGE_OFFSET) &&
  117. (addr < high_memory))
  118. return virt_to_head_page(addr);
  119. return NULL;
  120. }
  121. static void describe_object_addr(struct kmem_cache *cache, void *object,
  122. const void *addr)
  123. {
  124. unsigned long access_addr = (unsigned long)addr;
  125. unsigned long object_addr = (unsigned long)object;
  126. const char *rel_type;
  127. int rel_bytes;
  128. pr_err("The buggy address belongs to the object at %px\n"
  129. " which belongs to the cache %s of size %d\n",
  130. object, cache->name, cache->object_size);
  131. if (!addr)
  132. return;
  133. if (access_addr < object_addr) {
  134. rel_type = "to the left";
  135. rel_bytes = object_addr - access_addr;
  136. } else if (access_addr >= object_addr + cache->object_size) {
  137. rel_type = "to the right";
  138. rel_bytes = access_addr - (object_addr + cache->object_size);
  139. } else {
  140. rel_type = "inside";
  141. rel_bytes = access_addr - object_addr;
  142. }
  143. pr_err("The buggy address is located %d bytes %s of\n"
  144. " %d-byte region [%px, %px)\n",
  145. rel_bytes, rel_type, cache->object_size, (void *)object_addr,
  146. (void *)(object_addr + cache->object_size));
  147. }
  148. static void describe_object_stacks(struct kmem_cache *cache, void *object,
  149. const void *addr, u8 tag)
  150. {
  151. struct kasan_alloc_meta *alloc_meta;
  152. struct kasan_track *free_track;
  153. alloc_meta = kasan_get_alloc_meta(cache, object);
  154. if (alloc_meta) {
  155. print_track(&alloc_meta->alloc_track, "Allocated");
  156. pr_err("\n");
  157. }
  158. free_track = kasan_get_free_track(cache, object, tag);
  159. if (free_track) {
  160. print_track(free_track, "Freed");
  161. pr_err("\n");
  162. }
  163. #ifdef CONFIG_KASAN_GENERIC
  164. if (!alloc_meta)
  165. return;
  166. if (alloc_meta->aux_stack[0]) {
  167. pr_err("Last potentially related work creation:\n");
  168. print_stack(alloc_meta->aux_stack[0]);
  169. pr_err("\n");
  170. }
  171. if (alloc_meta->aux_stack[1]) {
  172. pr_err("Second to last potentially related work creation:\n");
  173. print_stack(alloc_meta->aux_stack[1]);
  174. pr_err("\n");
  175. }
  176. #endif
  177. }
  178. static void describe_object(struct kmem_cache *cache, void *object,
  179. const void *addr, u8 tag)
  180. {
  181. if (kasan_stack_collection_enabled())
  182. describe_object_stacks(cache, object, addr, tag);
  183. describe_object_addr(cache, object, addr);
  184. }
  185. static inline bool kernel_or_module_addr(const void *addr)
  186. {
  187. if (addr >= (void *)_stext && addr < (void *)_end)
  188. return true;
  189. if (is_module_address((unsigned long)addr))
  190. return true;
  191. return false;
  192. }
  193. static inline bool init_task_stack_addr(const void *addr)
  194. {
  195. return addr >= (void *)&init_thread_union.stack &&
  196. (addr <= (void *)&init_thread_union.stack +
  197. sizeof(init_thread_union.stack));
  198. }
  199. static void print_address_description(void *addr, u8 tag)
  200. {
  201. struct page *page = kasan_addr_to_page(addr);
  202. dump_stack_lvl(KERN_ERR);
  203. pr_err("\n");
  204. if (page && PageSlab(page)) {
  205. struct kmem_cache *cache = page->slab_cache;
  206. void *object = nearest_obj(cache, page, addr);
  207. describe_object(cache, object, addr, tag);
  208. }
  209. if (kernel_or_module_addr(addr) && !init_task_stack_addr(addr)) {
  210. pr_err("The buggy address belongs to the variable:\n");
  211. pr_err(" %pS\n", addr);
  212. }
  213. if (page) {
  214. pr_err("The buggy address belongs to the page:\n");
  215. dump_page(page, "kasan: bad access detected");
  216. }
  217. kasan_print_address_stack_frame(addr);
  218. }
  219. static bool meta_row_is_guilty(const void *row, const void *addr)
  220. {
  221. return (row <= addr) && (addr < row + META_MEM_BYTES_PER_ROW);
  222. }
  223. static int meta_pointer_offset(const void *row, const void *addr)
  224. {
  225. /*
  226. * Memory state around the buggy address:
  227. * ff00ff00ff00ff00: 00 00 00 05 fe fe fe fe fe fe fe fe fe fe fe fe
  228. * ...
  229. *
  230. * The length of ">ff00ff00ff00ff00: " is
  231. * 3 + (BITS_PER_LONG / 8) * 2 chars.
  232. * The length of each granule metadata is 2 bytes
  233. * plus 1 byte for space.
  234. */
  235. return 3 + (BITS_PER_LONG / 8) * 2 +
  236. (addr - row) / KASAN_GRANULE_SIZE * 3 + 1;
  237. }
  238. static void print_memory_metadata(const void *addr)
  239. {
  240. int i;
  241. void *row;
  242. row = (void *)round_down((unsigned long)addr, META_MEM_BYTES_PER_ROW)
  243. - META_ROWS_AROUND_ADDR * META_MEM_BYTES_PER_ROW;
  244. pr_err("Memory state around the buggy address:\n");
  245. for (i = -META_ROWS_AROUND_ADDR; i <= META_ROWS_AROUND_ADDR; i++) {
  246. char buffer[4 + (BITS_PER_LONG / 8) * 2];
  247. char metadata[META_BYTES_PER_ROW];
  248. snprintf(buffer, sizeof(buffer),
  249. (i == 0) ? ">%px: " : " %px: ", row);
  250. /*
  251. * We should not pass a shadow pointer to generic
  252. * function, because generic functions may try to
  253. * access kasan mapping for the passed address.
  254. */
  255. kasan_metadata_fetch_row(&metadata[0], row);
  256. print_hex_dump(KERN_ERR, buffer,
  257. DUMP_PREFIX_NONE, META_BYTES_PER_ROW, 1,
  258. metadata, META_BYTES_PER_ROW, 0);
  259. if (meta_row_is_guilty(row, addr))
  260. pr_err("%*c\n", meta_pointer_offset(row, addr), '^');
  261. row += META_MEM_BYTES_PER_ROW;
  262. }
  263. }
  264. static bool report_enabled(void)
  265. {
  266. #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
  267. if (current->kasan_depth)
  268. return false;
  269. #endif
  270. if (test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags))
  271. return true;
  272. return !test_and_set_bit(KASAN_BIT_REPORTED, &kasan_flags);
  273. }
  274. #if IS_ENABLED(CONFIG_KUNIT)
  275. static void kasan_update_kunit_status(struct kunit *cur_test)
  276. {
  277. struct kunit_resource *resource;
  278. struct kunit_kasan_expectation *kasan_data;
  279. resource = kunit_find_named_resource(cur_test, "kasan_data");
  280. if (!resource) {
  281. kunit_set_failure(cur_test);
  282. return;
  283. }
  284. kasan_data = (struct kunit_kasan_expectation *)resource->data;
  285. WRITE_ONCE(kasan_data->report_found, true);
  286. kunit_put_resource(resource);
  287. }
  288. #endif /* IS_ENABLED(CONFIG_KUNIT) */
  289. void kasan_report_invalid_free(void *object, unsigned long ip)
  290. {
  291. unsigned long flags;
  292. u8 tag = get_tag(object);
  293. object = kasan_reset_tag(object);
  294. #if IS_ENABLED(CONFIG_KUNIT)
  295. if (current->kunit_test)
  296. kasan_update_kunit_status(current->kunit_test);
  297. #endif /* IS_ENABLED(CONFIG_KUNIT) */
  298. start_report(&flags);
  299. pr_err("BUG: KASAN: double-free or invalid-free in %pS\n", (void *)ip);
  300. kasan_print_tags(tag, object);
  301. pr_err("\n");
  302. print_address_description(object, tag);
  303. pr_err("\n");
  304. print_memory_metadata(object);
  305. end_report(&flags, (unsigned long)object);
  306. }
  307. #ifdef CONFIG_KASAN_HW_TAGS
  308. void kasan_report_async(void)
  309. {
  310. unsigned long flags;
  311. #if IS_ENABLED(CONFIG_KUNIT)
  312. if (current->kunit_test)
  313. kasan_update_kunit_status(current->kunit_test);
  314. #endif /* IS_ENABLED(CONFIG_KUNIT) */
  315. start_report(&flags);
  316. pr_err("BUG: KASAN: invalid-access\n");
  317. pr_err("Asynchronous mode enabled: no access details available\n");
  318. pr_err("\n");
  319. dump_stack_lvl(KERN_ERR);
  320. end_report(&flags, 0);
  321. }
  322. #endif /* CONFIG_KASAN_HW_TAGS */
  323. static void __kasan_report(unsigned long addr, size_t size, bool is_write,
  324. unsigned long ip)
  325. {
  326. struct kasan_access_info info;
  327. void *tagged_addr;
  328. void *untagged_addr;
  329. unsigned long flags;
  330. #if IS_ENABLED(CONFIG_KUNIT)
  331. if (current->kunit_test)
  332. kasan_update_kunit_status(current->kunit_test);
  333. #endif /* IS_ENABLED(CONFIG_KUNIT) */
  334. disable_trace_on_warning();
  335. tagged_addr = (void *)addr;
  336. untagged_addr = kasan_reset_tag(tagged_addr);
  337. info.access_addr = tagged_addr;
  338. if (addr_has_metadata(untagged_addr))
  339. info.first_bad_addr =
  340. kasan_find_first_bad_addr(tagged_addr, size);
  341. else
  342. info.first_bad_addr = untagged_addr;
  343. info.access_size = size;
  344. info.is_write = is_write;
  345. info.ip = ip;
  346. start_report(&flags);
  347. print_error_description(&info);
  348. if (addr_has_metadata(untagged_addr))
  349. kasan_print_tags(get_tag(tagged_addr), info.first_bad_addr);
  350. pr_err("\n");
  351. if (addr_has_metadata(untagged_addr)) {
  352. print_address_description(untagged_addr, get_tag(tagged_addr));
  353. pr_err("\n");
  354. print_memory_metadata(info.first_bad_addr);
  355. } else {
  356. dump_stack_lvl(KERN_ERR);
  357. }
  358. end_report(&flags, addr);
  359. }
  360. bool kasan_report(unsigned long addr, size_t size, bool is_write,
  361. unsigned long ip)
  362. {
  363. unsigned long flags = user_access_save();
  364. bool ret = false;
  365. if (likely(report_enabled())) {
  366. __kasan_report(addr, size, is_write, ip);
  367. ret = true;
  368. }
  369. user_access_restore(flags);
  370. return ret;
  371. }
  372. #ifdef CONFIG_KASAN_INLINE
  373. /*
  374. * With CONFIG_KASAN_INLINE, accesses to bogus pointers (outside the high
  375. * canonical half of the address space) cause out-of-bounds shadow memory reads
  376. * before the actual access. For addresses in the low canonical half of the
  377. * address space, as well as most non-canonical addresses, that out-of-bounds
  378. * shadow memory access lands in the non-canonical part of the address space.
  379. * Help the user figure out what the original bogus pointer was.
  380. */
  381. void kasan_non_canonical_hook(unsigned long addr)
  382. {
  383. unsigned long orig_addr;
  384. const char *bug_type;
  385. if (addr < KASAN_SHADOW_OFFSET)
  386. return;
  387. orig_addr = (addr - KASAN_SHADOW_OFFSET) << KASAN_SHADOW_SCALE_SHIFT;
  388. /*
  389. * For faults near the shadow address for NULL, we can be fairly certain
  390. * that this is a KASAN shadow memory access.
  391. * For faults that correspond to shadow for low canonical addresses, we
  392. * can still be pretty sure - that shadow region is a fairly narrow
  393. * chunk of the non-canonical address space.
  394. * But faults that look like shadow for non-canonical addresses are a
  395. * really large chunk of the address space. In that case, we still
  396. * print the decoded address, but make it clear that this is not
  397. * necessarily what's actually going on.
  398. */
  399. if (orig_addr < PAGE_SIZE)
  400. bug_type = "null-ptr-deref";
  401. else if (orig_addr < TASK_SIZE)
  402. bug_type = "probably user-memory-access";
  403. else
  404. bug_type = "maybe wild-memory-access";
  405. pr_alert("KASAN: %s in range [0x%016lx-0x%016lx]\n", bug_type,
  406. orig_addr, orig_addr + KASAN_GRANULE_SIZE - 1);
  407. }
  408. #endif