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 #include <sys/param.h> 32 #include <sys/exec.h> 33 #include <sys/sysctl.h> 34 #include <sys/user.h> 35 #include <sys/mman.h> 36 37 #include <errno.h> 38 #include <fcntl.h> 39 #include <limits.h> 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <unistd.h> 43 #include <atf-c.h> 44 #include <vm/vm.h> 45 #include <vm/pmap.h> 46 #include <machine/vmparam.h> 47 48 static int scratch_file; 49 50 static int 51 copyin_checker(uintptr_t uaddr, size_t len) 52 { 53 ssize_t ret; 54 55 ret = write(scratch_file, (const void *)uaddr, len); 56 return (ret == -1 ? errno : 0); 57 } 58 59 #if __SIZEOF_POINTER__ == 8 60 /* 61 * A slightly more direct path to calling copyin(), but without the ability 62 * to specify a length. 63 */ 64 static int 65 copyin_checker2(uintptr_t uaddr) 66 { 67 int ret; 68 69 ret = fcntl(scratch_file, F_GETLK, (const void *)uaddr); 70 return (ret == -1 ? errno : 0); 71 } 72 #endif 73 74 static int 75 get_vm_layout(struct kinfo_vm_layout *kvm) 76 { 77 size_t len; 78 int mib[4]; 79 80 mib[0] = CTL_KERN; 81 mib[1] = KERN_PROC; 82 mib[2] = KERN_PROC_VM_LAYOUT; 83 mib[3] = getpid(); 84 len = sizeof(*kvm); 85 86 return (sysctl(mib, nitems(mib), kvm, &len, NULL, 0)); 87 } 88 89 #define FMAX ULONG_MAX 90 #if __SIZEOF_POINTER__ == 8 91 /* PR 257193 */ 92 #define ADDR_SIGNED 0x800000c000000000 93 #endif 94 95 ATF_TC_WITHOUT_HEAD(kern_copyin); 96 ATF_TC_BODY(kern_copyin, tc) 97 { 98 char template[] = "copyin.XXXXXX"; 99 struct kinfo_vm_layout kvm; 100 uintptr_t maxuser; 101 long page_size; 102 void *addr; 103 int error; 104 105 addr = MAP_FAILED; 106 107 error = get_vm_layout(&kvm); 108 ATF_REQUIRE(error == 0); 109 110 page_size = sysconf(_SC_PAGESIZE); 111 ATF_REQUIRE(page_size != (long)-1); 112 113 maxuser = kvm.kvm_max_user_addr; 114 scratch_file = mkstemp(template); 115 ATF_REQUIRE(scratch_file != -1); 116 unlink(template); 117 118 /* 119 * Since the shared page address can be randomized we need to make 120 * sure that something is mapped at the top of the user address space. 121 * Otherwise reading bytes from maxuser-X will fail rendering this test 122 * useless. 123 */ 124 if (kvm.kvm_shp_addr + kvm.kvm_shp_size < maxuser) { 125 addr = mmap((void *)(maxuser - page_size), page_size, PROT_READ, 126 MAP_ANON | MAP_FIXED, -1, 0); 127 ATF_REQUIRE(addr != MAP_FAILED); 128 } 129 130 ATF_CHECK(copyin_checker(0, 0) == 0); 131 ATF_CHECK(copyin_checker(maxuser - 10, 9) == 0); 132 ATF_CHECK(copyin_checker(maxuser - 10, 10) == 0); 133 ATF_CHECK(copyin_checker(maxuser - 10, 11) == EFAULT); 134 ATF_CHECK(copyin_checker(maxuser - 1, 1) == 0); 135 ATF_CHECK(copyin_checker(maxuser, 0) == 0); 136 ATF_CHECK(copyin_checker(maxuser, 1) == EFAULT); 137 ATF_CHECK(copyin_checker(maxuser, 2) == EFAULT); 138 ATF_CHECK(copyin_checker(maxuser + 1, 0) == 0); 139 ATF_CHECK(copyin_checker(maxuser + 1, 2) == EFAULT); 140 ATF_CHECK(copyin_checker(FMAX - 10, 9) == EFAULT); 141 ATF_CHECK(copyin_checker(FMAX - 10, 10) == EFAULT); 142 ATF_CHECK(copyin_checker(FMAX - 10, 11) == EFAULT); 143 #if __SIZEOF_POINTER__ == 8 144 ATF_CHECK(copyin_checker(ADDR_SIGNED, 1) == EFAULT); 145 ATF_CHECK(copyin_checker2(ADDR_SIGNED) == EFAULT); 146 #endif 147 148 if (addr != MAP_FAILED) 149 munmap(addr, PAGE_SIZE); 150 } 151 152 ATF_TP_ADD_TCS(tp) 153 { 154 155 ATF_TP_ADD_TC(tp, kern_copyin); 156 return (atf_no_error()); 157 } 158