2 * This is for all the tests relating directly to heap memory, including
3 * page allocation and slab allocations.
6 #include <linux/slab.h>
7 #include <linux/sched.h>
10 * This tries to stay within the next largest power-of-2 kmalloc cache
11 * to avoid actually overwriting anything important if it's not detected
14 void lkdtm_OVERWRITE_ALLOCATION(void)
17 u32 *data = kmalloc(len, GFP_KERNEL);
19 data[1024 / sizeof(u32)] = 0x12345678;
23 void lkdtm_WRITE_AFTER_FREE(void)
28 * The slub allocator uses the first word to store the free
29 * pointer in some configurations. Use the middle of the
30 * allocation to avoid running into the freelist
32 size_t offset = (len / sizeof(*base)) / 2;
34 base = kmalloc(len, GFP_KERNEL);
35 pr_info("Allocated memory %p-%p\n", base, &base[offset * 2]);
36 pr_info("Attempting bad write to freed memory at %p\n",
39 base[offset] = 0x0abcdef0;
40 /* Attempt to notice the overwrite. */
41 again = kmalloc(len, GFP_KERNEL);
44 pr_info("Hmm, didn't get the same memory range.\n");
47 void lkdtm_READ_AFTER_FREE(void)
52 * The slub allocator uses the first word to store the free
53 * pointer in some configurations. Use the middle of the
54 * allocation to avoid running into the freelist
56 size_t offset = (len / sizeof(*base)) / 2;
58 base = kmalloc(len, GFP_KERNEL);
60 pr_info("Unable to allocate base memory.\n");
64 val = kmalloc(len, GFP_KERNEL);
66 pr_info("Unable to allocate val memory.\n");
73 pr_info("Value in memory before free: %x\n", base[offset]);
77 pr_info("Attempting bad read from freed memory\n");
80 /* Good! Poisoning happened, so declare a win. */
81 pr_info("Memory correctly poisoned (%x)\n", saw);
84 pr_info("Memory was not poisoned\n");
89 void lkdtm_WRITE_BUDDY_AFTER_FREE(void)
91 unsigned long p = __get_free_page(GFP_KERNEL);
93 pr_info("Unable to allocate free page\n");
97 pr_info("Writing to the buddy page before free\n");
98 memset((void *)p, 0x3, PAGE_SIZE);
101 pr_info("Attempting bad write to the buddy page after free\n");
102 memset((void *)p, 0x78, PAGE_SIZE);
103 /* Attempt to notice the overwrite. */
104 p = __get_free_page(GFP_KERNEL);
109 void lkdtm_READ_BUDDY_AFTER_FREE(void)
111 unsigned long p = __get_free_page(GFP_KERNEL);
116 pr_info("Unable to allocate free page\n");
120 val = kmalloc(1024, GFP_KERNEL);
122 pr_info("Unable to allocate val memory.\n");
131 pr_info("Value in memory before free: %x\n", base[0]);
133 pr_info("Attempting to read from freed memory\n");
136 /* Good! Poisoning happened, so declare a win. */
137 pr_info("Memory correctly poisoned (%x)\n", saw);
140 pr_info("Buddy page was not poisoned\n");