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 <kunit/test.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 struct usercopy_test_priv
{
40 static bool is_zeroed(void *from
, size_t size
)
42 return memchr_inv(from
, 0x0, size
) == NULL
;
45 /* Test usage of check_nonzero_user(). */
46 static void usercopy_test_check_nonzero_user(struct kunit
*test
)
48 size_t start
, end
, i
, zero_start
, zero_end
;
49 struct usercopy_test_priv
*priv
= test
->priv
;
50 char __user
*umem
= priv
->umem
;
51 char *kmem
= priv
->kmem
;
52 size_t size
= priv
->size
;
54 KUNIT_ASSERT_GE_MSG(test
, size
, 2 * PAGE_SIZE
, "buffer too small");
57 * We want to cross a page boundary to exercise the code more
58 * effectively. We also don't want to make the size we scan too large,
59 * otherwise the test can take a long time and cause soft lockups. So
60 * scan a 1024 byte region across the page boundary.
63 start
= PAGE_SIZE
- (size
/ 2);
68 zero_start
= size
/ 4;
69 zero_end
= size
- zero_start
;
72 * We conduct a series of check_nonzero_user() tests on a block of
73 * memory with the following byte-pattern (trying every possible
76 * [ 00 ff 00 ff ... 00 00 00 00 ... ff 00 ff 00 ]
78 * And we verify that check_nonzero_user() acts identically to
82 memset(kmem
, 0x0, size
);
83 for (i
= 1; i
< zero_start
; i
+= 2)
85 for (i
= zero_end
; i
< size
; i
+= 2)
88 KUNIT_EXPECT_EQ_MSG(test
, copy_to_user(umem
, kmem
, size
), 0,
89 "legitimate copy_to_user failed");
91 for (start
= 0; start
<= size
; start
++) {
92 for (end
= start
; end
<= size
; end
++) {
93 size_t len
= end
- start
;
94 int retval
= check_zeroed_user(umem
+ start
, len
);
95 int expected
= is_zeroed(kmem
+ start
, len
);
97 KUNIT_ASSERT_EQ_MSG(test
, retval
, expected
,
98 "check_nonzero_user(=%d) != memchr_inv(=%d) mismatch (start=%zu, end=%zu)",
99 retval
, expected
, start
, end
);
104 /* Test usage of copy_struct_from_user(). */
105 static void usercopy_test_copy_struct_from_user(struct kunit
*test
)
107 char *umem_src
= NULL
, *expected
= NULL
;
108 struct usercopy_test_priv
*priv
= test
->priv
;
109 char __user
*umem
= priv
->umem
;
110 char *kmem
= priv
->kmem
;
111 size_t size
= priv
->size
;
114 umem_src
= kunit_kmalloc(test
, size
, GFP_KERNEL
);
115 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, umem_src
);
117 expected
= kunit_kmalloc(test
, size
, GFP_KERNEL
);
118 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, expected
);
120 /* Fill umem with a fixed byte pattern. */
121 memset(umem_src
, 0x3e, size
);
122 KUNIT_ASSERT_EQ_MSG(test
, copy_to_user(umem
, umem_src
, size
), 0,
123 "legitimate copy_to_user failed");
125 /* Check basic case -- (usize == ksize). */
129 memcpy(expected
, umem_src
, ksize
);
131 memset(kmem
, 0x0, size
);
132 KUNIT_EXPECT_EQ_MSG(test
, copy_struct_from_user(kmem
, ksize
, umem
, usize
), 0,
133 "copy_struct_from_user(usize == ksize) failed");
134 KUNIT_EXPECT_MEMEQ_MSG(test
, kmem
, expected
, ksize
,
135 "copy_struct_from_user(usize == ksize) gives unexpected copy");
137 /* Old userspace case -- (usize < ksize). */
141 memcpy(expected
, umem_src
, usize
);
142 memset(expected
+ usize
, 0x0, ksize
- usize
);
144 memset(kmem
, 0x0, size
);
145 KUNIT_EXPECT_EQ_MSG(test
, copy_struct_from_user(kmem
, ksize
, umem
, usize
), 0,
146 "copy_struct_from_user(usize < ksize) failed");
147 KUNIT_EXPECT_MEMEQ_MSG(test
, kmem
, expected
, ksize
,
148 "copy_struct_from_user(usize < ksize) gives unexpected copy");
150 /* New userspace (-E2BIG) case -- (usize > ksize). */
154 memset(kmem
, 0x0, size
);
155 KUNIT_EXPECT_EQ_MSG(test
, copy_struct_from_user(kmem
, ksize
, umem
, usize
), -E2BIG
,
156 "copy_struct_from_user(usize > ksize) didn't give E2BIG");
158 /* New userspace (success) case -- (usize > ksize). */
162 memcpy(expected
, umem_src
, ksize
);
163 KUNIT_EXPECT_EQ_MSG(test
, clear_user(umem
+ ksize
, usize
- ksize
), 0,
164 "legitimate clear_user failed");
166 memset(kmem
, 0x0, size
);
167 KUNIT_EXPECT_EQ_MSG(test
, copy_struct_from_user(kmem
, ksize
, umem
, usize
), 0,
168 "copy_struct_from_user(usize > ksize) failed");
169 KUNIT_EXPECT_MEMEQ_MSG(test
, kmem
, expected
, ksize
,
170 "copy_struct_from_user(usize > ksize) gives unexpected copy");
174 * Legitimate usage: none of these copies should fail.
176 static void usercopy_test_valid(struct kunit
*test
)
178 struct usercopy_test_priv
*priv
= test
->priv
;
179 char __user
*usermem
= priv
->umem
;
180 char *kmem
= priv
->kmem
;
182 memset(kmem
, 0x3a, PAGE_SIZE
* 2);
183 KUNIT_EXPECT_EQ_MSG(test
, 0, copy_to_user(usermem
, kmem
, PAGE_SIZE
),
184 "legitimate copy_to_user failed");
185 memset(kmem
, 0x0, PAGE_SIZE
);
186 KUNIT_EXPECT_EQ_MSG(test
, 0, copy_from_user(kmem
, usermem
, PAGE_SIZE
),
187 "legitimate copy_from_user failed");
188 KUNIT_EXPECT_MEMEQ_MSG(test
, kmem
, kmem
+ PAGE_SIZE
, PAGE_SIZE
,
189 "legitimate usercopy failed to copy data");
191 #define test_legit(size, check) \
193 size val_##size = (check); \
194 KUNIT_EXPECT_EQ_MSG(test, 0, \
195 put_user(val_##size, (size __user *)usermem), \
196 "legitimate put_user (" #size ") failed"); \
198 KUNIT_EXPECT_EQ_MSG(test, 0, \
199 get_user(val_##size, (size __user *)usermem), \
200 "legitimate get_user (" #size ") failed"); \
201 KUNIT_EXPECT_EQ_MSG(test, val_##size, check, \
202 "legitimate get_user (" #size ") failed to do copy"); \
205 test_legit(u8
, 0x5a);
206 test_legit(u16
, 0x5a5b);
207 test_legit(u32
, 0x5a5b5c5d);
209 test_legit(u64
, 0x5a5b5c5d6a6b6c6d);
215 * Invalid usage: none of these copies should succeed.
217 static void usercopy_test_invalid(struct kunit
*test
)
219 struct usercopy_test_priv
*priv
= test
->priv
;
220 char __user
*usermem
= priv
->umem
;
221 char *bad_usermem
= (char *)usermem
;
222 char *kmem
= priv
->kmem
;
223 u64
*kmem_u64
= (u64
*)kmem
;
225 if (IS_ENABLED(CONFIG_ALTERNATE_USER_ADDRESS_SPACE
) ||
226 !IS_ENABLED(CONFIG_MMU
)) {
227 kunit_skip(test
, "Testing for kernel/userspace address confusion is only sensible on architectures with a shared address space");
231 /* Prepare kernel memory with check values. */
232 memset(kmem
, 0x5a, PAGE_SIZE
);
233 memset(kmem
+ PAGE_SIZE
, 0, PAGE_SIZE
);
235 /* Reject kernel-to-kernel copies through copy_from_user(). */
236 KUNIT_EXPECT_NE_MSG(test
, copy_from_user(kmem
, (char __user
*)(kmem
+ PAGE_SIZE
),
238 "illegal all-kernel copy_from_user passed");
240 /* Destination half of buffer should have been zeroed. */
241 KUNIT_EXPECT_MEMEQ_MSG(test
, kmem
+ PAGE_SIZE
, kmem
, PAGE_SIZE
,
242 "zeroing failure for illegal all-kernel copy_from_user");
246 * When running with SMAP/PAN/etc, this will Oops the kernel
247 * due to the zeroing of userspace memory on failure. This needs
248 * to be tested in LKDTM instead, since this test module does not
251 KUNIT_EXPECT_NE_MSG(test
, copy_from_user(bad_usermem
, (char __user
*)kmem
,
253 "illegal reversed copy_from_user passed");
255 KUNIT_EXPECT_NE_MSG(test
, copy_to_user((char __user
*)kmem
, kmem
+ PAGE_SIZE
,
257 "illegal all-kernel copy_to_user passed");
259 KUNIT_EXPECT_NE_MSG(test
, copy_to_user((char __user
*)kmem
, bad_usermem
,
261 "illegal reversed copy_to_user passed");
263 #define test_illegal(size, check) \
265 size val_##size = (check); \
267 KUNIT_EXPECT_NE_MSG(test, get_user(val_##size, (size __user *)kmem), 0, \
268 "illegal get_user (" #size ") passed"); \
269 KUNIT_EXPECT_EQ_MSG(test, val_##size, 0, \
270 "zeroing failure for illegal get_user (" #size ")"); \
272 *kmem_u64 = 0xF09FA4AFF09FA4AF; \
273 KUNIT_EXPECT_NE_MSG(test, put_user(val_##size, (size __user *)kmem), 0, \
274 "illegal put_user (" #size ") passed"); \
275 KUNIT_EXPECT_EQ_MSG(test, *kmem_u64, 0xF09FA4AFF09FA4AF, \
276 "illegal put_user (" #size ") wrote to kernel memory!"); \
279 test_illegal(u8
, 0x5a);
280 test_illegal(u16
, 0x5a5b);
281 test_illegal(u32
, 0x5a5b5c5d);
283 test_illegal(u64
, 0x5a5b5c5d6a6b6c6d);
288 static int usercopy_test_init(struct kunit
*test
)
290 struct usercopy_test_priv
*priv
;
291 unsigned long user_addr
;
293 if (!IS_ENABLED(CONFIG_MMU
)) {
294 kunit_skip(test
, "Userspace allocation testing not available on non-MMU systems");
298 priv
= kunit_kzalloc(test
, sizeof(*priv
), GFP_KERNEL
);
299 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, priv
);
301 priv
->size
= PAGE_SIZE
* 2;
303 priv
->kmem
= kunit_kmalloc(test
, priv
->size
, GFP_KERNEL
);
304 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, priv
->kmem
);
306 user_addr
= kunit_vm_mmap(test
, NULL
, 0, priv
->size
,
307 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
308 MAP_ANONYMOUS
| MAP_PRIVATE
, 0);
309 KUNIT_ASSERT_NE_MSG(test
, user_addr
, 0,
310 "Could not create userspace mm");
311 KUNIT_ASSERT_LT_MSG(test
, user_addr
, (unsigned long)TASK_SIZE
,
312 "Failed to allocate user memory");
313 priv
->umem
= (char __user
*)user_addr
;
318 static struct kunit_case usercopy_test_cases
[] = {
319 KUNIT_CASE(usercopy_test_valid
),
320 KUNIT_CASE(usercopy_test_invalid
),
321 KUNIT_CASE(usercopy_test_check_nonzero_user
),
322 KUNIT_CASE(usercopy_test_copy_struct_from_user
),
326 static struct kunit_suite usercopy_test_suite
= {
328 .init
= usercopy_test_init
,
329 .test_cases
= usercopy_test_cases
,
332 kunit_test_suites(&usercopy_test_suite
);
333 MODULE_AUTHOR("Kees Cook <kees@kernel.org>");
334 MODULE_DESCRIPTION("Kernel module for testing copy_to/from_user infrastructure");
335 MODULE_LICENSE("GPL");