WIP FPC-III support
[linux/fpc-iii.git] / drivers / misc / lkdtm / perms.c
blob2dede2ef658f3b365597291f40afd0702766a8c1
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * This is for all the tests related to validating kernel memory
4 * permissions: non-executable regions, non-writable regions, and
5 * even non-readable regions.
6 */
7 #include "lkdtm.h"
8 #include <linux/slab.h>
9 #include <linux/vmalloc.h>
10 #include <linux/mman.h>
11 #include <linux/uaccess.h>
12 #include <asm/cacheflush.h>
14 /* Whether or not to fill the target memory area with do_nothing(). */
15 #define CODE_WRITE true
16 #define CODE_AS_IS false
18 /* How many bytes to copy to be sure we've copied enough of do_nothing(). */
19 #define EXEC_SIZE 64
21 /* This is non-const, so it will end up in the .data section. */
22 static u8 data_area[EXEC_SIZE];
24 /* This is cost, so it will end up in the .rodata section. */
25 static const unsigned long rodata = 0xAA55AA55;
27 /* This is marked __ro_after_init, so it should ultimately be .rodata. */
28 static unsigned long ro_after_init __ro_after_init = 0x55AA5500;
31 * This just returns to the caller. It is designed to be copied into
32 * non-executable memory regions.
34 static void do_nothing(void)
36 return;
39 /* Must immediately follow do_nothing for size calculuations to work out. */
40 static void do_overwritten(void)
42 pr_info("do_overwritten wasn't overwritten!\n");
43 return;
46 static noinline void execute_location(void *dst, bool write)
48 void (*func)(void) = dst;
50 pr_info("attempting ok execution at %px\n", do_nothing);
51 do_nothing();
53 if (write == CODE_WRITE) {
54 memcpy(dst, do_nothing, EXEC_SIZE);
55 flush_icache_range((unsigned long)dst,
56 (unsigned long)dst + EXEC_SIZE);
58 pr_info("attempting bad execution at %px\n", func);
59 func();
60 pr_err("FAIL: func returned\n");
63 static void execute_user_location(void *dst)
65 int copied;
67 /* Intentionally crossing kernel/user memory boundary. */
68 void (*func)(void) = dst;
70 pr_info("attempting ok execution at %px\n", do_nothing);
71 do_nothing();
73 copied = access_process_vm(current, (unsigned long)dst, do_nothing,
74 EXEC_SIZE, FOLL_WRITE);
75 if (copied < EXEC_SIZE)
76 return;
77 pr_info("attempting bad execution at %px\n", func);
78 func();
79 pr_err("FAIL: func returned\n");
82 void lkdtm_WRITE_RO(void)
84 /* Explicitly cast away "const" for the test and make volatile. */
85 volatile unsigned long *ptr = (unsigned long *)&rodata;
87 pr_info("attempting bad rodata write at %px\n", ptr);
88 *ptr ^= 0xabcd1234;
89 pr_err("FAIL: survived bad write\n");
92 void lkdtm_WRITE_RO_AFTER_INIT(void)
94 volatile unsigned long *ptr = &ro_after_init;
97 * Verify we were written to during init. Since an Oops
98 * is considered a "success", a failure is to just skip the
99 * real test.
101 if ((*ptr & 0xAA) != 0xAA) {
102 pr_info("%p was NOT written during init!?\n", ptr);
103 return;
106 pr_info("attempting bad ro_after_init write at %px\n", ptr);
107 *ptr ^= 0xabcd1234;
108 pr_err("FAIL: survived bad write\n");
111 void lkdtm_WRITE_KERN(void)
113 size_t size;
114 volatile unsigned char *ptr;
116 size = (unsigned long)do_overwritten - (unsigned long)do_nothing;
117 ptr = (unsigned char *)do_overwritten;
119 pr_info("attempting bad %zu byte write at %px\n", size, ptr);
120 memcpy((void *)ptr, (unsigned char *)do_nothing, size);
121 flush_icache_range((unsigned long)ptr, (unsigned long)(ptr + size));
122 pr_err("FAIL: survived bad write\n");
124 do_overwritten();
127 void lkdtm_EXEC_DATA(void)
129 execute_location(data_area, CODE_WRITE);
132 void lkdtm_EXEC_STACK(void)
134 u8 stack_area[EXEC_SIZE];
135 execute_location(stack_area, CODE_WRITE);
138 void lkdtm_EXEC_KMALLOC(void)
140 u32 *kmalloc_area = kmalloc(EXEC_SIZE, GFP_KERNEL);
141 execute_location(kmalloc_area, CODE_WRITE);
142 kfree(kmalloc_area);
145 void lkdtm_EXEC_VMALLOC(void)
147 u32 *vmalloc_area = vmalloc(EXEC_SIZE);
148 execute_location(vmalloc_area, CODE_WRITE);
149 vfree(vmalloc_area);
152 void lkdtm_EXEC_RODATA(void)
154 execute_location(lkdtm_rodata_do_nothing, CODE_AS_IS);
157 void lkdtm_EXEC_USERSPACE(void)
159 unsigned long user_addr;
161 user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
162 PROT_READ | PROT_WRITE | PROT_EXEC,
163 MAP_ANONYMOUS | MAP_PRIVATE, 0);
164 if (user_addr >= TASK_SIZE) {
165 pr_warn("Failed to allocate user memory\n");
166 return;
168 execute_user_location((void *)user_addr);
169 vm_munmap(user_addr, PAGE_SIZE);
172 void lkdtm_EXEC_NULL(void)
174 execute_location(NULL, CODE_AS_IS);
177 void lkdtm_ACCESS_USERSPACE(void)
179 unsigned long user_addr, tmp = 0;
180 unsigned long *ptr;
182 user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
183 PROT_READ | PROT_WRITE | PROT_EXEC,
184 MAP_ANONYMOUS | MAP_PRIVATE, 0);
185 if (user_addr >= TASK_SIZE) {
186 pr_warn("Failed to allocate user memory\n");
187 return;
190 if (copy_to_user((void __user *)user_addr, &tmp, sizeof(tmp))) {
191 pr_warn("copy_to_user failed\n");
192 vm_munmap(user_addr, PAGE_SIZE);
193 return;
196 ptr = (unsigned long *)user_addr;
198 pr_info("attempting bad read at %px\n", ptr);
199 tmp = *ptr;
200 tmp += 0xc0dec0de;
201 pr_err("FAIL: survived bad read\n");
203 pr_info("attempting bad write at %px\n", ptr);
204 *ptr = tmp;
205 pr_err("FAIL: survived bad write\n");
207 vm_munmap(user_addr, PAGE_SIZE);
210 void lkdtm_ACCESS_NULL(void)
212 unsigned long tmp;
213 volatile unsigned long *ptr = (unsigned long *)NULL;
215 pr_info("attempting bad read at %px\n", ptr);
216 tmp = *ptr;
217 tmp += 0xc0dec0de;
218 pr_err("FAIL: survived bad read\n");
220 pr_info("attempting bad write at %px\n", ptr);
221 *ptr = tmp;
222 pr_err("FAIL: survived bad write\n");
225 void __init lkdtm_perms_init(void)
227 /* Make sure we can write to __ro_after_init values during __init */
228 ro_after_init |= 0xAA;