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/sched.h>
10 static struct kmem_cache *double_free_cache;
11 static struct kmem_cache *a_cache;
12 static struct kmem_cache *b_cache;
15 * This tries to stay within the next largest power-of-2 kmalloc cache
16 * to avoid actually overwriting anything important if it's not detected
19 void lkdtm_OVERWRITE_ALLOCATION(void)
22 u32 *data = kmalloc(len, GFP_KERNEL);
26 data[1024 / sizeof(u32)] = 0x12345678;
30 void lkdtm_WRITE_AFTER_FREE(void)
35 * The slub allocator uses the first word to store the free
36 * pointer in some configurations. Use the middle of the
37 * allocation to avoid running into the freelist
39 size_t offset = (len / sizeof(*base)) / 2;
41 base = kmalloc(len, GFP_KERNEL);
44 pr_info("Allocated memory %p-%p\n", base, &base[offset * 2]);
45 pr_info("Attempting bad write to freed memory at %p\n",
48 base[offset] = 0x0abcdef0;
49 /* Attempt to notice the overwrite. */
50 again = kmalloc(len, GFP_KERNEL);
53 pr_info("Hmm, didn't get the same memory range.\n");
56 void lkdtm_READ_AFTER_FREE(void)
61 * The slub allocator will use the either the first word or
62 * the middle of the allocation to store the free pointer,
63 * depending on configurations. Store in the second word to
64 * avoid running into the freelist.
66 size_t offset = sizeof(*base);
68 base = kmalloc(len, GFP_KERNEL);
70 pr_info("Unable to allocate base memory.\n");
74 val = kmalloc(len, GFP_KERNEL);
76 pr_info("Unable to allocate val memory.\n");
83 pr_info("Value in memory before free: %x\n", base[offset]);
87 pr_info("Attempting bad read from freed memory\n");
90 /* Good! Poisoning happened, so declare a win. */
91 pr_info("Memory correctly poisoned (%x)\n", saw);
94 pr_info("Memory was not poisoned\n");
99 void lkdtm_WRITE_BUDDY_AFTER_FREE(void)
101 unsigned long p = __get_free_page(GFP_KERNEL);
103 pr_info("Unable to allocate free page\n");
107 pr_info("Writing to the buddy page before free\n");
108 memset((void *)p, 0x3, PAGE_SIZE);
111 pr_info("Attempting bad write to the buddy page after free\n");
112 memset((void *)p, 0x78, PAGE_SIZE);
113 /* Attempt to notice the overwrite. */
114 p = __get_free_page(GFP_KERNEL);
119 void lkdtm_READ_BUDDY_AFTER_FREE(void)
121 unsigned long p = __get_free_page(GFP_KERNEL);
126 pr_info("Unable to allocate free page\n");
130 val = kmalloc(1024, GFP_KERNEL);
132 pr_info("Unable to allocate val memory.\n");
141 pr_info("Value in memory before free: %x\n", base[0]);
143 pr_info("Attempting to read from freed memory\n");
146 /* Good! Poisoning happened, so declare a win. */
147 pr_info("Memory correctly poisoned (%x)\n", saw);
150 pr_info("Buddy page was not poisoned\n");
155 void lkdtm_SLAB_FREE_DOUBLE(void)
159 val = kmem_cache_alloc(double_free_cache, GFP_KERNEL);
161 pr_info("Unable to allocate double_free_cache memory.\n");
165 /* Just make sure we got real memory. */
167 pr_info("Attempting double slab free ...\n");
168 kmem_cache_free(double_free_cache, val);
169 kmem_cache_free(double_free_cache, val);
172 void lkdtm_SLAB_FREE_CROSS(void)
176 val = kmem_cache_alloc(a_cache, GFP_KERNEL);
178 pr_info("Unable to allocate a_cache memory.\n");
182 /* Just make sure we got real memory. */
184 pr_info("Attempting cross-cache slab free ...\n");
185 kmem_cache_free(b_cache, val);
188 void lkdtm_SLAB_FREE_PAGE(void)
190 unsigned long p = __get_free_page(GFP_KERNEL);
192 pr_info("Attempting non-Slab slab free ...\n");
193 kmem_cache_free(NULL, (void *)p);
198 * We have constructors to keep the caches distinctly separated without
199 * needing to boot with "slab_nomerge".
201 static void ctor_double_free(void *region)
203 static void ctor_a(void *region)
205 static void ctor_b(void *region)
208 void __init lkdtm_heap_init(void)
210 double_free_cache = kmem_cache_create("lkdtm-heap-double_free",
211 64, 0, 0, ctor_double_free);
212 a_cache = kmem_cache_create("lkdtm-heap-a", 64, 0, 0, ctor_a);
213 b_cache = kmem_cache_create("lkdtm-heap-b", 64, 0, 0, ctor_b);
216 void __exit lkdtm_heap_exit(void)
218 kmem_cache_destroy(double_free_cache);
219 kmem_cache_destroy(a_cache);
220 kmem_cache_destroy(b_cache);