blob: 73ff7a628e3a14c88c4b861e78225891d45cd571 [file] [log] [blame]
Kees Cook3e2a4c12014-01-23 15:54:38 -08001/*
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#define test(condition, msg) \
29({ \
30 int cond = (condition); \
31 if (cond) \
32 pr_warn("%s\n", msg); \
33 cond; \
34})
35
36static int __init test_user_copy_init(void)
37{
38 int ret = 0;
39 char *kmem;
40 char __user *usermem;
41 char *bad_usermem;
42 unsigned long user_addr;
43 unsigned long value = 0x5A;
44
45 kmem = kmalloc(PAGE_SIZE * 2, GFP_KERNEL);
46 if (!kmem)
47 return -ENOMEM;
48
49 user_addr = vm_mmap(NULL, 0, PAGE_SIZE * 2,
50 PROT_READ | PROT_WRITE | PROT_EXEC,
51 MAP_ANONYMOUS | MAP_PRIVATE, 0);
52 if (user_addr >= (unsigned long)(TASK_SIZE)) {
53 pr_warn("Failed to allocate user memory\n");
54 kfree(kmem);
55 return -ENOMEM;
56 }
57
58 usermem = (char __user *)user_addr;
59 bad_usermem = (char *)user_addr;
60
Kees Cookf5f893c2017-02-13 11:25:26 -080061 /*
62 * Legitimate usage: none of these copies should fail.
63 */
Kees Cook3e2a4c12014-01-23 15:54:38 -080064 ret |= test(copy_from_user(kmem, usermem, PAGE_SIZE),
65 "legitimate copy_from_user failed");
66 ret |= test(copy_to_user(usermem, kmem, PAGE_SIZE),
67 "legitimate copy_to_user failed");
68 ret |= test(get_user(value, (unsigned long __user *)usermem),
69 "legitimate get_user failed");
70 ret |= test(put_user(value, (unsigned long __user *)usermem),
71 "legitimate put_user failed");
72
Kees Cookf5f893c2017-02-13 11:25:26 -080073 /*
74 * Invalid usage: none of these copies should succeed.
75 */
76
77 /* Prepare kernel memory with check values. */
Hoeun Ryu4fbfeb82017-02-12 15:13:33 +090078 memset(kmem, 0x5a, PAGE_SIZE);
79 memset(kmem + PAGE_SIZE, 0, PAGE_SIZE);
Kees Cookf5f893c2017-02-13 11:25:26 -080080
81 /* Reject kernel-to-kernel copies through copy_from_user(). */
Kees Cook3e2a4c12014-01-23 15:54:38 -080082 ret |= test(!copy_from_user(kmem, (char __user *)(kmem + PAGE_SIZE),
83 PAGE_SIZE),
84 "illegal all-kernel copy_from_user passed");
Kees Cookf5f893c2017-02-13 11:25:26 -080085
86 /* Destination half of buffer should have been zeroed. */
Hoeun Ryu4fbfeb82017-02-12 15:13:33 +090087 ret |= test(memcmp(kmem + PAGE_SIZE, kmem, PAGE_SIZE),
88 "zeroing failure for illegal all-kernel copy_from_user");
Kees Cookf5f893c2017-02-13 11:25:26 -080089
90#if 0
91 /*
92 * When running with SMAP/PAN/etc, this will Oops the kernel
93 * due to the zeroing of userspace memory on failure. This needs
94 * to be tested in LKDTM instead, since this test module does not
95 * expect to explode.
96 */
Kees Cook3e2a4c12014-01-23 15:54:38 -080097 ret |= test(!copy_from_user(bad_usermem, (char __user *)kmem,
98 PAGE_SIZE),
99 "illegal reversed copy_from_user passed");
Kees Cookf5f893c2017-02-13 11:25:26 -0800100#endif
Kees Cook3e2a4c12014-01-23 15:54:38 -0800101 ret |= test(!copy_to_user((char __user *)kmem, kmem + PAGE_SIZE,
102 PAGE_SIZE),
103 "illegal all-kernel copy_to_user passed");
104 ret |= test(!copy_to_user((char __user *)kmem, bad_usermem,
105 PAGE_SIZE),
106 "illegal reversed copy_to_user passed");
Kees Cookf5f893c2017-02-13 11:25:26 -0800107
108 value = 0x5a;
Kees Cook3e2a4c12014-01-23 15:54:38 -0800109 ret |= test(!get_user(value, (unsigned long __user *)kmem),
110 "illegal get_user passed");
Kees Cookf5f893c2017-02-13 11:25:26 -0800111 ret |= test(value != 0, "zeroing failure for illegal get_user");
Kees Cook3e2a4c12014-01-23 15:54:38 -0800112 ret |= test(!put_user(value, (unsigned long __user *)kmem),
113 "illegal put_user passed");
114
115 vm_munmap(user_addr, PAGE_SIZE * 2);
116 kfree(kmem);
117
118 if (ret == 0) {
119 pr_info("tests passed.\n");
120 return 0;
121 }
122
123 return -EINVAL;
124}
125
126module_init(test_user_copy_init);
127
128static void __exit test_user_copy_exit(void)
129{
130 pr_info("unloaded.\n");
131}
132
133module_exit(test_user_copy_exit);
134
135MODULE_AUTHOR("Kees Cook <keescook@chromium.org>");
136MODULE_LICENSE("GPL");