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.
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(). */
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)
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");
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
);
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
);
61 static void execute_user_location(void *dst
)
63 /* Intentionally crossing kernel/user memory boundary. */
64 void (*func
)(void) = dst
;
66 pr_info("attempting ok execution at %p\n", do_nothing
);
69 if (copy_to_user((void __user
*)dst
, do_nothing
, EXEC_SIZE
))
71 flush_icache_range((unsigned long)dst
, (unsigned long)dst
+ EXEC_SIZE
);
72 pr_info("attempting bad execution at %p\n", func
);
76 void lkdtm_WRITE_RO(void)
78 /* Explicitly cast away "const" for the test. */
79 unsigned long *ptr
= (unsigned long *)&rodata
;
81 pr_info("attempting bad rodata write at %p\n", ptr
);
85 void lkdtm_WRITE_RO_AFTER_INIT(void)
87 unsigned long *ptr
= &ro_after_init
;
90 * Verify we were written to during init. Since an Oops
91 * is considered a "success", a failure is to just skip the
94 if ((*ptr
& 0xAA) != 0xAA) {
95 pr_info("%p was NOT written during init!?\n", ptr
);
99 pr_info("attempting bad ro_after_init write at %p\n", ptr
);
103 void lkdtm_WRITE_KERN(void)
108 size
= (unsigned long)do_overwritten
- (unsigned long)do_nothing
;
109 ptr
= (unsigned char *)do_overwritten
;
111 pr_info("attempting bad %zu byte write at %p\n", size
, ptr
);
112 memcpy(ptr
, (unsigned char *)do_nothing
, size
);
113 flush_icache_range((unsigned long)ptr
, (unsigned long)(ptr
+ size
));
118 void lkdtm_EXEC_DATA(void)
120 execute_location(data_area
, CODE_WRITE
);
123 void lkdtm_EXEC_STACK(void)
125 u8 stack_area
[EXEC_SIZE
];
126 execute_location(stack_area
, CODE_WRITE
);
129 void lkdtm_EXEC_KMALLOC(void)
131 u32
*kmalloc_area
= kmalloc(EXEC_SIZE
, GFP_KERNEL
);
132 execute_location(kmalloc_area
, CODE_WRITE
);
136 void lkdtm_EXEC_VMALLOC(void)
138 u32
*vmalloc_area
= vmalloc(EXEC_SIZE
);
139 execute_location(vmalloc_area
, CODE_WRITE
);
143 void lkdtm_EXEC_RODATA(void)
145 execute_location(lkdtm_rodata_do_nothing
, CODE_AS_IS
);
148 void lkdtm_EXEC_USERSPACE(void)
150 unsigned long user_addr
;
152 user_addr
= vm_mmap(NULL
, 0, PAGE_SIZE
,
153 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
154 MAP_ANONYMOUS
| MAP_PRIVATE
, 0);
155 if (user_addr
>= TASK_SIZE
) {
156 pr_warn("Failed to allocate user memory\n");
159 execute_user_location((void *)user_addr
);
160 vm_munmap(user_addr
, PAGE_SIZE
);
163 void lkdtm_ACCESS_USERSPACE(void)
165 unsigned long user_addr
, tmp
= 0;
168 user_addr
= vm_mmap(NULL
, 0, PAGE_SIZE
,
169 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
170 MAP_ANONYMOUS
| MAP_PRIVATE
, 0);
171 if (user_addr
>= TASK_SIZE
) {
172 pr_warn("Failed to allocate user memory\n");
176 if (copy_to_user((void __user
*)user_addr
, &tmp
, sizeof(tmp
))) {
177 pr_warn("copy_to_user failed\n");
178 vm_munmap(user_addr
, PAGE_SIZE
);
182 ptr
= (unsigned long *)user_addr
;
184 pr_info("attempting bad read at %p\n", ptr
);
188 pr_info("attempting bad write at %p\n", ptr
);
191 vm_munmap(user_addr
, PAGE_SIZE
);
194 void __init
lkdtm_perms_init(void)
196 /* Make sure we can write to __ro_after_init values during __init */
197 ro_after_init
|= 0xAA;