x86: fix section mismatch in head_64.S:initial_code
[sfrench/cifs-2.6.git] / arch / x86 / kernel / test_nx.c
1 /*
2  * test_nx.c: functional test for NX functionality
3  *
4  * (C) Copyright 2008 Intel Corporation
5  * Author: Arjan van de Ven <arjan@linux.intel.com>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; version 2
10  * of the License.
11  */
12 #include <linux/module.h>
13 #include <linux/sort.h>
14 #include <asm/uaccess.h>
15 #include <asm/asm.h>
16
17 extern int rodata_test_data;
18
19 /*
20  * This file checks 4 things:
21  * 1) Check if the stack is not executable
22  * 2) Check if kmalloc memory is not executable
23  * 3) Check if the .rodata section is not executable
24  * 4) Check if the .data section of a module is not executable
25  *
26  * To do this, the test code tries to execute memory in stack/kmalloc/etc,
27  * and then checks if the expected trap happens.
28  *
29  * Sadly, this implies having a dynamic exception handling table entry.
30  * ... which can be done (and will make Rusty cry)... but it can only
31  * be done in a stand-alone module with only 1 entry total.
32  * (otherwise we'd have to sort and that's just too messy)
33  */
34
35
36
37 /*
38  * We want to set up an exception handling point on our stack,
39  * which means a variable value. This function is rather dirty
40  * and walks the exception table of the module, looking for a magic
41  * marker and replaces it with a specific function.
42  */
43 static void fudze_exception_table(void *marker, void *new)
44 {
45         struct module *mod = THIS_MODULE;
46         struct exception_table_entry *extable;
47
48         /*
49          * Note: This module has only 1 exception table entry,
50          * so searching and sorting is not needed. If that changes,
51          * this would be the place to search and re-sort the exception
52          * table.
53          */
54         if (mod->num_exentries > 1) {
55                 printk(KERN_ERR "test_nx: too many exception table entries!\n");
56                 printk(KERN_ERR "test_nx: test results are not reliable.\n");
57                 return;
58         }
59         extable = (struct exception_table_entry *)mod->extable;
60         extable[0].insn = (unsigned long)new;
61 }
62
63
64 /*
65  * exception tables get their symbols translated so we need
66  * to use a fake function to put in there, which we can then
67  * replace at runtime.
68  */
69 void foo_label(void);
70
71 /*
72  * returns 0 for not-executable, negative for executable
73  *
74  * Note: we cannot allow this function to be inlined, because
75  * that would give us more than 1 exception table entry.
76  * This in turn would break the assumptions above.
77  */
78 static noinline int test_address(void *address)
79 {
80         unsigned long result;
81
82         /* Set up an exception table entry for our address */
83         fudze_exception_table(&foo_label, address);
84         result = 1;
85         asm volatile(
86                 "foo_label:\n"
87                 "0:     call *%[fake_code]\n"
88                 "1:\n"
89                 ".section .fixup,\"ax\"\n"
90                 "2:     mov %[zero], %[rslt]\n"
91                 "       ret\n"
92                 ".previous\n"
93                 _ASM_EXTABLE(0b,2b)
94                 : [rslt] "=r" (result)
95                 : [fake_code] "r" (address), [zero] "r" (0UL), "0" (result)
96         );
97         /* change the exception table back for the next round */
98         fudze_exception_table(address, &foo_label);
99
100         if (result)
101                 return -ENODEV;
102         return 0;
103 }
104
105 static unsigned char test_data = 0xC3; /* 0xC3 is the opcode for "ret" */
106
107 static int test_NX(void)
108 {
109         int ret = 0;
110         /* 0xC3 is the opcode for "ret" */
111         char stackcode[] = {0xC3, 0x90, 0 };
112         char *heap;
113
114         test_data = 0xC3;
115
116         printk(KERN_INFO "Testing NX protection\n");
117
118         /* Test 1: check if the stack is not executable */
119         if (test_address(&stackcode)) {
120                 printk(KERN_ERR "test_nx: stack was executable\n");
121                 ret = -ENODEV;
122         }
123
124
125         /* Test 2: Check if the heap is executable */
126         heap = kmalloc(64, GFP_KERNEL);
127         if (!heap)
128                 return -ENOMEM;
129         heap[0] = 0xC3; /* opcode for "ret" */
130
131         if (test_address(heap)) {
132                 printk(KERN_ERR "test_nx: heap was executable\n");
133                 ret = -ENODEV;
134         }
135         kfree(heap);
136
137         /*
138          * The following 2 tests currently fail, this needs to get fixed
139          * Until then, don't run them to avoid too many people getting scared
140          * by the error message
141          */
142
143 #ifdef CONFIG_DEBUG_RODATA
144         /* Test 3: Check if the .rodata section is executable */
145         if (rodata_test_data != 0xC3) {
146                 printk(KERN_ERR "test_nx: .rodata marker has invalid value\n");
147                 ret = -ENODEV;
148         } else if (test_address(&rodata_test_data)) {
149                 printk(KERN_ERR "test_nx: .rodata section is executable\n");
150                 ret = -ENODEV;
151         }
152 #endif
153
154 #if 0
155         /* Test 4: Check if the .data section of a module is executable */
156         if (test_address(&test_data)) {
157                 printk(KERN_ERR "test_nx: .data section is executable\n");
158                 ret = -ENODEV;
159         }
160
161 #endif
162         return 0;
163 }
164
165 static void test_exit(void)
166 {
167 }
168
169 module_init(test_NX);
170 module_exit(test_exit);
171 MODULE_LICENSE("GPL");
172 MODULE_DESCRIPTION("Testcase for the NX infrastructure");
173 MODULE_AUTHOR("Arjan van de Ven <arjan@linux.intel.com>");