blob: 12f50e8dcbfe0be294b23a9dd987c5d11d8557a8 [file] [log] [blame]
Kees Cookffc514f2016-06-26 21:45:23 -07001/*
2 * This is for all the tests relating directly to heap memory, including
3 * page allocation and slab allocations.
4 */
5#define pr_fmt(fmt) "lkdtm: " fmt
6
7#include <linux/kernel.h>
8#include <linux/slab.h>
9
10#include "lkdtm.h"
11
12/*
13 * This tries to stay within the next largest power-of-2 kmalloc cache
14 * to avoid actually overwriting anything important if it's not detected
15 * correctly.
16 */
17void lkdtm_OVERWRITE_ALLOCATION(void)
18{
19 size_t len = 1020;
20 u32 *data = kmalloc(len, GFP_KERNEL);
21
22 data[1024 / sizeof(u32)] = 0x12345678;
23 kfree(data);
24}
25
26void lkdtm_WRITE_AFTER_FREE(void)
27{
28 int *base, *again;
29 size_t len = 1024;
30 /*
31 * The slub allocator uses the first word to store the free
32 * pointer in some configurations. Use the middle of the
33 * allocation to avoid running into the freelist
34 */
35 size_t offset = (len / sizeof(*base)) / 2;
36
37 base = kmalloc(len, GFP_KERNEL);
38 pr_info("Allocated memory %p-%p\n", base, &base[offset * 2]);
39 pr_info("Attempting bad write to freed memory at %p\n",
40 &base[offset]);
41 kfree(base);
42 base[offset] = 0x0abcdef0;
43 /* Attempt to notice the overwrite. */
44 again = kmalloc(len, GFP_KERNEL);
45 kfree(again);
46 if (again != base)
47 pr_info("Hmm, didn't get the same memory range.\n");
48}
49
50void lkdtm_READ_AFTER_FREE(void)
51{
52 int *base, *val, saw;
53 size_t len = 1024;
54 /*
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
58 */
59 size_t offset = (len / sizeof(*base)) / 2;
60
61 base = kmalloc(len, GFP_KERNEL);
62 if (!base) {
63 pr_info("Unable to allocate base memory.\n");
64 return;
65 }
66
67 val = kmalloc(len, GFP_KERNEL);
68 if (!val) {
69 pr_info("Unable to allocate val memory.\n");
70 kfree(base);
71 return;
72 }
73
74 *val = 0x12345678;
75 base[offset] = *val;
76 pr_info("Value in memory before free: %x\n", base[offset]);
77
78 kfree(base);
79
80 pr_info("Attempting bad read from freed memory\n");
81 saw = base[offset];
82 if (saw != *val) {
83 /* Good! Poisoning happened, so declare a win. */
84 pr_info("Memory correctly poisoned (%x)\n", saw);
85 BUG();
86 }
87 pr_info("Memory was not poisoned\n");
88
89 kfree(val);
90}
91
92void lkdtm_WRITE_BUDDY_AFTER_FREE(void)
93{
94 unsigned long p = __get_free_page(GFP_KERNEL);
95 if (!p) {
96 pr_info("Unable to allocate free page\n");
97 return;
98 }
99
100 pr_info("Writing to the buddy page before free\n");
101 memset((void *)p, 0x3, PAGE_SIZE);
102 free_page(p);
103 schedule();
104 pr_info("Attempting bad write to the buddy page after free\n");
105 memset((void *)p, 0x78, PAGE_SIZE);
106 /* Attempt to notice the overwrite. */
107 p = __get_free_page(GFP_KERNEL);
108 free_page(p);
109 schedule();
110}
111
112void lkdtm_READ_BUDDY_AFTER_FREE(void)
113{
114 unsigned long p = __get_free_page(GFP_KERNEL);
115 int saw, *val;
116 int *base;
117
118 if (!p) {
119 pr_info("Unable to allocate free page\n");
120 return;
121 }
122
123 val = kmalloc(1024, GFP_KERNEL);
124 if (!val) {
125 pr_info("Unable to allocate val memory.\n");
126 free_page(p);
127 return;
128 }
129
130 base = (int *)p;
131
132 *val = 0x12345678;
133 base[0] = *val;
134 pr_info("Value in memory before free: %x\n", base[0]);
135 free_page(p);
136 pr_info("Attempting to read from freed memory\n");
137 saw = base[0];
138 if (saw != *val) {
139 /* Good! Poisoning happened, so declare a win. */
140 pr_info("Memory correctly poisoned (%x)\n", saw);
141 BUG();
142 }
143 pr_info("Buddy page was not poisoned\n");
144
145 kfree(val);
146}