kasan.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /*
  2. * This file contains shadow memory manipulation 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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #define DISABLE_BRANCH_PROFILING
  17. #include <linux/export.h>
  18. #include <linux/init.h>
  19. #include <linux/kernel.h>
  20. #include <linux/kmemleak.h>
  21. #include <linux/memblock.h>
  22. #include <linux/memory.h>
  23. #include <linux/mm.h>
  24. #include <linux/module.h>
  25. #include <linux/printk.h>
  26. #include <linux/sched.h>
  27. #include <linux/slab.h>
  28. #include <linux/stacktrace.h>
  29. #include <linux/string.h>
  30. #include <linux/types.h>
  31. #include <linux/vmalloc.h>
  32. #include <linux/kasan.h>
  33. #include "kasan.h"
  34. #include "../slab.h"
  35. /*
  36. * Poisons the shadow memory for 'size' bytes starting from 'addr'.
  37. * Memory addresses should be aligned to KASAN_SHADOW_SCALE_SIZE.
  38. */
  39. static void kasan_poison_shadow(const void *address, size_t size, u8 value)
  40. {
  41. void *shadow_start, *shadow_end;
  42. shadow_start = kasan_mem_to_shadow(address);
  43. shadow_end = kasan_mem_to_shadow(address + size);
  44. memset(shadow_start, value, shadow_end - shadow_start);
  45. }
  46. void kasan_unpoison_shadow(const void *address, size_t size)
  47. {
  48. kasan_poison_shadow(address, size, 0);
  49. if (size & KASAN_SHADOW_MASK) {
  50. u8 *shadow = (u8 *)kasan_mem_to_shadow(address + size);
  51. *shadow = size & KASAN_SHADOW_MASK;
  52. }
  53. }
  54. /*
  55. * All functions below always inlined so compiler could
  56. * perform better optimizations in each of __asan_loadX/__assn_storeX
  57. * depending on memory access size X.
  58. */
  59. static __always_inline bool memory_is_poisoned_1(unsigned long addr)
  60. {
  61. s8 shadow_value = *(s8 *)kasan_mem_to_shadow((void *)addr);
  62. if (unlikely(shadow_value)) {
  63. s8 last_accessible_byte = addr & KASAN_SHADOW_MASK;
  64. return unlikely(last_accessible_byte >= shadow_value);
  65. }
  66. return false;
  67. }
  68. static __always_inline bool memory_is_poisoned_2(unsigned long addr)
  69. {
  70. u16 *shadow_addr = (u16 *)kasan_mem_to_shadow((void *)addr);
  71. if (unlikely(*shadow_addr)) {
  72. if (memory_is_poisoned_1(addr + 1))
  73. return true;
  74. /*
  75. * If single shadow byte covers 2-byte access, we don't
  76. * need to do anything more. Otherwise, test the first
  77. * shadow byte.
  78. */
  79. if (likely(((addr + 1) & KASAN_SHADOW_MASK) != 0))
  80. return false;
  81. return unlikely(*(u8 *)shadow_addr);
  82. }
  83. return false;
  84. }
  85. static __always_inline bool memory_is_poisoned_4(unsigned long addr)
  86. {
  87. u16 *shadow_addr = (u16 *)kasan_mem_to_shadow((void *)addr);
  88. if (unlikely(*shadow_addr)) {
  89. if (memory_is_poisoned_1(addr + 3))
  90. return true;
  91. /*
  92. * If single shadow byte covers 4-byte access, we don't
  93. * need to do anything more. Otherwise, test the first
  94. * shadow byte.
  95. */
  96. if (likely(((addr + 3) & KASAN_SHADOW_MASK) >= 3))
  97. return false;
  98. return unlikely(*(u8 *)shadow_addr);
  99. }
  100. return false;
  101. }
  102. static __always_inline bool memory_is_poisoned_8(unsigned long addr)
  103. {
  104. u16 *shadow_addr = (u16 *)kasan_mem_to_shadow((void *)addr);
  105. if (unlikely(*shadow_addr)) {
  106. if (memory_is_poisoned_1(addr + 7))
  107. return true;
  108. /*
  109. * If single shadow byte covers 8-byte access, we don't
  110. * need to do anything more. Otherwise, test the first
  111. * shadow byte.
  112. */
  113. if (likely(IS_ALIGNED(addr, KASAN_SHADOW_SCALE_SIZE)))
  114. return false;
  115. return unlikely(*(u8 *)shadow_addr);
  116. }
  117. return false;
  118. }
  119. static __always_inline bool memory_is_poisoned_16(unsigned long addr)
  120. {
  121. u32 *shadow_addr = (u32 *)kasan_mem_to_shadow((void *)addr);
  122. if (unlikely(*shadow_addr)) {
  123. u16 shadow_first_bytes = *(u16 *)shadow_addr;
  124. if (unlikely(shadow_first_bytes))
  125. return true;
  126. /*
  127. * If two shadow bytes covers 16-byte access, we don't
  128. * need to do anything more. Otherwise, test the last
  129. * shadow byte.
  130. */
  131. if (likely(IS_ALIGNED(addr, KASAN_SHADOW_SCALE_SIZE)))
  132. return false;
  133. return memory_is_poisoned_1(addr + 15);
  134. }
  135. return false;
  136. }
  137. static __always_inline unsigned long bytes_is_zero(const u8 *start,
  138. size_t size)
  139. {
  140. while (size) {
  141. if (unlikely(*start))
  142. return (unsigned long)start;
  143. start++;
  144. size--;
  145. }
  146. return 0;
  147. }
  148. static __always_inline unsigned long memory_is_zero(const void *start,
  149. const void *end)
  150. {
  151. unsigned int words;
  152. unsigned long ret;
  153. unsigned int prefix = (unsigned long)start % 8;
  154. if (end - start <= 16)
  155. return bytes_is_zero(start, end - start);
  156. if (prefix) {
  157. prefix = 8 - prefix;
  158. ret = bytes_is_zero(start, prefix);
  159. if (unlikely(ret))
  160. return ret;
  161. start += prefix;
  162. }
  163. words = (end - start) / 8;
  164. while (words) {
  165. if (unlikely(*(u64 *)start))
  166. return bytes_is_zero(start, 8);
  167. start += 8;
  168. words--;
  169. }
  170. return bytes_is_zero(start, (end - start) % 8);
  171. }
  172. static __always_inline bool memory_is_poisoned_n(unsigned long addr,
  173. size_t size)
  174. {
  175. unsigned long ret;
  176. ret = memory_is_zero(kasan_mem_to_shadow((void *)addr),
  177. kasan_mem_to_shadow((void *)addr + size - 1) + 1);
  178. if (unlikely(ret)) {
  179. unsigned long last_byte = addr + size - 1;
  180. s8 *last_shadow = (s8 *)kasan_mem_to_shadow((void *)last_byte);
  181. if (unlikely(ret != (unsigned long)last_shadow ||
  182. ((long)(last_byte & KASAN_SHADOW_MASK) >= *last_shadow)))
  183. return true;
  184. }
  185. return false;
  186. }
  187. static __always_inline bool memory_is_poisoned(unsigned long addr, size_t size)
  188. {
  189. if (__builtin_constant_p(size)) {
  190. switch (size) {
  191. case 1:
  192. return memory_is_poisoned_1(addr);
  193. case 2:
  194. return memory_is_poisoned_2(addr);
  195. case 4:
  196. return memory_is_poisoned_4(addr);
  197. case 8:
  198. return memory_is_poisoned_8(addr);
  199. case 16:
  200. return memory_is_poisoned_16(addr);
  201. default:
  202. BUILD_BUG();
  203. }
  204. }
  205. return memory_is_poisoned_n(addr, size);
  206. }
  207. static __always_inline void check_memory_region(unsigned long addr,
  208. size_t size, bool write)
  209. {
  210. if (unlikely(size == 0))
  211. return;
  212. if (unlikely((void *)addr <
  213. kasan_shadow_to_mem((void *)KASAN_SHADOW_START))) {
  214. kasan_report(addr, size, write, _RET_IP_);
  215. return;
  216. }
  217. if (likely(!memory_is_poisoned(addr, size)))
  218. return;
  219. kasan_report(addr, size, write, _RET_IP_);
  220. }
  221. void __asan_loadN(unsigned long addr, size_t size);
  222. void __asan_storeN(unsigned long addr, size_t size);
  223. #undef memset
  224. void *memset(void *addr, int c, size_t len)
  225. {
  226. __asan_storeN((unsigned long)addr, len);
  227. return __memset(addr, c, len);
  228. }
  229. #undef memmove
  230. void *memmove(void *dest, const void *src, size_t len)
  231. {
  232. __asan_loadN((unsigned long)src, len);
  233. __asan_storeN((unsigned long)dest, len);
  234. return __memmove(dest, src, len);
  235. }
  236. #undef memcpy
  237. void *memcpy(void *dest, const void *src, size_t len)
  238. {
  239. __asan_loadN((unsigned long)src, len);
  240. __asan_storeN((unsigned long)dest, len);
  241. return __memcpy(dest, src, len);
  242. }
  243. void kasan_alloc_pages(struct page *page, unsigned int order)
  244. {
  245. if (likely(!PageHighMem(page)))
  246. kasan_unpoison_shadow(page_address(page), PAGE_SIZE << order);
  247. }
  248. void kasan_free_pages(struct page *page, unsigned int order)
  249. {
  250. if (likely(!PageHighMem(page)))
  251. kasan_poison_shadow(page_address(page),
  252. PAGE_SIZE << order,
  253. KASAN_FREE_PAGE);
  254. }
  255. void kasan_poison_slab(struct page *page)
  256. {
  257. kasan_poison_shadow(page_address(page),
  258. PAGE_SIZE << compound_order(page),
  259. KASAN_KMALLOC_REDZONE);
  260. }
  261. void kasan_unpoison_object_data(struct kmem_cache *cache, void *object)
  262. {
  263. kasan_unpoison_shadow(object, cache->object_size);
  264. }
  265. void kasan_poison_object_data(struct kmem_cache *cache, void *object)
  266. {
  267. kasan_poison_shadow(object,
  268. round_up(cache->object_size, KASAN_SHADOW_SCALE_SIZE),
  269. KASAN_KMALLOC_REDZONE);
  270. }
  271. void kasan_slab_alloc(struct kmem_cache *cache, void *object)
  272. {
  273. kasan_kmalloc(cache, object, cache->object_size);
  274. }
  275. void kasan_slab_free(struct kmem_cache *cache, void *object)
  276. {
  277. unsigned long size = cache->object_size;
  278. unsigned long rounded_up_size = round_up(size, KASAN_SHADOW_SCALE_SIZE);
  279. /* RCU slabs could be legally used after free within the RCU period */
  280. if (unlikely(cache->flags & SLAB_DESTROY_BY_RCU))
  281. return;
  282. kasan_poison_shadow(object, rounded_up_size, KASAN_KMALLOC_FREE);
  283. }
  284. void kasan_kmalloc(struct kmem_cache *cache, const void *object, size_t size)
  285. {
  286. unsigned long redzone_start;
  287. unsigned long redzone_end;
  288. if (unlikely(object == NULL))
  289. return;
  290. redzone_start = round_up((unsigned long)(object + size),
  291. KASAN_SHADOW_SCALE_SIZE);
  292. redzone_end = round_up((unsigned long)object + cache->object_size,
  293. KASAN_SHADOW_SCALE_SIZE);
  294. kasan_unpoison_shadow(object, size);
  295. kasan_poison_shadow((void *)redzone_start, redzone_end - redzone_start,
  296. KASAN_KMALLOC_REDZONE);
  297. }
  298. EXPORT_SYMBOL(kasan_kmalloc);
  299. void kasan_kmalloc_large(const void *ptr, size_t size)
  300. {
  301. struct page *page;
  302. unsigned long redzone_start;
  303. unsigned long redzone_end;
  304. if (unlikely(ptr == NULL))
  305. return;
  306. page = virt_to_page(ptr);
  307. redzone_start = round_up((unsigned long)(ptr + size),
  308. KASAN_SHADOW_SCALE_SIZE);
  309. redzone_end = (unsigned long)ptr + (PAGE_SIZE << compound_order(page));
  310. kasan_unpoison_shadow(ptr, size);
  311. kasan_poison_shadow((void *)redzone_start, redzone_end - redzone_start,
  312. KASAN_PAGE_REDZONE);
  313. }
  314. void kasan_krealloc(const void *object, size_t size)
  315. {
  316. struct page *page;
  317. if (unlikely(object == ZERO_SIZE_PTR))
  318. return;
  319. page = virt_to_head_page(object);
  320. if (unlikely(!PageSlab(page)))
  321. kasan_kmalloc_large(object, size);
  322. else
  323. kasan_kmalloc(page->slab_cache, object, size);
  324. }
  325. void kasan_kfree(void *ptr)
  326. {
  327. struct page *page;
  328. page = virt_to_head_page(ptr);
  329. if (unlikely(!PageSlab(page)))
  330. kasan_poison_shadow(ptr, PAGE_SIZE << compound_order(page),
  331. KASAN_FREE_PAGE);
  332. else
  333. kasan_slab_free(page->slab_cache, ptr);
  334. }
  335. void kasan_kfree_large(const void *ptr)
  336. {
  337. struct page *page = virt_to_page(ptr);
  338. kasan_poison_shadow(ptr, PAGE_SIZE << compound_order(page),
  339. KASAN_FREE_PAGE);
  340. }
  341. int kasan_module_alloc(void *addr, size_t size)
  342. {
  343. void *ret;
  344. size_t scaled_size;
  345. size_t shadow_size;
  346. unsigned long shadow_start;
  347. shadow_start = (unsigned long)kasan_mem_to_shadow(addr);
  348. scaled_size = (size + KASAN_SHADOW_MASK) >> KASAN_SHADOW_SCALE_SHIFT;
  349. shadow_size = round_up(scaled_size, PAGE_SIZE);
  350. if (WARN_ON(!PAGE_ALIGNED(shadow_start)))
  351. return -EINVAL;
  352. ret = __vmalloc_node_range(shadow_size, 1, shadow_start,
  353. shadow_start + shadow_size,
  354. GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
  355. PAGE_KERNEL, VM_NO_GUARD, NUMA_NO_NODE,
  356. __builtin_return_address(0));
  357. if (ret) {
  358. find_vm_area(addr)->flags |= VM_KASAN;
  359. kmemleak_ignore(ret);
  360. return 0;
  361. }
  362. return -ENOMEM;
  363. }
  364. void kasan_free_shadow(const struct vm_struct *vm)
  365. {
  366. if (vm->flags & VM_KASAN)
  367. vfree(kasan_mem_to_shadow(vm->addr));
  368. }
  369. static void register_global(struct kasan_global *global)
  370. {
  371. size_t aligned_size = round_up(global->size, KASAN_SHADOW_SCALE_SIZE);
  372. kasan_unpoison_shadow(global->beg, global->size);
  373. kasan_poison_shadow(global->beg + aligned_size,
  374. global->size_with_redzone - aligned_size,
  375. KASAN_GLOBAL_REDZONE);
  376. }
  377. void __asan_register_globals(struct kasan_global *globals, size_t size)
  378. {
  379. int i;
  380. for (i = 0; i < size; i++)
  381. register_global(&globals[i]);
  382. }
  383. EXPORT_SYMBOL(__asan_register_globals);
  384. void __asan_unregister_globals(struct kasan_global *globals, size_t size)
  385. {
  386. }
  387. EXPORT_SYMBOL(__asan_unregister_globals);
  388. #define DEFINE_ASAN_LOAD_STORE(size) \
  389. void __asan_load##size(unsigned long addr) \
  390. { \
  391. check_memory_region(addr, size, false); \
  392. } \
  393. EXPORT_SYMBOL(__asan_load##size); \
  394. __alias(__asan_load##size) \
  395. void __asan_load##size##_noabort(unsigned long); \
  396. EXPORT_SYMBOL(__asan_load##size##_noabort); \
  397. void __asan_store##size(unsigned long addr) \
  398. { \
  399. check_memory_region(addr, size, true); \
  400. } \
  401. EXPORT_SYMBOL(__asan_store##size); \
  402. __alias(__asan_store##size) \
  403. void __asan_store##size##_noabort(unsigned long); \
  404. EXPORT_SYMBOL(__asan_store##size##_noabort)
  405. DEFINE_ASAN_LOAD_STORE(1);
  406. DEFINE_ASAN_LOAD_STORE(2);
  407. DEFINE_ASAN_LOAD_STORE(4);
  408. DEFINE_ASAN_LOAD_STORE(8);
  409. DEFINE_ASAN_LOAD_STORE(16);
  410. void __asan_loadN(unsigned long addr, size_t size)
  411. {
  412. check_memory_region(addr, size, false);
  413. }
  414. EXPORT_SYMBOL(__asan_loadN);
  415. __alias(__asan_loadN)
  416. void __asan_loadN_noabort(unsigned long, size_t);
  417. EXPORT_SYMBOL(__asan_loadN_noabort);
  418. void __asan_storeN(unsigned long addr, size_t size)
  419. {
  420. check_memory_region(addr, size, true);
  421. }
  422. EXPORT_SYMBOL(__asan_storeN);
  423. __alias(__asan_storeN)
  424. void __asan_storeN_noabort(unsigned long, size_t);
  425. EXPORT_SYMBOL(__asan_storeN_noabort);
  426. /* to shut up compiler complaints */
  427. void __asan_handle_no_return(void) {}
  428. EXPORT_SYMBOL(__asan_handle_no_return);
  429. #ifdef CONFIG_MEMORY_HOTPLUG
  430. static int kasan_mem_notifier(struct notifier_block *nb,
  431. unsigned long action, void *data)
  432. {
  433. return (action == MEM_GOING_ONLINE) ? NOTIFY_BAD : NOTIFY_OK;
  434. }
  435. static int __init kasan_memhotplug_init(void)
  436. {
  437. pr_err("WARNING: KASAN doesn't support memory hot-add\n");
  438. pr_err("Memory hot-add will be disabled\n");
  439. hotplug_memory_notifier(kasan_mem_notifier, 0);
  440. return 0;
  441. }
  442. core_initcall(kasan_memhotplug_init);
  443. #endif