xref: /freebsd/tests/sys/kern/kern_copyin.c (revision c7046f76c2c027b00c0e6ba57cfd28f1a78f5e23)
1 /*-
2  * Copyright (c) 2015, 2020 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
6  * under sponsorship from the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/exec.h>
35 #include <sys/sysctl.h>
36 #include <sys/user.h>
37 #include <sys/mman.h>
38 
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <limits.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45 #include <atf-c.h>
46 #include <vm/vm.h>
47 #include <vm/pmap.h>
48 #include <machine/vmparam.h>
49 
50 static int scratch_file;
51 
52 static int
53 copyin_checker(uintptr_t uaddr, size_t len)
54 {
55 	ssize_t ret;
56 
57 	ret = write(scratch_file, (const void *)uaddr, len);
58 	return (ret == -1 ? errno : 0);
59 }
60 
61 #if __SIZEOF_POINTER__ == 8
62 /*
63  * A slightly more direct path to calling copyin(), but without the ability
64  * to specify a length.
65  */
66 static int
67 copyin_checker2(uintptr_t uaddr)
68 {
69 	int ret;
70 
71 	ret = fcntl(scratch_file, F_GETLK, (const void *)uaddr);
72 	return (ret == -1 ? errno : 0);
73 }
74 #endif
75 
76 static int
77 get_vm_layout(struct kinfo_vm_layout *kvm)
78 {
79 	size_t len;
80 	int mib[4];
81 
82 	mib[0] = CTL_KERN;
83 	mib[1] = KERN_PROC;
84 	mib[2] = KERN_PROC_VM_LAYOUT;
85 	mib[3] = getpid();
86 	len = sizeof(*kvm);
87 
88 	return (sysctl(mib, nitems(mib), kvm, &len, NULL, 0));
89 }
90 
91 #define	FMAX	ULONG_MAX
92 #if __SIZEOF_POINTER__ == 8
93 /* PR 257193 */
94 #define	ADDR_SIGNED	0x800000c000000000
95 #endif
96 
97 ATF_TC_WITHOUT_HEAD(kern_copyin);
98 ATF_TC_BODY(kern_copyin, tc)
99 {
100 	char template[] = "copyin.XXXXXX";
101 	struct kinfo_vm_layout kvm;
102 	uintptr_t maxuser;
103 	long page_size;
104 	void *addr;
105 	int error;
106 
107 	addr = MAP_FAILED;
108 
109 	error = get_vm_layout(&kvm);
110 	ATF_REQUIRE(error == 0);
111 
112 	page_size = sysconf(_SC_PAGESIZE);
113 	ATF_REQUIRE(page_size != (long)-1);
114 
115 	maxuser = kvm.kvm_max_user_addr;
116 	scratch_file = mkstemp(template);
117 	ATF_REQUIRE(scratch_file != -1);
118 	unlink(template);
119 
120 	/*
121 	 * Since the shared page address can be randomized we need to make
122 	 * sure that something is mapped at the top of the user address space.
123 	 * Otherwise reading bytes from maxuser-X will fail rendering this test
124 	 * useless.
125 	 */
126 	if (kvm.kvm_shp_addr + kvm.kvm_shp_size < maxuser) {
127 		addr = mmap((void *)(maxuser - page_size), page_size, PROT_READ,
128 		    MAP_ANON | MAP_FIXED, -1, 0);
129 		ATF_REQUIRE(addr != MAP_FAILED);
130 	}
131 
132 	ATF_CHECK(copyin_checker(0, 0) == 0);
133 	ATF_CHECK(copyin_checker(maxuser - 10, 9) == 0);
134 	ATF_CHECK(copyin_checker(maxuser - 10, 10) == 0);
135 	ATF_CHECK(copyin_checker(maxuser - 10, 11) == EFAULT);
136 	ATF_CHECK(copyin_checker(maxuser - 1, 1) == 0);
137 	ATF_CHECK(copyin_checker(maxuser, 0) == 0);
138 	ATF_CHECK(copyin_checker(maxuser, 1) == EFAULT);
139 	ATF_CHECK(copyin_checker(maxuser, 2) == EFAULT);
140 	ATF_CHECK(copyin_checker(maxuser + 1, 0) == 0);
141 	ATF_CHECK(copyin_checker(maxuser + 1, 2) == EFAULT);
142 	ATF_CHECK(copyin_checker(FMAX - 10, 9) == EFAULT);
143 	ATF_CHECK(copyin_checker(FMAX - 10, 10) == EFAULT);
144 	ATF_CHECK(copyin_checker(FMAX - 10, 11) == EFAULT);
145 #if __SIZEOF_POINTER__ == 8
146 	ATF_CHECK(copyin_checker(ADDR_SIGNED, 1) == EFAULT);
147 	ATF_CHECK(copyin_checker2(ADDR_SIGNED) == EFAULT);
148 #endif
149 
150 	if (addr != MAP_FAILED)
151 		munmap(addr, PAGE_SIZE);
152 }
153 
154 ATF_TP_ADD_TCS(tp)
155 {
156 
157 	ATF_TP_ADD_TC(tp, kern_copyin);
158 	return (atf_no_error());
159 }
160