1 // SPDX-License-Identifier: GPL-2.0
3 * This is for all the tests relating directly to heap memory, including
4 * page allocation and slab allocations.
7 #include <linux/kfence.h>
8 #include <linux/slab.h>
9 #include <linux/vmalloc.h>
10 #include <linux/sched.h>
12 static struct kmem_cache *double_free_cache;
13 static struct kmem_cache *a_cache;
14 static struct kmem_cache *b_cache;
17 * Using volatile here means the compiler cannot ever make assumptions
18 * about this value. This means compile-time length checks involving
19 * this variable cannot be performed; only run-time checks.
21 static volatile int __offset = 1;
24 * If there aren't guard pages, it's likely that a consecutive allocation will
25 * let us overflow into the second allocation without overwriting something real.
27 * This should always be caught because there is an unconditional unmapped
28 * page after vmap allocations.
30 static void lkdtm_VMALLOC_LINEAR_OVERFLOW(void)
34 one = vzalloc(PAGE_SIZE);
35 OPTIMIZER_HIDE_VAR(one);
36 two = vzalloc(PAGE_SIZE);
38 pr_info("Attempting vmalloc linear overflow ...\n");
39 memset(one, 0xAA, PAGE_SIZE + __offset);
46 * This tries to stay within the next largest power-of-2 kmalloc cache
47 * to avoid actually overwriting anything important if it's not detected
50 * This should get caught by either memory tagging, KASan, or by using
51 * CONFIG_SLUB_DEBUG=y and slab_debug=ZF (or CONFIG_SLUB_DEBUG_ON=y).
53 static void lkdtm_SLAB_LINEAR_OVERFLOW(void)
56 u32 *data = kmalloc(len, GFP_KERNEL);
60 pr_info("Attempting slab linear overflow ...\n");
61 OPTIMIZER_HIDE_VAR(data);
62 data[1024 / sizeof(u32)] = 0x12345678;
66 static void lkdtm_WRITE_AFTER_FREE(void)
71 * The slub allocator uses the first word to store the free
72 * pointer in some configurations. Use the middle of the
73 * allocation to avoid running into the freelist
75 size_t offset = (len / sizeof(*base)) / 2;
77 base = kmalloc(len, GFP_KERNEL);
80 pr_info("Allocated memory %p-%p\n", base, &base[offset * 2]);
81 pr_info("Attempting bad write to freed memory at %p\n",
84 base[offset] = 0x0abcdef0;
85 /* Attempt to notice the overwrite. */
86 again = kmalloc(len, GFP_KERNEL);
89 pr_info("Hmm, didn't get the same memory range.\n");
92 static void lkdtm_READ_AFTER_FREE(void)
97 * The slub allocator will use the either the first word or
98 * the middle of the allocation to store the free pointer,
99 * depending on configurations. Store in the second word to
100 * avoid running into the freelist.
102 size_t offset = sizeof(*base);
104 base = kmalloc(len, GFP_KERNEL);
106 pr_info("Unable to allocate base memory.\n");
110 val = kmalloc(len, GFP_KERNEL);
112 pr_info("Unable to allocate val memory.\n");
119 pr_info("Value in memory before free: %x\n", base[offset]);
123 pr_info("Attempting bad read from freed memory\n");
126 /* Good! Poisoning happened, so declare a win. */
127 pr_info("Memory correctly poisoned (%x)\n", saw);
129 pr_err("FAIL: Memory was not poisoned!\n");
130 pr_expected_config_param(CONFIG_INIT_ON_FREE_DEFAULT_ON, "init_on_free");
136 static void lkdtm_KFENCE_READ_AFTER_FREE(void)
139 unsigned long timeout, resched_after;
142 * The slub allocator will use the either the first word or
143 * the middle of the allocation to store the free pointer,
144 * depending on configurations. Store in the second word to
145 * avoid running into the freelist.
147 size_t offset = sizeof(*base);
150 * 100x the sample interval should be more than enough to ensure we get
151 * a KFENCE allocation eventually.
153 timeout = jiffies + msecs_to_jiffies(100 * kfence_sample_interval);
155 * Especially for non-preemption kernels, ensure the allocation-gate
156 * timer can catch up: after @resched_after, every failed allocation
157 * attempt yields, to ensure the allocation-gate timer is scheduled.
159 resched_after = jiffies + msecs_to_jiffies(kfence_sample_interval);
161 base = kmalloc(len, GFP_KERNEL);
163 pr_err("FAIL: Unable to allocate kfence memory!\n");
167 if (is_kfence_address(base)) {
170 pr_info("Value in memory before free: %x\n", base[offset]);
174 pr_info("Attempting bad read from freed memory\n");
177 /* Good! Poisoning happened, so declare a win. */
178 pr_info("Memory correctly poisoned (%x)\n", saw);
180 pr_err("FAIL: Memory was not poisoned!\n");
181 pr_expected_config_param(CONFIG_INIT_ON_FREE_DEFAULT_ON, "init_on_free");
187 if (time_after(jiffies, resched_after))
189 } while (time_before(jiffies, timeout));
191 pr_err("FAIL: kfence memory never allocated!\n");
194 static void lkdtm_WRITE_BUDDY_AFTER_FREE(void)
196 unsigned long p = __get_free_page(GFP_KERNEL);
198 pr_info("Unable to allocate free page\n");
202 pr_info("Writing to the buddy page before free\n");
203 memset((void *)p, 0x3, PAGE_SIZE);
206 pr_info("Attempting bad write to the buddy page after free\n");
207 memset((void *)p, 0x78, PAGE_SIZE);
208 /* Attempt to notice the overwrite. */
209 p = __get_free_page(GFP_KERNEL);
214 static void lkdtm_READ_BUDDY_AFTER_FREE(void)
216 unsigned long p = __get_free_page(GFP_KERNEL);
221 pr_info("Unable to allocate free page\n");
225 val = kmalloc(1024, GFP_KERNEL);
227 pr_info("Unable to allocate val memory.\n");
236 pr_info("Value in memory before free: %x\n", base[0]);
238 pr_info("Attempting to read from freed memory\n");
241 /* Good! Poisoning happened, so declare a win. */
242 pr_info("Memory correctly poisoned (%x)\n", saw);
244 pr_err("FAIL: Buddy page was not poisoned!\n");
245 pr_expected_config_param(CONFIG_INIT_ON_FREE_DEFAULT_ON, "init_on_free");
251 static void lkdtm_SLAB_INIT_ON_ALLOC(void)
256 first = kmalloc(512, GFP_KERNEL);
258 pr_info("Unable to allocate 512 bytes the first time.\n");
262 memset(first, 0xAB, 512);
265 val = kmalloc(512, GFP_KERNEL);
267 pr_info("Unable to allocate 512 bytes the second time.\n");
271 pr_warn("Reallocation missed clobbered memory.\n");
274 if (memchr(val, 0xAB, 512) == NULL) {
275 pr_info("Memory appears initialized (%x, no earlier values)\n", *val);
277 pr_err("FAIL: Slab was not initialized\n");
278 pr_expected_config_param(CONFIG_INIT_ON_ALLOC_DEFAULT_ON, "init_on_alloc");
283 static void lkdtm_BUDDY_INIT_ON_ALLOC(void)
288 first = (u8 *)__get_free_page(GFP_KERNEL);
290 pr_info("Unable to allocate first free page\n");
294 memset(first, 0xAB, PAGE_SIZE);
295 free_page((unsigned long)first);
297 val = (u8 *)__get_free_page(GFP_KERNEL);
299 pr_info("Unable to allocate second free page\n");
304 pr_warn("Reallocation missed clobbered memory.\n");
307 if (memchr(val, 0xAB, PAGE_SIZE) == NULL) {
308 pr_info("Memory appears initialized (%x, no earlier values)\n", *val);
310 pr_err("FAIL: Slab was not initialized\n");
311 pr_expected_config_param(CONFIG_INIT_ON_ALLOC_DEFAULT_ON, "init_on_alloc");
313 free_page((unsigned long)val);
316 static void lkdtm_SLAB_FREE_DOUBLE(void)
320 val = kmem_cache_alloc(double_free_cache, GFP_KERNEL);
322 pr_info("Unable to allocate double_free_cache memory.\n");
326 /* Just make sure we got real memory. */
328 pr_info("Attempting double slab free ...\n");
329 kmem_cache_free(double_free_cache, val);
330 kmem_cache_free(double_free_cache, val);
333 static void lkdtm_SLAB_FREE_CROSS(void)
337 val = kmem_cache_alloc(a_cache, GFP_KERNEL);
339 pr_info("Unable to allocate a_cache memory.\n");
343 /* Just make sure we got real memory. */
345 pr_info("Attempting cross-cache slab free ...\n");
346 kmem_cache_free(b_cache, val);
349 static void lkdtm_SLAB_FREE_PAGE(void)
351 unsigned long p = __get_free_page(GFP_KERNEL);
353 pr_info("Attempting non-Slab slab free ...\n");
354 kmem_cache_free(NULL, (void *)p);
359 * We have constructors to keep the caches distinctly separated without
360 * needing to boot with "slab_nomerge".
362 static void ctor_double_free(void *region)
364 static void ctor_a(void *region)
366 static void ctor_b(void *region)
369 void __init lkdtm_heap_init(void)
371 double_free_cache = kmem_cache_create("lkdtm-heap-double_free",
372 64, 0, 0, ctor_double_free);
373 a_cache = kmem_cache_create("lkdtm-heap-a", 64, 0, 0, ctor_a);
374 b_cache = kmem_cache_create("lkdtm-heap-b", 64, 0, 0, ctor_b);
377 void __exit lkdtm_heap_exit(void)
379 kmem_cache_destroy(double_free_cache);
380 kmem_cache_destroy(a_cache);
381 kmem_cache_destroy(b_cache);
384 static struct crashtype crashtypes[] = {
385 CRASHTYPE(SLAB_LINEAR_OVERFLOW),
386 CRASHTYPE(VMALLOC_LINEAR_OVERFLOW),
387 CRASHTYPE(WRITE_AFTER_FREE),
388 CRASHTYPE(READ_AFTER_FREE),
389 CRASHTYPE(KFENCE_READ_AFTER_FREE),
390 CRASHTYPE(WRITE_BUDDY_AFTER_FREE),
391 CRASHTYPE(READ_BUDDY_AFTER_FREE),
392 CRASHTYPE(SLAB_INIT_ON_ALLOC),
393 CRASHTYPE(BUDDY_INIT_ON_ALLOC),
394 CRASHTYPE(SLAB_FREE_DOUBLE),
395 CRASHTYPE(SLAB_FREE_CROSS),
396 CRASHTYPE(SLAB_FREE_PAGE),
399 struct crashtype_category heap_crashtypes = {
400 .crashtypes = crashtypes,
401 .len = ARRAY_SIZE(crashtypes),