1 // SPDX-License-Identifier: GPL-2.0
3 * This is for all the tests related to copy_to_user() and copy_from_user()
7 #include <linux/slab.h>
8 #include <linux/vmalloc.h>
9 #include <linux/sched/task_stack.h>
10 #include <linux/mman.h>
11 #include <linux/uaccess.h>
12 #include <asm/cacheflush.h>
15 * Many of the tests here end up using const sizes, but those would
16 * normally be ignored by hardened usercopy, so force the compiler
17 * into choosing the non-const path to make sure we trigger the
18 * hardened usercopy checks by added "unconst" to all the const copies,
19 * and making sure "cache_size" isn't optimized into a const.
21 static volatile size_t unconst = 0;
22 static volatile size_t cache_size = 1024;
23 static struct kmem_cache *bad_cache;
25 static const unsigned char test_text[] = "This is a test.\n";
28 * Instead of adding -Wno-return-local-addr, just pass the stack address
29 * through a function to obfuscate it from the compiler.
31 static noinline unsigned char *trick_compiler(unsigned char *stack)
33 return stack + unconst;
36 static noinline unsigned char *do_usercopy_stack_callee(int value)
38 unsigned char buf[128];
41 /* Exercise stack to avoid everything living in registers. */
42 for (i = 0; i < sizeof(buf); i++) {
43 buf[i] = value & 0xff;
47 * Put the target buffer in the middle of stack allocation
48 * so that we don't step on future stack users regardless
49 * of stack growth direction.
51 return trick_compiler(&buf[(128/2)-32]);
54 static noinline void do_usercopy_stack(bool to_user, bool bad_frame)
56 unsigned long user_addr;
57 unsigned char good_stack[32];
58 unsigned char *bad_stack;
61 /* Exercise stack to avoid everything living in registers. */
62 for (i = 0; i < sizeof(good_stack); i++)
63 good_stack[i] = test_text[i % sizeof(test_text)];
65 /* This is a pointer to outside our current stack frame. */
67 bad_stack = do_usercopy_stack_callee((uintptr_t)&bad_stack);
69 /* Put start address just inside stack. */
70 bad_stack = task_stack_page(current) + THREAD_SIZE;
71 bad_stack -= sizeof(unsigned long);
74 #ifdef ARCH_HAS_CURRENT_STACK_POINTER
75 pr_info("stack : %px\n", (void *)current_stack_pointer);
77 pr_info("good_stack: %px-%px\n", good_stack, good_stack + sizeof(good_stack));
78 pr_info("bad_stack : %px-%px\n", bad_stack, bad_stack + sizeof(good_stack));
80 user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
81 PROT_READ | PROT_WRITE | PROT_EXEC,
82 MAP_ANONYMOUS | MAP_PRIVATE, 0);
83 if (user_addr >= TASK_SIZE) {
84 pr_warn("Failed to allocate user memory\n");
89 pr_info("attempting good copy_to_user of local stack\n");
90 if (copy_to_user((void __user *)user_addr, good_stack,
91 unconst + sizeof(good_stack))) {
92 pr_warn("copy_to_user failed unexpectedly?!\n");
96 pr_info("attempting bad copy_to_user of distant stack\n");
97 if (copy_to_user((void __user *)user_addr, bad_stack,
98 unconst + sizeof(good_stack))) {
99 pr_warn("copy_to_user failed, but lacked Oops\n");
104 * There isn't a safe way to not be protected by usercopy
105 * if we're going to write to another thread's stack.
110 pr_info("attempting good copy_from_user of local stack\n");
111 if (copy_from_user(good_stack, (void __user *)user_addr,
112 unconst + sizeof(good_stack))) {
113 pr_warn("copy_from_user failed unexpectedly?!\n");
117 pr_info("attempting bad copy_from_user of distant stack\n");
118 if (copy_from_user(bad_stack, (void __user *)user_addr,
119 unconst + sizeof(good_stack))) {
120 pr_warn("copy_from_user failed, but lacked Oops\n");
126 vm_munmap(user_addr, PAGE_SIZE);
129 static void do_usercopy_heap_size(bool to_user)
131 unsigned long user_addr;
132 unsigned char *one, *two;
133 size_t size = unconst + 1024;
135 one = kmalloc(size, GFP_KERNEL);
136 two = kmalloc(size, GFP_KERNEL);
138 pr_warn("Failed to allocate kernel memory\n");
142 user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
143 PROT_READ | PROT_WRITE | PROT_EXEC,
144 MAP_ANONYMOUS | MAP_PRIVATE, 0);
145 if (user_addr >= TASK_SIZE) {
146 pr_warn("Failed to allocate user memory\n");
150 memset(one, 'A', size);
151 memset(two, 'B', size);
154 pr_info("attempting good copy_to_user of correct size\n");
155 if (copy_to_user((void __user *)user_addr, one, size)) {
156 pr_warn("copy_to_user failed unexpectedly?!\n");
160 pr_info("attempting bad copy_to_user of too large size\n");
161 if (copy_to_user((void __user *)user_addr, one, 2 * size)) {
162 pr_warn("copy_to_user failed, but lacked Oops\n");
166 pr_info("attempting good copy_from_user of correct size\n");
167 if (copy_from_user(one, (void __user *)user_addr, size)) {
168 pr_warn("copy_from_user failed unexpectedly?!\n");
172 pr_info("attempting bad copy_from_user of too large size\n");
173 if (copy_from_user(one, (void __user *)user_addr, 2 * size)) {
174 pr_warn("copy_from_user failed, but lacked Oops\n");
180 vm_munmap(user_addr, PAGE_SIZE);
186 static void do_usercopy_heap_flag(bool to_user)
188 unsigned long user_addr;
189 unsigned char *good_buf = NULL;
190 unsigned char *bad_buf = NULL;
192 /* Make sure cache was prepared. */
194 pr_warn("Failed to allocate kernel cache\n");
199 * Allocate one buffer from each cache (kmalloc will have the
200 * SLAB_USERCOPY flag already, but "bad_cache" won't).
202 good_buf = kmalloc(cache_size, GFP_KERNEL);
203 bad_buf = kmem_cache_alloc(bad_cache, GFP_KERNEL);
204 if (!good_buf || !bad_buf) {
205 pr_warn("Failed to allocate buffers from caches\n");
209 /* Allocate user memory we'll poke at. */
210 user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
211 PROT_READ | PROT_WRITE | PROT_EXEC,
212 MAP_ANONYMOUS | MAP_PRIVATE, 0);
213 if (user_addr >= TASK_SIZE) {
214 pr_warn("Failed to allocate user memory\n");
218 memset(good_buf, 'A', cache_size);
219 memset(bad_buf, 'B', cache_size);
222 pr_info("attempting good copy_to_user with SLAB_USERCOPY\n");
223 if (copy_to_user((void __user *)user_addr, good_buf,
225 pr_warn("copy_to_user failed unexpectedly?!\n");
229 pr_info("attempting bad copy_to_user w/o SLAB_USERCOPY\n");
230 if (copy_to_user((void __user *)user_addr, bad_buf,
232 pr_warn("copy_to_user failed, but lacked Oops\n");
236 pr_info("attempting good copy_from_user with SLAB_USERCOPY\n");
237 if (copy_from_user(good_buf, (void __user *)user_addr,
239 pr_warn("copy_from_user failed unexpectedly?!\n");
243 pr_info("attempting bad copy_from_user w/o SLAB_USERCOPY\n");
244 if (copy_from_user(bad_buf, (void __user *)user_addr,
246 pr_warn("copy_from_user failed, but lacked Oops\n");
252 vm_munmap(user_addr, PAGE_SIZE);
255 kmem_cache_free(bad_cache, bad_buf);
259 /* Callable tests. */
260 void lkdtm_USERCOPY_HEAP_SIZE_TO(void)
262 do_usercopy_heap_size(true);
265 void lkdtm_USERCOPY_HEAP_SIZE_FROM(void)
267 do_usercopy_heap_size(false);
270 void lkdtm_USERCOPY_HEAP_FLAG_TO(void)
272 do_usercopy_heap_flag(true);
275 void lkdtm_USERCOPY_HEAP_FLAG_FROM(void)
277 do_usercopy_heap_flag(false);
280 void lkdtm_USERCOPY_STACK_FRAME_TO(void)
282 do_usercopy_stack(true, true);
285 void lkdtm_USERCOPY_STACK_FRAME_FROM(void)
287 do_usercopy_stack(false, true);
290 void lkdtm_USERCOPY_STACK_BEYOND(void)
292 do_usercopy_stack(true, false);
295 void lkdtm_USERCOPY_KERNEL(void)
297 unsigned long user_addr;
299 user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
300 PROT_READ | PROT_WRITE | PROT_EXEC,
301 MAP_ANONYMOUS | MAP_PRIVATE, 0);
302 if (user_addr >= TASK_SIZE) {
303 pr_warn("Failed to allocate user memory\n");
307 pr_info("attempting good copy_to_user from kernel rodata\n");
308 if (copy_to_user((void __user *)user_addr, test_text,
309 unconst + sizeof(test_text))) {
310 pr_warn("copy_to_user failed unexpectedly?!\n");
314 pr_info("attempting bad copy_to_user from kernel text\n");
315 if (copy_to_user((void __user *)user_addr, vm_mmap,
316 unconst + PAGE_SIZE)) {
317 pr_warn("copy_to_user failed, but lacked Oops\n");
322 vm_munmap(user_addr, PAGE_SIZE);
325 void __init lkdtm_usercopy_init(void)
327 /* Prepare cache that lacks SLAB_USERCOPY flag. */
328 bad_cache = kmem_cache_create("lkdtm-no-usercopy", cache_size, 0,
332 void __exit lkdtm_usercopy_exit(void)
334 kmem_cache_destroy(bad_cache);