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