a6556f3364d167e15f91eec6cd39f369d130cd8b
[linux-2.6-microblaze.git] / lib / test_user_copy.c
1 /*
2  * Kernel module for testing copy_to/from_user infrastructure.
3  *
4  * Copyright 2013 Google Inc. All Rights Reserved
5  *
6  * Authors:
7  *      Kees Cook       <keescook@chromium.org>
8  *
9  * This software is licensed under the terms of the GNU General Public
10  * License version 2, as published by the Free Software Foundation, and
11  * may be copied, distributed, and modified under those terms.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  */
18
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21 #include <linux/mman.h>
22 #include <linux/module.h>
23 #include <linux/sched.h>
24 #include <linux/slab.h>
25 #include <linux/uaccess.h>
26 #include <linux/vmalloc.h>
27
28 /*
29  * Several 32-bit architectures support 64-bit {get,put}_user() calls.
30  * As there doesn't appear to be anything that can safely determine
31  * their capability at compile-time, we just have to opt-out certain archs.
32  */
33 #if BITS_PER_LONG == 64 || (!(defined(CONFIG_ARM) && !defined(MMU)) && \
34                             !defined(CONFIG_BLACKFIN) &&        \
35                             !defined(CONFIG_M32R) &&            \
36                             !defined(CONFIG_M68K) &&            \
37                             !defined(CONFIG_MICROBLAZE) &&      \
38                             !defined(CONFIG_NIOS2) &&           \
39                             !defined(CONFIG_PPC32) &&           \
40                             !defined(CONFIG_SUPERH))
41 # define TEST_U64
42 #endif
43
44 #define test(condition, msg)            \
45 ({                                      \
46         int cond = (condition);         \
47         if (cond)                       \
48                 pr_warn("%s\n", msg);   \
49         cond;                           \
50 })
51
52 static int __init test_user_copy_init(void)
53 {
54         int ret = 0;
55         char *kmem;
56         char __user *usermem;
57         char *bad_usermem;
58         unsigned long user_addr;
59         u8 val_u8;
60         u16 val_u16;
61         u32 val_u32;
62 #ifdef TEST_U64
63         u64 val_u64;
64 #endif
65
66         kmem = kmalloc(PAGE_SIZE * 2, GFP_KERNEL);
67         if (!kmem)
68                 return -ENOMEM;
69
70         user_addr = vm_mmap(NULL, 0, PAGE_SIZE * 2,
71                             PROT_READ | PROT_WRITE | PROT_EXEC,
72                             MAP_ANONYMOUS | MAP_PRIVATE, 0);
73         if (user_addr >= (unsigned long)(TASK_SIZE)) {
74                 pr_warn("Failed to allocate user memory\n");
75                 kfree(kmem);
76                 return -ENOMEM;
77         }
78
79         usermem = (char __user *)user_addr;
80         bad_usermem = (char *)user_addr;
81
82         /*
83          * Legitimate usage: none of these copies should fail.
84          */
85         memset(kmem, 0x3a, PAGE_SIZE * 2);
86         ret |= test(copy_to_user(usermem, kmem, PAGE_SIZE),
87                     "legitimate copy_to_user failed");
88         memset(kmem, 0x0, PAGE_SIZE);
89         ret |= test(copy_from_user(kmem, usermem, PAGE_SIZE),
90                     "legitimate copy_from_user failed");
91         ret |= test(memcmp(kmem, kmem + PAGE_SIZE, PAGE_SIZE),
92                     "legitimate usercopy failed to copy data");
93
94 #define test_legit(size, check)                                           \
95         do {                                                              \
96                 val_##size = check;                                       \
97                 ret |= test(put_user(val_##size, (size __user *)usermem), \
98                     "legitimate put_user (" #size ") failed");            \
99                 val_##size = 0;                                           \
100                 ret |= test(get_user(val_##size, (size __user *)usermem), \
101                     "legitimate get_user (" #size ") failed");            \
102                 ret |= test(val_##size != check,                          \
103                     "legitimate get_user (" #size ") failed to do copy"); \
104                 if (val_##size != check) {                                \
105                         pr_info("0x%llx != 0x%llx\n",                     \
106                                 (unsigned long long)val_##size,           \
107                                 (unsigned long long)check);               \
108                 }                                                         \
109         } while (0)
110
111         test_legit(u8,  0x5a);
112         test_legit(u16, 0x5a5b);
113         test_legit(u32, 0x5a5b5c5d);
114 #ifdef TEST_U64
115         test_legit(u64, 0x5a5b5c5d6a6b6c6d);
116 #endif
117 #undef test_legit
118
119         /*
120          * Invalid usage: none of these copies should succeed.
121          */
122
123         /* Prepare kernel memory with check values. */
124         memset(kmem, 0x5a, PAGE_SIZE);
125         memset(kmem + PAGE_SIZE, 0, PAGE_SIZE);
126
127         /* Reject kernel-to-kernel copies through copy_from_user(). */
128         ret |= test(!copy_from_user(kmem, (char __user *)(kmem + PAGE_SIZE),
129                                     PAGE_SIZE),
130                     "illegal all-kernel copy_from_user passed");
131
132         /* Destination half of buffer should have been zeroed. */
133         ret |= test(memcmp(kmem + PAGE_SIZE, kmem, PAGE_SIZE),
134                     "zeroing failure for illegal all-kernel copy_from_user");
135
136 #if 0
137         /*
138          * When running with SMAP/PAN/etc, this will Oops the kernel
139          * due to the zeroing of userspace memory on failure. This needs
140          * to be tested in LKDTM instead, since this test module does not
141          * expect to explode.
142          */
143         ret |= test(!copy_from_user(bad_usermem, (char __user *)kmem,
144                                     PAGE_SIZE),
145                     "illegal reversed copy_from_user passed");
146 #endif
147         ret |= test(!copy_to_user((char __user *)kmem, kmem + PAGE_SIZE,
148                                   PAGE_SIZE),
149                     "illegal all-kernel copy_to_user passed");
150         ret |= test(!copy_to_user((char __user *)kmem, bad_usermem,
151                                   PAGE_SIZE),
152                     "illegal reversed copy_to_user passed");
153
154 #define test_illegal(size, check)                                           \
155         do {                                                                \
156                 val_##size = (check);                                       \
157                 ret |= test(!get_user(val_##size, (size __user *)kmem),     \
158                     "illegal get_user (" #size ") passed");                 \
159                 ret |= test(val_##size != (size)0,                          \
160                     "zeroing failure for illegal get_user (" #size ")");    \
161                 if (val_##size != (size)0) {                                \
162                         pr_info("0x%llx != 0\n",                            \
163                                 (unsigned long long)val_##size);            \
164                 }                                                           \
165                 ret |= test(!put_user(val_##size, (size __user *)kmem),     \
166                     "illegal put_user (" #size ") passed");                 \
167         } while (0)
168
169         test_illegal(u8,  0x5a);
170         test_illegal(u16, 0x5a5b);
171         test_illegal(u32, 0x5a5b5c5d);
172 #ifdef TEST_U64
173         test_illegal(u64, 0x5a5b5c5d6a6b6c6d);
174 #endif
175 #undef test_illegal
176
177         vm_munmap(user_addr, PAGE_SIZE * 2);
178         kfree(kmem);
179
180         if (ret == 0) {
181                 pr_info("tests passed.\n");
182                 return 0;
183         }
184
185         return -EINVAL;
186 }
187
188 module_init(test_user_copy_init);
189
190 static void __exit test_user_copy_exit(void)
191 {
192         pr_info("unloaded.\n");
193 }
194
195 module_exit(test_user_copy_exit);
196
197 MODULE_AUTHOR("Kees Cook <keescook@chromium.org>");
198 MODULE_LICENSE("GPL");