]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
2d21ac55 | 2 | * Copyright (c) 2007 Apple Inc. All Rights Reserved. |
0a7de745 | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
0a7de745 | 5 | * |
2d21ac55 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
0a7de745 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
2d21ac55 A |
18 | * The Original Code and all software distributed under the License are |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
0a7de745 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * Copyright (c) 1988 University of Utah. | |
30 | * Copyright (c) 1991, 1993 | |
31 | * The Regents of the University of California. All rights reserved. | |
32 | * | |
33 | * This code is derived from software contributed to Berkeley by | |
34 | * the Systems Programming Group of the University of Utah Computer | |
35 | * Science Department. | |
36 | * | |
37 | * Redistribution and use in source and binary forms, with or without | |
38 | * modification, are permitted provided that the following conditions | |
39 | * are met: | |
40 | * 1. Redistributions of source code must retain the above copyright | |
41 | * notice, this list of conditions and the following disclaimer. | |
42 | * 2. Redistributions in binary form must reproduce the above copyright | |
43 | * notice, this list of conditions and the following disclaimer in the | |
44 | * documentation and/or other materials provided with the distribution. | |
45 | * 3. All advertising materials mentioning features or use of this software | |
46 | * must display the following acknowledgement: | |
47 | * This product includes software developed by the University of | |
48 | * California, Berkeley and its contributors. | |
49 | * 4. Neither the name of the University nor the names of its contributors | |
50 | * may be used to endorse or promote products derived from this software | |
51 | * without specific prior written permission. | |
52 | * | |
53 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
54 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
55 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
56 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
57 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
58 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
59 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
60 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
61 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
62 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
63 | * SUCH DAMAGE. | |
64 | * | |
65 | * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$ | |
66 | * | |
67 | * @(#)vm_mmap.c 8.10 (Berkeley) 2/19/95 | |
68 | */ | |
2d21ac55 A |
69 | /* |
70 | * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce | |
71 | * support for mandatory and extensible security protections. This notice | |
72 | * is included in support of clause 2.2 (b) of the Apple Public License, | |
73 | * Version 2.0. | |
74 | */ | |
1c79356b A |
75 | |
76 | /* | |
77 | * Mapped file (mmap) interface to VM | |
78 | */ | |
79 | ||
80 | #include <sys/param.h> | |
81 | #include <sys/systm.h> | |
82 | #include <sys/filedesc.h> | |
91447636 A |
83 | #include <sys/proc_internal.h> |
84 | #include <sys/kauth.h> | |
1c79356b | 85 | #include <sys/resourcevar.h> |
91447636 | 86 | #include <sys/vnode_internal.h> |
1c79356b A |
87 | #include <sys/acct.h> |
88 | #include <sys/wait.h> | |
91447636 | 89 | #include <sys/file_internal.h> |
1c79356b A |
90 | #include <sys/vadvise.h> |
91 | #include <sys/trace.h> | |
92 | #include <sys/mman.h> | |
93 | #include <sys/conf.h> | |
94 | #include <sys/stat.h> | |
95 | #include <sys/ubc.h> | |
2d21ac55 | 96 | #include <sys/ubc_internal.h> |
91447636 | 97 | #include <sys/sysproto.h> |
1c79356b | 98 | |
2d21ac55 A |
99 | #include <sys/syscall.h> |
100 | #include <sys/kdebug.h> | |
fe8ab488 | 101 | #include <sys/bsdtask_info.h> |
2d21ac55 | 102 | |
b0d623f7 | 103 | #include <security/audit/audit.h> |
e5568f75 A |
104 | #include <bsm/audit_kevents.h> |
105 | ||
1c79356b | 106 | #include <mach/mach_types.h> |
91447636 A |
107 | #include <mach/mach_traps.h> |
108 | #include <mach/vm_sync.h> | |
109 | #include <mach/vm_behavior.h> | |
110 | #include <mach/vm_inherit.h> | |
111 | #include <mach/vm_statistics.h> | |
112 | #include <mach/mach_vm.h> | |
113 | #include <mach/vm_map.h> | |
114 | #include <mach/host_priv.h> | |
3e170ce0 | 115 | #include <mach/sdt.h> |
1c79356b | 116 | |
316670eb A |
117 | #include <machine/machine_routines.h> |
118 | ||
1c79356b | 119 | #include <kern/cpu_number.h> |
91447636 | 120 | #include <kern/host.h> |
316670eb | 121 | #include <kern/task.h> |
fe8ab488 A |
122 | #include <kern/page_decrypt.h> |
123 | ||
124 | #include <IOKit/IOReturn.h> | |
1c79356b A |
125 | |
126 | #include <vm/vm_map.h> | |
127 | #include <vm/vm_kern.h> | |
128 | #include <vm/vm_pager.h> | |
b0d623f7 | 129 | #include <vm/vm_protos.h> |
2d21ac55 | 130 | |
5ba3f43e A |
131 | #if CONFIG_MACF |
132 | #include <security/mac_framework.h> | |
133 | #endif | |
134 | ||
2d21ac55 A |
135 | /* |
136 | * XXX Internally, we use VM_PROT_* somewhat interchangeably, but the correct | |
137 | * XXX usage is PROT_* from an interface perspective. Thus the values of | |
138 | * XXX VM_PROT_* and PROT_* need to correspond. | |
139 | */ | |
1c79356b | 140 | int |
2d21ac55 | 141 | mmap(proc_t p, struct mmap_args *uap, user_addr_t *retval) |
1c79356b A |
142 | { |
143 | /* | |
144 | * Map in special device (must be SHARED) or file | |
145 | */ | |
91447636 | 146 | struct fileproc *fp; |
0a7de745 A |
147 | struct vnode *vp; |
148 | int flags; | |
149 | int prot; | |
150 | int err = 0; | |
151 | vm_map_t user_map; | |
152 | kern_return_t result; | |
153 | vm_map_offset_t user_addr; | |
154 | vm_map_size_t user_size; | |
155 | vm_object_offset_t pageoff; | |
156 | vm_object_offset_t file_pos; | |
157 | int alloc_flags = 0; | |
158 | vm_tag_t tag = VM_KERN_MEMORY_NONE; | |
159 | vm_map_kernel_flags_t vmk_flags = VM_MAP_KERNEL_FLAGS_NONE; | |
160 | boolean_t docow; | |
161 | vm_prot_t maxprot; | |
162 | void *handle; | |
163 | memory_object_t pager = MEMORY_OBJECT_NULL; | |
164 | memory_object_control_t control; | |
165 | int mapanon = 0; | |
166 | int fpref = 0; | |
167 | int error = 0; | |
91447636 | 168 | int fd = uap->fd; |
6d2010ae | 169 | int num_retries = 0; |
1c79356b | 170 | |
3e170ce0 A |
171 | /* |
172 | * Note that for UNIX03 conformance, there is additional parameter checking for | |
0a7de745 | 173 | * mmap() system call in libsyscall prior to entering the kernel. The sanity |
3e170ce0 A |
174 | * checks and argument validation done in this function are not the only places |
175 | * one can get returned errnos. | |
176 | */ | |
177 | ||
39236c6e | 178 | user_map = current_map(); |
316670eb A |
179 | user_addr = (vm_map_offset_t)uap->addr; |
180 | user_size = (vm_map_size_t) uap->len; | |
91447636 A |
181 | |
182 | AUDIT_ARG(addr, user_addr); | |
183 | AUDIT_ARG(len, user_size); | |
e5568f75 A |
184 | AUDIT_ARG(fd, uap->fd); |
185 | ||
1c79356b | 186 | prot = (uap->prot & VM_PROT_ALL); |
2d21ac55 A |
187 | #if 3777787 |
188 | /* | |
189 | * Since the hardware currently does not support writing without | |
190 | * read-before-write, or execution-without-read, if the request is | |
191 | * for write or execute access, we must imply read access as well; | |
192 | * otherwise programs expecting this to work will fail to operate. | |
193 | */ | |
0a7de745 | 194 | if (prot & (VM_PROT_EXECUTE | VM_PROT_WRITE)) { |
2d21ac55 | 195 | prot |= VM_PROT_READ; |
0a7de745 A |
196 | } |
197 | #endif /* radar 3777787 */ | |
2d21ac55 | 198 | |
1c79356b | 199 | flags = uap->flags; |
91447636 | 200 | vp = NULLVP; |
1c79356b A |
201 | |
202 | /* | |
203 | * The vm code does not have prototypes & compiler doesn't do the' | |
0a7de745 | 204 | * the right thing when you cast 64bit value and pass it in function |
1c79356b A |
205 | * call. So here it is. |
206 | */ | |
207 | file_pos = (vm_object_offset_t)uap->pos; | |
208 | ||
209 | ||
210 | /* make sure mapping fits into numeric range etc */ | |
0a7de745 A |
211 | if (file_pos + user_size > (vm_object_offset_t)-PAGE_SIZE_64) { |
212 | return EINVAL; | |
213 | } | |
1c79356b A |
214 | |
215 | /* | |
216 | * Align the file position to a page boundary, | |
217 | * and save its page offset component. | |
218 | */ | |
39236c6e | 219 | pageoff = (file_pos & vm_map_page_mask(user_map)); |
1c79356b A |
220 | file_pos -= (vm_object_offset_t)pageoff; |
221 | ||
222 | ||
223 | /* Adjust size for rounding (on both ends). */ | |
0a7de745 A |
224 | user_size += pageoff; /* low end... */ |
225 | user_size = vm_map_round_page(user_size, | |
226 | vm_map_page_mask(user_map)); /* hi end */ | |
1c79356b | 227 | |
3e170ce0 A |
228 | if (flags & MAP_JIT) { |
229 | if ((flags & MAP_FIXED) || | |
230 | (flags & MAP_SHARED) || | |
231 | !(flags & MAP_ANON) || | |
39037602 A |
232 | (flags & MAP_RESILIENT_CODESIGN) || |
233 | (flags & MAP_RESILIENT_MEDIA)) { | |
3e170ce0 A |
234 | return EINVAL; |
235 | } | |
236 | } | |
237 | ||
238 | if ((flags & MAP_RESILIENT_CODESIGN) || | |
239 | (flags & MAP_RESILIENT_MEDIA)) { | |
39037602 A |
240 | if ((flags & MAP_ANON) || |
241 | (flags & MAP_JIT)) { | |
3e170ce0 A |
242 | return EINVAL; |
243 | } | |
244 | if (prot & (VM_PROT_WRITE | VM_PROT_EXECUTE)) { | |
245 | return EPERM; | |
246 | } | |
6d2010ae | 247 | } |
3e170ce0 | 248 | |
1c79356b A |
249 | /* |
250 | * Check for illegal addresses. Watch out for address wrap... Note | |
251 | * that VM_*_ADDRESS are not constants due to casts (argh). | |
252 | */ | |
253 | if (flags & MAP_FIXED) { | |
254 | /* | |
255 | * The specified address must have the same remainder | |
256 | * as the file offset taken modulo PAGE_SIZE, so it | |
257 | * should be aligned after adjustment by pageoff. | |
258 | */ | |
259 | user_addr -= pageoff; | |
0a7de745 A |
260 | if (user_addr & vm_map_page_mask(user_map)) { |
261 | return EINVAL; | |
262 | } | |
1c79356b A |
263 | } |
264 | #ifdef notyet | |
265 | /* DO not have apis to get this info, need to wait till then*/ | |
266 | /* | |
267 | * XXX for non-fixed mappings where no hint is provided or | |
268 | * the hint would fall in the potential heap space, | |
269 | * place it after the end of the largest possible heap. | |
270 | * | |
271 | * There should really be a pmap call to determine a reasonable | |
272 | * location. | |
273 | */ | |
39236c6e | 274 | else if (addr < vm_map_round_page(p->p_vmspace->vm_daddr + MAXDSIZ, |
0a7de745 | 275 | vm_map_page_mask(user_map))) { |
39236c6e | 276 | addr = vm_map_round_page(p->p_vmspace->vm_daddr + MAXDSIZ, |
0a7de745 A |
277 | vm_map_page_mask(user_map)); |
278 | } | |
1c79356b A |
279 | |
280 | #endif | |
281 | ||
2d21ac55 | 282 | alloc_flags = 0; |
1c79356b A |
283 | |
284 | if (flags & MAP_ANON) { | |
6d2010ae A |
285 | maxprot = VM_PROT_ALL; |
286 | #if CONFIG_MACF | |
287 | /* | |
288 | * Entitlement check. | |
6d2010ae | 289 | */ |
316670eb | 290 | error = mac_proc_check_map_anon(p, user_addr, user_size, prot, flags, &maxprot); |
6d2010ae A |
291 | if (error) { |
292 | return EINVAL; | |
0a7de745 | 293 | } |
6d2010ae A |
294 | #endif /* MAC */ |
295 | ||
1c79356b | 296 | /* |
2d21ac55 | 297 | * Mapping blank space is trivial. Use positive fds as the alias |
0a7de745 | 298 | * value for memory tracking. |
1c79356b | 299 | */ |
2d21ac55 A |
300 | if (fd != -1) { |
301 | /* | |
302 | * Use "fd" to pass (some) Mach VM allocation flags, | |
303 | * (see the VM_FLAGS_* definitions). | |
304 | */ | |
5ba3f43e | 305 | alloc_flags = fd & (VM_FLAGS_ALIAS_MASK | |
0a7de745 A |
306 | VM_FLAGS_SUPERPAGE_MASK | |
307 | VM_FLAGS_PURGABLE | | |
308 | VM_FLAGS_4GB_CHUNK); | |
2d21ac55 A |
309 | if (alloc_flags != fd) { |
310 | /* reject if there are any extra flags */ | |
311 | return EINVAL; | |
312 | } | |
5ba3f43e A |
313 | VM_GET_FLAGS_ALIAS(alloc_flags, tag); |
314 | alloc_flags &= ~VM_FLAGS_ALIAS_MASK; | |
2d21ac55 | 315 | } |
0a7de745 | 316 | |
1c79356b | 317 | handle = NULL; |
1c79356b A |
318 | file_pos = 0; |
319 | mapanon = 1; | |
320 | } else { | |
91447636 | 321 | struct vnode_attr va; |
2d21ac55 A |
322 | vfs_context_t ctx = vfs_context_current(); |
323 | ||
0a7de745 | 324 | if (flags & MAP_JIT) { |
316670eb | 325 | return EINVAL; |
0a7de745 | 326 | } |
316670eb | 327 | |
1c79356b A |
328 | /* |
329 | * Mapping file, get fp for validation. Obtain vnode and make | |
330 | * sure it is of appropriate type. | |
331 | */ | |
91447636 | 332 | err = fp_lookup(p, fd, &fp, 0); |
0a7de745 A |
333 | if (err) { |
334 | return err; | |
335 | } | |
91447636 | 336 | fpref = 1; |
39236c6e A |
337 | switch (FILEGLOB_DTYPE(fp->f_fglob)) { |
338 | case DTYPE_PSXSHM: | |
91447636 A |
339 | uap->addr = (user_addr_t)user_addr; |
340 | uap->len = (user_size_t)user_size; | |
1c79356b A |
341 | uap->prot = prot; |
342 | uap->flags = flags; | |
343 | uap->pos = file_pos; | |
91447636 A |
344 | error = pshm_mmap(p, uap, retval, fp, (off_t)pageoff); |
345 | goto bad; | |
39236c6e A |
346 | case DTYPE_VNODE: |
347 | break; | |
348 | default: | |
91447636 A |
349 | error = EINVAL; |
350 | goto bad; | |
351 | } | |
352 | vp = (struct vnode *)fp->f_fglob->fg_data; | |
353 | error = vnode_getwithref(vp); | |
0a7de745 | 354 | if (error != 0) { |
91447636 | 355 | goto bad; |
0a7de745 | 356 | } |
91447636 A |
357 | |
358 | if (vp->v_type != VREG && vp->v_type != VCHR) { | |
359 | (void)vnode_put(vp); | |
360 | error = EINVAL; | |
361 | goto bad; | |
362 | } | |
e5568f75 A |
363 | |
364 | AUDIT_ARG(vnpath, vp, ARG_VNODE1); | |
0a7de745 | 365 | |
2d21ac55 A |
366 | /* |
367 | * POSIX: mmap needs to update access time for mapped files | |
91447636 | 368 | */ |
2d21ac55 A |
369 | if ((vnode_vfsvisflags(vp) & MNT_NOATIME) == 0) { |
370 | VATTR_INIT(&va); | |
371 | nanotime(&va.va_access_time); | |
372 | VATTR_SET_ACTIVE(&va, va_access_time); | |
373 | vnode_setattr(vp, &va, ctx); | |
374 | } | |
b0d623f7 | 375 | |
1c79356b A |
376 | /* |
377 | * XXX hack to handle use of /dev/zero to map anon memory (ala | |
378 | * SunOS). | |
379 | */ | |
380 | if (vp->v_type == VCHR || vp->v_type == VSTR) { | |
91447636 A |
381 | (void)vnode_put(vp); |
382 | error = ENODEV; | |
383 | goto bad; | |
1c79356b A |
384 | } else { |
385 | /* | |
386 | * Ensure that file and memory protections are | |
387 | * compatible. Note that we only worry about | |
388 | * writability if mapping is shared; in this case, | |
389 | * current and max prot are dictated by the open file. | |
390 | * XXX use the vnode instead? Problem is: what | |
391 | * credentials do we use for determination? What if | |
392 | * proc does a setuid? | |
393 | */ | |
0a7de745 A |
394 | maxprot = VM_PROT_EXECUTE; /* ??? */ |
395 | if (fp->f_fglob->fg_flag & FREAD) { | |
1c79356b | 396 | maxprot |= VM_PROT_READ; |
0a7de745 | 397 | } else if (prot & PROT_READ) { |
91447636 A |
398 | (void)vnode_put(vp); |
399 | error = EACCES; | |
400 | goto bad; | |
401 | } | |
1c79356b A |
402 | /* |
403 | * If we are sharing potential changes (either via | |
404 | * MAP_SHARED or via the implicit sharing of character | |
405 | * device mappings), and we are trying to get write | |
406 | * permission although we opened it without asking | |
0a7de745 | 407 | * for it, bail out. |
1c79356b A |
408 | */ |
409 | ||
410 | if ((flags & MAP_SHARED) != 0) { | |
b0d623f7 A |
411 | if ((fp->f_fglob->fg_flag & FWRITE) != 0 && |
412 | /* | |
0a7de745 | 413 | * Do not allow writable mappings of |
b0d623f7 A |
414 | * swap files (see vm_swapfile_pager.c). |
415 | */ | |
416 | !vnode_isswap(vp)) { | |
0a7de745 A |
417 | /* |
418 | * check for write access | |
419 | * | |
420 | * Note that we already made this check when granting FWRITE | |
421 | * against the file, so it seems redundant here. | |
422 | */ | |
423 | error = vnode_authorize(vp, NULL, KAUTH_VNODE_CHECKIMMUTABLE, ctx); | |
424 | ||
425 | /* if not granted for any reason, but we wanted it, bad */ | |
426 | if ((prot & PROT_WRITE) && (error != 0)) { | |
427 | vnode_put(vp); | |
428 | goto bad; | |
429 | } | |
430 | ||
431 | /* if writable, remember */ | |
432 | if (error == 0) { | |
433 | maxprot |= VM_PROT_WRITE; | |
434 | } | |
91447636 A |
435 | } else if ((prot & PROT_WRITE) != 0) { |
436 | (void)vnode_put(vp); | |
437 | error = EACCES; | |
438 | goto bad; | |
439 | } | |
0a7de745 | 440 | } else { |
1c79356b | 441 | maxprot |= VM_PROT_WRITE; |
0a7de745 | 442 | } |
1c79356b A |
443 | |
444 | handle = (void *)vp; | |
2d21ac55 A |
445 | #if CONFIG_MACF |
446 | error = mac_file_check_mmap(vfs_context_ucred(ctx), | |
3e170ce0 | 447 | fp->f_fglob, prot, flags, file_pos, &maxprot); |
2d21ac55 A |
448 | if (error) { |
449 | (void)vnode_put(vp); | |
450 | goto bad; | |
451 | } | |
452 | #endif /* MAC */ | |
1c79356b A |
453 | } |
454 | } | |
455 | ||
0a7de745 A |
456 | if (user_size == 0) { |
457 | if (!mapanon) { | |
91447636 | 458 | (void)vnode_put(vp); |
0a7de745 | 459 | } |
91447636 A |
460 | error = 0; |
461 | goto bad; | |
462 | } | |
1c79356b A |
463 | |
464 | /* | |
465 | * We bend a little - round the start and end addresses | |
466 | * to the nearest page boundary. | |
467 | */ | |
39236c6e | 468 | user_size = vm_map_round_page(user_size, |
0a7de745 | 469 | vm_map_page_mask(user_map)); |
1c79356b | 470 | |
39236c6e | 471 | if (file_pos & vm_map_page_mask(user_map)) { |
0a7de745 | 472 | if (!mapanon) { |
91447636 | 473 | (void)vnode_put(vp); |
0a7de745 | 474 | } |
91447636 A |
475 | error = EINVAL; |
476 | goto bad; | |
477 | } | |
1c79356b | 478 | |
1c79356b | 479 | if ((flags & MAP_FIXED) == 0) { |
2d21ac55 | 480 | alloc_flags |= VM_FLAGS_ANYWHERE; |
39236c6e | 481 | user_addr = vm_map_round_page(user_addr, |
0a7de745 | 482 | vm_map_page_mask(user_map)); |
1c79356b | 483 | } else { |
39236c6e | 484 | if (user_addr != vm_map_trunc_page(user_addr, |
0a7de745 A |
485 | vm_map_page_mask(user_map))) { |
486 | if (!mapanon) { | |
487 | (void)vnode_put(vp); | |
488 | } | |
91447636 A |
489 | error = EINVAL; |
490 | goto bad; | |
491 | } | |
492 | /* | |
493 | * mmap(MAP_FIXED) will replace any existing mappings in the | |
494 | * specified range, if the new mapping is successful. | |
495 | * If we just deallocate the specified address range here, | |
496 | * another thread might jump in and allocate memory in that | |
497 | * range before we get a chance to establish the new mapping, | |
498 | * and we won't have a chance to restore the old mappings. | |
499 | * So we use VM_FLAGS_OVERWRITE to let Mach VM know that it | |
500 | * has to deallocate the existing mappings and establish the | |
501 | * new ones atomically. | |
502 | */ | |
2d21ac55 | 503 | alloc_flags |= VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE; |
1c79356b A |
504 | } |
505 | ||
0a7de745 | 506 | if (flags & MAP_NOCACHE) { |
2d21ac55 | 507 | alloc_flags |= VM_FLAGS_NO_CACHE; |
0a7de745 | 508 | } |
1c79356b | 509 | |
3e170ce0 | 510 | if (flags & MAP_JIT) { |
5ba3f43e | 511 | vmk_flags.vmkf_map_jit = TRUE; |
6d2010ae | 512 | } |
3e170ce0 A |
513 | |
514 | if (flags & MAP_RESILIENT_CODESIGN) { | |
515 | alloc_flags |= VM_FLAGS_RESILIENT_CODESIGN; | |
516 | } | |
517 | ||
1c79356b A |
518 | /* |
519 | * Lookup/allocate object. | |
520 | */ | |
1c79356b | 521 | if (handle == NULL) { |
b0d623f7 | 522 | control = NULL; |
1c79356b A |
523 | #ifdef notyet |
524 | /* Hmm .. */ | |
525 | #if defined(VM_PROT_READ_IS_EXEC) | |
0a7de745 | 526 | if (prot & VM_PROT_READ) { |
1c79356b | 527 | prot |= VM_PROT_EXECUTE; |
0a7de745 A |
528 | } |
529 | if (maxprot & VM_PROT_READ) { | |
1c79356b | 530 | maxprot |= VM_PROT_EXECUTE; |
0a7de745 | 531 | } |
1c79356b A |
532 | #endif |
533 | #endif | |
2d21ac55 A |
534 | |
535 | #if 3777787 | |
0a7de745 | 536 | if (prot & (VM_PROT_EXECUTE | VM_PROT_WRITE)) { |
2d21ac55 | 537 | prot |= VM_PROT_READ; |
0a7de745 A |
538 | } |
539 | if (maxprot & (VM_PROT_EXECUTE | VM_PROT_WRITE)) { | |
2d21ac55 | 540 | maxprot |= VM_PROT_READ; |
0a7de745 A |
541 | } |
542 | #endif /* radar 3777787 */ | |
6d2010ae | 543 | map_anon_retry: |
2d21ac55 | 544 | result = vm_map_enter_mem_object(user_map, |
0a7de745 A |
545 | &user_addr, user_size, |
546 | 0, alloc_flags, vmk_flags, | |
547 | tag, | |
548 | IPC_PORT_NULL, 0, FALSE, | |
549 | prot, maxprot, | |
550 | (flags & MAP_SHARED) ? | |
551 | VM_INHERIT_SHARE : | |
552 | VM_INHERIT_DEFAULT); | |
6d2010ae A |
553 | |
554 | /* If a non-binding address was specified for this anonymous | |
555 | * mapping, retry the mapping with a zero base | |
556 | * in the event the mapping operation failed due to | |
557 | * lack of space between the address and the map's maximum. | |
558 | */ | |
559 | if ((result == KERN_NO_SPACE) && ((flags & MAP_FIXED) == 0) && user_addr && (num_retries++ == 0)) { | |
39236c6e | 560 | user_addr = vm_map_page_size(user_map); |
6d2010ae A |
561 | goto map_anon_retry; |
562 | } | |
1c79356b | 563 | } else { |
b0d623f7 A |
564 | if (vnode_isswap(vp)) { |
565 | /* | |
566 | * Map swap files with a special pager | |
567 | * that returns obfuscated contents. | |
568 | */ | |
569 | control = NULL; | |
570 | pager = swapfile_pager_setup(vp); | |
571 | if (pager != MEMORY_OBJECT_NULL) { | |
572 | control = swapfile_pager_control(pager); | |
573 | } | |
574 | } else { | |
575 | control = ubc_getobject(vp, UBC_FLAGS_NONE); | |
576 | } | |
0a7de745 | 577 | |
b0d623f7 | 578 | if (control == NULL) { |
91447636 A |
579 | (void)vnode_put(vp); |
580 | error = ENOMEM; | |
581 | goto bad; | |
582 | } | |
1c79356b A |
583 | |
584 | /* | |
585 | * Set credentials: | |
586 | * FIXME: if we're writing the file we need a way to | |
587 | * ensure that someone doesn't replace our R/W creds | |
0a7de745 | 588 | * with ones that only work for read. |
1c79356b A |
589 | */ |
590 | ||
13fec989 | 591 | ubc_setthreadcred(vp, p, current_thread()); |
1c79356b | 592 | docow = FALSE; |
0a7de745 | 593 | if ((flags & (MAP_ANON | MAP_SHARED)) == 0) { |
1c79356b A |
594 | docow = TRUE; |
595 | } | |
596 | ||
597 | #ifdef notyet | |
598 | /* Hmm .. */ | |
599 | #if defined(VM_PROT_READ_IS_EXEC) | |
0a7de745 | 600 | if (prot & VM_PROT_READ) { |
1c79356b | 601 | prot |= VM_PROT_EXECUTE; |
0a7de745 A |
602 | } |
603 | if (maxprot & VM_PROT_READ) { | |
1c79356b | 604 | maxprot |= VM_PROT_EXECUTE; |
0a7de745 | 605 | } |
1c79356b A |
606 | #endif |
607 | #endif /* notyet */ | |
608 | ||
2d21ac55 | 609 | #if 3777787 |
0a7de745 | 610 | if (prot & (VM_PROT_EXECUTE | VM_PROT_WRITE)) { |
2d21ac55 | 611 | prot |= VM_PROT_READ; |
0a7de745 A |
612 | } |
613 | if (maxprot & (VM_PROT_EXECUTE | VM_PROT_WRITE)) { | |
2d21ac55 | 614 | maxprot |= VM_PROT_READ; |
0a7de745 A |
615 | } |
616 | #endif /* radar 3777787 */ | |
3e170ce0 | 617 | |
6d2010ae | 618 | map_file_retry: |
3e170ce0 A |
619 | if ((flags & MAP_RESILIENT_CODESIGN) || |
620 | (flags & MAP_RESILIENT_MEDIA)) { | |
621 | if (prot & (VM_PROT_WRITE | VM_PROT_EXECUTE)) { | |
622 | assert(!mapanon); | |
623 | vnode_put(vp); | |
624 | error = EPERM; | |
625 | goto bad; | |
626 | } | |
627 | /* strictly limit access to "prot" */ | |
628 | maxprot &= prot; | |
629 | } | |
a39ff7e2 A |
630 | |
631 | vm_object_offset_t end_pos = 0; | |
632 | if (os_add_overflow(user_size, file_pos, &end_pos)) { | |
633 | vnode_put(vp); | |
634 | error = EINVAL; | |
635 | goto bad; | |
636 | } | |
637 | ||
b0d623f7 | 638 | result = vm_map_enter_mem_object_control(user_map, |
0a7de745 A |
639 | &user_addr, user_size, |
640 | 0, alloc_flags, vmk_flags, | |
641 | tag, | |
642 | control, file_pos, | |
643 | docow, prot, maxprot, | |
644 | (flags & MAP_SHARED) ? | |
645 | VM_INHERIT_SHARE : | |
646 | VM_INHERIT_DEFAULT); | |
6d2010ae A |
647 | |
648 | /* If a non-binding address was specified for this file backed | |
649 | * mapping, retry the mapping with a zero base | |
650 | * in the event the mapping operation failed due to | |
651 | * lack of space between the address and the map's maximum. | |
652 | */ | |
653 | if ((result == KERN_NO_SPACE) && ((flags & MAP_FIXED) == 0) && user_addr && (num_retries++ == 0)) { | |
39236c6e | 654 | user_addr = vm_map_page_size(user_map); |
6d2010ae A |
655 | goto map_file_retry; |
656 | } | |
1c79356b A |
657 | } |
658 | ||
b0d623f7 | 659 | if (!mapanon) { |
91447636 | 660 | (void)vnode_put(vp); |
b0d623f7 | 661 | } |
1c79356b | 662 | |
1c79356b A |
663 | switch (result) { |
664 | case KERN_SUCCESS: | |
91447636 A |
665 | *retval = user_addr + pageoff; |
666 | error = 0; | |
667 | break; | |
1c79356b A |
668 | case KERN_INVALID_ADDRESS: |
669 | case KERN_NO_SPACE: | |
91447636 A |
670 | error = ENOMEM; |
671 | break; | |
1c79356b | 672 | case KERN_PROTECTION_FAILURE: |
91447636 A |
673 | error = EACCES; |
674 | break; | |
1c79356b | 675 | default: |
91447636 A |
676 | error = EINVAL; |
677 | break; | |
1c79356b | 678 | } |
91447636 | 679 | bad: |
b0d623f7 A |
680 | if (pager != MEMORY_OBJECT_NULL) { |
681 | /* | |
682 | * Release the reference on the pager. | |
683 | * If the mapping was successful, it now holds | |
684 | * an extra reference. | |
685 | */ | |
686 | memory_object_deallocate(pager); | |
687 | } | |
0a7de745 | 688 | if (fpref) { |
91447636 | 689 | fp_drop(p, fd, fp, 0); |
0a7de745 | 690 | } |
2d21ac55 A |
691 | |
692 | KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO, SYS_mmap) | DBG_FUNC_NONE), fd, (uint32_t)(*retval), (uint32_t)user_size, error, 0); | |
0a7de745 | 693 | #ifndef CONFIG_EMBEDDED |
2d21ac55 | 694 | KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO2, SYS_mmap) | DBG_FUNC_NONE), (uint32_t)(*retval >> 32), (uint32_t)(user_size >> 32), |
0a7de745 | 695 | (uint32_t)(file_pos >> 32), (uint32_t)file_pos, 0); |
5ba3f43e | 696 | #endif |
0a7de745 | 697 | return error; |
1c79356b A |
698 | } |
699 | ||
1c79356b | 700 | int |
b0d623f7 | 701 | msync(__unused proc_t p, struct msync_args *uap, int32_t *retval) |
2d21ac55 A |
702 | { |
703 | __pthread_testcancel(1); | |
0a7de745 | 704 | return msync_nocancel(p, (struct msync_nocancel_args *)uap, retval); |
2d21ac55 A |
705 | } |
706 | ||
707 | int | |
b0d623f7 | 708 | msync_nocancel(__unused proc_t p, struct msync_nocancel_args *uap, __unused int32_t *retval) |
1c79356b | 709 | { |
91447636 A |
710 | mach_vm_offset_t addr; |
711 | mach_vm_size_t size; | |
1c79356b A |
712 | int flags; |
713 | vm_map_t user_map; | |
714 | int rv; | |
0a7de745 | 715 | vm_sync_t sync_flags = 0; |
1c79356b | 716 | |
39236c6e | 717 | user_map = current_map(); |
91447636 A |
718 | addr = (mach_vm_offset_t) uap->addr; |
719 | size = (mach_vm_size_t)uap->len; | |
0a7de745 | 720 | #ifndef CONFIG_EMBEDDED |
2d21ac55 | 721 | KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO, SYS_msync) | DBG_FUNC_NONE), (uint32_t)(addr >> 32), (uint32_t)(size >> 32), 0, 0, 0); |
5ba3f43e | 722 | #endif |
39236c6e | 723 | if (addr & vm_map_page_mask(user_map)) { |
91447636 A |
724 | /* UNIX SPEC: user address is not page-aligned, return EINVAL */ |
725 | return EINVAL; | |
726 | } | |
1c79356b A |
727 | if (size == 0) { |
728 | /* | |
729 | * We cannot support this properly without maintaining | |
730 | * list all mmaps done. Cannot use vm_map_entry as they could be | |
0a7de745 | 731 | * split or coalesced by indepenedant actions. So instead of |
1c79356b A |
732 | * inaccurate results, lets just return error as invalid size |
733 | * specified | |
734 | */ | |
0a7de745 | 735 | return EINVAL; /* XXX breaks posix apps */ |
1c79356b A |
736 | } |
737 | ||
91447636 A |
738 | flags = uap->flags; |
739 | /* disallow contradictory flags */ | |
0a7de745 A |
740 | if ((flags & (MS_SYNC | MS_ASYNC)) == (MS_SYNC | MS_ASYNC)) { |
741 | return EINVAL; | |
742 | } | |
743 | ||
744 | if (flags & MS_KILLPAGES) { | |
745 | sync_flags |= VM_SYNC_KILLPAGES; | |
746 | } | |
747 | if (flags & MS_DEACTIVATE) { | |
748 | sync_flags |= VM_SYNC_DEACTIVATE; | |
749 | } | |
750 | if (flags & MS_INVALIDATE) { | |
751 | sync_flags |= VM_SYNC_INVALIDATE; | |
752 | } | |
753 | ||
754 | if (!(flags & (MS_KILLPAGES | MS_DEACTIVATE))) { | |
755 | if (flags & MS_ASYNC) { | |
756 | sync_flags |= VM_SYNC_ASYNCHRONOUS; | |
757 | } else { | |
758 | sync_flags |= VM_SYNC_SYNCHRONOUS; | |
759 | } | |
1c79356b | 760 | } |
91447636 | 761 | |
0a7de745 | 762 | sync_flags |= VM_SYNC_CONTIGUOUS; /* complain if holes */ |
91447636 | 763 | |
91447636 | 764 | rv = mach_vm_msync(user_map, addr, size, sync_flags); |
1c79356b A |
765 | |
766 | switch (rv) { | |
767 | case KERN_SUCCESS: | |
768 | break; | |
0a7de745 A |
769 | case KERN_INVALID_ADDRESS: /* hole in region being sync'ed */ |
770 | return ENOMEM; | |
1c79356b | 771 | case KERN_FAILURE: |
0a7de745 | 772 | return EIO; |
1c79356b | 773 | default: |
0a7de745 | 774 | return EINVAL; |
1c79356b | 775 | } |
0a7de745 | 776 | return 0; |
1c79356b A |
777 | } |
778 | ||
779 | ||
55e303ae | 780 | int |
b0d623f7 | 781 | munmap(__unused proc_t p, struct munmap_args *uap, __unused int32_t *retval) |
1c79356b | 782 | { |
0a7de745 A |
783 | mach_vm_offset_t user_addr; |
784 | mach_vm_size_t user_size; | |
785 | kern_return_t result; | |
786 | vm_map_t user_map; | |
1c79356b | 787 | |
39236c6e | 788 | user_map = current_map(); |
91447636 A |
789 | user_addr = (mach_vm_offset_t) uap->addr; |
790 | user_size = (mach_vm_size_t) uap->len; | |
1c79356b | 791 | |
91447636 A |
792 | AUDIT_ARG(addr, user_addr); |
793 | AUDIT_ARG(len, user_size); | |
e5568f75 | 794 | |
39236c6e | 795 | if (user_addr & vm_map_page_mask(user_map)) { |
91447636 A |
796 | /* UNIX SPEC: user address is not page-aligned, return EINVAL */ |
797 | return EINVAL; | |
798 | } | |
1c79356b | 799 | |
0a7de745 A |
800 | if (user_addr + user_size < user_addr) { |
801 | return EINVAL; | |
802 | } | |
1c79356b | 803 | |
91447636 A |
804 | if (user_size == 0) { |
805 | /* UNIX SPEC: size is 0, return EINVAL */ | |
806 | return EINVAL; | |
807 | } | |
1c79356b | 808 | |
39236c6e | 809 | result = mach_vm_deallocate(user_map, user_addr, user_size); |
1c79356b | 810 | if (result != KERN_SUCCESS) { |
0a7de745 | 811 | return EINVAL; |
1c79356b | 812 | } |
0a7de745 | 813 | return 0; |
1c79356b A |
814 | } |
815 | ||
1c79356b | 816 | int |
b0d623f7 | 817 | mprotect(__unused proc_t p, struct mprotect_args *uap, __unused int32_t *retval) |
1c79356b | 818 | { |
39037602 | 819 | vm_prot_t prot; |
0a7de745 A |
820 | mach_vm_offset_t user_addr; |
821 | mach_vm_size_t user_size; | |
822 | kern_return_t result; | |
823 | vm_map_t user_map; | |
2d21ac55 A |
824 | #if CONFIG_MACF |
825 | int error; | |
826 | #endif | |
1c79356b | 827 | |
e5568f75 A |
828 | AUDIT_ARG(addr, uap->addr); |
829 | AUDIT_ARG(len, uap->len); | |
b0d623f7 | 830 | AUDIT_ARG(value32, uap->prot); |
91447636 | 831 | |
39236c6e | 832 | user_map = current_map(); |
91447636 A |
833 | user_addr = (mach_vm_offset_t) uap->addr; |
834 | user_size = (mach_vm_size_t) uap->len; | |
39037602 | 835 | prot = (vm_prot_t)(uap->prot & (VM_PROT_ALL | VM_PROT_TRUSTED | VM_PROT_STRIP_READ)); |
1c79356b | 836 | |
39236c6e | 837 | if (user_addr & vm_map_page_mask(user_map)) { |
91447636 A |
838 | /* UNIX SPEC: user address is not page-aligned, return EINVAL */ |
839 | return EINVAL; | |
840 | } | |
0a7de745 | 841 | |
1c79356b A |
842 | #ifdef notyet |
843 | /* Hmm .. */ | |
844 | #if defined(VM_PROT_READ_IS_EXEC) | |
0a7de745 | 845 | if (prot & VM_PROT_READ) { |
1c79356b | 846 | prot |= VM_PROT_EXECUTE; |
0a7de745 | 847 | } |
1c79356b A |
848 | #endif |
849 | #endif /* notyet */ | |
850 | ||
2d21ac55 | 851 | #if 3936456 |
0a7de745 | 852 | if (prot & (VM_PROT_EXECUTE | VM_PROT_WRITE)) { |
2d21ac55 | 853 | prot |= VM_PROT_READ; |
0a7de745 A |
854 | } |
855 | #endif /* 3936456 */ | |
2d21ac55 | 856 | |
5ba3f43e | 857 | #if defined(__arm64__) |
0a7de745 | 858 | if (prot & VM_PROT_STRIP_READ) { |
5ba3f43e | 859 | prot &= ~(VM_PROT_READ | VM_PROT_STRIP_READ); |
0a7de745 | 860 | } |
5ba3f43e | 861 | #endif |
39037602 | 862 | |
2d21ac55 A |
863 | #if CONFIG_MACF |
864 | /* | |
865 | * The MAC check for mprotect is of limited use for 2 reasons: | |
866 | * Without mmap revocation, the caller could have asked for the max | |
867 | * protections initially instead of a reduced set, so a mprotect | |
868 | * check would offer no new security. | |
869 | * It is not possible to extract the vnode from the pager object(s) | |
870 | * of the target memory range. | |
871 | * However, the MAC check may be used to prevent a process from, | |
872 | * e.g., making the stack executable. | |
873 | */ | |
874 | error = mac_proc_check_mprotect(p, user_addr, | |
0a7de745 A |
875 | user_size, prot); |
876 | if (error) { | |
877 | return error; | |
878 | } | |
2d21ac55 | 879 | #endif |
b7266188 | 880 | |
0a7de745 | 881 | if (prot & VM_PROT_TRUSTED) { |
b7266188 A |
882 | #if CONFIG_DYNAMIC_CODE_SIGNING |
883 | /* CODE SIGNING ENFORCEMENT - JIT support */ | |
884 | /* The special protection value VM_PROT_TRUSTED requests that we treat | |
885 | * this page as if it had a valid code signature. | |
0a7de745 | 886 | * If this is enabled, there MUST be a MAC policy implementing the |
b7266188 A |
887 | * mac_proc_check_mprotect() hook above. Otherwise, Codesigning will be |
888 | * compromised because the check would always succeed and thusly any | |
889 | * process could sign dynamically. */ | |
39236c6e | 890 | result = vm_map_sign( |
0a7de745 | 891 | user_map, |
39236c6e | 892 | vm_map_trunc_page(user_addr, |
0a7de745 A |
893 | vm_map_page_mask(user_map)), |
894 | vm_map_round_page(user_addr + user_size, | |
895 | vm_map_page_mask(user_map))); | |
b7266188 | 896 | switch (result) { |
0a7de745 A |
897 | case KERN_SUCCESS: |
898 | break; | |
899 | case KERN_INVALID_ADDRESS: | |
900 | /* UNIX SPEC: for an invalid address range, return ENOMEM */ | |
901 | return ENOMEM; | |
902 | default: | |
903 | return EINVAL; | |
b7266188 A |
904 | } |
905 | #else | |
906 | return ENOTSUP; | |
907 | #endif | |
908 | } | |
909 | prot &= ~VM_PROT_TRUSTED; | |
0a7de745 | 910 | |
91447636 | 911 | result = mach_vm_protect(user_map, user_addr, user_size, |
0a7de745 | 912 | FALSE, prot); |
1c79356b A |
913 | switch (result) { |
914 | case KERN_SUCCESS: | |
0a7de745 | 915 | return 0; |
1c79356b | 916 | case KERN_PROTECTION_FAILURE: |
0a7de745 | 917 | return EACCES; |
91447636 A |
918 | case KERN_INVALID_ADDRESS: |
919 | /* UNIX SPEC: for an invalid address range, return ENOMEM */ | |
920 | return ENOMEM; | |
1c79356b | 921 | } |
0a7de745 | 922 | return EINVAL; |
1c79356b A |
923 | } |
924 | ||
925 | ||
1c79356b | 926 | int |
b0d623f7 | 927 | minherit(__unused proc_t p, struct minherit_args *uap, __unused int32_t *retval) |
1c79356b | 928 | { |
91447636 A |
929 | mach_vm_offset_t addr; |
930 | mach_vm_size_t size; | |
39037602 | 931 | vm_inherit_t inherit; |
0a7de745 A |
932 | vm_map_t user_map; |
933 | kern_return_t result; | |
1c79356b | 934 | |
e5568f75 A |
935 | AUDIT_ARG(addr, uap->addr); |
936 | AUDIT_ARG(len, uap->len); | |
b0d623f7 | 937 | AUDIT_ARG(value32, uap->inherit); |
1c79356b | 938 | |
91447636 A |
939 | addr = (mach_vm_offset_t)uap->addr; |
940 | size = (mach_vm_size_t)uap->len; | |
941 | inherit = uap->inherit; | |
1c79356b A |
942 | |
943 | user_map = current_map(); | |
91447636 | 944 | result = mach_vm_inherit(user_map, addr, size, |
0a7de745 | 945 | inherit); |
1c79356b A |
946 | switch (result) { |
947 | case KERN_SUCCESS: | |
0a7de745 | 948 | return 0; |
1c79356b | 949 | case KERN_PROTECTION_FAILURE: |
0a7de745 | 950 | return EACCES; |
1c79356b | 951 | } |
0a7de745 | 952 | return EINVAL; |
1c79356b A |
953 | } |
954 | ||
1c79356b | 955 | int |
b0d623f7 | 956 | madvise(__unused proc_t p, struct madvise_args *uap, __unused int32_t *retval) |
1c79356b A |
957 | { |
958 | vm_map_t user_map; | |
91447636 A |
959 | mach_vm_offset_t start; |
960 | mach_vm_size_t size; | |
1c79356b | 961 | vm_behavior_t new_behavior; |
0a7de745 | 962 | kern_return_t result; |
1c79356b | 963 | |
1c79356b A |
964 | /* |
965 | * Since this routine is only advisory, we default to conservative | |
966 | * behavior. | |
967 | */ | |
1c79356b | 968 | switch (uap->behav) { |
0a7de745 A |
969 | case MADV_RANDOM: |
970 | new_behavior = VM_BEHAVIOR_RANDOM; | |
971 | break; | |
972 | case MADV_SEQUENTIAL: | |
973 | new_behavior = VM_BEHAVIOR_SEQUENTIAL; | |
974 | break; | |
975 | case MADV_NORMAL: | |
976 | new_behavior = VM_BEHAVIOR_DEFAULT; | |
977 | break; | |
978 | case MADV_WILLNEED: | |
979 | new_behavior = VM_BEHAVIOR_WILLNEED; | |
980 | break; | |
981 | case MADV_DONTNEED: | |
982 | new_behavior = VM_BEHAVIOR_DONTNEED; | |
983 | break; | |
984 | case MADV_FREE: | |
985 | new_behavior = VM_BEHAVIOR_FREE; | |
986 | break; | |
987 | case MADV_ZERO_WIRED_PAGES: | |
988 | new_behavior = VM_BEHAVIOR_ZERO_WIRED_PAGES; | |
989 | break; | |
990 | case MADV_FREE_REUSABLE: | |
991 | new_behavior = VM_BEHAVIOR_REUSABLE; | |
992 | break; | |
993 | case MADV_FREE_REUSE: | |
994 | new_behavior = VM_BEHAVIOR_REUSE; | |
995 | break; | |
996 | case MADV_CAN_REUSE: | |
997 | new_behavior = VM_BEHAVIOR_CAN_REUSE; | |
998 | break; | |
999 | case MADV_PAGEOUT: | |
3e170ce0 | 1000 | #if MACH_ASSERT |
0a7de745 A |
1001 | new_behavior = VM_BEHAVIOR_PAGEOUT; |
1002 | break; | |
3e170ce0 | 1003 | #else /* MACH_ASSERT */ |
0a7de745 | 1004 | return ENOTSUP; |
3e170ce0 | 1005 | #endif /* MACH_ASSERT */ |
0a7de745 A |
1006 | default: |
1007 | return EINVAL; | |
1c79356b A |
1008 | } |
1009 | ||
91447636 A |
1010 | start = (mach_vm_offset_t) uap->addr; |
1011 | size = (mach_vm_size_t) uap->len; | |
0a7de745 | 1012 | |
5ba3f43e A |
1013 | #if __arm64__ |
1014 | if (start == 0 && | |
1015 | size != 0 && | |
1016 | (uap->behav == MADV_FREE || | |
0a7de745 | 1017 | uap->behav == MADV_FREE_REUSABLE)) { |
5ba3f43e | 1018 | printf("** FOURK_COMPAT: %d[%s] " |
0a7de745 A |
1019 | "failing madvise(0x%llx,0x%llx,%s)\n", |
1020 | p->p_pid, p->p_comm, start, size, | |
1021 | ((uap->behav == MADV_FREE_REUSABLE) | |
1022 | ? "MADV_FREE_REUSABLE" | |
1023 | : "MADV_FREE")); | |
5ba3f43e | 1024 | DTRACE_VM3(fourk_compat_madvise, |
0a7de745 A |
1025 | uint64_t, start, |
1026 | uint64_t, size, | |
1027 | int, uap->behav); | |
5ba3f43e A |
1028 | return EINVAL; |
1029 | } | |
1030 | #endif /* __arm64__ */ | |
3e170ce0 | 1031 | |
91447636 A |
1032 | user_map = current_map(); |
1033 | ||
1034 | result = mach_vm_behavior_set(user_map, start, size, new_behavior); | |
1c79356b | 1035 | switch (result) { |
6d2010ae A |
1036 | case KERN_SUCCESS: |
1037 | return 0; | |
1038 | case KERN_INVALID_ADDRESS: | |
1039 | return EINVAL; | |
0a7de745 | 1040 | case KERN_NO_SPACE: |
6d2010ae | 1041 | return ENOMEM; |
1c79356b A |
1042 | } |
1043 | ||
6d2010ae | 1044 | return EINVAL; |
1c79356b A |
1045 | } |
1046 | ||
1c79356b | 1047 | int |
b0d623f7 | 1048 | mincore(__unused proc_t p, struct mincore_args *uap, __unused int32_t *retval) |
1c79356b | 1049 | { |
5ba3f43e A |
1050 | mach_vm_offset_t addr = 0, first_addr = 0, end = 0, cur_end = 0; |
1051 | vm_map_t map = VM_MAP_NULL; | |
1052 | user_addr_t vec = 0; | |
1053 | int error = 0; | |
a39ff7e2 | 1054 | int lastvecindex = 0; |
0a7de745 | 1055 | int mincoreinfo = 0; |
5ba3f43e A |
1056 | int pqueryinfo = 0; |
1057 | unsigned int pqueryinfo_vec_size = 0; | |
1058 | vm_page_info_basic_t info = NULL; | |
1059 | mach_msg_type_number_t count = 0; | |
1060 | char *kernel_vec = NULL; | |
0a7de745 | 1061 | uint64_t req_vec_size_pages = 0, cur_vec_size_pages = 0, vecindex = 0; |
5ba3f43e | 1062 | kern_return_t kr = KERN_SUCCESS; |
91447636 | 1063 | |
1c79356b A |
1064 | map = current_map(); |
1065 | ||
1066 | /* | |
1067 | * Make sure that the addresses presented are valid for user | |
1068 | * mode. | |
1069 | */ | |
39236c6e | 1070 | first_addr = addr = vm_map_trunc_page(uap->addr, |
0a7de745 | 1071 | vm_map_page_mask(map)); |
5ba3f43e | 1072 | end = vm_map_round_page(uap->addr + uap->len, |
0a7de745 | 1073 | vm_map_page_mask(map)); |
1c79356b | 1074 | |
0a7de745 A |
1075 | if (end < addr) { |
1076 | return EINVAL; | |
1077 | } | |
1c79356b | 1078 | |
0a7de745 A |
1079 | if (end == addr) { |
1080 | return 0; | |
1081 | } | |
5ba3f43e | 1082 | |
1c79356b | 1083 | /* |
5ba3f43e A |
1084 | * We are going to loop through the whole 'req_vec_size' pages |
1085 | * range in chunks of 'cur_vec_size'. | |
1c79356b | 1086 | */ |
1c79356b | 1087 | |
5ba3f43e | 1088 | req_vec_size_pages = (end - addr) >> PAGE_SHIFT; |
a39ff7e2 | 1089 | cur_vec_size_pages = MIN(req_vec_size_pages, (MAX_PAGE_RANGE_QUERY >> PAGE_SHIFT)); |
5ba3f43e | 1090 | |
527f9951 | 1091 | kernel_vec = (void*) _MALLOC(cur_vec_size_pages * sizeof(char), M_TEMP, M_WAITOK | M_ZERO); |
5ba3f43e A |
1092 | |
1093 | if (kernel_vec == NULL) { | |
0a7de745 | 1094 | return ENOMEM; |
5ba3f43e | 1095 | } |
1c79356b A |
1096 | |
1097 | /* | |
5ba3f43e | 1098 | * Address of byte vector |
1c79356b | 1099 | */ |
5ba3f43e A |
1100 | vec = uap->vec; |
1101 | ||
1102 | pqueryinfo_vec_size = cur_vec_size_pages * sizeof(struct vm_page_info_basic); | |
1103 | info = (void*) _MALLOC(pqueryinfo_vec_size, M_TEMP, M_WAITOK); | |
1104 | ||
1105 | if (info == NULL) { | |
1106 | FREE(kernel_vec, M_TEMP); | |
0a7de745 | 1107 | return ENOMEM; |
5ba3f43e A |
1108 | } |
1109 | ||
1110 | while (addr < end) { | |
5ba3f43e A |
1111 | cur_end = addr + (cur_vec_size_pages * PAGE_SIZE_64); |
1112 | ||
1113 | count = VM_PAGE_INFO_BASIC_COUNT; | |
1114 | kr = vm_map_page_range_info_internal(map, | |
0a7de745 A |
1115 | addr, |
1116 | cur_end, | |
1117 | VM_PAGE_INFO_BASIC, | |
1118 | (vm_page_info_t) info, | |
1119 | &count); | |
5ba3f43e A |
1120 | |
1121 | assert(kr == KERN_SUCCESS); | |
1c79356b A |
1122 | |
1123 | /* | |
5ba3f43e A |
1124 | * Do this on a map entry basis so that if the pages are not |
1125 | * in the current processes address space, we can easily look | |
1126 | * up the pages elsewhere. | |
1c79356b | 1127 | */ |
5ba3f43e | 1128 | lastvecindex = -1; |
0a7de745 | 1129 | for (; addr < cur_end; addr += PAGE_SIZE) { |
5ba3f43e A |
1130 | pqueryinfo = info[lastvecindex + 1].disposition; |
1131 | ||
1132 | mincoreinfo = 0; | |
1133 | ||
0a7de745 | 1134 | if (pqueryinfo & VM_PAGE_QUERY_PAGE_PRESENT) { |
5ba3f43e | 1135 | mincoreinfo |= MINCORE_INCORE; |
0a7de745 A |
1136 | } |
1137 | if (pqueryinfo & VM_PAGE_QUERY_PAGE_REF) { | |
5ba3f43e | 1138 | mincoreinfo |= MINCORE_REFERENCED; |
0a7de745 A |
1139 | } |
1140 | if (pqueryinfo & VM_PAGE_QUERY_PAGE_DIRTY) { | |
5ba3f43e | 1141 | mincoreinfo |= MINCORE_MODIFIED; |
0a7de745 A |
1142 | } |
1143 | if (pqueryinfo & VM_PAGE_QUERY_PAGE_PAGED_OUT) { | |
5ba3f43e | 1144 | mincoreinfo |= MINCORE_PAGED_OUT; |
0a7de745 A |
1145 | } |
1146 | if (pqueryinfo & VM_PAGE_QUERY_PAGE_COPIED) { | |
5ba3f43e | 1147 | mincoreinfo |= MINCORE_COPIED; |
0a7de745 A |
1148 | } |
1149 | if ((pqueryinfo & VM_PAGE_QUERY_PAGE_EXTERNAL) == 0) { | |
5ba3f43e | 1150 | mincoreinfo |= MINCORE_ANONYMOUS; |
0a7de745 | 1151 | } |
5ba3f43e A |
1152 | /* |
1153 | * calculate index into user supplied byte vector | |
1154 | */ | |
0a7de745 | 1155 | vecindex = (addr - first_addr) >> PAGE_SHIFT; |
5ba3f43e A |
1156 | kernel_vec[vecindex] = (char)mincoreinfo; |
1157 | lastvecindex = vecindex; | |
1c79356b A |
1158 | } |
1159 | ||
5ba3f43e A |
1160 | |
1161 | assert(vecindex == (cur_vec_size_pages - 1)); | |
1162 | ||
1163 | error = copyout(kernel_vec, vec, cur_vec_size_pages * sizeof(char) /* a char per page */); | |
1164 | ||
1c79356b | 1165 | if (error) { |
5ba3f43e | 1166 | break; |
1c79356b | 1167 | } |
5ba3f43e A |
1168 | |
1169 | /* | |
1170 | * For the next chunk, we'll need: | |
1171 | * - bump the location in the user buffer for our next disposition. | |
1172 | * - new length | |
1173 | * - starting address | |
1174 | */ | |
1175 | vec += cur_vec_size_pages * sizeof(char); | |
1176 | req_vec_size_pages = (end - addr) >> PAGE_SHIFT; | |
a39ff7e2 | 1177 | cur_vec_size_pages = MIN(req_vec_size_pages, (MAX_PAGE_RANGE_QUERY >> PAGE_SHIFT)); |
5ba3f43e A |
1178 | |
1179 | first_addr = addr; | |
1c79356b A |
1180 | } |
1181 | ||
5ba3f43e A |
1182 | FREE(kernel_vec, M_TEMP); |
1183 | FREE(info, M_TEMP); | |
1c79356b | 1184 | |
5ba3f43e | 1185 | if (error) { |
0a7de745 | 1186 | return EFAULT; |
1c79356b | 1187 | } |
5ba3f43e | 1188 | |
0a7de745 | 1189 | return 0; |
1c79356b A |
1190 | } |
1191 | ||
1c79356b | 1192 | int |
b0d623f7 | 1193 | mlock(__unused proc_t p, struct mlock_args *uap, __unused int32_t *retvalval) |
1c79356b A |
1194 | { |
1195 | vm_map_t user_map; | |
91447636 A |
1196 | vm_map_offset_t addr; |
1197 | vm_map_size_t size, pageoff; | |
0a7de745 | 1198 | kern_return_t result; |
1c79356b | 1199 | |
e5568f75 A |
1200 | AUDIT_ARG(addr, uap->addr); |
1201 | AUDIT_ARG(len, uap->len); | |
1c79356b | 1202 | |
91447636 A |
1203 | addr = (vm_map_offset_t) uap->addr; |
1204 | size = (vm_map_size_t)uap->len; | |
1c79356b A |
1205 | |
1206 | /* disable wrap around */ | |
0a7de745 A |
1207 | if (addr + size < addr) { |
1208 | return EINVAL; | |
1209 | } | |
91447636 | 1210 | |
0a7de745 A |
1211 | if (size == 0) { |
1212 | return 0; | |
1213 | } | |
91447636 | 1214 | |
1c79356b | 1215 | user_map = current_map(); |
39236c6e A |
1216 | pageoff = (addr & vm_map_page_mask(user_map)); |
1217 | addr -= pageoff; | |
0a7de745 | 1218 | size = vm_map_round_page(size + pageoff, vm_map_page_mask(user_map)); |
1c79356b | 1219 | |
91447636 | 1220 | /* have to call vm_map_wire directly to pass "I don't know" protections */ |
0a7de745 | 1221 | result = vm_map_wire_kernel(user_map, addr, addr + size, VM_PROT_NONE, VM_KERN_MEMORY_MLOCK, TRUE); |
2d21ac55 | 1222 | |
0a7de745 | 1223 | if (result == KERN_RESOURCE_SHORTAGE) { |
2d21ac55 | 1224 | return EAGAIN; |
0a7de745 | 1225 | } else if (result == KERN_PROTECTION_FAILURE) { |
39037602 | 1226 | return EACCES; |
0a7de745 | 1227 | } else if (result != KERN_SUCCESS) { |
2d21ac55 | 1228 | return ENOMEM; |
0a7de745 | 1229 | } |
2d21ac55 | 1230 | |
0a7de745 | 1231 | return 0; /* KERN_SUCCESS */ |
1c79356b A |
1232 | } |
1233 | ||
1c79356b | 1234 | int |
b0d623f7 | 1235 | munlock(__unused proc_t p, struct munlock_args *uap, __unused int32_t *retval) |
1c79356b | 1236 | { |
91447636 A |
1237 | mach_vm_offset_t addr; |
1238 | mach_vm_size_t size; | |
1c79356b | 1239 | vm_map_t user_map; |
0a7de745 | 1240 | kern_return_t result; |
1c79356b | 1241 | |
e5568f75 | 1242 | AUDIT_ARG(addr, uap->addr); |
91447636 | 1243 | AUDIT_ARG(addr, uap->len); |
1c79356b | 1244 | |
91447636 A |
1245 | addr = (mach_vm_offset_t) uap->addr; |
1246 | size = (mach_vm_size_t)uap->len; | |
1c79356b A |
1247 | user_map = current_map(); |
1248 | ||
91447636 | 1249 | /* JMM - need to remove all wirings by spec - this just removes one */ |
5ba3f43e | 1250 | result = mach_vm_wire_kernel(host_priv_self(), user_map, addr, size, VM_PROT_NONE, VM_KERN_MEMORY_MLOCK); |
0a7de745 | 1251 | return result == KERN_SUCCESS ? 0 : ENOMEM; |
1c79356b A |
1252 | } |
1253 | ||
1254 | ||
1c79356b | 1255 | int |
b0d623f7 | 1256 | mlockall(__unused proc_t p, __unused struct mlockall_args *uap, __unused int32_t *retval) |
1c79356b | 1257 | { |
0a7de745 | 1258 | return ENOSYS; |
1c79356b A |
1259 | } |
1260 | ||
1c79356b | 1261 | int |
b0d623f7 | 1262 | munlockall(__unused proc_t p, __unused struct munlockall_args *uap, __unused int32_t *retval) |
1c79356b | 1263 | { |
0a7de745 | 1264 | return ENOSYS; |
1c79356b A |
1265 | } |
1266 | ||
fe8ab488 A |
1267 | #if CONFIG_CODE_DECRYPTION |
1268 | int | |
1269 | mremap_encrypted(__unused struct proc *p, struct mremap_encrypted_args *uap, __unused int32_t *retval) | |
1c79356b | 1270 | { |
0a7de745 A |
1271 | mach_vm_offset_t user_addr; |
1272 | mach_vm_size_t user_size; | |
1273 | kern_return_t result; | |
1274 | vm_map_t user_map; | |
1275 | uint32_t cryptid; | |
1276 | cpu_type_t cputype; | |
1277 | cpu_subtype_t cpusubtype; | |
1278 | pager_crypt_info_t crypt_info; | |
1279 | const char * cryptname = 0; | |
1280 | char *vpath; | |
1281 | int len, ret; | |
1282 | struct proc_regioninfo_internal pinfo; | |
1283 | vnode_t vp; | |
1284 | uintptr_t vnodeaddr; | |
1285 | uint32_t vid; | |
1286 | ||
1287 | AUDIT_ARG(addr, uap->addr); | |
1288 | AUDIT_ARG(len, uap->len); | |
1289 | ||
1290 | user_map = current_map(); | |
1291 | user_addr = (mach_vm_offset_t) uap->addr; | |
1292 | user_size = (mach_vm_size_t) uap->len; | |
1293 | ||
1294 | cryptid = uap->cryptid; | |
1295 | cputype = uap->cputype; | |
1296 | cpusubtype = uap->cpusubtype; | |
1297 | ||
1298 | if (user_addr & vm_map_page_mask(user_map)) { | |
1299 | /* UNIX SPEC: user address is not page-aligned, return EINVAL */ | |
1300 | return EINVAL; | |
1301 | } | |
1302 | ||
1303 | switch (cryptid) { | |
1304 | case 0: | |
1305 | /* not encrypted, just an empty load command */ | |
1306 | return 0; | |
1307 | case 1: | |
1308 | cryptname = "com.apple.unfree"; | |
1309 | break; | |
1310 | case 0x10: | |
1311 | /* some random cryptid that you could manually put into | |
1312 | * your binary if you want NULL */ | |
1313 | cryptname = "com.apple.null"; | |
1314 | break; | |
1315 | default: | |
1316 | return EINVAL; | |
1317 | } | |
1318 | ||
1319 | if (NULL == text_crypter_create) { | |
1320 | return ENOTSUP; | |
1321 | } | |
1322 | ||
1323 | ret = fill_procregioninfo_onlymappedvnodes( proc_task(p), user_addr, &pinfo, &vnodeaddr, &vid); | |
1324 | if (ret == 0 || !vnodeaddr) { | |
1325 | /* No really, this returns 0 if the memory address is not backed by a file */ | |
1326 | return EINVAL; | |
1327 | } | |
1328 | ||
1329 | vp = (vnode_t)vnodeaddr; | |
1330 | if ((vnode_getwithvid(vp, vid)) == 0) { | |
1331 | MALLOC_ZONE(vpath, char *, MAXPATHLEN, M_NAMEI, M_WAITOK); | |
1332 | if (vpath == NULL) { | |
1333 | vnode_put(vp); | |
1334 | return ENOMEM; | |
1335 | } | |
1336 | ||
1337 | len = MAXPATHLEN; | |
1338 | ret = vn_getpath(vp, vpath, &len); | |
1339 | if (ret) { | |
1340 | FREE_ZONE(vpath, MAXPATHLEN, M_NAMEI); | |
1341 | vnode_put(vp); | |
1342 | return ret; | |
1343 | } | |
1344 | ||
1345 | vnode_put(vp); | |
1346 | } else { | |
1347 | return EINVAL; | |
1348 | } | |
fe8ab488 A |
1349 | |
1350 | #if 0 | |
0a7de745 A |
1351 | kprintf("%s vpath %s cryptid 0x%08x cputype 0x%08x cpusubtype 0x%08x range 0x%016llx size 0x%016llx\n", |
1352 | __FUNCTION__, vpath, cryptid, cputype, cpusubtype, (uint64_t)user_addr, (uint64_t)user_size); | |
fe8ab488 | 1353 | #endif |
3e170ce0 | 1354 | |
0a7de745 A |
1355 | /* set up decrypter first */ |
1356 | crypt_file_data_t crypt_data = { | |
1357 | .filename = vpath, | |
1358 | .cputype = cputype, | |
1359 | .cpusubtype = cpusubtype | |
1360 | }; | |
1361 | result = text_crypter_create(&crypt_info, cryptname, (void*)&crypt_data); | |
39037602 | 1362 | #if VM_MAP_DEBUG_APPLE_PROTECT |
0a7de745 A |
1363 | if (vm_map_debug_apple_protect) { |
1364 | printf("APPLE_PROTECT: %d[%s] map %p [0x%llx:0x%llx] %s(%s) -> 0x%x\n", | |
1365 | p->p_pid, p->p_comm, | |
1366 | user_map, | |
1367 | (uint64_t) user_addr, | |
1368 | (uint64_t) (user_addr + user_size), | |
1369 | __FUNCTION__, vpath, result); | |
1370 | } | |
39037602 | 1371 | #endif /* VM_MAP_DEBUG_APPLE_PROTECT */ |
0a7de745 A |
1372 | FREE_ZONE(vpath, MAXPATHLEN, M_NAMEI); |
1373 | ||
1374 | if (result) { | |
1375 | printf("%s: unable to create decrypter %s, kr=%d\n", | |
1376 | __FUNCTION__, cryptname, result); | |
1377 | if (result == kIOReturnNotPrivileged) { | |
1378 | /* text encryption returned decryption failure */ | |
1379 | return EPERM; | |
1380 | } else { | |
1381 | return ENOMEM; | |
1382 | } | |
1383 | } | |
1384 | ||
1385 | /* now remap using the decrypter */ | |
1386 | vm_object_offset_t crypto_backing_offset; | |
1387 | crypto_backing_offset = -1; /* i.e. use map entry's offset */ | |
1388 | result = vm_map_apple_protected(user_map, | |
1389 | user_addr, | |
1390 | user_addr + user_size, | |
1391 | crypto_backing_offset, | |
1392 | &crypt_info); | |
1393 | if (result) { | |
1394 | printf("%s: mapping failed with %d\n", __FUNCTION__, result); | |
1395 | } | |
1396 | ||
1397 | if (result) { | |
1398 | return EPERM; | |
1399 | } | |
1400 | return 0; | |
1c79356b | 1401 | } |
fe8ab488 | 1402 | #endif /* CONFIG_CODE_DECRYPTION */ |