test_kasan.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /*
  2. *
  3. * Copyright (c) 2014 Samsung Electronics Co., Ltd.
  4. * Author: Andrey Ryabinin <a.ryabinin@samsung.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. */
  11. #define pr_fmt(fmt) "kasan test: %s " fmt, __func__
  12. #include <linux/delay.h>
  13. #include <linux/kernel.h>
  14. #include <linux/mman.h>
  15. #include <linux/mm.h>
  16. #include <linux/printk.h>
  17. #include <linux/slab.h>
  18. #include <linux/string.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/module.h>
  21. #include <linux/kasan.h>
  22. /*
  23. * Note: test functions are marked noinline so that their names appear in
  24. * reports.
  25. */
  26. static noinline void __init kmalloc_oob_right(void)
  27. {
  28. char *ptr;
  29. size_t size = 123;
  30. pr_info("out-of-bounds to right\n");
  31. ptr = kmalloc(size, GFP_KERNEL);
  32. if (!ptr) {
  33. pr_err("Allocation failed\n");
  34. return;
  35. }
  36. ptr[size] = 'x';
  37. kfree(ptr);
  38. }
  39. static noinline void __init kmalloc_oob_left(void)
  40. {
  41. char *ptr;
  42. size_t size = 15;
  43. pr_info("out-of-bounds to left\n");
  44. ptr = kmalloc(size, GFP_KERNEL);
  45. if (!ptr) {
  46. pr_err("Allocation failed\n");
  47. return;
  48. }
  49. *ptr = *(ptr - 1);
  50. kfree(ptr);
  51. }
  52. static noinline void __init kmalloc_node_oob_right(void)
  53. {
  54. char *ptr;
  55. size_t size = 4096;
  56. pr_info("kmalloc_node(): out-of-bounds to right\n");
  57. ptr = kmalloc_node(size, GFP_KERNEL, 0);
  58. if (!ptr) {
  59. pr_err("Allocation failed\n");
  60. return;
  61. }
  62. ptr[size] = 0;
  63. kfree(ptr);
  64. }
  65. #ifdef CONFIG_SLUB
  66. static noinline void __init kmalloc_pagealloc_oob_right(void)
  67. {
  68. char *ptr;
  69. size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
  70. /* Allocate a chunk that does not fit into a SLUB cache to trigger
  71. * the page allocator fallback.
  72. */
  73. pr_info("kmalloc pagealloc allocation: out-of-bounds to right\n");
  74. ptr = kmalloc(size, GFP_KERNEL);
  75. if (!ptr) {
  76. pr_err("Allocation failed\n");
  77. return;
  78. }
  79. ptr[size] = 0;
  80. kfree(ptr);
  81. }
  82. static noinline void __init kmalloc_pagealloc_uaf(void)
  83. {
  84. char *ptr;
  85. size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
  86. pr_info("kmalloc pagealloc allocation: use-after-free\n");
  87. ptr = kmalloc(size, GFP_KERNEL);
  88. if (!ptr) {
  89. pr_err("Allocation failed\n");
  90. return;
  91. }
  92. kfree(ptr);
  93. ptr[0] = 0;
  94. }
  95. static noinline void __init kmalloc_pagealloc_invalid_free(void)
  96. {
  97. char *ptr;
  98. size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
  99. pr_info("kmalloc pagealloc allocation: invalid-free\n");
  100. ptr = kmalloc(size, GFP_KERNEL);
  101. if (!ptr) {
  102. pr_err("Allocation failed\n");
  103. return;
  104. }
  105. kfree(ptr + 1);
  106. }
  107. #endif
  108. static noinline void __init kmalloc_large_oob_right(void)
  109. {
  110. char *ptr;
  111. size_t size = KMALLOC_MAX_CACHE_SIZE - 256;
  112. /* Allocate a chunk that is large enough, but still fits into a slab
  113. * and does not trigger the page allocator fallback in SLUB.
  114. */
  115. pr_info("kmalloc large allocation: out-of-bounds to right\n");
  116. ptr = kmalloc(size, GFP_KERNEL);
  117. if (!ptr) {
  118. pr_err("Allocation failed\n");
  119. return;
  120. }
  121. ptr[size] = 0;
  122. kfree(ptr);
  123. }
  124. static noinline void __init kmalloc_oob_krealloc_more(void)
  125. {
  126. char *ptr1, *ptr2;
  127. size_t size1 = 17;
  128. size_t size2 = 19;
  129. pr_info("out-of-bounds after krealloc more\n");
  130. ptr1 = kmalloc(size1, GFP_KERNEL);
  131. ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
  132. if (!ptr1 || !ptr2) {
  133. pr_err("Allocation failed\n");
  134. kfree(ptr1);
  135. kfree(ptr2);
  136. return;
  137. }
  138. ptr2[size2] = 'x';
  139. kfree(ptr2);
  140. }
  141. static noinline void __init kmalloc_oob_krealloc_less(void)
  142. {
  143. char *ptr1, *ptr2;
  144. size_t size1 = 17;
  145. size_t size2 = 15;
  146. pr_info("out-of-bounds after krealloc less\n");
  147. ptr1 = kmalloc(size1, GFP_KERNEL);
  148. ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
  149. if (!ptr1 || !ptr2) {
  150. pr_err("Allocation failed\n");
  151. kfree(ptr1);
  152. return;
  153. }
  154. ptr2[size2] = 'x';
  155. kfree(ptr2);
  156. }
  157. static noinline void __init kmalloc_oob_16(void)
  158. {
  159. struct {
  160. u64 words[2];
  161. } *ptr1, *ptr2;
  162. pr_info("kmalloc out-of-bounds for 16-bytes access\n");
  163. ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
  164. ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
  165. if (!ptr1 || !ptr2) {
  166. pr_err("Allocation failed\n");
  167. kfree(ptr1);
  168. kfree(ptr2);
  169. return;
  170. }
  171. *ptr1 = *ptr2;
  172. kfree(ptr1);
  173. kfree(ptr2);
  174. }
  175. static noinline void __init kmalloc_oob_memset_2(void)
  176. {
  177. char *ptr;
  178. size_t size = 8;
  179. pr_info("out-of-bounds in memset2\n");
  180. ptr = kmalloc(size, GFP_KERNEL);
  181. if (!ptr) {
  182. pr_err("Allocation failed\n");
  183. return;
  184. }
  185. memset(ptr+7, 0, 2);
  186. kfree(ptr);
  187. }
  188. static noinline void __init kmalloc_oob_memset_4(void)
  189. {
  190. char *ptr;
  191. size_t size = 8;
  192. pr_info("out-of-bounds in memset4\n");
  193. ptr = kmalloc(size, GFP_KERNEL);
  194. if (!ptr) {
  195. pr_err("Allocation failed\n");
  196. return;
  197. }
  198. memset(ptr+5, 0, 4);
  199. kfree(ptr);
  200. }
  201. static noinline void __init kmalloc_oob_memset_8(void)
  202. {
  203. char *ptr;
  204. size_t size = 8;
  205. pr_info("out-of-bounds in memset8\n");
  206. ptr = kmalloc(size, GFP_KERNEL);
  207. if (!ptr) {
  208. pr_err("Allocation failed\n");
  209. return;
  210. }
  211. memset(ptr+1, 0, 8);
  212. kfree(ptr);
  213. }
  214. static noinline void __init kmalloc_oob_memset_16(void)
  215. {
  216. char *ptr;
  217. size_t size = 16;
  218. pr_info("out-of-bounds in memset16\n");
  219. ptr = kmalloc(size, GFP_KERNEL);
  220. if (!ptr) {
  221. pr_err("Allocation failed\n");
  222. return;
  223. }
  224. memset(ptr+1, 0, 16);
  225. kfree(ptr);
  226. }
  227. static noinline void __init kmalloc_oob_in_memset(void)
  228. {
  229. char *ptr;
  230. size_t size = 666;
  231. pr_info("out-of-bounds in memset\n");
  232. ptr = kmalloc(size, GFP_KERNEL);
  233. if (!ptr) {
  234. pr_err("Allocation failed\n");
  235. return;
  236. }
  237. memset(ptr, 0, size+5);
  238. kfree(ptr);
  239. }
  240. static noinline void __init kmalloc_uaf(void)
  241. {
  242. char *ptr;
  243. size_t size = 10;
  244. pr_info("use-after-free\n");
  245. ptr = kmalloc(size, GFP_KERNEL);
  246. if (!ptr) {
  247. pr_err("Allocation failed\n");
  248. return;
  249. }
  250. kfree(ptr);
  251. *(ptr + 8) = 'x';
  252. }
  253. static noinline void __init kmalloc_uaf_memset(void)
  254. {
  255. char *ptr;
  256. size_t size = 33;
  257. pr_info("use-after-free in memset\n");
  258. ptr = kmalloc(size, GFP_KERNEL);
  259. if (!ptr) {
  260. pr_err("Allocation failed\n");
  261. return;
  262. }
  263. kfree(ptr);
  264. memset(ptr, 0, size);
  265. }
  266. static noinline void __init kmalloc_uaf2(void)
  267. {
  268. char *ptr1, *ptr2;
  269. size_t size = 43;
  270. pr_info("use-after-free after another kmalloc\n");
  271. ptr1 = kmalloc(size, GFP_KERNEL);
  272. if (!ptr1) {
  273. pr_err("Allocation failed\n");
  274. return;
  275. }
  276. kfree(ptr1);
  277. ptr2 = kmalloc(size, GFP_KERNEL);
  278. if (!ptr2) {
  279. pr_err("Allocation failed\n");
  280. return;
  281. }
  282. ptr1[40] = 'x';
  283. if (ptr1 == ptr2)
  284. pr_err("Could not detect use-after-free: ptr1 == ptr2\n");
  285. kfree(ptr2);
  286. }
  287. static noinline void __init kmem_cache_oob(void)
  288. {
  289. char *p;
  290. size_t size = 200;
  291. struct kmem_cache *cache = kmem_cache_create("test_cache",
  292. size, 0,
  293. 0, NULL);
  294. if (!cache) {
  295. pr_err("Cache allocation failed\n");
  296. return;
  297. }
  298. pr_info("out-of-bounds in kmem_cache_alloc\n");
  299. p = kmem_cache_alloc(cache, GFP_KERNEL);
  300. if (!p) {
  301. pr_err("Allocation failed\n");
  302. kmem_cache_destroy(cache);
  303. return;
  304. }
  305. *p = p[size];
  306. kmem_cache_free(cache, p);
  307. kmem_cache_destroy(cache);
  308. }
  309. static noinline void __init memcg_accounted_kmem_cache(void)
  310. {
  311. int i;
  312. char *p;
  313. size_t size = 200;
  314. struct kmem_cache *cache;
  315. cache = kmem_cache_create("test_cache", size, 0, SLAB_ACCOUNT, NULL);
  316. if (!cache) {
  317. pr_err("Cache allocation failed\n");
  318. return;
  319. }
  320. pr_info("allocate memcg accounted object\n");
  321. /*
  322. * Several allocations with a delay to allow for lazy per memcg kmem
  323. * cache creation.
  324. */
  325. for (i = 0; i < 5; i++) {
  326. p = kmem_cache_alloc(cache, GFP_KERNEL);
  327. if (!p)
  328. goto free_cache;
  329. kmem_cache_free(cache, p);
  330. msleep(100);
  331. }
  332. free_cache:
  333. kmem_cache_destroy(cache);
  334. }
  335. static char global_array[10];
  336. static noinline void __init kasan_global_oob(void)
  337. {
  338. volatile int i = 3;
  339. char *p = &global_array[ARRAY_SIZE(global_array) + i];
  340. pr_info("out-of-bounds global variable\n");
  341. *(volatile char *)p;
  342. }
  343. static noinline void __init kasan_stack_oob(void)
  344. {
  345. char stack_array[10];
  346. volatile int i = 0;
  347. char *p = &stack_array[ARRAY_SIZE(stack_array) + i];
  348. pr_info("out-of-bounds on stack\n");
  349. *(volatile char *)p;
  350. }
  351. static noinline void __init ksize_unpoisons_memory(void)
  352. {
  353. char *ptr;
  354. size_t size = 123, real_size;
  355. pr_info("ksize() unpoisons the whole allocated chunk\n");
  356. ptr = kmalloc(size, GFP_KERNEL);
  357. if (!ptr) {
  358. pr_err("Allocation failed\n");
  359. return;
  360. }
  361. real_size = ksize(ptr);
  362. /* This access doesn't trigger an error. */
  363. ptr[size] = 'x';
  364. /* This one does. */
  365. ptr[real_size] = 'y';
  366. kfree(ptr);
  367. }
  368. static noinline void __init copy_user_test(void)
  369. {
  370. char *kmem;
  371. char __user *usermem;
  372. size_t size = 10;
  373. int unused;
  374. kmem = kmalloc(size, GFP_KERNEL);
  375. if (!kmem)
  376. return;
  377. usermem = (char __user *)vm_mmap(NULL, 0, PAGE_SIZE,
  378. PROT_READ | PROT_WRITE | PROT_EXEC,
  379. MAP_ANONYMOUS | MAP_PRIVATE, 0);
  380. if (IS_ERR(usermem)) {
  381. pr_err("Failed to allocate user memory\n");
  382. kfree(kmem);
  383. return;
  384. }
  385. pr_info("out-of-bounds in copy_from_user()\n");
  386. unused = copy_from_user(kmem, usermem, size + 1);
  387. pr_info("out-of-bounds in copy_to_user()\n");
  388. unused = copy_to_user(usermem, kmem, size + 1);
  389. pr_info("out-of-bounds in __copy_from_user()\n");
  390. unused = __copy_from_user(kmem, usermem, size + 1);
  391. pr_info("out-of-bounds in __copy_to_user()\n");
  392. unused = __copy_to_user(usermem, kmem, size + 1);
  393. pr_info("out-of-bounds in __copy_from_user_inatomic()\n");
  394. unused = __copy_from_user_inatomic(kmem, usermem, size + 1);
  395. pr_info("out-of-bounds in __copy_to_user_inatomic()\n");
  396. unused = __copy_to_user_inatomic(usermem, kmem, size + 1);
  397. pr_info("out-of-bounds in strncpy_from_user()\n");
  398. unused = strncpy_from_user(kmem, usermem, size + 1);
  399. vm_munmap((unsigned long)usermem, PAGE_SIZE);
  400. kfree(kmem);
  401. }
  402. static noinline void __init use_after_scope_test(void)
  403. {
  404. volatile char *volatile p;
  405. pr_info("use-after-scope on int\n");
  406. {
  407. int local = 0;
  408. p = (char *)&local;
  409. }
  410. p[0] = 1;
  411. p[3] = 1;
  412. pr_info("use-after-scope on array\n");
  413. {
  414. char local[1024] = {0};
  415. p = local;
  416. }
  417. p[0] = 1;
  418. p[1023] = 1;
  419. }
  420. static noinline void __init kasan_alloca_oob_left(void)
  421. {
  422. volatile int i = 10;
  423. char alloca_array[i];
  424. char *p = alloca_array - 1;
  425. pr_info("out-of-bounds to left on alloca\n");
  426. *(volatile char *)p;
  427. }
  428. static noinline void __init kasan_alloca_oob_right(void)
  429. {
  430. volatile int i = 10;
  431. char alloca_array[i];
  432. char *p = alloca_array + i;
  433. pr_info("out-of-bounds to right on alloca\n");
  434. *(volatile char *)p;
  435. }
  436. static noinline void __init kmem_cache_double_free(void)
  437. {
  438. char *p;
  439. size_t size = 200;
  440. struct kmem_cache *cache;
  441. cache = kmem_cache_create("test_cache", size, 0, 0, NULL);
  442. if (!cache) {
  443. pr_err("Cache allocation failed\n");
  444. return;
  445. }
  446. pr_info("double-free on heap object\n");
  447. p = kmem_cache_alloc(cache, GFP_KERNEL);
  448. if (!p) {
  449. pr_err("Allocation failed\n");
  450. kmem_cache_destroy(cache);
  451. return;
  452. }
  453. kmem_cache_free(cache, p);
  454. kmem_cache_free(cache, p);
  455. kmem_cache_destroy(cache);
  456. }
  457. static noinline void __init kmem_cache_invalid_free(void)
  458. {
  459. char *p;
  460. size_t size = 200;
  461. struct kmem_cache *cache;
  462. cache = kmem_cache_create("test_cache", size, 0, SLAB_TYPESAFE_BY_RCU,
  463. NULL);
  464. if (!cache) {
  465. pr_err("Cache allocation failed\n");
  466. return;
  467. }
  468. pr_info("invalid-free of heap object\n");
  469. p = kmem_cache_alloc(cache, GFP_KERNEL);
  470. if (!p) {
  471. pr_err("Allocation failed\n");
  472. kmem_cache_destroy(cache);
  473. return;
  474. }
  475. /* Trigger invalid free, the object doesn't get freed */
  476. kmem_cache_free(cache, p + 1);
  477. /*
  478. * Properly free the object to prevent the "Objects remaining in
  479. * test_cache on __kmem_cache_shutdown" BUG failure.
  480. */
  481. kmem_cache_free(cache, p);
  482. kmem_cache_destroy(cache);
  483. }
  484. static int __init kmalloc_tests_init(void)
  485. {
  486. /*
  487. * Temporarily enable multi-shot mode. Otherwise, we'd only get a
  488. * report for the first case.
  489. */
  490. bool multishot = kasan_save_enable_multi_shot();
  491. kmalloc_oob_right();
  492. kmalloc_oob_left();
  493. kmalloc_node_oob_right();
  494. #ifdef CONFIG_SLUB
  495. kmalloc_pagealloc_oob_right();
  496. kmalloc_pagealloc_uaf();
  497. kmalloc_pagealloc_invalid_free();
  498. #endif
  499. kmalloc_large_oob_right();
  500. kmalloc_oob_krealloc_more();
  501. kmalloc_oob_krealloc_less();
  502. kmalloc_oob_16();
  503. kmalloc_oob_in_memset();
  504. kmalloc_oob_memset_2();
  505. kmalloc_oob_memset_4();
  506. kmalloc_oob_memset_8();
  507. kmalloc_oob_memset_16();
  508. kmalloc_uaf();
  509. kmalloc_uaf_memset();
  510. kmalloc_uaf2();
  511. kmem_cache_oob();
  512. memcg_accounted_kmem_cache();
  513. kasan_stack_oob();
  514. kasan_global_oob();
  515. kasan_alloca_oob_left();
  516. kasan_alloca_oob_right();
  517. ksize_unpoisons_memory();
  518. copy_user_test();
  519. use_after_scope_test();
  520. kmem_cache_double_free();
  521. kmem_cache_invalid_free();
  522. kasan_restore_multi_shot(multishot);
  523. return -EAGAIN;
  524. }
  525. module_init(kmalloc_tests_init);
  526. MODULE_LICENSE("GPL");