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 uses the first word to store the free
62 * pointer in some configurations. Use the middle of the
63 * allocation to avoid running into the freelist
65 size_t offset
= (len
/ sizeof(*base
)) / 2;
67 base
= kmalloc(len
, GFP_KERNEL
);
69 pr_info("Unable to allocate base memory.\n");
73 val
= kmalloc(len
, GFP_KERNEL
);
75 pr_info("Unable to allocate val memory.\n");
82 pr_info("Value in memory before free: %x\n", base
[offset
]);
86 pr_info("Attempting bad read from freed memory\n");
89 /* Good! Poisoning happened, so declare a win. */
90 pr_info("Memory correctly poisoned (%x)\n", saw
);
93 pr_info("Memory was not poisoned\n");
98 void lkdtm_WRITE_BUDDY_AFTER_FREE(void)
100 unsigned long p
= __get_free_page(GFP_KERNEL
);
102 pr_info("Unable to allocate free page\n");
106 pr_info("Writing to the buddy page before free\n");
107 memset((void *)p
, 0x3, PAGE_SIZE
);
110 pr_info("Attempting bad write to the buddy page after free\n");
111 memset((void *)p
, 0x78, PAGE_SIZE
);
112 /* Attempt to notice the overwrite. */
113 p
= __get_free_page(GFP_KERNEL
);
118 void lkdtm_READ_BUDDY_AFTER_FREE(void)
120 unsigned long p
= __get_free_page(GFP_KERNEL
);
125 pr_info("Unable to allocate free page\n");
129 val
= kmalloc(1024, GFP_KERNEL
);
131 pr_info("Unable to allocate val memory.\n");
140 pr_info("Value in memory before free: %x\n", base
[0]);
142 pr_info("Attempting to read from freed memory\n");
145 /* Good! Poisoning happened, so declare a win. */
146 pr_info("Memory correctly poisoned (%x)\n", saw
);
149 pr_info("Buddy page was not poisoned\n");
154 void lkdtm_SLAB_FREE_DOUBLE(void)
158 val
= kmem_cache_alloc(double_free_cache
, GFP_KERNEL
);
160 pr_info("Unable to allocate double_free_cache memory.\n");
164 /* Just make sure we got real memory. */
166 pr_info("Attempting double slab free ...\n");
167 kmem_cache_free(double_free_cache
, val
);
168 kmem_cache_free(double_free_cache
, val
);
171 void lkdtm_SLAB_FREE_CROSS(void)
175 val
= kmem_cache_alloc(a_cache
, GFP_KERNEL
);
177 pr_info("Unable to allocate a_cache memory.\n");
181 /* Just make sure we got real memory. */
183 pr_info("Attempting cross-cache slab free ...\n");
184 kmem_cache_free(b_cache
, val
);
187 void lkdtm_SLAB_FREE_PAGE(void)
189 unsigned long p
= __get_free_page(GFP_KERNEL
);
191 pr_info("Attempting non-Slab slab free ...\n");
192 kmem_cache_free(NULL
, (void *)p
);
197 * We have constructors to keep the caches distinctly separated without
198 * needing to boot with "slab_nomerge".
200 static void ctor_double_free(void *region
)
202 static void ctor_a(void *region
)
204 static void ctor_b(void *region
)
207 void __init
lkdtm_heap_init(void)
209 double_free_cache
= kmem_cache_create("lkdtm-heap-double_free",
210 64, 0, 0, ctor_double_free
);
211 a_cache
= kmem_cache_create("lkdtm-heap-a", 64, 0, 0, ctor_a
);
212 b_cache
= kmem_cache_create("lkdtm-heap-b", 64, 0, 0, ctor_b
);
215 void __exit
lkdtm_heap_exit(void)
217 kmem_cache_destroy(double_free_cache
);
218 kmem_cache_destroy(a_cache
);
219 kmem_cache_destroy(b_cache
);