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)
50 size_t start, end, i, zero_start, zero_end;
52 if (test(size < 2 * PAGE_SIZE, "buffer too small"))
56 * We want to cross a page boundary to exercise the code more
57 * effectively. We also don't want to make the size we scan too large,
58 * otherwise the test can take a long time and cause soft lockups. So
59 * scan a 1024 byte region across the page boundary.
62 start = PAGE_SIZE - (size / 2);
67 zero_start = size / 4;
68 zero_end = size - zero_start;
71 * We conduct a series of check_nonzero_user() tests on a block of
72 * memory with the following byte-pattern (trying every possible
75 * [ 00 ff 00 ff ... 00 00 00 00 ... ff 00 ff 00 ]
77 * And we verify that check_nonzero_user() acts identically to
81 memset(kmem, 0x0, size);
82 for (i = 1; i < zero_start; i += 2)
84 for (i = zero_end; i < size; i += 2)
87 ret |= test(copy_to_user(umem, kmem, size),
88 "legitimate copy_to_user failed");
90 for (start = 0; start <= size; start++) {
91 for (end = start; end <= size; end++) {
92 size_t len = end - start;
93 int retval = check_zeroed_user(umem + start, len);
94 int expected = is_zeroed(kmem + start, len);
96 ret |= test(retval != expected,
97 "check_nonzero_user(=%d) != memchr_inv(=%d) mismatch (start=%zu, end=%zu)",
98 retval, expected, start, end);
105 static int test_copy_struct_from_user(char *kmem, char __user *umem,
109 char *umem_src = NULL, *expected = NULL;
112 umem_src = kmalloc(size, GFP_KERNEL);
113 ret = test(umem_src == NULL, "kmalloc failed");
117 expected = kmalloc(size, GFP_KERNEL);
118 ret = test(expected == NULL, "kmalloc failed");
122 /* Fill umem with a fixed byte pattern. */
123 memset(umem_src, 0x3e, size);
124 ret |= test(copy_to_user(umem, umem_src, size),
125 "legitimate copy_to_user failed");
127 /* Check basic case -- (usize == ksize). */
131 memcpy(expected, umem_src, ksize);
133 memset(kmem, 0x0, size);
134 ret |= test(copy_struct_from_user(kmem, ksize, umem, usize),
135 "copy_struct_from_user(usize == ksize) failed");
136 ret |= test(memcmp(kmem, expected, ksize),
137 "copy_struct_from_user(usize == ksize) gives unexpected copy");
139 /* Old userspace case -- (usize < ksize). */
143 memcpy(expected, umem_src, usize);
144 memset(expected + usize, 0x0, ksize - usize);
146 memset(kmem, 0x0, size);
147 ret |= test(copy_struct_from_user(kmem, ksize, umem, usize),
148 "copy_struct_from_user(usize < ksize) failed");
149 ret |= test(memcmp(kmem, expected, ksize),
150 "copy_struct_from_user(usize < ksize) gives unexpected copy");
152 /* New userspace (-E2BIG) case -- (usize > ksize). */
156 memset(kmem, 0x0, size);
157 ret |= test(copy_struct_from_user(kmem, ksize, umem, usize) != -E2BIG,
158 "copy_struct_from_user(usize > ksize) didn't give E2BIG");
160 /* New userspace (success) case -- (usize > ksize). */
164 memcpy(expected, umem_src, ksize);
165 ret |= test(clear_user(umem + ksize, usize - ksize),
166 "legitimate clear_user failed");
168 memset(kmem, 0x0, size);
169 ret |= test(copy_struct_from_user(kmem, ksize, umem, usize),
170 "copy_struct_from_user(usize > ksize) failed");
171 ret |= test(memcmp(kmem, expected, ksize),
172 "copy_struct_from_user(usize > ksize) gives unexpected copy");
180 static int __init test_user_copy_init(void)
184 char __user *usermem;
186 unsigned long user_addr;
194 kmem = kmalloc(PAGE_SIZE * 2, GFP_KERNEL);
198 user_addr = vm_mmap(NULL, 0, PAGE_SIZE * 2,
199 PROT_READ | PROT_WRITE | PROT_EXEC,
200 MAP_ANONYMOUS | MAP_PRIVATE, 0);
201 if (user_addr >= (unsigned long)(TASK_SIZE)) {
202 pr_warn("Failed to allocate user memory\n");
207 usermem = (char __user *)user_addr;
208 bad_usermem = (char *)user_addr;
211 * Legitimate usage: none of these copies should fail.
213 memset(kmem, 0x3a, PAGE_SIZE * 2);
214 ret |= test(copy_to_user(usermem, kmem, PAGE_SIZE),
215 "legitimate copy_to_user failed");
216 memset(kmem, 0x0, PAGE_SIZE);
217 ret |= test(copy_from_user(kmem, usermem, PAGE_SIZE),
218 "legitimate copy_from_user failed");
219 ret |= test(memcmp(kmem, kmem + PAGE_SIZE, PAGE_SIZE),
220 "legitimate usercopy failed to copy data");
222 #define test_legit(size, check) \
224 val_##size = check; \
225 ret |= test(put_user(val_##size, (size __user *)usermem), \
226 "legitimate put_user (" #size ") failed"); \
228 ret |= test(get_user(val_##size, (size __user *)usermem), \
229 "legitimate get_user (" #size ") failed"); \
230 ret |= test(val_##size != check, \
231 "legitimate get_user (" #size ") failed to do copy"); \
232 if (val_##size != check) { \
233 pr_info("0x%llx != 0x%llx\n", \
234 (unsigned long long)val_##size, \
235 (unsigned long long)check); \
239 test_legit(u8, 0x5a);
240 test_legit(u16, 0x5a5b);
241 test_legit(u32, 0x5a5b5c5d);
243 test_legit(u64, 0x5a5b5c5d6a6b6c6d);
247 /* Test usage of check_nonzero_user(). */
248 ret |= test_check_nonzero_user(kmem, usermem, 2 * PAGE_SIZE);
249 /* Test usage of copy_struct_from_user(). */
250 ret |= test_copy_struct_from_user(kmem, usermem, 2 * PAGE_SIZE);
253 * Invalid usage: none of these copies should succeed.
256 /* Prepare kernel memory with check values. */
257 memset(kmem, 0x5a, PAGE_SIZE);
258 memset(kmem + PAGE_SIZE, 0, PAGE_SIZE);
260 /* Reject kernel-to-kernel copies through copy_from_user(). */
261 ret |= test(!copy_from_user(kmem, (char __user *)(kmem + PAGE_SIZE),
263 "illegal all-kernel copy_from_user passed");
265 /* Destination half of buffer should have been zeroed. */
266 ret |= test(memcmp(kmem + PAGE_SIZE, kmem, PAGE_SIZE),
267 "zeroing failure for illegal all-kernel copy_from_user");
271 * When running with SMAP/PAN/etc, this will Oops the kernel
272 * due to the zeroing of userspace memory on failure. This needs
273 * to be tested in LKDTM instead, since this test module does not
276 ret |= test(!copy_from_user(bad_usermem, (char __user *)kmem,
278 "illegal reversed copy_from_user passed");
280 ret |= test(!copy_to_user((char __user *)kmem, kmem + PAGE_SIZE,
282 "illegal all-kernel copy_to_user passed");
283 ret |= test(!copy_to_user((char __user *)kmem, bad_usermem,
285 "illegal reversed copy_to_user passed");
287 #define test_illegal(size, check) \
289 val_##size = (check); \
290 ret |= test(!get_user(val_##size, (size __user *)kmem), \
291 "illegal get_user (" #size ") passed"); \
292 ret |= test(val_##size != (size)0, \
293 "zeroing failure for illegal get_user (" #size ")"); \
294 if (val_##size != (size)0) { \
295 pr_info("0x%llx != 0\n", \
296 (unsigned long long)val_##size); \
298 ret |= test(!put_user(val_##size, (size __user *)kmem), \
299 "illegal put_user (" #size ") passed"); \
302 test_illegal(u8, 0x5a);
303 test_illegal(u16, 0x5a5b);
304 test_illegal(u32, 0x5a5b5c5d);
306 test_illegal(u64, 0x5a5b5c5d6a6b6c6d);
310 vm_munmap(user_addr, PAGE_SIZE * 2);
314 pr_info("tests passed.\n");
321 module_init(test_user_copy_init);
323 static void __exit test_user_copy_exit(void)
325 pr_info("unloaded.\n");
328 module_exit(test_user_copy_exit);
330 MODULE_AUTHOR("Kees Cook <keescook@chromium.org>");
331 MODULE_LICENSE("GPL");