1
2
3
4
5
6
7
8
9
10
11
12#include <linux/module.h>
13#include <linux/sort.h>
14#include <linux/slab.h>
15
16#include <asm/uaccess.h>
17#include <asm/asm.h>
18
19extern int rodata_test_data;
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45static void fudze_exception_table(void *marker, void *new)
46{
47 struct module *mod = THIS_MODULE;
48 struct exception_table_entry *extable;
49
50
51
52
53
54
55
56 if (mod->num_exentries > 1) {
57 printk(KERN_ERR "test_nx: too many exception table entries!\n");
58 printk(KERN_ERR "test_nx: test results are not reliable.\n");
59 return;
60 }
61 extable = (struct exception_table_entry *)mod->extable;
62 extable[0].insn = (unsigned long)new;
63}
64
65
66
67
68
69
70
71void foo_label(void);
72
73
74
75
76
77
78
79
80static noinline int test_address(void *address)
81{
82 unsigned long result;
83
84
85 fudze_exception_table(&foo_label, address);
86 result = 1;
87 asm volatile(
88 "foo_label:\n"
89 "0: call *%[fake_code]\n"
90 "1:\n"
91 ".section .fixup,\"ax\"\n"
92 "2: mov %[zero], %[rslt]\n"
93 " ret\n"
94 ".previous\n"
95 _ASM_EXTABLE(0b,2b)
96 : [rslt] "=r" (result)
97 : [fake_code] "r" (address), [zero] "r" (0UL), "0" (result)
98 );
99
100 fudze_exception_table(address, &foo_label);
101
102 if (result)
103 return -ENODEV;
104 return 0;
105}
106
107static unsigned char test_data = 0xC3;
108
109static int test_NX(void)
110{
111 int ret = 0;
112
113 char stackcode[] = {0xC3, 0x90, 0 };
114 char *heap;
115
116 test_data = 0xC3;
117
118 printk(KERN_INFO "Testing NX protection\n");
119
120
121 if (test_address(&stackcode)) {
122 printk(KERN_ERR "test_nx: stack was executable\n");
123 ret = -ENODEV;
124 }
125
126
127
128 heap = kmalloc(64, GFP_KERNEL);
129 if (!heap)
130 return -ENOMEM;
131 heap[0] = 0xC3;
132
133 if (test_address(heap)) {
134 printk(KERN_ERR "test_nx: heap was executable\n");
135 ret = -ENODEV;
136 }
137 kfree(heap);
138
139
140
141
142
143
144
145#ifdef CONFIG_DEBUG_RODATA
146
147 if (rodata_test_data != 0xC3) {
148 printk(KERN_ERR "test_nx: .rodata marker has invalid value\n");
149 ret = -ENODEV;
150 } else if (test_address(&rodata_test_data)) {
151 printk(KERN_ERR "test_nx: .rodata section is executable\n");
152 ret = -ENODEV;
153 }
154#endif
155
156#if 0
157
158 if (test_address(&test_data)) {
159 printk(KERN_ERR "test_nx: .data section is executable\n");
160 ret = -ENODEV;
161 }
162
163#endif
164 return ret;
165}
166
167static void test_exit(void)
168{
169}
170
171module_init(test_NX);
172module_exit(test_exit);
173MODULE_LICENSE("GPL");
174MODULE_DESCRIPTION("Testcase for the NX infrastructure");
175MODULE_AUTHOR("Arjan van de Ven <arjan@linux.intel.com>");
176