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 data[1024 / sizeof(u32)] = 0x12345678;
63 static void lkdtm_WRITE_AFTER_FREE(void)
68 * The slub allocator uses the first word to store the free
69 * pointer in some configurations. Use the middle of the
70 * allocation to avoid running into the freelist
72 size_t offset = (len / sizeof(*base)) / 2;
74 base = kmalloc(len, GFP_KERNEL);
77 pr_info("Allocated memory %p-%p\n", base, &base[offset * 2]);
78 pr_info("Attempting bad write to freed memory at %p\n",
81 base[offset] = 0x0abcdef0;
82 /* Attempt to notice the overwrite. */
83 again = kmalloc(len, GFP_KERNEL);
86 pr_info("Hmm, didn't get the same memory range.\n");
89 static void lkdtm_READ_AFTER_FREE(void)
94 * The slub allocator will use the either the first word or
95 * the middle of the allocation to store the free pointer,
96 * depending on configurations. Store in the second word to
97 * avoid running into the freelist.
99 size_t offset = sizeof(*base);
101 base = kmalloc(len, GFP_KERNEL);
103 pr_info("Unable to allocate base memory.\n");
107 val = kmalloc(len, GFP_KERNEL);
109 pr_info("Unable to allocate val memory.\n");
116 pr_info("Value in memory before free: %x\n", base[offset]);
120 pr_info("Attempting bad read from freed memory\n");
123 /* Good! Poisoning happened, so declare a win. */
124 pr_info("Memory correctly poisoned (%x)\n", saw);
126 pr_err("FAIL: Memory was not poisoned!\n");
127 pr_expected_config_param(CONFIG_INIT_ON_FREE_DEFAULT_ON, "init_on_free");
133 static void lkdtm_WRITE_BUDDY_AFTER_FREE(void)
135 unsigned long p = __get_free_page(GFP_KERNEL);
137 pr_info("Unable to allocate free page\n");
141 pr_info("Writing to the buddy page before free\n");
142 memset((void *)p, 0x3, PAGE_SIZE);
145 pr_info("Attempting bad write to the buddy page after free\n");
146 memset((void *)p, 0x78, PAGE_SIZE);
147 /* Attempt to notice the overwrite. */
148 p = __get_free_page(GFP_KERNEL);
153 static void lkdtm_READ_BUDDY_AFTER_FREE(void)
155 unsigned long p = __get_free_page(GFP_KERNEL);
160 pr_info("Unable to allocate free page\n");
164 val = kmalloc(1024, GFP_KERNEL);
166 pr_info("Unable to allocate val memory.\n");
175 pr_info("Value in memory before free: %x\n", base[0]);
177 pr_info("Attempting to read from freed memory\n");
180 /* Good! Poisoning happened, so declare a win. */
181 pr_info("Memory correctly poisoned (%x)\n", saw);
183 pr_err("FAIL: Buddy page was not poisoned!\n");
184 pr_expected_config_param(CONFIG_INIT_ON_FREE_DEFAULT_ON, "init_on_free");
190 static void lkdtm_SLAB_INIT_ON_ALLOC(void)
195 first = kmalloc(512, GFP_KERNEL);
197 pr_info("Unable to allocate 512 bytes the first time.\n");
201 memset(first, 0xAB, 512);
204 val = kmalloc(512, GFP_KERNEL);
206 pr_info("Unable to allocate 512 bytes the second time.\n");
210 pr_warn("Reallocation missed clobbered memory.\n");
213 if (memchr(val, 0xAB, 512) == NULL) {
214 pr_info("Memory appears initialized (%x, no earlier values)\n", *val);
216 pr_err("FAIL: Slab was not initialized\n");
217 pr_expected_config_param(CONFIG_INIT_ON_ALLOC_DEFAULT_ON, "init_on_alloc");
222 static void lkdtm_BUDDY_INIT_ON_ALLOC(void)
227 first = (u8 *)__get_free_page(GFP_KERNEL);
229 pr_info("Unable to allocate first free page\n");
233 memset(first, 0xAB, PAGE_SIZE);
234 free_page((unsigned long)first);
236 val = (u8 *)__get_free_page(GFP_KERNEL);
238 pr_info("Unable to allocate second free page\n");
243 pr_warn("Reallocation missed clobbered memory.\n");
246 if (memchr(val, 0xAB, PAGE_SIZE) == NULL) {
247 pr_info("Memory appears initialized (%x, no earlier values)\n", *val);
249 pr_err("FAIL: Slab was not initialized\n");
250 pr_expected_config_param(CONFIG_INIT_ON_ALLOC_DEFAULT_ON, "init_on_alloc");
252 free_page((unsigned long)val);
255 static void lkdtm_SLAB_FREE_DOUBLE(void)
259 val = kmem_cache_alloc(double_free_cache, GFP_KERNEL);
261 pr_info("Unable to allocate double_free_cache memory.\n");
265 /* Just make sure we got real memory. */
267 pr_info("Attempting double slab free ...\n");
268 kmem_cache_free(double_free_cache, val);
269 kmem_cache_free(double_free_cache, val);
272 static void lkdtm_SLAB_FREE_CROSS(void)
276 val = kmem_cache_alloc(a_cache, GFP_KERNEL);
278 pr_info("Unable to allocate a_cache memory.\n");
282 /* Just make sure we got real memory. */
284 pr_info("Attempting cross-cache slab free ...\n");
285 kmem_cache_free(b_cache, val);
288 static void lkdtm_SLAB_FREE_PAGE(void)
290 unsigned long p = __get_free_page(GFP_KERNEL);
292 pr_info("Attempting non-Slab slab free ...\n");
293 kmem_cache_free(NULL, (void *)p);
298 * We have constructors to keep the caches distinctly separated without
299 * needing to boot with "slab_nomerge".
301 static void ctor_double_free(void *region)
303 static void ctor_a(void *region)
305 static void ctor_b(void *region)
308 void __init lkdtm_heap_init(void)
310 double_free_cache = kmem_cache_create("lkdtm-heap-double_free",
311 64, 0, 0, ctor_double_free);
312 a_cache = kmem_cache_create("lkdtm-heap-a", 64, 0, 0, ctor_a);
313 b_cache = kmem_cache_create("lkdtm-heap-b", 64, 0, 0, ctor_b);
316 void __exit lkdtm_heap_exit(void)
318 kmem_cache_destroy(double_free_cache);
319 kmem_cache_destroy(a_cache);
320 kmem_cache_destroy(b_cache);
323 static struct crashtype crashtypes[] = {
324 CRASHTYPE(SLAB_LINEAR_OVERFLOW),
325 CRASHTYPE(VMALLOC_LINEAR_OVERFLOW),
326 CRASHTYPE(WRITE_AFTER_FREE),
327 CRASHTYPE(READ_AFTER_FREE),
328 CRASHTYPE(WRITE_BUDDY_AFTER_FREE),
329 CRASHTYPE(READ_BUDDY_AFTER_FREE),
330 CRASHTYPE(SLAB_INIT_ON_ALLOC),
331 CRASHTYPE(BUDDY_INIT_ON_ALLOC),
332 CRASHTYPE(SLAB_FREE_DOUBLE),
333 CRASHTYPE(SLAB_FREE_CROSS),
334 CRASHTYPE(SLAB_FREE_PAGE),
337 struct crashtype_category heap_crashtypes = {
338 .crashtypes = crashtypes,
339 .len = ARRAY_SIZE(crashtypes),