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 * If there aren't guard pages, it's likely that a consecutive allocation will
17 * let us overflow into the second allocation without overwriting something real.
19 void lkdtm_VMALLOC_LINEAR_OVERFLOW(void)
23 one = vzalloc(PAGE_SIZE);
24 two = vzalloc(PAGE_SIZE);
26 pr_info("Attempting vmalloc linear overflow ...\n");
27 memset(one, 0xAA, PAGE_SIZE + 1);
34 * This tries to stay within the next largest power-of-2 kmalloc cache
35 * to avoid actually overwriting anything important if it's not detected
38 void lkdtm_SLAB_LINEAR_OVERFLOW(void)
41 u32 *data = kmalloc(len, GFP_KERNEL);
45 pr_info("Attempting slab linear overflow ...\n");
46 data[1024 / sizeof(u32)] = 0x12345678;
50 void lkdtm_WRITE_AFTER_FREE(void)
55 * The slub allocator uses the first word to store the free
56 * pointer in some configurations. Use the middle of the
57 * allocation to avoid running into the freelist
59 size_t offset = (len / sizeof(*base)) / 2;
61 base = kmalloc(len, GFP_KERNEL);
64 pr_info("Allocated memory %p-%p\n", base, &base[offset * 2]);
65 pr_info("Attempting bad write to freed memory at %p\n",
68 base[offset] = 0x0abcdef0;
69 /* Attempt to notice the overwrite. */
70 again = kmalloc(len, GFP_KERNEL);
73 pr_info("Hmm, didn't get the same memory range.\n");
76 void lkdtm_READ_AFTER_FREE(void)
81 * The slub allocator will use the either the first word or
82 * the middle of the allocation to store the free pointer,
83 * depending on configurations. Store in the second word to
84 * avoid running into the freelist.
86 size_t offset = sizeof(*base);
88 base = kmalloc(len, GFP_KERNEL);
90 pr_info("Unable to allocate base memory.\n");
94 val = kmalloc(len, GFP_KERNEL);
96 pr_info("Unable to allocate val memory.\n");
103 pr_info("Value in memory before free: %x\n", base[offset]);
107 pr_info("Attempting bad read from freed memory\n");
110 /* Good! Poisoning happened, so declare a win. */
111 pr_info("Memory correctly poisoned (%x)\n", saw);
113 pr_err("FAIL: Memory was not poisoned!\n");
114 pr_expected_config_param(CONFIG_INIT_ON_FREE_DEFAULT_ON, "init_on_free");
120 void lkdtm_WRITE_BUDDY_AFTER_FREE(void)
122 unsigned long p = __get_free_page(GFP_KERNEL);
124 pr_info("Unable to allocate free page\n");
128 pr_info("Writing to the buddy page before free\n");
129 memset((void *)p, 0x3, PAGE_SIZE);
132 pr_info("Attempting bad write to the buddy page after free\n");
133 memset((void *)p, 0x78, PAGE_SIZE);
134 /* Attempt to notice the overwrite. */
135 p = __get_free_page(GFP_KERNEL);
140 void lkdtm_READ_BUDDY_AFTER_FREE(void)
142 unsigned long p = __get_free_page(GFP_KERNEL);
147 pr_info("Unable to allocate free page\n");
151 val = kmalloc(1024, GFP_KERNEL);
153 pr_info("Unable to allocate val memory.\n");
162 pr_info("Value in memory before free: %x\n", base[0]);
164 pr_info("Attempting to read from freed memory\n");
167 /* Good! Poisoning happened, so declare a win. */
168 pr_info("Memory correctly poisoned (%x)\n", saw);
170 pr_err("FAIL: Buddy page was not poisoned!\n");
171 pr_expected_config_param(CONFIG_INIT_ON_FREE_DEFAULT_ON, "init_on_free");
177 void lkdtm_SLAB_INIT_ON_ALLOC(void)
182 first = kmalloc(512, GFP_KERNEL);
184 pr_info("Unable to allocate 512 bytes the first time.\n");
188 memset(first, 0xAB, 512);
191 val = kmalloc(512, GFP_KERNEL);
193 pr_info("Unable to allocate 512 bytes the second time.\n");
197 pr_warn("Reallocation missed clobbered memory.\n");
200 if (memchr(val, 0xAB, 512) == NULL) {
201 pr_info("Memory appears initialized (%x, no earlier values)\n", *val);
203 pr_err("FAIL: Slab was not initialized\n");
204 pr_expected_config_param(CONFIG_INIT_ON_ALLOC_DEFAULT_ON, "init_on_alloc");
209 void lkdtm_BUDDY_INIT_ON_ALLOC(void)
214 first = (u8 *)__get_free_page(GFP_KERNEL);
216 pr_info("Unable to allocate first free page\n");
220 memset(first, 0xAB, PAGE_SIZE);
221 free_page((unsigned long)first);
223 val = (u8 *)__get_free_page(GFP_KERNEL);
225 pr_info("Unable to allocate second free page\n");
230 pr_warn("Reallocation missed clobbered memory.\n");
233 if (memchr(val, 0xAB, PAGE_SIZE) == NULL) {
234 pr_info("Memory appears initialized (%x, no earlier values)\n", *val);
236 pr_err("FAIL: Slab was not initialized\n");
237 pr_expected_config_param(CONFIG_INIT_ON_ALLOC_DEFAULT_ON, "init_on_alloc");
239 free_page((unsigned long)val);
242 void lkdtm_SLAB_FREE_DOUBLE(void)
246 val = kmem_cache_alloc(double_free_cache, GFP_KERNEL);
248 pr_info("Unable to allocate double_free_cache memory.\n");
252 /* Just make sure we got real memory. */
254 pr_info("Attempting double slab free ...\n");
255 kmem_cache_free(double_free_cache, val);
256 kmem_cache_free(double_free_cache, val);
259 void lkdtm_SLAB_FREE_CROSS(void)
263 val = kmem_cache_alloc(a_cache, GFP_KERNEL);
265 pr_info("Unable to allocate a_cache memory.\n");
269 /* Just make sure we got real memory. */
271 pr_info("Attempting cross-cache slab free ...\n");
272 kmem_cache_free(b_cache, val);
275 void lkdtm_SLAB_FREE_PAGE(void)
277 unsigned long p = __get_free_page(GFP_KERNEL);
279 pr_info("Attempting non-Slab slab free ...\n");
280 kmem_cache_free(NULL, (void *)p);
285 * We have constructors to keep the caches distinctly separated without
286 * needing to boot with "slab_nomerge".
288 static void ctor_double_free(void *region)
290 static void ctor_a(void *region)
292 static void ctor_b(void *region)
295 void __init lkdtm_heap_init(void)
297 double_free_cache = kmem_cache_create("lkdtm-heap-double_free",
298 64, 0, 0, ctor_double_free);
299 a_cache = kmem_cache_create("lkdtm-heap-a", 64, 0, 0, ctor_a);
300 b_cache = kmem_cache_create("lkdtm-heap-b", 64, 0, 0, ctor_b);
303 void __exit lkdtm_heap_exit(void)
305 kmem_cache_destroy(double_free_cache);
306 kmem_cache_destroy(a_cache);
307 kmem_cache_destroy(b_cache);