1 // SPDX-License-Identifier: GPL-2.0-only
3 * Kernel module for testing copy_to/from_user infrastructure.
5 * Copyright 2013 Google Inc. All Rights Reserved
8 * Kees Cook <keescook@chromium.org>
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/mman.h>
14 #include <linux/module.h>
15 #include <linux/sched.h>
16 #include <linux/slab.h>
17 #include <linux/uaccess.h>
18 #include <linux/vmalloc.h>
21 * Several 32-bit architectures support 64-bit {get,put}_user() calls.
22 * As there doesn't appear to be anything that can safely determine
23 * their capability at compile-time, we just have to opt-out certain archs.
25 #if BITS_PER_LONG == 64 || (!(defined(CONFIG_ARM) && !defined(MMU)) && \
26 !defined(CONFIG_M68K) && \
27 !defined(CONFIG_MICROBLAZE) && \
28 !defined(CONFIG_NIOS2) && \
29 !defined(CONFIG_PPC32) && \
30 !defined(CONFIG_SUPERH))
34 #define test(condition, msg, ...) \
36 int cond = (condition); \
38 pr_warn("[%d] " msg "\n", __LINE__, ##__VA_ARGS__); \
42 static bool is_zeroed(void *from, size_t size)
44 return memchr_inv(from, 0x0, size) == NULL;
47 static int test_check_nonzero_user(char *kmem, char __user *umem, size_t size)
51 size_t zero_start = size / 4;
52 size_t zero_end = size - zero_start;
55 * We conduct a series of check_nonzero_user() tests on a block of memory
56 * with the following byte-pattern (trying every possible [start,end]
59 * [ 00 ff 00 ff ... 00 00 00 00 ... ff 00 ff 00 ]
61 * And we verify that check_nonzero_user() acts identically to memchr_inv().
64 memset(kmem, 0x0, size);
65 for (i = 1; i < zero_start; i += 2)
67 for (i = zero_end; i < size; i += 2)
70 ret |= test(copy_to_user(umem, kmem, size),
71 "legitimate copy_to_user failed");
73 for (start = 0; start <= size; start++) {
74 for (end = start; end <= size; end++) {
75 size_t len = end - start;
76 int retval = check_zeroed_user(umem + start, len);
77 int expected = is_zeroed(kmem + start, len);
79 ret |= test(retval != expected,
80 "check_nonzero_user(=%d) != memchr_inv(=%d) mismatch (start=%zu, end=%zu)",
81 retval, expected, start, end);
88 static int test_copy_struct_from_user(char *kmem, char __user *umem,
92 char *umem_src = NULL, *expected = NULL;
95 umem_src = kmalloc(size, GFP_KERNEL);
96 if ((ret |= test(umem_src == NULL, "kmalloc failed")))
99 expected = kmalloc(size, GFP_KERNEL);
100 if ((ret |= test(expected == NULL, "kmalloc failed")))
103 /* Fill umem with a fixed byte pattern. */
104 memset(umem_src, 0x3e, size);
105 ret |= test(copy_to_user(umem, umem_src, size),
106 "legitimate copy_to_user failed");
108 /* Check basic case -- (usize == ksize). */
112 memcpy(expected, umem_src, ksize);
114 memset(kmem, 0x0, size);
115 ret |= test(copy_struct_from_user(kmem, ksize, umem, usize),
116 "copy_struct_from_user(usize == ksize) failed");
117 ret |= test(memcmp(kmem, expected, ksize),
118 "copy_struct_from_user(usize == ksize) gives unexpected copy");
120 /* Old userspace case -- (usize < ksize). */
124 memcpy(expected, umem_src, usize);
125 memset(expected + usize, 0x0, ksize - usize);
127 memset(kmem, 0x0, size);
128 ret |= test(copy_struct_from_user(kmem, ksize, umem, usize),
129 "copy_struct_from_user(usize < ksize) failed");
130 ret |= test(memcmp(kmem, expected, ksize),
131 "copy_struct_from_user(usize < ksize) gives unexpected copy");
133 /* New userspace (-E2BIG) case -- (usize > ksize). */
137 memset(kmem, 0x0, size);
138 ret |= test(copy_struct_from_user(kmem, ksize, umem, usize) != -E2BIG,
139 "copy_struct_from_user(usize > ksize) didn't give E2BIG");
141 /* New userspace (success) case -- (usize > ksize). */
145 memcpy(expected, umem_src, ksize);
146 ret |= test(clear_user(umem + ksize, usize - ksize),
147 "legitimate clear_user failed");
149 memset(kmem, 0x0, size);
150 ret |= test(copy_struct_from_user(kmem, ksize, umem, usize),
151 "copy_struct_from_user(usize > ksize) failed");
152 ret |= test(memcmp(kmem, expected, ksize),
153 "copy_struct_from_user(usize > ksize) gives unexpected copy");
161 static int __init test_user_copy_init(void)
165 char __user *usermem;
167 unsigned long user_addr;
175 kmem = kmalloc(PAGE_SIZE * 2, GFP_KERNEL);
179 user_addr = vm_mmap(NULL, 0, PAGE_SIZE * 2,
180 PROT_READ | PROT_WRITE | PROT_EXEC,
181 MAP_ANONYMOUS | MAP_PRIVATE, 0);
182 if (user_addr >= (unsigned long)(TASK_SIZE)) {
183 pr_warn("Failed to allocate user memory\n");
188 usermem = (char __user *)user_addr;
189 bad_usermem = (char *)user_addr;
192 * Legitimate usage: none of these copies should fail.
194 memset(kmem, 0x3a, PAGE_SIZE * 2);
195 ret |= test(copy_to_user(usermem, kmem, PAGE_SIZE),
196 "legitimate copy_to_user failed");
197 memset(kmem, 0x0, PAGE_SIZE);
198 ret |= test(copy_from_user(kmem, usermem, PAGE_SIZE),
199 "legitimate copy_from_user failed");
200 ret |= test(memcmp(kmem, kmem + PAGE_SIZE, PAGE_SIZE),
201 "legitimate usercopy failed to copy data");
203 #define test_legit(size, check) \
205 val_##size = check; \
206 ret |= test(put_user(val_##size, (size __user *)usermem), \
207 "legitimate put_user (" #size ") failed"); \
209 ret |= test(get_user(val_##size, (size __user *)usermem), \
210 "legitimate get_user (" #size ") failed"); \
211 ret |= test(val_##size != check, \
212 "legitimate get_user (" #size ") failed to do copy"); \
213 if (val_##size != check) { \
214 pr_info("0x%llx != 0x%llx\n", \
215 (unsigned long long)val_##size, \
216 (unsigned long long)check); \
220 test_legit(u8, 0x5a);
221 test_legit(u16, 0x5a5b);
222 test_legit(u32, 0x5a5b5c5d);
224 test_legit(u64, 0x5a5b5c5d6a6b6c6d);
228 /* Test usage of check_nonzero_user(). */
229 ret |= test_check_nonzero_user(kmem, usermem, 2 * PAGE_SIZE);
230 /* Test usage of copy_struct_from_user(). */
231 ret |= test_copy_struct_from_user(kmem, usermem, 2 * PAGE_SIZE);
234 * Invalid usage: none of these copies should succeed.
237 /* Prepare kernel memory with check values. */
238 memset(kmem, 0x5a, PAGE_SIZE);
239 memset(kmem + PAGE_SIZE, 0, PAGE_SIZE);
241 /* Reject kernel-to-kernel copies through copy_from_user(). */
242 ret |= test(!copy_from_user(kmem, (char __user *)(kmem + PAGE_SIZE),
244 "illegal all-kernel copy_from_user passed");
246 /* Destination half of buffer should have been zeroed. */
247 ret |= test(memcmp(kmem + PAGE_SIZE, kmem, PAGE_SIZE),
248 "zeroing failure for illegal all-kernel copy_from_user");
252 * When running with SMAP/PAN/etc, this will Oops the kernel
253 * due to the zeroing of userspace memory on failure. This needs
254 * to be tested in LKDTM instead, since this test module does not
257 ret |= test(!copy_from_user(bad_usermem, (char __user *)kmem,
259 "illegal reversed copy_from_user passed");
261 ret |= test(!copy_to_user((char __user *)kmem, kmem + PAGE_SIZE,
263 "illegal all-kernel copy_to_user passed");
264 ret |= test(!copy_to_user((char __user *)kmem, bad_usermem,
266 "illegal reversed copy_to_user passed");
268 #define test_illegal(size, check) \
270 val_##size = (check); \
271 ret |= test(!get_user(val_##size, (size __user *)kmem), \
272 "illegal get_user (" #size ") passed"); \
273 ret |= test(val_##size != (size)0, \
274 "zeroing failure for illegal get_user (" #size ")"); \
275 if (val_##size != (size)0) { \
276 pr_info("0x%llx != 0\n", \
277 (unsigned long long)val_##size); \
279 ret |= test(!put_user(val_##size, (size __user *)kmem), \
280 "illegal put_user (" #size ") passed"); \
283 test_illegal(u8, 0x5a);
284 test_illegal(u16, 0x5a5b);
285 test_illegal(u32, 0x5a5b5c5d);
287 test_illegal(u64, 0x5a5b5c5d6a6b6c6d);
291 vm_munmap(user_addr, PAGE_SIZE * 2);
295 pr_info("tests passed.\n");
302 module_init(test_user_copy_init);
304 static void __exit test_user_copy_exit(void)
306 pr_info("unloaded.\n");
309 module_exit(test_user_copy_exit);
311 MODULE_AUTHOR("Kees Cook <keescook@chromium.org>");
312 MODULE_LICENSE("GPL");