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("%s\n", msg); \
42 static int __init test_user_copy_init(void)
48 unsigned long user_addr;
56 kmem = kmalloc(PAGE_SIZE * 2, GFP_KERNEL);
60 user_addr = vm_mmap(NULL, 0, PAGE_SIZE * 2,
61 PROT_READ | PROT_WRITE | PROT_EXEC,
62 MAP_ANONYMOUS | MAP_PRIVATE, 0);
63 if (user_addr >= (unsigned long)(TASK_SIZE)) {
64 pr_warn("Failed to allocate user memory\n");
69 usermem = (char __user *)user_addr;
70 bad_usermem = (char *)user_addr;
73 * Legitimate usage: none of these copies should fail.
75 memset(kmem, 0x3a, PAGE_SIZE * 2);
76 ret |= test(copy_to_user(usermem, kmem, PAGE_SIZE),
77 "legitimate copy_to_user failed");
78 memset(kmem, 0x0, PAGE_SIZE);
79 ret |= test(copy_from_user(kmem, usermem, PAGE_SIZE),
80 "legitimate copy_from_user failed");
81 ret |= test(memcmp(kmem, kmem + PAGE_SIZE, PAGE_SIZE),
82 "legitimate usercopy failed to copy data");
84 #define test_legit(size, check) \
87 ret |= test(put_user(val_##size, (size __user *)usermem), \
88 "legitimate put_user (" #size ") failed"); \
90 ret |= test(get_user(val_##size, (size __user *)usermem), \
91 "legitimate get_user (" #size ") failed"); \
92 ret |= test(val_##size != check, \
93 "legitimate get_user (" #size ") failed to do copy"); \
94 if (val_##size != check) { \
95 pr_info("0x%llx != 0x%llx\n", \
96 (unsigned long long)val_##size, \
97 (unsigned long long)check); \
101 test_legit(u8, 0x5a);
102 test_legit(u16, 0x5a5b);
103 test_legit(u32, 0x5a5b5c5d);
105 test_legit(u64, 0x5a5b5c5d6a6b6c6d);
110 * Invalid usage: none of these copies should succeed.
113 /* Prepare kernel memory with check values. */
114 memset(kmem, 0x5a, PAGE_SIZE);
115 memset(kmem + PAGE_SIZE, 0, PAGE_SIZE);
117 /* Reject kernel-to-kernel copies through copy_from_user(). */
118 ret |= test(!copy_from_user(kmem, (char __user *)(kmem + PAGE_SIZE),
120 "illegal all-kernel copy_from_user passed");
122 /* Destination half of buffer should have been zeroed. */
123 ret |= test(memcmp(kmem + PAGE_SIZE, kmem, PAGE_SIZE),
124 "zeroing failure for illegal all-kernel copy_from_user");
128 * When running with SMAP/PAN/etc, this will Oops the kernel
129 * due to the zeroing of userspace memory on failure. This needs
130 * to be tested in LKDTM instead, since this test module does not
133 ret |= test(!copy_from_user(bad_usermem, (char __user *)kmem,
135 "illegal reversed copy_from_user passed");
137 ret |= test(!copy_to_user((char __user *)kmem, kmem + PAGE_SIZE,
139 "illegal all-kernel copy_to_user passed");
140 ret |= test(!copy_to_user((char __user *)kmem, bad_usermem,
142 "illegal reversed copy_to_user passed");
144 #define test_illegal(size, check) \
146 val_##size = (check); \
147 ret |= test(!get_user(val_##size, (size __user *)kmem), \
148 "illegal get_user (" #size ") passed"); \
149 ret |= test(val_##size != (size)0, \
150 "zeroing failure for illegal get_user (" #size ")"); \
151 if (val_##size != (size)0) { \
152 pr_info("0x%llx != 0\n", \
153 (unsigned long long)val_##size); \
155 ret |= test(!put_user(val_##size, (size __user *)kmem), \
156 "illegal put_user (" #size ") passed"); \
159 test_illegal(u8, 0x5a);
160 test_illegal(u16, 0x5a5b);
161 test_illegal(u32, 0x5a5b5c5d);
163 test_illegal(u64, 0x5a5b5c5d6a6b6c6d);
167 vm_munmap(user_addr, PAGE_SIZE * 2);
171 pr_info("tests passed.\n");
178 module_init(test_user_copy_init);
180 static void __exit test_user_copy_exit(void)
182 pr_info("unloaded.\n");
185 module_exit(test_user_copy_exit);
187 MODULE_AUTHOR("Kees Cook <keescook@chromium.org>");
188 MODULE_LICENSE("GPL");