xref: /linux/lib/test_user_copy.c (revision f418dddf)
19c92ab61SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
23e2a4c18SKees Cook /*
33e2a4c18SKees Cook  * Kernel module for testing copy_to/from_user infrastructure.
43e2a4c18SKees Cook  *
53e2a4c18SKees Cook  * Copyright 2013 Google Inc. All Rights Reserved
63e2a4c18SKees Cook  *
73e2a4c18SKees Cook  * Authors:
83e2a4c18SKees Cook  *      Kees Cook       <keescook@chromium.org>
93e2a4c18SKees Cook  */
103e2a4c18SKees Cook 
113e2a4c18SKees Cook #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
123e2a4c18SKees Cook 
133e2a4c18SKees Cook #include <linux/mman.h>
143e2a4c18SKees Cook #include <linux/module.h>
153e2a4c18SKees Cook #include <linux/sched.h>
163e2a4c18SKees Cook #include <linux/slab.h>
173e2a4c18SKees Cook #include <linux/uaccess.h>
183e2a4c18SKees Cook #include <linux/vmalloc.h>
193e2a4c18SKees Cook 
204c5d7bc6SKees Cook /*
214c5d7bc6SKees Cook  * Several 32-bit architectures support 64-bit {get,put}_user() calls.
224c5d7bc6SKees Cook  * As there doesn't appear to be anything that can safely determine
234c5d7bc6SKees Cook  * their capability at compile-time, we just have to opt-out certain archs.
244c5d7bc6SKees Cook  */
254deaa6fdSArnd Bergmann #if BITS_PER_LONG == 64 || (!(defined(CONFIG_ARM) && !defined(MMU)) && \
264c5d7bc6SKees Cook 			    !defined(CONFIG_M68K) &&		\
274c5d7bc6SKees Cook 			    !defined(CONFIG_MICROBLAZE) &&	\
284c5d7bc6SKees Cook 			    !defined(CONFIG_NIOS2) &&		\
294c5d7bc6SKees Cook 			    !defined(CONFIG_PPC32) &&		\
304c5d7bc6SKees Cook 			    !defined(CONFIG_SUPERH))
314c5d7bc6SKees Cook # define TEST_U64
324c5d7bc6SKees Cook #endif
334c5d7bc6SKees Cook 
34f5a1a536SAleksa Sarai #define test(condition, msg, ...)					\
353e2a4c18SKees Cook ({									\
363e2a4c18SKees Cook 	int cond = (condition);						\
373e2a4c18SKees Cook 	if (cond)							\
38f5a1a536SAleksa Sarai 		pr_warn("[%d] " msg "\n", __LINE__, ##__VA_ARGS__);	\
393e2a4c18SKees Cook 	cond;								\
403e2a4c18SKees Cook })
413e2a4c18SKees Cook 
is_zeroed(void * from,size_t size)42f5a1a536SAleksa Sarai static bool is_zeroed(void *from, size_t size)
43f5a1a536SAleksa Sarai {
44f5a1a536SAleksa Sarai 	return memchr_inv(from, 0x0, size) == NULL;
45f5a1a536SAleksa Sarai }
46f5a1a536SAleksa Sarai 
test_check_nonzero_user(char * kmem,char __user * umem,size_t size)47f5a1a536SAleksa Sarai static int test_check_nonzero_user(char *kmem, char __user *umem, size_t size)
48f5a1a536SAleksa Sarai {
49f5a1a536SAleksa Sarai 	int ret = 0;
50*f418dddfSMichael Ellerman 	size_t start, end, i, zero_start, zero_end;
51*f418dddfSMichael Ellerman 
52*f418dddfSMichael Ellerman 	if (test(size < 2 * PAGE_SIZE, "buffer too small"))
53*f418dddfSMichael Ellerman 		return -EINVAL;
54*f418dddfSMichael Ellerman 
55*f418dddfSMichael Ellerman 	/*
56*f418dddfSMichael Ellerman 	 * We want to cross a page boundary to exercise the code more
57*f418dddfSMichael Ellerman 	 * effectively. We also don't want to make the size we scan too large,
58*f418dddfSMichael Ellerman 	 * otherwise the test can take a long time and cause soft lockups. So
59*f418dddfSMichael Ellerman 	 * scan a 1024 byte region across the page boundary.
60*f418dddfSMichael Ellerman 	 */
61*f418dddfSMichael Ellerman 	size = 1024;
62*f418dddfSMichael Ellerman 	start = PAGE_SIZE - (size / 2);
63*f418dddfSMichael Ellerman 
64*f418dddfSMichael Ellerman 	kmem += start;
65*f418dddfSMichael Ellerman 	umem += start;
66*f418dddfSMichael Ellerman 
67*f418dddfSMichael Ellerman 	zero_start = size / 4;
68*f418dddfSMichael Ellerman 	zero_end = size - zero_start;
69f5a1a536SAleksa Sarai 
70f5a1a536SAleksa Sarai 	/*
71c90012acSAleksa Sarai 	 * We conduct a series of check_nonzero_user() tests on a block of
72c90012acSAleksa Sarai 	 * memory with the following byte-pattern (trying every possible
73c90012acSAleksa Sarai 	 * [start,end] pair):
74f5a1a536SAleksa Sarai 	 *
75f5a1a536SAleksa Sarai 	 *   [ 00 ff 00 ff ... 00 00 00 00 ... ff 00 ff 00 ]
76f5a1a536SAleksa Sarai 	 *
77c90012acSAleksa Sarai 	 * And we verify that check_nonzero_user() acts identically to
78c90012acSAleksa Sarai 	 * memchr_inv().
79f5a1a536SAleksa Sarai 	 */
80f5a1a536SAleksa Sarai 
81f5a1a536SAleksa Sarai 	memset(kmem, 0x0, size);
82f5a1a536SAleksa Sarai 	for (i = 1; i < zero_start; i += 2)
83f5a1a536SAleksa Sarai 		kmem[i] = 0xff;
84f5a1a536SAleksa Sarai 	for (i = zero_end; i < size; i += 2)
85f5a1a536SAleksa Sarai 		kmem[i] = 0xff;
86f5a1a536SAleksa Sarai 
87f5a1a536SAleksa Sarai 	ret |= test(copy_to_user(umem, kmem, size),
88f5a1a536SAleksa Sarai 		    "legitimate copy_to_user failed");
89f5a1a536SAleksa Sarai 
90f5a1a536SAleksa Sarai 	for (start = 0; start <= size; start++) {
91f5a1a536SAleksa Sarai 		for (end = start; end <= size; end++) {
92f5a1a536SAleksa Sarai 			size_t len = end - start;
93f5a1a536SAleksa Sarai 			int retval = check_zeroed_user(umem + start, len);
94f5a1a536SAleksa Sarai 			int expected = is_zeroed(kmem + start, len);
95f5a1a536SAleksa Sarai 
96f5a1a536SAleksa Sarai 			ret |= test(retval != expected,
97f5a1a536SAleksa Sarai 				    "check_nonzero_user(=%d) != memchr_inv(=%d) mismatch (start=%zu, end=%zu)",
98f5a1a536SAleksa Sarai 				    retval, expected, start, end);
99f5a1a536SAleksa Sarai 		}
100f5a1a536SAleksa Sarai 	}
101f5a1a536SAleksa Sarai 
102f5a1a536SAleksa Sarai 	return ret;
103f5a1a536SAleksa Sarai }
104f5a1a536SAleksa Sarai 
test_copy_struct_from_user(char * kmem,char __user * umem,size_t size)105f5a1a536SAleksa Sarai static int test_copy_struct_from_user(char *kmem, char __user *umem,
106f5a1a536SAleksa Sarai 				      size_t size)
107f5a1a536SAleksa Sarai {
108f5a1a536SAleksa Sarai 	int ret = 0;
109f5a1a536SAleksa Sarai 	char *umem_src = NULL, *expected = NULL;
110f5a1a536SAleksa Sarai 	size_t ksize, usize;
111f5a1a536SAleksa Sarai 
112f5a1a536SAleksa Sarai 	umem_src = kmalloc(size, GFP_KERNEL);
113c90012acSAleksa Sarai 	ret = test(umem_src == NULL, "kmalloc failed");
114c90012acSAleksa Sarai 	if (ret)
115f5a1a536SAleksa Sarai 		goto out_free;
116f5a1a536SAleksa Sarai 
117f5a1a536SAleksa Sarai 	expected = kmalloc(size, GFP_KERNEL);
118c90012acSAleksa Sarai 	ret = test(expected == NULL, "kmalloc failed");
119c90012acSAleksa Sarai 	if (ret)
120f5a1a536SAleksa Sarai 		goto out_free;
121f5a1a536SAleksa Sarai 
122f5a1a536SAleksa Sarai 	/* Fill umem with a fixed byte pattern. */
123f5a1a536SAleksa Sarai 	memset(umem_src, 0x3e, size);
124f5a1a536SAleksa Sarai 	ret |= test(copy_to_user(umem, umem_src, size),
125f5a1a536SAleksa Sarai 		    "legitimate copy_to_user failed");
126f5a1a536SAleksa Sarai 
127f5a1a536SAleksa Sarai 	/* Check basic case -- (usize == ksize). */
128f5a1a536SAleksa Sarai 	ksize = size;
129f5a1a536SAleksa Sarai 	usize = size;
130f5a1a536SAleksa Sarai 
131f5a1a536SAleksa Sarai 	memcpy(expected, umem_src, ksize);
132f5a1a536SAleksa Sarai 
133f5a1a536SAleksa Sarai 	memset(kmem, 0x0, size);
134f5a1a536SAleksa Sarai 	ret |= test(copy_struct_from_user(kmem, ksize, umem, usize),
135f5a1a536SAleksa Sarai 		    "copy_struct_from_user(usize == ksize) failed");
136f5a1a536SAleksa Sarai 	ret |= test(memcmp(kmem, expected, ksize),
137f5a1a536SAleksa Sarai 		    "copy_struct_from_user(usize == ksize) gives unexpected copy");
138f5a1a536SAleksa Sarai 
139f5a1a536SAleksa Sarai 	/* Old userspace case -- (usize < ksize). */
140f5a1a536SAleksa Sarai 	ksize = size;
141f5a1a536SAleksa Sarai 	usize = size / 2;
142f5a1a536SAleksa Sarai 
143f5a1a536SAleksa Sarai 	memcpy(expected, umem_src, usize);
144f5a1a536SAleksa Sarai 	memset(expected + usize, 0x0, ksize - usize);
145f5a1a536SAleksa Sarai 
146f5a1a536SAleksa Sarai 	memset(kmem, 0x0, size);
147f5a1a536SAleksa Sarai 	ret |= test(copy_struct_from_user(kmem, ksize, umem, usize),
148f5a1a536SAleksa Sarai 		    "copy_struct_from_user(usize < ksize) failed");
149f5a1a536SAleksa Sarai 	ret |= test(memcmp(kmem, expected, ksize),
150f5a1a536SAleksa Sarai 		    "copy_struct_from_user(usize < ksize) gives unexpected copy");
151f5a1a536SAleksa Sarai 
152f5a1a536SAleksa Sarai 	/* New userspace (-E2BIG) case -- (usize > ksize). */
153f5a1a536SAleksa Sarai 	ksize = size / 2;
154f5a1a536SAleksa Sarai 	usize = size;
155f5a1a536SAleksa Sarai 
156f5a1a536SAleksa Sarai 	memset(kmem, 0x0, size);
157f5a1a536SAleksa Sarai 	ret |= test(copy_struct_from_user(kmem, ksize, umem, usize) != -E2BIG,
158f5a1a536SAleksa Sarai 		    "copy_struct_from_user(usize > ksize) didn't give E2BIG");
159f5a1a536SAleksa Sarai 
160f5a1a536SAleksa Sarai 	/* New userspace (success) case -- (usize > ksize). */
161f5a1a536SAleksa Sarai 	ksize = size / 2;
162f5a1a536SAleksa Sarai 	usize = size;
163f5a1a536SAleksa Sarai 
164f5a1a536SAleksa Sarai 	memcpy(expected, umem_src, ksize);
165f5a1a536SAleksa Sarai 	ret |= test(clear_user(umem + ksize, usize - ksize),
166f5a1a536SAleksa Sarai 		    "legitimate clear_user failed");
167f5a1a536SAleksa Sarai 
168f5a1a536SAleksa Sarai 	memset(kmem, 0x0, size);
169f5a1a536SAleksa Sarai 	ret |= test(copy_struct_from_user(kmem, ksize, umem, usize),
170f5a1a536SAleksa Sarai 		    "copy_struct_from_user(usize > ksize) failed");
171f5a1a536SAleksa Sarai 	ret |= test(memcmp(kmem, expected, ksize),
172f5a1a536SAleksa Sarai 		    "copy_struct_from_user(usize > ksize) gives unexpected copy");
173f5a1a536SAleksa Sarai 
174f5a1a536SAleksa Sarai out_free:
175f5a1a536SAleksa Sarai 	kfree(expected);
176f5a1a536SAleksa Sarai 	kfree(umem_src);
177f5a1a536SAleksa Sarai 	return ret;
178f5a1a536SAleksa Sarai }
179f5a1a536SAleksa Sarai 
test_user_copy_init(void)1803e2a4c18SKees Cook static int __init test_user_copy_init(void)
1813e2a4c18SKees Cook {
1823e2a4c18SKees Cook 	int ret = 0;
1833e2a4c18SKees Cook 	char *kmem;
1843e2a4c18SKees Cook 	char __user *usermem;
1853e2a4c18SKees Cook 	char *bad_usermem;
1863e2a4c18SKees Cook 	unsigned long user_addr;
1874c5d7bc6SKees Cook 	u8 val_u8;
1884c5d7bc6SKees Cook 	u16 val_u16;
1894c5d7bc6SKees Cook 	u32 val_u32;
1904c5d7bc6SKees Cook #ifdef TEST_U64
1914c5d7bc6SKees Cook 	u64 val_u64;
1924c5d7bc6SKees Cook #endif
1933e2a4c18SKees Cook 
1943e2a4c18SKees Cook 	kmem = kmalloc(PAGE_SIZE * 2, GFP_KERNEL);
1953e2a4c18SKees Cook 	if (!kmem)
1963e2a4c18SKees Cook 		return -ENOMEM;
1973e2a4c18SKees Cook 
1983e2a4c18SKees Cook 	user_addr = vm_mmap(NULL, 0, PAGE_SIZE * 2,
1993e2a4c18SKees Cook 			    PROT_READ | PROT_WRITE | PROT_EXEC,
2003e2a4c18SKees Cook 			    MAP_ANONYMOUS | MAP_PRIVATE, 0);
2013e2a4c18SKees Cook 	if (user_addr >= (unsigned long)(TASK_SIZE)) {
2023e2a4c18SKees Cook 		pr_warn("Failed to allocate user memory\n");
2033e2a4c18SKees Cook 		kfree(kmem);
2043e2a4c18SKees Cook 		return -ENOMEM;
2053e2a4c18SKees Cook 	}
2063e2a4c18SKees Cook 
2073e2a4c18SKees Cook 	usermem = (char __user *)user_addr;
2083e2a4c18SKees Cook 	bad_usermem = (char *)user_addr;
2093e2a4c18SKees Cook 
210f5f893c5SKees Cook 	/*
211f5f893c5SKees Cook 	 * Legitimate usage: none of these copies should fail.
212f5f893c5SKees Cook 	 */
2134c5d7bc6SKees Cook 	memset(kmem, 0x3a, PAGE_SIZE * 2);
2143e2a4c18SKees Cook 	ret |= test(copy_to_user(usermem, kmem, PAGE_SIZE),
2153e2a4c18SKees Cook 		    "legitimate copy_to_user failed");
2164c5d7bc6SKees Cook 	memset(kmem, 0x0, PAGE_SIZE);
2174c5d7bc6SKees Cook 	ret |= test(copy_from_user(kmem, usermem, PAGE_SIZE),
2184c5d7bc6SKees Cook 		    "legitimate copy_from_user failed");
2194c5d7bc6SKees Cook 	ret |= test(memcmp(kmem, kmem + PAGE_SIZE, PAGE_SIZE),
2204c5d7bc6SKees Cook 		    "legitimate usercopy failed to copy data");
2214c5d7bc6SKees Cook 
2224c5d7bc6SKees Cook #define test_legit(size, check)						  \
2234c5d7bc6SKees Cook 	do {								  \
2244c5d7bc6SKees Cook 		val_##size = check;					  \
2254c5d7bc6SKees Cook 		ret |= test(put_user(val_##size, (size __user *)usermem), \
2264c5d7bc6SKees Cook 		    "legitimate put_user (" #size ") failed");		  \
2274c5d7bc6SKees Cook 		val_##size = 0;						  \
2284c5d7bc6SKees Cook 		ret |= test(get_user(val_##size, (size __user *)usermem), \
2294c5d7bc6SKees Cook 		    "legitimate get_user (" #size ") failed");		  \
2304c5d7bc6SKees Cook 		ret |= test(val_##size != check,			  \
2314c5d7bc6SKees Cook 		    "legitimate get_user (" #size ") failed to do copy"); \
2324c5d7bc6SKees Cook 		if (val_##size != check) {				  \
2334c5d7bc6SKees Cook 			pr_info("0x%llx != 0x%llx\n",			  \
2344c5d7bc6SKees Cook 				(unsigned long long)val_##size,		  \
2354c5d7bc6SKees Cook 				(unsigned long long)check);		  \
2364c5d7bc6SKees Cook 		}							  \
2374c5d7bc6SKees Cook 	} while (0)
2384c5d7bc6SKees Cook 
2394c5d7bc6SKees Cook 	test_legit(u8,  0x5a);
2404c5d7bc6SKees Cook 	test_legit(u16, 0x5a5b);
2414c5d7bc6SKees Cook 	test_legit(u32, 0x5a5b5c5d);
2424c5d7bc6SKees Cook #ifdef TEST_U64
2434c5d7bc6SKees Cook 	test_legit(u64, 0x5a5b5c5d6a6b6c6d);
2444c5d7bc6SKees Cook #endif
2454c5d7bc6SKees Cook #undef test_legit
2463e2a4c18SKees Cook 
247f5a1a536SAleksa Sarai 	/* Test usage of check_nonzero_user(). */
248f5a1a536SAleksa Sarai 	ret |= test_check_nonzero_user(kmem, usermem, 2 * PAGE_SIZE);
249f5a1a536SAleksa Sarai 	/* Test usage of copy_struct_from_user(). */
250f5a1a536SAleksa Sarai 	ret |= test_copy_struct_from_user(kmem, usermem, 2 * PAGE_SIZE);
251f5a1a536SAleksa Sarai 
252f5f893c5SKees Cook 	/*
253f5f893c5SKees Cook 	 * Invalid usage: none of these copies should succeed.
254f5f893c5SKees Cook 	 */
255f5f893c5SKees Cook 
256f5f893c5SKees Cook 	/* Prepare kernel memory with check values. */
2574fbfeb8bSHoeun Ryu 	memset(kmem, 0x5a, PAGE_SIZE);
2584fbfeb8bSHoeun Ryu 	memset(kmem + PAGE_SIZE, 0, PAGE_SIZE);
259f5f893c5SKees Cook 
260f5f893c5SKees Cook 	/* Reject kernel-to-kernel copies through copy_from_user(). */
2613e2a4c18SKees Cook 	ret |= test(!copy_from_user(kmem, (char __user *)(kmem + PAGE_SIZE),
2623e2a4c18SKees Cook 				    PAGE_SIZE),
2633e2a4c18SKees Cook 		    "illegal all-kernel copy_from_user passed");
264f5f893c5SKees Cook 
265f5f893c5SKees Cook 	/* Destination half of buffer should have been zeroed. */
2664fbfeb8bSHoeun Ryu 	ret |= test(memcmp(kmem + PAGE_SIZE, kmem, PAGE_SIZE),
2674fbfeb8bSHoeun Ryu 		    "zeroing failure for illegal all-kernel copy_from_user");
268f5f893c5SKees Cook 
269f5f893c5SKees Cook #if 0
270f5f893c5SKees Cook 	/*
271f5f893c5SKees Cook 	 * When running with SMAP/PAN/etc, this will Oops the kernel
272f5f893c5SKees Cook 	 * due to the zeroing of userspace memory on failure. This needs
273f5f893c5SKees Cook 	 * to be tested in LKDTM instead, since this test module does not
274f5f893c5SKees Cook 	 * expect to explode.
275f5f893c5SKees Cook 	 */
2763e2a4c18SKees Cook 	ret |= test(!copy_from_user(bad_usermem, (char __user *)kmem,
2773e2a4c18SKees Cook 				    PAGE_SIZE),
2783e2a4c18SKees Cook 		    "illegal reversed copy_from_user passed");
279f5f893c5SKees Cook #endif
2803e2a4c18SKees Cook 	ret |= test(!copy_to_user((char __user *)kmem, kmem + PAGE_SIZE,
2813e2a4c18SKees Cook 				  PAGE_SIZE),
2823e2a4c18SKees Cook 		    "illegal all-kernel copy_to_user passed");
2833e2a4c18SKees Cook 	ret |= test(!copy_to_user((char __user *)kmem, bad_usermem,
2843e2a4c18SKees Cook 				  PAGE_SIZE),
2853e2a4c18SKees Cook 		    "illegal reversed copy_to_user passed");
286f5f893c5SKees Cook 
2874c5d7bc6SKees Cook #define test_illegal(size, check)					    \
2884c5d7bc6SKees Cook 	do {								    \
2894c5d7bc6SKees Cook 		val_##size = (check);					    \
2904c5d7bc6SKees Cook 		ret |= test(!get_user(val_##size, (size __user *)kmem),	    \
2914c5d7bc6SKees Cook 		    "illegal get_user (" #size ") passed");		    \
2924c5d7bc6SKees Cook 		ret |= test(val_##size != (size)0,			    \
2934c5d7bc6SKees Cook 		    "zeroing failure for illegal get_user (" #size ")");    \
2944c5d7bc6SKees Cook 		if (val_##size != (size)0) {				    \
2954c5d7bc6SKees Cook 			pr_info("0x%llx != 0\n",			    \
2964c5d7bc6SKees Cook 				(unsigned long long)val_##size);	    \
2974c5d7bc6SKees Cook 		}							    \
2984c5d7bc6SKees Cook 		ret |= test(!put_user(val_##size, (size __user *)kmem),	    \
2994c5d7bc6SKees Cook 		    "illegal put_user (" #size ") passed");		    \
3004c5d7bc6SKees Cook 	} while (0)
3014c5d7bc6SKees Cook 
3024c5d7bc6SKees Cook 	test_illegal(u8,  0x5a);
3034c5d7bc6SKees Cook 	test_illegal(u16, 0x5a5b);
3044c5d7bc6SKees Cook 	test_illegal(u32, 0x5a5b5c5d);
3054c5d7bc6SKees Cook #ifdef TEST_U64
3064c5d7bc6SKees Cook 	test_illegal(u64, 0x5a5b5c5d6a6b6c6d);
3074c5d7bc6SKees Cook #endif
3084c5d7bc6SKees Cook #undef test_illegal
3093e2a4c18SKees Cook 
3103e2a4c18SKees Cook 	vm_munmap(user_addr, PAGE_SIZE * 2);
3113e2a4c18SKees Cook 	kfree(kmem);
3123e2a4c18SKees Cook 
3133e2a4c18SKees Cook 	if (ret == 0) {
3143e2a4c18SKees Cook 		pr_info("tests passed.\n");
3153e2a4c18SKees Cook 		return 0;
3163e2a4c18SKees Cook 	}
3173e2a4c18SKees Cook 
3183e2a4c18SKees Cook 	return -EINVAL;
3193e2a4c18SKees Cook }
3203e2a4c18SKees Cook 
3213e2a4c18SKees Cook module_init(test_user_copy_init);
3223e2a4c18SKees Cook 
test_user_copy_exit(void)3233e2a4c18SKees Cook static void __exit test_user_copy_exit(void)
3243e2a4c18SKees Cook {
3253e2a4c18SKees Cook 	pr_info("unloaded.\n");
3263e2a4c18SKees Cook }
3273e2a4c18SKees Cook 
3283e2a4c18SKees Cook module_exit(test_user_copy_exit);
3293e2a4c18SKees Cook 
3303e2a4c18SKees Cook MODULE_AUTHOR("Kees Cook <keescook@chromium.org>");
3313e2a4c18SKees Cook MODULE_LICENSE("GPL");
332