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 <errno.h> 37 #include <limits.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <unistd.h> 41 #include <atf-c.h> 42 #include <vm/vm.h> 43 #include <vm/pmap.h> 44 #include <machine/vmparam.h> 45 46 static int scratch_file; 47 48 static int 49 copyin_checker(uintptr_t uaddr, size_t len) 50 { 51 ssize_t ret; 52 53 ret = write(scratch_file, (const void *)uaddr, len); 54 return (ret == -1 ? errno : 0); 55 } 56 57 #ifdef __amd64__ 58 static uintptr_t 59 get_maxuser_address(void) 60 { 61 size_t len; 62 uintptr_t psstrings; 63 int error, mib[4]; 64 65 mib[0] = CTL_KERN; 66 mib[1] = KERN_PROC; 67 mib[2] = KERN_PROC_PS_STRINGS; 68 mib[3] = getpid(); 69 len = sizeof(psstrings); 70 error = sysctl(mib, nitems(mib), &psstrings, &len, NULL, 0); 71 if (error != 0) 72 return (0); 73 74 if (psstrings == PS_STRINGS_LA57) 75 return (VM_MAXUSER_ADDRESS_LA57); 76 if (psstrings == PS_STRINGS_LA48) 77 return (VM_MAXUSER_ADDRESS_LA48); 78 /* AMD LA48 with clipped UVA */ 79 if (psstrings == PS_STRINGS_LA48 - PAGE_SIZE) 80 return (VM_MAXUSER_ADDRESS_LA48 - PAGE_SIZE); 81 return (0); 82 } 83 #endif 84 85 #define FMAX ULONG_MAX 86 87 ATF_TC_WITHOUT_HEAD(kern_copyin); 88 ATF_TC_BODY(kern_copyin, tc) 89 { 90 char template[] = "copyin.XXXXXX"; 91 uintptr_t maxuser; 92 93 #if defined(__mips__) 94 /* 95 * MIPS has different VM layout: the UVA map on mips ends the 96 * highest mapped entry at the VM_MAXUSER_ADDRESS - PAGE_SIZE, 97 * while all other arches map either stack or shared page up 98 * to the VM_MAXUSER_ADDRESS. 99 */ 100 maxuser = VM_MAXUSER_ADDRESS - PAGE_SIZE; 101 #elif defined(__amd64__) 102 maxuser = get_maxuser_address(); 103 ATF_REQUIRE(maxuser != 0); 104 #else 105 maxuser = VM_MAXUSER_ADDRESS; 106 #endif 107 108 scratch_file = mkstemp(template); 109 ATF_REQUIRE(scratch_file != -1); 110 unlink(template); 111 112 ATF_CHECK(copyin_checker(0, 0) == 0); 113 ATF_CHECK(copyin_checker(maxuser - 10, 9) == 0); 114 ATF_CHECK(copyin_checker(maxuser - 10, 10) == 0); 115 ATF_CHECK(copyin_checker(maxuser - 10, 11) == EFAULT); 116 ATF_CHECK(copyin_checker(maxuser - 1, 1) == 0); 117 ATF_CHECK(copyin_checker(maxuser, 0) == 0); 118 ATF_CHECK(copyin_checker(maxuser, 1) == EFAULT); 119 ATF_CHECK(copyin_checker(maxuser, 2) == EFAULT); 120 ATF_CHECK(copyin_checker(maxuser + 1, 0) == 0); 121 ATF_CHECK(copyin_checker(maxuser + 1, 2) == EFAULT); 122 ATF_CHECK(copyin_checker(FMAX - 10, 9) == EFAULT); 123 ATF_CHECK(copyin_checker(FMAX - 10, 10) == EFAULT); 124 ATF_CHECK(copyin_checker(FMAX - 10, 11) == EFAULT); 125 } 126 127 ATF_TP_ADD_TCS(tp) 128 { 129 130 ATF_TP_ADD_TC(tp, kern_copyin); 131 return (atf_no_error()); 132 } 133