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>
13 #include <asm/sections.h>
15 /* Whether or not to fill the target memory area with do_nothing(). */
16 #define CODE_WRITE true
17 #define CODE_AS_IS false
19 /* How many bytes to copy to be sure we've copied enough of do_nothing(). */
22 /* This is non-const, so it will end up in the .data section. */
23 static u8 data_area
[EXEC_SIZE
];
25 /* This is const, so it will end up in the .rodata section. */
26 static const unsigned long rodata
= 0xAA55AA55;
28 /* This is marked __ro_after_init, so it should ultimately be .rodata. */
29 static unsigned long ro_after_init __ro_after_init
= 0x55AA5500;
32 * This just returns to the caller. It is designed to be copied into
33 * non-executable memory regions.
35 static noinline
void do_nothing(void)
40 /* Must immediately follow do_nothing for size calculuations to work out. */
41 static noinline
void do_overwritten(void)
43 pr_info("do_overwritten wasn't overwritten!\n");
47 static noinline
void do_almost_nothing(void)
49 pr_info("do_nothing was hijacked!\n");
52 static void *setup_function_descriptor(func_desc_t
*fdesc
, void *dst
)
54 if (!have_function_descriptors())
57 memcpy(fdesc
, do_nothing
, sizeof(*fdesc
));
58 fdesc
->addr
= (unsigned long)dst
;
64 static noinline __nocfi
void execute_location(void *dst
, bool write
)
68 void *do_nothing_text
= dereference_function_descriptor(do_nothing
);
70 pr_info("attempting ok execution at %px\n", do_nothing_text
);
73 if (write
== CODE_WRITE
) {
74 memcpy(dst
, do_nothing_text
, EXEC_SIZE
);
75 flush_icache_range((unsigned long)dst
,
76 (unsigned long)dst
+ EXEC_SIZE
);
78 pr_info("attempting bad execution at %px\n", dst
);
79 func
= setup_function_descriptor(&fdesc
, dst
);
81 pr_err("FAIL: func returned\n");
84 static void execute_user_location(void *dst
)
88 /* Intentionally crossing kernel/user memory boundary. */
91 void *do_nothing_text
= dereference_function_descriptor(do_nothing
);
93 pr_info("attempting ok execution at %px\n", do_nothing_text
);
96 copied
= access_process_vm(current
, (unsigned long)dst
, do_nothing_text
,
97 EXEC_SIZE
, FOLL_WRITE
);
98 if (copied
< EXEC_SIZE
)
100 pr_info("attempting bad execution at %px\n", dst
);
101 func
= setup_function_descriptor(&fdesc
, dst
);
103 pr_err("FAIL: func returned\n");
106 static void lkdtm_WRITE_RO(void)
108 /* Explicitly cast away "const" for the test and make volatile. */
109 volatile unsigned long *ptr
= (unsigned long *)&rodata
;
111 pr_info("attempting bad rodata write at %px\n", ptr
);
113 pr_err("FAIL: survived bad write\n");
116 static void lkdtm_WRITE_RO_AFTER_INIT(void)
118 volatile unsigned long *ptr
= &ro_after_init
;
121 * Verify we were written to during init. Since an Oops
122 * is considered a "success", a failure is to just skip the
125 if ((*ptr
& 0xAA) != 0xAA) {
126 pr_info("%p was NOT written during init!?\n", ptr
);
130 pr_info("attempting bad ro_after_init write at %px\n", ptr
);
132 pr_err("FAIL: survived bad write\n");
135 static void lkdtm_WRITE_KERN(void)
138 volatile unsigned char *ptr
;
140 size
= (unsigned long)dereference_function_descriptor(do_overwritten
) -
141 (unsigned long)dereference_function_descriptor(do_nothing
);
142 ptr
= dereference_function_descriptor(do_overwritten
);
144 pr_info("attempting bad %zu byte write at %px\n", size
, ptr
);
145 memcpy((void *)ptr
, (unsigned char *)do_nothing
, size
);
146 flush_icache_range((unsigned long)ptr
, (unsigned long)(ptr
+ size
));
147 pr_err("FAIL: survived bad write\n");
152 static void lkdtm_WRITE_OPD(void)
154 size_t size
= sizeof(func_desc_t
);
155 void (*func
)(void) = do_nothing
;
157 if (!have_function_descriptors()) {
158 pr_info("XFAIL: Platform doesn't use function descriptors.\n");
161 pr_info("attempting bad %zu bytes write at %px\n", size
, do_nothing
);
162 memcpy(do_nothing
, do_almost_nothing
, size
);
163 pr_err("FAIL: survived bad write\n");
165 asm("" : "=m"(func
));
169 static void lkdtm_EXEC_DATA(void)
171 execute_location(data_area
, CODE_WRITE
);
174 static void lkdtm_EXEC_STACK(void)
176 u8 stack_area
[EXEC_SIZE
];
177 execute_location(stack_area
, CODE_WRITE
);
180 static void lkdtm_EXEC_KMALLOC(void)
182 u32
*kmalloc_area
= kmalloc(EXEC_SIZE
, GFP_KERNEL
);
183 execute_location(kmalloc_area
, CODE_WRITE
);
187 static void lkdtm_EXEC_VMALLOC(void)
189 u32
*vmalloc_area
= vmalloc(EXEC_SIZE
);
190 execute_location(vmalloc_area
, CODE_WRITE
);
194 static void lkdtm_EXEC_RODATA(void)
196 execute_location(dereference_function_descriptor(lkdtm_rodata_do_nothing
),
200 static void lkdtm_EXEC_USERSPACE(void)
202 unsigned long user_addr
;
204 user_addr
= vm_mmap(NULL
, 0, PAGE_SIZE
,
205 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
206 MAP_ANONYMOUS
| MAP_PRIVATE
, 0);
207 if (user_addr
>= TASK_SIZE
) {
208 pr_warn("Failed to allocate user memory\n");
211 execute_user_location((void *)user_addr
);
212 vm_munmap(user_addr
, PAGE_SIZE
);
215 static void lkdtm_EXEC_NULL(void)
217 execute_location(NULL
, CODE_AS_IS
);
220 static void lkdtm_ACCESS_USERSPACE(void)
222 unsigned long user_addr
, tmp
= 0;
225 user_addr
= vm_mmap(NULL
, 0, PAGE_SIZE
,
226 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
227 MAP_ANONYMOUS
| MAP_PRIVATE
, 0);
228 if (user_addr
>= TASK_SIZE
) {
229 pr_warn("Failed to allocate user memory\n");
233 if (copy_to_user((void __user
*)user_addr
, &tmp
, sizeof(tmp
))) {
234 pr_warn("copy_to_user failed\n");
235 vm_munmap(user_addr
, PAGE_SIZE
);
239 ptr
= (unsigned long *)user_addr
;
241 pr_info("attempting bad read at %px\n", ptr
);
244 pr_err("FAIL: survived bad read\n");
246 pr_info("attempting bad write at %px\n", ptr
);
248 pr_err("FAIL: survived bad write\n");
250 vm_munmap(user_addr
, PAGE_SIZE
);
253 static void lkdtm_ACCESS_NULL(void)
256 volatile unsigned long *ptr
= (unsigned long *)NULL
;
258 pr_info("attempting bad read at %px\n", ptr
);
261 pr_err("FAIL: survived bad read\n");
263 pr_info("attempting bad write at %px\n", ptr
);
265 pr_err("FAIL: survived bad write\n");
268 void __init
lkdtm_perms_init(void)
270 /* Make sure we can write to __ro_after_init values during __init */
271 ro_after_init
|= 0xAA;
274 static struct crashtype crashtypes
[] = {
276 CRASHTYPE(WRITE_RO_AFTER_INIT
),
277 CRASHTYPE(WRITE_KERN
),
278 CRASHTYPE(WRITE_OPD
),
279 CRASHTYPE(EXEC_DATA
),
280 CRASHTYPE(EXEC_STACK
),
281 CRASHTYPE(EXEC_KMALLOC
),
282 CRASHTYPE(EXEC_VMALLOC
),
283 CRASHTYPE(EXEC_RODATA
),
284 CRASHTYPE(EXEC_USERSPACE
),
285 CRASHTYPE(EXEC_NULL
),
286 CRASHTYPE(ACCESS_USERSPACE
),
287 CRASHTYPE(ACCESS_NULL
),
290 struct crashtype_category perms_crashtypes
= {
291 .crashtypes
= crashtypes
,
292 .len
= ARRAY_SIZE(crashtypes
),