xref: /linux/drivers/misc/lkdtm/perms.c (revision 621cde16e49b3ecf7d59a8106a20aaebfb4a59a9)
1039a1c42SKees Cook // SPDX-License-Identifier: GPL-2.0
2039a1c42SKees Cook /*
3039a1c42SKees Cook  * This is for all the tests related to validating kernel memory
4039a1c42SKees Cook  * permissions: non-executable regions, non-writable regions, and
5039a1c42SKees Cook  * even non-readable regions.
6039a1c42SKees Cook  */
7039a1c42SKees Cook #include "lkdtm.h"
8039a1c42SKees Cook #include <linux/slab.h>
9039a1c42SKees Cook #include <linux/vmalloc.h>
10039a1c42SKees Cook #include <linux/mman.h>
11039a1c42SKees Cook #include <linux/uaccess.h>
12039a1c42SKees Cook #include <asm/cacheflush.h>
13b6491339SChristophe Leroy #include <asm/sections.h>
14039a1c42SKees Cook 
15039a1c42SKees Cook /* Whether or not to fill the target memory area with do_nothing(). */
16039a1c42SKees Cook #define CODE_WRITE	true
17039a1c42SKees Cook #define CODE_AS_IS	false
18039a1c42SKees Cook 
19039a1c42SKees Cook /* How many bytes to copy to be sure we've copied enough of do_nothing(). */
20039a1c42SKees Cook #define EXEC_SIZE 64
21039a1c42SKees Cook 
22039a1c42SKees Cook /* This is non-const, so it will end up in the .data section. */
23039a1c42SKees Cook static u8 data_area[EXEC_SIZE];
24039a1c42SKees Cook 
2569b420edSChristophe Leroy /* This is const, so it will end up in the .rodata section. */
26039a1c42SKees Cook static const unsigned long rodata = 0xAA55AA55;
27039a1c42SKees Cook 
28039a1c42SKees Cook /* This is marked __ro_after_init, so it should ultimately be .rodata. */
29039a1c42SKees Cook static unsigned long ro_after_init __ro_after_init = 0x55AA5500;
30039a1c42SKees Cook 
31039a1c42SKees Cook /*
32039a1c42SKees Cook  * This just returns to the caller. It is designed to be copied into
33039a1c42SKees Cook  * non-executable memory regions.
34039a1c42SKees Cook  */
do_nothing(void)3569b420edSChristophe Leroy static noinline void do_nothing(void)
36039a1c42SKees Cook {
37039a1c42SKees Cook 	return;
38039a1c42SKees Cook }
39039a1c42SKees Cook 
40039a1c42SKees Cook /* Must immediately follow do_nothing for size calculuations to work out. */
do_overwritten(void)41b6491339SChristophe Leroy static noinline void do_overwritten(void)
42039a1c42SKees Cook {
43039a1c42SKees Cook 	pr_info("do_overwritten wasn't overwritten!\n");
44039a1c42SKees Cook 	return;
45039a1c42SKees Cook }
46039a1c42SKees Cook 
do_almost_nothing(void)475e5a6c54SChristophe Leroy static noinline void do_almost_nothing(void)
485e5a6c54SChristophe Leroy {
495e5a6c54SChristophe Leroy 	pr_info("do_nothing was hijacked!\n");
505e5a6c54SChristophe Leroy }
515e5a6c54SChristophe Leroy 
setup_function_descriptor(func_desc_t * fdesc,void * dst)5272a86433SChristophe Leroy static void *setup_function_descriptor(func_desc_t *fdesc, void *dst)
5372a86433SChristophe Leroy {
5472a86433SChristophe Leroy 	if (!have_function_descriptors())
5572a86433SChristophe Leroy 		return dst;
5672a86433SChristophe Leroy 
5772a86433SChristophe Leroy 	memcpy(fdesc, do_nothing, sizeof(*fdesc));
5872a86433SChristophe Leroy 	fdesc->addr = (unsigned long)dst;
5972a86433SChristophe Leroy 	barrier();
6072a86433SChristophe Leroy 
6172a86433SChristophe Leroy 	return fdesc;
6272a86433SChristophe Leroy }
6372a86433SChristophe Leroy 
execute_location(void * dst,bool write)64*fb28a886SKees Cook static noinline __nocfi void execute_location(void *dst, bool write)
65039a1c42SKees Cook {
6672a86433SChristophe Leroy 	void (*func)(void);
6772a86433SChristophe Leroy 	func_desc_t fdesc;
6872a86433SChristophe Leroy 	void *do_nothing_text = dereference_function_descriptor(do_nothing);
69039a1c42SKees Cook 
7072a86433SChristophe Leroy 	pr_info("attempting ok execution at %px\n", do_nothing_text);
71039a1c42SKees Cook 	do_nothing();
72039a1c42SKees Cook 
73039a1c42SKees Cook 	if (write == CODE_WRITE) {
7472a86433SChristophe Leroy 		memcpy(dst, do_nothing_text, EXEC_SIZE);
75039a1c42SKees Cook 		flush_icache_range((unsigned long)dst,
76039a1c42SKees Cook 				   (unsigned long)dst + EXEC_SIZE);
77039a1c42SKees Cook 	}
7872a86433SChristophe Leroy 	pr_info("attempting bad execution at %px\n", dst);
7972a86433SChristophe Leroy 	func = setup_function_descriptor(&fdesc, dst);
80039a1c42SKees Cook 	func();
81464e86b4SKees Cook 	pr_err("FAIL: func returned\n");
82039a1c42SKees Cook }
83039a1c42SKees Cook 
execute_user_location(void * dst)84039a1c42SKees Cook static void execute_user_location(void *dst)
85039a1c42SKees Cook {
86039a1c42SKees Cook 	int copied;
87039a1c42SKees Cook 
88039a1c42SKees Cook 	/* Intentionally crossing kernel/user memory boundary. */
8972a86433SChristophe Leroy 	void (*func)(void);
9072a86433SChristophe Leroy 	func_desc_t fdesc;
9172a86433SChristophe Leroy 	void *do_nothing_text = dereference_function_descriptor(do_nothing);
92039a1c42SKees Cook 
9372a86433SChristophe Leroy 	pr_info("attempting ok execution at %px\n", do_nothing_text);
94039a1c42SKees Cook 	do_nothing();
95039a1c42SKees Cook 
9672a86433SChristophe Leroy 	copied = access_process_vm(current, (unsigned long)dst, do_nothing_text,
97039a1c42SKees Cook 				   EXEC_SIZE, FOLL_WRITE);
98039a1c42SKees Cook 	if (copied < EXEC_SIZE)
99039a1c42SKees Cook 		return;
10072a86433SChristophe Leroy 	pr_info("attempting bad execution at %px\n", dst);
10172a86433SChristophe Leroy 	func = setup_function_descriptor(&fdesc, dst);
102039a1c42SKees Cook 	func();
103464e86b4SKees Cook 	pr_err("FAIL: func returned\n");
104039a1c42SKees Cook }
105039a1c42SKees Cook 
lkdtm_WRITE_RO(void)10673f62e60SKees Cook static void lkdtm_WRITE_RO(void)
107039a1c42SKees Cook {
108464e86b4SKees Cook 	/* Explicitly cast away "const" for the test and make volatile. */
109464e86b4SKees Cook 	volatile unsigned long *ptr = (unsigned long *)&rodata;
110039a1c42SKees Cook 
1114c411157SChristophe Leroy 	pr_info("attempting bad rodata write at %px\n", ptr);
112039a1c42SKees Cook 	*ptr ^= 0xabcd1234;
113464e86b4SKees Cook 	pr_err("FAIL: survived bad write\n");
114039a1c42SKees Cook }
115039a1c42SKees Cook 
lkdtm_WRITE_RO_AFTER_INIT(void)11673f62e60SKees Cook static void lkdtm_WRITE_RO_AFTER_INIT(void)
117039a1c42SKees Cook {
118464e86b4SKees Cook 	volatile unsigned long *ptr = &ro_after_init;
119039a1c42SKees Cook 
120039a1c42SKees Cook 	/*
121039a1c42SKees Cook 	 * Verify we were written to during init. Since an Oops
122039a1c42SKees Cook 	 * is considered a "success", a failure is to just skip the
123039a1c42SKees Cook 	 * real test.
124039a1c42SKees Cook 	 */
125039a1c42SKees Cook 	if ((*ptr & 0xAA) != 0xAA) {
126039a1c42SKees Cook 		pr_info("%p was NOT written during init!?\n", ptr);
127039a1c42SKees Cook 		return;
128039a1c42SKees Cook 	}
129039a1c42SKees Cook 
1304c411157SChristophe Leroy 	pr_info("attempting bad ro_after_init write at %px\n", ptr);
131039a1c42SKees Cook 	*ptr ^= 0xabcd1234;
132464e86b4SKees Cook 	pr_err("FAIL: survived bad write\n");
133039a1c42SKees Cook }
134039a1c42SKees Cook 
lkdtm_WRITE_KERN(void)13573f62e60SKees Cook static void lkdtm_WRITE_KERN(void)
136039a1c42SKees Cook {
137039a1c42SKees Cook 	size_t size;
138464e86b4SKees Cook 	volatile unsigned char *ptr;
139039a1c42SKees Cook 
140b6491339SChristophe Leroy 	size = (unsigned long)dereference_function_descriptor(do_overwritten) -
141b6491339SChristophe Leroy 	       (unsigned long)dereference_function_descriptor(do_nothing);
142b6491339SChristophe Leroy 	ptr = dereference_function_descriptor(do_overwritten);
143039a1c42SKees Cook 
1444c411157SChristophe Leroy 	pr_info("attempting bad %zu byte write at %px\n", size, ptr);
145464e86b4SKees Cook 	memcpy((void *)ptr, (unsigned char *)do_nothing, size);
146039a1c42SKees Cook 	flush_icache_range((unsigned long)ptr, (unsigned long)(ptr + size));
147464e86b4SKees Cook 	pr_err("FAIL: survived bad write\n");
148039a1c42SKees Cook 
149039a1c42SKees Cook 	do_overwritten();
150039a1c42SKees Cook }
151039a1c42SKees Cook 
lkdtm_WRITE_OPD(void)15273f62e60SKees Cook static void lkdtm_WRITE_OPD(void)
1535e5a6c54SChristophe Leroy {
1545e5a6c54SChristophe Leroy 	size_t size = sizeof(func_desc_t);
1555e5a6c54SChristophe Leroy 	void (*func)(void) = do_nothing;
1565e5a6c54SChristophe Leroy 
1575e5a6c54SChristophe Leroy 	if (!have_function_descriptors()) {
1585e5a6c54SChristophe Leroy 		pr_info("XFAIL: Platform doesn't use function descriptors.\n");
1595e5a6c54SChristophe Leroy 		return;
1605e5a6c54SChristophe Leroy 	}
1615e5a6c54SChristophe Leroy 	pr_info("attempting bad %zu bytes write at %px\n", size, do_nothing);
1625e5a6c54SChristophe Leroy 	memcpy(do_nothing, do_almost_nothing, size);
1635e5a6c54SChristophe Leroy 	pr_err("FAIL: survived bad write\n");
1645e5a6c54SChristophe Leroy 
1655e5a6c54SChristophe Leroy 	asm("" : "=m"(func));
1665e5a6c54SChristophe Leroy 	func();
1675e5a6c54SChristophe Leroy }
1685e5a6c54SChristophe Leroy 
lkdtm_EXEC_DATA(void)16973f62e60SKees Cook static void lkdtm_EXEC_DATA(void)
170039a1c42SKees Cook {
171039a1c42SKees Cook 	execute_location(data_area, CODE_WRITE);
172039a1c42SKees Cook }
173039a1c42SKees Cook 
lkdtm_EXEC_STACK(void)17473f62e60SKees Cook static void lkdtm_EXEC_STACK(void)
175039a1c42SKees Cook {
176039a1c42SKees Cook 	u8 stack_area[EXEC_SIZE];
177039a1c42SKees Cook 	execute_location(stack_area, CODE_WRITE);
178039a1c42SKees Cook }
179039a1c42SKees Cook 
lkdtm_EXEC_KMALLOC(void)18073f62e60SKees Cook static void lkdtm_EXEC_KMALLOC(void)
181039a1c42SKees Cook {
182039a1c42SKees Cook 	u32 *kmalloc_area = kmalloc(EXEC_SIZE, GFP_KERNEL);
183039a1c42SKees Cook 	execute_location(kmalloc_area, CODE_WRITE);
184039a1c42SKees Cook 	kfree(kmalloc_area);
185039a1c42SKees Cook }
186039a1c42SKees Cook 
lkdtm_EXEC_VMALLOC(void)18773f62e60SKees Cook static void lkdtm_EXEC_VMALLOC(void)
188039a1c42SKees Cook {
189039a1c42SKees Cook 	u32 *vmalloc_area = vmalloc(EXEC_SIZE);
190039a1c42SKees Cook 	execute_location(vmalloc_area, CODE_WRITE);
191039a1c42SKees Cook 	vfree(vmalloc_area);
192039a1c42SKees Cook }
193039a1c42SKees Cook 
lkdtm_EXEC_RODATA(void)19473f62e60SKees Cook static void lkdtm_EXEC_RODATA(void)
195039a1c42SKees Cook {
19672a86433SChristophe Leroy 	execute_location(dereference_function_descriptor(lkdtm_rodata_do_nothing),
19772a86433SChristophe Leroy 			 CODE_AS_IS);
198039a1c42SKees Cook }
199039a1c42SKees Cook 
lkdtm_EXEC_USERSPACE(void)20073f62e60SKees Cook static void lkdtm_EXEC_USERSPACE(void)
201039a1c42SKees Cook {
202039a1c42SKees Cook 	unsigned long user_addr;
203039a1c42SKees Cook 
204039a1c42SKees Cook 	user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
205039a1c42SKees Cook 			    PROT_READ | PROT_WRITE | PROT_EXEC,
206039a1c42SKees Cook 			    MAP_ANONYMOUS | MAP_PRIVATE, 0);
207039a1c42SKees Cook 	if (user_addr >= TASK_SIZE) {
208039a1c42SKees Cook 		pr_warn("Failed to allocate user memory\n");
209039a1c42SKees Cook 		return;
210039a1c42SKees Cook 	}
211039a1c42SKees Cook 	execute_user_location((void *)user_addr);
212039a1c42SKees Cook 	vm_munmap(user_addr, PAGE_SIZE);
213039a1c42SKees Cook }
214039a1c42SKees Cook 
lkdtm_EXEC_NULL(void)21573f62e60SKees Cook static void lkdtm_EXEC_NULL(void)
21659a12205SChristophe Leroy {
21759a12205SChristophe Leroy 	execute_location(NULL, CODE_AS_IS);
21859a12205SChristophe Leroy }
21959a12205SChristophe Leroy 
lkdtm_ACCESS_USERSPACE(void)22073f62e60SKees Cook static void lkdtm_ACCESS_USERSPACE(void)
221039a1c42SKees Cook {
222039a1c42SKees Cook 	unsigned long user_addr, tmp = 0;
223039a1c42SKees Cook 	unsigned long *ptr;
224039a1c42SKees Cook 
225039a1c42SKees Cook 	user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
226039a1c42SKees Cook 			    PROT_READ | PROT_WRITE | PROT_EXEC,
227039a1c42SKees Cook 			    MAP_ANONYMOUS | MAP_PRIVATE, 0);
228039a1c42SKees Cook 	if (user_addr >= TASK_SIZE) {
229039a1c42SKees Cook 		pr_warn("Failed to allocate user memory\n");
230039a1c42SKees Cook 		return;
231039a1c42SKees Cook 	}
232039a1c42SKees Cook 
233039a1c42SKees Cook 	if (copy_to_user((void __user *)user_addr, &tmp, sizeof(tmp))) {
234039a1c42SKees Cook 		pr_warn("copy_to_user failed\n");
235039a1c42SKees Cook 		vm_munmap(user_addr, PAGE_SIZE);
236039a1c42SKees Cook 		return;
237039a1c42SKees Cook 	}
238039a1c42SKees Cook 
239039a1c42SKees Cook 	ptr = (unsigned long *)user_addr;
240039a1c42SKees Cook 
2414c411157SChristophe Leroy 	pr_info("attempting bad read at %px\n", ptr);
242039a1c42SKees Cook 	tmp = *ptr;
243039a1c42SKees Cook 	tmp += 0xc0dec0de;
244464e86b4SKees Cook 	pr_err("FAIL: survived bad read\n");
245039a1c42SKees Cook 
2464c411157SChristophe Leroy 	pr_info("attempting bad write at %px\n", ptr);
247039a1c42SKees Cook 	*ptr = tmp;
248464e86b4SKees Cook 	pr_err("FAIL: survived bad write\n");
249039a1c42SKees Cook 
250039a1c42SKees Cook 	vm_munmap(user_addr, PAGE_SIZE);
251039a1c42SKees Cook }
252039a1c42SKees Cook 
lkdtm_ACCESS_NULL(void)25373f62e60SKees Cook static void lkdtm_ACCESS_NULL(void)
25459a12205SChristophe Leroy {
25559a12205SChristophe Leroy 	unsigned long tmp;
256464e86b4SKees Cook 	volatile unsigned long *ptr = (unsigned long *)NULL;
25759a12205SChristophe Leroy 
25859a12205SChristophe Leroy 	pr_info("attempting bad read at %px\n", ptr);
25959a12205SChristophe Leroy 	tmp = *ptr;
26059a12205SChristophe Leroy 	tmp += 0xc0dec0de;
261464e86b4SKees Cook 	pr_err("FAIL: survived bad read\n");
26259a12205SChristophe Leroy 
26359a12205SChristophe Leroy 	pr_info("attempting bad write at %px\n", ptr);
26459a12205SChristophe Leroy 	*ptr = tmp;
265464e86b4SKees Cook 	pr_err("FAIL: survived bad write\n");
26659a12205SChristophe Leroy }
26759a12205SChristophe Leroy 
lkdtm_perms_init(void)268039a1c42SKees Cook void __init lkdtm_perms_init(void)
269039a1c42SKees Cook {
270039a1c42SKees Cook 	/* Make sure we can write to __ro_after_init values during __init */
271039a1c42SKees Cook 	ro_after_init |= 0xAA;
272039a1c42SKees Cook }
27373f62e60SKees Cook 
27473f62e60SKees Cook static struct crashtype crashtypes[] = {
27573f62e60SKees Cook 	CRASHTYPE(WRITE_RO),
27673f62e60SKees Cook 	CRASHTYPE(WRITE_RO_AFTER_INIT),
27773f62e60SKees Cook 	CRASHTYPE(WRITE_KERN),
27873f62e60SKees Cook 	CRASHTYPE(WRITE_OPD),
27973f62e60SKees Cook 	CRASHTYPE(EXEC_DATA),
28073f62e60SKees Cook 	CRASHTYPE(EXEC_STACK),
28173f62e60SKees Cook 	CRASHTYPE(EXEC_KMALLOC),
28273f62e60SKees Cook 	CRASHTYPE(EXEC_VMALLOC),
28373f62e60SKees Cook 	CRASHTYPE(EXEC_RODATA),
28473f62e60SKees Cook 	CRASHTYPE(EXEC_USERSPACE),
28573f62e60SKees Cook 	CRASHTYPE(EXEC_NULL),
28673f62e60SKees Cook 	CRASHTYPE(ACCESS_USERSPACE),
28773f62e60SKees Cook 	CRASHTYPE(ACCESS_NULL),
28873f62e60SKees Cook };
28973f62e60SKees Cook 
29073f62e60SKees Cook struct crashtype_category perms_crashtypes = {
29173f62e60SKees Cook 	.crashtypes = crashtypes,
29273f62e60SKees Cook 	.len	    = ARRAY_SIZE(crashtypes),
29373f62e60SKees Cook };
294