1 // SPDX-License-Identifier: GPL-2.0
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.
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(). */
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)
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");
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
);
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
);
60 pr_err("FAIL: func returned\n");
63 static void execute_user_location(void *dst
)
67 /* Intentionally crossing kernel/user memory boundary. */
68 void (*func
)(void) = dst
;
70 pr_info("attempting ok execution at %px\n", do_nothing
);
73 copied
= access_process_vm(current
, (unsigned long)dst
, do_nothing
,
74 EXEC_SIZE
, FOLL_WRITE
);
75 if (copied
< EXEC_SIZE
)
77 pr_info("attempting bad execution at %px\n", 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
);
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
101 if ((*ptr
& 0xAA) != 0xAA) {
102 pr_info("%p was NOT written during init!?\n", ptr
);
106 pr_info("attempting bad ro_after_init write at %px\n", ptr
);
108 pr_err("FAIL: survived bad write\n");
111 void lkdtm_WRITE_KERN(void)
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");
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
);
145 void lkdtm_EXEC_VMALLOC(void)
147 u32
*vmalloc_area
= vmalloc(EXEC_SIZE
);
148 execute_location(vmalloc_area
, CODE_WRITE
);
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");
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;
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");
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
);
196 ptr
= (unsigned long *)user_addr
;
198 pr_info("attempting bad read at %px\n", ptr
);
201 pr_err("FAIL: survived bad read\n");
203 pr_info("attempting bad write at %px\n", ptr
);
205 pr_err("FAIL: survived bad write\n");
207 vm_munmap(user_addr
, PAGE_SIZE
);
210 void lkdtm_ACCESS_NULL(void)
213 volatile unsigned long *ptr
= (unsigned long *)NULL
;
215 pr_info("attempting bad read at %px\n", ptr
);
218 pr_err("FAIL: survived bad read\n");
220 pr_info("attempting bad write at %px\n", ptr
);
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;