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