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