1 /*- 2 * Copyright (c) 2004 Tim J. Robbins 3 * Copyright (c) 2002 Doug Rabson 4 * Copyright (c) 2000 Marcel Moolenaar 5 * Copyright (c) 1994-1995 Søren Schmidt 6 * All rights reserved. 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 * in this position and unchanged. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 * 31 * $FreeBSD$ 32 */ 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 #include <sys/capsicum.h> 38 #include <sys/file.h> 39 #include <sys/imgact.h> 40 #include <sys/ktr.h> 41 #include <sys/mman.h> 42 #include <sys/proc.h> 43 #include <sys/resourcevar.h> 44 #include <sys/syscallsubr.h> 45 #include <sys/sysent.h> 46 #include <sys/sysproto.h> 47 48 #include <vm/pmap.h> 49 #include <vm/vm_extern.h> 50 #include <vm/vm_map.h> 51 52 #include <compat/linux/linux_emul.h> 53 #include <compat/linux/linux_mmap.h> 54 #include <compat/linux/linux_persona.h> 55 #include <compat/linux/linux_util.h> 56 57 58 #define STACK_SIZE (2 * 1024 * 1024) 59 #define GUARD_SIZE (4 * PAGE_SIZE) 60 61 #if defined(__amd64__) 62 static void linux_fixup_prot(struct thread *td, int *prot); 63 #endif 64 65 66 int 67 linux_mmap_common(struct thread *td, uintptr_t addr, size_t len, int prot, 68 int flags, int fd, off_t pos) 69 { 70 struct proc *p = td->td_proc; 71 struct vmspace *vms = td->td_proc->p_vmspace; 72 int bsd_flags, error; 73 struct file *fp; 74 75 cap_rights_t rights; 76 LINUX_CTR6(mmap2, "0x%lx, %ld, %ld, 0x%08lx, %ld, 0x%lx", 77 addr, len, prot, flags, fd, pos); 78 79 error = 0; 80 bsd_flags = 0; 81 fp = NULL; 82 83 /* 84 * Linux mmap(2): 85 * You must specify exactly one of MAP_SHARED and MAP_PRIVATE 86 */ 87 if (!((flags & LINUX_MAP_SHARED) ^ (flags & LINUX_MAP_PRIVATE))) 88 return (EINVAL); 89 90 if (flags & LINUX_MAP_SHARED) 91 bsd_flags |= MAP_SHARED; 92 if (flags & LINUX_MAP_PRIVATE) 93 bsd_flags |= MAP_PRIVATE; 94 if (flags & LINUX_MAP_FIXED) 95 bsd_flags |= MAP_FIXED; 96 if (flags & LINUX_MAP_ANON) { 97 /* Enforce pos to be on page boundary, then ignore. */ 98 if ((pos & PAGE_MASK) != 0) 99 return (EINVAL); 100 pos = 0; 101 bsd_flags |= MAP_ANON; 102 } else 103 bsd_flags |= MAP_NOSYNC; 104 if (flags & LINUX_MAP_GROWSDOWN) 105 bsd_flags |= MAP_STACK; 106 107 /* 108 * PROT_READ, PROT_WRITE, or PROT_EXEC implies PROT_READ and PROT_EXEC 109 * on Linux/i386 if the binary requires executable stack. 110 * We do this only for IA32 emulation as on native i386 this is does not 111 * make sense without PAE. 112 * 113 * XXX. Linux checks that the file system is not mounted with noexec. 114 */ 115 #if defined(__amd64__) 116 linux_fixup_prot(td, &prot); 117 #endif 118 119 /* Linux does not check file descriptor when MAP_ANONYMOUS is set. */ 120 fd = (bsd_flags & MAP_ANON) ? -1 : fd; 121 if (fd != -1) { 122 /* 123 * Linux follows Solaris mmap(2) description: 124 * The file descriptor fildes is opened with 125 * read permission, regardless of the 126 * protection options specified. 127 */ 128 129 error = fget(td, fd, cap_rights_init(&rights, CAP_MMAP), &fp); 130 if (error != 0) 131 return (error); 132 if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_DEV) { 133 fdrop(fp, td); 134 return (EINVAL); 135 } 136 137 /* Linux mmap() just fails for O_WRONLY files */ 138 if (!(fp->f_flag & FREAD)) { 139 fdrop(fp, td); 140 return (EACCES); 141 } 142 143 fdrop(fp, td); 144 } 145 146 if (flags & LINUX_MAP_GROWSDOWN) { 147 /* 148 * The Linux MAP_GROWSDOWN option does not limit auto 149 * growth of the region. Linux mmap with this option 150 * takes as addr the initial BOS, and as len, the initial 151 * region size. It can then grow down from addr without 152 * limit. However, Linux threads has an implicit internal 153 * limit to stack size of STACK_SIZE. Its just not 154 * enforced explicitly in Linux. But, here we impose 155 * a limit of (STACK_SIZE - GUARD_SIZE) on the stack 156 * region, since we can do this with our mmap. 157 * 158 * Our mmap with MAP_STACK takes addr as the maximum 159 * downsize limit on BOS, and as len the max size of 160 * the region. It then maps the top SGROWSIZ bytes, 161 * and auto grows the region down, up to the limit 162 * in addr. 163 * 164 * If we don't use the MAP_STACK option, the effect 165 * of this code is to allocate a stack region of a 166 * fixed size of (STACK_SIZE - GUARD_SIZE). 167 */ 168 169 if ((caddr_t)addr + len > vms->vm_maxsaddr) { 170 /* 171 * Some Linux apps will attempt to mmap 172 * thread stacks near the top of their 173 * address space. If their TOS is greater 174 * than vm_maxsaddr, vm_map_growstack() 175 * will confuse the thread stack with the 176 * process stack and deliver a SEGV if they 177 * attempt to grow the thread stack past their 178 * current stacksize rlimit. To avoid this, 179 * adjust vm_maxsaddr upwards to reflect 180 * the current stacksize rlimit rather 181 * than the maximum possible stacksize. 182 * It would be better to adjust the 183 * mmap'ed region, but some apps do not check 184 * mmap's return value. 185 */ 186 PROC_LOCK(p); 187 vms->vm_maxsaddr = (char *)p->p_sysent->sv_usrstack - 188 lim_cur_proc(p, RLIMIT_STACK); 189 PROC_UNLOCK(p); 190 } 191 192 /* 193 * This gives us our maximum stack size and a new BOS. 194 * If we're using VM_STACK, then mmap will just map 195 * the top SGROWSIZ bytes, and let the stack grow down 196 * to the limit at BOS. If we're not using VM_STACK 197 * we map the full stack, since we don't have a way 198 * to autogrow it. 199 */ 200 if (len <= STACK_SIZE - GUARD_SIZE) { 201 addr = addr - (STACK_SIZE - GUARD_SIZE - len); 202 len = STACK_SIZE - GUARD_SIZE; 203 } 204 } 205 206 /* 207 * FreeBSD is free to ignore the address hint if MAP_FIXED wasn't 208 * passed. However, some Linux applications, like the ART runtime, 209 * depend on the hint. If the MAP_FIXED wasn't passed, but the 210 * address is not zero, try with MAP_FIXED and MAP_EXCL first, 211 * and fall back to the normal behaviour if that fails. 212 */ 213 if (addr != 0 && (bsd_flags & MAP_FIXED) == 0 && 214 (bsd_flags & MAP_EXCL) == 0) { 215 error = kern_mmap(td, addr, len, prot, 216 bsd_flags | MAP_FIXED | MAP_EXCL, fd, pos); 217 if (error == 0) 218 goto out; 219 } 220 221 error = kern_mmap(td, addr, len, prot, bsd_flags, fd, pos); 222 out: 223 LINUX_CTR2(mmap2, "return: %d (%p)", error, td->td_retval[0]); 224 225 return (error); 226 } 227 228 int 229 linux_mprotect_common(struct thread *td, uintptr_t addr, size_t len, int prot) 230 { 231 232 #if defined(__amd64__) 233 linux_fixup_prot(td, &prot); 234 #endif 235 return (kern_mprotect(td, addr, len, prot)); 236 } 237 238 #if defined(__amd64__) 239 static void 240 linux_fixup_prot(struct thread *td, int *prot) 241 { 242 struct linux_pemuldata *pem; 243 244 if (SV_PROC_FLAG(td->td_proc, SV_ILP32) && *prot & PROT_READ) { 245 pem = pem_find(td->td_proc); 246 if (pem->persona & LINUX_READ_IMPLIES_EXEC) 247 *prot |= PROT_EXEC; 248 } 249 250 } 251 #endif 252