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
)
36 static noinline
unsigned char *do_usercopy_stack_callee(int value
)
38 unsigned char buf
[32];
41 /* Exercise stack to avoid everything living in registers. */
42 for (i
= 0; i
< sizeof(buf
); i
++) {
43 buf
[i
] = value
& 0xff;
46 return trick_compiler(buf
);
49 static noinline
void do_usercopy_stack(bool to_user
, bool bad_frame
)
51 unsigned long user_addr
;
52 unsigned char good_stack
[32];
53 unsigned char *bad_stack
;
56 /* Exercise stack to avoid everything living in registers. */
57 for (i
= 0; i
< sizeof(good_stack
); i
++)
58 good_stack
[i
] = test_text
[i
% sizeof(test_text
)];
60 /* This is a pointer to outside our current stack frame. */
62 bad_stack
= do_usercopy_stack_callee((uintptr_t)&bad_stack
);
64 /* Put start address just inside stack. */
65 bad_stack
= task_stack_page(current
) + THREAD_SIZE
;
66 bad_stack
-= sizeof(unsigned long);
69 user_addr
= vm_mmap(NULL
, 0, PAGE_SIZE
,
70 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
71 MAP_ANONYMOUS
| MAP_PRIVATE
, 0);
72 if (user_addr
>= TASK_SIZE
) {
73 pr_warn("Failed to allocate user memory\n");
78 pr_info("attempting good copy_to_user of local stack\n");
79 if (copy_to_user((void __user
*)user_addr
, good_stack
,
80 unconst
+ sizeof(good_stack
))) {
81 pr_warn("copy_to_user failed unexpectedly?!\n");
85 pr_info("attempting bad copy_to_user of distant stack\n");
86 if (copy_to_user((void __user
*)user_addr
, bad_stack
,
87 unconst
+ sizeof(good_stack
))) {
88 pr_warn("copy_to_user failed, but lacked Oops\n");
93 * There isn't a safe way to not be protected by usercopy
94 * if we're going to write to another thread's stack.
99 pr_info("attempting good copy_from_user of local stack\n");
100 if (copy_from_user(good_stack
, (void __user
*)user_addr
,
101 unconst
+ sizeof(good_stack
))) {
102 pr_warn("copy_from_user failed unexpectedly?!\n");
106 pr_info("attempting bad copy_from_user of distant stack\n");
107 if (copy_from_user(bad_stack
, (void __user
*)user_addr
,
108 unconst
+ sizeof(good_stack
))) {
109 pr_warn("copy_from_user failed, but lacked Oops\n");
115 vm_munmap(user_addr
, PAGE_SIZE
);
118 static void do_usercopy_heap_size(bool to_user
)
120 unsigned long user_addr
;
121 unsigned char *one
, *two
;
122 size_t size
= unconst
+ 1024;
124 one
= kmalloc(size
, GFP_KERNEL
);
125 two
= kmalloc(size
, GFP_KERNEL
);
127 pr_warn("Failed to allocate kernel memory\n");
131 user_addr
= vm_mmap(NULL
, 0, PAGE_SIZE
,
132 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
133 MAP_ANONYMOUS
| MAP_PRIVATE
, 0);
134 if (user_addr
>= TASK_SIZE
) {
135 pr_warn("Failed to allocate user memory\n");
139 memset(one
, 'A', size
);
140 memset(two
, 'B', size
);
143 pr_info("attempting good copy_to_user of correct size\n");
144 if (copy_to_user((void __user
*)user_addr
, one
, size
)) {
145 pr_warn("copy_to_user failed unexpectedly?!\n");
149 pr_info("attempting bad copy_to_user of too large size\n");
150 if (copy_to_user((void __user
*)user_addr
, one
, 2 * size
)) {
151 pr_warn("copy_to_user failed, but lacked Oops\n");
155 pr_info("attempting good copy_from_user of correct size\n");
156 if (copy_from_user(one
, (void __user
*)user_addr
, size
)) {
157 pr_warn("copy_from_user failed unexpectedly?!\n");
161 pr_info("attempting bad copy_from_user of too large size\n");
162 if (copy_from_user(one
, (void __user
*)user_addr
, 2 * size
)) {
163 pr_warn("copy_from_user failed, but lacked Oops\n");
169 vm_munmap(user_addr
, PAGE_SIZE
);
175 static void do_usercopy_heap_flag(bool to_user
)
177 unsigned long user_addr
;
178 unsigned char *good_buf
= NULL
;
179 unsigned char *bad_buf
= NULL
;
181 /* Make sure cache was prepared. */
183 pr_warn("Failed to allocate kernel cache\n");
188 * Allocate one buffer from each cache (kmalloc will have the
189 * SLAB_USERCOPY flag already, but "bad_cache" won't).
191 good_buf
= kmalloc(cache_size
, GFP_KERNEL
);
192 bad_buf
= kmem_cache_alloc(bad_cache
, GFP_KERNEL
);
193 if (!good_buf
|| !bad_buf
) {
194 pr_warn("Failed to allocate buffers from caches\n");
198 /* Allocate user memory we'll poke at. */
199 user_addr
= vm_mmap(NULL
, 0, PAGE_SIZE
,
200 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
201 MAP_ANONYMOUS
| MAP_PRIVATE
, 0);
202 if (user_addr
>= TASK_SIZE
) {
203 pr_warn("Failed to allocate user memory\n");
207 memset(good_buf
, 'A', cache_size
);
208 memset(bad_buf
, 'B', cache_size
);
211 pr_info("attempting good copy_to_user with SLAB_USERCOPY\n");
212 if (copy_to_user((void __user
*)user_addr
, good_buf
,
214 pr_warn("copy_to_user failed unexpectedly?!\n");
218 pr_info("attempting bad copy_to_user w/o SLAB_USERCOPY\n");
219 if (copy_to_user((void __user
*)user_addr
, bad_buf
,
221 pr_warn("copy_to_user failed, but lacked Oops\n");
225 pr_info("attempting good copy_from_user with SLAB_USERCOPY\n");
226 if (copy_from_user(good_buf
, (void __user
*)user_addr
,
228 pr_warn("copy_from_user failed unexpectedly?!\n");
232 pr_info("attempting bad copy_from_user w/o SLAB_USERCOPY\n");
233 if (copy_from_user(bad_buf
, (void __user
*)user_addr
,
235 pr_warn("copy_from_user failed, but lacked Oops\n");
241 vm_munmap(user_addr
, PAGE_SIZE
);
244 kmem_cache_free(bad_cache
, bad_buf
);
248 /* Callable tests. */
249 void lkdtm_USERCOPY_HEAP_SIZE_TO(void)
251 do_usercopy_heap_size(true);
254 void lkdtm_USERCOPY_HEAP_SIZE_FROM(void)
256 do_usercopy_heap_size(false);
259 void lkdtm_USERCOPY_HEAP_FLAG_TO(void)
261 do_usercopy_heap_flag(true);
264 void lkdtm_USERCOPY_HEAP_FLAG_FROM(void)
266 do_usercopy_heap_flag(false);
269 void lkdtm_USERCOPY_STACK_FRAME_TO(void)
271 do_usercopy_stack(true, true);
274 void lkdtm_USERCOPY_STACK_FRAME_FROM(void)
276 do_usercopy_stack(false, true);
279 void lkdtm_USERCOPY_STACK_BEYOND(void)
281 do_usercopy_stack(true, false);
284 void lkdtm_USERCOPY_KERNEL(void)
286 unsigned long user_addr
;
288 user_addr
= vm_mmap(NULL
, 0, PAGE_SIZE
,
289 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
290 MAP_ANONYMOUS
| MAP_PRIVATE
, 0);
291 if (user_addr
>= TASK_SIZE
) {
292 pr_warn("Failed to allocate user memory\n");
296 pr_info("attempting good copy_to_user from kernel rodata\n");
297 if (copy_to_user((void __user
*)user_addr
, test_text
,
298 unconst
+ sizeof(test_text
))) {
299 pr_warn("copy_to_user failed unexpectedly?!\n");
303 pr_info("attempting bad copy_to_user from kernel text\n");
304 if (copy_to_user((void __user
*)user_addr
, vm_mmap
,
305 unconst
+ PAGE_SIZE
)) {
306 pr_warn("copy_to_user failed, but lacked Oops\n");
311 vm_munmap(user_addr
, PAGE_SIZE
);
314 void __init
lkdtm_usercopy_init(void)
316 /* Prepare cache that lacks SLAB_USERCOPY flag. */
317 bad_cache
= kmem_cache_create("lkdtm-no-usercopy", cache_size
, 0,
321 void __exit
lkdtm_usercopy_exit(void)
323 kmem_cache_destroy(bad_cache
);