heap.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * This is for all the tests relating directly to heap memory, including
  4. * page allocation and slab allocations.
  5. */
  6. #include "lkdtm.h"
  7. #include <linux/kfence.h>
  8. #include <linux/slab.h>
  9. #include <linux/vmalloc.h>
  10. #include <linux/sched.h>
  11. static struct kmem_cache *double_free_cache;
  12. static struct kmem_cache *a_cache;
  13. static struct kmem_cache *b_cache;
  14. /*
  15. * Using volatile here means the compiler cannot ever make assumptions
  16. * about this value. This means compile-time length checks involving
  17. * this variable cannot be performed; only run-time checks.
  18. */
  19. static volatile int __offset = 1;
  20. /*
  21. * If there aren't guard pages, it's likely that a consecutive allocation will
  22. * let us overflow into the second allocation without overwriting something real.
  23. *
  24. * This should always be caught because there is an unconditional unmapped
  25. * page after vmap allocations.
  26. */
  27. static void lkdtm_VMALLOC_LINEAR_OVERFLOW(void)
  28. {
  29. char *one, *two;
  30. one = vzalloc(PAGE_SIZE);
  31. OPTIMIZER_HIDE_VAR(one);
  32. two = vzalloc(PAGE_SIZE);
  33. pr_info("Attempting vmalloc linear overflow ...\n");
  34. memset(one, 0xAA, PAGE_SIZE + __offset);
  35. vfree(two);
  36. vfree(one);
  37. }
  38. /*
  39. * This tries to stay within the next largest power-of-2 kmalloc cache
  40. * to avoid actually overwriting anything important if it's not detected
  41. * correctly.
  42. *
  43. * This should get caught by either memory tagging, KASan, or by using
  44. * CONFIG_SLUB_DEBUG=y and slab_debug=ZF (or CONFIG_SLUB_DEBUG_ON=y).
  45. */
  46. static void lkdtm_SLAB_LINEAR_OVERFLOW(void)
  47. {
  48. size_t len = 1020;
  49. u32 *data = kmalloc(len, GFP_KERNEL);
  50. if (!data)
  51. return;
  52. pr_info("Attempting slab linear overflow ...\n");
  53. OPTIMIZER_HIDE_VAR(data);
  54. data[1024 / sizeof(u32)] = 0x12345678;
  55. kfree(data);
  56. }
  57. static void lkdtm_WRITE_AFTER_FREE(void)
  58. {
  59. int *base, *again;
  60. size_t len = 1024;
  61. /*
  62. * The slub allocator uses the first word to store the free
  63. * pointer in some configurations. Use the middle of the
  64. * allocation to avoid running into the freelist
  65. */
  66. size_t offset = (len / sizeof(*base)) / 2;
  67. base = kmalloc(len, GFP_KERNEL);
  68. if (!base)
  69. return;
  70. pr_info("Allocated memory %p-%p\n", base, &base[offset * 2]);
  71. pr_info("Attempting bad write to freed memory at %p\n",
  72. &base[offset]);
  73. kfree(base);
  74. base[offset] = 0x0abcdef0;
  75. /* Attempt to notice the overwrite. */
  76. again = kmalloc(len, GFP_KERNEL);
  77. kfree(again);
  78. if (again != base)
  79. pr_info("Hmm, didn't get the same memory range.\n");
  80. }
  81. static void lkdtm_READ_AFTER_FREE(void)
  82. {
  83. int *base, *val, saw;
  84. size_t len = 1024;
  85. /*
  86. * The slub allocator will use the either the first word or
  87. * the middle of the allocation to store the free pointer,
  88. * depending on configurations. Store in the second word to
  89. * avoid running into the freelist.
  90. */
  91. size_t offset = sizeof(*base);
  92. base = kmalloc(len, GFP_KERNEL);
  93. if (!base) {
  94. pr_info("Unable to allocate base memory.\n");
  95. return;
  96. }
  97. val = kmalloc(len, GFP_KERNEL);
  98. if (!val) {
  99. pr_info("Unable to allocate val memory.\n");
  100. kfree(base);
  101. return;
  102. }
  103. *val = 0x12345678;
  104. base[offset] = *val;
  105. pr_info("Value in memory before free: %x\n", base[offset]);
  106. kfree(base);
  107. pr_info("Attempting bad read from freed memory\n");
  108. saw = base[offset];
  109. if (saw != *val) {
  110. /* Good! Poisoning happened, so declare a win. */
  111. pr_info("Memory correctly poisoned (%x)\n", saw);
  112. } else {
  113. pr_err("FAIL: Memory was not poisoned!\n");
  114. pr_expected_config_param(CONFIG_INIT_ON_FREE_DEFAULT_ON, "init_on_free");
  115. }
  116. kfree(val);
  117. }
  118. static void lkdtm_KFENCE_READ_AFTER_FREE(void)
  119. {
  120. int *base, val, saw;
  121. unsigned long timeout, resched_after;
  122. size_t len = 1024;
  123. /*
  124. * The slub allocator will use the either the first word or
  125. * the middle of the allocation to store the free pointer,
  126. * depending on configurations. Store in the second word to
  127. * avoid running into the freelist.
  128. */
  129. size_t offset = sizeof(*base);
  130. /*
  131. * 100x the sample interval should be more than enough to ensure we get
  132. * a KFENCE allocation eventually.
  133. */
  134. timeout = jiffies + msecs_to_jiffies(100 * kfence_sample_interval);
  135. /*
  136. * Especially for non-preemption kernels, ensure the allocation-gate
  137. * timer can catch up: after @resched_after, every failed allocation
  138. * attempt yields, to ensure the allocation-gate timer is scheduled.
  139. */
  140. resched_after = jiffies + msecs_to_jiffies(kfence_sample_interval);
  141. do {
  142. base = kmalloc(len, GFP_KERNEL);
  143. if (!base) {
  144. pr_err("FAIL: Unable to allocate kfence memory!\n");
  145. return;
  146. }
  147. if (is_kfence_address(base)) {
  148. val = 0x12345678;
  149. base[offset] = val;
  150. pr_info("Value in memory before free: %x\n", base[offset]);
  151. kfree(base);
  152. pr_info("Attempting bad read from freed memory\n");
  153. saw = base[offset];
  154. if (saw != val) {
  155. /* Good! Poisoning happened, so declare a win. */
  156. pr_info("Memory correctly poisoned (%x)\n", saw);
  157. } else {
  158. pr_err("FAIL: Memory was not poisoned!\n");
  159. pr_expected_config_param(CONFIG_INIT_ON_FREE_DEFAULT_ON, "init_on_free");
  160. }
  161. return;
  162. }
  163. kfree(base);
  164. if (time_after(jiffies, resched_after))
  165. cond_resched();
  166. } while (time_before(jiffies, timeout));
  167. pr_err("FAIL: kfence memory never allocated!\n");
  168. }
  169. static void lkdtm_WRITE_BUDDY_AFTER_FREE(void)
  170. {
  171. unsigned long p = __get_free_page(GFP_KERNEL);
  172. if (!p) {
  173. pr_info("Unable to allocate free page\n");
  174. return;
  175. }
  176. pr_info("Writing to the buddy page before free\n");
  177. memset((void *)p, 0x3, PAGE_SIZE);
  178. free_page(p);
  179. schedule();
  180. pr_info("Attempting bad write to the buddy page after free\n");
  181. memset((void *)p, 0x78, PAGE_SIZE);
  182. /* Attempt to notice the overwrite. */
  183. p = __get_free_page(GFP_KERNEL);
  184. free_page(p);
  185. schedule();
  186. }
  187. static void lkdtm_READ_BUDDY_AFTER_FREE(void)
  188. {
  189. unsigned long p = __get_free_page(GFP_KERNEL);
  190. int saw, *val;
  191. int *base;
  192. if (!p) {
  193. pr_info("Unable to allocate free page\n");
  194. return;
  195. }
  196. val = kmalloc(1024, GFP_KERNEL);
  197. if (!val) {
  198. pr_info("Unable to allocate val memory.\n");
  199. free_page(p);
  200. return;
  201. }
  202. base = (int *)p;
  203. *val = 0x12345678;
  204. base[0] = *val;
  205. pr_info("Value in memory before free: %x\n", base[0]);
  206. free_page(p);
  207. pr_info("Attempting to read from freed memory\n");
  208. saw = base[0];
  209. if (saw != *val) {
  210. /* Good! Poisoning happened, so declare a win. */
  211. pr_info("Memory correctly poisoned (%x)\n", saw);
  212. } else {
  213. pr_err("FAIL: Buddy page was not poisoned!\n");
  214. pr_expected_config_param(CONFIG_INIT_ON_FREE_DEFAULT_ON, "init_on_free");
  215. }
  216. kfree(val);
  217. }
  218. static void lkdtm_SLAB_INIT_ON_ALLOC(void)
  219. {
  220. u8 *first;
  221. u8 *val;
  222. first = kmalloc(512, GFP_KERNEL);
  223. if (!first) {
  224. pr_info("Unable to allocate 512 bytes the first time.\n");
  225. return;
  226. }
  227. memset(first, 0xAB, 512);
  228. kfree(first);
  229. val = kmalloc(512, GFP_KERNEL);
  230. if (!val) {
  231. pr_info("Unable to allocate 512 bytes the second time.\n");
  232. return;
  233. }
  234. if (val != first) {
  235. pr_warn("Reallocation missed clobbered memory.\n");
  236. }
  237. if (memchr(val, 0xAB, 512) == NULL) {
  238. pr_info("Memory appears initialized (%x, no earlier values)\n", *val);
  239. } else {
  240. pr_err("FAIL: Slab was not initialized\n");
  241. pr_expected_config_param(CONFIG_INIT_ON_ALLOC_DEFAULT_ON, "init_on_alloc");
  242. }
  243. kfree(val);
  244. }
  245. static void lkdtm_BUDDY_INIT_ON_ALLOC(void)
  246. {
  247. u8 *first;
  248. u8 *val;
  249. first = (u8 *)__get_free_page(GFP_KERNEL);
  250. if (!first) {
  251. pr_info("Unable to allocate first free page\n");
  252. return;
  253. }
  254. memset(first, 0xAB, PAGE_SIZE);
  255. free_page((unsigned long)first);
  256. val = (u8 *)__get_free_page(GFP_KERNEL);
  257. if (!val) {
  258. pr_info("Unable to allocate second free page\n");
  259. return;
  260. }
  261. if (val != first) {
  262. pr_warn("Reallocation missed clobbered memory.\n");
  263. }
  264. if (memchr(val, 0xAB, PAGE_SIZE) == NULL) {
  265. pr_info("Memory appears initialized (%x, no earlier values)\n", *val);
  266. } else {
  267. pr_err("FAIL: Slab was not initialized\n");
  268. pr_expected_config_param(CONFIG_INIT_ON_ALLOC_DEFAULT_ON, "init_on_alloc");
  269. }
  270. free_page((unsigned long)val);
  271. }
  272. static void lkdtm_SLAB_FREE_DOUBLE(void)
  273. {
  274. int *val;
  275. val = kmem_cache_alloc(double_free_cache, GFP_KERNEL);
  276. if (!val) {
  277. pr_info("Unable to allocate double_free_cache memory.\n");
  278. return;
  279. }
  280. /* Just make sure we got real memory. */
  281. *val = 0x12345678;
  282. pr_info("Attempting double slab free ...\n");
  283. kmem_cache_free(double_free_cache, val);
  284. kmem_cache_free(double_free_cache, val);
  285. }
  286. static void lkdtm_SLAB_FREE_CROSS(void)
  287. {
  288. int *val;
  289. val = kmem_cache_alloc(a_cache, GFP_KERNEL);
  290. if (!val) {
  291. pr_info("Unable to allocate a_cache memory.\n");
  292. return;
  293. }
  294. /* Just make sure we got real memory. */
  295. *val = 0x12345679;
  296. pr_info("Attempting cross-cache slab free ...\n");
  297. kmem_cache_free(b_cache, val);
  298. }
  299. static void lkdtm_SLAB_FREE_PAGE(void)
  300. {
  301. unsigned long p = __get_free_page(GFP_KERNEL);
  302. pr_info("Attempting non-Slab slab free ...\n");
  303. kmem_cache_free(NULL, (void *)p);
  304. free_page(p);
  305. }
  306. /*
  307. * We have constructors to keep the caches distinctly separated without
  308. * needing to boot with "slab_nomerge".
  309. */
  310. static void ctor_double_free(void *region)
  311. { }
  312. static void ctor_a(void *region)
  313. { }
  314. static void ctor_b(void *region)
  315. { }
  316. void __init lkdtm_heap_init(void)
  317. {
  318. double_free_cache = kmem_cache_create("lkdtm-heap-double_free",
  319. 64, 0, 0, ctor_double_free);
  320. a_cache = kmem_cache_create("lkdtm-heap-a", 64, 0, 0, ctor_a);
  321. b_cache = kmem_cache_create("lkdtm-heap-b", 64, 0, 0, ctor_b);
  322. }
  323. void __exit lkdtm_heap_exit(void)
  324. {
  325. kmem_cache_destroy(double_free_cache);
  326. kmem_cache_destroy(a_cache);
  327. kmem_cache_destroy(b_cache);
  328. }
  329. static struct crashtype crashtypes[] = {
  330. CRASHTYPE(SLAB_LINEAR_OVERFLOW),
  331. CRASHTYPE(VMALLOC_LINEAR_OVERFLOW),
  332. CRASHTYPE(WRITE_AFTER_FREE),
  333. CRASHTYPE(READ_AFTER_FREE),
  334. CRASHTYPE(KFENCE_READ_AFTER_FREE),
  335. CRASHTYPE(WRITE_BUDDY_AFTER_FREE),
  336. CRASHTYPE(READ_BUDDY_AFTER_FREE),
  337. CRASHTYPE(SLAB_INIT_ON_ALLOC),
  338. CRASHTYPE(BUDDY_INIT_ON_ALLOC),
  339. CRASHTYPE(SLAB_FREE_DOUBLE),
  340. CRASHTYPE(SLAB_FREE_CROSS),
  341. CRASHTYPE(SLAB_FREE_PAGE),
  342. };
  343. struct crashtype_category heap_crashtypes = {
  344. .crashtypes = crashtypes,
  345. .len = ARRAY_SIZE(crashtypes),
  346. };