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/slab.h>
8 #include <linux/vmalloc.h>
9 #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;
16 * Using volatile here means the compiler cannot ever make assumptions
17 * about this value. This means compile-time length checks involving
18 * this variable cannot be performed; only run-time checks.
20 static volatile int __offset = 1;
23 * If there aren't guard pages, it's likely that a consecutive allocation will
24 * let us overflow into the second allocation without overwriting something real.
26 * This should always be caught because there is an unconditional unmapped
27 * page after vmap allocations.
29 static void lkdtm_VMALLOC_LINEAR_OVERFLOW(void)
33 one = vzalloc(PAGE_SIZE);
34 two = vzalloc(PAGE_SIZE);
36 pr_info("Attempting vmalloc linear overflow ...\n");
37 memset(one, 0xAA, PAGE_SIZE + __offset);
44 * This tries to stay within the next largest power-of-2 kmalloc cache
45 * to avoid actually overwriting anything important if it's not detected
48 * This should get caught by either memory tagging, KASan, or by using
49 * CONFIG_SLUB_DEBUG=y and slub_debug=ZF (or CONFIG_SLUB_DEBUG_ON=y).
51 static void lkdtm_SLAB_LINEAR_OVERFLOW(void)
54 u32 *data = kmalloc(len, GFP_KERNEL);
58 pr_info("Attempting slab linear overflow ...\n");
59 OPTIMIZER_HIDE_VAR(data);
60 data[1024 / sizeof(u32)] = 0x12345678;
64 static void lkdtm_WRITE_AFTER_FREE(void)
69 * The slub allocator uses the first word to store the free
70 * pointer in some configurations. Use the middle of the
71 * allocation to avoid running into the freelist
73 size_t offset = (len / sizeof(*base)) / 2;
75 base = kmalloc(len, GFP_KERNEL);
78 pr_info("Allocated memory %p-%p\n", base, &base[offset * 2]);
79 pr_info("Attempting bad write to freed memory at %p\n",
82 base[offset] = 0x0abcdef0;
83 /* Attempt to notice the overwrite. */
84 again = kmalloc(len, GFP_KERNEL);
87 pr_info("Hmm, didn't get the same memory range.\n");
90 static void lkdtm_READ_AFTER_FREE(void)
95 * The slub allocator will use the either the first word or
96 * the middle of the allocation to store the free pointer,
97 * depending on configurations. Store in the second word to
98 * avoid running into the freelist.
100 size_t offset = sizeof(*base);
102 base = kmalloc(len, GFP_KERNEL);
104 pr_info("Unable to allocate base memory.\n");
108 val = kmalloc(len, GFP_KERNEL);
110 pr_info("Unable to allocate val memory.\n");
117 pr_info("Value in memory before free: %x\n", base[offset]);
121 pr_info("Attempting bad read from freed memory\n");
124 /* Good! Poisoning happened, so declare a win. */
125 pr_info("Memory correctly poisoned (%x)\n", saw);
127 pr_err("FAIL: Memory was not poisoned!\n");
128 pr_expected_config_param(CONFIG_INIT_ON_FREE_DEFAULT_ON, "init_on_free");
134 static void lkdtm_WRITE_BUDDY_AFTER_FREE(void)
136 unsigned long p = __get_free_page(GFP_KERNEL);
138 pr_info("Unable to allocate free page\n");
142 pr_info("Writing to the buddy page before free\n");
143 memset((void *)p, 0x3, PAGE_SIZE);
146 pr_info("Attempting bad write to the buddy page after free\n");
147 memset((void *)p, 0x78, PAGE_SIZE);
148 /* Attempt to notice the overwrite. */
149 p = __get_free_page(GFP_KERNEL);
154 static void lkdtm_READ_BUDDY_AFTER_FREE(void)
156 unsigned long p = __get_free_page(GFP_KERNEL);
161 pr_info("Unable to allocate free page\n");
165 val = kmalloc(1024, GFP_KERNEL);
167 pr_info("Unable to allocate val memory.\n");
176 pr_info("Value in memory before free: %x\n", base[0]);
178 pr_info("Attempting to read from freed memory\n");
181 /* Good! Poisoning happened, so declare a win. */
182 pr_info("Memory correctly poisoned (%x)\n", saw);
184 pr_err("FAIL: Buddy page was not poisoned!\n");
185 pr_expected_config_param(CONFIG_INIT_ON_FREE_DEFAULT_ON, "init_on_free");
191 static void lkdtm_SLAB_INIT_ON_ALLOC(void)
196 first = kmalloc(512, GFP_KERNEL);
198 pr_info("Unable to allocate 512 bytes the first time.\n");
202 memset(first, 0xAB, 512);
205 val = kmalloc(512, GFP_KERNEL);
207 pr_info("Unable to allocate 512 bytes the second time.\n");
211 pr_warn("Reallocation missed clobbered memory.\n");
214 if (memchr(val, 0xAB, 512) == NULL) {
215 pr_info("Memory appears initialized (%x, no earlier values)\n", *val);
217 pr_err("FAIL: Slab was not initialized\n");
218 pr_expected_config_param(CONFIG_INIT_ON_ALLOC_DEFAULT_ON, "init_on_alloc");
223 static void lkdtm_BUDDY_INIT_ON_ALLOC(void)
228 first = (u8 *)__get_free_page(GFP_KERNEL);
230 pr_info("Unable to allocate first free page\n");
234 memset(first, 0xAB, PAGE_SIZE);
235 free_page((unsigned long)first);
237 val = (u8 *)__get_free_page(GFP_KERNEL);
239 pr_info("Unable to allocate second free page\n");
244 pr_warn("Reallocation missed clobbered memory.\n");
247 if (memchr(val, 0xAB, PAGE_SIZE) == NULL) {
248 pr_info("Memory appears initialized (%x, no earlier values)\n", *val);
250 pr_err("FAIL: Slab was not initialized\n");
251 pr_expected_config_param(CONFIG_INIT_ON_ALLOC_DEFAULT_ON, "init_on_alloc");
253 free_page((unsigned long)val);
256 static void lkdtm_SLAB_FREE_DOUBLE(void)
260 val = kmem_cache_alloc(double_free_cache, GFP_KERNEL);
262 pr_info("Unable to allocate double_free_cache memory.\n");
266 /* Just make sure we got real memory. */
268 pr_info("Attempting double slab free ...\n");
269 kmem_cache_free(double_free_cache, val);
270 kmem_cache_free(double_free_cache, val);
273 static void lkdtm_SLAB_FREE_CROSS(void)
277 val = kmem_cache_alloc(a_cache, GFP_KERNEL);
279 pr_info("Unable to allocate a_cache memory.\n");
283 /* Just make sure we got real memory. */
285 pr_info("Attempting cross-cache slab free ...\n");
286 kmem_cache_free(b_cache, val);
289 static void lkdtm_SLAB_FREE_PAGE(void)
291 unsigned long p = __get_free_page(GFP_KERNEL);
293 pr_info("Attempting non-Slab slab free ...\n");
294 kmem_cache_free(NULL, (void *)p);
299 * We have constructors to keep the caches distinctly separated without
300 * needing to boot with "slab_nomerge".
302 static void ctor_double_free(void *region)
304 static void ctor_a(void *region)
306 static void ctor_b(void *region)
309 void __init lkdtm_heap_init(void)
311 double_free_cache = kmem_cache_create("lkdtm-heap-double_free",
312 64, 0, 0, ctor_double_free);
313 a_cache = kmem_cache_create("lkdtm-heap-a", 64, 0, 0, ctor_a);
314 b_cache = kmem_cache_create("lkdtm-heap-b", 64, 0, 0, ctor_b);
317 void __exit lkdtm_heap_exit(void)
319 kmem_cache_destroy(double_free_cache);
320 kmem_cache_destroy(a_cache);
321 kmem_cache_destroy(b_cache);
324 static struct crashtype crashtypes[] = {
325 CRASHTYPE(SLAB_LINEAR_OVERFLOW),
326 CRASHTYPE(VMALLOC_LINEAR_OVERFLOW),
327 CRASHTYPE(WRITE_AFTER_FREE),
328 CRASHTYPE(READ_AFTER_FREE),
329 CRASHTYPE(WRITE_BUDDY_AFTER_FREE),
330 CRASHTYPE(READ_BUDDY_AFTER_FREE),
331 CRASHTYPE(SLAB_INIT_ON_ALLOC),
332 CRASHTYPE(BUDDY_INIT_ON_ALLOC),
333 CRASHTYPE(SLAB_FREE_DOUBLE),
334 CRASHTYPE(SLAB_FREE_CROSS),
335 CRASHTYPE(SLAB_FREE_PAGE),
338 struct crashtype_category heap_crashtypes = {
339 .crashtypes = crashtypes,
340 .len = ARRAY_SIZE(crashtypes),