MIPS: perf: fix deadlock
[linux-stable.git] / drivers / misc / lkdtm_perms.c
blobc7635a79341fb3e52d527135db2a1c958cc7e087
1 /*
2 * This is for all the tests related to validating kernel memory
3 * permissions: non-executable regions, non-writable regions, and
4 * even non-readable regions.
5 */
6 #include "lkdtm.h"
7 #include <linux/slab.h>
8 #include <linux/vmalloc.h>
9 #include <linux/mman.h>
10 #include <linux/uaccess.h>
11 #include <asm/cacheflush.h>
13 /* Whether or not to fill the target memory area with do_nothing(). */
14 #define CODE_WRITE true
15 #define CODE_AS_IS false
17 /* How many bytes to copy to be sure we've copied enough of do_nothing(). */
18 #define EXEC_SIZE 64
20 /* This is non-const, so it will end up in the .data section. */
21 static u8 data_area[EXEC_SIZE];
23 /* This is cost, so it will end up in the .rodata section. */
24 static const unsigned long rodata = 0xAA55AA55;
26 /* This is marked __ro_after_init, so it should ultimately be .rodata. */
27 static unsigned long ro_after_init __ro_after_init = 0x55AA5500;
30 * This just returns to the caller. It is designed to be copied into
31 * non-executable memory regions.
33 static void do_nothing(void)
35 return;
38 /* Must immediately follow do_nothing for size calculuations to work out. */
39 static void do_overwritten(void)
41 pr_info("do_overwritten wasn't overwritten!\n");
42 return;
45 static noinline void execute_location(void *dst, bool write)
47 void (*func)(void) = dst;
49 pr_info("attempting ok execution at %p\n", do_nothing);
50 do_nothing();
52 if (write == CODE_WRITE) {
53 memcpy(dst, do_nothing, EXEC_SIZE);
54 flush_icache_range((unsigned long)dst,
55 (unsigned long)dst + EXEC_SIZE);
57 pr_info("attempting bad execution at %p\n", func);
58 func();
61 static void execute_user_location(void *dst)
63 int copied;
65 /* Intentionally crossing kernel/user memory boundary. */
66 void (*func)(void) = dst;
68 pr_info("attempting ok execution at %p\n", do_nothing);
69 do_nothing();
71 copied = access_process_vm(current, (unsigned long)dst, do_nothing,
72 EXEC_SIZE, FOLL_WRITE);
73 if (copied < EXEC_SIZE)
74 return;
75 pr_info("attempting bad execution at %p\n", func);
76 func();
79 void lkdtm_WRITE_RO(void)
81 /* Explicitly cast away "const" for the test. */
82 unsigned long *ptr = (unsigned long *)&rodata;
84 pr_info("attempting bad rodata write at %p\n", ptr);
85 *ptr ^= 0xabcd1234;
88 void lkdtm_WRITE_RO_AFTER_INIT(void)
90 unsigned long *ptr = &ro_after_init;
93 * Verify we were written to during init. Since an Oops
94 * is considered a "success", a failure is to just skip the
95 * real test.
97 if ((*ptr & 0xAA) != 0xAA) {
98 pr_info("%p was NOT written during init!?\n", ptr);
99 return;
102 pr_info("attempting bad ro_after_init write at %p\n", ptr);
103 *ptr ^= 0xabcd1234;
106 void lkdtm_WRITE_KERN(void)
108 size_t size;
109 unsigned char *ptr;
111 size = (unsigned long)do_overwritten - (unsigned long)do_nothing;
112 ptr = (unsigned char *)do_overwritten;
114 pr_info("attempting bad %zu byte write at %p\n", size, ptr);
115 memcpy(ptr, (unsigned char *)do_nothing, size);
116 flush_icache_range((unsigned long)ptr, (unsigned long)(ptr + size));
118 do_overwritten();
121 void lkdtm_EXEC_DATA(void)
123 execute_location(data_area, CODE_WRITE);
126 void lkdtm_EXEC_STACK(void)
128 u8 stack_area[EXEC_SIZE];
129 execute_location(stack_area, CODE_WRITE);
132 void lkdtm_EXEC_KMALLOC(void)
134 u32 *kmalloc_area = kmalloc(EXEC_SIZE, GFP_KERNEL);
135 execute_location(kmalloc_area, CODE_WRITE);
136 kfree(kmalloc_area);
139 void lkdtm_EXEC_VMALLOC(void)
141 u32 *vmalloc_area = vmalloc(EXEC_SIZE);
142 execute_location(vmalloc_area, CODE_WRITE);
143 vfree(vmalloc_area);
146 void lkdtm_EXEC_RODATA(void)
148 execute_location(lkdtm_rodata_do_nothing, CODE_AS_IS);
151 void lkdtm_EXEC_USERSPACE(void)
153 unsigned long user_addr;
155 user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
156 PROT_READ | PROT_WRITE | PROT_EXEC,
157 MAP_ANONYMOUS | MAP_PRIVATE, 0);
158 if (user_addr >= TASK_SIZE) {
159 pr_warn("Failed to allocate user memory\n");
160 return;
162 execute_user_location((void *)user_addr);
163 vm_munmap(user_addr, PAGE_SIZE);
166 void lkdtm_ACCESS_USERSPACE(void)
168 unsigned long user_addr, tmp = 0;
169 unsigned long *ptr;
171 user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
172 PROT_READ | PROT_WRITE | PROT_EXEC,
173 MAP_ANONYMOUS | MAP_PRIVATE, 0);
174 if (user_addr >= TASK_SIZE) {
175 pr_warn("Failed to allocate user memory\n");
176 return;
179 if (copy_to_user((void __user *)user_addr, &tmp, sizeof(tmp))) {
180 pr_warn("copy_to_user failed\n");
181 vm_munmap(user_addr, PAGE_SIZE);
182 return;
185 ptr = (unsigned long *)user_addr;
187 pr_info("attempting bad read at %p\n", ptr);
188 tmp = *ptr;
189 tmp += 0xc0dec0de;
191 pr_info("attempting bad write at %p\n", ptr);
192 *ptr = tmp;
194 vm_munmap(user_addr, PAGE_SIZE);
197 void __init lkdtm_perms_init(void)
199 /* Make sure we can write to __ro_after_init values during __init */
200 ro_after_init |= 0xAA;