xref: /freebsd/tests/sys/kern/kern_copyin.c (revision 6eeccaa91b3884239efc0ad1431bb706c2326540)
13b624bd6SKonstantin Belousov /*-
2*6eeccaa9SKonstantin Belousov  * Copyright (c) 2015, 2020 The FreeBSD Foundation
33b624bd6SKonstantin Belousov  * All rights reserved.
43b624bd6SKonstantin Belousov  *
53b624bd6SKonstantin Belousov  * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
63b624bd6SKonstantin Belousov  * under sponsorship from the FreeBSD Foundation.
73b624bd6SKonstantin Belousov  *
83b624bd6SKonstantin Belousov  * Redistribution and use in source and binary forms, with or without
93b624bd6SKonstantin Belousov  * modification, are permitted provided that the following conditions
103b624bd6SKonstantin Belousov  * are met:
113b624bd6SKonstantin Belousov  * 1. Redistributions of source code must retain the above copyright
123b624bd6SKonstantin Belousov  *    notice, this list of conditions and the following disclaimer.
133b624bd6SKonstantin Belousov  * 2. Redistributions in binary form must reproduce the above copyright
143b624bd6SKonstantin Belousov  *    notice, this list of conditions and the following disclaimer in the
153b624bd6SKonstantin Belousov  *    documentation and/or other materials provided with the distribution.
163b624bd6SKonstantin Belousov  *
173b624bd6SKonstantin Belousov  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
183b624bd6SKonstantin Belousov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
193b624bd6SKonstantin Belousov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
203b624bd6SKonstantin Belousov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
213b624bd6SKonstantin Belousov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
223b624bd6SKonstantin Belousov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
233b624bd6SKonstantin Belousov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
243b624bd6SKonstantin Belousov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
253b624bd6SKonstantin Belousov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
263b624bd6SKonstantin Belousov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
273b624bd6SKonstantin Belousov  * SUCH DAMAGE.
283b624bd6SKonstantin Belousov  */
293b624bd6SKonstantin Belousov 
303b624bd6SKonstantin Belousov #include <sys/cdefs.h>
313b624bd6SKonstantin Belousov __FBSDID("$FreeBSD$");
323b624bd6SKonstantin Belousov 
333b624bd6SKonstantin Belousov #include <sys/param.h>
34*6eeccaa9SKonstantin Belousov #include <sys/exec.h>
35*6eeccaa9SKonstantin Belousov #include <sys/sysctl.h>
363b624bd6SKonstantin Belousov #include <errno.h>
373b624bd6SKonstantin Belousov #include <limits.h>
383b624bd6SKonstantin Belousov #include <stdio.h>
393b624bd6SKonstantin Belousov #include <stdlib.h>
403b624bd6SKonstantin Belousov #include <unistd.h>
413b624bd6SKonstantin Belousov #include <atf-c.h>
423b624bd6SKonstantin Belousov #include <vm/vm.h>
433b624bd6SKonstantin Belousov #include <vm/pmap.h>
443b624bd6SKonstantin Belousov #include <machine/vmparam.h>
453b624bd6SKonstantin Belousov 
463b624bd6SKonstantin Belousov static int scratch_file;
473b624bd6SKonstantin Belousov 
483b624bd6SKonstantin Belousov static int
493b624bd6SKonstantin Belousov copyin_checker(uintptr_t uaddr, size_t len)
503b624bd6SKonstantin Belousov {
513b624bd6SKonstantin Belousov 	ssize_t ret;
523b624bd6SKonstantin Belousov 
533b624bd6SKonstantin Belousov 	ret = write(scratch_file, (const void *)uaddr, len);
543b624bd6SKonstantin Belousov 	return (ret == -1 ? errno : 0);
553b624bd6SKonstantin Belousov }
563b624bd6SKonstantin Belousov 
57*6eeccaa9SKonstantin Belousov #ifdef __amd64__
58*6eeccaa9SKonstantin Belousov static uintptr_t
59*6eeccaa9SKonstantin Belousov get_maxuser_address(void)
60*6eeccaa9SKonstantin Belousov {
61*6eeccaa9SKonstantin Belousov 	size_t len;
62*6eeccaa9SKonstantin Belousov 	uintptr_t psstrings;
63*6eeccaa9SKonstantin Belousov 	int error, mib[4];
64*6eeccaa9SKonstantin Belousov 
65*6eeccaa9SKonstantin Belousov 	mib[0] = CTL_KERN;
66*6eeccaa9SKonstantin Belousov 	mib[1] = KERN_PROC;
67*6eeccaa9SKonstantin Belousov 	mib[2] = KERN_PROC_PS_STRINGS;
68*6eeccaa9SKonstantin Belousov 	mib[3] = getpid();
69*6eeccaa9SKonstantin Belousov 	error = sysctl(mib, nitems(mib), &psstrings, &len, NULL, 0);
70*6eeccaa9SKonstantin Belousov 	if (error != 0)
71*6eeccaa9SKonstantin Belousov 		return (0);
72*6eeccaa9SKonstantin Belousov 
73*6eeccaa9SKonstantin Belousov 	if (psstrings == PS_STRINGS_LA57)
74*6eeccaa9SKonstantin Belousov 		return (VM_MAXUSER_ADDRESS_LA57);
75*6eeccaa9SKonstantin Belousov 	if (psstrings == PS_STRINGS_LA48)
76*6eeccaa9SKonstantin Belousov 		return (VM_MAXUSER_ADDRESS_LA48);
77*6eeccaa9SKonstantin Belousov 	/* AMD LA48 with clipped UVA */
78*6eeccaa9SKonstantin Belousov 	if (psstrings == PS_STRINGS_LA48 - PAGE_SIZE)
79*6eeccaa9SKonstantin Belousov 		return (VM_MAXUSER_ADDRESS_LA48 - PAGE_SIZE);
80*6eeccaa9SKonstantin Belousov 	return (0);
81*6eeccaa9SKonstantin Belousov }
82*6eeccaa9SKonstantin Belousov #endif
83*6eeccaa9SKonstantin Belousov 
843b624bd6SKonstantin Belousov #define	FMAX	ULONG_MAX
853b624bd6SKonstantin Belousov 
863b624bd6SKonstantin Belousov ATF_TC_WITHOUT_HEAD(kern_copyin);
873b624bd6SKonstantin Belousov ATF_TC_BODY(kern_copyin, tc)
883b624bd6SKonstantin Belousov {
893b624bd6SKonstantin Belousov 	char template[] = "copyin.XXXXXX";
90*6eeccaa9SKonstantin Belousov 	uintptr_t maxuser;
913b624bd6SKonstantin Belousov 
92*6eeccaa9SKonstantin Belousov #if defined(__mips__)
9305533a6fSRuslan Bukin 	/*
9405533a6fSRuslan Bukin 	 * MIPS has different VM layout: the UVA map on mips ends the
9505533a6fSRuslan Bukin 	 * highest mapped entry at the VM_MAXUSER_ADDRESS - PAGE_SIZE,
9605533a6fSRuslan Bukin 	 * while all other arches map either stack or shared page up
9705533a6fSRuslan Bukin 	 * to the VM_MAXUSER_ADDRESS.
9805533a6fSRuslan Bukin 	 */
99*6eeccaa9SKonstantin Belousov 	maxuser = VM_MAXUSER_ADDRESS - PAGE_SIZE;
100*6eeccaa9SKonstantin Belousov #elif defined(__amd64__)
101*6eeccaa9SKonstantin Belousov 	maxuser = get_maxuser_address();
102*6eeccaa9SKonstantin Belousov 	ATF_REQUIRE(maxuser != 0);
103*6eeccaa9SKonstantin Belousov #else
104*6eeccaa9SKonstantin Belousov 	maxuser = VM_MAXUSER_ADDRESS;
105470d063aSRuslan Bukin #endif
106470d063aSRuslan Bukin 
1073b624bd6SKonstantin Belousov 	scratch_file = mkstemp(template);
1083b624bd6SKonstantin Belousov 	ATF_REQUIRE(scratch_file != -1);
1093b624bd6SKonstantin Belousov 	unlink(template);
1103b624bd6SKonstantin Belousov 
1113b624bd6SKonstantin Belousov 	ATF_CHECK(copyin_checker(0, 0) == 0);
112*6eeccaa9SKonstantin Belousov 	ATF_CHECK(copyin_checker(maxuser - 10, 9) == 0);
113*6eeccaa9SKonstantin Belousov 	ATF_CHECK(copyin_checker(maxuser - 10, 10) == 0);
114*6eeccaa9SKonstantin Belousov 	ATF_CHECK(copyin_checker(maxuser - 10, 11) == EFAULT);
115*6eeccaa9SKonstantin Belousov 	ATF_CHECK(copyin_checker(maxuser - 1, 1) == 0);
116*6eeccaa9SKonstantin Belousov 	ATF_CHECK(copyin_checker(maxuser, 0) == 0);
117*6eeccaa9SKonstantin Belousov 	ATF_CHECK(copyin_checker(maxuser, 1) == EFAULT);
118*6eeccaa9SKonstantin Belousov 	ATF_CHECK(copyin_checker(maxuser, 2) == EFAULT);
119*6eeccaa9SKonstantin Belousov 	ATF_CHECK(copyin_checker(maxuser + 1, 0) == 0);
120*6eeccaa9SKonstantin Belousov 	ATF_CHECK(copyin_checker(maxuser + 1, 2) == EFAULT);
1213b624bd6SKonstantin Belousov 	ATF_CHECK(copyin_checker(FMAX - 10, 9) == EFAULT);
1223b624bd6SKonstantin Belousov 	ATF_CHECK(copyin_checker(FMAX - 10, 10) == EFAULT);
1233b624bd6SKonstantin Belousov 	ATF_CHECK(copyin_checker(FMAX - 10, 11) == EFAULT);
1243b624bd6SKonstantin Belousov }
1253b624bd6SKonstantin Belousov 
1263b624bd6SKonstantin Belousov ATF_TP_ADD_TCS(tp)
1273b624bd6SKonstantin Belousov {
1283b624bd6SKonstantin Belousov 
1293b624bd6SKonstantin Belousov 	ATF_TP_ADD_TC(tp, kern_copyin);
1303b624bd6SKonstantin Belousov 	return (atf_no_error());
1313b624bd6SKonstantin Belousov }
132