2 * This is for all the tests relating directly to heap memory, including
3 * page allocation and slab allocations.
6 #include <linux/slab.h>
9 * This tries to stay within the next largest power-of-2 kmalloc cache
10 * to avoid actually overwriting anything important if it's not detected
13 void lkdtm_OVERWRITE_ALLOCATION(void)
16 u32
*data
= kmalloc(len
, GFP_KERNEL
);
18 data
[1024 / sizeof(u32
)] = 0x12345678;
22 void lkdtm_WRITE_AFTER_FREE(void)
27 * The slub allocator uses the first word to store the free
28 * pointer in some configurations. Use the middle of the
29 * allocation to avoid running into the freelist
31 size_t offset
= (len
/ sizeof(*base
)) / 2;
33 base
= kmalloc(len
, GFP_KERNEL
);
34 pr_info("Allocated memory %p-%p\n", base
, &base
[offset
* 2]);
35 pr_info("Attempting bad write to freed memory at %p\n",
38 base
[offset
] = 0x0abcdef0;
39 /* Attempt to notice the overwrite. */
40 again
= kmalloc(len
, GFP_KERNEL
);
43 pr_info("Hmm, didn't get the same memory range.\n");
46 void lkdtm_READ_AFTER_FREE(void)
51 * The slub allocator uses the first word to store the free
52 * pointer in some configurations. Use the middle of the
53 * allocation to avoid running into the freelist
55 size_t offset
= (len
/ sizeof(*base
)) / 2;
57 base
= kmalloc(len
, GFP_KERNEL
);
59 pr_info("Unable to allocate base memory.\n");
63 val
= kmalloc(len
, GFP_KERNEL
);
65 pr_info("Unable to allocate val memory.\n");
72 pr_info("Value in memory before free: %x\n", base
[offset
]);
76 pr_info("Attempting bad read from freed memory\n");
79 /* Good! Poisoning happened, so declare a win. */
80 pr_info("Memory correctly poisoned (%x)\n", saw
);
83 pr_info("Memory was not poisoned\n");
88 void lkdtm_WRITE_BUDDY_AFTER_FREE(void)
90 unsigned long p
= __get_free_page(GFP_KERNEL
);
92 pr_info("Unable to allocate free page\n");
96 pr_info("Writing to the buddy page before free\n");
97 memset((void *)p
, 0x3, PAGE_SIZE
);
100 pr_info("Attempting bad write to the buddy page after free\n");
101 memset((void *)p
, 0x78, PAGE_SIZE
);
102 /* Attempt to notice the overwrite. */
103 p
= __get_free_page(GFP_KERNEL
);
108 void lkdtm_READ_BUDDY_AFTER_FREE(void)
110 unsigned long p
= __get_free_page(GFP_KERNEL
);
115 pr_info("Unable to allocate free page\n");
119 val
= kmalloc(1024, GFP_KERNEL
);
121 pr_info("Unable to allocate val memory.\n");
130 pr_info("Value in memory before free: %x\n", base
[0]);
132 pr_info("Attempting to read from freed memory\n");
135 /* Good! Poisoning happened, so declare a win. */
136 pr_info("Memory correctly poisoned (%x)\n", saw
);
139 pr_info("Buddy page was not poisoned\n");