xref: /freebsd/sys/compat/linux/linux_mmap.c (revision d6eb98610fa65663bf0df4574b7cb2c5c4ffda71)
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 	LINUX_CTR6(mmap2, "0x%lx, %ld, %ld, 0x%08lx, %ld, 0x%lx",
76 	    addr, len, prot, flags, fd, pos);
77 
78 	error = 0;
79 	bsd_flags = 0;
80 	fp = NULL;
81 
82 	/*
83 	 * Linux mmap(2):
84 	 * You must specify exactly one of MAP_SHARED and MAP_PRIVATE
85 	 */
86 	if (!((flags & LINUX_MAP_SHARED) ^ (flags & LINUX_MAP_PRIVATE)))
87 		return (EINVAL);
88 
89 	if (flags & LINUX_MAP_SHARED)
90 		bsd_flags |= MAP_SHARED;
91 	if (flags & LINUX_MAP_PRIVATE)
92 		bsd_flags |= MAP_PRIVATE;
93 	if (flags & LINUX_MAP_FIXED)
94 		bsd_flags |= MAP_FIXED;
95 	if (flags & LINUX_MAP_ANON) {
96 		/* Enforce pos to be on page boundary, then ignore. */
97 		if ((pos & PAGE_MASK) != 0)
98 			return (EINVAL);
99 		pos = 0;
100 		bsd_flags |= MAP_ANON;
101 	} else
102 		bsd_flags |= MAP_NOSYNC;
103 	if (flags & LINUX_MAP_GROWSDOWN)
104 		bsd_flags |= MAP_STACK;
105 
106 	/*
107 	 * PROT_READ, PROT_WRITE, or PROT_EXEC implies PROT_READ and PROT_EXEC
108 	 * on Linux/i386 if the binary requires executable stack.
109 	 * We do this only for IA32 emulation as on native i386 this is does not
110 	 * make sense without PAE.
111 	 *
112 	 * XXX. Linux checks that the file system is not mounted with noexec.
113 	 */
114 #if defined(__amd64__)
115 	linux_fixup_prot(td, &prot);
116 #endif
117 
118 	/* Linux does not check file descriptor when MAP_ANONYMOUS is set. */
119 	fd = (bsd_flags & MAP_ANON) ? -1 : fd;
120 	if (fd != -1) {
121 		/*
122 		 * Linux follows Solaris mmap(2) description:
123 		 * The file descriptor fildes is opened with
124 		 * read permission, regardless of the
125 		 * protection options specified.
126 		 */
127 
128 		error = fget(td, fd, &cap_mmap_rights, &fp);
129 		if (error != 0)
130 			return (error);
131 		if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_DEV) {
132 			fdrop(fp, td);
133 			return (EINVAL);
134 		}
135 
136 		/* Linux mmap() just fails for O_WRONLY files */
137 		if (!(fp->f_flag & FREAD)) {
138 			fdrop(fp, td);
139 			return (EACCES);
140 		}
141 
142 		fdrop(fp, td);
143 	}
144 
145 	if (flags & LINUX_MAP_GROWSDOWN) {
146 		/*
147 		 * The Linux MAP_GROWSDOWN option does not limit auto
148 		 * growth of the region.  Linux mmap with this option
149 		 * takes as addr the initial BOS, and as len, the initial
150 		 * region size.  It can then grow down from addr without
151 		 * limit.  However, Linux threads has an implicit internal
152 		 * limit to stack size of STACK_SIZE.  Its just not
153 		 * enforced explicitly in Linux.  But, here we impose
154 		 * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
155 		 * region, since we can do this with our mmap.
156 		 *
157 		 * Our mmap with MAP_STACK takes addr as the maximum
158 		 * downsize limit on BOS, and as len the max size of
159 		 * the region.  It then maps the top SGROWSIZ bytes,
160 		 * and auto grows the region down, up to the limit
161 		 * in addr.
162 		 *
163 		 * If we don't use the MAP_STACK option, the effect
164 		 * of this code is to allocate a stack region of a
165 		 * fixed size of (STACK_SIZE - GUARD_SIZE).
166 		 */
167 
168 		if ((caddr_t)addr + len > vms->vm_maxsaddr) {
169 			/*
170 			 * Some Linux apps will attempt to mmap
171 			 * thread stacks near the top of their
172 			 * address space.  If their TOS is greater
173 			 * than vm_maxsaddr, vm_map_growstack()
174 			 * will confuse the thread stack with the
175 			 * process stack and deliver a SEGV if they
176 			 * attempt to grow the thread stack past their
177 			 * current stacksize rlimit.  To avoid this,
178 			 * adjust vm_maxsaddr upwards to reflect
179 			 * the current stacksize rlimit rather
180 			 * than the maximum possible stacksize.
181 			 * It would be better to adjust the
182 			 * mmap'ed region, but some apps do not check
183 			 * mmap's return value.
184 			 */
185 			PROC_LOCK(p);
186 			vms->vm_maxsaddr = (char *)p->p_sysent->sv_usrstack -
187 			    lim_cur_proc(p, RLIMIT_STACK);
188 			PROC_UNLOCK(p);
189 		}
190 
191 		/*
192 		 * This gives us our maximum stack size and a new BOS.
193 		 * If we're using VM_STACK, then mmap will just map
194 		 * the top SGROWSIZ bytes, and let the stack grow down
195 		 * to the limit at BOS.  If we're not using VM_STACK
196 		 * we map the full stack, since we don't have a way
197 		 * to autogrow it.
198 		 */
199 		if (len <= STACK_SIZE - GUARD_SIZE) {
200 			addr = addr - (STACK_SIZE - GUARD_SIZE - len);
201 			len = STACK_SIZE - GUARD_SIZE;
202 		}
203 	}
204 
205 	/*
206 	 * FreeBSD is free to ignore the address hint if MAP_FIXED wasn't
207 	 * passed.  However, some Linux applications, like the ART runtime,
208 	 * depend on the hint.  If the MAP_FIXED wasn't passed, but the
209 	 * address is not zero, try with MAP_FIXED and MAP_EXCL first,
210 	 * and fall back to the normal behaviour if that fails.
211 	 */
212 	if (addr != 0 && (bsd_flags & MAP_FIXED) == 0 &&
213 	    (bsd_flags & MAP_EXCL) == 0) {
214 		error = kern_mmap(td, addr, len, prot,
215 		    bsd_flags | MAP_FIXED | MAP_EXCL, fd, pos);
216 		if (error == 0)
217 			goto out;
218 	}
219 
220 	error = kern_mmap(td, addr, len, prot, bsd_flags, fd, pos);
221 out:
222 	LINUX_CTR2(mmap2, "return: %d (%p)", error, td->td_retval[0]);
223 
224 	return (error);
225 }
226 
227 int
228 linux_mprotect_common(struct thread *td, uintptr_t addr, size_t len, int prot)
229 {
230 
231 	/* XXX Ignore PROT_GROWSDOWN and PROT_GROWSUP for now. */
232 	prot &= ~(LINUX_PROT_GROWSDOWN | LINUX_PROT_GROWSUP);
233 	if ((prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC)) != 0)
234 		return (EINVAL);
235 
236 #if defined(__amd64__)
237 	linux_fixup_prot(td, &prot);
238 #endif
239 	return (kern_mprotect(td, addr, len, prot));
240 }
241 
242 #if defined(__amd64__)
243 static void
244 linux_fixup_prot(struct thread *td, int *prot)
245 {
246 	struct linux_pemuldata *pem;
247 
248 	if (SV_PROC_FLAG(td->td_proc, SV_ILP32) && *prot & PROT_READ) {
249 		pem = pem_find(td->td_proc);
250 		if (pem->persona & LINUX_READ_IMPLIES_EXEC)
251 			*prot |= PROT_EXEC;
252 	}
253 
254 }
255 #endif
256